]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpu/drm/i915/intel_display.c
drm/i915: Drop the nop intel_update_watermarks() call from haswell_crtc_enable()
[mirror_ubuntu-bionic-kernel.git] / drivers / gpu / drm / i915 / intel_display.c
CommitLineData
79e53945
JB
1/*
2 * Copyright © 2006-2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 */
26
618563e3 27#include <linux/dmi.h>
c1c7af60
JB
28#include <linux/module.h>
29#include <linux/input.h>
79e53945 30#include <linux/i2c.h>
7662c8bd 31#include <linux/kernel.h>
5a0e3ad6 32#include <linux/slab.h>
9cce37f4 33#include <linux/vgaarb.h>
e0dac65e 34#include <drm/drm_edid.h>
760285e7 35#include <drm/drmP.h>
79e53945 36#include "intel_drv.h"
5d723d7a 37#include "intel_frontbuffer.h"
760285e7 38#include <drm/i915_drm.h>
79e53945 39#include "i915_drv.h"
db18b6a6 40#include "intel_dsi.h"
e5510fac 41#include "i915_trace.h"
319c1d42 42#include <drm/drm_atomic.h>
c196e1d6 43#include <drm/drm_atomic_helper.h>
760285e7
DH
44#include <drm/drm_dp_helper.h>
45#include <drm/drm_crtc_helper.h>
465c120c
MR
46#include <drm/drm_plane_helper.h>
47#include <drm/drm_rect.h>
c0f372b3 48#include <linux/dma_remapping.h>
fd8e058a 49#include <linux/reservation.h>
79e53945 50
5a21b665
DV
51static bool is_mmio_work(struct intel_flip_work *work)
52{
53 return work->mmio_work.func;
54}
55
465c120c 56/* Primary plane formats for gen <= 3 */
568db4f2 57static const uint32_t i8xx_primary_formats[] = {
67fe7dc5
DL
58 DRM_FORMAT_C8,
59 DRM_FORMAT_RGB565,
465c120c 60 DRM_FORMAT_XRGB1555,
67fe7dc5 61 DRM_FORMAT_XRGB8888,
465c120c
MR
62};
63
64/* Primary plane formats for gen >= 4 */
568db4f2 65static const uint32_t i965_primary_formats[] = {
6c0fd451
DL
66 DRM_FORMAT_C8,
67 DRM_FORMAT_RGB565,
68 DRM_FORMAT_XRGB8888,
69 DRM_FORMAT_XBGR8888,
70 DRM_FORMAT_XRGB2101010,
71 DRM_FORMAT_XBGR2101010,
72};
73
74static const uint32_t skl_primary_formats[] = {
67fe7dc5
DL
75 DRM_FORMAT_C8,
76 DRM_FORMAT_RGB565,
77 DRM_FORMAT_XRGB8888,
465c120c 78 DRM_FORMAT_XBGR8888,
67fe7dc5 79 DRM_FORMAT_ARGB8888,
465c120c
MR
80 DRM_FORMAT_ABGR8888,
81 DRM_FORMAT_XRGB2101010,
465c120c 82 DRM_FORMAT_XBGR2101010,
ea916ea0
KM
83 DRM_FORMAT_YUYV,
84 DRM_FORMAT_YVYU,
85 DRM_FORMAT_UYVY,
86 DRM_FORMAT_VYUY,
465c120c
MR
87};
88
3d7d6510
MR
89/* Cursor formats */
90static const uint32_t intel_cursor_formats[] = {
91 DRM_FORMAT_ARGB8888,
92};
93
f1f644dc 94static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 95 struct intel_crtc_state *pipe_config);
18442d08 96static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 97 struct intel_crtc_state *pipe_config);
f1f644dc 98
eb1bfe80
JB
99static int intel_framebuffer_init(struct drm_device *dev,
100 struct intel_framebuffer *ifb,
101 struct drm_mode_fb_cmd2 *mode_cmd,
102 struct drm_i915_gem_object *obj);
5b18e57c
DV
103static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
104static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
bc58be60 105static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
29407aab 106static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
107 struct intel_link_m_n *m_n,
108 struct intel_link_m_n *m2_n2);
29407aab 109static void ironlake_set_pipeconf(struct drm_crtc *crtc);
229fca97 110static void haswell_set_pipeconf(struct drm_crtc *crtc);
391bf048 111static void haswell_set_pipemisc(struct drm_crtc *crtc);
d288f65f 112static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 113 const struct intel_crtc_state *pipe_config);
d288f65f 114static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 115 const struct intel_crtc_state *pipe_config);
5a21b665
DV
116static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
117static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
65edccce
VS
118static void skl_init_scalers(struct drm_i915_private *dev_priv,
119 struct intel_crtc *crtc,
120 struct intel_crtc_state *crtc_state);
bfd16b2a
ML
121static void skylake_pfit_enable(struct intel_crtc *crtc);
122static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
123static void ironlake_pfit_enable(struct intel_crtc *crtc);
043e9bda 124static void intel_modeset_setup_hw_state(struct drm_device *dev);
2622a081 125static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
4e5ca60f 126static int ilk_max_pixel_rate(struct drm_atomic_state *state);
89b3c3c7 127static int glk_calc_cdclk(int max_pixclk);
324513c0 128static int bxt_calc_cdclk(int max_pixclk);
e7457a9a 129
d4906093 130struct intel_limit {
4c5def93
ACO
131 struct {
132 int min, max;
133 } dot, vco, n, m, m1, m2, p, p1;
134
135 struct {
136 int dot_limit;
137 int p2_slow, p2_fast;
138 } p2;
d4906093 139};
79e53945 140
bfa7df01
VS
141/* returns HPLL frequency in kHz */
142static int valleyview_get_vco(struct drm_i915_private *dev_priv)
143{
144 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
145
146 /* Obtain SKU information */
147 mutex_lock(&dev_priv->sb_lock);
148 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
149 CCK_FUSE_HPLL_FREQ_MASK;
150 mutex_unlock(&dev_priv->sb_lock);
151
152 return vco_freq[hpll_freq] * 1000;
153}
154
c30fec65
VS
155int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
156 const char *name, u32 reg, int ref_freq)
bfa7df01
VS
157{
158 u32 val;
159 int divider;
160
bfa7df01
VS
161 mutex_lock(&dev_priv->sb_lock);
162 val = vlv_cck_read(dev_priv, reg);
163 mutex_unlock(&dev_priv->sb_lock);
164
165 divider = val & CCK_FREQUENCY_VALUES;
166
167 WARN((val & CCK_FREQUENCY_STATUS) !=
168 (divider << CCK_FREQUENCY_STATUS_SHIFT),
169 "%s change in progress\n", name);
170
c30fec65
VS
171 return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
172}
173
174static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
175 const char *name, u32 reg)
176{
177 if (dev_priv->hpll_freq == 0)
178 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
179
180 return vlv_get_cck_clock(dev_priv, name, reg,
181 dev_priv->hpll_freq);
bfa7df01
VS
182}
183
e7dc33f3
VS
184static int
185intel_pch_rawclk(struct drm_i915_private *dev_priv)
d2acd215 186{
e7dc33f3
VS
187 return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
188}
d2acd215 189
e7dc33f3
VS
190static int
191intel_vlv_hrawclk(struct drm_i915_private *dev_priv)
192{
19ab4ed3 193 /* RAWCLK_FREQ_VLV register updated from power well code */
35d38d1f
VS
194 return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
195 CCK_DISPLAY_REF_CLOCK_CONTROL);
d2acd215
DV
196}
197
e7dc33f3
VS
198static int
199intel_g4x_hrawclk(struct drm_i915_private *dev_priv)
79e50a4f 200{
79e50a4f
JN
201 uint32_t clkcfg;
202
e7dc33f3 203 /* hrawclock is 1/4 the FSB frequency */
79e50a4f
JN
204 clkcfg = I915_READ(CLKCFG);
205 switch (clkcfg & CLKCFG_FSB_MASK) {
206 case CLKCFG_FSB_400:
e7dc33f3 207 return 100000;
79e50a4f 208 case CLKCFG_FSB_533:
e7dc33f3 209 return 133333;
79e50a4f 210 case CLKCFG_FSB_667:
e7dc33f3 211 return 166667;
79e50a4f 212 case CLKCFG_FSB_800:
e7dc33f3 213 return 200000;
79e50a4f 214 case CLKCFG_FSB_1067:
e7dc33f3 215 return 266667;
79e50a4f 216 case CLKCFG_FSB_1333:
e7dc33f3 217 return 333333;
79e50a4f
JN
218 /* these two are just a guess; one of them might be right */
219 case CLKCFG_FSB_1600:
220 case CLKCFG_FSB_1600_ALT:
e7dc33f3 221 return 400000;
79e50a4f 222 default:
e7dc33f3 223 return 133333;
79e50a4f
JN
224 }
225}
226
19ab4ed3 227void intel_update_rawclk(struct drm_i915_private *dev_priv)
e7dc33f3
VS
228{
229 if (HAS_PCH_SPLIT(dev_priv))
230 dev_priv->rawclk_freq = intel_pch_rawclk(dev_priv);
231 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
232 dev_priv->rawclk_freq = intel_vlv_hrawclk(dev_priv);
233 else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
234 dev_priv->rawclk_freq = intel_g4x_hrawclk(dev_priv);
235 else
236 return; /* no rawclk on other platforms, or no need to know it */
237
238 DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
239}
240
bfa7df01
VS
241static void intel_update_czclk(struct drm_i915_private *dev_priv)
242{
666a4537 243 if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
bfa7df01
VS
244 return;
245
246 dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
247 CCK_CZ_CLOCK_CONTROL);
248
249 DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
250}
251
021357ac 252static inline u32 /* units of 100MHz */
21a727b3
VS
253intel_fdi_link_freq(struct drm_i915_private *dev_priv,
254 const struct intel_crtc_state *pipe_config)
021357ac 255{
21a727b3
VS
256 if (HAS_DDI(dev_priv))
257 return pipe_config->port_clock; /* SPLL */
258 else if (IS_GEN5(dev_priv))
259 return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
e3b247da 260 else
21a727b3 261 return 270000;
021357ac
CW
262}
263
1b6f4958 264static const struct intel_limit intel_limits_i8xx_dac = {
0206e353 265 .dot = { .min = 25000, .max = 350000 },
9c333719 266 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 267 .n = { .min = 2, .max = 16 },
0206e353
AJ
268 .m = { .min = 96, .max = 140 },
269 .m1 = { .min = 18, .max = 26 },
270 .m2 = { .min = 6, .max = 16 },
271 .p = { .min = 4, .max = 128 },
272 .p1 = { .min = 2, .max = 33 },
273e27ca
EA
273 .p2 = { .dot_limit = 165000,
274 .p2_slow = 4, .p2_fast = 2 },
e4b36699
KP
275};
276
1b6f4958 277static const struct intel_limit intel_limits_i8xx_dvo = {
5d536e28 278 .dot = { .min = 25000, .max = 350000 },
9c333719 279 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 280 .n = { .min = 2, .max = 16 },
5d536e28
DV
281 .m = { .min = 96, .max = 140 },
282 .m1 = { .min = 18, .max = 26 },
283 .m2 = { .min = 6, .max = 16 },
284 .p = { .min = 4, .max = 128 },
285 .p1 = { .min = 2, .max = 33 },
286 .p2 = { .dot_limit = 165000,
287 .p2_slow = 4, .p2_fast = 4 },
288};
289
1b6f4958 290static const struct intel_limit intel_limits_i8xx_lvds = {
0206e353 291 .dot = { .min = 25000, .max = 350000 },
9c333719 292 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 293 .n = { .min = 2, .max = 16 },
0206e353
AJ
294 .m = { .min = 96, .max = 140 },
295 .m1 = { .min = 18, .max = 26 },
296 .m2 = { .min = 6, .max = 16 },
297 .p = { .min = 4, .max = 128 },
298 .p1 = { .min = 1, .max = 6 },
273e27ca
EA
299 .p2 = { .dot_limit = 165000,
300 .p2_slow = 14, .p2_fast = 7 },
e4b36699 301};
273e27ca 302
1b6f4958 303static const struct intel_limit intel_limits_i9xx_sdvo = {
0206e353
AJ
304 .dot = { .min = 20000, .max = 400000 },
305 .vco = { .min = 1400000, .max = 2800000 },
306 .n = { .min = 1, .max = 6 },
307 .m = { .min = 70, .max = 120 },
4f7dfb67
PJ
308 .m1 = { .min = 8, .max = 18 },
309 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
310 .p = { .min = 5, .max = 80 },
311 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
312 .p2 = { .dot_limit = 200000,
313 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
314};
315
1b6f4958 316static const struct intel_limit intel_limits_i9xx_lvds = {
0206e353
AJ
317 .dot = { .min = 20000, .max = 400000 },
318 .vco = { .min = 1400000, .max = 2800000 },
319 .n = { .min = 1, .max = 6 },
320 .m = { .min = 70, .max = 120 },
53a7d2d1
PJ
321 .m1 = { .min = 8, .max = 18 },
322 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
323 .p = { .min = 7, .max = 98 },
324 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
325 .p2 = { .dot_limit = 112000,
326 .p2_slow = 14, .p2_fast = 7 },
e4b36699
KP
327};
328
273e27ca 329
1b6f4958 330static const struct intel_limit intel_limits_g4x_sdvo = {
273e27ca
EA
331 .dot = { .min = 25000, .max = 270000 },
332 .vco = { .min = 1750000, .max = 3500000},
333 .n = { .min = 1, .max = 4 },
334 .m = { .min = 104, .max = 138 },
335 .m1 = { .min = 17, .max = 23 },
336 .m2 = { .min = 5, .max = 11 },
337 .p = { .min = 10, .max = 30 },
338 .p1 = { .min = 1, .max = 3},
339 .p2 = { .dot_limit = 270000,
340 .p2_slow = 10,
341 .p2_fast = 10
044c7c41 342 },
e4b36699
KP
343};
344
1b6f4958 345static const struct intel_limit intel_limits_g4x_hdmi = {
273e27ca
EA
346 .dot = { .min = 22000, .max = 400000 },
347 .vco = { .min = 1750000, .max = 3500000},
348 .n = { .min = 1, .max = 4 },
349 .m = { .min = 104, .max = 138 },
350 .m1 = { .min = 16, .max = 23 },
351 .m2 = { .min = 5, .max = 11 },
352 .p = { .min = 5, .max = 80 },
353 .p1 = { .min = 1, .max = 8},
354 .p2 = { .dot_limit = 165000,
355 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
356};
357
1b6f4958 358static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
273e27ca
EA
359 .dot = { .min = 20000, .max = 115000 },
360 .vco = { .min = 1750000, .max = 3500000 },
361 .n = { .min = 1, .max = 3 },
362 .m = { .min = 104, .max = 138 },
363 .m1 = { .min = 17, .max = 23 },
364 .m2 = { .min = 5, .max = 11 },
365 .p = { .min = 28, .max = 112 },
366 .p1 = { .min = 2, .max = 8 },
367 .p2 = { .dot_limit = 0,
368 .p2_slow = 14, .p2_fast = 14
044c7c41 369 },
e4b36699
KP
370};
371
1b6f4958 372static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
273e27ca
EA
373 .dot = { .min = 80000, .max = 224000 },
374 .vco = { .min = 1750000, .max = 3500000 },
375 .n = { .min = 1, .max = 3 },
376 .m = { .min = 104, .max = 138 },
377 .m1 = { .min = 17, .max = 23 },
378 .m2 = { .min = 5, .max = 11 },
379 .p = { .min = 14, .max = 42 },
380 .p1 = { .min = 2, .max = 6 },
381 .p2 = { .dot_limit = 0,
382 .p2_slow = 7, .p2_fast = 7
044c7c41 383 },
e4b36699
KP
384};
385
1b6f4958 386static const struct intel_limit intel_limits_pineview_sdvo = {
0206e353
AJ
387 .dot = { .min = 20000, .max = 400000},
388 .vco = { .min = 1700000, .max = 3500000 },
273e27ca 389 /* Pineview's Ncounter is a ring counter */
0206e353
AJ
390 .n = { .min = 3, .max = 6 },
391 .m = { .min = 2, .max = 256 },
273e27ca 392 /* Pineview only has one combined m divider, which we treat as m2. */
0206e353
AJ
393 .m1 = { .min = 0, .max = 0 },
394 .m2 = { .min = 0, .max = 254 },
395 .p = { .min = 5, .max = 80 },
396 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
397 .p2 = { .dot_limit = 200000,
398 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
399};
400
1b6f4958 401static const struct intel_limit intel_limits_pineview_lvds = {
0206e353
AJ
402 .dot = { .min = 20000, .max = 400000 },
403 .vco = { .min = 1700000, .max = 3500000 },
404 .n = { .min = 3, .max = 6 },
405 .m = { .min = 2, .max = 256 },
406 .m1 = { .min = 0, .max = 0 },
407 .m2 = { .min = 0, .max = 254 },
408 .p = { .min = 7, .max = 112 },
409 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
410 .p2 = { .dot_limit = 112000,
411 .p2_slow = 14, .p2_fast = 14 },
e4b36699
KP
412};
413
273e27ca
EA
414/* Ironlake / Sandybridge
415 *
416 * We calculate clock using (register_value + 2) for N/M1/M2, so here
417 * the range value for them is (actual_value - 2).
418 */
1b6f4958 419static const struct intel_limit intel_limits_ironlake_dac = {
273e27ca
EA
420 .dot = { .min = 25000, .max = 350000 },
421 .vco = { .min = 1760000, .max = 3510000 },
422 .n = { .min = 1, .max = 5 },
423 .m = { .min = 79, .max = 127 },
424 .m1 = { .min = 12, .max = 22 },
425 .m2 = { .min = 5, .max = 9 },
426 .p = { .min = 5, .max = 80 },
427 .p1 = { .min = 1, .max = 8 },
428 .p2 = { .dot_limit = 225000,
429 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
430};
431
1b6f4958 432static const struct intel_limit intel_limits_ironlake_single_lvds = {
273e27ca
EA
433 .dot = { .min = 25000, .max = 350000 },
434 .vco = { .min = 1760000, .max = 3510000 },
435 .n = { .min = 1, .max = 3 },
436 .m = { .min = 79, .max = 118 },
437 .m1 = { .min = 12, .max = 22 },
438 .m2 = { .min = 5, .max = 9 },
439 .p = { .min = 28, .max = 112 },
440 .p1 = { .min = 2, .max = 8 },
441 .p2 = { .dot_limit = 225000,
442 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
443};
444
1b6f4958 445static const struct intel_limit intel_limits_ironlake_dual_lvds = {
273e27ca
EA
446 .dot = { .min = 25000, .max = 350000 },
447 .vco = { .min = 1760000, .max = 3510000 },
448 .n = { .min = 1, .max = 3 },
449 .m = { .min = 79, .max = 127 },
450 .m1 = { .min = 12, .max = 22 },
451 .m2 = { .min = 5, .max = 9 },
452 .p = { .min = 14, .max = 56 },
453 .p1 = { .min = 2, .max = 8 },
454 .p2 = { .dot_limit = 225000,
455 .p2_slow = 7, .p2_fast = 7 },
b91ad0ec
ZW
456};
457
273e27ca 458/* LVDS 100mhz refclk limits. */
1b6f4958 459static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
273e27ca
EA
460 .dot = { .min = 25000, .max = 350000 },
461 .vco = { .min = 1760000, .max = 3510000 },
462 .n = { .min = 1, .max = 2 },
463 .m = { .min = 79, .max = 126 },
464 .m1 = { .min = 12, .max = 22 },
465 .m2 = { .min = 5, .max = 9 },
466 .p = { .min = 28, .max = 112 },
0206e353 467 .p1 = { .min = 2, .max = 8 },
273e27ca
EA
468 .p2 = { .dot_limit = 225000,
469 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
470};
471
1b6f4958 472static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
273e27ca
EA
473 .dot = { .min = 25000, .max = 350000 },
474 .vco = { .min = 1760000, .max = 3510000 },
475 .n = { .min = 1, .max = 3 },
476 .m = { .min = 79, .max = 126 },
477 .m1 = { .min = 12, .max = 22 },
478 .m2 = { .min = 5, .max = 9 },
479 .p = { .min = 14, .max = 42 },
0206e353 480 .p1 = { .min = 2, .max = 6 },
273e27ca
EA
481 .p2 = { .dot_limit = 225000,
482 .p2_slow = 7, .p2_fast = 7 },
4547668a
ZY
483};
484
1b6f4958 485static const struct intel_limit intel_limits_vlv = {
f01b7962
VS
486 /*
487 * These are the data rate limits (measured in fast clocks)
488 * since those are the strictest limits we have. The fast
489 * clock and actual rate limits are more relaxed, so checking
490 * them would make no difference.
491 */
492 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
75e53986 493 .vco = { .min = 4000000, .max = 6000000 },
a0c4da24 494 .n = { .min = 1, .max = 7 },
a0c4da24
JB
495 .m1 = { .min = 2, .max = 3 },
496 .m2 = { .min = 11, .max = 156 },
b99ab663 497 .p1 = { .min = 2, .max = 3 },
5fdc9c49 498 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
a0c4da24
JB
499};
500
1b6f4958 501static const struct intel_limit intel_limits_chv = {
ef9348c8
CML
502 /*
503 * These are the data rate limits (measured in fast clocks)
504 * since those are the strictest limits we have. The fast
505 * clock and actual rate limits are more relaxed, so checking
506 * them would make no difference.
507 */
508 .dot = { .min = 25000 * 5, .max = 540000 * 5},
17fe1021 509 .vco = { .min = 4800000, .max = 6480000 },
ef9348c8
CML
510 .n = { .min = 1, .max = 1 },
511 .m1 = { .min = 2, .max = 2 },
512 .m2 = { .min = 24 << 22, .max = 175 << 22 },
513 .p1 = { .min = 2, .max = 4 },
514 .p2 = { .p2_slow = 1, .p2_fast = 14 },
515};
516
1b6f4958 517static const struct intel_limit intel_limits_bxt = {
5ab7b0b7
ID
518 /* FIXME: find real dot limits */
519 .dot = { .min = 0, .max = INT_MAX },
e6292556 520 .vco = { .min = 4800000, .max = 6700000 },
5ab7b0b7
ID
521 .n = { .min = 1, .max = 1 },
522 .m1 = { .min = 2, .max = 2 },
523 /* FIXME: find real m2 limits */
524 .m2 = { .min = 2 << 22, .max = 255 << 22 },
525 .p1 = { .min = 2, .max = 4 },
526 .p2 = { .p2_slow = 1, .p2_fast = 20 },
527};
528
cdba954e
ACO
529static bool
530needs_modeset(struct drm_crtc_state *state)
531{
fc596660 532 return drm_atomic_crtc_needs_modeset(state);
cdba954e
ACO
533}
534
dccbea3b
ID
535/*
536 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
537 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
538 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
539 * The helpers' return value is the rate of the clock that is fed to the
540 * display engine's pipe which can be the above fast dot clock rate or a
541 * divided-down version of it.
542 */
f2b115e6 543/* m1 is reserved as 0 in Pineview, n is a ring counter */
9e2c8475 544static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
79e53945 545{
2177832f
SL
546 clock->m = clock->m2 + 2;
547 clock->p = clock->p1 * clock->p2;
ed5ca77e 548 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 549 return 0;
fb03ac01
VS
550 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
551 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
552
553 return clock->dot;
2177832f
SL
554}
555
7429e9d4
DV
556static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
557{
558 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
559}
560
9e2c8475 561static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
2177832f 562{
7429e9d4 563 clock->m = i9xx_dpll_compute_m(clock);
79e53945 564 clock->p = clock->p1 * clock->p2;
ed5ca77e 565 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
dccbea3b 566 return 0;
fb03ac01
VS
567 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
568 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
569
570 return clock->dot;
79e53945
JB
571}
572
9e2c8475 573static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
589eca67
ID
574{
575 clock->m = clock->m1 * clock->m2;
576 clock->p = clock->p1 * clock->p2;
577 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 578 return 0;
589eca67
ID
579 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
580 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
581
582 return clock->dot / 5;
589eca67
ID
583}
584
9e2c8475 585int chv_calc_dpll_params(int refclk, struct dpll *clock)
ef9348c8
CML
586{
587 clock->m = clock->m1 * clock->m2;
588 clock->p = clock->p1 * clock->p2;
589 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 590 return 0;
ef9348c8
CML
591 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
592 clock->n << 22);
593 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
594
595 return clock->dot / 5;
ef9348c8
CML
596}
597
7c04d1d9 598#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
79e53945
JB
599/**
600 * Returns whether the given set of divisors are valid for a given refclk with
601 * the given connectors.
602 */
603
e2d214ae 604static bool intel_PLL_is_valid(struct drm_i915_private *dev_priv,
1b6f4958 605 const struct intel_limit *limit,
9e2c8475 606 const struct dpll *clock)
79e53945 607{
f01b7962
VS
608 if (clock->n < limit->n.min || limit->n.max < clock->n)
609 INTELPllInvalid("n out of range\n");
79e53945 610 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
0206e353 611 INTELPllInvalid("p1 out of range\n");
79e53945 612 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
0206e353 613 INTELPllInvalid("m2 out of range\n");
79e53945 614 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
0206e353 615 INTELPllInvalid("m1 out of range\n");
f01b7962 616
e2d214ae 617 if (!IS_PINEVIEW(dev_priv) && !IS_VALLEYVIEW(dev_priv) &&
cc3f90f0 618 !IS_CHERRYVIEW(dev_priv) && !IS_GEN9_LP(dev_priv))
f01b7962
VS
619 if (clock->m1 <= clock->m2)
620 INTELPllInvalid("m1 <= m2\n");
621
e2d214ae 622 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
cc3f90f0 623 !IS_GEN9_LP(dev_priv)) {
f01b7962
VS
624 if (clock->p < limit->p.min || limit->p.max < clock->p)
625 INTELPllInvalid("p out of range\n");
626 if (clock->m < limit->m.min || limit->m.max < clock->m)
627 INTELPllInvalid("m out of range\n");
628 }
629
79e53945 630 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
0206e353 631 INTELPllInvalid("vco out of range\n");
79e53945
JB
632 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
633 * connector, etc., rather than just a single range.
634 */
635 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
0206e353 636 INTELPllInvalid("dot out of range\n");
79e53945
JB
637
638 return true;
639}
640
3b1429d9 641static int
1b6f4958 642i9xx_select_p2_div(const struct intel_limit *limit,
3b1429d9
VS
643 const struct intel_crtc_state *crtc_state,
644 int target)
79e53945 645{
3b1429d9 646 struct drm_device *dev = crtc_state->base.crtc->dev;
79e53945 647
2d84d2b3 648 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
79e53945 649 /*
a210b028
DV
650 * For LVDS just rely on its current settings for dual-channel.
651 * We haven't figured out how to reliably set up different
652 * single/dual channel state, if we even can.
79e53945 653 */
1974cad0 654 if (intel_is_dual_link_lvds(dev))
3b1429d9 655 return limit->p2.p2_fast;
79e53945 656 else
3b1429d9 657 return limit->p2.p2_slow;
79e53945
JB
658 } else {
659 if (target < limit->p2.dot_limit)
3b1429d9 660 return limit->p2.p2_slow;
79e53945 661 else
3b1429d9 662 return limit->p2.p2_fast;
79e53945 663 }
3b1429d9
VS
664}
665
70e8aa21
ACO
666/*
667 * Returns a set of divisors for the desired target clock with the given
668 * refclk, or FALSE. The returned values represent the clock equation:
669 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
670 *
671 * Target and reference clocks are specified in kHz.
672 *
673 * If match_clock is provided, then best_clock P divider must match the P
674 * divider from @match_clock used for LVDS downclocking.
675 */
3b1429d9 676static bool
1b6f4958 677i9xx_find_best_dpll(const struct intel_limit *limit,
3b1429d9 678 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
679 int target, int refclk, struct dpll *match_clock,
680 struct dpll *best_clock)
3b1429d9
VS
681{
682 struct drm_device *dev = crtc_state->base.crtc->dev;
9e2c8475 683 struct dpll clock;
3b1429d9 684 int err = target;
79e53945 685
0206e353 686 memset(best_clock, 0, sizeof(*best_clock));
79e53945 687
3b1429d9
VS
688 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
689
42158660
ZY
690 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
691 clock.m1++) {
692 for (clock.m2 = limit->m2.min;
693 clock.m2 <= limit->m2.max; clock.m2++) {
c0efc387 694 if (clock.m2 >= clock.m1)
42158660
ZY
695 break;
696 for (clock.n = limit->n.min;
697 clock.n <= limit->n.max; clock.n++) {
698 for (clock.p1 = limit->p1.min;
699 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
700 int this_err;
701
dccbea3b 702 i9xx_calc_dpll_params(refclk, &clock);
e2d214ae
TU
703 if (!intel_PLL_is_valid(to_i915(dev),
704 limit,
ac58c3f0
DV
705 &clock))
706 continue;
707 if (match_clock &&
708 clock.p != match_clock->p)
709 continue;
710
711 this_err = abs(clock.dot - target);
712 if (this_err < err) {
713 *best_clock = clock;
714 err = this_err;
715 }
716 }
717 }
718 }
719 }
720
721 return (err != target);
722}
723
70e8aa21
ACO
724/*
725 * Returns a set of divisors for the desired target clock with the given
726 * refclk, or FALSE. The returned values represent the clock equation:
727 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
728 *
729 * Target and reference clocks are specified in kHz.
730 *
731 * If match_clock is provided, then best_clock P divider must match the P
732 * divider from @match_clock used for LVDS downclocking.
733 */
ac58c3f0 734static bool
1b6f4958 735pnv_find_best_dpll(const struct intel_limit *limit,
a93e255f 736 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
737 int target, int refclk, struct dpll *match_clock,
738 struct dpll *best_clock)
79e53945 739{
3b1429d9 740 struct drm_device *dev = crtc_state->base.crtc->dev;
9e2c8475 741 struct dpll clock;
79e53945
JB
742 int err = target;
743
0206e353 744 memset(best_clock, 0, sizeof(*best_clock));
79e53945 745
3b1429d9
VS
746 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
747
42158660
ZY
748 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
749 clock.m1++) {
750 for (clock.m2 = limit->m2.min;
751 clock.m2 <= limit->m2.max; clock.m2++) {
42158660
ZY
752 for (clock.n = limit->n.min;
753 clock.n <= limit->n.max; clock.n++) {
754 for (clock.p1 = limit->p1.min;
755 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
756 int this_err;
757
dccbea3b 758 pnv_calc_dpll_params(refclk, &clock);
e2d214ae
TU
759 if (!intel_PLL_is_valid(to_i915(dev),
760 limit,
1b894b59 761 &clock))
79e53945 762 continue;
cec2f356
SP
763 if (match_clock &&
764 clock.p != match_clock->p)
765 continue;
79e53945
JB
766
767 this_err = abs(clock.dot - target);
768 if (this_err < err) {
769 *best_clock = clock;
770 err = this_err;
771 }
772 }
773 }
774 }
775 }
776
777 return (err != target);
778}
779
997c030c
ACO
780/*
781 * Returns a set of divisors for the desired target clock with the given
782 * refclk, or FALSE. The returned values represent the clock equation:
783 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
70e8aa21
ACO
784 *
785 * Target and reference clocks are specified in kHz.
786 *
787 * If match_clock is provided, then best_clock P divider must match the P
788 * divider from @match_clock used for LVDS downclocking.
997c030c 789 */
d4906093 790static bool
1b6f4958 791g4x_find_best_dpll(const struct intel_limit *limit,
a93e255f 792 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
793 int target, int refclk, struct dpll *match_clock,
794 struct dpll *best_clock)
d4906093 795{
3b1429d9 796 struct drm_device *dev = crtc_state->base.crtc->dev;
9e2c8475 797 struct dpll clock;
d4906093 798 int max_n;
3b1429d9 799 bool found = false;
6ba770dc
AJ
800 /* approximately equals target * 0.00585 */
801 int err_most = (target >> 8) + (target >> 9);
d4906093
ML
802
803 memset(best_clock, 0, sizeof(*best_clock));
3b1429d9
VS
804
805 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
806
d4906093 807 max_n = limit->n.max;
f77f13e2 808 /* based on hardware requirement, prefer smaller n to precision */
d4906093 809 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
f77f13e2 810 /* based on hardware requirement, prefere larger m1,m2 */
d4906093
ML
811 for (clock.m1 = limit->m1.max;
812 clock.m1 >= limit->m1.min; clock.m1--) {
813 for (clock.m2 = limit->m2.max;
814 clock.m2 >= limit->m2.min; clock.m2--) {
815 for (clock.p1 = limit->p1.max;
816 clock.p1 >= limit->p1.min; clock.p1--) {
817 int this_err;
818
dccbea3b 819 i9xx_calc_dpll_params(refclk, &clock);
e2d214ae
TU
820 if (!intel_PLL_is_valid(to_i915(dev),
821 limit,
1b894b59 822 &clock))
d4906093 823 continue;
1b894b59
CW
824
825 this_err = abs(clock.dot - target);
d4906093
ML
826 if (this_err < err_most) {
827 *best_clock = clock;
828 err_most = this_err;
829 max_n = clock.n;
830 found = true;
831 }
832 }
833 }
834 }
835 }
2c07245f
ZW
836 return found;
837}
838
d5dd62bd
ID
839/*
840 * Check if the calculated PLL configuration is more optimal compared to the
841 * best configuration and error found so far. Return the calculated error.
842 */
843static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
9e2c8475
ACO
844 const struct dpll *calculated_clock,
845 const struct dpll *best_clock,
d5dd62bd
ID
846 unsigned int best_error_ppm,
847 unsigned int *error_ppm)
848{
9ca3ba01
ID
849 /*
850 * For CHV ignore the error and consider only the P value.
851 * Prefer a bigger P value based on HW requirements.
852 */
920a14b2 853 if (IS_CHERRYVIEW(to_i915(dev))) {
9ca3ba01
ID
854 *error_ppm = 0;
855
856 return calculated_clock->p > best_clock->p;
857 }
858
24be4e46
ID
859 if (WARN_ON_ONCE(!target_freq))
860 return false;
861
d5dd62bd
ID
862 *error_ppm = div_u64(1000000ULL *
863 abs(target_freq - calculated_clock->dot),
864 target_freq);
865 /*
866 * Prefer a better P value over a better (smaller) error if the error
867 * is small. Ensure this preference for future configurations too by
868 * setting the error to 0.
869 */
870 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
871 *error_ppm = 0;
872
873 return true;
874 }
875
876 return *error_ppm + 10 < best_error_ppm;
877}
878
65b3d6a9
ACO
879/*
880 * Returns a set of divisors for the desired target clock with the given
881 * refclk, or FALSE. The returned values represent the clock equation:
882 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
883 */
a0c4da24 884static bool
1b6f4958 885vlv_find_best_dpll(const struct intel_limit *limit,
a93e255f 886 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
887 int target, int refclk, struct dpll *match_clock,
888 struct dpll *best_clock)
a0c4da24 889{
a93e255f 890 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 891 struct drm_device *dev = crtc->base.dev;
9e2c8475 892 struct dpll clock;
69e4f900 893 unsigned int bestppm = 1000000;
27e639bf
VS
894 /* min update 19.2 MHz */
895 int max_n = min(limit->n.max, refclk / 19200);
49e497ef 896 bool found = false;
a0c4da24 897
6b4bf1c4
VS
898 target *= 5; /* fast clock */
899
900 memset(best_clock, 0, sizeof(*best_clock));
a0c4da24
JB
901
902 /* based on hardware requirement, prefer smaller n to precision */
27e639bf 903 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
811bbf05 904 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
889059d8 905 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
c1a9ae43 906 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
6b4bf1c4 907 clock.p = clock.p1 * clock.p2;
a0c4da24 908 /* based on hardware requirement, prefer bigger m1,m2 values */
6b4bf1c4 909 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
d5dd62bd 910 unsigned int ppm;
69e4f900 911
6b4bf1c4
VS
912 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
913 refclk * clock.m1);
914
dccbea3b 915 vlv_calc_dpll_params(refclk, &clock);
43b0ac53 916
e2d214ae
TU
917 if (!intel_PLL_is_valid(to_i915(dev),
918 limit,
f01b7962 919 &clock))
43b0ac53
VS
920 continue;
921
d5dd62bd
ID
922 if (!vlv_PLL_is_optimal(dev, target,
923 &clock,
924 best_clock,
925 bestppm, &ppm))
926 continue;
6b4bf1c4 927
d5dd62bd
ID
928 *best_clock = clock;
929 bestppm = ppm;
930 found = true;
a0c4da24
JB
931 }
932 }
933 }
934 }
a0c4da24 935
49e497ef 936 return found;
a0c4da24 937}
a4fc5ed6 938
65b3d6a9
ACO
939/*
940 * Returns a set of divisors for the desired target clock with the given
941 * refclk, or FALSE. The returned values represent the clock equation:
942 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
943 */
ef9348c8 944static bool
1b6f4958 945chv_find_best_dpll(const struct intel_limit *limit,
a93e255f 946 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
947 int target, int refclk, struct dpll *match_clock,
948 struct dpll *best_clock)
ef9348c8 949{
a93e255f 950 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 951 struct drm_device *dev = crtc->base.dev;
9ca3ba01 952 unsigned int best_error_ppm;
9e2c8475 953 struct dpll clock;
ef9348c8
CML
954 uint64_t m2;
955 int found = false;
956
957 memset(best_clock, 0, sizeof(*best_clock));
9ca3ba01 958 best_error_ppm = 1000000;
ef9348c8
CML
959
960 /*
961 * Based on hardware doc, the n always set to 1, and m1 always
962 * set to 2. If requires to support 200Mhz refclk, we need to
963 * revisit this because n may not 1 anymore.
964 */
965 clock.n = 1, clock.m1 = 2;
966 target *= 5; /* fast clock */
967
968 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
969 for (clock.p2 = limit->p2.p2_fast;
970 clock.p2 >= limit->p2.p2_slow;
971 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
9ca3ba01 972 unsigned int error_ppm;
ef9348c8
CML
973
974 clock.p = clock.p1 * clock.p2;
975
976 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
977 clock.n) << 22, refclk * clock.m1);
978
979 if (m2 > INT_MAX/clock.m1)
980 continue;
981
982 clock.m2 = m2;
983
dccbea3b 984 chv_calc_dpll_params(refclk, &clock);
ef9348c8 985
e2d214ae 986 if (!intel_PLL_is_valid(to_i915(dev), limit, &clock))
ef9348c8
CML
987 continue;
988
9ca3ba01
ID
989 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
990 best_error_ppm, &error_ppm))
991 continue;
992
993 *best_clock = clock;
994 best_error_ppm = error_ppm;
995 found = true;
ef9348c8
CML
996 }
997 }
998
999 return found;
1000}
1001
5ab7b0b7 1002bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
9e2c8475 1003 struct dpll *best_clock)
5ab7b0b7 1004{
65b3d6a9 1005 int refclk = 100000;
1b6f4958 1006 const struct intel_limit *limit = &intel_limits_bxt;
5ab7b0b7 1007
65b3d6a9 1008 return chv_find_best_dpll(limit, crtc_state,
5ab7b0b7
ID
1009 target_clock, refclk, NULL, best_clock);
1010}
1011
525b9311 1012bool intel_crtc_active(struct intel_crtc *crtc)
20ddf665 1013{
20ddf665
VS
1014 /* Be paranoid as we can arrive here with only partial
1015 * state retrieved from the hardware during setup.
1016 *
241bfc38 1017 * We can ditch the adjusted_mode.crtc_clock check as soon
20ddf665
VS
1018 * as Haswell has gained clock readout/fastboot support.
1019 *
66e514c1 1020 * We can ditch the crtc->primary->fb check as soon as we can
20ddf665 1021 * properly reconstruct framebuffers.
c3d1f436
MR
1022 *
1023 * FIXME: The intel_crtc->active here should be switched to
1024 * crtc->state->active once we have proper CRTC states wired up
1025 * for atomic.
20ddf665 1026 */
525b9311
VS
1027 return crtc->active && crtc->base.primary->state->fb &&
1028 crtc->config->base.adjusted_mode.crtc_clock;
20ddf665
VS
1029}
1030
a5c961d1
PZ
1031enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1032 enum pipe pipe)
1033{
98187836 1034 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
a5c961d1 1035
e2af48c6 1036 return crtc->config->cpu_transcoder;
a5c961d1
PZ
1037}
1038
6315b5d3 1039static bool pipe_dsl_stopped(struct drm_i915_private *dev_priv, enum pipe pipe)
fbf49ea2 1040{
f0f59a00 1041 i915_reg_t reg = PIPEDSL(pipe);
fbf49ea2
VS
1042 u32 line1, line2;
1043 u32 line_mask;
1044
5db94019 1045 if (IS_GEN2(dev_priv))
fbf49ea2
VS
1046 line_mask = DSL_LINEMASK_GEN2;
1047 else
1048 line_mask = DSL_LINEMASK_GEN3;
1049
1050 line1 = I915_READ(reg) & line_mask;
6adfb1ef 1051 msleep(5);
fbf49ea2
VS
1052 line2 = I915_READ(reg) & line_mask;
1053
1054 return line1 == line2;
1055}
1056
ab7ad7f6
KP
1057/*
1058 * intel_wait_for_pipe_off - wait for pipe to turn off
575f7ab7 1059 * @crtc: crtc whose pipe to wait for
9d0498a2
JB
1060 *
1061 * After disabling a pipe, we can't wait for vblank in the usual way,
1062 * spinning on the vblank interrupt status bit, since we won't actually
1063 * see an interrupt when the pipe is disabled.
1064 *
ab7ad7f6
KP
1065 * On Gen4 and above:
1066 * wait for the pipe register state bit to turn off
1067 *
1068 * Otherwise:
1069 * wait for the display line value to settle (it usually
1070 * ends up stopping at the start of the next frame).
58e10eb9 1071 *
9d0498a2 1072 */
575f7ab7 1073static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
9d0498a2 1074{
6315b5d3 1075 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6e3c9717 1076 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 1077 enum pipe pipe = crtc->pipe;
ab7ad7f6 1078
6315b5d3 1079 if (INTEL_GEN(dev_priv) >= 4) {
f0f59a00 1080 i915_reg_t reg = PIPECONF(cpu_transcoder);
ab7ad7f6
KP
1081
1082 /* Wait for the Pipe State to go off */
b8511f53
CW
1083 if (intel_wait_for_register(dev_priv,
1084 reg, I965_PIPECONF_ACTIVE, 0,
1085 100))
284637d9 1086 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1087 } else {
ab7ad7f6 1088 /* Wait for the display line to settle */
6315b5d3 1089 if (wait_for(pipe_dsl_stopped(dev_priv, pipe), 100))
284637d9 1090 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1091 }
79e53945
JB
1092}
1093
b24e7179 1094/* Only for pre-ILK configs */
55607e8a
DV
1095void assert_pll(struct drm_i915_private *dev_priv,
1096 enum pipe pipe, bool state)
b24e7179 1097{
b24e7179
JB
1098 u32 val;
1099 bool cur_state;
1100
649636ef 1101 val = I915_READ(DPLL(pipe));
b24e7179 1102 cur_state = !!(val & DPLL_VCO_ENABLE);
e2c719b7 1103 I915_STATE_WARN(cur_state != state,
b24e7179 1104 "PLL state assertion failure (expected %s, current %s)\n",
87ad3212 1105 onoff(state), onoff(cur_state));
b24e7179 1106}
b24e7179 1107
23538ef1 1108/* XXX: the dsi pll is shared between MIPI DSI ports */
8563b1e8 1109void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
23538ef1
JN
1110{
1111 u32 val;
1112 bool cur_state;
1113
a580516d 1114 mutex_lock(&dev_priv->sb_lock);
23538ef1 1115 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
a580516d 1116 mutex_unlock(&dev_priv->sb_lock);
23538ef1
JN
1117
1118 cur_state = val & DSI_PLL_VCO_EN;
e2c719b7 1119 I915_STATE_WARN(cur_state != state,
23538ef1 1120 "DSI PLL state assertion failure (expected %s, current %s)\n",
87ad3212 1121 onoff(state), onoff(cur_state));
23538ef1 1122}
23538ef1 1123
040484af
JB
1124static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1125 enum pipe pipe, bool state)
1126{
040484af 1127 bool cur_state;
ad80a810
PZ
1128 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1129 pipe);
040484af 1130
2d1fe073 1131 if (HAS_DDI(dev_priv)) {
affa9354 1132 /* DDI does not have a specific FDI_TX register */
649636ef 1133 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
ad80a810 1134 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
bf507ef7 1135 } else {
649636ef 1136 u32 val = I915_READ(FDI_TX_CTL(pipe));
bf507ef7
ED
1137 cur_state = !!(val & FDI_TX_ENABLE);
1138 }
e2c719b7 1139 I915_STATE_WARN(cur_state != state,
040484af 1140 "FDI TX state assertion failure (expected %s, current %s)\n",
87ad3212 1141 onoff(state), onoff(cur_state));
040484af
JB
1142}
1143#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1144#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1145
1146static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1147 enum pipe pipe, bool state)
1148{
040484af
JB
1149 u32 val;
1150 bool cur_state;
1151
649636ef 1152 val = I915_READ(FDI_RX_CTL(pipe));
d63fa0dc 1153 cur_state = !!(val & FDI_RX_ENABLE);
e2c719b7 1154 I915_STATE_WARN(cur_state != state,
040484af 1155 "FDI RX state assertion failure (expected %s, current %s)\n",
87ad3212 1156 onoff(state), onoff(cur_state));
040484af
JB
1157}
1158#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1159#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1160
1161static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1162 enum pipe pipe)
1163{
040484af
JB
1164 u32 val;
1165
1166 /* ILK FDI PLL is always enabled */
7e22dbbb 1167 if (IS_GEN5(dev_priv))
040484af
JB
1168 return;
1169
bf507ef7 1170 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
2d1fe073 1171 if (HAS_DDI(dev_priv))
bf507ef7
ED
1172 return;
1173
649636ef 1174 val = I915_READ(FDI_TX_CTL(pipe));
e2c719b7 1175 I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
040484af
JB
1176}
1177
55607e8a
DV
1178void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1179 enum pipe pipe, bool state)
040484af 1180{
040484af 1181 u32 val;
55607e8a 1182 bool cur_state;
040484af 1183
649636ef 1184 val = I915_READ(FDI_RX_CTL(pipe));
55607e8a 1185 cur_state = !!(val & FDI_RX_PLL_ENABLE);
e2c719b7 1186 I915_STATE_WARN(cur_state != state,
55607e8a 1187 "FDI RX PLL assertion failure (expected %s, current %s)\n",
87ad3212 1188 onoff(state), onoff(cur_state));
040484af
JB
1189}
1190
4f8036a2 1191void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
ea0760cf 1192{
f0f59a00 1193 i915_reg_t pp_reg;
ea0760cf
JB
1194 u32 val;
1195 enum pipe panel_pipe = PIPE_A;
0de3b485 1196 bool locked = true;
ea0760cf 1197
4f8036a2 1198 if (WARN_ON(HAS_DDI(dev_priv)))
bedd4dba
JN
1199 return;
1200
4f8036a2 1201 if (HAS_PCH_SPLIT(dev_priv)) {
bedd4dba
JN
1202 u32 port_sel;
1203
44cb734c
ID
1204 pp_reg = PP_CONTROL(0);
1205 port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
bedd4dba
JN
1206
1207 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1208 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1209 panel_pipe = PIPE_B;
1210 /* XXX: else fix for eDP */
4f8036a2 1211 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
bedd4dba 1212 /* presumably write lock depends on pipe, not port select */
44cb734c 1213 pp_reg = PP_CONTROL(pipe);
bedd4dba 1214 panel_pipe = pipe;
ea0760cf 1215 } else {
44cb734c 1216 pp_reg = PP_CONTROL(0);
bedd4dba
JN
1217 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1218 panel_pipe = PIPE_B;
ea0760cf
JB
1219 }
1220
1221 val = I915_READ(pp_reg);
1222 if (!(val & PANEL_POWER_ON) ||
ec49ba2d 1223 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
ea0760cf
JB
1224 locked = false;
1225
e2c719b7 1226 I915_STATE_WARN(panel_pipe == pipe && locked,
ea0760cf 1227 "panel assertion failure, pipe %c regs locked\n",
9db4a9c7 1228 pipe_name(pipe));
ea0760cf
JB
1229}
1230
93ce0ba6
JN
1231static void assert_cursor(struct drm_i915_private *dev_priv,
1232 enum pipe pipe, bool state)
1233{
93ce0ba6
JN
1234 bool cur_state;
1235
50a0bc90 1236 if (IS_845G(dev_priv) || IS_I865G(dev_priv))
0b87c24e 1237 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
d9d82081 1238 else
5efb3e28 1239 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
93ce0ba6 1240
e2c719b7 1241 I915_STATE_WARN(cur_state != state,
93ce0ba6 1242 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
87ad3212 1243 pipe_name(pipe), onoff(state), onoff(cur_state));
93ce0ba6
JN
1244}
1245#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1246#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1247
b840d907
JB
1248void assert_pipe(struct drm_i915_private *dev_priv,
1249 enum pipe pipe, bool state)
b24e7179 1250{
63d7bbe9 1251 bool cur_state;
702e7a56
PZ
1252 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1253 pipe);
4feed0eb 1254 enum intel_display_power_domain power_domain;
b24e7179 1255
b6b5d049
VS
1256 /* if we need the pipe quirk it must be always on */
1257 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1258 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
8e636784
DV
1259 state = true;
1260
4feed0eb
ID
1261 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1262 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
649636ef 1263 u32 val = I915_READ(PIPECONF(cpu_transcoder));
69310161 1264 cur_state = !!(val & PIPECONF_ENABLE);
4feed0eb
ID
1265
1266 intel_display_power_put(dev_priv, power_domain);
1267 } else {
1268 cur_state = false;
69310161
PZ
1269 }
1270
e2c719b7 1271 I915_STATE_WARN(cur_state != state,
63d7bbe9 1272 "pipe %c assertion failure (expected %s, current %s)\n",
87ad3212 1273 pipe_name(pipe), onoff(state), onoff(cur_state));
b24e7179
JB
1274}
1275
931872fc
CW
1276static void assert_plane(struct drm_i915_private *dev_priv,
1277 enum plane plane, bool state)
b24e7179 1278{
b24e7179 1279 u32 val;
931872fc 1280 bool cur_state;
b24e7179 1281
649636ef 1282 val = I915_READ(DSPCNTR(plane));
931872fc 1283 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
e2c719b7 1284 I915_STATE_WARN(cur_state != state,
931872fc 1285 "plane %c assertion failure (expected %s, current %s)\n",
87ad3212 1286 plane_name(plane), onoff(state), onoff(cur_state));
b24e7179
JB
1287}
1288
931872fc
CW
1289#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1290#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1291
b24e7179
JB
1292static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1293 enum pipe pipe)
1294{
649636ef 1295 int i;
b24e7179 1296
653e1026 1297 /* Primary planes are fixed to pipes on gen4+ */
6315b5d3 1298 if (INTEL_GEN(dev_priv) >= 4) {
649636ef 1299 u32 val = I915_READ(DSPCNTR(pipe));
e2c719b7 1300 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
28c05794
AJ
1301 "plane %c assertion failure, should be disabled but not\n",
1302 plane_name(pipe));
19ec1358 1303 return;
28c05794 1304 }
19ec1358 1305
b24e7179 1306 /* Need to check both planes against the pipe */
055e393f 1307 for_each_pipe(dev_priv, i) {
649636ef
VS
1308 u32 val = I915_READ(DSPCNTR(i));
1309 enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
b24e7179 1310 DISPPLANE_SEL_PIPE_SHIFT;
e2c719b7 1311 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
9db4a9c7
JB
1312 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1313 plane_name(i), pipe_name(pipe));
b24e7179
JB
1314 }
1315}
1316
19332d7a
JB
1317static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1318 enum pipe pipe)
1319{
649636ef 1320 int sprite;
19332d7a 1321
6315b5d3 1322 if (INTEL_GEN(dev_priv) >= 9) {
3bdcfc0c 1323 for_each_sprite(dev_priv, pipe, sprite) {
649636ef 1324 u32 val = I915_READ(PLANE_CTL(pipe, sprite));
e2c719b7 1325 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
7feb8b88
DL
1326 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1327 sprite, pipe_name(pipe));
1328 }
920a14b2 1329 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
3bdcfc0c 1330 for_each_sprite(dev_priv, pipe, sprite) {
83c04a62 1331 u32 val = I915_READ(SPCNTR(pipe, PLANE_SPRITE0 + sprite));
e2c719b7 1332 I915_STATE_WARN(val & SP_ENABLE,
20674eef 1333 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1fe47785 1334 sprite_name(pipe, sprite), pipe_name(pipe));
20674eef 1335 }
6315b5d3 1336 } else if (INTEL_GEN(dev_priv) >= 7) {
649636ef 1337 u32 val = I915_READ(SPRCTL(pipe));
e2c719b7 1338 I915_STATE_WARN(val & SPRITE_ENABLE,
06da8da2 1339 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef 1340 plane_name(pipe), pipe_name(pipe));
6315b5d3 1341 } else if (INTEL_GEN(dev_priv) >= 5) {
649636ef 1342 u32 val = I915_READ(DVSCNTR(pipe));
e2c719b7 1343 I915_STATE_WARN(val & DVS_ENABLE,
06da8da2 1344 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef 1345 plane_name(pipe), pipe_name(pipe));
19332d7a
JB
1346 }
1347}
1348
08c71e5e
VS
1349static void assert_vblank_disabled(struct drm_crtc *crtc)
1350{
e2c719b7 1351 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
08c71e5e
VS
1352 drm_crtc_vblank_put(crtc);
1353}
1354
7abd4b35
ACO
1355void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1356 enum pipe pipe)
92f2584a 1357{
92f2584a
JB
1358 u32 val;
1359 bool enabled;
1360
649636ef 1361 val = I915_READ(PCH_TRANSCONF(pipe));
92f2584a 1362 enabled = !!(val & TRANS_ENABLE);
e2c719b7 1363 I915_STATE_WARN(enabled,
9db4a9c7
JB
1364 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1365 pipe_name(pipe));
92f2584a
JB
1366}
1367
4e634389
KP
1368static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1369 enum pipe pipe, u32 port_sel, u32 val)
f0575e92
KP
1370{
1371 if ((val & DP_PORT_EN) == 0)
1372 return false;
1373
2d1fe073 1374 if (HAS_PCH_CPT(dev_priv)) {
f0f59a00 1375 u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
f0575e92
KP
1376 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1377 return false;
2d1fe073 1378 } else if (IS_CHERRYVIEW(dev_priv)) {
44f37d1f
CML
1379 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1380 return false;
f0575e92
KP
1381 } else {
1382 if ((val & DP_PIPE_MASK) != (pipe << 30))
1383 return false;
1384 }
1385 return true;
1386}
1387
1519b995
KP
1388static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1389 enum pipe pipe, u32 val)
1390{
dc0fa718 1391 if ((val & SDVO_ENABLE) == 0)
1519b995
KP
1392 return false;
1393
2d1fe073 1394 if (HAS_PCH_CPT(dev_priv)) {
dc0fa718 1395 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1519b995 1396 return false;
2d1fe073 1397 } else if (IS_CHERRYVIEW(dev_priv)) {
44f37d1f
CML
1398 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1399 return false;
1519b995 1400 } else {
dc0fa718 1401 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1519b995
KP
1402 return false;
1403 }
1404 return true;
1405}
1406
1407static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1408 enum pipe pipe, u32 val)
1409{
1410 if ((val & LVDS_PORT_EN) == 0)
1411 return false;
1412
2d1fe073 1413 if (HAS_PCH_CPT(dev_priv)) {
1519b995
KP
1414 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1415 return false;
1416 } else {
1417 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1418 return false;
1419 }
1420 return true;
1421}
1422
1423static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1424 enum pipe pipe, u32 val)
1425{
1426 if ((val & ADPA_DAC_ENABLE) == 0)
1427 return false;
2d1fe073 1428 if (HAS_PCH_CPT(dev_priv)) {
1519b995
KP
1429 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1430 return false;
1431 } else {
1432 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1433 return false;
1434 }
1435 return true;
1436}
1437
291906f1 1438static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
f0f59a00
VS
1439 enum pipe pipe, i915_reg_t reg,
1440 u32 port_sel)
291906f1 1441{
47a05eca 1442 u32 val = I915_READ(reg);
e2c719b7 1443 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
291906f1 1444 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
f0f59a00 1445 i915_mmio_reg_offset(reg), pipe_name(pipe));
de9a35ab 1446
2d1fe073 1447 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
75c5da27 1448 && (val & DP_PIPEB_SELECT),
de9a35ab 1449 "IBX PCH dp port still using transcoder B\n");
291906f1
JB
1450}
1451
1452static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
f0f59a00 1453 enum pipe pipe, i915_reg_t reg)
291906f1 1454{
47a05eca 1455 u32 val = I915_READ(reg);
e2c719b7 1456 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
23c99e77 1457 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
f0f59a00 1458 i915_mmio_reg_offset(reg), pipe_name(pipe));
de9a35ab 1459
2d1fe073 1460 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
75c5da27 1461 && (val & SDVO_PIPE_B_SELECT),
de9a35ab 1462 "IBX PCH hdmi port still using transcoder B\n");
291906f1
JB
1463}
1464
1465static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1466 enum pipe pipe)
1467{
291906f1 1468 u32 val;
291906f1 1469
f0575e92
KP
1470 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1471 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1472 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
291906f1 1473
649636ef 1474 val = I915_READ(PCH_ADPA);
e2c719b7 1475 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
291906f1 1476 "PCH VGA enabled on transcoder %c, should be disabled\n",
9db4a9c7 1477 pipe_name(pipe));
291906f1 1478
649636ef 1479 val = I915_READ(PCH_LVDS);
e2c719b7 1480 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
291906f1 1481 "PCH LVDS enabled on transcoder %c, should be disabled\n",
9db4a9c7 1482 pipe_name(pipe));
291906f1 1483
e2debe91
PZ
1484 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1485 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1486 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
291906f1
JB
1487}
1488
cd2d34d9
VS
1489static void _vlv_enable_pll(struct intel_crtc *crtc,
1490 const struct intel_crtc_state *pipe_config)
1491{
1492 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1493 enum pipe pipe = crtc->pipe;
1494
1495 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1496 POSTING_READ(DPLL(pipe));
1497 udelay(150);
1498
2c30b43b
CW
1499 if (intel_wait_for_register(dev_priv,
1500 DPLL(pipe),
1501 DPLL_LOCK_VLV,
1502 DPLL_LOCK_VLV,
1503 1))
cd2d34d9
VS
1504 DRM_ERROR("DPLL %d failed to lock\n", pipe);
1505}
1506
d288f65f 1507static void vlv_enable_pll(struct intel_crtc *crtc,
5cec258b 1508 const struct intel_crtc_state *pipe_config)
87442f73 1509{
cd2d34d9 1510 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8bd3f301 1511 enum pipe pipe = crtc->pipe;
87442f73 1512
8bd3f301 1513 assert_pipe_disabled(dev_priv, pipe);
87442f73 1514
87442f73 1515 /* PLL is protected by panel, make sure we can write it */
7d1a83cb 1516 assert_panel_unlocked(dev_priv, pipe);
87442f73 1517
cd2d34d9
VS
1518 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1519 _vlv_enable_pll(crtc, pipe_config);
426115cf 1520
8bd3f301
VS
1521 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1522 POSTING_READ(DPLL_MD(pipe));
87442f73
DV
1523}
1524
cd2d34d9
VS
1525
1526static void _chv_enable_pll(struct intel_crtc *crtc,
1527 const struct intel_crtc_state *pipe_config)
9d556c99 1528{
cd2d34d9 1529 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8bd3f301 1530 enum pipe pipe = crtc->pipe;
9d556c99 1531 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9d556c99
CML
1532 u32 tmp;
1533
a580516d 1534 mutex_lock(&dev_priv->sb_lock);
9d556c99
CML
1535
1536 /* Enable back the 10bit clock to display controller */
1537 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1538 tmp |= DPIO_DCLKP_EN;
1539 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1540
54433e91
VS
1541 mutex_unlock(&dev_priv->sb_lock);
1542
9d556c99
CML
1543 /*
1544 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1545 */
1546 udelay(1);
1547
1548 /* Enable PLL */
d288f65f 1549 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
9d556c99
CML
1550
1551 /* Check PLL is locked */
6b18826a
CW
1552 if (intel_wait_for_register(dev_priv,
1553 DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
1554 1))
9d556c99 1555 DRM_ERROR("PLL %d failed to lock\n", pipe);
cd2d34d9
VS
1556}
1557
1558static void chv_enable_pll(struct intel_crtc *crtc,
1559 const struct intel_crtc_state *pipe_config)
1560{
1561 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1562 enum pipe pipe = crtc->pipe;
1563
1564 assert_pipe_disabled(dev_priv, pipe);
1565
1566 /* PLL is protected by panel, make sure we can write it */
1567 assert_panel_unlocked(dev_priv, pipe);
1568
1569 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1570 _chv_enable_pll(crtc, pipe_config);
9d556c99 1571
c231775c
VS
1572 if (pipe != PIPE_A) {
1573 /*
1574 * WaPixelRepeatModeFixForC0:chv
1575 *
1576 * DPLLCMD is AWOL. Use chicken bits to propagate
1577 * the value from DPLLBMD to either pipe B or C.
1578 */
1579 I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
1580 I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
1581 I915_WRITE(CBR4_VLV, 0);
1582 dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
1583
1584 /*
1585 * DPLLB VGA mode also seems to cause problems.
1586 * We should always have it disabled.
1587 */
1588 WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
1589 } else {
1590 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1591 POSTING_READ(DPLL_MD(pipe));
1592 }
9d556c99
CML
1593}
1594
6315b5d3 1595static int intel_num_dvo_pipes(struct drm_i915_private *dev_priv)
1c4e0274
VS
1596{
1597 struct intel_crtc *crtc;
1598 int count = 0;
1599
6315b5d3 1600 for_each_intel_crtc(&dev_priv->drm, crtc) {
3538b9df 1601 count += crtc->base.state->active &&
2d84d2b3
VS
1602 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
1603 }
1c4e0274
VS
1604
1605 return count;
1606}
1607
66e3d5c0 1608static void i9xx_enable_pll(struct intel_crtc *crtc)
63d7bbe9 1609{
6315b5d3 1610 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
f0f59a00 1611 i915_reg_t reg = DPLL(crtc->pipe);
6e3c9717 1612 u32 dpll = crtc->config->dpll_hw_state.dpll;
63d7bbe9 1613
66e3d5c0 1614 assert_pipe_disabled(dev_priv, crtc->pipe);
58c6eaa2 1615
63d7bbe9 1616 /* PLL is protected by panel, make sure we can write it */
50a0bc90 1617 if (IS_MOBILE(dev_priv) && !IS_I830(dev_priv))
66e3d5c0 1618 assert_panel_unlocked(dev_priv, crtc->pipe);
63d7bbe9 1619
1c4e0274 1620 /* Enable DVO 2x clock on both PLLs if necessary */
6315b5d3 1621 if (IS_I830(dev_priv) && intel_num_dvo_pipes(dev_priv) > 0) {
1c4e0274
VS
1622 /*
1623 * It appears to be important that we don't enable this
1624 * for the current pipe before otherwise configuring the
1625 * PLL. No idea how this should be handled if multiple
1626 * DVO outputs are enabled simultaneosly.
1627 */
1628 dpll |= DPLL_DVO_2X_MODE;
1629 I915_WRITE(DPLL(!crtc->pipe),
1630 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1631 }
66e3d5c0 1632
c2b63374
VS
1633 /*
1634 * Apparently we need to have VGA mode enabled prior to changing
1635 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1636 * dividers, even though the register value does change.
1637 */
1638 I915_WRITE(reg, 0);
1639
8e7a65aa
VS
1640 I915_WRITE(reg, dpll);
1641
66e3d5c0
DV
1642 /* Wait for the clocks to stabilize. */
1643 POSTING_READ(reg);
1644 udelay(150);
1645
6315b5d3 1646 if (INTEL_GEN(dev_priv) >= 4) {
66e3d5c0 1647 I915_WRITE(DPLL_MD(crtc->pipe),
6e3c9717 1648 crtc->config->dpll_hw_state.dpll_md);
66e3d5c0
DV
1649 } else {
1650 /* The pixel multiplier can only be updated once the
1651 * DPLL is enabled and the clocks are stable.
1652 *
1653 * So write it again.
1654 */
1655 I915_WRITE(reg, dpll);
1656 }
63d7bbe9
JB
1657
1658 /* We do this three times for luck */
66e3d5c0 1659 I915_WRITE(reg, dpll);
63d7bbe9
JB
1660 POSTING_READ(reg);
1661 udelay(150); /* wait for warmup */
66e3d5c0 1662 I915_WRITE(reg, dpll);
63d7bbe9
JB
1663 POSTING_READ(reg);
1664 udelay(150); /* wait for warmup */
66e3d5c0 1665 I915_WRITE(reg, dpll);
63d7bbe9
JB
1666 POSTING_READ(reg);
1667 udelay(150); /* wait for warmup */
1668}
1669
1670/**
50b44a44 1671 * i9xx_disable_pll - disable a PLL
63d7bbe9
JB
1672 * @dev_priv: i915 private structure
1673 * @pipe: pipe PLL to disable
1674 *
1675 * Disable the PLL for @pipe, making sure the pipe is off first.
1676 *
1677 * Note! This is for pre-ILK only.
1678 */
1c4e0274 1679static void i9xx_disable_pll(struct intel_crtc *crtc)
63d7bbe9 1680{
6315b5d3 1681 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1c4e0274
VS
1682 enum pipe pipe = crtc->pipe;
1683
1684 /* Disable DVO 2x clock on both PLLs if necessary */
50a0bc90 1685 if (IS_I830(dev_priv) &&
2d84d2b3 1686 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
6315b5d3 1687 !intel_num_dvo_pipes(dev_priv)) {
1c4e0274
VS
1688 I915_WRITE(DPLL(PIPE_B),
1689 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1690 I915_WRITE(DPLL(PIPE_A),
1691 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1692 }
1693
b6b5d049
VS
1694 /* Don't disable pipe or pipe PLLs if needed */
1695 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1696 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
63d7bbe9
JB
1697 return;
1698
1699 /* Make sure the pipe isn't still relying on us */
1700 assert_pipe_disabled(dev_priv, pipe);
1701
b8afb911 1702 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
50b44a44 1703 POSTING_READ(DPLL(pipe));
63d7bbe9
JB
1704}
1705
f6071166
JB
1706static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1707{
b8afb911 1708 u32 val;
f6071166
JB
1709
1710 /* Make sure the pipe isn't still relying on us */
1711 assert_pipe_disabled(dev_priv, pipe);
1712
03ed5cbf
VS
1713 val = DPLL_INTEGRATED_REF_CLK_VLV |
1714 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1715 if (pipe != PIPE_A)
1716 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1717
f6071166
JB
1718 I915_WRITE(DPLL(pipe), val);
1719 POSTING_READ(DPLL(pipe));
076ed3b2
CML
1720}
1721
1722static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1723{
d752048d 1724 enum dpio_channel port = vlv_pipe_to_channel(pipe);
076ed3b2
CML
1725 u32 val;
1726
a11b0703
VS
1727 /* Make sure the pipe isn't still relying on us */
1728 assert_pipe_disabled(dev_priv, pipe);
076ed3b2 1729
60bfe44f
VS
1730 val = DPLL_SSC_REF_CLK_CHV |
1731 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
a11b0703
VS
1732 if (pipe != PIPE_A)
1733 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
03ed5cbf 1734
a11b0703
VS
1735 I915_WRITE(DPLL(pipe), val);
1736 POSTING_READ(DPLL(pipe));
d752048d 1737
a580516d 1738 mutex_lock(&dev_priv->sb_lock);
d752048d
VS
1739
1740 /* Disable 10bit clock to display controller */
1741 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1742 val &= ~DPIO_DCLKP_EN;
1743 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1744
a580516d 1745 mutex_unlock(&dev_priv->sb_lock);
f6071166
JB
1746}
1747
e4607fcf 1748void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
9b6de0a1
VS
1749 struct intel_digital_port *dport,
1750 unsigned int expected_mask)
89b667f8
JB
1751{
1752 u32 port_mask;
f0f59a00 1753 i915_reg_t dpll_reg;
89b667f8 1754
e4607fcf
CML
1755 switch (dport->port) {
1756 case PORT_B:
89b667f8 1757 port_mask = DPLL_PORTB_READY_MASK;
00fc31b7 1758 dpll_reg = DPLL(0);
e4607fcf
CML
1759 break;
1760 case PORT_C:
89b667f8 1761 port_mask = DPLL_PORTC_READY_MASK;
00fc31b7 1762 dpll_reg = DPLL(0);
9b6de0a1 1763 expected_mask <<= 4;
00fc31b7
CML
1764 break;
1765 case PORT_D:
1766 port_mask = DPLL_PORTD_READY_MASK;
1767 dpll_reg = DPIO_PHY_STATUS;
e4607fcf
CML
1768 break;
1769 default:
1770 BUG();
1771 }
89b667f8 1772
370004d3
CW
1773 if (intel_wait_for_register(dev_priv,
1774 dpll_reg, port_mask, expected_mask,
1775 1000))
9b6de0a1
VS
1776 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1777 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
89b667f8
JB
1778}
1779
b8a4f404
PZ
1780static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1781 enum pipe pipe)
040484af 1782{
98187836
VS
1783 struct intel_crtc *intel_crtc = intel_get_crtc_for_pipe(dev_priv,
1784 pipe);
f0f59a00
VS
1785 i915_reg_t reg;
1786 uint32_t val, pipeconf_val;
040484af 1787
040484af 1788 /* Make sure PCH DPLL is enabled */
8106ddbd 1789 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
040484af
JB
1790
1791 /* FDI must be feeding us bits for PCH ports */
1792 assert_fdi_tx_enabled(dev_priv, pipe);
1793 assert_fdi_rx_enabled(dev_priv, pipe);
1794
6e266956 1795 if (HAS_PCH_CPT(dev_priv)) {
23670b32
DV
1796 /* Workaround: Set the timing override bit before enabling the
1797 * pch transcoder. */
1798 reg = TRANS_CHICKEN2(pipe);
1799 val = I915_READ(reg);
1800 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1801 I915_WRITE(reg, val);
59c859d6 1802 }
23670b32 1803
ab9412ba 1804 reg = PCH_TRANSCONF(pipe);
040484af 1805 val = I915_READ(reg);
5f7f726d 1806 pipeconf_val = I915_READ(PIPECONF(pipe));
e9bcff5c 1807
2d1fe073 1808 if (HAS_PCH_IBX(dev_priv)) {
e9bcff5c 1809 /*
c5de7c6f
VS
1810 * Make the BPC in transcoder be consistent with
1811 * that in pipeconf reg. For HDMI we must use 8bpc
1812 * here for both 8bpc and 12bpc.
e9bcff5c 1813 */
dfd07d72 1814 val &= ~PIPECONF_BPC_MASK;
2d84d2b3 1815 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
c5de7c6f
VS
1816 val |= PIPECONF_8BPC;
1817 else
1818 val |= pipeconf_val & PIPECONF_BPC_MASK;
e9bcff5c 1819 }
5f7f726d
PZ
1820
1821 val &= ~TRANS_INTERLACE_MASK;
1822 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
2d1fe073 1823 if (HAS_PCH_IBX(dev_priv) &&
2d84d2b3 1824 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
7c26e5c6
PZ
1825 val |= TRANS_LEGACY_INTERLACED_ILK;
1826 else
1827 val |= TRANS_INTERLACED;
5f7f726d
PZ
1828 else
1829 val |= TRANS_PROGRESSIVE;
1830
040484af 1831 I915_WRITE(reg, val | TRANS_ENABLE);
650fbd84
CW
1832 if (intel_wait_for_register(dev_priv,
1833 reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
1834 100))
4bb6f1f3 1835 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
040484af
JB
1836}
1837
8fb033d7 1838static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
937bb610 1839 enum transcoder cpu_transcoder)
040484af 1840{
8fb033d7 1841 u32 val, pipeconf_val;
8fb033d7 1842
8fb033d7 1843 /* FDI must be feeding us bits for PCH ports */
1a240d4d 1844 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
937bb610 1845 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
8fb033d7 1846
223a6fdf 1847 /* Workaround: set timing override bit. */
36c0d0cf 1848 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
23670b32 1849 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
36c0d0cf 1850 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
223a6fdf 1851
25f3ef11 1852 val = TRANS_ENABLE;
937bb610 1853 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
8fb033d7 1854
9a76b1c6
PZ
1855 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1856 PIPECONF_INTERLACED_ILK)
a35f2679 1857 val |= TRANS_INTERLACED;
8fb033d7
PZ
1858 else
1859 val |= TRANS_PROGRESSIVE;
1860
ab9412ba 1861 I915_WRITE(LPT_TRANSCONF, val);
d9f96244
CW
1862 if (intel_wait_for_register(dev_priv,
1863 LPT_TRANSCONF,
1864 TRANS_STATE_ENABLE,
1865 TRANS_STATE_ENABLE,
1866 100))
937bb610 1867 DRM_ERROR("Failed to enable PCH transcoder\n");
8fb033d7
PZ
1868}
1869
b8a4f404
PZ
1870static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1871 enum pipe pipe)
040484af 1872{
f0f59a00
VS
1873 i915_reg_t reg;
1874 uint32_t val;
040484af
JB
1875
1876 /* FDI relies on the transcoder */
1877 assert_fdi_tx_disabled(dev_priv, pipe);
1878 assert_fdi_rx_disabled(dev_priv, pipe);
1879
291906f1
JB
1880 /* Ports must be off as well */
1881 assert_pch_ports_disabled(dev_priv, pipe);
1882
ab9412ba 1883 reg = PCH_TRANSCONF(pipe);
040484af
JB
1884 val = I915_READ(reg);
1885 val &= ~TRANS_ENABLE;
1886 I915_WRITE(reg, val);
1887 /* wait for PCH transcoder off, transcoder state */
a7d04662
CW
1888 if (intel_wait_for_register(dev_priv,
1889 reg, TRANS_STATE_ENABLE, 0,
1890 50))
4bb6f1f3 1891 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
23670b32 1892
6e266956 1893 if (HAS_PCH_CPT(dev_priv)) {
23670b32
DV
1894 /* Workaround: Clear the timing override chicken bit again. */
1895 reg = TRANS_CHICKEN2(pipe);
1896 val = I915_READ(reg);
1897 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1898 I915_WRITE(reg, val);
1899 }
040484af
JB
1900}
1901
b7076546 1902void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
8fb033d7 1903{
8fb033d7
PZ
1904 u32 val;
1905
ab9412ba 1906 val = I915_READ(LPT_TRANSCONF);
8fb033d7 1907 val &= ~TRANS_ENABLE;
ab9412ba 1908 I915_WRITE(LPT_TRANSCONF, val);
8fb033d7 1909 /* wait for PCH transcoder off, transcoder state */
dfdb4749
CW
1910 if (intel_wait_for_register(dev_priv,
1911 LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
1912 50))
8a52fd9f 1913 DRM_ERROR("Failed to disable PCH transcoder\n");
223a6fdf
PZ
1914
1915 /* Workaround: clear timing override bit. */
36c0d0cf 1916 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
23670b32 1917 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
36c0d0cf 1918 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
040484af
JB
1919}
1920
65f2130c
VS
1921enum transcoder intel_crtc_pch_transcoder(struct intel_crtc *crtc)
1922{
1923 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1924
1925 WARN_ON(!crtc->config->has_pch_encoder);
1926
1927 if (HAS_PCH_LPT(dev_priv))
1928 return TRANSCODER_A;
1929 else
1930 return (enum transcoder) crtc->pipe;
1931}
1932
b24e7179 1933/**
309cfea8 1934 * intel_enable_pipe - enable a pipe, asserting requirements
0372264a 1935 * @crtc: crtc responsible for the pipe
b24e7179 1936 *
0372264a 1937 * Enable @crtc's pipe, making sure that various hardware specific requirements
b24e7179 1938 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
b24e7179 1939 */
e1fdc473 1940static void intel_enable_pipe(struct intel_crtc *crtc)
b24e7179 1941{
0372264a 1942 struct drm_device *dev = crtc->base.dev;
fac5e23e 1943 struct drm_i915_private *dev_priv = to_i915(dev);
0372264a 1944 enum pipe pipe = crtc->pipe;
1a70a728 1945 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
f0f59a00 1946 i915_reg_t reg;
b24e7179
JB
1947 u32 val;
1948
9e2ee2dd
VS
1949 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1950
58c6eaa2 1951 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 1952 assert_cursor_disabled(dev_priv, pipe);
58c6eaa2
DV
1953 assert_sprites_disabled(dev_priv, pipe);
1954
b24e7179
JB
1955 /*
1956 * A pipe without a PLL won't actually be able to drive bits from
1957 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1958 * need the check.
1959 */
09fa8bb9 1960 if (HAS_GMCH_DISPLAY(dev_priv)) {
d7edc4e5 1961 if (intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DSI))
23538ef1
JN
1962 assert_dsi_pll_enabled(dev_priv);
1963 else
1964 assert_pll_enabled(dev_priv, pipe);
09fa8bb9 1965 } else {
6e3c9717 1966 if (crtc->config->has_pch_encoder) {
040484af 1967 /* if driving the PCH, we need FDI enabled */
65f2130c
VS
1968 assert_fdi_rx_pll_enabled(dev_priv,
1969 (enum pipe) intel_crtc_pch_transcoder(crtc));
1a240d4d
DV
1970 assert_fdi_tx_pll_enabled(dev_priv,
1971 (enum pipe) cpu_transcoder);
040484af
JB
1972 }
1973 /* FIXME: assert CPU port conditions for SNB+ */
1974 }
b24e7179 1975
702e7a56 1976 reg = PIPECONF(cpu_transcoder);
b24e7179 1977 val = I915_READ(reg);
7ad25d48 1978 if (val & PIPECONF_ENABLE) {
b6b5d049
VS
1979 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1980 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
00d70b15 1981 return;
7ad25d48 1982 }
00d70b15
CW
1983
1984 I915_WRITE(reg, val | PIPECONF_ENABLE);
851855d8 1985 POSTING_READ(reg);
b7792d8b
VS
1986
1987 /*
1988 * Until the pipe starts DSL will read as 0, which would cause
1989 * an apparent vblank timestamp jump, which messes up also the
1990 * frame count when it's derived from the timestamps. So let's
1991 * wait for the pipe to start properly before we call
1992 * drm_crtc_vblank_on()
1993 */
1994 if (dev->max_vblank_count == 0 &&
1995 wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
1996 DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
b24e7179
JB
1997}
1998
1999/**
309cfea8 2000 * intel_disable_pipe - disable a pipe, asserting requirements
575f7ab7 2001 * @crtc: crtc whose pipes is to be disabled
b24e7179 2002 *
575f7ab7
VS
2003 * Disable the pipe of @crtc, making sure that various hardware
2004 * specific requirements are met, if applicable, e.g. plane
2005 * disabled, panel fitter off, etc.
b24e7179
JB
2006 *
2007 * Will wait until the pipe has shut down before returning.
2008 */
575f7ab7 2009static void intel_disable_pipe(struct intel_crtc *crtc)
b24e7179 2010{
fac5e23e 2011 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6e3c9717 2012 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 2013 enum pipe pipe = crtc->pipe;
f0f59a00 2014 i915_reg_t reg;
b24e7179
JB
2015 u32 val;
2016
9e2ee2dd
VS
2017 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2018
b24e7179
JB
2019 /*
2020 * Make sure planes won't keep trying to pump pixels to us,
2021 * or we might hang the display.
2022 */
2023 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2024 assert_cursor_disabled(dev_priv, pipe);
19332d7a 2025 assert_sprites_disabled(dev_priv, pipe);
b24e7179 2026
702e7a56 2027 reg = PIPECONF(cpu_transcoder);
b24e7179 2028 val = I915_READ(reg);
00d70b15
CW
2029 if ((val & PIPECONF_ENABLE) == 0)
2030 return;
2031
67adc644
VS
2032 /*
2033 * Double wide has implications for planes
2034 * so best keep it disabled when not needed.
2035 */
6e3c9717 2036 if (crtc->config->double_wide)
67adc644
VS
2037 val &= ~PIPECONF_DOUBLE_WIDE;
2038
2039 /* Don't disable pipe or pipe PLLs if needed */
b6b5d049
VS
2040 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2041 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
67adc644
VS
2042 val &= ~PIPECONF_ENABLE;
2043
2044 I915_WRITE(reg, val);
2045 if ((val & PIPECONF_ENABLE) == 0)
2046 intel_wait_for_pipe_off(crtc);
b24e7179
JB
2047}
2048
832be82f
VS
2049static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
2050{
2051 return IS_GEN2(dev_priv) ? 2048 : 4096;
2052}
2053
27ba3910
VS
2054static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
2055 uint64_t fb_modifier, unsigned int cpp)
7b49f948
VS
2056{
2057 switch (fb_modifier) {
2058 case DRM_FORMAT_MOD_NONE:
2059 return cpp;
2060 case I915_FORMAT_MOD_X_TILED:
2061 if (IS_GEN2(dev_priv))
2062 return 128;
2063 else
2064 return 512;
2065 case I915_FORMAT_MOD_Y_TILED:
2066 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2067 return 128;
2068 else
2069 return 512;
2070 case I915_FORMAT_MOD_Yf_TILED:
2071 switch (cpp) {
2072 case 1:
2073 return 64;
2074 case 2:
2075 case 4:
2076 return 128;
2077 case 8:
2078 case 16:
2079 return 256;
2080 default:
2081 MISSING_CASE(cpp);
2082 return cpp;
2083 }
2084 break;
2085 default:
2086 MISSING_CASE(fb_modifier);
2087 return cpp;
2088 }
2089}
2090
832be82f
VS
2091unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
2092 uint64_t fb_modifier, unsigned int cpp)
a57ce0b2 2093{
832be82f
VS
2094 if (fb_modifier == DRM_FORMAT_MOD_NONE)
2095 return 1;
2096 else
2097 return intel_tile_size(dev_priv) /
27ba3910 2098 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
6761dd31
TU
2099}
2100
8d0deca8
VS
2101/* Return the tile dimensions in pixel units */
2102static void intel_tile_dims(const struct drm_i915_private *dev_priv,
2103 unsigned int *tile_width,
2104 unsigned int *tile_height,
2105 uint64_t fb_modifier,
2106 unsigned int cpp)
2107{
2108 unsigned int tile_width_bytes =
2109 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
2110
2111 *tile_width = tile_width_bytes / cpp;
2112 *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
2113}
2114
6761dd31
TU
2115unsigned int
2116intel_fb_align_height(struct drm_device *dev, unsigned int height,
832be82f 2117 uint32_t pixel_format, uint64_t fb_modifier)
6761dd31 2118{
832be82f
VS
2119 unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
2120 unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
2121
2122 return ALIGN(height, tile_height);
a57ce0b2
JB
2123}
2124
1663b9d6
VS
2125unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2126{
2127 unsigned int size = 0;
2128 int i;
2129
2130 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2131 size += rot_info->plane[i].width * rot_info->plane[i].height;
2132
2133 return size;
2134}
2135
75c82a53 2136static void
3465c580
VS
2137intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2138 const struct drm_framebuffer *fb,
2139 unsigned int rotation)
f64b98cd 2140{
bd2ef25d 2141 if (drm_rotation_90_or_270(rotation)) {
2d7a215f
VS
2142 *view = i915_ggtt_view_rotated;
2143 view->params.rotated = to_intel_framebuffer(fb)->rot_info;
2144 } else {
2145 *view = i915_ggtt_view_normal;
2146 }
2147}
50470bb0 2148
603525d7 2149static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
4e9a86b6
VS
2150{
2151 if (INTEL_INFO(dev_priv)->gen >= 9)
2152 return 256 * 1024;
985b8bb4 2153 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
666a4537 2154 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
4e9a86b6
VS
2155 return 128 * 1024;
2156 else if (INTEL_INFO(dev_priv)->gen >= 4)
2157 return 4 * 1024;
2158 else
44c5905e 2159 return 0;
4e9a86b6
VS
2160}
2161
603525d7
VS
2162static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
2163 uint64_t fb_modifier)
2164{
2165 switch (fb_modifier) {
2166 case DRM_FORMAT_MOD_NONE:
2167 return intel_linear_alignment(dev_priv);
2168 case I915_FORMAT_MOD_X_TILED:
2169 if (INTEL_INFO(dev_priv)->gen >= 9)
2170 return 256 * 1024;
2171 return 0;
2172 case I915_FORMAT_MOD_Y_TILED:
2173 case I915_FORMAT_MOD_Yf_TILED:
2174 return 1 * 1024 * 1024;
2175 default:
2176 MISSING_CASE(fb_modifier);
2177 return 0;
2178 }
2179}
2180
058d88c4
CW
2181struct i915_vma *
2182intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
6b95a207 2183{
850c4cdc 2184 struct drm_device *dev = fb->dev;
fac5e23e 2185 struct drm_i915_private *dev_priv = to_i915(dev);
850c4cdc 2186 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2187 struct i915_ggtt_view view;
058d88c4 2188 struct i915_vma *vma;
6b95a207 2189 u32 alignment;
6b95a207 2190
ebcdd39e
MR
2191 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2192
bae781b2 2193 alignment = intel_surf_alignment(dev_priv, fb->modifier);
6b95a207 2194
3465c580 2195 intel_fill_fb_ggtt_view(&view, fb, rotation);
f64b98cd 2196
693db184
CW
2197 /* Note that the w/a also requires 64 PTE of padding following the
2198 * bo. We currently fill all unused PTE with the shadow page and so
2199 * we should always have valid PTE following the scanout preventing
2200 * the VT-d warning.
2201 */
48f112fe 2202 if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
693db184
CW
2203 alignment = 256 * 1024;
2204
d6dd6843
PZ
2205 /*
2206 * Global gtt pte registers are special registers which actually forward
2207 * writes to a chunk of system memory. Which means that there is no risk
2208 * that the register values disappear as soon as we call
2209 * intel_runtime_pm_put(), so it is correct to wrap only the
2210 * pin/unpin/fence and not more.
2211 */
2212 intel_runtime_pm_get(dev_priv);
2213
058d88c4 2214 vma = i915_gem_object_pin_to_display_plane(obj, alignment, &view);
49ef5294
CW
2215 if (IS_ERR(vma))
2216 goto err;
6b95a207 2217
05a20d09 2218 if (i915_vma_is_map_and_fenceable(vma)) {
49ef5294
CW
2219 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2220 * fence, whereas 965+ only requires a fence if using
2221 * framebuffer compression. For simplicity, we always, when
2222 * possible, install a fence as the cost is not that onerous.
2223 *
2224 * If we fail to fence the tiled scanout, then either the
2225 * modeset will reject the change (which is highly unlikely as
2226 * the affected systems, all but one, do not have unmappable
2227 * space) or we will not be able to enable full powersaving
2228 * techniques (also likely not to apply due to various limits
2229 * FBC and the like impose on the size of the buffer, which
2230 * presumably we violated anyway with this unmappable buffer).
2231 * Anyway, it is presumably better to stumble onwards with
2232 * something and try to run the system in a "less than optimal"
2233 * mode that matches the user configuration.
2234 */
2235 if (i915_vma_get_fence(vma) == 0)
2236 i915_vma_pin_fence(vma);
9807216f 2237 }
6b95a207 2238
49ef5294 2239err:
d6dd6843 2240 intel_runtime_pm_put(dev_priv);
058d88c4 2241 return vma;
6b95a207
KH
2242}
2243
fb4b8ce1 2244void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
1690e1eb 2245{
82bc3b2d 2246 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2247 struct i915_ggtt_view view;
058d88c4 2248 struct i915_vma *vma;
82bc3b2d 2249
ebcdd39e
MR
2250 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2251
3465c580 2252 intel_fill_fb_ggtt_view(&view, fb, rotation);
05a20d09 2253 vma = i915_gem_object_to_ggtt(obj, &view);
f64b98cd 2254
49ef5294 2255 i915_vma_unpin_fence(vma);
058d88c4 2256 i915_gem_object_unpin_from_display_plane(vma);
1690e1eb
CW
2257}
2258
ef78ec94
VS
2259static int intel_fb_pitch(const struct drm_framebuffer *fb, int plane,
2260 unsigned int rotation)
2261{
bd2ef25d 2262 if (drm_rotation_90_or_270(rotation))
ef78ec94
VS
2263 return to_intel_framebuffer(fb)->rotated[plane].pitch;
2264 else
2265 return fb->pitches[plane];
2266}
2267
6687c906
VS
2268/*
2269 * Convert the x/y offsets into a linear offset.
2270 * Only valid with 0/180 degree rotation, which is fine since linear
2271 * offset is only used with linear buffers on pre-hsw and tiled buffers
2272 * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
2273 */
2274u32 intel_fb_xy_to_linear(int x, int y,
2949056c
VS
2275 const struct intel_plane_state *state,
2276 int plane)
6687c906 2277{
2949056c 2278 const struct drm_framebuffer *fb = state->base.fb;
6687c906
VS
2279 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
2280 unsigned int pitch = fb->pitches[plane];
2281
2282 return y * pitch + x * cpp;
2283}
2284
2285/*
2286 * Add the x/y offsets derived from fb->offsets[] to the user
2287 * specified plane src x/y offsets. The resulting x/y offsets
2288 * specify the start of scanout from the beginning of the gtt mapping.
2289 */
2290void intel_add_fb_offsets(int *x, int *y,
2949056c
VS
2291 const struct intel_plane_state *state,
2292 int plane)
6687c906
VS
2293
2294{
2949056c
VS
2295 const struct intel_framebuffer *intel_fb = to_intel_framebuffer(state->base.fb);
2296 unsigned int rotation = state->base.rotation;
6687c906 2297
bd2ef25d 2298 if (drm_rotation_90_or_270(rotation)) {
6687c906
VS
2299 *x += intel_fb->rotated[plane].x;
2300 *y += intel_fb->rotated[plane].y;
2301 } else {
2302 *x += intel_fb->normal[plane].x;
2303 *y += intel_fb->normal[plane].y;
2304 }
2305}
2306
29cf9491 2307/*
29cf9491
VS
2308 * Input tile dimensions and pitch must already be
2309 * rotated to match x and y, and in pixel units.
2310 */
66a2d927
VS
2311static u32 _intel_adjust_tile_offset(int *x, int *y,
2312 unsigned int tile_width,
2313 unsigned int tile_height,
2314 unsigned int tile_size,
2315 unsigned int pitch_tiles,
2316 u32 old_offset,
2317 u32 new_offset)
29cf9491 2318{
b9b24038 2319 unsigned int pitch_pixels = pitch_tiles * tile_width;
29cf9491
VS
2320 unsigned int tiles;
2321
2322 WARN_ON(old_offset & (tile_size - 1));
2323 WARN_ON(new_offset & (tile_size - 1));
2324 WARN_ON(new_offset > old_offset);
2325
2326 tiles = (old_offset - new_offset) / tile_size;
2327
2328 *y += tiles / pitch_tiles * tile_height;
2329 *x += tiles % pitch_tiles * tile_width;
2330
b9b24038
VS
2331 /* minimize x in case it got needlessly big */
2332 *y += *x / pitch_pixels * tile_height;
2333 *x %= pitch_pixels;
2334
29cf9491
VS
2335 return new_offset;
2336}
2337
66a2d927
VS
2338/*
2339 * Adjust the tile offset by moving the difference into
2340 * the x/y offsets.
2341 */
2342static u32 intel_adjust_tile_offset(int *x, int *y,
2343 const struct intel_plane_state *state, int plane,
2344 u32 old_offset, u32 new_offset)
2345{
2346 const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
2347 const struct drm_framebuffer *fb = state->base.fb;
2348 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
2349 unsigned int rotation = state->base.rotation;
2350 unsigned int pitch = intel_fb_pitch(fb, plane, rotation);
2351
2352 WARN_ON(new_offset > old_offset);
2353
bae781b2 2354 if (fb->modifier != DRM_FORMAT_MOD_NONE) {
66a2d927
VS
2355 unsigned int tile_size, tile_width, tile_height;
2356 unsigned int pitch_tiles;
2357
2358 tile_size = intel_tile_size(dev_priv);
2359 intel_tile_dims(dev_priv, &tile_width, &tile_height,
bae781b2 2360 fb->modifier, cpp);
66a2d927 2361
bd2ef25d 2362 if (drm_rotation_90_or_270(rotation)) {
66a2d927
VS
2363 pitch_tiles = pitch / tile_height;
2364 swap(tile_width, tile_height);
2365 } else {
2366 pitch_tiles = pitch / (tile_width * cpp);
2367 }
2368
2369 _intel_adjust_tile_offset(x, y, tile_width, tile_height,
2370 tile_size, pitch_tiles,
2371 old_offset, new_offset);
2372 } else {
2373 old_offset += *y * pitch + *x * cpp;
2374
2375 *y = (old_offset - new_offset) / pitch;
2376 *x = ((old_offset - new_offset) - *y * pitch) / cpp;
2377 }
2378
2379 return new_offset;
2380}
2381
8d0deca8
VS
2382/*
2383 * Computes the linear offset to the base tile and adjusts
2384 * x, y. bytes per pixel is assumed to be a power-of-two.
2385 *
2386 * In the 90/270 rotated case, x and y are assumed
2387 * to be already rotated to match the rotated GTT view, and
2388 * pitch is the tile_height aligned framebuffer height.
6687c906
VS
2389 *
2390 * This function is used when computing the derived information
2391 * under intel_framebuffer, so using any of that information
2392 * here is not allowed. Anything under drm_framebuffer can be
2393 * used. This is why the user has to pass in the pitch since it
2394 * is specified in the rotated orientation.
8d0deca8 2395 */
6687c906
VS
2396static u32 _intel_compute_tile_offset(const struct drm_i915_private *dev_priv,
2397 int *x, int *y,
2398 const struct drm_framebuffer *fb, int plane,
2399 unsigned int pitch,
2400 unsigned int rotation,
2401 u32 alignment)
c2c75131 2402{
bae781b2 2403 uint64_t fb_modifier = fb->modifier;
4f2d9934 2404 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
6687c906 2405 u32 offset, offset_aligned;
29cf9491 2406
29cf9491
VS
2407 if (alignment)
2408 alignment--;
2409
b5c65338 2410 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
8d0deca8
VS
2411 unsigned int tile_size, tile_width, tile_height;
2412 unsigned int tile_rows, tiles, pitch_tiles;
c2c75131 2413
d843310d 2414 tile_size = intel_tile_size(dev_priv);
8d0deca8
VS
2415 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2416 fb_modifier, cpp);
2417
bd2ef25d 2418 if (drm_rotation_90_or_270(rotation)) {
8d0deca8
VS
2419 pitch_tiles = pitch / tile_height;
2420 swap(tile_width, tile_height);
2421 } else {
2422 pitch_tiles = pitch / (tile_width * cpp);
2423 }
d843310d
VS
2424
2425 tile_rows = *y / tile_height;
2426 *y %= tile_height;
c2c75131 2427
8d0deca8
VS
2428 tiles = *x / tile_width;
2429 *x %= tile_width;
bc752862 2430
29cf9491
VS
2431 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2432 offset_aligned = offset & ~alignment;
bc752862 2433
66a2d927
VS
2434 _intel_adjust_tile_offset(x, y, tile_width, tile_height,
2435 tile_size, pitch_tiles,
2436 offset, offset_aligned);
29cf9491 2437 } else {
bc752862 2438 offset = *y * pitch + *x * cpp;
29cf9491
VS
2439 offset_aligned = offset & ~alignment;
2440
4e9a86b6
VS
2441 *y = (offset & alignment) / pitch;
2442 *x = ((offset & alignment) - *y * pitch) / cpp;
bc752862 2443 }
29cf9491
VS
2444
2445 return offset_aligned;
c2c75131
DV
2446}
2447
6687c906 2448u32 intel_compute_tile_offset(int *x, int *y,
2949056c
VS
2449 const struct intel_plane_state *state,
2450 int plane)
6687c906 2451{
2949056c
VS
2452 const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
2453 const struct drm_framebuffer *fb = state->base.fb;
2454 unsigned int rotation = state->base.rotation;
ef78ec94 2455 int pitch = intel_fb_pitch(fb, plane, rotation);
8d970654
VS
2456 u32 alignment;
2457
2458 /* AUX_DIST needs only 4K alignment */
2459 if (fb->pixel_format == DRM_FORMAT_NV12 && plane == 1)
2460 alignment = 4096;
2461 else
bae781b2 2462 alignment = intel_surf_alignment(dev_priv, fb->modifier);
6687c906
VS
2463
2464 return _intel_compute_tile_offset(dev_priv, x, y, fb, plane, pitch,
2465 rotation, alignment);
2466}
2467
2468/* Convert the fb->offset[] linear offset into x/y offsets */
2469static void intel_fb_offset_to_xy(int *x, int *y,
2470 const struct drm_framebuffer *fb, int plane)
2471{
2472 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
2473 unsigned int pitch = fb->pitches[plane];
2474 u32 linear_offset = fb->offsets[plane];
2475
2476 *y = linear_offset / pitch;
2477 *x = linear_offset % pitch / cpp;
2478}
2479
72618ebf
VS
2480static unsigned int intel_fb_modifier_to_tiling(uint64_t fb_modifier)
2481{
2482 switch (fb_modifier) {
2483 case I915_FORMAT_MOD_X_TILED:
2484 return I915_TILING_X;
2485 case I915_FORMAT_MOD_Y_TILED:
2486 return I915_TILING_Y;
2487 default:
2488 return I915_TILING_NONE;
2489 }
2490}
2491
6687c906
VS
2492static int
2493intel_fill_fb_info(struct drm_i915_private *dev_priv,
2494 struct drm_framebuffer *fb)
2495{
2496 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
2497 struct intel_rotation_info *rot_info = &intel_fb->rot_info;
2498 u32 gtt_offset_rotated = 0;
2499 unsigned int max_size = 0;
2500 uint32_t format = fb->pixel_format;
2501 int i, num_planes = drm_format_num_planes(format);
2502 unsigned int tile_size = intel_tile_size(dev_priv);
2503
2504 for (i = 0; i < num_planes; i++) {
2505 unsigned int width, height;
2506 unsigned int cpp, size;
2507 u32 offset;
2508 int x, y;
2509
2510 cpp = drm_format_plane_cpp(format, i);
2511 width = drm_format_plane_width(fb->width, format, i);
2512 height = drm_format_plane_height(fb->height, format, i);
2513
2514 intel_fb_offset_to_xy(&x, &y, fb, i);
2515
60d5f2a4
VS
2516 /*
2517 * The fence (if used) is aligned to the start of the object
2518 * so having the framebuffer wrap around across the edge of the
2519 * fenced region doesn't really work. We have no API to configure
2520 * the fence start offset within the object (nor could we probably
2521 * on gen2/3). So it's just easier if we just require that the
2522 * fb layout agrees with the fence layout. We already check that the
2523 * fb stride matches the fence stride elsewhere.
2524 */
2525 if (i915_gem_object_is_tiled(intel_fb->obj) &&
2526 (x + width) * cpp > fb->pitches[i]) {
2527 DRM_DEBUG("bad fb plane %d offset: 0x%x\n",
2528 i, fb->offsets[i]);
2529 return -EINVAL;
2530 }
2531
6687c906
VS
2532 /*
2533 * First pixel of the framebuffer from
2534 * the start of the normal gtt mapping.
2535 */
2536 intel_fb->normal[i].x = x;
2537 intel_fb->normal[i].y = y;
2538
2539 offset = _intel_compute_tile_offset(dev_priv, &x, &y,
2540 fb, 0, fb->pitches[i],
cc926387 2541 DRM_ROTATE_0, tile_size);
6687c906
VS
2542 offset /= tile_size;
2543
bae781b2 2544 if (fb->modifier != DRM_FORMAT_MOD_NONE) {
6687c906
VS
2545 unsigned int tile_width, tile_height;
2546 unsigned int pitch_tiles;
2547 struct drm_rect r;
2548
2549 intel_tile_dims(dev_priv, &tile_width, &tile_height,
bae781b2 2550 fb->modifier, cpp);
6687c906
VS
2551
2552 rot_info->plane[i].offset = offset;
2553 rot_info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i], tile_width * cpp);
2554 rot_info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
2555 rot_info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
2556
2557 intel_fb->rotated[i].pitch =
2558 rot_info->plane[i].height * tile_height;
2559
2560 /* how many tiles does this plane need */
2561 size = rot_info->plane[i].stride * rot_info->plane[i].height;
2562 /*
2563 * If the plane isn't horizontally tile aligned,
2564 * we need one more tile.
2565 */
2566 if (x != 0)
2567 size++;
2568
2569 /* rotate the x/y offsets to match the GTT view */
2570 r.x1 = x;
2571 r.y1 = y;
2572 r.x2 = x + width;
2573 r.y2 = y + height;
2574 drm_rect_rotate(&r,
2575 rot_info->plane[i].width * tile_width,
2576 rot_info->plane[i].height * tile_height,
cc926387 2577 DRM_ROTATE_270);
6687c906
VS
2578 x = r.x1;
2579 y = r.y1;
2580
2581 /* rotate the tile dimensions to match the GTT view */
2582 pitch_tiles = intel_fb->rotated[i].pitch / tile_height;
2583 swap(tile_width, tile_height);
2584
2585 /*
2586 * We only keep the x/y offsets, so push all of the
2587 * gtt offset into the x/y offsets.
2588 */
66a2d927
VS
2589 _intel_adjust_tile_offset(&x, &y, tile_size,
2590 tile_width, tile_height, pitch_tiles,
2591 gtt_offset_rotated * tile_size, 0);
6687c906
VS
2592
2593 gtt_offset_rotated += rot_info->plane[i].width * rot_info->plane[i].height;
2594
2595 /*
2596 * First pixel of the framebuffer from
2597 * the start of the rotated gtt mapping.
2598 */
2599 intel_fb->rotated[i].x = x;
2600 intel_fb->rotated[i].y = y;
2601 } else {
2602 size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
2603 x * cpp, tile_size);
2604 }
2605
2606 /* how many tiles in total needed in the bo */
2607 max_size = max(max_size, offset + size);
2608 }
2609
2610 if (max_size * tile_size > to_intel_framebuffer(fb)->obj->base.size) {
2611 DRM_DEBUG("fb too big for bo (need %u bytes, have %zu bytes)\n",
2612 max_size * tile_size, to_intel_framebuffer(fb)->obj->base.size);
2613 return -EINVAL;
2614 }
2615
2616 return 0;
2617}
2618
b35d63fa 2619static int i9xx_format_to_fourcc(int format)
46f297fb
JB
2620{
2621 switch (format) {
2622 case DISPPLANE_8BPP:
2623 return DRM_FORMAT_C8;
2624 case DISPPLANE_BGRX555:
2625 return DRM_FORMAT_XRGB1555;
2626 case DISPPLANE_BGRX565:
2627 return DRM_FORMAT_RGB565;
2628 default:
2629 case DISPPLANE_BGRX888:
2630 return DRM_FORMAT_XRGB8888;
2631 case DISPPLANE_RGBX888:
2632 return DRM_FORMAT_XBGR8888;
2633 case DISPPLANE_BGRX101010:
2634 return DRM_FORMAT_XRGB2101010;
2635 case DISPPLANE_RGBX101010:
2636 return DRM_FORMAT_XBGR2101010;
2637 }
2638}
2639
bc8d7dff
DL
2640static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2641{
2642 switch (format) {
2643 case PLANE_CTL_FORMAT_RGB_565:
2644 return DRM_FORMAT_RGB565;
2645 default:
2646 case PLANE_CTL_FORMAT_XRGB_8888:
2647 if (rgb_order) {
2648 if (alpha)
2649 return DRM_FORMAT_ABGR8888;
2650 else
2651 return DRM_FORMAT_XBGR8888;
2652 } else {
2653 if (alpha)
2654 return DRM_FORMAT_ARGB8888;
2655 else
2656 return DRM_FORMAT_XRGB8888;
2657 }
2658 case PLANE_CTL_FORMAT_XRGB_2101010:
2659 if (rgb_order)
2660 return DRM_FORMAT_XBGR2101010;
2661 else
2662 return DRM_FORMAT_XRGB2101010;
2663 }
2664}
2665
5724dbd1 2666static bool
f6936e29
DV
2667intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2668 struct intel_initial_plane_config *plane_config)
46f297fb
JB
2669{
2670 struct drm_device *dev = crtc->base.dev;
3badb49f 2671 struct drm_i915_private *dev_priv = to_i915(dev);
72e96d64 2672 struct i915_ggtt *ggtt = &dev_priv->ggtt;
46f297fb
JB
2673 struct drm_i915_gem_object *obj = NULL;
2674 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2d14030b 2675 struct drm_framebuffer *fb = &plane_config->fb->base;
f37b5c2b
DV
2676 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2677 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2678 PAGE_SIZE);
2679
2680 size_aligned -= base_aligned;
46f297fb 2681
ff2652ea
CW
2682 if (plane_config->size == 0)
2683 return false;
2684
3badb49f
PZ
2685 /* If the FB is too big, just don't use it since fbdev is not very
2686 * important and we should probably use that space with FBC or other
2687 * features. */
72e96d64 2688 if (size_aligned * 2 > ggtt->stolen_usable_size)
3badb49f
PZ
2689 return false;
2690
12c83d99
TU
2691 mutex_lock(&dev->struct_mutex);
2692
187685cb 2693 obj = i915_gem_object_create_stolen_for_preallocated(dev_priv,
f37b5c2b
DV
2694 base_aligned,
2695 base_aligned,
2696 size_aligned);
12c83d99
TU
2697 if (!obj) {
2698 mutex_unlock(&dev->struct_mutex);
484b41dd 2699 return false;
12c83d99 2700 }
46f297fb 2701
3e510a8e
CW
2702 if (plane_config->tiling == I915_TILING_X)
2703 obj->tiling_and_stride = fb->pitches[0] | I915_TILING_X;
46f297fb 2704
6bf129df
DL
2705 mode_cmd.pixel_format = fb->pixel_format;
2706 mode_cmd.width = fb->width;
2707 mode_cmd.height = fb->height;
2708 mode_cmd.pitches[0] = fb->pitches[0];
bae781b2 2709 mode_cmd.modifier[0] = fb->modifier;
18c5247e 2710 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
46f297fb 2711
6bf129df 2712 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
484b41dd 2713 &mode_cmd, obj)) {
46f297fb
JB
2714 DRM_DEBUG_KMS("intel fb init failed\n");
2715 goto out_unref_obj;
2716 }
12c83d99 2717
46f297fb 2718 mutex_unlock(&dev->struct_mutex);
484b41dd 2719
f6936e29 2720 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
484b41dd 2721 return true;
46f297fb
JB
2722
2723out_unref_obj:
f8c417cd 2724 i915_gem_object_put(obj);
46f297fb 2725 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2726 return false;
2727}
2728
5a21b665
DV
2729/* Update plane->state->fb to match plane->fb after driver-internal updates */
2730static void
2731update_state_fb(struct drm_plane *plane)
2732{
2733 if (plane->fb == plane->state->fb)
2734 return;
2735
2736 if (plane->state->fb)
2737 drm_framebuffer_unreference(plane->state->fb);
2738 plane->state->fb = plane->fb;
2739 if (plane->state->fb)
2740 drm_framebuffer_reference(plane->state->fb);
2741}
2742
5724dbd1 2743static void
f6936e29
DV
2744intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2745 struct intel_initial_plane_config *plane_config)
484b41dd
JB
2746{
2747 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 2748 struct drm_i915_private *dev_priv = to_i915(dev);
484b41dd
JB
2749 struct drm_crtc *c;
2750 struct intel_crtc *i;
2ff8fde1 2751 struct drm_i915_gem_object *obj;
88595ac9 2752 struct drm_plane *primary = intel_crtc->base.primary;
be5651f2 2753 struct drm_plane_state *plane_state = primary->state;
200757f5
MR
2754 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2755 struct intel_plane *intel_plane = to_intel_plane(primary);
0a8d8a86
MR
2756 struct intel_plane_state *intel_state =
2757 to_intel_plane_state(plane_state);
88595ac9 2758 struct drm_framebuffer *fb;
484b41dd 2759
2d14030b 2760 if (!plane_config->fb)
484b41dd
JB
2761 return;
2762
f6936e29 2763 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
88595ac9
DV
2764 fb = &plane_config->fb->base;
2765 goto valid_fb;
f55548b5 2766 }
484b41dd 2767
2d14030b 2768 kfree(plane_config->fb);
484b41dd
JB
2769
2770 /*
2771 * Failed to alloc the obj, check to see if we should share
2772 * an fb with another CRTC instead
2773 */
70e1e0ec 2774 for_each_crtc(dev, c) {
484b41dd
JB
2775 i = to_intel_crtc(c);
2776
2777 if (c == &intel_crtc->base)
2778 continue;
2779
2ff8fde1
MR
2780 if (!i->active)
2781 continue;
2782
88595ac9
DV
2783 fb = c->primary->fb;
2784 if (!fb)
484b41dd
JB
2785 continue;
2786
88595ac9 2787 obj = intel_fb_obj(fb);
058d88c4 2788 if (i915_gem_object_ggtt_offset(obj, NULL) == plane_config->base) {
88595ac9
DV
2789 drm_framebuffer_reference(fb);
2790 goto valid_fb;
484b41dd
JB
2791 }
2792 }
88595ac9 2793
200757f5
MR
2794 /*
2795 * We've failed to reconstruct the BIOS FB. Current display state
2796 * indicates that the primary plane is visible, but has a NULL FB,
2797 * which will lead to problems later if we don't fix it up. The
2798 * simplest solution is to just disable the primary plane now and
2799 * pretend the BIOS never had it enabled.
2800 */
936e71e3 2801 to_intel_plane_state(plane_state)->base.visible = false;
200757f5 2802 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
2622a081 2803 intel_pre_disable_primary_noatomic(&intel_crtc->base);
200757f5
MR
2804 intel_plane->disable_plane(primary, &intel_crtc->base);
2805
88595ac9
DV
2806 return;
2807
2808valid_fb:
f44e2659
VS
2809 plane_state->src_x = 0;
2810 plane_state->src_y = 0;
be5651f2
ML
2811 plane_state->src_w = fb->width << 16;
2812 plane_state->src_h = fb->height << 16;
2813
f44e2659
VS
2814 plane_state->crtc_x = 0;
2815 plane_state->crtc_y = 0;
be5651f2
ML
2816 plane_state->crtc_w = fb->width;
2817 plane_state->crtc_h = fb->height;
2818
1638d30c
RC
2819 intel_state->base.src = drm_plane_state_src(plane_state);
2820 intel_state->base.dst = drm_plane_state_dest(plane_state);
0a8d8a86 2821
88595ac9 2822 obj = intel_fb_obj(fb);
3e510a8e 2823 if (i915_gem_object_is_tiled(obj))
88595ac9
DV
2824 dev_priv->preserve_bios_swizzle = true;
2825
be5651f2
ML
2826 drm_framebuffer_reference(fb);
2827 primary->fb = primary->state->fb = fb;
36750f28 2828 primary->crtc = primary->state->crtc = &intel_crtc->base;
36750f28 2829 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
faf5bf0a
CW
2830 atomic_or(to_intel_plane(primary)->frontbuffer_bit,
2831 &obj->frontbuffer_bits);
46f297fb
JB
2832}
2833
b63a16f6
VS
2834static int skl_max_plane_width(const struct drm_framebuffer *fb, int plane,
2835 unsigned int rotation)
2836{
2837 int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
2838
bae781b2 2839 switch (fb->modifier) {
b63a16f6
VS
2840 case DRM_FORMAT_MOD_NONE:
2841 case I915_FORMAT_MOD_X_TILED:
2842 switch (cpp) {
2843 case 8:
2844 return 4096;
2845 case 4:
2846 case 2:
2847 case 1:
2848 return 8192;
2849 default:
2850 MISSING_CASE(cpp);
2851 break;
2852 }
2853 break;
2854 case I915_FORMAT_MOD_Y_TILED:
2855 case I915_FORMAT_MOD_Yf_TILED:
2856 switch (cpp) {
2857 case 8:
2858 return 2048;
2859 case 4:
2860 return 4096;
2861 case 2:
2862 case 1:
2863 return 8192;
2864 default:
2865 MISSING_CASE(cpp);
2866 break;
2867 }
2868 break;
2869 default:
bae781b2 2870 MISSING_CASE(fb->modifier);
b63a16f6
VS
2871 }
2872
2873 return 2048;
2874}
2875
2876static int skl_check_main_surface(struct intel_plane_state *plane_state)
2877{
2878 const struct drm_i915_private *dev_priv = to_i915(plane_state->base.plane->dev);
2879 const struct drm_framebuffer *fb = plane_state->base.fb;
2880 unsigned int rotation = plane_state->base.rotation;
cc926387
DV
2881 int x = plane_state->base.src.x1 >> 16;
2882 int y = plane_state->base.src.y1 >> 16;
2883 int w = drm_rect_width(&plane_state->base.src) >> 16;
2884 int h = drm_rect_height(&plane_state->base.src) >> 16;
b63a16f6
VS
2885 int max_width = skl_max_plane_width(fb, 0, rotation);
2886 int max_height = 4096;
8d970654 2887 u32 alignment, offset, aux_offset = plane_state->aux.offset;
b63a16f6
VS
2888
2889 if (w > max_width || h > max_height) {
2890 DRM_DEBUG_KMS("requested Y/RGB source size %dx%d too big (limit %dx%d)\n",
2891 w, h, max_width, max_height);
2892 return -EINVAL;
2893 }
2894
2895 intel_add_fb_offsets(&x, &y, plane_state, 0);
2896 offset = intel_compute_tile_offset(&x, &y, plane_state, 0);
2897
bae781b2 2898 alignment = intel_surf_alignment(dev_priv, fb->modifier);
b63a16f6 2899
8d970654
VS
2900 /*
2901 * AUX surface offset is specified as the distance from the
2902 * main surface offset, and it must be non-negative. Make
2903 * sure that is what we will get.
2904 */
2905 if (offset > aux_offset)
2906 offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
2907 offset, aux_offset & ~(alignment - 1));
2908
b63a16f6
VS
2909 /*
2910 * When using an X-tiled surface, the plane blows up
2911 * if the x offset + width exceed the stride.
2912 *
2913 * TODO: linear and Y-tiled seem fine, Yf untested,
2914 */
bae781b2 2915 if (fb->modifier == I915_FORMAT_MOD_X_TILED) {
b63a16f6
VS
2916 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
2917
2918 while ((x + w) * cpp > fb->pitches[0]) {
2919 if (offset == 0) {
2920 DRM_DEBUG_KMS("Unable to find suitable display surface offset\n");
2921 return -EINVAL;
2922 }
2923
2924 offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
2925 offset, offset - alignment);
2926 }
2927 }
2928
2929 plane_state->main.offset = offset;
2930 plane_state->main.x = x;
2931 plane_state->main.y = y;
2932
2933 return 0;
2934}
2935
8d970654
VS
2936static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
2937{
2938 const struct drm_framebuffer *fb = plane_state->base.fb;
2939 unsigned int rotation = plane_state->base.rotation;
2940 int max_width = skl_max_plane_width(fb, 1, rotation);
2941 int max_height = 4096;
cc926387
DV
2942 int x = plane_state->base.src.x1 >> 17;
2943 int y = plane_state->base.src.y1 >> 17;
2944 int w = drm_rect_width(&plane_state->base.src) >> 17;
2945 int h = drm_rect_height(&plane_state->base.src) >> 17;
8d970654
VS
2946 u32 offset;
2947
2948 intel_add_fb_offsets(&x, &y, plane_state, 1);
2949 offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
2950
2951 /* FIXME not quite sure how/if these apply to the chroma plane */
2952 if (w > max_width || h > max_height) {
2953 DRM_DEBUG_KMS("CbCr source size %dx%d too big (limit %dx%d)\n",
2954 w, h, max_width, max_height);
2955 return -EINVAL;
2956 }
2957
2958 plane_state->aux.offset = offset;
2959 plane_state->aux.x = x;
2960 plane_state->aux.y = y;
2961
2962 return 0;
2963}
2964
b63a16f6
VS
2965int skl_check_plane_surface(struct intel_plane_state *plane_state)
2966{
2967 const struct drm_framebuffer *fb = plane_state->base.fb;
2968 unsigned int rotation = plane_state->base.rotation;
2969 int ret;
2970
2971 /* Rotate src coordinates to match rotated GTT view */
bd2ef25d 2972 if (drm_rotation_90_or_270(rotation))
cc926387 2973 drm_rect_rotate(&plane_state->base.src,
da064b47
VS
2974 fb->width << 16, fb->height << 16,
2975 DRM_ROTATE_270);
b63a16f6 2976
8d970654
VS
2977 /*
2978 * Handle the AUX surface first since
2979 * the main surface setup depends on it.
2980 */
2981 if (fb->pixel_format == DRM_FORMAT_NV12) {
2982 ret = skl_check_nv12_aux_surface(plane_state);
2983 if (ret)
2984 return ret;
2985 } else {
2986 plane_state->aux.offset = ~0xfff;
2987 plane_state->aux.x = 0;
2988 plane_state->aux.y = 0;
2989 }
2990
b63a16f6
VS
2991 ret = skl_check_main_surface(plane_state);
2992 if (ret)
2993 return ret;
2994
2995 return 0;
2996}
2997
a8d201af
ML
2998static void i9xx_update_primary_plane(struct drm_plane *primary,
2999 const struct intel_crtc_state *crtc_state,
3000 const struct intel_plane_state *plane_state)
81255565 3001{
6315b5d3 3002 struct drm_i915_private *dev_priv = to_i915(primary->dev);
a8d201af
ML
3003 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3004 struct drm_framebuffer *fb = plane_state->base.fb;
81255565 3005 int plane = intel_crtc->plane;
54ea9da8 3006 u32 linear_offset;
81255565 3007 u32 dspcntr;
f0f59a00 3008 i915_reg_t reg = DSPCNTR(plane);
8d0deca8 3009 unsigned int rotation = plane_state->base.rotation;
936e71e3
VS
3010 int x = plane_state->base.src.x1 >> 16;
3011 int y = plane_state->base.src.y1 >> 16;
c9ba6fad 3012
f45651ba
VS
3013 dspcntr = DISPPLANE_GAMMA_ENABLE;
3014
fdd508a6 3015 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba 3016
6315b5d3 3017 if (INTEL_GEN(dev_priv) < 4) {
f45651ba
VS
3018 if (intel_crtc->pipe == PIPE_B)
3019 dspcntr |= DISPPLANE_SEL_PIPE_B;
3020
3021 /* pipesrc and dspsize control the size that is scaled from,
3022 * which should always be the user's requested size.
3023 */
3024 I915_WRITE(DSPSIZE(plane),
a8d201af
ML
3025 ((crtc_state->pipe_src_h - 1) << 16) |
3026 (crtc_state->pipe_src_w - 1));
f45651ba 3027 I915_WRITE(DSPPOS(plane), 0);
920a14b2 3028 } else if (IS_CHERRYVIEW(dev_priv) && plane == PLANE_B) {
c14b0485 3029 I915_WRITE(PRIMSIZE(plane),
a8d201af
ML
3030 ((crtc_state->pipe_src_h - 1) << 16) |
3031 (crtc_state->pipe_src_w - 1));
c14b0485
VS
3032 I915_WRITE(PRIMPOS(plane), 0);
3033 I915_WRITE(PRIMCNSTALPHA(plane), 0);
f45651ba 3034 }
81255565 3035
57779d06
VS
3036 switch (fb->pixel_format) {
3037 case DRM_FORMAT_C8:
81255565
JB
3038 dspcntr |= DISPPLANE_8BPP;
3039 break;
57779d06 3040 case DRM_FORMAT_XRGB1555:
57779d06 3041 dspcntr |= DISPPLANE_BGRX555;
81255565 3042 break;
57779d06
VS
3043 case DRM_FORMAT_RGB565:
3044 dspcntr |= DISPPLANE_BGRX565;
3045 break;
3046 case DRM_FORMAT_XRGB8888:
57779d06
VS
3047 dspcntr |= DISPPLANE_BGRX888;
3048 break;
3049 case DRM_FORMAT_XBGR8888:
57779d06
VS
3050 dspcntr |= DISPPLANE_RGBX888;
3051 break;
3052 case DRM_FORMAT_XRGB2101010:
57779d06
VS
3053 dspcntr |= DISPPLANE_BGRX101010;
3054 break;
3055 case DRM_FORMAT_XBGR2101010:
57779d06 3056 dspcntr |= DISPPLANE_RGBX101010;
81255565
JB
3057 break;
3058 default:
baba133a 3059 BUG();
81255565 3060 }
57779d06 3061
72618ebf 3062 if (INTEL_GEN(dev_priv) >= 4 &&
bae781b2 3063 fb->modifier == I915_FORMAT_MOD_X_TILED)
f45651ba 3064 dspcntr |= DISPPLANE_TILED;
81255565 3065
df0cd455
VS
3066 if (rotation & DRM_ROTATE_180)
3067 dspcntr |= DISPPLANE_ROTATE_180;
3068
4ea7be2b
VS
3069 if (rotation & DRM_REFLECT_X)
3070 dspcntr |= DISPPLANE_MIRROR;
3071
9beb5fea 3072 if (IS_G4X(dev_priv))
de1aa629
VS
3073 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
3074
2949056c 3075 intel_add_fb_offsets(&x, &y, plane_state, 0);
81255565 3076
6315b5d3 3077 if (INTEL_GEN(dev_priv) >= 4)
c2c75131 3078 intel_crtc->dspaddr_offset =
2949056c 3079 intel_compute_tile_offset(&x, &y, plane_state, 0);
e506a0c6 3080
f22aa143 3081 if (rotation & DRM_ROTATE_180) {
df0cd455
VS
3082 x += crtc_state->pipe_src_w - 1;
3083 y += crtc_state->pipe_src_h - 1;
4ea7be2b
VS
3084 } else if (rotation & DRM_REFLECT_X) {
3085 x += crtc_state->pipe_src_w - 1;
48404c1e
SJ
3086 }
3087
2949056c 3088 linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
6687c906 3089
6315b5d3 3090 if (INTEL_GEN(dev_priv) < 4)
6687c906
VS
3091 intel_crtc->dspaddr_offset = linear_offset;
3092
2db3366b
PZ
3093 intel_crtc->adjusted_x = x;
3094 intel_crtc->adjusted_y = y;
3095
48404c1e
SJ
3096 I915_WRITE(reg, dspcntr);
3097
01f2c773 3098 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
6315b5d3 3099 if (INTEL_GEN(dev_priv) >= 4) {
85ba7b7d 3100 I915_WRITE(DSPSURF(plane),
6687c906
VS
3101 intel_fb_gtt_offset(fb, rotation) +
3102 intel_crtc->dspaddr_offset);
5eddb70b 3103 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
e506a0c6 3104 I915_WRITE(DSPLINOFF(plane), linear_offset);
bfb81049
VS
3105 } else {
3106 I915_WRITE(DSPADDR(plane),
3107 intel_fb_gtt_offset(fb, rotation) +
3108 intel_crtc->dspaddr_offset);
3109 }
5eddb70b 3110 POSTING_READ(reg);
17638cd6
JB
3111}
3112
a8d201af
ML
3113static void i9xx_disable_primary_plane(struct drm_plane *primary,
3114 struct drm_crtc *crtc)
17638cd6
JB
3115{
3116 struct drm_device *dev = crtc->dev;
fac5e23e 3117 struct drm_i915_private *dev_priv = to_i915(dev);
17638cd6 3118 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
17638cd6 3119 int plane = intel_crtc->plane;
f45651ba 3120
a8d201af
ML
3121 I915_WRITE(DSPCNTR(plane), 0);
3122 if (INTEL_INFO(dev_priv)->gen >= 4)
fdd508a6 3123 I915_WRITE(DSPSURF(plane), 0);
a8d201af
ML
3124 else
3125 I915_WRITE(DSPADDR(plane), 0);
3126 POSTING_READ(DSPCNTR(plane));
3127}
c9ba6fad 3128
a8d201af
ML
3129static void ironlake_update_primary_plane(struct drm_plane *primary,
3130 const struct intel_crtc_state *crtc_state,
3131 const struct intel_plane_state *plane_state)
3132{
3133 struct drm_device *dev = primary->dev;
fac5e23e 3134 struct drm_i915_private *dev_priv = to_i915(dev);
a8d201af
ML
3135 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3136 struct drm_framebuffer *fb = plane_state->base.fb;
a8d201af 3137 int plane = intel_crtc->plane;
54ea9da8 3138 u32 linear_offset;
a8d201af
ML
3139 u32 dspcntr;
3140 i915_reg_t reg = DSPCNTR(plane);
8d0deca8 3141 unsigned int rotation = plane_state->base.rotation;
936e71e3
VS
3142 int x = plane_state->base.src.x1 >> 16;
3143 int y = plane_state->base.src.y1 >> 16;
c9ba6fad 3144
f45651ba 3145 dspcntr = DISPPLANE_GAMMA_ENABLE;
fdd508a6 3146 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba 3147
8652744b 3148 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
f45651ba 3149 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
17638cd6 3150
57779d06
VS
3151 switch (fb->pixel_format) {
3152 case DRM_FORMAT_C8:
17638cd6
JB
3153 dspcntr |= DISPPLANE_8BPP;
3154 break;
57779d06
VS
3155 case DRM_FORMAT_RGB565:
3156 dspcntr |= DISPPLANE_BGRX565;
17638cd6 3157 break;
57779d06 3158 case DRM_FORMAT_XRGB8888:
57779d06
VS
3159 dspcntr |= DISPPLANE_BGRX888;
3160 break;
3161 case DRM_FORMAT_XBGR8888:
57779d06
VS
3162 dspcntr |= DISPPLANE_RGBX888;
3163 break;
3164 case DRM_FORMAT_XRGB2101010:
57779d06
VS
3165 dspcntr |= DISPPLANE_BGRX101010;
3166 break;
3167 case DRM_FORMAT_XBGR2101010:
57779d06 3168 dspcntr |= DISPPLANE_RGBX101010;
17638cd6
JB
3169 break;
3170 default:
baba133a 3171 BUG();
17638cd6
JB
3172 }
3173
bae781b2 3174 if (fb->modifier == I915_FORMAT_MOD_X_TILED)
17638cd6 3175 dspcntr |= DISPPLANE_TILED;
17638cd6 3176
df0cd455
VS
3177 if (rotation & DRM_ROTATE_180)
3178 dspcntr |= DISPPLANE_ROTATE_180;
3179
8652744b 3180 if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv))
1f5d76db 3181 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
17638cd6 3182
2949056c 3183 intel_add_fb_offsets(&x, &y, plane_state, 0);
6687c906 3184
c2c75131 3185 intel_crtc->dspaddr_offset =
2949056c 3186 intel_compute_tile_offset(&x, &y, plane_state, 0);
6687c906 3187
df0cd455
VS
3188 /* HSW+ does this automagically in hardware */
3189 if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv) &&
3190 rotation & DRM_ROTATE_180) {
3191 x += crtc_state->pipe_src_w - 1;
3192 y += crtc_state->pipe_src_h - 1;
48404c1e
SJ
3193 }
3194
2949056c 3195 linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
6687c906 3196
2db3366b
PZ
3197 intel_crtc->adjusted_x = x;
3198 intel_crtc->adjusted_y = y;
3199
48404c1e 3200 I915_WRITE(reg, dspcntr);
17638cd6 3201
01f2c773 3202 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
85ba7b7d 3203 I915_WRITE(DSPSURF(plane),
6687c906
VS
3204 intel_fb_gtt_offset(fb, rotation) +
3205 intel_crtc->dspaddr_offset);
8652744b 3206 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
bc1c91eb
DL
3207 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
3208 } else {
3209 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
3210 I915_WRITE(DSPLINOFF(plane), linear_offset);
3211 }
17638cd6 3212 POSTING_READ(reg);
17638cd6
JB
3213}
3214
7b49f948
VS
3215u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
3216 uint64_t fb_modifier, uint32_t pixel_format)
b321803d 3217{
7b49f948 3218 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
b321803d 3219 return 64;
7b49f948
VS
3220 } else {
3221 int cpp = drm_format_plane_cpp(pixel_format, 0);
3222
27ba3910 3223 return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
b321803d
DL
3224 }
3225}
3226
6687c906
VS
3227u32 intel_fb_gtt_offset(struct drm_framebuffer *fb,
3228 unsigned int rotation)
121920fa 3229{
6687c906 3230 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
ce7f1728 3231 struct i915_ggtt_view view;
058d88c4 3232 struct i915_vma *vma;
121920fa 3233
6687c906 3234 intel_fill_fb_ggtt_view(&view, fb, rotation);
dedf278c 3235
058d88c4
CW
3236 vma = i915_gem_object_to_ggtt(obj, &view);
3237 if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
3238 view.type))
3239 return -1;
3240
bde13ebd 3241 return i915_ggtt_offset(vma);
121920fa
TU
3242}
3243
e435d6e5
ML
3244static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
3245{
3246 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 3247 struct drm_i915_private *dev_priv = to_i915(dev);
e435d6e5
ML
3248
3249 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
3250 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
3251 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
e435d6e5
ML
3252}
3253
a1b2278e
CK
3254/*
3255 * This function detaches (aka. unbinds) unused scalers in hardware
3256 */
0583236e 3257static void skl_detach_scalers(struct intel_crtc *intel_crtc)
a1b2278e 3258{
a1b2278e
CK
3259 struct intel_crtc_scaler_state *scaler_state;
3260 int i;
3261
a1b2278e
CK
3262 scaler_state = &intel_crtc->config->scaler_state;
3263
3264 /* loop through and disable scalers that aren't in use */
3265 for (i = 0; i < intel_crtc->num_scalers; i++) {
e435d6e5
ML
3266 if (!scaler_state->scalers[i].in_use)
3267 skl_detach_scaler(intel_crtc, i);
a1b2278e
CK
3268 }
3269}
3270
d2196774
VS
3271u32 skl_plane_stride(const struct drm_framebuffer *fb, int plane,
3272 unsigned int rotation)
3273{
3274 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
3275 u32 stride = intel_fb_pitch(fb, plane, rotation);
3276
3277 /*
3278 * The stride is either expressed as a multiple of 64 bytes chunks for
3279 * linear buffers or in number of tiles for tiled buffers.
3280 */
bd2ef25d 3281 if (drm_rotation_90_or_270(rotation)) {
d2196774
VS
3282 int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
3283
bae781b2 3284 stride /= intel_tile_height(dev_priv, fb->modifier, cpp);
d2196774 3285 } else {
bae781b2 3286 stride /= intel_fb_stride_alignment(dev_priv, fb->modifier,
d2196774
VS
3287 fb->pixel_format);
3288 }
3289
3290 return stride;
3291}
3292
6156a456 3293u32 skl_plane_ctl_format(uint32_t pixel_format)
70d21f0e 3294{
6156a456 3295 switch (pixel_format) {
d161cf7a 3296 case DRM_FORMAT_C8:
c34ce3d1 3297 return PLANE_CTL_FORMAT_INDEXED;
70d21f0e 3298 case DRM_FORMAT_RGB565:
c34ce3d1 3299 return PLANE_CTL_FORMAT_RGB_565;
70d21f0e 3300 case DRM_FORMAT_XBGR8888:
c34ce3d1 3301 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
6156a456 3302 case DRM_FORMAT_XRGB8888:
c34ce3d1 3303 return PLANE_CTL_FORMAT_XRGB_8888;
6156a456
CK
3304 /*
3305 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
3306 * to be already pre-multiplied. We need to add a knob (or a different
3307 * DRM_FORMAT) for user-space to configure that.
3308 */
f75fb42a 3309 case DRM_FORMAT_ABGR8888:
c34ce3d1 3310 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
6156a456 3311 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
6156a456 3312 case DRM_FORMAT_ARGB8888:
c34ce3d1 3313 return PLANE_CTL_FORMAT_XRGB_8888 |
6156a456 3314 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
70d21f0e 3315 case DRM_FORMAT_XRGB2101010:
c34ce3d1 3316 return PLANE_CTL_FORMAT_XRGB_2101010;
70d21f0e 3317 case DRM_FORMAT_XBGR2101010:
c34ce3d1 3318 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
6156a456 3319 case DRM_FORMAT_YUYV:
c34ce3d1 3320 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
6156a456 3321 case DRM_FORMAT_YVYU:
c34ce3d1 3322 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
6156a456 3323 case DRM_FORMAT_UYVY:
c34ce3d1 3324 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
6156a456 3325 case DRM_FORMAT_VYUY:
c34ce3d1 3326 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
70d21f0e 3327 default:
4249eeef 3328 MISSING_CASE(pixel_format);
70d21f0e 3329 }
8cfcba41 3330
c34ce3d1 3331 return 0;
6156a456 3332}
70d21f0e 3333
6156a456
CK
3334u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
3335{
6156a456 3336 switch (fb_modifier) {
30af77c4 3337 case DRM_FORMAT_MOD_NONE:
70d21f0e 3338 break;
30af77c4 3339 case I915_FORMAT_MOD_X_TILED:
c34ce3d1 3340 return PLANE_CTL_TILED_X;
b321803d 3341 case I915_FORMAT_MOD_Y_TILED:
c34ce3d1 3342 return PLANE_CTL_TILED_Y;
b321803d 3343 case I915_FORMAT_MOD_Yf_TILED:
c34ce3d1 3344 return PLANE_CTL_TILED_YF;
70d21f0e 3345 default:
6156a456 3346 MISSING_CASE(fb_modifier);
70d21f0e 3347 }
8cfcba41 3348
c34ce3d1 3349 return 0;
6156a456 3350}
70d21f0e 3351
6156a456
CK
3352u32 skl_plane_ctl_rotation(unsigned int rotation)
3353{
3b7a5119 3354 switch (rotation) {
31ad61e4 3355 case DRM_ROTATE_0:
6156a456 3356 break;
1e8df167
SJ
3357 /*
3358 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
3359 * while i915 HW rotation is clockwise, thats why this swapping.
3360 */
31ad61e4 3361 case DRM_ROTATE_90:
1e8df167 3362 return PLANE_CTL_ROTATE_270;
31ad61e4 3363 case DRM_ROTATE_180:
c34ce3d1 3364 return PLANE_CTL_ROTATE_180;
31ad61e4 3365 case DRM_ROTATE_270:
1e8df167 3366 return PLANE_CTL_ROTATE_90;
6156a456
CK
3367 default:
3368 MISSING_CASE(rotation);
3369 }
3370
c34ce3d1 3371 return 0;
6156a456
CK
3372}
3373
a8d201af
ML
3374static void skylake_update_primary_plane(struct drm_plane *plane,
3375 const struct intel_crtc_state *crtc_state,
3376 const struct intel_plane_state *plane_state)
6156a456 3377{
a8d201af 3378 struct drm_device *dev = plane->dev;
fac5e23e 3379 struct drm_i915_private *dev_priv = to_i915(dev);
a8d201af
ML
3380 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3381 struct drm_framebuffer *fb = plane_state->base.fb;
8e816bb4
VS
3382 enum plane_id plane_id = to_intel_plane(plane)->id;
3383 enum pipe pipe = to_intel_plane(plane)->pipe;
d2196774 3384 u32 plane_ctl;
a8d201af 3385 unsigned int rotation = plane_state->base.rotation;
d2196774 3386 u32 stride = skl_plane_stride(fb, 0, rotation);
b63a16f6 3387 u32 surf_addr = plane_state->main.offset;
a8d201af 3388 int scaler_id = plane_state->scaler_id;
b63a16f6
VS
3389 int src_x = plane_state->main.x;
3390 int src_y = plane_state->main.y;
936e71e3
VS
3391 int src_w = drm_rect_width(&plane_state->base.src) >> 16;
3392 int src_h = drm_rect_height(&plane_state->base.src) >> 16;
3393 int dst_x = plane_state->base.dst.x1;
3394 int dst_y = plane_state->base.dst.y1;
3395 int dst_w = drm_rect_width(&plane_state->base.dst);
3396 int dst_h = drm_rect_height(&plane_state->base.dst);
70d21f0e 3397
6156a456
CK
3398 plane_ctl = PLANE_CTL_ENABLE |
3399 PLANE_CTL_PIPE_GAMMA_ENABLE |
3400 PLANE_CTL_PIPE_CSC_ENABLE;
3401
3402 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
bae781b2 3403 plane_ctl |= skl_plane_ctl_tiling(fb->modifier);
6156a456 3404 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
6156a456
CK
3405 plane_ctl |= skl_plane_ctl_rotation(rotation);
3406
6687c906
VS
3407 /* Sizes are 0 based */
3408 src_w--;
3409 src_h--;
3410 dst_w--;
3411 dst_h--;
3412
4c0b8a8b
PZ
3413 intel_crtc->dspaddr_offset = surf_addr;
3414
6687c906
VS
3415 intel_crtc->adjusted_x = src_x;
3416 intel_crtc->adjusted_y = src_y;
2db3366b 3417
8e816bb4
VS
3418 I915_WRITE(PLANE_CTL(pipe, plane_id), plane_ctl);
3419 I915_WRITE(PLANE_OFFSET(pipe, plane_id), (src_y << 16) | src_x);
3420 I915_WRITE(PLANE_STRIDE(pipe, plane_id), stride);
3421 I915_WRITE(PLANE_SIZE(pipe, plane_id), (src_h << 16) | src_w);
6156a456
CK
3422
3423 if (scaler_id >= 0) {
3424 uint32_t ps_ctrl = 0;
3425
3426 WARN_ON(!dst_w || !dst_h);
8e816bb4 3427 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(plane_id) |
6156a456
CK
3428 crtc_state->scaler_state.scalers[scaler_id].mode;
3429 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3430 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3431 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3432 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
8e816bb4 3433 I915_WRITE(PLANE_POS(pipe, plane_id), 0);
6156a456 3434 } else {
8e816bb4 3435 I915_WRITE(PLANE_POS(pipe, plane_id), (dst_y << 16) | dst_x);
6156a456
CK
3436 }
3437
8e816bb4 3438 I915_WRITE(PLANE_SURF(pipe, plane_id),
6687c906 3439 intel_fb_gtt_offset(fb, rotation) + surf_addr);
70d21f0e 3440
8e816bb4 3441 POSTING_READ(PLANE_SURF(pipe, plane_id));
70d21f0e
DL
3442}
3443
a8d201af
ML
3444static void skylake_disable_primary_plane(struct drm_plane *primary,
3445 struct drm_crtc *crtc)
17638cd6
JB
3446{
3447 struct drm_device *dev = crtc->dev;
fac5e23e 3448 struct drm_i915_private *dev_priv = to_i915(dev);
8e816bb4
VS
3449 enum plane_id plane_id = to_intel_plane(primary)->id;
3450 enum pipe pipe = to_intel_plane(primary)->pipe;
62e0fb88 3451
8e816bb4
VS
3452 I915_WRITE(PLANE_CTL(pipe, plane_id), 0);
3453 I915_WRITE(PLANE_SURF(pipe, plane_id), 0);
3454 POSTING_READ(PLANE_SURF(pipe, plane_id));
a8d201af 3455}
29b9bde6 3456
a8d201af
ML
3457/* Assume fb object is pinned & idle & fenced and just update base pointers */
3458static int
3459intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3460 int x, int y, enum mode_set_atomic state)
3461{
3462 /* Support for kgdboc is disabled, this needs a major rework. */
3463 DRM_ERROR("legacy panic handler not supported any more.\n");
3464
3465 return -ENODEV;
81255565
JB
3466}
3467
5a21b665
DV
3468static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
3469{
3470 struct intel_crtc *crtc;
3471
91c8a326 3472 for_each_intel_crtc(&dev_priv->drm, crtc)
5a21b665
DV
3473 intel_finish_page_flip_cs(dev_priv, crtc->pipe);
3474}
3475
7514747d
VS
3476static void intel_update_primary_planes(struct drm_device *dev)
3477{
7514747d 3478 struct drm_crtc *crtc;
96a02917 3479
70e1e0ec 3480 for_each_crtc(dev, crtc) {
11c22da6 3481 struct intel_plane *plane = to_intel_plane(crtc->primary);
73974893
ML
3482 struct intel_plane_state *plane_state =
3483 to_intel_plane_state(plane->base.state);
11c22da6 3484
936e71e3 3485 if (plane_state->base.visible)
a8d201af
ML
3486 plane->update_plane(&plane->base,
3487 to_intel_crtc_state(crtc->state),
3488 plane_state);
73974893
ML
3489 }
3490}
3491
3492static int
3493__intel_display_resume(struct drm_device *dev,
3494 struct drm_atomic_state *state)
3495{
3496 struct drm_crtc_state *crtc_state;
3497 struct drm_crtc *crtc;
3498 int i, ret;
11c22da6 3499
73974893 3500 intel_modeset_setup_hw_state(dev);
29b74b7f 3501 i915_redisable_vga(to_i915(dev));
73974893
ML
3502
3503 if (!state)
3504 return 0;
3505
3506 for_each_crtc_in_state(state, crtc, crtc_state, i) {
3507 /*
3508 * Force recalculation even if we restore
3509 * current state. With fast modeset this may not result
3510 * in a modeset when the state is compatible.
3511 */
3512 crtc_state->mode_changed = true;
96a02917 3513 }
73974893
ML
3514
3515 /* ignore any reset values/BIOS leftovers in the WM registers */
3516 to_intel_atomic_state(state)->skip_intermediate_wm = true;
3517
3518 ret = drm_atomic_commit(state);
3519
3520 WARN_ON(ret == -EDEADLK);
3521 return ret;
96a02917
VS
3522}
3523
4ac2ba2f
VS
3524static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
3525{
ae98104b
VS
3526 return intel_has_gpu_reset(dev_priv) &&
3527 INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
4ac2ba2f
VS
3528}
3529
c033666a 3530void intel_prepare_reset(struct drm_i915_private *dev_priv)
7514747d 3531{
73974893
ML
3532 struct drm_device *dev = &dev_priv->drm;
3533 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
3534 struct drm_atomic_state *state;
3535 int ret;
3536
73974893
ML
3537 /*
3538 * Need mode_config.mutex so that we don't
3539 * trample ongoing ->detect() and whatnot.
3540 */
3541 mutex_lock(&dev->mode_config.mutex);
3542 drm_modeset_acquire_init(ctx, 0);
3543 while (1) {
3544 ret = drm_modeset_lock_all_ctx(dev, ctx);
3545 if (ret != -EDEADLK)
3546 break;
3547
3548 drm_modeset_backoff(ctx);
3549 }
3550
3551 /* reset doesn't touch the display, but flips might get nuked anyway, */
522a63de 3552 if (!i915.force_reset_modeset_test &&
4ac2ba2f 3553 !gpu_reset_clobbers_display(dev_priv))
7514747d
VS
3554 return;
3555
f98ce92f
VS
3556 /*
3557 * Disabling the crtcs gracefully seems nicer. Also the
3558 * g33 docs say we should at least disable all the planes.
3559 */
73974893
ML
3560 state = drm_atomic_helper_duplicate_state(dev, ctx);
3561 if (IS_ERR(state)) {
3562 ret = PTR_ERR(state);
3563 state = NULL;
3564 DRM_ERROR("Duplicating state failed with %i\n", ret);
3565 goto err;
3566 }
3567
3568 ret = drm_atomic_helper_disable_all(dev, ctx);
3569 if (ret) {
3570 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
3571 goto err;
3572 }
3573
3574 dev_priv->modeset_restore_state = state;
3575 state->acquire_ctx = ctx;
3576 return;
3577
3578err:
0853695c 3579 drm_atomic_state_put(state);
7514747d
VS
3580}
3581
c033666a 3582void intel_finish_reset(struct drm_i915_private *dev_priv)
7514747d 3583{
73974893
ML
3584 struct drm_device *dev = &dev_priv->drm;
3585 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
3586 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
3587 int ret;
3588
5a21b665
DV
3589 /*
3590 * Flips in the rings will be nuked by the reset,
3591 * so complete all pending flips so that user space
3592 * will get its events and not get stuck.
3593 */
3594 intel_complete_page_flips(dev_priv);
3595
73974893
ML
3596 dev_priv->modeset_restore_state = NULL;
3597
7514747d 3598 /* reset doesn't touch the display */
4ac2ba2f 3599 if (!gpu_reset_clobbers_display(dev_priv)) {
522a63de
ML
3600 if (!state) {
3601 /*
3602 * Flips in the rings have been nuked by the reset,
3603 * so update the base address of all primary
3604 * planes to the the last fb to make sure we're
3605 * showing the correct fb after a reset.
3606 *
3607 * FIXME: Atomic will make this obsolete since we won't schedule
3608 * CS-based flips (which might get lost in gpu resets) any more.
3609 */
3610 intel_update_primary_planes(dev);
3611 } else {
3612 ret = __intel_display_resume(dev, state);
3613 if (ret)
3614 DRM_ERROR("Restoring old state failed with %i\n", ret);
3615 }
73974893
ML
3616 } else {
3617 /*
3618 * The display has been reset as well,
3619 * so need a full re-initialization.
3620 */
3621 intel_runtime_pm_disable_interrupts(dev_priv);
3622 intel_runtime_pm_enable_interrupts(dev_priv);
7514747d 3623
51f59205 3624 intel_pps_unlock_regs_wa(dev_priv);
73974893 3625 intel_modeset_init_hw(dev);
7514747d 3626
73974893
ML
3627 spin_lock_irq(&dev_priv->irq_lock);
3628 if (dev_priv->display.hpd_irq_setup)
3629 dev_priv->display.hpd_irq_setup(dev_priv);
3630 spin_unlock_irq(&dev_priv->irq_lock);
7514747d 3631
73974893
ML
3632 ret = __intel_display_resume(dev, state);
3633 if (ret)
3634 DRM_ERROR("Restoring old state failed with %i\n", ret);
7514747d 3635
73974893
ML
3636 intel_hpd_init(dev_priv);
3637 }
7514747d 3638
0853695c
CW
3639 if (state)
3640 drm_atomic_state_put(state);
73974893
ML
3641 drm_modeset_drop_locks(ctx);
3642 drm_modeset_acquire_fini(ctx);
3643 mutex_unlock(&dev->mode_config.mutex);
7514747d
VS
3644}
3645
8af29b0c
CW
3646static bool abort_flip_on_reset(struct intel_crtc *crtc)
3647{
3648 struct i915_gpu_error *error = &to_i915(crtc->base.dev)->gpu_error;
3649
3650 if (i915_reset_in_progress(error))
3651 return true;
3652
3653 if (crtc->reset_count != i915_reset_count(error))
3654 return true;
3655
3656 return false;
3657}
3658
7d5e3799
CW
3659static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3660{
5a21b665
DV
3661 struct drm_device *dev = crtc->dev;
3662 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5a21b665
DV
3663 bool pending;
3664
8af29b0c 3665 if (abort_flip_on_reset(intel_crtc))
5a21b665
DV
3666 return false;
3667
3668 spin_lock_irq(&dev->event_lock);
3669 pending = to_intel_crtc(crtc)->flip_work != NULL;
3670 spin_unlock_irq(&dev->event_lock);
3671
3672 return pending;
7d5e3799
CW
3673}
3674
bfd16b2a
ML
3675static void intel_update_pipe_config(struct intel_crtc *crtc,
3676 struct intel_crtc_state *old_crtc_state)
e30e8f75 3677{
6315b5d3 3678 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
bfd16b2a
ML
3679 struct intel_crtc_state *pipe_config =
3680 to_intel_crtc_state(crtc->base.state);
e30e8f75 3681
bfd16b2a
ML
3682 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3683 crtc->base.mode = crtc->base.state->mode;
3684
3685 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3686 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3687 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
e30e8f75
GP
3688
3689 /*
3690 * Update pipe size and adjust fitter if needed: the reason for this is
3691 * that in compute_mode_changes we check the native mode (not the pfit
3692 * mode) to see if we can flip rather than do a full mode set. In the
3693 * fastboot case, we'll flip, but if we don't update the pipesrc and
3694 * pfit state, we'll end up with a big fb scanned out into the wrong
3695 * sized surface.
e30e8f75
GP
3696 */
3697
e30e8f75 3698 I915_WRITE(PIPESRC(crtc->pipe),
bfd16b2a
ML
3699 ((pipe_config->pipe_src_w - 1) << 16) |
3700 (pipe_config->pipe_src_h - 1));
3701
3702 /* on skylake this is done by detaching scalers */
6315b5d3 3703 if (INTEL_GEN(dev_priv) >= 9) {
bfd16b2a
ML
3704 skl_detach_scalers(crtc);
3705
3706 if (pipe_config->pch_pfit.enabled)
3707 skylake_pfit_enable(crtc);
6e266956 3708 } else if (HAS_PCH_SPLIT(dev_priv)) {
bfd16b2a
ML
3709 if (pipe_config->pch_pfit.enabled)
3710 ironlake_pfit_enable(crtc);
3711 else if (old_crtc_state->pch_pfit.enabled)
3712 ironlake_pfit_disable(crtc, true);
e30e8f75 3713 }
e30e8f75
GP
3714}
3715
5e84e1a4
ZW
3716static void intel_fdi_normal_train(struct drm_crtc *crtc)
3717{
3718 struct drm_device *dev = crtc->dev;
fac5e23e 3719 struct drm_i915_private *dev_priv = to_i915(dev);
5e84e1a4
ZW
3720 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3721 int pipe = intel_crtc->pipe;
f0f59a00
VS
3722 i915_reg_t reg;
3723 u32 temp;
5e84e1a4
ZW
3724
3725 /* enable normal train */
3726 reg = FDI_TX_CTL(pipe);
3727 temp = I915_READ(reg);
fd6b8f43 3728 if (IS_IVYBRIDGE(dev_priv)) {
357555c0
JB
3729 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3730 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
3731 } else {
3732 temp &= ~FDI_LINK_TRAIN_NONE;
3733 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 3734 }
5e84e1a4
ZW
3735 I915_WRITE(reg, temp);
3736
3737 reg = FDI_RX_CTL(pipe);
3738 temp = I915_READ(reg);
6e266956 3739 if (HAS_PCH_CPT(dev_priv)) {
5e84e1a4
ZW
3740 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3741 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3742 } else {
3743 temp &= ~FDI_LINK_TRAIN_NONE;
3744 temp |= FDI_LINK_TRAIN_NONE;
3745 }
3746 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3747
3748 /* wait one idle pattern time */
3749 POSTING_READ(reg);
3750 udelay(1000);
357555c0
JB
3751
3752 /* IVB wants error correction enabled */
fd6b8f43 3753 if (IS_IVYBRIDGE(dev_priv))
357555c0
JB
3754 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3755 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
3756}
3757
8db9d77b
ZW
3758/* The FDI link training functions for ILK/Ibexpeak. */
3759static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3760{
3761 struct drm_device *dev = crtc->dev;
fac5e23e 3762 struct drm_i915_private *dev_priv = to_i915(dev);
8db9d77b
ZW
3763 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3764 int pipe = intel_crtc->pipe;
f0f59a00
VS
3765 i915_reg_t reg;
3766 u32 temp, tries;
8db9d77b 3767
1c8562f6 3768 /* FDI needs bits from pipe first */
0fc932b8 3769 assert_pipe_enabled(dev_priv, pipe);
0fc932b8 3770
e1a44743
AJ
3771 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3772 for train result */
5eddb70b
CW
3773 reg = FDI_RX_IMR(pipe);
3774 temp = I915_READ(reg);
e1a44743
AJ
3775 temp &= ~FDI_RX_SYMBOL_LOCK;
3776 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3777 I915_WRITE(reg, temp);
3778 I915_READ(reg);
e1a44743
AJ
3779 udelay(150);
3780
8db9d77b 3781 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3782 reg = FDI_TX_CTL(pipe);
3783 temp = I915_READ(reg);
627eb5a3 3784 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3785 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3786 temp &= ~FDI_LINK_TRAIN_NONE;
3787 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 3788 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3789
5eddb70b
CW
3790 reg = FDI_RX_CTL(pipe);
3791 temp = I915_READ(reg);
8db9d77b
ZW
3792 temp &= ~FDI_LINK_TRAIN_NONE;
3793 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b
CW
3794 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3795
3796 POSTING_READ(reg);
8db9d77b
ZW
3797 udelay(150);
3798
5b2adf89 3799 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
DV
3800 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3801 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3802 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 3803
5eddb70b 3804 reg = FDI_RX_IIR(pipe);
e1a44743 3805 for (tries = 0; tries < 5; tries++) {
5eddb70b 3806 temp = I915_READ(reg);
8db9d77b
ZW
3807 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3808
3809 if ((temp & FDI_RX_BIT_LOCK)) {
3810 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 3811 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
3812 break;
3813 }
8db9d77b 3814 }
e1a44743 3815 if (tries == 5)
5eddb70b 3816 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3817
3818 /* Train 2 */
5eddb70b
CW
3819 reg = FDI_TX_CTL(pipe);
3820 temp = I915_READ(reg);
8db9d77b
ZW
3821 temp &= ~FDI_LINK_TRAIN_NONE;
3822 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3823 I915_WRITE(reg, temp);
8db9d77b 3824
5eddb70b
CW
3825 reg = FDI_RX_CTL(pipe);
3826 temp = I915_READ(reg);
8db9d77b
ZW
3827 temp &= ~FDI_LINK_TRAIN_NONE;
3828 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3829 I915_WRITE(reg, temp);
8db9d77b 3830
5eddb70b
CW
3831 POSTING_READ(reg);
3832 udelay(150);
8db9d77b 3833
5eddb70b 3834 reg = FDI_RX_IIR(pipe);
e1a44743 3835 for (tries = 0; tries < 5; tries++) {
5eddb70b 3836 temp = I915_READ(reg);
8db9d77b
ZW
3837 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3838
3839 if (temp & FDI_RX_SYMBOL_LOCK) {
5eddb70b 3840 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
3841 DRM_DEBUG_KMS("FDI train 2 done.\n");
3842 break;
3843 }
8db9d77b 3844 }
e1a44743 3845 if (tries == 5)
5eddb70b 3846 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3847
3848 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 3849
8db9d77b
ZW
3850}
3851
0206e353 3852static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
3853 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3854 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3855 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3856 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3857};
3858
3859/* The FDI link training functions for SNB/Cougarpoint. */
3860static void gen6_fdi_link_train(struct drm_crtc *crtc)
3861{
3862 struct drm_device *dev = crtc->dev;
fac5e23e 3863 struct drm_i915_private *dev_priv = to_i915(dev);
8db9d77b
ZW
3864 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3865 int pipe = intel_crtc->pipe;
f0f59a00
VS
3866 i915_reg_t reg;
3867 u32 temp, i, retry;
8db9d77b 3868
e1a44743
AJ
3869 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3870 for train result */
5eddb70b
CW
3871 reg = FDI_RX_IMR(pipe);
3872 temp = I915_READ(reg);
e1a44743
AJ
3873 temp &= ~FDI_RX_SYMBOL_LOCK;
3874 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3875 I915_WRITE(reg, temp);
3876
3877 POSTING_READ(reg);
e1a44743
AJ
3878 udelay(150);
3879
8db9d77b 3880 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3881 reg = FDI_TX_CTL(pipe);
3882 temp = I915_READ(reg);
627eb5a3 3883 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3884 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3885 temp &= ~FDI_LINK_TRAIN_NONE;
3886 temp |= FDI_LINK_TRAIN_PATTERN_1;
3887 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3888 /* SNB-B */
3889 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 3890 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3891
d74cf324
DV
3892 I915_WRITE(FDI_RX_MISC(pipe),
3893 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3894
5eddb70b
CW
3895 reg = FDI_RX_CTL(pipe);
3896 temp = I915_READ(reg);
6e266956 3897 if (HAS_PCH_CPT(dev_priv)) {
8db9d77b
ZW
3898 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3899 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3900 } else {
3901 temp &= ~FDI_LINK_TRAIN_NONE;
3902 temp |= FDI_LINK_TRAIN_PATTERN_1;
3903 }
5eddb70b
CW
3904 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3905
3906 POSTING_READ(reg);
8db9d77b
ZW
3907 udelay(150);
3908
0206e353 3909 for (i = 0; i < 4; i++) {
5eddb70b
CW
3910 reg = FDI_TX_CTL(pipe);
3911 temp = I915_READ(reg);
8db9d77b
ZW
3912 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3913 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3914 I915_WRITE(reg, temp);
3915
3916 POSTING_READ(reg);
8db9d77b
ZW
3917 udelay(500);
3918
fa37d39e
SP
3919 for (retry = 0; retry < 5; retry++) {
3920 reg = FDI_RX_IIR(pipe);
3921 temp = I915_READ(reg);
3922 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3923 if (temp & FDI_RX_BIT_LOCK) {
3924 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3925 DRM_DEBUG_KMS("FDI train 1 done.\n");
3926 break;
3927 }
3928 udelay(50);
8db9d77b 3929 }
fa37d39e
SP
3930 if (retry < 5)
3931 break;
8db9d77b
ZW
3932 }
3933 if (i == 4)
5eddb70b 3934 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3935
3936 /* Train 2 */
5eddb70b
CW
3937 reg = FDI_TX_CTL(pipe);
3938 temp = I915_READ(reg);
8db9d77b
ZW
3939 temp &= ~FDI_LINK_TRAIN_NONE;
3940 temp |= FDI_LINK_TRAIN_PATTERN_2;
5db94019 3941 if (IS_GEN6(dev_priv)) {
8db9d77b
ZW
3942 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3943 /* SNB-B */
3944 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3945 }
5eddb70b 3946 I915_WRITE(reg, temp);
8db9d77b 3947
5eddb70b
CW
3948 reg = FDI_RX_CTL(pipe);
3949 temp = I915_READ(reg);
6e266956 3950 if (HAS_PCH_CPT(dev_priv)) {
8db9d77b
ZW
3951 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3952 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3953 } else {
3954 temp &= ~FDI_LINK_TRAIN_NONE;
3955 temp |= FDI_LINK_TRAIN_PATTERN_2;
3956 }
5eddb70b
CW
3957 I915_WRITE(reg, temp);
3958
3959 POSTING_READ(reg);
8db9d77b
ZW
3960 udelay(150);
3961
0206e353 3962 for (i = 0; i < 4; i++) {
5eddb70b
CW
3963 reg = FDI_TX_CTL(pipe);
3964 temp = I915_READ(reg);
8db9d77b
ZW
3965 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3966 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3967 I915_WRITE(reg, temp);
3968
3969 POSTING_READ(reg);
8db9d77b
ZW
3970 udelay(500);
3971
fa37d39e
SP
3972 for (retry = 0; retry < 5; retry++) {
3973 reg = FDI_RX_IIR(pipe);
3974 temp = I915_READ(reg);
3975 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3976 if (temp & FDI_RX_SYMBOL_LOCK) {
3977 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3978 DRM_DEBUG_KMS("FDI train 2 done.\n");
3979 break;
3980 }
3981 udelay(50);
8db9d77b 3982 }
fa37d39e
SP
3983 if (retry < 5)
3984 break;
8db9d77b
ZW
3985 }
3986 if (i == 4)
5eddb70b 3987 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3988
3989 DRM_DEBUG_KMS("FDI train done.\n");
3990}
3991
357555c0
JB
3992/* Manual link training for Ivy Bridge A0 parts */
3993static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3994{
3995 struct drm_device *dev = crtc->dev;
fac5e23e 3996 struct drm_i915_private *dev_priv = to_i915(dev);
357555c0
JB
3997 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3998 int pipe = intel_crtc->pipe;
f0f59a00
VS
3999 i915_reg_t reg;
4000 u32 temp, i, j;
357555c0
JB
4001
4002 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
4003 for train result */
4004 reg = FDI_RX_IMR(pipe);
4005 temp = I915_READ(reg);
4006 temp &= ~FDI_RX_SYMBOL_LOCK;
4007 temp &= ~FDI_RX_BIT_LOCK;
4008 I915_WRITE(reg, temp);
4009
4010 POSTING_READ(reg);
4011 udelay(150);
4012
01a415fd
DV
4013 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
4014 I915_READ(FDI_RX_IIR(pipe)));
4015
139ccd3f
JB
4016 /* Try each vswing and preemphasis setting twice before moving on */
4017 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
4018 /* disable first in case we need to retry */
4019 reg = FDI_TX_CTL(pipe);
4020 temp = I915_READ(reg);
4021 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
4022 temp &= ~FDI_TX_ENABLE;
4023 I915_WRITE(reg, temp);
357555c0 4024
139ccd3f
JB
4025 reg = FDI_RX_CTL(pipe);
4026 temp = I915_READ(reg);
4027 temp &= ~FDI_LINK_TRAIN_AUTO;
4028 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4029 temp &= ~FDI_RX_ENABLE;
4030 I915_WRITE(reg, temp);
357555c0 4031
139ccd3f 4032 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
4033 reg = FDI_TX_CTL(pipe);
4034 temp = I915_READ(reg);
139ccd3f 4035 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 4036 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
139ccd3f 4037 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 4038 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
4039 temp |= snb_b_fdi_train_param[j/2];
4040 temp |= FDI_COMPOSITE_SYNC;
4041 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 4042
139ccd3f
JB
4043 I915_WRITE(FDI_RX_MISC(pipe),
4044 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 4045
139ccd3f 4046 reg = FDI_RX_CTL(pipe);
357555c0 4047 temp = I915_READ(reg);
139ccd3f
JB
4048 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
4049 temp |= FDI_COMPOSITE_SYNC;
4050 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 4051
139ccd3f
JB
4052 POSTING_READ(reg);
4053 udelay(1); /* should be 0.5us */
357555c0 4054
139ccd3f
JB
4055 for (i = 0; i < 4; i++) {
4056 reg = FDI_RX_IIR(pipe);
4057 temp = I915_READ(reg);
4058 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 4059
139ccd3f
JB
4060 if (temp & FDI_RX_BIT_LOCK ||
4061 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
4062 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
4063 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
4064 i);
4065 break;
4066 }
4067 udelay(1); /* should be 0.5us */
4068 }
4069 if (i == 4) {
4070 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
4071 continue;
4072 }
357555c0 4073
139ccd3f 4074 /* Train 2 */
357555c0
JB
4075 reg = FDI_TX_CTL(pipe);
4076 temp = I915_READ(reg);
139ccd3f
JB
4077 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
4078 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
4079 I915_WRITE(reg, temp);
4080
4081 reg = FDI_RX_CTL(pipe);
4082 temp = I915_READ(reg);
4083 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4084 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
4085 I915_WRITE(reg, temp);
4086
4087 POSTING_READ(reg);
139ccd3f 4088 udelay(2); /* should be 1.5us */
357555c0 4089
139ccd3f
JB
4090 for (i = 0; i < 4; i++) {
4091 reg = FDI_RX_IIR(pipe);
4092 temp = I915_READ(reg);
4093 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 4094
139ccd3f
JB
4095 if (temp & FDI_RX_SYMBOL_LOCK ||
4096 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
4097 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
4098 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
4099 i);
4100 goto train_done;
4101 }
4102 udelay(2); /* should be 1.5us */
357555c0 4103 }
139ccd3f
JB
4104 if (i == 4)
4105 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 4106 }
357555c0 4107
139ccd3f 4108train_done:
357555c0
JB
4109 DRM_DEBUG_KMS("FDI train done.\n");
4110}
4111
88cefb6c 4112static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 4113{
88cefb6c 4114 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 4115 struct drm_i915_private *dev_priv = to_i915(dev);
2c07245f 4116 int pipe = intel_crtc->pipe;
f0f59a00
VS
4117 i915_reg_t reg;
4118 u32 temp;
c64e311e 4119
c98e9dcf 4120 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
4121 reg = FDI_RX_CTL(pipe);
4122 temp = I915_READ(reg);
627eb5a3 4123 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
6e3c9717 4124 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
dfd07d72 4125 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
4126 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
4127
4128 POSTING_READ(reg);
c98e9dcf
JB
4129 udelay(200);
4130
4131 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
4132 temp = I915_READ(reg);
4133 I915_WRITE(reg, temp | FDI_PCDCLK);
4134
4135 POSTING_READ(reg);
c98e9dcf
JB
4136 udelay(200);
4137
20749730
PZ
4138 /* Enable CPU FDI TX PLL, always on for Ironlake */
4139 reg = FDI_TX_CTL(pipe);
4140 temp = I915_READ(reg);
4141 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
4142 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 4143
20749730
PZ
4144 POSTING_READ(reg);
4145 udelay(100);
6be4a607 4146 }
0e23b99d
JB
4147}
4148
88cefb6c
DV
4149static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
4150{
4151 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 4152 struct drm_i915_private *dev_priv = to_i915(dev);
88cefb6c 4153 int pipe = intel_crtc->pipe;
f0f59a00
VS
4154 i915_reg_t reg;
4155 u32 temp;
88cefb6c
DV
4156
4157 /* Switch from PCDclk to Rawclk */
4158 reg = FDI_RX_CTL(pipe);
4159 temp = I915_READ(reg);
4160 I915_WRITE(reg, temp & ~FDI_PCDCLK);
4161
4162 /* Disable CPU FDI TX PLL */
4163 reg = FDI_TX_CTL(pipe);
4164 temp = I915_READ(reg);
4165 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
4166
4167 POSTING_READ(reg);
4168 udelay(100);
4169
4170 reg = FDI_RX_CTL(pipe);
4171 temp = I915_READ(reg);
4172 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
4173
4174 /* Wait for the clocks to turn off. */
4175 POSTING_READ(reg);
4176 udelay(100);
4177}
4178
0fc932b8
JB
4179static void ironlake_fdi_disable(struct drm_crtc *crtc)
4180{
4181 struct drm_device *dev = crtc->dev;
fac5e23e 4182 struct drm_i915_private *dev_priv = to_i915(dev);
0fc932b8
JB
4183 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4184 int pipe = intel_crtc->pipe;
f0f59a00
VS
4185 i915_reg_t reg;
4186 u32 temp;
0fc932b8
JB
4187
4188 /* disable CPU FDI tx and PCH FDI rx */
4189 reg = FDI_TX_CTL(pipe);
4190 temp = I915_READ(reg);
4191 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
4192 POSTING_READ(reg);
4193
4194 reg = FDI_RX_CTL(pipe);
4195 temp = I915_READ(reg);
4196 temp &= ~(0x7 << 16);
dfd07d72 4197 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
4198 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
4199
4200 POSTING_READ(reg);
4201 udelay(100);
4202
4203 /* Ironlake workaround, disable clock pointer after downing FDI */
6e266956 4204 if (HAS_PCH_IBX(dev_priv))
6f06ce18 4205 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
0fc932b8
JB
4206
4207 /* still set train pattern 1 */
4208 reg = FDI_TX_CTL(pipe);
4209 temp = I915_READ(reg);
4210 temp &= ~FDI_LINK_TRAIN_NONE;
4211 temp |= FDI_LINK_TRAIN_PATTERN_1;
4212 I915_WRITE(reg, temp);
4213
4214 reg = FDI_RX_CTL(pipe);
4215 temp = I915_READ(reg);
6e266956 4216 if (HAS_PCH_CPT(dev_priv)) {
0fc932b8
JB
4217 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4218 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
4219 } else {
4220 temp &= ~FDI_LINK_TRAIN_NONE;
4221 temp |= FDI_LINK_TRAIN_PATTERN_1;
4222 }
4223 /* BPC in FDI rx is consistent with that in PIPECONF */
4224 temp &= ~(0x07 << 16);
dfd07d72 4225 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
4226 I915_WRITE(reg, temp);
4227
4228 POSTING_READ(reg);
4229 udelay(100);
4230}
4231
49d73912 4232bool intel_has_pending_fb_unpin(struct drm_i915_private *dev_priv)
5dce5b93
CW
4233{
4234 struct intel_crtc *crtc;
4235
4236 /* Note that we don't need to be called with mode_config.lock here
4237 * as our list of CRTC objects is static for the lifetime of the
4238 * device and so cannot disappear as we iterate. Similarly, we can
4239 * happily treat the predicates as racy, atomic checks as userspace
4240 * cannot claim and pin a new fb without at least acquring the
4241 * struct_mutex and so serialising with us.
4242 */
49d73912 4243 for_each_intel_crtc(&dev_priv->drm, crtc) {
5dce5b93
CW
4244 if (atomic_read(&crtc->unpin_work_count) == 0)
4245 continue;
4246
5a21b665 4247 if (crtc->flip_work)
0f0f74bc 4248 intel_wait_for_vblank(dev_priv, crtc->pipe);
5dce5b93
CW
4249
4250 return true;
4251 }
4252
4253 return false;
4254}
4255
5a21b665 4256static void page_flip_completed(struct intel_crtc *intel_crtc)
d6bbafa1
CW
4257{
4258 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
5a21b665
DV
4259 struct intel_flip_work *work = intel_crtc->flip_work;
4260
4261 intel_crtc->flip_work = NULL;
d6bbafa1
CW
4262
4263 if (work->event)
560ce1dc 4264 drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
d6bbafa1
CW
4265
4266 drm_crtc_vblank_put(&intel_crtc->base);
4267
5a21b665 4268 wake_up_all(&dev_priv->pending_flip_queue);
143f73b3 4269 queue_work(dev_priv->wq, &work->unpin_work);
5a21b665
DV
4270
4271 trace_i915_flip_complete(intel_crtc->plane,
4272 work->pending_flip_obj);
d6bbafa1
CW
4273}
4274
5008e874 4275static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
e6c3a2a6 4276{
0f91128d 4277 struct drm_device *dev = crtc->dev;
fac5e23e 4278 struct drm_i915_private *dev_priv = to_i915(dev);
5008e874 4279 long ret;
e6c3a2a6 4280
2c10d571 4281 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
5008e874
ML
4282
4283 ret = wait_event_interruptible_timeout(
4284 dev_priv->pending_flip_queue,
4285 !intel_crtc_has_pending_flip(crtc),
4286 60*HZ);
4287
4288 if (ret < 0)
4289 return ret;
4290
5a21b665
DV
4291 if (ret == 0) {
4292 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4293 struct intel_flip_work *work;
4294
4295 spin_lock_irq(&dev->event_lock);
4296 work = intel_crtc->flip_work;
4297 if (work && !is_mmio_work(work)) {
4298 WARN_ONCE(1, "Removing stuck page flip\n");
4299 page_flip_completed(intel_crtc);
4300 }
4301 spin_unlock_irq(&dev->event_lock);
4302 }
5bb61643 4303
5008e874 4304 return 0;
e6c3a2a6
CW
4305}
4306
b7076546 4307void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
060f02d8
VS
4308{
4309 u32 temp;
4310
4311 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
4312
4313 mutex_lock(&dev_priv->sb_lock);
4314
4315 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4316 temp |= SBI_SSCCTL_DISABLE;
4317 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
4318
4319 mutex_unlock(&dev_priv->sb_lock);
4320}
4321
e615efe4
ED
4322/* Program iCLKIP clock to the desired frequency */
4323static void lpt_program_iclkip(struct drm_crtc *crtc)
4324{
64b46a06 4325 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6e3c9717 4326 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
e615efe4
ED
4327 u32 divsel, phaseinc, auxdiv, phasedir = 0;
4328 u32 temp;
4329
060f02d8 4330 lpt_disable_iclkip(dev_priv);
e615efe4 4331
64b46a06
VS
4332 /* The iCLK virtual clock root frequency is in MHz,
4333 * but the adjusted_mode->crtc_clock in in KHz. To get the
4334 * divisors, it is necessary to divide one by another, so we
4335 * convert the virtual clock precision to KHz here for higher
4336 * precision.
4337 */
4338 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
e615efe4
ED
4339 u32 iclk_virtual_root_freq = 172800 * 1000;
4340 u32 iclk_pi_range = 64;
64b46a06 4341 u32 desired_divisor;
e615efe4 4342
64b46a06
VS
4343 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4344 clock << auxdiv);
4345 divsel = (desired_divisor / iclk_pi_range) - 2;
4346 phaseinc = desired_divisor % iclk_pi_range;
e615efe4 4347
64b46a06
VS
4348 /*
4349 * Near 20MHz is a corner case which is
4350 * out of range for the 7-bit divisor
4351 */
4352 if (divsel <= 0x7f)
4353 break;
e615efe4
ED
4354 }
4355
4356 /* This should not happen with any sane values */
4357 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
4358 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
4359 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
4360 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
4361
4362 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 4363 clock,
e615efe4
ED
4364 auxdiv,
4365 divsel,
4366 phasedir,
4367 phaseinc);
4368
060f02d8
VS
4369 mutex_lock(&dev_priv->sb_lock);
4370
e615efe4 4371 /* Program SSCDIVINTPHASE6 */
988d6ee8 4372 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
4373 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
4374 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
4375 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
4376 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
4377 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
4378 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 4379 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
4380
4381 /* Program SSCAUXDIV */
988d6ee8 4382 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
4383 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
4384 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 4385 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
4386
4387 /* Enable modulator and associated divider */
988d6ee8 4388 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 4389 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 4390 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4 4391
060f02d8
VS
4392 mutex_unlock(&dev_priv->sb_lock);
4393
e615efe4
ED
4394 /* Wait for initialization time */
4395 udelay(24);
4396
4397 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
4398}
4399
8802e5b6
VS
4400int lpt_get_iclkip(struct drm_i915_private *dev_priv)
4401{
4402 u32 divsel, phaseinc, auxdiv;
4403 u32 iclk_virtual_root_freq = 172800 * 1000;
4404 u32 iclk_pi_range = 64;
4405 u32 desired_divisor;
4406 u32 temp;
4407
4408 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
4409 return 0;
4410
4411 mutex_lock(&dev_priv->sb_lock);
4412
4413 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4414 if (temp & SBI_SSCCTL_DISABLE) {
4415 mutex_unlock(&dev_priv->sb_lock);
4416 return 0;
4417 }
4418
4419 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
4420 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
4421 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
4422 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
4423 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
4424
4425 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
4426 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
4427 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
4428
4429 mutex_unlock(&dev_priv->sb_lock);
4430
4431 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
4432
4433 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4434 desired_divisor << auxdiv);
4435}
4436
275f01b2
DV
4437static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4438 enum pipe pch_transcoder)
4439{
4440 struct drm_device *dev = crtc->base.dev;
fac5e23e 4441 struct drm_i915_private *dev_priv = to_i915(dev);
6e3c9717 4442 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
275f01b2
DV
4443
4444 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4445 I915_READ(HTOTAL(cpu_transcoder)));
4446 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4447 I915_READ(HBLANK(cpu_transcoder)));
4448 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4449 I915_READ(HSYNC(cpu_transcoder)));
4450
4451 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4452 I915_READ(VTOTAL(cpu_transcoder)));
4453 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4454 I915_READ(VBLANK(cpu_transcoder)));
4455 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4456 I915_READ(VSYNC(cpu_transcoder)));
4457 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4458 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4459}
4460
003632d9 4461static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
1fbc0d78 4462{
fac5e23e 4463 struct drm_i915_private *dev_priv = to_i915(dev);
1fbc0d78
DV
4464 uint32_t temp;
4465
4466 temp = I915_READ(SOUTH_CHICKEN1);
003632d9 4467 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
1fbc0d78
DV
4468 return;
4469
4470 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4471 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4472
003632d9
ACO
4473 temp &= ~FDI_BC_BIFURCATION_SELECT;
4474 if (enable)
4475 temp |= FDI_BC_BIFURCATION_SELECT;
4476
4477 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
1fbc0d78
DV
4478 I915_WRITE(SOUTH_CHICKEN1, temp);
4479 POSTING_READ(SOUTH_CHICKEN1);
4480}
4481
4482static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4483{
4484 struct drm_device *dev = intel_crtc->base.dev;
1fbc0d78
DV
4485
4486 switch (intel_crtc->pipe) {
4487 case PIPE_A:
4488 break;
4489 case PIPE_B:
6e3c9717 4490 if (intel_crtc->config->fdi_lanes > 2)
003632d9 4491 cpt_set_fdi_bc_bifurcation(dev, false);
1fbc0d78 4492 else
003632d9 4493 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4494
4495 break;
4496 case PIPE_C:
003632d9 4497 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4498
4499 break;
4500 default:
4501 BUG();
4502 }
4503}
4504
c48b5305
VS
4505/* Return which DP Port should be selected for Transcoder DP control */
4506static enum port
4507intel_trans_dp_port_sel(struct drm_crtc *crtc)
4508{
4509 struct drm_device *dev = crtc->dev;
4510 struct intel_encoder *encoder;
4511
4512 for_each_encoder_on_crtc(dev, crtc, encoder) {
cca0502b 4513 if (encoder->type == INTEL_OUTPUT_DP ||
c48b5305
VS
4514 encoder->type == INTEL_OUTPUT_EDP)
4515 return enc_to_dig_port(&encoder->base)->port;
4516 }
4517
4518 return -1;
4519}
4520
f67a559d
JB
4521/*
4522 * Enable PCH resources required for PCH ports:
4523 * - PCH PLLs
4524 * - FDI training & RX/TX
4525 * - update transcoder timings
4526 * - DP transcoding bits
4527 * - transcoder
4528 */
4529static void ironlake_pch_enable(struct drm_crtc *crtc)
0e23b99d
JB
4530{
4531 struct drm_device *dev = crtc->dev;
fac5e23e 4532 struct drm_i915_private *dev_priv = to_i915(dev);
0e23b99d
JB
4533 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4534 int pipe = intel_crtc->pipe;
f0f59a00 4535 u32 temp;
2c07245f 4536
ab9412ba 4537 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 4538
fd6b8f43 4539 if (IS_IVYBRIDGE(dev_priv))
1fbc0d78
DV
4540 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4541
cd986abb
DV
4542 /* Write the TU size bits before fdi link training, so that error
4543 * detection works. */
4544 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4545 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4546
c98e9dcf 4547 /* For PCH output, training FDI link */
674cf967 4548 dev_priv->display.fdi_link_train(crtc);
2c07245f 4549
3ad8a208
DV
4550 /* We need to program the right clock selection before writing the pixel
4551 * mutliplier into the DPLL. */
6e266956 4552 if (HAS_PCH_CPT(dev_priv)) {
ee7b9f93 4553 u32 sel;
4b645f14 4554
c98e9dcf 4555 temp = I915_READ(PCH_DPLL_SEL);
11887397
DV
4556 temp |= TRANS_DPLL_ENABLE(pipe);
4557 sel = TRANS_DPLLB_SEL(pipe);
8106ddbd
ACO
4558 if (intel_crtc->config->shared_dpll ==
4559 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
ee7b9f93
JB
4560 temp |= sel;
4561 else
4562 temp &= ~sel;
c98e9dcf 4563 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 4564 }
5eddb70b 4565
3ad8a208
DV
4566 /* XXX: pch pll's can be enabled any time before we enable the PCH
4567 * transcoder, and we actually should do this to not upset any PCH
4568 * transcoder that already use the clock when we share it.
4569 *
4570 * Note that enable_shared_dpll tries to do the right thing, but
4571 * get_shared_dpll unconditionally resets the pll - we need that to have
4572 * the right LVDS enable sequence. */
85b3894f 4573 intel_enable_shared_dpll(intel_crtc);
3ad8a208 4574
d9b6cb56
JB
4575 /* set transcoder timing, panel must allow it */
4576 assert_panel_unlocked(dev_priv, pipe);
275f01b2 4577 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
8db9d77b 4578
303b81e0 4579 intel_fdi_normal_train(crtc);
5e84e1a4 4580
c98e9dcf 4581 /* For PCH DP, enable TRANS_DP_CTL */
6e266956
TU
4582 if (HAS_PCH_CPT(dev_priv) &&
4583 intel_crtc_has_dp_encoder(intel_crtc->config)) {
9c4edaee
VS
4584 const struct drm_display_mode *adjusted_mode =
4585 &intel_crtc->config->base.adjusted_mode;
dfd07d72 4586 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
f0f59a00 4587 i915_reg_t reg = TRANS_DP_CTL(pipe);
5eddb70b
CW
4588 temp = I915_READ(reg);
4589 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
4590 TRANS_DP_SYNC_MASK |
4591 TRANS_DP_BPC_MASK);
e3ef4479 4592 temp |= TRANS_DP_OUTPUT_ENABLE;
9325c9f0 4593 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf 4594
9c4edaee 4595 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 4596 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
9c4edaee 4597 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 4598 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf
JB
4599
4600 switch (intel_trans_dp_port_sel(crtc)) {
c48b5305 4601 case PORT_B:
5eddb70b 4602 temp |= TRANS_DP_PORT_SEL_B;
c98e9dcf 4603 break;
c48b5305 4604 case PORT_C:
5eddb70b 4605 temp |= TRANS_DP_PORT_SEL_C;
c98e9dcf 4606 break;
c48b5305 4607 case PORT_D:
5eddb70b 4608 temp |= TRANS_DP_PORT_SEL_D;
c98e9dcf
JB
4609 break;
4610 default:
e95d41e1 4611 BUG();
32f9d658 4612 }
2c07245f 4613
5eddb70b 4614 I915_WRITE(reg, temp);
6be4a607 4615 }
b52eb4dc 4616
b8a4f404 4617 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
4618}
4619
1507e5bd
PZ
4620static void lpt_pch_enable(struct drm_crtc *crtc)
4621{
4622 struct drm_device *dev = crtc->dev;
fac5e23e 4623 struct drm_i915_private *dev_priv = to_i915(dev);
1507e5bd 4624 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 4625 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
1507e5bd 4626
ab9412ba 4627 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
1507e5bd 4628
8c52b5e8 4629 lpt_program_iclkip(crtc);
1507e5bd 4630
0540e488 4631 /* Set transcoder timing. */
275f01b2 4632 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
1507e5bd 4633
937bb610 4634 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
4635}
4636
a1520318 4637static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57 4638{
fac5e23e 4639 struct drm_i915_private *dev_priv = to_i915(dev);
f0f59a00 4640 i915_reg_t dslreg = PIPEDSL(pipe);
d4270e57
JB
4641 u32 temp;
4642
4643 temp = I915_READ(dslreg);
4644 udelay(500);
4645 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 4646 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 4647 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
4648 }
4649}
4650
86adf9d7
ML
4651static int
4652skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4653 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4654 int src_w, int src_h, int dst_w, int dst_h)
a1b2278e 4655{
86adf9d7
ML
4656 struct intel_crtc_scaler_state *scaler_state =
4657 &crtc_state->scaler_state;
4658 struct intel_crtc *intel_crtc =
4659 to_intel_crtc(crtc_state->base.crtc);
a1b2278e 4660 int need_scaling;
6156a456 4661
bd2ef25d 4662 need_scaling = drm_rotation_90_or_270(rotation) ?
6156a456
CK
4663 (src_h != dst_w || src_w != dst_h):
4664 (src_w != dst_w || src_h != dst_h);
a1b2278e
CK
4665
4666 /*
4667 * if plane is being disabled or scaler is no more required or force detach
4668 * - free scaler binded to this plane/crtc
4669 * - in order to do this, update crtc->scaler_usage
4670 *
4671 * Here scaler state in crtc_state is set free so that
4672 * scaler can be assigned to other user. Actual register
4673 * update to free the scaler is done in plane/panel-fit programming.
4674 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4675 */
86adf9d7 4676 if (force_detach || !need_scaling) {
a1b2278e 4677 if (*scaler_id >= 0) {
86adf9d7 4678 scaler_state->scaler_users &= ~(1 << scaler_user);
a1b2278e
CK
4679 scaler_state->scalers[*scaler_id].in_use = 0;
4680
86adf9d7
ML
4681 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4682 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4683 intel_crtc->pipe, scaler_user, *scaler_id,
a1b2278e
CK
4684 scaler_state->scaler_users);
4685 *scaler_id = -1;
4686 }
4687 return 0;
4688 }
4689
4690 /* range checks */
4691 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4692 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4693
4694 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4695 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
86adf9d7 4696 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
a1b2278e 4697 "size is out of scaler range\n",
86adf9d7 4698 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
a1b2278e
CK
4699 return -EINVAL;
4700 }
4701
86adf9d7
ML
4702 /* mark this plane as a scaler user in crtc_state */
4703 scaler_state->scaler_users |= (1 << scaler_user);
4704 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4705 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4706 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4707 scaler_state->scaler_users);
4708
4709 return 0;
4710}
4711
4712/**
4713 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4714 *
4715 * @state: crtc's scaler state
86adf9d7
ML
4716 *
4717 * Return
4718 * 0 - scaler_usage updated successfully
4719 * error - requested scaling cannot be supported or other error condition
4720 */
e435d6e5 4721int skl_update_scaler_crtc(struct intel_crtc_state *state)
86adf9d7 4722{
7c5f93b0 4723 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
86adf9d7 4724
e435d6e5 4725 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
31ad61e4 4726 &state->scaler_state.scaler_id, DRM_ROTATE_0,
86adf9d7 4727 state->pipe_src_w, state->pipe_src_h,
aad941d5 4728 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
86adf9d7
ML
4729}
4730
4731/**
4732 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4733 *
4734 * @state: crtc's scaler state
86adf9d7
ML
4735 * @plane_state: atomic plane state to update
4736 *
4737 * Return
4738 * 0 - scaler_usage updated successfully
4739 * error - requested scaling cannot be supported or other error condition
4740 */
da20eabd
ML
4741static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4742 struct intel_plane_state *plane_state)
86adf9d7
ML
4743{
4744
da20eabd
ML
4745 struct intel_plane *intel_plane =
4746 to_intel_plane(plane_state->base.plane);
86adf9d7
ML
4747 struct drm_framebuffer *fb = plane_state->base.fb;
4748 int ret;
4749
936e71e3 4750 bool force_detach = !fb || !plane_state->base.visible;
86adf9d7 4751
86adf9d7
ML
4752 ret = skl_update_scaler(crtc_state, force_detach,
4753 drm_plane_index(&intel_plane->base),
4754 &plane_state->scaler_id,
4755 plane_state->base.rotation,
936e71e3
VS
4756 drm_rect_width(&plane_state->base.src) >> 16,
4757 drm_rect_height(&plane_state->base.src) >> 16,
4758 drm_rect_width(&plane_state->base.dst),
4759 drm_rect_height(&plane_state->base.dst));
86adf9d7
ML
4760
4761 if (ret || plane_state->scaler_id < 0)
4762 return ret;
4763
a1b2278e 4764 /* check colorkey */
818ed961 4765 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
72660ce0
VS
4766 DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
4767 intel_plane->base.base.id,
4768 intel_plane->base.name);
a1b2278e
CK
4769 return -EINVAL;
4770 }
4771
4772 /* Check src format */
86adf9d7
ML
4773 switch (fb->pixel_format) {
4774 case DRM_FORMAT_RGB565:
4775 case DRM_FORMAT_XBGR8888:
4776 case DRM_FORMAT_XRGB8888:
4777 case DRM_FORMAT_ABGR8888:
4778 case DRM_FORMAT_ARGB8888:
4779 case DRM_FORMAT_XRGB2101010:
4780 case DRM_FORMAT_XBGR2101010:
4781 case DRM_FORMAT_YUYV:
4782 case DRM_FORMAT_YVYU:
4783 case DRM_FORMAT_UYVY:
4784 case DRM_FORMAT_VYUY:
4785 break;
4786 default:
72660ce0
VS
4787 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
4788 intel_plane->base.base.id, intel_plane->base.name,
4789 fb->base.id, fb->pixel_format);
86adf9d7 4790 return -EINVAL;
a1b2278e
CK
4791 }
4792
a1b2278e
CK
4793 return 0;
4794}
4795
e435d6e5
ML
4796static void skylake_scaler_disable(struct intel_crtc *crtc)
4797{
4798 int i;
4799
4800 for (i = 0; i < crtc->num_scalers; i++)
4801 skl_detach_scaler(crtc, i);
4802}
4803
4804static void skylake_pfit_enable(struct intel_crtc *crtc)
bd2e244f
JB
4805{
4806 struct drm_device *dev = crtc->base.dev;
fac5e23e 4807 struct drm_i915_private *dev_priv = to_i915(dev);
bd2e244f 4808 int pipe = crtc->pipe;
a1b2278e
CK
4809 struct intel_crtc_scaler_state *scaler_state =
4810 &crtc->config->scaler_state;
4811
4812 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4813
6e3c9717 4814 if (crtc->config->pch_pfit.enabled) {
a1b2278e
CK
4815 int id;
4816
4817 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4818 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4819 return;
4820 }
4821
4822 id = scaler_state->scaler_id;
4823 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4824 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4825 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4826 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4827
4828 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
bd2e244f
JB
4829 }
4830}
4831
b074cec8
JB
4832static void ironlake_pfit_enable(struct intel_crtc *crtc)
4833{
4834 struct drm_device *dev = crtc->base.dev;
fac5e23e 4835 struct drm_i915_private *dev_priv = to_i915(dev);
b074cec8
JB
4836 int pipe = crtc->pipe;
4837
6e3c9717 4838 if (crtc->config->pch_pfit.enabled) {
b074cec8
JB
4839 /* Force use of hard-coded filter coefficients
4840 * as some pre-programmed values are broken,
4841 * e.g. x201.
4842 */
fd6b8f43 4843 if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
b074cec8
JB
4844 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4845 PF_PIPE_SEL_IVB(pipe));
4846 else
4847 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
6e3c9717
ACO
4848 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4849 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
d4270e57
JB
4850 }
4851}
4852
20bc8673 4853void hsw_enable_ips(struct intel_crtc *crtc)
d77e4531 4854{
cea165c3 4855 struct drm_device *dev = crtc->base.dev;
fac5e23e 4856 struct drm_i915_private *dev_priv = to_i915(dev);
d77e4531 4857
6e3c9717 4858 if (!crtc->config->ips_enabled)
d77e4531
PZ
4859 return;
4860
307e4498
ML
4861 /*
4862 * We can only enable IPS after we enable a plane and wait for a vblank
4863 * This function is called from post_plane_update, which is run after
4864 * a vblank wait.
4865 */
cea165c3 4866
d77e4531 4867 assert_plane_enabled(dev_priv, crtc->plane);
8652744b 4868 if (IS_BROADWELL(dev_priv)) {
2a114cc1
BW
4869 mutex_lock(&dev_priv->rps.hw_lock);
4870 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4871 mutex_unlock(&dev_priv->rps.hw_lock);
4872 /* Quoting Art Runyan: "its not safe to expect any particular
4873 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
4874 * mailbox." Moreover, the mailbox may return a bogus state,
4875 * so we need to just enable it and continue on.
2a114cc1
BW
4876 */
4877 } else {
4878 I915_WRITE(IPS_CTL, IPS_ENABLE);
4879 /* The bit only becomes 1 in the next vblank, so this wait here
4880 * is essentially intel_wait_for_vblank. If we don't have this
4881 * and don't wait for vblanks until the end of crtc_enable, then
4882 * the HW state readout code will complain that the expected
4883 * IPS_CTL value is not the one we read. */
2ec9ba3c
CW
4884 if (intel_wait_for_register(dev_priv,
4885 IPS_CTL, IPS_ENABLE, IPS_ENABLE,
4886 50))
2a114cc1
BW
4887 DRM_ERROR("Timed out waiting for IPS enable\n");
4888 }
d77e4531
PZ
4889}
4890
20bc8673 4891void hsw_disable_ips(struct intel_crtc *crtc)
d77e4531
PZ
4892{
4893 struct drm_device *dev = crtc->base.dev;
fac5e23e 4894 struct drm_i915_private *dev_priv = to_i915(dev);
d77e4531 4895
6e3c9717 4896 if (!crtc->config->ips_enabled)
d77e4531
PZ
4897 return;
4898
4899 assert_plane_enabled(dev_priv, crtc->plane);
8652744b 4900 if (IS_BROADWELL(dev_priv)) {
2a114cc1
BW
4901 mutex_lock(&dev_priv->rps.hw_lock);
4902 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4903 mutex_unlock(&dev_priv->rps.hw_lock);
23d0b130 4904 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
b85c1ecf
CW
4905 if (intel_wait_for_register(dev_priv,
4906 IPS_CTL, IPS_ENABLE, 0,
4907 42))
23d0b130 4908 DRM_ERROR("Timed out waiting for IPS disable\n");
e59150dc 4909 } else {
2a114cc1 4910 I915_WRITE(IPS_CTL, 0);
e59150dc
JB
4911 POSTING_READ(IPS_CTL);
4912 }
d77e4531
PZ
4913
4914 /* We need to wait for a vblank before we can disable the plane. */
0f0f74bc 4915 intel_wait_for_vblank(dev_priv, crtc->pipe);
d77e4531
PZ
4916}
4917
7cac945f 4918static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
d3eedb1a 4919{
7cac945f 4920 if (intel_crtc->overlay) {
d3eedb1a 4921 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 4922 struct drm_i915_private *dev_priv = to_i915(dev);
d3eedb1a
VS
4923
4924 mutex_lock(&dev->struct_mutex);
4925 dev_priv->mm.interruptible = false;
4926 (void) intel_overlay_switch_off(intel_crtc->overlay);
4927 dev_priv->mm.interruptible = true;
4928 mutex_unlock(&dev->struct_mutex);
4929 }
4930
4931 /* Let userspace switch the overlay on again. In most cases userspace
4932 * has to recompute where to put it anyway.
4933 */
4934}
4935
87d4300a
ML
4936/**
4937 * intel_post_enable_primary - Perform operations after enabling primary plane
4938 * @crtc: the CRTC whose primary plane was just enabled
4939 *
4940 * Performs potentially sleeping operations that must be done after the primary
4941 * plane is enabled, such as updating FBC and IPS. Note that this may be
4942 * called due to an explicit primary plane update, or due to an implicit
4943 * re-enable that is caused when a sprite plane is updated to no longer
4944 * completely hide the primary plane.
4945 */
4946static void
4947intel_post_enable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4948{
4949 struct drm_device *dev = crtc->dev;
fac5e23e 4950 struct drm_i915_private *dev_priv = to_i915(dev);
a5c4d7bc
VS
4951 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4952 int pipe = intel_crtc->pipe;
a5c4d7bc 4953
87d4300a
ML
4954 /*
4955 * FIXME IPS should be fine as long as one plane is
4956 * enabled, but in practice it seems to have problems
4957 * when going from primary only to sprite only and vice
4958 * versa.
4959 */
a5c4d7bc
VS
4960 hsw_enable_ips(intel_crtc);
4961
f99d7069 4962 /*
87d4300a
ML
4963 * Gen2 reports pipe underruns whenever all planes are disabled.
4964 * So don't enable underrun reporting before at least some planes
4965 * are enabled.
4966 * FIXME: Need to fix the logic to work when we turn off all planes
4967 * but leave the pipe running.
f99d7069 4968 */
5db94019 4969 if (IS_GEN2(dev_priv))
87d4300a
ML
4970 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4971
aca7b684
VS
4972 /* Underruns don't always raise interrupts, so check manually. */
4973 intel_check_cpu_fifo_underruns(dev_priv);
4974 intel_check_pch_fifo_underruns(dev_priv);
a5c4d7bc
VS
4975}
4976
2622a081 4977/* FIXME move all this to pre_plane_update() with proper state tracking */
87d4300a
ML
4978static void
4979intel_pre_disable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4980{
4981 struct drm_device *dev = crtc->dev;
fac5e23e 4982 struct drm_i915_private *dev_priv = to_i915(dev);
a5c4d7bc
VS
4983 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4984 int pipe = intel_crtc->pipe;
a5c4d7bc 4985
87d4300a
ML
4986 /*
4987 * Gen2 reports pipe underruns whenever all planes are disabled.
4988 * So diasble underrun reporting before all the planes get disabled.
4989 * FIXME: Need to fix the logic to work when we turn off all planes
4990 * but leave the pipe running.
4991 */
5db94019 4992 if (IS_GEN2(dev_priv))
87d4300a 4993 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
a5c4d7bc 4994
2622a081
VS
4995 /*
4996 * FIXME IPS should be fine as long as one plane is
4997 * enabled, but in practice it seems to have problems
4998 * when going from primary only to sprite only and vice
4999 * versa.
5000 */
5001 hsw_disable_ips(intel_crtc);
5002}
5003
5004/* FIXME get rid of this and use pre_plane_update */
5005static void
5006intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
5007{
5008 struct drm_device *dev = crtc->dev;
fac5e23e 5009 struct drm_i915_private *dev_priv = to_i915(dev);
2622a081
VS
5010 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5011 int pipe = intel_crtc->pipe;
5012
5013 intel_pre_disable_primary(crtc);
5014
87d4300a
ML
5015 /*
5016 * Vblank time updates from the shadow to live plane control register
5017 * are blocked if the memory self-refresh mode is active at that
5018 * moment. So to make sure the plane gets truly disabled, disable
5019 * first the self-refresh mode. The self-refresh enable bit in turn
5020 * will be checked/applied by the HW only at the next frame start
5021 * event which is after the vblank start event, so we need to have a
5022 * wait-for-vblank between disabling the plane and the pipe.
5023 */
49cff963 5024 if (HAS_GMCH_DISPLAY(dev_priv)) {
87d4300a 5025 intel_set_memory_cxsr(dev_priv, false);
262cd2e1 5026 dev_priv->wm.vlv.cxsr = false;
0f0f74bc 5027 intel_wait_for_vblank(dev_priv, pipe);
262cd2e1 5028 }
87d4300a
ML
5029}
5030
5a21b665
DV
5031static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
5032{
5033 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
5034 struct drm_atomic_state *old_state = old_crtc_state->base.state;
5035 struct intel_crtc_state *pipe_config =
5036 to_intel_crtc_state(crtc->base.state);
5a21b665
DV
5037 struct drm_plane *primary = crtc->base.primary;
5038 struct drm_plane_state *old_pri_state =
5039 drm_atomic_get_existing_plane_state(old_state, primary);
5040
5748b6a1 5041 intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
5a21b665
DV
5042
5043 crtc->wm.cxsr_allowed = true;
5044
5045 if (pipe_config->update_wm_post && pipe_config->base.active)
432081bc 5046 intel_update_watermarks(crtc);
5a21b665
DV
5047
5048 if (old_pri_state) {
5049 struct intel_plane_state *primary_state =
5050 to_intel_plane_state(primary->state);
5051 struct intel_plane_state *old_primary_state =
5052 to_intel_plane_state(old_pri_state);
5053
5054 intel_fbc_post_update(crtc);
5055
936e71e3 5056 if (primary_state->base.visible &&
5a21b665 5057 (needs_modeset(&pipe_config->base) ||
936e71e3 5058 !old_primary_state->base.visible))
5a21b665
DV
5059 intel_post_enable_primary(&crtc->base);
5060 }
5061}
5062
5c74cd73 5063static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
ac21b225 5064{
5c74cd73 5065 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
ac21b225 5066 struct drm_device *dev = crtc->base.dev;
fac5e23e 5067 struct drm_i915_private *dev_priv = to_i915(dev);
ab1d3a0e
ML
5068 struct intel_crtc_state *pipe_config =
5069 to_intel_crtc_state(crtc->base.state);
5c74cd73
ML
5070 struct drm_atomic_state *old_state = old_crtc_state->base.state;
5071 struct drm_plane *primary = crtc->base.primary;
5072 struct drm_plane_state *old_pri_state =
5073 drm_atomic_get_existing_plane_state(old_state, primary);
5074 bool modeset = needs_modeset(&pipe_config->base);
ccf010fb
ML
5075 struct intel_atomic_state *old_intel_state =
5076 to_intel_atomic_state(old_state);
ac21b225 5077
5c74cd73
ML
5078 if (old_pri_state) {
5079 struct intel_plane_state *primary_state =
5080 to_intel_plane_state(primary->state);
5081 struct intel_plane_state *old_primary_state =
5082 to_intel_plane_state(old_pri_state);
5083
faf68d92 5084 intel_fbc_pre_update(crtc, pipe_config, primary_state);
31ae71fc 5085
936e71e3
VS
5086 if (old_primary_state->base.visible &&
5087 (modeset || !primary_state->base.visible))
5c74cd73
ML
5088 intel_pre_disable_primary(&crtc->base);
5089 }
852eb00d 5090
49cff963 5091 if (pipe_config->disable_cxsr && HAS_GMCH_DISPLAY(dev_priv)) {
852eb00d 5092 crtc->wm.cxsr_allowed = false;
2dfd178d 5093
2622a081
VS
5094 /*
5095 * Vblank time updates from the shadow to live plane control register
5096 * are blocked if the memory self-refresh mode is active at that
5097 * moment. So to make sure the plane gets truly disabled, disable
5098 * first the self-refresh mode. The self-refresh enable bit in turn
5099 * will be checked/applied by the HW only at the next frame start
5100 * event which is after the vblank start event, so we need to have a
5101 * wait-for-vblank between disabling the plane and the pipe.
5102 */
5103 if (old_crtc_state->base.active) {
2dfd178d 5104 intel_set_memory_cxsr(dev_priv, false);
2622a081 5105 dev_priv->wm.vlv.cxsr = false;
0f0f74bc 5106 intel_wait_for_vblank(dev_priv, crtc->pipe);
2622a081 5107 }
852eb00d 5108 }
92826fcd 5109
ed4a6a7c
MR
5110 /*
5111 * IVB workaround: must disable low power watermarks for at least
5112 * one frame before enabling scaling. LP watermarks can be re-enabled
5113 * when scaling is disabled.
5114 *
5115 * WaCxSRDisabledForSpriteScaling:ivb
5116 */
5117 if (pipe_config->disable_lp_wm) {
5118 ilk_disable_lp_wm(dev);
0f0f74bc 5119 intel_wait_for_vblank(dev_priv, crtc->pipe);
ed4a6a7c
MR
5120 }
5121
5122 /*
5123 * If we're doing a modeset, we're done. No need to do any pre-vblank
5124 * watermark programming here.
5125 */
5126 if (needs_modeset(&pipe_config->base))
5127 return;
5128
5129 /*
5130 * For platforms that support atomic watermarks, program the
5131 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
5132 * will be the intermediate values that are safe for both pre- and
5133 * post- vblank; when vblank happens, the 'active' values will be set
5134 * to the final 'target' values and we'll do this again to get the
5135 * optimal watermarks. For gen9+ platforms, the values we program here
5136 * will be the final target values which will get automatically latched
5137 * at vblank time; no further programming will be necessary.
5138 *
5139 * If a platform hasn't been transitioned to atomic watermarks yet,
5140 * we'll continue to update watermarks the old way, if flags tell
5141 * us to.
5142 */
5143 if (dev_priv->display.initial_watermarks != NULL)
ccf010fb
ML
5144 dev_priv->display.initial_watermarks(old_intel_state,
5145 pipe_config);
caed361d 5146 else if (pipe_config->update_wm_pre)
432081bc 5147 intel_update_watermarks(crtc);
ac21b225
ML
5148}
5149
d032ffa0 5150static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
87d4300a
ML
5151{
5152 struct drm_device *dev = crtc->dev;
5153 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
d032ffa0 5154 struct drm_plane *p;
87d4300a
ML
5155 int pipe = intel_crtc->pipe;
5156
7cac945f 5157 intel_crtc_dpms_overlay_disable(intel_crtc);
27321ae8 5158
d032ffa0
ML
5159 drm_for_each_plane_mask(p, dev, plane_mask)
5160 to_intel_plane(p)->disable_plane(p, crtc);
f98551ae 5161
f99d7069
DV
5162 /*
5163 * FIXME: Once we grow proper nuclear flip support out of this we need
5164 * to compute the mask of flip planes precisely. For the time being
5165 * consider this a flip to a NULL plane.
5166 */
5748b6a1 5167 intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
5168}
5169
fb1c98b1 5170static void intel_encoders_pre_pll_enable(struct drm_crtc *crtc,
fd6bbda9 5171 struct intel_crtc_state *crtc_state,
fb1c98b1
ML
5172 struct drm_atomic_state *old_state)
5173{
5174 struct drm_connector_state *old_conn_state;
5175 struct drm_connector *conn;
5176 int i;
5177
5178 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5179 struct drm_connector_state *conn_state = conn->state;
5180 struct intel_encoder *encoder =
5181 to_intel_encoder(conn_state->best_encoder);
5182
5183 if (conn_state->crtc != crtc)
5184 continue;
5185
5186 if (encoder->pre_pll_enable)
fd6bbda9 5187 encoder->pre_pll_enable(encoder, crtc_state, conn_state);
fb1c98b1
ML
5188 }
5189}
5190
5191static void intel_encoders_pre_enable(struct drm_crtc *crtc,
fd6bbda9 5192 struct intel_crtc_state *crtc_state,
fb1c98b1
ML
5193 struct drm_atomic_state *old_state)
5194{
5195 struct drm_connector_state *old_conn_state;
5196 struct drm_connector *conn;
5197 int i;
5198
5199 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5200 struct drm_connector_state *conn_state = conn->state;
5201 struct intel_encoder *encoder =
5202 to_intel_encoder(conn_state->best_encoder);
5203
5204 if (conn_state->crtc != crtc)
5205 continue;
5206
5207 if (encoder->pre_enable)
fd6bbda9 5208 encoder->pre_enable(encoder, crtc_state, conn_state);
fb1c98b1
ML
5209 }
5210}
5211
5212static void intel_encoders_enable(struct drm_crtc *crtc,
fd6bbda9 5213 struct intel_crtc_state *crtc_state,
fb1c98b1
ML
5214 struct drm_atomic_state *old_state)
5215{
5216 struct drm_connector_state *old_conn_state;
5217 struct drm_connector *conn;
5218 int i;
5219
5220 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5221 struct drm_connector_state *conn_state = conn->state;
5222 struct intel_encoder *encoder =
5223 to_intel_encoder(conn_state->best_encoder);
5224
5225 if (conn_state->crtc != crtc)
5226 continue;
5227
fd6bbda9 5228 encoder->enable(encoder, crtc_state, conn_state);
fb1c98b1
ML
5229 intel_opregion_notify_encoder(encoder, true);
5230 }
5231}
5232
5233static void intel_encoders_disable(struct drm_crtc *crtc,
fd6bbda9 5234 struct intel_crtc_state *old_crtc_state,
fb1c98b1
ML
5235 struct drm_atomic_state *old_state)
5236{
5237 struct drm_connector_state *old_conn_state;
5238 struct drm_connector *conn;
5239 int i;
5240
5241 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5242 struct intel_encoder *encoder =
5243 to_intel_encoder(old_conn_state->best_encoder);
5244
5245 if (old_conn_state->crtc != crtc)
5246 continue;
5247
5248 intel_opregion_notify_encoder(encoder, false);
fd6bbda9 5249 encoder->disable(encoder, old_crtc_state, old_conn_state);
fb1c98b1
ML
5250 }
5251}
5252
5253static void intel_encoders_post_disable(struct drm_crtc *crtc,
fd6bbda9 5254 struct intel_crtc_state *old_crtc_state,
fb1c98b1
ML
5255 struct drm_atomic_state *old_state)
5256{
5257 struct drm_connector_state *old_conn_state;
5258 struct drm_connector *conn;
5259 int i;
5260
5261 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5262 struct intel_encoder *encoder =
5263 to_intel_encoder(old_conn_state->best_encoder);
5264
5265 if (old_conn_state->crtc != crtc)
5266 continue;
5267
5268 if (encoder->post_disable)
fd6bbda9 5269 encoder->post_disable(encoder, old_crtc_state, old_conn_state);
fb1c98b1
ML
5270 }
5271}
5272
5273static void intel_encoders_post_pll_disable(struct drm_crtc *crtc,
fd6bbda9 5274 struct intel_crtc_state *old_crtc_state,
fb1c98b1
ML
5275 struct drm_atomic_state *old_state)
5276{
5277 struct drm_connector_state *old_conn_state;
5278 struct drm_connector *conn;
5279 int i;
5280
5281 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5282 struct intel_encoder *encoder =
5283 to_intel_encoder(old_conn_state->best_encoder);
5284
5285 if (old_conn_state->crtc != crtc)
5286 continue;
5287
5288 if (encoder->post_pll_disable)
fd6bbda9 5289 encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
fb1c98b1
ML
5290 }
5291}
5292
4a806558
ML
5293static void ironlake_crtc_enable(struct intel_crtc_state *pipe_config,
5294 struct drm_atomic_state *old_state)
f67a559d 5295{
4a806558 5296 struct drm_crtc *crtc = pipe_config->base.crtc;
f67a559d 5297 struct drm_device *dev = crtc->dev;
fac5e23e 5298 struct drm_i915_private *dev_priv = to_i915(dev);
f67a559d
JB
5299 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5300 int pipe = intel_crtc->pipe;
ccf010fb
ML
5301 struct intel_atomic_state *old_intel_state =
5302 to_intel_atomic_state(old_state);
f67a559d 5303
53d9f4e9 5304 if (WARN_ON(intel_crtc->active))
f67a559d
JB
5305 return;
5306
b2c0593a
VS
5307 /*
5308 * Sometimes spurious CPU pipe underruns happen during FDI
5309 * training, at least with VGA+HDMI cloning. Suppress them.
5310 *
5311 * On ILK we get an occasional spurious CPU pipe underruns
5312 * between eDP port A enable and vdd enable. Also PCH port
5313 * enable seems to result in the occasional CPU pipe underrun.
5314 *
5315 * Spurious PCH underruns also occur during PCH enabling.
5316 */
5317 if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
5318 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
81b088ca
VS
5319 if (intel_crtc->config->has_pch_encoder)
5320 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
5321
6e3c9717 5322 if (intel_crtc->config->has_pch_encoder)
b14b1055
DV
5323 intel_prepare_shared_dpll(intel_crtc);
5324
37a5650b 5325 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 5326 intel_dp_set_m_n(intel_crtc, M1_N1);
29407aab
DV
5327
5328 intel_set_pipe_timings(intel_crtc);
bc58be60 5329 intel_set_pipe_src_size(intel_crtc);
29407aab 5330
6e3c9717 5331 if (intel_crtc->config->has_pch_encoder) {
29407aab 5332 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 5333 &intel_crtc->config->fdi_m_n, NULL);
29407aab
DV
5334 }
5335
5336 ironlake_set_pipeconf(crtc);
5337
f67a559d 5338 intel_crtc->active = true;
8664281b 5339
fd6bbda9 5340 intel_encoders_pre_enable(crtc, pipe_config, old_state);
f67a559d 5341
6e3c9717 5342 if (intel_crtc->config->has_pch_encoder) {
fff367c7
DV
5343 /* Note: FDI PLL enabling _must_ be done before we enable the
5344 * cpu pipes, hence this is separate from all the other fdi/pch
5345 * enabling. */
88cefb6c 5346 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
DV
5347 } else {
5348 assert_fdi_tx_disabled(dev_priv, pipe);
5349 assert_fdi_rx_disabled(dev_priv, pipe);
5350 }
f67a559d 5351
b074cec8 5352 ironlake_pfit_enable(intel_crtc);
f67a559d 5353
9c54c0dd
JB
5354 /*
5355 * On ILK+ LUT must be loaded before the pipe is running but with
5356 * clocks enabled
5357 */
b95c5321 5358 intel_color_load_luts(&pipe_config->base);
9c54c0dd 5359
1d5bf5d9 5360 if (dev_priv->display.initial_watermarks != NULL)
ccf010fb 5361 dev_priv->display.initial_watermarks(old_intel_state, intel_crtc->config);
e1fdc473 5362 intel_enable_pipe(intel_crtc);
f67a559d 5363
6e3c9717 5364 if (intel_crtc->config->has_pch_encoder)
f67a559d 5365 ironlake_pch_enable(crtc);
c98e9dcf 5366
f9b61ff6
DV
5367 assert_vblank_disabled(crtc);
5368 drm_crtc_vblank_on(crtc);
5369
fd6bbda9 5370 intel_encoders_enable(crtc, pipe_config, old_state);
61b77ddd 5371
6e266956 5372 if (HAS_PCH_CPT(dev_priv))
a1520318 5373 cpt_verify_modeset(dev, intel_crtc->pipe);
37ca8d4c
VS
5374
5375 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
5376 if (intel_crtc->config->has_pch_encoder)
0f0f74bc 5377 intel_wait_for_vblank(dev_priv, pipe);
b2c0593a 5378 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
37ca8d4c 5379 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607
JB
5380}
5381
42db64ef
PZ
5382/* IPS only exists on ULT machines and is tied to pipe A. */
5383static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
5384{
50a0bc90 5385 return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
42db64ef
PZ
5386}
5387
4a806558
ML
5388static void haswell_crtc_enable(struct intel_crtc_state *pipe_config,
5389 struct drm_atomic_state *old_state)
4f771f10 5390{
4a806558 5391 struct drm_crtc *crtc = pipe_config->base.crtc;
6315b5d3 5392 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
4f771f10 5393 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
99d736a2 5394 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
4d1de975 5395 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ccf010fb
ML
5396 struct intel_atomic_state *old_intel_state =
5397 to_intel_atomic_state(old_state);
4f771f10 5398
53d9f4e9 5399 if (WARN_ON(intel_crtc->active))
4f771f10
PZ
5400 return;
5401
81b088ca
VS
5402 if (intel_crtc->config->has_pch_encoder)
5403 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5404 false);
5405
fd6bbda9 5406 intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
95a7a2ae 5407
8106ddbd 5408 if (intel_crtc->config->shared_dpll)
df8ad70c
DV
5409 intel_enable_shared_dpll(intel_crtc);
5410
37a5650b 5411 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 5412 intel_dp_set_m_n(intel_crtc, M1_N1);
229fca97 5413
d7edc4e5 5414 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975
JN
5415 intel_set_pipe_timings(intel_crtc);
5416
bc58be60 5417 intel_set_pipe_src_size(intel_crtc);
229fca97 5418
4d1de975
JN
5419 if (cpu_transcoder != TRANSCODER_EDP &&
5420 !transcoder_is_dsi(cpu_transcoder)) {
5421 I915_WRITE(PIPE_MULT(cpu_transcoder),
6e3c9717 5422 intel_crtc->config->pixel_multiplier - 1);
ebb69c95
CT
5423 }
5424
6e3c9717 5425 if (intel_crtc->config->has_pch_encoder) {
229fca97 5426 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 5427 &intel_crtc->config->fdi_m_n, NULL);
229fca97
DV
5428 }
5429
d7edc4e5 5430 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975
JN
5431 haswell_set_pipeconf(crtc);
5432
391bf048 5433 haswell_set_pipemisc(crtc);
229fca97 5434
b95c5321 5435 intel_color_set_csc(&pipe_config->base);
229fca97 5436
4f771f10 5437 intel_crtc->active = true;
8664281b 5438
6b698516
DV
5439 if (intel_crtc->config->has_pch_encoder)
5440 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
5441 else
5442 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5443
fd6bbda9 5444 intel_encoders_pre_enable(crtc, pipe_config, old_state);
4f771f10 5445
d2d65408 5446 if (intel_crtc->config->has_pch_encoder)
4fe9467d 5447 dev_priv->display.fdi_link_train(crtc);
4fe9467d 5448
d7edc4e5 5449 if (!transcoder_is_dsi(cpu_transcoder))
7d4aefd0 5450 intel_ddi_enable_pipe_clock(intel_crtc);
4f771f10 5451
6315b5d3 5452 if (INTEL_GEN(dev_priv) >= 9)
e435d6e5 5453 skylake_pfit_enable(intel_crtc);
ff6d9f55 5454 else
1c132b44 5455 ironlake_pfit_enable(intel_crtc);
4f771f10
PZ
5456
5457 /*
5458 * On ILK+ LUT must be loaded before the pipe is running but with
5459 * clocks enabled
5460 */
b95c5321 5461 intel_color_load_luts(&pipe_config->base);
4f771f10 5462
1f544388 5463 intel_ddi_set_pipe_settings(crtc);
d7edc4e5 5464 if (!transcoder_is_dsi(cpu_transcoder))
7d4aefd0 5465 intel_ddi_enable_transcoder_func(crtc);
4f771f10 5466
1d5bf5d9 5467 if (dev_priv->display.initial_watermarks != NULL)
3125d39f 5468 dev_priv->display.initial_watermarks(old_intel_state, pipe_config);
4d1de975
JN
5469
5470 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
d7edc4e5 5471 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975 5472 intel_enable_pipe(intel_crtc);
42db64ef 5473
6e3c9717 5474 if (intel_crtc->config->has_pch_encoder)
1507e5bd 5475 lpt_pch_enable(crtc);
4f771f10 5476
0037071d 5477 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
0e32b39c
DA
5478 intel_ddi_set_vc_payload_alloc(crtc, true);
5479
f9b61ff6
DV
5480 assert_vblank_disabled(crtc);
5481 drm_crtc_vblank_on(crtc);
5482
fd6bbda9 5483 intel_encoders_enable(crtc, pipe_config, old_state);
4f771f10 5484
6b698516 5485 if (intel_crtc->config->has_pch_encoder) {
0f0f74bc
VS
5486 intel_wait_for_vblank(dev_priv, pipe);
5487 intel_wait_for_vblank(dev_priv, pipe);
6b698516 5488 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
d2d65408
VS
5489 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5490 true);
6b698516 5491 }
d2d65408 5492
e4916946
PZ
5493 /* If we change the relative order between pipe/planes enabling, we need
5494 * to change the workaround. */
99d736a2 5495 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
772c2a51 5496 if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
0f0f74bc
VS
5497 intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
5498 intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
99d736a2 5499 }
4f771f10
PZ
5500}
5501
bfd16b2a 5502static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
3f8dce3a
DV
5503{
5504 struct drm_device *dev = crtc->base.dev;
fac5e23e 5505 struct drm_i915_private *dev_priv = to_i915(dev);
3f8dce3a
DV
5506 int pipe = crtc->pipe;
5507
5508 /* To avoid upsetting the power well on haswell only disable the pfit if
5509 * it's in use. The hw state code will make sure we get this right. */
bfd16b2a 5510 if (force || crtc->config->pch_pfit.enabled) {
3f8dce3a
DV
5511 I915_WRITE(PF_CTL(pipe), 0);
5512 I915_WRITE(PF_WIN_POS(pipe), 0);
5513 I915_WRITE(PF_WIN_SZ(pipe), 0);
5514 }
5515}
5516
4a806558
ML
5517static void ironlake_crtc_disable(struct intel_crtc_state *old_crtc_state,
5518 struct drm_atomic_state *old_state)
6be4a607 5519{
4a806558 5520 struct drm_crtc *crtc = old_crtc_state->base.crtc;
6be4a607 5521 struct drm_device *dev = crtc->dev;
fac5e23e 5522 struct drm_i915_private *dev_priv = to_i915(dev);
6be4a607
JB
5523 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5524 int pipe = intel_crtc->pipe;
b52eb4dc 5525
b2c0593a
VS
5526 /*
5527 * Sometimes spurious CPU pipe underruns happen when the
5528 * pipe is already disabled, but FDI RX/TX is still enabled.
5529 * Happens at least with VGA+HDMI cloning. Suppress them.
5530 */
5531 if (intel_crtc->config->has_pch_encoder) {
5532 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
37ca8d4c 5533 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
b2c0593a 5534 }
37ca8d4c 5535
fd6bbda9 5536 intel_encoders_disable(crtc, old_crtc_state, old_state);
ea9d758d 5537
f9b61ff6
DV
5538 drm_crtc_vblank_off(crtc);
5539 assert_vblank_disabled(crtc);
5540
575f7ab7 5541 intel_disable_pipe(intel_crtc);
32f9d658 5542
bfd16b2a 5543 ironlake_pfit_disable(intel_crtc, false);
2c07245f 5544
b2c0593a 5545 if (intel_crtc->config->has_pch_encoder)
5a74f70a
VS
5546 ironlake_fdi_disable(crtc);
5547
fd6bbda9 5548 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
2c07245f 5549
6e3c9717 5550 if (intel_crtc->config->has_pch_encoder) {
d925c59a 5551 ironlake_disable_pch_transcoder(dev_priv, pipe);
6be4a607 5552
6e266956 5553 if (HAS_PCH_CPT(dev_priv)) {
f0f59a00
VS
5554 i915_reg_t reg;
5555 u32 temp;
5556
d925c59a
DV
5557 /* disable TRANS_DP_CTL */
5558 reg = TRANS_DP_CTL(pipe);
5559 temp = I915_READ(reg);
5560 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5561 TRANS_DP_PORT_SEL_MASK);
5562 temp |= TRANS_DP_PORT_SEL_NONE;
5563 I915_WRITE(reg, temp);
5564
5565 /* disable DPLL_SEL */
5566 temp = I915_READ(PCH_DPLL_SEL);
11887397 5567 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 5568 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 5569 }
e3421a18 5570
d925c59a
DV
5571 ironlake_fdi_pll_disable(intel_crtc);
5572 }
81b088ca 5573
b2c0593a 5574 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
81b088ca 5575 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607 5576}
1b3c7a47 5577
4a806558
ML
5578static void haswell_crtc_disable(struct intel_crtc_state *old_crtc_state,
5579 struct drm_atomic_state *old_state)
ee7b9f93 5580{
4a806558 5581 struct drm_crtc *crtc = old_crtc_state->base.crtc;
6315b5d3 5582 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
ee7b9f93 5583 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 5584 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee7b9f93 5585
d2d65408
VS
5586 if (intel_crtc->config->has_pch_encoder)
5587 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5588 false);
5589
fd6bbda9 5590 intel_encoders_disable(crtc, old_crtc_state, old_state);
4f771f10 5591
f9b61ff6
DV
5592 drm_crtc_vblank_off(crtc);
5593 assert_vblank_disabled(crtc);
5594
4d1de975 5595 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
d7edc4e5 5596 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975 5597 intel_disable_pipe(intel_crtc);
4f771f10 5598
0037071d 5599 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
a4bf214f
VS
5600 intel_ddi_set_vc_payload_alloc(crtc, false);
5601
d7edc4e5 5602 if (!transcoder_is_dsi(cpu_transcoder))
7d4aefd0 5603 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4f771f10 5604
6315b5d3 5605 if (INTEL_GEN(dev_priv) >= 9)
e435d6e5 5606 skylake_scaler_disable(intel_crtc);
ff6d9f55 5607 else
bfd16b2a 5608 ironlake_pfit_disable(intel_crtc, false);
4f771f10 5609
d7edc4e5 5610 if (!transcoder_is_dsi(cpu_transcoder))
7d4aefd0 5611 intel_ddi_disable_pipe_clock(intel_crtc);
4f771f10 5612
fd6bbda9 5613 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
81b088ca 5614
b7076546 5615 if (old_crtc_state->has_pch_encoder)
81b088ca
VS
5616 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5617 true);
4f771f10
PZ
5618}
5619
2dd24552
JB
5620static void i9xx_pfit_enable(struct intel_crtc *crtc)
5621{
5622 struct drm_device *dev = crtc->base.dev;
fac5e23e 5623 struct drm_i915_private *dev_priv = to_i915(dev);
6e3c9717 5624 struct intel_crtc_state *pipe_config = crtc->config;
2dd24552 5625
681a8504 5626 if (!pipe_config->gmch_pfit.control)
2dd24552
JB
5627 return;
5628
2dd24552 5629 /*
c0b03411
DV
5630 * The panel fitter should only be adjusted whilst the pipe is disabled,
5631 * according to register description and PRM.
2dd24552 5632 */
c0b03411
DV
5633 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5634 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 5635
b074cec8
JB
5636 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5637 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
DV
5638
5639 /* Border color in case we don't scale up to the full screen. Black by
5640 * default, change to something else for debugging. */
5641 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
5642}
5643
d05410f9
DA
5644static enum intel_display_power_domain port_to_power_domain(enum port port)
5645{
5646 switch (port) {
5647 case PORT_A:
6331a704 5648 return POWER_DOMAIN_PORT_DDI_A_LANES;
d05410f9 5649 case PORT_B:
6331a704 5650 return POWER_DOMAIN_PORT_DDI_B_LANES;
d05410f9 5651 case PORT_C:
6331a704 5652 return POWER_DOMAIN_PORT_DDI_C_LANES;
d05410f9 5653 case PORT_D:
6331a704 5654 return POWER_DOMAIN_PORT_DDI_D_LANES;
d8e19f99 5655 case PORT_E:
6331a704 5656 return POWER_DOMAIN_PORT_DDI_E_LANES;
d05410f9 5657 default:
b9fec167 5658 MISSING_CASE(port);
d05410f9
DA
5659 return POWER_DOMAIN_PORT_OTHER;
5660 }
5661}
5662
25f78f58
VS
5663static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5664{
5665 switch (port) {
5666 case PORT_A:
5667 return POWER_DOMAIN_AUX_A;
5668 case PORT_B:
5669 return POWER_DOMAIN_AUX_B;
5670 case PORT_C:
5671 return POWER_DOMAIN_AUX_C;
5672 case PORT_D:
5673 return POWER_DOMAIN_AUX_D;
5674 case PORT_E:
5675 /* FIXME: Check VBT for actual wiring of PORT E */
5676 return POWER_DOMAIN_AUX_D;
5677 default:
b9fec167 5678 MISSING_CASE(port);
25f78f58
VS
5679 return POWER_DOMAIN_AUX_A;
5680 }
5681}
5682
319be8ae
ID
5683enum intel_display_power_domain
5684intel_display_port_power_domain(struct intel_encoder *intel_encoder)
5685{
4f8036a2 5686 struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
319be8ae
ID
5687 struct intel_digital_port *intel_dig_port;
5688
5689 switch (intel_encoder->type) {
5690 case INTEL_OUTPUT_UNKNOWN:
5691 /* Only DDI platforms should ever use this output type */
4f8036a2 5692 WARN_ON_ONCE(!HAS_DDI(dev_priv));
cca0502b 5693 case INTEL_OUTPUT_DP:
319be8ae
ID
5694 case INTEL_OUTPUT_HDMI:
5695 case INTEL_OUTPUT_EDP:
5696 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
d05410f9 5697 return port_to_power_domain(intel_dig_port->port);
0e32b39c
DA
5698 case INTEL_OUTPUT_DP_MST:
5699 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5700 return port_to_power_domain(intel_dig_port->port);
319be8ae
ID
5701 case INTEL_OUTPUT_ANALOG:
5702 return POWER_DOMAIN_PORT_CRT;
5703 case INTEL_OUTPUT_DSI:
5704 return POWER_DOMAIN_PORT_DSI;
5705 default:
5706 return POWER_DOMAIN_PORT_OTHER;
5707 }
5708}
5709
25f78f58
VS
5710enum intel_display_power_domain
5711intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5712{
4f8036a2 5713 struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
25f78f58
VS
5714 struct intel_digital_port *intel_dig_port;
5715
5716 switch (intel_encoder->type) {
5717 case INTEL_OUTPUT_UNKNOWN:
651174a4
ID
5718 case INTEL_OUTPUT_HDMI:
5719 /*
5720 * Only DDI platforms should ever use these output types.
5721 * We can get here after the HDMI detect code has already set
5722 * the type of the shared encoder. Since we can't be sure
5723 * what's the status of the given connectors, play safe and
5724 * run the DP detection too.
5725 */
4f8036a2 5726 WARN_ON_ONCE(!HAS_DDI(dev_priv));
cca0502b 5727 case INTEL_OUTPUT_DP:
25f78f58
VS
5728 case INTEL_OUTPUT_EDP:
5729 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5730 return port_to_aux_power_domain(intel_dig_port->port);
5731 case INTEL_OUTPUT_DP_MST:
5732 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5733 return port_to_aux_power_domain(intel_dig_port->port);
5734 default:
b9fec167 5735 MISSING_CASE(intel_encoder->type);
25f78f58
VS
5736 return POWER_DOMAIN_AUX_A;
5737 }
5738}
5739
74bff5f9
ML
5740static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
5741 struct intel_crtc_state *crtc_state)
77d22dca 5742{
319be8ae 5743 struct drm_device *dev = crtc->dev;
74bff5f9 5744 struct drm_encoder *encoder;
319be8ae
ID
5745 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5746 enum pipe pipe = intel_crtc->pipe;
77d22dca 5747 unsigned long mask;
74bff5f9 5748 enum transcoder transcoder = crtc_state->cpu_transcoder;
77d22dca 5749
74bff5f9 5750 if (!crtc_state->base.active)
292b990e
ML
5751 return 0;
5752
77d22dca
ID
5753 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5754 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
74bff5f9
ML
5755 if (crtc_state->pch_pfit.enabled ||
5756 crtc_state->pch_pfit.force_thru)
77d22dca
ID
5757 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5758
74bff5f9
ML
5759 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5760 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5761
319be8ae 5762 mask |= BIT(intel_display_port_power_domain(intel_encoder));
74bff5f9 5763 }
319be8ae 5764
15e7ec29
ML
5765 if (crtc_state->shared_dpll)
5766 mask |= BIT(POWER_DOMAIN_PLLS);
5767
77d22dca
ID
5768 return mask;
5769}
5770
74bff5f9
ML
5771static unsigned long
5772modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5773 struct intel_crtc_state *crtc_state)
77d22dca 5774{
fac5e23e 5775 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
292b990e
ML
5776 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5777 enum intel_display_power_domain domain;
5a21b665 5778 unsigned long domains, new_domains, old_domains;
77d22dca 5779
292b990e 5780 old_domains = intel_crtc->enabled_power_domains;
74bff5f9
ML
5781 intel_crtc->enabled_power_domains = new_domains =
5782 get_crtc_power_domains(crtc, crtc_state);
77d22dca 5783
5a21b665 5784 domains = new_domains & ~old_domains;
292b990e
ML
5785
5786 for_each_power_domain(domain, domains)
5787 intel_display_power_get(dev_priv, domain);
5788
5a21b665 5789 return old_domains & ~new_domains;
292b990e
ML
5790}
5791
5792static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5793 unsigned long domains)
5794{
5795 enum intel_display_power_domain domain;
5796
5797 for_each_power_domain(domain, domains)
5798 intel_display_power_put(dev_priv, domain);
5799}
77d22dca 5800
adafdc6f
MK
5801static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5802{
5803 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5804
09d09386
ACO
5805 if (IS_GEMINILAKE(dev_priv))
5806 return 2 * max_cdclk_freq;
5807 else if (INTEL_INFO(dev_priv)->gen >= 9 ||
5808 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
adafdc6f
MK
5809 return max_cdclk_freq;
5810 else if (IS_CHERRYVIEW(dev_priv))
5811 return max_cdclk_freq*95/100;
5812 else if (INTEL_INFO(dev_priv)->gen < 4)
5813 return 2*max_cdclk_freq*90/100;
5814 else
5815 return max_cdclk_freq*90/100;
5816}
5817
b2045352
VS
5818static int skl_calc_cdclk(int max_pixclk, int vco);
5819
4c75b940 5820static void intel_update_max_cdclk(struct drm_i915_private *dev_priv)
560a7ae4 5821{
0853723b 5822 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
560a7ae4 5823 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
b2045352
VS
5824 int max_cdclk, vco;
5825
5826 vco = dev_priv->skl_preferred_vco_freq;
63911d72 5827 WARN_ON(vco != 8100000 && vco != 8640000);
560a7ae4 5828
b2045352
VS
5829 /*
5830 * Use the lower (vco 8640) cdclk values as a
5831 * first guess. skl_calc_cdclk() will correct it
5832 * if the preferred vco is 8100 instead.
5833 */
560a7ae4 5834 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
487ed2e4 5835 max_cdclk = 617143;
560a7ae4 5836 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
b2045352 5837 max_cdclk = 540000;
560a7ae4 5838 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
b2045352 5839 max_cdclk = 432000;
560a7ae4 5840 else
487ed2e4 5841 max_cdclk = 308571;
b2045352
VS
5842
5843 dev_priv->max_cdclk_freq = skl_calc_cdclk(max_cdclk, vco);
89b3c3c7
ACO
5844 } else if (IS_GEMINILAKE(dev_priv)) {
5845 dev_priv->max_cdclk_freq = 316800;
e2d214ae 5846 } else if (IS_BROXTON(dev_priv)) {
281c114f 5847 dev_priv->max_cdclk_freq = 624000;
8652744b 5848 } else if (IS_BROADWELL(dev_priv)) {
560a7ae4
DL
5849 /*
5850 * FIXME with extra cooling we can allow
5851 * 540 MHz for ULX and 675 Mhz for ULT.
5852 * How can we know if extra cooling is
5853 * available? PCI ID, VTB, something else?
5854 */
5855 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5856 dev_priv->max_cdclk_freq = 450000;
50a0bc90 5857 else if (IS_BDW_ULX(dev_priv))
560a7ae4 5858 dev_priv->max_cdclk_freq = 450000;
50a0bc90 5859 else if (IS_BDW_ULT(dev_priv))
560a7ae4
DL
5860 dev_priv->max_cdclk_freq = 540000;
5861 else
5862 dev_priv->max_cdclk_freq = 675000;
920a14b2 5863 } else if (IS_CHERRYVIEW(dev_priv)) {
0904deaf 5864 dev_priv->max_cdclk_freq = 320000;
11a914c2 5865 } else if (IS_VALLEYVIEW(dev_priv)) {
560a7ae4
DL
5866 dev_priv->max_cdclk_freq = 400000;
5867 } else {
5868 /* otherwise assume cdclk is fixed */
5869 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5870 }
5871
adafdc6f
MK
5872 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5873
560a7ae4
DL
5874 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5875 dev_priv->max_cdclk_freq);
adafdc6f
MK
5876
5877 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5878 dev_priv->max_dotclk_freq);
560a7ae4
DL
5879}
5880
4c75b940 5881static void intel_update_cdclk(struct drm_i915_private *dev_priv)
560a7ae4 5882{
1353c4fb 5883 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev_priv);
2f2a121a 5884
83d7c81f 5885 if (INTEL_GEN(dev_priv) >= 9)
709e05c3
VS
5886 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz, VCO: %d kHz, ref: %d kHz\n",
5887 dev_priv->cdclk_freq, dev_priv->cdclk_pll.vco,
5888 dev_priv->cdclk_pll.ref);
2f2a121a
VS
5889 else
5890 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5891 dev_priv->cdclk_freq);
560a7ae4
DL
5892
5893 /*
b5d99ff9
VS
5894 * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
5895 * Programmng [sic] note: bit[9:2] should be programmed to the number
5896 * of cdclk that generates 4MHz reference clock freq which is used to
5897 * generate GMBus clock. This will vary with the cdclk freq.
560a7ae4 5898 */
b5d99ff9 5899 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
560a7ae4 5900 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
560a7ae4
DL
5901}
5902
92891e45
VS
5903/* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5904static int skl_cdclk_decimal(int cdclk)
5905{
5906 return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
5907}
5908
5f199dfa
VS
5909static int bxt_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
5910{
5911 int ratio;
5912
5913 if (cdclk == dev_priv->cdclk_pll.ref)
5914 return 0;
5915
5916 switch (cdclk) {
5917 default:
5918 MISSING_CASE(cdclk);
5919 case 144000:
5920 case 288000:
5921 case 384000:
5922 case 576000:
5923 ratio = 60;
5924 break;
5925 case 624000:
5926 ratio = 65;
5927 break;
5928 }
5929
5930 return dev_priv->cdclk_pll.ref * ratio;
5931}
5932
89b3c3c7
ACO
5933static int glk_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
5934{
5935 int ratio;
5936
5937 if (cdclk == dev_priv->cdclk_pll.ref)
5938 return 0;
5939
5940 switch (cdclk) {
5941 default:
5942 MISSING_CASE(cdclk);
5943 case 79200:
5944 case 158400:
5945 case 316800:
5946 ratio = 33;
5947 break;
5948 }
5949
5950 return dev_priv->cdclk_pll.ref * ratio;
5951}
5952
2b73001e
VS
5953static void bxt_de_pll_disable(struct drm_i915_private *dev_priv)
5954{
5955 I915_WRITE(BXT_DE_PLL_ENABLE, 0);
5956
5957 /* Timeout 200us */
95cac283
CW
5958 if (intel_wait_for_register(dev_priv,
5959 BXT_DE_PLL_ENABLE, BXT_DE_PLL_LOCK, 0,
5960 1))
2b73001e 5961 DRM_ERROR("timeout waiting for DE PLL unlock\n");
83d7c81f
VS
5962
5963 dev_priv->cdclk_pll.vco = 0;
2b73001e
VS
5964}
5965
5f199dfa 5966static void bxt_de_pll_enable(struct drm_i915_private *dev_priv, int vco)
2b73001e 5967{
5f199dfa 5968 int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->cdclk_pll.ref);
2b73001e
VS
5969 u32 val;
5970
5971 val = I915_READ(BXT_DE_PLL_CTL);
5972 val &= ~BXT_DE_PLL_RATIO_MASK;
5f199dfa 5973 val |= BXT_DE_PLL_RATIO(ratio);
2b73001e
VS
5974 I915_WRITE(BXT_DE_PLL_CTL, val);
5975
5976 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5977
5978 /* Timeout 200us */
e084e1b9
CW
5979 if (intel_wait_for_register(dev_priv,
5980 BXT_DE_PLL_ENABLE,
5981 BXT_DE_PLL_LOCK,
5982 BXT_DE_PLL_LOCK,
5983 1))
2b73001e 5984 DRM_ERROR("timeout waiting for DE PLL lock\n");
83d7c81f 5985
5f199dfa 5986 dev_priv->cdclk_pll.vco = vco;
2b73001e
VS
5987}
5988
324513c0 5989static void bxt_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
f8437dd1 5990{
5f199dfa
VS
5991 u32 val, divider;
5992 int vco, ret;
f8437dd1 5993
89b3c3c7
ACO
5994 if (IS_GEMINILAKE(dev_priv))
5995 vco = glk_de_pll_vco(dev_priv, cdclk);
5996 else
5997 vco = bxt_de_pll_vco(dev_priv, cdclk);
5f199dfa
VS
5998
5999 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
6000
6001 /* cdclk = vco / 2 / div{1,1.5,2,4} */
6002 switch (DIV_ROUND_CLOSEST(vco, cdclk)) {
6003 case 8:
f8437dd1 6004 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
f8437dd1 6005 break;
5f199dfa 6006 case 4:
f8437dd1 6007 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
f8437dd1 6008 break;
5f199dfa 6009 case 3:
89b3c3c7 6010 WARN(IS_GEMINILAKE(dev_priv), "Unsupported divider\n");
f8437dd1 6011 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
f8437dd1 6012 break;
5f199dfa 6013 case 2:
f8437dd1 6014 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
f8437dd1
VK
6015 break;
6016 default:
5f199dfa
VS
6017 WARN_ON(cdclk != dev_priv->cdclk_pll.ref);
6018 WARN_ON(vco != 0);
f8437dd1 6019
5f199dfa
VS
6020 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
6021 break;
f8437dd1
VK
6022 }
6023
f8437dd1 6024 /* Inform power controller of upcoming frequency change */
5f199dfa 6025 mutex_lock(&dev_priv->rps.hw_lock);
f8437dd1
VK
6026 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
6027 0x80000000);
6028 mutex_unlock(&dev_priv->rps.hw_lock);
6029
6030 if (ret) {
6031 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
9ef56154 6032 ret, cdclk);
f8437dd1
VK
6033 return;
6034 }
6035
5f199dfa
VS
6036 if (dev_priv->cdclk_pll.vco != 0 &&
6037 dev_priv->cdclk_pll.vco != vco)
2b73001e 6038 bxt_de_pll_disable(dev_priv);
f8437dd1 6039
5f199dfa
VS
6040 if (dev_priv->cdclk_pll.vco != vco)
6041 bxt_de_pll_enable(dev_priv, vco);
f8437dd1 6042
5f199dfa
VS
6043 val = divider | skl_cdclk_decimal(cdclk);
6044 /*
6045 * FIXME if only the cd2x divider needs changing, it could be done
6046 * without shutting off the pipe (if only one pipe is active).
6047 */
6048 val |= BXT_CDCLK_CD2X_PIPE_NONE;
6049 /*
6050 * Disable SSA Precharge when CD clock frequency < 500 MHz,
6051 * enable otherwise.
6052 */
6053 if (cdclk >= 500000)
6054 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
6055 I915_WRITE(CDCLK_CTL, val);
f8437dd1
VK
6056
6057 mutex_lock(&dev_priv->rps.hw_lock);
6058 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
9ef56154 6059 DIV_ROUND_UP(cdclk, 25000));
f8437dd1
VK
6060 mutex_unlock(&dev_priv->rps.hw_lock);
6061
6062 if (ret) {
6063 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
9ef56154 6064 ret, cdclk);
f8437dd1
VK
6065 return;
6066 }
6067
4c75b940 6068 intel_update_cdclk(dev_priv);
f8437dd1
VK
6069}
6070
d66a2194 6071static void bxt_sanitize_cdclk(struct drm_i915_private *dev_priv)
f8437dd1 6072{
d66a2194
ID
6073 u32 cdctl, expected;
6074
4c75b940 6075 intel_update_cdclk(dev_priv);
f8437dd1 6076
d66a2194
ID
6077 if (dev_priv->cdclk_pll.vco == 0 ||
6078 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
6079 goto sanitize;
6080
6081 /* DPLL okay; verify the cdclock
6082 *
6083 * Some BIOS versions leave an incorrect decimal frequency value and
6084 * set reserved MBZ bits in CDCLK_CTL at least during exiting from S4,
6085 * so sanitize this register.
6086 */
6087 cdctl = I915_READ(CDCLK_CTL);
6088 /*
6089 * Let's ignore the pipe field, since BIOS could have configured the
6090 * dividers both synching to an active pipe, or asynchronously
6091 * (PIPE_NONE).
6092 */
6093 cdctl &= ~BXT_CDCLK_CD2X_PIPE_NONE;
6094
6095 expected = (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) |
6096 skl_cdclk_decimal(dev_priv->cdclk_freq);
6097 /*
6098 * Disable SSA Precharge when CD clock frequency < 500 MHz,
6099 * enable otherwise.
6100 */
6101 if (dev_priv->cdclk_freq >= 500000)
6102 expected |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
6103
6104 if (cdctl == expected)
6105 /* All well; nothing to sanitize */
6106 return;
6107
6108sanitize:
6109 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
6110
6111 /* force cdclk programming */
6112 dev_priv->cdclk_freq = 0;
6113
6114 /* force full PLL disable + enable */
6115 dev_priv->cdclk_pll.vco = -1;
6116}
6117
324513c0 6118void bxt_init_cdclk(struct drm_i915_private *dev_priv)
d66a2194 6119{
89b3c3c7
ACO
6120 int cdclk;
6121
d66a2194
ID
6122 bxt_sanitize_cdclk(dev_priv);
6123
6124 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0)
089c6fd5 6125 return;
c2e001ef 6126
f8437dd1
VK
6127 /*
6128 * FIXME:
6129 * - The initial CDCLK needs to be read from VBT.
6130 * Need to make this change after VBT has changes for BXT.
f8437dd1 6131 */
89b3c3c7
ACO
6132 if (IS_GEMINILAKE(dev_priv))
6133 cdclk = glk_calc_cdclk(0);
6134 else
6135 cdclk = bxt_calc_cdclk(0);
6136
6137 bxt_set_cdclk(dev_priv, cdclk);
f8437dd1
VK
6138}
6139
324513c0 6140void bxt_uninit_cdclk(struct drm_i915_private *dev_priv)
f8437dd1 6141{
324513c0 6142 bxt_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref);
f8437dd1
VK
6143}
6144
a8ca4934
VS
6145static int skl_calc_cdclk(int max_pixclk, int vco)
6146{
63911d72 6147 if (vco == 8640000) {
a8ca4934 6148 if (max_pixclk > 540000)
487ed2e4 6149 return 617143;
a8ca4934
VS
6150 else if (max_pixclk > 432000)
6151 return 540000;
487ed2e4 6152 else if (max_pixclk > 308571)
a8ca4934
VS
6153 return 432000;
6154 else
487ed2e4 6155 return 308571;
a8ca4934 6156 } else {
a8ca4934
VS
6157 if (max_pixclk > 540000)
6158 return 675000;
6159 else if (max_pixclk > 450000)
6160 return 540000;
6161 else if (max_pixclk > 337500)
6162 return 450000;
6163 else
6164 return 337500;
6165 }
6166}
6167
ea61791e
VS
6168static void
6169skl_dpll0_update(struct drm_i915_private *dev_priv)
5d96d8af 6170{
ea61791e 6171 u32 val;
5d96d8af 6172
709e05c3 6173 dev_priv->cdclk_pll.ref = 24000;
1c3f7700 6174 dev_priv->cdclk_pll.vco = 0;
709e05c3 6175
ea61791e 6176 val = I915_READ(LCPLL1_CTL);
1c3f7700 6177 if ((val & LCPLL_PLL_ENABLE) == 0)
ea61791e 6178 return;
5d96d8af 6179
1c3f7700
ID
6180 if (WARN_ON((val & LCPLL_PLL_LOCK) == 0))
6181 return;
9f7eb31a 6182
ea61791e
VS
6183 val = I915_READ(DPLL_CTRL1);
6184
1c3f7700
ID
6185 if (WARN_ON((val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
6186 DPLL_CTRL1_SSC(SKL_DPLL0) |
6187 DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
6188 DPLL_CTRL1_OVERRIDE(SKL_DPLL0)))
6189 return;
9f7eb31a 6190
ea61791e
VS
6191 switch (val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) {
6192 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0):
6193 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, SKL_DPLL0):
6194 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, SKL_DPLL0):
6195 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, SKL_DPLL0):
63911d72 6196 dev_priv->cdclk_pll.vco = 8100000;
ea61791e
VS
6197 break;
6198 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0):
6199 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, SKL_DPLL0):
63911d72 6200 dev_priv->cdclk_pll.vco = 8640000;
ea61791e
VS
6201 break;
6202 default:
6203 MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
ea61791e
VS
6204 break;
6205 }
5d96d8af
DL
6206}
6207
b2045352
VS
6208void skl_set_preferred_cdclk_vco(struct drm_i915_private *dev_priv, int vco)
6209{
6210 bool changed = dev_priv->skl_preferred_vco_freq != vco;
6211
6212 dev_priv->skl_preferred_vco_freq = vco;
6213
6214 if (changed)
4c75b940 6215 intel_update_max_cdclk(dev_priv);
b2045352
VS
6216}
6217
5d96d8af 6218static void
3861fc60 6219skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
5d96d8af 6220{
a8ca4934 6221 int min_cdclk = skl_calc_cdclk(0, vco);
5d96d8af
DL
6222 u32 val;
6223
63911d72 6224 WARN_ON(vco != 8100000 && vco != 8640000);
b2045352 6225
5d96d8af 6226 /* select the minimum CDCLK before enabling DPLL 0 */
9ef56154 6227 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_cdclk);
5d96d8af
DL
6228 I915_WRITE(CDCLK_CTL, val);
6229 POSTING_READ(CDCLK_CTL);
6230
6231 /*
6232 * We always enable DPLL0 with the lowest link rate possible, but still
6233 * taking into account the VCO required to operate the eDP panel at the
6234 * desired frequency. The usual DP link rates operate with a VCO of
6235 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
6236 * The modeset code is responsible for the selection of the exact link
6237 * rate later on, with the constraint of choosing a frequency that
a8ca4934 6238 * works with vco.
5d96d8af
DL
6239 */
6240 val = I915_READ(DPLL_CTRL1);
6241
6242 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
6243 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
6244 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
63911d72 6245 if (vco == 8640000)
5d96d8af
DL
6246 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
6247 SKL_DPLL0);
6248 else
6249 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
6250 SKL_DPLL0);
6251
6252 I915_WRITE(DPLL_CTRL1, val);
6253 POSTING_READ(DPLL_CTRL1);
6254
6255 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
6256
e24ca054
CW
6257 if (intel_wait_for_register(dev_priv,
6258 LCPLL1_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
6259 5))
5d96d8af 6260 DRM_ERROR("DPLL0 not locked\n");
1cd593e0 6261
63911d72 6262 dev_priv->cdclk_pll.vco = vco;
b2045352
VS
6263
6264 /* We'll want to keep using the current vco from now on. */
6265 skl_set_preferred_cdclk_vco(dev_priv, vco);
5d96d8af
DL
6266}
6267
430e05de
VS
6268static void
6269skl_dpll0_disable(struct drm_i915_private *dev_priv)
6270{
6271 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
8ad32a05
CW
6272 if (intel_wait_for_register(dev_priv,
6273 LCPLL1_CTL, LCPLL_PLL_LOCK, 0,
6274 1))
430e05de 6275 DRM_ERROR("Couldn't disable DPLL0\n");
1cd593e0 6276
63911d72 6277 dev_priv->cdclk_pll.vco = 0;
430e05de
VS
6278}
6279
5d96d8af
DL
6280static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
6281{
6282 int ret;
6283 u32 val;
6284
6285 /* inform PCU we want to change CDCLK */
6286 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
6287 mutex_lock(&dev_priv->rps.hw_lock);
6288 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
6289 mutex_unlock(&dev_priv->rps.hw_lock);
6290
6291 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
6292}
6293
6294static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
6295{
848496e5 6296 return _wait_for(skl_cdclk_pcu_ready(dev_priv), 3000, 10) == 0;
5d96d8af
DL
6297}
6298
1cd593e0 6299static void skl_set_cdclk(struct drm_i915_private *dev_priv, int cdclk, int vco)
5d96d8af
DL
6300{
6301 u32 freq_select, pcu_ack;
6302
1cd593e0
VS
6303 WARN_ON((cdclk == 24000) != (vco == 0));
6304
63911d72 6305 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
5d96d8af
DL
6306
6307 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
6308 DRM_ERROR("failed to inform PCU about cdclk change\n");
6309 return;
6310 }
6311
6312 /* set CDCLK_CTL */
9ef56154 6313 switch (cdclk) {
5d96d8af
DL
6314 case 450000:
6315 case 432000:
6316 freq_select = CDCLK_FREQ_450_432;
6317 pcu_ack = 1;
6318 break;
6319 case 540000:
6320 freq_select = CDCLK_FREQ_540;
6321 pcu_ack = 2;
6322 break;
487ed2e4 6323 case 308571:
5d96d8af
DL
6324 case 337500:
6325 default:
6326 freq_select = CDCLK_FREQ_337_308;
6327 pcu_ack = 0;
6328 break;
487ed2e4 6329 case 617143:
5d96d8af
DL
6330 case 675000:
6331 freq_select = CDCLK_FREQ_675_617;
6332 pcu_ack = 3;
6333 break;
6334 }
6335
63911d72
VS
6336 if (dev_priv->cdclk_pll.vco != 0 &&
6337 dev_priv->cdclk_pll.vco != vco)
1cd593e0
VS
6338 skl_dpll0_disable(dev_priv);
6339
63911d72 6340 if (dev_priv->cdclk_pll.vco != vco)
1cd593e0
VS
6341 skl_dpll0_enable(dev_priv, vco);
6342
9ef56154 6343 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(cdclk));
5d96d8af
DL
6344 POSTING_READ(CDCLK_CTL);
6345
6346 /* inform PCU of the change */
6347 mutex_lock(&dev_priv->rps.hw_lock);
6348 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
6349 mutex_unlock(&dev_priv->rps.hw_lock);
560a7ae4 6350
4c75b940 6351 intel_update_cdclk(dev_priv);
5d96d8af
DL
6352}
6353
9f7eb31a
VS
6354static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv);
6355
5d96d8af
DL
6356void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
6357{
709e05c3 6358 skl_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref, 0);
5d96d8af
DL
6359}
6360
6361void skl_init_cdclk(struct drm_i915_private *dev_priv)
6362{
9f7eb31a
VS
6363 int cdclk, vco;
6364
6365 skl_sanitize_cdclk(dev_priv);
5d96d8af 6366
63911d72 6367 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0) {
9f7eb31a
VS
6368 /*
6369 * Use the current vco as our initial
6370 * guess as to what the preferred vco is.
6371 */
6372 if (dev_priv->skl_preferred_vco_freq == 0)
6373 skl_set_preferred_cdclk_vco(dev_priv,
63911d72 6374 dev_priv->cdclk_pll.vco);
70c2c184 6375 return;
1cd593e0 6376 }
5d96d8af 6377
70c2c184
VS
6378 vco = dev_priv->skl_preferred_vco_freq;
6379 if (vco == 0)
63911d72 6380 vco = 8100000;
70c2c184 6381 cdclk = skl_calc_cdclk(0, vco);
5d96d8af 6382
70c2c184 6383 skl_set_cdclk(dev_priv, cdclk, vco);
5d96d8af
DL
6384}
6385
9f7eb31a 6386static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
c73666f3 6387{
09492498 6388 uint32_t cdctl, expected;
c73666f3 6389
f1b391a5
SK
6390 /*
6391 * check if the pre-os intialized the display
6392 * There is SWF18 scratchpad register defined which is set by the
6393 * pre-os which can be used by the OS drivers to check the status
6394 */
6395 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
6396 goto sanitize;
6397
4c75b940 6398 intel_update_cdclk(dev_priv);
c73666f3 6399 /* Is PLL enabled and locked ? */
1c3f7700
ID
6400 if (dev_priv->cdclk_pll.vco == 0 ||
6401 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
c73666f3
SK
6402 goto sanitize;
6403
6404 /* DPLL okay; verify the cdclock
6405 *
6406 * Noticed in some instances that the freq selection is correct but
6407 * decimal part is programmed wrong from BIOS where pre-os does not
6408 * enable display. Verify the same as well.
6409 */
09492498
VS
6410 cdctl = I915_READ(CDCLK_CTL);
6411 expected = (cdctl & CDCLK_FREQ_SEL_MASK) |
6412 skl_cdclk_decimal(dev_priv->cdclk_freq);
6413 if (cdctl == expected)
c73666f3 6414 /* All well; nothing to sanitize */
9f7eb31a 6415 return;
c89e39f3 6416
9f7eb31a
VS
6417sanitize:
6418 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
c73666f3 6419
9f7eb31a
VS
6420 /* force cdclk programming */
6421 dev_priv->cdclk_freq = 0;
6422 /* force full PLL disable + enable */
63911d72 6423 dev_priv->cdclk_pll.vco = -1;
c73666f3
SK
6424}
6425
30a970c6
JB
6426/* Adjust CDclk dividers to allow high res or save power if possible */
6427static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
6428{
fac5e23e 6429 struct drm_i915_private *dev_priv = to_i915(dev);
30a970c6
JB
6430 u32 val, cmd;
6431
1353c4fb 6432 WARN_ON(dev_priv->display.get_display_clock_speed(dev_priv)
164dfd28 6433 != dev_priv->cdclk_freq);
d60c4473 6434
dfcab17e 6435 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
30a970c6 6436 cmd = 2;
dfcab17e 6437 else if (cdclk == 266667)
30a970c6
JB
6438 cmd = 1;
6439 else
6440 cmd = 0;
6441
6442 mutex_lock(&dev_priv->rps.hw_lock);
6443 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
6444 val &= ~DSPFREQGUAR_MASK;
6445 val |= (cmd << DSPFREQGUAR_SHIFT);
6446 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
6447 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
6448 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
6449 50)) {
6450 DRM_ERROR("timed out waiting for CDclk change\n");
6451 }
6452 mutex_unlock(&dev_priv->rps.hw_lock);
6453
54433e91
VS
6454 mutex_lock(&dev_priv->sb_lock);
6455
dfcab17e 6456 if (cdclk == 400000) {
6bcda4f0 6457 u32 divider;
30a970c6 6458
6bcda4f0 6459 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
30a970c6 6460
30a970c6
JB
6461 /* adjust cdclk divider */
6462 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
87d5d259 6463 val &= ~CCK_FREQUENCY_VALUES;
30a970c6
JB
6464 val |= divider;
6465 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
a877e801
VS
6466
6467 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
87d5d259 6468 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
a877e801
VS
6469 50))
6470 DRM_ERROR("timed out waiting for CDclk change\n");
30a970c6
JB
6471 }
6472
30a970c6
JB
6473 /* adjust self-refresh exit latency value */
6474 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
6475 val &= ~0x7f;
6476
6477 /*
6478 * For high bandwidth configs, we set a higher latency in the bunit
6479 * so that the core display fetch happens in time to avoid underruns.
6480 */
dfcab17e 6481 if (cdclk == 400000)
30a970c6
JB
6482 val |= 4500 / 250; /* 4.5 usec */
6483 else
6484 val |= 3000 / 250; /* 3.0 usec */
6485 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
54433e91 6486
a580516d 6487 mutex_unlock(&dev_priv->sb_lock);
30a970c6 6488
4c75b940 6489 intel_update_cdclk(dev_priv);
30a970c6
JB
6490}
6491
383c5a6a
VS
6492static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
6493{
fac5e23e 6494 struct drm_i915_private *dev_priv = to_i915(dev);
383c5a6a
VS
6495 u32 val, cmd;
6496
1353c4fb 6497 WARN_ON(dev_priv->display.get_display_clock_speed(dev_priv)
164dfd28 6498 != dev_priv->cdclk_freq);
383c5a6a
VS
6499
6500 switch (cdclk) {
383c5a6a
VS
6501 case 333333:
6502 case 320000:
383c5a6a 6503 case 266667:
383c5a6a 6504 case 200000:
383c5a6a
VS
6505 break;
6506 default:
5f77eeb0 6507 MISSING_CASE(cdclk);
383c5a6a
VS
6508 return;
6509 }
6510
9d0d3fda
VS
6511 /*
6512 * Specs are full of misinformation, but testing on actual
6513 * hardware has shown that we just need to write the desired
6514 * CCK divider into the Punit register.
6515 */
6516 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
6517
383c5a6a
VS
6518 mutex_lock(&dev_priv->rps.hw_lock);
6519 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
6520 val &= ~DSPFREQGUAR_MASK_CHV;
6521 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
6522 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
6523 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
6524 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
6525 50)) {
6526 DRM_ERROR("timed out waiting for CDclk change\n");
6527 }
6528 mutex_unlock(&dev_priv->rps.hw_lock);
6529
4c75b940 6530 intel_update_cdclk(dev_priv);
383c5a6a
VS
6531}
6532
30a970c6
JB
6533static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
6534 int max_pixclk)
6535{
6bcda4f0 6536 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
6cca3195 6537 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
29dc7ef3 6538
30a970c6
JB
6539 /*
6540 * Really only a few cases to deal with, as only 4 CDclks are supported:
6541 * 200MHz
6542 * 267MHz
29dc7ef3 6543 * 320/333MHz (depends on HPLL freq)
6cca3195
VS
6544 * 400MHz (VLV only)
6545 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
6546 * of the lower bin and adjust if needed.
e37c67a1
VS
6547 *
6548 * We seem to get an unstable or solid color picture at 200MHz.
6549 * Not sure what's wrong. For now use 200MHz only when all pipes
6550 * are off.
30a970c6 6551 */
6cca3195
VS
6552 if (!IS_CHERRYVIEW(dev_priv) &&
6553 max_pixclk > freq_320*limit/100)
dfcab17e 6554 return 400000;
6cca3195 6555 else if (max_pixclk > 266667*limit/100)
29dc7ef3 6556 return freq_320;
e37c67a1 6557 else if (max_pixclk > 0)
dfcab17e 6558 return 266667;
e37c67a1
VS
6559 else
6560 return 200000;
30a970c6
JB
6561}
6562
89b3c3c7
ACO
6563static int glk_calc_cdclk(int max_pixclk)
6564{
09d09386 6565 if (max_pixclk > 2 * 158400)
89b3c3c7 6566 return 316800;
09d09386 6567 else if (max_pixclk > 2 * 79200)
89b3c3c7
ACO
6568 return 158400;
6569 else
6570 return 79200;
6571}
6572
324513c0 6573static int bxt_calc_cdclk(int max_pixclk)
f8437dd1 6574{
760e1477 6575 if (max_pixclk > 576000)
f8437dd1 6576 return 624000;
760e1477 6577 else if (max_pixclk > 384000)
f8437dd1 6578 return 576000;
760e1477 6579 else if (max_pixclk > 288000)
f8437dd1 6580 return 384000;
760e1477 6581 else if (max_pixclk > 144000)
f8437dd1
VK
6582 return 288000;
6583 else
6584 return 144000;
6585}
6586
e8788cbc 6587/* Compute the max pixel clock for new configuration. */
a821fc46
ACO
6588static int intel_mode_max_pixclk(struct drm_device *dev,
6589 struct drm_atomic_state *state)
30a970c6 6590{
565602d7 6591 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 6592 struct drm_i915_private *dev_priv = to_i915(dev);
565602d7
ML
6593 struct drm_crtc *crtc;
6594 struct drm_crtc_state *crtc_state;
6595 unsigned max_pixclk = 0, i;
6596 enum pipe pipe;
30a970c6 6597
565602d7
ML
6598 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
6599 sizeof(intel_state->min_pixclk));
304603f4 6600
565602d7
ML
6601 for_each_crtc_in_state(state, crtc, crtc_state, i) {
6602 int pixclk = 0;
6603
6604 if (crtc_state->enable)
6605 pixclk = crtc_state->adjusted_mode.crtc_clock;
304603f4 6606
565602d7 6607 intel_state->min_pixclk[i] = pixclk;
30a970c6
JB
6608 }
6609
565602d7
ML
6610 for_each_pipe(dev_priv, pipe)
6611 max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
6612
30a970c6
JB
6613 return max_pixclk;
6614}
6615
27c329ed 6616static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
30a970c6 6617{
27c329ed 6618 struct drm_device *dev = state->dev;
fac5e23e 6619 struct drm_i915_private *dev_priv = to_i915(dev);
27c329ed 6620 int max_pixclk = intel_mode_max_pixclk(dev, state);
1a617b77
ML
6621 struct intel_atomic_state *intel_state =
6622 to_intel_atomic_state(state);
30a970c6 6623
1a617b77 6624 intel_state->cdclk = intel_state->dev_cdclk =
27c329ed 6625 valleyview_calc_cdclk(dev_priv, max_pixclk);
0a9ab303 6626
1a617b77
ML
6627 if (!intel_state->active_crtcs)
6628 intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
6629
27c329ed
ML
6630 return 0;
6631}
304603f4 6632
324513c0 6633static int bxt_modeset_calc_cdclk(struct drm_atomic_state *state)
27c329ed 6634{
89b3c3c7 6635 struct drm_i915_private *dev_priv = to_i915(state->dev);
4e5ca60f 6636 int max_pixclk = ilk_max_pixel_rate(state);
1a617b77
ML
6637 struct intel_atomic_state *intel_state =
6638 to_intel_atomic_state(state);
89b3c3c7 6639 int cdclk;
85a96e7a 6640
89b3c3c7
ACO
6641 if (IS_GEMINILAKE(dev_priv))
6642 cdclk = glk_calc_cdclk(max_pixclk);
6643 else
6644 cdclk = bxt_calc_cdclk(max_pixclk);
85a96e7a 6645
89b3c3c7
ACO
6646 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
6647
6648 if (!intel_state->active_crtcs) {
6649 if (IS_GEMINILAKE(dev_priv))
6650 cdclk = glk_calc_cdclk(0);
6651 else
6652 cdclk = bxt_calc_cdclk(0);
6653
6654 intel_state->dev_cdclk = cdclk;
6655 }
1a617b77 6656
27c329ed 6657 return 0;
30a970c6
JB
6658}
6659
1e69cd74
VS
6660static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
6661{
6662 unsigned int credits, default_credits;
6663
6664 if (IS_CHERRYVIEW(dev_priv))
6665 default_credits = PFI_CREDIT(12);
6666 else
6667 default_credits = PFI_CREDIT(8);
6668
bfa7df01 6669 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
1e69cd74
VS
6670 /* CHV suggested value is 31 or 63 */
6671 if (IS_CHERRYVIEW(dev_priv))
fcc0008f 6672 credits = PFI_CREDIT_63;
1e69cd74
VS
6673 else
6674 credits = PFI_CREDIT(15);
6675 } else {
6676 credits = default_credits;
6677 }
6678
6679 /*
6680 * WA - write default credits before re-programming
6681 * FIXME: should we also set the resend bit here?
6682 */
6683 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6684 default_credits);
6685
6686 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6687 credits | PFI_CREDIT_RESEND);
6688
6689 /*
6690 * FIXME is this guaranteed to clear
6691 * immediately or should we poll for it?
6692 */
6693 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6694}
6695
27c329ed 6696static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
30a970c6 6697{
a821fc46 6698 struct drm_device *dev = old_state->dev;
fac5e23e 6699 struct drm_i915_private *dev_priv = to_i915(dev);
1a617b77
ML
6700 struct intel_atomic_state *old_intel_state =
6701 to_intel_atomic_state(old_state);
6702 unsigned req_cdclk = old_intel_state->dev_cdclk;
30a970c6 6703
27c329ed
ML
6704 /*
6705 * FIXME: We can end up here with all power domains off, yet
6706 * with a CDCLK frequency other than the minimum. To account
6707 * for this take the PIPE-A power domain, which covers the HW
6708 * blocks needed for the following programming. This can be
6709 * removed once it's guaranteed that we get here either with
6710 * the minimum CDCLK set, or the required power domains
6711 * enabled.
6712 */
6713 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
738c05c0 6714
920a14b2 6715 if (IS_CHERRYVIEW(dev_priv))
27c329ed
ML
6716 cherryview_set_cdclk(dev, req_cdclk);
6717 else
6718 valleyview_set_cdclk(dev, req_cdclk);
738c05c0 6719
27c329ed 6720 vlv_program_pfi_credits(dev_priv);
1e69cd74 6721
27c329ed 6722 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
30a970c6
JB
6723}
6724
4a806558
ML
6725static void valleyview_crtc_enable(struct intel_crtc_state *pipe_config,
6726 struct drm_atomic_state *old_state)
89b667f8 6727{
4a806558 6728 struct drm_crtc *crtc = pipe_config->base.crtc;
89b667f8 6729 struct drm_device *dev = crtc->dev;
a72e4c9f 6730 struct drm_i915_private *dev_priv = to_i915(dev);
89b667f8 6731 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
89b667f8 6732 int pipe = intel_crtc->pipe;
89b667f8 6733
53d9f4e9 6734 if (WARN_ON(intel_crtc->active))
89b667f8
JB
6735 return;
6736
37a5650b 6737 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 6738 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
6739
6740 intel_set_pipe_timings(intel_crtc);
bc58be60 6741 intel_set_pipe_src_size(intel_crtc);
5b18e57c 6742
920a14b2 6743 if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
fac5e23e 6744 struct drm_i915_private *dev_priv = to_i915(dev);
c14b0485
VS
6745
6746 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6747 I915_WRITE(CHV_CANVAS(pipe), 0);
6748 }
6749
5b18e57c
DV
6750 i9xx_set_pipeconf(intel_crtc);
6751
89b667f8 6752 intel_crtc->active = true;
89b667f8 6753
a72e4c9f 6754 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6755
fd6bbda9 6756 intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
89b667f8 6757
920a14b2 6758 if (IS_CHERRYVIEW(dev_priv)) {
cd2d34d9
VS
6759 chv_prepare_pll(intel_crtc, intel_crtc->config);
6760 chv_enable_pll(intel_crtc, intel_crtc->config);
6761 } else {
6762 vlv_prepare_pll(intel_crtc, intel_crtc->config);
6763 vlv_enable_pll(intel_crtc, intel_crtc->config);
9d556c99 6764 }
89b667f8 6765
fd6bbda9 6766 intel_encoders_pre_enable(crtc, pipe_config, old_state);
89b667f8 6767
2dd24552
JB
6768 i9xx_pfit_enable(intel_crtc);
6769
b95c5321 6770 intel_color_load_luts(&pipe_config->base);
63cbb074 6771
432081bc 6772 intel_update_watermarks(intel_crtc);
e1fdc473 6773 intel_enable_pipe(intel_crtc);
be6a6f8e 6774
4b3a9526
VS
6775 assert_vblank_disabled(crtc);
6776 drm_crtc_vblank_on(crtc);
6777
fd6bbda9 6778 intel_encoders_enable(crtc, pipe_config, old_state);
89b667f8
JB
6779}
6780
f13c2ef3
DV
6781static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6782{
6783 struct drm_device *dev = crtc->base.dev;
fac5e23e 6784 struct drm_i915_private *dev_priv = to_i915(dev);
f13c2ef3 6785
6e3c9717
ACO
6786 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6787 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
f13c2ef3
DV
6788}
6789
4a806558
ML
6790static void i9xx_crtc_enable(struct intel_crtc_state *pipe_config,
6791 struct drm_atomic_state *old_state)
79e53945 6792{
4a806558 6793 struct drm_crtc *crtc = pipe_config->base.crtc;
79e53945 6794 struct drm_device *dev = crtc->dev;
a72e4c9f 6795 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 6796 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cd2d34d9 6797 enum pipe pipe = intel_crtc->pipe;
79e53945 6798
53d9f4e9 6799 if (WARN_ON(intel_crtc->active))
f7abfe8b
CW
6800 return;
6801
f13c2ef3
DV
6802 i9xx_set_pll_dividers(intel_crtc);
6803
37a5650b 6804 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 6805 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
6806
6807 intel_set_pipe_timings(intel_crtc);
bc58be60 6808 intel_set_pipe_src_size(intel_crtc);
5b18e57c 6809
5b18e57c
DV
6810 i9xx_set_pipeconf(intel_crtc);
6811
f7abfe8b 6812 intel_crtc->active = true;
6b383a7f 6813
5db94019 6814 if (!IS_GEN2(dev_priv))
a72e4c9f 6815 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6816
fd6bbda9 6817 intel_encoders_pre_enable(crtc, pipe_config, old_state);
9d6d9f19 6818
f6736a1a
DV
6819 i9xx_enable_pll(intel_crtc);
6820
2dd24552
JB
6821 i9xx_pfit_enable(intel_crtc);
6822
b95c5321 6823 intel_color_load_luts(&pipe_config->base);
63cbb074 6824
432081bc 6825 intel_update_watermarks(intel_crtc);
e1fdc473 6826 intel_enable_pipe(intel_crtc);
be6a6f8e 6827
4b3a9526
VS
6828 assert_vblank_disabled(crtc);
6829 drm_crtc_vblank_on(crtc);
6830
fd6bbda9 6831 intel_encoders_enable(crtc, pipe_config, old_state);
0b8765c6 6832}
79e53945 6833
87476d63
DV
6834static void i9xx_pfit_disable(struct intel_crtc *crtc)
6835{
6836 struct drm_device *dev = crtc->base.dev;
fac5e23e 6837 struct drm_i915_private *dev_priv = to_i915(dev);
87476d63 6838
6e3c9717 6839 if (!crtc->config->gmch_pfit.control)
328d8e82 6840 return;
87476d63 6841
328d8e82 6842 assert_pipe_disabled(dev_priv, crtc->pipe);
87476d63 6843
328d8e82
DV
6844 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6845 I915_READ(PFIT_CONTROL));
6846 I915_WRITE(PFIT_CONTROL, 0);
87476d63
DV
6847}
6848
4a806558
ML
6849static void i9xx_crtc_disable(struct intel_crtc_state *old_crtc_state,
6850 struct drm_atomic_state *old_state)
0b8765c6 6851{
4a806558 6852 struct drm_crtc *crtc = old_crtc_state->base.crtc;
0b8765c6 6853 struct drm_device *dev = crtc->dev;
fac5e23e 6854 struct drm_i915_private *dev_priv = to_i915(dev);
0b8765c6
JB
6855 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6856 int pipe = intel_crtc->pipe;
ef9c3aee 6857
6304cd91
VS
6858 /*
6859 * On gen2 planes are double buffered but the pipe isn't, so we must
6860 * wait for planes to fully turn off before disabling the pipe.
6861 */
5db94019 6862 if (IS_GEN2(dev_priv))
0f0f74bc 6863 intel_wait_for_vblank(dev_priv, pipe);
6304cd91 6864
fd6bbda9 6865 intel_encoders_disable(crtc, old_crtc_state, old_state);
4b3a9526 6866
f9b61ff6
DV
6867 drm_crtc_vblank_off(crtc);
6868 assert_vblank_disabled(crtc);
6869
575f7ab7 6870 intel_disable_pipe(intel_crtc);
24a1f16d 6871
87476d63 6872 i9xx_pfit_disable(intel_crtc);
24a1f16d 6873
fd6bbda9 6874 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
89b667f8 6875
d7edc4e5 6876 if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
920a14b2 6877 if (IS_CHERRYVIEW(dev_priv))
076ed3b2 6878 chv_disable_pll(dev_priv, pipe);
11a914c2 6879 else if (IS_VALLEYVIEW(dev_priv))
076ed3b2
CML
6880 vlv_disable_pll(dev_priv, pipe);
6881 else
1c4e0274 6882 i9xx_disable_pll(intel_crtc);
076ed3b2 6883 }
0b8765c6 6884
fd6bbda9 6885 intel_encoders_post_pll_disable(crtc, old_crtc_state, old_state);
d6db995f 6886
5db94019 6887 if (!IS_GEN2(dev_priv))
a72e4c9f 6888 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
0b8765c6
JB
6889}
6890
b17d48e2
ML
6891static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
6892{
842e0307 6893 struct intel_encoder *encoder;
b17d48e2
ML
6894 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6895 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6896 enum intel_display_power_domain domain;
6897 unsigned long domains;
4a806558
ML
6898 struct drm_atomic_state *state;
6899 struct intel_crtc_state *crtc_state;
6900 int ret;
b17d48e2
ML
6901
6902 if (!intel_crtc->active)
6903 return;
6904
936e71e3 6905 if (to_intel_plane_state(crtc->primary->state)->base.visible) {
5a21b665 6906 WARN_ON(intel_crtc->flip_work);
fc32b1fd 6907
2622a081 6908 intel_pre_disable_primary_noatomic(crtc);
54a41961
ML
6909
6910 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
936e71e3 6911 to_intel_plane_state(crtc->primary->state)->base.visible = false;
a539205a
ML
6912 }
6913
4a806558
ML
6914 state = drm_atomic_state_alloc(crtc->dev);
6915 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
6916
6917 /* Everything's already locked, -EDEADLK can't happen. */
6918 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
6919 ret = drm_atomic_add_affected_connectors(state, crtc);
6920
6921 WARN_ON(IS_ERR(crtc_state) || ret);
6922
6923 dev_priv->display.crtc_disable(crtc_state, state);
6924
0853695c 6925 drm_atomic_state_put(state);
842e0307 6926
78108b7c
VS
6927 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
6928 crtc->base.id, crtc->name);
842e0307
ML
6929
6930 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6931 crtc->state->active = false;
37d9078b 6932 intel_crtc->active = false;
842e0307
ML
6933 crtc->enabled = false;
6934 crtc->state->connector_mask = 0;
6935 crtc->state->encoder_mask = 0;
6936
6937 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6938 encoder->base.crtc = NULL;
6939
58f9c0bc 6940 intel_fbc_disable(intel_crtc);
432081bc 6941 intel_update_watermarks(intel_crtc);
1f7457b1 6942 intel_disable_shared_dpll(intel_crtc);
b17d48e2
ML
6943
6944 domains = intel_crtc->enabled_power_domains;
6945 for_each_power_domain(domain, domains)
6946 intel_display_power_put(dev_priv, domain);
6947 intel_crtc->enabled_power_domains = 0;
565602d7
ML
6948
6949 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
6950 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
b17d48e2
ML
6951}
6952
6b72d486
ML
6953/*
6954 * turn all crtc's off, but do not adjust state
6955 * This has to be paired with a call to intel_modeset_setup_hw_state.
6956 */
70e0bd74 6957int intel_display_suspend(struct drm_device *dev)
ee7b9f93 6958{
e2c8b870 6959 struct drm_i915_private *dev_priv = to_i915(dev);
70e0bd74 6960 struct drm_atomic_state *state;
e2c8b870 6961 int ret;
70e0bd74 6962
e2c8b870
ML
6963 state = drm_atomic_helper_suspend(dev);
6964 ret = PTR_ERR_OR_ZERO(state);
70e0bd74
ML
6965 if (ret)
6966 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
e2c8b870
ML
6967 else
6968 dev_priv->modeset_restore_state = state;
70e0bd74 6969 return ret;
ee7b9f93
JB
6970}
6971
ea5b213a 6972void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 6973{
4ef69c7a 6974 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 6975
ea5b213a
CW
6976 drm_encoder_cleanup(encoder);
6977 kfree(intel_encoder);
7e7d76c3
JB
6978}
6979
0a91ca29
DV
6980/* Cross check the actual hw state with our own modeset state tracking (and it's
6981 * internal consistency). */
5a21b665 6982static void intel_connector_verify_state(struct intel_connector *connector)
79e53945 6983{
5a21b665 6984 struct drm_crtc *crtc = connector->base.state->crtc;
35dd3c64
ML
6985
6986 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6987 connector->base.base.id,
6988 connector->base.name);
6989
0a91ca29 6990 if (connector->get_hw_state(connector)) {
e85376cb 6991 struct intel_encoder *encoder = connector->encoder;
5a21b665 6992 struct drm_connector_state *conn_state = connector->base.state;
0a91ca29 6993
35dd3c64
ML
6994 I915_STATE_WARN(!crtc,
6995 "connector enabled without attached crtc\n");
0a91ca29 6996
35dd3c64
ML
6997 if (!crtc)
6998 return;
6999
7000 I915_STATE_WARN(!crtc->state->active,
7001 "connector is active, but attached crtc isn't\n");
7002
e85376cb 7003 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
35dd3c64
ML
7004 return;
7005
e85376cb 7006 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
35dd3c64
ML
7007 "atomic encoder doesn't match attached encoder\n");
7008
e85376cb 7009 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
35dd3c64
ML
7010 "attached encoder crtc differs from connector crtc\n");
7011 } else {
4d688a2a
ML
7012 I915_STATE_WARN(crtc && crtc->state->active,
7013 "attached crtc is active, but connector isn't\n");
5a21b665 7014 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
35dd3c64 7015 "best encoder set without crtc!\n");
0a91ca29 7016 }
79e53945
JB
7017}
7018
08d9bc92
ACO
7019int intel_connector_init(struct intel_connector *connector)
7020{
5350a031 7021 drm_atomic_helper_connector_reset(&connector->base);
08d9bc92 7022
5350a031 7023 if (!connector->base.state)
08d9bc92
ACO
7024 return -ENOMEM;
7025
08d9bc92
ACO
7026 return 0;
7027}
7028
7029struct intel_connector *intel_connector_alloc(void)
7030{
7031 struct intel_connector *connector;
7032
7033 connector = kzalloc(sizeof *connector, GFP_KERNEL);
7034 if (!connector)
7035 return NULL;
7036
7037 if (intel_connector_init(connector) < 0) {
7038 kfree(connector);
7039 return NULL;
7040 }
7041
7042 return connector;
7043}
7044
f0947c37
DV
7045/* Simple connector->get_hw_state implementation for encoders that support only
7046 * one connector and no cloning and hence the encoder state determines the state
7047 * of the connector. */
7048bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 7049{
24929352 7050 enum pipe pipe = 0;
f0947c37 7051 struct intel_encoder *encoder = connector->encoder;
ea5b213a 7052
f0947c37 7053 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
7054}
7055
6d293983 7056static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
d272ddfa 7057{
6d293983
ACO
7058 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
7059 return crtc_state->fdi_lanes;
d272ddfa
VS
7060
7061 return 0;
7062}
7063
6d293983 7064static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5cec258b 7065 struct intel_crtc_state *pipe_config)
1857e1da 7066{
8652744b 7067 struct drm_i915_private *dev_priv = to_i915(dev);
6d293983
ACO
7068 struct drm_atomic_state *state = pipe_config->base.state;
7069 struct intel_crtc *other_crtc;
7070 struct intel_crtc_state *other_crtc_state;
7071
1857e1da
DV
7072 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
7073 pipe_name(pipe), pipe_config->fdi_lanes);
7074 if (pipe_config->fdi_lanes > 4) {
7075 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
7076 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 7077 return -EINVAL;
1857e1da
DV
7078 }
7079
8652744b 7080 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
1857e1da
DV
7081 if (pipe_config->fdi_lanes > 2) {
7082 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
7083 pipe_config->fdi_lanes);
6d293983 7084 return -EINVAL;
1857e1da 7085 } else {
6d293983 7086 return 0;
1857e1da
DV
7087 }
7088 }
7089
b7f05d4a 7090 if (INTEL_INFO(dev_priv)->num_pipes == 2)
6d293983 7091 return 0;
1857e1da
DV
7092
7093 /* Ivybridge 3 pipe is really complicated */
7094 switch (pipe) {
7095 case PIPE_A:
6d293983 7096 return 0;
1857e1da 7097 case PIPE_B:
6d293983
ACO
7098 if (pipe_config->fdi_lanes <= 2)
7099 return 0;
7100
b91eb5cc 7101 other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
6d293983
ACO
7102 other_crtc_state =
7103 intel_atomic_get_crtc_state(state, other_crtc);
7104 if (IS_ERR(other_crtc_state))
7105 return PTR_ERR(other_crtc_state);
7106
7107 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
1857e1da
DV
7108 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
7109 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 7110 return -EINVAL;
1857e1da 7111 }
6d293983 7112 return 0;
1857e1da 7113 case PIPE_C:
251cc67c
VS
7114 if (pipe_config->fdi_lanes > 2) {
7115 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
7116 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 7117 return -EINVAL;
251cc67c 7118 }
6d293983 7119
b91eb5cc 7120 other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
6d293983
ACO
7121 other_crtc_state =
7122 intel_atomic_get_crtc_state(state, other_crtc);
7123 if (IS_ERR(other_crtc_state))
7124 return PTR_ERR(other_crtc_state);
7125
7126 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
1857e1da 7127 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
6d293983 7128 return -EINVAL;
1857e1da 7129 }
6d293983 7130 return 0;
1857e1da
DV
7131 default:
7132 BUG();
7133 }
7134}
7135
e29c22c0
DV
7136#define RETRY 1
7137static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5cec258b 7138 struct intel_crtc_state *pipe_config)
877d48d5 7139{
1857e1da 7140 struct drm_device *dev = intel_crtc->base.dev;
7c5f93b0 7141 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6d293983
ACO
7142 int lane, link_bw, fdi_dotclock, ret;
7143 bool needs_recompute = false;
877d48d5 7144
e29c22c0 7145retry:
877d48d5
DV
7146 /* FDI is a binary signal running at ~2.7GHz, encoding
7147 * each output octet as 10 bits. The actual frequency
7148 * is stored as a divider into a 100MHz clock, and the
7149 * mode pixel clock is stored in units of 1KHz.
7150 * Hence the bw of each lane in terms of the mode signal
7151 * is:
7152 */
21a727b3 7153 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
877d48d5 7154
241bfc38 7155 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 7156
2bd89a07 7157 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
DV
7158 pipe_config->pipe_bpp);
7159
7160 pipe_config->fdi_lanes = lane;
7161
2bd89a07 7162 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
877d48d5 7163 link_bw, &pipe_config->fdi_m_n);
1857e1da 7164
e3b247da 7165 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
6d293983 7166 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
e29c22c0
DV
7167 pipe_config->pipe_bpp -= 2*3;
7168 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
7169 pipe_config->pipe_bpp);
7170 needs_recompute = true;
7171 pipe_config->bw_constrained = true;
7172
7173 goto retry;
7174 }
7175
7176 if (needs_recompute)
7177 return RETRY;
7178
6d293983 7179 return ret;
877d48d5
DV
7180}
7181
8cfb3407
VS
7182static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
7183 struct intel_crtc_state *pipe_config)
7184{
7185 if (pipe_config->pipe_bpp > 24)
7186 return false;
7187
7188 /* HSW can handle pixel rate up to cdclk? */
2d1fe073 7189 if (IS_HASWELL(dev_priv))
8cfb3407
VS
7190 return true;
7191
7192 /*
b432e5cf
VS
7193 * We compare against max which means we must take
7194 * the increased cdclk requirement into account when
7195 * calculating the new cdclk.
7196 *
7197 * Should measure whether using a lower cdclk w/o IPS
8cfb3407
VS
7198 */
7199 return ilk_pipe_pixel_rate(pipe_config) <=
7200 dev_priv->max_cdclk_freq * 95 / 100;
7201}
7202
42db64ef 7203static void hsw_compute_ips_config(struct intel_crtc *crtc,
5cec258b 7204 struct intel_crtc_state *pipe_config)
42db64ef 7205{
8cfb3407 7206 struct drm_device *dev = crtc->base.dev;
fac5e23e 7207 struct drm_i915_private *dev_priv = to_i915(dev);
8cfb3407 7208
d330a953 7209 pipe_config->ips_enabled = i915.enable_ips &&
8cfb3407
VS
7210 hsw_crtc_supports_ips(crtc) &&
7211 pipe_config_supports_ips(dev_priv, pipe_config);
42db64ef
PZ
7212}
7213
39acb4aa
VS
7214static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
7215{
7216 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7217
7218 /* GDG double wide on either pipe, otherwise pipe A only */
7219 return INTEL_INFO(dev_priv)->gen < 4 &&
7220 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
7221}
7222
a43f6e0f 7223static int intel_crtc_compute_config(struct intel_crtc *crtc,
5cec258b 7224 struct intel_crtc_state *pipe_config)
79e53945 7225{
a43f6e0f 7226 struct drm_device *dev = crtc->base.dev;
fac5e23e 7227 struct drm_i915_private *dev_priv = to_i915(dev);
7c5f93b0 7228 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
f3261156 7229 int clock_limit = dev_priv->max_dotclk_freq;
89749350 7230
6315b5d3 7231 if (INTEL_GEN(dev_priv) < 4) {
f3261156 7232 clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
cf532bb2
VS
7233
7234 /*
39acb4aa 7235 * Enable double wide mode when the dot clock
cf532bb2 7236 * is > 90% of the (display) core speed.
cf532bb2 7237 */
39acb4aa
VS
7238 if (intel_crtc_supports_double_wide(crtc) &&
7239 adjusted_mode->crtc_clock > clock_limit) {
f3261156 7240 clock_limit = dev_priv->max_dotclk_freq;
cf532bb2 7241 pipe_config->double_wide = true;
ad3a4479 7242 }
f3261156 7243 }
ad3a4479 7244
f3261156
VS
7245 if (adjusted_mode->crtc_clock > clock_limit) {
7246 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
7247 adjusted_mode->crtc_clock, clock_limit,
7248 yesno(pipe_config->double_wide));
7249 return -EINVAL;
2c07245f 7250 }
89749350 7251
1d1d0e27
VS
7252 /*
7253 * Pipe horizontal size must be even in:
7254 * - DVO ganged mode
7255 * - LVDS dual channel mode
7256 * - Double wide pipe
7257 */
2d84d2b3 7258 if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
1d1d0e27
VS
7259 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
7260 pipe_config->pipe_src_w &= ~1;
7261
8693a824
DL
7262 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
7263 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
44f46b42 7264 */
9beb5fea 7265 if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
aad941d5 7266 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
e29c22c0 7267 return -EINVAL;
44f46b42 7268
50a0bc90 7269 if (HAS_IPS(dev_priv))
a43f6e0f
DV
7270 hsw_compute_ips_config(crtc, pipe_config);
7271
877d48d5 7272 if (pipe_config->has_pch_encoder)
a43f6e0f 7273 return ironlake_fdi_compute_config(crtc, pipe_config);
877d48d5 7274
cf5a15be 7275 return 0;
79e53945
JB
7276}
7277
1353c4fb 7278static int skylake_get_display_clock_speed(struct drm_i915_private *dev_priv)
1652d19e 7279{
1353c4fb 7280 u32 cdctl;
1652d19e 7281
ea61791e 7282 skl_dpll0_update(dev_priv);
1652d19e 7283
63911d72 7284 if (dev_priv->cdclk_pll.vco == 0)
709e05c3 7285 return dev_priv->cdclk_pll.ref;
1652d19e 7286
ea61791e 7287 cdctl = I915_READ(CDCLK_CTL);
1652d19e 7288
63911d72 7289 if (dev_priv->cdclk_pll.vco == 8640000) {
1652d19e
VS
7290 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
7291 case CDCLK_FREQ_450_432:
7292 return 432000;
7293 case CDCLK_FREQ_337_308:
487ed2e4 7294 return 308571;
ea61791e
VS
7295 case CDCLK_FREQ_540:
7296 return 540000;
1652d19e 7297 case CDCLK_FREQ_675_617:
487ed2e4 7298 return 617143;
1652d19e 7299 default:
ea61791e 7300 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
1652d19e
VS
7301 }
7302 } else {
1652d19e
VS
7303 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
7304 case CDCLK_FREQ_450_432:
7305 return 450000;
7306 case CDCLK_FREQ_337_308:
7307 return 337500;
ea61791e
VS
7308 case CDCLK_FREQ_540:
7309 return 540000;
1652d19e
VS
7310 case CDCLK_FREQ_675_617:
7311 return 675000;
7312 default:
ea61791e 7313 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
1652d19e
VS
7314 }
7315 }
7316
709e05c3 7317 return dev_priv->cdclk_pll.ref;
1652d19e
VS
7318}
7319
83d7c81f
VS
7320static void bxt_de_pll_update(struct drm_i915_private *dev_priv)
7321{
7322 u32 val;
7323
7324 dev_priv->cdclk_pll.ref = 19200;
1c3f7700 7325 dev_priv->cdclk_pll.vco = 0;
83d7c81f
VS
7326
7327 val = I915_READ(BXT_DE_PLL_ENABLE);
1c3f7700 7328 if ((val & BXT_DE_PLL_PLL_ENABLE) == 0)
83d7c81f 7329 return;
83d7c81f 7330
1c3f7700
ID
7331 if (WARN_ON((val & BXT_DE_PLL_LOCK) == 0))
7332 return;
83d7c81f
VS
7333
7334 val = I915_READ(BXT_DE_PLL_CTL);
7335 dev_priv->cdclk_pll.vco = (val & BXT_DE_PLL_RATIO_MASK) *
7336 dev_priv->cdclk_pll.ref;
7337}
7338
1353c4fb 7339static int broxton_get_display_clock_speed(struct drm_i915_private *dev_priv)
acd3f3d3 7340{
f5986242
VS
7341 u32 divider;
7342 int div, vco;
acd3f3d3 7343
83d7c81f
VS
7344 bxt_de_pll_update(dev_priv);
7345
f5986242
VS
7346 vco = dev_priv->cdclk_pll.vco;
7347 if (vco == 0)
7348 return dev_priv->cdclk_pll.ref;
acd3f3d3 7349
f5986242 7350 divider = I915_READ(CDCLK_CTL) & BXT_CDCLK_CD2X_DIV_SEL_MASK;
acd3f3d3 7351
f5986242 7352 switch (divider) {
acd3f3d3 7353 case BXT_CDCLK_CD2X_DIV_SEL_1:
f5986242
VS
7354 div = 2;
7355 break;
acd3f3d3 7356 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
89b3c3c7 7357 WARN(IS_GEMINILAKE(dev_priv), "Unsupported divider\n");
f5986242
VS
7358 div = 3;
7359 break;
acd3f3d3 7360 case BXT_CDCLK_CD2X_DIV_SEL_2:
f5986242
VS
7361 div = 4;
7362 break;
acd3f3d3 7363 case BXT_CDCLK_CD2X_DIV_SEL_4:
f5986242
VS
7364 div = 8;
7365 break;
7366 default:
7367 MISSING_CASE(divider);
7368 return dev_priv->cdclk_pll.ref;
acd3f3d3
BP
7369 }
7370
f5986242 7371 return DIV_ROUND_CLOSEST(vco, div);
acd3f3d3
BP
7372}
7373
1353c4fb 7374static int broadwell_get_display_clock_speed(struct drm_i915_private *dev_priv)
1652d19e 7375{
1652d19e
VS
7376 uint32_t lcpll = I915_READ(LCPLL_CTL);
7377 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
7378
7379 if (lcpll & LCPLL_CD_SOURCE_FCLK)
7380 return 800000;
7381 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
7382 return 450000;
7383 else if (freq == LCPLL_CLK_FREQ_450)
7384 return 450000;
7385 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
7386 return 540000;
7387 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
7388 return 337500;
7389 else
7390 return 675000;
7391}
7392
1353c4fb 7393static int haswell_get_display_clock_speed(struct drm_i915_private *dev_priv)
1652d19e 7394{
1652d19e
VS
7395 uint32_t lcpll = I915_READ(LCPLL_CTL);
7396 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
7397
7398 if (lcpll & LCPLL_CD_SOURCE_FCLK)
7399 return 800000;
7400 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
7401 return 450000;
7402 else if (freq == LCPLL_CLK_FREQ_450)
7403 return 450000;
50a0bc90 7404 else if (IS_HSW_ULT(dev_priv))
1652d19e
VS
7405 return 337500;
7406 else
7407 return 540000;
79e53945
JB
7408}
7409
1353c4fb 7410static int valleyview_get_display_clock_speed(struct drm_i915_private *dev_priv)
25eb05fc 7411{
1353c4fb 7412 return vlv_get_cck_clock_hpll(dev_priv, "cdclk",
bfa7df01 7413 CCK_DISPLAY_CLOCK_CONTROL);
25eb05fc
JB
7414}
7415
1353c4fb 7416static int ilk_get_display_clock_speed(struct drm_i915_private *dev_priv)
b37a6434
VS
7417{
7418 return 450000;
7419}
7420
1353c4fb 7421static int i945_get_display_clock_speed(struct drm_i915_private *dev_priv)
e70236a8
JB
7422{
7423 return 400000;
7424}
79e53945 7425
1353c4fb 7426static int i915_get_display_clock_speed(struct drm_i915_private *dev_priv)
79e53945 7427{
e907f170 7428 return 333333;
e70236a8 7429}
79e53945 7430
1353c4fb 7431static int i9xx_misc_get_display_clock_speed(struct drm_i915_private *dev_priv)
e70236a8
JB
7432{
7433 return 200000;
7434}
79e53945 7435
1353c4fb 7436static int pnv_get_display_clock_speed(struct drm_i915_private *dev_priv)
257a7ffc 7437{
1353c4fb 7438 struct pci_dev *pdev = dev_priv->drm.pdev;
257a7ffc
DV
7439 u16 gcfgc = 0;
7440
52a05c30 7441 pci_read_config_word(pdev, GCFGC, &gcfgc);
257a7ffc
DV
7442
7443 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
7444 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
e907f170 7445 return 266667;
257a7ffc 7446 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
e907f170 7447 return 333333;
257a7ffc 7448 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
e907f170 7449 return 444444;
257a7ffc
DV
7450 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
7451 return 200000;
7452 default:
7453 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
7454 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
e907f170 7455 return 133333;
257a7ffc 7456 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
e907f170 7457 return 166667;
257a7ffc
DV
7458 }
7459}
7460
1353c4fb 7461static int i915gm_get_display_clock_speed(struct drm_i915_private *dev_priv)
e70236a8 7462{
1353c4fb 7463 struct pci_dev *pdev = dev_priv->drm.pdev;
e70236a8 7464 u16 gcfgc = 0;
79e53945 7465
52a05c30 7466 pci_read_config_word(pdev, GCFGC, &gcfgc);
e70236a8
JB
7467
7468 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
e907f170 7469 return 133333;
e70236a8
JB
7470 else {
7471 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
7472 case GC_DISPLAY_CLOCK_333_MHZ:
e907f170 7473 return 333333;
e70236a8
JB
7474 default:
7475 case GC_DISPLAY_CLOCK_190_200_MHZ:
7476 return 190000;
79e53945 7477 }
e70236a8
JB
7478 }
7479}
7480
1353c4fb 7481static int i865_get_display_clock_speed(struct drm_i915_private *dev_priv)
e70236a8 7482{
e907f170 7483 return 266667;
e70236a8
JB
7484}
7485
1353c4fb 7486static int i85x_get_display_clock_speed(struct drm_i915_private *dev_priv)
e70236a8 7487{
1353c4fb 7488 struct pci_dev *pdev = dev_priv->drm.pdev;
e70236a8 7489 u16 hpllcc = 0;
1b1d2716 7490
65cd2b3f
VS
7491 /*
7492 * 852GM/852GMV only supports 133 MHz and the HPLLCC
7493 * encoding is different :(
7494 * FIXME is this the right way to detect 852GM/852GMV?
7495 */
52a05c30 7496 if (pdev->revision == 0x1)
65cd2b3f
VS
7497 return 133333;
7498
52a05c30 7499 pci_bus_read_config_word(pdev->bus,
1b1d2716
VS
7500 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
7501
e70236a8
JB
7502 /* Assume that the hardware is in the high speed state. This
7503 * should be the default.
7504 */
7505 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
7506 case GC_CLOCK_133_200:
1b1d2716 7507 case GC_CLOCK_133_200_2:
e70236a8
JB
7508 case GC_CLOCK_100_200:
7509 return 200000;
7510 case GC_CLOCK_166_250:
7511 return 250000;
7512 case GC_CLOCK_100_133:
e907f170 7513 return 133333;
1b1d2716
VS
7514 case GC_CLOCK_133_266:
7515 case GC_CLOCK_133_266_2:
7516 case GC_CLOCK_166_266:
7517 return 266667;
e70236a8 7518 }
79e53945 7519
e70236a8
JB
7520 /* Shouldn't happen */
7521 return 0;
7522}
79e53945 7523
1353c4fb 7524static int i830_get_display_clock_speed(struct drm_i915_private *dev_priv)
e70236a8 7525{
e907f170 7526 return 133333;
79e53945
JB
7527}
7528
1353c4fb 7529static unsigned int intel_hpll_vco(struct drm_i915_private *dev_priv)
34edce2f 7530{
34edce2f
VS
7531 static const unsigned int blb_vco[8] = {
7532 [0] = 3200000,
7533 [1] = 4000000,
7534 [2] = 5333333,
7535 [3] = 4800000,
7536 [4] = 6400000,
7537 };
7538 static const unsigned int pnv_vco[8] = {
7539 [0] = 3200000,
7540 [1] = 4000000,
7541 [2] = 5333333,
7542 [3] = 4800000,
7543 [4] = 2666667,
7544 };
7545 static const unsigned int cl_vco[8] = {
7546 [0] = 3200000,
7547 [1] = 4000000,
7548 [2] = 5333333,
7549 [3] = 6400000,
7550 [4] = 3333333,
7551 [5] = 3566667,
7552 [6] = 4266667,
7553 };
7554 static const unsigned int elk_vco[8] = {
7555 [0] = 3200000,
7556 [1] = 4000000,
7557 [2] = 5333333,
7558 [3] = 4800000,
7559 };
7560 static const unsigned int ctg_vco[8] = {
7561 [0] = 3200000,
7562 [1] = 4000000,
7563 [2] = 5333333,
7564 [3] = 6400000,
7565 [4] = 2666667,
7566 [5] = 4266667,
7567 };
7568 const unsigned int *vco_table;
7569 unsigned int vco;
7570 uint8_t tmp = 0;
7571
7572 /* FIXME other chipsets? */
50a0bc90 7573 if (IS_GM45(dev_priv))
34edce2f 7574 vco_table = ctg_vco;
9beb5fea 7575 else if (IS_G4X(dev_priv))
34edce2f 7576 vco_table = elk_vco;
1353c4fb 7577 else if (IS_CRESTLINE(dev_priv))
34edce2f 7578 vco_table = cl_vco;
1353c4fb 7579 else if (IS_PINEVIEW(dev_priv))
34edce2f 7580 vco_table = pnv_vco;
1353c4fb 7581 else if (IS_G33(dev_priv))
34edce2f
VS
7582 vco_table = blb_vco;
7583 else
7584 return 0;
7585
1353c4fb 7586 tmp = I915_READ(IS_MOBILE(dev_priv) ? HPLLVCO_MOBILE : HPLLVCO);
34edce2f
VS
7587
7588 vco = vco_table[tmp & 0x7];
7589 if (vco == 0)
7590 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
7591 else
7592 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
7593
7594 return vco;
7595}
7596
1353c4fb 7597static int gm45_get_display_clock_speed(struct drm_i915_private *dev_priv)
34edce2f 7598{
1353c4fb
VS
7599 struct pci_dev *pdev = dev_priv->drm.pdev;
7600 unsigned int cdclk_sel, vco = intel_hpll_vco(dev_priv);
34edce2f
VS
7601 uint16_t tmp = 0;
7602
52a05c30 7603 pci_read_config_word(pdev, GCFGC, &tmp);
34edce2f
VS
7604
7605 cdclk_sel = (tmp >> 12) & 0x1;
7606
7607 switch (vco) {
7608 case 2666667:
7609 case 4000000:
7610 case 5333333:
7611 return cdclk_sel ? 333333 : 222222;
7612 case 3200000:
7613 return cdclk_sel ? 320000 : 228571;
7614 default:
7615 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
7616 return 222222;
7617 }
7618}
7619
1353c4fb 7620static int i965gm_get_display_clock_speed(struct drm_i915_private *dev_priv)
34edce2f 7621{
1353c4fb 7622 struct pci_dev *pdev = dev_priv->drm.pdev;
34edce2f
VS
7623 static const uint8_t div_3200[] = { 16, 10, 8 };
7624 static const uint8_t div_4000[] = { 20, 12, 10 };
7625 static const uint8_t div_5333[] = { 24, 16, 14 };
7626 const uint8_t *div_table;
1353c4fb 7627 unsigned int cdclk_sel, vco = intel_hpll_vco(dev_priv);
34edce2f
VS
7628 uint16_t tmp = 0;
7629
52a05c30 7630 pci_read_config_word(pdev, GCFGC, &tmp);
34edce2f
VS
7631
7632 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
7633
7634 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7635 goto fail;
7636
7637 switch (vco) {
7638 case 3200000:
7639 div_table = div_3200;
7640 break;
7641 case 4000000:
7642 div_table = div_4000;
7643 break;
7644 case 5333333:
7645 div_table = div_5333;
7646 break;
7647 default:
7648 goto fail;
7649 }
7650
7651 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7652
caf4e252 7653fail:
34edce2f
VS
7654 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
7655 return 200000;
7656}
7657
1353c4fb 7658static int g33_get_display_clock_speed(struct drm_i915_private *dev_priv)
34edce2f 7659{
1353c4fb 7660 struct pci_dev *pdev = dev_priv->drm.pdev;
34edce2f
VS
7661 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
7662 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
7663 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
7664 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
7665 const uint8_t *div_table;
1353c4fb 7666 unsigned int cdclk_sel, vco = intel_hpll_vco(dev_priv);
34edce2f
VS
7667 uint16_t tmp = 0;
7668
52a05c30 7669 pci_read_config_word(pdev, GCFGC, &tmp);
34edce2f
VS
7670
7671 cdclk_sel = (tmp >> 4) & 0x7;
7672
7673 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7674 goto fail;
7675
7676 switch (vco) {
7677 case 3200000:
7678 div_table = div_3200;
7679 break;
7680 case 4000000:
7681 div_table = div_4000;
7682 break;
7683 case 4800000:
7684 div_table = div_4800;
7685 break;
7686 case 5333333:
7687 div_table = div_5333;
7688 break;
7689 default:
7690 goto fail;
7691 }
7692
7693 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7694
caf4e252 7695fail:
34edce2f
VS
7696 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7697 return 190476;
7698}
7699
2c07245f 7700static void
a65851af 7701intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 7702{
a65851af
VS
7703 while (*num > DATA_LINK_M_N_MASK ||
7704 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
7705 *num >>= 1;
7706 *den >>= 1;
7707 }
7708}
7709
a65851af
VS
7710static void compute_m_n(unsigned int m, unsigned int n,
7711 uint32_t *ret_m, uint32_t *ret_n)
7712{
7713 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7714 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7715 intel_reduce_m_n_ratio(ret_m, ret_n);
7716}
7717
e69d0bc1
DV
7718void
7719intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7720 int pixel_clock, int link_clock,
7721 struct intel_link_m_n *m_n)
2c07245f 7722{
e69d0bc1 7723 m_n->tu = 64;
a65851af
VS
7724
7725 compute_m_n(bits_per_pixel * pixel_clock,
7726 link_clock * nlanes * 8,
7727 &m_n->gmch_m, &m_n->gmch_n);
7728
7729 compute_m_n(pixel_clock, link_clock,
7730 &m_n->link_m, &m_n->link_n);
2c07245f
ZW
7731}
7732
a7615030
CW
7733static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7734{
d330a953
JN
7735 if (i915.panel_use_ssc >= 0)
7736 return i915.panel_use_ssc != 0;
41aa3448 7737 return dev_priv->vbt.lvds_use_ssc
435793df 7738 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
7739}
7740
7429e9d4 7741static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 7742{
7df00d7a 7743 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 7744}
f47709a9 7745
7429e9d4
DV
7746static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7747{
7748 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
7749}
7750
f47709a9 7751static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
190f68c5 7752 struct intel_crtc_state *crtc_state,
9e2c8475 7753 struct dpll *reduced_clock)
a7516a05 7754{
9b1e14f4 7755 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
a7516a05
JB
7756 u32 fp, fp2 = 0;
7757
9b1e14f4 7758 if (IS_PINEVIEW(dev_priv)) {
190f68c5 7759 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7760 if (reduced_clock)
7429e9d4 7761 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 7762 } else {
190f68c5 7763 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7764 if (reduced_clock)
7429e9d4 7765 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
7766 }
7767
190f68c5 7768 crtc_state->dpll_hw_state.fp0 = fp;
a7516a05 7769
f47709a9 7770 crtc->lowfreq_avail = false;
2d84d2b3 7771 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ab585dea 7772 reduced_clock) {
190f68c5 7773 crtc_state->dpll_hw_state.fp1 = fp2;
f47709a9 7774 crtc->lowfreq_avail = true;
a7516a05 7775 } else {
190f68c5 7776 crtc_state->dpll_hw_state.fp1 = fp;
a7516a05
JB
7777 }
7778}
7779
5e69f97f
CML
7780static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7781 pipe)
89b667f8
JB
7782{
7783 u32 reg_val;
7784
7785 /*
7786 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7787 * and set it to a reasonable value instead.
7788 */
ab3c759a 7789 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
7790 reg_val &= 0xffffff00;
7791 reg_val |= 0x00000030;
ab3c759a 7792 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7793
ab3c759a 7794 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7795 reg_val &= 0x8cffffff;
7796 reg_val = 0x8c000000;
ab3c759a 7797 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 7798
ab3c759a 7799 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 7800 reg_val &= 0xffffff00;
ab3c759a 7801 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7802
ab3c759a 7803 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7804 reg_val &= 0x00ffffff;
7805 reg_val |= 0xb0000000;
ab3c759a 7806 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
7807}
7808
b551842d
DV
7809static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7810 struct intel_link_m_n *m_n)
7811{
7812 struct drm_device *dev = crtc->base.dev;
fac5e23e 7813 struct drm_i915_private *dev_priv = to_i915(dev);
b551842d
DV
7814 int pipe = crtc->pipe;
7815
e3b95f1e
DV
7816 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7817 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7818 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7819 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
7820}
7821
7822static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
7823 struct intel_link_m_n *m_n,
7824 struct intel_link_m_n *m2_n2)
b551842d 7825{
6315b5d3 7826 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
b551842d 7827 int pipe = crtc->pipe;
6e3c9717 7828 enum transcoder transcoder = crtc->config->cpu_transcoder;
b551842d 7829
6315b5d3 7830 if (INTEL_GEN(dev_priv) >= 5) {
b551842d
DV
7831 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7832 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7833 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7834 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
f769cd24
VK
7835 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7836 * for gen < 8) and if DRRS is supported (to make sure the
7837 * registers are not unnecessarily accessed).
7838 */
920a14b2
TU
7839 if (m2_n2 && (IS_CHERRYVIEW(dev_priv) ||
7840 INTEL_GEN(dev_priv) < 8) && crtc->config->has_drrs) {
f769cd24
VK
7841 I915_WRITE(PIPE_DATA_M2(transcoder),
7842 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7843 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7844 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7845 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7846 }
b551842d 7847 } else {
e3b95f1e
DV
7848 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7849 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7850 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7851 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
7852 }
7853}
7854
fe3cd48d 7855void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
03afc4a2 7856{
fe3cd48d
R
7857 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7858
7859 if (m_n == M1_N1) {
7860 dp_m_n = &crtc->config->dp_m_n;
7861 dp_m2_n2 = &crtc->config->dp_m2_n2;
7862 } else if (m_n == M2_N2) {
7863
7864 /*
7865 * M2_N2 registers are not supported. Hence m2_n2 divider value
7866 * needs to be programmed into M1_N1.
7867 */
7868 dp_m_n = &crtc->config->dp_m2_n2;
7869 } else {
7870 DRM_ERROR("Unsupported divider value\n");
7871 return;
7872 }
7873
6e3c9717
ACO
7874 if (crtc->config->has_pch_encoder)
7875 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
03afc4a2 7876 else
fe3cd48d 7877 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
03afc4a2
DV
7878}
7879
251ac862
DV
7880static void vlv_compute_dpll(struct intel_crtc *crtc,
7881 struct intel_crtc_state *pipe_config)
bdd4b6a6 7882{
03ed5cbf 7883 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
cd2d34d9 7884 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
03ed5cbf
VS
7885 if (crtc->pipe != PIPE_A)
7886 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
bdd4b6a6 7887
cd2d34d9 7888 /* DPLL not used with DSI, but still need the rest set up */
d7edc4e5 7889 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
cd2d34d9
VS
7890 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
7891 DPLL_EXT_BUFFER_ENABLE_VLV;
7892
03ed5cbf
VS
7893 pipe_config->dpll_hw_state.dpll_md =
7894 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
7895}
bdd4b6a6 7896
03ed5cbf
VS
7897static void chv_compute_dpll(struct intel_crtc *crtc,
7898 struct intel_crtc_state *pipe_config)
7899{
7900 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
cd2d34d9 7901 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
03ed5cbf
VS
7902 if (crtc->pipe != PIPE_A)
7903 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7904
cd2d34d9 7905 /* DPLL not used with DSI, but still need the rest set up */
d7edc4e5 7906 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
cd2d34d9
VS
7907 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
7908
03ed5cbf
VS
7909 pipe_config->dpll_hw_state.dpll_md =
7910 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
bdd4b6a6
DV
7911}
7912
d288f65f 7913static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7914 const struct intel_crtc_state *pipe_config)
a0c4da24 7915{
f47709a9 7916 struct drm_device *dev = crtc->base.dev;
fac5e23e 7917 struct drm_i915_private *dev_priv = to_i915(dev);
cd2d34d9 7918 enum pipe pipe = crtc->pipe;
bdd4b6a6 7919 u32 mdiv;
a0c4da24 7920 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 7921 u32 coreclk, reg_val;
a0c4da24 7922
cd2d34d9
VS
7923 /* Enable Refclk */
7924 I915_WRITE(DPLL(pipe),
7925 pipe_config->dpll_hw_state.dpll &
7926 ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
7927
7928 /* No need to actually set up the DPLL with DSI */
7929 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7930 return;
7931
a580516d 7932 mutex_lock(&dev_priv->sb_lock);
09153000 7933
d288f65f
VS
7934 bestn = pipe_config->dpll.n;
7935 bestm1 = pipe_config->dpll.m1;
7936 bestm2 = pipe_config->dpll.m2;
7937 bestp1 = pipe_config->dpll.p1;
7938 bestp2 = pipe_config->dpll.p2;
a0c4da24 7939
89b667f8
JB
7940 /* See eDP HDMI DPIO driver vbios notes doc */
7941
7942 /* PLL B needs special handling */
bdd4b6a6 7943 if (pipe == PIPE_B)
5e69f97f 7944 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
7945
7946 /* Set up Tx target for periodic Rcomp update */
ab3c759a 7947 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
7948
7949 /* Disable target IRef on PLL */
ab3c759a 7950 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 7951 reg_val &= 0x00ffffff;
ab3c759a 7952 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
7953
7954 /* Disable fast lock */
ab3c759a 7955 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
7956
7957 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
7958 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7959 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7960 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 7961 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
7962
7963 /*
7964 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7965 * but we don't support that).
7966 * Note: don't use the DAC post divider as it seems unstable.
7967 */
7968 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 7969 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7970
a0c4da24 7971 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 7972 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7973
89b667f8 7974 /* Set HBR and RBR LPF coefficients */
d288f65f 7975 if (pipe_config->port_clock == 162000 ||
2d84d2b3
VS
7976 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
7977 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
ab3c759a 7978 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 7979 0x009f0003);
89b667f8 7980 else
ab3c759a 7981 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
7982 0x00d0000f);
7983
37a5650b 7984 if (intel_crtc_has_dp_encoder(pipe_config)) {
89b667f8 7985 /* Use SSC source */
bdd4b6a6 7986 if (pipe == PIPE_A)
ab3c759a 7987 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7988 0x0df40000);
7989 else
ab3c759a 7990 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7991 0x0df70000);
7992 } else { /* HDMI or VGA */
7993 /* Use bend source */
bdd4b6a6 7994 if (pipe == PIPE_A)
ab3c759a 7995 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7996 0x0df70000);
7997 else
ab3c759a 7998 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7999 0x0df40000);
8000 }
a0c4da24 8001
ab3c759a 8002 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8 8003 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
2210ce7f 8004 if (intel_crtc_has_dp_encoder(crtc->config))
89b667f8 8005 coreclk |= 0x01000000;
ab3c759a 8006 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 8007
ab3c759a 8008 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
a580516d 8009 mutex_unlock(&dev_priv->sb_lock);
a0c4da24
JB
8010}
8011
d288f65f 8012static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 8013 const struct intel_crtc_state *pipe_config)
9d556c99
CML
8014{
8015 struct drm_device *dev = crtc->base.dev;
fac5e23e 8016 struct drm_i915_private *dev_priv = to_i915(dev);
cd2d34d9 8017 enum pipe pipe = crtc->pipe;
9d556c99 8018 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9cbe40c1 8019 u32 loopfilter, tribuf_calcntr;
9d556c99 8020 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
a945ce7e 8021 u32 dpio_val;
9cbe40c1 8022 int vco;
9d556c99 8023
cd2d34d9
VS
8024 /* Enable Refclk and SSC */
8025 I915_WRITE(DPLL(pipe),
8026 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
8027
8028 /* No need to actually set up the DPLL with DSI */
8029 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8030 return;
8031
d288f65f
VS
8032 bestn = pipe_config->dpll.n;
8033 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
8034 bestm1 = pipe_config->dpll.m1;
8035 bestm2 = pipe_config->dpll.m2 >> 22;
8036 bestp1 = pipe_config->dpll.p1;
8037 bestp2 = pipe_config->dpll.p2;
9cbe40c1 8038 vco = pipe_config->dpll.vco;
a945ce7e 8039 dpio_val = 0;
9cbe40c1 8040 loopfilter = 0;
9d556c99 8041
a580516d 8042 mutex_lock(&dev_priv->sb_lock);
9d556c99 8043
9d556c99
CML
8044 /* p1 and p2 divider */
8045 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
8046 5 << DPIO_CHV_S1_DIV_SHIFT |
8047 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
8048 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
8049 1 << DPIO_CHV_K_DIV_SHIFT);
8050
8051 /* Feedback post-divider - m2 */
8052 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
8053
8054 /* Feedback refclk divider - n and m1 */
8055 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
8056 DPIO_CHV_M1_DIV_BY_2 |
8057 1 << DPIO_CHV_N_DIV_SHIFT);
8058
8059 /* M2 fraction division */
25a25dfc 8060 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
9d556c99
CML
8061
8062 /* M2 fraction division enable */
a945ce7e
VP
8063 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
8064 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
8065 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
8066 if (bestm2_frac)
8067 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
8068 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
9d556c99 8069
de3a0fde
VP
8070 /* Program digital lock detect threshold */
8071 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
8072 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
8073 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
8074 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
8075 if (!bestm2_frac)
8076 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
8077 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
8078
9d556c99 8079 /* Loop filter */
9cbe40c1
VP
8080 if (vco == 5400000) {
8081 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
8082 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
8083 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
8084 tribuf_calcntr = 0x9;
8085 } else if (vco <= 6200000) {
8086 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
8087 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
8088 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
8089 tribuf_calcntr = 0x9;
8090 } else if (vco <= 6480000) {
8091 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
8092 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
8093 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
8094 tribuf_calcntr = 0x8;
8095 } else {
8096 /* Not supported. Apply the same limits as in the max case */
8097 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
8098 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
8099 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
8100 tribuf_calcntr = 0;
8101 }
9d556c99
CML
8102 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
8103
968040b2 8104 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
9cbe40c1
VP
8105 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
8106 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
8107 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
8108
9d556c99
CML
8109 /* AFC Recal */
8110 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
8111 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
8112 DPIO_AFC_RECAL);
8113
a580516d 8114 mutex_unlock(&dev_priv->sb_lock);
9d556c99
CML
8115}
8116
d288f65f
VS
8117/**
8118 * vlv_force_pll_on - forcibly enable just the PLL
8119 * @dev_priv: i915 private structure
8120 * @pipe: pipe PLL to enable
8121 * @dpll: PLL configuration
8122 *
8123 * Enable the PLL for @pipe using the supplied @dpll config. To be used
8124 * in cases where we need the PLL enabled even when @pipe is not going to
8125 * be enabled.
8126 */
30ad9814 8127int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
3f36b937 8128 const struct dpll *dpll)
d288f65f 8129{
b91eb5cc 8130 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
3f36b937
TU
8131 struct intel_crtc_state *pipe_config;
8132
8133 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
8134 if (!pipe_config)
8135 return -ENOMEM;
8136
8137 pipe_config->base.crtc = &crtc->base;
8138 pipe_config->pixel_multiplier = 1;
8139 pipe_config->dpll = *dpll;
d288f65f 8140
30ad9814 8141 if (IS_CHERRYVIEW(dev_priv)) {
3f36b937
TU
8142 chv_compute_dpll(crtc, pipe_config);
8143 chv_prepare_pll(crtc, pipe_config);
8144 chv_enable_pll(crtc, pipe_config);
d288f65f 8145 } else {
3f36b937
TU
8146 vlv_compute_dpll(crtc, pipe_config);
8147 vlv_prepare_pll(crtc, pipe_config);
8148 vlv_enable_pll(crtc, pipe_config);
d288f65f 8149 }
3f36b937
TU
8150
8151 kfree(pipe_config);
8152
8153 return 0;
d288f65f
VS
8154}
8155
8156/**
8157 * vlv_force_pll_off - forcibly disable just the PLL
8158 * @dev_priv: i915 private structure
8159 * @pipe: pipe PLL to disable
8160 *
8161 * Disable the PLL for @pipe. To be used in cases where we need
8162 * the PLL enabled even when @pipe is not going to be enabled.
8163 */
30ad9814 8164void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
d288f65f 8165{
30ad9814
VS
8166 if (IS_CHERRYVIEW(dev_priv))
8167 chv_disable_pll(dev_priv, pipe);
d288f65f 8168 else
30ad9814 8169 vlv_disable_pll(dev_priv, pipe);
d288f65f
VS
8170}
8171
251ac862
DV
8172static void i9xx_compute_dpll(struct intel_crtc *crtc,
8173 struct intel_crtc_state *crtc_state,
9e2c8475 8174 struct dpll *reduced_clock)
eb1cbe48 8175{
9b1e14f4 8176 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
eb1cbe48 8177 u32 dpll;
190f68c5 8178 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 8179
190f68c5 8180 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 8181
eb1cbe48
DV
8182 dpll = DPLL_VGA_MODE_DIS;
8183
2d84d2b3 8184 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
8185 dpll |= DPLLB_MODE_LVDS;
8186 else
8187 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 8188
50a0bc90 8189 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) || IS_G33(dev_priv)) {
190f68c5 8190 dpll |= (crtc_state->pixel_multiplier - 1)
198a037f 8191 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 8192 }
198a037f 8193
3d6e9ee0
VS
8194 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
8195 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
4a33e48d 8196 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 8197
37a5650b 8198 if (intel_crtc_has_dp_encoder(crtc_state))
4a33e48d 8199 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
8200
8201 /* compute bitmask from p1 value */
9b1e14f4 8202 if (IS_PINEVIEW(dev_priv))
eb1cbe48
DV
8203 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
8204 else {
8205 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
9beb5fea 8206 if (IS_G4X(dev_priv) && reduced_clock)
eb1cbe48
DV
8207 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
8208 }
8209 switch (clock->p2) {
8210 case 5:
8211 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8212 break;
8213 case 7:
8214 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8215 break;
8216 case 10:
8217 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8218 break;
8219 case 14:
8220 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8221 break;
8222 }
9b1e14f4 8223 if (INTEL_GEN(dev_priv) >= 4)
eb1cbe48
DV
8224 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
8225
190f68c5 8226 if (crtc_state->sdvo_tv_clock)
eb1cbe48 8227 dpll |= PLL_REF_INPUT_TVCLKINBC;
2d84d2b3 8228 else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 8229 intel_panel_use_ssc(dev_priv))
eb1cbe48
DV
8230 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
8231 else
8232 dpll |= PLL_REF_INPUT_DREFCLK;
8233
8234 dpll |= DPLL_VCO_ENABLE;
190f68c5 8235 crtc_state->dpll_hw_state.dpll = dpll;
8bcc2795 8236
9b1e14f4 8237 if (INTEL_GEN(dev_priv) >= 4) {
190f68c5 8238 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
ef1b460d 8239 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
190f68c5 8240 crtc_state->dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
DV
8241 }
8242}
8243
251ac862
DV
8244static void i8xx_compute_dpll(struct intel_crtc *crtc,
8245 struct intel_crtc_state *crtc_state,
9e2c8475 8246 struct dpll *reduced_clock)
eb1cbe48 8247{
f47709a9 8248 struct drm_device *dev = crtc->base.dev;
fac5e23e 8249 struct drm_i915_private *dev_priv = to_i915(dev);
eb1cbe48 8250 u32 dpll;
190f68c5 8251 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 8252
190f68c5 8253 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 8254
eb1cbe48
DV
8255 dpll = DPLL_VGA_MODE_DIS;
8256
2d84d2b3 8257 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
8258 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
8259 } else {
8260 if (clock->p1 == 2)
8261 dpll |= PLL_P1_DIVIDE_BY_TWO;
8262 else
8263 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
8264 if (clock->p2 == 4)
8265 dpll |= PLL_P2_DIVIDE_BY_4;
8266 }
8267
50a0bc90
TU
8268 if (!IS_I830(dev_priv) &&
8269 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
4a33e48d
DV
8270 dpll |= DPLL_DVO_2X_MODE;
8271
2d84d2b3 8272 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 8273 intel_panel_use_ssc(dev_priv))
eb1cbe48
DV
8274 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
8275 else
8276 dpll |= PLL_REF_INPUT_DREFCLK;
8277
8278 dpll |= DPLL_VCO_ENABLE;
190f68c5 8279 crtc_state->dpll_hw_state.dpll = dpll;
eb1cbe48
DV
8280}
8281
8a654f3b 8282static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c 8283{
6315b5d3 8284 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
b0e77b9c 8285 enum pipe pipe = intel_crtc->pipe;
6e3c9717 8286 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
7c5f93b0 8287 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
1caea6e9
VS
8288 uint32_t crtc_vtotal, crtc_vblank_end;
8289 int vsyncshift = 0;
4d8a62ea
DV
8290
8291 /* We need to be careful not to changed the adjusted mode, for otherwise
8292 * the hw state checker will get angry at the mismatch. */
8293 crtc_vtotal = adjusted_mode->crtc_vtotal;
8294 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 8295
609aeaca 8296 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 8297 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
8298 crtc_vtotal -= 1;
8299 crtc_vblank_end -= 1;
609aeaca 8300
2d84d2b3 8301 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
609aeaca
VS
8302 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
8303 else
8304 vsyncshift = adjusted_mode->crtc_hsync_start -
8305 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
8306 if (vsyncshift < 0)
8307 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
8308 }
8309
6315b5d3 8310 if (INTEL_GEN(dev_priv) > 3)
fe2b8f9d 8311 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 8312
fe2b8f9d 8313 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
8314 (adjusted_mode->crtc_hdisplay - 1) |
8315 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 8316 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
8317 (adjusted_mode->crtc_hblank_start - 1) |
8318 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 8319 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
8320 (adjusted_mode->crtc_hsync_start - 1) |
8321 ((adjusted_mode->crtc_hsync_end - 1) << 16));
8322
fe2b8f9d 8323 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 8324 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 8325 ((crtc_vtotal - 1) << 16));
fe2b8f9d 8326 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 8327 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 8328 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 8329 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
8330 (adjusted_mode->crtc_vsync_start - 1) |
8331 ((adjusted_mode->crtc_vsync_end - 1) << 16));
8332
b5e508d4
PZ
8333 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
8334 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
8335 * documented on the DDI_FUNC_CTL register description, EDP Input Select
8336 * bits. */
772c2a51 8337 if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
b5e508d4
PZ
8338 (pipe == PIPE_B || pipe == PIPE_C))
8339 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
8340
bc58be60
JN
8341}
8342
8343static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
8344{
8345 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 8346 struct drm_i915_private *dev_priv = to_i915(dev);
bc58be60
JN
8347 enum pipe pipe = intel_crtc->pipe;
8348
b0e77b9c
PZ
8349 /* pipesrc controls the size that is scaled from, which should
8350 * always be the user's requested size.
8351 */
8352 I915_WRITE(PIPESRC(pipe),
6e3c9717
ACO
8353 ((intel_crtc->config->pipe_src_w - 1) << 16) |
8354 (intel_crtc->config->pipe_src_h - 1));
b0e77b9c
PZ
8355}
8356
1bd1bd80 8357static void intel_get_pipe_timings(struct intel_crtc *crtc,
5cec258b 8358 struct intel_crtc_state *pipe_config)
1bd1bd80
DV
8359{
8360 struct drm_device *dev = crtc->base.dev;
fac5e23e 8361 struct drm_i915_private *dev_priv = to_i915(dev);
1bd1bd80
DV
8362 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
8363 uint32_t tmp;
8364
8365 tmp = I915_READ(HTOTAL(cpu_transcoder));
2d112de7
ACO
8366 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
8367 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 8368 tmp = I915_READ(HBLANK(cpu_transcoder));
2d112de7
ACO
8369 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
8370 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 8371 tmp = I915_READ(HSYNC(cpu_transcoder));
2d112de7
ACO
8372 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
8373 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
8374
8375 tmp = I915_READ(VTOTAL(cpu_transcoder));
2d112de7
ACO
8376 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
8377 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 8378 tmp = I915_READ(VBLANK(cpu_transcoder));
2d112de7
ACO
8379 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
8380 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 8381 tmp = I915_READ(VSYNC(cpu_transcoder));
2d112de7
ACO
8382 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
8383 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
8384
8385 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
2d112de7
ACO
8386 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
8387 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
8388 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
1bd1bd80 8389 }
bc58be60
JN
8390}
8391
8392static void intel_get_pipe_src_size(struct intel_crtc *crtc,
8393 struct intel_crtc_state *pipe_config)
8394{
8395 struct drm_device *dev = crtc->base.dev;
fac5e23e 8396 struct drm_i915_private *dev_priv = to_i915(dev);
bc58be60 8397 u32 tmp;
1bd1bd80
DV
8398
8399 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
8400 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
8401 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
8402
2d112de7
ACO
8403 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
8404 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
DV
8405}
8406
f6a83288 8407void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5cec258b 8408 struct intel_crtc_state *pipe_config)
babea61d 8409{
2d112de7
ACO
8410 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
8411 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
8412 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
8413 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
babea61d 8414
2d112de7
ACO
8415 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
8416 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
8417 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
8418 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
babea61d 8419
2d112de7 8420 mode->flags = pipe_config->base.adjusted_mode.flags;
cd13f5ab 8421 mode->type = DRM_MODE_TYPE_DRIVER;
babea61d 8422
2d112de7
ACO
8423 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
8424 mode->flags |= pipe_config->base.adjusted_mode.flags;
cd13f5ab
ML
8425
8426 mode->hsync = drm_mode_hsync(mode);
8427 mode->vrefresh = drm_mode_vrefresh(mode);
8428 drm_mode_set_name(mode);
babea61d
JB
8429}
8430
84b046f3
DV
8431static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
8432{
6315b5d3 8433 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
84b046f3
DV
8434 uint32_t pipeconf;
8435
9f11a9e4 8436 pipeconf = 0;
84b046f3 8437
b6b5d049
VS
8438 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
8439 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
8440 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
67c72a12 8441
6e3c9717 8442 if (intel_crtc->config->double_wide)
cf532bb2 8443 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 8444
ff9ce46e 8445 /* only g4x and later have fancy bpc/dither controls */
9beb5fea
TU
8446 if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
8447 IS_CHERRYVIEW(dev_priv)) {
ff9ce46e 8448 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6e3c9717 8449 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
ff9ce46e 8450 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 8451 PIPECONF_DITHER_TYPE_SP;
84b046f3 8452
6e3c9717 8453 switch (intel_crtc->config->pipe_bpp) {
ff9ce46e
DV
8454 case 18:
8455 pipeconf |= PIPECONF_6BPC;
8456 break;
8457 case 24:
8458 pipeconf |= PIPECONF_8BPC;
8459 break;
8460 case 30:
8461 pipeconf |= PIPECONF_10BPC;
8462 break;
8463 default:
8464 /* Case prevented by intel_choose_pipe_bpp_dither. */
8465 BUG();
84b046f3
DV
8466 }
8467 }
8468
56b857a5 8469 if (HAS_PIPE_CXSR(dev_priv)) {
84b046f3
DV
8470 if (intel_crtc->lowfreq_avail) {
8471 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
8472 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
8473 } else {
8474 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
84b046f3
DV
8475 }
8476 }
8477
6e3c9717 8478 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
6315b5d3 8479 if (INTEL_GEN(dev_priv) < 4 ||
2d84d2b3 8480 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
efc2cfff
VS
8481 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
8482 else
8483 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
8484 } else
84b046f3
DV
8485 pipeconf |= PIPECONF_PROGRESSIVE;
8486
920a14b2 8487 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
666a4537 8488 intel_crtc->config->limited_color_range)
9f11a9e4 8489 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 8490
84b046f3
DV
8491 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
8492 POSTING_READ(PIPECONF(intel_crtc->pipe));
8493}
8494
81c97f52
ACO
8495static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
8496 struct intel_crtc_state *crtc_state)
8497{
8498 struct drm_device *dev = crtc->base.dev;
fac5e23e 8499 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 8500 const struct intel_limit *limit;
81c97f52
ACO
8501 int refclk = 48000;
8502
8503 memset(&crtc_state->dpll_hw_state, 0,
8504 sizeof(crtc_state->dpll_hw_state));
8505
2d84d2b3 8506 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
81c97f52
ACO
8507 if (intel_panel_use_ssc(dev_priv)) {
8508 refclk = dev_priv->vbt.lvds_ssc_freq;
8509 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8510 }
8511
8512 limit = &intel_limits_i8xx_lvds;
2d84d2b3 8513 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
81c97f52
ACO
8514 limit = &intel_limits_i8xx_dvo;
8515 } else {
8516 limit = &intel_limits_i8xx_dac;
8517 }
8518
8519 if (!crtc_state->clock_set &&
8520 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8521 refclk, NULL, &crtc_state->dpll)) {
8522 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8523 return -EINVAL;
8524 }
8525
8526 i8xx_compute_dpll(crtc, crtc_state, NULL);
8527
8528 return 0;
8529}
8530
19ec6693
ACO
8531static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
8532 struct intel_crtc_state *crtc_state)
8533{
8534 struct drm_device *dev = crtc->base.dev;
fac5e23e 8535 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 8536 const struct intel_limit *limit;
19ec6693
ACO
8537 int refclk = 96000;
8538
8539 memset(&crtc_state->dpll_hw_state, 0,
8540 sizeof(crtc_state->dpll_hw_state));
8541
2d84d2b3 8542 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
19ec6693
ACO
8543 if (intel_panel_use_ssc(dev_priv)) {
8544 refclk = dev_priv->vbt.lvds_ssc_freq;
8545 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8546 }
8547
8548 if (intel_is_dual_link_lvds(dev))
8549 limit = &intel_limits_g4x_dual_channel_lvds;
8550 else
8551 limit = &intel_limits_g4x_single_channel_lvds;
2d84d2b3
VS
8552 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
8553 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
19ec6693 8554 limit = &intel_limits_g4x_hdmi;
2d84d2b3 8555 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
19ec6693
ACO
8556 limit = &intel_limits_g4x_sdvo;
8557 } else {
8558 /* The option is for other outputs */
8559 limit = &intel_limits_i9xx_sdvo;
8560 }
8561
8562 if (!crtc_state->clock_set &&
8563 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8564 refclk, NULL, &crtc_state->dpll)) {
8565 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8566 return -EINVAL;
8567 }
8568
8569 i9xx_compute_dpll(crtc, crtc_state, NULL);
8570
8571 return 0;
8572}
8573
70e8aa21
ACO
8574static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
8575 struct intel_crtc_state *crtc_state)
8576{
8577 struct drm_device *dev = crtc->base.dev;
fac5e23e 8578 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 8579 const struct intel_limit *limit;
70e8aa21
ACO
8580 int refclk = 96000;
8581
8582 memset(&crtc_state->dpll_hw_state, 0,
8583 sizeof(crtc_state->dpll_hw_state));
8584
2d84d2b3 8585 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
70e8aa21
ACO
8586 if (intel_panel_use_ssc(dev_priv)) {
8587 refclk = dev_priv->vbt.lvds_ssc_freq;
8588 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8589 }
8590
8591 limit = &intel_limits_pineview_lvds;
8592 } else {
8593 limit = &intel_limits_pineview_sdvo;
8594 }
8595
8596 if (!crtc_state->clock_set &&
8597 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8598 refclk, NULL, &crtc_state->dpll)) {
8599 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8600 return -EINVAL;
8601 }
8602
8603 i9xx_compute_dpll(crtc, crtc_state, NULL);
8604
8605 return 0;
8606}
8607
190f68c5
ACO
8608static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
8609 struct intel_crtc_state *crtc_state)
79e53945 8610{
c7653199 8611 struct drm_device *dev = crtc->base.dev;
fac5e23e 8612 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 8613 const struct intel_limit *limit;
81c97f52 8614 int refclk = 96000;
79e53945 8615
dd3cd74a
ACO
8616 memset(&crtc_state->dpll_hw_state, 0,
8617 sizeof(crtc_state->dpll_hw_state));
8618
2d84d2b3 8619 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
70e8aa21
ACO
8620 if (intel_panel_use_ssc(dev_priv)) {
8621 refclk = dev_priv->vbt.lvds_ssc_freq;
8622 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8623 }
43565a06 8624
70e8aa21
ACO
8625 limit = &intel_limits_i9xx_lvds;
8626 } else {
8627 limit = &intel_limits_i9xx_sdvo;
81c97f52 8628 }
79e53945 8629
70e8aa21
ACO
8630 if (!crtc_state->clock_set &&
8631 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8632 refclk, NULL, &crtc_state->dpll)) {
8633 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8634 return -EINVAL;
f47709a9 8635 }
7026d4ac 8636
81c97f52 8637 i9xx_compute_dpll(crtc, crtc_state, NULL);
79e53945 8638
c8f7a0db 8639 return 0;
f564048e
EA
8640}
8641
65b3d6a9
ACO
8642static int chv_crtc_compute_clock(struct intel_crtc *crtc,
8643 struct intel_crtc_state *crtc_state)
8644{
8645 int refclk = 100000;
1b6f4958 8646 const struct intel_limit *limit = &intel_limits_chv;
65b3d6a9
ACO
8647
8648 memset(&crtc_state->dpll_hw_state, 0,
8649 sizeof(crtc_state->dpll_hw_state));
8650
65b3d6a9
ACO
8651 if (!crtc_state->clock_set &&
8652 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8653 refclk, NULL, &crtc_state->dpll)) {
8654 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8655 return -EINVAL;
8656 }
8657
8658 chv_compute_dpll(crtc, crtc_state);
8659
8660 return 0;
8661}
8662
8663static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
8664 struct intel_crtc_state *crtc_state)
8665{
8666 int refclk = 100000;
1b6f4958 8667 const struct intel_limit *limit = &intel_limits_vlv;
65b3d6a9
ACO
8668
8669 memset(&crtc_state->dpll_hw_state, 0,
8670 sizeof(crtc_state->dpll_hw_state));
8671
65b3d6a9
ACO
8672 if (!crtc_state->clock_set &&
8673 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8674 refclk, NULL, &crtc_state->dpll)) {
8675 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8676 return -EINVAL;
8677 }
8678
8679 vlv_compute_dpll(crtc, crtc_state);
8680
8681 return 0;
8682}
8683
2fa2fe9a 8684static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5cec258b 8685 struct intel_crtc_state *pipe_config)
2fa2fe9a 8686{
6315b5d3 8687 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2fa2fe9a
DV
8688 uint32_t tmp;
8689
50a0bc90
TU
8690 if (INTEL_GEN(dev_priv) <= 3 &&
8691 (IS_I830(dev_priv) || !IS_MOBILE(dev_priv)))
dc9e7dec
VS
8692 return;
8693
2fa2fe9a 8694 tmp = I915_READ(PFIT_CONTROL);
06922821
DV
8695 if (!(tmp & PFIT_ENABLE))
8696 return;
2fa2fe9a 8697
06922821 8698 /* Check whether the pfit is attached to our pipe. */
6315b5d3 8699 if (INTEL_GEN(dev_priv) < 4) {
2fa2fe9a
DV
8700 if (crtc->pipe != PIPE_B)
8701 return;
2fa2fe9a
DV
8702 } else {
8703 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
8704 return;
8705 }
8706
06922821 8707 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a 8708 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
2fa2fe9a
DV
8709}
8710
acbec814 8711static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 8712 struct intel_crtc_state *pipe_config)
acbec814
JB
8713{
8714 struct drm_device *dev = crtc->base.dev;
fac5e23e 8715 struct drm_i915_private *dev_priv = to_i915(dev);
acbec814 8716 int pipe = pipe_config->cpu_transcoder;
9e2c8475 8717 struct dpll clock;
acbec814 8718 u32 mdiv;
662c6ecb 8719 int refclk = 100000;
acbec814 8720
b521973b
VS
8721 /* In case of DSI, DPLL will not be used */
8722 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
f573de5a
SK
8723 return;
8724
a580516d 8725 mutex_lock(&dev_priv->sb_lock);
ab3c759a 8726 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
a580516d 8727 mutex_unlock(&dev_priv->sb_lock);
acbec814
JB
8728
8729 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8730 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8731 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8732 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8733 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8734
dccbea3b 8735 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
acbec814
JB
8736}
8737
5724dbd1
DL
8738static void
8739i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8740 struct intel_initial_plane_config *plane_config)
1ad292b5
JB
8741{
8742 struct drm_device *dev = crtc->base.dev;
fac5e23e 8743 struct drm_i915_private *dev_priv = to_i915(dev);
1ad292b5
JB
8744 u32 val, base, offset;
8745 int pipe = crtc->pipe, plane = crtc->plane;
8746 int fourcc, pixel_format;
6761dd31 8747 unsigned int aligned_height;
b113d5ee 8748 struct drm_framebuffer *fb;
1b842c89 8749 struct intel_framebuffer *intel_fb;
1ad292b5 8750
42a7b088
DL
8751 val = I915_READ(DSPCNTR(plane));
8752 if (!(val & DISPLAY_PLANE_ENABLE))
8753 return;
8754
d9806c9f 8755 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 8756 if (!intel_fb) {
1ad292b5
JB
8757 DRM_DEBUG_KMS("failed to alloc fb\n");
8758 return;
8759 }
8760
1b842c89
DL
8761 fb = &intel_fb->base;
8762
6315b5d3 8763 if (INTEL_GEN(dev_priv) >= 4) {
18c5247e 8764 if (val & DISPPLANE_TILED) {
49af449b 8765 plane_config->tiling = I915_TILING_X;
bae781b2 8766 fb->modifier = I915_FORMAT_MOD_X_TILED;
18c5247e
DV
8767 }
8768 }
1ad292b5
JB
8769
8770 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 8771 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
8772 fb->pixel_format = fourcc;
8773 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
1ad292b5 8774
6315b5d3 8775 if (INTEL_GEN(dev_priv) >= 4) {
49af449b 8776 if (plane_config->tiling)
1ad292b5
JB
8777 offset = I915_READ(DSPTILEOFF(plane));
8778 else
8779 offset = I915_READ(DSPLINOFF(plane));
8780 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8781 } else {
8782 base = I915_READ(DSPADDR(plane));
8783 }
8784 plane_config->base = base;
8785
8786 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
8787 fb->width = ((val >> 16) & 0xfff) + 1;
8788 fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
8789
8790 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 8791 fb->pitches[0] = val & 0xffffffc0;
1ad292b5 8792
b113d5ee 8793 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb 8794 fb->pixel_format,
bae781b2 8795 fb->modifier);
1ad292b5 8796
f37b5c2b 8797 plane_config->size = fb->pitches[0] * aligned_height;
1ad292b5 8798
2844a921
DL
8799 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8800 pipe_name(pipe), plane, fb->width, fb->height,
8801 fb->bits_per_pixel, base, fb->pitches[0],
8802 plane_config->size);
1ad292b5 8803
2d14030b 8804 plane_config->fb = intel_fb;
1ad292b5
JB
8805}
8806
70b23a98 8807static void chv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 8808 struct intel_crtc_state *pipe_config)
70b23a98
VS
8809{
8810 struct drm_device *dev = crtc->base.dev;
fac5e23e 8811 struct drm_i915_private *dev_priv = to_i915(dev);
70b23a98
VS
8812 int pipe = pipe_config->cpu_transcoder;
8813 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9e2c8475 8814 struct dpll clock;
0d7b6b11 8815 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
70b23a98
VS
8816 int refclk = 100000;
8817
b521973b
VS
8818 /* In case of DSI, DPLL will not be used */
8819 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8820 return;
8821
a580516d 8822 mutex_lock(&dev_priv->sb_lock);
70b23a98
VS
8823 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8824 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8825 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8826 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
0d7b6b11 8827 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
a580516d 8828 mutex_unlock(&dev_priv->sb_lock);
70b23a98
VS
8829
8830 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
0d7b6b11
ID
8831 clock.m2 = (pll_dw0 & 0xff) << 22;
8832 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8833 clock.m2 |= pll_dw2 & 0x3fffff;
70b23a98
VS
8834 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8835 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8836 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8837
dccbea3b 8838 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
70b23a98
VS
8839}
8840
0e8ffe1b 8841static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5cec258b 8842 struct intel_crtc_state *pipe_config)
0e8ffe1b 8843{
6315b5d3 8844 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1729050e 8845 enum intel_display_power_domain power_domain;
0e8ffe1b 8846 uint32_t tmp;
1729050e 8847 bool ret;
0e8ffe1b 8848
1729050e
ID
8849 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8850 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0
ID
8851 return false;
8852
e143a21c 8853 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 8854 pipe_config->shared_dpll = NULL;
eccb140b 8855
1729050e
ID
8856 ret = false;
8857
0e8ffe1b
DV
8858 tmp = I915_READ(PIPECONF(crtc->pipe));
8859 if (!(tmp & PIPECONF_ENABLE))
1729050e 8860 goto out;
0e8ffe1b 8861
9beb5fea
TU
8862 if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
8863 IS_CHERRYVIEW(dev_priv)) {
42571aef
VS
8864 switch (tmp & PIPECONF_BPC_MASK) {
8865 case PIPECONF_6BPC:
8866 pipe_config->pipe_bpp = 18;
8867 break;
8868 case PIPECONF_8BPC:
8869 pipe_config->pipe_bpp = 24;
8870 break;
8871 case PIPECONF_10BPC:
8872 pipe_config->pipe_bpp = 30;
8873 break;
8874 default:
8875 break;
8876 }
8877 }
8878
920a14b2 8879 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
666a4537 8880 (tmp & PIPECONF_COLOR_RANGE_SELECT))
b5a9fa09
DV
8881 pipe_config->limited_color_range = true;
8882
6315b5d3 8883 if (INTEL_GEN(dev_priv) < 4)
282740f7
VS
8884 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8885
1bd1bd80 8886 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 8887 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 8888
2fa2fe9a
DV
8889 i9xx_get_pfit_config(crtc, pipe_config);
8890
6315b5d3 8891 if (INTEL_GEN(dev_priv) >= 4) {
c231775c 8892 /* No way to read it out on pipes B and C */
920a14b2 8893 if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
c231775c
VS
8894 tmp = dev_priv->chv_dpll_md[crtc->pipe];
8895 else
8896 tmp = I915_READ(DPLL_MD(crtc->pipe));
6c49f241
DV
8897 pipe_config->pixel_multiplier =
8898 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8899 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 8900 pipe_config->dpll_hw_state.dpll_md = tmp;
50a0bc90
TU
8901 } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
8902 IS_G33(dev_priv)) {
6c49f241
DV
8903 tmp = I915_READ(DPLL(crtc->pipe));
8904 pipe_config->pixel_multiplier =
8905 ((tmp & SDVO_MULTIPLIER_MASK)
8906 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8907 } else {
8908 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8909 * port and will be fixed up in the encoder->get_config
8910 * function. */
8911 pipe_config->pixel_multiplier = 1;
8912 }
8bcc2795 8913 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
920a14b2 8914 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
1c4e0274
VS
8915 /*
8916 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8917 * on 830. Filter it out here so that we don't
8918 * report errors due to that.
8919 */
50a0bc90 8920 if (IS_I830(dev_priv))
1c4e0274
VS
8921 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8922
8bcc2795
DV
8923 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8924 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
8925 } else {
8926 /* Mask out read-only status bits. */
8927 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8928 DPLL_PORTC_READY_MASK |
8929 DPLL_PORTB_READY_MASK);
8bcc2795 8930 }
6c49f241 8931
920a14b2 8932 if (IS_CHERRYVIEW(dev_priv))
70b23a98 8933 chv_crtc_clock_get(crtc, pipe_config);
11a914c2 8934 else if (IS_VALLEYVIEW(dev_priv))
acbec814
JB
8935 vlv_crtc_clock_get(crtc, pipe_config);
8936 else
8937 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 8938
0f64614d
VS
8939 /*
8940 * Normally the dotclock is filled in by the encoder .get_config()
8941 * but in case the pipe is enabled w/o any ports we need a sane
8942 * default.
8943 */
8944 pipe_config->base.adjusted_mode.crtc_clock =
8945 pipe_config->port_clock / pipe_config->pixel_multiplier;
8946
1729050e
ID
8947 ret = true;
8948
8949out:
8950 intel_display_power_put(dev_priv, power_domain);
8951
8952 return ret;
0e8ffe1b
DV
8953}
8954
c39055b0 8955static void ironlake_init_pch_refclk(struct drm_i915_private *dev_priv)
13d83a67 8956{
13d83a67 8957 struct intel_encoder *encoder;
1c1a24d2 8958 int i;
74cfd7ac 8959 u32 val, final;
13d83a67 8960 bool has_lvds = false;
199e5d79 8961 bool has_cpu_edp = false;
199e5d79 8962 bool has_panel = false;
99eb6a01
KP
8963 bool has_ck505 = false;
8964 bool can_ssc = false;
1c1a24d2 8965 bool using_ssc_source = false;
13d83a67
JB
8966
8967 /* We need to take the global config into account */
c39055b0 8968 for_each_intel_encoder(&dev_priv->drm, encoder) {
199e5d79
KP
8969 switch (encoder->type) {
8970 case INTEL_OUTPUT_LVDS:
8971 has_panel = true;
8972 has_lvds = true;
8973 break;
8974 case INTEL_OUTPUT_EDP:
8975 has_panel = true;
2de6905f 8976 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
8977 has_cpu_edp = true;
8978 break;
6847d71b
PZ
8979 default:
8980 break;
13d83a67
JB
8981 }
8982 }
8983
6e266956 8984 if (HAS_PCH_IBX(dev_priv)) {
41aa3448 8985 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
8986 can_ssc = has_ck505;
8987 } else {
8988 has_ck505 = false;
8989 can_ssc = true;
8990 }
8991
1c1a24d2
L
8992 /* Check if any DPLLs are using the SSC source */
8993 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
8994 u32 temp = I915_READ(PCH_DPLL(i));
8995
8996 if (!(temp & DPLL_VCO_ENABLE))
8997 continue;
8998
8999 if ((temp & PLL_REF_INPUT_MASK) ==
9000 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
9001 using_ssc_source = true;
9002 break;
9003 }
9004 }
9005
9006 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
9007 has_panel, has_lvds, has_ck505, using_ssc_source);
13d83a67
JB
9008
9009 /* Ironlake: try to setup display ref clock before DPLL
9010 * enabling. This is only under driver's control after
9011 * PCH B stepping, previous chipset stepping should be
9012 * ignoring this setting.
9013 */
74cfd7ac
CW
9014 val = I915_READ(PCH_DREF_CONTROL);
9015
9016 /* As we must carefully and slowly disable/enable each source in turn,
9017 * compute the final state we want first and check if we need to
9018 * make any changes at all.
9019 */
9020 final = val;
9021 final &= ~DREF_NONSPREAD_SOURCE_MASK;
9022 if (has_ck505)
9023 final |= DREF_NONSPREAD_CK505_ENABLE;
9024 else
9025 final |= DREF_NONSPREAD_SOURCE_ENABLE;
9026
8c07eb68 9027 final &= ~DREF_SSC_SOURCE_MASK;
74cfd7ac 9028 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8c07eb68 9029 final &= ~DREF_SSC1_ENABLE;
74cfd7ac
CW
9030
9031 if (has_panel) {
9032 final |= DREF_SSC_SOURCE_ENABLE;
9033
9034 if (intel_panel_use_ssc(dev_priv) && can_ssc)
9035 final |= DREF_SSC1_ENABLE;
9036
9037 if (has_cpu_edp) {
9038 if (intel_panel_use_ssc(dev_priv) && can_ssc)
9039 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
9040 else
9041 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
9042 } else
9043 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
1c1a24d2
L
9044 } else if (using_ssc_source) {
9045 final |= DREF_SSC_SOURCE_ENABLE;
9046 final |= DREF_SSC1_ENABLE;
74cfd7ac
CW
9047 }
9048
9049 if (final == val)
9050 return;
9051
13d83a67 9052 /* Always enable nonspread source */
74cfd7ac 9053 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 9054
99eb6a01 9055 if (has_ck505)
74cfd7ac 9056 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 9057 else
74cfd7ac 9058 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 9059
199e5d79 9060 if (has_panel) {
74cfd7ac
CW
9061 val &= ~DREF_SSC_SOURCE_MASK;
9062 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 9063
199e5d79 9064 /* SSC must be turned on before enabling the CPU output */
99eb6a01 9065 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 9066 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 9067 val |= DREF_SSC1_ENABLE;
e77166b5 9068 } else
74cfd7ac 9069 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
9070
9071 /* Get SSC going before enabling the outputs */
74cfd7ac 9072 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
9073 POSTING_READ(PCH_DREF_CONTROL);
9074 udelay(200);
9075
74cfd7ac 9076 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
9077
9078 /* Enable CPU source on CPU attached eDP */
199e5d79 9079 if (has_cpu_edp) {
99eb6a01 9080 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 9081 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 9082 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 9083 } else
74cfd7ac 9084 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 9085 } else
74cfd7ac 9086 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 9087
74cfd7ac 9088 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
9089 POSTING_READ(PCH_DREF_CONTROL);
9090 udelay(200);
9091 } else {
1c1a24d2 9092 DRM_DEBUG_KMS("Disabling CPU source output\n");
199e5d79 9093
74cfd7ac 9094 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
9095
9096 /* Turn off CPU output */
74cfd7ac 9097 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 9098
74cfd7ac 9099 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
9100 POSTING_READ(PCH_DREF_CONTROL);
9101 udelay(200);
9102
1c1a24d2
L
9103 if (!using_ssc_source) {
9104 DRM_DEBUG_KMS("Disabling SSC source\n");
199e5d79 9105
1c1a24d2
L
9106 /* Turn off the SSC source */
9107 val &= ~DREF_SSC_SOURCE_MASK;
9108 val |= DREF_SSC_SOURCE_DISABLE;
f165d283 9109
1c1a24d2
L
9110 /* Turn off SSC1 */
9111 val &= ~DREF_SSC1_ENABLE;
9112
9113 I915_WRITE(PCH_DREF_CONTROL, val);
9114 POSTING_READ(PCH_DREF_CONTROL);
9115 udelay(200);
9116 }
13d83a67 9117 }
74cfd7ac
CW
9118
9119 BUG_ON(val != final);
13d83a67
JB
9120}
9121
f31f2d55 9122static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 9123{
f31f2d55 9124 uint32_t tmp;
dde86e2d 9125
0ff066a9
PZ
9126 tmp = I915_READ(SOUTH_CHICKEN2);
9127 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
9128 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 9129
cf3598c2
ID
9130 if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
9131 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
0ff066a9 9132 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 9133
0ff066a9
PZ
9134 tmp = I915_READ(SOUTH_CHICKEN2);
9135 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
9136 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 9137
cf3598c2
ID
9138 if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
9139 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
0ff066a9 9140 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
9141}
9142
9143/* WaMPhyProgramming:hsw */
9144static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
9145{
9146 uint32_t tmp;
dde86e2d
PZ
9147
9148 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
9149 tmp &= ~(0xFF << 24);
9150 tmp |= (0x12 << 24);
9151 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
9152
dde86e2d
PZ
9153 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
9154 tmp |= (1 << 11);
9155 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
9156
9157 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
9158 tmp |= (1 << 11);
9159 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
9160
dde86e2d
PZ
9161 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
9162 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
9163 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
9164
9165 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
9166 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
9167 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
9168
0ff066a9
PZ
9169 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
9170 tmp &= ~(7 << 13);
9171 tmp |= (5 << 13);
9172 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 9173
0ff066a9
PZ
9174 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
9175 tmp &= ~(7 << 13);
9176 tmp |= (5 << 13);
9177 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
9178
9179 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
9180 tmp &= ~0xFF;
9181 tmp |= 0x1C;
9182 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
9183
9184 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
9185 tmp &= ~0xFF;
9186 tmp |= 0x1C;
9187 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
9188
9189 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
9190 tmp &= ~(0xFF << 16);
9191 tmp |= (0x1C << 16);
9192 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
9193
9194 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
9195 tmp &= ~(0xFF << 16);
9196 tmp |= (0x1C << 16);
9197 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
9198
0ff066a9
PZ
9199 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
9200 tmp |= (1 << 27);
9201 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 9202
0ff066a9
PZ
9203 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
9204 tmp |= (1 << 27);
9205 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 9206
0ff066a9
PZ
9207 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
9208 tmp &= ~(0xF << 28);
9209 tmp |= (4 << 28);
9210 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 9211
0ff066a9
PZ
9212 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
9213 tmp &= ~(0xF << 28);
9214 tmp |= (4 << 28);
9215 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
9216}
9217
2fa86a1f
PZ
9218/* Implements 3 different sequences from BSpec chapter "Display iCLK
9219 * Programming" based on the parameters passed:
9220 * - Sequence to enable CLKOUT_DP
9221 * - Sequence to enable CLKOUT_DP without spread
9222 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
9223 */
c39055b0
ACO
9224static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
9225 bool with_spread, bool with_fdi)
f31f2d55 9226{
2fa86a1f
PZ
9227 uint32_t reg, tmp;
9228
9229 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
9230 with_spread = true;
4f8036a2
TU
9231 if (WARN(HAS_PCH_LPT_LP(dev_priv) &&
9232 with_fdi, "LP PCH doesn't have FDI\n"))
2fa86a1f 9233 with_fdi = false;
f31f2d55 9234
a580516d 9235 mutex_lock(&dev_priv->sb_lock);
f31f2d55
PZ
9236
9237 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
9238 tmp &= ~SBI_SSCCTL_DISABLE;
9239 tmp |= SBI_SSCCTL_PATHALT;
9240 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
9241
9242 udelay(24);
9243
2fa86a1f
PZ
9244 if (with_spread) {
9245 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
9246 tmp &= ~SBI_SSCCTL_PATHALT;
9247 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 9248
2fa86a1f
PZ
9249 if (with_fdi) {
9250 lpt_reset_fdi_mphy(dev_priv);
9251 lpt_program_fdi_mphy(dev_priv);
9252 }
9253 }
dde86e2d 9254
4f8036a2 9255 reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
2fa86a1f
PZ
9256 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
9257 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
9258 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246 9259
a580516d 9260 mutex_unlock(&dev_priv->sb_lock);
dde86e2d
PZ
9261}
9262
47701c3b 9263/* Sequence to disable CLKOUT_DP */
c39055b0 9264static void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
47701c3b 9265{
47701c3b
PZ
9266 uint32_t reg, tmp;
9267
a580516d 9268 mutex_lock(&dev_priv->sb_lock);
47701c3b 9269
4f8036a2 9270 reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
47701c3b
PZ
9271 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
9272 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
9273 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
9274
9275 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
9276 if (!(tmp & SBI_SSCCTL_DISABLE)) {
9277 if (!(tmp & SBI_SSCCTL_PATHALT)) {
9278 tmp |= SBI_SSCCTL_PATHALT;
9279 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
9280 udelay(32);
9281 }
9282 tmp |= SBI_SSCCTL_DISABLE;
9283 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
9284 }
9285
a580516d 9286 mutex_unlock(&dev_priv->sb_lock);
47701c3b
PZ
9287}
9288
f7be2c21
VS
9289#define BEND_IDX(steps) ((50 + (steps)) / 5)
9290
9291static const uint16_t sscdivintphase[] = {
9292 [BEND_IDX( 50)] = 0x3B23,
9293 [BEND_IDX( 45)] = 0x3B23,
9294 [BEND_IDX( 40)] = 0x3C23,
9295 [BEND_IDX( 35)] = 0x3C23,
9296 [BEND_IDX( 30)] = 0x3D23,
9297 [BEND_IDX( 25)] = 0x3D23,
9298 [BEND_IDX( 20)] = 0x3E23,
9299 [BEND_IDX( 15)] = 0x3E23,
9300 [BEND_IDX( 10)] = 0x3F23,
9301 [BEND_IDX( 5)] = 0x3F23,
9302 [BEND_IDX( 0)] = 0x0025,
9303 [BEND_IDX( -5)] = 0x0025,
9304 [BEND_IDX(-10)] = 0x0125,
9305 [BEND_IDX(-15)] = 0x0125,
9306 [BEND_IDX(-20)] = 0x0225,
9307 [BEND_IDX(-25)] = 0x0225,
9308 [BEND_IDX(-30)] = 0x0325,
9309 [BEND_IDX(-35)] = 0x0325,
9310 [BEND_IDX(-40)] = 0x0425,
9311 [BEND_IDX(-45)] = 0x0425,
9312 [BEND_IDX(-50)] = 0x0525,
9313};
9314
9315/*
9316 * Bend CLKOUT_DP
9317 * steps -50 to 50 inclusive, in steps of 5
9318 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
9319 * change in clock period = -(steps / 10) * 5.787 ps
9320 */
9321static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
9322{
9323 uint32_t tmp;
9324 int idx = BEND_IDX(steps);
9325
9326 if (WARN_ON(steps % 5 != 0))
9327 return;
9328
9329 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
9330 return;
9331
9332 mutex_lock(&dev_priv->sb_lock);
9333
9334 if (steps % 10 != 0)
9335 tmp = 0xAAAAAAAB;
9336 else
9337 tmp = 0x00000000;
9338 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
9339
9340 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
9341 tmp &= 0xffff0000;
9342 tmp |= sscdivintphase[idx];
9343 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
9344
9345 mutex_unlock(&dev_priv->sb_lock);
9346}
9347
9348#undef BEND_IDX
9349
c39055b0 9350static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
bf8fa3d3 9351{
bf8fa3d3
PZ
9352 struct intel_encoder *encoder;
9353 bool has_vga = false;
9354
c39055b0 9355 for_each_intel_encoder(&dev_priv->drm, encoder) {
bf8fa3d3
PZ
9356 switch (encoder->type) {
9357 case INTEL_OUTPUT_ANALOG:
9358 has_vga = true;
9359 break;
6847d71b
PZ
9360 default:
9361 break;
bf8fa3d3
PZ
9362 }
9363 }
9364
f7be2c21 9365 if (has_vga) {
c39055b0
ACO
9366 lpt_bend_clkout_dp(dev_priv, 0);
9367 lpt_enable_clkout_dp(dev_priv, true, true);
f7be2c21 9368 } else {
c39055b0 9369 lpt_disable_clkout_dp(dev_priv);
f7be2c21 9370 }
bf8fa3d3
PZ
9371}
9372
dde86e2d
PZ
9373/*
9374 * Initialize reference clocks when the driver loads
9375 */
c39055b0 9376void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
dde86e2d 9377{
6e266956 9378 if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
c39055b0 9379 ironlake_init_pch_refclk(dev_priv);
6e266956 9380 else if (HAS_PCH_LPT(dev_priv))
c39055b0 9381 lpt_init_pch_refclk(dev_priv);
dde86e2d
PZ
9382}
9383
6ff93609 9384static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 9385{
fac5e23e 9386 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
79e53945
JB
9387 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9388 int pipe = intel_crtc->pipe;
c8203565
PZ
9389 uint32_t val;
9390
78114071 9391 val = 0;
c8203565 9392
6e3c9717 9393 switch (intel_crtc->config->pipe_bpp) {
c8203565 9394 case 18:
dfd07d72 9395 val |= PIPECONF_6BPC;
c8203565
PZ
9396 break;
9397 case 24:
dfd07d72 9398 val |= PIPECONF_8BPC;
c8203565
PZ
9399 break;
9400 case 30:
dfd07d72 9401 val |= PIPECONF_10BPC;
c8203565
PZ
9402 break;
9403 case 36:
dfd07d72 9404 val |= PIPECONF_12BPC;
c8203565
PZ
9405 break;
9406 default:
cc769b62
PZ
9407 /* Case prevented by intel_choose_pipe_bpp_dither. */
9408 BUG();
c8203565
PZ
9409 }
9410
6e3c9717 9411 if (intel_crtc->config->dither)
c8203565
PZ
9412 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
9413
6e3c9717 9414 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
9415 val |= PIPECONF_INTERLACED_ILK;
9416 else
9417 val |= PIPECONF_PROGRESSIVE;
9418
6e3c9717 9419 if (intel_crtc->config->limited_color_range)
3685a8f3 9420 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 9421
c8203565
PZ
9422 I915_WRITE(PIPECONF(pipe), val);
9423 POSTING_READ(PIPECONF(pipe));
9424}
9425
6ff93609 9426static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 9427{
fac5e23e 9428 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
ee2b0b38 9429 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 9430 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
391bf048 9431 u32 val = 0;
ee2b0b38 9432
391bf048 9433 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
ee2b0b38
PZ
9434 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
9435
6e3c9717 9436 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
9437 val |= PIPECONF_INTERLACED_ILK;
9438 else
9439 val |= PIPECONF_PROGRESSIVE;
9440
702e7a56
PZ
9441 I915_WRITE(PIPECONF(cpu_transcoder), val);
9442 POSTING_READ(PIPECONF(cpu_transcoder));
391bf048
JN
9443}
9444
391bf048
JN
9445static void haswell_set_pipemisc(struct drm_crtc *crtc)
9446{
fac5e23e 9447 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
391bf048 9448 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756f85cf 9449
391bf048
JN
9450 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
9451 u32 val = 0;
756f85cf 9452
6e3c9717 9453 switch (intel_crtc->config->pipe_bpp) {
756f85cf
PZ
9454 case 18:
9455 val |= PIPEMISC_DITHER_6_BPC;
9456 break;
9457 case 24:
9458 val |= PIPEMISC_DITHER_8_BPC;
9459 break;
9460 case 30:
9461 val |= PIPEMISC_DITHER_10_BPC;
9462 break;
9463 case 36:
9464 val |= PIPEMISC_DITHER_12_BPC;
9465 break;
9466 default:
9467 /* Case prevented by pipe_config_set_bpp. */
9468 BUG();
9469 }
9470
6e3c9717 9471 if (intel_crtc->config->dither)
756f85cf
PZ
9472 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
9473
391bf048 9474 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
756f85cf 9475 }
ee2b0b38
PZ
9476}
9477
d4b1931c
PZ
9478int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
9479{
9480 /*
9481 * Account for spread spectrum to avoid
9482 * oversubscribing the link. Max center spread
9483 * is 2.5%; use 5% for safety's sake.
9484 */
9485 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 9486 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
9487}
9488
7429e9d4 9489static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 9490{
7429e9d4 9491 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
9492}
9493
b75ca6f6
ACO
9494static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
9495 struct intel_crtc_state *crtc_state,
9e2c8475 9496 struct dpll *reduced_clock)
79e53945 9497{
de13a2e3 9498 struct drm_crtc *crtc = &intel_crtc->base;
79e53945 9499 struct drm_device *dev = crtc->dev;
fac5e23e 9500 struct drm_i915_private *dev_priv = to_i915(dev);
b75ca6f6 9501 u32 dpll, fp, fp2;
3d6e9ee0 9502 int factor;
79e53945 9503
c1858123 9504 /* Enable autotuning of the PLL clock (if permissible) */
8febb297 9505 factor = 21;
3d6e9ee0 9506 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8febb297 9507 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 9508 dev_priv->vbt.lvds_ssc_freq == 100000) ||
6e266956 9509 (HAS_PCH_IBX(dev_priv) && intel_is_dual_link_lvds(dev)))
8febb297 9510 factor = 25;
190f68c5 9511 } else if (crtc_state->sdvo_tv_clock)
8febb297 9512 factor = 20;
c1858123 9513
b75ca6f6
ACO
9514 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
9515
190f68c5 9516 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
b75ca6f6
ACO
9517 fp |= FP_CB_TUNE;
9518
9519 if (reduced_clock) {
9520 fp2 = i9xx_dpll_compute_fp(reduced_clock);
2c07245f 9521
b75ca6f6
ACO
9522 if (reduced_clock->m < factor * reduced_clock->n)
9523 fp2 |= FP_CB_TUNE;
9524 } else {
9525 fp2 = fp;
9526 }
9a7c7890 9527
5eddb70b 9528 dpll = 0;
2c07245f 9529
3d6e9ee0 9530 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
a07d6787
EA
9531 dpll |= DPLLB_MODE_LVDS;
9532 else
9533 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 9534
190f68c5 9535 dpll |= (crtc_state->pixel_multiplier - 1)
ef1b460d 9536 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f 9537
3d6e9ee0
VS
9538 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
9539 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
4a33e48d 9540 dpll |= DPLL_SDVO_HIGH_SPEED;
3d6e9ee0 9541
37a5650b 9542 if (intel_crtc_has_dp_encoder(crtc_state))
4a33e48d 9543 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 9544
7d7f8633
VS
9545 /*
9546 * The high speed IO clock is only really required for
9547 * SDVO/HDMI/DP, but we also enable it for CRT to make it
9548 * possible to share the DPLL between CRT and HDMI. Enabling
9549 * the clock needlessly does no real harm, except use up a
9550 * bit of power potentially.
9551 *
9552 * We'll limit this to IVB with 3 pipes, since it has only two
9553 * DPLLs and so DPLL sharing is the only way to get three pipes
9554 * driving PCH ports at the same time. On SNB we could do this,
9555 * and potentially avoid enabling the second DPLL, but it's not
9556 * clear if it''s a win or loss power wise. No point in doing
9557 * this on ILK at all since it has a fixed DPLL<->pipe mapping.
9558 */
9559 if (INTEL_INFO(dev_priv)->num_pipes == 3 &&
9560 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
9561 dpll |= DPLL_SDVO_HIGH_SPEED;
9562
a07d6787 9563 /* compute bitmask from p1 value */
190f68c5 9564 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 9565 /* also FPA1 */
190f68c5 9566 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 9567
190f68c5 9568 switch (crtc_state->dpll.p2) {
a07d6787
EA
9569 case 5:
9570 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
9571 break;
9572 case 7:
9573 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
9574 break;
9575 case 10:
9576 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
9577 break;
9578 case 14:
9579 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
9580 break;
79e53945
JB
9581 }
9582
3d6e9ee0
VS
9583 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
9584 intel_panel_use_ssc(dev_priv))
43565a06 9585 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
9586 else
9587 dpll |= PLL_REF_INPUT_DREFCLK;
9588
b75ca6f6
ACO
9589 dpll |= DPLL_VCO_ENABLE;
9590
9591 crtc_state->dpll_hw_state.dpll = dpll;
9592 crtc_state->dpll_hw_state.fp0 = fp;
9593 crtc_state->dpll_hw_state.fp1 = fp2;
de13a2e3
PZ
9594}
9595
190f68c5
ACO
9596static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
9597 struct intel_crtc_state *crtc_state)
de13a2e3 9598{
997c030c 9599 struct drm_device *dev = crtc->base.dev;
fac5e23e 9600 struct drm_i915_private *dev_priv = to_i915(dev);
9e2c8475 9601 struct dpll reduced_clock;
7ed9f894 9602 bool has_reduced_clock = false;
e2b78267 9603 struct intel_shared_dpll *pll;
1b6f4958 9604 const struct intel_limit *limit;
997c030c 9605 int refclk = 120000;
de13a2e3 9606
dd3cd74a
ACO
9607 memset(&crtc_state->dpll_hw_state, 0,
9608 sizeof(crtc_state->dpll_hw_state));
9609
ded220e2
ACO
9610 crtc->lowfreq_avail = false;
9611
9612 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
9613 if (!crtc_state->has_pch_encoder)
9614 return 0;
79e53945 9615
2d84d2b3 9616 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
997c030c
ACO
9617 if (intel_panel_use_ssc(dev_priv)) {
9618 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
9619 dev_priv->vbt.lvds_ssc_freq);
9620 refclk = dev_priv->vbt.lvds_ssc_freq;
9621 }
9622
9623 if (intel_is_dual_link_lvds(dev)) {
9624 if (refclk == 100000)
9625 limit = &intel_limits_ironlake_dual_lvds_100m;
9626 else
9627 limit = &intel_limits_ironlake_dual_lvds;
9628 } else {
9629 if (refclk == 100000)
9630 limit = &intel_limits_ironlake_single_lvds_100m;
9631 else
9632 limit = &intel_limits_ironlake_single_lvds;
9633 }
9634 } else {
9635 limit = &intel_limits_ironlake_dac;
9636 }
9637
364ee29d 9638 if (!crtc_state->clock_set &&
997c030c
ACO
9639 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9640 refclk, NULL, &crtc_state->dpll)) {
364ee29d
ACO
9641 DRM_ERROR("Couldn't find PLL settings for mode!\n");
9642 return -EINVAL;
f47709a9 9643 }
79e53945 9644
b75ca6f6
ACO
9645 ironlake_compute_dpll(crtc, crtc_state,
9646 has_reduced_clock ? &reduced_clock : NULL);
66e985c0 9647
ded220e2
ACO
9648 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
9649 if (pll == NULL) {
9650 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
9651 pipe_name(crtc->pipe));
9652 return -EINVAL;
3fb37703 9653 }
79e53945 9654
2d84d2b3 9655 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ded220e2 9656 has_reduced_clock)
c7653199 9657 crtc->lowfreq_avail = true;
e2b78267 9658
c8f7a0db 9659 return 0;
79e53945
JB
9660}
9661
eb14cb74
VS
9662static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
9663 struct intel_link_m_n *m_n)
9664{
9665 struct drm_device *dev = crtc->base.dev;
fac5e23e 9666 struct drm_i915_private *dev_priv = to_i915(dev);
eb14cb74
VS
9667 enum pipe pipe = crtc->pipe;
9668
9669 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
9670 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
9671 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
9672 & ~TU_SIZE_MASK;
9673 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
9674 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
9675 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9676}
9677
9678static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
9679 enum transcoder transcoder,
b95af8be
VK
9680 struct intel_link_m_n *m_n,
9681 struct intel_link_m_n *m2_n2)
72419203 9682{
6315b5d3 9683 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
eb14cb74 9684 enum pipe pipe = crtc->pipe;
72419203 9685
6315b5d3 9686 if (INTEL_GEN(dev_priv) >= 5) {
eb14cb74
VS
9687 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
9688 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
9689 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
9690 & ~TU_SIZE_MASK;
9691 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
9692 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
9693 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
b95af8be
VK
9694 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
9695 * gen < 8) and if DRRS is supported (to make sure the
9696 * registers are not unnecessarily read).
9697 */
6315b5d3 9698 if (m2_n2 && INTEL_GEN(dev_priv) < 8 &&
6e3c9717 9699 crtc->config->has_drrs) {
b95af8be
VK
9700 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
9701 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
9702 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
9703 & ~TU_SIZE_MASK;
9704 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
9705 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
9706 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9707 }
eb14cb74
VS
9708 } else {
9709 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
9710 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
9711 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
9712 & ~TU_SIZE_MASK;
9713 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
9714 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
9715 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9716 }
9717}
9718
9719void intel_dp_get_m_n(struct intel_crtc *crtc,
5cec258b 9720 struct intel_crtc_state *pipe_config)
eb14cb74 9721{
681a8504 9722 if (pipe_config->has_pch_encoder)
eb14cb74
VS
9723 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
9724 else
9725 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be
VK
9726 &pipe_config->dp_m_n,
9727 &pipe_config->dp_m2_n2);
eb14cb74 9728}
72419203 9729
eb14cb74 9730static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5cec258b 9731 struct intel_crtc_state *pipe_config)
eb14cb74
VS
9732{
9733 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be 9734 &pipe_config->fdi_m_n, NULL);
72419203
DV
9735}
9736
bd2e244f 9737static void skylake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9738 struct intel_crtc_state *pipe_config)
bd2e244f
JB
9739{
9740 struct drm_device *dev = crtc->base.dev;
fac5e23e 9741 struct drm_i915_private *dev_priv = to_i915(dev);
a1b2278e
CK
9742 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9743 uint32_t ps_ctrl = 0;
9744 int id = -1;
9745 int i;
bd2e244f 9746
a1b2278e
CK
9747 /* find scaler attached to this pipe */
9748 for (i = 0; i < crtc->num_scalers; i++) {
9749 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9750 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9751 id = i;
9752 pipe_config->pch_pfit.enabled = true;
9753 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9754 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9755 break;
9756 }
9757 }
bd2e244f 9758
a1b2278e
CK
9759 scaler_state->scaler_id = id;
9760 if (id >= 0) {
9761 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9762 } else {
9763 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
bd2e244f
JB
9764 }
9765}
9766
5724dbd1
DL
9767static void
9768skylake_get_initial_plane_config(struct intel_crtc *crtc,
9769 struct intel_initial_plane_config *plane_config)
bc8d7dff
DL
9770{
9771 struct drm_device *dev = crtc->base.dev;
fac5e23e 9772 struct drm_i915_private *dev_priv = to_i915(dev);
40f46283 9773 u32 val, base, offset, stride_mult, tiling;
bc8d7dff
DL
9774 int pipe = crtc->pipe;
9775 int fourcc, pixel_format;
6761dd31 9776 unsigned int aligned_height;
bc8d7dff 9777 struct drm_framebuffer *fb;
1b842c89 9778 struct intel_framebuffer *intel_fb;
bc8d7dff 9779
d9806c9f 9780 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9781 if (!intel_fb) {
bc8d7dff
DL
9782 DRM_DEBUG_KMS("failed to alloc fb\n");
9783 return;
9784 }
9785
1b842c89
DL
9786 fb = &intel_fb->base;
9787
bc8d7dff 9788 val = I915_READ(PLANE_CTL(pipe, 0));
42a7b088
DL
9789 if (!(val & PLANE_CTL_ENABLE))
9790 goto error;
9791
bc8d7dff
DL
9792 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9793 fourcc = skl_format_to_fourcc(pixel_format,
9794 val & PLANE_CTL_ORDER_RGBX,
9795 val & PLANE_CTL_ALPHA_MASK);
9796 fb->pixel_format = fourcc;
9797 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9798
40f46283
DL
9799 tiling = val & PLANE_CTL_TILED_MASK;
9800 switch (tiling) {
9801 case PLANE_CTL_TILED_LINEAR:
bae781b2 9802 fb->modifier = DRM_FORMAT_MOD_NONE;
40f46283
DL
9803 break;
9804 case PLANE_CTL_TILED_X:
9805 plane_config->tiling = I915_TILING_X;
bae781b2 9806 fb->modifier = I915_FORMAT_MOD_X_TILED;
40f46283
DL
9807 break;
9808 case PLANE_CTL_TILED_Y:
bae781b2 9809 fb->modifier = I915_FORMAT_MOD_Y_TILED;
40f46283
DL
9810 break;
9811 case PLANE_CTL_TILED_YF:
bae781b2 9812 fb->modifier = I915_FORMAT_MOD_Yf_TILED;
40f46283
DL
9813 break;
9814 default:
9815 MISSING_CASE(tiling);
9816 goto error;
9817 }
9818
bc8d7dff
DL
9819 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9820 plane_config->base = base;
9821
9822 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9823
9824 val = I915_READ(PLANE_SIZE(pipe, 0));
9825 fb->height = ((val >> 16) & 0xfff) + 1;
9826 fb->width = ((val >> 0) & 0x1fff) + 1;
9827
9828 val = I915_READ(PLANE_STRIDE(pipe, 0));
bae781b2 9829 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier,
40f46283 9830 fb->pixel_format);
bc8d7dff
DL
9831 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9832
9833 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb 9834 fb->pixel_format,
bae781b2 9835 fb->modifier);
bc8d7dff 9836
f37b5c2b 9837 plane_config->size = fb->pitches[0] * aligned_height;
bc8d7dff
DL
9838
9839 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9840 pipe_name(pipe), fb->width, fb->height,
9841 fb->bits_per_pixel, base, fb->pitches[0],
9842 plane_config->size);
9843
2d14030b 9844 plane_config->fb = intel_fb;
bc8d7dff
DL
9845 return;
9846
9847error:
d1a3a036 9848 kfree(intel_fb);
bc8d7dff
DL
9849}
9850
2fa2fe9a 9851static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9852 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
9853{
9854 struct drm_device *dev = crtc->base.dev;
fac5e23e 9855 struct drm_i915_private *dev_priv = to_i915(dev);
2fa2fe9a
DV
9856 uint32_t tmp;
9857
9858 tmp = I915_READ(PF_CTL(crtc->pipe));
9859
9860 if (tmp & PF_ENABLE) {
fd4daa9c 9861 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
DV
9862 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9863 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
DV
9864
9865 /* We currently do not free assignements of panel fitters on
9866 * ivb/hsw (since we don't use the higher upscaling modes which
9867 * differentiates them) so just WARN about this case for now. */
5db94019 9868 if (IS_GEN7(dev_priv)) {
cb8b2a30
DV
9869 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9870 PF_PIPE_SEL_IVB(crtc->pipe));
9871 }
2fa2fe9a 9872 }
79e53945
JB
9873}
9874
5724dbd1
DL
9875static void
9876ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9877 struct intel_initial_plane_config *plane_config)
4c6baa59
JB
9878{
9879 struct drm_device *dev = crtc->base.dev;
fac5e23e 9880 struct drm_i915_private *dev_priv = to_i915(dev);
4c6baa59 9881 u32 val, base, offset;
aeee5a49 9882 int pipe = crtc->pipe;
4c6baa59 9883 int fourcc, pixel_format;
6761dd31 9884 unsigned int aligned_height;
b113d5ee 9885 struct drm_framebuffer *fb;
1b842c89 9886 struct intel_framebuffer *intel_fb;
4c6baa59 9887
42a7b088
DL
9888 val = I915_READ(DSPCNTR(pipe));
9889 if (!(val & DISPLAY_PLANE_ENABLE))
9890 return;
9891
d9806c9f 9892 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9893 if (!intel_fb) {
4c6baa59
JB
9894 DRM_DEBUG_KMS("failed to alloc fb\n");
9895 return;
9896 }
9897
1b842c89
DL
9898 fb = &intel_fb->base;
9899
6315b5d3 9900 if (INTEL_GEN(dev_priv) >= 4) {
18c5247e 9901 if (val & DISPPLANE_TILED) {
49af449b 9902 plane_config->tiling = I915_TILING_X;
bae781b2 9903 fb->modifier = I915_FORMAT_MOD_X_TILED;
18c5247e
DV
9904 }
9905 }
4c6baa59
JB
9906
9907 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 9908 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
9909 fb->pixel_format = fourcc;
9910 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
4c6baa59 9911
aeee5a49 9912 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
8652744b 9913 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
aeee5a49 9914 offset = I915_READ(DSPOFFSET(pipe));
4c6baa59 9915 } else {
49af449b 9916 if (plane_config->tiling)
aeee5a49 9917 offset = I915_READ(DSPTILEOFF(pipe));
4c6baa59 9918 else
aeee5a49 9919 offset = I915_READ(DSPLINOFF(pipe));
4c6baa59
JB
9920 }
9921 plane_config->base = base;
9922
9923 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
9924 fb->width = ((val >> 16) & 0xfff) + 1;
9925 fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
9926
9927 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 9928 fb->pitches[0] = val & 0xffffffc0;
4c6baa59 9929
b113d5ee 9930 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb 9931 fb->pixel_format,
bae781b2 9932 fb->modifier);
4c6baa59 9933
f37b5c2b 9934 plane_config->size = fb->pitches[0] * aligned_height;
4c6baa59 9935
2844a921
DL
9936 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9937 pipe_name(pipe), fb->width, fb->height,
9938 fb->bits_per_pixel, base, fb->pitches[0],
9939 plane_config->size);
b113d5ee 9940
2d14030b 9941 plane_config->fb = intel_fb;
4c6baa59
JB
9942}
9943
0e8ffe1b 9944static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9945 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
9946{
9947 struct drm_device *dev = crtc->base.dev;
fac5e23e 9948 struct drm_i915_private *dev_priv = to_i915(dev);
1729050e 9949 enum intel_display_power_domain power_domain;
0e8ffe1b 9950 uint32_t tmp;
1729050e 9951 bool ret;
0e8ffe1b 9952
1729050e
ID
9953 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9954 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
930e8c9e
PZ
9955 return false;
9956
e143a21c 9957 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 9958 pipe_config->shared_dpll = NULL;
eccb140b 9959
1729050e 9960 ret = false;
0e8ffe1b
DV
9961 tmp = I915_READ(PIPECONF(crtc->pipe));
9962 if (!(tmp & PIPECONF_ENABLE))
1729050e 9963 goto out;
0e8ffe1b 9964
42571aef
VS
9965 switch (tmp & PIPECONF_BPC_MASK) {
9966 case PIPECONF_6BPC:
9967 pipe_config->pipe_bpp = 18;
9968 break;
9969 case PIPECONF_8BPC:
9970 pipe_config->pipe_bpp = 24;
9971 break;
9972 case PIPECONF_10BPC:
9973 pipe_config->pipe_bpp = 30;
9974 break;
9975 case PIPECONF_12BPC:
9976 pipe_config->pipe_bpp = 36;
9977 break;
9978 default:
9979 break;
9980 }
9981
b5a9fa09
DV
9982 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9983 pipe_config->limited_color_range = true;
9984
ab9412ba 9985 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0 9986 struct intel_shared_dpll *pll;
8106ddbd 9987 enum intel_dpll_id pll_id;
66e985c0 9988
88adfff1
DV
9989 pipe_config->has_pch_encoder = true;
9990
627eb5a3
DV
9991 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9992 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9993 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
9994
9995 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 9996
2d1fe073 9997 if (HAS_PCH_IBX(dev_priv)) {
d9a7bc67
ID
9998 /*
9999 * The pipe->pch transcoder and pch transcoder->pll
10000 * mapping is fixed.
10001 */
8106ddbd 10002 pll_id = (enum intel_dpll_id) crtc->pipe;
c0d43d62
DV
10003 } else {
10004 tmp = I915_READ(PCH_DPLL_SEL);
10005 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
8106ddbd 10006 pll_id = DPLL_ID_PCH_PLL_B;
c0d43d62 10007 else
8106ddbd 10008 pll_id= DPLL_ID_PCH_PLL_A;
c0d43d62 10009 }
66e985c0 10010
8106ddbd
ACO
10011 pipe_config->shared_dpll =
10012 intel_get_shared_dpll_by_id(dev_priv, pll_id);
10013 pll = pipe_config->shared_dpll;
66e985c0 10014
2edd6443
ACO
10015 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
10016 &pipe_config->dpll_hw_state));
c93f54cf
DV
10017
10018 tmp = pipe_config->dpll_hw_state.dpll;
10019 pipe_config->pixel_multiplier =
10020 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
10021 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
10022
10023 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
DV
10024 } else {
10025 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
10026 }
10027
1bd1bd80 10028 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 10029 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 10030
2fa2fe9a
DV
10031 ironlake_get_pfit_config(crtc, pipe_config);
10032
1729050e
ID
10033 ret = true;
10034
10035out:
10036 intel_display_power_put(dev_priv, power_domain);
10037
10038 return ret;
0e8ffe1b
DV
10039}
10040
be256dc7
PZ
10041static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
10042{
91c8a326 10043 struct drm_device *dev = &dev_priv->drm;
be256dc7 10044 struct intel_crtc *crtc;
be256dc7 10045
d3fcc808 10046 for_each_intel_crtc(dev, crtc)
e2c719b7 10047 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
10048 pipe_name(crtc->pipe));
10049
e2c719b7
RC
10050 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
10051 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
01403de3
VS
10052 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
10053 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
44cb734c 10054 I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
e2c719b7 10055 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
be256dc7 10056 "CPU PWM1 enabled\n");
772c2a51 10057 if (IS_HASWELL(dev_priv))
e2c719b7 10058 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
c5107b87 10059 "CPU PWM2 enabled\n");
e2c719b7 10060 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
be256dc7 10061 "PCH PWM1 enabled\n");
e2c719b7 10062 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
be256dc7 10063 "Utility pin enabled\n");
e2c719b7 10064 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
be256dc7 10065
9926ada1
PZ
10066 /*
10067 * In theory we can still leave IRQs enabled, as long as only the HPD
10068 * interrupts remain enabled. We used to check for that, but since it's
10069 * gen-specific and since we only disable LCPLL after we fully disable
10070 * the interrupts, the check below should be enough.
10071 */
e2c719b7 10072 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
be256dc7
PZ
10073}
10074
9ccd5aeb
PZ
10075static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
10076{
772c2a51 10077 if (IS_HASWELL(dev_priv))
9ccd5aeb
PZ
10078 return I915_READ(D_COMP_HSW);
10079 else
10080 return I915_READ(D_COMP_BDW);
10081}
10082
3c4c9b81
PZ
10083static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
10084{
772c2a51 10085 if (IS_HASWELL(dev_priv)) {
3c4c9b81
PZ
10086 mutex_lock(&dev_priv->rps.hw_lock);
10087 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
10088 val))
79cf219a 10089 DRM_DEBUG_KMS("Failed to write to D_COMP\n");
3c4c9b81
PZ
10090 mutex_unlock(&dev_priv->rps.hw_lock);
10091 } else {
9ccd5aeb
PZ
10092 I915_WRITE(D_COMP_BDW, val);
10093 POSTING_READ(D_COMP_BDW);
3c4c9b81 10094 }
be256dc7
PZ
10095}
10096
10097/*
10098 * This function implements pieces of two sequences from BSpec:
10099 * - Sequence for display software to disable LCPLL
10100 * - Sequence for display software to allow package C8+
10101 * The steps implemented here are just the steps that actually touch the LCPLL
10102 * register. Callers should take care of disabling all the display engine
10103 * functions, doing the mode unset, fixing interrupts, etc.
10104 */
6ff58d53
PZ
10105static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
10106 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
10107{
10108 uint32_t val;
10109
10110 assert_can_disable_lcpll(dev_priv);
10111
10112 val = I915_READ(LCPLL_CTL);
10113
10114 if (switch_to_fclk) {
10115 val |= LCPLL_CD_SOURCE_FCLK;
10116 I915_WRITE(LCPLL_CTL, val);
10117
f53dd63f
ID
10118 if (wait_for_us(I915_READ(LCPLL_CTL) &
10119 LCPLL_CD_SOURCE_FCLK_DONE, 1))
be256dc7
PZ
10120 DRM_ERROR("Switching to FCLK failed\n");
10121
10122 val = I915_READ(LCPLL_CTL);
10123 }
10124
10125 val |= LCPLL_PLL_DISABLE;
10126 I915_WRITE(LCPLL_CTL, val);
10127 POSTING_READ(LCPLL_CTL);
10128
24d8441d 10129 if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
be256dc7
PZ
10130 DRM_ERROR("LCPLL still locked\n");
10131
9ccd5aeb 10132 val = hsw_read_dcomp(dev_priv);
be256dc7 10133 val |= D_COMP_COMP_DISABLE;
3c4c9b81 10134 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
10135 ndelay(100);
10136
9ccd5aeb
PZ
10137 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
10138 1))
be256dc7
PZ
10139 DRM_ERROR("D_COMP RCOMP still in progress\n");
10140
10141 if (allow_power_down) {
10142 val = I915_READ(LCPLL_CTL);
10143 val |= LCPLL_POWER_DOWN_ALLOW;
10144 I915_WRITE(LCPLL_CTL, val);
10145 POSTING_READ(LCPLL_CTL);
10146 }
10147}
10148
10149/*
10150 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
10151 * source.
10152 */
6ff58d53 10153static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
10154{
10155 uint32_t val;
10156
10157 val = I915_READ(LCPLL_CTL);
10158
10159 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
10160 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
10161 return;
10162
a8a8bd54
PZ
10163 /*
10164 * Make sure we're not on PC8 state before disabling PC8, otherwise
10165 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
a8a8bd54 10166 */
59bad947 10167 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
215733fa 10168
be256dc7
PZ
10169 if (val & LCPLL_POWER_DOWN_ALLOW) {
10170 val &= ~LCPLL_POWER_DOWN_ALLOW;
10171 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 10172 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
10173 }
10174
9ccd5aeb 10175 val = hsw_read_dcomp(dev_priv);
be256dc7
PZ
10176 val |= D_COMP_COMP_FORCE;
10177 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 10178 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
10179
10180 val = I915_READ(LCPLL_CTL);
10181 val &= ~LCPLL_PLL_DISABLE;
10182 I915_WRITE(LCPLL_CTL, val);
10183
93220c08
CW
10184 if (intel_wait_for_register(dev_priv,
10185 LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
10186 5))
be256dc7
PZ
10187 DRM_ERROR("LCPLL not locked yet\n");
10188
10189 if (val & LCPLL_CD_SOURCE_FCLK) {
10190 val = I915_READ(LCPLL_CTL);
10191 val &= ~LCPLL_CD_SOURCE_FCLK;
10192 I915_WRITE(LCPLL_CTL, val);
10193
f53dd63f
ID
10194 if (wait_for_us((I915_READ(LCPLL_CTL) &
10195 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
be256dc7
PZ
10196 DRM_ERROR("Switching back to LCPLL failed\n");
10197 }
215733fa 10198
59bad947 10199 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4c75b940 10200 intel_update_cdclk(dev_priv);
be256dc7
PZ
10201}
10202
765dab67
PZ
10203/*
10204 * Package states C8 and deeper are really deep PC states that can only be
10205 * reached when all the devices on the system allow it, so even if the graphics
10206 * device allows PC8+, it doesn't mean the system will actually get to these
10207 * states. Our driver only allows PC8+ when going into runtime PM.
10208 *
10209 * The requirements for PC8+ are that all the outputs are disabled, the power
10210 * well is disabled and most interrupts are disabled, and these are also
10211 * requirements for runtime PM. When these conditions are met, we manually do
10212 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
10213 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
10214 * hang the machine.
10215 *
10216 * When we really reach PC8 or deeper states (not just when we allow it) we lose
10217 * the state of some registers, so when we come back from PC8+ we need to
10218 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
10219 * need to take care of the registers kept by RC6. Notice that this happens even
10220 * if we don't put the device in PCI D3 state (which is what currently happens
10221 * because of the runtime PM support).
10222 *
10223 * For more, read "Display Sequences for Package C8" on the hardware
10224 * documentation.
10225 */
a14cb6fc 10226void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 10227{
c67a470b
PZ
10228 uint32_t val;
10229
c67a470b
PZ
10230 DRM_DEBUG_KMS("Enabling package C8+\n");
10231
4f8036a2 10232 if (HAS_PCH_LPT_LP(dev_priv)) {
c67a470b
PZ
10233 val = I915_READ(SOUTH_DSPCLK_GATE_D);
10234 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
10235 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
10236 }
10237
c39055b0 10238 lpt_disable_clkout_dp(dev_priv);
c67a470b
PZ
10239 hsw_disable_lcpll(dev_priv, true, true);
10240}
10241
a14cb6fc 10242void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b 10243{
c67a470b
PZ
10244 uint32_t val;
10245
c67a470b
PZ
10246 DRM_DEBUG_KMS("Disabling package C8+\n");
10247
10248 hsw_restore_lcpll(dev_priv);
c39055b0 10249 lpt_init_pch_refclk(dev_priv);
c67a470b 10250
4f8036a2 10251 if (HAS_PCH_LPT_LP(dev_priv)) {
c67a470b
PZ
10252 val = I915_READ(SOUTH_DSPCLK_GATE_D);
10253 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
10254 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
10255 }
c67a470b
PZ
10256}
10257
324513c0 10258static void bxt_modeset_commit_cdclk(struct drm_atomic_state *old_state)
f8437dd1 10259{
a821fc46 10260 struct drm_device *dev = old_state->dev;
1a617b77
ML
10261 struct intel_atomic_state *old_intel_state =
10262 to_intel_atomic_state(old_state);
10263 unsigned int req_cdclk = old_intel_state->dev_cdclk;
f8437dd1 10264
324513c0 10265 bxt_set_cdclk(to_i915(dev), req_cdclk);
f8437dd1
VK
10266}
10267
b30ce9e0
DP
10268static int bdw_adjust_min_pipe_pixel_rate(struct intel_crtc_state *crtc_state,
10269 int pixel_rate)
10270{
9c754024
DP
10271 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
10272
b30ce9e0 10273 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
9c754024 10274 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
b30ce9e0
DP
10275 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
10276
10277 /* BSpec says "Do not use DisplayPort with CDCLK less than
10278 * 432 MHz, audio enabled, port width x4, and link rate
10279 * HBR2 (5.4 GHz), or else there may be audio corruption or
10280 * screen corruption."
10281 */
10282 if (intel_crtc_has_dp_encoder(crtc_state) &&
10283 crtc_state->has_audio &&
10284 crtc_state->port_clock >= 540000 &&
10285 crtc_state->lane_count == 4)
10286 pixel_rate = max(432000, pixel_rate);
10287
10288 return pixel_rate;
10289}
10290
b432e5cf 10291/* compute the max rate for new configuration */
27c329ed 10292static int ilk_max_pixel_rate(struct drm_atomic_state *state)
b432e5cf 10293{
565602d7 10294 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 10295 struct drm_i915_private *dev_priv = to_i915(state->dev);
565602d7
ML
10296 struct drm_crtc *crtc;
10297 struct drm_crtc_state *cstate;
27c329ed 10298 struct intel_crtc_state *crtc_state;
565602d7
ML
10299 unsigned max_pixel_rate = 0, i;
10300 enum pipe pipe;
b432e5cf 10301
565602d7
ML
10302 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
10303 sizeof(intel_state->min_pixclk));
27c329ed 10304
565602d7
ML
10305 for_each_crtc_in_state(state, crtc, cstate, i) {
10306 int pixel_rate;
27c329ed 10307
565602d7
ML
10308 crtc_state = to_intel_crtc_state(cstate);
10309 if (!crtc_state->base.enable) {
10310 intel_state->min_pixclk[i] = 0;
b432e5cf 10311 continue;
565602d7 10312 }
b432e5cf 10313
27c329ed 10314 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
b432e5cf 10315
9c754024 10316 if (IS_BROADWELL(dev_priv) || IS_GEN9(dev_priv))
b30ce9e0
DP
10317 pixel_rate = bdw_adjust_min_pipe_pixel_rate(crtc_state,
10318 pixel_rate);
b432e5cf 10319
565602d7 10320 intel_state->min_pixclk[i] = pixel_rate;
b432e5cf
VS
10321 }
10322
565602d7
ML
10323 for_each_pipe(dev_priv, pipe)
10324 max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
10325
b432e5cf
VS
10326 return max_pixel_rate;
10327}
10328
10329static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
10330{
fac5e23e 10331 struct drm_i915_private *dev_priv = to_i915(dev);
b432e5cf
VS
10332 uint32_t val, data;
10333 int ret;
10334
10335 if (WARN((I915_READ(LCPLL_CTL) &
10336 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
10337 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
10338 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
10339 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
10340 "trying to change cdclk frequency with cdclk not enabled\n"))
10341 return;
10342
10343 mutex_lock(&dev_priv->rps.hw_lock);
10344 ret = sandybridge_pcode_write(dev_priv,
10345 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
10346 mutex_unlock(&dev_priv->rps.hw_lock);
10347 if (ret) {
10348 DRM_ERROR("failed to inform pcode about cdclk change\n");
10349 return;
10350 }
10351
10352 val = I915_READ(LCPLL_CTL);
10353 val |= LCPLL_CD_SOURCE_FCLK;
10354 I915_WRITE(LCPLL_CTL, val);
10355
5ba00178
TU
10356 if (wait_for_us(I915_READ(LCPLL_CTL) &
10357 LCPLL_CD_SOURCE_FCLK_DONE, 1))
b432e5cf
VS
10358 DRM_ERROR("Switching to FCLK failed\n");
10359
10360 val = I915_READ(LCPLL_CTL);
10361 val &= ~LCPLL_CLK_FREQ_MASK;
10362
10363 switch (cdclk) {
10364 case 450000:
10365 val |= LCPLL_CLK_FREQ_450;
10366 data = 0;
10367 break;
10368 case 540000:
10369 val |= LCPLL_CLK_FREQ_54O_BDW;
10370 data = 1;
10371 break;
10372 case 337500:
10373 val |= LCPLL_CLK_FREQ_337_5_BDW;
10374 data = 2;
10375 break;
10376 case 675000:
10377 val |= LCPLL_CLK_FREQ_675_BDW;
10378 data = 3;
10379 break;
10380 default:
10381 WARN(1, "invalid cdclk frequency\n");
10382 return;
10383 }
10384
10385 I915_WRITE(LCPLL_CTL, val);
10386
10387 val = I915_READ(LCPLL_CTL);
10388 val &= ~LCPLL_CD_SOURCE_FCLK;
10389 I915_WRITE(LCPLL_CTL, val);
10390
5ba00178
TU
10391 if (wait_for_us((I915_READ(LCPLL_CTL) &
10392 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
b432e5cf
VS
10393 DRM_ERROR("Switching back to LCPLL failed\n");
10394
10395 mutex_lock(&dev_priv->rps.hw_lock);
10396 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
10397 mutex_unlock(&dev_priv->rps.hw_lock);
10398
7f1052a8
VS
10399 I915_WRITE(CDCLK_FREQ, DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
10400
4c75b940 10401 intel_update_cdclk(dev_priv);
b432e5cf
VS
10402
10403 WARN(cdclk != dev_priv->cdclk_freq,
10404 "cdclk requested %d kHz but got %d kHz\n",
10405 cdclk, dev_priv->cdclk_freq);
10406}
10407
587c7914
VS
10408static int broadwell_calc_cdclk(int max_pixclk)
10409{
10410 if (max_pixclk > 540000)
10411 return 675000;
10412 else if (max_pixclk > 450000)
10413 return 540000;
10414 else if (max_pixclk > 337500)
10415 return 450000;
10416 else
10417 return 337500;
10418}
10419
27c329ed 10420static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
b432e5cf 10421{
27c329ed 10422 struct drm_i915_private *dev_priv = to_i915(state->dev);
1a617b77 10423 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
27c329ed 10424 int max_pixclk = ilk_max_pixel_rate(state);
b432e5cf
VS
10425 int cdclk;
10426
10427 /*
10428 * FIXME should also account for plane ratio
10429 * once 64bpp pixel formats are supported.
10430 */
587c7914 10431 cdclk = broadwell_calc_cdclk(max_pixclk);
b432e5cf 10432
b432e5cf 10433 if (cdclk > dev_priv->max_cdclk_freq) {
63ba534e
ML
10434 DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
10435 cdclk, dev_priv->max_cdclk_freq);
10436 return -EINVAL;
b432e5cf
VS
10437 }
10438
1a617b77
ML
10439 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
10440 if (!intel_state->active_crtcs)
587c7914 10441 intel_state->dev_cdclk = broadwell_calc_cdclk(0);
b432e5cf
VS
10442
10443 return 0;
10444}
10445
27c329ed 10446static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
b432e5cf 10447{
27c329ed 10448 struct drm_device *dev = old_state->dev;
1a617b77
ML
10449 struct intel_atomic_state *old_intel_state =
10450 to_intel_atomic_state(old_state);
10451 unsigned req_cdclk = old_intel_state->dev_cdclk;
b432e5cf 10452
27c329ed 10453 broadwell_set_cdclk(dev, req_cdclk);
b432e5cf
VS
10454}
10455
c89e39f3
CT
10456static int skl_modeset_calc_cdclk(struct drm_atomic_state *state)
10457{
10458 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
10459 struct drm_i915_private *dev_priv = to_i915(state->dev);
10460 const int max_pixclk = ilk_max_pixel_rate(state);
a8ca4934 10461 int vco = intel_state->cdclk_pll_vco;
c89e39f3
CT
10462 int cdclk;
10463
10464 /*
10465 * FIXME should also account for plane ratio
10466 * once 64bpp pixel formats are supported.
10467 */
a8ca4934 10468 cdclk = skl_calc_cdclk(max_pixclk, vco);
c89e39f3
CT
10469
10470 /*
10471 * FIXME move the cdclk caclulation to
10472 * compute_config() so we can fail gracegully.
10473 */
10474 if (cdclk > dev_priv->max_cdclk_freq) {
10475 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
10476 cdclk, dev_priv->max_cdclk_freq);
10477 cdclk = dev_priv->max_cdclk_freq;
10478 }
10479
10480 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
10481 if (!intel_state->active_crtcs)
a8ca4934 10482 intel_state->dev_cdclk = skl_calc_cdclk(0, vco);
c89e39f3
CT
10483
10484 return 0;
10485}
10486
10487static void skl_modeset_commit_cdclk(struct drm_atomic_state *old_state)
10488{
1cd593e0
VS
10489 struct drm_i915_private *dev_priv = to_i915(old_state->dev);
10490 struct intel_atomic_state *intel_state = to_intel_atomic_state(old_state);
10491 unsigned int req_cdclk = intel_state->dev_cdclk;
10492 unsigned int req_vco = intel_state->cdclk_pll_vco;
c89e39f3 10493
1cd593e0 10494 skl_set_cdclk(dev_priv, req_cdclk, req_vco);
c89e39f3
CT
10495}
10496
190f68c5
ACO
10497static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
10498 struct intel_crtc_state *crtc_state)
09b4ddf9 10499{
d7edc4e5 10500 if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
af3997b5
MK
10501 if (!intel_ddi_pll_select(crtc, crtc_state))
10502 return -EINVAL;
10503 }
716c2e55 10504
c7653199 10505 crtc->lowfreq_avail = false;
644cef34 10506
c8f7a0db 10507 return 0;
79e53945
JB
10508}
10509
3760b59c
S
10510static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
10511 enum port port,
10512 struct intel_crtc_state *pipe_config)
10513{
8106ddbd
ACO
10514 enum intel_dpll_id id;
10515
3760b59c
S
10516 switch (port) {
10517 case PORT_A:
08250c4b 10518 id = DPLL_ID_SKL_DPLL0;
3760b59c
S
10519 break;
10520 case PORT_B:
08250c4b 10521 id = DPLL_ID_SKL_DPLL1;
3760b59c
S
10522 break;
10523 case PORT_C:
08250c4b 10524 id = DPLL_ID_SKL_DPLL2;
3760b59c
S
10525 break;
10526 default:
10527 DRM_ERROR("Incorrect port type\n");
8106ddbd 10528 return;
3760b59c 10529 }
8106ddbd
ACO
10530
10531 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
3760b59c
S
10532}
10533
96b7dfb7
S
10534static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
10535 enum port port,
5cec258b 10536 struct intel_crtc_state *pipe_config)
96b7dfb7 10537{
8106ddbd 10538 enum intel_dpll_id id;
a3c988ea 10539 u32 temp;
96b7dfb7
S
10540
10541 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
c856052a 10542 id = temp >> (port * 3 + 1);
96b7dfb7 10543
c856052a 10544 if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL3))
8106ddbd 10545 return;
8106ddbd
ACO
10546
10547 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
96b7dfb7
S
10548}
10549
7d2c8175
DL
10550static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
10551 enum port port,
5cec258b 10552 struct intel_crtc_state *pipe_config)
7d2c8175 10553{
8106ddbd 10554 enum intel_dpll_id id;
c856052a 10555 uint32_t ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
8106ddbd 10556
c856052a 10557 switch (ddi_pll_sel) {
7d2c8175 10558 case PORT_CLK_SEL_WRPLL1:
8106ddbd 10559 id = DPLL_ID_WRPLL1;
7d2c8175
DL
10560 break;
10561 case PORT_CLK_SEL_WRPLL2:
8106ddbd 10562 id = DPLL_ID_WRPLL2;
7d2c8175 10563 break;
00490c22 10564 case PORT_CLK_SEL_SPLL:
8106ddbd 10565 id = DPLL_ID_SPLL;
79bd23da 10566 break;
9d16da65
ACO
10567 case PORT_CLK_SEL_LCPLL_810:
10568 id = DPLL_ID_LCPLL_810;
10569 break;
10570 case PORT_CLK_SEL_LCPLL_1350:
10571 id = DPLL_ID_LCPLL_1350;
10572 break;
10573 case PORT_CLK_SEL_LCPLL_2700:
10574 id = DPLL_ID_LCPLL_2700;
10575 break;
8106ddbd 10576 default:
c856052a 10577 MISSING_CASE(ddi_pll_sel);
8106ddbd
ACO
10578 /* fall through */
10579 case PORT_CLK_SEL_NONE:
8106ddbd 10580 return;
7d2c8175 10581 }
8106ddbd
ACO
10582
10583 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
7d2c8175
DL
10584}
10585
cf30429e
JN
10586static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
10587 struct intel_crtc_state *pipe_config,
10588 unsigned long *power_domain_mask)
10589{
10590 struct drm_device *dev = crtc->base.dev;
fac5e23e 10591 struct drm_i915_private *dev_priv = to_i915(dev);
cf30429e
JN
10592 enum intel_display_power_domain power_domain;
10593 u32 tmp;
10594
d9a7bc67
ID
10595 /*
10596 * The pipe->transcoder mapping is fixed with the exception of the eDP
10597 * transcoder handled below.
10598 */
cf30429e
JN
10599 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
10600
10601 /*
10602 * XXX: Do intel_display_power_get_if_enabled before reading this (for
10603 * consistency and less surprising code; it's in always on power).
10604 */
10605 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
10606 if (tmp & TRANS_DDI_FUNC_ENABLE) {
10607 enum pipe trans_edp_pipe;
10608 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
10609 default:
10610 WARN(1, "unknown pipe linked to edp transcoder\n");
10611 case TRANS_DDI_EDP_INPUT_A_ONOFF:
10612 case TRANS_DDI_EDP_INPUT_A_ON:
10613 trans_edp_pipe = PIPE_A;
10614 break;
10615 case TRANS_DDI_EDP_INPUT_B_ONOFF:
10616 trans_edp_pipe = PIPE_B;
10617 break;
10618 case TRANS_DDI_EDP_INPUT_C_ONOFF:
10619 trans_edp_pipe = PIPE_C;
10620 break;
10621 }
10622
10623 if (trans_edp_pipe == crtc->pipe)
10624 pipe_config->cpu_transcoder = TRANSCODER_EDP;
10625 }
10626
10627 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
10628 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10629 return false;
10630 *power_domain_mask |= BIT(power_domain);
10631
10632 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
10633
10634 return tmp & PIPECONF_ENABLE;
10635}
10636
4d1de975
JN
10637static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
10638 struct intel_crtc_state *pipe_config,
10639 unsigned long *power_domain_mask)
10640{
10641 struct drm_device *dev = crtc->base.dev;
fac5e23e 10642 struct drm_i915_private *dev_priv = to_i915(dev);
4d1de975
JN
10643 enum intel_display_power_domain power_domain;
10644 enum port port;
10645 enum transcoder cpu_transcoder;
10646 u32 tmp;
10647
4d1de975
JN
10648 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
10649 if (port == PORT_A)
10650 cpu_transcoder = TRANSCODER_DSI_A;
10651 else
10652 cpu_transcoder = TRANSCODER_DSI_C;
10653
10654 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
10655 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10656 continue;
10657 *power_domain_mask |= BIT(power_domain);
10658
db18b6a6
ID
10659 /*
10660 * The PLL needs to be enabled with a valid divider
10661 * configuration, otherwise accessing DSI registers will hang
10662 * the machine. See BSpec North Display Engine
10663 * registers/MIPI[BXT]. We can break out here early, since we
10664 * need the same DSI PLL to be enabled for both DSI ports.
10665 */
10666 if (!intel_dsi_pll_is_enabled(dev_priv))
10667 break;
10668
4d1de975
JN
10669 /* XXX: this works for video mode only */
10670 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
10671 if (!(tmp & DPI_ENABLE))
10672 continue;
10673
10674 tmp = I915_READ(MIPI_CTRL(port));
10675 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
10676 continue;
10677
10678 pipe_config->cpu_transcoder = cpu_transcoder;
4d1de975
JN
10679 break;
10680 }
10681
d7edc4e5 10682 return transcoder_is_dsi(pipe_config->cpu_transcoder);
4d1de975
JN
10683}
10684
26804afd 10685static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
5cec258b 10686 struct intel_crtc_state *pipe_config)
26804afd 10687{
6315b5d3 10688 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
d452c5b6 10689 struct intel_shared_dpll *pll;
26804afd
DV
10690 enum port port;
10691 uint32_t tmp;
10692
10693 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
10694
10695 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
10696
0853723b 10697 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
96b7dfb7 10698 skylake_get_ddi_pll(dev_priv, port, pipe_config);
cc3f90f0 10699 else if (IS_GEN9_LP(dev_priv))
3760b59c 10700 bxt_get_ddi_pll(dev_priv, port, pipe_config);
96b7dfb7
S
10701 else
10702 haswell_get_ddi_pll(dev_priv, port, pipe_config);
9cd86933 10703
8106ddbd
ACO
10704 pll = pipe_config->shared_dpll;
10705 if (pll) {
2edd6443
ACO
10706 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
10707 &pipe_config->dpll_hw_state));
d452c5b6
DV
10708 }
10709
26804afd
DV
10710 /*
10711 * Haswell has only FDI/PCH transcoder A. It is which is connected to
10712 * DDI E. So just check whether this pipe is wired to DDI E and whether
10713 * the PCH transcoder is on.
10714 */
6315b5d3 10715 if (INTEL_GEN(dev_priv) < 9 &&
ca370455 10716 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
26804afd
DV
10717 pipe_config->has_pch_encoder = true;
10718
10719 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
10720 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
10721 FDI_DP_PORT_WIDTH_SHIFT) + 1;
10722
10723 ironlake_get_fdi_m_n_config(crtc, pipe_config);
10724 }
10725}
10726
0e8ffe1b 10727static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5cec258b 10728 struct intel_crtc_state *pipe_config)
0e8ffe1b 10729{
6315b5d3 10730 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1729050e
ID
10731 enum intel_display_power_domain power_domain;
10732 unsigned long power_domain_mask;
cf30429e 10733 bool active;
0e8ffe1b 10734
1729050e
ID
10735 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
10736 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0 10737 return false;
1729050e
ID
10738 power_domain_mask = BIT(power_domain);
10739
8106ddbd 10740 pipe_config->shared_dpll = NULL;
c0d43d62 10741
cf30429e 10742 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
eccb140b 10743
cc3f90f0 10744 if (IS_GEN9_LP(dev_priv) &&
d7edc4e5
VS
10745 bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
10746 WARN_ON(active);
10747 active = true;
4d1de975
JN
10748 }
10749
cf30429e 10750 if (!active)
1729050e 10751 goto out;
0e8ffe1b 10752
d7edc4e5 10753 if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
4d1de975
JN
10754 haswell_get_ddi_port_state(crtc, pipe_config);
10755 intel_get_pipe_timings(crtc, pipe_config);
10756 }
627eb5a3 10757
bc58be60 10758 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 10759
05dc698c
LL
10760 pipe_config->gamma_mode =
10761 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
10762
6315b5d3 10763 if (INTEL_GEN(dev_priv) >= 9) {
65edccce 10764 skl_init_scalers(dev_priv, crtc, pipe_config);
a1b2278e 10765
af99ceda
CK
10766 pipe_config->scaler_state.scaler_id = -1;
10767 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
10768 }
10769
1729050e
ID
10770 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
10771 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
10772 power_domain_mask |= BIT(power_domain);
6315b5d3 10773 if (INTEL_GEN(dev_priv) >= 9)
bd2e244f 10774 skylake_get_pfit_config(crtc, pipe_config);
ff6d9f55 10775 else
1c132b44 10776 ironlake_get_pfit_config(crtc, pipe_config);
bd2e244f 10777 }
88adfff1 10778
772c2a51 10779 if (IS_HASWELL(dev_priv))
e59150dc
JB
10780 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
10781 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 10782
4d1de975
JN
10783 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
10784 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
ebb69c95
CT
10785 pipe_config->pixel_multiplier =
10786 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
10787 } else {
10788 pipe_config->pixel_multiplier = 1;
10789 }
6c49f241 10790
1729050e
ID
10791out:
10792 for_each_power_domain(power_domain, power_domain_mask)
10793 intel_display_power_put(dev_priv, power_domain);
10794
cf30429e 10795 return active;
0e8ffe1b
DV
10796}
10797
55a08b3f
ML
10798static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
10799 const struct intel_plane_state *plane_state)
560b85bb
CW
10800{
10801 struct drm_device *dev = crtc->dev;
fac5e23e 10802 struct drm_i915_private *dev_priv = to_i915(dev);
560b85bb 10803 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
dc41c154 10804 uint32_t cntl = 0, size = 0;
560b85bb 10805
936e71e3 10806 if (plane_state && plane_state->base.visible) {
55a08b3f
ML
10807 unsigned int width = plane_state->base.crtc_w;
10808 unsigned int height = plane_state->base.crtc_h;
dc41c154
VS
10809 unsigned int stride = roundup_pow_of_two(width) * 4;
10810
10811 switch (stride) {
10812 default:
10813 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
10814 width, stride);
10815 stride = 256;
10816 /* fallthrough */
10817 case 256:
10818 case 512:
10819 case 1024:
10820 case 2048:
10821 break;
4b0e333e
CW
10822 }
10823
dc41c154
VS
10824 cntl |= CURSOR_ENABLE |
10825 CURSOR_GAMMA_ENABLE |
10826 CURSOR_FORMAT_ARGB |
10827 CURSOR_STRIDE(stride);
10828
10829 size = (height << 12) | width;
4b0e333e 10830 }
560b85bb 10831
dc41c154
VS
10832 if (intel_crtc->cursor_cntl != 0 &&
10833 (intel_crtc->cursor_base != base ||
10834 intel_crtc->cursor_size != size ||
10835 intel_crtc->cursor_cntl != cntl)) {
10836 /* On these chipsets we can only modify the base/size/stride
10837 * whilst the cursor is disabled.
10838 */
0b87c24e
VS
10839 I915_WRITE(CURCNTR(PIPE_A), 0);
10840 POSTING_READ(CURCNTR(PIPE_A));
dc41c154 10841 intel_crtc->cursor_cntl = 0;
4b0e333e 10842 }
560b85bb 10843
99d1f387 10844 if (intel_crtc->cursor_base != base) {
0b87c24e 10845 I915_WRITE(CURBASE(PIPE_A), base);
99d1f387
VS
10846 intel_crtc->cursor_base = base;
10847 }
4726e0b0 10848
dc41c154
VS
10849 if (intel_crtc->cursor_size != size) {
10850 I915_WRITE(CURSIZE, size);
10851 intel_crtc->cursor_size = size;
4b0e333e 10852 }
560b85bb 10853
4b0e333e 10854 if (intel_crtc->cursor_cntl != cntl) {
0b87c24e
VS
10855 I915_WRITE(CURCNTR(PIPE_A), cntl);
10856 POSTING_READ(CURCNTR(PIPE_A));
4b0e333e 10857 intel_crtc->cursor_cntl = cntl;
560b85bb 10858 }
560b85bb
CW
10859}
10860
55a08b3f
ML
10861static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
10862 const struct intel_plane_state *plane_state)
65a21cd6
JB
10863{
10864 struct drm_device *dev = crtc->dev;
fac5e23e 10865 struct drm_i915_private *dev_priv = to_i915(dev);
65a21cd6
JB
10866 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10867 int pipe = intel_crtc->pipe;
663f3122 10868 uint32_t cntl = 0;
4b0e333e 10869
936e71e3 10870 if (plane_state && plane_state->base.visible) {
4b0e333e 10871 cntl = MCURSOR_GAMMA_ENABLE;
55a08b3f 10872 switch (plane_state->base.crtc_w) {
4726e0b0
SK
10873 case 64:
10874 cntl |= CURSOR_MODE_64_ARGB_AX;
10875 break;
10876 case 128:
10877 cntl |= CURSOR_MODE_128_ARGB_AX;
10878 break;
10879 case 256:
10880 cntl |= CURSOR_MODE_256_ARGB_AX;
10881 break;
10882 default:
55a08b3f 10883 MISSING_CASE(plane_state->base.crtc_w);
4726e0b0 10884 return;
65a21cd6 10885 }
4b0e333e 10886 cntl |= pipe << 28; /* Connect to correct pipe */
47bf17a7 10887
4f8036a2 10888 if (HAS_DDI(dev_priv))
47bf17a7 10889 cntl |= CURSOR_PIPE_CSC_ENABLE;
65a21cd6 10890
f22aa143 10891 if (plane_state->base.rotation & DRM_ROTATE_180)
55a08b3f
ML
10892 cntl |= CURSOR_ROTATE_180;
10893 }
4398ad45 10894
4b0e333e
CW
10895 if (intel_crtc->cursor_cntl != cntl) {
10896 I915_WRITE(CURCNTR(pipe), cntl);
10897 POSTING_READ(CURCNTR(pipe));
10898 intel_crtc->cursor_cntl = cntl;
65a21cd6 10899 }
4b0e333e 10900
65a21cd6 10901 /* and commit changes on next vblank */
5efb3e28
VS
10902 I915_WRITE(CURBASE(pipe), base);
10903 POSTING_READ(CURBASE(pipe));
99d1f387
VS
10904
10905 intel_crtc->cursor_base = base;
65a21cd6
JB
10906}
10907
cda4b7d3 10908/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6b383a7f 10909static void intel_crtc_update_cursor(struct drm_crtc *crtc,
55a08b3f 10910 const struct intel_plane_state *plane_state)
cda4b7d3
CW
10911{
10912 struct drm_device *dev = crtc->dev;
fac5e23e 10913 struct drm_i915_private *dev_priv = to_i915(dev);
cda4b7d3
CW
10914 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10915 int pipe = intel_crtc->pipe;
55a08b3f
ML
10916 u32 base = intel_crtc->cursor_addr;
10917 u32 pos = 0;
cda4b7d3 10918
55a08b3f
ML
10919 if (plane_state) {
10920 int x = plane_state->base.crtc_x;
10921 int y = plane_state->base.crtc_y;
cda4b7d3 10922
55a08b3f
ML
10923 if (x < 0) {
10924 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10925 x = -x;
10926 }
10927 pos |= x << CURSOR_X_SHIFT;
cda4b7d3 10928
55a08b3f
ML
10929 if (y < 0) {
10930 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10931 y = -y;
10932 }
10933 pos |= y << CURSOR_Y_SHIFT;
10934
10935 /* ILK+ do this automagically */
49cff963 10936 if (HAS_GMCH_DISPLAY(dev_priv) &&
f22aa143 10937 plane_state->base.rotation & DRM_ROTATE_180) {
55a08b3f
ML
10938 base += (plane_state->base.crtc_h *
10939 plane_state->base.crtc_w - 1) * 4;
10940 }
cda4b7d3 10941 }
cda4b7d3 10942
5efb3e28
VS
10943 I915_WRITE(CURPOS(pipe), pos);
10944
50a0bc90 10945 if (IS_845G(dev_priv) || IS_I865G(dev_priv))
55a08b3f 10946 i845_update_cursor(crtc, base, plane_state);
5efb3e28 10947 else
55a08b3f 10948 i9xx_update_cursor(crtc, base, plane_state);
cda4b7d3
CW
10949}
10950
50a0bc90 10951static bool cursor_size_ok(struct drm_i915_private *dev_priv,
dc41c154
VS
10952 uint32_t width, uint32_t height)
10953{
10954 if (width == 0 || height == 0)
10955 return false;
10956
10957 /*
10958 * 845g/865g are special in that they are only limited by
10959 * the width of their cursors, the height is arbitrary up to
10960 * the precision of the register. Everything else requires
10961 * square cursors, limited to a few power-of-two sizes.
10962 */
50a0bc90 10963 if (IS_845G(dev_priv) || IS_I865G(dev_priv)) {
dc41c154
VS
10964 if ((width & 63) != 0)
10965 return false;
10966
50a0bc90 10967 if (width > (IS_845G(dev_priv) ? 64 : 512))
dc41c154
VS
10968 return false;
10969
10970 if (height > 1023)
10971 return false;
10972 } else {
10973 switch (width | height) {
10974 case 256:
10975 case 128:
50a0bc90 10976 if (IS_GEN2(dev_priv))
dc41c154
VS
10977 return false;
10978 case 64:
10979 break;
10980 default:
10981 return false;
10982 }
10983 }
10984
10985 return true;
10986}
10987
79e53945
JB
10988/* VESA 640x480x72Hz mode to set on the pipe */
10989static struct drm_display_mode load_detect_mode = {
10990 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10991 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10992};
10993
a8bb6818
DV
10994struct drm_framebuffer *
10995__intel_framebuffer_create(struct drm_device *dev,
10996 struct drm_mode_fb_cmd2 *mode_cmd,
10997 struct drm_i915_gem_object *obj)
d2dff872
CW
10998{
10999 struct intel_framebuffer *intel_fb;
11000 int ret;
11001
11002 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
dcb1394e 11003 if (!intel_fb)
d2dff872 11004 return ERR_PTR(-ENOMEM);
d2dff872
CW
11005
11006 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
dd4916c5
DV
11007 if (ret)
11008 goto err;
d2dff872
CW
11009
11010 return &intel_fb->base;
dcb1394e 11011
dd4916c5 11012err:
dd4916c5 11013 kfree(intel_fb);
dd4916c5 11014 return ERR_PTR(ret);
d2dff872
CW
11015}
11016
b5ea642a 11017static struct drm_framebuffer *
a8bb6818
DV
11018intel_framebuffer_create(struct drm_device *dev,
11019 struct drm_mode_fb_cmd2 *mode_cmd,
11020 struct drm_i915_gem_object *obj)
11021{
11022 struct drm_framebuffer *fb;
11023 int ret;
11024
11025 ret = i915_mutex_lock_interruptible(dev);
11026 if (ret)
11027 return ERR_PTR(ret);
11028 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
11029 mutex_unlock(&dev->struct_mutex);
11030
11031 return fb;
11032}
11033
d2dff872
CW
11034static u32
11035intel_framebuffer_pitch_for_width(int width, int bpp)
11036{
11037 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
11038 return ALIGN(pitch, 64);
11039}
11040
11041static u32
11042intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
11043{
11044 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
1267a26b 11045 return PAGE_ALIGN(pitch * mode->vdisplay);
d2dff872
CW
11046}
11047
11048static struct drm_framebuffer *
11049intel_framebuffer_create_for_mode(struct drm_device *dev,
11050 struct drm_display_mode *mode,
11051 int depth, int bpp)
11052{
dcb1394e 11053 struct drm_framebuffer *fb;
d2dff872 11054 struct drm_i915_gem_object *obj;
0fed39bd 11055 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872 11056
12d79d78 11057 obj = i915_gem_object_create(to_i915(dev),
d2dff872 11058 intel_framebuffer_size_for_mode(mode, bpp));
fe3db79b
CW
11059 if (IS_ERR(obj))
11060 return ERR_CAST(obj);
d2dff872
CW
11061
11062 mode_cmd.width = mode->hdisplay;
11063 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
11064 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
11065 bpp);
5ca0c34a 11066 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872 11067
dcb1394e
LW
11068 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
11069 if (IS_ERR(fb))
f0cd5182 11070 i915_gem_object_put(obj);
dcb1394e
LW
11071
11072 return fb;
d2dff872
CW
11073}
11074
11075static struct drm_framebuffer *
11076mode_fits_in_fbdev(struct drm_device *dev,
11077 struct drm_display_mode *mode)
11078{
0695726e 11079#ifdef CONFIG_DRM_FBDEV_EMULATION
fac5e23e 11080 struct drm_i915_private *dev_priv = to_i915(dev);
d2dff872
CW
11081 struct drm_i915_gem_object *obj;
11082 struct drm_framebuffer *fb;
11083
4c0e5528 11084 if (!dev_priv->fbdev)
d2dff872
CW
11085 return NULL;
11086
4c0e5528 11087 if (!dev_priv->fbdev->fb)
d2dff872
CW
11088 return NULL;
11089
4c0e5528
DV
11090 obj = dev_priv->fbdev->fb->obj;
11091 BUG_ON(!obj);
11092
8bcd4553 11093 fb = &dev_priv->fbdev->fb->base;
01f2c773
VS
11094 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
11095 fb->bits_per_pixel))
d2dff872
CW
11096 return NULL;
11097
01f2c773 11098 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
11099 return NULL;
11100
edde3617 11101 drm_framebuffer_reference(fb);
d2dff872 11102 return fb;
4520f53a
DV
11103#else
11104 return NULL;
11105#endif
d2dff872
CW
11106}
11107
d3a40d1b
ACO
11108static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
11109 struct drm_crtc *crtc,
11110 struct drm_display_mode *mode,
11111 struct drm_framebuffer *fb,
11112 int x, int y)
11113{
11114 struct drm_plane_state *plane_state;
11115 int hdisplay, vdisplay;
11116 int ret;
11117
11118 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
11119 if (IS_ERR(plane_state))
11120 return PTR_ERR(plane_state);
11121
11122 if (mode)
11123 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
11124 else
11125 hdisplay = vdisplay = 0;
11126
11127 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
11128 if (ret)
11129 return ret;
11130 drm_atomic_set_fb_for_plane(plane_state, fb);
11131 plane_state->crtc_x = 0;
11132 plane_state->crtc_y = 0;
11133 plane_state->crtc_w = hdisplay;
11134 plane_state->crtc_h = vdisplay;
11135 plane_state->src_x = x << 16;
11136 plane_state->src_y = y << 16;
11137 plane_state->src_w = hdisplay << 16;
11138 plane_state->src_h = vdisplay << 16;
11139
11140 return 0;
11141}
11142
d2434ab7 11143bool intel_get_load_detect_pipe(struct drm_connector *connector,
7173188d 11144 struct drm_display_mode *mode,
51fd371b
RC
11145 struct intel_load_detect_pipe *old,
11146 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
11147{
11148 struct intel_crtc *intel_crtc;
d2434ab7
DV
11149 struct intel_encoder *intel_encoder =
11150 intel_attached_encoder(connector);
79e53945 11151 struct drm_crtc *possible_crtc;
4ef69c7a 11152 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
11153 struct drm_crtc *crtc = NULL;
11154 struct drm_device *dev = encoder->dev;
0f0f74bc 11155 struct drm_i915_private *dev_priv = to_i915(dev);
94352cf9 11156 struct drm_framebuffer *fb;
51fd371b 11157 struct drm_mode_config *config = &dev->mode_config;
edde3617 11158 struct drm_atomic_state *state = NULL, *restore_state = NULL;
944b0c76 11159 struct drm_connector_state *connector_state;
4be07317 11160 struct intel_crtc_state *crtc_state;
51fd371b 11161 int ret, i = -1;
79e53945 11162
d2dff872 11163 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 11164 connector->base.id, connector->name,
8e329a03 11165 encoder->base.id, encoder->name);
d2dff872 11166
edde3617
ML
11167 old->restore_state = NULL;
11168
51fd371b
RC
11169retry:
11170 ret = drm_modeset_lock(&config->connection_mutex, ctx);
11171 if (ret)
ad3c558f 11172 goto fail;
6e9f798d 11173
79e53945
JB
11174 /*
11175 * Algorithm gets a little messy:
7a5e4805 11176 *
79e53945
JB
11177 * - if the connector already has an assigned crtc, use it (but make
11178 * sure it's on first)
7a5e4805 11179 *
79e53945
JB
11180 * - try to find the first unused crtc that can drive this connector,
11181 * and use that if we find one
79e53945
JB
11182 */
11183
11184 /* See if we already have a CRTC for this connector */
edde3617
ML
11185 if (connector->state->crtc) {
11186 crtc = connector->state->crtc;
8261b191 11187
51fd371b 11188 ret = drm_modeset_lock(&crtc->mutex, ctx);
4d02e2de 11189 if (ret)
ad3c558f 11190 goto fail;
8261b191
CW
11191
11192 /* Make sure the crtc and connector are running */
edde3617 11193 goto found;
79e53945
JB
11194 }
11195
11196 /* Find an unused one (if possible) */
70e1e0ec 11197 for_each_crtc(dev, possible_crtc) {
79e53945
JB
11198 i++;
11199 if (!(encoder->possible_crtcs & (1 << i)))
11200 continue;
edde3617
ML
11201
11202 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
11203 if (ret)
11204 goto fail;
11205
11206 if (possible_crtc->state->enable) {
11207 drm_modeset_unlock(&possible_crtc->mutex);
a459249c 11208 continue;
edde3617 11209 }
a459249c
VS
11210
11211 crtc = possible_crtc;
11212 break;
79e53945
JB
11213 }
11214
11215 /*
11216 * If we didn't find an unused CRTC, don't use any.
11217 */
11218 if (!crtc) {
7173188d 11219 DRM_DEBUG_KMS("no pipe available for load-detect\n");
ad3c558f 11220 goto fail;
79e53945
JB
11221 }
11222
edde3617
ML
11223found:
11224 intel_crtc = to_intel_crtc(crtc);
11225
4d02e2de
DV
11226 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
11227 if (ret)
ad3c558f 11228 goto fail;
79e53945 11229
83a57153 11230 state = drm_atomic_state_alloc(dev);
edde3617
ML
11231 restore_state = drm_atomic_state_alloc(dev);
11232 if (!state || !restore_state) {
11233 ret = -ENOMEM;
11234 goto fail;
11235 }
83a57153
ACO
11236
11237 state->acquire_ctx = ctx;
edde3617 11238 restore_state->acquire_ctx = ctx;
83a57153 11239
944b0c76
ACO
11240 connector_state = drm_atomic_get_connector_state(state, connector);
11241 if (IS_ERR(connector_state)) {
11242 ret = PTR_ERR(connector_state);
11243 goto fail;
11244 }
11245
edde3617
ML
11246 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
11247 if (ret)
11248 goto fail;
944b0c76 11249
4be07317
ACO
11250 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
11251 if (IS_ERR(crtc_state)) {
11252 ret = PTR_ERR(crtc_state);
11253 goto fail;
11254 }
11255
49d6fa21 11256 crtc_state->base.active = crtc_state->base.enable = true;
4be07317 11257
6492711d
CW
11258 if (!mode)
11259 mode = &load_detect_mode;
79e53945 11260
d2dff872
CW
11261 /* We need a framebuffer large enough to accommodate all accesses
11262 * that the plane may generate whilst we perform load detection.
11263 * We can not rely on the fbcon either being present (we get called
11264 * during its initialisation to detect all boot displays, or it may
11265 * not even exist) or that it is large enough to satisfy the
11266 * requested mode.
11267 */
94352cf9
DV
11268 fb = mode_fits_in_fbdev(dev, mode);
11269 if (fb == NULL) {
d2dff872 11270 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9 11271 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
d2dff872
CW
11272 } else
11273 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 11274 if (IS_ERR(fb)) {
d2dff872 11275 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
412b61d8 11276 goto fail;
79e53945 11277 }
79e53945 11278
d3a40d1b
ACO
11279 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
11280 if (ret)
11281 goto fail;
11282
edde3617
ML
11283 drm_framebuffer_unreference(fb);
11284
11285 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
11286 if (ret)
11287 goto fail;
11288
11289 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
11290 if (!ret)
11291 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
11292 if (!ret)
11293 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
11294 if (ret) {
11295 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
11296 goto fail;
11297 }
8c7b5ccb 11298
3ba86073
ML
11299 ret = drm_atomic_commit(state);
11300 if (ret) {
6492711d 11301 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
412b61d8 11302 goto fail;
79e53945 11303 }
edde3617
ML
11304
11305 old->restore_state = restore_state;
7173188d 11306
79e53945 11307 /* let the connector get through one full cycle before testing */
0f0f74bc 11308 intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
7173188d 11309 return true;
412b61d8 11310
ad3c558f 11311fail:
7fb71c8f
CW
11312 if (state) {
11313 drm_atomic_state_put(state);
11314 state = NULL;
11315 }
11316 if (restore_state) {
11317 drm_atomic_state_put(restore_state);
11318 restore_state = NULL;
11319 }
83a57153 11320
51fd371b
RC
11321 if (ret == -EDEADLK) {
11322 drm_modeset_backoff(ctx);
11323 goto retry;
11324 }
11325
412b61d8 11326 return false;
79e53945
JB
11327}
11328
d2434ab7 11329void intel_release_load_detect_pipe(struct drm_connector *connector,
49172fee
ACO
11330 struct intel_load_detect_pipe *old,
11331 struct drm_modeset_acquire_ctx *ctx)
79e53945 11332{
d2434ab7
DV
11333 struct intel_encoder *intel_encoder =
11334 intel_attached_encoder(connector);
4ef69c7a 11335 struct drm_encoder *encoder = &intel_encoder->base;
edde3617 11336 struct drm_atomic_state *state = old->restore_state;
d3a40d1b 11337 int ret;
79e53945 11338
d2dff872 11339 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 11340 connector->base.id, connector->name,
8e329a03 11341 encoder->base.id, encoder->name);
d2dff872 11342
edde3617 11343 if (!state)
0622a53c 11344 return;
79e53945 11345
edde3617 11346 ret = drm_atomic_commit(state);
0853695c 11347 if (ret)
edde3617 11348 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
0853695c 11349 drm_atomic_state_put(state);
79e53945
JB
11350}
11351
da4a1efa 11352static int i9xx_pll_refclk(struct drm_device *dev,
5cec258b 11353 const struct intel_crtc_state *pipe_config)
da4a1efa 11354{
fac5e23e 11355 struct drm_i915_private *dev_priv = to_i915(dev);
da4a1efa
VS
11356 u32 dpll = pipe_config->dpll_hw_state.dpll;
11357
11358 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 11359 return dev_priv->vbt.lvds_ssc_freq;
6e266956 11360 else if (HAS_PCH_SPLIT(dev_priv))
da4a1efa 11361 return 120000;
5db94019 11362 else if (!IS_GEN2(dev_priv))
da4a1efa
VS
11363 return 96000;
11364 else
11365 return 48000;
11366}
11367
79e53945 11368/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc 11369static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 11370 struct intel_crtc_state *pipe_config)
79e53945 11371{
f1f644dc 11372 struct drm_device *dev = crtc->base.dev;
fac5e23e 11373 struct drm_i915_private *dev_priv = to_i915(dev);
f1f644dc 11374 int pipe = pipe_config->cpu_transcoder;
293623f7 11375 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945 11376 u32 fp;
9e2c8475 11377 struct dpll clock;
dccbea3b 11378 int port_clock;
da4a1efa 11379 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
11380
11381 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 11382 fp = pipe_config->dpll_hw_state.fp0;
79e53945 11383 else
293623f7 11384 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
11385
11386 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
9b1e14f4 11387 if (IS_PINEVIEW(dev_priv)) {
f2b115e6
AJ
11388 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
11389 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
11390 } else {
11391 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
11392 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
11393 }
11394
5db94019 11395 if (!IS_GEN2(dev_priv)) {
9b1e14f4 11396 if (IS_PINEVIEW(dev_priv))
f2b115e6
AJ
11397 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
11398 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
11399 else
11400 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
11401 DPLL_FPA01_P1_POST_DIV_SHIFT);
11402
11403 switch (dpll & DPLL_MODE_MASK) {
11404 case DPLLB_MODE_DAC_SERIAL:
11405 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
11406 5 : 10;
11407 break;
11408 case DPLLB_MODE_LVDS:
11409 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
11410 7 : 14;
11411 break;
11412 default:
28c97730 11413 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 11414 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 11415 return;
79e53945
JB
11416 }
11417
9b1e14f4 11418 if (IS_PINEVIEW(dev_priv))
dccbea3b 11419 port_clock = pnv_calc_dpll_params(refclk, &clock);
ac58c3f0 11420 else
dccbea3b 11421 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945 11422 } else {
50a0bc90 11423 u32 lvds = IS_I830(dev_priv) ? 0 : I915_READ(LVDS);
b1c560d1 11424 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
11425
11426 if (is_lvds) {
11427 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
11428 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
11429
11430 if (lvds & LVDS_CLKB_POWER_UP)
11431 clock.p2 = 7;
11432 else
11433 clock.p2 = 14;
79e53945
JB
11434 } else {
11435 if (dpll & PLL_P1_DIVIDE_BY_TWO)
11436 clock.p1 = 2;
11437 else {
11438 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
11439 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
11440 }
11441 if (dpll & PLL_P2_DIVIDE_BY_4)
11442 clock.p2 = 4;
11443 else
11444 clock.p2 = 2;
79e53945 11445 }
da4a1efa 11446
dccbea3b 11447 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945
JB
11448 }
11449
18442d08
VS
11450 /*
11451 * This value includes pixel_multiplier. We will use
241bfc38 11452 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
11453 * encoder's get_config() function.
11454 */
dccbea3b 11455 pipe_config->port_clock = port_clock;
f1f644dc
JB
11456}
11457
6878da05
VS
11458int intel_dotclock_calculate(int link_freq,
11459 const struct intel_link_m_n *m_n)
f1f644dc 11460{
f1f644dc
JB
11461 /*
11462 * The calculation for the data clock is:
1041a02f 11463 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 11464 * But we want to avoid losing precison if possible, so:
1041a02f 11465 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
11466 *
11467 * and the link clock is simpler:
1041a02f 11468 * link_clock = (m * link_clock) / n
f1f644dc
JB
11469 */
11470
6878da05
VS
11471 if (!m_n->link_n)
11472 return 0;
f1f644dc 11473
6878da05
VS
11474 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
11475}
f1f644dc 11476
18442d08 11477static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 11478 struct intel_crtc_state *pipe_config)
6878da05 11479{
e3b247da 11480 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
79e53945 11481
18442d08
VS
11482 /* read out port_clock from the DPLL */
11483 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 11484
f1f644dc 11485 /*
e3b247da
VS
11486 * In case there is an active pipe without active ports,
11487 * we may need some idea for the dotclock anyway.
11488 * Calculate one based on the FDI configuration.
79e53945 11489 */
2d112de7 11490 pipe_config->base.adjusted_mode.crtc_clock =
21a727b3 11491 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
18442d08 11492 &pipe_config->fdi_m_n);
79e53945
JB
11493}
11494
11495/** Returns the currently programmed mode of the given pipe. */
11496struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
11497 struct drm_crtc *crtc)
11498{
fac5e23e 11499 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 11500 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 11501 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
79e53945 11502 struct drm_display_mode *mode;
3f36b937 11503 struct intel_crtc_state *pipe_config;
fe2b8f9d
PZ
11504 int htot = I915_READ(HTOTAL(cpu_transcoder));
11505 int hsync = I915_READ(HSYNC(cpu_transcoder));
11506 int vtot = I915_READ(VTOTAL(cpu_transcoder));
11507 int vsync = I915_READ(VSYNC(cpu_transcoder));
293623f7 11508 enum pipe pipe = intel_crtc->pipe;
79e53945
JB
11509
11510 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
11511 if (!mode)
11512 return NULL;
11513
3f36b937
TU
11514 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
11515 if (!pipe_config) {
11516 kfree(mode);
11517 return NULL;
11518 }
11519
f1f644dc
JB
11520 /*
11521 * Construct a pipe_config sufficient for getting the clock info
11522 * back out of crtc_clock_get.
11523 *
11524 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
11525 * to use a real value here instead.
11526 */
3f36b937
TU
11527 pipe_config->cpu_transcoder = (enum transcoder) pipe;
11528 pipe_config->pixel_multiplier = 1;
11529 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
11530 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
11531 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
11532 i9xx_crtc_clock_get(intel_crtc, pipe_config);
11533
11534 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
79e53945
JB
11535 mode->hdisplay = (htot & 0xffff) + 1;
11536 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
11537 mode->hsync_start = (hsync & 0xffff) + 1;
11538 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
11539 mode->vdisplay = (vtot & 0xffff) + 1;
11540 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
11541 mode->vsync_start = (vsync & 0xffff) + 1;
11542 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
11543
11544 drm_mode_set_name(mode);
79e53945 11545
3f36b937
TU
11546 kfree(pipe_config);
11547
79e53945
JB
11548 return mode;
11549}
11550
11551static void intel_crtc_destroy(struct drm_crtc *crtc)
11552{
11553 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
67e77c5a 11554 struct drm_device *dev = crtc->dev;
51cbaf01 11555 struct intel_flip_work *work;
67e77c5a 11556
5e2d7afc 11557 spin_lock_irq(&dev->event_lock);
5a21b665
DV
11558 work = intel_crtc->flip_work;
11559 intel_crtc->flip_work = NULL;
11560 spin_unlock_irq(&dev->event_lock);
67e77c5a 11561
5a21b665 11562 if (work) {
51cbaf01
ML
11563 cancel_work_sync(&work->mmio_work);
11564 cancel_work_sync(&work->unpin_work);
5a21b665 11565 kfree(work);
67e77c5a 11566 }
79e53945
JB
11567
11568 drm_crtc_cleanup(crtc);
67e77c5a 11569
79e53945
JB
11570 kfree(intel_crtc);
11571}
11572
6b95a207
KH
11573static void intel_unpin_work_fn(struct work_struct *__work)
11574{
51cbaf01
ML
11575 struct intel_flip_work *work =
11576 container_of(__work, struct intel_flip_work, unpin_work);
5a21b665
DV
11577 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
11578 struct drm_device *dev = crtc->base.dev;
11579 struct drm_plane *primary = crtc->base.primary;
03f476e1 11580
5a21b665
DV
11581 if (is_mmio_work(work))
11582 flush_work(&work->mmio_work);
03f476e1 11583
5a21b665
DV
11584 mutex_lock(&dev->struct_mutex);
11585 intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
f8c417cd 11586 i915_gem_object_put(work->pending_flip_obj);
5a21b665 11587 mutex_unlock(&dev->struct_mutex);
143f73b3 11588
e8a261ea
CW
11589 i915_gem_request_put(work->flip_queued_req);
11590
5748b6a1
CW
11591 intel_frontbuffer_flip_complete(to_i915(dev),
11592 to_intel_plane(primary)->frontbuffer_bit);
5a21b665
DV
11593 intel_fbc_post_update(crtc);
11594 drm_framebuffer_unreference(work->old_fb);
143f73b3 11595
5a21b665
DV
11596 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
11597 atomic_dec(&crtc->unpin_work_count);
a6747b73 11598
5a21b665
DV
11599 kfree(work);
11600}
d9e86c0e 11601
5a21b665
DV
11602/* Is 'a' after or equal to 'b'? */
11603static bool g4x_flip_count_after_eq(u32 a, u32 b)
11604{
11605 return !((a - b) & 0x80000000);
11606}
143f73b3 11607
5a21b665
DV
11608static bool __pageflip_finished_cs(struct intel_crtc *crtc,
11609 struct intel_flip_work *work)
11610{
11611 struct drm_device *dev = crtc->base.dev;
fac5e23e 11612 struct drm_i915_private *dev_priv = to_i915(dev);
143f73b3 11613
8af29b0c 11614 if (abort_flip_on_reset(crtc))
5a21b665 11615 return true;
143f73b3 11616
5a21b665
DV
11617 /*
11618 * The relevant registers doen't exist on pre-ctg.
11619 * As the flip done interrupt doesn't trigger for mmio
11620 * flips on gmch platforms, a flip count check isn't
11621 * really needed there. But since ctg has the registers,
11622 * include it in the check anyway.
11623 */
9beb5fea 11624 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
5a21b665 11625 return true;
b4a98e57 11626
5a21b665
DV
11627 /*
11628 * BDW signals flip done immediately if the plane
11629 * is disabled, even if the plane enable is already
11630 * armed to occur at the next vblank :(
11631 */
f99d7069 11632
5a21b665
DV
11633 /*
11634 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
11635 * used the same base address. In that case the mmio flip might
11636 * have completed, but the CS hasn't even executed the flip yet.
11637 *
11638 * A flip count check isn't enough as the CS might have updated
11639 * the base address just after start of vblank, but before we
11640 * managed to process the interrupt. This means we'd complete the
11641 * CS flip too soon.
11642 *
11643 * Combining both checks should get us a good enough result. It may
11644 * still happen that the CS flip has been executed, but has not
11645 * yet actually completed. But in case the base address is the same
11646 * anyway, we don't really care.
11647 */
11648 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
11649 crtc->flip_work->gtt_offset &&
11650 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
11651 crtc->flip_work->flip_count);
11652}
b4a98e57 11653
5a21b665
DV
11654static bool
11655__pageflip_finished_mmio(struct intel_crtc *crtc,
11656 struct intel_flip_work *work)
11657{
11658 /*
11659 * MMIO work completes when vblank is different from
11660 * flip_queued_vblank.
11661 *
11662 * Reset counter value doesn't matter, this is handled by
11663 * i915_wait_request finishing early, so no need to handle
11664 * reset here.
11665 */
11666 return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
6b95a207
KH
11667}
11668
51cbaf01
ML
11669
11670static bool pageflip_finished(struct intel_crtc *crtc,
11671 struct intel_flip_work *work)
11672{
11673 if (!atomic_read(&work->pending))
11674 return false;
11675
11676 smp_rmb();
11677
5a21b665
DV
11678 if (is_mmio_work(work))
11679 return __pageflip_finished_mmio(crtc, work);
11680 else
11681 return __pageflip_finished_cs(crtc, work);
11682}
11683
11684void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
11685{
91c8a326 11686 struct drm_device *dev = &dev_priv->drm;
98187836 11687 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
5a21b665
DV
11688 struct intel_flip_work *work;
11689 unsigned long flags;
11690
11691 /* Ignore early vblank irqs */
11692 if (!crtc)
11693 return;
11694
51cbaf01 11695 /*
5a21b665
DV
11696 * This is called both by irq handlers and the reset code (to complete
11697 * lost pageflips) so needs the full irqsave spinlocks.
51cbaf01 11698 */
5a21b665 11699 spin_lock_irqsave(&dev->event_lock, flags);
e2af48c6 11700 work = crtc->flip_work;
5a21b665
DV
11701
11702 if (work != NULL &&
11703 !is_mmio_work(work) &&
e2af48c6
VS
11704 pageflip_finished(crtc, work))
11705 page_flip_completed(crtc);
5a21b665
DV
11706
11707 spin_unlock_irqrestore(&dev->event_lock, flags);
75f7f3ec
VS
11708}
11709
51cbaf01 11710void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
6b95a207 11711{
91c8a326 11712 struct drm_device *dev = &dev_priv->drm;
98187836 11713 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
51cbaf01 11714 struct intel_flip_work *work;
6b95a207
KH
11715 unsigned long flags;
11716
5251f04e
ML
11717 /* Ignore early vblank irqs */
11718 if (!crtc)
11719 return;
f326038a
DV
11720
11721 /*
11722 * This is called both by irq handlers and the reset code (to complete
11723 * lost pageflips) so needs the full irqsave spinlocks.
e7d841ca 11724 */
6b95a207 11725 spin_lock_irqsave(&dev->event_lock, flags);
e2af48c6 11726 work = crtc->flip_work;
5251f04e 11727
5a21b665
DV
11728 if (work != NULL &&
11729 is_mmio_work(work) &&
e2af48c6
VS
11730 pageflip_finished(crtc, work))
11731 page_flip_completed(crtc);
5251f04e 11732
6b95a207
KH
11733 spin_unlock_irqrestore(&dev->event_lock, flags);
11734}
11735
5a21b665
DV
11736static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
11737 struct intel_flip_work *work)
84c33a64 11738{
5a21b665 11739 work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
84c33a64 11740
5a21b665
DV
11741 /* Ensure that the work item is consistent when activating it ... */
11742 smp_mb__before_atomic();
11743 atomic_set(&work->pending, 1);
11744}
a6747b73 11745
5a21b665
DV
11746static int intel_gen2_queue_flip(struct drm_device *dev,
11747 struct drm_crtc *crtc,
11748 struct drm_framebuffer *fb,
11749 struct drm_i915_gem_object *obj,
11750 struct drm_i915_gem_request *req,
11751 uint32_t flags)
11752{
7e37f889 11753 struct intel_ring *ring = req->ring;
5a21b665
DV
11754 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11755 u32 flip_mask;
11756 int ret;
143f73b3 11757
5a21b665
DV
11758 ret = intel_ring_begin(req, 6);
11759 if (ret)
11760 return ret;
143f73b3 11761
5a21b665
DV
11762 /* Can't queue multiple flips, so wait for the previous
11763 * one to finish before executing the next.
11764 */
11765 if (intel_crtc->plane)
11766 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11767 else
11768 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
b5321f30
CW
11769 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11770 intel_ring_emit(ring, MI_NOOP);
11771 intel_ring_emit(ring, MI_DISPLAY_FLIP |
5a21b665 11772 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
b5321f30
CW
11773 intel_ring_emit(ring, fb->pitches[0]);
11774 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
11775 intel_ring_emit(ring, 0); /* aux display base address, unused */
143f73b3 11776
5a21b665
DV
11777 return 0;
11778}
84c33a64 11779
5a21b665
DV
11780static int intel_gen3_queue_flip(struct drm_device *dev,
11781 struct drm_crtc *crtc,
11782 struct drm_framebuffer *fb,
11783 struct drm_i915_gem_object *obj,
11784 struct drm_i915_gem_request *req,
11785 uint32_t flags)
11786{
7e37f889 11787 struct intel_ring *ring = req->ring;
5a21b665
DV
11788 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11789 u32 flip_mask;
11790 int ret;
d55dbd06 11791
5a21b665
DV
11792 ret = intel_ring_begin(req, 6);
11793 if (ret)
11794 return ret;
d55dbd06 11795
5a21b665
DV
11796 if (intel_crtc->plane)
11797 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11798 else
11799 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
b5321f30
CW
11800 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11801 intel_ring_emit(ring, MI_NOOP);
11802 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
5a21b665 11803 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
b5321f30
CW
11804 intel_ring_emit(ring, fb->pitches[0]);
11805 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
11806 intel_ring_emit(ring, MI_NOOP);
fd8e058a 11807
5a21b665
DV
11808 return 0;
11809}
84c33a64 11810
5a21b665
DV
11811static int intel_gen4_queue_flip(struct drm_device *dev,
11812 struct drm_crtc *crtc,
11813 struct drm_framebuffer *fb,
11814 struct drm_i915_gem_object *obj,
11815 struct drm_i915_gem_request *req,
11816 uint32_t flags)
11817{
7e37f889 11818 struct intel_ring *ring = req->ring;
fac5e23e 11819 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
DV
11820 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11821 uint32_t pf, pipesrc;
11822 int ret;
143f73b3 11823
5a21b665
DV
11824 ret = intel_ring_begin(req, 4);
11825 if (ret)
11826 return ret;
143f73b3 11827
5a21b665
DV
11828 /* i965+ uses the linear or tiled offsets from the
11829 * Display Registers (which do not change across a page-flip)
11830 * so we need only reprogram the base address.
11831 */
b5321f30 11832 intel_ring_emit(ring, MI_DISPLAY_FLIP |
5a21b665 11833 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
b5321f30
CW
11834 intel_ring_emit(ring, fb->pitches[0]);
11835 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset |
bae781b2 11836 intel_fb_modifier_to_tiling(fb->modifier));
5a21b665
DV
11837
11838 /* XXX Enabling the panel-fitter across page-flip is so far
11839 * untested on non-native modes, so ignore it for now.
11840 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11841 */
11842 pf = 0;
11843 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
b5321f30 11844 intel_ring_emit(ring, pf | pipesrc);
143f73b3 11845
5a21b665 11846 return 0;
8c9f3aaf
JB
11847}
11848
5a21b665
DV
11849static int intel_gen6_queue_flip(struct drm_device *dev,
11850 struct drm_crtc *crtc,
11851 struct drm_framebuffer *fb,
11852 struct drm_i915_gem_object *obj,
11853 struct drm_i915_gem_request *req,
11854 uint32_t flags)
da20eabd 11855{
7e37f889 11856 struct intel_ring *ring = req->ring;
fac5e23e 11857 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
DV
11858 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11859 uint32_t pf, pipesrc;
11860 int ret;
d21fbe87 11861
5a21b665
DV
11862 ret = intel_ring_begin(req, 4);
11863 if (ret)
11864 return ret;
92826fcd 11865
b5321f30 11866 intel_ring_emit(ring, MI_DISPLAY_FLIP |
5a21b665 11867 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
72618ebf 11868 intel_ring_emit(ring, fb->pitches[0] |
bae781b2 11869 intel_fb_modifier_to_tiling(fb->modifier));
b5321f30 11870 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
92826fcd 11871
5a21b665
DV
11872 /* Contrary to the suggestions in the documentation,
11873 * "Enable Panel Fitter" does not seem to be required when page
11874 * flipping with a non-native mode, and worse causes a normal
11875 * modeset to fail.
11876 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11877 */
11878 pf = 0;
11879 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
b5321f30 11880 intel_ring_emit(ring, pf | pipesrc);
7809e5ae 11881
5a21b665 11882 return 0;
7809e5ae
MR
11883}
11884
5a21b665
DV
11885static int intel_gen7_queue_flip(struct drm_device *dev,
11886 struct drm_crtc *crtc,
11887 struct drm_framebuffer *fb,
11888 struct drm_i915_gem_object *obj,
11889 struct drm_i915_gem_request *req,
11890 uint32_t flags)
d21fbe87 11891{
5db94019 11892 struct drm_i915_private *dev_priv = to_i915(dev);
7e37f889 11893 struct intel_ring *ring = req->ring;
5a21b665
DV
11894 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11895 uint32_t plane_bit = 0;
11896 int len, ret;
d21fbe87 11897
5a21b665
DV
11898 switch (intel_crtc->plane) {
11899 case PLANE_A:
11900 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11901 break;
11902 case PLANE_B:
11903 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11904 break;
11905 case PLANE_C:
11906 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11907 break;
11908 default:
11909 WARN_ONCE(1, "unknown plane in flip command\n");
11910 return -ENODEV;
11911 }
11912
11913 len = 4;
b5321f30 11914 if (req->engine->id == RCS) {
5a21b665
DV
11915 len += 6;
11916 /*
11917 * On Gen 8, SRM is now taking an extra dword to accommodate
11918 * 48bits addresses, and we need a NOOP for the batch size to
11919 * stay even.
11920 */
5db94019 11921 if (IS_GEN8(dev_priv))
5a21b665
DV
11922 len += 2;
11923 }
11924
11925 /*
11926 * BSpec MI_DISPLAY_FLIP for IVB:
11927 * "The full packet must be contained within the same cache line."
11928 *
11929 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11930 * cacheline, if we ever start emitting more commands before
11931 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11932 * then do the cacheline alignment, and finally emit the
11933 * MI_DISPLAY_FLIP.
11934 */
11935 ret = intel_ring_cacheline_align(req);
11936 if (ret)
11937 return ret;
11938
11939 ret = intel_ring_begin(req, len);
11940 if (ret)
11941 return ret;
11942
11943 /* Unmask the flip-done completion message. Note that the bspec says that
11944 * we should do this for both the BCS and RCS, and that we must not unmask
11945 * more than one flip event at any time (or ensure that one flip message
11946 * can be sent by waiting for flip-done prior to queueing new flips).
11947 * Experimentation says that BCS works despite DERRMR masking all
11948 * flip-done completion events and that unmasking all planes at once
11949 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11950 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11951 */
b5321f30
CW
11952 if (req->engine->id == RCS) {
11953 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
11954 intel_ring_emit_reg(ring, DERRMR);
11955 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
5a21b665
DV
11956 DERRMR_PIPEB_PRI_FLIP_DONE |
11957 DERRMR_PIPEC_PRI_FLIP_DONE));
5db94019 11958 if (IS_GEN8(dev_priv))
b5321f30 11959 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8 |
5a21b665
DV
11960 MI_SRM_LRM_GLOBAL_GTT);
11961 else
b5321f30 11962 intel_ring_emit(ring, MI_STORE_REGISTER_MEM |
5a21b665 11963 MI_SRM_LRM_GLOBAL_GTT);
b5321f30 11964 intel_ring_emit_reg(ring, DERRMR);
bde13ebd
CW
11965 intel_ring_emit(ring,
11966 i915_ggtt_offset(req->engine->scratch) + 256);
5db94019 11967 if (IS_GEN8(dev_priv)) {
b5321f30
CW
11968 intel_ring_emit(ring, 0);
11969 intel_ring_emit(ring, MI_NOOP);
5a21b665
DV
11970 }
11971 }
11972
b5321f30 11973 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
72618ebf 11974 intel_ring_emit(ring, fb->pitches[0] |
bae781b2 11975 intel_fb_modifier_to_tiling(fb->modifier));
b5321f30
CW
11976 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
11977 intel_ring_emit(ring, (MI_NOOP));
5a21b665
DV
11978
11979 return 0;
11980}
11981
11982static bool use_mmio_flip(struct intel_engine_cs *engine,
11983 struct drm_i915_gem_object *obj)
11984{
11985 /*
11986 * This is not being used for older platforms, because
11987 * non-availability of flip done interrupt forces us to use
11988 * CS flips. Older platforms derive flip done using some clever
11989 * tricks involving the flip_pending status bits and vblank irqs.
11990 * So using MMIO flips there would disrupt this mechanism.
11991 */
11992
11993 if (engine == NULL)
11994 return true;
11995
11996 if (INTEL_GEN(engine->i915) < 5)
11997 return false;
11998
11999 if (i915.use_mmio_flip < 0)
12000 return false;
12001 else if (i915.use_mmio_flip > 0)
12002 return true;
12003 else if (i915.enable_execlists)
12004 return true;
c37efb99 12005
d07f0e59 12006 return engine != i915_gem_object_last_write_engine(obj);
5a21b665
DV
12007}
12008
12009static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
12010 unsigned int rotation,
12011 struct intel_flip_work *work)
12012{
12013 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 12014 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
DV
12015 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
12016 const enum pipe pipe = intel_crtc->pipe;
d2196774 12017 u32 ctl, stride = skl_plane_stride(fb, 0, rotation);
5a21b665
DV
12018
12019 ctl = I915_READ(PLANE_CTL(pipe, 0));
12020 ctl &= ~PLANE_CTL_TILED_MASK;
bae781b2 12021 switch (fb->modifier) {
5a21b665
DV
12022 case DRM_FORMAT_MOD_NONE:
12023 break;
12024 case I915_FORMAT_MOD_X_TILED:
12025 ctl |= PLANE_CTL_TILED_X;
12026 break;
12027 case I915_FORMAT_MOD_Y_TILED:
12028 ctl |= PLANE_CTL_TILED_Y;
12029 break;
12030 case I915_FORMAT_MOD_Yf_TILED:
12031 ctl |= PLANE_CTL_TILED_YF;
12032 break;
12033 default:
bae781b2 12034 MISSING_CASE(fb->modifier);
5a21b665
DV
12035 }
12036
5a21b665
DV
12037 /*
12038 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
12039 * PLANE_SURF updates, the update is then guaranteed to be atomic.
12040 */
12041 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
12042 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
12043
12044 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
12045 POSTING_READ(PLANE_SURF(pipe, 0));
12046}
12047
12048static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
12049 struct intel_flip_work *work)
12050{
12051 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 12052 struct drm_i915_private *dev_priv = to_i915(dev);
72618ebf 12053 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
5a21b665
DV
12054 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
12055 u32 dspcntr;
12056
12057 dspcntr = I915_READ(reg);
12058
bae781b2 12059 if (fb->modifier == I915_FORMAT_MOD_X_TILED)
5a21b665
DV
12060 dspcntr |= DISPPLANE_TILED;
12061 else
12062 dspcntr &= ~DISPPLANE_TILED;
12063
12064 I915_WRITE(reg, dspcntr);
12065
12066 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
12067 POSTING_READ(DSPSURF(intel_crtc->plane));
12068}
12069
12070static void intel_mmio_flip_work_func(struct work_struct *w)
12071{
12072 struct intel_flip_work *work =
12073 container_of(w, struct intel_flip_work, mmio_work);
12074 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
12075 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
12076 struct intel_framebuffer *intel_fb =
12077 to_intel_framebuffer(crtc->base.primary->fb);
12078 struct drm_i915_gem_object *obj = intel_fb->obj;
12079
d07f0e59 12080 WARN_ON(i915_gem_object_wait(obj, 0, MAX_SCHEDULE_TIMEOUT, NULL) < 0);
5a21b665
DV
12081
12082 intel_pipe_update_start(crtc);
12083
12084 if (INTEL_GEN(dev_priv) >= 9)
12085 skl_do_mmio_flip(crtc, work->rotation, work);
12086 else
12087 /* use_mmio_flip() retricts MMIO flips to ilk+ */
12088 ilk_do_mmio_flip(crtc, work);
12089
12090 intel_pipe_update_end(crtc, work);
12091}
12092
12093static int intel_default_queue_flip(struct drm_device *dev,
12094 struct drm_crtc *crtc,
12095 struct drm_framebuffer *fb,
12096 struct drm_i915_gem_object *obj,
12097 struct drm_i915_gem_request *req,
12098 uint32_t flags)
12099{
12100 return -ENODEV;
12101}
12102
12103static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
12104 struct intel_crtc *intel_crtc,
12105 struct intel_flip_work *work)
12106{
12107 u32 addr, vblank;
12108
12109 if (!atomic_read(&work->pending))
12110 return false;
12111
12112 smp_rmb();
12113
12114 vblank = intel_crtc_get_vblank_counter(intel_crtc);
12115 if (work->flip_ready_vblank == 0) {
12116 if (work->flip_queued_req &&
f69a02c9 12117 !i915_gem_request_completed(work->flip_queued_req))
5a21b665
DV
12118 return false;
12119
12120 work->flip_ready_vblank = vblank;
12121 }
12122
12123 if (vblank - work->flip_ready_vblank < 3)
12124 return false;
12125
12126 /* Potential stall - if we see that the flip has happened,
12127 * assume a missed interrupt. */
12128 if (INTEL_GEN(dev_priv) >= 4)
12129 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
12130 else
12131 addr = I915_READ(DSPADDR(intel_crtc->plane));
12132
12133 /* There is a potential issue here with a false positive after a flip
12134 * to the same address. We could address this by checking for a
12135 * non-incrementing frame counter.
12136 */
12137 return addr == work->gtt_offset;
12138}
12139
12140void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
12141{
91c8a326 12142 struct drm_device *dev = &dev_priv->drm;
98187836 12143 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
5a21b665
DV
12144 struct intel_flip_work *work;
12145
12146 WARN_ON(!in_interrupt());
12147
12148 if (crtc == NULL)
12149 return;
12150
12151 spin_lock(&dev->event_lock);
e2af48c6 12152 work = crtc->flip_work;
5a21b665
DV
12153
12154 if (work != NULL && !is_mmio_work(work) &&
e2af48c6 12155 __pageflip_stall_check_cs(dev_priv, crtc, work)) {
5a21b665
DV
12156 WARN_ONCE(1,
12157 "Kicking stuck page flip: queued at %d, now %d\n",
e2af48c6
VS
12158 work->flip_queued_vblank, intel_crtc_get_vblank_counter(crtc));
12159 page_flip_completed(crtc);
5a21b665
DV
12160 work = NULL;
12161 }
12162
12163 if (work != NULL && !is_mmio_work(work) &&
e2af48c6 12164 intel_crtc_get_vblank_counter(crtc) - work->flip_queued_vblank > 1)
5a21b665
DV
12165 intel_queue_rps_boost_for_request(work->flip_queued_req);
12166 spin_unlock(&dev->event_lock);
12167}
12168
12169static int intel_crtc_page_flip(struct drm_crtc *crtc,
12170 struct drm_framebuffer *fb,
12171 struct drm_pending_vblank_event *event,
12172 uint32_t page_flip_flags)
12173{
12174 struct drm_device *dev = crtc->dev;
fac5e23e 12175 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
DV
12176 struct drm_framebuffer *old_fb = crtc->primary->fb;
12177 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
12178 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12179 struct drm_plane *primary = crtc->primary;
12180 enum pipe pipe = intel_crtc->pipe;
12181 struct intel_flip_work *work;
12182 struct intel_engine_cs *engine;
12183 bool mmio_flip;
8e637178 12184 struct drm_i915_gem_request *request;
058d88c4 12185 struct i915_vma *vma;
5a21b665
DV
12186 int ret;
12187
12188 /*
12189 * drm_mode_page_flip_ioctl() should already catch this, but double
12190 * check to be safe. In the future we may enable pageflipping from
12191 * a disabled primary plane.
12192 */
12193 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
12194 return -EBUSY;
12195
12196 /* Can't change pixel format via MI display flips. */
12197 if (fb->pixel_format != crtc->primary->fb->pixel_format)
12198 return -EINVAL;
12199
12200 /*
12201 * TILEOFF/LINOFF registers can't be changed via MI display flips.
12202 * Note that pitch changes could also affect these register.
12203 */
6315b5d3 12204 if (INTEL_GEN(dev_priv) > 3 &&
5a21b665
DV
12205 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
12206 fb->pitches[0] != crtc->primary->fb->pitches[0]))
12207 return -EINVAL;
12208
12209 if (i915_terminally_wedged(&dev_priv->gpu_error))
12210 goto out_hang;
12211
12212 work = kzalloc(sizeof(*work), GFP_KERNEL);
12213 if (work == NULL)
12214 return -ENOMEM;
12215
12216 work->event = event;
12217 work->crtc = crtc;
12218 work->old_fb = old_fb;
12219 INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
12220
12221 ret = drm_crtc_vblank_get(crtc);
12222 if (ret)
12223 goto free_work;
12224
12225 /* We borrow the event spin lock for protecting flip_work */
12226 spin_lock_irq(&dev->event_lock);
12227 if (intel_crtc->flip_work) {
12228 /* Before declaring the flip queue wedged, check if
12229 * the hardware completed the operation behind our backs.
12230 */
12231 if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
12232 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
12233 page_flip_completed(intel_crtc);
12234 } else {
12235 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
12236 spin_unlock_irq(&dev->event_lock);
12237
12238 drm_crtc_vblank_put(crtc);
12239 kfree(work);
12240 return -EBUSY;
12241 }
12242 }
12243 intel_crtc->flip_work = work;
12244 spin_unlock_irq(&dev->event_lock);
12245
12246 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
12247 flush_workqueue(dev_priv->wq);
12248
12249 /* Reference the objects for the scheduled work. */
12250 drm_framebuffer_reference(work->old_fb);
5a21b665
DV
12251
12252 crtc->primary->fb = fb;
12253 update_state_fb(crtc->primary);
faf68d92 12254
25dc556a 12255 work->pending_flip_obj = i915_gem_object_get(obj);
5a21b665
DV
12256
12257 ret = i915_mutex_lock_interruptible(dev);
12258 if (ret)
12259 goto cleanup;
12260
8af29b0c
CW
12261 intel_crtc->reset_count = i915_reset_count(&dev_priv->gpu_error);
12262 if (i915_reset_in_progress_or_wedged(&dev_priv->gpu_error)) {
5a21b665 12263 ret = -EIO;
ddbb271a 12264 goto unlock;
5a21b665
DV
12265 }
12266
12267 atomic_inc(&intel_crtc->unpin_work_count);
12268
9beb5fea 12269 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
5a21b665
DV
12270 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
12271
920a14b2 12272 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
3b3f1650 12273 engine = dev_priv->engine[BCS];
bae781b2 12274 if (fb->modifier != old_fb->modifier)
5a21b665
DV
12275 /* vlv: DISPLAY_FLIP fails to change tiling */
12276 engine = NULL;
fd6b8f43 12277 } else if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
3b3f1650 12278 engine = dev_priv->engine[BCS];
6315b5d3 12279 } else if (INTEL_GEN(dev_priv) >= 7) {
d07f0e59 12280 engine = i915_gem_object_last_write_engine(obj);
5a21b665 12281 if (engine == NULL || engine->id != RCS)
3b3f1650 12282 engine = dev_priv->engine[BCS];
5a21b665 12283 } else {
3b3f1650 12284 engine = dev_priv->engine[RCS];
5a21b665
DV
12285 }
12286
12287 mmio_flip = use_mmio_flip(engine, obj);
12288
058d88c4
CW
12289 vma = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
12290 if (IS_ERR(vma)) {
12291 ret = PTR_ERR(vma);
5a21b665 12292 goto cleanup_pending;
058d88c4 12293 }
5a21b665 12294
6687c906 12295 work->gtt_offset = intel_fb_gtt_offset(fb, primary->state->rotation);
5a21b665
DV
12296 work->gtt_offset += intel_crtc->dspaddr_offset;
12297 work->rotation = crtc->primary->state->rotation;
12298
1f061316
PZ
12299 /*
12300 * There's the potential that the next frame will not be compatible with
12301 * FBC, so we want to call pre_update() before the actual page flip.
12302 * The problem is that pre_update() caches some information about the fb
12303 * object, so we want to do this only after the object is pinned. Let's
12304 * be on the safe side and do this immediately before scheduling the
12305 * flip.
12306 */
12307 intel_fbc_pre_update(intel_crtc, intel_crtc->config,
12308 to_intel_plane_state(primary->state));
12309
5a21b665
DV
12310 if (mmio_flip) {
12311 INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
6277c8d0 12312 queue_work(system_unbound_wq, &work->mmio_work);
5a21b665 12313 } else {
8e637178
CW
12314 request = i915_gem_request_alloc(engine, engine->last_context);
12315 if (IS_ERR(request)) {
12316 ret = PTR_ERR(request);
12317 goto cleanup_unpin;
12318 }
12319
a2bc4695 12320 ret = i915_gem_request_await_object(request, obj, false);
8e637178
CW
12321 if (ret)
12322 goto cleanup_request;
12323
5a21b665
DV
12324 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
12325 page_flip_flags);
12326 if (ret)
8e637178 12327 goto cleanup_request;
5a21b665
DV
12328
12329 intel_mark_page_flip_active(intel_crtc, work);
12330
8e637178 12331 work->flip_queued_req = i915_gem_request_get(request);
5a21b665
DV
12332 i915_add_request_no_flush(request);
12333 }
12334
92117f0b 12335 i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
5a21b665
DV
12336 i915_gem_track_fb(intel_fb_obj(old_fb), obj,
12337 to_intel_plane(primary)->frontbuffer_bit);
12338 mutex_unlock(&dev->struct_mutex);
12339
5748b6a1 12340 intel_frontbuffer_flip_prepare(to_i915(dev),
5a21b665
DV
12341 to_intel_plane(primary)->frontbuffer_bit);
12342
12343 trace_i915_flip_request(intel_crtc->plane, obj);
12344
12345 return 0;
12346
8e637178
CW
12347cleanup_request:
12348 i915_add_request_no_flush(request);
5a21b665
DV
12349cleanup_unpin:
12350 intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
12351cleanup_pending:
5a21b665 12352 atomic_dec(&intel_crtc->unpin_work_count);
ddbb271a 12353unlock:
5a21b665
DV
12354 mutex_unlock(&dev->struct_mutex);
12355cleanup:
12356 crtc->primary->fb = old_fb;
12357 update_state_fb(crtc->primary);
12358
f0cd5182 12359 i915_gem_object_put(obj);
5a21b665
DV
12360 drm_framebuffer_unreference(work->old_fb);
12361
12362 spin_lock_irq(&dev->event_lock);
12363 intel_crtc->flip_work = NULL;
12364 spin_unlock_irq(&dev->event_lock);
12365
12366 drm_crtc_vblank_put(crtc);
12367free_work:
12368 kfree(work);
12369
12370 if (ret == -EIO) {
12371 struct drm_atomic_state *state;
12372 struct drm_plane_state *plane_state;
12373
12374out_hang:
12375 state = drm_atomic_state_alloc(dev);
12376 if (!state)
12377 return -ENOMEM;
12378 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
12379
12380retry:
12381 plane_state = drm_atomic_get_plane_state(state, primary);
12382 ret = PTR_ERR_OR_ZERO(plane_state);
12383 if (!ret) {
12384 drm_atomic_set_fb_for_plane(plane_state, fb);
12385
12386 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
12387 if (!ret)
12388 ret = drm_atomic_commit(state);
12389 }
12390
12391 if (ret == -EDEADLK) {
12392 drm_modeset_backoff(state->acquire_ctx);
12393 drm_atomic_state_clear(state);
12394 goto retry;
12395 }
12396
0853695c 12397 drm_atomic_state_put(state);
5a21b665
DV
12398
12399 if (ret == 0 && event) {
12400 spin_lock_irq(&dev->event_lock);
12401 drm_crtc_send_vblank_event(crtc, event);
12402 spin_unlock_irq(&dev->event_lock);
12403 }
12404 }
12405 return ret;
12406}
12407
12408
12409/**
12410 * intel_wm_need_update - Check whether watermarks need updating
12411 * @plane: drm plane
12412 * @state: new plane state
12413 *
12414 * Check current plane state versus the new one to determine whether
12415 * watermarks need to be recalculated.
12416 *
12417 * Returns true or false.
12418 */
12419static bool intel_wm_need_update(struct drm_plane *plane,
12420 struct drm_plane_state *state)
12421{
12422 struct intel_plane_state *new = to_intel_plane_state(state);
12423 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
12424
12425 /* Update watermarks on tiling or size changes. */
936e71e3 12426 if (new->base.visible != cur->base.visible)
5a21b665
DV
12427 return true;
12428
12429 if (!cur->base.fb || !new->base.fb)
12430 return false;
12431
bae781b2 12432 if (cur->base.fb->modifier != new->base.fb->modifier ||
5a21b665 12433 cur->base.rotation != new->base.rotation ||
936e71e3
VS
12434 drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
12435 drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
12436 drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
12437 drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
5a21b665
DV
12438 return true;
12439
12440 return false;
12441}
12442
12443static bool needs_scaling(struct intel_plane_state *state)
12444{
936e71e3
VS
12445 int src_w = drm_rect_width(&state->base.src) >> 16;
12446 int src_h = drm_rect_height(&state->base.src) >> 16;
12447 int dst_w = drm_rect_width(&state->base.dst);
12448 int dst_h = drm_rect_height(&state->base.dst);
5a21b665
DV
12449
12450 return (src_w != dst_w || src_h != dst_h);
12451}
d21fbe87 12452
da20eabd
ML
12453int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
12454 struct drm_plane_state *plane_state)
12455{
ab1d3a0e 12456 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
da20eabd
ML
12457 struct drm_crtc *crtc = crtc_state->crtc;
12458 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12459 struct drm_plane *plane = plane_state->plane;
12460 struct drm_device *dev = crtc->dev;
ed4a6a7c 12461 struct drm_i915_private *dev_priv = to_i915(dev);
da20eabd
ML
12462 struct intel_plane_state *old_plane_state =
12463 to_intel_plane_state(plane->state);
da20eabd
ML
12464 bool mode_changed = needs_modeset(crtc_state);
12465 bool was_crtc_enabled = crtc->state->active;
12466 bool is_crtc_enabled = crtc_state->active;
da20eabd
ML
12467 bool turn_off, turn_on, visible, was_visible;
12468 struct drm_framebuffer *fb = plane_state->fb;
78108b7c 12469 int ret;
da20eabd 12470
55b8f2a7 12471 if (INTEL_GEN(dev_priv) >= 9 && plane->type != DRM_PLANE_TYPE_CURSOR) {
da20eabd
ML
12472 ret = skl_update_scaler_plane(
12473 to_intel_crtc_state(crtc_state),
12474 to_intel_plane_state(plane_state));
12475 if (ret)
12476 return ret;
12477 }
12478
936e71e3
VS
12479 was_visible = old_plane_state->base.visible;
12480 visible = to_intel_plane_state(plane_state)->base.visible;
da20eabd
ML
12481
12482 if (!was_crtc_enabled && WARN_ON(was_visible))
12483 was_visible = false;
12484
35c08f43
ML
12485 /*
12486 * Visibility is calculated as if the crtc was on, but
12487 * after scaler setup everything depends on it being off
12488 * when the crtc isn't active.
f818ffea
VS
12489 *
12490 * FIXME this is wrong for watermarks. Watermarks should also
12491 * be computed as if the pipe would be active. Perhaps move
12492 * per-plane wm computation to the .check_plane() hook, and
12493 * only combine the results from all planes in the current place?
35c08f43
ML
12494 */
12495 if (!is_crtc_enabled)
936e71e3 12496 to_intel_plane_state(plane_state)->base.visible = visible = false;
da20eabd
ML
12497
12498 if (!was_visible && !visible)
12499 return 0;
12500
e8861675
ML
12501 if (fb != old_plane_state->base.fb)
12502 pipe_config->fb_changed = true;
12503
da20eabd
ML
12504 turn_off = was_visible && (!visible || mode_changed);
12505 turn_on = visible && (!was_visible || mode_changed);
12506
72660ce0 12507 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
78108b7c
VS
12508 intel_crtc->base.base.id,
12509 intel_crtc->base.name,
72660ce0
VS
12510 plane->base.id, plane->name,
12511 fb ? fb->base.id : -1);
da20eabd 12512
72660ce0
VS
12513 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
12514 plane->base.id, plane->name,
12515 was_visible, visible,
da20eabd
ML
12516 turn_off, turn_on, mode_changed);
12517
caed361d
VS
12518 if (turn_on) {
12519 pipe_config->update_wm_pre = true;
12520
12521 /* must disable cxsr around plane enable/disable */
12522 if (plane->type != DRM_PLANE_TYPE_CURSOR)
12523 pipe_config->disable_cxsr = true;
12524 } else if (turn_off) {
12525 pipe_config->update_wm_post = true;
92826fcd 12526
852eb00d 12527 /* must disable cxsr around plane enable/disable */
e8861675 12528 if (plane->type != DRM_PLANE_TYPE_CURSOR)
ab1d3a0e 12529 pipe_config->disable_cxsr = true;
852eb00d 12530 } else if (intel_wm_need_update(plane, plane_state)) {
caed361d
VS
12531 /* FIXME bollocks */
12532 pipe_config->update_wm_pre = true;
12533 pipe_config->update_wm_post = true;
852eb00d 12534 }
da20eabd 12535
ed4a6a7c 12536 /* Pre-gen9 platforms need two-step watermark updates */
caed361d 12537 if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
6315b5d3 12538 INTEL_GEN(dev_priv) < 9 && dev_priv->display.optimize_watermarks)
ed4a6a7c
MR
12539 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
12540
8be6ca85 12541 if (visible || was_visible)
cd202f69 12542 pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
a9ff8714 12543
31ae71fc
ML
12544 /*
12545 * WaCxSRDisabledForSpriteScaling:ivb
12546 *
12547 * cstate->update_wm was already set above, so this flag will
12548 * take effect when we commit and program watermarks.
12549 */
fd6b8f43 12550 if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev_priv) &&
31ae71fc
ML
12551 needs_scaling(to_intel_plane_state(plane_state)) &&
12552 !needs_scaling(old_plane_state))
12553 pipe_config->disable_lp_wm = true;
d21fbe87 12554
da20eabd
ML
12555 return 0;
12556}
12557
6d3a1ce7
ML
12558static bool encoders_cloneable(const struct intel_encoder *a,
12559 const struct intel_encoder *b)
12560{
12561 /* masks could be asymmetric, so check both ways */
12562 return a == b || (a->cloneable & (1 << b->type) &&
12563 b->cloneable & (1 << a->type));
12564}
12565
12566static bool check_single_encoder_cloning(struct drm_atomic_state *state,
12567 struct intel_crtc *crtc,
12568 struct intel_encoder *encoder)
12569{
12570 struct intel_encoder *source_encoder;
12571 struct drm_connector *connector;
12572 struct drm_connector_state *connector_state;
12573 int i;
12574
12575 for_each_connector_in_state(state, connector, connector_state, i) {
12576 if (connector_state->crtc != &crtc->base)
12577 continue;
12578
12579 source_encoder =
12580 to_intel_encoder(connector_state->best_encoder);
12581 if (!encoders_cloneable(encoder, source_encoder))
12582 return false;
12583 }
12584
12585 return true;
12586}
12587
6d3a1ce7
ML
12588static int intel_crtc_atomic_check(struct drm_crtc *crtc,
12589 struct drm_crtc_state *crtc_state)
12590{
cf5a15be 12591 struct drm_device *dev = crtc->dev;
fac5e23e 12592 struct drm_i915_private *dev_priv = to_i915(dev);
6d3a1ce7 12593 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cf5a15be
ML
12594 struct intel_crtc_state *pipe_config =
12595 to_intel_crtc_state(crtc_state);
6d3a1ce7 12596 struct drm_atomic_state *state = crtc_state->state;
4d20cd86 12597 int ret;
6d3a1ce7
ML
12598 bool mode_changed = needs_modeset(crtc_state);
12599
852eb00d 12600 if (mode_changed && !crtc_state->active)
caed361d 12601 pipe_config->update_wm_post = true;
eddfcbcd 12602
ad421372
ML
12603 if (mode_changed && crtc_state->enable &&
12604 dev_priv->display.crtc_compute_clock &&
8106ddbd 12605 !WARN_ON(pipe_config->shared_dpll)) {
ad421372
ML
12606 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
12607 pipe_config);
12608 if (ret)
12609 return ret;
12610 }
12611
82cf435b
LL
12612 if (crtc_state->color_mgmt_changed) {
12613 ret = intel_color_check(crtc, crtc_state);
12614 if (ret)
12615 return ret;
e7852a4b
LL
12616
12617 /*
12618 * Changing color management on Intel hardware is
12619 * handled as part of planes update.
12620 */
12621 crtc_state->planes_changed = true;
82cf435b
LL
12622 }
12623
e435d6e5 12624 ret = 0;
86c8bbbe 12625 if (dev_priv->display.compute_pipe_wm) {
e3bddded 12626 ret = dev_priv->display.compute_pipe_wm(pipe_config);
ed4a6a7c
MR
12627 if (ret) {
12628 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
12629 return ret;
12630 }
12631 }
12632
12633 if (dev_priv->display.compute_intermediate_wm &&
12634 !to_intel_atomic_state(state)->skip_intermediate_wm) {
12635 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
12636 return 0;
12637
12638 /*
12639 * Calculate 'intermediate' watermarks that satisfy both the
12640 * old state and the new state. We can program these
12641 * immediately.
12642 */
6315b5d3 12643 ret = dev_priv->display.compute_intermediate_wm(dev,
ed4a6a7c
MR
12644 intel_crtc,
12645 pipe_config);
12646 if (ret) {
12647 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
86c8bbbe 12648 return ret;
ed4a6a7c 12649 }
e3d5457c
VS
12650 } else if (dev_priv->display.compute_intermediate_wm) {
12651 if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
12652 pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
86c8bbbe
MR
12653 }
12654
6315b5d3 12655 if (INTEL_GEN(dev_priv) >= 9) {
e435d6e5
ML
12656 if (mode_changed)
12657 ret = skl_update_scaler_crtc(pipe_config);
12658
12659 if (!ret)
12660 ret = intel_atomic_setup_scalers(dev, intel_crtc,
12661 pipe_config);
12662 }
12663
12664 return ret;
6d3a1ce7
ML
12665}
12666
65b38e0d 12667static const struct drm_crtc_helper_funcs intel_helper_funcs = {
f6e5b160 12668 .mode_set_base_atomic = intel_pipe_set_base_atomic,
5a21b665
DV
12669 .atomic_begin = intel_begin_crtc_commit,
12670 .atomic_flush = intel_finish_crtc_commit,
6d3a1ce7 12671 .atomic_check = intel_crtc_atomic_check,
f6e5b160
CW
12672};
12673
d29b2f9d
ACO
12674static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
12675{
12676 struct intel_connector *connector;
12677
12678 for_each_intel_connector(dev, connector) {
8863dc7f
DV
12679 if (connector->base.state->crtc)
12680 drm_connector_unreference(&connector->base);
12681
d29b2f9d
ACO
12682 if (connector->base.encoder) {
12683 connector->base.state->best_encoder =
12684 connector->base.encoder;
12685 connector->base.state->crtc =
12686 connector->base.encoder->crtc;
8863dc7f
DV
12687
12688 drm_connector_reference(&connector->base);
d29b2f9d
ACO
12689 } else {
12690 connector->base.state->best_encoder = NULL;
12691 connector->base.state->crtc = NULL;
12692 }
12693 }
12694}
12695
050f7aeb 12696static void
eba905b2 12697connected_sink_compute_bpp(struct intel_connector *connector,
5cec258b 12698 struct intel_crtc_state *pipe_config)
050f7aeb 12699{
6a2a5c5d 12700 const struct drm_display_info *info = &connector->base.display_info;
050f7aeb
DV
12701 int bpp = pipe_config->pipe_bpp;
12702
12703 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
6a2a5c5d
VS
12704 connector->base.base.id,
12705 connector->base.name);
050f7aeb
DV
12706
12707 /* Don't use an invalid EDID bpc value */
6a2a5c5d 12708 if (info->bpc != 0 && info->bpc * 3 < bpp) {
050f7aeb 12709 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
6a2a5c5d
VS
12710 bpp, info->bpc * 3);
12711 pipe_config->pipe_bpp = info->bpc * 3;
050f7aeb
DV
12712 }
12713
196f954e 12714 /* Clamp bpp to 8 on screens without EDID 1.4 */
6a2a5c5d 12715 if (info->bpc == 0 && bpp > 24) {
196f954e
MK
12716 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
12717 bpp);
12718 pipe_config->pipe_bpp = 24;
050f7aeb
DV
12719 }
12720}
12721
4e53c2e0 12722static int
050f7aeb 12723compute_baseline_pipe_bpp(struct intel_crtc *crtc,
5cec258b 12724 struct intel_crtc_state *pipe_config)
4e53c2e0 12725{
9beb5fea 12726 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1486017f 12727 struct drm_atomic_state *state;
da3ced29
ACO
12728 struct drm_connector *connector;
12729 struct drm_connector_state *connector_state;
1486017f 12730 int bpp, i;
4e53c2e0 12731
9beb5fea
TU
12732 if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
12733 IS_CHERRYVIEW(dev_priv)))
4e53c2e0 12734 bpp = 10*3;
9beb5fea 12735 else if (INTEL_GEN(dev_priv) >= 5)
d328c9d7
DV
12736 bpp = 12*3;
12737 else
12738 bpp = 8*3;
12739
4e53c2e0 12740
4e53c2e0
DV
12741 pipe_config->pipe_bpp = bpp;
12742
1486017f
ACO
12743 state = pipe_config->base.state;
12744
4e53c2e0 12745 /* Clamp display bpp to EDID value */
da3ced29
ACO
12746 for_each_connector_in_state(state, connector, connector_state, i) {
12747 if (connector_state->crtc != &crtc->base)
4e53c2e0
DV
12748 continue;
12749
da3ced29
ACO
12750 connected_sink_compute_bpp(to_intel_connector(connector),
12751 pipe_config);
4e53c2e0
DV
12752 }
12753
12754 return bpp;
12755}
12756
644db711
DV
12757static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
12758{
12759 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
12760 "type: 0x%x flags: 0x%x\n",
1342830c 12761 mode->crtc_clock,
644db711
DV
12762 mode->crtc_hdisplay, mode->crtc_hsync_start,
12763 mode->crtc_hsync_end, mode->crtc_htotal,
12764 mode->crtc_vdisplay, mode->crtc_vsync_start,
12765 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
12766}
12767
f6982332
TU
12768static inline void
12769intel_dump_m_n_config(struct intel_crtc_state *pipe_config, char *id,
a4309657 12770 unsigned int lane_count, struct intel_link_m_n *m_n)
f6982332 12771{
a4309657
TU
12772 DRM_DEBUG_KMS("%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12773 id, lane_count,
f6982332
TU
12774 m_n->gmch_m, m_n->gmch_n,
12775 m_n->link_m, m_n->link_n, m_n->tu);
12776}
12777
c0b03411 12778static void intel_dump_pipe_config(struct intel_crtc *crtc,
5cec258b 12779 struct intel_crtc_state *pipe_config,
c0b03411
DV
12780 const char *context)
12781{
6a60cd87 12782 struct drm_device *dev = crtc->base.dev;
4f8036a2 12783 struct drm_i915_private *dev_priv = to_i915(dev);
6a60cd87
CK
12784 struct drm_plane *plane;
12785 struct intel_plane *intel_plane;
12786 struct intel_plane_state *state;
12787 struct drm_framebuffer *fb;
12788
66766e4f
TU
12789 DRM_DEBUG_KMS("[CRTC:%d:%s]%s\n",
12790 crtc->base.base.id, crtc->base.name, context);
c0b03411 12791
2c89429e
TU
12792 DRM_DEBUG_KMS("cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
12793 transcoder_name(pipe_config->cpu_transcoder),
c0b03411 12794 pipe_config->pipe_bpp, pipe_config->dither);
a4309657
TU
12795
12796 if (pipe_config->has_pch_encoder)
12797 intel_dump_m_n_config(pipe_config, "fdi",
12798 pipe_config->fdi_lanes,
12799 &pipe_config->fdi_m_n);
f6982332
TU
12800
12801 if (intel_crtc_has_dp_encoder(pipe_config)) {
a4309657
TU
12802 intel_dump_m_n_config(pipe_config, "dp m_n",
12803 pipe_config->lane_count, &pipe_config->dp_m_n);
d806e682
TU
12804 if (pipe_config->has_drrs)
12805 intel_dump_m_n_config(pipe_config, "dp m2_n2",
12806 pipe_config->lane_count,
12807 &pipe_config->dp_m2_n2);
f6982332 12808 }
b95af8be 12809
55072d19 12810 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
2c89429e 12811 pipe_config->has_audio, pipe_config->has_infoframe);
55072d19 12812
c0b03411 12813 DRM_DEBUG_KMS("requested mode:\n");
2d112de7 12814 drm_mode_debug_printmodeline(&pipe_config->base.mode);
c0b03411 12815 DRM_DEBUG_KMS("adjusted mode:\n");
2d112de7
ACO
12816 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12817 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
2c89429e
TU
12818 DRM_DEBUG_KMS("port clock: %d, pipe src size: %dx%d\n",
12819 pipe_config->port_clock,
37327abd 12820 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
dd2f616d
TU
12821
12822 if (INTEL_GEN(dev_priv) >= 9)
12823 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12824 crtc->num_scalers,
12825 pipe_config->scaler_state.scaler_users,
12826 pipe_config->scaler_state.scaler_id);
a74f8375
TU
12827
12828 if (HAS_GMCH_DISPLAY(dev_priv))
12829 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12830 pipe_config->gmch_pfit.control,
12831 pipe_config->gmch_pfit.pgm_ratios,
12832 pipe_config->gmch_pfit.lvds_border_bits);
12833 else
12834 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
12835 pipe_config->pch_pfit.pos,
12836 pipe_config->pch_pfit.size,
08c4d7fc 12837 enableddisabled(pipe_config->pch_pfit.enabled));
a74f8375 12838
2c89429e
TU
12839 DRM_DEBUG_KMS("ips: %i, double wide: %i\n",
12840 pipe_config->ips_enabled, pipe_config->double_wide);
6a60cd87 12841
cc3f90f0 12842 if (IS_GEN9_LP(dev_priv)) {
c856052a 12843 DRM_DEBUG_KMS("dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
415ff0f6 12844 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
c8453338 12845 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
415ff0f6 12846 pipe_config->dpll_hw_state.ebb0,
05712c15 12847 pipe_config->dpll_hw_state.ebb4,
415ff0f6
TU
12848 pipe_config->dpll_hw_state.pll0,
12849 pipe_config->dpll_hw_state.pll1,
12850 pipe_config->dpll_hw_state.pll2,
12851 pipe_config->dpll_hw_state.pll3,
12852 pipe_config->dpll_hw_state.pll6,
12853 pipe_config->dpll_hw_state.pll8,
05712c15 12854 pipe_config->dpll_hw_state.pll9,
c8453338 12855 pipe_config->dpll_hw_state.pll10,
415ff0f6 12856 pipe_config->dpll_hw_state.pcsdw12);
0853723b 12857 } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
c856052a 12858 DRM_DEBUG_KMS("dpll_hw_state: "
415ff0f6 12859 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
415ff0f6
TU
12860 pipe_config->dpll_hw_state.ctrl1,
12861 pipe_config->dpll_hw_state.cfgcr1,
12862 pipe_config->dpll_hw_state.cfgcr2);
4f8036a2 12863 } else if (HAS_DDI(dev_priv)) {
c856052a 12864 DRM_DEBUG_KMS("dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
00490c22
ML
12865 pipe_config->dpll_hw_state.wrpll,
12866 pipe_config->dpll_hw_state.spll);
415ff0f6
TU
12867 } else {
12868 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12869 "fp0: 0x%x, fp1: 0x%x\n",
12870 pipe_config->dpll_hw_state.dpll,
12871 pipe_config->dpll_hw_state.dpll_md,
12872 pipe_config->dpll_hw_state.fp0,
12873 pipe_config->dpll_hw_state.fp1);
12874 }
12875
6a60cd87
CK
12876 DRM_DEBUG_KMS("planes on this crtc\n");
12877 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
b3c11ac2 12878 struct drm_format_name_buf format_name;
6a60cd87
CK
12879 intel_plane = to_intel_plane(plane);
12880 if (intel_plane->pipe != crtc->pipe)
12881 continue;
12882
12883 state = to_intel_plane_state(plane->state);
12884 fb = state->base.fb;
12885 if (!fb) {
1d577e02
VS
12886 DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
12887 plane->base.id, plane->name, state->scaler_id);
6a60cd87
CK
12888 continue;
12889 }
12890
dd2f616d
TU
12891 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d, fb = %ux%u format = %s\n",
12892 plane->base.id, plane->name,
b3c11ac2
EE
12893 fb->base.id, fb->width, fb->height,
12894 drm_get_format_name(fb->pixel_format, &format_name));
dd2f616d
TU
12895 if (INTEL_GEN(dev_priv) >= 9)
12896 DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
12897 state->scaler_id,
12898 state->base.src.x1 >> 16,
12899 state->base.src.y1 >> 16,
12900 drm_rect_width(&state->base.src) >> 16,
12901 drm_rect_height(&state->base.src) >> 16,
12902 state->base.dst.x1, state->base.dst.y1,
12903 drm_rect_width(&state->base.dst),
12904 drm_rect_height(&state->base.dst));
6a60cd87 12905 }
c0b03411
DV
12906}
12907
5448a00d 12908static bool check_digital_port_conflicts(struct drm_atomic_state *state)
00f0b378 12909{
5448a00d 12910 struct drm_device *dev = state->dev;
da3ced29 12911 struct drm_connector *connector;
00f0b378 12912 unsigned int used_ports = 0;
477321e0 12913 unsigned int used_mst_ports = 0;
00f0b378
VS
12914
12915 /*
12916 * Walk the connector list instead of the encoder
12917 * list to detect the problem on ddi platforms
12918 * where there's just one encoder per digital port.
12919 */
0bff4858
VS
12920 drm_for_each_connector(connector, dev) {
12921 struct drm_connector_state *connector_state;
12922 struct intel_encoder *encoder;
12923
12924 connector_state = drm_atomic_get_existing_connector_state(state, connector);
12925 if (!connector_state)
12926 connector_state = connector->state;
12927
5448a00d 12928 if (!connector_state->best_encoder)
00f0b378
VS
12929 continue;
12930
5448a00d
ACO
12931 encoder = to_intel_encoder(connector_state->best_encoder);
12932
12933 WARN_ON(!connector_state->crtc);
00f0b378
VS
12934
12935 switch (encoder->type) {
12936 unsigned int port_mask;
12937 case INTEL_OUTPUT_UNKNOWN:
4f8036a2 12938 if (WARN_ON(!HAS_DDI(to_i915(dev))))
00f0b378 12939 break;
cca0502b 12940 case INTEL_OUTPUT_DP:
00f0b378
VS
12941 case INTEL_OUTPUT_HDMI:
12942 case INTEL_OUTPUT_EDP:
12943 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12944
12945 /* the same port mustn't appear more than once */
12946 if (used_ports & port_mask)
12947 return false;
12948
12949 used_ports |= port_mask;
477321e0
VS
12950 break;
12951 case INTEL_OUTPUT_DP_MST:
12952 used_mst_ports |=
12953 1 << enc_to_mst(&encoder->base)->primary->port;
12954 break;
00f0b378
VS
12955 default:
12956 break;
12957 }
12958 }
12959
477321e0
VS
12960 /* can't mix MST and SST/HDMI on the same port */
12961 if (used_ports & used_mst_ports)
12962 return false;
12963
00f0b378
VS
12964 return true;
12965}
12966
83a57153
ACO
12967static void
12968clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12969{
12970 struct drm_crtc_state tmp_state;
663a3640 12971 struct intel_crtc_scaler_state scaler_state;
4978cc93 12972 struct intel_dpll_hw_state dpll_hw_state;
8106ddbd 12973 struct intel_shared_dpll *shared_dpll;
c4e2d043 12974 bool force_thru;
83a57153 12975
7546a384
ACO
12976 /* FIXME: before the switch to atomic started, a new pipe_config was
12977 * kzalloc'd. Code that depends on any field being zero should be
12978 * fixed, so that the crtc_state can be safely duplicated. For now,
12979 * only fields that are know to not cause problems are preserved. */
12980
83a57153 12981 tmp_state = crtc_state->base;
663a3640 12982 scaler_state = crtc_state->scaler_state;
4978cc93
ACO
12983 shared_dpll = crtc_state->shared_dpll;
12984 dpll_hw_state = crtc_state->dpll_hw_state;
c4e2d043 12985 force_thru = crtc_state->pch_pfit.force_thru;
4978cc93 12986
83a57153 12987 memset(crtc_state, 0, sizeof *crtc_state);
4978cc93 12988
83a57153 12989 crtc_state->base = tmp_state;
663a3640 12990 crtc_state->scaler_state = scaler_state;
4978cc93
ACO
12991 crtc_state->shared_dpll = shared_dpll;
12992 crtc_state->dpll_hw_state = dpll_hw_state;
c4e2d043 12993 crtc_state->pch_pfit.force_thru = force_thru;
83a57153
ACO
12994}
12995
548ee15b 12996static int
b8cecdf5 12997intel_modeset_pipe_config(struct drm_crtc *crtc,
b359283a 12998 struct intel_crtc_state *pipe_config)
ee7b9f93 12999{
b359283a 13000 struct drm_atomic_state *state = pipe_config->base.state;
7758a113 13001 struct intel_encoder *encoder;
da3ced29 13002 struct drm_connector *connector;
0b901879 13003 struct drm_connector_state *connector_state;
d328c9d7 13004 int base_bpp, ret = -EINVAL;
0b901879 13005 int i;
e29c22c0 13006 bool retry = true;
ee7b9f93 13007
83a57153 13008 clear_intel_crtc_state(pipe_config);
7758a113 13009
e143a21c
DV
13010 pipe_config->cpu_transcoder =
13011 (enum transcoder) to_intel_crtc(crtc)->pipe;
b8cecdf5 13012
2960bc9c
ID
13013 /*
13014 * Sanitize sync polarity flags based on requested ones. If neither
13015 * positive or negative polarity is requested, treat this as meaning
13016 * negative polarity.
13017 */
2d112de7 13018 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 13019 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
2d112de7 13020 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
2960bc9c 13021
2d112de7 13022 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 13023 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
2d112de7 13024 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
2960bc9c 13025
d328c9d7
DV
13026 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
13027 pipe_config);
13028 if (base_bpp < 0)
4e53c2e0
DV
13029 goto fail;
13030
e41a56be
VS
13031 /*
13032 * Determine the real pipe dimensions. Note that stereo modes can
13033 * increase the actual pipe size due to the frame doubling and
13034 * insertion of additional space for blanks between the frame. This
13035 * is stored in the crtc timings. We use the requested mode to do this
13036 * computation to clearly distinguish it from the adjusted mode, which
13037 * can be changed by the connectors in the below retry loop.
13038 */
2d112de7 13039 drm_crtc_get_hv_timing(&pipe_config->base.mode,
ecb7e16b
GP
13040 &pipe_config->pipe_src_w,
13041 &pipe_config->pipe_src_h);
e41a56be 13042
253c84c8
VS
13043 for_each_connector_in_state(state, connector, connector_state, i) {
13044 if (connector_state->crtc != crtc)
13045 continue;
13046
13047 encoder = to_intel_encoder(connector_state->best_encoder);
13048
e25148d0
VS
13049 if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
13050 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
13051 goto fail;
13052 }
13053
253c84c8
VS
13054 /*
13055 * Determine output_types before calling the .compute_config()
13056 * hooks so that the hooks can use this information safely.
13057 */
13058 pipe_config->output_types |= 1 << encoder->type;
13059 }
13060
e29c22c0 13061encoder_retry:
ef1b460d 13062 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 13063 pipe_config->port_clock = 0;
ef1b460d 13064 pipe_config->pixel_multiplier = 1;
ff9a6750 13065
135c81b8 13066 /* Fill in default crtc timings, allow encoders to overwrite them. */
2d112de7
ACO
13067 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
13068 CRTC_STEREO_DOUBLE);
135c81b8 13069
7758a113
DV
13070 /* Pass our mode to the connectors and the CRTC to give them a chance to
13071 * adjust it according to limitations or connector properties, and also
13072 * a chance to reject the mode entirely.
47f1c6c9 13073 */
da3ced29 13074 for_each_connector_in_state(state, connector, connector_state, i) {
0b901879 13075 if (connector_state->crtc != crtc)
7758a113 13076 continue;
7ae89233 13077
0b901879
ACO
13078 encoder = to_intel_encoder(connector_state->best_encoder);
13079
0a478c27 13080 if (!(encoder->compute_config(encoder, pipe_config, connector_state))) {
efea6e8e 13081 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
DV
13082 goto fail;
13083 }
ee7b9f93 13084 }
47f1c6c9 13085
ff9a6750
DV
13086 /* Set default port clock if not overwritten by the encoder. Needs to be
13087 * done afterwards in case the encoder adjusts the mode. */
13088 if (!pipe_config->port_clock)
2d112de7 13089 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
241bfc38 13090 * pipe_config->pixel_multiplier;
ff9a6750 13091
a43f6e0f 13092 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 13093 if (ret < 0) {
7758a113
DV
13094 DRM_DEBUG_KMS("CRTC fixup failed\n");
13095 goto fail;
ee7b9f93 13096 }
e29c22c0
DV
13097
13098 if (ret == RETRY) {
13099 if (WARN(!retry, "loop in pipe configuration computation\n")) {
13100 ret = -EINVAL;
13101 goto fail;
13102 }
13103
13104 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
13105 retry = false;
13106 goto encoder_retry;
13107 }
13108
e8fa4270
DV
13109 /* Dithering seems to not pass-through bits correctly when it should, so
13110 * only enable it on 6bpc panels. */
13111 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
62f0ace5 13112 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
d328c9d7 13113 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
4e53c2e0 13114
7758a113 13115fail:
548ee15b 13116 return ret;
ee7b9f93 13117}
47f1c6c9 13118
ea9d758d 13119static void
4740b0f2 13120intel_modeset_update_crtc_state(struct drm_atomic_state *state)
ea9d758d 13121{
0a9ab303
ACO
13122 struct drm_crtc *crtc;
13123 struct drm_crtc_state *crtc_state;
8a75d157 13124 int i;
ea9d758d 13125
7668851f 13126 /* Double check state. */
8a75d157 13127 for_each_crtc_in_state(state, crtc, crtc_state, i) {
3cb480bc 13128 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
fc467a22
ML
13129
13130 /* Update hwmode for vblank functions */
13131 if (crtc->state->active)
13132 crtc->hwmode = crtc->state->adjusted_mode;
13133 else
13134 crtc->hwmode.crtc_clock = 0;
61067a5e
ML
13135
13136 /*
13137 * Update legacy state to satisfy fbc code. This can
13138 * be removed when fbc uses the atomic state.
13139 */
13140 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
13141 struct drm_plane_state *plane_state = crtc->primary->state;
13142
13143 crtc->primary->fb = plane_state->fb;
13144 crtc->x = plane_state->src_x >> 16;
13145 crtc->y = plane_state->src_y >> 16;
13146 }
ea9d758d 13147 }
ea9d758d
DV
13148}
13149
3bd26263 13150static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 13151{
3bd26263 13152 int diff;
f1f644dc
JB
13153
13154 if (clock1 == clock2)
13155 return true;
13156
13157 if (!clock1 || !clock2)
13158 return false;
13159
13160 diff = abs(clock1 - clock2);
13161
13162 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
13163 return true;
13164
13165 return false;
13166}
13167
cfb23ed6
ML
13168static bool
13169intel_compare_m_n(unsigned int m, unsigned int n,
13170 unsigned int m2, unsigned int n2,
13171 bool exact)
13172{
13173 if (m == m2 && n == n2)
13174 return true;
13175
13176 if (exact || !m || !n || !m2 || !n2)
13177 return false;
13178
13179 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
13180
31d10b57
ML
13181 if (n > n2) {
13182 while (n > n2) {
cfb23ed6
ML
13183 m2 <<= 1;
13184 n2 <<= 1;
13185 }
31d10b57
ML
13186 } else if (n < n2) {
13187 while (n < n2) {
cfb23ed6
ML
13188 m <<= 1;
13189 n <<= 1;
13190 }
13191 }
13192
31d10b57
ML
13193 if (n != n2)
13194 return false;
13195
13196 return intel_fuzzy_clock_check(m, m2);
cfb23ed6
ML
13197}
13198
13199static bool
13200intel_compare_link_m_n(const struct intel_link_m_n *m_n,
13201 struct intel_link_m_n *m2_n2,
13202 bool adjust)
13203{
13204 if (m_n->tu == m2_n2->tu &&
13205 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
13206 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
13207 intel_compare_m_n(m_n->link_m, m_n->link_n,
13208 m2_n2->link_m, m2_n2->link_n, !adjust)) {
13209 if (adjust)
13210 *m2_n2 = *m_n;
13211
13212 return true;
13213 }
13214
13215 return false;
13216}
13217
0e8ffe1b 13218static bool
6315b5d3 13219intel_pipe_config_compare(struct drm_i915_private *dev_priv,
5cec258b 13220 struct intel_crtc_state *current_config,
cfb23ed6
ML
13221 struct intel_crtc_state *pipe_config,
13222 bool adjust)
0e8ffe1b 13223{
cfb23ed6
ML
13224 bool ret = true;
13225
13226#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
13227 do { \
13228 if (!adjust) \
13229 DRM_ERROR(fmt, ##__VA_ARGS__); \
13230 else \
13231 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
13232 } while (0)
13233
66e985c0
DV
13234#define PIPE_CONF_CHECK_X(name) \
13235 if (current_config->name != pipe_config->name) { \
cfb23ed6 13236 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
66e985c0
DV
13237 "(expected 0x%08x, found 0x%08x)\n", \
13238 current_config->name, \
13239 pipe_config->name); \
cfb23ed6 13240 ret = false; \
66e985c0
DV
13241 }
13242
08a24034
DV
13243#define PIPE_CONF_CHECK_I(name) \
13244 if (current_config->name != pipe_config->name) { \
cfb23ed6 13245 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
08a24034
DV
13246 "(expected %i, found %i)\n", \
13247 current_config->name, \
13248 pipe_config->name); \
cfb23ed6
ML
13249 ret = false; \
13250 }
13251
8106ddbd
ACO
13252#define PIPE_CONF_CHECK_P(name) \
13253 if (current_config->name != pipe_config->name) { \
13254 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
13255 "(expected %p, found %p)\n", \
13256 current_config->name, \
13257 pipe_config->name); \
13258 ret = false; \
13259 }
13260
cfb23ed6
ML
13261#define PIPE_CONF_CHECK_M_N(name) \
13262 if (!intel_compare_link_m_n(&current_config->name, \
13263 &pipe_config->name,\
13264 adjust)) { \
13265 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
13266 "(expected tu %i gmch %i/%i link %i/%i, " \
13267 "found tu %i, gmch %i/%i link %i/%i)\n", \
13268 current_config->name.tu, \
13269 current_config->name.gmch_m, \
13270 current_config->name.gmch_n, \
13271 current_config->name.link_m, \
13272 current_config->name.link_n, \
13273 pipe_config->name.tu, \
13274 pipe_config->name.gmch_m, \
13275 pipe_config->name.gmch_n, \
13276 pipe_config->name.link_m, \
13277 pipe_config->name.link_n); \
13278 ret = false; \
13279 }
13280
55c561a7
DV
13281/* This is required for BDW+ where there is only one set of registers for
13282 * switching between high and low RR.
13283 * This macro can be used whenever a comparison has to be made between one
13284 * hw state and multiple sw state variables.
13285 */
cfb23ed6
ML
13286#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
13287 if (!intel_compare_link_m_n(&current_config->name, \
13288 &pipe_config->name, adjust) && \
13289 !intel_compare_link_m_n(&current_config->alt_name, \
13290 &pipe_config->name, adjust)) { \
13291 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
13292 "(expected tu %i gmch %i/%i link %i/%i, " \
13293 "or tu %i gmch %i/%i link %i/%i, " \
13294 "found tu %i, gmch %i/%i link %i/%i)\n", \
13295 current_config->name.tu, \
13296 current_config->name.gmch_m, \
13297 current_config->name.gmch_n, \
13298 current_config->name.link_m, \
13299 current_config->name.link_n, \
13300 current_config->alt_name.tu, \
13301 current_config->alt_name.gmch_m, \
13302 current_config->alt_name.gmch_n, \
13303 current_config->alt_name.link_m, \
13304 current_config->alt_name.link_n, \
13305 pipe_config->name.tu, \
13306 pipe_config->name.gmch_m, \
13307 pipe_config->name.gmch_n, \
13308 pipe_config->name.link_m, \
13309 pipe_config->name.link_n); \
13310 ret = false; \
88adfff1
DV
13311 }
13312
1bd1bd80
DV
13313#define PIPE_CONF_CHECK_FLAGS(name, mask) \
13314 if ((current_config->name ^ pipe_config->name) & (mask)) { \
cfb23ed6 13315 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
1bd1bd80
DV
13316 "(expected %i, found %i)\n", \
13317 current_config->name & (mask), \
13318 pipe_config->name & (mask)); \
cfb23ed6 13319 ret = false; \
1bd1bd80
DV
13320 }
13321
5e550656
VS
13322#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
13323 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
cfb23ed6 13324 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
5e550656
VS
13325 "(expected %i, found %i)\n", \
13326 current_config->name, \
13327 pipe_config->name); \
cfb23ed6 13328 ret = false; \
5e550656
VS
13329 }
13330
bb760063
DV
13331#define PIPE_CONF_QUIRK(quirk) \
13332 ((current_config->quirks | pipe_config->quirks) & (quirk))
13333
eccb140b
DV
13334 PIPE_CONF_CHECK_I(cpu_transcoder);
13335
08a24034
DV
13336 PIPE_CONF_CHECK_I(has_pch_encoder);
13337 PIPE_CONF_CHECK_I(fdi_lanes);
cfb23ed6 13338 PIPE_CONF_CHECK_M_N(fdi_m_n);
08a24034 13339
90a6b7b0 13340 PIPE_CONF_CHECK_I(lane_count);
95a7a2ae 13341 PIPE_CONF_CHECK_X(lane_lat_optim_mask);
b95af8be 13342
6315b5d3 13343 if (INTEL_GEN(dev_priv) < 8) {
cfb23ed6
ML
13344 PIPE_CONF_CHECK_M_N(dp_m_n);
13345
cfb23ed6
ML
13346 if (current_config->has_drrs)
13347 PIPE_CONF_CHECK_M_N(dp_m2_n2);
13348 } else
13349 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
eb14cb74 13350
253c84c8 13351 PIPE_CONF_CHECK_X(output_types);
a65347ba 13352
2d112de7
ACO
13353 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
13354 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
13355 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
13356 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
13357 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
13358 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
1bd1bd80 13359
2d112de7
ACO
13360 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
13361 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
13362 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
13363 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
13364 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
13365 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
1bd1bd80 13366
c93f54cf 13367 PIPE_CONF_CHECK_I(pixel_multiplier);
6897b4b5 13368 PIPE_CONF_CHECK_I(has_hdmi_sink);
772c2a51 13369 if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
920a14b2 13370 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
b5a9fa09 13371 PIPE_CONF_CHECK_I(limited_color_range);
e43823ec 13372 PIPE_CONF_CHECK_I(has_infoframe);
6c49f241 13373
9ed109a7
DV
13374 PIPE_CONF_CHECK_I(has_audio);
13375
2d112de7 13376 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
1bd1bd80
DV
13377 DRM_MODE_FLAG_INTERLACE);
13378
bb760063 13379 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
2d112de7 13380 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 13381 DRM_MODE_FLAG_PHSYNC);
2d112de7 13382 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 13383 DRM_MODE_FLAG_NHSYNC);
2d112de7 13384 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 13385 DRM_MODE_FLAG_PVSYNC);
2d112de7 13386 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063
DV
13387 DRM_MODE_FLAG_NVSYNC);
13388 }
045ac3b5 13389
333b8ca8 13390 PIPE_CONF_CHECK_X(gmch_pfit.control);
e2ff2d4a 13391 /* pfit ratios are autocomputed by the hw on gen4+ */
6315b5d3 13392 if (INTEL_GEN(dev_priv) < 4)
7f7d8dd6 13393 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
333b8ca8 13394 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
9953599b 13395
bfd16b2a
ML
13396 if (!adjust) {
13397 PIPE_CONF_CHECK_I(pipe_src_w);
13398 PIPE_CONF_CHECK_I(pipe_src_h);
13399
13400 PIPE_CONF_CHECK_I(pch_pfit.enabled);
13401 if (current_config->pch_pfit.enabled) {
13402 PIPE_CONF_CHECK_X(pch_pfit.pos);
13403 PIPE_CONF_CHECK_X(pch_pfit.size);
13404 }
2fa2fe9a 13405
7aefe2b5
ML
13406 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
13407 }
a1b2278e 13408
e59150dc 13409 /* BDW+ don't expose a synchronous way to read the state */
772c2a51 13410 if (IS_HASWELL(dev_priv))
e59150dc 13411 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 13412
282740f7
VS
13413 PIPE_CONF_CHECK_I(double_wide);
13414
8106ddbd 13415 PIPE_CONF_CHECK_P(shared_dpll);
66e985c0 13416 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 13417 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
DV
13418 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
13419 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
d452c5b6 13420 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
00490c22 13421 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
3f4cd19f
DL
13422 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
13423 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
13424 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
c0d43d62 13425
47eacbab
VS
13426 PIPE_CONF_CHECK_X(dsi_pll.ctrl);
13427 PIPE_CONF_CHECK_X(dsi_pll.div);
13428
9beb5fea 13429 if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
42571aef
VS
13430 PIPE_CONF_CHECK_I(pipe_bpp);
13431
2d112de7 13432 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
a9a7e98a 13433 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 13434
66e985c0 13435#undef PIPE_CONF_CHECK_X
08a24034 13436#undef PIPE_CONF_CHECK_I
8106ddbd 13437#undef PIPE_CONF_CHECK_P
1bd1bd80 13438#undef PIPE_CONF_CHECK_FLAGS
5e550656 13439#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 13440#undef PIPE_CONF_QUIRK
cfb23ed6 13441#undef INTEL_ERR_OR_DBG_KMS
88adfff1 13442
cfb23ed6 13443 return ret;
0e8ffe1b
DV
13444}
13445
e3b247da
VS
13446static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
13447 const struct intel_crtc_state *pipe_config)
13448{
13449 if (pipe_config->has_pch_encoder) {
21a727b3 13450 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
e3b247da
VS
13451 &pipe_config->fdi_m_n);
13452 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
13453
13454 /*
13455 * FDI already provided one idea for the dotclock.
13456 * Yell if the encoder disagrees.
13457 */
13458 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
13459 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
13460 fdi_dotclock, dotclock);
13461 }
13462}
13463
c0ead703
ML
13464static void verify_wm_state(struct drm_crtc *crtc,
13465 struct drm_crtc_state *new_state)
08db6652 13466{
6315b5d3 13467 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
08db6652 13468 struct skl_ddb_allocation hw_ddb, *sw_ddb;
3de8a14c 13469 struct skl_pipe_wm hw_wm, *sw_wm;
13470 struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
13471 struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
e7c84544
ML
13472 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13473 const enum pipe pipe = intel_crtc->pipe;
3de8a14c 13474 int plane, level, max_level = ilk_wm_max_level(dev_priv);
08db6652 13475
6315b5d3 13476 if (INTEL_GEN(dev_priv) < 9 || !new_state->active)
08db6652
DL
13477 return;
13478
3de8a14c 13479 skl_pipe_wm_get_hw_state(crtc, &hw_wm);
03af79e0 13480 sw_wm = &to_intel_crtc_state(new_state)->wm.skl.optimal;
3de8a14c 13481
08db6652
DL
13482 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
13483 sw_ddb = &dev_priv->wm.skl_hw.ddb;
13484
e7c84544 13485 /* planes */
8b364b41 13486 for_each_universal_plane(dev_priv, pipe, plane) {
3de8a14c 13487 hw_plane_wm = &hw_wm.planes[plane];
13488 sw_plane_wm = &sw_wm->planes[plane];
08db6652 13489
3de8a14c 13490 /* Watermarks */
13491 for (level = 0; level <= max_level; level++) {
13492 if (skl_wm_level_equals(&hw_plane_wm->wm[level],
13493 &sw_plane_wm->wm[level]))
13494 continue;
13495
13496 DRM_ERROR("mismatch in WM pipe %c plane %d level %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
13497 pipe_name(pipe), plane + 1, level,
13498 sw_plane_wm->wm[level].plane_en,
13499 sw_plane_wm->wm[level].plane_res_b,
13500 sw_plane_wm->wm[level].plane_res_l,
13501 hw_plane_wm->wm[level].plane_en,
13502 hw_plane_wm->wm[level].plane_res_b,
13503 hw_plane_wm->wm[level].plane_res_l);
13504 }
08db6652 13505
3de8a14c 13506 if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
13507 &sw_plane_wm->trans_wm)) {
13508 DRM_ERROR("mismatch in trans WM pipe %c plane %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
13509 pipe_name(pipe), plane + 1,
13510 sw_plane_wm->trans_wm.plane_en,
13511 sw_plane_wm->trans_wm.plane_res_b,
13512 sw_plane_wm->trans_wm.plane_res_l,
13513 hw_plane_wm->trans_wm.plane_en,
13514 hw_plane_wm->trans_wm.plane_res_b,
13515 hw_plane_wm->trans_wm.plane_res_l);
13516 }
13517
13518 /* DDB */
13519 hw_ddb_entry = &hw_ddb.plane[pipe][plane];
13520 sw_ddb_entry = &sw_ddb->plane[pipe][plane];
13521
13522 if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
faccd994 13523 DRM_ERROR("mismatch in DDB state pipe %c plane %d (expected (%u,%u), found (%u,%u))\n",
3de8a14c 13524 pipe_name(pipe), plane + 1,
13525 sw_ddb_entry->start, sw_ddb_entry->end,
13526 hw_ddb_entry->start, hw_ddb_entry->end);
13527 }
e7c84544 13528 }
08db6652 13529
27082493
L
13530 /*
13531 * cursor
13532 * If the cursor plane isn't active, we may not have updated it's ddb
13533 * allocation. In that case since the ddb allocation will be updated
13534 * once the plane becomes visible, we can skip this check
13535 */
13536 if (intel_crtc->cursor_addr) {
3de8a14c 13537 hw_plane_wm = &hw_wm.planes[PLANE_CURSOR];
13538 sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
13539
13540 /* Watermarks */
13541 for (level = 0; level <= max_level; level++) {
13542 if (skl_wm_level_equals(&hw_plane_wm->wm[level],
13543 &sw_plane_wm->wm[level]))
13544 continue;
13545
13546 DRM_ERROR("mismatch in WM pipe %c cursor level %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
13547 pipe_name(pipe), level,
13548 sw_plane_wm->wm[level].plane_en,
13549 sw_plane_wm->wm[level].plane_res_b,
13550 sw_plane_wm->wm[level].plane_res_l,
13551 hw_plane_wm->wm[level].plane_en,
13552 hw_plane_wm->wm[level].plane_res_b,
13553 hw_plane_wm->wm[level].plane_res_l);
13554 }
13555
13556 if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
13557 &sw_plane_wm->trans_wm)) {
13558 DRM_ERROR("mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
13559 pipe_name(pipe),
13560 sw_plane_wm->trans_wm.plane_en,
13561 sw_plane_wm->trans_wm.plane_res_b,
13562 sw_plane_wm->trans_wm.plane_res_l,
13563 hw_plane_wm->trans_wm.plane_en,
13564 hw_plane_wm->trans_wm.plane_res_b,
13565 hw_plane_wm->trans_wm.plane_res_l);
13566 }
13567
13568 /* DDB */
13569 hw_ddb_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
13570 sw_ddb_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
27082493 13571
3de8a14c 13572 if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
faccd994 13573 DRM_ERROR("mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
27082493 13574 pipe_name(pipe),
3de8a14c 13575 sw_ddb_entry->start, sw_ddb_entry->end,
13576 hw_ddb_entry->start, hw_ddb_entry->end);
27082493 13577 }
08db6652
DL
13578 }
13579}
13580
91d1b4bd 13581static void
677100ce
ML
13582verify_connector_state(struct drm_device *dev,
13583 struct drm_atomic_state *state,
13584 struct drm_crtc *crtc)
8af6cf88 13585{
35dd3c64 13586 struct drm_connector *connector;
677100ce
ML
13587 struct drm_connector_state *old_conn_state;
13588 int i;
8af6cf88 13589
677100ce 13590 for_each_connector_in_state(state, connector, old_conn_state, i) {
35dd3c64
ML
13591 struct drm_encoder *encoder = connector->encoder;
13592 struct drm_connector_state *state = connector->state;
ad3c558f 13593
e7c84544
ML
13594 if (state->crtc != crtc)
13595 continue;
13596
5a21b665 13597 intel_connector_verify_state(to_intel_connector(connector));
8af6cf88 13598
ad3c558f 13599 I915_STATE_WARN(state->best_encoder != encoder,
35dd3c64 13600 "connector's atomic encoder doesn't match legacy encoder\n");
8af6cf88 13601 }
91d1b4bd
DV
13602}
13603
13604static void
c0ead703 13605verify_encoder_state(struct drm_device *dev)
91d1b4bd
DV
13606{
13607 struct intel_encoder *encoder;
13608 struct intel_connector *connector;
8af6cf88 13609
b2784e15 13610 for_each_intel_encoder(dev, encoder) {
8af6cf88 13611 bool enabled = false;
4d20cd86 13612 enum pipe pipe;
8af6cf88
DV
13613
13614 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
13615 encoder->base.base.id,
8e329a03 13616 encoder->base.name);
8af6cf88 13617
3a3371ff 13618 for_each_intel_connector(dev, connector) {
4d20cd86 13619 if (connector->base.state->best_encoder != &encoder->base)
8af6cf88
DV
13620 continue;
13621 enabled = true;
ad3c558f
ML
13622
13623 I915_STATE_WARN(connector->base.state->crtc !=
13624 encoder->base.crtc,
13625 "connector's crtc doesn't match encoder crtc\n");
8af6cf88 13626 }
0e32b39c 13627
e2c719b7 13628 I915_STATE_WARN(!!encoder->base.crtc != enabled,
8af6cf88
DV
13629 "encoder's enabled state mismatch "
13630 "(expected %i, found %i)\n",
13631 !!encoder->base.crtc, enabled);
7c60d198
ML
13632
13633 if (!encoder->base.crtc) {
4d20cd86 13634 bool active;
7c60d198 13635
4d20cd86
ML
13636 active = encoder->get_hw_state(encoder, &pipe);
13637 I915_STATE_WARN(active,
13638 "encoder detached but still enabled on pipe %c.\n",
13639 pipe_name(pipe));
7c60d198 13640 }
8af6cf88 13641 }
91d1b4bd
DV
13642}
13643
13644static void
c0ead703
ML
13645verify_crtc_state(struct drm_crtc *crtc,
13646 struct drm_crtc_state *old_crtc_state,
13647 struct drm_crtc_state *new_crtc_state)
91d1b4bd 13648{
e7c84544 13649 struct drm_device *dev = crtc->dev;
fac5e23e 13650 struct drm_i915_private *dev_priv = to_i915(dev);
91d1b4bd 13651 struct intel_encoder *encoder;
e7c84544
ML
13652 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13653 struct intel_crtc_state *pipe_config, *sw_config;
13654 struct drm_atomic_state *old_state;
13655 bool active;
045ac3b5 13656
e7c84544 13657 old_state = old_crtc_state->state;
ec2dc6a0 13658 __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
e7c84544
ML
13659 pipe_config = to_intel_crtc_state(old_crtc_state);
13660 memset(pipe_config, 0, sizeof(*pipe_config));
13661 pipe_config->base.crtc = crtc;
13662 pipe_config->base.state = old_state;
8af6cf88 13663
78108b7c 13664 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
8af6cf88 13665
e7c84544 13666 active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
d62cf62a 13667
e7c84544
ML
13668 /* hw state is inconsistent with the pipe quirk */
13669 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
13670 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
13671 active = new_crtc_state->active;
6c49f241 13672
e7c84544
ML
13673 I915_STATE_WARN(new_crtc_state->active != active,
13674 "crtc active state doesn't match with hw state "
13675 "(expected %i, found %i)\n", new_crtc_state->active, active);
0e8ffe1b 13676
e7c84544
ML
13677 I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
13678 "transitional active state does not match atomic hw state "
13679 "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
4d20cd86 13680
e7c84544
ML
13681 for_each_encoder_on_crtc(dev, crtc, encoder) {
13682 enum pipe pipe;
4d20cd86 13683
e7c84544
ML
13684 active = encoder->get_hw_state(encoder, &pipe);
13685 I915_STATE_WARN(active != new_crtc_state->active,
13686 "[ENCODER:%i] active %i with crtc active %i\n",
13687 encoder->base.base.id, active, new_crtc_state->active);
4d20cd86 13688
e7c84544
ML
13689 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
13690 "Encoder connected to wrong pipe %c\n",
13691 pipe_name(pipe));
4d20cd86 13692
253c84c8
VS
13693 if (active) {
13694 pipe_config->output_types |= 1 << encoder->type;
e7c84544 13695 encoder->get_config(encoder, pipe_config);
253c84c8 13696 }
e7c84544 13697 }
53d9f4e9 13698
e7c84544
ML
13699 if (!new_crtc_state->active)
13700 return;
cfb23ed6 13701
e7c84544 13702 intel_pipe_config_sanity_check(dev_priv, pipe_config);
e3b247da 13703
e7c84544 13704 sw_config = to_intel_crtc_state(crtc->state);
6315b5d3 13705 if (!intel_pipe_config_compare(dev_priv, sw_config,
e7c84544
ML
13706 pipe_config, false)) {
13707 I915_STATE_WARN(1, "pipe state doesn't match!\n");
13708 intel_dump_pipe_config(intel_crtc, pipe_config,
13709 "[hw state]");
13710 intel_dump_pipe_config(intel_crtc, sw_config,
13711 "[sw state]");
8af6cf88
DV
13712 }
13713}
13714
91d1b4bd 13715static void
c0ead703
ML
13716verify_single_dpll_state(struct drm_i915_private *dev_priv,
13717 struct intel_shared_dpll *pll,
13718 struct drm_crtc *crtc,
13719 struct drm_crtc_state *new_state)
91d1b4bd 13720{
91d1b4bd 13721 struct intel_dpll_hw_state dpll_hw_state;
e7c84544
ML
13722 unsigned crtc_mask;
13723 bool active;
5358901f 13724
e7c84544 13725 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
5358901f 13726
e7c84544 13727 DRM_DEBUG_KMS("%s\n", pll->name);
5358901f 13728
e7c84544 13729 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
5358901f 13730
e7c84544
ML
13731 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
13732 I915_STATE_WARN(!pll->on && pll->active_mask,
13733 "pll in active use but not on in sw tracking\n");
13734 I915_STATE_WARN(pll->on && !pll->active_mask,
13735 "pll is on but not used by any active crtc\n");
13736 I915_STATE_WARN(pll->on != active,
13737 "pll on state mismatch (expected %i, found %i)\n",
13738 pll->on, active);
13739 }
5358901f 13740
e7c84544 13741 if (!crtc) {
2dd66ebd 13742 I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
e7c84544
ML
13743 "more active pll users than references: %x vs %x\n",
13744 pll->active_mask, pll->config.crtc_mask);
5358901f 13745
e7c84544
ML
13746 return;
13747 }
13748
13749 crtc_mask = 1 << drm_crtc_index(crtc);
13750
13751 if (new_state->active)
13752 I915_STATE_WARN(!(pll->active_mask & crtc_mask),
13753 "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
13754 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
13755 else
13756 I915_STATE_WARN(pll->active_mask & crtc_mask,
13757 "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
13758 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
2dd66ebd 13759
e7c84544
ML
13760 I915_STATE_WARN(!(pll->config.crtc_mask & crtc_mask),
13761 "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
13762 crtc_mask, pll->config.crtc_mask);
66e985c0 13763
e7c84544
ML
13764 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state,
13765 &dpll_hw_state,
13766 sizeof(dpll_hw_state)),
13767 "pll hw state mismatch\n");
13768}
13769
13770static void
c0ead703
ML
13771verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
13772 struct drm_crtc_state *old_crtc_state,
13773 struct drm_crtc_state *new_crtc_state)
e7c84544 13774{
fac5e23e 13775 struct drm_i915_private *dev_priv = to_i915(dev);
e7c84544
ML
13776 struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
13777 struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
13778
13779 if (new_state->shared_dpll)
c0ead703 13780 verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
e7c84544
ML
13781
13782 if (old_state->shared_dpll &&
13783 old_state->shared_dpll != new_state->shared_dpll) {
13784 unsigned crtc_mask = 1 << drm_crtc_index(crtc);
13785 struct intel_shared_dpll *pll = old_state->shared_dpll;
13786
13787 I915_STATE_WARN(pll->active_mask & crtc_mask,
13788 "pll active mismatch (didn't expect pipe %c in active mask)\n",
13789 pipe_name(drm_crtc_index(crtc)));
13790 I915_STATE_WARN(pll->config.crtc_mask & crtc_mask,
13791 "pll enabled crtcs mismatch (found %x in enabled mask)\n",
13792 pipe_name(drm_crtc_index(crtc)));
5358901f 13793 }
8af6cf88
DV
13794}
13795
e7c84544 13796static void
c0ead703 13797intel_modeset_verify_crtc(struct drm_crtc *crtc,
677100ce
ML
13798 struct drm_atomic_state *state,
13799 struct drm_crtc_state *old_state,
13800 struct drm_crtc_state *new_state)
e7c84544 13801{
5a21b665
DV
13802 if (!needs_modeset(new_state) &&
13803 !to_intel_crtc_state(new_state)->update_pipe)
13804 return;
13805
c0ead703 13806 verify_wm_state(crtc, new_state);
677100ce 13807 verify_connector_state(crtc->dev, state, crtc);
c0ead703
ML
13808 verify_crtc_state(crtc, old_state, new_state);
13809 verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
e7c84544
ML
13810}
13811
13812static void
c0ead703 13813verify_disabled_dpll_state(struct drm_device *dev)
e7c84544 13814{
fac5e23e 13815 struct drm_i915_private *dev_priv = to_i915(dev);
e7c84544
ML
13816 int i;
13817
13818 for (i = 0; i < dev_priv->num_shared_dpll; i++)
c0ead703 13819 verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
e7c84544
ML
13820}
13821
13822static void
677100ce
ML
13823intel_modeset_verify_disabled(struct drm_device *dev,
13824 struct drm_atomic_state *state)
e7c84544 13825{
c0ead703 13826 verify_encoder_state(dev);
677100ce 13827 verify_connector_state(dev, state, NULL);
c0ead703 13828 verify_disabled_dpll_state(dev);
e7c84544
ML
13829}
13830
80715b2f
VS
13831static void update_scanline_offset(struct intel_crtc *crtc)
13832{
4f8036a2 13833 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
80715b2f
VS
13834
13835 /*
13836 * The scanline counter increments at the leading edge of hsync.
13837 *
13838 * On most platforms it starts counting from vtotal-1 on the
13839 * first active line. That means the scanline counter value is
13840 * always one less than what we would expect. Ie. just after
13841 * start of vblank, which also occurs at start of hsync (on the
13842 * last active line), the scanline counter will read vblank_start-1.
13843 *
13844 * On gen2 the scanline counter starts counting from 1 instead
13845 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
13846 * to keep the value positive), instead of adding one.
13847 *
13848 * On HSW+ the behaviour of the scanline counter depends on the output
13849 * type. For DP ports it behaves like most other platforms, but on HDMI
13850 * there's an extra 1 line difference. So we need to add two instead of
13851 * one to the value.
13852 */
4f8036a2 13853 if (IS_GEN2(dev_priv)) {
124abe07 13854 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
80715b2f
VS
13855 int vtotal;
13856
124abe07
VS
13857 vtotal = adjusted_mode->crtc_vtotal;
13858 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
80715b2f
VS
13859 vtotal /= 2;
13860
13861 crtc->scanline_offset = vtotal - 1;
4f8036a2 13862 } else if (HAS_DDI(dev_priv) &&
2d84d2b3 13863 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
80715b2f
VS
13864 crtc->scanline_offset = 2;
13865 } else
13866 crtc->scanline_offset = 1;
13867}
13868
ad421372 13869static void intel_modeset_clear_plls(struct drm_atomic_state *state)
ed6739ef 13870{
225da59b 13871 struct drm_device *dev = state->dev;
ed6739ef 13872 struct drm_i915_private *dev_priv = to_i915(dev);
ad421372 13873 struct intel_shared_dpll_config *shared_dpll = NULL;
0a9ab303
ACO
13874 struct drm_crtc *crtc;
13875 struct drm_crtc_state *crtc_state;
0a9ab303 13876 int i;
ed6739ef
ACO
13877
13878 if (!dev_priv->display.crtc_compute_clock)
ad421372 13879 return;
ed6739ef 13880
0a9ab303 13881 for_each_crtc_in_state(state, crtc, crtc_state, i) {
fb1a38a9 13882 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8106ddbd
ACO
13883 struct intel_shared_dpll *old_dpll =
13884 to_intel_crtc_state(crtc->state)->shared_dpll;
0a9ab303 13885
fb1a38a9 13886 if (!needs_modeset(crtc_state))
225da59b
ACO
13887 continue;
13888
8106ddbd 13889 to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
fb1a38a9 13890
8106ddbd 13891 if (!old_dpll)
fb1a38a9 13892 continue;
0a9ab303 13893
ad421372
ML
13894 if (!shared_dpll)
13895 shared_dpll = intel_atomic_get_shared_dpll_state(state);
ed6739ef 13896
8106ddbd 13897 intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
ad421372 13898 }
ed6739ef
ACO
13899}
13900
99d736a2
ML
13901/*
13902 * This implements the workaround described in the "notes" section of the mode
13903 * set sequence documentation. When going from no pipes or single pipe to
13904 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13905 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13906 */
13907static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13908{
13909 struct drm_crtc_state *crtc_state;
13910 struct intel_crtc *intel_crtc;
13911 struct drm_crtc *crtc;
13912 struct intel_crtc_state *first_crtc_state = NULL;
13913 struct intel_crtc_state *other_crtc_state = NULL;
13914 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13915 int i;
13916
13917 /* look at all crtc's that are going to be enabled in during modeset */
13918 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13919 intel_crtc = to_intel_crtc(crtc);
13920
13921 if (!crtc_state->active || !needs_modeset(crtc_state))
13922 continue;
13923
13924 if (first_crtc_state) {
13925 other_crtc_state = to_intel_crtc_state(crtc_state);
13926 break;
13927 } else {
13928 first_crtc_state = to_intel_crtc_state(crtc_state);
13929 first_pipe = intel_crtc->pipe;
13930 }
13931 }
13932
13933 /* No workaround needed? */
13934 if (!first_crtc_state)
13935 return 0;
13936
13937 /* w/a possibly needed, check how many crtc's are already enabled. */
13938 for_each_intel_crtc(state->dev, intel_crtc) {
13939 struct intel_crtc_state *pipe_config;
13940
13941 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13942 if (IS_ERR(pipe_config))
13943 return PTR_ERR(pipe_config);
13944
13945 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13946
13947 if (!pipe_config->base.active ||
13948 needs_modeset(&pipe_config->base))
13949 continue;
13950
13951 /* 2 or more enabled crtcs means no need for w/a */
13952 if (enabled_pipe != INVALID_PIPE)
13953 return 0;
13954
13955 enabled_pipe = intel_crtc->pipe;
13956 }
13957
13958 if (enabled_pipe != INVALID_PIPE)
13959 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13960 else if (other_crtc_state)
13961 other_crtc_state->hsw_workaround_pipe = first_pipe;
13962
13963 return 0;
13964}
13965
8d96561a
VS
13966static int intel_lock_all_pipes(struct drm_atomic_state *state)
13967{
13968 struct drm_crtc *crtc;
13969
13970 /* Add all pipes to the state */
13971 for_each_crtc(state->dev, crtc) {
13972 struct drm_crtc_state *crtc_state;
13973
13974 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13975 if (IS_ERR(crtc_state))
13976 return PTR_ERR(crtc_state);
13977 }
13978
13979 return 0;
13980}
13981
27c329ed
ML
13982static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13983{
13984 struct drm_crtc *crtc;
27c329ed 13985
8d96561a
VS
13986 /*
13987 * Add all pipes to the state, and force
13988 * a modeset on all the active ones.
13989 */
27c329ed 13990 for_each_crtc(state->dev, crtc) {
9780aad5
VS
13991 struct drm_crtc_state *crtc_state;
13992 int ret;
13993
27c329ed
ML
13994 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13995 if (IS_ERR(crtc_state))
13996 return PTR_ERR(crtc_state);
13997
13998 if (!crtc_state->active || needs_modeset(crtc_state))
13999 continue;
14000
14001 crtc_state->mode_changed = true;
14002
14003 ret = drm_atomic_add_affected_connectors(state, crtc);
14004 if (ret)
9780aad5 14005 return ret;
27c329ed
ML
14006
14007 ret = drm_atomic_add_affected_planes(state, crtc);
14008 if (ret)
9780aad5 14009 return ret;
27c329ed
ML
14010 }
14011
9780aad5 14012 return 0;
27c329ed
ML
14013}
14014
c347a676 14015static int intel_modeset_checks(struct drm_atomic_state *state)
054518dd 14016{
565602d7 14017 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 14018 struct drm_i915_private *dev_priv = to_i915(state->dev);
565602d7
ML
14019 struct drm_crtc *crtc;
14020 struct drm_crtc_state *crtc_state;
14021 int ret = 0, i;
054518dd 14022
b359283a
ML
14023 if (!check_digital_port_conflicts(state)) {
14024 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
14025 return -EINVAL;
14026 }
14027
565602d7
ML
14028 intel_state->modeset = true;
14029 intel_state->active_crtcs = dev_priv->active_crtcs;
14030
14031 for_each_crtc_in_state(state, crtc, crtc_state, i) {
14032 if (crtc_state->active)
14033 intel_state->active_crtcs |= 1 << i;
14034 else
14035 intel_state->active_crtcs &= ~(1 << i);
8b4a7d05
MR
14036
14037 if (crtc_state->active != crtc->state->active)
14038 intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
565602d7
ML
14039 }
14040
054518dd
ACO
14041 /*
14042 * See if the config requires any additional preparation, e.g.
14043 * to adjust global state with pipes off. We need to do this
14044 * here so we can get the modeset_pipe updated config for the new
14045 * mode set on this crtc. For other crtcs we need to use the
14046 * adjusted_mode bits in the crtc directly.
14047 */
27c329ed 14048 if (dev_priv->display.modeset_calc_cdclk) {
c89e39f3 14049 if (!intel_state->cdclk_pll_vco)
63911d72 14050 intel_state->cdclk_pll_vco = dev_priv->cdclk_pll.vco;
b2045352
VS
14051 if (!intel_state->cdclk_pll_vco)
14052 intel_state->cdclk_pll_vco = dev_priv->skl_preferred_vco_freq;
c89e39f3 14053
27c329ed 14054 ret = dev_priv->display.modeset_calc_cdclk(state);
c89e39f3
CT
14055 if (ret < 0)
14056 return ret;
27c329ed 14057
8d96561a
VS
14058 /*
14059 * Writes to dev_priv->atomic_cdclk_freq must protected by
14060 * holding all the crtc locks, even if we don't end up
14061 * touching the hardware
14062 */
14063 if (intel_state->cdclk != dev_priv->atomic_cdclk_freq) {
14064 ret = intel_lock_all_pipes(state);
14065 if (ret < 0)
14066 return ret;
14067 }
14068
14069 /* All pipes must be switched off while we change the cdclk. */
c89e39f3 14070 if (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
8d96561a 14071 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco) {
27c329ed 14072 ret = intel_modeset_all_pipes(state);
8d96561a
VS
14073 if (ret < 0)
14074 return ret;
14075 }
e8788cbc
ML
14076
14077 DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
14078 intel_state->cdclk, intel_state->dev_cdclk);
e0ca7a6b 14079 } else {
1a617b77 14080 to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
e0ca7a6b 14081 }
054518dd 14082
ad421372 14083 intel_modeset_clear_plls(state);
054518dd 14084
565602d7 14085 if (IS_HASWELL(dev_priv))
ad421372 14086 return haswell_mode_set_planes_workaround(state);
99d736a2 14087
ad421372 14088 return 0;
c347a676
ACO
14089}
14090
aa363136
MR
14091/*
14092 * Handle calculation of various watermark data at the end of the atomic check
14093 * phase. The code here should be run after the per-crtc and per-plane 'check'
14094 * handlers to ensure that all derived state has been updated.
14095 */
55994c2c 14096static int calc_watermark_data(struct drm_atomic_state *state)
aa363136
MR
14097{
14098 struct drm_device *dev = state->dev;
98d39494 14099 struct drm_i915_private *dev_priv = to_i915(dev);
98d39494
MR
14100
14101 /* Is there platform-specific watermark information to calculate? */
14102 if (dev_priv->display.compute_global_watermarks)
55994c2c
MR
14103 return dev_priv->display.compute_global_watermarks(state);
14104
14105 return 0;
aa363136
MR
14106}
14107
74c090b1
ML
14108/**
14109 * intel_atomic_check - validate state object
14110 * @dev: drm device
14111 * @state: state to validate
14112 */
14113static int intel_atomic_check(struct drm_device *dev,
14114 struct drm_atomic_state *state)
c347a676 14115{
dd8b3bdb 14116 struct drm_i915_private *dev_priv = to_i915(dev);
aa363136 14117 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
c347a676
ACO
14118 struct drm_crtc *crtc;
14119 struct drm_crtc_state *crtc_state;
14120 int ret, i;
61333b60 14121 bool any_ms = false;
c347a676 14122
74c090b1 14123 ret = drm_atomic_helper_check_modeset(dev, state);
054518dd
ACO
14124 if (ret)
14125 return ret;
14126
c347a676 14127 for_each_crtc_in_state(state, crtc, crtc_state, i) {
cfb23ed6
ML
14128 struct intel_crtc_state *pipe_config =
14129 to_intel_crtc_state(crtc_state);
1ed51de9
DV
14130
14131 /* Catch I915_MODE_FLAG_INHERITED */
14132 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
14133 crtc_state->mode_changed = true;
cfb23ed6 14134
af4a879e 14135 if (!needs_modeset(crtc_state))
c347a676
ACO
14136 continue;
14137
af4a879e
DV
14138 if (!crtc_state->enable) {
14139 any_ms = true;
cfb23ed6 14140 continue;
af4a879e 14141 }
cfb23ed6 14142
26495481
DV
14143 /* FIXME: For only active_changed we shouldn't need to do any
14144 * state recomputation at all. */
14145
1ed51de9
DV
14146 ret = drm_atomic_add_affected_connectors(state, crtc);
14147 if (ret)
14148 return ret;
b359283a 14149
cfb23ed6 14150 ret = intel_modeset_pipe_config(crtc, pipe_config);
25aa1c39
ML
14151 if (ret) {
14152 intel_dump_pipe_config(to_intel_crtc(crtc),
14153 pipe_config, "[failed]");
c347a676 14154 return ret;
25aa1c39 14155 }
c347a676 14156
73831236 14157 if (i915.fastboot &&
6315b5d3 14158 intel_pipe_config_compare(dev_priv,
cfb23ed6 14159 to_intel_crtc_state(crtc->state),
1ed51de9 14160 pipe_config, true)) {
26495481 14161 crtc_state->mode_changed = false;
bfd16b2a 14162 to_intel_crtc_state(crtc_state)->update_pipe = true;
26495481
DV
14163 }
14164
af4a879e 14165 if (needs_modeset(crtc_state))
26495481 14166 any_ms = true;
cfb23ed6 14167
af4a879e
DV
14168 ret = drm_atomic_add_affected_planes(state, crtc);
14169 if (ret)
14170 return ret;
61333b60 14171
26495481
DV
14172 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
14173 needs_modeset(crtc_state) ?
14174 "[modeset]" : "[fastset]");
c347a676
ACO
14175 }
14176
61333b60
ML
14177 if (any_ms) {
14178 ret = intel_modeset_checks(state);
14179
14180 if (ret)
14181 return ret;
e0ca7a6b
VS
14182 } else {
14183 intel_state->cdclk = dev_priv->atomic_cdclk_freq;
14184 }
76305b1a 14185
dd8b3bdb 14186 ret = drm_atomic_helper_check_planes(dev, state);
aa363136
MR
14187 if (ret)
14188 return ret;
14189
f51be2e0 14190 intel_fbc_choose_crtc(dev_priv, state);
55994c2c 14191 return calc_watermark_data(state);
054518dd
ACO
14192}
14193
5008e874 14194static int intel_atomic_prepare_commit(struct drm_device *dev,
d07f0e59 14195 struct drm_atomic_state *state)
5008e874 14196{
fac5e23e 14197 struct drm_i915_private *dev_priv = to_i915(dev);
5008e874
ML
14198 struct drm_crtc_state *crtc_state;
14199 struct drm_crtc *crtc;
14200 int i, ret;
14201
5a21b665
DV
14202 for_each_crtc_in_state(state, crtc, crtc_state, i) {
14203 if (state->legacy_cursor_update)
a6747b73
ML
14204 continue;
14205
5a21b665
DV
14206 ret = intel_crtc_wait_for_pending_flips(crtc);
14207 if (ret)
14208 return ret;
5008e874 14209
5a21b665
DV
14210 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
14211 flush_workqueue(dev_priv->wq);
d55dbd06
ML
14212 }
14213
f935675f
ML
14214 ret = mutex_lock_interruptible(&dev->struct_mutex);
14215 if (ret)
14216 return ret;
14217
5008e874 14218 ret = drm_atomic_helper_prepare_planes(dev, state);
f7e5838b 14219 mutex_unlock(&dev->struct_mutex);
7580d774 14220
5008e874
ML
14221 return ret;
14222}
14223
a2991414
ML
14224u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
14225{
14226 struct drm_device *dev = crtc->base.dev;
14227
14228 if (!dev->max_vblank_count)
14229 return drm_accurate_vblank_count(&crtc->base);
14230
14231 return dev->driver->get_vblank_counter(dev, crtc->pipe);
14232}
14233
5a21b665
DV
14234static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
14235 struct drm_i915_private *dev_priv,
14236 unsigned crtc_mask)
e8861675 14237{
5a21b665
DV
14238 unsigned last_vblank_count[I915_MAX_PIPES];
14239 enum pipe pipe;
14240 int ret;
e8861675 14241
5a21b665
DV
14242 if (!crtc_mask)
14243 return;
e8861675 14244
5a21b665 14245 for_each_pipe(dev_priv, pipe) {
98187836
VS
14246 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
14247 pipe);
e8861675 14248
5a21b665 14249 if (!((1 << pipe) & crtc_mask))
e8861675
ML
14250 continue;
14251
e2af48c6 14252 ret = drm_crtc_vblank_get(&crtc->base);
5a21b665
DV
14253 if (WARN_ON(ret != 0)) {
14254 crtc_mask &= ~(1 << pipe);
14255 continue;
e8861675
ML
14256 }
14257
e2af48c6 14258 last_vblank_count[pipe] = drm_crtc_vblank_count(&crtc->base);
e8861675
ML
14259 }
14260
5a21b665 14261 for_each_pipe(dev_priv, pipe) {
98187836
VS
14262 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
14263 pipe);
5a21b665 14264 long lret;
e8861675 14265
5a21b665
DV
14266 if (!((1 << pipe) & crtc_mask))
14267 continue;
d55dbd06 14268
5a21b665
DV
14269 lret = wait_event_timeout(dev->vblank[pipe].queue,
14270 last_vblank_count[pipe] !=
e2af48c6 14271 drm_crtc_vblank_count(&crtc->base),
5a21b665 14272 msecs_to_jiffies(50));
d55dbd06 14273
5a21b665 14274 WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
d55dbd06 14275
e2af48c6 14276 drm_crtc_vblank_put(&crtc->base);
d55dbd06
ML
14277 }
14278}
14279
5a21b665 14280static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
a6747b73 14281{
5a21b665
DV
14282 /* fb updated, need to unpin old fb */
14283 if (crtc_state->fb_changed)
14284 return true;
a6747b73 14285
5a21b665
DV
14286 /* wm changes, need vblank before final wm's */
14287 if (crtc_state->update_wm_post)
14288 return true;
a6747b73 14289
5a21b665
DV
14290 /*
14291 * cxsr is re-enabled after vblank.
14292 * This is already handled by crtc_state->update_wm_post,
14293 * but added for clarity.
14294 */
14295 if (crtc_state->disable_cxsr)
14296 return true;
a6747b73 14297
5a21b665 14298 return false;
e8861675
ML
14299}
14300
896e5bb0
L
14301static void intel_update_crtc(struct drm_crtc *crtc,
14302 struct drm_atomic_state *state,
14303 struct drm_crtc_state *old_crtc_state,
14304 unsigned int *crtc_vblank_mask)
14305{
14306 struct drm_device *dev = crtc->dev;
14307 struct drm_i915_private *dev_priv = to_i915(dev);
14308 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14309 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc->state);
14310 bool modeset = needs_modeset(crtc->state);
14311
14312 if (modeset) {
14313 update_scanline_offset(intel_crtc);
14314 dev_priv->display.crtc_enable(pipe_config, state);
14315 } else {
14316 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
14317 }
14318
14319 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
14320 intel_fbc_enable(
14321 intel_crtc, pipe_config,
14322 to_intel_plane_state(crtc->primary->state));
14323 }
14324
14325 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
14326
14327 if (needs_vblank_wait(pipe_config))
14328 *crtc_vblank_mask |= drm_crtc_mask(crtc);
14329}
14330
14331static void intel_update_crtcs(struct drm_atomic_state *state,
14332 unsigned int *crtc_vblank_mask)
14333{
14334 struct drm_crtc *crtc;
14335 struct drm_crtc_state *old_crtc_state;
14336 int i;
14337
14338 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
14339 if (!crtc->state->active)
14340 continue;
14341
14342 intel_update_crtc(crtc, state, old_crtc_state,
14343 crtc_vblank_mask);
14344 }
14345}
14346
27082493
L
14347static void skl_update_crtcs(struct drm_atomic_state *state,
14348 unsigned int *crtc_vblank_mask)
14349{
0f0f74bc 14350 struct drm_i915_private *dev_priv = to_i915(state->dev);
27082493
L
14351 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
14352 struct drm_crtc *crtc;
ce0ba283 14353 struct intel_crtc *intel_crtc;
27082493 14354 struct drm_crtc_state *old_crtc_state;
ce0ba283 14355 struct intel_crtc_state *cstate;
27082493
L
14356 unsigned int updated = 0;
14357 bool progress;
14358 enum pipe pipe;
5eff503b
ML
14359 int i;
14360
14361 const struct skl_ddb_entry *entries[I915_MAX_PIPES] = {};
14362
14363 for_each_crtc_in_state(state, crtc, old_crtc_state, i)
14364 /* ignore allocations for crtc's that have been turned off. */
14365 if (crtc->state->active)
14366 entries[i] = &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb;
27082493
L
14367
14368 /*
14369 * Whenever the number of active pipes changes, we need to make sure we
14370 * update the pipes in the right order so that their ddb allocations
14371 * never overlap with eachother inbetween CRTC updates. Otherwise we'll
14372 * cause pipe underruns and other bad stuff.
14373 */
14374 do {
27082493
L
14375 progress = false;
14376
14377 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
14378 bool vbl_wait = false;
14379 unsigned int cmask = drm_crtc_mask(crtc);
ce0ba283
L
14380
14381 intel_crtc = to_intel_crtc(crtc);
14382 cstate = to_intel_crtc_state(crtc->state);
14383 pipe = intel_crtc->pipe;
27082493 14384
5eff503b 14385 if (updated & cmask || !cstate->base.active)
27082493 14386 continue;
5eff503b
ML
14387
14388 if (skl_ddb_allocation_overlaps(entries, &cstate->wm.skl.ddb, i))
27082493
L
14389 continue;
14390
14391 updated |= cmask;
5eff503b 14392 entries[i] = &cstate->wm.skl.ddb;
27082493
L
14393
14394 /*
14395 * If this is an already active pipe, it's DDB changed,
14396 * and this isn't the last pipe that needs updating
14397 * then we need to wait for a vblank to pass for the
14398 * new ddb allocation to take effect.
14399 */
ce0ba283 14400 if (!skl_ddb_entry_equal(&cstate->wm.skl.ddb,
512b5527 14401 &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb) &&
27082493
L
14402 !crtc->state->active_changed &&
14403 intel_state->wm_results.dirty_pipes != updated)
14404 vbl_wait = true;
14405
14406 intel_update_crtc(crtc, state, old_crtc_state,
14407 crtc_vblank_mask);
14408
14409 if (vbl_wait)
0f0f74bc 14410 intel_wait_for_vblank(dev_priv, pipe);
27082493
L
14411
14412 progress = true;
14413 }
14414 } while (progress);
14415}
14416
94f05024 14417static void intel_atomic_commit_tail(struct drm_atomic_state *state)
a6778b3c 14418{
94f05024 14419 struct drm_device *dev = state->dev;
565602d7 14420 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 14421 struct drm_i915_private *dev_priv = to_i915(dev);
29ceb0e6 14422 struct drm_crtc_state *old_crtc_state;
7580d774 14423 struct drm_crtc *crtc;
5a21b665 14424 struct intel_crtc_state *intel_cstate;
5a21b665
DV
14425 bool hw_check = intel_state->modeset;
14426 unsigned long put_domains[I915_MAX_PIPES] = {};
14427 unsigned crtc_vblank_mask = 0;
e95433c7 14428 int i;
a6778b3c 14429
ea0000f0
DV
14430 drm_atomic_helper_wait_for_dependencies(state);
14431
c3b32658 14432 if (intel_state->modeset)
5a21b665 14433 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
565602d7 14434
29ceb0e6 14435 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
a539205a
ML
14436 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14437
5a21b665
DV
14438 if (needs_modeset(crtc->state) ||
14439 to_intel_crtc_state(crtc->state)->update_pipe) {
14440 hw_check = true;
14441
14442 put_domains[to_intel_crtc(crtc)->pipe] =
14443 modeset_get_crtc_power_domains(crtc,
14444 to_intel_crtc_state(crtc->state));
14445 }
14446
61333b60
ML
14447 if (!needs_modeset(crtc->state))
14448 continue;
14449
29ceb0e6 14450 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
460da916 14451
29ceb0e6
VS
14452 if (old_crtc_state->active) {
14453 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
4a806558 14454 dev_priv->display.crtc_disable(to_intel_crtc_state(old_crtc_state), state);
eddfcbcd 14455 intel_crtc->active = false;
58f9c0bc 14456 intel_fbc_disable(intel_crtc);
eddfcbcd 14457 intel_disable_shared_dpll(intel_crtc);
9bbc8258
VS
14458
14459 /*
14460 * Underruns don't always raise
14461 * interrupts, so check manually.
14462 */
14463 intel_check_cpu_fifo_underruns(dev_priv);
14464 intel_check_pch_fifo_underruns(dev_priv);
b9001114 14465
e62929b3
ML
14466 if (!crtc->state->active) {
14467 /*
14468 * Make sure we don't call initial_watermarks
14469 * for ILK-style watermark updates.
14470 */
14471 if (dev_priv->display.atomic_update_watermarks)
14472 dev_priv->display.initial_watermarks(intel_state,
14473 to_intel_crtc_state(crtc->state));
14474 else
14475 intel_update_watermarks(intel_crtc);
14476 }
a539205a 14477 }
b8cecdf5 14478 }
7758a113 14479
ea9d758d
DV
14480 /* Only after disabling all output pipelines that will be changed can we
14481 * update the the output configuration. */
4740b0f2 14482 intel_modeset_update_crtc_state(state);
f6e5b160 14483
565602d7 14484 if (intel_state->modeset) {
4740b0f2 14485 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
33c8df89
ML
14486
14487 if (dev_priv->display.modeset_commit_cdclk &&
c89e39f3 14488 (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
63911d72 14489 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco))
33c8df89 14490 dev_priv->display.modeset_commit_cdclk(state);
f6d1973d 14491
656d1b89
L
14492 /*
14493 * SKL workaround: bspec recommends we disable the SAGV when we
14494 * have more then one pipe enabled
14495 */
56feca91 14496 if (!intel_can_enable_sagv(state))
16dcdc4e 14497 intel_disable_sagv(dev_priv);
656d1b89 14498
677100ce 14499 intel_modeset_verify_disabled(dev, state);
4740b0f2 14500 }
47fab737 14501
896e5bb0 14502 /* Complete the events for pipes that have now been disabled */
29ceb0e6 14503 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
f6ac4b2a 14504 bool modeset = needs_modeset(crtc->state);
80715b2f 14505
1f7528c4
DV
14506 /* Complete events for now disable pipes here. */
14507 if (modeset && !crtc->state->active && crtc->state->event) {
14508 spin_lock_irq(&dev->event_lock);
14509 drm_crtc_send_vblank_event(crtc, crtc->state->event);
14510 spin_unlock_irq(&dev->event_lock);
14511
14512 crtc->state->event = NULL;
14513 }
177246a8
MR
14514 }
14515
896e5bb0
L
14516 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
14517 dev_priv->display.update_crtcs(state, &crtc_vblank_mask);
14518
94f05024
DV
14519 /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
14520 * already, but still need the state for the delayed optimization. To
14521 * fix this:
14522 * - wrap the optimization/post_plane_update stuff into a per-crtc work.
14523 * - schedule that vblank worker _before_ calling hw_done
14524 * - at the start of commit_tail, cancel it _synchrously
14525 * - switch over to the vblank wait helper in the core after that since
14526 * we don't need out special handling any more.
14527 */
5a21b665
DV
14528 if (!state->legacy_cursor_update)
14529 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
14530
14531 /*
14532 * Now that the vblank has passed, we can go ahead and program the
14533 * optimal watermarks on platforms that need two-step watermark
14534 * programming.
14535 *
14536 * TODO: Move this (and other cleanup) to an async worker eventually.
14537 */
14538 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
14539 intel_cstate = to_intel_crtc_state(crtc->state);
14540
14541 if (dev_priv->display.optimize_watermarks)
ccf010fb
ML
14542 dev_priv->display.optimize_watermarks(intel_state,
14543 intel_cstate);
5a21b665
DV
14544 }
14545
14546 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
14547 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
14548
14549 if (put_domains[i])
14550 modeset_put_power_domains(dev_priv, put_domains[i]);
14551
677100ce 14552 intel_modeset_verify_crtc(crtc, state, old_crtc_state, crtc->state);
5a21b665
DV
14553 }
14554
56feca91 14555 if (intel_state->modeset && intel_can_enable_sagv(state))
16dcdc4e 14556 intel_enable_sagv(dev_priv);
656d1b89 14557
94f05024
DV
14558 drm_atomic_helper_commit_hw_done(state);
14559
5a21b665
DV
14560 if (intel_state->modeset)
14561 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
14562
14563 mutex_lock(&dev->struct_mutex);
14564 drm_atomic_helper_cleanup_planes(dev, state);
14565 mutex_unlock(&dev->struct_mutex);
14566
ea0000f0
DV
14567 drm_atomic_helper_commit_cleanup_done(state);
14568
0853695c 14569 drm_atomic_state_put(state);
f30da187 14570
75714940
MK
14571 /* As one of the primary mmio accessors, KMS has a high likelihood
14572 * of triggering bugs in unclaimed access. After we finish
14573 * modesetting, see if an error has been flagged, and if so
14574 * enable debugging for the next modeset - and hope we catch
14575 * the culprit.
14576 *
14577 * XXX note that we assume display power is on at this point.
14578 * This might hold true now but we need to add pm helper to check
14579 * unclaimed only when the hardware is on, as atomic commits
14580 * can happen also when the device is completely off.
14581 */
14582 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
94f05024
DV
14583}
14584
14585static void intel_atomic_commit_work(struct work_struct *work)
14586{
c004a90b
CW
14587 struct drm_atomic_state *state =
14588 container_of(work, struct drm_atomic_state, commit_work);
14589
94f05024
DV
14590 intel_atomic_commit_tail(state);
14591}
14592
c004a90b
CW
14593static int __i915_sw_fence_call
14594intel_atomic_commit_ready(struct i915_sw_fence *fence,
14595 enum i915_sw_fence_notify notify)
14596{
14597 struct intel_atomic_state *state =
14598 container_of(fence, struct intel_atomic_state, commit_ready);
14599
14600 switch (notify) {
14601 case FENCE_COMPLETE:
14602 if (state->base.commit_work.func)
14603 queue_work(system_unbound_wq, &state->base.commit_work);
14604 break;
14605
14606 case FENCE_FREE:
14607 drm_atomic_state_put(&state->base);
14608 break;
14609 }
14610
14611 return NOTIFY_DONE;
14612}
14613
6c9c1b38
DV
14614static void intel_atomic_track_fbs(struct drm_atomic_state *state)
14615{
14616 struct drm_plane_state *old_plane_state;
14617 struct drm_plane *plane;
6c9c1b38
DV
14618 int i;
14619
faf5bf0a
CW
14620 for_each_plane_in_state(state, plane, old_plane_state, i)
14621 i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
14622 intel_fb_obj(plane->state->fb),
14623 to_intel_plane(plane)->frontbuffer_bit);
6c9c1b38
DV
14624}
14625
94f05024
DV
14626/**
14627 * intel_atomic_commit - commit validated state object
14628 * @dev: DRM device
14629 * @state: the top-level driver state object
14630 * @nonblock: nonblocking commit
14631 *
14632 * This function commits a top-level state object that has been validated
14633 * with drm_atomic_helper_check().
14634 *
94f05024
DV
14635 * RETURNS
14636 * Zero for success or -errno.
14637 */
14638static int intel_atomic_commit(struct drm_device *dev,
14639 struct drm_atomic_state *state,
14640 bool nonblock)
14641{
14642 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 14643 struct drm_i915_private *dev_priv = to_i915(dev);
94f05024
DV
14644 int ret = 0;
14645
94f05024
DV
14646 ret = drm_atomic_helper_setup_commit(state, nonblock);
14647 if (ret)
14648 return ret;
14649
c004a90b
CW
14650 drm_atomic_state_get(state);
14651 i915_sw_fence_init(&intel_state->commit_ready,
14652 intel_atomic_commit_ready);
94f05024 14653
d07f0e59 14654 ret = intel_atomic_prepare_commit(dev, state);
94f05024
DV
14655 if (ret) {
14656 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
c004a90b 14657 i915_sw_fence_commit(&intel_state->commit_ready);
94f05024
DV
14658 return ret;
14659 }
14660
14661 drm_atomic_helper_swap_state(state, true);
14662 dev_priv->wm.distrust_bios_wm = false;
94f05024 14663 intel_shared_dpll_commit(state);
6c9c1b38 14664 intel_atomic_track_fbs(state);
94f05024 14665
c3b32658
ML
14666 if (intel_state->modeset) {
14667 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
14668 sizeof(intel_state->min_pixclk));
14669 dev_priv->active_crtcs = intel_state->active_crtcs;
14670 dev_priv->atomic_cdclk_freq = intel_state->cdclk;
14671 }
14672
0853695c 14673 drm_atomic_state_get(state);
c004a90b
CW
14674 INIT_WORK(&state->commit_work,
14675 nonblock ? intel_atomic_commit_work : NULL);
14676
14677 i915_sw_fence_commit(&intel_state->commit_ready);
14678 if (!nonblock) {
14679 i915_sw_fence_wait(&intel_state->commit_ready);
94f05024 14680 intel_atomic_commit_tail(state);
c004a90b 14681 }
75714940 14682
74c090b1 14683 return 0;
7f27126e
JB
14684}
14685
c0c36b94
CW
14686void intel_crtc_restore_mode(struct drm_crtc *crtc)
14687{
83a57153
ACO
14688 struct drm_device *dev = crtc->dev;
14689 struct drm_atomic_state *state;
e694eb02 14690 struct drm_crtc_state *crtc_state;
2bfb4627 14691 int ret;
83a57153
ACO
14692
14693 state = drm_atomic_state_alloc(dev);
14694 if (!state) {
78108b7c
VS
14695 DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
14696 crtc->base.id, crtc->name);
83a57153
ACO
14697 return;
14698 }
14699
e694eb02 14700 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
83a57153 14701
e694eb02
ML
14702retry:
14703 crtc_state = drm_atomic_get_crtc_state(state, crtc);
14704 ret = PTR_ERR_OR_ZERO(crtc_state);
14705 if (!ret) {
14706 if (!crtc_state->active)
14707 goto out;
83a57153 14708
e694eb02 14709 crtc_state->mode_changed = true;
74c090b1 14710 ret = drm_atomic_commit(state);
83a57153
ACO
14711 }
14712
e694eb02
ML
14713 if (ret == -EDEADLK) {
14714 drm_atomic_state_clear(state);
14715 drm_modeset_backoff(state->acquire_ctx);
14716 goto retry;
4ed9fb37 14717 }
4be07317 14718
e694eb02 14719out:
0853695c 14720 drm_atomic_state_put(state);
c0c36b94
CW
14721}
14722
a8784875
BP
14723/*
14724 * FIXME: Remove this once i915 is fully DRIVER_ATOMIC by calling
14725 * drm_atomic_helper_legacy_gamma_set() directly.
14726 */
14727static int intel_atomic_legacy_gamma_set(struct drm_crtc *crtc,
14728 u16 *red, u16 *green, u16 *blue,
14729 uint32_t size)
14730{
14731 struct drm_device *dev = crtc->dev;
14732 struct drm_mode_config *config = &dev->mode_config;
14733 struct drm_crtc_state *state;
14734 int ret;
14735
14736 ret = drm_atomic_helper_legacy_gamma_set(crtc, red, green, blue, size);
14737 if (ret)
14738 return ret;
14739
14740 /*
14741 * Make sure we update the legacy properties so this works when
14742 * atomic is not enabled.
14743 */
14744
14745 state = crtc->state;
14746
14747 drm_object_property_set_value(&crtc->base,
14748 config->degamma_lut_property,
14749 (state->degamma_lut) ?
14750 state->degamma_lut->base.id : 0);
14751
14752 drm_object_property_set_value(&crtc->base,
14753 config->ctm_property,
14754 (state->ctm) ?
14755 state->ctm->base.id : 0);
14756
14757 drm_object_property_set_value(&crtc->base,
14758 config->gamma_lut_property,
14759 (state->gamma_lut) ?
14760 state->gamma_lut->base.id : 0);
14761
14762 return 0;
14763}
14764
f6e5b160 14765static const struct drm_crtc_funcs intel_crtc_funcs = {
a8784875 14766 .gamma_set = intel_atomic_legacy_gamma_set,
74c090b1 14767 .set_config = drm_atomic_helper_set_config,
82cf435b 14768 .set_property = drm_atomic_helper_crtc_set_property,
f6e5b160 14769 .destroy = intel_crtc_destroy,
527b6abe 14770 .page_flip = intel_crtc_page_flip,
1356837e
MR
14771 .atomic_duplicate_state = intel_crtc_duplicate_state,
14772 .atomic_destroy_state = intel_crtc_destroy_state,
f6e5b160
CW
14773};
14774
6beb8c23
MR
14775/**
14776 * intel_prepare_plane_fb - Prepare fb for usage on plane
14777 * @plane: drm plane to prepare for
14778 * @fb: framebuffer to prepare for presentation
14779 *
14780 * Prepares a framebuffer for usage on a display plane. Generally this
14781 * involves pinning the underlying object and updating the frontbuffer tracking
14782 * bits. Some older platforms need special physical address handling for
14783 * cursor planes.
14784 *
f935675f
ML
14785 * Must be called with struct_mutex held.
14786 *
6beb8c23
MR
14787 * Returns 0 on success, negative error code on failure.
14788 */
14789int
14790intel_prepare_plane_fb(struct drm_plane *plane,
1832040d 14791 struct drm_plane_state *new_state)
465c120c 14792{
c004a90b
CW
14793 struct intel_atomic_state *intel_state =
14794 to_intel_atomic_state(new_state->state);
b7f05d4a 14795 struct drm_i915_private *dev_priv = to_i915(plane->dev);
844f9111 14796 struct drm_framebuffer *fb = new_state->fb;
6beb8c23 14797 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
1ee49399 14798 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
c004a90b 14799 int ret;
465c120c 14800
1ee49399 14801 if (!obj && !old_obj)
465c120c
MR
14802 return 0;
14803
5008e874
ML
14804 if (old_obj) {
14805 struct drm_crtc_state *crtc_state =
c004a90b
CW
14806 drm_atomic_get_existing_crtc_state(new_state->state,
14807 plane->state->crtc);
5008e874
ML
14808
14809 /* Big Hammer, we also need to ensure that any pending
14810 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
14811 * current scanout is retired before unpinning the old
14812 * framebuffer. Note that we rely on userspace rendering
14813 * into the buffer attached to the pipe they are waiting
14814 * on. If not, userspace generates a GPU hang with IPEHR
14815 * point to the MI_WAIT_FOR_EVENT.
14816 *
14817 * This should only fail upon a hung GPU, in which case we
14818 * can safely continue.
14819 */
c004a90b
CW
14820 if (needs_modeset(crtc_state)) {
14821 ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
14822 old_obj->resv, NULL,
14823 false, 0,
14824 GFP_KERNEL);
14825 if (ret < 0)
14826 return ret;
f4457ae7 14827 }
5008e874
ML
14828 }
14829
c004a90b
CW
14830 if (new_state->fence) { /* explicit fencing */
14831 ret = i915_sw_fence_await_dma_fence(&intel_state->commit_ready,
14832 new_state->fence,
14833 I915_FENCE_TIMEOUT,
14834 GFP_KERNEL);
14835 if (ret < 0)
14836 return ret;
14837 }
14838
c37efb99
CW
14839 if (!obj)
14840 return 0;
14841
c004a90b
CW
14842 if (!new_state->fence) { /* implicit fencing */
14843 ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
14844 obj->resv, NULL,
14845 false, I915_FENCE_TIMEOUT,
14846 GFP_KERNEL);
14847 if (ret < 0)
14848 return ret;
6b5e90f5
CW
14849
14850 i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
c004a90b 14851 }
5a21b665 14852
c37efb99 14853 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
b7f05d4a 14854 INTEL_INFO(dev_priv)->cursor_needs_physical) {
50a0bc90 14855 int align = IS_I830(dev_priv) ? 16 * 1024 : 256;
6beb8c23 14856 ret = i915_gem_object_attach_phys(obj, align);
d07f0e59 14857 if (ret) {
6beb8c23 14858 DRM_DEBUG_KMS("failed to attach phys object\n");
d07f0e59
CW
14859 return ret;
14860 }
6beb8c23 14861 } else {
058d88c4
CW
14862 struct i915_vma *vma;
14863
14864 vma = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
d07f0e59
CW
14865 if (IS_ERR(vma)) {
14866 DRM_DEBUG_KMS("failed to pin object\n");
14867 return PTR_ERR(vma);
14868 }
7580d774 14869 }
fdd508a6 14870
d07f0e59 14871 return 0;
6beb8c23
MR
14872}
14873
38f3ce3a
MR
14874/**
14875 * intel_cleanup_plane_fb - Cleans up an fb after plane use
14876 * @plane: drm plane to clean up for
14877 * @fb: old framebuffer that was on plane
14878 *
14879 * Cleans up a framebuffer that has just been removed from a plane.
f935675f
ML
14880 *
14881 * Must be called with struct_mutex held.
38f3ce3a
MR
14882 */
14883void
14884intel_cleanup_plane_fb(struct drm_plane *plane,
1832040d 14885 struct drm_plane_state *old_state)
38f3ce3a 14886{
b7f05d4a 14887 struct drm_i915_private *dev_priv = to_i915(plane->dev);
7580d774 14888 struct intel_plane_state *old_intel_state;
1ee49399
ML
14889 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
14890 struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
38f3ce3a 14891
7580d774
ML
14892 old_intel_state = to_intel_plane_state(old_state);
14893
1ee49399 14894 if (!obj && !old_obj)
38f3ce3a
MR
14895 return;
14896
1ee49399 14897 if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
b7f05d4a 14898 !INTEL_INFO(dev_priv)->cursor_needs_physical))
3465c580 14899 intel_unpin_fb_obj(old_state->fb, old_state->rotation);
465c120c
MR
14900}
14901
6156a456
CK
14902int
14903skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
14904{
14905 int max_scale;
6156a456
CK
14906 int crtc_clock, cdclk;
14907
bf8a0af0 14908 if (!intel_crtc || !crtc_state->base.enable)
6156a456
CK
14909 return DRM_PLANE_HELPER_NO_SCALING;
14910
6156a456 14911 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
27c329ed 14912 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
6156a456 14913
54bf1ce6 14914 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
6156a456
CK
14915 return DRM_PLANE_HELPER_NO_SCALING;
14916
14917 /*
14918 * skl max scale is lower of:
14919 * close to 3 but not 3, -1 is for that purpose
14920 * or
14921 * cdclk/crtc_clock
14922 */
14923 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
14924
14925 return max_scale;
14926}
14927
465c120c 14928static int
3c692a41 14929intel_check_primary_plane(struct drm_plane *plane,
061e4b8d 14930 struct intel_crtc_state *crtc_state,
3c692a41
GP
14931 struct intel_plane_state *state)
14932{
b63a16f6 14933 struct drm_i915_private *dev_priv = to_i915(plane->dev);
2b875c22 14934 struct drm_crtc *crtc = state->base.crtc;
6156a456 14935 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
061e4b8d
ML
14936 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
14937 bool can_position = false;
b63a16f6 14938 int ret;
465c120c 14939
b63a16f6 14940 if (INTEL_GEN(dev_priv) >= 9) {
693bdc28
VS
14941 /* use scaler when colorkey is not required */
14942 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
14943 min_scale = 1;
14944 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
14945 }
d8106366 14946 can_position = true;
6156a456 14947 }
d8106366 14948
cc926387
DV
14949 ret = drm_plane_helper_check_state(&state->base,
14950 &state->clip,
14951 min_scale, max_scale,
14952 can_position, true);
b63a16f6
VS
14953 if (ret)
14954 return ret;
14955
cc926387 14956 if (!state->base.fb)
b63a16f6
VS
14957 return 0;
14958
14959 if (INTEL_GEN(dev_priv) >= 9) {
14960 ret = skl_check_plane_surface(state);
14961 if (ret)
14962 return ret;
14963 }
14964
14965 return 0;
14af293f
GP
14966}
14967
5a21b665
DV
14968static void intel_begin_crtc_commit(struct drm_crtc *crtc,
14969 struct drm_crtc_state *old_crtc_state)
14970{
14971 struct drm_device *dev = crtc->dev;
62e0fb88 14972 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665 14973 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b707aa50
L
14974 struct intel_crtc_state *intel_cstate =
14975 to_intel_crtc_state(crtc->state);
ccf010fb 14976 struct intel_crtc_state *old_intel_cstate =
5a21b665 14977 to_intel_crtc_state(old_crtc_state);
ccf010fb
ML
14978 struct intel_atomic_state *old_intel_state =
14979 to_intel_atomic_state(old_crtc_state->state);
5a21b665
DV
14980 bool modeset = needs_modeset(crtc->state);
14981
14982 /* Perform vblank evasion around commit operation */
14983 intel_pipe_update_start(intel_crtc);
14984
14985 if (modeset)
e62929b3 14986 goto out;
5a21b665
DV
14987
14988 if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
14989 intel_color_set_csc(crtc->state);
14990 intel_color_load_luts(crtc->state);
14991 }
14992
ccf010fb
ML
14993 if (intel_cstate->update_pipe)
14994 intel_update_pipe_config(intel_crtc, old_intel_cstate);
14995 else if (INTEL_GEN(dev_priv) >= 9)
5a21b665 14996 skl_detach_scalers(intel_crtc);
62e0fb88 14997
e62929b3 14998out:
ccf010fb
ML
14999 if (dev_priv->display.atomic_update_watermarks)
15000 dev_priv->display.atomic_update_watermarks(old_intel_state,
15001 intel_cstate);
5a21b665
DV
15002}
15003
15004static void intel_finish_crtc_commit(struct drm_crtc *crtc,
15005 struct drm_crtc_state *old_crtc_state)
15006{
15007 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
15008
15009 intel_pipe_update_end(intel_crtc, NULL);
15010}
15011
cf4c7c12 15012/**
4a3b8769
MR
15013 * intel_plane_destroy - destroy a plane
15014 * @plane: plane to destroy
cf4c7c12 15015 *
4a3b8769
MR
15016 * Common destruction function for all types of planes (primary, cursor,
15017 * sprite).
cf4c7c12 15018 */
4a3b8769 15019void intel_plane_destroy(struct drm_plane *plane)
465c120c 15020{
465c120c 15021 drm_plane_cleanup(plane);
69ae561f 15022 kfree(to_intel_plane(plane));
465c120c
MR
15023}
15024
65a3fea0 15025const struct drm_plane_funcs intel_plane_funcs = {
70a101f8
MR
15026 .update_plane = drm_atomic_helper_update_plane,
15027 .disable_plane = drm_atomic_helper_disable_plane,
3d7d6510 15028 .destroy = intel_plane_destroy,
c196e1d6 15029 .set_property = drm_atomic_helper_plane_set_property,
a98b3431
MR
15030 .atomic_get_property = intel_plane_atomic_get_property,
15031 .atomic_set_property = intel_plane_atomic_set_property,
ea2c67bb
MR
15032 .atomic_duplicate_state = intel_plane_duplicate_state,
15033 .atomic_destroy_state = intel_plane_destroy_state,
465c120c
MR
15034};
15035
b079bd17 15036static struct intel_plane *
580503c7 15037intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
465c120c 15038{
fca0ce2a
VS
15039 struct intel_plane *primary = NULL;
15040 struct intel_plane_state *state = NULL;
465c120c 15041 const uint32_t *intel_primary_formats;
93ca7e00 15042 unsigned int supported_rotations;
45e3743a 15043 unsigned int num_formats;
fca0ce2a 15044 int ret;
465c120c
MR
15045
15046 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
b079bd17
VS
15047 if (!primary) {
15048 ret = -ENOMEM;
fca0ce2a 15049 goto fail;
b079bd17 15050 }
465c120c 15051
8e7d688b 15052 state = intel_create_plane_state(&primary->base);
b079bd17
VS
15053 if (!state) {
15054 ret = -ENOMEM;
fca0ce2a 15055 goto fail;
b079bd17
VS
15056 }
15057
8e7d688b 15058 primary->base.state = &state->base;
ea2c67bb 15059
465c120c
MR
15060 primary->can_scale = false;
15061 primary->max_downscale = 1;
580503c7 15062 if (INTEL_GEN(dev_priv) >= 9) {
6156a456 15063 primary->can_scale = true;
af99ceda 15064 state->scaler_id = -1;
6156a456 15065 }
465c120c 15066 primary->pipe = pipe;
e3c566df
VS
15067 /*
15068 * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
15069 * port is hooked to pipe B. Hence we want plane A feeding pipe B.
15070 */
15071 if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4)
15072 primary->plane = (enum plane) !pipe;
15073 else
15074 primary->plane = (enum plane) pipe;
b14e5848 15075 primary->id = PLANE_PRIMARY;
a9ff8714 15076 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
c59cb179 15077 primary->check_plane = intel_check_primary_plane;
465c120c 15078
580503c7 15079 if (INTEL_GEN(dev_priv) >= 9) {
6c0fd451
DL
15080 intel_primary_formats = skl_primary_formats;
15081 num_formats = ARRAY_SIZE(skl_primary_formats);
a8d201af
ML
15082
15083 primary->update_plane = skylake_update_primary_plane;
15084 primary->disable_plane = skylake_disable_primary_plane;
6e266956 15085 } else if (HAS_PCH_SPLIT(dev_priv)) {
a8d201af
ML
15086 intel_primary_formats = i965_primary_formats;
15087 num_formats = ARRAY_SIZE(i965_primary_formats);
15088
15089 primary->update_plane = ironlake_update_primary_plane;
15090 primary->disable_plane = i9xx_disable_primary_plane;
580503c7 15091 } else if (INTEL_GEN(dev_priv) >= 4) {
568db4f2
DL
15092 intel_primary_formats = i965_primary_formats;
15093 num_formats = ARRAY_SIZE(i965_primary_formats);
a8d201af
ML
15094
15095 primary->update_plane = i9xx_update_primary_plane;
15096 primary->disable_plane = i9xx_disable_primary_plane;
6c0fd451
DL
15097 } else {
15098 intel_primary_formats = i8xx_primary_formats;
15099 num_formats = ARRAY_SIZE(i8xx_primary_formats);
a8d201af
ML
15100
15101 primary->update_plane = i9xx_update_primary_plane;
15102 primary->disable_plane = i9xx_disable_primary_plane;
465c120c
MR
15103 }
15104
580503c7
VS
15105 if (INTEL_GEN(dev_priv) >= 9)
15106 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
15107 0, &intel_plane_funcs,
38573dc1
VS
15108 intel_primary_formats, num_formats,
15109 DRM_PLANE_TYPE_PRIMARY,
15110 "plane 1%c", pipe_name(pipe));
9beb5fea 15111 else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
580503c7
VS
15112 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
15113 0, &intel_plane_funcs,
38573dc1
VS
15114 intel_primary_formats, num_formats,
15115 DRM_PLANE_TYPE_PRIMARY,
15116 "primary %c", pipe_name(pipe));
15117 else
580503c7
VS
15118 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
15119 0, &intel_plane_funcs,
38573dc1
VS
15120 intel_primary_formats, num_formats,
15121 DRM_PLANE_TYPE_PRIMARY,
15122 "plane %c", plane_name(primary->plane));
fca0ce2a
VS
15123 if (ret)
15124 goto fail;
48404c1e 15125
5481e27f 15126 if (INTEL_GEN(dev_priv) >= 9) {
93ca7e00
VS
15127 supported_rotations =
15128 DRM_ROTATE_0 | DRM_ROTATE_90 |
15129 DRM_ROTATE_180 | DRM_ROTATE_270;
4ea7be2b
VS
15130 } else if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
15131 supported_rotations =
15132 DRM_ROTATE_0 | DRM_ROTATE_180 |
15133 DRM_REFLECT_X;
5481e27f 15134 } else if (INTEL_GEN(dev_priv) >= 4) {
93ca7e00
VS
15135 supported_rotations =
15136 DRM_ROTATE_0 | DRM_ROTATE_180;
15137 } else {
15138 supported_rotations = DRM_ROTATE_0;
15139 }
15140
5481e27f 15141 if (INTEL_GEN(dev_priv) >= 4)
93ca7e00
VS
15142 drm_plane_create_rotation_property(&primary->base,
15143 DRM_ROTATE_0,
15144 supported_rotations);
48404c1e 15145
ea2c67bb
MR
15146 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
15147
b079bd17 15148 return primary;
fca0ce2a
VS
15149
15150fail:
15151 kfree(state);
15152 kfree(primary);
15153
b079bd17 15154 return ERR_PTR(ret);
465c120c
MR
15155}
15156
3d7d6510 15157static int
852e787c 15158intel_check_cursor_plane(struct drm_plane *plane,
061e4b8d 15159 struct intel_crtc_state *crtc_state,
852e787c 15160 struct intel_plane_state *state)
3d7d6510 15161{
2b875c22 15162 struct drm_framebuffer *fb = state->base.fb;
757f9a3e 15163 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
b29ec92c 15164 enum pipe pipe = to_intel_plane(plane)->pipe;
757f9a3e
GP
15165 unsigned stride;
15166 int ret;
3d7d6510 15167
f8856a44
VS
15168 ret = drm_plane_helper_check_state(&state->base,
15169 &state->clip,
15170 DRM_PLANE_HELPER_NO_SCALING,
15171 DRM_PLANE_HELPER_NO_SCALING,
15172 true, true);
757f9a3e
GP
15173 if (ret)
15174 return ret;
15175
757f9a3e
GP
15176 /* if we want to turn off the cursor ignore width and height */
15177 if (!obj)
da20eabd 15178 return 0;
757f9a3e 15179
757f9a3e 15180 /* Check for which cursor types we support */
50a0bc90
TU
15181 if (!cursor_size_ok(to_i915(plane->dev), state->base.crtc_w,
15182 state->base.crtc_h)) {
ea2c67bb
MR
15183 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
15184 state->base.crtc_w, state->base.crtc_h);
757f9a3e
GP
15185 return -EINVAL;
15186 }
15187
ea2c67bb
MR
15188 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
15189 if (obj->base.size < stride * state->base.crtc_h) {
757f9a3e
GP
15190 DRM_DEBUG_KMS("buffer is too small\n");
15191 return -ENOMEM;
15192 }
15193
bae781b2 15194 if (fb->modifier != DRM_FORMAT_MOD_NONE) {
757f9a3e 15195 DRM_DEBUG_KMS("cursor cannot be tiled\n");
da20eabd 15196 return -EINVAL;
32b7eeec
MR
15197 }
15198
b29ec92c
VS
15199 /*
15200 * There's something wrong with the cursor on CHV pipe C.
15201 * If it straddles the left edge of the screen then
15202 * moving it away from the edge or disabling it often
15203 * results in a pipe underrun, and often that can lead to
15204 * dead pipe (constant underrun reported, and it scans
15205 * out just a solid color). To recover from that, the
15206 * display power well must be turned off and on again.
15207 * Refuse the put the cursor into that compromised position.
15208 */
920a14b2 15209 if (IS_CHERRYVIEW(to_i915(plane->dev)) && pipe == PIPE_C &&
936e71e3 15210 state->base.visible && state->base.crtc_x < 0) {
b29ec92c
VS
15211 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
15212 return -EINVAL;
15213 }
15214
da20eabd 15215 return 0;
852e787c 15216}
3d7d6510 15217
a8ad0d8e
ML
15218static void
15219intel_disable_cursor_plane(struct drm_plane *plane,
7fabf5ef 15220 struct drm_crtc *crtc)
a8ad0d8e 15221{
f2858021
ML
15222 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
15223
15224 intel_crtc->cursor_addr = 0;
55a08b3f 15225 intel_crtc_update_cursor(crtc, NULL);
a8ad0d8e
ML
15226}
15227
f4a2cf29 15228static void
55a08b3f
ML
15229intel_update_cursor_plane(struct drm_plane *plane,
15230 const struct intel_crtc_state *crtc_state,
15231 const struct intel_plane_state *state)
852e787c 15232{
55a08b3f
ML
15233 struct drm_crtc *crtc = crtc_state->base.crtc;
15234 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b7f05d4a 15235 struct drm_i915_private *dev_priv = to_i915(plane->dev);
2b875c22 15236 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
a912f12f 15237 uint32_t addr;
852e787c 15238
f4a2cf29 15239 if (!obj)
a912f12f 15240 addr = 0;
b7f05d4a 15241 else if (!INTEL_INFO(dev_priv)->cursor_needs_physical)
058d88c4 15242 addr = i915_gem_object_ggtt_offset(obj, NULL);
f4a2cf29 15243 else
a912f12f 15244 addr = obj->phys_handle->busaddr;
852e787c 15245
a912f12f 15246 intel_crtc->cursor_addr = addr;
55a08b3f 15247 intel_crtc_update_cursor(crtc, state);
852e787c
GP
15248}
15249
b079bd17 15250static struct intel_plane *
580503c7 15251intel_cursor_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
3d7d6510 15252{
fca0ce2a
VS
15253 struct intel_plane *cursor = NULL;
15254 struct intel_plane_state *state = NULL;
15255 int ret;
3d7d6510
MR
15256
15257 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
b079bd17
VS
15258 if (!cursor) {
15259 ret = -ENOMEM;
fca0ce2a 15260 goto fail;
b079bd17 15261 }
3d7d6510 15262
8e7d688b 15263 state = intel_create_plane_state(&cursor->base);
b079bd17
VS
15264 if (!state) {
15265 ret = -ENOMEM;
fca0ce2a 15266 goto fail;
b079bd17
VS
15267 }
15268
8e7d688b 15269 cursor->base.state = &state->base;
ea2c67bb 15270
3d7d6510
MR
15271 cursor->can_scale = false;
15272 cursor->max_downscale = 1;
15273 cursor->pipe = pipe;
15274 cursor->plane = pipe;
b14e5848 15275 cursor->id = PLANE_CURSOR;
a9ff8714 15276 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
c59cb179 15277 cursor->check_plane = intel_check_cursor_plane;
55a08b3f 15278 cursor->update_plane = intel_update_cursor_plane;
a8ad0d8e 15279 cursor->disable_plane = intel_disable_cursor_plane;
3d7d6510 15280
580503c7
VS
15281 ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
15282 0, &intel_plane_funcs,
fca0ce2a
VS
15283 intel_cursor_formats,
15284 ARRAY_SIZE(intel_cursor_formats),
38573dc1
VS
15285 DRM_PLANE_TYPE_CURSOR,
15286 "cursor %c", pipe_name(pipe));
fca0ce2a
VS
15287 if (ret)
15288 goto fail;
4398ad45 15289
5481e27f 15290 if (INTEL_GEN(dev_priv) >= 4)
93ca7e00
VS
15291 drm_plane_create_rotation_property(&cursor->base,
15292 DRM_ROTATE_0,
15293 DRM_ROTATE_0 |
15294 DRM_ROTATE_180);
4398ad45 15295
580503c7 15296 if (INTEL_GEN(dev_priv) >= 9)
af99ceda
CK
15297 state->scaler_id = -1;
15298
ea2c67bb
MR
15299 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
15300
b079bd17 15301 return cursor;
fca0ce2a
VS
15302
15303fail:
15304 kfree(state);
15305 kfree(cursor);
15306
b079bd17 15307 return ERR_PTR(ret);
3d7d6510
MR
15308}
15309
65edccce
VS
15310static void skl_init_scalers(struct drm_i915_private *dev_priv,
15311 struct intel_crtc *crtc,
15312 struct intel_crtc_state *crtc_state)
549e2bfb 15313{
65edccce
VS
15314 struct intel_crtc_scaler_state *scaler_state =
15315 &crtc_state->scaler_state;
549e2bfb 15316 int i;
549e2bfb 15317
65edccce
VS
15318 for (i = 0; i < crtc->num_scalers; i++) {
15319 struct intel_scaler *scaler = &scaler_state->scalers[i];
15320
15321 scaler->in_use = 0;
15322 scaler->mode = PS_SCALER_MODE_DYN;
549e2bfb
CK
15323 }
15324
15325 scaler_state->scaler_id = -1;
15326}
15327
5ab0d85b 15328static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
79e53945
JB
15329{
15330 struct intel_crtc *intel_crtc;
f5de6e07 15331 struct intel_crtc_state *crtc_state = NULL;
b079bd17
VS
15332 struct intel_plane *primary = NULL;
15333 struct intel_plane *cursor = NULL;
a81d6fa0 15334 int sprite, ret;
79e53945 15335
955382f3 15336 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
b079bd17
VS
15337 if (!intel_crtc)
15338 return -ENOMEM;
79e53945 15339
f5de6e07 15340 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
b079bd17
VS
15341 if (!crtc_state) {
15342 ret = -ENOMEM;
f5de6e07 15343 goto fail;
b079bd17 15344 }
550acefd
ACO
15345 intel_crtc->config = crtc_state;
15346 intel_crtc->base.state = &crtc_state->base;
07878248 15347 crtc_state->base.crtc = &intel_crtc->base;
f5de6e07 15348
549e2bfb 15349 /* initialize shared scalers */
5ab0d85b 15350 if (INTEL_GEN(dev_priv) >= 9) {
549e2bfb
CK
15351 if (pipe == PIPE_C)
15352 intel_crtc->num_scalers = 1;
15353 else
15354 intel_crtc->num_scalers = SKL_NUM_SCALERS;
15355
65edccce 15356 skl_init_scalers(dev_priv, intel_crtc, crtc_state);
549e2bfb
CK
15357 }
15358
580503c7 15359 primary = intel_primary_plane_create(dev_priv, pipe);
b079bd17
VS
15360 if (IS_ERR(primary)) {
15361 ret = PTR_ERR(primary);
3d7d6510 15362 goto fail;
b079bd17 15363 }
d97d7b48 15364 intel_crtc->plane_ids_mask |= BIT(primary->id);
3d7d6510 15365
a81d6fa0 15366 for_each_sprite(dev_priv, pipe, sprite) {
b079bd17
VS
15367 struct intel_plane *plane;
15368
580503c7 15369 plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
d2b2cbce 15370 if (IS_ERR(plane)) {
b079bd17
VS
15371 ret = PTR_ERR(plane);
15372 goto fail;
15373 }
d97d7b48 15374 intel_crtc->plane_ids_mask |= BIT(plane->id);
a81d6fa0
VS
15375 }
15376
580503c7 15377 cursor = intel_cursor_plane_create(dev_priv, pipe);
d2b2cbce 15378 if (IS_ERR(cursor)) {
b079bd17 15379 ret = PTR_ERR(cursor);
3d7d6510 15380 goto fail;
b079bd17 15381 }
d97d7b48 15382 intel_crtc->plane_ids_mask |= BIT(cursor->id);
3d7d6510 15383
5ab0d85b 15384 ret = drm_crtc_init_with_planes(&dev_priv->drm, &intel_crtc->base,
b079bd17
VS
15385 &primary->base, &cursor->base,
15386 &intel_crtc_funcs,
4d5d72b7 15387 "pipe %c", pipe_name(pipe));
3d7d6510
MR
15388 if (ret)
15389 goto fail;
79e53945 15390
80824003 15391 intel_crtc->pipe = pipe;
e3c566df 15392 intel_crtc->plane = primary->plane;
80824003 15393
4b0e333e
CW
15394 intel_crtc->cursor_base = ~0;
15395 intel_crtc->cursor_cntl = ~0;
dc41c154 15396 intel_crtc->cursor_size = ~0;
8d7849db 15397
852eb00d
VS
15398 intel_crtc->wm.cxsr_allowed = true;
15399
22fd0fab
JB
15400 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
15401 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
e2af48c6
VS
15402 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = intel_crtc;
15403 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = intel_crtc;
22fd0fab 15404
79e53945 15405 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
87b6b101 15406
8563b1e8
LL
15407 intel_color_init(&intel_crtc->base);
15408
87b6b101 15409 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
b079bd17
VS
15410
15411 return 0;
3d7d6510
MR
15412
15413fail:
b079bd17
VS
15414 /*
15415 * drm_mode_config_cleanup() will free up any
15416 * crtcs/planes already initialized.
15417 */
f5de6e07 15418 kfree(crtc_state);
3d7d6510 15419 kfree(intel_crtc);
b079bd17
VS
15420
15421 return ret;
79e53945
JB
15422}
15423
752aa88a
JB
15424enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
15425{
15426 struct drm_encoder *encoder = connector->base.encoder;
6e9f798d 15427 struct drm_device *dev = connector->base.dev;
752aa88a 15428
51fd371b 15429 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
752aa88a 15430
d3babd3f 15431 if (!encoder || WARN_ON(!encoder->crtc))
752aa88a
JB
15432 return INVALID_PIPE;
15433
15434 return to_intel_crtc(encoder->crtc)->pipe;
15435}
15436
08d7b3d1 15437int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 15438 struct drm_file *file)
08d7b3d1 15439{
08d7b3d1 15440 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7707e653 15441 struct drm_crtc *drmmode_crtc;
c05422d5 15442 struct intel_crtc *crtc;
08d7b3d1 15443
7707e653 15444 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
71240ed2 15445 if (!drmmode_crtc)
3f2c2057 15446 return -ENOENT;
08d7b3d1 15447
7707e653 15448 crtc = to_intel_crtc(drmmode_crtc);
c05422d5 15449 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 15450
c05422d5 15451 return 0;
08d7b3d1
CW
15452}
15453
66a9278e 15454static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 15455{
66a9278e
DV
15456 struct drm_device *dev = encoder->base.dev;
15457 struct intel_encoder *source_encoder;
79e53945 15458 int index_mask = 0;
79e53945
JB
15459 int entry = 0;
15460
b2784e15 15461 for_each_intel_encoder(dev, source_encoder) {
bc079e8b 15462 if (encoders_cloneable(encoder, source_encoder))
66a9278e
DV
15463 index_mask |= (1 << entry);
15464
79e53945
JB
15465 entry++;
15466 }
4ef69c7a 15467
79e53945
JB
15468 return index_mask;
15469}
15470
646d5772 15471static bool has_edp_a(struct drm_i915_private *dev_priv)
4d302442 15472{
646d5772 15473 if (!IS_MOBILE(dev_priv))
4d302442
CW
15474 return false;
15475
15476 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
15477 return false;
15478
5db94019 15479 if (IS_GEN5(dev_priv) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
15480 return false;
15481
15482 return true;
15483}
15484
6315b5d3 15485static bool intel_crt_present(struct drm_i915_private *dev_priv)
84b4e042 15486{
6315b5d3 15487 if (INTEL_GEN(dev_priv) >= 9)
884497ed
DL
15488 return false;
15489
50a0bc90 15490 if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
84b4e042
JB
15491 return false;
15492
920a14b2 15493 if (IS_CHERRYVIEW(dev_priv))
84b4e042
JB
15494 return false;
15495
4f8036a2
TU
15496 if (HAS_PCH_LPT_H(dev_priv) &&
15497 I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
65e472e4
VS
15498 return false;
15499
70ac54d0 15500 /* DDI E can't be used if DDI A requires 4 lanes */
4f8036a2 15501 if (HAS_DDI(dev_priv) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
70ac54d0
VS
15502 return false;
15503
e4abb733 15504 if (!dev_priv->vbt.int_crt_support)
84b4e042
JB
15505 return false;
15506
15507 return true;
15508}
15509
8090ba8c
ID
15510void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
15511{
15512 int pps_num;
15513 int pps_idx;
15514
15515 if (HAS_DDI(dev_priv))
15516 return;
15517 /*
15518 * This w/a is needed at least on CPT/PPT, but to be sure apply it
15519 * everywhere where registers can be write protected.
15520 */
15521 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
15522 pps_num = 2;
15523 else
15524 pps_num = 1;
15525
15526 for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
15527 u32 val = I915_READ(PP_CONTROL(pps_idx));
15528
15529 val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
15530 I915_WRITE(PP_CONTROL(pps_idx), val);
15531 }
15532}
15533
44cb734c
ID
15534static void intel_pps_init(struct drm_i915_private *dev_priv)
15535{
cc3f90f0 15536 if (HAS_PCH_SPLIT(dev_priv) || IS_GEN9_LP(dev_priv))
44cb734c
ID
15537 dev_priv->pps_mmio_base = PCH_PPS_BASE;
15538 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
15539 dev_priv->pps_mmio_base = VLV_PPS_BASE;
15540 else
15541 dev_priv->pps_mmio_base = PPS_BASE;
8090ba8c
ID
15542
15543 intel_pps_unlock_regs_wa(dev_priv);
44cb734c
ID
15544}
15545
c39055b0 15546static void intel_setup_outputs(struct drm_i915_private *dev_priv)
79e53945 15547{
4ef69c7a 15548 struct intel_encoder *encoder;
cb0953d7 15549 bool dpd_is_edp = false;
79e53945 15550
44cb734c
ID
15551 intel_pps_init(dev_priv);
15552
97a824e1
ID
15553 /*
15554 * intel_edp_init_connector() depends on this completing first, to
15555 * prevent the registeration of both eDP and LVDS and the incorrect
15556 * sharing of the PPS.
15557 */
c39055b0 15558 intel_lvds_init(dev_priv);
79e53945 15559
6315b5d3 15560 if (intel_crt_present(dev_priv))
c39055b0 15561 intel_crt_init(dev_priv);
cb0953d7 15562
cc3f90f0 15563 if (IS_GEN9_LP(dev_priv)) {
c776eb2e
VK
15564 /*
15565 * FIXME: Broxton doesn't support port detection via the
15566 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
15567 * detect the ports.
15568 */
c39055b0
ACO
15569 intel_ddi_init(dev_priv, PORT_A);
15570 intel_ddi_init(dev_priv, PORT_B);
15571 intel_ddi_init(dev_priv, PORT_C);
c6c794a2 15572
c39055b0 15573 intel_dsi_init(dev_priv);
4f8036a2 15574 } else if (HAS_DDI(dev_priv)) {
0e72a5b5
ED
15575 int found;
15576
de31facd
JB
15577 /*
15578 * Haswell uses DDI functions to detect digital outputs.
15579 * On SKL pre-D0 the strap isn't connected, so we assume
15580 * it's there.
15581 */
77179400 15582 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
de31facd 15583 /* WaIgnoreDDIAStrap: skl */
0853723b 15584 if (found || IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
c39055b0 15585 intel_ddi_init(dev_priv, PORT_A);
0e72a5b5
ED
15586
15587 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
15588 * register */
15589 found = I915_READ(SFUSE_STRAP);
15590
15591 if (found & SFUSE_STRAP_DDIB_DETECTED)
c39055b0 15592 intel_ddi_init(dev_priv, PORT_B);
0e72a5b5 15593 if (found & SFUSE_STRAP_DDIC_DETECTED)
c39055b0 15594 intel_ddi_init(dev_priv, PORT_C);
0e72a5b5 15595 if (found & SFUSE_STRAP_DDID_DETECTED)
c39055b0 15596 intel_ddi_init(dev_priv, PORT_D);
2800e4c2
RV
15597 /*
15598 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
15599 */
0853723b 15600 if ((IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) &&
2800e4c2
RV
15601 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
15602 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
15603 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
c39055b0 15604 intel_ddi_init(dev_priv, PORT_E);
2800e4c2 15605
6e266956 15606 } else if (HAS_PCH_SPLIT(dev_priv)) {
cb0953d7 15607 int found;
dd11bc10 15608 dpd_is_edp = intel_dp_is_edp(dev_priv, PORT_D);
270b3042 15609
646d5772 15610 if (has_edp_a(dev_priv))
c39055b0 15611 intel_dp_init(dev_priv, DP_A, PORT_A);
cb0953d7 15612
dc0fa718 15613 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 15614 /* PCH SDVOB multiplex with HDMIB */
c39055b0 15615 found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
30ad48b7 15616 if (!found)
c39055b0 15617 intel_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
5eb08b69 15618 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
c39055b0 15619 intel_dp_init(dev_priv, PCH_DP_B, PORT_B);
30ad48b7
ZW
15620 }
15621
dc0fa718 15622 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
c39055b0 15623 intel_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
30ad48b7 15624
dc0fa718 15625 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
c39055b0 15626 intel_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
30ad48b7 15627
5eb08b69 15628 if (I915_READ(PCH_DP_C) & DP_DETECTED)
c39055b0 15629 intel_dp_init(dev_priv, PCH_DP_C, PORT_C);
5eb08b69 15630
270b3042 15631 if (I915_READ(PCH_DP_D) & DP_DETECTED)
c39055b0 15632 intel_dp_init(dev_priv, PCH_DP_D, PORT_D);
920a14b2 15633 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
22f35042 15634 bool has_edp, has_port;
457c52d8 15635
e17ac6db
VS
15636 /*
15637 * The DP_DETECTED bit is the latched state of the DDC
15638 * SDA pin at boot. However since eDP doesn't require DDC
15639 * (no way to plug in a DP->HDMI dongle) the DDC pins for
15640 * eDP ports may have been muxed to an alternate function.
15641 * Thus we can't rely on the DP_DETECTED bit alone to detect
15642 * eDP ports. Consult the VBT as well as DP_DETECTED to
15643 * detect eDP ports.
22f35042
VS
15644 *
15645 * Sadly the straps seem to be missing sometimes even for HDMI
15646 * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
15647 * and VBT for the presence of the port. Additionally we can't
15648 * trust the port type the VBT declares as we've seen at least
15649 * HDMI ports that the VBT claim are DP or eDP.
e17ac6db 15650 */
dd11bc10 15651 has_edp = intel_dp_is_edp(dev_priv, PORT_B);
22f35042
VS
15652 has_port = intel_bios_is_port_present(dev_priv, PORT_B);
15653 if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
c39055b0 15654 has_edp &= intel_dp_init(dev_priv, VLV_DP_B, PORT_B);
22f35042 15655 if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
c39055b0 15656 intel_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
585a94b8 15657
dd11bc10 15658 has_edp = intel_dp_is_edp(dev_priv, PORT_C);
22f35042
VS
15659 has_port = intel_bios_is_port_present(dev_priv, PORT_C);
15660 if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
c39055b0 15661 has_edp &= intel_dp_init(dev_priv, VLV_DP_C, PORT_C);
22f35042 15662 if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
c39055b0 15663 intel_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
19c03924 15664
920a14b2 15665 if (IS_CHERRYVIEW(dev_priv)) {
22f35042
VS
15666 /*
15667 * eDP not supported on port D,
15668 * so no need to worry about it
15669 */
15670 has_port = intel_bios_is_port_present(dev_priv, PORT_D);
15671 if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
c39055b0 15672 intel_dp_init(dev_priv, CHV_DP_D, PORT_D);
22f35042 15673 if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
c39055b0 15674 intel_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
9418c1f1
VS
15675 }
15676
c39055b0 15677 intel_dsi_init(dev_priv);
5db94019 15678 } else if (!IS_GEN2(dev_priv) && !IS_PINEVIEW(dev_priv)) {
27185ae1 15679 bool found = false;
7d57382e 15680
e2debe91 15681 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 15682 DRM_DEBUG_KMS("probing SDVOB\n");
c39055b0 15683 found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
9beb5fea 15684 if (!found && IS_G4X(dev_priv)) {
b01f2c3a 15685 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
c39055b0 15686 intel_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
b01f2c3a 15687 }
27185ae1 15688
9beb5fea 15689 if (!found && IS_G4X(dev_priv))
c39055b0 15690 intel_dp_init(dev_priv, DP_B, PORT_B);
725e30ad 15691 }
13520b05
KH
15692
15693 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 15694
e2debe91 15695 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 15696 DRM_DEBUG_KMS("probing SDVOC\n");
c39055b0 15697 found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
b01f2c3a 15698 }
27185ae1 15699
e2debe91 15700 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 15701
9beb5fea 15702 if (IS_G4X(dev_priv)) {
b01f2c3a 15703 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
c39055b0 15704 intel_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
b01f2c3a 15705 }
9beb5fea 15706 if (IS_G4X(dev_priv))
c39055b0 15707 intel_dp_init(dev_priv, DP_C, PORT_C);
725e30ad 15708 }
27185ae1 15709
9beb5fea 15710 if (IS_G4X(dev_priv) && (I915_READ(DP_D) & DP_DETECTED))
c39055b0 15711 intel_dp_init(dev_priv, DP_D, PORT_D);
5db94019 15712 } else if (IS_GEN2(dev_priv))
c39055b0 15713 intel_dvo_init(dev_priv);
79e53945 15714
56b857a5 15715 if (SUPPORTS_TV(dev_priv))
c39055b0 15716 intel_tv_init(dev_priv);
79e53945 15717
c39055b0 15718 intel_psr_init(dev_priv);
7c8f8a70 15719
c39055b0 15720 for_each_intel_encoder(&dev_priv->drm, encoder) {
4ef69c7a
CW
15721 encoder->base.possible_crtcs = encoder->crtc_mask;
15722 encoder->base.possible_clones =
66a9278e 15723 intel_encoder_clones(encoder);
79e53945 15724 }
47356eb6 15725
c39055b0 15726 intel_init_pch_refclk(dev_priv);
270b3042 15727
c39055b0 15728 drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
79e53945
JB
15729}
15730
15731static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
15732{
60a5ca01 15733 struct drm_device *dev = fb->dev;
79e53945 15734 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 15735
ef2d633e 15736 drm_framebuffer_cleanup(fb);
60a5ca01 15737 mutex_lock(&dev->struct_mutex);
ef2d633e 15738 WARN_ON(!intel_fb->obj->framebuffer_references--);
f8c417cd 15739 i915_gem_object_put(intel_fb->obj);
60a5ca01 15740 mutex_unlock(&dev->struct_mutex);
79e53945
JB
15741 kfree(intel_fb);
15742}
15743
15744static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 15745 struct drm_file *file,
79e53945
JB
15746 unsigned int *handle)
15747{
15748 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 15749 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 15750
cc917ab4
CW
15751 if (obj->userptr.mm) {
15752 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
15753 return -EINVAL;
15754 }
15755
05394f39 15756 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
15757}
15758
86c98588
RV
15759static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
15760 struct drm_file *file,
15761 unsigned flags, unsigned color,
15762 struct drm_clip_rect *clips,
15763 unsigned num_clips)
15764{
15765 struct drm_device *dev = fb->dev;
15766 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
15767 struct drm_i915_gem_object *obj = intel_fb->obj;
15768
15769 mutex_lock(&dev->struct_mutex);
a6a7cc4b
CW
15770 if (obj->pin_display && obj->cache_dirty)
15771 i915_gem_clflush_object(obj, true);
74b4ea1e 15772 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
86c98588
RV
15773 mutex_unlock(&dev->struct_mutex);
15774
15775 return 0;
15776}
15777
79e53945
JB
15778static const struct drm_framebuffer_funcs intel_fb_funcs = {
15779 .destroy = intel_user_framebuffer_destroy,
15780 .create_handle = intel_user_framebuffer_create_handle,
86c98588 15781 .dirty = intel_user_framebuffer_dirty,
79e53945
JB
15782};
15783
b321803d 15784static
920a14b2
TU
15785u32 intel_fb_pitch_limit(struct drm_i915_private *dev_priv,
15786 uint64_t fb_modifier, uint32_t pixel_format)
b321803d 15787{
920a14b2 15788 u32 gen = INTEL_INFO(dev_priv)->gen;
b321803d
DL
15789
15790 if (gen >= 9) {
ac484963
VS
15791 int cpp = drm_format_plane_cpp(pixel_format, 0);
15792
b321803d
DL
15793 /* "The stride in bytes must not exceed the of the size of 8K
15794 * pixels and 32K bytes."
15795 */
ac484963 15796 return min(8192 * cpp, 32768);
920a14b2
TU
15797 } else if (gen >= 5 && !IS_VALLEYVIEW(dev_priv) &&
15798 !IS_CHERRYVIEW(dev_priv)) {
b321803d
DL
15799 return 32*1024;
15800 } else if (gen >= 4) {
15801 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
15802 return 16*1024;
15803 else
15804 return 32*1024;
15805 } else if (gen >= 3) {
15806 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
15807 return 8*1024;
15808 else
15809 return 16*1024;
15810 } else {
15811 /* XXX DSPC is limited to 4k tiled */
15812 return 8*1024;
15813 }
15814}
15815
b5ea642a
DV
15816static int intel_framebuffer_init(struct drm_device *dev,
15817 struct intel_framebuffer *intel_fb,
15818 struct drm_mode_fb_cmd2 *mode_cmd,
15819 struct drm_i915_gem_object *obj)
79e53945 15820{
7b49f948 15821 struct drm_i915_private *dev_priv = to_i915(dev);
c2ff7370 15822 unsigned int tiling = i915_gem_object_get_tiling(obj);
79e53945 15823 int ret;
b321803d 15824 u32 pitch_limit, stride_alignment;
b3c11ac2 15825 struct drm_format_name_buf format_name;
79e53945 15826
dd4916c5
DV
15827 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
15828
2a80eada 15829 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
c2ff7370
VS
15830 /*
15831 * If there's a fence, enforce that
15832 * the fb modifier and tiling mode match.
15833 */
15834 if (tiling != I915_TILING_NONE &&
15835 tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
2a80eada
DV
15836 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
15837 return -EINVAL;
15838 }
15839 } else {
c2ff7370 15840 if (tiling == I915_TILING_X) {
2a80eada 15841 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
c2ff7370 15842 } else if (tiling == I915_TILING_Y) {
2a80eada
DV
15843 DRM_DEBUG("No Y tiling for legacy addfb\n");
15844 return -EINVAL;
15845 }
15846 }
15847
9a8f0a12
TU
15848 /* Passed in modifier sanity checking. */
15849 switch (mode_cmd->modifier[0]) {
15850 case I915_FORMAT_MOD_Y_TILED:
15851 case I915_FORMAT_MOD_Yf_TILED:
6315b5d3 15852 if (INTEL_GEN(dev_priv) < 9) {
9a8f0a12
TU
15853 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
15854 mode_cmd->modifier[0]);
15855 return -EINVAL;
15856 }
15857 case DRM_FORMAT_MOD_NONE:
15858 case I915_FORMAT_MOD_X_TILED:
15859 break;
15860 default:
c0f40428
JB
15861 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
15862 mode_cmd->modifier[0]);
57cd6508 15863 return -EINVAL;
c16ed4be 15864 }
57cd6508 15865
c2ff7370
VS
15866 /*
15867 * gen2/3 display engine uses the fence if present,
15868 * so the tiling mode must match the fb modifier exactly.
15869 */
15870 if (INTEL_INFO(dev_priv)->gen < 4 &&
15871 tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
15872 DRM_DEBUG("tiling_mode must match fb modifier exactly on gen2/3\n");
15873 return -EINVAL;
15874 }
15875
7b49f948
VS
15876 stride_alignment = intel_fb_stride_alignment(dev_priv,
15877 mode_cmd->modifier[0],
b321803d
DL
15878 mode_cmd->pixel_format);
15879 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
15880 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
15881 mode_cmd->pitches[0], stride_alignment);
57cd6508 15882 return -EINVAL;
c16ed4be 15883 }
57cd6508 15884
920a14b2 15885 pitch_limit = intel_fb_pitch_limit(dev_priv, mode_cmd->modifier[0],
b321803d 15886 mode_cmd->pixel_format);
a35cdaa0 15887 if (mode_cmd->pitches[0] > pitch_limit) {
b321803d
DL
15888 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
15889 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
2a80eada 15890 "tiled" : "linear",
a35cdaa0 15891 mode_cmd->pitches[0], pitch_limit);
5d7bd705 15892 return -EINVAL;
c16ed4be 15893 }
5d7bd705 15894
c2ff7370
VS
15895 /*
15896 * If there's a fence, enforce that
15897 * the fb pitch and fence stride match.
15898 */
15899 if (tiling != I915_TILING_NONE &&
3e510a8e 15900 mode_cmd->pitches[0] != i915_gem_object_get_stride(obj)) {
c16ed4be 15901 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
3e510a8e
CW
15902 mode_cmd->pitches[0],
15903 i915_gem_object_get_stride(obj));
5d7bd705 15904 return -EINVAL;
c16ed4be 15905 }
5d7bd705 15906
57779d06 15907 /* Reject formats not supported by any plane early. */
308e5bcb 15908 switch (mode_cmd->pixel_format) {
57779d06 15909 case DRM_FORMAT_C8:
04b3924d
VS
15910 case DRM_FORMAT_RGB565:
15911 case DRM_FORMAT_XRGB8888:
15912 case DRM_FORMAT_ARGB8888:
57779d06
VS
15913 break;
15914 case DRM_FORMAT_XRGB1555:
6315b5d3 15915 if (INTEL_GEN(dev_priv) > 3) {
b3c11ac2
EE
15916 DRM_DEBUG("unsupported pixel format: %s\n",
15917 drm_get_format_name(mode_cmd->pixel_format, &format_name));
57779d06 15918 return -EINVAL;
c16ed4be 15919 }
57779d06 15920 break;
57779d06 15921 case DRM_FORMAT_ABGR8888:
920a14b2 15922 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
6315b5d3 15923 INTEL_GEN(dev_priv) < 9) {
b3c11ac2
EE
15924 DRM_DEBUG("unsupported pixel format: %s\n",
15925 drm_get_format_name(mode_cmd->pixel_format, &format_name));
6c0fd451
DL
15926 return -EINVAL;
15927 }
15928 break;
15929 case DRM_FORMAT_XBGR8888:
04b3924d 15930 case DRM_FORMAT_XRGB2101010:
57779d06 15931 case DRM_FORMAT_XBGR2101010:
6315b5d3 15932 if (INTEL_GEN(dev_priv) < 4) {
b3c11ac2
EE
15933 DRM_DEBUG("unsupported pixel format: %s\n",
15934 drm_get_format_name(mode_cmd->pixel_format, &format_name));
57779d06 15935 return -EINVAL;
c16ed4be 15936 }
b5626747 15937 break;
7531208b 15938 case DRM_FORMAT_ABGR2101010:
920a14b2 15939 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
b3c11ac2
EE
15940 DRM_DEBUG("unsupported pixel format: %s\n",
15941 drm_get_format_name(mode_cmd->pixel_format, &format_name));
7531208b
DL
15942 return -EINVAL;
15943 }
15944 break;
04b3924d
VS
15945 case DRM_FORMAT_YUYV:
15946 case DRM_FORMAT_UYVY:
15947 case DRM_FORMAT_YVYU:
15948 case DRM_FORMAT_VYUY:
6315b5d3 15949 if (INTEL_GEN(dev_priv) < 5) {
b3c11ac2
EE
15950 DRM_DEBUG("unsupported pixel format: %s\n",
15951 drm_get_format_name(mode_cmd->pixel_format, &format_name));
57779d06 15952 return -EINVAL;
c16ed4be 15953 }
57cd6508
CW
15954 break;
15955 default:
b3c11ac2
EE
15956 DRM_DEBUG("unsupported pixel format: %s\n",
15957 drm_get_format_name(mode_cmd->pixel_format, &format_name));
57cd6508
CW
15958 return -EINVAL;
15959 }
15960
90f9a336
VS
15961 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
15962 if (mode_cmd->offsets[0] != 0)
15963 return -EINVAL;
15964
c7d73f6a
DV
15965 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
15966 intel_fb->obj = obj;
15967
6687c906
VS
15968 ret = intel_fill_fb_info(dev_priv, &intel_fb->base);
15969 if (ret)
15970 return ret;
2d7a215f 15971
79e53945
JB
15972 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
15973 if (ret) {
15974 DRM_ERROR("framebuffer init failed %d\n", ret);
15975 return ret;
15976 }
15977
0b05e1e0
VS
15978 intel_fb->obj->framebuffer_references++;
15979
79e53945
JB
15980 return 0;
15981}
15982
79e53945
JB
15983static struct drm_framebuffer *
15984intel_user_framebuffer_create(struct drm_device *dev,
15985 struct drm_file *filp,
1eb83451 15986 const struct drm_mode_fb_cmd2 *user_mode_cmd)
79e53945 15987{
dcb1394e 15988 struct drm_framebuffer *fb;
05394f39 15989 struct drm_i915_gem_object *obj;
76dc3769 15990 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
79e53945 15991
03ac0642
CW
15992 obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
15993 if (!obj)
cce13ff7 15994 return ERR_PTR(-ENOENT);
79e53945 15995
92907cbb 15996 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
dcb1394e 15997 if (IS_ERR(fb))
f0cd5182 15998 i915_gem_object_put(obj);
dcb1394e
LW
15999
16000 return fb;
79e53945
JB
16001}
16002
79e53945 16003static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 16004 .fb_create = intel_user_framebuffer_create,
0632fef6 16005 .output_poll_changed = intel_fbdev_output_poll_changed,
5ee67f1c
MR
16006 .atomic_check = intel_atomic_check,
16007 .atomic_commit = intel_atomic_commit,
de419ab6
ML
16008 .atomic_state_alloc = intel_atomic_state_alloc,
16009 .atomic_state_clear = intel_atomic_state_clear,
79e53945
JB
16010};
16011
88212941
ID
16012/**
16013 * intel_init_display_hooks - initialize the display modesetting hooks
16014 * @dev_priv: device private
16015 */
16016void intel_init_display_hooks(struct drm_i915_private *dev_priv)
e70236a8 16017{
88212941 16018 if (INTEL_INFO(dev_priv)->gen >= 9) {
bc8d7dff 16019 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
16020 dev_priv->display.get_initial_plane_config =
16021 skylake_get_initial_plane_config;
bc8d7dff
DL
16022 dev_priv->display.crtc_compute_clock =
16023 haswell_crtc_compute_clock;
16024 dev_priv->display.crtc_enable = haswell_crtc_enable;
16025 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 16026 } else if (HAS_DDI(dev_priv)) {
0e8ffe1b 16027 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
16028 dev_priv->display.get_initial_plane_config =
16029 ironlake_get_initial_plane_config;
797d0259
ACO
16030 dev_priv->display.crtc_compute_clock =
16031 haswell_crtc_compute_clock;
4f771f10
PZ
16032 dev_priv->display.crtc_enable = haswell_crtc_enable;
16033 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 16034 } else if (HAS_PCH_SPLIT(dev_priv)) {
0e8ffe1b 16035 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
5724dbd1
DL
16036 dev_priv->display.get_initial_plane_config =
16037 ironlake_get_initial_plane_config;
3fb37703
ACO
16038 dev_priv->display.crtc_compute_clock =
16039 ironlake_crtc_compute_clock;
76e5a89c
DV
16040 dev_priv->display.crtc_enable = ironlake_crtc_enable;
16041 dev_priv->display.crtc_disable = ironlake_crtc_disable;
65b3d6a9 16042 } else if (IS_CHERRYVIEW(dev_priv)) {
89b667f8 16043 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
16044 dev_priv->display.get_initial_plane_config =
16045 i9xx_get_initial_plane_config;
65b3d6a9
ACO
16046 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
16047 dev_priv->display.crtc_enable = valleyview_crtc_enable;
16048 dev_priv->display.crtc_disable = i9xx_crtc_disable;
16049 } else if (IS_VALLEYVIEW(dev_priv)) {
16050 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
16051 dev_priv->display.get_initial_plane_config =
16052 i9xx_get_initial_plane_config;
16053 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
89b667f8
JB
16054 dev_priv->display.crtc_enable = valleyview_crtc_enable;
16055 dev_priv->display.crtc_disable = i9xx_crtc_disable;
19ec6693
ACO
16056 } else if (IS_G4X(dev_priv)) {
16057 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
16058 dev_priv->display.get_initial_plane_config =
16059 i9xx_get_initial_plane_config;
16060 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
16061 dev_priv->display.crtc_enable = i9xx_crtc_enable;
16062 dev_priv->display.crtc_disable = i9xx_crtc_disable;
70e8aa21
ACO
16063 } else if (IS_PINEVIEW(dev_priv)) {
16064 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
16065 dev_priv->display.get_initial_plane_config =
16066 i9xx_get_initial_plane_config;
16067 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
16068 dev_priv->display.crtc_enable = i9xx_crtc_enable;
16069 dev_priv->display.crtc_disable = i9xx_crtc_disable;
81c97f52 16070 } else if (!IS_GEN2(dev_priv)) {
0e8ffe1b 16071 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
16072 dev_priv->display.get_initial_plane_config =
16073 i9xx_get_initial_plane_config;
d6dfee7a 16074 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
76e5a89c
DV
16075 dev_priv->display.crtc_enable = i9xx_crtc_enable;
16076 dev_priv->display.crtc_disable = i9xx_crtc_disable;
81c97f52
ACO
16077 } else {
16078 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
16079 dev_priv->display.get_initial_plane_config =
16080 i9xx_get_initial_plane_config;
16081 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
16082 dev_priv->display.crtc_enable = i9xx_crtc_enable;
16083 dev_priv->display.crtc_disable = i9xx_crtc_disable;
f564048e 16084 }
e70236a8 16085
e70236a8 16086 /* Returns the core display clock speed */
88212941 16087 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
1652d19e
VS
16088 dev_priv->display.get_display_clock_speed =
16089 skylake_get_display_clock_speed;
89b3c3c7 16090 else if (IS_GEN9_LP(dev_priv))
acd3f3d3
BP
16091 dev_priv->display.get_display_clock_speed =
16092 broxton_get_display_clock_speed;
88212941 16093 else if (IS_BROADWELL(dev_priv))
1652d19e
VS
16094 dev_priv->display.get_display_clock_speed =
16095 broadwell_get_display_clock_speed;
88212941 16096 else if (IS_HASWELL(dev_priv))
1652d19e
VS
16097 dev_priv->display.get_display_clock_speed =
16098 haswell_get_display_clock_speed;
88212941 16099 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
25eb05fc
JB
16100 dev_priv->display.get_display_clock_speed =
16101 valleyview_get_display_clock_speed;
88212941 16102 else if (IS_GEN5(dev_priv))
b37a6434
VS
16103 dev_priv->display.get_display_clock_speed =
16104 ilk_get_display_clock_speed;
88212941
ID
16105 else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
16106 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
e70236a8
JB
16107 dev_priv->display.get_display_clock_speed =
16108 i945_get_display_clock_speed;
88212941 16109 else if (IS_GM45(dev_priv))
34edce2f
VS
16110 dev_priv->display.get_display_clock_speed =
16111 gm45_get_display_clock_speed;
88212941 16112 else if (IS_CRESTLINE(dev_priv))
34edce2f
VS
16113 dev_priv->display.get_display_clock_speed =
16114 i965gm_get_display_clock_speed;
88212941 16115 else if (IS_PINEVIEW(dev_priv))
34edce2f
VS
16116 dev_priv->display.get_display_clock_speed =
16117 pnv_get_display_clock_speed;
88212941 16118 else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
34edce2f
VS
16119 dev_priv->display.get_display_clock_speed =
16120 g33_get_display_clock_speed;
88212941 16121 else if (IS_I915G(dev_priv))
e70236a8
JB
16122 dev_priv->display.get_display_clock_speed =
16123 i915_get_display_clock_speed;
88212941 16124 else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
e70236a8
JB
16125 dev_priv->display.get_display_clock_speed =
16126 i9xx_misc_get_display_clock_speed;
88212941 16127 else if (IS_I915GM(dev_priv))
e70236a8
JB
16128 dev_priv->display.get_display_clock_speed =
16129 i915gm_get_display_clock_speed;
88212941 16130 else if (IS_I865G(dev_priv))
e70236a8
JB
16131 dev_priv->display.get_display_clock_speed =
16132 i865_get_display_clock_speed;
88212941 16133 else if (IS_I85X(dev_priv))
e70236a8 16134 dev_priv->display.get_display_clock_speed =
1b1d2716 16135 i85x_get_display_clock_speed;
623e01e5 16136 else { /* 830 */
88212941 16137 WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
e70236a8
JB
16138 dev_priv->display.get_display_clock_speed =
16139 i830_get_display_clock_speed;
623e01e5 16140 }
e70236a8 16141
88212941 16142 if (IS_GEN5(dev_priv)) {
3bb11b53 16143 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
88212941 16144 } else if (IS_GEN6(dev_priv)) {
3bb11b53 16145 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
88212941 16146 } else if (IS_IVYBRIDGE(dev_priv)) {
3bb11b53
SJ
16147 /* FIXME: detect B0+ stepping and use auto training */
16148 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
88212941 16149 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
3bb11b53 16150 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
445e780b
VS
16151 }
16152
16153 if (IS_BROADWELL(dev_priv)) {
16154 dev_priv->display.modeset_commit_cdclk =
16155 broadwell_modeset_commit_cdclk;
16156 dev_priv->display.modeset_calc_cdclk =
16157 broadwell_modeset_calc_cdclk;
88212941 16158 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
27c329ed
ML
16159 dev_priv->display.modeset_commit_cdclk =
16160 valleyview_modeset_commit_cdclk;
16161 dev_priv->display.modeset_calc_cdclk =
16162 valleyview_modeset_calc_cdclk;
89b3c3c7 16163 } else if (IS_GEN9_LP(dev_priv)) {
27c329ed 16164 dev_priv->display.modeset_commit_cdclk =
324513c0 16165 bxt_modeset_commit_cdclk;
27c329ed 16166 dev_priv->display.modeset_calc_cdclk =
324513c0 16167 bxt_modeset_calc_cdclk;
c89e39f3
CT
16168 } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
16169 dev_priv->display.modeset_commit_cdclk =
16170 skl_modeset_commit_cdclk;
16171 dev_priv->display.modeset_calc_cdclk =
16172 skl_modeset_calc_cdclk;
e70236a8 16173 }
5a21b665 16174
27082493
L
16175 if (dev_priv->info.gen >= 9)
16176 dev_priv->display.update_crtcs = skl_update_crtcs;
16177 else
16178 dev_priv->display.update_crtcs = intel_update_crtcs;
16179
5a21b665
DV
16180 switch (INTEL_INFO(dev_priv)->gen) {
16181 case 2:
16182 dev_priv->display.queue_flip = intel_gen2_queue_flip;
16183 break;
16184
16185 case 3:
16186 dev_priv->display.queue_flip = intel_gen3_queue_flip;
16187 break;
16188
16189 case 4:
16190 case 5:
16191 dev_priv->display.queue_flip = intel_gen4_queue_flip;
16192 break;
16193
16194 case 6:
16195 dev_priv->display.queue_flip = intel_gen6_queue_flip;
16196 break;
16197 case 7:
16198 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
16199 dev_priv->display.queue_flip = intel_gen7_queue_flip;
16200 break;
16201 case 9:
16202 /* Drop through - unsupported since execlist only. */
16203 default:
16204 /* Default just returns -ENODEV to indicate unsupported */
16205 dev_priv->display.queue_flip = intel_default_queue_flip;
16206 }
e70236a8
JB
16207}
16208
b690e96c
JB
16209/*
16210 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
16211 * resume, or other times. This quirk makes sure that's the case for
16212 * affected systems.
16213 */
0206e353 16214static void quirk_pipea_force(struct drm_device *dev)
b690e96c 16215{
fac5e23e 16216 struct drm_i915_private *dev_priv = to_i915(dev);
b690e96c
JB
16217
16218 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
bc0daf48 16219 DRM_INFO("applying pipe a force quirk\n");
b690e96c
JB
16220}
16221
b6b5d049
VS
16222static void quirk_pipeb_force(struct drm_device *dev)
16223{
fac5e23e 16224 struct drm_i915_private *dev_priv = to_i915(dev);
b6b5d049
VS
16225
16226 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
16227 DRM_INFO("applying pipe b force quirk\n");
16228}
16229
435793df
KP
16230/*
16231 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
16232 */
16233static void quirk_ssc_force_disable(struct drm_device *dev)
16234{
fac5e23e 16235 struct drm_i915_private *dev_priv = to_i915(dev);
435793df 16236 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 16237 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
16238}
16239
4dca20ef 16240/*
5a15ab5b
CE
16241 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
16242 * brightness value
4dca20ef
CE
16243 */
16244static void quirk_invert_brightness(struct drm_device *dev)
16245{
fac5e23e 16246 struct drm_i915_private *dev_priv = to_i915(dev);
4dca20ef 16247 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 16248 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
16249}
16250
9c72cc6f
SD
16251/* Some VBT's incorrectly indicate no backlight is present */
16252static void quirk_backlight_present(struct drm_device *dev)
16253{
fac5e23e 16254 struct drm_i915_private *dev_priv = to_i915(dev);
9c72cc6f
SD
16255 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
16256 DRM_INFO("applying backlight present quirk\n");
16257}
16258
b690e96c
JB
16259struct intel_quirk {
16260 int device;
16261 int subsystem_vendor;
16262 int subsystem_device;
16263 void (*hook)(struct drm_device *dev);
16264};
16265
5f85f176
EE
16266/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
16267struct intel_dmi_quirk {
16268 void (*hook)(struct drm_device *dev);
16269 const struct dmi_system_id (*dmi_id_list)[];
16270};
16271
16272static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
16273{
16274 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
16275 return 1;
16276}
16277
16278static const struct intel_dmi_quirk intel_dmi_quirks[] = {
16279 {
16280 .dmi_id_list = &(const struct dmi_system_id[]) {
16281 {
16282 .callback = intel_dmi_reverse_brightness,
16283 .ident = "NCR Corporation",
16284 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
16285 DMI_MATCH(DMI_PRODUCT_NAME, ""),
16286 },
16287 },
16288 { } /* terminating entry */
16289 },
16290 .hook = quirk_invert_brightness,
16291 },
16292};
16293
c43b5634 16294static struct intel_quirk intel_quirks[] = {
b690e96c
JB
16295 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
16296 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
16297
b690e96c
JB
16298 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
16299 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
16300
5f080c0f
VS
16301 /* 830 needs to leave pipe A & dpll A up */
16302 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
16303
b6b5d049
VS
16304 /* 830 needs to leave pipe B & dpll B up */
16305 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
16306
435793df
KP
16307 /* Lenovo U160 cannot use SSC on LVDS */
16308 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
16309
16310 /* Sony Vaio Y cannot use SSC on LVDS */
16311 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 16312
be505f64
AH
16313 /* Acer Aspire 5734Z must invert backlight brightness */
16314 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
16315
16316 /* Acer/eMachines G725 */
16317 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
16318
16319 /* Acer/eMachines e725 */
16320 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
16321
16322 /* Acer/Packard Bell NCL20 */
16323 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
16324
16325 /* Acer Aspire 4736Z */
16326 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
16327
16328 /* Acer Aspire 5336 */
16329 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
2e93a1aa
SD
16330
16331 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
16332 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
d4967d8c 16333
dfb3d47b
SD
16334 /* Acer C720 Chromebook (Core i3 4005U) */
16335 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
16336
b2a9601c 16337 /* Apple Macbook 2,1 (Core 2 T7400) */
16338 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
16339
1b9448b0
JN
16340 /* Apple Macbook 4,1 */
16341 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
16342
d4967d8c
SD
16343 /* Toshiba CB35 Chromebook (Celeron 2955U) */
16344 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
724cb06f
SD
16345
16346 /* HP Chromebook 14 (Celeron 2955U) */
16347 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
cf6f0af9
JN
16348
16349 /* Dell Chromebook 11 */
16350 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
9be64eee
JN
16351
16352 /* Dell Chromebook 11 (2015 version) */
16353 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
b690e96c
JB
16354};
16355
16356static void intel_init_quirks(struct drm_device *dev)
16357{
16358 struct pci_dev *d = dev->pdev;
16359 int i;
16360
16361 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
16362 struct intel_quirk *q = &intel_quirks[i];
16363
16364 if (d->device == q->device &&
16365 (d->subsystem_vendor == q->subsystem_vendor ||
16366 q->subsystem_vendor == PCI_ANY_ID) &&
16367 (d->subsystem_device == q->subsystem_device ||
16368 q->subsystem_device == PCI_ANY_ID))
16369 q->hook(dev);
16370 }
5f85f176
EE
16371 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
16372 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
16373 intel_dmi_quirks[i].hook(dev);
16374 }
b690e96c
JB
16375}
16376
9cce37f4 16377/* Disable the VGA plane that we never use */
29b74b7f 16378static void i915_disable_vga(struct drm_i915_private *dev_priv)
9cce37f4 16379{
52a05c30 16380 struct pci_dev *pdev = dev_priv->drm.pdev;
9cce37f4 16381 u8 sr1;
920a14b2 16382 i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
9cce37f4 16383
2b37c616 16384 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
52a05c30 16385 vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 16386 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
16387 sr1 = inb(VGA_SR_DATA);
16388 outb(sr1 | 1<<5, VGA_SR_DATA);
52a05c30 16389 vga_put(pdev, VGA_RSRC_LEGACY_IO);
9cce37f4
JB
16390 udelay(300);
16391
01f5a626 16392 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9cce37f4
JB
16393 POSTING_READ(vga_reg);
16394}
16395
f817586c
DV
16396void intel_modeset_init_hw(struct drm_device *dev)
16397{
fac5e23e 16398 struct drm_i915_private *dev_priv = to_i915(dev);
1a617b77 16399
4c75b940 16400 intel_update_cdclk(dev_priv);
1a617b77
ML
16401
16402 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
16403
46f16e63 16404 intel_init_clock_gating(dev_priv);
f817586c
DV
16405}
16406
d93c0372
MR
16407/*
16408 * Calculate what we think the watermarks should be for the state we've read
16409 * out of the hardware and then immediately program those watermarks so that
16410 * we ensure the hardware settings match our internal state.
16411 *
16412 * We can calculate what we think WM's should be by creating a duplicate of the
16413 * current state (which was constructed during hardware readout) and running it
16414 * through the atomic check code to calculate new watermark values in the
16415 * state object.
16416 */
16417static void sanitize_watermarks(struct drm_device *dev)
16418{
16419 struct drm_i915_private *dev_priv = to_i915(dev);
16420 struct drm_atomic_state *state;
ccf010fb 16421 struct intel_atomic_state *intel_state;
d93c0372
MR
16422 struct drm_crtc *crtc;
16423 struct drm_crtc_state *cstate;
16424 struct drm_modeset_acquire_ctx ctx;
16425 int ret;
16426 int i;
16427
16428 /* Only supported on platforms that use atomic watermark design */
ed4a6a7c 16429 if (!dev_priv->display.optimize_watermarks)
d93c0372
MR
16430 return;
16431
16432 /*
16433 * We need to hold connection_mutex before calling duplicate_state so
16434 * that the connector loop is protected.
16435 */
16436 drm_modeset_acquire_init(&ctx, 0);
16437retry:
0cd1262d 16438 ret = drm_modeset_lock_all_ctx(dev, &ctx);
d93c0372
MR
16439 if (ret == -EDEADLK) {
16440 drm_modeset_backoff(&ctx);
16441 goto retry;
16442 } else if (WARN_ON(ret)) {
0cd1262d 16443 goto fail;
d93c0372
MR
16444 }
16445
16446 state = drm_atomic_helper_duplicate_state(dev, &ctx);
16447 if (WARN_ON(IS_ERR(state)))
0cd1262d 16448 goto fail;
d93c0372 16449
ccf010fb
ML
16450 intel_state = to_intel_atomic_state(state);
16451
ed4a6a7c
MR
16452 /*
16453 * Hardware readout is the only time we don't want to calculate
16454 * intermediate watermarks (since we don't trust the current
16455 * watermarks).
16456 */
ccf010fb 16457 intel_state->skip_intermediate_wm = true;
ed4a6a7c 16458
d93c0372
MR
16459 ret = intel_atomic_check(dev, state);
16460 if (ret) {
16461 /*
16462 * If we fail here, it means that the hardware appears to be
16463 * programmed in a way that shouldn't be possible, given our
16464 * understanding of watermark requirements. This might mean a
16465 * mistake in the hardware readout code or a mistake in the
16466 * watermark calculations for a given platform. Raise a WARN
16467 * so that this is noticeable.
16468 *
16469 * If this actually happens, we'll have to just leave the
16470 * BIOS-programmed watermarks untouched and hope for the best.
16471 */
16472 WARN(true, "Could not determine valid watermarks for inherited state\n");
b9a1b717 16473 goto put_state;
d93c0372
MR
16474 }
16475
16476 /* Write calculated watermark values back */
d93c0372
MR
16477 for_each_crtc_in_state(state, crtc, cstate, i) {
16478 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
16479
ed4a6a7c 16480 cs->wm.need_postvbl_update = true;
ccf010fb 16481 dev_priv->display.optimize_watermarks(intel_state, cs);
d93c0372
MR
16482 }
16483
b9a1b717 16484put_state:
0853695c 16485 drm_atomic_state_put(state);
0cd1262d 16486fail:
d93c0372
MR
16487 drm_modeset_drop_locks(&ctx);
16488 drm_modeset_acquire_fini(&ctx);
16489}
16490
b079bd17 16491int intel_modeset_init(struct drm_device *dev)
79e53945 16492{
72e96d64
JL
16493 struct drm_i915_private *dev_priv = to_i915(dev);
16494 struct i915_ggtt *ggtt = &dev_priv->ggtt;
8cc87b75 16495 enum pipe pipe;
46f297fb 16496 struct intel_crtc *crtc;
79e53945
JB
16497
16498 drm_mode_config_init(dev);
16499
16500 dev->mode_config.min_width = 0;
16501 dev->mode_config.min_height = 0;
16502
019d96cb
DA
16503 dev->mode_config.preferred_depth = 24;
16504 dev->mode_config.prefer_shadow = 1;
16505
25bab385
TU
16506 dev->mode_config.allow_fb_modifiers = true;
16507
e6ecefaa 16508 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 16509
b690e96c
JB
16510 intel_init_quirks(dev);
16511
62d75df7 16512 intel_init_pm(dev_priv);
1fa61106 16513
b7f05d4a 16514 if (INTEL_INFO(dev_priv)->num_pipes == 0)
b079bd17 16515 return 0;
e3c74757 16516
69f92f67
LW
16517 /*
16518 * There may be no VBT; and if the BIOS enabled SSC we can
16519 * just keep using it to avoid unnecessary flicker. Whereas if the
16520 * BIOS isn't using it, don't assume it will work even if the VBT
16521 * indicates as much.
16522 */
6e266956 16523 if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
69f92f67
LW
16524 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
16525 DREF_SSC1_ENABLE);
16526
16527 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
16528 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
16529 bios_lvds_use_ssc ? "en" : "dis",
16530 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
16531 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
16532 }
16533 }
16534
5db94019 16535 if (IS_GEN2(dev_priv)) {
a6c45cf0
CW
16536 dev->mode_config.max_width = 2048;
16537 dev->mode_config.max_height = 2048;
5db94019 16538 } else if (IS_GEN3(dev_priv)) {
5e4d6fa7
KP
16539 dev->mode_config.max_width = 4096;
16540 dev->mode_config.max_height = 4096;
79e53945 16541 } else {
a6c45cf0
CW
16542 dev->mode_config.max_width = 8192;
16543 dev->mode_config.max_height = 8192;
79e53945 16544 }
068be561 16545
50a0bc90
TU
16546 if (IS_845G(dev_priv) || IS_I865G(dev_priv)) {
16547 dev->mode_config.cursor_width = IS_845G(dev_priv) ? 64 : 512;
dc41c154 16548 dev->mode_config.cursor_height = 1023;
5db94019 16549 } else if (IS_GEN2(dev_priv)) {
068be561
DL
16550 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
16551 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
16552 } else {
16553 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
16554 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
16555 }
16556
72e96d64 16557 dev->mode_config.fb_base = ggtt->mappable_base;
79e53945 16558
28c97730 16559 DRM_DEBUG_KMS("%d display pipe%s available.\n",
b7f05d4a
TU
16560 INTEL_INFO(dev_priv)->num_pipes,
16561 INTEL_INFO(dev_priv)->num_pipes > 1 ? "s" : "");
79e53945 16562
055e393f 16563 for_each_pipe(dev_priv, pipe) {
b079bd17
VS
16564 int ret;
16565
5ab0d85b 16566 ret = intel_crtc_init(dev_priv, pipe);
b079bd17
VS
16567 if (ret) {
16568 drm_mode_config_cleanup(dev);
16569 return ret;
16570 }
79e53945
JB
16571 }
16572
bfa7df01 16573 intel_update_czclk(dev_priv);
4c75b940 16574 intel_update_cdclk(dev_priv);
6a259b1f 16575 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
bfa7df01 16576
e72f9fbf 16577 intel_shared_dpll_init(dev);
ee7b9f93 16578
b2045352 16579 if (dev_priv->max_cdclk_freq == 0)
4c75b940 16580 intel_update_max_cdclk(dev_priv);
b2045352 16581
9cce37f4 16582 /* Just disable it once at startup */
29b74b7f 16583 i915_disable_vga(dev_priv);
c39055b0 16584 intel_setup_outputs(dev_priv);
11be49eb 16585
6e9f798d 16586 drm_modeset_lock_all(dev);
043e9bda 16587 intel_modeset_setup_hw_state(dev);
6e9f798d 16588 drm_modeset_unlock_all(dev);
46f297fb 16589
d3fcc808 16590 for_each_intel_crtc(dev, crtc) {
eeebeac5
ML
16591 struct intel_initial_plane_config plane_config = {};
16592
46f297fb
JB
16593 if (!crtc->active)
16594 continue;
16595
46f297fb 16596 /*
46f297fb
JB
16597 * Note that reserving the BIOS fb up front prevents us
16598 * from stuffing other stolen allocations like the ring
16599 * on top. This prevents some ugliness at boot time, and
16600 * can even allow for smooth boot transitions if the BIOS
16601 * fb is large enough for the active pipe configuration.
16602 */
eeebeac5
ML
16603 dev_priv->display.get_initial_plane_config(crtc,
16604 &plane_config);
16605
16606 /*
16607 * If the fb is shared between multiple heads, we'll
16608 * just get the first one.
16609 */
16610 intel_find_initial_plane_obj(crtc, &plane_config);
46f297fb 16611 }
d93c0372
MR
16612
16613 /*
16614 * Make sure hardware watermarks really match the state we read out.
16615 * Note that we need to do this after reconstructing the BIOS fb's
16616 * since the watermark calculation done here will use pstate->fb.
16617 */
16618 sanitize_watermarks(dev);
b079bd17
VS
16619
16620 return 0;
2c7111db
CW
16621}
16622
7fad798e
DV
16623static void intel_enable_pipe_a(struct drm_device *dev)
16624{
16625 struct intel_connector *connector;
16626 struct drm_connector *crt = NULL;
16627 struct intel_load_detect_pipe load_detect_temp;
208bf9fd 16628 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
7fad798e
DV
16629
16630 /* We can't just switch on the pipe A, we need to set things up with a
16631 * proper mode and output configuration. As a gross hack, enable pipe A
16632 * by enabling the load detect pipe once. */
3a3371ff 16633 for_each_intel_connector(dev, connector) {
7fad798e
DV
16634 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
16635 crt = &connector->base;
16636 break;
16637 }
16638 }
16639
16640 if (!crt)
16641 return;
16642
208bf9fd 16643 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
49172fee 16644 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
7fad798e
DV
16645}
16646
fa555837
DV
16647static bool
16648intel_check_plane_mapping(struct intel_crtc *crtc)
16649{
b7f05d4a 16650 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
649636ef 16651 u32 val;
fa555837 16652
b7f05d4a 16653 if (INTEL_INFO(dev_priv)->num_pipes == 1)
fa555837
DV
16654 return true;
16655
649636ef 16656 val = I915_READ(DSPCNTR(!crtc->plane));
fa555837
DV
16657
16658 if ((val & DISPLAY_PLANE_ENABLE) &&
16659 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
16660 return false;
16661
16662 return true;
16663}
16664
02e93c35
VS
16665static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
16666{
16667 struct drm_device *dev = crtc->base.dev;
16668 struct intel_encoder *encoder;
16669
16670 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
16671 return true;
16672
16673 return false;
16674}
16675
496b0fc3
ML
16676static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
16677{
16678 struct drm_device *dev = encoder->base.dev;
16679 struct intel_connector *connector;
16680
16681 for_each_connector_on_encoder(dev, &encoder->base, connector)
16682 return connector;
16683
16684 return NULL;
16685}
16686
a168f5b3
VS
16687static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
16688 enum transcoder pch_transcoder)
16689{
16690 return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
16691 (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == TRANSCODER_A);
16692}
16693
24929352
DV
16694static void intel_sanitize_crtc(struct intel_crtc *crtc)
16695{
16696 struct drm_device *dev = crtc->base.dev;
fac5e23e 16697 struct drm_i915_private *dev_priv = to_i915(dev);
4d1de975 16698 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
24929352 16699
24929352 16700 /* Clear any frame start delays used for debugging left by the BIOS */
4d1de975
JN
16701 if (!transcoder_is_dsi(cpu_transcoder)) {
16702 i915_reg_t reg = PIPECONF(cpu_transcoder);
16703
16704 I915_WRITE(reg,
16705 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
16706 }
24929352 16707
d3eaf884 16708 /* restore vblank interrupts to correct state */
9625604c 16709 drm_crtc_vblank_reset(&crtc->base);
d297e103 16710 if (crtc->active) {
f9cd7b88
VS
16711 struct intel_plane *plane;
16712
9625604c 16713 drm_crtc_vblank_on(&crtc->base);
f9cd7b88
VS
16714
16715 /* Disable everything but the primary plane */
16716 for_each_intel_plane_on_crtc(dev, crtc, plane) {
16717 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
16718 continue;
16719
16720 plane->disable_plane(&plane->base, &crtc->base);
16721 }
9625604c 16722 }
d3eaf884 16723
24929352 16724 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
DV
16725 * disable the crtc (and hence change the state) if it is wrong. Note
16726 * that gen4+ has a fixed plane -> pipe mapping. */
6315b5d3 16727 if (INTEL_GEN(dev_priv) < 4 && !intel_check_plane_mapping(crtc)) {
24929352
DV
16728 bool plane;
16729
78108b7c
VS
16730 DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
16731 crtc->base.base.id, crtc->base.name);
24929352
DV
16732
16733 /* Pipe has the wrong plane attached and the plane is active.
16734 * Temporarily change the plane mapping and disable everything
16735 * ... */
16736 plane = crtc->plane;
936e71e3 16737 to_intel_plane_state(crtc->base.primary->state)->base.visible = true;
24929352 16738 crtc->plane = !plane;
b17d48e2 16739 intel_crtc_disable_noatomic(&crtc->base);
24929352 16740 crtc->plane = plane;
24929352 16741 }
24929352 16742
7fad798e
DV
16743 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
16744 crtc->pipe == PIPE_A && !crtc->active) {
16745 /* BIOS forgot to enable pipe A, this mostly happens after
16746 * resume. Force-enable the pipe to fix this, the update_dpms
16747 * call below we restore the pipe to the right state, but leave
16748 * the required bits on. */
16749 intel_enable_pipe_a(dev);
16750 }
16751
24929352
DV
16752 /* Adjust the state of the output pipe according to whether we
16753 * have active connectors/encoders. */
842e0307 16754 if (crtc->active && !intel_crtc_has_encoders(crtc))
b17d48e2 16755 intel_crtc_disable_noatomic(&crtc->base);
24929352 16756
49cff963 16757 if (crtc->active || HAS_GMCH_DISPLAY(dev_priv)) {
4cc31489
DV
16758 /*
16759 * We start out with underrun reporting disabled to avoid races.
16760 * For correct bookkeeping mark this on active crtcs.
16761 *
c5ab3bc0
DV
16762 * Also on gmch platforms we dont have any hardware bits to
16763 * disable the underrun reporting. Which means we need to start
16764 * out with underrun reporting disabled also on inactive pipes,
16765 * since otherwise we'll complain about the garbage we read when
16766 * e.g. coming up after runtime pm.
16767 *
4cc31489
DV
16768 * No protection against concurrent access is required - at
16769 * worst a fifo underrun happens which also sets this to false.
16770 */
16771 crtc->cpu_fifo_underrun_disabled = true;
a168f5b3
VS
16772 /*
16773 * We track the PCH trancoder underrun reporting state
16774 * within the crtc. With crtc for pipe A housing the underrun
16775 * reporting state for PCH transcoder A, crtc for pipe B housing
16776 * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
16777 * and marking underrun reporting as disabled for the non-existing
16778 * PCH transcoders B and C would prevent enabling the south
16779 * error interrupt (see cpt_can_enable_serr_int()).
16780 */
16781 if (has_pch_trancoder(dev_priv, (enum transcoder)crtc->pipe))
16782 crtc->pch_fifo_underrun_disabled = true;
4cc31489 16783 }
24929352
DV
16784}
16785
16786static void intel_sanitize_encoder(struct intel_encoder *encoder)
16787{
16788 struct intel_connector *connector;
24929352
DV
16789
16790 /* We need to check both for a crtc link (meaning that the
16791 * encoder is active and trying to read from a pipe) and the
16792 * pipe itself being active. */
16793 bool has_active_crtc = encoder->base.crtc &&
16794 to_intel_crtc(encoder->base.crtc)->active;
16795
496b0fc3
ML
16796 connector = intel_encoder_find_connector(encoder);
16797 if (connector && !has_active_crtc) {
24929352
DV
16798 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
16799 encoder->base.base.id,
8e329a03 16800 encoder->base.name);
24929352
DV
16801
16802 /* Connector is active, but has no active pipe. This is
16803 * fallout from our resume register restoring. Disable
16804 * the encoder manually again. */
16805 if (encoder->base.crtc) {
fd6bbda9
ML
16806 struct drm_crtc_state *crtc_state = encoder->base.crtc->state;
16807
24929352
DV
16808 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
16809 encoder->base.base.id,
8e329a03 16810 encoder->base.name);
fd6bbda9 16811 encoder->disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
a62d1497 16812 if (encoder->post_disable)
fd6bbda9 16813 encoder->post_disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
24929352 16814 }
7f1950fb 16815 encoder->base.crtc = NULL;
24929352
DV
16816
16817 /* Inconsistent output/port/pipe state happens presumably due to
16818 * a bug in one of the get_hw_state functions. Or someplace else
16819 * in our code, like the register restore mess on resume. Clamp
16820 * things to off as a safer default. */
fd6bbda9
ML
16821
16822 connector->base.dpms = DRM_MODE_DPMS_OFF;
16823 connector->base.encoder = NULL;
24929352
DV
16824 }
16825 /* Enabled encoders without active connectors will be fixed in
16826 * the crtc fixup. */
16827}
16828
29b74b7f 16829void i915_redisable_vga_power_on(struct drm_i915_private *dev_priv)
0fde901f 16830{
920a14b2 16831 i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
0fde901f 16832
04098753
ID
16833 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
16834 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
29b74b7f 16835 i915_disable_vga(dev_priv);
04098753
ID
16836 }
16837}
16838
29b74b7f 16839void i915_redisable_vga(struct drm_i915_private *dev_priv)
04098753 16840{
8dc8a27c
PZ
16841 /* This function can be called both from intel_modeset_setup_hw_state or
16842 * at a very early point in our resume sequence, where the power well
16843 * structures are not yet restored. Since this function is at a very
16844 * paranoid "someone might have enabled VGA while we were not looking"
16845 * level, just check if the power well is enabled instead of trying to
16846 * follow the "don't touch the power well if we don't need it" policy
16847 * the rest of the driver uses. */
6392f847 16848 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
16849 return;
16850
29b74b7f 16851 i915_redisable_vga_power_on(dev_priv);
6392f847
ID
16852
16853 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
0fde901f
KM
16854}
16855
f9cd7b88 16856static bool primary_get_hw_state(struct intel_plane *plane)
98ec7739 16857{
f9cd7b88 16858 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
98ec7739 16859
f9cd7b88 16860 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
d032ffa0
ML
16861}
16862
f9cd7b88
VS
16863/* FIXME read out full plane state for all planes */
16864static void readout_plane_state(struct intel_crtc *crtc)
d032ffa0 16865{
b26d3ea3 16866 struct drm_plane *primary = crtc->base.primary;
f9cd7b88 16867 struct intel_plane_state *plane_state =
b26d3ea3 16868 to_intel_plane_state(primary->state);
d032ffa0 16869
936e71e3 16870 plane_state->base.visible = crtc->active &&
b26d3ea3
ML
16871 primary_get_hw_state(to_intel_plane(primary));
16872
936e71e3 16873 if (plane_state->base.visible)
b26d3ea3 16874 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
98ec7739
VS
16875}
16876
30e984df 16877static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352 16878{
fac5e23e 16879 struct drm_i915_private *dev_priv = to_i915(dev);
24929352 16880 enum pipe pipe;
24929352
DV
16881 struct intel_crtc *crtc;
16882 struct intel_encoder *encoder;
16883 struct intel_connector *connector;
5358901f 16884 int i;
24929352 16885
565602d7
ML
16886 dev_priv->active_crtcs = 0;
16887
d3fcc808 16888 for_each_intel_crtc(dev, crtc) {
565602d7
ML
16889 struct intel_crtc_state *crtc_state = crtc->config;
16890 int pixclk = 0;
3b117c8f 16891
ec2dc6a0 16892 __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
565602d7
ML
16893 memset(crtc_state, 0, sizeof(*crtc_state));
16894 crtc_state->base.crtc = &crtc->base;
24929352 16895
565602d7
ML
16896 crtc_state->base.active = crtc_state->base.enable =
16897 dev_priv->display.get_pipe_config(crtc, crtc_state);
16898
16899 crtc->base.enabled = crtc_state->base.enable;
16900 crtc->active = crtc_state->base.active;
16901
16902 if (crtc_state->base.active) {
16903 dev_priv->active_crtcs |= 1 << crtc->pipe;
16904
c89e39f3 16905 if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
565602d7 16906 pixclk = ilk_pipe_pixel_rate(crtc_state);
9558d15d 16907 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
565602d7
ML
16908 pixclk = crtc_state->base.adjusted_mode.crtc_clock;
16909 else
16910 WARN_ON(dev_priv->display.modeset_calc_cdclk);
9558d15d
VS
16911
16912 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
16913 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
16914 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
565602d7
ML
16915 }
16916
16917 dev_priv->min_pixclk[crtc->pipe] = pixclk;
b70709a6 16918
f9cd7b88 16919 readout_plane_state(crtc);
24929352 16920
78108b7c
VS
16921 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
16922 crtc->base.base.id, crtc->base.name,
08c4d7fc 16923 enableddisabled(crtc->active));
24929352
DV
16924 }
16925
5358901f
DV
16926 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
16927 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
16928
2edd6443
ACO
16929 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
16930 &pll->config.hw_state);
3e369b76 16931 pll->config.crtc_mask = 0;
d3fcc808 16932 for_each_intel_crtc(dev, crtc) {
2dd66ebd 16933 if (crtc->active && crtc->config->shared_dpll == pll)
3e369b76 16934 pll->config.crtc_mask |= 1 << crtc->pipe;
5358901f 16935 }
2dd66ebd 16936 pll->active_mask = pll->config.crtc_mask;
5358901f 16937
1e6f2ddc 16938 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
3e369b76 16939 pll->name, pll->config.crtc_mask, pll->on);
5358901f
DV
16940 }
16941
b2784e15 16942 for_each_intel_encoder(dev, encoder) {
24929352
DV
16943 pipe = 0;
16944
16945 if (encoder->get_hw_state(encoder, &pipe)) {
98187836 16946 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
e2af48c6 16947
045ac3b5 16948 encoder->base.crtc = &crtc->base;
253c84c8 16949 crtc->config->output_types |= 1 << encoder->type;
6e3c9717 16950 encoder->get_config(encoder, crtc->config);
24929352
DV
16951 } else {
16952 encoder->base.crtc = NULL;
16953 }
16954
6f2bcceb 16955 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
08c4d7fc
TU
16956 encoder->base.base.id, encoder->base.name,
16957 enableddisabled(encoder->base.crtc),
6f2bcceb 16958 pipe_name(pipe));
24929352
DV
16959 }
16960
3a3371ff 16961 for_each_intel_connector(dev, connector) {
24929352
DV
16962 if (connector->get_hw_state(connector)) {
16963 connector->base.dpms = DRM_MODE_DPMS_ON;
2aa974c9
ML
16964
16965 encoder = connector->encoder;
16966 connector->base.encoder = &encoder->base;
16967
16968 if (encoder->base.crtc &&
16969 encoder->base.crtc->state->active) {
16970 /*
16971 * This has to be done during hardware readout
16972 * because anything calling .crtc_disable may
16973 * rely on the connector_mask being accurate.
16974 */
16975 encoder->base.crtc->state->connector_mask |=
16976 1 << drm_connector_index(&connector->base);
e87a52b3
ML
16977 encoder->base.crtc->state->encoder_mask |=
16978 1 << drm_encoder_index(&encoder->base);
2aa974c9
ML
16979 }
16980
24929352
DV
16981 } else {
16982 connector->base.dpms = DRM_MODE_DPMS_OFF;
16983 connector->base.encoder = NULL;
16984 }
16985 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
08c4d7fc
TU
16986 connector->base.base.id, connector->base.name,
16987 enableddisabled(connector->base.encoder));
24929352 16988 }
7f4c6284
VS
16989
16990 for_each_intel_crtc(dev, crtc) {
16991 crtc->base.hwmode = crtc->config->base.adjusted_mode;
16992
16993 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
16994 if (crtc->base.state->active) {
16995 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
16996 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
16997 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
16998
16999 /*
17000 * The initial mode needs to be set in order to keep
17001 * the atomic core happy. It wants a valid mode if the
17002 * crtc's enabled, so we do the above call.
17003 *
17004 * At this point some state updated by the connectors
17005 * in their ->detect() callback has not run yet, so
17006 * no recalculation can be done yet.
17007 *
17008 * Even if we could do a recalculation and modeset
17009 * right now it would cause a double modeset if
17010 * fbdev or userspace chooses a different initial mode.
17011 *
17012 * If that happens, someone indicated they wanted a
17013 * mode change, which means it's safe to do a full
17014 * recalculation.
17015 */
17016 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
9eca6832
VS
17017
17018 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
17019 update_scanline_offset(crtc);
7f4c6284 17020 }
e3b247da
VS
17021
17022 intel_pipe_config_sanity_check(dev_priv, crtc->config);
7f4c6284 17023 }
30e984df
DV
17024}
17025
043e9bda
ML
17026/* Scan out the current hw modeset state,
17027 * and sanitizes it to the current state
17028 */
17029static void
17030intel_modeset_setup_hw_state(struct drm_device *dev)
30e984df 17031{
fac5e23e 17032 struct drm_i915_private *dev_priv = to_i915(dev);
30e984df 17033 enum pipe pipe;
30e984df
DV
17034 struct intel_crtc *crtc;
17035 struct intel_encoder *encoder;
35c95375 17036 int i;
30e984df
DV
17037
17038 intel_modeset_readout_hw_state(dev);
24929352
DV
17039
17040 /* HW state is read out, now we need to sanitize this mess. */
b2784e15 17041 for_each_intel_encoder(dev, encoder) {
24929352
DV
17042 intel_sanitize_encoder(encoder);
17043 }
17044
055e393f 17045 for_each_pipe(dev_priv, pipe) {
98187836 17046 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
e2af48c6 17047
24929352 17048 intel_sanitize_crtc(crtc);
6e3c9717
ACO
17049 intel_dump_pipe_config(crtc, crtc->config,
17050 "[setup_hw_state]");
24929352 17051 }
9a935856 17052
d29b2f9d
ACO
17053 intel_modeset_update_connector_atomic_state(dev);
17054
35c95375
DV
17055 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
17056 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
17057
2dd66ebd 17058 if (!pll->on || pll->active_mask)
35c95375
DV
17059 continue;
17060
17061 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
17062
2edd6443 17063 pll->funcs.disable(dev_priv, pll);
35c95375
DV
17064 pll->on = false;
17065 }
17066
920a14b2 17067 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
6eb1a681 17068 vlv_wm_get_hw_state(dev);
5db94019 17069 else if (IS_GEN9(dev_priv))
3078999f 17070 skl_wm_get_hw_state(dev);
6e266956 17071 else if (HAS_PCH_SPLIT(dev_priv))
243e6a44 17072 ilk_wm_get_hw_state(dev);
292b990e
ML
17073
17074 for_each_intel_crtc(dev, crtc) {
17075 unsigned long put_domains;
17076
74bff5f9 17077 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
292b990e
ML
17078 if (WARN_ON(put_domains))
17079 modeset_put_power_domains(dev_priv, put_domains);
17080 }
17081 intel_display_set_init_power(dev_priv, false);
010cf73d
PZ
17082
17083 intel_fbc_init_pipe_state(dev_priv);
043e9bda 17084}
7d0bc1ea 17085
043e9bda
ML
17086void intel_display_resume(struct drm_device *dev)
17087{
e2c8b870
ML
17088 struct drm_i915_private *dev_priv = to_i915(dev);
17089 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
17090 struct drm_modeset_acquire_ctx ctx;
043e9bda 17091 int ret;
f30da187 17092
e2c8b870 17093 dev_priv->modeset_restore_state = NULL;
73974893
ML
17094 if (state)
17095 state->acquire_ctx = &ctx;
043e9bda 17096
ea49c9ac
ML
17097 /*
17098 * This is a cludge because with real atomic modeset mode_config.mutex
17099 * won't be taken. Unfortunately some probed state like
17100 * audio_codec_enable is still protected by mode_config.mutex, so lock
17101 * it here for now.
17102 */
17103 mutex_lock(&dev->mode_config.mutex);
e2c8b870 17104 drm_modeset_acquire_init(&ctx, 0);
043e9bda 17105
73974893
ML
17106 while (1) {
17107 ret = drm_modeset_lock_all_ctx(dev, &ctx);
17108 if (ret != -EDEADLK)
17109 break;
043e9bda 17110
e2c8b870 17111 drm_modeset_backoff(&ctx);
e2c8b870 17112 }
043e9bda 17113
73974893
ML
17114 if (!ret)
17115 ret = __intel_display_resume(dev, state);
17116
e2c8b870
ML
17117 drm_modeset_drop_locks(&ctx);
17118 drm_modeset_acquire_fini(&ctx);
ea49c9ac 17119 mutex_unlock(&dev->mode_config.mutex);
043e9bda 17120
0853695c 17121 if (ret)
e2c8b870 17122 DRM_ERROR("Restoring old state failed with %i\n", ret);
0853695c 17123 drm_atomic_state_put(state);
2c7111db
CW
17124}
17125
17126void intel_modeset_gem_init(struct drm_device *dev)
17127{
dc97997a 17128 struct drm_i915_private *dev_priv = to_i915(dev);
484b41dd 17129 struct drm_crtc *c;
2ff8fde1 17130 struct drm_i915_gem_object *obj;
484b41dd 17131
dc97997a 17132 intel_init_gt_powersave(dev_priv);
ae48434c 17133
1833b134 17134 intel_modeset_init_hw(dev);
02e792fb 17135
1ee8da6d 17136 intel_setup_overlay(dev_priv);
484b41dd
JB
17137
17138 /*
17139 * Make sure any fbs we allocated at startup are properly
17140 * pinned & fenced. When we do the allocation it's too early
17141 * for this.
17142 */
70e1e0ec 17143 for_each_crtc(dev, c) {
058d88c4
CW
17144 struct i915_vma *vma;
17145
2ff8fde1
MR
17146 obj = intel_fb_obj(c->primary->fb);
17147 if (obj == NULL)
484b41dd
JB
17148 continue;
17149
e0d6149b 17150 mutex_lock(&dev->struct_mutex);
058d88c4 17151 vma = intel_pin_and_fence_fb_obj(c->primary->fb,
3465c580 17152 c->primary->state->rotation);
e0d6149b 17153 mutex_unlock(&dev->struct_mutex);
058d88c4 17154 if (IS_ERR(vma)) {
484b41dd
JB
17155 DRM_ERROR("failed to pin boot fb on pipe %d\n",
17156 to_intel_crtc(c)->pipe);
66e514c1 17157 drm_framebuffer_unreference(c->primary->fb);
5a21b665 17158 c->primary->fb = NULL;
36750f28 17159 c->primary->crtc = c->primary->state->crtc = NULL;
5a21b665 17160 update_state_fb(c->primary);
36750f28 17161 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
484b41dd
JB
17162 }
17163 }
1ebaa0b9
CW
17164}
17165
17166int intel_connector_register(struct drm_connector *connector)
17167{
17168 struct intel_connector *intel_connector = to_intel_connector(connector);
17169 int ret;
17170
17171 ret = intel_backlight_device_register(intel_connector);
17172 if (ret)
17173 goto err;
17174
17175 return 0;
0962c3c9 17176
1ebaa0b9
CW
17177err:
17178 return ret;
79e53945
JB
17179}
17180
c191eca1 17181void intel_connector_unregister(struct drm_connector *connector)
4932e2c3 17182{
e63d87c0 17183 struct intel_connector *intel_connector = to_intel_connector(connector);
4932e2c3 17184
e63d87c0 17185 intel_backlight_device_unregister(intel_connector);
4932e2c3 17186 intel_panel_destroy_backlight(connector);
4932e2c3
ID
17187}
17188
79e53945
JB
17189void intel_modeset_cleanup(struct drm_device *dev)
17190{
fac5e23e 17191 struct drm_i915_private *dev_priv = to_i915(dev);
652c393a 17192
dc97997a 17193 intel_disable_gt_powersave(dev_priv);
2eb5252e 17194
fd0c0642
DV
17195 /*
17196 * Interrupts and polling as the first thing to avoid creating havoc.
2eb5252e 17197 * Too much stuff here (turning of connectors, ...) would
fd0c0642
DV
17198 * experience fancy races otherwise.
17199 */
2aeb7d3a 17200 intel_irq_uninstall(dev_priv);
eb21b92b 17201
fd0c0642
DV
17202 /*
17203 * Due to the hpd irq storm handling the hotplug work can re-arm the
17204 * poll handlers. Hence disable polling after hpd handling is shut down.
17205 */
f87ea761 17206 drm_kms_helper_poll_fini(dev);
fd0c0642 17207
723bfd70
JB
17208 intel_unregister_dsm_handler();
17209
c937ab3e 17210 intel_fbc_global_disable(dev_priv);
69341a5e 17211
1630fe75
CW
17212 /* flush any delayed tasks or pending work */
17213 flush_scheduled_work();
17214
79e53945 17215 drm_mode_config_cleanup(dev);
4d7bb011 17216
1ee8da6d 17217 intel_cleanup_overlay(dev_priv);
ae48434c 17218
dc97997a 17219 intel_cleanup_gt_powersave(dev_priv);
f5949141 17220
40196446 17221 intel_teardown_gmbus(dev_priv);
79e53945
JB
17222}
17223
df0e9248
CW
17224void intel_connector_attach_encoder(struct intel_connector *connector,
17225 struct intel_encoder *encoder)
17226{
17227 connector->encoder = encoder;
17228 drm_mode_connector_attach_encoder(&connector->base,
17229 &encoder->base);
79e53945 17230}
28d52043
DA
17231
17232/*
17233 * set vga decode state - true == enable VGA decode
17234 */
6315b5d3 17235int intel_modeset_vga_set_state(struct drm_i915_private *dev_priv, bool state)
28d52043 17236{
6315b5d3 17237 unsigned reg = INTEL_GEN(dev_priv) >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
17238 u16 gmch_ctrl;
17239
75fa041d
CW
17240 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
17241 DRM_ERROR("failed to read control word\n");
17242 return -EIO;
17243 }
17244
c0cc8a55
CW
17245 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
17246 return 0;
17247
28d52043
DA
17248 if (state)
17249 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
17250 else
17251 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
17252
17253 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
17254 DRM_ERROR("failed to write control word\n");
17255 return -EIO;
17256 }
17257
28d52043
DA
17258 return 0;
17259}
c4a1d9e4 17260
98a2f411
CW
17261#if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
17262
c4a1d9e4 17263struct intel_display_error_state {
ff57f1b0
PZ
17264
17265 u32 power_well_driver;
17266
63b66e5b
CW
17267 int num_transcoders;
17268
c4a1d9e4
CW
17269 struct intel_cursor_error_state {
17270 u32 control;
17271 u32 position;
17272 u32 base;
17273 u32 size;
52331309 17274 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
17275
17276 struct intel_pipe_error_state {
ddf9c536 17277 bool power_domain_on;
c4a1d9e4 17278 u32 source;
f301b1e1 17279 u32 stat;
52331309 17280 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
17281
17282 struct intel_plane_error_state {
17283 u32 control;
17284 u32 stride;
17285 u32 size;
17286 u32 pos;
17287 u32 addr;
17288 u32 surface;
17289 u32 tile_offset;
52331309 17290 } plane[I915_MAX_PIPES];
63b66e5b
CW
17291
17292 struct intel_transcoder_error_state {
ddf9c536 17293 bool power_domain_on;
63b66e5b
CW
17294 enum transcoder cpu_transcoder;
17295
17296 u32 conf;
17297
17298 u32 htotal;
17299 u32 hblank;
17300 u32 hsync;
17301 u32 vtotal;
17302 u32 vblank;
17303 u32 vsync;
17304 } transcoder[4];
c4a1d9e4
CW
17305};
17306
17307struct intel_display_error_state *
c033666a 17308intel_display_capture_error_state(struct drm_i915_private *dev_priv)
c4a1d9e4 17309{
c4a1d9e4 17310 struct intel_display_error_state *error;
63b66e5b
CW
17311 int transcoders[] = {
17312 TRANSCODER_A,
17313 TRANSCODER_B,
17314 TRANSCODER_C,
17315 TRANSCODER_EDP,
17316 };
c4a1d9e4
CW
17317 int i;
17318
c033666a 17319 if (INTEL_INFO(dev_priv)->num_pipes == 0)
63b66e5b
CW
17320 return NULL;
17321
9d1cb914 17322 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
17323 if (error == NULL)
17324 return NULL;
17325
c033666a 17326 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
ff57f1b0
PZ
17327 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
17328
055e393f 17329 for_each_pipe(dev_priv, i) {
ddf9c536 17330 error->pipe[i].power_domain_on =
f458ebbc
DV
17331 __intel_display_power_is_enabled(dev_priv,
17332 POWER_DOMAIN_PIPE(i));
ddf9c536 17333 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
17334 continue;
17335
5efb3e28
VS
17336 error->cursor[i].control = I915_READ(CURCNTR(i));
17337 error->cursor[i].position = I915_READ(CURPOS(i));
17338 error->cursor[i].base = I915_READ(CURBASE(i));
c4a1d9e4
CW
17339
17340 error->plane[i].control = I915_READ(DSPCNTR(i));
17341 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
c033666a 17342 if (INTEL_GEN(dev_priv) <= 3) {
51889b35 17343 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
17344 error->plane[i].pos = I915_READ(DSPPOS(i));
17345 }
c033666a 17346 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
ca291363 17347 error->plane[i].addr = I915_READ(DSPADDR(i));
c033666a 17348 if (INTEL_GEN(dev_priv) >= 4) {
c4a1d9e4
CW
17349 error->plane[i].surface = I915_READ(DSPSURF(i));
17350 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
17351 }
17352
c4a1d9e4 17353 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1 17354
c033666a 17355 if (HAS_GMCH_DISPLAY(dev_priv))
f301b1e1 17356 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
17357 }
17358
4d1de975 17359 /* Note: this does not include DSI transcoders. */
c033666a 17360 error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
2d1fe073 17361 if (HAS_DDI(dev_priv))
63b66e5b
CW
17362 error->num_transcoders++; /* Account for eDP. */
17363
17364 for (i = 0; i < error->num_transcoders; i++) {
17365 enum transcoder cpu_transcoder = transcoders[i];
17366
ddf9c536 17367 error->transcoder[i].power_domain_on =
f458ebbc 17368 __intel_display_power_is_enabled(dev_priv,
38cc1daf 17369 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 17370 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
17371 continue;
17372
63b66e5b
CW
17373 error->transcoder[i].cpu_transcoder = cpu_transcoder;
17374
17375 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
17376 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
17377 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
17378 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
17379 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
17380 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
17381 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
17382 }
17383
17384 return error;
17385}
17386
edc3d884
MK
17387#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
17388
c4a1d9e4 17389void
edc3d884 17390intel_display_print_error_state(struct drm_i915_error_state_buf *m,
5f56d5f9 17391 struct drm_i915_private *dev_priv,
c4a1d9e4
CW
17392 struct intel_display_error_state *error)
17393{
17394 int i;
17395
63b66e5b
CW
17396 if (!error)
17397 return;
17398
b7f05d4a 17399 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev_priv)->num_pipes);
8652744b 17400 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
edc3d884 17401 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 17402 error->power_well_driver);
055e393f 17403 for_each_pipe(dev_priv, i) {
edc3d884 17404 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536 17405 err_printf(m, " Power: %s\n",
87ad3212 17406 onoff(error->pipe[i].power_domain_on));
edc3d884 17407 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 17408 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
17409
17410 err_printf(m, "Plane [%d]:\n", i);
17411 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
17412 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
5f56d5f9 17413 if (INTEL_GEN(dev_priv) <= 3) {
edc3d884
MK
17414 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
17415 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 17416 }
772c2a51 17417 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
edc3d884 17418 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
5f56d5f9 17419 if (INTEL_GEN(dev_priv) >= 4) {
edc3d884
MK
17420 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
17421 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
17422 }
17423
edc3d884
MK
17424 err_printf(m, "Cursor [%d]:\n", i);
17425 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
17426 err_printf(m, " POS: %08x\n", error->cursor[i].position);
17427 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 17428 }
63b66e5b
CW
17429
17430 for (i = 0; i < error->num_transcoders; i++) {
da205630 17431 err_printf(m, "CPU transcoder: %s\n",
63b66e5b 17432 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536 17433 err_printf(m, " Power: %s\n",
87ad3212 17434 onoff(error->transcoder[i].power_domain_on));
63b66e5b
CW
17435 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
17436 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
17437 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
17438 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
17439 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
17440 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
17441 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
17442 }
c4a1d9e4 17443}
98a2f411
CW
17444
17445#endif