]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpu/drm/i915/intel_display.c
drm/i915/guc: Log significant events at the info level
[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 *);
1c74eeaf
NM
118static void intel_crtc_init_scalers(struct intel_crtc *crtc,
119 struct intel_crtc_state *crtc_state);
bfd16b2a
ML
120static void skylake_pfit_enable(struct intel_crtc *crtc);
121static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
122static void ironlake_pfit_enable(struct intel_crtc *crtc);
043e9bda 123static void intel_modeset_setup_hw_state(struct drm_device *dev);
2622a081 124static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
4e5ca60f 125static int ilk_max_pixel_rate(struct drm_atomic_state *state);
89b3c3c7 126static int glk_calc_cdclk(int max_pixclk);
324513c0 127static int bxt_calc_cdclk(int max_pixclk);
e7457a9a 128
d4906093 129struct intel_limit {
4c5def93
ACO
130 struct {
131 int min, max;
132 } dot, vco, n, m, m1, m2, p, p1;
133
134 struct {
135 int dot_limit;
136 int p2_slow, p2_fast;
137 } p2;
d4906093 138};
79e53945 139
bfa7df01
VS
140/* returns HPLL frequency in kHz */
141static int valleyview_get_vco(struct drm_i915_private *dev_priv)
142{
143 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
144
145 /* Obtain SKU information */
146 mutex_lock(&dev_priv->sb_lock);
147 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
148 CCK_FUSE_HPLL_FREQ_MASK;
149 mutex_unlock(&dev_priv->sb_lock);
150
151 return vco_freq[hpll_freq] * 1000;
152}
153
c30fec65
VS
154int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
155 const char *name, u32 reg, int ref_freq)
bfa7df01
VS
156{
157 u32 val;
158 int divider;
159
bfa7df01
VS
160 mutex_lock(&dev_priv->sb_lock);
161 val = vlv_cck_read(dev_priv, reg);
162 mutex_unlock(&dev_priv->sb_lock);
163
164 divider = val & CCK_FREQUENCY_VALUES;
165
166 WARN((val & CCK_FREQUENCY_STATUS) !=
167 (divider << CCK_FREQUENCY_STATUS_SHIFT),
168 "%s change in progress\n", name);
169
c30fec65
VS
170 return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
171}
172
173static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
174 const char *name, u32 reg)
175{
176 if (dev_priv->hpll_freq == 0)
177 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
178
179 return vlv_get_cck_clock(dev_priv, name, reg,
180 dev_priv->hpll_freq);
bfa7df01
VS
181}
182
e7dc33f3
VS
183static int
184intel_pch_rawclk(struct drm_i915_private *dev_priv)
d2acd215 185{
e7dc33f3
VS
186 return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
187}
d2acd215 188
e7dc33f3
VS
189static int
190intel_vlv_hrawclk(struct drm_i915_private *dev_priv)
191{
19ab4ed3 192 /* RAWCLK_FREQ_VLV register updated from power well code */
35d38d1f
VS
193 return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
194 CCK_DISPLAY_REF_CLOCK_CONTROL);
d2acd215
DV
195}
196
e7dc33f3
VS
197static int
198intel_g4x_hrawclk(struct drm_i915_private *dev_priv)
79e50a4f 199{
79e50a4f
JN
200 uint32_t clkcfg;
201
e7dc33f3 202 /* hrawclock is 1/4 the FSB frequency */
79e50a4f
JN
203 clkcfg = I915_READ(CLKCFG);
204 switch (clkcfg & CLKCFG_FSB_MASK) {
205 case CLKCFG_FSB_400:
e7dc33f3 206 return 100000;
79e50a4f 207 case CLKCFG_FSB_533:
e7dc33f3 208 return 133333;
79e50a4f 209 case CLKCFG_FSB_667:
e7dc33f3 210 return 166667;
79e50a4f 211 case CLKCFG_FSB_800:
e7dc33f3 212 return 200000;
79e50a4f 213 case CLKCFG_FSB_1067:
e7dc33f3 214 return 266667;
79e50a4f 215 case CLKCFG_FSB_1333:
e7dc33f3 216 return 333333;
79e50a4f
JN
217 /* these two are just a guess; one of them might be right */
218 case CLKCFG_FSB_1600:
219 case CLKCFG_FSB_1600_ALT:
e7dc33f3 220 return 400000;
79e50a4f 221 default:
e7dc33f3 222 return 133333;
79e50a4f
JN
223 }
224}
225
19ab4ed3 226void intel_update_rawclk(struct drm_i915_private *dev_priv)
e7dc33f3
VS
227{
228 if (HAS_PCH_SPLIT(dev_priv))
229 dev_priv->rawclk_freq = intel_pch_rawclk(dev_priv);
230 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
231 dev_priv->rawclk_freq = intel_vlv_hrawclk(dev_priv);
232 else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
233 dev_priv->rawclk_freq = intel_g4x_hrawclk(dev_priv);
234 else
235 return; /* no rawclk on other platforms, or no need to know it */
236
237 DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
238}
239
bfa7df01
VS
240static void intel_update_czclk(struct drm_i915_private *dev_priv)
241{
666a4537 242 if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
bfa7df01
VS
243 return;
244
245 dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
246 CCK_CZ_CLOCK_CONTROL);
247
248 DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
249}
250
021357ac 251static inline u32 /* units of 100MHz */
21a727b3
VS
252intel_fdi_link_freq(struct drm_i915_private *dev_priv,
253 const struct intel_crtc_state *pipe_config)
021357ac 254{
21a727b3
VS
255 if (HAS_DDI(dev_priv))
256 return pipe_config->port_clock; /* SPLL */
257 else if (IS_GEN5(dev_priv))
258 return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
e3b247da 259 else
21a727b3 260 return 270000;
021357ac
CW
261}
262
1b6f4958 263static const struct intel_limit intel_limits_i8xx_dac = {
0206e353 264 .dot = { .min = 25000, .max = 350000 },
9c333719 265 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 266 .n = { .min = 2, .max = 16 },
0206e353
AJ
267 .m = { .min = 96, .max = 140 },
268 .m1 = { .min = 18, .max = 26 },
269 .m2 = { .min = 6, .max = 16 },
270 .p = { .min = 4, .max = 128 },
271 .p1 = { .min = 2, .max = 33 },
273e27ca
EA
272 .p2 = { .dot_limit = 165000,
273 .p2_slow = 4, .p2_fast = 2 },
e4b36699
KP
274};
275
1b6f4958 276static const struct intel_limit intel_limits_i8xx_dvo = {
5d536e28 277 .dot = { .min = 25000, .max = 350000 },
9c333719 278 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 279 .n = { .min = 2, .max = 16 },
5d536e28
DV
280 .m = { .min = 96, .max = 140 },
281 .m1 = { .min = 18, .max = 26 },
282 .m2 = { .min = 6, .max = 16 },
283 .p = { .min = 4, .max = 128 },
284 .p1 = { .min = 2, .max = 33 },
285 .p2 = { .dot_limit = 165000,
286 .p2_slow = 4, .p2_fast = 4 },
287};
288
1b6f4958 289static const struct intel_limit intel_limits_i8xx_lvds = {
0206e353 290 .dot = { .min = 25000, .max = 350000 },
9c333719 291 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 292 .n = { .min = 2, .max = 16 },
0206e353
AJ
293 .m = { .min = 96, .max = 140 },
294 .m1 = { .min = 18, .max = 26 },
295 .m2 = { .min = 6, .max = 16 },
296 .p = { .min = 4, .max = 128 },
297 .p1 = { .min = 1, .max = 6 },
273e27ca
EA
298 .p2 = { .dot_limit = 165000,
299 .p2_slow = 14, .p2_fast = 7 },
e4b36699 300};
273e27ca 301
1b6f4958 302static const struct intel_limit intel_limits_i9xx_sdvo = {
0206e353
AJ
303 .dot = { .min = 20000, .max = 400000 },
304 .vco = { .min = 1400000, .max = 2800000 },
305 .n = { .min = 1, .max = 6 },
306 .m = { .min = 70, .max = 120 },
4f7dfb67
PJ
307 .m1 = { .min = 8, .max = 18 },
308 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
309 .p = { .min = 5, .max = 80 },
310 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
311 .p2 = { .dot_limit = 200000,
312 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
313};
314
1b6f4958 315static const struct intel_limit intel_limits_i9xx_lvds = {
0206e353
AJ
316 .dot = { .min = 20000, .max = 400000 },
317 .vco = { .min = 1400000, .max = 2800000 },
318 .n = { .min = 1, .max = 6 },
319 .m = { .min = 70, .max = 120 },
53a7d2d1
PJ
320 .m1 = { .min = 8, .max = 18 },
321 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
322 .p = { .min = 7, .max = 98 },
323 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
324 .p2 = { .dot_limit = 112000,
325 .p2_slow = 14, .p2_fast = 7 },
e4b36699
KP
326};
327
273e27ca 328
1b6f4958 329static const struct intel_limit intel_limits_g4x_sdvo = {
273e27ca
EA
330 .dot = { .min = 25000, .max = 270000 },
331 .vco = { .min = 1750000, .max = 3500000},
332 .n = { .min = 1, .max = 4 },
333 .m = { .min = 104, .max = 138 },
334 .m1 = { .min = 17, .max = 23 },
335 .m2 = { .min = 5, .max = 11 },
336 .p = { .min = 10, .max = 30 },
337 .p1 = { .min = 1, .max = 3},
338 .p2 = { .dot_limit = 270000,
339 .p2_slow = 10,
340 .p2_fast = 10
044c7c41 341 },
e4b36699
KP
342};
343
1b6f4958 344static const struct intel_limit intel_limits_g4x_hdmi = {
273e27ca
EA
345 .dot = { .min = 22000, .max = 400000 },
346 .vco = { .min = 1750000, .max = 3500000},
347 .n = { .min = 1, .max = 4 },
348 .m = { .min = 104, .max = 138 },
349 .m1 = { .min = 16, .max = 23 },
350 .m2 = { .min = 5, .max = 11 },
351 .p = { .min = 5, .max = 80 },
352 .p1 = { .min = 1, .max = 8},
353 .p2 = { .dot_limit = 165000,
354 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
355};
356
1b6f4958 357static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
273e27ca
EA
358 .dot = { .min = 20000, .max = 115000 },
359 .vco = { .min = 1750000, .max = 3500000 },
360 .n = { .min = 1, .max = 3 },
361 .m = { .min = 104, .max = 138 },
362 .m1 = { .min = 17, .max = 23 },
363 .m2 = { .min = 5, .max = 11 },
364 .p = { .min = 28, .max = 112 },
365 .p1 = { .min = 2, .max = 8 },
366 .p2 = { .dot_limit = 0,
367 .p2_slow = 14, .p2_fast = 14
044c7c41 368 },
e4b36699
KP
369};
370
1b6f4958 371static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
273e27ca
EA
372 .dot = { .min = 80000, .max = 224000 },
373 .vco = { .min = 1750000, .max = 3500000 },
374 .n = { .min = 1, .max = 3 },
375 .m = { .min = 104, .max = 138 },
376 .m1 = { .min = 17, .max = 23 },
377 .m2 = { .min = 5, .max = 11 },
378 .p = { .min = 14, .max = 42 },
379 .p1 = { .min = 2, .max = 6 },
380 .p2 = { .dot_limit = 0,
381 .p2_slow = 7, .p2_fast = 7
044c7c41 382 },
e4b36699
KP
383};
384
1b6f4958 385static const struct intel_limit intel_limits_pineview_sdvo = {
0206e353
AJ
386 .dot = { .min = 20000, .max = 400000},
387 .vco = { .min = 1700000, .max = 3500000 },
273e27ca 388 /* Pineview's Ncounter is a ring counter */
0206e353
AJ
389 .n = { .min = 3, .max = 6 },
390 .m = { .min = 2, .max = 256 },
273e27ca 391 /* Pineview only has one combined m divider, which we treat as m2. */
0206e353
AJ
392 .m1 = { .min = 0, .max = 0 },
393 .m2 = { .min = 0, .max = 254 },
394 .p = { .min = 5, .max = 80 },
395 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
396 .p2 = { .dot_limit = 200000,
397 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
398};
399
1b6f4958 400static const struct intel_limit intel_limits_pineview_lvds = {
0206e353
AJ
401 .dot = { .min = 20000, .max = 400000 },
402 .vco = { .min = 1700000, .max = 3500000 },
403 .n = { .min = 3, .max = 6 },
404 .m = { .min = 2, .max = 256 },
405 .m1 = { .min = 0, .max = 0 },
406 .m2 = { .min = 0, .max = 254 },
407 .p = { .min = 7, .max = 112 },
408 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
409 .p2 = { .dot_limit = 112000,
410 .p2_slow = 14, .p2_fast = 14 },
e4b36699
KP
411};
412
273e27ca
EA
413/* Ironlake / Sandybridge
414 *
415 * We calculate clock using (register_value + 2) for N/M1/M2, so here
416 * the range value for them is (actual_value - 2).
417 */
1b6f4958 418static const struct intel_limit intel_limits_ironlake_dac = {
273e27ca
EA
419 .dot = { .min = 25000, .max = 350000 },
420 .vco = { .min = 1760000, .max = 3510000 },
421 .n = { .min = 1, .max = 5 },
422 .m = { .min = 79, .max = 127 },
423 .m1 = { .min = 12, .max = 22 },
424 .m2 = { .min = 5, .max = 9 },
425 .p = { .min = 5, .max = 80 },
426 .p1 = { .min = 1, .max = 8 },
427 .p2 = { .dot_limit = 225000,
428 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
429};
430
1b6f4958 431static const struct intel_limit intel_limits_ironlake_single_lvds = {
273e27ca
EA
432 .dot = { .min = 25000, .max = 350000 },
433 .vco = { .min = 1760000, .max = 3510000 },
434 .n = { .min = 1, .max = 3 },
435 .m = { .min = 79, .max = 118 },
436 .m1 = { .min = 12, .max = 22 },
437 .m2 = { .min = 5, .max = 9 },
438 .p = { .min = 28, .max = 112 },
439 .p1 = { .min = 2, .max = 8 },
440 .p2 = { .dot_limit = 225000,
441 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
442};
443
1b6f4958 444static const struct intel_limit intel_limits_ironlake_dual_lvds = {
273e27ca
EA
445 .dot = { .min = 25000, .max = 350000 },
446 .vco = { .min = 1760000, .max = 3510000 },
447 .n = { .min = 1, .max = 3 },
448 .m = { .min = 79, .max = 127 },
449 .m1 = { .min = 12, .max = 22 },
450 .m2 = { .min = 5, .max = 9 },
451 .p = { .min = 14, .max = 56 },
452 .p1 = { .min = 2, .max = 8 },
453 .p2 = { .dot_limit = 225000,
454 .p2_slow = 7, .p2_fast = 7 },
b91ad0ec
ZW
455};
456
273e27ca 457/* LVDS 100mhz refclk limits. */
1b6f4958 458static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
273e27ca
EA
459 .dot = { .min = 25000, .max = 350000 },
460 .vco = { .min = 1760000, .max = 3510000 },
461 .n = { .min = 1, .max = 2 },
462 .m = { .min = 79, .max = 126 },
463 .m1 = { .min = 12, .max = 22 },
464 .m2 = { .min = 5, .max = 9 },
465 .p = { .min = 28, .max = 112 },
0206e353 466 .p1 = { .min = 2, .max = 8 },
273e27ca
EA
467 .p2 = { .dot_limit = 225000,
468 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
469};
470
1b6f4958 471static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
273e27ca
EA
472 .dot = { .min = 25000, .max = 350000 },
473 .vco = { .min = 1760000, .max = 3510000 },
474 .n = { .min = 1, .max = 3 },
475 .m = { .min = 79, .max = 126 },
476 .m1 = { .min = 12, .max = 22 },
477 .m2 = { .min = 5, .max = 9 },
478 .p = { .min = 14, .max = 42 },
0206e353 479 .p1 = { .min = 2, .max = 6 },
273e27ca
EA
480 .p2 = { .dot_limit = 225000,
481 .p2_slow = 7, .p2_fast = 7 },
4547668a
ZY
482};
483
1b6f4958 484static const struct intel_limit intel_limits_vlv = {
f01b7962
VS
485 /*
486 * These are the data rate limits (measured in fast clocks)
487 * since those are the strictest limits we have. The fast
488 * clock and actual rate limits are more relaxed, so checking
489 * them would make no difference.
490 */
491 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
75e53986 492 .vco = { .min = 4000000, .max = 6000000 },
a0c4da24 493 .n = { .min = 1, .max = 7 },
a0c4da24
JB
494 .m1 = { .min = 2, .max = 3 },
495 .m2 = { .min = 11, .max = 156 },
b99ab663 496 .p1 = { .min = 2, .max = 3 },
5fdc9c49 497 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
a0c4da24
JB
498};
499
1b6f4958 500static const struct intel_limit intel_limits_chv = {
ef9348c8
CML
501 /*
502 * These are the data rate limits (measured in fast clocks)
503 * since those are the strictest limits we have. The fast
504 * clock and actual rate limits are more relaxed, so checking
505 * them would make no difference.
506 */
507 .dot = { .min = 25000 * 5, .max = 540000 * 5},
17fe1021 508 .vco = { .min = 4800000, .max = 6480000 },
ef9348c8
CML
509 .n = { .min = 1, .max = 1 },
510 .m1 = { .min = 2, .max = 2 },
511 .m2 = { .min = 24 << 22, .max = 175 << 22 },
512 .p1 = { .min = 2, .max = 4 },
513 .p2 = { .p2_slow = 1, .p2_fast = 14 },
514};
515
1b6f4958 516static const struct intel_limit intel_limits_bxt = {
5ab7b0b7
ID
517 /* FIXME: find real dot limits */
518 .dot = { .min = 0, .max = INT_MAX },
e6292556 519 .vco = { .min = 4800000, .max = 6700000 },
5ab7b0b7
ID
520 .n = { .min = 1, .max = 1 },
521 .m1 = { .min = 2, .max = 2 },
522 /* FIXME: find real m2 limits */
523 .m2 = { .min = 2 << 22, .max = 255 << 22 },
524 .p1 = { .min = 2, .max = 4 },
525 .p2 = { .p2_slow = 1, .p2_fast = 20 },
526};
527
cdba954e
ACO
528static bool
529needs_modeset(struct drm_crtc_state *state)
530{
fc596660 531 return drm_atomic_crtc_needs_modeset(state);
cdba954e
ACO
532}
533
dccbea3b
ID
534/*
535 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
536 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
537 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
538 * The helpers' return value is the rate of the clock that is fed to the
539 * display engine's pipe which can be the above fast dot clock rate or a
540 * divided-down version of it.
541 */
f2b115e6 542/* m1 is reserved as 0 in Pineview, n is a ring counter */
9e2c8475 543static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
79e53945 544{
2177832f
SL
545 clock->m = clock->m2 + 2;
546 clock->p = clock->p1 * clock->p2;
ed5ca77e 547 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 548 return 0;
fb03ac01
VS
549 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
550 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
551
552 return clock->dot;
2177832f
SL
553}
554
7429e9d4
DV
555static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
556{
557 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
558}
559
9e2c8475 560static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
2177832f 561{
7429e9d4 562 clock->m = i9xx_dpll_compute_m(clock);
79e53945 563 clock->p = clock->p1 * clock->p2;
ed5ca77e 564 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
dccbea3b 565 return 0;
fb03ac01
VS
566 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
567 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
568
569 return clock->dot;
79e53945
JB
570}
571
9e2c8475 572static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
589eca67
ID
573{
574 clock->m = clock->m1 * clock->m2;
575 clock->p = clock->p1 * clock->p2;
576 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 577 return 0;
589eca67
ID
578 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
579 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
580
581 return clock->dot / 5;
589eca67
ID
582}
583
9e2c8475 584int chv_calc_dpll_params(int refclk, struct dpll *clock)
ef9348c8
CML
585{
586 clock->m = clock->m1 * clock->m2;
587 clock->p = clock->p1 * clock->p2;
588 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 589 return 0;
ef9348c8
CML
590 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
591 clock->n << 22);
592 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
593
594 return clock->dot / 5;
ef9348c8
CML
595}
596
7c04d1d9 597#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
79e53945
JB
598/**
599 * Returns whether the given set of divisors are valid for a given refclk with
600 * the given connectors.
601 */
602
e2d214ae 603static bool intel_PLL_is_valid(struct drm_i915_private *dev_priv,
1b6f4958 604 const struct intel_limit *limit,
9e2c8475 605 const struct dpll *clock)
79e53945 606{
f01b7962
VS
607 if (clock->n < limit->n.min || limit->n.max < clock->n)
608 INTELPllInvalid("n out of range\n");
79e53945 609 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
0206e353 610 INTELPllInvalid("p1 out of range\n");
79e53945 611 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
0206e353 612 INTELPllInvalid("m2 out of range\n");
79e53945 613 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
0206e353 614 INTELPllInvalid("m1 out of range\n");
f01b7962 615
e2d214ae 616 if (!IS_PINEVIEW(dev_priv) && !IS_VALLEYVIEW(dev_priv) &&
cc3f90f0 617 !IS_CHERRYVIEW(dev_priv) && !IS_GEN9_LP(dev_priv))
f01b7962
VS
618 if (clock->m1 <= clock->m2)
619 INTELPllInvalid("m1 <= m2\n");
620
e2d214ae 621 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
cc3f90f0 622 !IS_GEN9_LP(dev_priv)) {
f01b7962
VS
623 if (clock->p < limit->p.min || limit->p.max < clock->p)
624 INTELPllInvalid("p out of range\n");
625 if (clock->m < limit->m.min || limit->m.max < clock->m)
626 INTELPllInvalid("m out of range\n");
627 }
628
79e53945 629 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
0206e353 630 INTELPllInvalid("vco out of range\n");
79e53945
JB
631 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
632 * connector, etc., rather than just a single range.
633 */
634 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
0206e353 635 INTELPllInvalid("dot out of range\n");
79e53945
JB
636
637 return true;
638}
639
3b1429d9 640static int
1b6f4958 641i9xx_select_p2_div(const struct intel_limit *limit,
3b1429d9
VS
642 const struct intel_crtc_state *crtc_state,
643 int target)
79e53945 644{
3b1429d9 645 struct drm_device *dev = crtc_state->base.crtc->dev;
79e53945 646
2d84d2b3 647 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
79e53945 648 /*
a210b028
DV
649 * For LVDS just rely on its current settings for dual-channel.
650 * We haven't figured out how to reliably set up different
651 * single/dual channel state, if we even can.
79e53945 652 */
1974cad0 653 if (intel_is_dual_link_lvds(dev))
3b1429d9 654 return limit->p2.p2_fast;
79e53945 655 else
3b1429d9 656 return limit->p2.p2_slow;
79e53945
JB
657 } else {
658 if (target < limit->p2.dot_limit)
3b1429d9 659 return limit->p2.p2_slow;
79e53945 660 else
3b1429d9 661 return limit->p2.p2_fast;
79e53945 662 }
3b1429d9
VS
663}
664
70e8aa21
ACO
665/*
666 * Returns a set of divisors for the desired target clock with the given
667 * refclk, or FALSE. The returned values represent the clock equation:
668 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
669 *
670 * Target and reference clocks are specified in kHz.
671 *
672 * If match_clock is provided, then best_clock P divider must match the P
673 * divider from @match_clock used for LVDS downclocking.
674 */
3b1429d9 675static bool
1b6f4958 676i9xx_find_best_dpll(const struct intel_limit *limit,
3b1429d9 677 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
678 int target, int refclk, struct dpll *match_clock,
679 struct dpll *best_clock)
3b1429d9
VS
680{
681 struct drm_device *dev = crtc_state->base.crtc->dev;
9e2c8475 682 struct dpll clock;
3b1429d9 683 int err = target;
79e53945 684
0206e353 685 memset(best_clock, 0, sizeof(*best_clock));
79e53945 686
3b1429d9
VS
687 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
688
42158660
ZY
689 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
690 clock.m1++) {
691 for (clock.m2 = limit->m2.min;
692 clock.m2 <= limit->m2.max; clock.m2++) {
c0efc387 693 if (clock.m2 >= clock.m1)
42158660
ZY
694 break;
695 for (clock.n = limit->n.min;
696 clock.n <= limit->n.max; clock.n++) {
697 for (clock.p1 = limit->p1.min;
698 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
699 int this_err;
700
dccbea3b 701 i9xx_calc_dpll_params(refclk, &clock);
e2d214ae
TU
702 if (!intel_PLL_is_valid(to_i915(dev),
703 limit,
ac58c3f0
DV
704 &clock))
705 continue;
706 if (match_clock &&
707 clock.p != match_clock->p)
708 continue;
709
710 this_err = abs(clock.dot - target);
711 if (this_err < err) {
712 *best_clock = clock;
713 err = this_err;
714 }
715 }
716 }
717 }
718 }
719
720 return (err != target);
721}
722
70e8aa21
ACO
723/*
724 * Returns a set of divisors for the desired target clock with the given
725 * refclk, or FALSE. The returned values represent the clock equation:
726 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
727 *
728 * Target and reference clocks are specified in kHz.
729 *
730 * If match_clock is provided, then best_clock P divider must match the P
731 * divider from @match_clock used for LVDS downclocking.
732 */
ac58c3f0 733static bool
1b6f4958 734pnv_find_best_dpll(const struct intel_limit *limit,
a93e255f 735 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
736 int target, int refclk, struct dpll *match_clock,
737 struct dpll *best_clock)
79e53945 738{
3b1429d9 739 struct drm_device *dev = crtc_state->base.crtc->dev;
9e2c8475 740 struct dpll clock;
79e53945
JB
741 int err = target;
742
0206e353 743 memset(best_clock, 0, sizeof(*best_clock));
79e53945 744
3b1429d9
VS
745 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
746
42158660
ZY
747 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
748 clock.m1++) {
749 for (clock.m2 = limit->m2.min;
750 clock.m2 <= limit->m2.max; clock.m2++) {
42158660
ZY
751 for (clock.n = limit->n.min;
752 clock.n <= limit->n.max; clock.n++) {
753 for (clock.p1 = limit->p1.min;
754 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
755 int this_err;
756
dccbea3b 757 pnv_calc_dpll_params(refclk, &clock);
e2d214ae
TU
758 if (!intel_PLL_is_valid(to_i915(dev),
759 limit,
1b894b59 760 &clock))
79e53945 761 continue;
cec2f356
SP
762 if (match_clock &&
763 clock.p != match_clock->p)
764 continue;
79e53945
JB
765
766 this_err = abs(clock.dot - target);
767 if (this_err < err) {
768 *best_clock = clock;
769 err = this_err;
770 }
771 }
772 }
773 }
774 }
775
776 return (err != target);
777}
778
997c030c
ACO
779/*
780 * Returns a set of divisors for the desired target clock with the given
781 * refclk, or FALSE. The returned values represent the clock equation:
782 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
70e8aa21
ACO
783 *
784 * Target and reference clocks are specified in kHz.
785 *
786 * If match_clock is provided, then best_clock P divider must match the P
787 * divider from @match_clock used for LVDS downclocking.
997c030c 788 */
d4906093 789static bool
1b6f4958 790g4x_find_best_dpll(const struct intel_limit *limit,
a93e255f 791 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
792 int target, int refclk, struct dpll *match_clock,
793 struct dpll *best_clock)
d4906093 794{
3b1429d9 795 struct drm_device *dev = crtc_state->base.crtc->dev;
9e2c8475 796 struct dpll clock;
d4906093 797 int max_n;
3b1429d9 798 bool found = false;
6ba770dc
AJ
799 /* approximately equals target * 0.00585 */
800 int err_most = (target >> 8) + (target >> 9);
d4906093
ML
801
802 memset(best_clock, 0, sizeof(*best_clock));
3b1429d9
VS
803
804 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
805
d4906093 806 max_n = limit->n.max;
f77f13e2 807 /* based on hardware requirement, prefer smaller n to precision */
d4906093 808 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
f77f13e2 809 /* based on hardware requirement, prefere larger m1,m2 */
d4906093
ML
810 for (clock.m1 = limit->m1.max;
811 clock.m1 >= limit->m1.min; clock.m1--) {
812 for (clock.m2 = limit->m2.max;
813 clock.m2 >= limit->m2.min; clock.m2--) {
814 for (clock.p1 = limit->p1.max;
815 clock.p1 >= limit->p1.min; clock.p1--) {
816 int this_err;
817
dccbea3b 818 i9xx_calc_dpll_params(refclk, &clock);
e2d214ae
TU
819 if (!intel_PLL_is_valid(to_i915(dev),
820 limit,
1b894b59 821 &clock))
d4906093 822 continue;
1b894b59
CW
823
824 this_err = abs(clock.dot - target);
d4906093
ML
825 if (this_err < err_most) {
826 *best_clock = clock;
827 err_most = this_err;
828 max_n = clock.n;
829 found = true;
830 }
831 }
832 }
833 }
834 }
2c07245f
ZW
835 return found;
836}
837
d5dd62bd
ID
838/*
839 * Check if the calculated PLL configuration is more optimal compared to the
840 * best configuration and error found so far. Return the calculated error.
841 */
842static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
9e2c8475
ACO
843 const struct dpll *calculated_clock,
844 const struct dpll *best_clock,
d5dd62bd
ID
845 unsigned int best_error_ppm,
846 unsigned int *error_ppm)
847{
9ca3ba01
ID
848 /*
849 * For CHV ignore the error and consider only the P value.
850 * Prefer a bigger P value based on HW requirements.
851 */
920a14b2 852 if (IS_CHERRYVIEW(to_i915(dev))) {
9ca3ba01
ID
853 *error_ppm = 0;
854
855 return calculated_clock->p > best_clock->p;
856 }
857
24be4e46
ID
858 if (WARN_ON_ONCE(!target_freq))
859 return false;
860
d5dd62bd
ID
861 *error_ppm = div_u64(1000000ULL *
862 abs(target_freq - calculated_clock->dot),
863 target_freq);
864 /*
865 * Prefer a better P value over a better (smaller) error if the error
866 * is small. Ensure this preference for future configurations too by
867 * setting the error to 0.
868 */
869 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
870 *error_ppm = 0;
871
872 return true;
873 }
874
875 return *error_ppm + 10 < best_error_ppm;
876}
877
65b3d6a9
ACO
878/*
879 * Returns a set of divisors for the desired target clock with the given
880 * refclk, or FALSE. The returned values represent the clock equation:
881 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
882 */
a0c4da24 883static bool
1b6f4958 884vlv_find_best_dpll(const struct intel_limit *limit,
a93e255f 885 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
886 int target, int refclk, struct dpll *match_clock,
887 struct dpll *best_clock)
a0c4da24 888{
a93e255f 889 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 890 struct drm_device *dev = crtc->base.dev;
9e2c8475 891 struct dpll clock;
69e4f900 892 unsigned int bestppm = 1000000;
27e639bf
VS
893 /* min update 19.2 MHz */
894 int max_n = min(limit->n.max, refclk / 19200);
49e497ef 895 bool found = false;
a0c4da24 896
6b4bf1c4
VS
897 target *= 5; /* fast clock */
898
899 memset(best_clock, 0, sizeof(*best_clock));
a0c4da24
JB
900
901 /* based on hardware requirement, prefer smaller n to precision */
27e639bf 902 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
811bbf05 903 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
889059d8 904 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
c1a9ae43 905 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
6b4bf1c4 906 clock.p = clock.p1 * clock.p2;
a0c4da24 907 /* based on hardware requirement, prefer bigger m1,m2 values */
6b4bf1c4 908 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
d5dd62bd 909 unsigned int ppm;
69e4f900 910
6b4bf1c4
VS
911 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
912 refclk * clock.m1);
913
dccbea3b 914 vlv_calc_dpll_params(refclk, &clock);
43b0ac53 915
e2d214ae
TU
916 if (!intel_PLL_is_valid(to_i915(dev),
917 limit,
f01b7962 918 &clock))
43b0ac53
VS
919 continue;
920
d5dd62bd
ID
921 if (!vlv_PLL_is_optimal(dev, target,
922 &clock,
923 best_clock,
924 bestppm, &ppm))
925 continue;
6b4bf1c4 926
d5dd62bd
ID
927 *best_clock = clock;
928 bestppm = ppm;
929 found = true;
a0c4da24
JB
930 }
931 }
932 }
933 }
a0c4da24 934
49e497ef 935 return found;
a0c4da24 936}
a4fc5ed6 937
65b3d6a9
ACO
938/*
939 * Returns a set of divisors for the desired target clock with the given
940 * refclk, or FALSE. The returned values represent the clock equation:
941 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
942 */
ef9348c8 943static bool
1b6f4958 944chv_find_best_dpll(const struct intel_limit *limit,
a93e255f 945 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
946 int target, int refclk, struct dpll *match_clock,
947 struct dpll *best_clock)
ef9348c8 948{
a93e255f 949 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 950 struct drm_device *dev = crtc->base.dev;
9ca3ba01 951 unsigned int best_error_ppm;
9e2c8475 952 struct dpll clock;
ef9348c8
CML
953 uint64_t m2;
954 int found = false;
955
956 memset(best_clock, 0, sizeof(*best_clock));
9ca3ba01 957 best_error_ppm = 1000000;
ef9348c8
CML
958
959 /*
960 * Based on hardware doc, the n always set to 1, and m1 always
961 * set to 2. If requires to support 200Mhz refclk, we need to
962 * revisit this because n may not 1 anymore.
963 */
964 clock.n = 1, clock.m1 = 2;
965 target *= 5; /* fast clock */
966
967 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
968 for (clock.p2 = limit->p2.p2_fast;
969 clock.p2 >= limit->p2.p2_slow;
970 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
9ca3ba01 971 unsigned int error_ppm;
ef9348c8
CML
972
973 clock.p = clock.p1 * clock.p2;
974
975 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
976 clock.n) << 22, refclk * clock.m1);
977
978 if (m2 > INT_MAX/clock.m1)
979 continue;
980
981 clock.m2 = m2;
982
dccbea3b 983 chv_calc_dpll_params(refclk, &clock);
ef9348c8 984
e2d214ae 985 if (!intel_PLL_is_valid(to_i915(dev), limit, &clock))
ef9348c8
CML
986 continue;
987
9ca3ba01
ID
988 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
989 best_error_ppm, &error_ppm))
990 continue;
991
992 *best_clock = clock;
993 best_error_ppm = error_ppm;
994 found = true;
ef9348c8
CML
995 }
996 }
997
998 return found;
999}
1000
5ab7b0b7 1001bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
9e2c8475 1002 struct dpll *best_clock)
5ab7b0b7 1003{
65b3d6a9 1004 int refclk = 100000;
1b6f4958 1005 const struct intel_limit *limit = &intel_limits_bxt;
5ab7b0b7 1006
65b3d6a9 1007 return chv_find_best_dpll(limit, crtc_state,
5ab7b0b7
ID
1008 target_clock, refclk, NULL, best_clock);
1009}
1010
525b9311 1011bool intel_crtc_active(struct intel_crtc *crtc)
20ddf665 1012{
20ddf665
VS
1013 /* Be paranoid as we can arrive here with only partial
1014 * state retrieved from the hardware during setup.
1015 *
241bfc38 1016 * We can ditch the adjusted_mode.crtc_clock check as soon
20ddf665
VS
1017 * as Haswell has gained clock readout/fastboot support.
1018 *
66e514c1 1019 * We can ditch the crtc->primary->fb check as soon as we can
20ddf665 1020 * properly reconstruct framebuffers.
c3d1f436
MR
1021 *
1022 * FIXME: The intel_crtc->active here should be switched to
1023 * crtc->state->active once we have proper CRTC states wired up
1024 * for atomic.
20ddf665 1025 */
525b9311
VS
1026 return crtc->active && crtc->base.primary->state->fb &&
1027 crtc->config->base.adjusted_mode.crtc_clock;
20ddf665
VS
1028}
1029
a5c961d1
PZ
1030enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1031 enum pipe pipe)
1032{
98187836 1033 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
a5c961d1 1034
e2af48c6 1035 return crtc->config->cpu_transcoder;
a5c961d1
PZ
1036}
1037
6315b5d3 1038static bool pipe_dsl_stopped(struct drm_i915_private *dev_priv, enum pipe pipe)
fbf49ea2 1039{
f0f59a00 1040 i915_reg_t reg = PIPEDSL(pipe);
fbf49ea2
VS
1041 u32 line1, line2;
1042 u32 line_mask;
1043
5db94019 1044 if (IS_GEN2(dev_priv))
fbf49ea2
VS
1045 line_mask = DSL_LINEMASK_GEN2;
1046 else
1047 line_mask = DSL_LINEMASK_GEN3;
1048
1049 line1 = I915_READ(reg) & line_mask;
6adfb1ef 1050 msleep(5);
fbf49ea2
VS
1051 line2 = I915_READ(reg) & line_mask;
1052
1053 return line1 == line2;
1054}
1055
ab7ad7f6
KP
1056/*
1057 * intel_wait_for_pipe_off - wait for pipe to turn off
575f7ab7 1058 * @crtc: crtc whose pipe to wait for
9d0498a2
JB
1059 *
1060 * After disabling a pipe, we can't wait for vblank in the usual way,
1061 * spinning on the vblank interrupt status bit, since we won't actually
1062 * see an interrupt when the pipe is disabled.
1063 *
ab7ad7f6
KP
1064 * On Gen4 and above:
1065 * wait for the pipe register state bit to turn off
1066 *
1067 * Otherwise:
1068 * wait for the display line value to settle (it usually
1069 * ends up stopping at the start of the next frame).
58e10eb9 1070 *
9d0498a2 1071 */
575f7ab7 1072static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
9d0498a2 1073{
6315b5d3 1074 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6e3c9717 1075 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 1076 enum pipe pipe = crtc->pipe;
ab7ad7f6 1077
6315b5d3 1078 if (INTEL_GEN(dev_priv) >= 4) {
f0f59a00 1079 i915_reg_t reg = PIPECONF(cpu_transcoder);
ab7ad7f6
KP
1080
1081 /* Wait for the Pipe State to go off */
b8511f53
CW
1082 if (intel_wait_for_register(dev_priv,
1083 reg, I965_PIPECONF_ACTIVE, 0,
1084 100))
284637d9 1085 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1086 } else {
ab7ad7f6 1087 /* Wait for the display line to settle */
6315b5d3 1088 if (wait_for(pipe_dsl_stopped(dev_priv, pipe), 100))
284637d9 1089 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1090 }
79e53945
JB
1091}
1092
b24e7179 1093/* Only for pre-ILK configs */
55607e8a
DV
1094void assert_pll(struct drm_i915_private *dev_priv,
1095 enum pipe pipe, bool state)
b24e7179 1096{
b24e7179
JB
1097 u32 val;
1098 bool cur_state;
1099
649636ef 1100 val = I915_READ(DPLL(pipe));
b24e7179 1101 cur_state = !!(val & DPLL_VCO_ENABLE);
e2c719b7 1102 I915_STATE_WARN(cur_state != state,
b24e7179 1103 "PLL state assertion failure (expected %s, current %s)\n",
87ad3212 1104 onoff(state), onoff(cur_state));
b24e7179 1105}
b24e7179 1106
23538ef1 1107/* XXX: the dsi pll is shared between MIPI DSI ports */
8563b1e8 1108void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
23538ef1
JN
1109{
1110 u32 val;
1111 bool cur_state;
1112
a580516d 1113 mutex_lock(&dev_priv->sb_lock);
23538ef1 1114 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
a580516d 1115 mutex_unlock(&dev_priv->sb_lock);
23538ef1
JN
1116
1117 cur_state = val & DSI_PLL_VCO_EN;
e2c719b7 1118 I915_STATE_WARN(cur_state != state,
23538ef1 1119 "DSI PLL state assertion failure (expected %s, current %s)\n",
87ad3212 1120 onoff(state), onoff(cur_state));
23538ef1 1121}
23538ef1 1122
040484af
JB
1123static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1124 enum pipe pipe, bool state)
1125{
040484af 1126 bool cur_state;
ad80a810
PZ
1127 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1128 pipe);
040484af 1129
2d1fe073 1130 if (HAS_DDI(dev_priv)) {
affa9354 1131 /* DDI does not have a specific FDI_TX register */
649636ef 1132 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
ad80a810 1133 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
bf507ef7 1134 } else {
649636ef 1135 u32 val = I915_READ(FDI_TX_CTL(pipe));
bf507ef7
ED
1136 cur_state = !!(val & FDI_TX_ENABLE);
1137 }
e2c719b7 1138 I915_STATE_WARN(cur_state != state,
040484af 1139 "FDI TX state assertion failure (expected %s, current %s)\n",
87ad3212 1140 onoff(state), onoff(cur_state));
040484af
JB
1141}
1142#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1143#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1144
1145static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1146 enum pipe pipe, bool state)
1147{
040484af
JB
1148 u32 val;
1149 bool cur_state;
1150
649636ef 1151 val = I915_READ(FDI_RX_CTL(pipe));
d63fa0dc 1152 cur_state = !!(val & FDI_RX_ENABLE);
e2c719b7 1153 I915_STATE_WARN(cur_state != state,
040484af 1154 "FDI RX state assertion failure (expected %s, current %s)\n",
87ad3212 1155 onoff(state), onoff(cur_state));
040484af
JB
1156}
1157#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1158#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1159
1160static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1161 enum pipe pipe)
1162{
040484af
JB
1163 u32 val;
1164
1165 /* ILK FDI PLL is always enabled */
7e22dbbb 1166 if (IS_GEN5(dev_priv))
040484af
JB
1167 return;
1168
bf507ef7 1169 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
2d1fe073 1170 if (HAS_DDI(dev_priv))
bf507ef7
ED
1171 return;
1172
649636ef 1173 val = I915_READ(FDI_TX_CTL(pipe));
e2c719b7 1174 I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
040484af
JB
1175}
1176
55607e8a
DV
1177void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1178 enum pipe pipe, bool state)
040484af 1179{
040484af 1180 u32 val;
55607e8a 1181 bool cur_state;
040484af 1182
649636ef 1183 val = I915_READ(FDI_RX_CTL(pipe));
55607e8a 1184 cur_state = !!(val & FDI_RX_PLL_ENABLE);
e2c719b7 1185 I915_STATE_WARN(cur_state != state,
55607e8a 1186 "FDI RX PLL assertion failure (expected %s, current %s)\n",
87ad3212 1187 onoff(state), onoff(cur_state));
040484af
JB
1188}
1189
4f8036a2 1190void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
ea0760cf 1191{
f0f59a00 1192 i915_reg_t pp_reg;
ea0760cf
JB
1193 u32 val;
1194 enum pipe panel_pipe = PIPE_A;
0de3b485 1195 bool locked = true;
ea0760cf 1196
4f8036a2 1197 if (WARN_ON(HAS_DDI(dev_priv)))
bedd4dba
JN
1198 return;
1199
4f8036a2 1200 if (HAS_PCH_SPLIT(dev_priv)) {
bedd4dba
JN
1201 u32 port_sel;
1202
44cb734c
ID
1203 pp_reg = PP_CONTROL(0);
1204 port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
bedd4dba
JN
1205
1206 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1207 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1208 panel_pipe = PIPE_B;
1209 /* XXX: else fix for eDP */
4f8036a2 1210 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
bedd4dba 1211 /* presumably write lock depends on pipe, not port select */
44cb734c 1212 pp_reg = PP_CONTROL(pipe);
bedd4dba 1213 panel_pipe = pipe;
ea0760cf 1214 } else {
44cb734c 1215 pp_reg = PP_CONTROL(0);
bedd4dba
JN
1216 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1217 panel_pipe = PIPE_B;
ea0760cf
JB
1218 }
1219
1220 val = I915_READ(pp_reg);
1221 if (!(val & PANEL_POWER_ON) ||
ec49ba2d 1222 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
ea0760cf
JB
1223 locked = false;
1224
e2c719b7 1225 I915_STATE_WARN(panel_pipe == pipe && locked,
ea0760cf 1226 "panel assertion failure, pipe %c regs locked\n",
9db4a9c7 1227 pipe_name(pipe));
ea0760cf
JB
1228}
1229
93ce0ba6
JN
1230static void assert_cursor(struct drm_i915_private *dev_priv,
1231 enum pipe pipe, bool state)
1232{
93ce0ba6
JN
1233 bool cur_state;
1234
2a307c2e 1235 if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
0b87c24e 1236 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
d9d82081 1237 else
5efb3e28 1238 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
93ce0ba6 1239
e2c719b7 1240 I915_STATE_WARN(cur_state != state,
93ce0ba6 1241 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
87ad3212 1242 pipe_name(pipe), onoff(state), onoff(cur_state));
93ce0ba6
JN
1243}
1244#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1245#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1246
b840d907
JB
1247void assert_pipe(struct drm_i915_private *dev_priv,
1248 enum pipe pipe, bool state)
b24e7179 1249{
63d7bbe9 1250 bool cur_state;
702e7a56
PZ
1251 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1252 pipe);
4feed0eb 1253 enum intel_display_power_domain power_domain;
b24e7179 1254
b6b5d049
VS
1255 /* if we need the pipe quirk it must be always on */
1256 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1257 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
8e636784
DV
1258 state = true;
1259
4feed0eb
ID
1260 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1261 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
649636ef 1262 u32 val = I915_READ(PIPECONF(cpu_transcoder));
69310161 1263 cur_state = !!(val & PIPECONF_ENABLE);
4feed0eb
ID
1264
1265 intel_display_power_put(dev_priv, power_domain);
1266 } else {
1267 cur_state = false;
69310161
PZ
1268 }
1269
e2c719b7 1270 I915_STATE_WARN(cur_state != state,
63d7bbe9 1271 "pipe %c assertion failure (expected %s, current %s)\n",
87ad3212 1272 pipe_name(pipe), onoff(state), onoff(cur_state));
b24e7179
JB
1273}
1274
931872fc
CW
1275static void assert_plane(struct drm_i915_private *dev_priv,
1276 enum plane plane, bool state)
b24e7179 1277{
b24e7179 1278 u32 val;
931872fc 1279 bool cur_state;
b24e7179 1280
649636ef 1281 val = I915_READ(DSPCNTR(plane));
931872fc 1282 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
e2c719b7 1283 I915_STATE_WARN(cur_state != state,
931872fc 1284 "plane %c assertion failure (expected %s, current %s)\n",
87ad3212 1285 plane_name(plane), onoff(state), onoff(cur_state));
b24e7179
JB
1286}
1287
931872fc
CW
1288#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1289#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1290
b24e7179
JB
1291static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1292 enum pipe pipe)
1293{
649636ef 1294 int i;
b24e7179 1295
653e1026 1296 /* Primary planes are fixed to pipes on gen4+ */
6315b5d3 1297 if (INTEL_GEN(dev_priv) >= 4) {
649636ef 1298 u32 val = I915_READ(DSPCNTR(pipe));
e2c719b7 1299 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
28c05794
AJ
1300 "plane %c assertion failure, should be disabled but not\n",
1301 plane_name(pipe));
19ec1358 1302 return;
28c05794 1303 }
19ec1358 1304
b24e7179 1305 /* Need to check both planes against the pipe */
055e393f 1306 for_each_pipe(dev_priv, i) {
649636ef
VS
1307 u32 val = I915_READ(DSPCNTR(i));
1308 enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
b24e7179 1309 DISPPLANE_SEL_PIPE_SHIFT;
e2c719b7 1310 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
9db4a9c7
JB
1311 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1312 plane_name(i), pipe_name(pipe));
b24e7179
JB
1313 }
1314}
1315
19332d7a
JB
1316static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1317 enum pipe pipe)
1318{
649636ef 1319 int sprite;
19332d7a 1320
6315b5d3 1321 if (INTEL_GEN(dev_priv) >= 9) {
3bdcfc0c 1322 for_each_sprite(dev_priv, pipe, sprite) {
649636ef 1323 u32 val = I915_READ(PLANE_CTL(pipe, sprite));
e2c719b7 1324 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
7feb8b88
DL
1325 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1326 sprite, pipe_name(pipe));
1327 }
920a14b2 1328 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
3bdcfc0c 1329 for_each_sprite(dev_priv, pipe, sprite) {
83c04a62 1330 u32 val = I915_READ(SPCNTR(pipe, PLANE_SPRITE0 + sprite));
e2c719b7 1331 I915_STATE_WARN(val & SP_ENABLE,
20674eef 1332 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1fe47785 1333 sprite_name(pipe, sprite), pipe_name(pipe));
20674eef 1334 }
6315b5d3 1335 } else if (INTEL_GEN(dev_priv) >= 7) {
649636ef 1336 u32 val = I915_READ(SPRCTL(pipe));
e2c719b7 1337 I915_STATE_WARN(val & SPRITE_ENABLE,
06da8da2 1338 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef 1339 plane_name(pipe), pipe_name(pipe));
6315b5d3 1340 } else if (INTEL_GEN(dev_priv) >= 5) {
649636ef 1341 u32 val = I915_READ(DVSCNTR(pipe));
e2c719b7 1342 I915_STATE_WARN(val & DVS_ENABLE,
06da8da2 1343 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef 1344 plane_name(pipe), pipe_name(pipe));
19332d7a
JB
1345 }
1346}
1347
08c71e5e
VS
1348static void assert_vblank_disabled(struct drm_crtc *crtc)
1349{
e2c719b7 1350 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
08c71e5e
VS
1351 drm_crtc_vblank_put(crtc);
1352}
1353
7abd4b35
ACO
1354void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1355 enum pipe pipe)
92f2584a 1356{
92f2584a
JB
1357 u32 val;
1358 bool enabled;
1359
649636ef 1360 val = I915_READ(PCH_TRANSCONF(pipe));
92f2584a 1361 enabled = !!(val & TRANS_ENABLE);
e2c719b7 1362 I915_STATE_WARN(enabled,
9db4a9c7
JB
1363 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1364 pipe_name(pipe));
92f2584a
JB
1365}
1366
4e634389
KP
1367static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1368 enum pipe pipe, u32 port_sel, u32 val)
f0575e92
KP
1369{
1370 if ((val & DP_PORT_EN) == 0)
1371 return false;
1372
2d1fe073 1373 if (HAS_PCH_CPT(dev_priv)) {
f0f59a00 1374 u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
f0575e92
KP
1375 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1376 return false;
2d1fe073 1377 } else if (IS_CHERRYVIEW(dev_priv)) {
44f37d1f
CML
1378 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1379 return false;
f0575e92
KP
1380 } else {
1381 if ((val & DP_PIPE_MASK) != (pipe << 30))
1382 return false;
1383 }
1384 return true;
1385}
1386
1519b995
KP
1387static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1388 enum pipe pipe, u32 val)
1389{
dc0fa718 1390 if ((val & SDVO_ENABLE) == 0)
1519b995
KP
1391 return false;
1392
2d1fe073 1393 if (HAS_PCH_CPT(dev_priv)) {
dc0fa718 1394 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1519b995 1395 return false;
2d1fe073 1396 } else if (IS_CHERRYVIEW(dev_priv)) {
44f37d1f
CML
1397 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1398 return false;
1519b995 1399 } else {
dc0fa718 1400 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1519b995
KP
1401 return false;
1402 }
1403 return true;
1404}
1405
1406static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1407 enum pipe pipe, u32 val)
1408{
1409 if ((val & LVDS_PORT_EN) == 0)
1410 return false;
1411
2d1fe073 1412 if (HAS_PCH_CPT(dev_priv)) {
1519b995
KP
1413 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1414 return false;
1415 } else {
1416 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1417 return false;
1418 }
1419 return true;
1420}
1421
1422static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1423 enum pipe pipe, u32 val)
1424{
1425 if ((val & ADPA_DAC_ENABLE) == 0)
1426 return false;
2d1fe073 1427 if (HAS_PCH_CPT(dev_priv)) {
1519b995
KP
1428 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1429 return false;
1430 } else {
1431 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1432 return false;
1433 }
1434 return true;
1435}
1436
291906f1 1437static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
f0f59a00
VS
1438 enum pipe pipe, i915_reg_t reg,
1439 u32 port_sel)
291906f1 1440{
47a05eca 1441 u32 val = I915_READ(reg);
e2c719b7 1442 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
291906f1 1443 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
f0f59a00 1444 i915_mmio_reg_offset(reg), pipe_name(pipe));
de9a35ab 1445
2d1fe073 1446 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
75c5da27 1447 && (val & DP_PIPEB_SELECT),
de9a35ab 1448 "IBX PCH dp port still using transcoder B\n");
291906f1
JB
1449}
1450
1451static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
f0f59a00 1452 enum pipe pipe, i915_reg_t reg)
291906f1 1453{
47a05eca 1454 u32 val = I915_READ(reg);
e2c719b7 1455 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
23c99e77 1456 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
f0f59a00 1457 i915_mmio_reg_offset(reg), pipe_name(pipe));
de9a35ab 1458
2d1fe073 1459 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
75c5da27 1460 && (val & SDVO_PIPE_B_SELECT),
de9a35ab 1461 "IBX PCH hdmi port still using transcoder B\n");
291906f1
JB
1462}
1463
1464static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1465 enum pipe pipe)
1466{
291906f1 1467 u32 val;
291906f1 1468
f0575e92
KP
1469 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1470 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1471 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
291906f1 1472
649636ef 1473 val = I915_READ(PCH_ADPA);
e2c719b7 1474 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
291906f1 1475 "PCH VGA enabled on transcoder %c, should be disabled\n",
9db4a9c7 1476 pipe_name(pipe));
291906f1 1477
649636ef 1478 val = I915_READ(PCH_LVDS);
e2c719b7 1479 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
291906f1 1480 "PCH LVDS enabled on transcoder %c, should be disabled\n",
9db4a9c7 1481 pipe_name(pipe));
291906f1 1482
e2debe91
PZ
1483 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1484 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1485 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
291906f1
JB
1486}
1487
cd2d34d9
VS
1488static void _vlv_enable_pll(struct intel_crtc *crtc,
1489 const struct intel_crtc_state *pipe_config)
1490{
1491 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1492 enum pipe pipe = crtc->pipe;
1493
1494 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1495 POSTING_READ(DPLL(pipe));
1496 udelay(150);
1497
2c30b43b
CW
1498 if (intel_wait_for_register(dev_priv,
1499 DPLL(pipe),
1500 DPLL_LOCK_VLV,
1501 DPLL_LOCK_VLV,
1502 1))
cd2d34d9
VS
1503 DRM_ERROR("DPLL %d failed to lock\n", pipe);
1504}
1505
d288f65f 1506static void vlv_enable_pll(struct intel_crtc *crtc,
5cec258b 1507 const struct intel_crtc_state *pipe_config)
87442f73 1508{
cd2d34d9 1509 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8bd3f301 1510 enum pipe pipe = crtc->pipe;
87442f73 1511
8bd3f301 1512 assert_pipe_disabled(dev_priv, pipe);
87442f73 1513
87442f73 1514 /* PLL is protected by panel, make sure we can write it */
7d1a83cb 1515 assert_panel_unlocked(dev_priv, pipe);
87442f73 1516
cd2d34d9
VS
1517 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1518 _vlv_enable_pll(crtc, pipe_config);
426115cf 1519
8bd3f301
VS
1520 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1521 POSTING_READ(DPLL_MD(pipe));
87442f73
DV
1522}
1523
cd2d34d9
VS
1524
1525static void _chv_enable_pll(struct intel_crtc *crtc,
1526 const struct intel_crtc_state *pipe_config)
9d556c99 1527{
cd2d34d9 1528 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8bd3f301 1529 enum pipe pipe = crtc->pipe;
9d556c99 1530 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9d556c99
CML
1531 u32 tmp;
1532
a580516d 1533 mutex_lock(&dev_priv->sb_lock);
9d556c99
CML
1534
1535 /* Enable back the 10bit clock to display controller */
1536 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1537 tmp |= DPIO_DCLKP_EN;
1538 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1539
54433e91
VS
1540 mutex_unlock(&dev_priv->sb_lock);
1541
9d556c99
CML
1542 /*
1543 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1544 */
1545 udelay(1);
1546
1547 /* Enable PLL */
d288f65f 1548 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
9d556c99
CML
1549
1550 /* Check PLL is locked */
6b18826a
CW
1551 if (intel_wait_for_register(dev_priv,
1552 DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
1553 1))
9d556c99 1554 DRM_ERROR("PLL %d failed to lock\n", pipe);
cd2d34d9
VS
1555}
1556
1557static void chv_enable_pll(struct intel_crtc *crtc,
1558 const struct intel_crtc_state *pipe_config)
1559{
1560 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1561 enum pipe pipe = crtc->pipe;
1562
1563 assert_pipe_disabled(dev_priv, pipe);
1564
1565 /* PLL is protected by panel, make sure we can write it */
1566 assert_panel_unlocked(dev_priv, pipe);
1567
1568 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1569 _chv_enable_pll(crtc, pipe_config);
9d556c99 1570
c231775c
VS
1571 if (pipe != PIPE_A) {
1572 /*
1573 * WaPixelRepeatModeFixForC0:chv
1574 *
1575 * DPLLCMD is AWOL. Use chicken bits to propagate
1576 * the value from DPLLBMD to either pipe B or C.
1577 */
1578 I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
1579 I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
1580 I915_WRITE(CBR4_VLV, 0);
1581 dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
1582
1583 /*
1584 * DPLLB VGA mode also seems to cause problems.
1585 * We should always have it disabled.
1586 */
1587 WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
1588 } else {
1589 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1590 POSTING_READ(DPLL_MD(pipe));
1591 }
9d556c99
CML
1592}
1593
6315b5d3 1594static int intel_num_dvo_pipes(struct drm_i915_private *dev_priv)
1c4e0274
VS
1595{
1596 struct intel_crtc *crtc;
1597 int count = 0;
1598
6315b5d3 1599 for_each_intel_crtc(&dev_priv->drm, crtc) {
3538b9df 1600 count += crtc->base.state->active &&
2d84d2b3
VS
1601 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
1602 }
1c4e0274
VS
1603
1604 return count;
1605}
1606
66e3d5c0 1607static void i9xx_enable_pll(struct intel_crtc *crtc)
63d7bbe9 1608{
6315b5d3 1609 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
f0f59a00 1610 i915_reg_t reg = DPLL(crtc->pipe);
6e3c9717 1611 u32 dpll = crtc->config->dpll_hw_state.dpll;
63d7bbe9 1612
66e3d5c0 1613 assert_pipe_disabled(dev_priv, crtc->pipe);
58c6eaa2 1614
63d7bbe9 1615 /* PLL is protected by panel, make sure we can write it */
50a0bc90 1616 if (IS_MOBILE(dev_priv) && !IS_I830(dev_priv))
66e3d5c0 1617 assert_panel_unlocked(dev_priv, crtc->pipe);
63d7bbe9 1618
1c4e0274 1619 /* Enable DVO 2x clock on both PLLs if necessary */
6315b5d3 1620 if (IS_I830(dev_priv) && intel_num_dvo_pipes(dev_priv) > 0) {
1c4e0274
VS
1621 /*
1622 * It appears to be important that we don't enable this
1623 * for the current pipe before otherwise configuring the
1624 * PLL. No idea how this should be handled if multiple
1625 * DVO outputs are enabled simultaneosly.
1626 */
1627 dpll |= DPLL_DVO_2X_MODE;
1628 I915_WRITE(DPLL(!crtc->pipe),
1629 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1630 }
66e3d5c0 1631
c2b63374
VS
1632 /*
1633 * Apparently we need to have VGA mode enabled prior to changing
1634 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1635 * dividers, even though the register value does change.
1636 */
1637 I915_WRITE(reg, 0);
1638
8e7a65aa
VS
1639 I915_WRITE(reg, dpll);
1640
66e3d5c0
DV
1641 /* Wait for the clocks to stabilize. */
1642 POSTING_READ(reg);
1643 udelay(150);
1644
6315b5d3 1645 if (INTEL_GEN(dev_priv) >= 4) {
66e3d5c0 1646 I915_WRITE(DPLL_MD(crtc->pipe),
6e3c9717 1647 crtc->config->dpll_hw_state.dpll_md);
66e3d5c0
DV
1648 } else {
1649 /* The pixel multiplier can only be updated once the
1650 * DPLL is enabled and the clocks are stable.
1651 *
1652 * So write it again.
1653 */
1654 I915_WRITE(reg, dpll);
1655 }
63d7bbe9
JB
1656
1657 /* We do this three times for luck */
66e3d5c0 1658 I915_WRITE(reg, dpll);
63d7bbe9
JB
1659 POSTING_READ(reg);
1660 udelay(150); /* wait for warmup */
66e3d5c0 1661 I915_WRITE(reg, dpll);
63d7bbe9
JB
1662 POSTING_READ(reg);
1663 udelay(150); /* wait for warmup */
66e3d5c0 1664 I915_WRITE(reg, dpll);
63d7bbe9
JB
1665 POSTING_READ(reg);
1666 udelay(150); /* wait for warmup */
1667}
1668
1669/**
50b44a44 1670 * i9xx_disable_pll - disable a PLL
63d7bbe9
JB
1671 * @dev_priv: i915 private structure
1672 * @pipe: pipe PLL to disable
1673 *
1674 * Disable the PLL for @pipe, making sure the pipe is off first.
1675 *
1676 * Note! This is for pre-ILK only.
1677 */
1c4e0274 1678static void i9xx_disable_pll(struct intel_crtc *crtc)
63d7bbe9 1679{
6315b5d3 1680 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1c4e0274
VS
1681 enum pipe pipe = crtc->pipe;
1682
1683 /* Disable DVO 2x clock on both PLLs if necessary */
50a0bc90 1684 if (IS_I830(dev_priv) &&
2d84d2b3 1685 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
6315b5d3 1686 !intel_num_dvo_pipes(dev_priv)) {
1c4e0274
VS
1687 I915_WRITE(DPLL(PIPE_B),
1688 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1689 I915_WRITE(DPLL(PIPE_A),
1690 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1691 }
1692
b6b5d049
VS
1693 /* Don't disable pipe or pipe PLLs if needed */
1694 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1695 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
63d7bbe9
JB
1696 return;
1697
1698 /* Make sure the pipe isn't still relying on us */
1699 assert_pipe_disabled(dev_priv, pipe);
1700
b8afb911 1701 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
50b44a44 1702 POSTING_READ(DPLL(pipe));
63d7bbe9
JB
1703}
1704
f6071166
JB
1705static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1706{
b8afb911 1707 u32 val;
f6071166
JB
1708
1709 /* Make sure the pipe isn't still relying on us */
1710 assert_pipe_disabled(dev_priv, pipe);
1711
03ed5cbf
VS
1712 val = DPLL_INTEGRATED_REF_CLK_VLV |
1713 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1714 if (pipe != PIPE_A)
1715 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1716
f6071166
JB
1717 I915_WRITE(DPLL(pipe), val);
1718 POSTING_READ(DPLL(pipe));
076ed3b2
CML
1719}
1720
1721static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1722{
d752048d 1723 enum dpio_channel port = vlv_pipe_to_channel(pipe);
076ed3b2
CML
1724 u32 val;
1725
a11b0703
VS
1726 /* Make sure the pipe isn't still relying on us */
1727 assert_pipe_disabled(dev_priv, pipe);
076ed3b2 1728
60bfe44f
VS
1729 val = DPLL_SSC_REF_CLK_CHV |
1730 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
a11b0703
VS
1731 if (pipe != PIPE_A)
1732 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
03ed5cbf 1733
a11b0703
VS
1734 I915_WRITE(DPLL(pipe), val);
1735 POSTING_READ(DPLL(pipe));
d752048d 1736
a580516d 1737 mutex_lock(&dev_priv->sb_lock);
d752048d
VS
1738
1739 /* Disable 10bit clock to display controller */
1740 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1741 val &= ~DPIO_DCLKP_EN;
1742 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1743
a580516d 1744 mutex_unlock(&dev_priv->sb_lock);
f6071166
JB
1745}
1746
e4607fcf 1747void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
9b6de0a1
VS
1748 struct intel_digital_port *dport,
1749 unsigned int expected_mask)
89b667f8
JB
1750{
1751 u32 port_mask;
f0f59a00 1752 i915_reg_t dpll_reg;
89b667f8 1753
e4607fcf
CML
1754 switch (dport->port) {
1755 case PORT_B:
89b667f8 1756 port_mask = DPLL_PORTB_READY_MASK;
00fc31b7 1757 dpll_reg = DPLL(0);
e4607fcf
CML
1758 break;
1759 case PORT_C:
89b667f8 1760 port_mask = DPLL_PORTC_READY_MASK;
00fc31b7 1761 dpll_reg = DPLL(0);
9b6de0a1 1762 expected_mask <<= 4;
00fc31b7
CML
1763 break;
1764 case PORT_D:
1765 port_mask = DPLL_PORTD_READY_MASK;
1766 dpll_reg = DPIO_PHY_STATUS;
e4607fcf
CML
1767 break;
1768 default:
1769 BUG();
1770 }
89b667f8 1771
370004d3
CW
1772 if (intel_wait_for_register(dev_priv,
1773 dpll_reg, port_mask, expected_mask,
1774 1000))
9b6de0a1
VS
1775 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1776 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
89b667f8
JB
1777}
1778
b8a4f404
PZ
1779static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1780 enum pipe pipe)
040484af 1781{
98187836
VS
1782 struct intel_crtc *intel_crtc = intel_get_crtc_for_pipe(dev_priv,
1783 pipe);
f0f59a00
VS
1784 i915_reg_t reg;
1785 uint32_t val, pipeconf_val;
040484af 1786
040484af 1787 /* Make sure PCH DPLL is enabled */
8106ddbd 1788 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
040484af
JB
1789
1790 /* FDI must be feeding us bits for PCH ports */
1791 assert_fdi_tx_enabled(dev_priv, pipe);
1792 assert_fdi_rx_enabled(dev_priv, pipe);
1793
6e266956 1794 if (HAS_PCH_CPT(dev_priv)) {
23670b32
DV
1795 /* Workaround: Set the timing override bit before enabling the
1796 * pch transcoder. */
1797 reg = TRANS_CHICKEN2(pipe);
1798 val = I915_READ(reg);
1799 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1800 I915_WRITE(reg, val);
59c859d6 1801 }
23670b32 1802
ab9412ba 1803 reg = PCH_TRANSCONF(pipe);
040484af 1804 val = I915_READ(reg);
5f7f726d 1805 pipeconf_val = I915_READ(PIPECONF(pipe));
e9bcff5c 1806
2d1fe073 1807 if (HAS_PCH_IBX(dev_priv)) {
e9bcff5c 1808 /*
c5de7c6f
VS
1809 * Make the BPC in transcoder be consistent with
1810 * that in pipeconf reg. For HDMI we must use 8bpc
1811 * here for both 8bpc and 12bpc.
e9bcff5c 1812 */
dfd07d72 1813 val &= ~PIPECONF_BPC_MASK;
2d84d2b3 1814 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
c5de7c6f
VS
1815 val |= PIPECONF_8BPC;
1816 else
1817 val |= pipeconf_val & PIPECONF_BPC_MASK;
e9bcff5c 1818 }
5f7f726d
PZ
1819
1820 val &= ~TRANS_INTERLACE_MASK;
1821 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
2d1fe073 1822 if (HAS_PCH_IBX(dev_priv) &&
2d84d2b3 1823 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
7c26e5c6
PZ
1824 val |= TRANS_LEGACY_INTERLACED_ILK;
1825 else
1826 val |= TRANS_INTERLACED;
5f7f726d
PZ
1827 else
1828 val |= TRANS_PROGRESSIVE;
1829
040484af 1830 I915_WRITE(reg, val | TRANS_ENABLE);
650fbd84
CW
1831 if (intel_wait_for_register(dev_priv,
1832 reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
1833 100))
4bb6f1f3 1834 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
040484af
JB
1835}
1836
8fb033d7 1837static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
937bb610 1838 enum transcoder cpu_transcoder)
040484af 1839{
8fb033d7 1840 u32 val, pipeconf_val;
8fb033d7 1841
8fb033d7 1842 /* FDI must be feeding us bits for PCH ports */
1a240d4d 1843 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
937bb610 1844 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
8fb033d7 1845
223a6fdf 1846 /* Workaround: set timing override bit. */
36c0d0cf 1847 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
23670b32 1848 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
36c0d0cf 1849 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
223a6fdf 1850
25f3ef11 1851 val = TRANS_ENABLE;
937bb610 1852 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
8fb033d7 1853
9a76b1c6
PZ
1854 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1855 PIPECONF_INTERLACED_ILK)
a35f2679 1856 val |= TRANS_INTERLACED;
8fb033d7
PZ
1857 else
1858 val |= TRANS_PROGRESSIVE;
1859
ab9412ba 1860 I915_WRITE(LPT_TRANSCONF, val);
d9f96244
CW
1861 if (intel_wait_for_register(dev_priv,
1862 LPT_TRANSCONF,
1863 TRANS_STATE_ENABLE,
1864 TRANS_STATE_ENABLE,
1865 100))
937bb610 1866 DRM_ERROR("Failed to enable PCH transcoder\n");
8fb033d7
PZ
1867}
1868
b8a4f404
PZ
1869static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1870 enum pipe pipe)
040484af 1871{
f0f59a00
VS
1872 i915_reg_t reg;
1873 uint32_t val;
040484af
JB
1874
1875 /* FDI relies on the transcoder */
1876 assert_fdi_tx_disabled(dev_priv, pipe);
1877 assert_fdi_rx_disabled(dev_priv, pipe);
1878
291906f1
JB
1879 /* Ports must be off as well */
1880 assert_pch_ports_disabled(dev_priv, pipe);
1881
ab9412ba 1882 reg = PCH_TRANSCONF(pipe);
040484af
JB
1883 val = I915_READ(reg);
1884 val &= ~TRANS_ENABLE;
1885 I915_WRITE(reg, val);
1886 /* wait for PCH transcoder off, transcoder state */
a7d04662
CW
1887 if (intel_wait_for_register(dev_priv,
1888 reg, TRANS_STATE_ENABLE, 0,
1889 50))
4bb6f1f3 1890 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
23670b32 1891
6e266956 1892 if (HAS_PCH_CPT(dev_priv)) {
23670b32
DV
1893 /* Workaround: Clear the timing override chicken bit again. */
1894 reg = TRANS_CHICKEN2(pipe);
1895 val = I915_READ(reg);
1896 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1897 I915_WRITE(reg, val);
1898 }
040484af
JB
1899}
1900
b7076546 1901void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
8fb033d7 1902{
8fb033d7
PZ
1903 u32 val;
1904
ab9412ba 1905 val = I915_READ(LPT_TRANSCONF);
8fb033d7 1906 val &= ~TRANS_ENABLE;
ab9412ba 1907 I915_WRITE(LPT_TRANSCONF, val);
8fb033d7 1908 /* wait for PCH transcoder off, transcoder state */
dfdb4749
CW
1909 if (intel_wait_for_register(dev_priv,
1910 LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
1911 50))
8a52fd9f 1912 DRM_ERROR("Failed to disable PCH transcoder\n");
223a6fdf
PZ
1913
1914 /* Workaround: clear timing override bit. */
36c0d0cf 1915 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
23670b32 1916 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
36c0d0cf 1917 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
040484af
JB
1918}
1919
65f2130c
VS
1920enum transcoder intel_crtc_pch_transcoder(struct intel_crtc *crtc)
1921{
1922 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1923
1924 WARN_ON(!crtc->config->has_pch_encoder);
1925
1926 if (HAS_PCH_LPT(dev_priv))
1927 return TRANSCODER_A;
1928 else
1929 return (enum transcoder) crtc->pipe;
1930}
1931
b24e7179 1932/**
309cfea8 1933 * intel_enable_pipe - enable a pipe, asserting requirements
0372264a 1934 * @crtc: crtc responsible for the pipe
b24e7179 1935 *
0372264a 1936 * Enable @crtc's pipe, making sure that various hardware specific requirements
b24e7179 1937 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
b24e7179 1938 */
e1fdc473 1939static void intel_enable_pipe(struct intel_crtc *crtc)
b24e7179 1940{
0372264a 1941 struct drm_device *dev = crtc->base.dev;
fac5e23e 1942 struct drm_i915_private *dev_priv = to_i915(dev);
0372264a 1943 enum pipe pipe = crtc->pipe;
1a70a728 1944 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
f0f59a00 1945 i915_reg_t reg;
b24e7179
JB
1946 u32 val;
1947
9e2ee2dd
VS
1948 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1949
58c6eaa2 1950 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 1951 assert_cursor_disabled(dev_priv, pipe);
58c6eaa2
DV
1952 assert_sprites_disabled(dev_priv, pipe);
1953
b24e7179
JB
1954 /*
1955 * A pipe without a PLL won't actually be able to drive bits from
1956 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1957 * need the check.
1958 */
09fa8bb9 1959 if (HAS_GMCH_DISPLAY(dev_priv)) {
d7edc4e5 1960 if (intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DSI))
23538ef1
JN
1961 assert_dsi_pll_enabled(dev_priv);
1962 else
1963 assert_pll_enabled(dev_priv, pipe);
09fa8bb9 1964 } else {
6e3c9717 1965 if (crtc->config->has_pch_encoder) {
040484af 1966 /* if driving the PCH, we need FDI enabled */
65f2130c
VS
1967 assert_fdi_rx_pll_enabled(dev_priv,
1968 (enum pipe) intel_crtc_pch_transcoder(crtc));
1a240d4d
DV
1969 assert_fdi_tx_pll_enabled(dev_priv,
1970 (enum pipe) cpu_transcoder);
040484af
JB
1971 }
1972 /* FIXME: assert CPU port conditions for SNB+ */
1973 }
b24e7179 1974
702e7a56 1975 reg = PIPECONF(cpu_transcoder);
b24e7179 1976 val = I915_READ(reg);
7ad25d48 1977 if (val & PIPECONF_ENABLE) {
b6b5d049
VS
1978 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1979 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
00d70b15 1980 return;
7ad25d48 1981 }
00d70b15
CW
1982
1983 I915_WRITE(reg, val | PIPECONF_ENABLE);
851855d8 1984 POSTING_READ(reg);
b7792d8b
VS
1985
1986 /*
1987 * Until the pipe starts DSL will read as 0, which would cause
1988 * an apparent vblank timestamp jump, which messes up also the
1989 * frame count when it's derived from the timestamps. So let's
1990 * wait for the pipe to start properly before we call
1991 * drm_crtc_vblank_on()
1992 */
1993 if (dev->max_vblank_count == 0 &&
1994 wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
1995 DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
b24e7179
JB
1996}
1997
1998/**
309cfea8 1999 * intel_disable_pipe - disable a pipe, asserting requirements
575f7ab7 2000 * @crtc: crtc whose pipes is to be disabled
b24e7179 2001 *
575f7ab7
VS
2002 * Disable the pipe of @crtc, making sure that various hardware
2003 * specific requirements are met, if applicable, e.g. plane
2004 * disabled, panel fitter off, etc.
b24e7179
JB
2005 *
2006 * Will wait until the pipe has shut down before returning.
2007 */
575f7ab7 2008static void intel_disable_pipe(struct intel_crtc *crtc)
b24e7179 2009{
fac5e23e 2010 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6e3c9717 2011 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 2012 enum pipe pipe = crtc->pipe;
f0f59a00 2013 i915_reg_t reg;
b24e7179
JB
2014 u32 val;
2015
9e2ee2dd
VS
2016 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2017
b24e7179
JB
2018 /*
2019 * Make sure planes won't keep trying to pump pixels to us,
2020 * or we might hang the display.
2021 */
2022 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2023 assert_cursor_disabled(dev_priv, pipe);
19332d7a 2024 assert_sprites_disabled(dev_priv, pipe);
b24e7179 2025
702e7a56 2026 reg = PIPECONF(cpu_transcoder);
b24e7179 2027 val = I915_READ(reg);
00d70b15
CW
2028 if ((val & PIPECONF_ENABLE) == 0)
2029 return;
2030
67adc644
VS
2031 /*
2032 * Double wide has implications for planes
2033 * so best keep it disabled when not needed.
2034 */
6e3c9717 2035 if (crtc->config->double_wide)
67adc644
VS
2036 val &= ~PIPECONF_DOUBLE_WIDE;
2037
2038 /* Don't disable pipe or pipe PLLs if needed */
b6b5d049
VS
2039 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2040 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
67adc644
VS
2041 val &= ~PIPECONF_ENABLE;
2042
2043 I915_WRITE(reg, val);
2044 if ((val & PIPECONF_ENABLE) == 0)
2045 intel_wait_for_pipe_off(crtc);
b24e7179
JB
2046}
2047
832be82f
VS
2048static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
2049{
2050 return IS_GEN2(dev_priv) ? 2048 : 4096;
2051}
2052
27ba3910
VS
2053static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
2054 uint64_t fb_modifier, unsigned int cpp)
7b49f948
VS
2055{
2056 switch (fb_modifier) {
2057 case DRM_FORMAT_MOD_NONE:
2058 return cpp;
2059 case I915_FORMAT_MOD_X_TILED:
2060 if (IS_GEN2(dev_priv))
2061 return 128;
2062 else
2063 return 512;
2064 case I915_FORMAT_MOD_Y_TILED:
2065 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2066 return 128;
2067 else
2068 return 512;
2069 case I915_FORMAT_MOD_Yf_TILED:
2070 switch (cpp) {
2071 case 1:
2072 return 64;
2073 case 2:
2074 case 4:
2075 return 128;
2076 case 8:
2077 case 16:
2078 return 256;
2079 default:
2080 MISSING_CASE(cpp);
2081 return cpp;
2082 }
2083 break;
2084 default:
2085 MISSING_CASE(fb_modifier);
2086 return cpp;
2087 }
2088}
2089
832be82f
VS
2090unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
2091 uint64_t fb_modifier, unsigned int cpp)
a57ce0b2 2092{
832be82f
VS
2093 if (fb_modifier == DRM_FORMAT_MOD_NONE)
2094 return 1;
2095 else
2096 return intel_tile_size(dev_priv) /
27ba3910 2097 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
6761dd31
TU
2098}
2099
8d0deca8
VS
2100/* Return the tile dimensions in pixel units */
2101static void intel_tile_dims(const struct drm_i915_private *dev_priv,
2102 unsigned int *tile_width,
2103 unsigned int *tile_height,
2104 uint64_t fb_modifier,
2105 unsigned int cpp)
2106{
2107 unsigned int tile_width_bytes =
2108 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
2109
2110 *tile_width = tile_width_bytes / cpp;
2111 *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
2112}
2113
6761dd31
TU
2114unsigned int
2115intel_fb_align_height(struct drm_device *dev, unsigned int height,
832be82f 2116 uint32_t pixel_format, uint64_t fb_modifier)
6761dd31 2117{
832be82f
VS
2118 unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
2119 unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
2120
2121 return ALIGN(height, tile_height);
a57ce0b2
JB
2122}
2123
1663b9d6
VS
2124unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2125{
2126 unsigned int size = 0;
2127 int i;
2128
2129 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2130 size += rot_info->plane[i].width * rot_info->plane[i].height;
2131
2132 return size;
2133}
2134
75c82a53 2135static void
3465c580
VS
2136intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2137 const struct drm_framebuffer *fb,
2138 unsigned int rotation)
f64b98cd 2139{
7b92c047 2140 view->type = I915_GGTT_VIEW_NORMAL;
bd2ef25d 2141 if (drm_rotation_90_or_270(rotation)) {
7b92c047 2142 view->type = I915_GGTT_VIEW_ROTATED;
8bab1193 2143 view->rotated = to_intel_framebuffer(fb)->rot_info;
2d7a215f
VS
2144 }
2145}
50470bb0 2146
603525d7 2147static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
4e9a86b6
VS
2148{
2149 if (INTEL_INFO(dev_priv)->gen >= 9)
2150 return 256 * 1024;
c0f86832 2151 else if (IS_I965G(dev_priv) || IS_I965GM(dev_priv) ||
666a4537 2152 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
4e9a86b6
VS
2153 return 128 * 1024;
2154 else if (INTEL_INFO(dev_priv)->gen >= 4)
2155 return 4 * 1024;
2156 else
44c5905e 2157 return 0;
4e9a86b6
VS
2158}
2159
603525d7
VS
2160static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
2161 uint64_t fb_modifier)
2162{
2163 switch (fb_modifier) {
2164 case DRM_FORMAT_MOD_NONE:
2165 return intel_linear_alignment(dev_priv);
2166 case I915_FORMAT_MOD_X_TILED:
2167 if (INTEL_INFO(dev_priv)->gen >= 9)
2168 return 256 * 1024;
2169 return 0;
2170 case I915_FORMAT_MOD_Y_TILED:
2171 case I915_FORMAT_MOD_Yf_TILED:
2172 return 1 * 1024 * 1024;
2173 default:
2174 MISSING_CASE(fb_modifier);
2175 return 0;
2176 }
2177}
2178
058d88c4
CW
2179struct i915_vma *
2180intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
6b95a207 2181{
850c4cdc 2182 struct drm_device *dev = fb->dev;
fac5e23e 2183 struct drm_i915_private *dev_priv = to_i915(dev);
850c4cdc 2184 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2185 struct i915_ggtt_view view;
058d88c4 2186 struct i915_vma *vma;
6b95a207 2187 u32 alignment;
6b95a207 2188
ebcdd39e
MR
2189 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2190
bae781b2 2191 alignment = intel_surf_alignment(dev_priv, fb->modifier);
6b95a207 2192
3465c580 2193 intel_fill_fb_ggtt_view(&view, fb, rotation);
f64b98cd 2194
693db184
CW
2195 /* Note that the w/a also requires 64 PTE of padding following the
2196 * bo. We currently fill all unused PTE with the shadow page and so
2197 * we should always have valid PTE following the scanout preventing
2198 * the VT-d warning.
2199 */
48f112fe 2200 if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
693db184
CW
2201 alignment = 256 * 1024;
2202
d6dd6843
PZ
2203 /*
2204 * Global gtt pte registers are special registers which actually forward
2205 * writes to a chunk of system memory. Which means that there is no risk
2206 * that the register values disappear as soon as we call
2207 * intel_runtime_pm_put(), so it is correct to wrap only the
2208 * pin/unpin/fence and not more.
2209 */
2210 intel_runtime_pm_get(dev_priv);
2211
058d88c4 2212 vma = i915_gem_object_pin_to_display_plane(obj, alignment, &view);
49ef5294
CW
2213 if (IS_ERR(vma))
2214 goto err;
6b95a207 2215
05a20d09 2216 if (i915_vma_is_map_and_fenceable(vma)) {
49ef5294
CW
2217 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2218 * fence, whereas 965+ only requires a fence if using
2219 * framebuffer compression. For simplicity, we always, when
2220 * possible, install a fence as the cost is not that onerous.
2221 *
2222 * If we fail to fence the tiled scanout, then either the
2223 * modeset will reject the change (which is highly unlikely as
2224 * the affected systems, all but one, do not have unmappable
2225 * space) or we will not be able to enable full powersaving
2226 * techniques (also likely not to apply due to various limits
2227 * FBC and the like impose on the size of the buffer, which
2228 * presumably we violated anyway with this unmappable buffer).
2229 * Anyway, it is presumably better to stumble onwards with
2230 * something and try to run the system in a "less than optimal"
2231 * mode that matches the user configuration.
2232 */
2233 if (i915_vma_get_fence(vma) == 0)
2234 i915_vma_pin_fence(vma);
9807216f 2235 }
6b95a207 2236
be1e3415 2237 i915_vma_get(vma);
49ef5294 2238err:
d6dd6843 2239 intel_runtime_pm_put(dev_priv);
058d88c4 2240 return vma;
6b95a207
KH
2241}
2242
be1e3415 2243void intel_unpin_fb_vma(struct i915_vma *vma)
1690e1eb 2244{
be1e3415 2245 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
f64b98cd 2246
49ef5294 2247 i915_vma_unpin_fence(vma);
058d88c4 2248 i915_gem_object_unpin_from_display_plane(vma);
be1e3415 2249 i915_vma_put(vma);
1690e1eb
CW
2250}
2251
ef78ec94
VS
2252static int intel_fb_pitch(const struct drm_framebuffer *fb, int plane,
2253 unsigned int rotation)
2254{
bd2ef25d 2255 if (drm_rotation_90_or_270(rotation))
ef78ec94
VS
2256 return to_intel_framebuffer(fb)->rotated[plane].pitch;
2257 else
2258 return fb->pitches[plane];
2259}
2260
6687c906
VS
2261/*
2262 * Convert the x/y offsets into a linear offset.
2263 * Only valid with 0/180 degree rotation, which is fine since linear
2264 * offset is only used with linear buffers on pre-hsw and tiled buffers
2265 * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
2266 */
2267u32 intel_fb_xy_to_linear(int x, int y,
2949056c
VS
2268 const struct intel_plane_state *state,
2269 int plane)
6687c906 2270{
2949056c 2271 const struct drm_framebuffer *fb = state->base.fb;
353c8598 2272 unsigned int cpp = fb->format->cpp[plane];
6687c906
VS
2273 unsigned int pitch = fb->pitches[plane];
2274
2275 return y * pitch + x * cpp;
2276}
2277
2278/*
2279 * Add the x/y offsets derived from fb->offsets[] to the user
2280 * specified plane src x/y offsets. The resulting x/y offsets
2281 * specify the start of scanout from the beginning of the gtt mapping.
2282 */
2283void intel_add_fb_offsets(int *x, int *y,
2949056c
VS
2284 const struct intel_plane_state *state,
2285 int plane)
6687c906
VS
2286
2287{
2949056c
VS
2288 const struct intel_framebuffer *intel_fb = to_intel_framebuffer(state->base.fb);
2289 unsigned int rotation = state->base.rotation;
6687c906 2290
bd2ef25d 2291 if (drm_rotation_90_or_270(rotation)) {
6687c906
VS
2292 *x += intel_fb->rotated[plane].x;
2293 *y += intel_fb->rotated[plane].y;
2294 } else {
2295 *x += intel_fb->normal[plane].x;
2296 *y += intel_fb->normal[plane].y;
2297 }
2298}
2299
29cf9491 2300/*
29cf9491
VS
2301 * Input tile dimensions and pitch must already be
2302 * rotated to match x and y, and in pixel units.
2303 */
66a2d927
VS
2304static u32 _intel_adjust_tile_offset(int *x, int *y,
2305 unsigned int tile_width,
2306 unsigned int tile_height,
2307 unsigned int tile_size,
2308 unsigned int pitch_tiles,
2309 u32 old_offset,
2310 u32 new_offset)
29cf9491 2311{
b9b24038 2312 unsigned int pitch_pixels = pitch_tiles * tile_width;
29cf9491
VS
2313 unsigned int tiles;
2314
2315 WARN_ON(old_offset & (tile_size - 1));
2316 WARN_ON(new_offset & (tile_size - 1));
2317 WARN_ON(new_offset > old_offset);
2318
2319 tiles = (old_offset - new_offset) / tile_size;
2320
2321 *y += tiles / pitch_tiles * tile_height;
2322 *x += tiles % pitch_tiles * tile_width;
2323
b9b24038
VS
2324 /* minimize x in case it got needlessly big */
2325 *y += *x / pitch_pixels * tile_height;
2326 *x %= pitch_pixels;
2327
29cf9491
VS
2328 return new_offset;
2329}
2330
66a2d927
VS
2331/*
2332 * Adjust the tile offset by moving the difference into
2333 * the x/y offsets.
2334 */
2335static u32 intel_adjust_tile_offset(int *x, int *y,
2336 const struct intel_plane_state *state, int plane,
2337 u32 old_offset, u32 new_offset)
2338{
2339 const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
2340 const struct drm_framebuffer *fb = state->base.fb;
353c8598 2341 unsigned int cpp = fb->format->cpp[plane];
66a2d927
VS
2342 unsigned int rotation = state->base.rotation;
2343 unsigned int pitch = intel_fb_pitch(fb, plane, rotation);
2344
2345 WARN_ON(new_offset > old_offset);
2346
bae781b2 2347 if (fb->modifier != DRM_FORMAT_MOD_NONE) {
66a2d927
VS
2348 unsigned int tile_size, tile_width, tile_height;
2349 unsigned int pitch_tiles;
2350
2351 tile_size = intel_tile_size(dev_priv);
2352 intel_tile_dims(dev_priv, &tile_width, &tile_height,
bae781b2 2353 fb->modifier, cpp);
66a2d927 2354
bd2ef25d 2355 if (drm_rotation_90_or_270(rotation)) {
66a2d927
VS
2356 pitch_tiles = pitch / tile_height;
2357 swap(tile_width, tile_height);
2358 } else {
2359 pitch_tiles = pitch / (tile_width * cpp);
2360 }
2361
2362 _intel_adjust_tile_offset(x, y, tile_width, tile_height,
2363 tile_size, pitch_tiles,
2364 old_offset, new_offset);
2365 } else {
2366 old_offset += *y * pitch + *x * cpp;
2367
2368 *y = (old_offset - new_offset) / pitch;
2369 *x = ((old_offset - new_offset) - *y * pitch) / cpp;
2370 }
2371
2372 return new_offset;
2373}
2374
8d0deca8
VS
2375/*
2376 * Computes the linear offset to the base tile and adjusts
2377 * x, y. bytes per pixel is assumed to be a power-of-two.
2378 *
2379 * In the 90/270 rotated case, x and y are assumed
2380 * to be already rotated to match the rotated GTT view, and
2381 * pitch is the tile_height aligned framebuffer height.
6687c906
VS
2382 *
2383 * This function is used when computing the derived information
2384 * under intel_framebuffer, so using any of that information
2385 * here is not allowed. Anything under drm_framebuffer can be
2386 * used. This is why the user has to pass in the pitch since it
2387 * is specified in the rotated orientation.
8d0deca8 2388 */
6687c906
VS
2389static u32 _intel_compute_tile_offset(const struct drm_i915_private *dev_priv,
2390 int *x, int *y,
2391 const struct drm_framebuffer *fb, int plane,
2392 unsigned int pitch,
2393 unsigned int rotation,
2394 u32 alignment)
c2c75131 2395{
bae781b2 2396 uint64_t fb_modifier = fb->modifier;
353c8598 2397 unsigned int cpp = fb->format->cpp[plane];
6687c906 2398 u32 offset, offset_aligned;
29cf9491 2399
29cf9491
VS
2400 if (alignment)
2401 alignment--;
2402
b5c65338 2403 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
8d0deca8
VS
2404 unsigned int tile_size, tile_width, tile_height;
2405 unsigned int tile_rows, tiles, pitch_tiles;
c2c75131 2406
d843310d 2407 tile_size = intel_tile_size(dev_priv);
8d0deca8
VS
2408 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2409 fb_modifier, cpp);
2410
bd2ef25d 2411 if (drm_rotation_90_or_270(rotation)) {
8d0deca8
VS
2412 pitch_tiles = pitch / tile_height;
2413 swap(tile_width, tile_height);
2414 } else {
2415 pitch_tiles = pitch / (tile_width * cpp);
2416 }
d843310d
VS
2417
2418 tile_rows = *y / tile_height;
2419 *y %= tile_height;
c2c75131 2420
8d0deca8
VS
2421 tiles = *x / tile_width;
2422 *x %= tile_width;
bc752862 2423
29cf9491
VS
2424 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2425 offset_aligned = offset & ~alignment;
bc752862 2426
66a2d927
VS
2427 _intel_adjust_tile_offset(x, y, tile_width, tile_height,
2428 tile_size, pitch_tiles,
2429 offset, offset_aligned);
29cf9491 2430 } else {
bc752862 2431 offset = *y * pitch + *x * cpp;
29cf9491
VS
2432 offset_aligned = offset & ~alignment;
2433
4e9a86b6
VS
2434 *y = (offset & alignment) / pitch;
2435 *x = ((offset & alignment) - *y * pitch) / cpp;
bc752862 2436 }
29cf9491
VS
2437
2438 return offset_aligned;
c2c75131
DV
2439}
2440
6687c906 2441u32 intel_compute_tile_offset(int *x, int *y,
2949056c
VS
2442 const struct intel_plane_state *state,
2443 int plane)
6687c906 2444{
2949056c
VS
2445 const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
2446 const struct drm_framebuffer *fb = state->base.fb;
2447 unsigned int rotation = state->base.rotation;
ef78ec94 2448 int pitch = intel_fb_pitch(fb, plane, rotation);
8d970654
VS
2449 u32 alignment;
2450
2451 /* AUX_DIST needs only 4K alignment */
438b74a5 2452 if (fb->format->format == DRM_FORMAT_NV12 && plane == 1)
8d970654
VS
2453 alignment = 4096;
2454 else
bae781b2 2455 alignment = intel_surf_alignment(dev_priv, fb->modifier);
6687c906
VS
2456
2457 return _intel_compute_tile_offset(dev_priv, x, y, fb, plane, pitch,
2458 rotation, alignment);
2459}
2460
2461/* Convert the fb->offset[] linear offset into x/y offsets */
2462static void intel_fb_offset_to_xy(int *x, int *y,
2463 const struct drm_framebuffer *fb, int plane)
2464{
353c8598 2465 unsigned int cpp = fb->format->cpp[plane];
6687c906
VS
2466 unsigned int pitch = fb->pitches[plane];
2467 u32 linear_offset = fb->offsets[plane];
2468
2469 *y = linear_offset / pitch;
2470 *x = linear_offset % pitch / cpp;
2471}
2472
72618ebf
VS
2473static unsigned int intel_fb_modifier_to_tiling(uint64_t fb_modifier)
2474{
2475 switch (fb_modifier) {
2476 case I915_FORMAT_MOD_X_TILED:
2477 return I915_TILING_X;
2478 case I915_FORMAT_MOD_Y_TILED:
2479 return I915_TILING_Y;
2480 default:
2481 return I915_TILING_NONE;
2482 }
2483}
2484
6687c906
VS
2485static int
2486intel_fill_fb_info(struct drm_i915_private *dev_priv,
2487 struct drm_framebuffer *fb)
2488{
2489 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
2490 struct intel_rotation_info *rot_info = &intel_fb->rot_info;
2491 u32 gtt_offset_rotated = 0;
2492 unsigned int max_size = 0;
bcb0b461 2493 int i, num_planes = fb->format->num_planes;
6687c906
VS
2494 unsigned int tile_size = intel_tile_size(dev_priv);
2495
2496 for (i = 0; i < num_planes; i++) {
2497 unsigned int width, height;
2498 unsigned int cpp, size;
2499 u32 offset;
2500 int x, y;
2501
353c8598 2502 cpp = fb->format->cpp[i];
145fcb11
VS
2503 width = drm_framebuffer_plane_width(fb->width, fb, i);
2504 height = drm_framebuffer_plane_height(fb->height, fb, i);
6687c906
VS
2505
2506 intel_fb_offset_to_xy(&x, &y, fb, i);
2507
60d5f2a4
VS
2508 /*
2509 * The fence (if used) is aligned to the start of the object
2510 * so having the framebuffer wrap around across the edge of the
2511 * fenced region doesn't really work. We have no API to configure
2512 * the fence start offset within the object (nor could we probably
2513 * on gen2/3). So it's just easier if we just require that the
2514 * fb layout agrees with the fence layout. We already check that the
2515 * fb stride matches the fence stride elsewhere.
2516 */
2517 if (i915_gem_object_is_tiled(intel_fb->obj) &&
2518 (x + width) * cpp > fb->pitches[i]) {
2519 DRM_DEBUG("bad fb plane %d offset: 0x%x\n",
2520 i, fb->offsets[i]);
2521 return -EINVAL;
2522 }
2523
6687c906
VS
2524 /*
2525 * First pixel of the framebuffer from
2526 * the start of the normal gtt mapping.
2527 */
2528 intel_fb->normal[i].x = x;
2529 intel_fb->normal[i].y = y;
2530
2531 offset = _intel_compute_tile_offset(dev_priv, &x, &y,
2532 fb, 0, fb->pitches[i],
cc926387 2533 DRM_ROTATE_0, tile_size);
6687c906
VS
2534 offset /= tile_size;
2535
bae781b2 2536 if (fb->modifier != DRM_FORMAT_MOD_NONE) {
6687c906
VS
2537 unsigned int tile_width, tile_height;
2538 unsigned int pitch_tiles;
2539 struct drm_rect r;
2540
2541 intel_tile_dims(dev_priv, &tile_width, &tile_height,
bae781b2 2542 fb->modifier, cpp);
6687c906
VS
2543
2544 rot_info->plane[i].offset = offset;
2545 rot_info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i], tile_width * cpp);
2546 rot_info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
2547 rot_info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
2548
2549 intel_fb->rotated[i].pitch =
2550 rot_info->plane[i].height * tile_height;
2551
2552 /* how many tiles does this plane need */
2553 size = rot_info->plane[i].stride * rot_info->plane[i].height;
2554 /*
2555 * If the plane isn't horizontally tile aligned,
2556 * we need one more tile.
2557 */
2558 if (x != 0)
2559 size++;
2560
2561 /* rotate the x/y offsets to match the GTT view */
2562 r.x1 = x;
2563 r.y1 = y;
2564 r.x2 = x + width;
2565 r.y2 = y + height;
2566 drm_rect_rotate(&r,
2567 rot_info->plane[i].width * tile_width,
2568 rot_info->plane[i].height * tile_height,
cc926387 2569 DRM_ROTATE_270);
6687c906
VS
2570 x = r.x1;
2571 y = r.y1;
2572
2573 /* rotate the tile dimensions to match the GTT view */
2574 pitch_tiles = intel_fb->rotated[i].pitch / tile_height;
2575 swap(tile_width, tile_height);
2576
2577 /*
2578 * We only keep the x/y offsets, so push all of the
2579 * gtt offset into the x/y offsets.
2580 */
46a1bd28
ACO
2581 _intel_adjust_tile_offset(&x, &y,
2582 tile_width, tile_height,
2583 tile_size, pitch_tiles,
66a2d927 2584 gtt_offset_rotated * tile_size, 0);
6687c906
VS
2585
2586 gtt_offset_rotated += rot_info->plane[i].width * rot_info->plane[i].height;
2587
2588 /*
2589 * First pixel of the framebuffer from
2590 * the start of the rotated gtt mapping.
2591 */
2592 intel_fb->rotated[i].x = x;
2593 intel_fb->rotated[i].y = y;
2594 } else {
2595 size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
2596 x * cpp, tile_size);
2597 }
2598
2599 /* how many tiles in total needed in the bo */
2600 max_size = max(max_size, offset + size);
2601 }
2602
2603 if (max_size * tile_size > to_intel_framebuffer(fb)->obj->base.size) {
2604 DRM_DEBUG("fb too big for bo (need %u bytes, have %zu bytes)\n",
2605 max_size * tile_size, to_intel_framebuffer(fb)->obj->base.size);
2606 return -EINVAL;
2607 }
2608
2609 return 0;
2610}
2611
b35d63fa 2612static int i9xx_format_to_fourcc(int format)
46f297fb
JB
2613{
2614 switch (format) {
2615 case DISPPLANE_8BPP:
2616 return DRM_FORMAT_C8;
2617 case DISPPLANE_BGRX555:
2618 return DRM_FORMAT_XRGB1555;
2619 case DISPPLANE_BGRX565:
2620 return DRM_FORMAT_RGB565;
2621 default:
2622 case DISPPLANE_BGRX888:
2623 return DRM_FORMAT_XRGB8888;
2624 case DISPPLANE_RGBX888:
2625 return DRM_FORMAT_XBGR8888;
2626 case DISPPLANE_BGRX101010:
2627 return DRM_FORMAT_XRGB2101010;
2628 case DISPPLANE_RGBX101010:
2629 return DRM_FORMAT_XBGR2101010;
2630 }
2631}
2632
bc8d7dff
DL
2633static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2634{
2635 switch (format) {
2636 case PLANE_CTL_FORMAT_RGB_565:
2637 return DRM_FORMAT_RGB565;
2638 default:
2639 case PLANE_CTL_FORMAT_XRGB_8888:
2640 if (rgb_order) {
2641 if (alpha)
2642 return DRM_FORMAT_ABGR8888;
2643 else
2644 return DRM_FORMAT_XBGR8888;
2645 } else {
2646 if (alpha)
2647 return DRM_FORMAT_ARGB8888;
2648 else
2649 return DRM_FORMAT_XRGB8888;
2650 }
2651 case PLANE_CTL_FORMAT_XRGB_2101010:
2652 if (rgb_order)
2653 return DRM_FORMAT_XBGR2101010;
2654 else
2655 return DRM_FORMAT_XRGB2101010;
2656 }
2657}
2658
5724dbd1 2659static bool
f6936e29
DV
2660intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2661 struct intel_initial_plane_config *plane_config)
46f297fb
JB
2662{
2663 struct drm_device *dev = crtc->base.dev;
3badb49f 2664 struct drm_i915_private *dev_priv = to_i915(dev);
72e96d64 2665 struct i915_ggtt *ggtt = &dev_priv->ggtt;
46f297fb
JB
2666 struct drm_i915_gem_object *obj = NULL;
2667 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2d14030b 2668 struct drm_framebuffer *fb = &plane_config->fb->base;
f37b5c2b
DV
2669 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2670 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2671 PAGE_SIZE);
2672
2673 size_aligned -= base_aligned;
46f297fb 2674
ff2652ea
CW
2675 if (plane_config->size == 0)
2676 return false;
2677
3badb49f
PZ
2678 /* If the FB is too big, just don't use it since fbdev is not very
2679 * important and we should probably use that space with FBC or other
2680 * features. */
72e96d64 2681 if (size_aligned * 2 > ggtt->stolen_usable_size)
3badb49f
PZ
2682 return false;
2683
12c83d99
TU
2684 mutex_lock(&dev->struct_mutex);
2685
187685cb 2686 obj = i915_gem_object_create_stolen_for_preallocated(dev_priv,
f37b5c2b
DV
2687 base_aligned,
2688 base_aligned,
2689 size_aligned);
12c83d99
TU
2690 if (!obj) {
2691 mutex_unlock(&dev->struct_mutex);
484b41dd 2692 return false;
12c83d99 2693 }
46f297fb 2694
3e510a8e
CW
2695 if (plane_config->tiling == I915_TILING_X)
2696 obj->tiling_and_stride = fb->pitches[0] | I915_TILING_X;
46f297fb 2697
438b74a5 2698 mode_cmd.pixel_format = fb->format->format;
6bf129df
DL
2699 mode_cmd.width = fb->width;
2700 mode_cmd.height = fb->height;
2701 mode_cmd.pitches[0] = fb->pitches[0];
bae781b2 2702 mode_cmd.modifier[0] = fb->modifier;
18c5247e 2703 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
46f297fb 2704
6bf129df 2705 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
484b41dd 2706 &mode_cmd, obj)) {
46f297fb
JB
2707 DRM_DEBUG_KMS("intel fb init failed\n");
2708 goto out_unref_obj;
2709 }
12c83d99 2710
46f297fb 2711 mutex_unlock(&dev->struct_mutex);
484b41dd 2712
f6936e29 2713 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
484b41dd 2714 return true;
46f297fb
JB
2715
2716out_unref_obj:
f8c417cd 2717 i915_gem_object_put(obj);
46f297fb 2718 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2719 return false;
2720}
2721
5a21b665
DV
2722/* Update plane->state->fb to match plane->fb after driver-internal updates */
2723static void
2724update_state_fb(struct drm_plane *plane)
2725{
2726 if (plane->fb == plane->state->fb)
2727 return;
2728
2729 if (plane->state->fb)
2730 drm_framebuffer_unreference(plane->state->fb);
2731 plane->state->fb = plane->fb;
2732 if (plane->state->fb)
2733 drm_framebuffer_reference(plane->state->fb);
2734}
2735
5724dbd1 2736static void
f6936e29
DV
2737intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2738 struct intel_initial_plane_config *plane_config)
484b41dd
JB
2739{
2740 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 2741 struct drm_i915_private *dev_priv = to_i915(dev);
484b41dd 2742 struct drm_crtc *c;
2ff8fde1 2743 struct drm_i915_gem_object *obj;
88595ac9 2744 struct drm_plane *primary = intel_crtc->base.primary;
be5651f2 2745 struct drm_plane_state *plane_state = primary->state;
200757f5
MR
2746 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2747 struct intel_plane *intel_plane = to_intel_plane(primary);
0a8d8a86
MR
2748 struct intel_plane_state *intel_state =
2749 to_intel_plane_state(plane_state);
88595ac9 2750 struct drm_framebuffer *fb;
484b41dd 2751
2d14030b 2752 if (!plane_config->fb)
484b41dd
JB
2753 return;
2754
f6936e29 2755 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
88595ac9
DV
2756 fb = &plane_config->fb->base;
2757 goto valid_fb;
f55548b5 2758 }
484b41dd 2759
2d14030b 2760 kfree(plane_config->fb);
484b41dd
JB
2761
2762 /*
2763 * Failed to alloc the obj, check to see if we should share
2764 * an fb with another CRTC instead
2765 */
70e1e0ec 2766 for_each_crtc(dev, c) {
be1e3415 2767 struct intel_plane_state *state;
484b41dd
JB
2768
2769 if (c == &intel_crtc->base)
2770 continue;
2771
be1e3415 2772 if (!to_intel_crtc(c)->active)
2ff8fde1
MR
2773 continue;
2774
be1e3415
CW
2775 state = to_intel_plane_state(c->primary->state);
2776 if (!state->vma)
484b41dd
JB
2777 continue;
2778
be1e3415
CW
2779 if (intel_plane_ggtt_offset(state) == plane_config->base) {
2780 fb = c->primary->fb;
88595ac9
DV
2781 drm_framebuffer_reference(fb);
2782 goto valid_fb;
484b41dd
JB
2783 }
2784 }
88595ac9 2785
200757f5
MR
2786 /*
2787 * We've failed to reconstruct the BIOS FB. Current display state
2788 * indicates that the primary plane is visible, but has a NULL FB,
2789 * which will lead to problems later if we don't fix it up. The
2790 * simplest solution is to just disable the primary plane now and
2791 * pretend the BIOS never had it enabled.
2792 */
1d4258db 2793 plane_state->visible = false;
200757f5 2794 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
2622a081 2795 intel_pre_disable_primary_noatomic(&intel_crtc->base);
200757f5
MR
2796 intel_plane->disable_plane(primary, &intel_crtc->base);
2797
88595ac9
DV
2798 return;
2799
2800valid_fb:
be1e3415
CW
2801 mutex_lock(&dev->struct_mutex);
2802 intel_state->vma =
2803 intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
2804 mutex_unlock(&dev->struct_mutex);
2805 if (IS_ERR(intel_state->vma)) {
2806 DRM_ERROR("failed to pin boot fb on pipe %d: %li\n",
2807 intel_crtc->pipe, PTR_ERR(intel_state->vma));
2808
2809 intel_state->vma = NULL;
2810 drm_framebuffer_unreference(fb);
2811 return;
2812 }
2813
f44e2659
VS
2814 plane_state->src_x = 0;
2815 plane_state->src_y = 0;
be5651f2
ML
2816 plane_state->src_w = fb->width << 16;
2817 plane_state->src_h = fb->height << 16;
2818
f44e2659
VS
2819 plane_state->crtc_x = 0;
2820 plane_state->crtc_y = 0;
be5651f2
ML
2821 plane_state->crtc_w = fb->width;
2822 plane_state->crtc_h = fb->height;
2823
1638d30c
RC
2824 intel_state->base.src = drm_plane_state_src(plane_state);
2825 intel_state->base.dst = drm_plane_state_dest(plane_state);
0a8d8a86 2826
88595ac9 2827 obj = intel_fb_obj(fb);
3e510a8e 2828 if (i915_gem_object_is_tiled(obj))
88595ac9
DV
2829 dev_priv->preserve_bios_swizzle = true;
2830
be5651f2
ML
2831 drm_framebuffer_reference(fb);
2832 primary->fb = primary->state->fb = fb;
36750f28 2833 primary->crtc = primary->state->crtc = &intel_crtc->base;
36750f28 2834 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
faf5bf0a
CW
2835 atomic_or(to_intel_plane(primary)->frontbuffer_bit,
2836 &obj->frontbuffer_bits);
46f297fb
JB
2837}
2838
b63a16f6
VS
2839static int skl_max_plane_width(const struct drm_framebuffer *fb, int plane,
2840 unsigned int rotation)
2841{
353c8598 2842 int cpp = fb->format->cpp[plane];
b63a16f6 2843
bae781b2 2844 switch (fb->modifier) {
b63a16f6
VS
2845 case DRM_FORMAT_MOD_NONE:
2846 case I915_FORMAT_MOD_X_TILED:
2847 switch (cpp) {
2848 case 8:
2849 return 4096;
2850 case 4:
2851 case 2:
2852 case 1:
2853 return 8192;
2854 default:
2855 MISSING_CASE(cpp);
2856 break;
2857 }
2858 break;
2859 case I915_FORMAT_MOD_Y_TILED:
2860 case I915_FORMAT_MOD_Yf_TILED:
2861 switch (cpp) {
2862 case 8:
2863 return 2048;
2864 case 4:
2865 return 4096;
2866 case 2:
2867 case 1:
2868 return 8192;
2869 default:
2870 MISSING_CASE(cpp);
2871 break;
2872 }
2873 break;
2874 default:
bae781b2 2875 MISSING_CASE(fb->modifier);
b63a16f6
VS
2876 }
2877
2878 return 2048;
2879}
2880
2881static int skl_check_main_surface(struct intel_plane_state *plane_state)
2882{
2883 const struct drm_i915_private *dev_priv = to_i915(plane_state->base.plane->dev);
2884 const struct drm_framebuffer *fb = plane_state->base.fb;
2885 unsigned int rotation = plane_state->base.rotation;
cc926387
DV
2886 int x = plane_state->base.src.x1 >> 16;
2887 int y = plane_state->base.src.y1 >> 16;
2888 int w = drm_rect_width(&plane_state->base.src) >> 16;
2889 int h = drm_rect_height(&plane_state->base.src) >> 16;
b63a16f6
VS
2890 int max_width = skl_max_plane_width(fb, 0, rotation);
2891 int max_height = 4096;
8d970654 2892 u32 alignment, offset, aux_offset = plane_state->aux.offset;
b63a16f6
VS
2893
2894 if (w > max_width || h > max_height) {
2895 DRM_DEBUG_KMS("requested Y/RGB source size %dx%d too big (limit %dx%d)\n",
2896 w, h, max_width, max_height);
2897 return -EINVAL;
2898 }
2899
2900 intel_add_fb_offsets(&x, &y, plane_state, 0);
2901 offset = intel_compute_tile_offset(&x, &y, plane_state, 0);
2902
bae781b2 2903 alignment = intel_surf_alignment(dev_priv, fb->modifier);
b63a16f6 2904
8d970654
VS
2905 /*
2906 * AUX surface offset is specified as the distance from the
2907 * main surface offset, and it must be non-negative. Make
2908 * sure that is what we will get.
2909 */
2910 if (offset > aux_offset)
2911 offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
2912 offset, aux_offset & ~(alignment - 1));
2913
b63a16f6
VS
2914 /*
2915 * When using an X-tiled surface, the plane blows up
2916 * if the x offset + width exceed the stride.
2917 *
2918 * TODO: linear and Y-tiled seem fine, Yf untested,
2919 */
bae781b2 2920 if (fb->modifier == I915_FORMAT_MOD_X_TILED) {
353c8598 2921 int cpp = fb->format->cpp[0];
b63a16f6
VS
2922
2923 while ((x + w) * cpp > fb->pitches[0]) {
2924 if (offset == 0) {
2925 DRM_DEBUG_KMS("Unable to find suitable display surface offset\n");
2926 return -EINVAL;
2927 }
2928
2929 offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
2930 offset, offset - alignment);
2931 }
2932 }
2933
2934 plane_state->main.offset = offset;
2935 plane_state->main.x = x;
2936 plane_state->main.y = y;
2937
2938 return 0;
2939}
2940
8d970654
VS
2941static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
2942{
2943 const struct drm_framebuffer *fb = plane_state->base.fb;
2944 unsigned int rotation = plane_state->base.rotation;
2945 int max_width = skl_max_plane_width(fb, 1, rotation);
2946 int max_height = 4096;
cc926387
DV
2947 int x = plane_state->base.src.x1 >> 17;
2948 int y = plane_state->base.src.y1 >> 17;
2949 int w = drm_rect_width(&plane_state->base.src) >> 17;
2950 int h = drm_rect_height(&plane_state->base.src) >> 17;
8d970654
VS
2951 u32 offset;
2952
2953 intel_add_fb_offsets(&x, &y, plane_state, 1);
2954 offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
2955
2956 /* FIXME not quite sure how/if these apply to the chroma plane */
2957 if (w > max_width || h > max_height) {
2958 DRM_DEBUG_KMS("CbCr source size %dx%d too big (limit %dx%d)\n",
2959 w, h, max_width, max_height);
2960 return -EINVAL;
2961 }
2962
2963 plane_state->aux.offset = offset;
2964 plane_state->aux.x = x;
2965 plane_state->aux.y = y;
2966
2967 return 0;
2968}
2969
b63a16f6
VS
2970int skl_check_plane_surface(struct intel_plane_state *plane_state)
2971{
2972 const struct drm_framebuffer *fb = plane_state->base.fb;
2973 unsigned int rotation = plane_state->base.rotation;
2974 int ret;
2975
a5e4c7d0
VS
2976 if (!plane_state->base.visible)
2977 return 0;
2978
b63a16f6 2979 /* Rotate src coordinates to match rotated GTT view */
bd2ef25d 2980 if (drm_rotation_90_or_270(rotation))
cc926387 2981 drm_rect_rotate(&plane_state->base.src,
da064b47
VS
2982 fb->width << 16, fb->height << 16,
2983 DRM_ROTATE_270);
b63a16f6 2984
8d970654
VS
2985 /*
2986 * Handle the AUX surface first since
2987 * the main surface setup depends on it.
2988 */
438b74a5 2989 if (fb->format->format == DRM_FORMAT_NV12) {
8d970654
VS
2990 ret = skl_check_nv12_aux_surface(plane_state);
2991 if (ret)
2992 return ret;
2993 } else {
2994 plane_state->aux.offset = ~0xfff;
2995 plane_state->aux.x = 0;
2996 plane_state->aux.y = 0;
2997 }
2998
b63a16f6
VS
2999 ret = skl_check_main_surface(plane_state);
3000 if (ret)
3001 return ret;
3002
3003 return 0;
3004}
3005
a8d201af
ML
3006static void i9xx_update_primary_plane(struct drm_plane *primary,
3007 const struct intel_crtc_state *crtc_state,
3008 const struct intel_plane_state *plane_state)
81255565 3009{
6315b5d3 3010 struct drm_i915_private *dev_priv = to_i915(primary->dev);
a8d201af
ML
3011 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3012 struct drm_framebuffer *fb = plane_state->base.fb;
81255565 3013 int plane = intel_crtc->plane;
54ea9da8 3014 u32 linear_offset;
81255565 3015 u32 dspcntr;
f0f59a00 3016 i915_reg_t reg = DSPCNTR(plane);
8d0deca8 3017 unsigned int rotation = plane_state->base.rotation;
936e71e3
VS
3018 int x = plane_state->base.src.x1 >> 16;
3019 int y = plane_state->base.src.y1 >> 16;
c9ba6fad 3020
f45651ba
VS
3021 dspcntr = DISPPLANE_GAMMA_ENABLE;
3022
fdd508a6 3023 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba 3024
6315b5d3 3025 if (INTEL_GEN(dev_priv) < 4) {
f45651ba
VS
3026 if (intel_crtc->pipe == PIPE_B)
3027 dspcntr |= DISPPLANE_SEL_PIPE_B;
3028
3029 /* pipesrc and dspsize control the size that is scaled from,
3030 * which should always be the user's requested size.
3031 */
3032 I915_WRITE(DSPSIZE(plane),
a8d201af
ML
3033 ((crtc_state->pipe_src_h - 1) << 16) |
3034 (crtc_state->pipe_src_w - 1));
f45651ba 3035 I915_WRITE(DSPPOS(plane), 0);
920a14b2 3036 } else if (IS_CHERRYVIEW(dev_priv) && plane == PLANE_B) {
c14b0485 3037 I915_WRITE(PRIMSIZE(plane),
a8d201af
ML
3038 ((crtc_state->pipe_src_h - 1) << 16) |
3039 (crtc_state->pipe_src_w - 1));
c14b0485
VS
3040 I915_WRITE(PRIMPOS(plane), 0);
3041 I915_WRITE(PRIMCNSTALPHA(plane), 0);
f45651ba 3042 }
81255565 3043
438b74a5 3044 switch (fb->format->format) {
57779d06 3045 case DRM_FORMAT_C8:
81255565
JB
3046 dspcntr |= DISPPLANE_8BPP;
3047 break;
57779d06 3048 case DRM_FORMAT_XRGB1555:
57779d06 3049 dspcntr |= DISPPLANE_BGRX555;
81255565 3050 break;
57779d06
VS
3051 case DRM_FORMAT_RGB565:
3052 dspcntr |= DISPPLANE_BGRX565;
3053 break;
3054 case DRM_FORMAT_XRGB8888:
57779d06
VS
3055 dspcntr |= DISPPLANE_BGRX888;
3056 break;
3057 case DRM_FORMAT_XBGR8888:
57779d06
VS
3058 dspcntr |= DISPPLANE_RGBX888;
3059 break;
3060 case DRM_FORMAT_XRGB2101010:
57779d06
VS
3061 dspcntr |= DISPPLANE_BGRX101010;
3062 break;
3063 case DRM_FORMAT_XBGR2101010:
57779d06 3064 dspcntr |= DISPPLANE_RGBX101010;
81255565
JB
3065 break;
3066 default:
baba133a 3067 BUG();
81255565 3068 }
57779d06 3069
72618ebf 3070 if (INTEL_GEN(dev_priv) >= 4 &&
bae781b2 3071 fb->modifier == I915_FORMAT_MOD_X_TILED)
f45651ba 3072 dspcntr |= DISPPLANE_TILED;
81255565 3073
df0cd455
VS
3074 if (rotation & DRM_ROTATE_180)
3075 dspcntr |= DISPPLANE_ROTATE_180;
3076
4ea7be2b
VS
3077 if (rotation & DRM_REFLECT_X)
3078 dspcntr |= DISPPLANE_MIRROR;
3079
9beb5fea 3080 if (IS_G4X(dev_priv))
de1aa629
VS
3081 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
3082
2949056c 3083 intel_add_fb_offsets(&x, &y, plane_state, 0);
81255565 3084
6315b5d3 3085 if (INTEL_GEN(dev_priv) >= 4)
c2c75131 3086 intel_crtc->dspaddr_offset =
2949056c 3087 intel_compute_tile_offset(&x, &y, plane_state, 0);
e506a0c6 3088
f22aa143 3089 if (rotation & DRM_ROTATE_180) {
df0cd455
VS
3090 x += crtc_state->pipe_src_w - 1;
3091 y += crtc_state->pipe_src_h - 1;
4ea7be2b
VS
3092 } else if (rotation & DRM_REFLECT_X) {
3093 x += crtc_state->pipe_src_w - 1;
48404c1e
SJ
3094 }
3095
2949056c 3096 linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
6687c906 3097
6315b5d3 3098 if (INTEL_GEN(dev_priv) < 4)
6687c906
VS
3099 intel_crtc->dspaddr_offset = linear_offset;
3100
2db3366b
PZ
3101 intel_crtc->adjusted_x = x;
3102 intel_crtc->adjusted_y = y;
3103
48404c1e
SJ
3104 I915_WRITE(reg, dspcntr);
3105
01f2c773 3106 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
6315b5d3 3107 if (INTEL_GEN(dev_priv) >= 4) {
85ba7b7d 3108 I915_WRITE(DSPSURF(plane),
be1e3415 3109 intel_plane_ggtt_offset(plane_state) +
6687c906 3110 intel_crtc->dspaddr_offset);
5eddb70b 3111 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
e506a0c6 3112 I915_WRITE(DSPLINOFF(plane), linear_offset);
bfb81049
VS
3113 } else {
3114 I915_WRITE(DSPADDR(plane),
be1e3415 3115 intel_plane_ggtt_offset(plane_state) +
bfb81049
VS
3116 intel_crtc->dspaddr_offset);
3117 }
5eddb70b 3118 POSTING_READ(reg);
17638cd6
JB
3119}
3120
a8d201af
ML
3121static void i9xx_disable_primary_plane(struct drm_plane *primary,
3122 struct drm_crtc *crtc)
17638cd6
JB
3123{
3124 struct drm_device *dev = crtc->dev;
fac5e23e 3125 struct drm_i915_private *dev_priv = to_i915(dev);
17638cd6 3126 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
17638cd6 3127 int plane = intel_crtc->plane;
f45651ba 3128
a8d201af
ML
3129 I915_WRITE(DSPCNTR(plane), 0);
3130 if (INTEL_INFO(dev_priv)->gen >= 4)
fdd508a6 3131 I915_WRITE(DSPSURF(plane), 0);
a8d201af
ML
3132 else
3133 I915_WRITE(DSPADDR(plane), 0);
3134 POSTING_READ(DSPCNTR(plane));
3135}
c9ba6fad 3136
a8d201af
ML
3137static void ironlake_update_primary_plane(struct drm_plane *primary,
3138 const struct intel_crtc_state *crtc_state,
3139 const struct intel_plane_state *plane_state)
3140{
3141 struct drm_device *dev = primary->dev;
fac5e23e 3142 struct drm_i915_private *dev_priv = to_i915(dev);
a8d201af
ML
3143 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3144 struct drm_framebuffer *fb = plane_state->base.fb;
a8d201af 3145 int plane = intel_crtc->plane;
54ea9da8 3146 u32 linear_offset;
a8d201af
ML
3147 u32 dspcntr;
3148 i915_reg_t reg = DSPCNTR(plane);
8d0deca8 3149 unsigned int rotation = plane_state->base.rotation;
936e71e3
VS
3150 int x = plane_state->base.src.x1 >> 16;
3151 int y = plane_state->base.src.y1 >> 16;
c9ba6fad 3152
f45651ba 3153 dspcntr = DISPPLANE_GAMMA_ENABLE;
fdd508a6 3154 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba 3155
8652744b 3156 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
f45651ba 3157 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
17638cd6 3158
438b74a5 3159 switch (fb->format->format) {
57779d06 3160 case DRM_FORMAT_C8:
17638cd6
JB
3161 dspcntr |= DISPPLANE_8BPP;
3162 break;
57779d06
VS
3163 case DRM_FORMAT_RGB565:
3164 dspcntr |= DISPPLANE_BGRX565;
17638cd6 3165 break;
57779d06 3166 case DRM_FORMAT_XRGB8888:
57779d06
VS
3167 dspcntr |= DISPPLANE_BGRX888;
3168 break;
3169 case DRM_FORMAT_XBGR8888:
57779d06
VS
3170 dspcntr |= DISPPLANE_RGBX888;
3171 break;
3172 case DRM_FORMAT_XRGB2101010:
57779d06
VS
3173 dspcntr |= DISPPLANE_BGRX101010;
3174 break;
3175 case DRM_FORMAT_XBGR2101010:
57779d06 3176 dspcntr |= DISPPLANE_RGBX101010;
17638cd6
JB
3177 break;
3178 default:
baba133a 3179 BUG();
17638cd6
JB
3180 }
3181
bae781b2 3182 if (fb->modifier == I915_FORMAT_MOD_X_TILED)
17638cd6 3183 dspcntr |= DISPPLANE_TILED;
17638cd6 3184
df0cd455
VS
3185 if (rotation & DRM_ROTATE_180)
3186 dspcntr |= DISPPLANE_ROTATE_180;
3187
8652744b 3188 if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv))
1f5d76db 3189 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
17638cd6 3190
2949056c 3191 intel_add_fb_offsets(&x, &y, plane_state, 0);
6687c906 3192
c2c75131 3193 intel_crtc->dspaddr_offset =
2949056c 3194 intel_compute_tile_offset(&x, &y, plane_state, 0);
6687c906 3195
df0cd455
VS
3196 /* HSW+ does this automagically in hardware */
3197 if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv) &&
3198 rotation & DRM_ROTATE_180) {
3199 x += crtc_state->pipe_src_w - 1;
3200 y += crtc_state->pipe_src_h - 1;
48404c1e
SJ
3201 }
3202
2949056c 3203 linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
6687c906 3204
2db3366b
PZ
3205 intel_crtc->adjusted_x = x;
3206 intel_crtc->adjusted_y = y;
3207
48404c1e 3208 I915_WRITE(reg, dspcntr);
17638cd6 3209
01f2c773 3210 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
85ba7b7d 3211 I915_WRITE(DSPSURF(plane),
be1e3415 3212 intel_plane_ggtt_offset(plane_state) +
6687c906 3213 intel_crtc->dspaddr_offset);
8652744b 3214 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
bc1c91eb
DL
3215 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
3216 } else {
3217 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
3218 I915_WRITE(DSPLINOFF(plane), linear_offset);
3219 }
17638cd6 3220 POSTING_READ(reg);
17638cd6
JB
3221}
3222
7b49f948
VS
3223u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
3224 uint64_t fb_modifier, uint32_t pixel_format)
b321803d 3225{
7b49f948 3226 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
b321803d 3227 return 64;
7b49f948
VS
3228 } else {
3229 int cpp = drm_format_plane_cpp(pixel_format, 0);
3230
27ba3910 3231 return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
b321803d
DL
3232 }
3233}
3234
e435d6e5
ML
3235static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
3236{
3237 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 3238 struct drm_i915_private *dev_priv = to_i915(dev);
e435d6e5
ML
3239
3240 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
3241 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
3242 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
e435d6e5
ML
3243}
3244
a1b2278e
CK
3245/*
3246 * This function detaches (aka. unbinds) unused scalers in hardware
3247 */
0583236e 3248static void skl_detach_scalers(struct intel_crtc *intel_crtc)
a1b2278e 3249{
a1b2278e
CK
3250 struct intel_crtc_scaler_state *scaler_state;
3251 int i;
3252
a1b2278e
CK
3253 scaler_state = &intel_crtc->config->scaler_state;
3254
3255 /* loop through and disable scalers that aren't in use */
3256 for (i = 0; i < intel_crtc->num_scalers; i++) {
e435d6e5
ML
3257 if (!scaler_state->scalers[i].in_use)
3258 skl_detach_scaler(intel_crtc, i);
a1b2278e
CK
3259 }
3260}
3261
d2196774
VS
3262u32 skl_plane_stride(const struct drm_framebuffer *fb, int plane,
3263 unsigned int rotation)
3264{
3265 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
3266 u32 stride = intel_fb_pitch(fb, plane, rotation);
3267
3268 /*
3269 * The stride is either expressed as a multiple of 64 bytes chunks for
3270 * linear buffers or in number of tiles for tiled buffers.
3271 */
bd2ef25d 3272 if (drm_rotation_90_or_270(rotation)) {
353c8598 3273 int cpp = fb->format->cpp[plane];
d2196774 3274
bae781b2 3275 stride /= intel_tile_height(dev_priv, fb->modifier, cpp);
d2196774 3276 } else {
bae781b2 3277 stride /= intel_fb_stride_alignment(dev_priv, fb->modifier,
438b74a5 3278 fb->format->format);
d2196774
VS
3279 }
3280
3281 return stride;
3282}
3283
6156a456 3284u32 skl_plane_ctl_format(uint32_t pixel_format)
70d21f0e 3285{
6156a456 3286 switch (pixel_format) {
d161cf7a 3287 case DRM_FORMAT_C8:
c34ce3d1 3288 return PLANE_CTL_FORMAT_INDEXED;
70d21f0e 3289 case DRM_FORMAT_RGB565:
c34ce3d1 3290 return PLANE_CTL_FORMAT_RGB_565;
70d21f0e 3291 case DRM_FORMAT_XBGR8888:
c34ce3d1 3292 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
6156a456 3293 case DRM_FORMAT_XRGB8888:
c34ce3d1 3294 return PLANE_CTL_FORMAT_XRGB_8888;
6156a456
CK
3295 /*
3296 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
3297 * to be already pre-multiplied. We need to add a knob (or a different
3298 * DRM_FORMAT) for user-space to configure that.
3299 */
f75fb42a 3300 case DRM_FORMAT_ABGR8888:
c34ce3d1 3301 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
6156a456 3302 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
6156a456 3303 case DRM_FORMAT_ARGB8888:
c34ce3d1 3304 return PLANE_CTL_FORMAT_XRGB_8888 |
6156a456 3305 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
70d21f0e 3306 case DRM_FORMAT_XRGB2101010:
c34ce3d1 3307 return PLANE_CTL_FORMAT_XRGB_2101010;
70d21f0e 3308 case DRM_FORMAT_XBGR2101010:
c34ce3d1 3309 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
6156a456 3310 case DRM_FORMAT_YUYV:
c34ce3d1 3311 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
6156a456 3312 case DRM_FORMAT_YVYU:
c34ce3d1 3313 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
6156a456 3314 case DRM_FORMAT_UYVY:
c34ce3d1 3315 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
6156a456 3316 case DRM_FORMAT_VYUY:
c34ce3d1 3317 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
70d21f0e 3318 default:
4249eeef 3319 MISSING_CASE(pixel_format);
70d21f0e 3320 }
8cfcba41 3321
c34ce3d1 3322 return 0;
6156a456 3323}
70d21f0e 3324
6156a456
CK
3325u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
3326{
6156a456 3327 switch (fb_modifier) {
30af77c4 3328 case DRM_FORMAT_MOD_NONE:
70d21f0e 3329 break;
30af77c4 3330 case I915_FORMAT_MOD_X_TILED:
c34ce3d1 3331 return PLANE_CTL_TILED_X;
b321803d 3332 case I915_FORMAT_MOD_Y_TILED:
c34ce3d1 3333 return PLANE_CTL_TILED_Y;
b321803d 3334 case I915_FORMAT_MOD_Yf_TILED:
c34ce3d1 3335 return PLANE_CTL_TILED_YF;
70d21f0e 3336 default:
6156a456 3337 MISSING_CASE(fb_modifier);
70d21f0e 3338 }
8cfcba41 3339
c34ce3d1 3340 return 0;
6156a456 3341}
70d21f0e 3342
6156a456
CK
3343u32 skl_plane_ctl_rotation(unsigned int rotation)
3344{
3b7a5119 3345 switch (rotation) {
31ad61e4 3346 case DRM_ROTATE_0:
6156a456 3347 break;
1e8df167
SJ
3348 /*
3349 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
3350 * while i915 HW rotation is clockwise, thats why this swapping.
3351 */
31ad61e4 3352 case DRM_ROTATE_90:
1e8df167 3353 return PLANE_CTL_ROTATE_270;
31ad61e4 3354 case DRM_ROTATE_180:
c34ce3d1 3355 return PLANE_CTL_ROTATE_180;
31ad61e4 3356 case DRM_ROTATE_270:
1e8df167 3357 return PLANE_CTL_ROTATE_90;
6156a456
CK
3358 default:
3359 MISSING_CASE(rotation);
3360 }
3361
c34ce3d1 3362 return 0;
6156a456
CK
3363}
3364
a8d201af
ML
3365static void skylake_update_primary_plane(struct drm_plane *plane,
3366 const struct intel_crtc_state *crtc_state,
3367 const struct intel_plane_state *plane_state)
6156a456 3368{
a8d201af 3369 struct drm_device *dev = plane->dev;
fac5e23e 3370 struct drm_i915_private *dev_priv = to_i915(dev);
a8d201af
ML
3371 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3372 struct drm_framebuffer *fb = plane_state->base.fb;
8e816bb4
VS
3373 enum plane_id plane_id = to_intel_plane(plane)->id;
3374 enum pipe pipe = to_intel_plane(plane)->pipe;
d2196774 3375 u32 plane_ctl;
a8d201af 3376 unsigned int rotation = plane_state->base.rotation;
d2196774 3377 u32 stride = skl_plane_stride(fb, 0, rotation);
b63a16f6 3378 u32 surf_addr = plane_state->main.offset;
a8d201af 3379 int scaler_id = plane_state->scaler_id;
b63a16f6
VS
3380 int src_x = plane_state->main.x;
3381 int src_y = plane_state->main.y;
936e71e3
VS
3382 int src_w = drm_rect_width(&plane_state->base.src) >> 16;
3383 int src_h = drm_rect_height(&plane_state->base.src) >> 16;
3384 int dst_x = plane_state->base.dst.x1;
3385 int dst_y = plane_state->base.dst.y1;
3386 int dst_w = drm_rect_width(&plane_state->base.dst);
3387 int dst_h = drm_rect_height(&plane_state->base.dst);
70d21f0e 3388
47f9ea8b
ACO
3389 plane_ctl = PLANE_CTL_ENABLE;
3390
3391 if (IS_GEMINILAKE(dev_priv)) {
3392 I915_WRITE(PLANE_COLOR_CTL(pipe, plane_id),
3393 PLANE_COLOR_PIPE_GAMMA_ENABLE |
3394 PLANE_COLOR_PLANE_GAMMA_DISABLE);
3395 } else {
3396 plane_ctl |=
3397 PLANE_CTL_PIPE_GAMMA_ENABLE |
3398 PLANE_CTL_PIPE_CSC_ENABLE |
3399 PLANE_CTL_PLANE_GAMMA_DISABLE;
3400 }
6156a456 3401
438b74a5 3402 plane_ctl |= skl_plane_ctl_format(fb->format->format);
bae781b2 3403 plane_ctl |= skl_plane_ctl_tiling(fb->modifier);
6156a456
CK
3404 plane_ctl |= skl_plane_ctl_rotation(rotation);
3405
6687c906
VS
3406 /* Sizes are 0 based */
3407 src_w--;
3408 src_h--;
3409 dst_w--;
3410 dst_h--;
3411
4c0b8a8b
PZ
3412 intel_crtc->dspaddr_offset = surf_addr;
3413
6687c906
VS
3414 intel_crtc->adjusted_x = src_x;
3415 intel_crtc->adjusted_y = src_y;
2db3366b 3416
8e816bb4
VS
3417 I915_WRITE(PLANE_CTL(pipe, plane_id), plane_ctl);
3418 I915_WRITE(PLANE_OFFSET(pipe, plane_id), (src_y << 16) | src_x);
3419 I915_WRITE(PLANE_STRIDE(pipe, plane_id), stride);
3420 I915_WRITE(PLANE_SIZE(pipe, plane_id), (src_h << 16) | src_w);
6156a456
CK
3421
3422 if (scaler_id >= 0) {
3423 uint32_t ps_ctrl = 0;
3424
3425 WARN_ON(!dst_w || !dst_h);
8e816bb4 3426 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(plane_id) |
6156a456
CK
3427 crtc_state->scaler_state.scalers[scaler_id].mode;
3428 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3429 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3430 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3431 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
8e816bb4 3432 I915_WRITE(PLANE_POS(pipe, plane_id), 0);
6156a456 3433 } else {
8e816bb4 3434 I915_WRITE(PLANE_POS(pipe, plane_id), (dst_y << 16) | dst_x);
6156a456
CK
3435 }
3436
8e816bb4 3437 I915_WRITE(PLANE_SURF(pipe, plane_id),
be1e3415 3438 intel_plane_ggtt_offset(plane_state) + surf_addr);
70d21f0e 3439
8e816bb4 3440 POSTING_READ(PLANE_SURF(pipe, plane_id));
70d21f0e
DL
3441}
3442
a8d201af
ML
3443static void skylake_disable_primary_plane(struct drm_plane *primary,
3444 struct drm_crtc *crtc)
17638cd6
JB
3445{
3446 struct drm_device *dev = crtc->dev;
fac5e23e 3447 struct drm_i915_private *dev_priv = to_i915(dev);
8e816bb4
VS
3448 enum plane_id plane_id = to_intel_plane(primary)->id;
3449 enum pipe pipe = to_intel_plane(primary)->pipe;
62e0fb88 3450
8e816bb4
VS
3451 I915_WRITE(PLANE_CTL(pipe, plane_id), 0);
3452 I915_WRITE(PLANE_SURF(pipe, plane_id), 0);
3453 POSTING_READ(PLANE_SURF(pipe, plane_id));
a8d201af 3454}
29b9bde6 3455
a8d201af
ML
3456/* Assume fb object is pinned & idle & fenced and just update base pointers */
3457static int
3458intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3459 int x, int y, enum mode_set_atomic state)
3460{
3461 /* Support for kgdboc is disabled, this needs a major rework. */
3462 DRM_ERROR("legacy panic handler not supported any more.\n");
3463
3464 return -ENODEV;
81255565
JB
3465}
3466
5a21b665
DV
3467static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
3468{
3469 struct intel_crtc *crtc;
3470
91c8a326 3471 for_each_intel_crtc(&dev_priv->drm, crtc)
5a21b665
DV
3472 intel_finish_page_flip_cs(dev_priv, crtc->pipe);
3473}
3474
7514747d
VS
3475static void intel_update_primary_planes(struct drm_device *dev)
3476{
7514747d 3477 struct drm_crtc *crtc;
96a02917 3478
70e1e0ec 3479 for_each_crtc(dev, crtc) {
11c22da6 3480 struct intel_plane *plane = to_intel_plane(crtc->primary);
73974893
ML
3481 struct intel_plane_state *plane_state =
3482 to_intel_plane_state(plane->base.state);
11c22da6 3483
936e71e3 3484 if (plane_state->base.visible)
a8d201af
ML
3485 plane->update_plane(&plane->base,
3486 to_intel_crtc_state(crtc->state),
3487 plane_state);
73974893
ML
3488 }
3489}
3490
3491static int
3492__intel_display_resume(struct drm_device *dev,
3493 struct drm_atomic_state *state)
3494{
3495 struct drm_crtc_state *crtc_state;
3496 struct drm_crtc *crtc;
3497 int i, ret;
11c22da6 3498
73974893 3499 intel_modeset_setup_hw_state(dev);
29b74b7f 3500 i915_redisable_vga(to_i915(dev));
73974893
ML
3501
3502 if (!state)
3503 return 0;
3504
3505 for_each_crtc_in_state(state, crtc, crtc_state, i) {
3506 /*
3507 * Force recalculation even if we restore
3508 * current state. With fast modeset this may not result
3509 * in a modeset when the state is compatible.
3510 */
3511 crtc_state->mode_changed = true;
96a02917 3512 }
73974893
ML
3513
3514 /* ignore any reset values/BIOS leftovers in the WM registers */
3515 to_intel_atomic_state(state)->skip_intermediate_wm = true;
3516
3517 ret = drm_atomic_commit(state);
3518
3519 WARN_ON(ret == -EDEADLK);
3520 return ret;
96a02917
VS
3521}
3522
4ac2ba2f
VS
3523static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
3524{
ae98104b
VS
3525 return intel_has_gpu_reset(dev_priv) &&
3526 INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
4ac2ba2f
VS
3527}
3528
c033666a 3529void intel_prepare_reset(struct drm_i915_private *dev_priv)
7514747d 3530{
73974893
ML
3531 struct drm_device *dev = &dev_priv->drm;
3532 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
3533 struct drm_atomic_state *state;
3534 int ret;
3535
73974893
ML
3536 /*
3537 * Need mode_config.mutex so that we don't
3538 * trample ongoing ->detect() and whatnot.
3539 */
3540 mutex_lock(&dev->mode_config.mutex);
3541 drm_modeset_acquire_init(ctx, 0);
3542 while (1) {
3543 ret = drm_modeset_lock_all_ctx(dev, ctx);
3544 if (ret != -EDEADLK)
3545 break;
3546
3547 drm_modeset_backoff(ctx);
3548 }
3549
3550 /* reset doesn't touch the display, but flips might get nuked anyway, */
522a63de 3551 if (!i915.force_reset_modeset_test &&
4ac2ba2f 3552 !gpu_reset_clobbers_display(dev_priv))
7514747d
VS
3553 return;
3554
f98ce92f
VS
3555 /*
3556 * Disabling the crtcs gracefully seems nicer. Also the
3557 * g33 docs say we should at least disable all the planes.
3558 */
73974893
ML
3559 state = drm_atomic_helper_duplicate_state(dev, ctx);
3560 if (IS_ERR(state)) {
3561 ret = PTR_ERR(state);
73974893 3562 DRM_ERROR("Duplicating state failed with %i\n", ret);
1e5a15d6 3563 return;
73974893
ML
3564 }
3565
3566 ret = drm_atomic_helper_disable_all(dev, ctx);
3567 if (ret) {
3568 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
1e5a15d6
ACO
3569 drm_atomic_state_put(state);
3570 return;
73974893
ML
3571 }
3572
3573 dev_priv->modeset_restore_state = state;
3574 state->acquire_ctx = ctx;
7514747d
VS
3575}
3576
c033666a 3577void intel_finish_reset(struct drm_i915_private *dev_priv)
7514747d 3578{
73974893
ML
3579 struct drm_device *dev = &dev_priv->drm;
3580 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
3581 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
3582 int ret;
3583
5a21b665
DV
3584 /*
3585 * Flips in the rings will be nuked by the reset,
3586 * so complete all pending flips so that user space
3587 * will get its events and not get stuck.
3588 */
3589 intel_complete_page_flips(dev_priv);
3590
73974893
ML
3591 dev_priv->modeset_restore_state = NULL;
3592
7514747d 3593 /* reset doesn't touch the display */
4ac2ba2f 3594 if (!gpu_reset_clobbers_display(dev_priv)) {
522a63de
ML
3595 if (!state) {
3596 /*
3597 * Flips in the rings have been nuked by the reset,
3598 * so update the base address of all primary
3599 * planes to the the last fb to make sure we're
3600 * showing the correct fb after a reset.
3601 *
3602 * FIXME: Atomic will make this obsolete since we won't schedule
3603 * CS-based flips (which might get lost in gpu resets) any more.
3604 */
3605 intel_update_primary_planes(dev);
3606 } else {
3607 ret = __intel_display_resume(dev, state);
3608 if (ret)
3609 DRM_ERROR("Restoring old state failed with %i\n", ret);
3610 }
73974893
ML
3611 } else {
3612 /*
3613 * The display has been reset as well,
3614 * so need a full re-initialization.
3615 */
3616 intel_runtime_pm_disable_interrupts(dev_priv);
3617 intel_runtime_pm_enable_interrupts(dev_priv);
7514747d 3618
51f59205 3619 intel_pps_unlock_regs_wa(dev_priv);
73974893 3620 intel_modeset_init_hw(dev);
7514747d 3621
73974893
ML
3622 spin_lock_irq(&dev_priv->irq_lock);
3623 if (dev_priv->display.hpd_irq_setup)
3624 dev_priv->display.hpd_irq_setup(dev_priv);
3625 spin_unlock_irq(&dev_priv->irq_lock);
7514747d 3626
73974893
ML
3627 ret = __intel_display_resume(dev, state);
3628 if (ret)
3629 DRM_ERROR("Restoring old state failed with %i\n", ret);
7514747d 3630
73974893
ML
3631 intel_hpd_init(dev_priv);
3632 }
7514747d 3633
0853695c
CW
3634 if (state)
3635 drm_atomic_state_put(state);
73974893
ML
3636 drm_modeset_drop_locks(ctx);
3637 drm_modeset_acquire_fini(ctx);
3638 mutex_unlock(&dev->mode_config.mutex);
7514747d
VS
3639}
3640
8af29b0c
CW
3641static bool abort_flip_on_reset(struct intel_crtc *crtc)
3642{
3643 struct i915_gpu_error *error = &to_i915(crtc->base.dev)->gpu_error;
3644
3645 if (i915_reset_in_progress(error))
3646 return true;
3647
3648 if (crtc->reset_count != i915_reset_count(error))
3649 return true;
3650
3651 return false;
3652}
3653
7d5e3799
CW
3654static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3655{
5a21b665
DV
3656 struct drm_device *dev = crtc->dev;
3657 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5a21b665
DV
3658 bool pending;
3659
8af29b0c 3660 if (abort_flip_on_reset(intel_crtc))
5a21b665
DV
3661 return false;
3662
3663 spin_lock_irq(&dev->event_lock);
3664 pending = to_intel_crtc(crtc)->flip_work != NULL;
3665 spin_unlock_irq(&dev->event_lock);
3666
3667 return pending;
7d5e3799
CW
3668}
3669
bfd16b2a
ML
3670static void intel_update_pipe_config(struct intel_crtc *crtc,
3671 struct intel_crtc_state *old_crtc_state)
e30e8f75 3672{
6315b5d3 3673 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
bfd16b2a
ML
3674 struct intel_crtc_state *pipe_config =
3675 to_intel_crtc_state(crtc->base.state);
e30e8f75 3676
bfd16b2a
ML
3677 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3678 crtc->base.mode = crtc->base.state->mode;
3679
3680 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3681 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3682 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
e30e8f75
GP
3683
3684 /*
3685 * Update pipe size and adjust fitter if needed: the reason for this is
3686 * that in compute_mode_changes we check the native mode (not the pfit
3687 * mode) to see if we can flip rather than do a full mode set. In the
3688 * fastboot case, we'll flip, but if we don't update the pipesrc and
3689 * pfit state, we'll end up with a big fb scanned out into the wrong
3690 * sized surface.
e30e8f75
GP
3691 */
3692
e30e8f75 3693 I915_WRITE(PIPESRC(crtc->pipe),
bfd16b2a
ML
3694 ((pipe_config->pipe_src_w - 1) << 16) |
3695 (pipe_config->pipe_src_h - 1));
3696
3697 /* on skylake this is done by detaching scalers */
6315b5d3 3698 if (INTEL_GEN(dev_priv) >= 9) {
bfd16b2a
ML
3699 skl_detach_scalers(crtc);
3700
3701 if (pipe_config->pch_pfit.enabled)
3702 skylake_pfit_enable(crtc);
6e266956 3703 } else if (HAS_PCH_SPLIT(dev_priv)) {
bfd16b2a
ML
3704 if (pipe_config->pch_pfit.enabled)
3705 ironlake_pfit_enable(crtc);
3706 else if (old_crtc_state->pch_pfit.enabled)
3707 ironlake_pfit_disable(crtc, true);
e30e8f75 3708 }
e30e8f75
GP
3709}
3710
5e84e1a4
ZW
3711static void intel_fdi_normal_train(struct drm_crtc *crtc)
3712{
3713 struct drm_device *dev = crtc->dev;
fac5e23e 3714 struct drm_i915_private *dev_priv = to_i915(dev);
5e84e1a4
ZW
3715 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3716 int pipe = intel_crtc->pipe;
f0f59a00
VS
3717 i915_reg_t reg;
3718 u32 temp;
5e84e1a4
ZW
3719
3720 /* enable normal train */
3721 reg = FDI_TX_CTL(pipe);
3722 temp = I915_READ(reg);
fd6b8f43 3723 if (IS_IVYBRIDGE(dev_priv)) {
357555c0
JB
3724 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3725 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
3726 } else {
3727 temp &= ~FDI_LINK_TRAIN_NONE;
3728 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 3729 }
5e84e1a4
ZW
3730 I915_WRITE(reg, temp);
3731
3732 reg = FDI_RX_CTL(pipe);
3733 temp = I915_READ(reg);
6e266956 3734 if (HAS_PCH_CPT(dev_priv)) {
5e84e1a4
ZW
3735 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3736 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3737 } else {
3738 temp &= ~FDI_LINK_TRAIN_NONE;
3739 temp |= FDI_LINK_TRAIN_NONE;
3740 }
3741 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3742
3743 /* wait one idle pattern time */
3744 POSTING_READ(reg);
3745 udelay(1000);
357555c0
JB
3746
3747 /* IVB wants error correction enabled */
fd6b8f43 3748 if (IS_IVYBRIDGE(dev_priv))
357555c0
JB
3749 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3750 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
3751}
3752
8db9d77b
ZW
3753/* The FDI link training functions for ILK/Ibexpeak. */
3754static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3755{
3756 struct drm_device *dev = crtc->dev;
fac5e23e 3757 struct drm_i915_private *dev_priv = to_i915(dev);
8db9d77b
ZW
3758 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3759 int pipe = intel_crtc->pipe;
f0f59a00
VS
3760 i915_reg_t reg;
3761 u32 temp, tries;
8db9d77b 3762
1c8562f6 3763 /* FDI needs bits from pipe first */
0fc932b8 3764 assert_pipe_enabled(dev_priv, pipe);
0fc932b8 3765
e1a44743
AJ
3766 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3767 for train result */
5eddb70b
CW
3768 reg = FDI_RX_IMR(pipe);
3769 temp = I915_READ(reg);
e1a44743
AJ
3770 temp &= ~FDI_RX_SYMBOL_LOCK;
3771 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3772 I915_WRITE(reg, temp);
3773 I915_READ(reg);
e1a44743
AJ
3774 udelay(150);
3775
8db9d77b 3776 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3777 reg = FDI_TX_CTL(pipe);
3778 temp = I915_READ(reg);
627eb5a3 3779 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3780 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3781 temp &= ~FDI_LINK_TRAIN_NONE;
3782 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 3783 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3784
5eddb70b
CW
3785 reg = FDI_RX_CTL(pipe);
3786 temp = I915_READ(reg);
8db9d77b
ZW
3787 temp &= ~FDI_LINK_TRAIN_NONE;
3788 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b
CW
3789 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3790
3791 POSTING_READ(reg);
8db9d77b
ZW
3792 udelay(150);
3793
5b2adf89 3794 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
DV
3795 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3796 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3797 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 3798
5eddb70b 3799 reg = FDI_RX_IIR(pipe);
e1a44743 3800 for (tries = 0; tries < 5; tries++) {
5eddb70b 3801 temp = I915_READ(reg);
8db9d77b
ZW
3802 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3803
3804 if ((temp & FDI_RX_BIT_LOCK)) {
3805 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 3806 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
3807 break;
3808 }
8db9d77b 3809 }
e1a44743 3810 if (tries == 5)
5eddb70b 3811 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3812
3813 /* Train 2 */
5eddb70b
CW
3814 reg = FDI_TX_CTL(pipe);
3815 temp = I915_READ(reg);
8db9d77b
ZW
3816 temp &= ~FDI_LINK_TRAIN_NONE;
3817 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3818 I915_WRITE(reg, temp);
8db9d77b 3819
5eddb70b
CW
3820 reg = FDI_RX_CTL(pipe);
3821 temp = I915_READ(reg);
8db9d77b
ZW
3822 temp &= ~FDI_LINK_TRAIN_NONE;
3823 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3824 I915_WRITE(reg, temp);
8db9d77b 3825
5eddb70b
CW
3826 POSTING_READ(reg);
3827 udelay(150);
8db9d77b 3828
5eddb70b 3829 reg = FDI_RX_IIR(pipe);
e1a44743 3830 for (tries = 0; tries < 5; tries++) {
5eddb70b 3831 temp = I915_READ(reg);
8db9d77b
ZW
3832 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3833
3834 if (temp & FDI_RX_SYMBOL_LOCK) {
5eddb70b 3835 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
3836 DRM_DEBUG_KMS("FDI train 2 done.\n");
3837 break;
3838 }
8db9d77b 3839 }
e1a44743 3840 if (tries == 5)
5eddb70b 3841 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3842
3843 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 3844
8db9d77b
ZW
3845}
3846
0206e353 3847static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
3848 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3849 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3850 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3851 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3852};
3853
3854/* The FDI link training functions for SNB/Cougarpoint. */
3855static void gen6_fdi_link_train(struct drm_crtc *crtc)
3856{
3857 struct drm_device *dev = crtc->dev;
fac5e23e 3858 struct drm_i915_private *dev_priv = to_i915(dev);
8db9d77b
ZW
3859 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3860 int pipe = intel_crtc->pipe;
f0f59a00
VS
3861 i915_reg_t reg;
3862 u32 temp, i, retry;
8db9d77b 3863
e1a44743
AJ
3864 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3865 for train result */
5eddb70b
CW
3866 reg = FDI_RX_IMR(pipe);
3867 temp = I915_READ(reg);
e1a44743
AJ
3868 temp &= ~FDI_RX_SYMBOL_LOCK;
3869 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3870 I915_WRITE(reg, temp);
3871
3872 POSTING_READ(reg);
e1a44743
AJ
3873 udelay(150);
3874
8db9d77b 3875 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3876 reg = FDI_TX_CTL(pipe);
3877 temp = I915_READ(reg);
627eb5a3 3878 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3879 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3880 temp &= ~FDI_LINK_TRAIN_NONE;
3881 temp |= FDI_LINK_TRAIN_PATTERN_1;
3882 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3883 /* SNB-B */
3884 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 3885 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3886
d74cf324
DV
3887 I915_WRITE(FDI_RX_MISC(pipe),
3888 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3889
5eddb70b
CW
3890 reg = FDI_RX_CTL(pipe);
3891 temp = I915_READ(reg);
6e266956 3892 if (HAS_PCH_CPT(dev_priv)) {
8db9d77b
ZW
3893 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3894 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3895 } else {
3896 temp &= ~FDI_LINK_TRAIN_NONE;
3897 temp |= FDI_LINK_TRAIN_PATTERN_1;
3898 }
5eddb70b
CW
3899 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3900
3901 POSTING_READ(reg);
8db9d77b
ZW
3902 udelay(150);
3903
0206e353 3904 for (i = 0; i < 4; i++) {
5eddb70b
CW
3905 reg = FDI_TX_CTL(pipe);
3906 temp = I915_READ(reg);
8db9d77b
ZW
3907 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3908 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3909 I915_WRITE(reg, temp);
3910
3911 POSTING_READ(reg);
8db9d77b
ZW
3912 udelay(500);
3913
fa37d39e
SP
3914 for (retry = 0; retry < 5; retry++) {
3915 reg = FDI_RX_IIR(pipe);
3916 temp = I915_READ(reg);
3917 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3918 if (temp & FDI_RX_BIT_LOCK) {
3919 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3920 DRM_DEBUG_KMS("FDI train 1 done.\n");
3921 break;
3922 }
3923 udelay(50);
8db9d77b 3924 }
fa37d39e
SP
3925 if (retry < 5)
3926 break;
8db9d77b
ZW
3927 }
3928 if (i == 4)
5eddb70b 3929 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3930
3931 /* Train 2 */
5eddb70b
CW
3932 reg = FDI_TX_CTL(pipe);
3933 temp = I915_READ(reg);
8db9d77b
ZW
3934 temp &= ~FDI_LINK_TRAIN_NONE;
3935 temp |= FDI_LINK_TRAIN_PATTERN_2;
5db94019 3936 if (IS_GEN6(dev_priv)) {
8db9d77b
ZW
3937 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3938 /* SNB-B */
3939 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3940 }
5eddb70b 3941 I915_WRITE(reg, temp);
8db9d77b 3942
5eddb70b
CW
3943 reg = FDI_RX_CTL(pipe);
3944 temp = I915_READ(reg);
6e266956 3945 if (HAS_PCH_CPT(dev_priv)) {
8db9d77b
ZW
3946 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3947 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3948 } else {
3949 temp &= ~FDI_LINK_TRAIN_NONE;
3950 temp |= FDI_LINK_TRAIN_PATTERN_2;
3951 }
5eddb70b
CW
3952 I915_WRITE(reg, temp);
3953
3954 POSTING_READ(reg);
8db9d77b
ZW
3955 udelay(150);
3956
0206e353 3957 for (i = 0; i < 4; i++) {
5eddb70b
CW
3958 reg = FDI_TX_CTL(pipe);
3959 temp = I915_READ(reg);
8db9d77b
ZW
3960 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3961 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3962 I915_WRITE(reg, temp);
3963
3964 POSTING_READ(reg);
8db9d77b
ZW
3965 udelay(500);
3966
fa37d39e
SP
3967 for (retry = 0; retry < 5; retry++) {
3968 reg = FDI_RX_IIR(pipe);
3969 temp = I915_READ(reg);
3970 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3971 if (temp & FDI_RX_SYMBOL_LOCK) {
3972 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3973 DRM_DEBUG_KMS("FDI train 2 done.\n");
3974 break;
3975 }
3976 udelay(50);
8db9d77b 3977 }
fa37d39e
SP
3978 if (retry < 5)
3979 break;
8db9d77b
ZW
3980 }
3981 if (i == 4)
5eddb70b 3982 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3983
3984 DRM_DEBUG_KMS("FDI train done.\n");
3985}
3986
357555c0
JB
3987/* Manual link training for Ivy Bridge A0 parts */
3988static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3989{
3990 struct drm_device *dev = crtc->dev;
fac5e23e 3991 struct drm_i915_private *dev_priv = to_i915(dev);
357555c0
JB
3992 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3993 int pipe = intel_crtc->pipe;
f0f59a00
VS
3994 i915_reg_t reg;
3995 u32 temp, i, j;
357555c0
JB
3996
3997 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3998 for train result */
3999 reg = FDI_RX_IMR(pipe);
4000 temp = I915_READ(reg);
4001 temp &= ~FDI_RX_SYMBOL_LOCK;
4002 temp &= ~FDI_RX_BIT_LOCK;
4003 I915_WRITE(reg, temp);
4004
4005 POSTING_READ(reg);
4006 udelay(150);
4007
01a415fd
DV
4008 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
4009 I915_READ(FDI_RX_IIR(pipe)));
4010
139ccd3f
JB
4011 /* Try each vswing and preemphasis setting twice before moving on */
4012 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
4013 /* disable first in case we need to retry */
4014 reg = FDI_TX_CTL(pipe);
4015 temp = I915_READ(reg);
4016 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
4017 temp &= ~FDI_TX_ENABLE;
4018 I915_WRITE(reg, temp);
357555c0 4019
139ccd3f
JB
4020 reg = FDI_RX_CTL(pipe);
4021 temp = I915_READ(reg);
4022 temp &= ~FDI_LINK_TRAIN_AUTO;
4023 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4024 temp &= ~FDI_RX_ENABLE;
4025 I915_WRITE(reg, temp);
357555c0 4026
139ccd3f 4027 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
4028 reg = FDI_TX_CTL(pipe);
4029 temp = I915_READ(reg);
139ccd3f 4030 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 4031 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
139ccd3f 4032 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 4033 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
4034 temp |= snb_b_fdi_train_param[j/2];
4035 temp |= FDI_COMPOSITE_SYNC;
4036 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 4037
139ccd3f
JB
4038 I915_WRITE(FDI_RX_MISC(pipe),
4039 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 4040
139ccd3f 4041 reg = FDI_RX_CTL(pipe);
357555c0 4042 temp = I915_READ(reg);
139ccd3f
JB
4043 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
4044 temp |= FDI_COMPOSITE_SYNC;
4045 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 4046
139ccd3f
JB
4047 POSTING_READ(reg);
4048 udelay(1); /* should be 0.5us */
357555c0 4049
139ccd3f
JB
4050 for (i = 0; i < 4; i++) {
4051 reg = FDI_RX_IIR(pipe);
4052 temp = I915_READ(reg);
4053 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 4054
139ccd3f
JB
4055 if (temp & FDI_RX_BIT_LOCK ||
4056 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
4057 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
4058 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
4059 i);
4060 break;
4061 }
4062 udelay(1); /* should be 0.5us */
4063 }
4064 if (i == 4) {
4065 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
4066 continue;
4067 }
357555c0 4068
139ccd3f 4069 /* Train 2 */
357555c0
JB
4070 reg = FDI_TX_CTL(pipe);
4071 temp = I915_READ(reg);
139ccd3f
JB
4072 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
4073 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
4074 I915_WRITE(reg, temp);
4075
4076 reg = FDI_RX_CTL(pipe);
4077 temp = I915_READ(reg);
4078 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4079 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
4080 I915_WRITE(reg, temp);
4081
4082 POSTING_READ(reg);
139ccd3f 4083 udelay(2); /* should be 1.5us */
357555c0 4084
139ccd3f
JB
4085 for (i = 0; i < 4; i++) {
4086 reg = FDI_RX_IIR(pipe);
4087 temp = I915_READ(reg);
4088 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 4089
139ccd3f
JB
4090 if (temp & FDI_RX_SYMBOL_LOCK ||
4091 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
4092 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
4093 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
4094 i);
4095 goto train_done;
4096 }
4097 udelay(2); /* should be 1.5us */
357555c0 4098 }
139ccd3f
JB
4099 if (i == 4)
4100 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 4101 }
357555c0 4102
139ccd3f 4103train_done:
357555c0
JB
4104 DRM_DEBUG_KMS("FDI train done.\n");
4105}
4106
88cefb6c 4107static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 4108{
88cefb6c 4109 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 4110 struct drm_i915_private *dev_priv = to_i915(dev);
2c07245f 4111 int pipe = intel_crtc->pipe;
f0f59a00
VS
4112 i915_reg_t reg;
4113 u32 temp;
c64e311e 4114
c98e9dcf 4115 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
4116 reg = FDI_RX_CTL(pipe);
4117 temp = I915_READ(reg);
627eb5a3 4118 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
6e3c9717 4119 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
dfd07d72 4120 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
4121 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
4122
4123 POSTING_READ(reg);
c98e9dcf
JB
4124 udelay(200);
4125
4126 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
4127 temp = I915_READ(reg);
4128 I915_WRITE(reg, temp | FDI_PCDCLK);
4129
4130 POSTING_READ(reg);
c98e9dcf
JB
4131 udelay(200);
4132
20749730
PZ
4133 /* Enable CPU FDI TX PLL, always on for Ironlake */
4134 reg = FDI_TX_CTL(pipe);
4135 temp = I915_READ(reg);
4136 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
4137 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 4138
20749730
PZ
4139 POSTING_READ(reg);
4140 udelay(100);
6be4a607 4141 }
0e23b99d
JB
4142}
4143
88cefb6c
DV
4144static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
4145{
4146 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 4147 struct drm_i915_private *dev_priv = to_i915(dev);
88cefb6c 4148 int pipe = intel_crtc->pipe;
f0f59a00
VS
4149 i915_reg_t reg;
4150 u32 temp;
88cefb6c
DV
4151
4152 /* Switch from PCDclk to Rawclk */
4153 reg = FDI_RX_CTL(pipe);
4154 temp = I915_READ(reg);
4155 I915_WRITE(reg, temp & ~FDI_PCDCLK);
4156
4157 /* Disable CPU FDI TX PLL */
4158 reg = FDI_TX_CTL(pipe);
4159 temp = I915_READ(reg);
4160 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
4161
4162 POSTING_READ(reg);
4163 udelay(100);
4164
4165 reg = FDI_RX_CTL(pipe);
4166 temp = I915_READ(reg);
4167 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
4168
4169 /* Wait for the clocks to turn off. */
4170 POSTING_READ(reg);
4171 udelay(100);
4172}
4173
0fc932b8
JB
4174static void ironlake_fdi_disable(struct drm_crtc *crtc)
4175{
4176 struct drm_device *dev = crtc->dev;
fac5e23e 4177 struct drm_i915_private *dev_priv = to_i915(dev);
0fc932b8
JB
4178 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4179 int pipe = intel_crtc->pipe;
f0f59a00
VS
4180 i915_reg_t reg;
4181 u32 temp;
0fc932b8
JB
4182
4183 /* disable CPU FDI tx and PCH FDI rx */
4184 reg = FDI_TX_CTL(pipe);
4185 temp = I915_READ(reg);
4186 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
4187 POSTING_READ(reg);
4188
4189 reg = FDI_RX_CTL(pipe);
4190 temp = I915_READ(reg);
4191 temp &= ~(0x7 << 16);
dfd07d72 4192 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
4193 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
4194
4195 POSTING_READ(reg);
4196 udelay(100);
4197
4198 /* Ironlake workaround, disable clock pointer after downing FDI */
6e266956 4199 if (HAS_PCH_IBX(dev_priv))
6f06ce18 4200 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
0fc932b8
JB
4201
4202 /* still set train pattern 1 */
4203 reg = FDI_TX_CTL(pipe);
4204 temp = I915_READ(reg);
4205 temp &= ~FDI_LINK_TRAIN_NONE;
4206 temp |= FDI_LINK_TRAIN_PATTERN_1;
4207 I915_WRITE(reg, temp);
4208
4209 reg = FDI_RX_CTL(pipe);
4210 temp = I915_READ(reg);
6e266956 4211 if (HAS_PCH_CPT(dev_priv)) {
0fc932b8
JB
4212 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4213 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
4214 } else {
4215 temp &= ~FDI_LINK_TRAIN_NONE;
4216 temp |= FDI_LINK_TRAIN_PATTERN_1;
4217 }
4218 /* BPC in FDI rx is consistent with that in PIPECONF */
4219 temp &= ~(0x07 << 16);
dfd07d72 4220 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
4221 I915_WRITE(reg, temp);
4222
4223 POSTING_READ(reg);
4224 udelay(100);
4225}
4226
49d73912 4227bool intel_has_pending_fb_unpin(struct drm_i915_private *dev_priv)
5dce5b93
CW
4228{
4229 struct intel_crtc *crtc;
4230
4231 /* Note that we don't need to be called with mode_config.lock here
4232 * as our list of CRTC objects is static for the lifetime of the
4233 * device and so cannot disappear as we iterate. Similarly, we can
4234 * happily treat the predicates as racy, atomic checks as userspace
4235 * cannot claim and pin a new fb without at least acquring the
4236 * struct_mutex and so serialising with us.
4237 */
49d73912 4238 for_each_intel_crtc(&dev_priv->drm, crtc) {
5dce5b93
CW
4239 if (atomic_read(&crtc->unpin_work_count) == 0)
4240 continue;
4241
5a21b665 4242 if (crtc->flip_work)
0f0f74bc 4243 intel_wait_for_vblank(dev_priv, crtc->pipe);
5dce5b93
CW
4244
4245 return true;
4246 }
4247
4248 return false;
4249}
4250
5a21b665 4251static void page_flip_completed(struct intel_crtc *intel_crtc)
d6bbafa1
CW
4252{
4253 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
5a21b665
DV
4254 struct intel_flip_work *work = intel_crtc->flip_work;
4255
4256 intel_crtc->flip_work = NULL;
d6bbafa1
CW
4257
4258 if (work->event)
560ce1dc 4259 drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
d6bbafa1
CW
4260
4261 drm_crtc_vblank_put(&intel_crtc->base);
4262
5a21b665 4263 wake_up_all(&dev_priv->pending_flip_queue);
5a21b665
DV
4264 trace_i915_flip_complete(intel_crtc->plane,
4265 work->pending_flip_obj);
05c41f92
AR
4266
4267 queue_work(dev_priv->wq, &work->unpin_work);
d6bbafa1
CW
4268}
4269
5008e874 4270static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
e6c3a2a6 4271{
0f91128d 4272 struct drm_device *dev = crtc->dev;
fac5e23e 4273 struct drm_i915_private *dev_priv = to_i915(dev);
5008e874 4274 long ret;
e6c3a2a6 4275
2c10d571 4276 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
5008e874
ML
4277
4278 ret = wait_event_interruptible_timeout(
4279 dev_priv->pending_flip_queue,
4280 !intel_crtc_has_pending_flip(crtc),
4281 60*HZ);
4282
4283 if (ret < 0)
4284 return ret;
4285
5a21b665
DV
4286 if (ret == 0) {
4287 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4288 struct intel_flip_work *work;
4289
4290 spin_lock_irq(&dev->event_lock);
4291 work = intel_crtc->flip_work;
4292 if (work && !is_mmio_work(work)) {
4293 WARN_ONCE(1, "Removing stuck page flip\n");
4294 page_flip_completed(intel_crtc);
4295 }
4296 spin_unlock_irq(&dev->event_lock);
4297 }
5bb61643 4298
5008e874 4299 return 0;
e6c3a2a6
CW
4300}
4301
b7076546 4302void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
060f02d8
VS
4303{
4304 u32 temp;
4305
4306 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
4307
4308 mutex_lock(&dev_priv->sb_lock);
4309
4310 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4311 temp |= SBI_SSCCTL_DISABLE;
4312 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
4313
4314 mutex_unlock(&dev_priv->sb_lock);
4315}
4316
e615efe4
ED
4317/* Program iCLKIP clock to the desired frequency */
4318static void lpt_program_iclkip(struct drm_crtc *crtc)
4319{
64b46a06 4320 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6e3c9717 4321 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
e615efe4
ED
4322 u32 divsel, phaseinc, auxdiv, phasedir = 0;
4323 u32 temp;
4324
060f02d8 4325 lpt_disable_iclkip(dev_priv);
e615efe4 4326
64b46a06
VS
4327 /* The iCLK virtual clock root frequency is in MHz,
4328 * but the adjusted_mode->crtc_clock in in KHz. To get the
4329 * divisors, it is necessary to divide one by another, so we
4330 * convert the virtual clock precision to KHz here for higher
4331 * precision.
4332 */
4333 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
e615efe4
ED
4334 u32 iclk_virtual_root_freq = 172800 * 1000;
4335 u32 iclk_pi_range = 64;
64b46a06 4336 u32 desired_divisor;
e615efe4 4337
64b46a06
VS
4338 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4339 clock << auxdiv);
4340 divsel = (desired_divisor / iclk_pi_range) - 2;
4341 phaseinc = desired_divisor % iclk_pi_range;
e615efe4 4342
64b46a06
VS
4343 /*
4344 * Near 20MHz is a corner case which is
4345 * out of range for the 7-bit divisor
4346 */
4347 if (divsel <= 0x7f)
4348 break;
e615efe4
ED
4349 }
4350
4351 /* This should not happen with any sane values */
4352 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
4353 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
4354 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
4355 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
4356
4357 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 4358 clock,
e615efe4
ED
4359 auxdiv,
4360 divsel,
4361 phasedir,
4362 phaseinc);
4363
060f02d8
VS
4364 mutex_lock(&dev_priv->sb_lock);
4365
e615efe4 4366 /* Program SSCDIVINTPHASE6 */
988d6ee8 4367 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
4368 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
4369 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
4370 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
4371 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
4372 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
4373 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 4374 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
4375
4376 /* Program SSCAUXDIV */
988d6ee8 4377 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
4378 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
4379 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 4380 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
4381
4382 /* Enable modulator and associated divider */
988d6ee8 4383 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 4384 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 4385 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4 4386
060f02d8
VS
4387 mutex_unlock(&dev_priv->sb_lock);
4388
e615efe4
ED
4389 /* Wait for initialization time */
4390 udelay(24);
4391
4392 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
4393}
4394
8802e5b6
VS
4395int lpt_get_iclkip(struct drm_i915_private *dev_priv)
4396{
4397 u32 divsel, phaseinc, auxdiv;
4398 u32 iclk_virtual_root_freq = 172800 * 1000;
4399 u32 iclk_pi_range = 64;
4400 u32 desired_divisor;
4401 u32 temp;
4402
4403 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
4404 return 0;
4405
4406 mutex_lock(&dev_priv->sb_lock);
4407
4408 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4409 if (temp & SBI_SSCCTL_DISABLE) {
4410 mutex_unlock(&dev_priv->sb_lock);
4411 return 0;
4412 }
4413
4414 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
4415 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
4416 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
4417 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
4418 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
4419
4420 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
4421 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
4422 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
4423
4424 mutex_unlock(&dev_priv->sb_lock);
4425
4426 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
4427
4428 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4429 desired_divisor << auxdiv);
4430}
4431
275f01b2
DV
4432static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4433 enum pipe pch_transcoder)
4434{
4435 struct drm_device *dev = crtc->base.dev;
fac5e23e 4436 struct drm_i915_private *dev_priv = to_i915(dev);
6e3c9717 4437 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
275f01b2
DV
4438
4439 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4440 I915_READ(HTOTAL(cpu_transcoder)));
4441 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4442 I915_READ(HBLANK(cpu_transcoder)));
4443 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4444 I915_READ(HSYNC(cpu_transcoder)));
4445
4446 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4447 I915_READ(VTOTAL(cpu_transcoder)));
4448 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4449 I915_READ(VBLANK(cpu_transcoder)));
4450 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4451 I915_READ(VSYNC(cpu_transcoder)));
4452 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4453 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4454}
4455
003632d9 4456static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
1fbc0d78 4457{
fac5e23e 4458 struct drm_i915_private *dev_priv = to_i915(dev);
1fbc0d78
DV
4459 uint32_t temp;
4460
4461 temp = I915_READ(SOUTH_CHICKEN1);
003632d9 4462 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
1fbc0d78
DV
4463 return;
4464
4465 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4466 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4467
003632d9
ACO
4468 temp &= ~FDI_BC_BIFURCATION_SELECT;
4469 if (enable)
4470 temp |= FDI_BC_BIFURCATION_SELECT;
4471
4472 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
1fbc0d78
DV
4473 I915_WRITE(SOUTH_CHICKEN1, temp);
4474 POSTING_READ(SOUTH_CHICKEN1);
4475}
4476
4477static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4478{
4479 struct drm_device *dev = intel_crtc->base.dev;
1fbc0d78
DV
4480
4481 switch (intel_crtc->pipe) {
4482 case PIPE_A:
4483 break;
4484 case PIPE_B:
6e3c9717 4485 if (intel_crtc->config->fdi_lanes > 2)
003632d9 4486 cpt_set_fdi_bc_bifurcation(dev, false);
1fbc0d78 4487 else
003632d9 4488 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4489
4490 break;
4491 case PIPE_C:
003632d9 4492 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4493
4494 break;
4495 default:
4496 BUG();
4497 }
4498}
4499
c48b5305
VS
4500/* Return which DP Port should be selected for Transcoder DP control */
4501static enum port
4502intel_trans_dp_port_sel(struct drm_crtc *crtc)
4503{
4504 struct drm_device *dev = crtc->dev;
4505 struct intel_encoder *encoder;
4506
4507 for_each_encoder_on_crtc(dev, crtc, encoder) {
cca0502b 4508 if (encoder->type == INTEL_OUTPUT_DP ||
c48b5305
VS
4509 encoder->type == INTEL_OUTPUT_EDP)
4510 return enc_to_dig_port(&encoder->base)->port;
4511 }
4512
4513 return -1;
4514}
4515
f67a559d
JB
4516/*
4517 * Enable PCH resources required for PCH ports:
4518 * - PCH PLLs
4519 * - FDI training & RX/TX
4520 * - update transcoder timings
4521 * - DP transcoding bits
4522 * - transcoder
4523 */
4524static void ironlake_pch_enable(struct drm_crtc *crtc)
0e23b99d
JB
4525{
4526 struct drm_device *dev = crtc->dev;
fac5e23e 4527 struct drm_i915_private *dev_priv = to_i915(dev);
0e23b99d
JB
4528 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4529 int pipe = intel_crtc->pipe;
f0f59a00 4530 u32 temp;
2c07245f 4531
ab9412ba 4532 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 4533
fd6b8f43 4534 if (IS_IVYBRIDGE(dev_priv))
1fbc0d78
DV
4535 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4536
cd986abb
DV
4537 /* Write the TU size bits before fdi link training, so that error
4538 * detection works. */
4539 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4540 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4541
c98e9dcf 4542 /* For PCH output, training FDI link */
674cf967 4543 dev_priv->display.fdi_link_train(crtc);
2c07245f 4544
3ad8a208
DV
4545 /* We need to program the right clock selection before writing the pixel
4546 * mutliplier into the DPLL. */
6e266956 4547 if (HAS_PCH_CPT(dev_priv)) {
ee7b9f93 4548 u32 sel;
4b645f14 4549
c98e9dcf 4550 temp = I915_READ(PCH_DPLL_SEL);
11887397
DV
4551 temp |= TRANS_DPLL_ENABLE(pipe);
4552 sel = TRANS_DPLLB_SEL(pipe);
8106ddbd
ACO
4553 if (intel_crtc->config->shared_dpll ==
4554 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
ee7b9f93
JB
4555 temp |= sel;
4556 else
4557 temp &= ~sel;
c98e9dcf 4558 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 4559 }
5eddb70b 4560
3ad8a208
DV
4561 /* XXX: pch pll's can be enabled any time before we enable the PCH
4562 * transcoder, and we actually should do this to not upset any PCH
4563 * transcoder that already use the clock when we share it.
4564 *
4565 * Note that enable_shared_dpll tries to do the right thing, but
4566 * get_shared_dpll unconditionally resets the pll - we need that to have
4567 * the right LVDS enable sequence. */
85b3894f 4568 intel_enable_shared_dpll(intel_crtc);
3ad8a208 4569
d9b6cb56
JB
4570 /* set transcoder timing, panel must allow it */
4571 assert_panel_unlocked(dev_priv, pipe);
275f01b2 4572 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
8db9d77b 4573
303b81e0 4574 intel_fdi_normal_train(crtc);
5e84e1a4 4575
c98e9dcf 4576 /* For PCH DP, enable TRANS_DP_CTL */
6e266956
TU
4577 if (HAS_PCH_CPT(dev_priv) &&
4578 intel_crtc_has_dp_encoder(intel_crtc->config)) {
9c4edaee
VS
4579 const struct drm_display_mode *adjusted_mode =
4580 &intel_crtc->config->base.adjusted_mode;
dfd07d72 4581 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
f0f59a00 4582 i915_reg_t reg = TRANS_DP_CTL(pipe);
5eddb70b
CW
4583 temp = I915_READ(reg);
4584 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
4585 TRANS_DP_SYNC_MASK |
4586 TRANS_DP_BPC_MASK);
e3ef4479 4587 temp |= TRANS_DP_OUTPUT_ENABLE;
9325c9f0 4588 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf 4589
9c4edaee 4590 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 4591 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
9c4edaee 4592 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 4593 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf
JB
4594
4595 switch (intel_trans_dp_port_sel(crtc)) {
c48b5305 4596 case PORT_B:
5eddb70b 4597 temp |= TRANS_DP_PORT_SEL_B;
c98e9dcf 4598 break;
c48b5305 4599 case PORT_C:
5eddb70b 4600 temp |= TRANS_DP_PORT_SEL_C;
c98e9dcf 4601 break;
c48b5305 4602 case PORT_D:
5eddb70b 4603 temp |= TRANS_DP_PORT_SEL_D;
c98e9dcf
JB
4604 break;
4605 default:
e95d41e1 4606 BUG();
32f9d658 4607 }
2c07245f 4608
5eddb70b 4609 I915_WRITE(reg, temp);
6be4a607 4610 }
b52eb4dc 4611
b8a4f404 4612 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
4613}
4614
1507e5bd
PZ
4615static void lpt_pch_enable(struct drm_crtc *crtc)
4616{
4617 struct drm_device *dev = crtc->dev;
fac5e23e 4618 struct drm_i915_private *dev_priv = to_i915(dev);
1507e5bd 4619 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 4620 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
1507e5bd 4621
ab9412ba 4622 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
1507e5bd 4623
8c52b5e8 4624 lpt_program_iclkip(crtc);
1507e5bd 4625
0540e488 4626 /* Set transcoder timing. */
275f01b2 4627 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
1507e5bd 4628
937bb610 4629 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
4630}
4631
a1520318 4632static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57 4633{
fac5e23e 4634 struct drm_i915_private *dev_priv = to_i915(dev);
f0f59a00 4635 i915_reg_t dslreg = PIPEDSL(pipe);
d4270e57
JB
4636 u32 temp;
4637
4638 temp = I915_READ(dslreg);
4639 udelay(500);
4640 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 4641 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 4642 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
4643 }
4644}
4645
86adf9d7
ML
4646static int
4647skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4648 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4649 int src_w, int src_h, int dst_w, int dst_h)
a1b2278e 4650{
86adf9d7
ML
4651 struct intel_crtc_scaler_state *scaler_state =
4652 &crtc_state->scaler_state;
4653 struct intel_crtc *intel_crtc =
4654 to_intel_crtc(crtc_state->base.crtc);
a1b2278e 4655 int need_scaling;
6156a456 4656
bd2ef25d 4657 need_scaling = drm_rotation_90_or_270(rotation) ?
6156a456
CK
4658 (src_h != dst_w || src_w != dst_h):
4659 (src_w != dst_w || src_h != dst_h);
a1b2278e
CK
4660
4661 /*
4662 * if plane is being disabled or scaler is no more required or force detach
4663 * - free scaler binded to this plane/crtc
4664 * - in order to do this, update crtc->scaler_usage
4665 *
4666 * Here scaler state in crtc_state is set free so that
4667 * scaler can be assigned to other user. Actual register
4668 * update to free the scaler is done in plane/panel-fit programming.
4669 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4670 */
86adf9d7 4671 if (force_detach || !need_scaling) {
a1b2278e 4672 if (*scaler_id >= 0) {
86adf9d7 4673 scaler_state->scaler_users &= ~(1 << scaler_user);
a1b2278e
CK
4674 scaler_state->scalers[*scaler_id].in_use = 0;
4675
86adf9d7
ML
4676 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4677 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4678 intel_crtc->pipe, scaler_user, *scaler_id,
a1b2278e
CK
4679 scaler_state->scaler_users);
4680 *scaler_id = -1;
4681 }
4682 return 0;
4683 }
4684
4685 /* range checks */
4686 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4687 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4688
4689 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4690 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
86adf9d7 4691 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
a1b2278e 4692 "size is out of scaler range\n",
86adf9d7 4693 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
a1b2278e
CK
4694 return -EINVAL;
4695 }
4696
86adf9d7
ML
4697 /* mark this plane as a scaler user in crtc_state */
4698 scaler_state->scaler_users |= (1 << scaler_user);
4699 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4700 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4701 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4702 scaler_state->scaler_users);
4703
4704 return 0;
4705}
4706
4707/**
4708 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4709 *
4710 * @state: crtc's scaler state
86adf9d7
ML
4711 *
4712 * Return
4713 * 0 - scaler_usage updated successfully
4714 * error - requested scaling cannot be supported or other error condition
4715 */
e435d6e5 4716int skl_update_scaler_crtc(struct intel_crtc_state *state)
86adf9d7 4717{
7c5f93b0 4718 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
86adf9d7 4719
e435d6e5 4720 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
31ad61e4 4721 &state->scaler_state.scaler_id, DRM_ROTATE_0,
86adf9d7 4722 state->pipe_src_w, state->pipe_src_h,
aad941d5 4723 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
86adf9d7
ML
4724}
4725
4726/**
4727 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4728 *
4729 * @state: crtc's scaler state
86adf9d7
ML
4730 * @plane_state: atomic plane state to update
4731 *
4732 * Return
4733 * 0 - scaler_usage updated successfully
4734 * error - requested scaling cannot be supported or other error condition
4735 */
da20eabd
ML
4736static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4737 struct intel_plane_state *plane_state)
86adf9d7
ML
4738{
4739
da20eabd
ML
4740 struct intel_plane *intel_plane =
4741 to_intel_plane(plane_state->base.plane);
86adf9d7
ML
4742 struct drm_framebuffer *fb = plane_state->base.fb;
4743 int ret;
4744
936e71e3 4745 bool force_detach = !fb || !plane_state->base.visible;
86adf9d7 4746
86adf9d7
ML
4747 ret = skl_update_scaler(crtc_state, force_detach,
4748 drm_plane_index(&intel_plane->base),
4749 &plane_state->scaler_id,
4750 plane_state->base.rotation,
936e71e3
VS
4751 drm_rect_width(&plane_state->base.src) >> 16,
4752 drm_rect_height(&plane_state->base.src) >> 16,
4753 drm_rect_width(&plane_state->base.dst),
4754 drm_rect_height(&plane_state->base.dst));
86adf9d7
ML
4755
4756 if (ret || plane_state->scaler_id < 0)
4757 return ret;
4758
a1b2278e 4759 /* check colorkey */
818ed961 4760 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
72660ce0
VS
4761 DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
4762 intel_plane->base.base.id,
4763 intel_plane->base.name);
a1b2278e
CK
4764 return -EINVAL;
4765 }
4766
4767 /* Check src format */
438b74a5 4768 switch (fb->format->format) {
86adf9d7
ML
4769 case DRM_FORMAT_RGB565:
4770 case DRM_FORMAT_XBGR8888:
4771 case DRM_FORMAT_XRGB8888:
4772 case DRM_FORMAT_ABGR8888:
4773 case DRM_FORMAT_ARGB8888:
4774 case DRM_FORMAT_XRGB2101010:
4775 case DRM_FORMAT_XBGR2101010:
4776 case DRM_FORMAT_YUYV:
4777 case DRM_FORMAT_YVYU:
4778 case DRM_FORMAT_UYVY:
4779 case DRM_FORMAT_VYUY:
4780 break;
4781 default:
72660ce0
VS
4782 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
4783 intel_plane->base.base.id, intel_plane->base.name,
438b74a5 4784 fb->base.id, fb->format->format);
86adf9d7 4785 return -EINVAL;
a1b2278e
CK
4786 }
4787
a1b2278e
CK
4788 return 0;
4789}
4790
e435d6e5
ML
4791static void skylake_scaler_disable(struct intel_crtc *crtc)
4792{
4793 int i;
4794
4795 for (i = 0; i < crtc->num_scalers; i++)
4796 skl_detach_scaler(crtc, i);
4797}
4798
4799static void skylake_pfit_enable(struct intel_crtc *crtc)
bd2e244f
JB
4800{
4801 struct drm_device *dev = crtc->base.dev;
fac5e23e 4802 struct drm_i915_private *dev_priv = to_i915(dev);
bd2e244f 4803 int pipe = crtc->pipe;
a1b2278e
CK
4804 struct intel_crtc_scaler_state *scaler_state =
4805 &crtc->config->scaler_state;
4806
4807 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4808
6e3c9717 4809 if (crtc->config->pch_pfit.enabled) {
a1b2278e
CK
4810 int id;
4811
4812 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4813 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4814 return;
4815 }
4816
4817 id = scaler_state->scaler_id;
4818 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4819 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4820 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4821 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4822
4823 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
bd2e244f
JB
4824 }
4825}
4826
b074cec8
JB
4827static void ironlake_pfit_enable(struct intel_crtc *crtc)
4828{
4829 struct drm_device *dev = crtc->base.dev;
fac5e23e 4830 struct drm_i915_private *dev_priv = to_i915(dev);
b074cec8
JB
4831 int pipe = crtc->pipe;
4832
6e3c9717 4833 if (crtc->config->pch_pfit.enabled) {
b074cec8
JB
4834 /* Force use of hard-coded filter coefficients
4835 * as some pre-programmed values are broken,
4836 * e.g. x201.
4837 */
fd6b8f43 4838 if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
b074cec8
JB
4839 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4840 PF_PIPE_SEL_IVB(pipe));
4841 else
4842 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
6e3c9717
ACO
4843 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4844 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
d4270e57
JB
4845 }
4846}
4847
20bc8673 4848void hsw_enable_ips(struct intel_crtc *crtc)
d77e4531 4849{
cea165c3 4850 struct drm_device *dev = crtc->base.dev;
fac5e23e 4851 struct drm_i915_private *dev_priv = to_i915(dev);
d77e4531 4852
6e3c9717 4853 if (!crtc->config->ips_enabled)
d77e4531
PZ
4854 return;
4855
307e4498
ML
4856 /*
4857 * We can only enable IPS after we enable a plane and wait for a vblank
4858 * This function is called from post_plane_update, which is run after
4859 * a vblank wait.
4860 */
cea165c3 4861
d77e4531 4862 assert_plane_enabled(dev_priv, crtc->plane);
8652744b 4863 if (IS_BROADWELL(dev_priv)) {
2a114cc1
BW
4864 mutex_lock(&dev_priv->rps.hw_lock);
4865 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4866 mutex_unlock(&dev_priv->rps.hw_lock);
4867 /* Quoting Art Runyan: "its not safe to expect any particular
4868 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
4869 * mailbox." Moreover, the mailbox may return a bogus state,
4870 * so we need to just enable it and continue on.
2a114cc1
BW
4871 */
4872 } else {
4873 I915_WRITE(IPS_CTL, IPS_ENABLE);
4874 /* The bit only becomes 1 in the next vblank, so this wait here
4875 * is essentially intel_wait_for_vblank. If we don't have this
4876 * and don't wait for vblanks until the end of crtc_enable, then
4877 * the HW state readout code will complain that the expected
4878 * IPS_CTL value is not the one we read. */
2ec9ba3c
CW
4879 if (intel_wait_for_register(dev_priv,
4880 IPS_CTL, IPS_ENABLE, IPS_ENABLE,
4881 50))
2a114cc1
BW
4882 DRM_ERROR("Timed out waiting for IPS enable\n");
4883 }
d77e4531
PZ
4884}
4885
20bc8673 4886void hsw_disable_ips(struct intel_crtc *crtc)
d77e4531
PZ
4887{
4888 struct drm_device *dev = crtc->base.dev;
fac5e23e 4889 struct drm_i915_private *dev_priv = to_i915(dev);
d77e4531 4890
6e3c9717 4891 if (!crtc->config->ips_enabled)
d77e4531
PZ
4892 return;
4893
4894 assert_plane_enabled(dev_priv, crtc->plane);
8652744b 4895 if (IS_BROADWELL(dev_priv)) {
2a114cc1
BW
4896 mutex_lock(&dev_priv->rps.hw_lock);
4897 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4898 mutex_unlock(&dev_priv->rps.hw_lock);
23d0b130 4899 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
b85c1ecf
CW
4900 if (intel_wait_for_register(dev_priv,
4901 IPS_CTL, IPS_ENABLE, 0,
4902 42))
23d0b130 4903 DRM_ERROR("Timed out waiting for IPS disable\n");
e59150dc 4904 } else {
2a114cc1 4905 I915_WRITE(IPS_CTL, 0);
e59150dc
JB
4906 POSTING_READ(IPS_CTL);
4907 }
d77e4531
PZ
4908
4909 /* We need to wait for a vblank before we can disable the plane. */
0f0f74bc 4910 intel_wait_for_vblank(dev_priv, crtc->pipe);
d77e4531
PZ
4911}
4912
7cac945f 4913static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
d3eedb1a 4914{
7cac945f 4915 if (intel_crtc->overlay) {
d3eedb1a 4916 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 4917 struct drm_i915_private *dev_priv = to_i915(dev);
d3eedb1a
VS
4918
4919 mutex_lock(&dev->struct_mutex);
4920 dev_priv->mm.interruptible = false;
4921 (void) intel_overlay_switch_off(intel_crtc->overlay);
4922 dev_priv->mm.interruptible = true;
4923 mutex_unlock(&dev->struct_mutex);
4924 }
4925
4926 /* Let userspace switch the overlay on again. In most cases userspace
4927 * has to recompute where to put it anyway.
4928 */
4929}
4930
87d4300a
ML
4931/**
4932 * intel_post_enable_primary - Perform operations after enabling primary plane
4933 * @crtc: the CRTC whose primary plane was just enabled
4934 *
4935 * Performs potentially sleeping operations that must be done after the primary
4936 * plane is enabled, such as updating FBC and IPS. Note that this may be
4937 * called due to an explicit primary plane update, or due to an implicit
4938 * re-enable that is caused when a sprite plane is updated to no longer
4939 * completely hide the primary plane.
4940 */
4941static void
4942intel_post_enable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4943{
4944 struct drm_device *dev = crtc->dev;
fac5e23e 4945 struct drm_i915_private *dev_priv = to_i915(dev);
a5c4d7bc
VS
4946 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4947 int pipe = intel_crtc->pipe;
a5c4d7bc 4948
87d4300a
ML
4949 /*
4950 * FIXME IPS should be fine as long as one plane is
4951 * enabled, but in practice it seems to have problems
4952 * when going from primary only to sprite only and vice
4953 * versa.
4954 */
a5c4d7bc
VS
4955 hsw_enable_ips(intel_crtc);
4956
f99d7069 4957 /*
87d4300a
ML
4958 * Gen2 reports pipe underruns whenever all planes are disabled.
4959 * So don't enable underrun reporting before at least some planes
4960 * are enabled.
4961 * FIXME: Need to fix the logic to work when we turn off all planes
4962 * but leave the pipe running.
f99d7069 4963 */
5db94019 4964 if (IS_GEN2(dev_priv))
87d4300a
ML
4965 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4966
aca7b684
VS
4967 /* Underruns don't always raise interrupts, so check manually. */
4968 intel_check_cpu_fifo_underruns(dev_priv);
4969 intel_check_pch_fifo_underruns(dev_priv);
a5c4d7bc
VS
4970}
4971
2622a081 4972/* FIXME move all this to pre_plane_update() with proper state tracking */
87d4300a
ML
4973static void
4974intel_pre_disable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4975{
4976 struct drm_device *dev = crtc->dev;
fac5e23e 4977 struct drm_i915_private *dev_priv = to_i915(dev);
a5c4d7bc
VS
4978 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4979 int pipe = intel_crtc->pipe;
a5c4d7bc 4980
87d4300a
ML
4981 /*
4982 * Gen2 reports pipe underruns whenever all planes are disabled.
4983 * So diasble underrun reporting before all the planes get disabled.
4984 * FIXME: Need to fix the logic to work when we turn off all planes
4985 * but leave the pipe running.
4986 */
5db94019 4987 if (IS_GEN2(dev_priv))
87d4300a 4988 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
a5c4d7bc 4989
2622a081
VS
4990 /*
4991 * FIXME IPS should be fine as long as one plane is
4992 * enabled, but in practice it seems to have problems
4993 * when going from primary only to sprite only and vice
4994 * versa.
4995 */
4996 hsw_disable_ips(intel_crtc);
4997}
4998
4999/* FIXME get rid of this and use pre_plane_update */
5000static void
5001intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
5002{
5003 struct drm_device *dev = crtc->dev;
fac5e23e 5004 struct drm_i915_private *dev_priv = to_i915(dev);
2622a081
VS
5005 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5006 int pipe = intel_crtc->pipe;
5007
5008 intel_pre_disable_primary(crtc);
5009
87d4300a
ML
5010 /*
5011 * Vblank time updates from the shadow to live plane control register
5012 * are blocked if the memory self-refresh mode is active at that
5013 * moment. So to make sure the plane gets truly disabled, disable
5014 * first the self-refresh mode. The self-refresh enable bit in turn
5015 * will be checked/applied by the HW only at the next frame start
5016 * event which is after the vblank start event, so we need to have a
5017 * wait-for-vblank between disabling the plane and the pipe.
5018 */
11a85d6a
VS
5019 if (HAS_GMCH_DISPLAY(dev_priv) &&
5020 intel_set_memory_cxsr(dev_priv, false))
0f0f74bc 5021 intel_wait_for_vblank(dev_priv, pipe);
87d4300a
ML
5022}
5023
5a21b665
DV
5024static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
5025{
5026 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
5027 struct drm_atomic_state *old_state = old_crtc_state->base.state;
5028 struct intel_crtc_state *pipe_config =
5029 to_intel_crtc_state(crtc->base.state);
5a21b665
DV
5030 struct drm_plane *primary = crtc->base.primary;
5031 struct drm_plane_state *old_pri_state =
5032 drm_atomic_get_existing_plane_state(old_state, primary);
5033
5748b6a1 5034 intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
5a21b665
DV
5035
5036 crtc->wm.cxsr_allowed = true;
5037
5038 if (pipe_config->update_wm_post && pipe_config->base.active)
432081bc 5039 intel_update_watermarks(crtc);
5a21b665
DV
5040
5041 if (old_pri_state) {
5042 struct intel_plane_state *primary_state =
5043 to_intel_plane_state(primary->state);
5044 struct intel_plane_state *old_primary_state =
5045 to_intel_plane_state(old_pri_state);
5046
5047 intel_fbc_post_update(crtc);
5048
936e71e3 5049 if (primary_state->base.visible &&
5a21b665 5050 (needs_modeset(&pipe_config->base) ||
936e71e3 5051 !old_primary_state->base.visible))
5a21b665
DV
5052 intel_post_enable_primary(&crtc->base);
5053 }
5054}
5055
5c74cd73 5056static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
ac21b225 5057{
5c74cd73 5058 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
ac21b225 5059 struct drm_device *dev = crtc->base.dev;
fac5e23e 5060 struct drm_i915_private *dev_priv = to_i915(dev);
ab1d3a0e
ML
5061 struct intel_crtc_state *pipe_config =
5062 to_intel_crtc_state(crtc->base.state);
5c74cd73
ML
5063 struct drm_atomic_state *old_state = old_crtc_state->base.state;
5064 struct drm_plane *primary = crtc->base.primary;
5065 struct drm_plane_state *old_pri_state =
5066 drm_atomic_get_existing_plane_state(old_state, primary);
5067 bool modeset = needs_modeset(&pipe_config->base);
ccf010fb
ML
5068 struct intel_atomic_state *old_intel_state =
5069 to_intel_atomic_state(old_state);
ac21b225 5070
5c74cd73
ML
5071 if (old_pri_state) {
5072 struct intel_plane_state *primary_state =
5073 to_intel_plane_state(primary->state);
5074 struct intel_plane_state *old_primary_state =
5075 to_intel_plane_state(old_pri_state);
5076
faf68d92 5077 intel_fbc_pre_update(crtc, pipe_config, primary_state);
31ae71fc 5078
936e71e3
VS
5079 if (old_primary_state->base.visible &&
5080 (modeset || !primary_state->base.visible))
5c74cd73
ML
5081 intel_pre_disable_primary(&crtc->base);
5082 }
852eb00d 5083
49cff963 5084 if (pipe_config->disable_cxsr && HAS_GMCH_DISPLAY(dev_priv)) {
852eb00d 5085 crtc->wm.cxsr_allowed = false;
2dfd178d 5086
2622a081
VS
5087 /*
5088 * Vblank time updates from the shadow to live plane control register
5089 * are blocked if the memory self-refresh mode is active at that
5090 * moment. So to make sure the plane gets truly disabled, disable
5091 * first the self-refresh mode. The self-refresh enable bit in turn
5092 * will be checked/applied by the HW only at the next frame start
5093 * event which is after the vblank start event, so we need to have a
5094 * wait-for-vblank between disabling the plane and the pipe.
5095 */
11a85d6a
VS
5096 if (old_crtc_state->base.active &&
5097 intel_set_memory_cxsr(dev_priv, false))
0f0f74bc 5098 intel_wait_for_vblank(dev_priv, crtc->pipe);
852eb00d 5099 }
92826fcd 5100
ed4a6a7c
MR
5101 /*
5102 * IVB workaround: must disable low power watermarks for at least
5103 * one frame before enabling scaling. LP watermarks can be re-enabled
5104 * when scaling is disabled.
5105 *
5106 * WaCxSRDisabledForSpriteScaling:ivb
5107 */
ddd2b792 5108 if (pipe_config->disable_lp_wm && ilk_disable_lp_wm(dev))
0f0f74bc 5109 intel_wait_for_vblank(dev_priv, crtc->pipe);
ed4a6a7c
MR
5110
5111 /*
5112 * If we're doing a modeset, we're done. No need to do any pre-vblank
5113 * watermark programming here.
5114 */
5115 if (needs_modeset(&pipe_config->base))
5116 return;
5117
5118 /*
5119 * For platforms that support atomic watermarks, program the
5120 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
5121 * will be the intermediate values that are safe for both pre- and
5122 * post- vblank; when vblank happens, the 'active' values will be set
5123 * to the final 'target' values and we'll do this again to get the
5124 * optimal watermarks. For gen9+ platforms, the values we program here
5125 * will be the final target values which will get automatically latched
5126 * at vblank time; no further programming will be necessary.
5127 *
5128 * If a platform hasn't been transitioned to atomic watermarks yet,
5129 * we'll continue to update watermarks the old way, if flags tell
5130 * us to.
5131 */
5132 if (dev_priv->display.initial_watermarks != NULL)
ccf010fb
ML
5133 dev_priv->display.initial_watermarks(old_intel_state,
5134 pipe_config);
caed361d 5135 else if (pipe_config->update_wm_pre)
432081bc 5136 intel_update_watermarks(crtc);
ac21b225
ML
5137}
5138
d032ffa0 5139static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
87d4300a
ML
5140{
5141 struct drm_device *dev = crtc->dev;
5142 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
d032ffa0 5143 struct drm_plane *p;
87d4300a
ML
5144 int pipe = intel_crtc->pipe;
5145
7cac945f 5146 intel_crtc_dpms_overlay_disable(intel_crtc);
27321ae8 5147
d032ffa0
ML
5148 drm_for_each_plane_mask(p, dev, plane_mask)
5149 to_intel_plane(p)->disable_plane(p, crtc);
f98551ae 5150
f99d7069
DV
5151 /*
5152 * FIXME: Once we grow proper nuclear flip support out of this we need
5153 * to compute the mask of flip planes precisely. For the time being
5154 * consider this a flip to a NULL plane.
5155 */
5748b6a1 5156 intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
5157}
5158
fb1c98b1 5159static void intel_encoders_pre_pll_enable(struct drm_crtc *crtc,
fd6bbda9 5160 struct intel_crtc_state *crtc_state,
fb1c98b1
ML
5161 struct drm_atomic_state *old_state)
5162{
5163 struct drm_connector_state *old_conn_state;
5164 struct drm_connector *conn;
5165 int i;
5166
5167 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5168 struct drm_connector_state *conn_state = conn->state;
5169 struct intel_encoder *encoder =
5170 to_intel_encoder(conn_state->best_encoder);
5171
5172 if (conn_state->crtc != crtc)
5173 continue;
5174
5175 if (encoder->pre_pll_enable)
fd6bbda9 5176 encoder->pre_pll_enable(encoder, crtc_state, conn_state);
fb1c98b1
ML
5177 }
5178}
5179
5180static void intel_encoders_pre_enable(struct drm_crtc *crtc,
fd6bbda9 5181 struct intel_crtc_state *crtc_state,
fb1c98b1
ML
5182 struct drm_atomic_state *old_state)
5183{
5184 struct drm_connector_state *old_conn_state;
5185 struct drm_connector *conn;
5186 int i;
5187
5188 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5189 struct drm_connector_state *conn_state = conn->state;
5190 struct intel_encoder *encoder =
5191 to_intel_encoder(conn_state->best_encoder);
5192
5193 if (conn_state->crtc != crtc)
5194 continue;
5195
5196 if (encoder->pre_enable)
fd6bbda9 5197 encoder->pre_enable(encoder, crtc_state, conn_state);
fb1c98b1
ML
5198 }
5199}
5200
5201static void intel_encoders_enable(struct drm_crtc *crtc,
fd6bbda9 5202 struct intel_crtc_state *crtc_state,
fb1c98b1
ML
5203 struct drm_atomic_state *old_state)
5204{
5205 struct drm_connector_state *old_conn_state;
5206 struct drm_connector *conn;
5207 int i;
5208
5209 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5210 struct drm_connector_state *conn_state = conn->state;
5211 struct intel_encoder *encoder =
5212 to_intel_encoder(conn_state->best_encoder);
5213
5214 if (conn_state->crtc != crtc)
5215 continue;
5216
fd6bbda9 5217 encoder->enable(encoder, crtc_state, conn_state);
fb1c98b1
ML
5218 intel_opregion_notify_encoder(encoder, true);
5219 }
5220}
5221
5222static void intel_encoders_disable(struct drm_crtc *crtc,
fd6bbda9 5223 struct intel_crtc_state *old_crtc_state,
fb1c98b1
ML
5224 struct drm_atomic_state *old_state)
5225{
5226 struct drm_connector_state *old_conn_state;
5227 struct drm_connector *conn;
5228 int i;
5229
5230 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5231 struct intel_encoder *encoder =
5232 to_intel_encoder(old_conn_state->best_encoder);
5233
5234 if (old_conn_state->crtc != crtc)
5235 continue;
5236
5237 intel_opregion_notify_encoder(encoder, false);
fd6bbda9 5238 encoder->disable(encoder, old_crtc_state, old_conn_state);
fb1c98b1
ML
5239 }
5240}
5241
5242static void intel_encoders_post_disable(struct drm_crtc *crtc,
fd6bbda9 5243 struct intel_crtc_state *old_crtc_state,
fb1c98b1
ML
5244 struct drm_atomic_state *old_state)
5245{
5246 struct drm_connector_state *old_conn_state;
5247 struct drm_connector *conn;
5248 int i;
5249
5250 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5251 struct intel_encoder *encoder =
5252 to_intel_encoder(old_conn_state->best_encoder);
5253
5254 if (old_conn_state->crtc != crtc)
5255 continue;
5256
5257 if (encoder->post_disable)
fd6bbda9 5258 encoder->post_disable(encoder, old_crtc_state, old_conn_state);
fb1c98b1
ML
5259 }
5260}
5261
5262static void intel_encoders_post_pll_disable(struct drm_crtc *crtc,
fd6bbda9 5263 struct intel_crtc_state *old_crtc_state,
fb1c98b1
ML
5264 struct drm_atomic_state *old_state)
5265{
5266 struct drm_connector_state *old_conn_state;
5267 struct drm_connector *conn;
5268 int i;
5269
5270 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5271 struct intel_encoder *encoder =
5272 to_intel_encoder(old_conn_state->best_encoder);
5273
5274 if (old_conn_state->crtc != crtc)
5275 continue;
5276
5277 if (encoder->post_pll_disable)
fd6bbda9 5278 encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
fb1c98b1
ML
5279 }
5280}
5281
4a806558
ML
5282static void ironlake_crtc_enable(struct intel_crtc_state *pipe_config,
5283 struct drm_atomic_state *old_state)
f67a559d 5284{
4a806558 5285 struct drm_crtc *crtc = pipe_config->base.crtc;
f67a559d 5286 struct drm_device *dev = crtc->dev;
fac5e23e 5287 struct drm_i915_private *dev_priv = to_i915(dev);
f67a559d
JB
5288 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5289 int pipe = intel_crtc->pipe;
ccf010fb
ML
5290 struct intel_atomic_state *old_intel_state =
5291 to_intel_atomic_state(old_state);
f67a559d 5292
53d9f4e9 5293 if (WARN_ON(intel_crtc->active))
f67a559d
JB
5294 return;
5295
b2c0593a
VS
5296 /*
5297 * Sometimes spurious CPU pipe underruns happen during FDI
5298 * training, at least with VGA+HDMI cloning. Suppress them.
5299 *
5300 * On ILK we get an occasional spurious CPU pipe underruns
5301 * between eDP port A enable and vdd enable. Also PCH port
5302 * enable seems to result in the occasional CPU pipe underrun.
5303 *
5304 * Spurious PCH underruns also occur during PCH enabling.
5305 */
5306 if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
5307 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
81b088ca
VS
5308 if (intel_crtc->config->has_pch_encoder)
5309 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
5310
6e3c9717 5311 if (intel_crtc->config->has_pch_encoder)
b14b1055
DV
5312 intel_prepare_shared_dpll(intel_crtc);
5313
37a5650b 5314 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 5315 intel_dp_set_m_n(intel_crtc, M1_N1);
29407aab
DV
5316
5317 intel_set_pipe_timings(intel_crtc);
bc58be60 5318 intel_set_pipe_src_size(intel_crtc);
29407aab 5319
6e3c9717 5320 if (intel_crtc->config->has_pch_encoder) {
29407aab 5321 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 5322 &intel_crtc->config->fdi_m_n, NULL);
29407aab
DV
5323 }
5324
5325 ironlake_set_pipeconf(crtc);
5326
f67a559d 5327 intel_crtc->active = true;
8664281b 5328
fd6bbda9 5329 intel_encoders_pre_enable(crtc, pipe_config, old_state);
f67a559d 5330
6e3c9717 5331 if (intel_crtc->config->has_pch_encoder) {
fff367c7
DV
5332 /* Note: FDI PLL enabling _must_ be done before we enable the
5333 * cpu pipes, hence this is separate from all the other fdi/pch
5334 * enabling. */
88cefb6c 5335 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
DV
5336 } else {
5337 assert_fdi_tx_disabled(dev_priv, pipe);
5338 assert_fdi_rx_disabled(dev_priv, pipe);
5339 }
f67a559d 5340
b074cec8 5341 ironlake_pfit_enable(intel_crtc);
f67a559d 5342
9c54c0dd
JB
5343 /*
5344 * On ILK+ LUT must be loaded before the pipe is running but with
5345 * clocks enabled
5346 */
b95c5321 5347 intel_color_load_luts(&pipe_config->base);
9c54c0dd 5348
1d5bf5d9 5349 if (dev_priv->display.initial_watermarks != NULL)
ccf010fb 5350 dev_priv->display.initial_watermarks(old_intel_state, intel_crtc->config);
e1fdc473 5351 intel_enable_pipe(intel_crtc);
f67a559d 5352
6e3c9717 5353 if (intel_crtc->config->has_pch_encoder)
f67a559d 5354 ironlake_pch_enable(crtc);
c98e9dcf 5355
f9b61ff6
DV
5356 assert_vblank_disabled(crtc);
5357 drm_crtc_vblank_on(crtc);
5358
fd6bbda9 5359 intel_encoders_enable(crtc, pipe_config, old_state);
61b77ddd 5360
6e266956 5361 if (HAS_PCH_CPT(dev_priv))
a1520318 5362 cpt_verify_modeset(dev, intel_crtc->pipe);
37ca8d4c
VS
5363
5364 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
5365 if (intel_crtc->config->has_pch_encoder)
0f0f74bc 5366 intel_wait_for_vblank(dev_priv, pipe);
b2c0593a 5367 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
37ca8d4c 5368 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607
JB
5369}
5370
42db64ef
PZ
5371/* IPS only exists on ULT machines and is tied to pipe A. */
5372static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
5373{
50a0bc90 5374 return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
42db64ef
PZ
5375}
5376
4a806558
ML
5377static void haswell_crtc_enable(struct intel_crtc_state *pipe_config,
5378 struct drm_atomic_state *old_state)
4f771f10 5379{
4a806558 5380 struct drm_crtc *crtc = pipe_config->base.crtc;
6315b5d3 5381 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
4f771f10 5382 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
99d736a2 5383 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
4d1de975 5384 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ccf010fb
ML
5385 struct intel_atomic_state *old_intel_state =
5386 to_intel_atomic_state(old_state);
4f771f10 5387
53d9f4e9 5388 if (WARN_ON(intel_crtc->active))
4f771f10
PZ
5389 return;
5390
81b088ca
VS
5391 if (intel_crtc->config->has_pch_encoder)
5392 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5393 false);
5394
fd6bbda9 5395 intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
95a7a2ae 5396
8106ddbd 5397 if (intel_crtc->config->shared_dpll)
df8ad70c
DV
5398 intel_enable_shared_dpll(intel_crtc);
5399
37a5650b 5400 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 5401 intel_dp_set_m_n(intel_crtc, M1_N1);
229fca97 5402
d7edc4e5 5403 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975
JN
5404 intel_set_pipe_timings(intel_crtc);
5405
bc58be60 5406 intel_set_pipe_src_size(intel_crtc);
229fca97 5407
4d1de975
JN
5408 if (cpu_transcoder != TRANSCODER_EDP &&
5409 !transcoder_is_dsi(cpu_transcoder)) {
5410 I915_WRITE(PIPE_MULT(cpu_transcoder),
6e3c9717 5411 intel_crtc->config->pixel_multiplier - 1);
ebb69c95
CT
5412 }
5413
6e3c9717 5414 if (intel_crtc->config->has_pch_encoder) {
229fca97 5415 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 5416 &intel_crtc->config->fdi_m_n, NULL);
229fca97
DV
5417 }
5418
d7edc4e5 5419 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975
JN
5420 haswell_set_pipeconf(crtc);
5421
391bf048 5422 haswell_set_pipemisc(crtc);
229fca97 5423
b95c5321 5424 intel_color_set_csc(&pipe_config->base);
229fca97 5425
4f771f10 5426 intel_crtc->active = true;
8664281b 5427
6b698516
DV
5428 if (intel_crtc->config->has_pch_encoder)
5429 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
5430 else
5431 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5432
fd6bbda9 5433 intel_encoders_pre_enable(crtc, pipe_config, old_state);
4f771f10 5434
d2d65408 5435 if (intel_crtc->config->has_pch_encoder)
4fe9467d 5436 dev_priv->display.fdi_link_train(crtc);
4fe9467d 5437
d7edc4e5 5438 if (!transcoder_is_dsi(cpu_transcoder))
7d4aefd0 5439 intel_ddi_enable_pipe_clock(intel_crtc);
4f771f10 5440
6315b5d3 5441 if (INTEL_GEN(dev_priv) >= 9)
e435d6e5 5442 skylake_pfit_enable(intel_crtc);
ff6d9f55 5443 else
1c132b44 5444 ironlake_pfit_enable(intel_crtc);
4f771f10
PZ
5445
5446 /*
5447 * On ILK+ LUT must be loaded before the pipe is running but with
5448 * clocks enabled
5449 */
b95c5321 5450 intel_color_load_luts(&pipe_config->base);
4f771f10 5451
1f544388 5452 intel_ddi_set_pipe_settings(crtc);
d7edc4e5 5453 if (!transcoder_is_dsi(cpu_transcoder))
7d4aefd0 5454 intel_ddi_enable_transcoder_func(crtc);
4f771f10 5455
1d5bf5d9 5456 if (dev_priv->display.initial_watermarks != NULL)
3125d39f 5457 dev_priv->display.initial_watermarks(old_intel_state, pipe_config);
4d1de975
JN
5458
5459 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
d7edc4e5 5460 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975 5461 intel_enable_pipe(intel_crtc);
42db64ef 5462
6e3c9717 5463 if (intel_crtc->config->has_pch_encoder)
1507e5bd 5464 lpt_pch_enable(crtc);
4f771f10 5465
0037071d 5466 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
0e32b39c
DA
5467 intel_ddi_set_vc_payload_alloc(crtc, true);
5468
f9b61ff6
DV
5469 assert_vblank_disabled(crtc);
5470 drm_crtc_vblank_on(crtc);
5471
fd6bbda9 5472 intel_encoders_enable(crtc, pipe_config, old_state);
4f771f10 5473
6b698516 5474 if (intel_crtc->config->has_pch_encoder) {
0f0f74bc
VS
5475 intel_wait_for_vblank(dev_priv, pipe);
5476 intel_wait_for_vblank(dev_priv, pipe);
6b698516 5477 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
d2d65408
VS
5478 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5479 true);
6b698516 5480 }
d2d65408 5481
e4916946
PZ
5482 /* If we change the relative order between pipe/planes enabling, we need
5483 * to change the workaround. */
99d736a2 5484 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
772c2a51 5485 if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
0f0f74bc
VS
5486 intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
5487 intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
99d736a2 5488 }
4f771f10
PZ
5489}
5490
bfd16b2a 5491static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
3f8dce3a
DV
5492{
5493 struct drm_device *dev = crtc->base.dev;
fac5e23e 5494 struct drm_i915_private *dev_priv = to_i915(dev);
3f8dce3a
DV
5495 int pipe = crtc->pipe;
5496
5497 /* To avoid upsetting the power well on haswell only disable the pfit if
5498 * it's in use. The hw state code will make sure we get this right. */
bfd16b2a 5499 if (force || crtc->config->pch_pfit.enabled) {
3f8dce3a
DV
5500 I915_WRITE(PF_CTL(pipe), 0);
5501 I915_WRITE(PF_WIN_POS(pipe), 0);
5502 I915_WRITE(PF_WIN_SZ(pipe), 0);
5503 }
5504}
5505
4a806558
ML
5506static void ironlake_crtc_disable(struct intel_crtc_state *old_crtc_state,
5507 struct drm_atomic_state *old_state)
6be4a607 5508{
4a806558 5509 struct drm_crtc *crtc = old_crtc_state->base.crtc;
6be4a607 5510 struct drm_device *dev = crtc->dev;
fac5e23e 5511 struct drm_i915_private *dev_priv = to_i915(dev);
6be4a607
JB
5512 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5513 int pipe = intel_crtc->pipe;
b52eb4dc 5514
b2c0593a
VS
5515 /*
5516 * Sometimes spurious CPU pipe underruns happen when the
5517 * pipe is already disabled, but FDI RX/TX is still enabled.
5518 * Happens at least with VGA+HDMI cloning. Suppress them.
5519 */
5520 if (intel_crtc->config->has_pch_encoder) {
5521 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
37ca8d4c 5522 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
b2c0593a 5523 }
37ca8d4c 5524
fd6bbda9 5525 intel_encoders_disable(crtc, old_crtc_state, old_state);
ea9d758d 5526
f9b61ff6
DV
5527 drm_crtc_vblank_off(crtc);
5528 assert_vblank_disabled(crtc);
5529
575f7ab7 5530 intel_disable_pipe(intel_crtc);
32f9d658 5531
bfd16b2a 5532 ironlake_pfit_disable(intel_crtc, false);
2c07245f 5533
b2c0593a 5534 if (intel_crtc->config->has_pch_encoder)
5a74f70a
VS
5535 ironlake_fdi_disable(crtc);
5536
fd6bbda9 5537 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
2c07245f 5538
6e3c9717 5539 if (intel_crtc->config->has_pch_encoder) {
d925c59a 5540 ironlake_disable_pch_transcoder(dev_priv, pipe);
6be4a607 5541
6e266956 5542 if (HAS_PCH_CPT(dev_priv)) {
f0f59a00
VS
5543 i915_reg_t reg;
5544 u32 temp;
5545
d925c59a
DV
5546 /* disable TRANS_DP_CTL */
5547 reg = TRANS_DP_CTL(pipe);
5548 temp = I915_READ(reg);
5549 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5550 TRANS_DP_PORT_SEL_MASK);
5551 temp |= TRANS_DP_PORT_SEL_NONE;
5552 I915_WRITE(reg, temp);
5553
5554 /* disable DPLL_SEL */
5555 temp = I915_READ(PCH_DPLL_SEL);
11887397 5556 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 5557 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 5558 }
e3421a18 5559
d925c59a
DV
5560 ironlake_fdi_pll_disable(intel_crtc);
5561 }
81b088ca 5562
b2c0593a 5563 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
81b088ca 5564 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607 5565}
1b3c7a47 5566
4a806558
ML
5567static void haswell_crtc_disable(struct intel_crtc_state *old_crtc_state,
5568 struct drm_atomic_state *old_state)
ee7b9f93 5569{
4a806558 5570 struct drm_crtc *crtc = old_crtc_state->base.crtc;
6315b5d3 5571 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
ee7b9f93 5572 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 5573 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee7b9f93 5574
d2d65408
VS
5575 if (intel_crtc->config->has_pch_encoder)
5576 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5577 false);
5578
fd6bbda9 5579 intel_encoders_disable(crtc, old_crtc_state, old_state);
4f771f10 5580
f9b61ff6
DV
5581 drm_crtc_vblank_off(crtc);
5582 assert_vblank_disabled(crtc);
5583
4d1de975 5584 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
d7edc4e5 5585 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975 5586 intel_disable_pipe(intel_crtc);
4f771f10 5587
0037071d 5588 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
a4bf214f
VS
5589 intel_ddi_set_vc_payload_alloc(crtc, false);
5590
d7edc4e5 5591 if (!transcoder_is_dsi(cpu_transcoder))
7d4aefd0 5592 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4f771f10 5593
6315b5d3 5594 if (INTEL_GEN(dev_priv) >= 9)
e435d6e5 5595 skylake_scaler_disable(intel_crtc);
ff6d9f55 5596 else
bfd16b2a 5597 ironlake_pfit_disable(intel_crtc, false);
4f771f10 5598
d7edc4e5 5599 if (!transcoder_is_dsi(cpu_transcoder))
7d4aefd0 5600 intel_ddi_disable_pipe_clock(intel_crtc);
4f771f10 5601
fd6bbda9 5602 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
81b088ca 5603
b7076546 5604 if (old_crtc_state->has_pch_encoder)
81b088ca
VS
5605 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5606 true);
4f771f10
PZ
5607}
5608
2dd24552
JB
5609static void i9xx_pfit_enable(struct intel_crtc *crtc)
5610{
5611 struct drm_device *dev = crtc->base.dev;
fac5e23e 5612 struct drm_i915_private *dev_priv = to_i915(dev);
6e3c9717 5613 struct intel_crtc_state *pipe_config = crtc->config;
2dd24552 5614
681a8504 5615 if (!pipe_config->gmch_pfit.control)
2dd24552
JB
5616 return;
5617
2dd24552 5618 /*
c0b03411
DV
5619 * The panel fitter should only be adjusted whilst the pipe is disabled,
5620 * according to register description and PRM.
2dd24552 5621 */
c0b03411
DV
5622 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5623 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 5624
b074cec8
JB
5625 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5626 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
DV
5627
5628 /* Border color in case we don't scale up to the full screen. Black by
5629 * default, change to something else for debugging. */
5630 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
5631}
5632
d05410f9
DA
5633static enum intel_display_power_domain port_to_power_domain(enum port port)
5634{
5635 switch (port) {
5636 case PORT_A:
6331a704 5637 return POWER_DOMAIN_PORT_DDI_A_LANES;
d05410f9 5638 case PORT_B:
6331a704 5639 return POWER_DOMAIN_PORT_DDI_B_LANES;
d05410f9 5640 case PORT_C:
6331a704 5641 return POWER_DOMAIN_PORT_DDI_C_LANES;
d05410f9 5642 case PORT_D:
6331a704 5643 return POWER_DOMAIN_PORT_DDI_D_LANES;
d8e19f99 5644 case PORT_E:
6331a704 5645 return POWER_DOMAIN_PORT_DDI_E_LANES;
d05410f9 5646 default:
b9fec167 5647 MISSING_CASE(port);
d05410f9
DA
5648 return POWER_DOMAIN_PORT_OTHER;
5649 }
5650}
5651
25f78f58
VS
5652static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5653{
5654 switch (port) {
5655 case PORT_A:
5656 return POWER_DOMAIN_AUX_A;
5657 case PORT_B:
5658 return POWER_DOMAIN_AUX_B;
5659 case PORT_C:
5660 return POWER_DOMAIN_AUX_C;
5661 case PORT_D:
5662 return POWER_DOMAIN_AUX_D;
5663 case PORT_E:
5664 /* FIXME: Check VBT for actual wiring of PORT E */
5665 return POWER_DOMAIN_AUX_D;
5666 default:
b9fec167 5667 MISSING_CASE(port);
25f78f58
VS
5668 return POWER_DOMAIN_AUX_A;
5669 }
5670}
5671
319be8ae
ID
5672enum intel_display_power_domain
5673intel_display_port_power_domain(struct intel_encoder *intel_encoder)
5674{
4f8036a2 5675 struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
319be8ae
ID
5676 struct intel_digital_port *intel_dig_port;
5677
5678 switch (intel_encoder->type) {
5679 case INTEL_OUTPUT_UNKNOWN:
5680 /* Only DDI platforms should ever use this output type */
4f8036a2 5681 WARN_ON_ONCE(!HAS_DDI(dev_priv));
cca0502b 5682 case INTEL_OUTPUT_DP:
319be8ae
ID
5683 case INTEL_OUTPUT_HDMI:
5684 case INTEL_OUTPUT_EDP:
5685 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
d05410f9 5686 return port_to_power_domain(intel_dig_port->port);
0e32b39c
DA
5687 case INTEL_OUTPUT_DP_MST:
5688 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5689 return port_to_power_domain(intel_dig_port->port);
319be8ae
ID
5690 case INTEL_OUTPUT_ANALOG:
5691 return POWER_DOMAIN_PORT_CRT;
5692 case INTEL_OUTPUT_DSI:
5693 return POWER_DOMAIN_PORT_DSI;
5694 default:
5695 return POWER_DOMAIN_PORT_OTHER;
5696 }
5697}
5698
25f78f58
VS
5699enum intel_display_power_domain
5700intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5701{
4f8036a2 5702 struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
25f78f58
VS
5703 struct intel_digital_port *intel_dig_port;
5704
5705 switch (intel_encoder->type) {
5706 case INTEL_OUTPUT_UNKNOWN:
651174a4
ID
5707 case INTEL_OUTPUT_HDMI:
5708 /*
5709 * Only DDI platforms should ever use these output types.
5710 * We can get here after the HDMI detect code has already set
5711 * the type of the shared encoder. Since we can't be sure
5712 * what's the status of the given connectors, play safe and
5713 * run the DP detection too.
5714 */
4f8036a2 5715 WARN_ON_ONCE(!HAS_DDI(dev_priv));
cca0502b 5716 case INTEL_OUTPUT_DP:
25f78f58
VS
5717 case INTEL_OUTPUT_EDP:
5718 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5719 return port_to_aux_power_domain(intel_dig_port->port);
5720 case INTEL_OUTPUT_DP_MST:
5721 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5722 return port_to_aux_power_domain(intel_dig_port->port);
5723 default:
b9fec167 5724 MISSING_CASE(intel_encoder->type);
25f78f58
VS
5725 return POWER_DOMAIN_AUX_A;
5726 }
5727}
5728
74bff5f9
ML
5729static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
5730 struct intel_crtc_state *crtc_state)
77d22dca 5731{
319be8ae 5732 struct drm_device *dev = crtc->dev;
37255d8d 5733 struct drm_i915_private *dev_priv = to_i915(dev);
74bff5f9 5734 struct drm_encoder *encoder;
319be8ae
ID
5735 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5736 enum pipe pipe = intel_crtc->pipe;
77d22dca 5737 unsigned long mask;
74bff5f9 5738 enum transcoder transcoder = crtc_state->cpu_transcoder;
77d22dca 5739
74bff5f9 5740 if (!crtc_state->base.active)
292b990e
ML
5741 return 0;
5742
77d22dca
ID
5743 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5744 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
74bff5f9
ML
5745 if (crtc_state->pch_pfit.enabled ||
5746 crtc_state->pch_pfit.force_thru)
77d22dca
ID
5747 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5748
74bff5f9
ML
5749 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5750 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5751
319be8ae 5752 mask |= BIT(intel_display_port_power_domain(intel_encoder));
74bff5f9 5753 }
319be8ae 5754
37255d8d
ML
5755 if (HAS_DDI(dev_priv) && crtc_state->has_audio)
5756 mask |= BIT(POWER_DOMAIN_AUDIO);
5757
15e7ec29
ML
5758 if (crtc_state->shared_dpll)
5759 mask |= BIT(POWER_DOMAIN_PLLS);
5760
77d22dca
ID
5761 return mask;
5762}
5763
74bff5f9
ML
5764static unsigned long
5765modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5766 struct intel_crtc_state *crtc_state)
77d22dca 5767{
fac5e23e 5768 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
292b990e
ML
5769 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5770 enum intel_display_power_domain domain;
5a21b665 5771 unsigned long domains, new_domains, old_domains;
77d22dca 5772
292b990e 5773 old_domains = intel_crtc->enabled_power_domains;
74bff5f9
ML
5774 intel_crtc->enabled_power_domains = new_domains =
5775 get_crtc_power_domains(crtc, crtc_state);
77d22dca 5776
5a21b665 5777 domains = new_domains & ~old_domains;
292b990e
ML
5778
5779 for_each_power_domain(domain, domains)
5780 intel_display_power_get(dev_priv, domain);
5781
5a21b665 5782 return old_domains & ~new_domains;
292b990e
ML
5783}
5784
5785static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5786 unsigned long domains)
5787{
5788 enum intel_display_power_domain domain;
5789
5790 for_each_power_domain(domain, domains)
5791 intel_display_power_put(dev_priv, domain);
5792}
77d22dca 5793
adafdc6f
MK
5794static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5795{
5796 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5797
09d09386
ACO
5798 if (IS_GEMINILAKE(dev_priv))
5799 return 2 * max_cdclk_freq;
5800 else if (INTEL_INFO(dev_priv)->gen >= 9 ||
5801 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
adafdc6f
MK
5802 return max_cdclk_freq;
5803 else if (IS_CHERRYVIEW(dev_priv))
5804 return max_cdclk_freq*95/100;
5805 else if (INTEL_INFO(dev_priv)->gen < 4)
5806 return 2*max_cdclk_freq*90/100;
5807 else
5808 return max_cdclk_freq*90/100;
5809}
5810
b2045352
VS
5811static int skl_calc_cdclk(int max_pixclk, int vco);
5812
4c75b940 5813static void intel_update_max_cdclk(struct drm_i915_private *dev_priv)
560a7ae4 5814{
b976dc53 5815 if (IS_GEN9_BC(dev_priv)) {
560a7ae4 5816 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
b2045352
VS
5817 int max_cdclk, vco;
5818
5819 vco = dev_priv->skl_preferred_vco_freq;
63911d72 5820 WARN_ON(vco != 8100000 && vco != 8640000);
560a7ae4 5821
b2045352
VS
5822 /*
5823 * Use the lower (vco 8640) cdclk values as a
5824 * first guess. skl_calc_cdclk() will correct it
5825 * if the preferred vco is 8100 instead.
5826 */
560a7ae4 5827 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
487ed2e4 5828 max_cdclk = 617143;
560a7ae4 5829 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
b2045352 5830 max_cdclk = 540000;
560a7ae4 5831 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
b2045352 5832 max_cdclk = 432000;
560a7ae4 5833 else
487ed2e4 5834 max_cdclk = 308571;
b2045352
VS
5835
5836 dev_priv->max_cdclk_freq = skl_calc_cdclk(max_cdclk, vco);
89b3c3c7
ACO
5837 } else if (IS_GEMINILAKE(dev_priv)) {
5838 dev_priv->max_cdclk_freq = 316800;
e2d214ae 5839 } else if (IS_BROXTON(dev_priv)) {
281c114f 5840 dev_priv->max_cdclk_freq = 624000;
8652744b 5841 } else if (IS_BROADWELL(dev_priv)) {
560a7ae4
DL
5842 /*
5843 * FIXME with extra cooling we can allow
5844 * 540 MHz for ULX and 675 Mhz for ULT.
5845 * How can we know if extra cooling is
5846 * available? PCI ID, VTB, something else?
5847 */
5848 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5849 dev_priv->max_cdclk_freq = 450000;
50a0bc90 5850 else if (IS_BDW_ULX(dev_priv))
560a7ae4 5851 dev_priv->max_cdclk_freq = 450000;
50a0bc90 5852 else if (IS_BDW_ULT(dev_priv))
560a7ae4
DL
5853 dev_priv->max_cdclk_freq = 540000;
5854 else
5855 dev_priv->max_cdclk_freq = 675000;
920a14b2 5856 } else if (IS_CHERRYVIEW(dev_priv)) {
0904deaf 5857 dev_priv->max_cdclk_freq = 320000;
11a914c2 5858 } else if (IS_VALLEYVIEW(dev_priv)) {
560a7ae4
DL
5859 dev_priv->max_cdclk_freq = 400000;
5860 } else {
5861 /* otherwise assume cdclk is fixed */
5862 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5863 }
5864
adafdc6f
MK
5865 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5866
560a7ae4
DL
5867 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5868 dev_priv->max_cdclk_freq);
adafdc6f
MK
5869
5870 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5871 dev_priv->max_dotclk_freq);
560a7ae4
DL
5872}
5873
4c75b940 5874static void intel_update_cdclk(struct drm_i915_private *dev_priv)
560a7ae4 5875{
1353c4fb 5876 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev_priv);
2f2a121a 5877
83d7c81f 5878 if (INTEL_GEN(dev_priv) >= 9)
709e05c3
VS
5879 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz, VCO: %d kHz, ref: %d kHz\n",
5880 dev_priv->cdclk_freq, dev_priv->cdclk_pll.vco,
5881 dev_priv->cdclk_pll.ref);
2f2a121a
VS
5882 else
5883 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5884 dev_priv->cdclk_freq);
560a7ae4
DL
5885
5886 /*
b5d99ff9
VS
5887 * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
5888 * Programmng [sic] note: bit[9:2] should be programmed to the number
5889 * of cdclk that generates 4MHz reference clock freq which is used to
5890 * generate GMBus clock. This will vary with the cdclk freq.
560a7ae4 5891 */
b5d99ff9 5892 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
560a7ae4 5893 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
560a7ae4
DL
5894}
5895
92891e45
VS
5896/* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5897static int skl_cdclk_decimal(int cdclk)
5898{
5899 return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
5900}
5901
5f199dfa
VS
5902static int bxt_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
5903{
5904 int ratio;
5905
5906 if (cdclk == dev_priv->cdclk_pll.ref)
5907 return 0;
5908
5909 switch (cdclk) {
5910 default:
5911 MISSING_CASE(cdclk);
5912 case 144000:
5913 case 288000:
5914 case 384000:
5915 case 576000:
5916 ratio = 60;
5917 break;
5918 case 624000:
5919 ratio = 65;
5920 break;
5921 }
5922
5923 return dev_priv->cdclk_pll.ref * ratio;
5924}
5925
89b3c3c7
ACO
5926static int glk_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
5927{
5928 int ratio;
5929
5930 if (cdclk == dev_priv->cdclk_pll.ref)
5931 return 0;
5932
5933 switch (cdclk) {
5934 default:
5935 MISSING_CASE(cdclk);
5936 case 79200:
5937 case 158400:
5938 case 316800:
5939 ratio = 33;
5940 break;
5941 }
5942
5943 return dev_priv->cdclk_pll.ref * ratio;
5944}
5945
2b73001e
VS
5946static void bxt_de_pll_disable(struct drm_i915_private *dev_priv)
5947{
5948 I915_WRITE(BXT_DE_PLL_ENABLE, 0);
5949
5950 /* Timeout 200us */
95cac283
CW
5951 if (intel_wait_for_register(dev_priv,
5952 BXT_DE_PLL_ENABLE, BXT_DE_PLL_LOCK, 0,
5953 1))
2b73001e 5954 DRM_ERROR("timeout waiting for DE PLL unlock\n");
83d7c81f
VS
5955
5956 dev_priv->cdclk_pll.vco = 0;
2b73001e
VS
5957}
5958
5f199dfa 5959static void bxt_de_pll_enable(struct drm_i915_private *dev_priv, int vco)
2b73001e 5960{
5f199dfa 5961 int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->cdclk_pll.ref);
2b73001e
VS
5962 u32 val;
5963
5964 val = I915_READ(BXT_DE_PLL_CTL);
5965 val &= ~BXT_DE_PLL_RATIO_MASK;
5f199dfa 5966 val |= BXT_DE_PLL_RATIO(ratio);
2b73001e
VS
5967 I915_WRITE(BXT_DE_PLL_CTL, val);
5968
5969 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5970
5971 /* Timeout 200us */
e084e1b9
CW
5972 if (intel_wait_for_register(dev_priv,
5973 BXT_DE_PLL_ENABLE,
5974 BXT_DE_PLL_LOCK,
5975 BXT_DE_PLL_LOCK,
5976 1))
2b73001e 5977 DRM_ERROR("timeout waiting for DE PLL lock\n");
83d7c81f 5978
5f199dfa 5979 dev_priv->cdclk_pll.vco = vco;
2b73001e
VS
5980}
5981
324513c0 5982static void bxt_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
f8437dd1 5983{
5f199dfa
VS
5984 u32 val, divider;
5985 int vco, ret;
f8437dd1 5986
89b3c3c7
ACO
5987 if (IS_GEMINILAKE(dev_priv))
5988 vco = glk_de_pll_vco(dev_priv, cdclk);
5989 else
5990 vco = bxt_de_pll_vco(dev_priv, cdclk);
5f199dfa
VS
5991
5992 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
5993
5994 /* cdclk = vco / 2 / div{1,1.5,2,4} */
5995 switch (DIV_ROUND_CLOSEST(vco, cdclk)) {
5996 case 8:
f8437dd1 5997 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
f8437dd1 5998 break;
5f199dfa 5999 case 4:
f8437dd1 6000 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
f8437dd1 6001 break;
5f199dfa 6002 case 3:
89b3c3c7 6003 WARN(IS_GEMINILAKE(dev_priv), "Unsupported divider\n");
f8437dd1 6004 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
f8437dd1 6005 break;
5f199dfa 6006 case 2:
f8437dd1 6007 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
f8437dd1
VK
6008 break;
6009 default:
5f199dfa
VS
6010 WARN_ON(cdclk != dev_priv->cdclk_pll.ref);
6011 WARN_ON(vco != 0);
f8437dd1 6012
5f199dfa
VS
6013 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
6014 break;
f8437dd1
VK
6015 }
6016
f8437dd1 6017 /* Inform power controller of upcoming frequency change */
5f199dfa 6018 mutex_lock(&dev_priv->rps.hw_lock);
f8437dd1
VK
6019 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
6020 0x80000000);
6021 mutex_unlock(&dev_priv->rps.hw_lock);
6022
6023 if (ret) {
6024 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
9ef56154 6025 ret, cdclk);
f8437dd1
VK
6026 return;
6027 }
6028
5f199dfa
VS
6029 if (dev_priv->cdclk_pll.vco != 0 &&
6030 dev_priv->cdclk_pll.vco != vco)
2b73001e 6031 bxt_de_pll_disable(dev_priv);
f8437dd1 6032
5f199dfa
VS
6033 if (dev_priv->cdclk_pll.vco != vco)
6034 bxt_de_pll_enable(dev_priv, vco);
f8437dd1 6035
5f199dfa
VS
6036 val = divider | skl_cdclk_decimal(cdclk);
6037 /*
6038 * FIXME if only the cd2x divider needs changing, it could be done
6039 * without shutting off the pipe (if only one pipe is active).
6040 */
6041 val |= BXT_CDCLK_CD2X_PIPE_NONE;
6042 /*
6043 * Disable SSA Precharge when CD clock frequency < 500 MHz,
6044 * enable otherwise.
6045 */
6046 if (cdclk >= 500000)
6047 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
6048 I915_WRITE(CDCLK_CTL, val);
f8437dd1
VK
6049
6050 mutex_lock(&dev_priv->rps.hw_lock);
6051 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
9ef56154 6052 DIV_ROUND_UP(cdclk, 25000));
f8437dd1
VK
6053 mutex_unlock(&dev_priv->rps.hw_lock);
6054
6055 if (ret) {
6056 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
9ef56154 6057 ret, cdclk);
f8437dd1
VK
6058 return;
6059 }
6060
4c75b940 6061 intel_update_cdclk(dev_priv);
f8437dd1
VK
6062}
6063
d66a2194 6064static void bxt_sanitize_cdclk(struct drm_i915_private *dev_priv)
f8437dd1 6065{
d66a2194
ID
6066 u32 cdctl, expected;
6067
4c75b940 6068 intel_update_cdclk(dev_priv);
f8437dd1 6069
d66a2194
ID
6070 if (dev_priv->cdclk_pll.vco == 0 ||
6071 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
6072 goto sanitize;
6073
6074 /* DPLL okay; verify the cdclock
6075 *
6076 * Some BIOS versions leave an incorrect decimal frequency value and
6077 * set reserved MBZ bits in CDCLK_CTL at least during exiting from S4,
6078 * so sanitize this register.
6079 */
6080 cdctl = I915_READ(CDCLK_CTL);
6081 /*
6082 * Let's ignore the pipe field, since BIOS could have configured the
6083 * dividers both synching to an active pipe, or asynchronously
6084 * (PIPE_NONE).
6085 */
6086 cdctl &= ~BXT_CDCLK_CD2X_PIPE_NONE;
6087
6088 expected = (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) |
6089 skl_cdclk_decimal(dev_priv->cdclk_freq);
6090 /*
6091 * Disable SSA Precharge when CD clock frequency < 500 MHz,
6092 * enable otherwise.
6093 */
6094 if (dev_priv->cdclk_freq >= 500000)
6095 expected |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
6096
6097 if (cdctl == expected)
6098 /* All well; nothing to sanitize */
6099 return;
6100
6101sanitize:
6102 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
6103
6104 /* force cdclk programming */
6105 dev_priv->cdclk_freq = 0;
6106
6107 /* force full PLL disable + enable */
6108 dev_priv->cdclk_pll.vco = -1;
6109}
6110
324513c0 6111void bxt_init_cdclk(struct drm_i915_private *dev_priv)
d66a2194 6112{
89b3c3c7
ACO
6113 int cdclk;
6114
d66a2194
ID
6115 bxt_sanitize_cdclk(dev_priv);
6116
6117 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0)
089c6fd5 6118 return;
c2e001ef 6119
f8437dd1
VK
6120 /*
6121 * FIXME:
6122 * - The initial CDCLK needs to be read from VBT.
6123 * Need to make this change after VBT has changes for BXT.
f8437dd1 6124 */
89b3c3c7
ACO
6125 if (IS_GEMINILAKE(dev_priv))
6126 cdclk = glk_calc_cdclk(0);
6127 else
6128 cdclk = bxt_calc_cdclk(0);
6129
6130 bxt_set_cdclk(dev_priv, cdclk);
f8437dd1
VK
6131}
6132
324513c0 6133void bxt_uninit_cdclk(struct drm_i915_private *dev_priv)
f8437dd1 6134{
324513c0 6135 bxt_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref);
f8437dd1
VK
6136}
6137
a8ca4934
VS
6138static int skl_calc_cdclk(int max_pixclk, int vco)
6139{
63911d72 6140 if (vco == 8640000) {
a8ca4934 6141 if (max_pixclk > 540000)
487ed2e4 6142 return 617143;
a8ca4934
VS
6143 else if (max_pixclk > 432000)
6144 return 540000;
487ed2e4 6145 else if (max_pixclk > 308571)
a8ca4934
VS
6146 return 432000;
6147 else
487ed2e4 6148 return 308571;
a8ca4934 6149 } else {
a8ca4934
VS
6150 if (max_pixclk > 540000)
6151 return 675000;
6152 else if (max_pixclk > 450000)
6153 return 540000;
6154 else if (max_pixclk > 337500)
6155 return 450000;
6156 else
6157 return 337500;
6158 }
6159}
6160
ea61791e
VS
6161static void
6162skl_dpll0_update(struct drm_i915_private *dev_priv)
5d96d8af 6163{
ea61791e 6164 u32 val;
5d96d8af 6165
709e05c3 6166 dev_priv->cdclk_pll.ref = 24000;
1c3f7700 6167 dev_priv->cdclk_pll.vco = 0;
709e05c3 6168
ea61791e 6169 val = I915_READ(LCPLL1_CTL);
1c3f7700 6170 if ((val & LCPLL_PLL_ENABLE) == 0)
ea61791e 6171 return;
5d96d8af 6172
1c3f7700
ID
6173 if (WARN_ON((val & LCPLL_PLL_LOCK) == 0))
6174 return;
9f7eb31a 6175
ea61791e
VS
6176 val = I915_READ(DPLL_CTRL1);
6177
1c3f7700
ID
6178 if (WARN_ON((val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
6179 DPLL_CTRL1_SSC(SKL_DPLL0) |
6180 DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
6181 DPLL_CTRL1_OVERRIDE(SKL_DPLL0)))
6182 return;
9f7eb31a 6183
ea61791e
VS
6184 switch (val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) {
6185 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0):
6186 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, SKL_DPLL0):
6187 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, SKL_DPLL0):
6188 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, SKL_DPLL0):
63911d72 6189 dev_priv->cdclk_pll.vco = 8100000;
ea61791e
VS
6190 break;
6191 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0):
6192 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, SKL_DPLL0):
63911d72 6193 dev_priv->cdclk_pll.vco = 8640000;
ea61791e
VS
6194 break;
6195 default:
6196 MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
ea61791e
VS
6197 break;
6198 }
5d96d8af
DL
6199}
6200
b2045352
VS
6201void skl_set_preferred_cdclk_vco(struct drm_i915_private *dev_priv, int vco)
6202{
6203 bool changed = dev_priv->skl_preferred_vco_freq != vco;
6204
6205 dev_priv->skl_preferred_vco_freq = vco;
6206
6207 if (changed)
4c75b940 6208 intel_update_max_cdclk(dev_priv);
b2045352
VS
6209}
6210
5d96d8af 6211static void
3861fc60 6212skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
5d96d8af 6213{
a8ca4934 6214 int min_cdclk = skl_calc_cdclk(0, vco);
5d96d8af
DL
6215 u32 val;
6216
63911d72 6217 WARN_ON(vco != 8100000 && vco != 8640000);
b2045352 6218
5d96d8af 6219 /* select the minimum CDCLK before enabling DPLL 0 */
9ef56154 6220 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_cdclk);
5d96d8af
DL
6221 I915_WRITE(CDCLK_CTL, val);
6222 POSTING_READ(CDCLK_CTL);
6223
6224 /*
6225 * We always enable DPLL0 with the lowest link rate possible, but still
6226 * taking into account the VCO required to operate the eDP panel at the
6227 * desired frequency. The usual DP link rates operate with a VCO of
6228 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
6229 * The modeset code is responsible for the selection of the exact link
6230 * rate later on, with the constraint of choosing a frequency that
a8ca4934 6231 * works with vco.
5d96d8af
DL
6232 */
6233 val = I915_READ(DPLL_CTRL1);
6234
6235 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
6236 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
6237 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
63911d72 6238 if (vco == 8640000)
5d96d8af
DL
6239 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
6240 SKL_DPLL0);
6241 else
6242 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
6243 SKL_DPLL0);
6244
6245 I915_WRITE(DPLL_CTRL1, val);
6246 POSTING_READ(DPLL_CTRL1);
6247
6248 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
6249
e24ca054
CW
6250 if (intel_wait_for_register(dev_priv,
6251 LCPLL1_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
6252 5))
5d96d8af 6253 DRM_ERROR("DPLL0 not locked\n");
1cd593e0 6254
63911d72 6255 dev_priv->cdclk_pll.vco = vco;
b2045352
VS
6256
6257 /* We'll want to keep using the current vco from now on. */
6258 skl_set_preferred_cdclk_vco(dev_priv, vco);
5d96d8af
DL
6259}
6260
430e05de
VS
6261static void
6262skl_dpll0_disable(struct drm_i915_private *dev_priv)
6263{
6264 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
8ad32a05
CW
6265 if (intel_wait_for_register(dev_priv,
6266 LCPLL1_CTL, LCPLL_PLL_LOCK, 0,
6267 1))
430e05de 6268 DRM_ERROR("Couldn't disable DPLL0\n");
1cd593e0 6269
63911d72 6270 dev_priv->cdclk_pll.vco = 0;
430e05de
VS
6271}
6272
1cd593e0 6273static void skl_set_cdclk(struct drm_i915_private *dev_priv, int cdclk, int vco)
5d96d8af
DL
6274{
6275 u32 freq_select, pcu_ack;
a0b8a1fe 6276 int ret;
5d96d8af 6277
1cd593e0
VS
6278 WARN_ON((cdclk == 24000) != (vco == 0));
6279
63911d72 6280 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
5d96d8af 6281
a0b8a1fe
ID
6282 mutex_lock(&dev_priv->rps.hw_lock);
6283 ret = skl_pcode_request(dev_priv, SKL_PCODE_CDCLK_CONTROL,
6284 SKL_CDCLK_PREPARE_FOR_CHANGE,
6285 SKL_CDCLK_READY_FOR_CHANGE,
6286 SKL_CDCLK_READY_FOR_CHANGE, 3);
6287 mutex_unlock(&dev_priv->rps.hw_lock);
6288 if (ret) {
6289 DRM_ERROR("Failed to inform PCU about cdclk change (%d)\n",
6290 ret);
5d96d8af
DL
6291 return;
6292 }
6293
6294 /* set CDCLK_CTL */
9ef56154 6295 switch (cdclk) {
5d96d8af
DL
6296 case 450000:
6297 case 432000:
6298 freq_select = CDCLK_FREQ_450_432;
6299 pcu_ack = 1;
6300 break;
6301 case 540000:
6302 freq_select = CDCLK_FREQ_540;
6303 pcu_ack = 2;
6304 break;
487ed2e4 6305 case 308571:
5d96d8af
DL
6306 case 337500:
6307 default:
6308 freq_select = CDCLK_FREQ_337_308;
6309 pcu_ack = 0;
6310 break;
487ed2e4 6311 case 617143:
5d96d8af
DL
6312 case 675000:
6313 freq_select = CDCLK_FREQ_675_617;
6314 pcu_ack = 3;
6315 break;
6316 }
6317
63911d72
VS
6318 if (dev_priv->cdclk_pll.vco != 0 &&
6319 dev_priv->cdclk_pll.vco != vco)
1cd593e0
VS
6320 skl_dpll0_disable(dev_priv);
6321
63911d72 6322 if (dev_priv->cdclk_pll.vco != vco)
1cd593e0
VS
6323 skl_dpll0_enable(dev_priv, vco);
6324
9ef56154 6325 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(cdclk));
5d96d8af
DL
6326 POSTING_READ(CDCLK_CTL);
6327
6328 /* inform PCU of the change */
6329 mutex_lock(&dev_priv->rps.hw_lock);
6330 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
6331 mutex_unlock(&dev_priv->rps.hw_lock);
560a7ae4 6332
4c75b940 6333 intel_update_cdclk(dev_priv);
5d96d8af
DL
6334}
6335
9f7eb31a
VS
6336static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv);
6337
5d96d8af
DL
6338void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
6339{
709e05c3 6340 skl_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref, 0);
5d96d8af
DL
6341}
6342
6343void skl_init_cdclk(struct drm_i915_private *dev_priv)
6344{
9f7eb31a
VS
6345 int cdclk, vco;
6346
6347 skl_sanitize_cdclk(dev_priv);
5d96d8af 6348
63911d72 6349 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0) {
9f7eb31a
VS
6350 /*
6351 * Use the current vco as our initial
6352 * guess as to what the preferred vco is.
6353 */
6354 if (dev_priv->skl_preferred_vco_freq == 0)
6355 skl_set_preferred_cdclk_vco(dev_priv,
63911d72 6356 dev_priv->cdclk_pll.vco);
70c2c184 6357 return;
1cd593e0 6358 }
5d96d8af 6359
70c2c184
VS
6360 vco = dev_priv->skl_preferred_vco_freq;
6361 if (vco == 0)
63911d72 6362 vco = 8100000;
70c2c184 6363 cdclk = skl_calc_cdclk(0, vco);
5d96d8af 6364
70c2c184 6365 skl_set_cdclk(dev_priv, cdclk, vco);
5d96d8af
DL
6366}
6367
9f7eb31a 6368static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
c73666f3 6369{
09492498 6370 uint32_t cdctl, expected;
c73666f3 6371
f1b391a5
SK
6372 /*
6373 * check if the pre-os intialized the display
6374 * There is SWF18 scratchpad register defined which is set by the
6375 * pre-os which can be used by the OS drivers to check the status
6376 */
6377 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
6378 goto sanitize;
6379
4c75b940 6380 intel_update_cdclk(dev_priv);
c73666f3 6381 /* Is PLL enabled and locked ? */
1c3f7700
ID
6382 if (dev_priv->cdclk_pll.vco == 0 ||
6383 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
c73666f3
SK
6384 goto sanitize;
6385
6386 /* DPLL okay; verify the cdclock
6387 *
6388 * Noticed in some instances that the freq selection is correct but
6389 * decimal part is programmed wrong from BIOS where pre-os does not
6390 * enable display. Verify the same as well.
6391 */
09492498
VS
6392 cdctl = I915_READ(CDCLK_CTL);
6393 expected = (cdctl & CDCLK_FREQ_SEL_MASK) |
6394 skl_cdclk_decimal(dev_priv->cdclk_freq);
6395 if (cdctl == expected)
c73666f3 6396 /* All well; nothing to sanitize */
9f7eb31a 6397 return;
c89e39f3 6398
9f7eb31a
VS
6399sanitize:
6400 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
c73666f3 6401
9f7eb31a
VS
6402 /* force cdclk programming */
6403 dev_priv->cdclk_freq = 0;
6404 /* force full PLL disable + enable */
63911d72 6405 dev_priv->cdclk_pll.vco = -1;
c73666f3
SK
6406}
6407
30a970c6
JB
6408/* Adjust CDclk dividers to allow high res or save power if possible */
6409static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
6410{
fac5e23e 6411 struct drm_i915_private *dev_priv = to_i915(dev);
30a970c6
JB
6412 u32 val, cmd;
6413
1353c4fb 6414 WARN_ON(dev_priv->display.get_display_clock_speed(dev_priv)
164dfd28 6415 != dev_priv->cdclk_freq);
d60c4473 6416
dfcab17e 6417 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
30a970c6 6418 cmd = 2;
dfcab17e 6419 else if (cdclk == 266667)
30a970c6
JB
6420 cmd = 1;
6421 else
6422 cmd = 0;
6423
6424 mutex_lock(&dev_priv->rps.hw_lock);
6425 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
6426 val &= ~DSPFREQGUAR_MASK;
6427 val |= (cmd << DSPFREQGUAR_SHIFT);
6428 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
6429 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
6430 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
6431 50)) {
6432 DRM_ERROR("timed out waiting for CDclk change\n");
6433 }
6434 mutex_unlock(&dev_priv->rps.hw_lock);
6435
54433e91
VS
6436 mutex_lock(&dev_priv->sb_lock);
6437
dfcab17e 6438 if (cdclk == 400000) {
6bcda4f0 6439 u32 divider;
30a970c6 6440
6bcda4f0 6441 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
30a970c6 6442
30a970c6
JB
6443 /* adjust cdclk divider */
6444 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
87d5d259 6445 val &= ~CCK_FREQUENCY_VALUES;
30a970c6
JB
6446 val |= divider;
6447 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
a877e801
VS
6448
6449 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
87d5d259 6450 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
a877e801
VS
6451 50))
6452 DRM_ERROR("timed out waiting for CDclk change\n");
30a970c6
JB
6453 }
6454
30a970c6
JB
6455 /* adjust self-refresh exit latency value */
6456 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
6457 val &= ~0x7f;
6458
6459 /*
6460 * For high bandwidth configs, we set a higher latency in the bunit
6461 * so that the core display fetch happens in time to avoid underruns.
6462 */
dfcab17e 6463 if (cdclk == 400000)
30a970c6
JB
6464 val |= 4500 / 250; /* 4.5 usec */
6465 else
6466 val |= 3000 / 250; /* 3.0 usec */
6467 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
54433e91 6468
a580516d 6469 mutex_unlock(&dev_priv->sb_lock);
30a970c6 6470
4c75b940 6471 intel_update_cdclk(dev_priv);
30a970c6
JB
6472}
6473
383c5a6a
VS
6474static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
6475{
fac5e23e 6476 struct drm_i915_private *dev_priv = to_i915(dev);
383c5a6a
VS
6477 u32 val, cmd;
6478
1353c4fb 6479 WARN_ON(dev_priv->display.get_display_clock_speed(dev_priv)
164dfd28 6480 != dev_priv->cdclk_freq);
383c5a6a
VS
6481
6482 switch (cdclk) {
383c5a6a
VS
6483 case 333333:
6484 case 320000:
383c5a6a 6485 case 266667:
383c5a6a 6486 case 200000:
383c5a6a
VS
6487 break;
6488 default:
5f77eeb0 6489 MISSING_CASE(cdclk);
383c5a6a
VS
6490 return;
6491 }
6492
9d0d3fda
VS
6493 /*
6494 * Specs are full of misinformation, but testing on actual
6495 * hardware has shown that we just need to write the desired
6496 * CCK divider into the Punit register.
6497 */
6498 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
6499
383c5a6a
VS
6500 mutex_lock(&dev_priv->rps.hw_lock);
6501 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
6502 val &= ~DSPFREQGUAR_MASK_CHV;
6503 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
6504 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
6505 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
6506 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
6507 50)) {
6508 DRM_ERROR("timed out waiting for CDclk change\n");
6509 }
6510 mutex_unlock(&dev_priv->rps.hw_lock);
6511
4c75b940 6512 intel_update_cdclk(dev_priv);
383c5a6a
VS
6513}
6514
30a970c6
JB
6515static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
6516 int max_pixclk)
6517{
6bcda4f0 6518 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
6cca3195 6519 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
29dc7ef3 6520
30a970c6
JB
6521 /*
6522 * Really only a few cases to deal with, as only 4 CDclks are supported:
6523 * 200MHz
6524 * 267MHz
29dc7ef3 6525 * 320/333MHz (depends on HPLL freq)
6cca3195
VS
6526 * 400MHz (VLV only)
6527 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
6528 * of the lower bin and adjust if needed.
e37c67a1
VS
6529 *
6530 * We seem to get an unstable or solid color picture at 200MHz.
6531 * Not sure what's wrong. For now use 200MHz only when all pipes
6532 * are off.
30a970c6 6533 */
6cca3195
VS
6534 if (!IS_CHERRYVIEW(dev_priv) &&
6535 max_pixclk > freq_320*limit/100)
dfcab17e 6536 return 400000;
6cca3195 6537 else if (max_pixclk > 266667*limit/100)
29dc7ef3 6538 return freq_320;
e37c67a1 6539 else if (max_pixclk > 0)
dfcab17e 6540 return 266667;
e37c67a1
VS
6541 else
6542 return 200000;
30a970c6
JB
6543}
6544
89b3c3c7
ACO
6545static int glk_calc_cdclk(int max_pixclk)
6546{
09d09386 6547 if (max_pixclk > 2 * 158400)
89b3c3c7 6548 return 316800;
09d09386 6549 else if (max_pixclk > 2 * 79200)
89b3c3c7
ACO
6550 return 158400;
6551 else
6552 return 79200;
6553}
6554
324513c0 6555static int bxt_calc_cdclk(int max_pixclk)
f8437dd1 6556{
760e1477 6557 if (max_pixclk > 576000)
f8437dd1 6558 return 624000;
760e1477 6559 else if (max_pixclk > 384000)
f8437dd1 6560 return 576000;
760e1477 6561 else if (max_pixclk > 288000)
f8437dd1 6562 return 384000;
760e1477 6563 else if (max_pixclk > 144000)
f8437dd1
VK
6564 return 288000;
6565 else
6566 return 144000;
6567}
6568
e8788cbc 6569/* Compute the max pixel clock for new configuration. */
a821fc46
ACO
6570static int intel_mode_max_pixclk(struct drm_device *dev,
6571 struct drm_atomic_state *state)
30a970c6 6572{
565602d7 6573 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 6574 struct drm_i915_private *dev_priv = to_i915(dev);
565602d7
ML
6575 struct drm_crtc *crtc;
6576 struct drm_crtc_state *crtc_state;
6577 unsigned max_pixclk = 0, i;
6578 enum pipe pipe;
30a970c6 6579
565602d7
ML
6580 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
6581 sizeof(intel_state->min_pixclk));
304603f4 6582
565602d7
ML
6583 for_each_crtc_in_state(state, crtc, crtc_state, i) {
6584 int pixclk = 0;
6585
6586 if (crtc_state->enable)
6587 pixclk = crtc_state->adjusted_mode.crtc_clock;
304603f4 6588
565602d7 6589 intel_state->min_pixclk[i] = pixclk;
30a970c6
JB
6590 }
6591
565602d7
ML
6592 for_each_pipe(dev_priv, pipe)
6593 max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
6594
30a970c6
JB
6595 return max_pixclk;
6596}
6597
27c329ed 6598static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
30a970c6 6599{
27c329ed 6600 struct drm_device *dev = state->dev;
fac5e23e 6601 struct drm_i915_private *dev_priv = to_i915(dev);
27c329ed 6602 int max_pixclk = intel_mode_max_pixclk(dev, state);
1a617b77
ML
6603 struct intel_atomic_state *intel_state =
6604 to_intel_atomic_state(state);
30a970c6 6605
1a617b77 6606 intel_state->cdclk = intel_state->dev_cdclk =
27c329ed 6607 valleyview_calc_cdclk(dev_priv, max_pixclk);
0a9ab303 6608
1a617b77
ML
6609 if (!intel_state->active_crtcs)
6610 intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
6611
27c329ed
ML
6612 return 0;
6613}
304603f4 6614
324513c0 6615static int bxt_modeset_calc_cdclk(struct drm_atomic_state *state)
27c329ed 6616{
89b3c3c7 6617 struct drm_i915_private *dev_priv = to_i915(state->dev);
4e5ca60f 6618 int max_pixclk = ilk_max_pixel_rate(state);
1a617b77
ML
6619 struct intel_atomic_state *intel_state =
6620 to_intel_atomic_state(state);
89b3c3c7 6621 int cdclk;
85a96e7a 6622
89b3c3c7
ACO
6623 if (IS_GEMINILAKE(dev_priv))
6624 cdclk = glk_calc_cdclk(max_pixclk);
6625 else
6626 cdclk = bxt_calc_cdclk(max_pixclk);
85a96e7a 6627
89b3c3c7
ACO
6628 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
6629
6630 if (!intel_state->active_crtcs) {
6631 if (IS_GEMINILAKE(dev_priv))
6632 cdclk = glk_calc_cdclk(0);
6633 else
6634 cdclk = bxt_calc_cdclk(0);
6635
6636 intel_state->dev_cdclk = cdclk;
6637 }
1a617b77 6638
27c329ed 6639 return 0;
30a970c6
JB
6640}
6641
1e69cd74
VS
6642static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
6643{
6644 unsigned int credits, default_credits;
6645
6646 if (IS_CHERRYVIEW(dev_priv))
6647 default_credits = PFI_CREDIT(12);
6648 else
6649 default_credits = PFI_CREDIT(8);
6650
bfa7df01 6651 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
1e69cd74
VS
6652 /* CHV suggested value is 31 or 63 */
6653 if (IS_CHERRYVIEW(dev_priv))
fcc0008f 6654 credits = PFI_CREDIT_63;
1e69cd74
VS
6655 else
6656 credits = PFI_CREDIT(15);
6657 } else {
6658 credits = default_credits;
6659 }
6660
6661 /*
6662 * WA - write default credits before re-programming
6663 * FIXME: should we also set the resend bit here?
6664 */
6665 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6666 default_credits);
6667
6668 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6669 credits | PFI_CREDIT_RESEND);
6670
6671 /*
6672 * FIXME is this guaranteed to clear
6673 * immediately or should we poll for it?
6674 */
6675 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6676}
6677
27c329ed 6678static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
30a970c6 6679{
a821fc46 6680 struct drm_device *dev = old_state->dev;
fac5e23e 6681 struct drm_i915_private *dev_priv = to_i915(dev);
1a617b77
ML
6682 struct intel_atomic_state *old_intel_state =
6683 to_intel_atomic_state(old_state);
6684 unsigned req_cdclk = old_intel_state->dev_cdclk;
30a970c6 6685
27c329ed
ML
6686 /*
6687 * FIXME: We can end up here with all power domains off, yet
6688 * with a CDCLK frequency other than the minimum. To account
6689 * for this take the PIPE-A power domain, which covers the HW
6690 * blocks needed for the following programming. This can be
6691 * removed once it's guaranteed that we get here either with
6692 * the minimum CDCLK set, or the required power domains
6693 * enabled.
6694 */
6695 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
738c05c0 6696
920a14b2 6697 if (IS_CHERRYVIEW(dev_priv))
27c329ed
ML
6698 cherryview_set_cdclk(dev, req_cdclk);
6699 else
6700 valleyview_set_cdclk(dev, req_cdclk);
738c05c0 6701
27c329ed 6702 vlv_program_pfi_credits(dev_priv);
1e69cd74 6703
27c329ed 6704 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
30a970c6
JB
6705}
6706
4a806558
ML
6707static void valleyview_crtc_enable(struct intel_crtc_state *pipe_config,
6708 struct drm_atomic_state *old_state)
89b667f8 6709{
4a806558 6710 struct drm_crtc *crtc = pipe_config->base.crtc;
89b667f8 6711 struct drm_device *dev = crtc->dev;
a72e4c9f 6712 struct drm_i915_private *dev_priv = to_i915(dev);
89b667f8 6713 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
89b667f8 6714 int pipe = intel_crtc->pipe;
89b667f8 6715
53d9f4e9 6716 if (WARN_ON(intel_crtc->active))
89b667f8
JB
6717 return;
6718
37a5650b 6719 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 6720 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
6721
6722 intel_set_pipe_timings(intel_crtc);
bc58be60 6723 intel_set_pipe_src_size(intel_crtc);
5b18e57c 6724
920a14b2 6725 if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
fac5e23e 6726 struct drm_i915_private *dev_priv = to_i915(dev);
c14b0485
VS
6727
6728 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6729 I915_WRITE(CHV_CANVAS(pipe), 0);
6730 }
6731
5b18e57c
DV
6732 i9xx_set_pipeconf(intel_crtc);
6733
89b667f8 6734 intel_crtc->active = true;
89b667f8 6735
a72e4c9f 6736 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6737
fd6bbda9 6738 intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
89b667f8 6739
920a14b2 6740 if (IS_CHERRYVIEW(dev_priv)) {
cd2d34d9
VS
6741 chv_prepare_pll(intel_crtc, intel_crtc->config);
6742 chv_enable_pll(intel_crtc, intel_crtc->config);
6743 } else {
6744 vlv_prepare_pll(intel_crtc, intel_crtc->config);
6745 vlv_enable_pll(intel_crtc, intel_crtc->config);
9d556c99 6746 }
89b667f8 6747
fd6bbda9 6748 intel_encoders_pre_enable(crtc, pipe_config, old_state);
89b667f8 6749
2dd24552
JB
6750 i9xx_pfit_enable(intel_crtc);
6751
b95c5321 6752 intel_color_load_luts(&pipe_config->base);
63cbb074 6753
432081bc 6754 intel_update_watermarks(intel_crtc);
e1fdc473 6755 intel_enable_pipe(intel_crtc);
be6a6f8e 6756
4b3a9526
VS
6757 assert_vblank_disabled(crtc);
6758 drm_crtc_vblank_on(crtc);
6759
fd6bbda9 6760 intel_encoders_enable(crtc, pipe_config, old_state);
89b667f8
JB
6761}
6762
f13c2ef3
DV
6763static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6764{
6765 struct drm_device *dev = crtc->base.dev;
fac5e23e 6766 struct drm_i915_private *dev_priv = to_i915(dev);
f13c2ef3 6767
6e3c9717
ACO
6768 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6769 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
f13c2ef3
DV
6770}
6771
4a806558
ML
6772static void i9xx_crtc_enable(struct intel_crtc_state *pipe_config,
6773 struct drm_atomic_state *old_state)
79e53945 6774{
4a806558 6775 struct drm_crtc *crtc = pipe_config->base.crtc;
79e53945 6776 struct drm_device *dev = crtc->dev;
a72e4c9f 6777 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 6778 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cd2d34d9 6779 enum pipe pipe = intel_crtc->pipe;
79e53945 6780
53d9f4e9 6781 if (WARN_ON(intel_crtc->active))
f7abfe8b
CW
6782 return;
6783
f13c2ef3
DV
6784 i9xx_set_pll_dividers(intel_crtc);
6785
37a5650b 6786 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 6787 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
6788
6789 intel_set_pipe_timings(intel_crtc);
bc58be60 6790 intel_set_pipe_src_size(intel_crtc);
5b18e57c 6791
5b18e57c
DV
6792 i9xx_set_pipeconf(intel_crtc);
6793
f7abfe8b 6794 intel_crtc->active = true;
6b383a7f 6795
5db94019 6796 if (!IS_GEN2(dev_priv))
a72e4c9f 6797 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6798
fd6bbda9 6799 intel_encoders_pre_enable(crtc, pipe_config, old_state);
9d6d9f19 6800
f6736a1a
DV
6801 i9xx_enable_pll(intel_crtc);
6802
2dd24552
JB
6803 i9xx_pfit_enable(intel_crtc);
6804
b95c5321 6805 intel_color_load_luts(&pipe_config->base);
63cbb074 6806
432081bc 6807 intel_update_watermarks(intel_crtc);
e1fdc473 6808 intel_enable_pipe(intel_crtc);
be6a6f8e 6809
4b3a9526
VS
6810 assert_vblank_disabled(crtc);
6811 drm_crtc_vblank_on(crtc);
6812
fd6bbda9 6813 intel_encoders_enable(crtc, pipe_config, old_state);
0b8765c6 6814}
79e53945 6815
87476d63
DV
6816static void i9xx_pfit_disable(struct intel_crtc *crtc)
6817{
6818 struct drm_device *dev = crtc->base.dev;
fac5e23e 6819 struct drm_i915_private *dev_priv = to_i915(dev);
87476d63 6820
6e3c9717 6821 if (!crtc->config->gmch_pfit.control)
328d8e82 6822 return;
87476d63 6823
328d8e82 6824 assert_pipe_disabled(dev_priv, crtc->pipe);
87476d63 6825
328d8e82
DV
6826 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6827 I915_READ(PFIT_CONTROL));
6828 I915_WRITE(PFIT_CONTROL, 0);
87476d63
DV
6829}
6830
4a806558
ML
6831static void i9xx_crtc_disable(struct intel_crtc_state *old_crtc_state,
6832 struct drm_atomic_state *old_state)
0b8765c6 6833{
4a806558 6834 struct drm_crtc *crtc = old_crtc_state->base.crtc;
0b8765c6 6835 struct drm_device *dev = crtc->dev;
fac5e23e 6836 struct drm_i915_private *dev_priv = to_i915(dev);
0b8765c6
JB
6837 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6838 int pipe = intel_crtc->pipe;
ef9c3aee 6839
6304cd91
VS
6840 /*
6841 * On gen2 planes are double buffered but the pipe isn't, so we must
6842 * wait for planes to fully turn off before disabling the pipe.
6843 */
5db94019 6844 if (IS_GEN2(dev_priv))
0f0f74bc 6845 intel_wait_for_vblank(dev_priv, pipe);
6304cd91 6846
fd6bbda9 6847 intel_encoders_disable(crtc, old_crtc_state, old_state);
4b3a9526 6848
f9b61ff6
DV
6849 drm_crtc_vblank_off(crtc);
6850 assert_vblank_disabled(crtc);
6851
575f7ab7 6852 intel_disable_pipe(intel_crtc);
24a1f16d 6853
87476d63 6854 i9xx_pfit_disable(intel_crtc);
24a1f16d 6855
fd6bbda9 6856 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
89b667f8 6857
d7edc4e5 6858 if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
920a14b2 6859 if (IS_CHERRYVIEW(dev_priv))
076ed3b2 6860 chv_disable_pll(dev_priv, pipe);
11a914c2 6861 else if (IS_VALLEYVIEW(dev_priv))
076ed3b2
CML
6862 vlv_disable_pll(dev_priv, pipe);
6863 else
1c4e0274 6864 i9xx_disable_pll(intel_crtc);
076ed3b2 6865 }
0b8765c6 6866
fd6bbda9 6867 intel_encoders_post_pll_disable(crtc, old_crtc_state, old_state);
d6db995f 6868
5db94019 6869 if (!IS_GEN2(dev_priv))
a72e4c9f 6870 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
0b8765c6
JB
6871}
6872
b17d48e2
ML
6873static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
6874{
842e0307 6875 struct intel_encoder *encoder;
b17d48e2
ML
6876 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6877 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6878 enum intel_display_power_domain domain;
6879 unsigned long domains;
4a806558
ML
6880 struct drm_atomic_state *state;
6881 struct intel_crtc_state *crtc_state;
6882 int ret;
b17d48e2
ML
6883
6884 if (!intel_crtc->active)
6885 return;
6886
1d4258db 6887 if (crtc->primary->state->visible) {
5a21b665 6888 WARN_ON(intel_crtc->flip_work);
fc32b1fd 6889
2622a081 6890 intel_pre_disable_primary_noatomic(crtc);
54a41961
ML
6891
6892 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
1d4258db 6893 crtc->primary->state->visible = false;
a539205a
ML
6894 }
6895
4a806558 6896 state = drm_atomic_state_alloc(crtc->dev);
31bb2ef9
ACO
6897 if (!state) {
6898 DRM_DEBUG_KMS("failed to disable [CRTC:%d:%s], out of memory",
6899 crtc->base.id, crtc->name);
6900 return;
6901 }
6902
4a806558
ML
6903 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
6904
6905 /* Everything's already locked, -EDEADLK can't happen. */
6906 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
6907 ret = drm_atomic_add_affected_connectors(state, crtc);
6908
6909 WARN_ON(IS_ERR(crtc_state) || ret);
6910
6911 dev_priv->display.crtc_disable(crtc_state, state);
6912
0853695c 6913 drm_atomic_state_put(state);
842e0307 6914
78108b7c
VS
6915 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
6916 crtc->base.id, crtc->name);
842e0307
ML
6917
6918 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6919 crtc->state->active = false;
37d9078b 6920 intel_crtc->active = false;
842e0307
ML
6921 crtc->enabled = false;
6922 crtc->state->connector_mask = 0;
6923 crtc->state->encoder_mask = 0;
6924
6925 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6926 encoder->base.crtc = NULL;
6927
58f9c0bc 6928 intel_fbc_disable(intel_crtc);
432081bc 6929 intel_update_watermarks(intel_crtc);
1f7457b1 6930 intel_disable_shared_dpll(intel_crtc);
b17d48e2
ML
6931
6932 domains = intel_crtc->enabled_power_domains;
6933 for_each_power_domain(domain, domains)
6934 intel_display_power_put(dev_priv, domain);
6935 intel_crtc->enabled_power_domains = 0;
565602d7
ML
6936
6937 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
6938 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
b17d48e2
ML
6939}
6940
6b72d486
ML
6941/*
6942 * turn all crtc's off, but do not adjust state
6943 * This has to be paired with a call to intel_modeset_setup_hw_state.
6944 */
70e0bd74 6945int intel_display_suspend(struct drm_device *dev)
ee7b9f93 6946{
e2c8b870 6947 struct drm_i915_private *dev_priv = to_i915(dev);
70e0bd74 6948 struct drm_atomic_state *state;
e2c8b870 6949 int ret;
70e0bd74 6950
e2c8b870
ML
6951 state = drm_atomic_helper_suspend(dev);
6952 ret = PTR_ERR_OR_ZERO(state);
70e0bd74
ML
6953 if (ret)
6954 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
e2c8b870
ML
6955 else
6956 dev_priv->modeset_restore_state = state;
70e0bd74 6957 return ret;
ee7b9f93
JB
6958}
6959
ea5b213a 6960void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 6961{
4ef69c7a 6962 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 6963
ea5b213a
CW
6964 drm_encoder_cleanup(encoder);
6965 kfree(intel_encoder);
7e7d76c3
JB
6966}
6967
0a91ca29
DV
6968/* Cross check the actual hw state with our own modeset state tracking (and it's
6969 * internal consistency). */
5a21b665 6970static void intel_connector_verify_state(struct intel_connector *connector)
79e53945 6971{
5a21b665 6972 struct drm_crtc *crtc = connector->base.state->crtc;
35dd3c64
ML
6973
6974 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6975 connector->base.base.id,
6976 connector->base.name);
6977
0a91ca29 6978 if (connector->get_hw_state(connector)) {
e85376cb 6979 struct intel_encoder *encoder = connector->encoder;
5a21b665 6980 struct drm_connector_state *conn_state = connector->base.state;
0a91ca29 6981
35dd3c64
ML
6982 I915_STATE_WARN(!crtc,
6983 "connector enabled without attached crtc\n");
0a91ca29 6984
35dd3c64
ML
6985 if (!crtc)
6986 return;
6987
6988 I915_STATE_WARN(!crtc->state->active,
6989 "connector is active, but attached crtc isn't\n");
6990
e85376cb 6991 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
35dd3c64
ML
6992 return;
6993
e85376cb 6994 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
35dd3c64
ML
6995 "atomic encoder doesn't match attached encoder\n");
6996
e85376cb 6997 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
35dd3c64
ML
6998 "attached encoder crtc differs from connector crtc\n");
6999 } else {
4d688a2a
ML
7000 I915_STATE_WARN(crtc && crtc->state->active,
7001 "attached crtc is active, but connector isn't\n");
5a21b665 7002 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
35dd3c64 7003 "best encoder set without crtc!\n");
0a91ca29 7004 }
79e53945
JB
7005}
7006
08d9bc92
ACO
7007int intel_connector_init(struct intel_connector *connector)
7008{
5350a031 7009 drm_atomic_helper_connector_reset(&connector->base);
08d9bc92 7010
5350a031 7011 if (!connector->base.state)
08d9bc92
ACO
7012 return -ENOMEM;
7013
08d9bc92
ACO
7014 return 0;
7015}
7016
7017struct intel_connector *intel_connector_alloc(void)
7018{
7019 struct intel_connector *connector;
7020
7021 connector = kzalloc(sizeof *connector, GFP_KERNEL);
7022 if (!connector)
7023 return NULL;
7024
7025 if (intel_connector_init(connector) < 0) {
7026 kfree(connector);
7027 return NULL;
7028 }
7029
7030 return connector;
7031}
7032
f0947c37
DV
7033/* Simple connector->get_hw_state implementation for encoders that support only
7034 * one connector and no cloning and hence the encoder state determines the state
7035 * of the connector. */
7036bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 7037{
24929352 7038 enum pipe pipe = 0;
f0947c37 7039 struct intel_encoder *encoder = connector->encoder;
ea5b213a 7040
f0947c37 7041 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
7042}
7043
6d293983 7044static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
d272ddfa 7045{
6d293983
ACO
7046 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
7047 return crtc_state->fdi_lanes;
d272ddfa
VS
7048
7049 return 0;
7050}
7051
6d293983 7052static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5cec258b 7053 struct intel_crtc_state *pipe_config)
1857e1da 7054{
8652744b 7055 struct drm_i915_private *dev_priv = to_i915(dev);
6d293983
ACO
7056 struct drm_atomic_state *state = pipe_config->base.state;
7057 struct intel_crtc *other_crtc;
7058 struct intel_crtc_state *other_crtc_state;
7059
1857e1da
DV
7060 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
7061 pipe_name(pipe), pipe_config->fdi_lanes);
7062 if (pipe_config->fdi_lanes > 4) {
7063 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
7064 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 7065 return -EINVAL;
1857e1da
DV
7066 }
7067
8652744b 7068 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
1857e1da
DV
7069 if (pipe_config->fdi_lanes > 2) {
7070 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
7071 pipe_config->fdi_lanes);
6d293983 7072 return -EINVAL;
1857e1da 7073 } else {
6d293983 7074 return 0;
1857e1da
DV
7075 }
7076 }
7077
b7f05d4a 7078 if (INTEL_INFO(dev_priv)->num_pipes == 2)
6d293983 7079 return 0;
1857e1da
DV
7080
7081 /* Ivybridge 3 pipe is really complicated */
7082 switch (pipe) {
7083 case PIPE_A:
6d293983 7084 return 0;
1857e1da 7085 case PIPE_B:
6d293983
ACO
7086 if (pipe_config->fdi_lanes <= 2)
7087 return 0;
7088
b91eb5cc 7089 other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
6d293983
ACO
7090 other_crtc_state =
7091 intel_atomic_get_crtc_state(state, other_crtc);
7092 if (IS_ERR(other_crtc_state))
7093 return PTR_ERR(other_crtc_state);
7094
7095 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
1857e1da
DV
7096 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
7097 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 7098 return -EINVAL;
1857e1da 7099 }
6d293983 7100 return 0;
1857e1da 7101 case PIPE_C:
251cc67c
VS
7102 if (pipe_config->fdi_lanes > 2) {
7103 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
7104 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 7105 return -EINVAL;
251cc67c 7106 }
6d293983 7107
b91eb5cc 7108 other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
6d293983
ACO
7109 other_crtc_state =
7110 intel_atomic_get_crtc_state(state, other_crtc);
7111 if (IS_ERR(other_crtc_state))
7112 return PTR_ERR(other_crtc_state);
7113
7114 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
1857e1da 7115 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
6d293983 7116 return -EINVAL;
1857e1da 7117 }
6d293983 7118 return 0;
1857e1da
DV
7119 default:
7120 BUG();
7121 }
7122}
7123
e29c22c0
DV
7124#define RETRY 1
7125static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5cec258b 7126 struct intel_crtc_state *pipe_config)
877d48d5 7127{
1857e1da 7128 struct drm_device *dev = intel_crtc->base.dev;
7c5f93b0 7129 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6d293983
ACO
7130 int lane, link_bw, fdi_dotclock, ret;
7131 bool needs_recompute = false;
877d48d5 7132
e29c22c0 7133retry:
877d48d5
DV
7134 /* FDI is a binary signal running at ~2.7GHz, encoding
7135 * each output octet as 10 bits. The actual frequency
7136 * is stored as a divider into a 100MHz clock, and the
7137 * mode pixel clock is stored in units of 1KHz.
7138 * Hence the bw of each lane in terms of the mode signal
7139 * is:
7140 */
21a727b3 7141 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
877d48d5 7142
241bfc38 7143 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 7144
2bd89a07 7145 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
DV
7146 pipe_config->pipe_bpp);
7147
7148 pipe_config->fdi_lanes = lane;
7149
2bd89a07 7150 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
877d48d5 7151 link_bw, &pipe_config->fdi_m_n);
1857e1da 7152
e3b247da 7153 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
6d293983 7154 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
e29c22c0
DV
7155 pipe_config->pipe_bpp -= 2*3;
7156 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
7157 pipe_config->pipe_bpp);
7158 needs_recompute = true;
7159 pipe_config->bw_constrained = true;
7160
7161 goto retry;
7162 }
7163
7164 if (needs_recompute)
7165 return RETRY;
7166
6d293983 7167 return ret;
877d48d5
DV
7168}
7169
8cfb3407
VS
7170static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
7171 struct intel_crtc_state *pipe_config)
7172{
7173 if (pipe_config->pipe_bpp > 24)
7174 return false;
7175
7176 /* HSW can handle pixel rate up to cdclk? */
2d1fe073 7177 if (IS_HASWELL(dev_priv))
8cfb3407
VS
7178 return true;
7179
7180 /*
b432e5cf
VS
7181 * We compare against max which means we must take
7182 * the increased cdclk requirement into account when
7183 * calculating the new cdclk.
7184 *
7185 * Should measure whether using a lower cdclk w/o IPS
8cfb3407
VS
7186 */
7187 return ilk_pipe_pixel_rate(pipe_config) <=
7188 dev_priv->max_cdclk_freq * 95 / 100;
7189}
7190
42db64ef 7191static void hsw_compute_ips_config(struct intel_crtc *crtc,
5cec258b 7192 struct intel_crtc_state *pipe_config)
42db64ef 7193{
8cfb3407 7194 struct drm_device *dev = crtc->base.dev;
fac5e23e 7195 struct drm_i915_private *dev_priv = to_i915(dev);
8cfb3407 7196
d330a953 7197 pipe_config->ips_enabled = i915.enable_ips &&
8cfb3407
VS
7198 hsw_crtc_supports_ips(crtc) &&
7199 pipe_config_supports_ips(dev_priv, pipe_config);
42db64ef
PZ
7200}
7201
39acb4aa
VS
7202static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
7203{
7204 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7205
7206 /* GDG double wide on either pipe, otherwise pipe A only */
7207 return INTEL_INFO(dev_priv)->gen < 4 &&
7208 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
7209}
7210
a43f6e0f 7211static int intel_crtc_compute_config(struct intel_crtc *crtc,
5cec258b 7212 struct intel_crtc_state *pipe_config)
79e53945 7213{
a43f6e0f 7214 struct drm_device *dev = crtc->base.dev;
fac5e23e 7215 struct drm_i915_private *dev_priv = to_i915(dev);
7c5f93b0 7216 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
f3261156 7217 int clock_limit = dev_priv->max_dotclk_freq;
89749350 7218
6315b5d3 7219 if (INTEL_GEN(dev_priv) < 4) {
f3261156 7220 clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
cf532bb2
VS
7221
7222 /*
39acb4aa 7223 * Enable double wide mode when the dot clock
cf532bb2 7224 * is > 90% of the (display) core speed.
cf532bb2 7225 */
39acb4aa
VS
7226 if (intel_crtc_supports_double_wide(crtc) &&
7227 adjusted_mode->crtc_clock > clock_limit) {
f3261156 7228 clock_limit = dev_priv->max_dotclk_freq;
cf532bb2 7229 pipe_config->double_wide = true;
ad3a4479 7230 }
f3261156 7231 }
ad3a4479 7232
f3261156
VS
7233 if (adjusted_mode->crtc_clock > clock_limit) {
7234 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
7235 adjusted_mode->crtc_clock, clock_limit,
7236 yesno(pipe_config->double_wide));
7237 return -EINVAL;
2c07245f 7238 }
89749350 7239
1d1d0e27
VS
7240 /*
7241 * Pipe horizontal size must be even in:
7242 * - DVO ganged mode
7243 * - LVDS dual channel mode
7244 * - Double wide pipe
7245 */
2d84d2b3 7246 if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
1d1d0e27
VS
7247 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
7248 pipe_config->pipe_src_w &= ~1;
7249
8693a824
DL
7250 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
7251 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
44f46b42 7252 */
9beb5fea 7253 if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
aad941d5 7254 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
e29c22c0 7255 return -EINVAL;
44f46b42 7256
50a0bc90 7257 if (HAS_IPS(dev_priv))
a43f6e0f
DV
7258 hsw_compute_ips_config(crtc, pipe_config);
7259
877d48d5 7260 if (pipe_config->has_pch_encoder)
a43f6e0f 7261 return ironlake_fdi_compute_config(crtc, pipe_config);
877d48d5 7262
cf5a15be 7263 return 0;
79e53945
JB
7264}
7265
1353c4fb 7266static int skylake_get_display_clock_speed(struct drm_i915_private *dev_priv)
1652d19e 7267{
1353c4fb 7268 u32 cdctl;
1652d19e 7269
ea61791e 7270 skl_dpll0_update(dev_priv);
1652d19e 7271
63911d72 7272 if (dev_priv->cdclk_pll.vco == 0)
709e05c3 7273 return dev_priv->cdclk_pll.ref;
1652d19e 7274
ea61791e 7275 cdctl = I915_READ(CDCLK_CTL);
1652d19e 7276
63911d72 7277 if (dev_priv->cdclk_pll.vco == 8640000) {
1652d19e
VS
7278 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
7279 case CDCLK_FREQ_450_432:
7280 return 432000;
7281 case CDCLK_FREQ_337_308:
487ed2e4 7282 return 308571;
ea61791e
VS
7283 case CDCLK_FREQ_540:
7284 return 540000;
1652d19e 7285 case CDCLK_FREQ_675_617:
487ed2e4 7286 return 617143;
1652d19e 7287 default:
ea61791e 7288 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
1652d19e
VS
7289 }
7290 } else {
1652d19e
VS
7291 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
7292 case CDCLK_FREQ_450_432:
7293 return 450000;
7294 case CDCLK_FREQ_337_308:
7295 return 337500;
ea61791e
VS
7296 case CDCLK_FREQ_540:
7297 return 540000;
1652d19e
VS
7298 case CDCLK_FREQ_675_617:
7299 return 675000;
7300 default:
ea61791e 7301 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
1652d19e
VS
7302 }
7303 }
7304
709e05c3 7305 return dev_priv->cdclk_pll.ref;
1652d19e
VS
7306}
7307
83d7c81f
VS
7308static void bxt_de_pll_update(struct drm_i915_private *dev_priv)
7309{
7310 u32 val;
7311
7312 dev_priv->cdclk_pll.ref = 19200;
1c3f7700 7313 dev_priv->cdclk_pll.vco = 0;
83d7c81f
VS
7314
7315 val = I915_READ(BXT_DE_PLL_ENABLE);
1c3f7700 7316 if ((val & BXT_DE_PLL_PLL_ENABLE) == 0)
83d7c81f 7317 return;
83d7c81f 7318
1c3f7700
ID
7319 if (WARN_ON((val & BXT_DE_PLL_LOCK) == 0))
7320 return;
83d7c81f
VS
7321
7322 val = I915_READ(BXT_DE_PLL_CTL);
7323 dev_priv->cdclk_pll.vco = (val & BXT_DE_PLL_RATIO_MASK) *
7324 dev_priv->cdclk_pll.ref;
7325}
7326
1353c4fb 7327static int broxton_get_display_clock_speed(struct drm_i915_private *dev_priv)
acd3f3d3 7328{
f5986242
VS
7329 u32 divider;
7330 int div, vco;
acd3f3d3 7331
83d7c81f
VS
7332 bxt_de_pll_update(dev_priv);
7333
f5986242
VS
7334 vco = dev_priv->cdclk_pll.vco;
7335 if (vco == 0)
7336 return dev_priv->cdclk_pll.ref;
acd3f3d3 7337
f5986242 7338 divider = I915_READ(CDCLK_CTL) & BXT_CDCLK_CD2X_DIV_SEL_MASK;
acd3f3d3 7339
f5986242 7340 switch (divider) {
acd3f3d3 7341 case BXT_CDCLK_CD2X_DIV_SEL_1:
f5986242
VS
7342 div = 2;
7343 break;
acd3f3d3 7344 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
89b3c3c7 7345 WARN(IS_GEMINILAKE(dev_priv), "Unsupported divider\n");
f5986242
VS
7346 div = 3;
7347 break;
acd3f3d3 7348 case BXT_CDCLK_CD2X_DIV_SEL_2:
f5986242
VS
7349 div = 4;
7350 break;
acd3f3d3 7351 case BXT_CDCLK_CD2X_DIV_SEL_4:
f5986242
VS
7352 div = 8;
7353 break;
7354 default:
7355 MISSING_CASE(divider);
7356 return dev_priv->cdclk_pll.ref;
acd3f3d3
BP
7357 }
7358
f5986242 7359 return DIV_ROUND_CLOSEST(vco, div);
acd3f3d3
BP
7360}
7361
1353c4fb 7362static int broadwell_get_display_clock_speed(struct drm_i915_private *dev_priv)
1652d19e 7363{
1652d19e
VS
7364 uint32_t lcpll = I915_READ(LCPLL_CTL);
7365 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
7366
7367 if (lcpll & LCPLL_CD_SOURCE_FCLK)
7368 return 800000;
7369 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
7370 return 450000;
7371 else if (freq == LCPLL_CLK_FREQ_450)
7372 return 450000;
7373 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
7374 return 540000;
7375 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
7376 return 337500;
7377 else
7378 return 675000;
7379}
7380
1353c4fb 7381static int haswell_get_display_clock_speed(struct drm_i915_private *dev_priv)
1652d19e 7382{
1652d19e
VS
7383 uint32_t lcpll = I915_READ(LCPLL_CTL);
7384 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
7385
7386 if (lcpll & LCPLL_CD_SOURCE_FCLK)
7387 return 800000;
7388 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
7389 return 450000;
7390 else if (freq == LCPLL_CLK_FREQ_450)
7391 return 450000;
50a0bc90 7392 else if (IS_HSW_ULT(dev_priv))
1652d19e
VS
7393 return 337500;
7394 else
7395 return 540000;
79e53945
JB
7396}
7397
1353c4fb 7398static int valleyview_get_display_clock_speed(struct drm_i915_private *dev_priv)
25eb05fc 7399{
1353c4fb 7400 return vlv_get_cck_clock_hpll(dev_priv, "cdclk",
bfa7df01 7401 CCK_DISPLAY_CLOCK_CONTROL);
25eb05fc
JB
7402}
7403
1353c4fb 7404static int ilk_get_display_clock_speed(struct drm_i915_private *dev_priv)
b37a6434
VS
7405{
7406 return 450000;
7407}
7408
1353c4fb 7409static int i945_get_display_clock_speed(struct drm_i915_private *dev_priv)
e70236a8
JB
7410{
7411 return 400000;
7412}
79e53945 7413
6248017a
AH
7414static int i945gm_get_display_clock_speed(struct drm_i915_private *dev_priv)
7415{
7416 struct pci_dev *pdev = dev_priv->drm.pdev;
7417 u16 gcfgc = 0;
7418
7419 pci_read_config_word(pdev, GCFGC, &gcfgc);
7420
7421 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
7422 return 133333;
7423 else {
7424 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
7425 case GC_DISPLAY_CLOCK_333_320_MHZ:
7426 return 320000;
7427 default:
7428 case GC_DISPLAY_CLOCK_190_200_MHZ:
7429 return 200000;
7430 }
7431 }
7432}
7433
1353c4fb 7434static int i915_get_display_clock_speed(struct drm_i915_private *dev_priv)
79e53945 7435{
e907f170 7436 return 333333;
e70236a8 7437}
79e53945 7438
1353c4fb 7439static int i9xx_misc_get_display_clock_speed(struct drm_i915_private *dev_priv)
e70236a8
JB
7440{
7441 return 200000;
7442}
79e53945 7443
1353c4fb 7444static int pnv_get_display_clock_speed(struct drm_i915_private *dev_priv)
257a7ffc 7445{
1353c4fb 7446 struct pci_dev *pdev = dev_priv->drm.pdev;
257a7ffc
DV
7447 u16 gcfgc = 0;
7448
52a05c30 7449 pci_read_config_word(pdev, GCFGC, &gcfgc);
257a7ffc
DV
7450
7451 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
7452 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
e907f170 7453 return 266667;
257a7ffc 7454 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
e907f170 7455 return 333333;
257a7ffc 7456 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
e907f170 7457 return 444444;
257a7ffc
DV
7458 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
7459 return 200000;
7460 default:
7461 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
7462 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
e907f170 7463 return 133333;
257a7ffc 7464 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
e907f170 7465 return 166667;
257a7ffc
DV
7466 }
7467}
7468
1353c4fb 7469static int i915gm_get_display_clock_speed(struct drm_i915_private *dev_priv)
e70236a8 7470{
1353c4fb 7471 struct pci_dev *pdev = dev_priv->drm.pdev;
e70236a8 7472 u16 gcfgc = 0;
79e53945 7473
52a05c30 7474 pci_read_config_word(pdev, GCFGC, &gcfgc);
e70236a8
JB
7475
7476 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
e907f170 7477 return 133333;
e70236a8
JB
7478 else {
7479 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6248017a 7480 case GC_DISPLAY_CLOCK_333_320_MHZ:
e907f170 7481 return 333333;
e70236a8
JB
7482 default:
7483 case GC_DISPLAY_CLOCK_190_200_MHZ:
7484 return 190000;
79e53945 7485 }
e70236a8
JB
7486 }
7487}
7488
1353c4fb 7489static int i865_get_display_clock_speed(struct drm_i915_private *dev_priv)
e70236a8 7490{
e907f170 7491 return 266667;
e70236a8
JB
7492}
7493
1353c4fb 7494static int i85x_get_display_clock_speed(struct drm_i915_private *dev_priv)
e70236a8 7495{
1353c4fb 7496 struct pci_dev *pdev = dev_priv->drm.pdev;
e70236a8 7497 u16 hpllcc = 0;
1b1d2716 7498
65cd2b3f
VS
7499 /*
7500 * 852GM/852GMV only supports 133 MHz and the HPLLCC
7501 * encoding is different :(
7502 * FIXME is this the right way to detect 852GM/852GMV?
7503 */
52a05c30 7504 if (pdev->revision == 0x1)
65cd2b3f
VS
7505 return 133333;
7506
52a05c30 7507 pci_bus_read_config_word(pdev->bus,
1b1d2716
VS
7508 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
7509
e70236a8
JB
7510 /* Assume that the hardware is in the high speed state. This
7511 * should be the default.
7512 */
7513 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
7514 case GC_CLOCK_133_200:
1b1d2716 7515 case GC_CLOCK_133_200_2:
e70236a8
JB
7516 case GC_CLOCK_100_200:
7517 return 200000;
7518 case GC_CLOCK_166_250:
7519 return 250000;
7520 case GC_CLOCK_100_133:
e907f170 7521 return 133333;
1b1d2716
VS
7522 case GC_CLOCK_133_266:
7523 case GC_CLOCK_133_266_2:
7524 case GC_CLOCK_166_266:
7525 return 266667;
e70236a8 7526 }
79e53945 7527
e70236a8
JB
7528 /* Shouldn't happen */
7529 return 0;
7530}
79e53945 7531
1353c4fb 7532static int i830_get_display_clock_speed(struct drm_i915_private *dev_priv)
e70236a8 7533{
e907f170 7534 return 133333;
79e53945
JB
7535}
7536
1353c4fb 7537static unsigned int intel_hpll_vco(struct drm_i915_private *dev_priv)
34edce2f 7538{
34edce2f
VS
7539 static const unsigned int blb_vco[8] = {
7540 [0] = 3200000,
7541 [1] = 4000000,
7542 [2] = 5333333,
7543 [3] = 4800000,
7544 [4] = 6400000,
7545 };
7546 static const unsigned int pnv_vco[8] = {
7547 [0] = 3200000,
7548 [1] = 4000000,
7549 [2] = 5333333,
7550 [3] = 4800000,
7551 [4] = 2666667,
7552 };
7553 static const unsigned int cl_vco[8] = {
7554 [0] = 3200000,
7555 [1] = 4000000,
7556 [2] = 5333333,
7557 [3] = 6400000,
7558 [4] = 3333333,
7559 [5] = 3566667,
7560 [6] = 4266667,
7561 };
7562 static const unsigned int elk_vco[8] = {
7563 [0] = 3200000,
7564 [1] = 4000000,
7565 [2] = 5333333,
7566 [3] = 4800000,
7567 };
7568 static const unsigned int ctg_vco[8] = {
7569 [0] = 3200000,
7570 [1] = 4000000,
7571 [2] = 5333333,
7572 [3] = 6400000,
7573 [4] = 2666667,
7574 [5] = 4266667,
7575 };
7576 const unsigned int *vco_table;
7577 unsigned int vco;
7578 uint8_t tmp = 0;
7579
7580 /* FIXME other chipsets? */
50a0bc90 7581 if (IS_GM45(dev_priv))
34edce2f 7582 vco_table = ctg_vco;
9beb5fea 7583 else if (IS_G4X(dev_priv))
34edce2f 7584 vco_table = elk_vco;
c0f86832 7585 else if (IS_I965GM(dev_priv))
34edce2f 7586 vco_table = cl_vco;
1353c4fb 7587 else if (IS_PINEVIEW(dev_priv))
34edce2f 7588 vco_table = pnv_vco;
1353c4fb 7589 else if (IS_G33(dev_priv))
34edce2f
VS
7590 vco_table = blb_vco;
7591 else
7592 return 0;
7593
1353c4fb 7594 tmp = I915_READ(IS_MOBILE(dev_priv) ? HPLLVCO_MOBILE : HPLLVCO);
34edce2f
VS
7595
7596 vco = vco_table[tmp & 0x7];
7597 if (vco == 0)
7598 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
7599 else
7600 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
7601
7602 return vco;
7603}
7604
1353c4fb 7605static int gm45_get_display_clock_speed(struct drm_i915_private *dev_priv)
34edce2f 7606{
1353c4fb
VS
7607 struct pci_dev *pdev = dev_priv->drm.pdev;
7608 unsigned int cdclk_sel, vco = intel_hpll_vco(dev_priv);
34edce2f
VS
7609 uint16_t tmp = 0;
7610
52a05c30 7611 pci_read_config_word(pdev, GCFGC, &tmp);
34edce2f
VS
7612
7613 cdclk_sel = (tmp >> 12) & 0x1;
7614
7615 switch (vco) {
7616 case 2666667:
7617 case 4000000:
7618 case 5333333:
7619 return cdclk_sel ? 333333 : 222222;
7620 case 3200000:
7621 return cdclk_sel ? 320000 : 228571;
7622 default:
7623 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
7624 return 222222;
7625 }
7626}
7627
1353c4fb 7628static int i965gm_get_display_clock_speed(struct drm_i915_private *dev_priv)
34edce2f 7629{
1353c4fb 7630 struct pci_dev *pdev = dev_priv->drm.pdev;
34edce2f
VS
7631 static const uint8_t div_3200[] = { 16, 10, 8 };
7632 static const uint8_t div_4000[] = { 20, 12, 10 };
7633 static const uint8_t div_5333[] = { 24, 16, 14 };
7634 const uint8_t *div_table;
1353c4fb 7635 unsigned int cdclk_sel, vco = intel_hpll_vco(dev_priv);
34edce2f
VS
7636 uint16_t tmp = 0;
7637
52a05c30 7638 pci_read_config_word(pdev, GCFGC, &tmp);
34edce2f
VS
7639
7640 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
7641
7642 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7643 goto fail;
7644
7645 switch (vco) {
7646 case 3200000:
7647 div_table = div_3200;
7648 break;
7649 case 4000000:
7650 div_table = div_4000;
7651 break;
7652 case 5333333:
7653 div_table = div_5333;
7654 break;
7655 default:
7656 goto fail;
7657 }
7658
7659 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7660
caf4e252 7661fail:
34edce2f
VS
7662 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
7663 return 200000;
7664}
7665
1353c4fb 7666static int g33_get_display_clock_speed(struct drm_i915_private *dev_priv)
34edce2f 7667{
1353c4fb 7668 struct pci_dev *pdev = dev_priv->drm.pdev;
34edce2f
VS
7669 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
7670 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
7671 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
7672 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
7673 const uint8_t *div_table;
1353c4fb 7674 unsigned int cdclk_sel, vco = intel_hpll_vco(dev_priv);
34edce2f
VS
7675 uint16_t tmp = 0;
7676
52a05c30 7677 pci_read_config_word(pdev, GCFGC, &tmp);
34edce2f
VS
7678
7679 cdclk_sel = (tmp >> 4) & 0x7;
7680
7681 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7682 goto fail;
7683
7684 switch (vco) {
7685 case 3200000:
7686 div_table = div_3200;
7687 break;
7688 case 4000000:
7689 div_table = div_4000;
7690 break;
7691 case 4800000:
7692 div_table = div_4800;
7693 break;
7694 case 5333333:
7695 div_table = div_5333;
7696 break;
7697 default:
7698 goto fail;
7699 }
7700
7701 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7702
caf4e252 7703fail:
34edce2f
VS
7704 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7705 return 190476;
7706}
7707
2c07245f 7708static void
a65851af 7709intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 7710{
a65851af
VS
7711 while (*num > DATA_LINK_M_N_MASK ||
7712 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
7713 *num >>= 1;
7714 *den >>= 1;
7715 }
7716}
7717
a65851af
VS
7718static void compute_m_n(unsigned int m, unsigned int n,
7719 uint32_t *ret_m, uint32_t *ret_n)
7720{
7721 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7722 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7723 intel_reduce_m_n_ratio(ret_m, ret_n);
7724}
7725
e69d0bc1
DV
7726void
7727intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7728 int pixel_clock, int link_clock,
7729 struct intel_link_m_n *m_n)
2c07245f 7730{
e69d0bc1 7731 m_n->tu = 64;
a65851af
VS
7732
7733 compute_m_n(bits_per_pixel * pixel_clock,
7734 link_clock * nlanes * 8,
7735 &m_n->gmch_m, &m_n->gmch_n);
7736
7737 compute_m_n(pixel_clock, link_clock,
7738 &m_n->link_m, &m_n->link_n);
2c07245f
ZW
7739}
7740
a7615030
CW
7741static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7742{
d330a953
JN
7743 if (i915.panel_use_ssc >= 0)
7744 return i915.panel_use_ssc != 0;
41aa3448 7745 return dev_priv->vbt.lvds_use_ssc
435793df 7746 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
7747}
7748
7429e9d4 7749static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 7750{
7df00d7a 7751 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 7752}
f47709a9 7753
7429e9d4
DV
7754static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7755{
7756 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
7757}
7758
f47709a9 7759static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
190f68c5 7760 struct intel_crtc_state *crtc_state,
9e2c8475 7761 struct dpll *reduced_clock)
a7516a05 7762{
9b1e14f4 7763 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
a7516a05
JB
7764 u32 fp, fp2 = 0;
7765
9b1e14f4 7766 if (IS_PINEVIEW(dev_priv)) {
190f68c5 7767 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7768 if (reduced_clock)
7429e9d4 7769 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 7770 } else {
190f68c5 7771 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7772 if (reduced_clock)
7429e9d4 7773 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
7774 }
7775
190f68c5 7776 crtc_state->dpll_hw_state.fp0 = fp;
a7516a05 7777
f47709a9 7778 crtc->lowfreq_avail = false;
2d84d2b3 7779 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ab585dea 7780 reduced_clock) {
190f68c5 7781 crtc_state->dpll_hw_state.fp1 = fp2;
f47709a9 7782 crtc->lowfreq_avail = true;
a7516a05 7783 } else {
190f68c5 7784 crtc_state->dpll_hw_state.fp1 = fp;
a7516a05
JB
7785 }
7786}
7787
5e69f97f
CML
7788static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7789 pipe)
89b667f8
JB
7790{
7791 u32 reg_val;
7792
7793 /*
7794 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7795 * and set it to a reasonable value instead.
7796 */
ab3c759a 7797 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
7798 reg_val &= 0xffffff00;
7799 reg_val |= 0x00000030;
ab3c759a 7800 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7801
ab3c759a 7802 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7803 reg_val &= 0x8cffffff;
7804 reg_val = 0x8c000000;
ab3c759a 7805 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 7806
ab3c759a 7807 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 7808 reg_val &= 0xffffff00;
ab3c759a 7809 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7810
ab3c759a 7811 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7812 reg_val &= 0x00ffffff;
7813 reg_val |= 0xb0000000;
ab3c759a 7814 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
7815}
7816
b551842d
DV
7817static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7818 struct intel_link_m_n *m_n)
7819{
7820 struct drm_device *dev = crtc->base.dev;
fac5e23e 7821 struct drm_i915_private *dev_priv = to_i915(dev);
b551842d
DV
7822 int pipe = crtc->pipe;
7823
e3b95f1e
DV
7824 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7825 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7826 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7827 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
7828}
7829
7830static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
7831 struct intel_link_m_n *m_n,
7832 struct intel_link_m_n *m2_n2)
b551842d 7833{
6315b5d3 7834 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
b551842d 7835 int pipe = crtc->pipe;
6e3c9717 7836 enum transcoder transcoder = crtc->config->cpu_transcoder;
b551842d 7837
6315b5d3 7838 if (INTEL_GEN(dev_priv) >= 5) {
b551842d
DV
7839 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7840 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7841 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7842 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
f769cd24
VK
7843 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7844 * for gen < 8) and if DRRS is supported (to make sure the
7845 * registers are not unnecessarily accessed).
7846 */
920a14b2
TU
7847 if (m2_n2 && (IS_CHERRYVIEW(dev_priv) ||
7848 INTEL_GEN(dev_priv) < 8) && crtc->config->has_drrs) {
f769cd24
VK
7849 I915_WRITE(PIPE_DATA_M2(transcoder),
7850 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7851 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7852 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7853 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7854 }
b551842d 7855 } else {
e3b95f1e
DV
7856 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7857 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7858 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7859 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
7860 }
7861}
7862
fe3cd48d 7863void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
03afc4a2 7864{
fe3cd48d
R
7865 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7866
7867 if (m_n == M1_N1) {
7868 dp_m_n = &crtc->config->dp_m_n;
7869 dp_m2_n2 = &crtc->config->dp_m2_n2;
7870 } else if (m_n == M2_N2) {
7871
7872 /*
7873 * M2_N2 registers are not supported. Hence m2_n2 divider value
7874 * needs to be programmed into M1_N1.
7875 */
7876 dp_m_n = &crtc->config->dp_m2_n2;
7877 } else {
7878 DRM_ERROR("Unsupported divider value\n");
7879 return;
7880 }
7881
6e3c9717
ACO
7882 if (crtc->config->has_pch_encoder)
7883 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
03afc4a2 7884 else
fe3cd48d 7885 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
03afc4a2
DV
7886}
7887
251ac862
DV
7888static void vlv_compute_dpll(struct intel_crtc *crtc,
7889 struct intel_crtc_state *pipe_config)
bdd4b6a6 7890{
03ed5cbf 7891 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
cd2d34d9 7892 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
03ed5cbf
VS
7893 if (crtc->pipe != PIPE_A)
7894 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
bdd4b6a6 7895
cd2d34d9 7896 /* DPLL not used with DSI, but still need the rest set up */
d7edc4e5 7897 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
cd2d34d9
VS
7898 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
7899 DPLL_EXT_BUFFER_ENABLE_VLV;
7900
03ed5cbf
VS
7901 pipe_config->dpll_hw_state.dpll_md =
7902 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
7903}
bdd4b6a6 7904
03ed5cbf
VS
7905static void chv_compute_dpll(struct intel_crtc *crtc,
7906 struct intel_crtc_state *pipe_config)
7907{
7908 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
cd2d34d9 7909 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
03ed5cbf
VS
7910 if (crtc->pipe != PIPE_A)
7911 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7912
cd2d34d9 7913 /* DPLL not used with DSI, but still need the rest set up */
d7edc4e5 7914 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
cd2d34d9
VS
7915 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
7916
03ed5cbf
VS
7917 pipe_config->dpll_hw_state.dpll_md =
7918 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
bdd4b6a6
DV
7919}
7920
d288f65f 7921static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7922 const struct intel_crtc_state *pipe_config)
a0c4da24 7923{
f47709a9 7924 struct drm_device *dev = crtc->base.dev;
fac5e23e 7925 struct drm_i915_private *dev_priv = to_i915(dev);
cd2d34d9 7926 enum pipe pipe = crtc->pipe;
bdd4b6a6 7927 u32 mdiv;
a0c4da24 7928 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 7929 u32 coreclk, reg_val;
a0c4da24 7930
cd2d34d9
VS
7931 /* Enable Refclk */
7932 I915_WRITE(DPLL(pipe),
7933 pipe_config->dpll_hw_state.dpll &
7934 ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
7935
7936 /* No need to actually set up the DPLL with DSI */
7937 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7938 return;
7939
a580516d 7940 mutex_lock(&dev_priv->sb_lock);
09153000 7941
d288f65f
VS
7942 bestn = pipe_config->dpll.n;
7943 bestm1 = pipe_config->dpll.m1;
7944 bestm2 = pipe_config->dpll.m2;
7945 bestp1 = pipe_config->dpll.p1;
7946 bestp2 = pipe_config->dpll.p2;
a0c4da24 7947
89b667f8
JB
7948 /* See eDP HDMI DPIO driver vbios notes doc */
7949
7950 /* PLL B needs special handling */
bdd4b6a6 7951 if (pipe == PIPE_B)
5e69f97f 7952 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
7953
7954 /* Set up Tx target for periodic Rcomp update */
ab3c759a 7955 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
7956
7957 /* Disable target IRef on PLL */
ab3c759a 7958 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 7959 reg_val &= 0x00ffffff;
ab3c759a 7960 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
7961
7962 /* Disable fast lock */
ab3c759a 7963 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
7964
7965 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
7966 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7967 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7968 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 7969 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
7970
7971 /*
7972 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7973 * but we don't support that).
7974 * Note: don't use the DAC post divider as it seems unstable.
7975 */
7976 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 7977 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7978
a0c4da24 7979 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 7980 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7981
89b667f8 7982 /* Set HBR and RBR LPF coefficients */
d288f65f 7983 if (pipe_config->port_clock == 162000 ||
2d84d2b3
VS
7984 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
7985 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
ab3c759a 7986 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 7987 0x009f0003);
89b667f8 7988 else
ab3c759a 7989 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
7990 0x00d0000f);
7991
37a5650b 7992 if (intel_crtc_has_dp_encoder(pipe_config)) {
89b667f8 7993 /* Use SSC source */
bdd4b6a6 7994 if (pipe == PIPE_A)
ab3c759a 7995 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7996 0x0df40000);
7997 else
ab3c759a 7998 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7999 0x0df70000);
8000 } else { /* HDMI or VGA */
8001 /* Use bend source */
bdd4b6a6 8002 if (pipe == PIPE_A)
ab3c759a 8003 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
8004 0x0df70000);
8005 else
ab3c759a 8006 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
8007 0x0df40000);
8008 }
a0c4da24 8009
ab3c759a 8010 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8 8011 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
2210ce7f 8012 if (intel_crtc_has_dp_encoder(crtc->config))
89b667f8 8013 coreclk |= 0x01000000;
ab3c759a 8014 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 8015
ab3c759a 8016 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
a580516d 8017 mutex_unlock(&dev_priv->sb_lock);
a0c4da24
JB
8018}
8019
d288f65f 8020static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 8021 const struct intel_crtc_state *pipe_config)
9d556c99
CML
8022{
8023 struct drm_device *dev = crtc->base.dev;
fac5e23e 8024 struct drm_i915_private *dev_priv = to_i915(dev);
cd2d34d9 8025 enum pipe pipe = crtc->pipe;
9d556c99 8026 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9cbe40c1 8027 u32 loopfilter, tribuf_calcntr;
9d556c99 8028 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
a945ce7e 8029 u32 dpio_val;
9cbe40c1 8030 int vco;
9d556c99 8031
cd2d34d9
VS
8032 /* Enable Refclk and SSC */
8033 I915_WRITE(DPLL(pipe),
8034 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
8035
8036 /* No need to actually set up the DPLL with DSI */
8037 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8038 return;
8039
d288f65f
VS
8040 bestn = pipe_config->dpll.n;
8041 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
8042 bestm1 = pipe_config->dpll.m1;
8043 bestm2 = pipe_config->dpll.m2 >> 22;
8044 bestp1 = pipe_config->dpll.p1;
8045 bestp2 = pipe_config->dpll.p2;
9cbe40c1 8046 vco = pipe_config->dpll.vco;
a945ce7e 8047 dpio_val = 0;
9cbe40c1 8048 loopfilter = 0;
9d556c99 8049
a580516d 8050 mutex_lock(&dev_priv->sb_lock);
9d556c99 8051
9d556c99
CML
8052 /* p1 and p2 divider */
8053 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
8054 5 << DPIO_CHV_S1_DIV_SHIFT |
8055 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
8056 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
8057 1 << DPIO_CHV_K_DIV_SHIFT);
8058
8059 /* Feedback post-divider - m2 */
8060 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
8061
8062 /* Feedback refclk divider - n and m1 */
8063 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
8064 DPIO_CHV_M1_DIV_BY_2 |
8065 1 << DPIO_CHV_N_DIV_SHIFT);
8066
8067 /* M2 fraction division */
25a25dfc 8068 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
9d556c99
CML
8069
8070 /* M2 fraction division enable */
a945ce7e
VP
8071 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
8072 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
8073 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
8074 if (bestm2_frac)
8075 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
8076 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
9d556c99 8077
de3a0fde
VP
8078 /* Program digital lock detect threshold */
8079 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
8080 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
8081 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
8082 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
8083 if (!bestm2_frac)
8084 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
8085 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
8086
9d556c99 8087 /* Loop filter */
9cbe40c1
VP
8088 if (vco == 5400000) {
8089 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
8090 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
8091 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
8092 tribuf_calcntr = 0x9;
8093 } else if (vco <= 6200000) {
8094 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
8095 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
8096 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
8097 tribuf_calcntr = 0x9;
8098 } else if (vco <= 6480000) {
8099 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
8100 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
8101 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
8102 tribuf_calcntr = 0x8;
8103 } else {
8104 /* Not supported. Apply the same limits as in the max case */
8105 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
8106 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
8107 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
8108 tribuf_calcntr = 0;
8109 }
9d556c99
CML
8110 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
8111
968040b2 8112 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
9cbe40c1
VP
8113 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
8114 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
8115 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
8116
9d556c99
CML
8117 /* AFC Recal */
8118 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
8119 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
8120 DPIO_AFC_RECAL);
8121
a580516d 8122 mutex_unlock(&dev_priv->sb_lock);
9d556c99
CML
8123}
8124
d288f65f
VS
8125/**
8126 * vlv_force_pll_on - forcibly enable just the PLL
8127 * @dev_priv: i915 private structure
8128 * @pipe: pipe PLL to enable
8129 * @dpll: PLL configuration
8130 *
8131 * Enable the PLL for @pipe using the supplied @dpll config. To be used
8132 * in cases where we need the PLL enabled even when @pipe is not going to
8133 * be enabled.
8134 */
30ad9814 8135int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
3f36b937 8136 const struct dpll *dpll)
d288f65f 8137{
b91eb5cc 8138 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
3f36b937
TU
8139 struct intel_crtc_state *pipe_config;
8140
8141 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
8142 if (!pipe_config)
8143 return -ENOMEM;
8144
8145 pipe_config->base.crtc = &crtc->base;
8146 pipe_config->pixel_multiplier = 1;
8147 pipe_config->dpll = *dpll;
d288f65f 8148
30ad9814 8149 if (IS_CHERRYVIEW(dev_priv)) {
3f36b937
TU
8150 chv_compute_dpll(crtc, pipe_config);
8151 chv_prepare_pll(crtc, pipe_config);
8152 chv_enable_pll(crtc, pipe_config);
d288f65f 8153 } else {
3f36b937
TU
8154 vlv_compute_dpll(crtc, pipe_config);
8155 vlv_prepare_pll(crtc, pipe_config);
8156 vlv_enable_pll(crtc, pipe_config);
d288f65f 8157 }
3f36b937
TU
8158
8159 kfree(pipe_config);
8160
8161 return 0;
d288f65f
VS
8162}
8163
8164/**
8165 * vlv_force_pll_off - forcibly disable just the PLL
8166 * @dev_priv: i915 private structure
8167 * @pipe: pipe PLL to disable
8168 *
8169 * Disable the PLL for @pipe. To be used in cases where we need
8170 * the PLL enabled even when @pipe is not going to be enabled.
8171 */
30ad9814 8172void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
d288f65f 8173{
30ad9814
VS
8174 if (IS_CHERRYVIEW(dev_priv))
8175 chv_disable_pll(dev_priv, pipe);
d288f65f 8176 else
30ad9814 8177 vlv_disable_pll(dev_priv, pipe);
d288f65f
VS
8178}
8179
251ac862
DV
8180static void i9xx_compute_dpll(struct intel_crtc *crtc,
8181 struct intel_crtc_state *crtc_state,
9e2c8475 8182 struct dpll *reduced_clock)
eb1cbe48 8183{
9b1e14f4 8184 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
eb1cbe48 8185 u32 dpll;
190f68c5 8186 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 8187
190f68c5 8188 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 8189
eb1cbe48
DV
8190 dpll = DPLL_VGA_MODE_DIS;
8191
2d84d2b3 8192 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
8193 dpll |= DPLLB_MODE_LVDS;
8194 else
8195 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 8196
73f67aa8
JN
8197 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
8198 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
190f68c5 8199 dpll |= (crtc_state->pixel_multiplier - 1)
198a037f 8200 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 8201 }
198a037f 8202
3d6e9ee0
VS
8203 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
8204 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
4a33e48d 8205 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 8206
37a5650b 8207 if (intel_crtc_has_dp_encoder(crtc_state))
4a33e48d 8208 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
8209
8210 /* compute bitmask from p1 value */
9b1e14f4 8211 if (IS_PINEVIEW(dev_priv))
eb1cbe48
DV
8212 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
8213 else {
8214 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
9beb5fea 8215 if (IS_G4X(dev_priv) && reduced_clock)
eb1cbe48
DV
8216 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
8217 }
8218 switch (clock->p2) {
8219 case 5:
8220 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8221 break;
8222 case 7:
8223 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8224 break;
8225 case 10:
8226 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8227 break;
8228 case 14:
8229 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8230 break;
8231 }
9b1e14f4 8232 if (INTEL_GEN(dev_priv) >= 4)
eb1cbe48
DV
8233 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
8234
190f68c5 8235 if (crtc_state->sdvo_tv_clock)
eb1cbe48 8236 dpll |= PLL_REF_INPUT_TVCLKINBC;
2d84d2b3 8237 else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 8238 intel_panel_use_ssc(dev_priv))
eb1cbe48
DV
8239 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
8240 else
8241 dpll |= PLL_REF_INPUT_DREFCLK;
8242
8243 dpll |= DPLL_VCO_ENABLE;
190f68c5 8244 crtc_state->dpll_hw_state.dpll = dpll;
8bcc2795 8245
9b1e14f4 8246 if (INTEL_GEN(dev_priv) >= 4) {
190f68c5 8247 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
ef1b460d 8248 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
190f68c5 8249 crtc_state->dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
DV
8250 }
8251}
8252
251ac862
DV
8253static void i8xx_compute_dpll(struct intel_crtc *crtc,
8254 struct intel_crtc_state *crtc_state,
9e2c8475 8255 struct dpll *reduced_clock)
eb1cbe48 8256{
f47709a9 8257 struct drm_device *dev = crtc->base.dev;
fac5e23e 8258 struct drm_i915_private *dev_priv = to_i915(dev);
eb1cbe48 8259 u32 dpll;
190f68c5 8260 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 8261
190f68c5 8262 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 8263
eb1cbe48
DV
8264 dpll = DPLL_VGA_MODE_DIS;
8265
2d84d2b3 8266 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
8267 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
8268 } else {
8269 if (clock->p1 == 2)
8270 dpll |= PLL_P1_DIVIDE_BY_TWO;
8271 else
8272 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
8273 if (clock->p2 == 4)
8274 dpll |= PLL_P2_DIVIDE_BY_4;
8275 }
8276
50a0bc90
TU
8277 if (!IS_I830(dev_priv) &&
8278 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
4a33e48d
DV
8279 dpll |= DPLL_DVO_2X_MODE;
8280
2d84d2b3 8281 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 8282 intel_panel_use_ssc(dev_priv))
eb1cbe48
DV
8283 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
8284 else
8285 dpll |= PLL_REF_INPUT_DREFCLK;
8286
8287 dpll |= DPLL_VCO_ENABLE;
190f68c5 8288 crtc_state->dpll_hw_state.dpll = dpll;
eb1cbe48
DV
8289}
8290
8a654f3b 8291static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c 8292{
6315b5d3 8293 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
b0e77b9c 8294 enum pipe pipe = intel_crtc->pipe;
6e3c9717 8295 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
7c5f93b0 8296 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
1caea6e9
VS
8297 uint32_t crtc_vtotal, crtc_vblank_end;
8298 int vsyncshift = 0;
4d8a62ea
DV
8299
8300 /* We need to be careful not to changed the adjusted mode, for otherwise
8301 * the hw state checker will get angry at the mismatch. */
8302 crtc_vtotal = adjusted_mode->crtc_vtotal;
8303 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 8304
609aeaca 8305 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 8306 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
8307 crtc_vtotal -= 1;
8308 crtc_vblank_end -= 1;
609aeaca 8309
2d84d2b3 8310 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
609aeaca
VS
8311 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
8312 else
8313 vsyncshift = adjusted_mode->crtc_hsync_start -
8314 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
8315 if (vsyncshift < 0)
8316 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
8317 }
8318
6315b5d3 8319 if (INTEL_GEN(dev_priv) > 3)
fe2b8f9d 8320 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 8321
fe2b8f9d 8322 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
8323 (adjusted_mode->crtc_hdisplay - 1) |
8324 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 8325 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
8326 (adjusted_mode->crtc_hblank_start - 1) |
8327 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 8328 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
8329 (adjusted_mode->crtc_hsync_start - 1) |
8330 ((adjusted_mode->crtc_hsync_end - 1) << 16));
8331
fe2b8f9d 8332 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 8333 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 8334 ((crtc_vtotal - 1) << 16));
fe2b8f9d 8335 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 8336 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 8337 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 8338 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
8339 (adjusted_mode->crtc_vsync_start - 1) |
8340 ((adjusted_mode->crtc_vsync_end - 1) << 16));
8341
b5e508d4
PZ
8342 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
8343 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
8344 * documented on the DDI_FUNC_CTL register description, EDP Input Select
8345 * bits. */
772c2a51 8346 if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
b5e508d4
PZ
8347 (pipe == PIPE_B || pipe == PIPE_C))
8348 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
8349
bc58be60
JN
8350}
8351
8352static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
8353{
8354 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 8355 struct drm_i915_private *dev_priv = to_i915(dev);
bc58be60
JN
8356 enum pipe pipe = intel_crtc->pipe;
8357
b0e77b9c
PZ
8358 /* pipesrc controls the size that is scaled from, which should
8359 * always be the user's requested size.
8360 */
8361 I915_WRITE(PIPESRC(pipe),
6e3c9717
ACO
8362 ((intel_crtc->config->pipe_src_w - 1) << 16) |
8363 (intel_crtc->config->pipe_src_h - 1));
b0e77b9c
PZ
8364}
8365
1bd1bd80 8366static void intel_get_pipe_timings(struct intel_crtc *crtc,
5cec258b 8367 struct intel_crtc_state *pipe_config)
1bd1bd80
DV
8368{
8369 struct drm_device *dev = crtc->base.dev;
fac5e23e 8370 struct drm_i915_private *dev_priv = to_i915(dev);
1bd1bd80
DV
8371 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
8372 uint32_t tmp;
8373
8374 tmp = I915_READ(HTOTAL(cpu_transcoder));
2d112de7
ACO
8375 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
8376 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 8377 tmp = I915_READ(HBLANK(cpu_transcoder));
2d112de7
ACO
8378 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
8379 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 8380 tmp = I915_READ(HSYNC(cpu_transcoder));
2d112de7
ACO
8381 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
8382 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
8383
8384 tmp = I915_READ(VTOTAL(cpu_transcoder));
2d112de7
ACO
8385 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
8386 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 8387 tmp = I915_READ(VBLANK(cpu_transcoder));
2d112de7
ACO
8388 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
8389 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 8390 tmp = I915_READ(VSYNC(cpu_transcoder));
2d112de7
ACO
8391 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
8392 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
8393
8394 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
2d112de7
ACO
8395 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
8396 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
8397 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
1bd1bd80 8398 }
bc58be60
JN
8399}
8400
8401static void intel_get_pipe_src_size(struct intel_crtc *crtc,
8402 struct intel_crtc_state *pipe_config)
8403{
8404 struct drm_device *dev = crtc->base.dev;
fac5e23e 8405 struct drm_i915_private *dev_priv = to_i915(dev);
bc58be60 8406 u32 tmp;
1bd1bd80
DV
8407
8408 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
8409 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
8410 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
8411
2d112de7
ACO
8412 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
8413 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
DV
8414}
8415
f6a83288 8416void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5cec258b 8417 struct intel_crtc_state *pipe_config)
babea61d 8418{
2d112de7
ACO
8419 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
8420 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
8421 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
8422 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
babea61d 8423
2d112de7
ACO
8424 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
8425 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
8426 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
8427 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
babea61d 8428
2d112de7 8429 mode->flags = pipe_config->base.adjusted_mode.flags;
cd13f5ab 8430 mode->type = DRM_MODE_TYPE_DRIVER;
babea61d 8431
2d112de7 8432 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
cd13f5ab
ML
8433
8434 mode->hsync = drm_mode_hsync(mode);
8435 mode->vrefresh = drm_mode_vrefresh(mode);
8436 drm_mode_set_name(mode);
babea61d
JB
8437}
8438
84b046f3
DV
8439static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
8440{
6315b5d3 8441 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
84b046f3
DV
8442 uint32_t pipeconf;
8443
9f11a9e4 8444 pipeconf = 0;
84b046f3 8445
b6b5d049
VS
8446 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
8447 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
8448 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
67c72a12 8449
6e3c9717 8450 if (intel_crtc->config->double_wide)
cf532bb2 8451 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 8452
ff9ce46e 8453 /* only g4x and later have fancy bpc/dither controls */
9beb5fea
TU
8454 if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
8455 IS_CHERRYVIEW(dev_priv)) {
ff9ce46e 8456 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6e3c9717 8457 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
ff9ce46e 8458 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 8459 PIPECONF_DITHER_TYPE_SP;
84b046f3 8460
6e3c9717 8461 switch (intel_crtc->config->pipe_bpp) {
ff9ce46e
DV
8462 case 18:
8463 pipeconf |= PIPECONF_6BPC;
8464 break;
8465 case 24:
8466 pipeconf |= PIPECONF_8BPC;
8467 break;
8468 case 30:
8469 pipeconf |= PIPECONF_10BPC;
8470 break;
8471 default:
8472 /* Case prevented by intel_choose_pipe_bpp_dither. */
8473 BUG();
84b046f3
DV
8474 }
8475 }
8476
56b857a5 8477 if (HAS_PIPE_CXSR(dev_priv)) {
84b046f3
DV
8478 if (intel_crtc->lowfreq_avail) {
8479 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
8480 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
8481 } else {
8482 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
84b046f3
DV
8483 }
8484 }
8485
6e3c9717 8486 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
6315b5d3 8487 if (INTEL_GEN(dev_priv) < 4 ||
2d84d2b3 8488 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
efc2cfff
VS
8489 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
8490 else
8491 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
8492 } else
84b046f3
DV
8493 pipeconf |= PIPECONF_PROGRESSIVE;
8494
920a14b2 8495 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
666a4537 8496 intel_crtc->config->limited_color_range)
9f11a9e4 8497 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 8498
84b046f3
DV
8499 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
8500 POSTING_READ(PIPECONF(intel_crtc->pipe));
8501}
8502
81c97f52
ACO
8503static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
8504 struct intel_crtc_state *crtc_state)
8505{
8506 struct drm_device *dev = crtc->base.dev;
fac5e23e 8507 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 8508 const struct intel_limit *limit;
81c97f52
ACO
8509 int refclk = 48000;
8510
8511 memset(&crtc_state->dpll_hw_state, 0,
8512 sizeof(crtc_state->dpll_hw_state));
8513
2d84d2b3 8514 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
81c97f52
ACO
8515 if (intel_panel_use_ssc(dev_priv)) {
8516 refclk = dev_priv->vbt.lvds_ssc_freq;
8517 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8518 }
8519
8520 limit = &intel_limits_i8xx_lvds;
2d84d2b3 8521 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
81c97f52
ACO
8522 limit = &intel_limits_i8xx_dvo;
8523 } else {
8524 limit = &intel_limits_i8xx_dac;
8525 }
8526
8527 if (!crtc_state->clock_set &&
8528 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8529 refclk, NULL, &crtc_state->dpll)) {
8530 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8531 return -EINVAL;
8532 }
8533
8534 i8xx_compute_dpll(crtc, crtc_state, NULL);
8535
8536 return 0;
8537}
8538
19ec6693
ACO
8539static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
8540 struct intel_crtc_state *crtc_state)
8541{
8542 struct drm_device *dev = crtc->base.dev;
fac5e23e 8543 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 8544 const struct intel_limit *limit;
19ec6693
ACO
8545 int refclk = 96000;
8546
8547 memset(&crtc_state->dpll_hw_state, 0,
8548 sizeof(crtc_state->dpll_hw_state));
8549
2d84d2b3 8550 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
19ec6693
ACO
8551 if (intel_panel_use_ssc(dev_priv)) {
8552 refclk = dev_priv->vbt.lvds_ssc_freq;
8553 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8554 }
8555
8556 if (intel_is_dual_link_lvds(dev))
8557 limit = &intel_limits_g4x_dual_channel_lvds;
8558 else
8559 limit = &intel_limits_g4x_single_channel_lvds;
2d84d2b3
VS
8560 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
8561 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
19ec6693 8562 limit = &intel_limits_g4x_hdmi;
2d84d2b3 8563 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
19ec6693
ACO
8564 limit = &intel_limits_g4x_sdvo;
8565 } else {
8566 /* The option is for other outputs */
8567 limit = &intel_limits_i9xx_sdvo;
8568 }
8569
8570 if (!crtc_state->clock_set &&
8571 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8572 refclk, NULL, &crtc_state->dpll)) {
8573 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8574 return -EINVAL;
8575 }
8576
8577 i9xx_compute_dpll(crtc, crtc_state, NULL);
8578
8579 return 0;
8580}
8581
70e8aa21
ACO
8582static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
8583 struct intel_crtc_state *crtc_state)
8584{
8585 struct drm_device *dev = crtc->base.dev;
fac5e23e 8586 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 8587 const struct intel_limit *limit;
70e8aa21
ACO
8588 int refclk = 96000;
8589
8590 memset(&crtc_state->dpll_hw_state, 0,
8591 sizeof(crtc_state->dpll_hw_state));
8592
2d84d2b3 8593 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
70e8aa21
ACO
8594 if (intel_panel_use_ssc(dev_priv)) {
8595 refclk = dev_priv->vbt.lvds_ssc_freq;
8596 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8597 }
8598
8599 limit = &intel_limits_pineview_lvds;
8600 } else {
8601 limit = &intel_limits_pineview_sdvo;
8602 }
8603
8604 if (!crtc_state->clock_set &&
8605 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8606 refclk, NULL, &crtc_state->dpll)) {
8607 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8608 return -EINVAL;
8609 }
8610
8611 i9xx_compute_dpll(crtc, crtc_state, NULL);
8612
8613 return 0;
8614}
8615
190f68c5
ACO
8616static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
8617 struct intel_crtc_state *crtc_state)
79e53945 8618{
c7653199 8619 struct drm_device *dev = crtc->base.dev;
fac5e23e 8620 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 8621 const struct intel_limit *limit;
81c97f52 8622 int refclk = 96000;
79e53945 8623
dd3cd74a
ACO
8624 memset(&crtc_state->dpll_hw_state, 0,
8625 sizeof(crtc_state->dpll_hw_state));
8626
2d84d2b3 8627 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
70e8aa21
ACO
8628 if (intel_panel_use_ssc(dev_priv)) {
8629 refclk = dev_priv->vbt.lvds_ssc_freq;
8630 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8631 }
43565a06 8632
70e8aa21
ACO
8633 limit = &intel_limits_i9xx_lvds;
8634 } else {
8635 limit = &intel_limits_i9xx_sdvo;
81c97f52 8636 }
79e53945 8637
70e8aa21
ACO
8638 if (!crtc_state->clock_set &&
8639 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8640 refclk, NULL, &crtc_state->dpll)) {
8641 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8642 return -EINVAL;
f47709a9 8643 }
7026d4ac 8644
81c97f52 8645 i9xx_compute_dpll(crtc, crtc_state, NULL);
79e53945 8646
c8f7a0db 8647 return 0;
f564048e
EA
8648}
8649
65b3d6a9
ACO
8650static int chv_crtc_compute_clock(struct intel_crtc *crtc,
8651 struct intel_crtc_state *crtc_state)
8652{
8653 int refclk = 100000;
1b6f4958 8654 const struct intel_limit *limit = &intel_limits_chv;
65b3d6a9
ACO
8655
8656 memset(&crtc_state->dpll_hw_state, 0,
8657 sizeof(crtc_state->dpll_hw_state));
8658
65b3d6a9
ACO
8659 if (!crtc_state->clock_set &&
8660 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8661 refclk, NULL, &crtc_state->dpll)) {
8662 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8663 return -EINVAL;
8664 }
8665
8666 chv_compute_dpll(crtc, crtc_state);
8667
8668 return 0;
8669}
8670
8671static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
8672 struct intel_crtc_state *crtc_state)
8673{
8674 int refclk = 100000;
1b6f4958 8675 const struct intel_limit *limit = &intel_limits_vlv;
65b3d6a9
ACO
8676
8677 memset(&crtc_state->dpll_hw_state, 0,
8678 sizeof(crtc_state->dpll_hw_state));
8679
65b3d6a9
ACO
8680 if (!crtc_state->clock_set &&
8681 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8682 refclk, NULL, &crtc_state->dpll)) {
8683 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8684 return -EINVAL;
8685 }
8686
8687 vlv_compute_dpll(crtc, crtc_state);
8688
8689 return 0;
8690}
8691
2fa2fe9a 8692static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5cec258b 8693 struct intel_crtc_state *pipe_config)
2fa2fe9a 8694{
6315b5d3 8695 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2fa2fe9a
DV
8696 uint32_t tmp;
8697
50a0bc90
TU
8698 if (INTEL_GEN(dev_priv) <= 3 &&
8699 (IS_I830(dev_priv) || !IS_MOBILE(dev_priv)))
dc9e7dec
VS
8700 return;
8701
2fa2fe9a 8702 tmp = I915_READ(PFIT_CONTROL);
06922821
DV
8703 if (!(tmp & PFIT_ENABLE))
8704 return;
2fa2fe9a 8705
06922821 8706 /* Check whether the pfit is attached to our pipe. */
6315b5d3 8707 if (INTEL_GEN(dev_priv) < 4) {
2fa2fe9a
DV
8708 if (crtc->pipe != PIPE_B)
8709 return;
2fa2fe9a
DV
8710 } else {
8711 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
8712 return;
8713 }
8714
06922821 8715 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a 8716 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
2fa2fe9a
DV
8717}
8718
acbec814 8719static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 8720 struct intel_crtc_state *pipe_config)
acbec814
JB
8721{
8722 struct drm_device *dev = crtc->base.dev;
fac5e23e 8723 struct drm_i915_private *dev_priv = to_i915(dev);
acbec814 8724 int pipe = pipe_config->cpu_transcoder;
9e2c8475 8725 struct dpll clock;
acbec814 8726 u32 mdiv;
662c6ecb 8727 int refclk = 100000;
acbec814 8728
b521973b
VS
8729 /* In case of DSI, DPLL will not be used */
8730 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
f573de5a
SK
8731 return;
8732
a580516d 8733 mutex_lock(&dev_priv->sb_lock);
ab3c759a 8734 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
a580516d 8735 mutex_unlock(&dev_priv->sb_lock);
acbec814
JB
8736
8737 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8738 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8739 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8740 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8741 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8742
dccbea3b 8743 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
acbec814
JB
8744}
8745
5724dbd1
DL
8746static void
8747i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8748 struct intel_initial_plane_config *plane_config)
1ad292b5
JB
8749{
8750 struct drm_device *dev = crtc->base.dev;
fac5e23e 8751 struct drm_i915_private *dev_priv = to_i915(dev);
1ad292b5
JB
8752 u32 val, base, offset;
8753 int pipe = crtc->pipe, plane = crtc->plane;
8754 int fourcc, pixel_format;
6761dd31 8755 unsigned int aligned_height;
b113d5ee 8756 struct drm_framebuffer *fb;
1b842c89 8757 struct intel_framebuffer *intel_fb;
1ad292b5 8758
42a7b088
DL
8759 val = I915_READ(DSPCNTR(plane));
8760 if (!(val & DISPLAY_PLANE_ENABLE))
8761 return;
8762
d9806c9f 8763 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 8764 if (!intel_fb) {
1ad292b5
JB
8765 DRM_DEBUG_KMS("failed to alloc fb\n");
8766 return;
8767 }
8768
1b842c89
DL
8769 fb = &intel_fb->base;
8770
d2e9f5fc
VS
8771 fb->dev = dev;
8772
6315b5d3 8773 if (INTEL_GEN(dev_priv) >= 4) {
18c5247e 8774 if (val & DISPPLANE_TILED) {
49af449b 8775 plane_config->tiling = I915_TILING_X;
bae781b2 8776 fb->modifier = I915_FORMAT_MOD_X_TILED;
18c5247e
DV
8777 }
8778 }
1ad292b5
JB
8779
8780 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 8781 fourcc = i9xx_format_to_fourcc(pixel_format);
2f3f4763 8782 fb->format = drm_format_info(fourcc);
1ad292b5 8783
6315b5d3 8784 if (INTEL_GEN(dev_priv) >= 4) {
49af449b 8785 if (plane_config->tiling)
1ad292b5
JB
8786 offset = I915_READ(DSPTILEOFF(plane));
8787 else
8788 offset = I915_READ(DSPLINOFF(plane));
8789 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8790 } else {
8791 base = I915_READ(DSPADDR(plane));
8792 }
8793 plane_config->base = base;
8794
8795 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
8796 fb->width = ((val >> 16) & 0xfff) + 1;
8797 fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
8798
8799 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 8800 fb->pitches[0] = val & 0xffffffc0;
1ad292b5 8801
b113d5ee 8802 aligned_height = intel_fb_align_height(dev, fb->height,
438b74a5 8803 fb->format->format,
bae781b2 8804 fb->modifier);
1ad292b5 8805
f37b5c2b 8806 plane_config->size = fb->pitches[0] * aligned_height;
1ad292b5 8807
2844a921
DL
8808 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8809 pipe_name(pipe), plane, fb->width, fb->height,
272725c7 8810 fb->format->cpp[0] * 8, base, fb->pitches[0],
2844a921 8811 plane_config->size);
1ad292b5 8812
2d14030b 8813 plane_config->fb = intel_fb;
1ad292b5
JB
8814}
8815
70b23a98 8816static void chv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 8817 struct intel_crtc_state *pipe_config)
70b23a98
VS
8818{
8819 struct drm_device *dev = crtc->base.dev;
fac5e23e 8820 struct drm_i915_private *dev_priv = to_i915(dev);
70b23a98
VS
8821 int pipe = pipe_config->cpu_transcoder;
8822 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9e2c8475 8823 struct dpll clock;
0d7b6b11 8824 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
70b23a98
VS
8825 int refclk = 100000;
8826
b521973b
VS
8827 /* In case of DSI, DPLL will not be used */
8828 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8829 return;
8830
a580516d 8831 mutex_lock(&dev_priv->sb_lock);
70b23a98
VS
8832 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8833 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8834 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8835 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
0d7b6b11 8836 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
a580516d 8837 mutex_unlock(&dev_priv->sb_lock);
70b23a98
VS
8838
8839 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
0d7b6b11
ID
8840 clock.m2 = (pll_dw0 & 0xff) << 22;
8841 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8842 clock.m2 |= pll_dw2 & 0x3fffff;
70b23a98
VS
8843 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8844 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8845 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8846
dccbea3b 8847 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
70b23a98
VS
8848}
8849
0e8ffe1b 8850static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5cec258b 8851 struct intel_crtc_state *pipe_config)
0e8ffe1b 8852{
6315b5d3 8853 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1729050e 8854 enum intel_display_power_domain power_domain;
0e8ffe1b 8855 uint32_t tmp;
1729050e 8856 bool ret;
0e8ffe1b 8857
1729050e
ID
8858 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8859 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0
ID
8860 return false;
8861
e143a21c 8862 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 8863 pipe_config->shared_dpll = NULL;
eccb140b 8864
1729050e
ID
8865 ret = false;
8866
0e8ffe1b
DV
8867 tmp = I915_READ(PIPECONF(crtc->pipe));
8868 if (!(tmp & PIPECONF_ENABLE))
1729050e 8869 goto out;
0e8ffe1b 8870
9beb5fea
TU
8871 if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
8872 IS_CHERRYVIEW(dev_priv)) {
42571aef
VS
8873 switch (tmp & PIPECONF_BPC_MASK) {
8874 case PIPECONF_6BPC:
8875 pipe_config->pipe_bpp = 18;
8876 break;
8877 case PIPECONF_8BPC:
8878 pipe_config->pipe_bpp = 24;
8879 break;
8880 case PIPECONF_10BPC:
8881 pipe_config->pipe_bpp = 30;
8882 break;
8883 default:
8884 break;
8885 }
8886 }
8887
920a14b2 8888 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
666a4537 8889 (tmp & PIPECONF_COLOR_RANGE_SELECT))
b5a9fa09
DV
8890 pipe_config->limited_color_range = true;
8891
6315b5d3 8892 if (INTEL_GEN(dev_priv) < 4)
282740f7
VS
8893 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8894
1bd1bd80 8895 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 8896 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 8897
2fa2fe9a
DV
8898 i9xx_get_pfit_config(crtc, pipe_config);
8899
6315b5d3 8900 if (INTEL_GEN(dev_priv) >= 4) {
c231775c 8901 /* No way to read it out on pipes B and C */
920a14b2 8902 if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
c231775c
VS
8903 tmp = dev_priv->chv_dpll_md[crtc->pipe];
8904 else
8905 tmp = I915_READ(DPLL_MD(crtc->pipe));
6c49f241
DV
8906 pipe_config->pixel_multiplier =
8907 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8908 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 8909 pipe_config->dpll_hw_state.dpll_md = tmp;
50a0bc90 8910 } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
73f67aa8 8911 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
6c49f241
DV
8912 tmp = I915_READ(DPLL(crtc->pipe));
8913 pipe_config->pixel_multiplier =
8914 ((tmp & SDVO_MULTIPLIER_MASK)
8915 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8916 } else {
8917 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8918 * port and will be fixed up in the encoder->get_config
8919 * function. */
8920 pipe_config->pixel_multiplier = 1;
8921 }
8bcc2795 8922 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
920a14b2 8923 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
1c4e0274
VS
8924 /*
8925 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8926 * on 830. Filter it out here so that we don't
8927 * report errors due to that.
8928 */
50a0bc90 8929 if (IS_I830(dev_priv))
1c4e0274
VS
8930 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8931
8bcc2795
DV
8932 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8933 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
8934 } else {
8935 /* Mask out read-only status bits. */
8936 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8937 DPLL_PORTC_READY_MASK |
8938 DPLL_PORTB_READY_MASK);
8bcc2795 8939 }
6c49f241 8940
920a14b2 8941 if (IS_CHERRYVIEW(dev_priv))
70b23a98 8942 chv_crtc_clock_get(crtc, pipe_config);
11a914c2 8943 else if (IS_VALLEYVIEW(dev_priv))
acbec814
JB
8944 vlv_crtc_clock_get(crtc, pipe_config);
8945 else
8946 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 8947
0f64614d
VS
8948 /*
8949 * Normally the dotclock is filled in by the encoder .get_config()
8950 * but in case the pipe is enabled w/o any ports we need a sane
8951 * default.
8952 */
8953 pipe_config->base.adjusted_mode.crtc_clock =
8954 pipe_config->port_clock / pipe_config->pixel_multiplier;
8955
1729050e
ID
8956 ret = true;
8957
8958out:
8959 intel_display_power_put(dev_priv, power_domain);
8960
8961 return ret;
0e8ffe1b
DV
8962}
8963
c39055b0 8964static void ironlake_init_pch_refclk(struct drm_i915_private *dev_priv)
13d83a67 8965{
13d83a67 8966 struct intel_encoder *encoder;
1c1a24d2 8967 int i;
74cfd7ac 8968 u32 val, final;
13d83a67 8969 bool has_lvds = false;
199e5d79 8970 bool has_cpu_edp = false;
199e5d79 8971 bool has_panel = false;
99eb6a01
KP
8972 bool has_ck505 = false;
8973 bool can_ssc = false;
1c1a24d2 8974 bool using_ssc_source = false;
13d83a67
JB
8975
8976 /* We need to take the global config into account */
c39055b0 8977 for_each_intel_encoder(&dev_priv->drm, encoder) {
199e5d79
KP
8978 switch (encoder->type) {
8979 case INTEL_OUTPUT_LVDS:
8980 has_panel = true;
8981 has_lvds = true;
8982 break;
8983 case INTEL_OUTPUT_EDP:
8984 has_panel = true;
2de6905f 8985 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
8986 has_cpu_edp = true;
8987 break;
6847d71b
PZ
8988 default:
8989 break;
13d83a67
JB
8990 }
8991 }
8992
6e266956 8993 if (HAS_PCH_IBX(dev_priv)) {
41aa3448 8994 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
8995 can_ssc = has_ck505;
8996 } else {
8997 has_ck505 = false;
8998 can_ssc = true;
8999 }
9000
1c1a24d2
L
9001 /* Check if any DPLLs are using the SSC source */
9002 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
9003 u32 temp = I915_READ(PCH_DPLL(i));
9004
9005 if (!(temp & DPLL_VCO_ENABLE))
9006 continue;
9007
9008 if ((temp & PLL_REF_INPUT_MASK) ==
9009 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
9010 using_ssc_source = true;
9011 break;
9012 }
9013 }
9014
9015 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
9016 has_panel, has_lvds, has_ck505, using_ssc_source);
13d83a67
JB
9017
9018 /* Ironlake: try to setup display ref clock before DPLL
9019 * enabling. This is only under driver's control after
9020 * PCH B stepping, previous chipset stepping should be
9021 * ignoring this setting.
9022 */
74cfd7ac
CW
9023 val = I915_READ(PCH_DREF_CONTROL);
9024
9025 /* As we must carefully and slowly disable/enable each source in turn,
9026 * compute the final state we want first and check if we need to
9027 * make any changes at all.
9028 */
9029 final = val;
9030 final &= ~DREF_NONSPREAD_SOURCE_MASK;
9031 if (has_ck505)
9032 final |= DREF_NONSPREAD_CK505_ENABLE;
9033 else
9034 final |= DREF_NONSPREAD_SOURCE_ENABLE;
9035
8c07eb68 9036 final &= ~DREF_SSC_SOURCE_MASK;
74cfd7ac 9037 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8c07eb68 9038 final &= ~DREF_SSC1_ENABLE;
74cfd7ac
CW
9039
9040 if (has_panel) {
9041 final |= DREF_SSC_SOURCE_ENABLE;
9042
9043 if (intel_panel_use_ssc(dev_priv) && can_ssc)
9044 final |= DREF_SSC1_ENABLE;
9045
9046 if (has_cpu_edp) {
9047 if (intel_panel_use_ssc(dev_priv) && can_ssc)
9048 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
9049 else
9050 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
9051 } else
9052 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
1c1a24d2
L
9053 } else if (using_ssc_source) {
9054 final |= DREF_SSC_SOURCE_ENABLE;
9055 final |= DREF_SSC1_ENABLE;
74cfd7ac
CW
9056 }
9057
9058 if (final == val)
9059 return;
9060
13d83a67 9061 /* Always enable nonspread source */
74cfd7ac 9062 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 9063
99eb6a01 9064 if (has_ck505)
74cfd7ac 9065 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 9066 else
74cfd7ac 9067 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 9068
199e5d79 9069 if (has_panel) {
74cfd7ac
CW
9070 val &= ~DREF_SSC_SOURCE_MASK;
9071 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 9072
199e5d79 9073 /* SSC must be turned on before enabling the CPU output */
99eb6a01 9074 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 9075 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 9076 val |= DREF_SSC1_ENABLE;
e77166b5 9077 } else
74cfd7ac 9078 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
9079
9080 /* Get SSC going before enabling the outputs */
74cfd7ac 9081 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
9082 POSTING_READ(PCH_DREF_CONTROL);
9083 udelay(200);
9084
74cfd7ac 9085 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
9086
9087 /* Enable CPU source on CPU attached eDP */
199e5d79 9088 if (has_cpu_edp) {
99eb6a01 9089 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 9090 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 9091 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 9092 } else
74cfd7ac 9093 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 9094 } else
74cfd7ac 9095 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 9096
74cfd7ac 9097 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
9098 POSTING_READ(PCH_DREF_CONTROL);
9099 udelay(200);
9100 } else {
1c1a24d2 9101 DRM_DEBUG_KMS("Disabling CPU source output\n");
199e5d79 9102
74cfd7ac 9103 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
9104
9105 /* Turn off CPU output */
74cfd7ac 9106 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 9107
74cfd7ac 9108 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
9109 POSTING_READ(PCH_DREF_CONTROL);
9110 udelay(200);
9111
1c1a24d2
L
9112 if (!using_ssc_source) {
9113 DRM_DEBUG_KMS("Disabling SSC source\n");
199e5d79 9114
1c1a24d2
L
9115 /* Turn off the SSC source */
9116 val &= ~DREF_SSC_SOURCE_MASK;
9117 val |= DREF_SSC_SOURCE_DISABLE;
f165d283 9118
1c1a24d2
L
9119 /* Turn off SSC1 */
9120 val &= ~DREF_SSC1_ENABLE;
9121
9122 I915_WRITE(PCH_DREF_CONTROL, val);
9123 POSTING_READ(PCH_DREF_CONTROL);
9124 udelay(200);
9125 }
13d83a67 9126 }
74cfd7ac
CW
9127
9128 BUG_ON(val != final);
13d83a67
JB
9129}
9130
f31f2d55 9131static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 9132{
f31f2d55 9133 uint32_t tmp;
dde86e2d 9134
0ff066a9
PZ
9135 tmp = I915_READ(SOUTH_CHICKEN2);
9136 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
9137 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 9138
cf3598c2
ID
9139 if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
9140 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
0ff066a9 9141 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 9142
0ff066a9
PZ
9143 tmp = I915_READ(SOUTH_CHICKEN2);
9144 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
9145 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 9146
cf3598c2
ID
9147 if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
9148 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
0ff066a9 9149 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
9150}
9151
9152/* WaMPhyProgramming:hsw */
9153static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
9154{
9155 uint32_t tmp;
dde86e2d
PZ
9156
9157 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
9158 tmp &= ~(0xFF << 24);
9159 tmp |= (0x12 << 24);
9160 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
9161
dde86e2d
PZ
9162 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
9163 tmp |= (1 << 11);
9164 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
9165
9166 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
9167 tmp |= (1 << 11);
9168 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
9169
dde86e2d
PZ
9170 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
9171 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
9172 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
9173
9174 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
9175 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
9176 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
9177
0ff066a9
PZ
9178 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
9179 tmp &= ~(7 << 13);
9180 tmp |= (5 << 13);
9181 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 9182
0ff066a9
PZ
9183 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
9184 tmp &= ~(7 << 13);
9185 tmp |= (5 << 13);
9186 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
9187
9188 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
9189 tmp &= ~0xFF;
9190 tmp |= 0x1C;
9191 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
9192
9193 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
9194 tmp &= ~0xFF;
9195 tmp |= 0x1C;
9196 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
9197
9198 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
9199 tmp &= ~(0xFF << 16);
9200 tmp |= (0x1C << 16);
9201 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
9202
9203 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
9204 tmp &= ~(0xFF << 16);
9205 tmp |= (0x1C << 16);
9206 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
9207
0ff066a9
PZ
9208 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
9209 tmp |= (1 << 27);
9210 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 9211
0ff066a9
PZ
9212 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
9213 tmp |= (1 << 27);
9214 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 9215
0ff066a9
PZ
9216 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
9217 tmp &= ~(0xF << 28);
9218 tmp |= (4 << 28);
9219 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 9220
0ff066a9
PZ
9221 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
9222 tmp &= ~(0xF << 28);
9223 tmp |= (4 << 28);
9224 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
9225}
9226
2fa86a1f
PZ
9227/* Implements 3 different sequences from BSpec chapter "Display iCLK
9228 * Programming" based on the parameters passed:
9229 * - Sequence to enable CLKOUT_DP
9230 * - Sequence to enable CLKOUT_DP without spread
9231 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
9232 */
c39055b0
ACO
9233static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
9234 bool with_spread, bool with_fdi)
f31f2d55 9235{
2fa86a1f
PZ
9236 uint32_t reg, tmp;
9237
9238 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
9239 with_spread = true;
4f8036a2
TU
9240 if (WARN(HAS_PCH_LPT_LP(dev_priv) &&
9241 with_fdi, "LP PCH doesn't have FDI\n"))
2fa86a1f 9242 with_fdi = false;
f31f2d55 9243
a580516d 9244 mutex_lock(&dev_priv->sb_lock);
f31f2d55
PZ
9245
9246 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
9247 tmp &= ~SBI_SSCCTL_DISABLE;
9248 tmp |= SBI_SSCCTL_PATHALT;
9249 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
9250
9251 udelay(24);
9252
2fa86a1f
PZ
9253 if (with_spread) {
9254 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
9255 tmp &= ~SBI_SSCCTL_PATHALT;
9256 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 9257
2fa86a1f
PZ
9258 if (with_fdi) {
9259 lpt_reset_fdi_mphy(dev_priv);
9260 lpt_program_fdi_mphy(dev_priv);
9261 }
9262 }
dde86e2d 9263
4f8036a2 9264 reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
2fa86a1f
PZ
9265 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
9266 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
9267 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246 9268
a580516d 9269 mutex_unlock(&dev_priv->sb_lock);
dde86e2d
PZ
9270}
9271
47701c3b 9272/* Sequence to disable CLKOUT_DP */
c39055b0 9273static void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
47701c3b 9274{
47701c3b
PZ
9275 uint32_t reg, tmp;
9276
a580516d 9277 mutex_lock(&dev_priv->sb_lock);
47701c3b 9278
4f8036a2 9279 reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
47701c3b
PZ
9280 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
9281 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
9282 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
9283
9284 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
9285 if (!(tmp & SBI_SSCCTL_DISABLE)) {
9286 if (!(tmp & SBI_SSCCTL_PATHALT)) {
9287 tmp |= SBI_SSCCTL_PATHALT;
9288 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
9289 udelay(32);
9290 }
9291 tmp |= SBI_SSCCTL_DISABLE;
9292 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
9293 }
9294
a580516d 9295 mutex_unlock(&dev_priv->sb_lock);
47701c3b
PZ
9296}
9297
f7be2c21
VS
9298#define BEND_IDX(steps) ((50 + (steps)) / 5)
9299
9300static const uint16_t sscdivintphase[] = {
9301 [BEND_IDX( 50)] = 0x3B23,
9302 [BEND_IDX( 45)] = 0x3B23,
9303 [BEND_IDX( 40)] = 0x3C23,
9304 [BEND_IDX( 35)] = 0x3C23,
9305 [BEND_IDX( 30)] = 0x3D23,
9306 [BEND_IDX( 25)] = 0x3D23,
9307 [BEND_IDX( 20)] = 0x3E23,
9308 [BEND_IDX( 15)] = 0x3E23,
9309 [BEND_IDX( 10)] = 0x3F23,
9310 [BEND_IDX( 5)] = 0x3F23,
9311 [BEND_IDX( 0)] = 0x0025,
9312 [BEND_IDX( -5)] = 0x0025,
9313 [BEND_IDX(-10)] = 0x0125,
9314 [BEND_IDX(-15)] = 0x0125,
9315 [BEND_IDX(-20)] = 0x0225,
9316 [BEND_IDX(-25)] = 0x0225,
9317 [BEND_IDX(-30)] = 0x0325,
9318 [BEND_IDX(-35)] = 0x0325,
9319 [BEND_IDX(-40)] = 0x0425,
9320 [BEND_IDX(-45)] = 0x0425,
9321 [BEND_IDX(-50)] = 0x0525,
9322};
9323
9324/*
9325 * Bend CLKOUT_DP
9326 * steps -50 to 50 inclusive, in steps of 5
9327 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
9328 * change in clock period = -(steps / 10) * 5.787 ps
9329 */
9330static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
9331{
9332 uint32_t tmp;
9333 int idx = BEND_IDX(steps);
9334
9335 if (WARN_ON(steps % 5 != 0))
9336 return;
9337
9338 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
9339 return;
9340
9341 mutex_lock(&dev_priv->sb_lock);
9342
9343 if (steps % 10 != 0)
9344 tmp = 0xAAAAAAAB;
9345 else
9346 tmp = 0x00000000;
9347 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
9348
9349 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
9350 tmp &= 0xffff0000;
9351 tmp |= sscdivintphase[idx];
9352 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
9353
9354 mutex_unlock(&dev_priv->sb_lock);
9355}
9356
9357#undef BEND_IDX
9358
c39055b0 9359static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
bf8fa3d3 9360{
bf8fa3d3
PZ
9361 struct intel_encoder *encoder;
9362 bool has_vga = false;
9363
c39055b0 9364 for_each_intel_encoder(&dev_priv->drm, encoder) {
bf8fa3d3
PZ
9365 switch (encoder->type) {
9366 case INTEL_OUTPUT_ANALOG:
9367 has_vga = true;
9368 break;
6847d71b
PZ
9369 default:
9370 break;
bf8fa3d3
PZ
9371 }
9372 }
9373
f7be2c21 9374 if (has_vga) {
c39055b0
ACO
9375 lpt_bend_clkout_dp(dev_priv, 0);
9376 lpt_enable_clkout_dp(dev_priv, true, true);
f7be2c21 9377 } else {
c39055b0 9378 lpt_disable_clkout_dp(dev_priv);
f7be2c21 9379 }
bf8fa3d3
PZ
9380}
9381
dde86e2d
PZ
9382/*
9383 * Initialize reference clocks when the driver loads
9384 */
c39055b0 9385void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
dde86e2d 9386{
6e266956 9387 if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
c39055b0 9388 ironlake_init_pch_refclk(dev_priv);
6e266956 9389 else if (HAS_PCH_LPT(dev_priv))
c39055b0 9390 lpt_init_pch_refclk(dev_priv);
dde86e2d
PZ
9391}
9392
6ff93609 9393static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 9394{
fac5e23e 9395 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
79e53945
JB
9396 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9397 int pipe = intel_crtc->pipe;
c8203565
PZ
9398 uint32_t val;
9399
78114071 9400 val = 0;
c8203565 9401
6e3c9717 9402 switch (intel_crtc->config->pipe_bpp) {
c8203565 9403 case 18:
dfd07d72 9404 val |= PIPECONF_6BPC;
c8203565
PZ
9405 break;
9406 case 24:
dfd07d72 9407 val |= PIPECONF_8BPC;
c8203565
PZ
9408 break;
9409 case 30:
dfd07d72 9410 val |= PIPECONF_10BPC;
c8203565
PZ
9411 break;
9412 case 36:
dfd07d72 9413 val |= PIPECONF_12BPC;
c8203565
PZ
9414 break;
9415 default:
cc769b62
PZ
9416 /* Case prevented by intel_choose_pipe_bpp_dither. */
9417 BUG();
c8203565
PZ
9418 }
9419
6e3c9717 9420 if (intel_crtc->config->dither)
c8203565
PZ
9421 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
9422
6e3c9717 9423 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
9424 val |= PIPECONF_INTERLACED_ILK;
9425 else
9426 val |= PIPECONF_PROGRESSIVE;
9427
6e3c9717 9428 if (intel_crtc->config->limited_color_range)
3685a8f3 9429 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 9430
c8203565
PZ
9431 I915_WRITE(PIPECONF(pipe), val);
9432 POSTING_READ(PIPECONF(pipe));
9433}
9434
6ff93609 9435static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 9436{
fac5e23e 9437 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
ee2b0b38 9438 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 9439 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
391bf048 9440 u32 val = 0;
ee2b0b38 9441
391bf048 9442 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
ee2b0b38
PZ
9443 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
9444
6e3c9717 9445 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
9446 val |= PIPECONF_INTERLACED_ILK;
9447 else
9448 val |= PIPECONF_PROGRESSIVE;
9449
702e7a56
PZ
9450 I915_WRITE(PIPECONF(cpu_transcoder), val);
9451 POSTING_READ(PIPECONF(cpu_transcoder));
391bf048
JN
9452}
9453
391bf048
JN
9454static void haswell_set_pipemisc(struct drm_crtc *crtc)
9455{
fac5e23e 9456 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
391bf048 9457 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756f85cf 9458
391bf048
JN
9459 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
9460 u32 val = 0;
756f85cf 9461
6e3c9717 9462 switch (intel_crtc->config->pipe_bpp) {
756f85cf
PZ
9463 case 18:
9464 val |= PIPEMISC_DITHER_6_BPC;
9465 break;
9466 case 24:
9467 val |= PIPEMISC_DITHER_8_BPC;
9468 break;
9469 case 30:
9470 val |= PIPEMISC_DITHER_10_BPC;
9471 break;
9472 case 36:
9473 val |= PIPEMISC_DITHER_12_BPC;
9474 break;
9475 default:
9476 /* Case prevented by pipe_config_set_bpp. */
9477 BUG();
9478 }
9479
6e3c9717 9480 if (intel_crtc->config->dither)
756f85cf
PZ
9481 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
9482
391bf048 9483 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
756f85cf 9484 }
ee2b0b38
PZ
9485}
9486
d4b1931c
PZ
9487int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
9488{
9489 /*
9490 * Account for spread spectrum to avoid
9491 * oversubscribing the link. Max center spread
9492 * is 2.5%; use 5% for safety's sake.
9493 */
9494 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 9495 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
9496}
9497
7429e9d4 9498static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 9499{
7429e9d4 9500 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
9501}
9502
b75ca6f6
ACO
9503static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
9504 struct intel_crtc_state *crtc_state,
9e2c8475 9505 struct dpll *reduced_clock)
79e53945 9506{
de13a2e3 9507 struct drm_crtc *crtc = &intel_crtc->base;
79e53945 9508 struct drm_device *dev = crtc->dev;
fac5e23e 9509 struct drm_i915_private *dev_priv = to_i915(dev);
b75ca6f6 9510 u32 dpll, fp, fp2;
3d6e9ee0 9511 int factor;
79e53945 9512
c1858123 9513 /* Enable autotuning of the PLL clock (if permissible) */
8febb297 9514 factor = 21;
3d6e9ee0 9515 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8febb297 9516 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 9517 dev_priv->vbt.lvds_ssc_freq == 100000) ||
6e266956 9518 (HAS_PCH_IBX(dev_priv) && intel_is_dual_link_lvds(dev)))
8febb297 9519 factor = 25;
190f68c5 9520 } else if (crtc_state->sdvo_tv_clock)
8febb297 9521 factor = 20;
c1858123 9522
b75ca6f6
ACO
9523 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
9524
190f68c5 9525 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
b75ca6f6
ACO
9526 fp |= FP_CB_TUNE;
9527
9528 if (reduced_clock) {
9529 fp2 = i9xx_dpll_compute_fp(reduced_clock);
2c07245f 9530
b75ca6f6
ACO
9531 if (reduced_clock->m < factor * reduced_clock->n)
9532 fp2 |= FP_CB_TUNE;
9533 } else {
9534 fp2 = fp;
9535 }
9a7c7890 9536
5eddb70b 9537 dpll = 0;
2c07245f 9538
3d6e9ee0 9539 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
a07d6787
EA
9540 dpll |= DPLLB_MODE_LVDS;
9541 else
9542 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 9543
190f68c5 9544 dpll |= (crtc_state->pixel_multiplier - 1)
ef1b460d 9545 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f 9546
3d6e9ee0
VS
9547 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
9548 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
4a33e48d 9549 dpll |= DPLL_SDVO_HIGH_SPEED;
3d6e9ee0 9550
37a5650b 9551 if (intel_crtc_has_dp_encoder(crtc_state))
4a33e48d 9552 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 9553
7d7f8633
VS
9554 /*
9555 * The high speed IO clock is only really required for
9556 * SDVO/HDMI/DP, but we also enable it for CRT to make it
9557 * possible to share the DPLL between CRT and HDMI. Enabling
9558 * the clock needlessly does no real harm, except use up a
9559 * bit of power potentially.
9560 *
9561 * We'll limit this to IVB with 3 pipes, since it has only two
9562 * DPLLs and so DPLL sharing is the only way to get three pipes
9563 * driving PCH ports at the same time. On SNB we could do this,
9564 * and potentially avoid enabling the second DPLL, but it's not
9565 * clear if it''s a win or loss power wise. No point in doing
9566 * this on ILK at all since it has a fixed DPLL<->pipe mapping.
9567 */
9568 if (INTEL_INFO(dev_priv)->num_pipes == 3 &&
9569 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
9570 dpll |= DPLL_SDVO_HIGH_SPEED;
9571
a07d6787 9572 /* compute bitmask from p1 value */
190f68c5 9573 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 9574 /* also FPA1 */
190f68c5 9575 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 9576
190f68c5 9577 switch (crtc_state->dpll.p2) {
a07d6787
EA
9578 case 5:
9579 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
9580 break;
9581 case 7:
9582 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
9583 break;
9584 case 10:
9585 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
9586 break;
9587 case 14:
9588 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
9589 break;
79e53945
JB
9590 }
9591
3d6e9ee0
VS
9592 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
9593 intel_panel_use_ssc(dev_priv))
43565a06 9594 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
9595 else
9596 dpll |= PLL_REF_INPUT_DREFCLK;
9597
b75ca6f6
ACO
9598 dpll |= DPLL_VCO_ENABLE;
9599
9600 crtc_state->dpll_hw_state.dpll = dpll;
9601 crtc_state->dpll_hw_state.fp0 = fp;
9602 crtc_state->dpll_hw_state.fp1 = fp2;
de13a2e3
PZ
9603}
9604
190f68c5
ACO
9605static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
9606 struct intel_crtc_state *crtc_state)
de13a2e3 9607{
997c030c 9608 struct drm_device *dev = crtc->base.dev;
fac5e23e 9609 struct drm_i915_private *dev_priv = to_i915(dev);
9e2c8475 9610 struct dpll reduced_clock;
7ed9f894 9611 bool has_reduced_clock = false;
e2b78267 9612 struct intel_shared_dpll *pll;
1b6f4958 9613 const struct intel_limit *limit;
997c030c 9614 int refclk = 120000;
de13a2e3 9615
dd3cd74a
ACO
9616 memset(&crtc_state->dpll_hw_state, 0,
9617 sizeof(crtc_state->dpll_hw_state));
9618
ded220e2
ACO
9619 crtc->lowfreq_avail = false;
9620
9621 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
9622 if (!crtc_state->has_pch_encoder)
9623 return 0;
79e53945 9624
2d84d2b3 9625 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
997c030c
ACO
9626 if (intel_panel_use_ssc(dev_priv)) {
9627 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
9628 dev_priv->vbt.lvds_ssc_freq);
9629 refclk = dev_priv->vbt.lvds_ssc_freq;
9630 }
9631
9632 if (intel_is_dual_link_lvds(dev)) {
9633 if (refclk == 100000)
9634 limit = &intel_limits_ironlake_dual_lvds_100m;
9635 else
9636 limit = &intel_limits_ironlake_dual_lvds;
9637 } else {
9638 if (refclk == 100000)
9639 limit = &intel_limits_ironlake_single_lvds_100m;
9640 else
9641 limit = &intel_limits_ironlake_single_lvds;
9642 }
9643 } else {
9644 limit = &intel_limits_ironlake_dac;
9645 }
9646
364ee29d 9647 if (!crtc_state->clock_set &&
997c030c
ACO
9648 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9649 refclk, NULL, &crtc_state->dpll)) {
364ee29d
ACO
9650 DRM_ERROR("Couldn't find PLL settings for mode!\n");
9651 return -EINVAL;
f47709a9 9652 }
79e53945 9653
b75ca6f6
ACO
9654 ironlake_compute_dpll(crtc, crtc_state,
9655 has_reduced_clock ? &reduced_clock : NULL);
66e985c0 9656
ded220e2
ACO
9657 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
9658 if (pll == NULL) {
9659 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
9660 pipe_name(crtc->pipe));
9661 return -EINVAL;
3fb37703 9662 }
79e53945 9663
2d84d2b3 9664 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ded220e2 9665 has_reduced_clock)
c7653199 9666 crtc->lowfreq_avail = true;
e2b78267 9667
c8f7a0db 9668 return 0;
79e53945
JB
9669}
9670
eb14cb74
VS
9671static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
9672 struct intel_link_m_n *m_n)
9673{
9674 struct drm_device *dev = crtc->base.dev;
fac5e23e 9675 struct drm_i915_private *dev_priv = to_i915(dev);
eb14cb74
VS
9676 enum pipe pipe = crtc->pipe;
9677
9678 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
9679 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
9680 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
9681 & ~TU_SIZE_MASK;
9682 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
9683 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
9684 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9685}
9686
9687static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
9688 enum transcoder transcoder,
b95af8be
VK
9689 struct intel_link_m_n *m_n,
9690 struct intel_link_m_n *m2_n2)
72419203 9691{
6315b5d3 9692 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
eb14cb74 9693 enum pipe pipe = crtc->pipe;
72419203 9694
6315b5d3 9695 if (INTEL_GEN(dev_priv) >= 5) {
eb14cb74
VS
9696 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
9697 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
9698 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
9699 & ~TU_SIZE_MASK;
9700 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
9701 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
9702 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
b95af8be
VK
9703 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
9704 * gen < 8) and if DRRS is supported (to make sure the
9705 * registers are not unnecessarily read).
9706 */
6315b5d3 9707 if (m2_n2 && INTEL_GEN(dev_priv) < 8 &&
6e3c9717 9708 crtc->config->has_drrs) {
b95af8be
VK
9709 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
9710 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
9711 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
9712 & ~TU_SIZE_MASK;
9713 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
9714 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
9715 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9716 }
eb14cb74
VS
9717 } else {
9718 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
9719 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
9720 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
9721 & ~TU_SIZE_MASK;
9722 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
9723 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
9724 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9725 }
9726}
9727
9728void intel_dp_get_m_n(struct intel_crtc *crtc,
5cec258b 9729 struct intel_crtc_state *pipe_config)
eb14cb74 9730{
681a8504 9731 if (pipe_config->has_pch_encoder)
eb14cb74
VS
9732 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
9733 else
9734 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be
VK
9735 &pipe_config->dp_m_n,
9736 &pipe_config->dp_m2_n2);
eb14cb74 9737}
72419203 9738
eb14cb74 9739static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5cec258b 9740 struct intel_crtc_state *pipe_config)
eb14cb74
VS
9741{
9742 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be 9743 &pipe_config->fdi_m_n, NULL);
72419203
DV
9744}
9745
bd2e244f 9746static void skylake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9747 struct intel_crtc_state *pipe_config)
bd2e244f
JB
9748{
9749 struct drm_device *dev = crtc->base.dev;
fac5e23e 9750 struct drm_i915_private *dev_priv = to_i915(dev);
a1b2278e
CK
9751 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9752 uint32_t ps_ctrl = 0;
9753 int id = -1;
9754 int i;
bd2e244f 9755
a1b2278e
CK
9756 /* find scaler attached to this pipe */
9757 for (i = 0; i < crtc->num_scalers; i++) {
9758 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9759 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9760 id = i;
9761 pipe_config->pch_pfit.enabled = true;
9762 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9763 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9764 break;
9765 }
9766 }
bd2e244f 9767
a1b2278e
CK
9768 scaler_state->scaler_id = id;
9769 if (id >= 0) {
9770 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9771 } else {
9772 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
bd2e244f
JB
9773 }
9774}
9775
5724dbd1
DL
9776static void
9777skylake_get_initial_plane_config(struct intel_crtc *crtc,
9778 struct intel_initial_plane_config *plane_config)
bc8d7dff
DL
9779{
9780 struct drm_device *dev = crtc->base.dev;
fac5e23e 9781 struct drm_i915_private *dev_priv = to_i915(dev);
40f46283 9782 u32 val, base, offset, stride_mult, tiling;
bc8d7dff
DL
9783 int pipe = crtc->pipe;
9784 int fourcc, pixel_format;
6761dd31 9785 unsigned int aligned_height;
bc8d7dff 9786 struct drm_framebuffer *fb;
1b842c89 9787 struct intel_framebuffer *intel_fb;
bc8d7dff 9788
d9806c9f 9789 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9790 if (!intel_fb) {
bc8d7dff
DL
9791 DRM_DEBUG_KMS("failed to alloc fb\n");
9792 return;
9793 }
9794
1b842c89
DL
9795 fb = &intel_fb->base;
9796
d2e9f5fc
VS
9797 fb->dev = dev;
9798
bc8d7dff 9799 val = I915_READ(PLANE_CTL(pipe, 0));
42a7b088
DL
9800 if (!(val & PLANE_CTL_ENABLE))
9801 goto error;
9802
bc8d7dff
DL
9803 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9804 fourcc = skl_format_to_fourcc(pixel_format,
9805 val & PLANE_CTL_ORDER_RGBX,
9806 val & PLANE_CTL_ALPHA_MASK);
2f3f4763 9807 fb->format = drm_format_info(fourcc);
bc8d7dff 9808
40f46283
DL
9809 tiling = val & PLANE_CTL_TILED_MASK;
9810 switch (tiling) {
9811 case PLANE_CTL_TILED_LINEAR:
bae781b2 9812 fb->modifier = DRM_FORMAT_MOD_NONE;
40f46283
DL
9813 break;
9814 case PLANE_CTL_TILED_X:
9815 plane_config->tiling = I915_TILING_X;
bae781b2 9816 fb->modifier = I915_FORMAT_MOD_X_TILED;
40f46283
DL
9817 break;
9818 case PLANE_CTL_TILED_Y:
bae781b2 9819 fb->modifier = I915_FORMAT_MOD_Y_TILED;
40f46283
DL
9820 break;
9821 case PLANE_CTL_TILED_YF:
bae781b2 9822 fb->modifier = I915_FORMAT_MOD_Yf_TILED;
40f46283
DL
9823 break;
9824 default:
9825 MISSING_CASE(tiling);
9826 goto error;
9827 }
9828
bc8d7dff
DL
9829 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9830 plane_config->base = base;
9831
9832 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9833
9834 val = I915_READ(PLANE_SIZE(pipe, 0));
9835 fb->height = ((val >> 16) & 0xfff) + 1;
9836 fb->width = ((val >> 0) & 0x1fff) + 1;
9837
9838 val = I915_READ(PLANE_STRIDE(pipe, 0));
bae781b2 9839 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier,
438b74a5 9840 fb->format->format);
bc8d7dff
DL
9841 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9842
9843 aligned_height = intel_fb_align_height(dev, fb->height,
438b74a5 9844 fb->format->format,
bae781b2 9845 fb->modifier);
bc8d7dff 9846
f37b5c2b 9847 plane_config->size = fb->pitches[0] * aligned_height;
bc8d7dff
DL
9848
9849 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9850 pipe_name(pipe), fb->width, fb->height,
272725c7 9851 fb->format->cpp[0] * 8, base, fb->pitches[0],
bc8d7dff
DL
9852 plane_config->size);
9853
2d14030b 9854 plane_config->fb = intel_fb;
bc8d7dff
DL
9855 return;
9856
9857error:
d1a3a036 9858 kfree(intel_fb);
bc8d7dff
DL
9859}
9860
2fa2fe9a 9861static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9862 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
9863{
9864 struct drm_device *dev = crtc->base.dev;
fac5e23e 9865 struct drm_i915_private *dev_priv = to_i915(dev);
2fa2fe9a
DV
9866 uint32_t tmp;
9867
9868 tmp = I915_READ(PF_CTL(crtc->pipe));
9869
9870 if (tmp & PF_ENABLE) {
fd4daa9c 9871 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
DV
9872 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9873 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
DV
9874
9875 /* We currently do not free assignements of panel fitters on
9876 * ivb/hsw (since we don't use the higher upscaling modes which
9877 * differentiates them) so just WARN about this case for now. */
5db94019 9878 if (IS_GEN7(dev_priv)) {
cb8b2a30
DV
9879 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9880 PF_PIPE_SEL_IVB(crtc->pipe));
9881 }
2fa2fe9a 9882 }
79e53945
JB
9883}
9884
5724dbd1
DL
9885static void
9886ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9887 struct intel_initial_plane_config *plane_config)
4c6baa59
JB
9888{
9889 struct drm_device *dev = crtc->base.dev;
fac5e23e 9890 struct drm_i915_private *dev_priv = to_i915(dev);
4c6baa59 9891 u32 val, base, offset;
aeee5a49 9892 int pipe = crtc->pipe;
4c6baa59 9893 int fourcc, pixel_format;
6761dd31 9894 unsigned int aligned_height;
b113d5ee 9895 struct drm_framebuffer *fb;
1b842c89 9896 struct intel_framebuffer *intel_fb;
4c6baa59 9897
42a7b088
DL
9898 val = I915_READ(DSPCNTR(pipe));
9899 if (!(val & DISPLAY_PLANE_ENABLE))
9900 return;
9901
d9806c9f 9902 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9903 if (!intel_fb) {
4c6baa59
JB
9904 DRM_DEBUG_KMS("failed to alloc fb\n");
9905 return;
9906 }
9907
1b842c89
DL
9908 fb = &intel_fb->base;
9909
d2e9f5fc
VS
9910 fb->dev = dev;
9911
6315b5d3 9912 if (INTEL_GEN(dev_priv) >= 4) {
18c5247e 9913 if (val & DISPPLANE_TILED) {
49af449b 9914 plane_config->tiling = I915_TILING_X;
bae781b2 9915 fb->modifier = I915_FORMAT_MOD_X_TILED;
18c5247e
DV
9916 }
9917 }
4c6baa59
JB
9918
9919 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 9920 fourcc = i9xx_format_to_fourcc(pixel_format);
2f3f4763 9921 fb->format = drm_format_info(fourcc);
4c6baa59 9922
aeee5a49 9923 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
8652744b 9924 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
aeee5a49 9925 offset = I915_READ(DSPOFFSET(pipe));
4c6baa59 9926 } else {
49af449b 9927 if (plane_config->tiling)
aeee5a49 9928 offset = I915_READ(DSPTILEOFF(pipe));
4c6baa59 9929 else
aeee5a49 9930 offset = I915_READ(DSPLINOFF(pipe));
4c6baa59
JB
9931 }
9932 plane_config->base = base;
9933
9934 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
9935 fb->width = ((val >> 16) & 0xfff) + 1;
9936 fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
9937
9938 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 9939 fb->pitches[0] = val & 0xffffffc0;
4c6baa59 9940
b113d5ee 9941 aligned_height = intel_fb_align_height(dev, fb->height,
438b74a5 9942 fb->format->format,
bae781b2 9943 fb->modifier);
4c6baa59 9944
f37b5c2b 9945 plane_config->size = fb->pitches[0] * aligned_height;
4c6baa59 9946
2844a921
DL
9947 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9948 pipe_name(pipe), fb->width, fb->height,
272725c7 9949 fb->format->cpp[0] * 8, base, fb->pitches[0],
2844a921 9950 plane_config->size);
b113d5ee 9951
2d14030b 9952 plane_config->fb = intel_fb;
4c6baa59
JB
9953}
9954
0e8ffe1b 9955static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9956 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
9957{
9958 struct drm_device *dev = crtc->base.dev;
fac5e23e 9959 struct drm_i915_private *dev_priv = to_i915(dev);
1729050e 9960 enum intel_display_power_domain power_domain;
0e8ffe1b 9961 uint32_t tmp;
1729050e 9962 bool ret;
0e8ffe1b 9963
1729050e
ID
9964 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9965 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
930e8c9e
PZ
9966 return false;
9967
e143a21c 9968 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 9969 pipe_config->shared_dpll = NULL;
eccb140b 9970
1729050e 9971 ret = false;
0e8ffe1b
DV
9972 tmp = I915_READ(PIPECONF(crtc->pipe));
9973 if (!(tmp & PIPECONF_ENABLE))
1729050e 9974 goto out;
0e8ffe1b 9975
42571aef
VS
9976 switch (tmp & PIPECONF_BPC_MASK) {
9977 case PIPECONF_6BPC:
9978 pipe_config->pipe_bpp = 18;
9979 break;
9980 case PIPECONF_8BPC:
9981 pipe_config->pipe_bpp = 24;
9982 break;
9983 case PIPECONF_10BPC:
9984 pipe_config->pipe_bpp = 30;
9985 break;
9986 case PIPECONF_12BPC:
9987 pipe_config->pipe_bpp = 36;
9988 break;
9989 default:
9990 break;
9991 }
9992
b5a9fa09
DV
9993 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9994 pipe_config->limited_color_range = true;
9995
ab9412ba 9996 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0 9997 struct intel_shared_dpll *pll;
8106ddbd 9998 enum intel_dpll_id pll_id;
66e985c0 9999
88adfff1
DV
10000 pipe_config->has_pch_encoder = true;
10001
627eb5a3
DV
10002 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
10003 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
10004 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
10005
10006 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 10007
2d1fe073 10008 if (HAS_PCH_IBX(dev_priv)) {
d9a7bc67
ID
10009 /*
10010 * The pipe->pch transcoder and pch transcoder->pll
10011 * mapping is fixed.
10012 */
8106ddbd 10013 pll_id = (enum intel_dpll_id) crtc->pipe;
c0d43d62
DV
10014 } else {
10015 tmp = I915_READ(PCH_DPLL_SEL);
10016 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
8106ddbd 10017 pll_id = DPLL_ID_PCH_PLL_B;
c0d43d62 10018 else
8106ddbd 10019 pll_id= DPLL_ID_PCH_PLL_A;
c0d43d62 10020 }
66e985c0 10021
8106ddbd
ACO
10022 pipe_config->shared_dpll =
10023 intel_get_shared_dpll_by_id(dev_priv, pll_id);
10024 pll = pipe_config->shared_dpll;
66e985c0 10025
2edd6443
ACO
10026 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
10027 &pipe_config->dpll_hw_state));
c93f54cf
DV
10028
10029 tmp = pipe_config->dpll_hw_state.dpll;
10030 pipe_config->pixel_multiplier =
10031 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
10032 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
10033
10034 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
DV
10035 } else {
10036 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
10037 }
10038
1bd1bd80 10039 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 10040 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 10041
2fa2fe9a
DV
10042 ironlake_get_pfit_config(crtc, pipe_config);
10043
1729050e
ID
10044 ret = true;
10045
10046out:
10047 intel_display_power_put(dev_priv, power_domain);
10048
10049 return ret;
0e8ffe1b
DV
10050}
10051
be256dc7
PZ
10052static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
10053{
91c8a326 10054 struct drm_device *dev = &dev_priv->drm;
be256dc7 10055 struct intel_crtc *crtc;
be256dc7 10056
d3fcc808 10057 for_each_intel_crtc(dev, crtc)
e2c719b7 10058 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
10059 pipe_name(crtc->pipe));
10060
e2c719b7
RC
10061 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
10062 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
01403de3
VS
10063 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
10064 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
44cb734c 10065 I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
e2c719b7 10066 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
be256dc7 10067 "CPU PWM1 enabled\n");
772c2a51 10068 if (IS_HASWELL(dev_priv))
e2c719b7 10069 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
c5107b87 10070 "CPU PWM2 enabled\n");
e2c719b7 10071 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
be256dc7 10072 "PCH PWM1 enabled\n");
e2c719b7 10073 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
be256dc7 10074 "Utility pin enabled\n");
e2c719b7 10075 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
be256dc7 10076
9926ada1
PZ
10077 /*
10078 * In theory we can still leave IRQs enabled, as long as only the HPD
10079 * interrupts remain enabled. We used to check for that, but since it's
10080 * gen-specific and since we only disable LCPLL after we fully disable
10081 * the interrupts, the check below should be enough.
10082 */
e2c719b7 10083 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
be256dc7
PZ
10084}
10085
9ccd5aeb
PZ
10086static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
10087{
772c2a51 10088 if (IS_HASWELL(dev_priv))
9ccd5aeb
PZ
10089 return I915_READ(D_COMP_HSW);
10090 else
10091 return I915_READ(D_COMP_BDW);
10092}
10093
3c4c9b81
PZ
10094static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
10095{
772c2a51 10096 if (IS_HASWELL(dev_priv)) {
3c4c9b81
PZ
10097 mutex_lock(&dev_priv->rps.hw_lock);
10098 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
10099 val))
79cf219a 10100 DRM_DEBUG_KMS("Failed to write to D_COMP\n");
3c4c9b81
PZ
10101 mutex_unlock(&dev_priv->rps.hw_lock);
10102 } else {
9ccd5aeb
PZ
10103 I915_WRITE(D_COMP_BDW, val);
10104 POSTING_READ(D_COMP_BDW);
3c4c9b81 10105 }
be256dc7
PZ
10106}
10107
10108/*
10109 * This function implements pieces of two sequences from BSpec:
10110 * - Sequence for display software to disable LCPLL
10111 * - Sequence for display software to allow package C8+
10112 * The steps implemented here are just the steps that actually touch the LCPLL
10113 * register. Callers should take care of disabling all the display engine
10114 * functions, doing the mode unset, fixing interrupts, etc.
10115 */
6ff58d53
PZ
10116static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
10117 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
10118{
10119 uint32_t val;
10120
10121 assert_can_disable_lcpll(dev_priv);
10122
10123 val = I915_READ(LCPLL_CTL);
10124
10125 if (switch_to_fclk) {
10126 val |= LCPLL_CD_SOURCE_FCLK;
10127 I915_WRITE(LCPLL_CTL, val);
10128
f53dd63f
ID
10129 if (wait_for_us(I915_READ(LCPLL_CTL) &
10130 LCPLL_CD_SOURCE_FCLK_DONE, 1))
be256dc7
PZ
10131 DRM_ERROR("Switching to FCLK failed\n");
10132
10133 val = I915_READ(LCPLL_CTL);
10134 }
10135
10136 val |= LCPLL_PLL_DISABLE;
10137 I915_WRITE(LCPLL_CTL, val);
10138 POSTING_READ(LCPLL_CTL);
10139
24d8441d 10140 if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
be256dc7
PZ
10141 DRM_ERROR("LCPLL still locked\n");
10142
9ccd5aeb 10143 val = hsw_read_dcomp(dev_priv);
be256dc7 10144 val |= D_COMP_COMP_DISABLE;
3c4c9b81 10145 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
10146 ndelay(100);
10147
9ccd5aeb
PZ
10148 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
10149 1))
be256dc7
PZ
10150 DRM_ERROR("D_COMP RCOMP still in progress\n");
10151
10152 if (allow_power_down) {
10153 val = I915_READ(LCPLL_CTL);
10154 val |= LCPLL_POWER_DOWN_ALLOW;
10155 I915_WRITE(LCPLL_CTL, val);
10156 POSTING_READ(LCPLL_CTL);
10157 }
10158}
10159
10160/*
10161 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
10162 * source.
10163 */
6ff58d53 10164static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
10165{
10166 uint32_t val;
10167
10168 val = I915_READ(LCPLL_CTL);
10169
10170 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
10171 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
10172 return;
10173
a8a8bd54
PZ
10174 /*
10175 * Make sure we're not on PC8 state before disabling PC8, otherwise
10176 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
a8a8bd54 10177 */
59bad947 10178 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
215733fa 10179
be256dc7
PZ
10180 if (val & LCPLL_POWER_DOWN_ALLOW) {
10181 val &= ~LCPLL_POWER_DOWN_ALLOW;
10182 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 10183 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
10184 }
10185
9ccd5aeb 10186 val = hsw_read_dcomp(dev_priv);
be256dc7
PZ
10187 val |= D_COMP_COMP_FORCE;
10188 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 10189 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
10190
10191 val = I915_READ(LCPLL_CTL);
10192 val &= ~LCPLL_PLL_DISABLE;
10193 I915_WRITE(LCPLL_CTL, val);
10194
93220c08
CW
10195 if (intel_wait_for_register(dev_priv,
10196 LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
10197 5))
be256dc7
PZ
10198 DRM_ERROR("LCPLL not locked yet\n");
10199
10200 if (val & LCPLL_CD_SOURCE_FCLK) {
10201 val = I915_READ(LCPLL_CTL);
10202 val &= ~LCPLL_CD_SOURCE_FCLK;
10203 I915_WRITE(LCPLL_CTL, val);
10204
f53dd63f
ID
10205 if (wait_for_us((I915_READ(LCPLL_CTL) &
10206 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
be256dc7
PZ
10207 DRM_ERROR("Switching back to LCPLL failed\n");
10208 }
215733fa 10209
59bad947 10210 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4c75b940 10211 intel_update_cdclk(dev_priv);
be256dc7
PZ
10212}
10213
765dab67
PZ
10214/*
10215 * Package states C8 and deeper are really deep PC states that can only be
10216 * reached when all the devices on the system allow it, so even if the graphics
10217 * device allows PC8+, it doesn't mean the system will actually get to these
10218 * states. Our driver only allows PC8+ when going into runtime PM.
10219 *
10220 * The requirements for PC8+ are that all the outputs are disabled, the power
10221 * well is disabled and most interrupts are disabled, and these are also
10222 * requirements for runtime PM. When these conditions are met, we manually do
10223 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
10224 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
10225 * hang the machine.
10226 *
10227 * When we really reach PC8 or deeper states (not just when we allow it) we lose
10228 * the state of some registers, so when we come back from PC8+ we need to
10229 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
10230 * need to take care of the registers kept by RC6. Notice that this happens even
10231 * if we don't put the device in PCI D3 state (which is what currently happens
10232 * because of the runtime PM support).
10233 *
10234 * For more, read "Display Sequences for Package C8" on the hardware
10235 * documentation.
10236 */
a14cb6fc 10237void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 10238{
c67a470b
PZ
10239 uint32_t val;
10240
c67a470b
PZ
10241 DRM_DEBUG_KMS("Enabling package C8+\n");
10242
4f8036a2 10243 if (HAS_PCH_LPT_LP(dev_priv)) {
c67a470b
PZ
10244 val = I915_READ(SOUTH_DSPCLK_GATE_D);
10245 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
10246 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
10247 }
10248
c39055b0 10249 lpt_disable_clkout_dp(dev_priv);
c67a470b
PZ
10250 hsw_disable_lcpll(dev_priv, true, true);
10251}
10252
a14cb6fc 10253void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b 10254{
c67a470b
PZ
10255 uint32_t val;
10256
c67a470b
PZ
10257 DRM_DEBUG_KMS("Disabling package C8+\n");
10258
10259 hsw_restore_lcpll(dev_priv);
c39055b0 10260 lpt_init_pch_refclk(dev_priv);
c67a470b 10261
4f8036a2 10262 if (HAS_PCH_LPT_LP(dev_priv)) {
c67a470b
PZ
10263 val = I915_READ(SOUTH_DSPCLK_GATE_D);
10264 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
10265 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
10266 }
c67a470b
PZ
10267}
10268
324513c0 10269static void bxt_modeset_commit_cdclk(struct drm_atomic_state *old_state)
f8437dd1 10270{
a821fc46 10271 struct drm_device *dev = old_state->dev;
1a617b77
ML
10272 struct intel_atomic_state *old_intel_state =
10273 to_intel_atomic_state(old_state);
10274 unsigned int req_cdclk = old_intel_state->dev_cdclk;
f8437dd1 10275
324513c0 10276 bxt_set_cdclk(to_i915(dev), req_cdclk);
f8437dd1
VK
10277}
10278
b30ce9e0
DP
10279static int bdw_adjust_min_pipe_pixel_rate(struct intel_crtc_state *crtc_state,
10280 int pixel_rate)
10281{
9c754024
DP
10282 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
10283
b30ce9e0 10284 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
9c754024 10285 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
b30ce9e0
DP
10286 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
10287
10288 /* BSpec says "Do not use DisplayPort with CDCLK less than
10289 * 432 MHz, audio enabled, port width x4, and link rate
10290 * HBR2 (5.4 GHz), or else there may be audio corruption or
10291 * screen corruption."
10292 */
10293 if (intel_crtc_has_dp_encoder(crtc_state) &&
10294 crtc_state->has_audio &&
10295 crtc_state->port_clock >= 540000 &&
10296 crtc_state->lane_count == 4)
10297 pixel_rate = max(432000, pixel_rate);
10298
10299 return pixel_rate;
10300}
10301
b432e5cf 10302/* compute the max rate for new configuration */
27c329ed 10303static int ilk_max_pixel_rate(struct drm_atomic_state *state)
b432e5cf 10304{
565602d7 10305 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 10306 struct drm_i915_private *dev_priv = to_i915(state->dev);
565602d7
ML
10307 struct drm_crtc *crtc;
10308 struct drm_crtc_state *cstate;
27c329ed 10309 struct intel_crtc_state *crtc_state;
565602d7
ML
10310 unsigned max_pixel_rate = 0, i;
10311 enum pipe pipe;
b432e5cf 10312
565602d7
ML
10313 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
10314 sizeof(intel_state->min_pixclk));
27c329ed 10315
565602d7
ML
10316 for_each_crtc_in_state(state, crtc, cstate, i) {
10317 int pixel_rate;
27c329ed 10318
565602d7
ML
10319 crtc_state = to_intel_crtc_state(cstate);
10320 if (!crtc_state->base.enable) {
10321 intel_state->min_pixclk[i] = 0;
b432e5cf 10322 continue;
565602d7 10323 }
b432e5cf 10324
27c329ed 10325 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
b432e5cf 10326
9c754024 10327 if (IS_BROADWELL(dev_priv) || IS_GEN9(dev_priv))
b30ce9e0
DP
10328 pixel_rate = bdw_adjust_min_pipe_pixel_rate(crtc_state,
10329 pixel_rate);
b432e5cf 10330
565602d7 10331 intel_state->min_pixclk[i] = pixel_rate;
b432e5cf
VS
10332 }
10333
565602d7
ML
10334 for_each_pipe(dev_priv, pipe)
10335 max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
10336
b432e5cf
VS
10337 return max_pixel_rate;
10338}
10339
10340static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
10341{
fac5e23e 10342 struct drm_i915_private *dev_priv = to_i915(dev);
b432e5cf
VS
10343 uint32_t val, data;
10344 int ret;
10345
10346 if (WARN((I915_READ(LCPLL_CTL) &
10347 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
10348 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
10349 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
10350 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
10351 "trying to change cdclk frequency with cdclk not enabled\n"))
10352 return;
10353
10354 mutex_lock(&dev_priv->rps.hw_lock);
10355 ret = sandybridge_pcode_write(dev_priv,
10356 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
10357 mutex_unlock(&dev_priv->rps.hw_lock);
10358 if (ret) {
10359 DRM_ERROR("failed to inform pcode about cdclk change\n");
10360 return;
10361 }
10362
10363 val = I915_READ(LCPLL_CTL);
10364 val |= LCPLL_CD_SOURCE_FCLK;
10365 I915_WRITE(LCPLL_CTL, val);
10366
5ba00178
TU
10367 if (wait_for_us(I915_READ(LCPLL_CTL) &
10368 LCPLL_CD_SOURCE_FCLK_DONE, 1))
b432e5cf
VS
10369 DRM_ERROR("Switching to FCLK failed\n");
10370
10371 val = I915_READ(LCPLL_CTL);
10372 val &= ~LCPLL_CLK_FREQ_MASK;
10373
10374 switch (cdclk) {
10375 case 450000:
10376 val |= LCPLL_CLK_FREQ_450;
10377 data = 0;
10378 break;
10379 case 540000:
10380 val |= LCPLL_CLK_FREQ_54O_BDW;
10381 data = 1;
10382 break;
10383 case 337500:
10384 val |= LCPLL_CLK_FREQ_337_5_BDW;
10385 data = 2;
10386 break;
10387 case 675000:
10388 val |= LCPLL_CLK_FREQ_675_BDW;
10389 data = 3;
10390 break;
10391 default:
10392 WARN(1, "invalid cdclk frequency\n");
10393 return;
10394 }
10395
10396 I915_WRITE(LCPLL_CTL, val);
10397
10398 val = I915_READ(LCPLL_CTL);
10399 val &= ~LCPLL_CD_SOURCE_FCLK;
10400 I915_WRITE(LCPLL_CTL, val);
10401
5ba00178
TU
10402 if (wait_for_us((I915_READ(LCPLL_CTL) &
10403 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
b432e5cf
VS
10404 DRM_ERROR("Switching back to LCPLL failed\n");
10405
10406 mutex_lock(&dev_priv->rps.hw_lock);
10407 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
10408 mutex_unlock(&dev_priv->rps.hw_lock);
10409
7f1052a8
VS
10410 I915_WRITE(CDCLK_FREQ, DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
10411
4c75b940 10412 intel_update_cdclk(dev_priv);
b432e5cf
VS
10413
10414 WARN(cdclk != dev_priv->cdclk_freq,
10415 "cdclk requested %d kHz but got %d kHz\n",
10416 cdclk, dev_priv->cdclk_freq);
10417}
10418
587c7914
VS
10419static int broadwell_calc_cdclk(int max_pixclk)
10420{
10421 if (max_pixclk > 540000)
10422 return 675000;
10423 else if (max_pixclk > 450000)
10424 return 540000;
10425 else if (max_pixclk > 337500)
10426 return 450000;
10427 else
10428 return 337500;
10429}
10430
27c329ed 10431static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
b432e5cf 10432{
27c329ed 10433 struct drm_i915_private *dev_priv = to_i915(state->dev);
1a617b77 10434 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
27c329ed 10435 int max_pixclk = ilk_max_pixel_rate(state);
b432e5cf
VS
10436 int cdclk;
10437
10438 /*
10439 * FIXME should also account for plane ratio
10440 * once 64bpp pixel formats are supported.
10441 */
587c7914 10442 cdclk = broadwell_calc_cdclk(max_pixclk);
b432e5cf 10443
b432e5cf 10444 if (cdclk > dev_priv->max_cdclk_freq) {
63ba534e
ML
10445 DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
10446 cdclk, dev_priv->max_cdclk_freq);
10447 return -EINVAL;
b432e5cf
VS
10448 }
10449
1a617b77
ML
10450 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
10451 if (!intel_state->active_crtcs)
587c7914 10452 intel_state->dev_cdclk = broadwell_calc_cdclk(0);
b432e5cf
VS
10453
10454 return 0;
10455}
10456
27c329ed 10457static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
b432e5cf 10458{
27c329ed 10459 struct drm_device *dev = old_state->dev;
1a617b77
ML
10460 struct intel_atomic_state *old_intel_state =
10461 to_intel_atomic_state(old_state);
10462 unsigned req_cdclk = old_intel_state->dev_cdclk;
b432e5cf 10463
27c329ed 10464 broadwell_set_cdclk(dev, req_cdclk);
b432e5cf
VS
10465}
10466
c89e39f3
CT
10467static int skl_modeset_calc_cdclk(struct drm_atomic_state *state)
10468{
10469 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
10470 struct drm_i915_private *dev_priv = to_i915(state->dev);
10471 const int max_pixclk = ilk_max_pixel_rate(state);
a8ca4934 10472 int vco = intel_state->cdclk_pll_vco;
c89e39f3
CT
10473 int cdclk;
10474
10475 /*
10476 * FIXME should also account for plane ratio
10477 * once 64bpp pixel formats are supported.
10478 */
a8ca4934 10479 cdclk = skl_calc_cdclk(max_pixclk, vco);
c89e39f3
CT
10480
10481 /*
10482 * FIXME move the cdclk caclulation to
10483 * compute_config() so we can fail gracegully.
10484 */
10485 if (cdclk > dev_priv->max_cdclk_freq) {
10486 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
10487 cdclk, dev_priv->max_cdclk_freq);
10488 cdclk = dev_priv->max_cdclk_freq;
10489 }
10490
10491 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
10492 if (!intel_state->active_crtcs)
a8ca4934 10493 intel_state->dev_cdclk = skl_calc_cdclk(0, vco);
c89e39f3
CT
10494
10495 return 0;
10496}
10497
10498static void skl_modeset_commit_cdclk(struct drm_atomic_state *old_state)
10499{
1cd593e0
VS
10500 struct drm_i915_private *dev_priv = to_i915(old_state->dev);
10501 struct intel_atomic_state *intel_state = to_intel_atomic_state(old_state);
10502 unsigned int req_cdclk = intel_state->dev_cdclk;
10503 unsigned int req_vco = intel_state->cdclk_pll_vco;
c89e39f3 10504
1cd593e0 10505 skl_set_cdclk(dev_priv, req_cdclk, req_vco);
c89e39f3
CT
10506}
10507
190f68c5
ACO
10508static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
10509 struct intel_crtc_state *crtc_state)
09b4ddf9 10510{
d7edc4e5 10511 if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
af3997b5
MK
10512 if (!intel_ddi_pll_select(crtc, crtc_state))
10513 return -EINVAL;
10514 }
716c2e55 10515
c7653199 10516 crtc->lowfreq_avail = false;
644cef34 10517
c8f7a0db 10518 return 0;
79e53945
JB
10519}
10520
3760b59c
S
10521static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
10522 enum port port,
10523 struct intel_crtc_state *pipe_config)
10524{
8106ddbd
ACO
10525 enum intel_dpll_id id;
10526
3760b59c
S
10527 switch (port) {
10528 case PORT_A:
08250c4b 10529 id = DPLL_ID_SKL_DPLL0;
3760b59c
S
10530 break;
10531 case PORT_B:
08250c4b 10532 id = DPLL_ID_SKL_DPLL1;
3760b59c
S
10533 break;
10534 case PORT_C:
08250c4b 10535 id = DPLL_ID_SKL_DPLL2;
3760b59c
S
10536 break;
10537 default:
10538 DRM_ERROR("Incorrect port type\n");
8106ddbd 10539 return;
3760b59c 10540 }
8106ddbd
ACO
10541
10542 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
3760b59c
S
10543}
10544
96b7dfb7
S
10545static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
10546 enum port port,
5cec258b 10547 struct intel_crtc_state *pipe_config)
96b7dfb7 10548{
8106ddbd 10549 enum intel_dpll_id id;
a3c988ea 10550 u32 temp;
96b7dfb7
S
10551
10552 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
c856052a 10553 id = temp >> (port * 3 + 1);
96b7dfb7 10554
c856052a 10555 if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL3))
8106ddbd 10556 return;
8106ddbd
ACO
10557
10558 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
96b7dfb7
S
10559}
10560
7d2c8175
DL
10561static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
10562 enum port port,
5cec258b 10563 struct intel_crtc_state *pipe_config)
7d2c8175 10564{
8106ddbd 10565 enum intel_dpll_id id;
c856052a 10566 uint32_t ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
8106ddbd 10567
c856052a 10568 switch (ddi_pll_sel) {
7d2c8175 10569 case PORT_CLK_SEL_WRPLL1:
8106ddbd 10570 id = DPLL_ID_WRPLL1;
7d2c8175
DL
10571 break;
10572 case PORT_CLK_SEL_WRPLL2:
8106ddbd 10573 id = DPLL_ID_WRPLL2;
7d2c8175 10574 break;
00490c22 10575 case PORT_CLK_SEL_SPLL:
8106ddbd 10576 id = DPLL_ID_SPLL;
79bd23da 10577 break;
9d16da65
ACO
10578 case PORT_CLK_SEL_LCPLL_810:
10579 id = DPLL_ID_LCPLL_810;
10580 break;
10581 case PORT_CLK_SEL_LCPLL_1350:
10582 id = DPLL_ID_LCPLL_1350;
10583 break;
10584 case PORT_CLK_SEL_LCPLL_2700:
10585 id = DPLL_ID_LCPLL_2700;
10586 break;
8106ddbd 10587 default:
c856052a 10588 MISSING_CASE(ddi_pll_sel);
8106ddbd
ACO
10589 /* fall through */
10590 case PORT_CLK_SEL_NONE:
8106ddbd 10591 return;
7d2c8175 10592 }
8106ddbd
ACO
10593
10594 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
7d2c8175
DL
10595}
10596
cf30429e
JN
10597static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
10598 struct intel_crtc_state *pipe_config,
10599 unsigned long *power_domain_mask)
10600{
10601 struct drm_device *dev = crtc->base.dev;
fac5e23e 10602 struct drm_i915_private *dev_priv = to_i915(dev);
cf30429e
JN
10603 enum intel_display_power_domain power_domain;
10604 u32 tmp;
10605
d9a7bc67
ID
10606 /*
10607 * The pipe->transcoder mapping is fixed with the exception of the eDP
10608 * transcoder handled below.
10609 */
cf30429e
JN
10610 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
10611
10612 /*
10613 * XXX: Do intel_display_power_get_if_enabled before reading this (for
10614 * consistency and less surprising code; it's in always on power).
10615 */
10616 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
10617 if (tmp & TRANS_DDI_FUNC_ENABLE) {
10618 enum pipe trans_edp_pipe;
10619 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
10620 default:
10621 WARN(1, "unknown pipe linked to edp transcoder\n");
10622 case TRANS_DDI_EDP_INPUT_A_ONOFF:
10623 case TRANS_DDI_EDP_INPUT_A_ON:
10624 trans_edp_pipe = PIPE_A;
10625 break;
10626 case TRANS_DDI_EDP_INPUT_B_ONOFF:
10627 trans_edp_pipe = PIPE_B;
10628 break;
10629 case TRANS_DDI_EDP_INPUT_C_ONOFF:
10630 trans_edp_pipe = PIPE_C;
10631 break;
10632 }
10633
10634 if (trans_edp_pipe == crtc->pipe)
10635 pipe_config->cpu_transcoder = TRANSCODER_EDP;
10636 }
10637
10638 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
10639 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10640 return false;
10641 *power_domain_mask |= BIT(power_domain);
10642
10643 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
10644
10645 return tmp & PIPECONF_ENABLE;
10646}
10647
4d1de975
JN
10648static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
10649 struct intel_crtc_state *pipe_config,
10650 unsigned long *power_domain_mask)
10651{
10652 struct drm_device *dev = crtc->base.dev;
fac5e23e 10653 struct drm_i915_private *dev_priv = to_i915(dev);
4d1de975
JN
10654 enum intel_display_power_domain power_domain;
10655 enum port port;
10656 enum transcoder cpu_transcoder;
10657 u32 tmp;
10658
4d1de975
JN
10659 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
10660 if (port == PORT_A)
10661 cpu_transcoder = TRANSCODER_DSI_A;
10662 else
10663 cpu_transcoder = TRANSCODER_DSI_C;
10664
10665 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
10666 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10667 continue;
10668 *power_domain_mask |= BIT(power_domain);
10669
db18b6a6
ID
10670 /*
10671 * The PLL needs to be enabled with a valid divider
10672 * configuration, otherwise accessing DSI registers will hang
10673 * the machine. See BSpec North Display Engine
10674 * registers/MIPI[BXT]. We can break out here early, since we
10675 * need the same DSI PLL to be enabled for both DSI ports.
10676 */
10677 if (!intel_dsi_pll_is_enabled(dev_priv))
10678 break;
10679
4d1de975
JN
10680 /* XXX: this works for video mode only */
10681 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
10682 if (!(tmp & DPI_ENABLE))
10683 continue;
10684
10685 tmp = I915_READ(MIPI_CTRL(port));
10686 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
10687 continue;
10688
10689 pipe_config->cpu_transcoder = cpu_transcoder;
4d1de975
JN
10690 break;
10691 }
10692
d7edc4e5 10693 return transcoder_is_dsi(pipe_config->cpu_transcoder);
4d1de975
JN
10694}
10695
26804afd 10696static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
5cec258b 10697 struct intel_crtc_state *pipe_config)
26804afd 10698{
6315b5d3 10699 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
d452c5b6 10700 struct intel_shared_dpll *pll;
26804afd
DV
10701 enum port port;
10702 uint32_t tmp;
10703
10704 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
10705
10706 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
10707
b976dc53 10708 if (IS_GEN9_BC(dev_priv))
96b7dfb7 10709 skylake_get_ddi_pll(dev_priv, port, pipe_config);
cc3f90f0 10710 else if (IS_GEN9_LP(dev_priv))
3760b59c 10711 bxt_get_ddi_pll(dev_priv, port, pipe_config);
96b7dfb7
S
10712 else
10713 haswell_get_ddi_pll(dev_priv, port, pipe_config);
9cd86933 10714
8106ddbd
ACO
10715 pll = pipe_config->shared_dpll;
10716 if (pll) {
2edd6443
ACO
10717 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
10718 &pipe_config->dpll_hw_state));
d452c5b6
DV
10719 }
10720
26804afd
DV
10721 /*
10722 * Haswell has only FDI/PCH transcoder A. It is which is connected to
10723 * DDI E. So just check whether this pipe is wired to DDI E and whether
10724 * the PCH transcoder is on.
10725 */
6315b5d3 10726 if (INTEL_GEN(dev_priv) < 9 &&
ca370455 10727 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
26804afd
DV
10728 pipe_config->has_pch_encoder = true;
10729
10730 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
10731 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
10732 FDI_DP_PORT_WIDTH_SHIFT) + 1;
10733
10734 ironlake_get_fdi_m_n_config(crtc, pipe_config);
10735 }
10736}
10737
0e8ffe1b 10738static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5cec258b 10739 struct intel_crtc_state *pipe_config)
0e8ffe1b 10740{
6315b5d3 10741 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1729050e
ID
10742 enum intel_display_power_domain power_domain;
10743 unsigned long power_domain_mask;
cf30429e 10744 bool active;
0e8ffe1b 10745
1729050e
ID
10746 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
10747 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0 10748 return false;
1729050e
ID
10749 power_domain_mask = BIT(power_domain);
10750
8106ddbd 10751 pipe_config->shared_dpll = NULL;
c0d43d62 10752
cf30429e 10753 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
eccb140b 10754
cc3f90f0 10755 if (IS_GEN9_LP(dev_priv) &&
d7edc4e5
VS
10756 bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
10757 WARN_ON(active);
10758 active = true;
4d1de975
JN
10759 }
10760
cf30429e 10761 if (!active)
1729050e 10762 goto out;
0e8ffe1b 10763
d7edc4e5 10764 if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
4d1de975
JN
10765 haswell_get_ddi_port_state(crtc, pipe_config);
10766 intel_get_pipe_timings(crtc, pipe_config);
10767 }
627eb5a3 10768
bc58be60 10769 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 10770
05dc698c
LL
10771 pipe_config->gamma_mode =
10772 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
10773
6315b5d3 10774 if (INTEL_GEN(dev_priv) >= 9) {
1c74eeaf 10775 intel_crtc_init_scalers(crtc, pipe_config);
a1b2278e 10776
af99ceda
CK
10777 pipe_config->scaler_state.scaler_id = -1;
10778 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
10779 }
10780
1729050e
ID
10781 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
10782 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
10783 power_domain_mask |= BIT(power_domain);
6315b5d3 10784 if (INTEL_GEN(dev_priv) >= 9)
bd2e244f 10785 skylake_get_pfit_config(crtc, pipe_config);
ff6d9f55 10786 else
1c132b44 10787 ironlake_get_pfit_config(crtc, pipe_config);
bd2e244f 10788 }
88adfff1 10789
772c2a51 10790 if (IS_HASWELL(dev_priv))
e59150dc
JB
10791 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
10792 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 10793
4d1de975
JN
10794 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
10795 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
ebb69c95
CT
10796 pipe_config->pixel_multiplier =
10797 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
10798 } else {
10799 pipe_config->pixel_multiplier = 1;
10800 }
6c49f241 10801
1729050e
ID
10802out:
10803 for_each_power_domain(power_domain, power_domain_mask)
10804 intel_display_power_put(dev_priv, power_domain);
10805
cf30429e 10806 return active;
0e8ffe1b
DV
10807}
10808
55a08b3f
ML
10809static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
10810 const struct intel_plane_state *plane_state)
560b85bb
CW
10811{
10812 struct drm_device *dev = crtc->dev;
fac5e23e 10813 struct drm_i915_private *dev_priv = to_i915(dev);
560b85bb 10814 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
dc41c154 10815 uint32_t cntl = 0, size = 0;
560b85bb 10816
936e71e3 10817 if (plane_state && plane_state->base.visible) {
55a08b3f
ML
10818 unsigned int width = plane_state->base.crtc_w;
10819 unsigned int height = plane_state->base.crtc_h;
dc41c154
VS
10820 unsigned int stride = roundup_pow_of_two(width) * 4;
10821
10822 switch (stride) {
10823 default:
10824 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
10825 width, stride);
10826 stride = 256;
10827 /* fallthrough */
10828 case 256:
10829 case 512:
10830 case 1024:
10831 case 2048:
10832 break;
4b0e333e
CW
10833 }
10834
dc41c154
VS
10835 cntl |= CURSOR_ENABLE |
10836 CURSOR_GAMMA_ENABLE |
10837 CURSOR_FORMAT_ARGB |
10838 CURSOR_STRIDE(stride);
10839
10840 size = (height << 12) | width;
4b0e333e 10841 }
560b85bb 10842
dc41c154
VS
10843 if (intel_crtc->cursor_cntl != 0 &&
10844 (intel_crtc->cursor_base != base ||
10845 intel_crtc->cursor_size != size ||
10846 intel_crtc->cursor_cntl != cntl)) {
10847 /* On these chipsets we can only modify the base/size/stride
10848 * whilst the cursor is disabled.
10849 */
0b87c24e
VS
10850 I915_WRITE(CURCNTR(PIPE_A), 0);
10851 POSTING_READ(CURCNTR(PIPE_A));
dc41c154 10852 intel_crtc->cursor_cntl = 0;
4b0e333e 10853 }
560b85bb 10854
99d1f387 10855 if (intel_crtc->cursor_base != base) {
0b87c24e 10856 I915_WRITE(CURBASE(PIPE_A), base);
99d1f387
VS
10857 intel_crtc->cursor_base = base;
10858 }
4726e0b0 10859
dc41c154
VS
10860 if (intel_crtc->cursor_size != size) {
10861 I915_WRITE(CURSIZE, size);
10862 intel_crtc->cursor_size = size;
4b0e333e 10863 }
560b85bb 10864
4b0e333e 10865 if (intel_crtc->cursor_cntl != cntl) {
0b87c24e
VS
10866 I915_WRITE(CURCNTR(PIPE_A), cntl);
10867 POSTING_READ(CURCNTR(PIPE_A));
4b0e333e 10868 intel_crtc->cursor_cntl = cntl;
560b85bb 10869 }
560b85bb
CW
10870}
10871
55a08b3f
ML
10872static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
10873 const struct intel_plane_state *plane_state)
65a21cd6
JB
10874{
10875 struct drm_device *dev = crtc->dev;
fac5e23e 10876 struct drm_i915_private *dev_priv = to_i915(dev);
65a21cd6
JB
10877 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10878 int pipe = intel_crtc->pipe;
663f3122 10879 uint32_t cntl = 0;
4b0e333e 10880
936e71e3 10881 if (plane_state && plane_state->base.visible) {
4b0e333e 10882 cntl = MCURSOR_GAMMA_ENABLE;
55a08b3f 10883 switch (plane_state->base.crtc_w) {
4726e0b0
SK
10884 case 64:
10885 cntl |= CURSOR_MODE_64_ARGB_AX;
10886 break;
10887 case 128:
10888 cntl |= CURSOR_MODE_128_ARGB_AX;
10889 break;
10890 case 256:
10891 cntl |= CURSOR_MODE_256_ARGB_AX;
10892 break;
10893 default:
55a08b3f 10894 MISSING_CASE(plane_state->base.crtc_w);
4726e0b0 10895 return;
65a21cd6 10896 }
4b0e333e 10897 cntl |= pipe << 28; /* Connect to correct pipe */
47bf17a7 10898
4f8036a2 10899 if (HAS_DDI(dev_priv))
47bf17a7 10900 cntl |= CURSOR_PIPE_CSC_ENABLE;
65a21cd6 10901
f22aa143 10902 if (plane_state->base.rotation & DRM_ROTATE_180)
55a08b3f
ML
10903 cntl |= CURSOR_ROTATE_180;
10904 }
4398ad45 10905
4b0e333e
CW
10906 if (intel_crtc->cursor_cntl != cntl) {
10907 I915_WRITE(CURCNTR(pipe), cntl);
10908 POSTING_READ(CURCNTR(pipe));
10909 intel_crtc->cursor_cntl = cntl;
65a21cd6 10910 }
4b0e333e 10911
65a21cd6 10912 /* and commit changes on next vblank */
5efb3e28
VS
10913 I915_WRITE(CURBASE(pipe), base);
10914 POSTING_READ(CURBASE(pipe));
99d1f387
VS
10915
10916 intel_crtc->cursor_base = base;
65a21cd6
JB
10917}
10918
cda4b7d3 10919/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6b383a7f 10920static void intel_crtc_update_cursor(struct drm_crtc *crtc,
55a08b3f 10921 const struct intel_plane_state *plane_state)
cda4b7d3
CW
10922{
10923 struct drm_device *dev = crtc->dev;
fac5e23e 10924 struct drm_i915_private *dev_priv = to_i915(dev);
cda4b7d3
CW
10925 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10926 int pipe = intel_crtc->pipe;
55a08b3f
ML
10927 u32 base = intel_crtc->cursor_addr;
10928 u32 pos = 0;
cda4b7d3 10929
55a08b3f
ML
10930 if (plane_state) {
10931 int x = plane_state->base.crtc_x;
10932 int y = plane_state->base.crtc_y;
cda4b7d3 10933
55a08b3f
ML
10934 if (x < 0) {
10935 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10936 x = -x;
10937 }
10938 pos |= x << CURSOR_X_SHIFT;
cda4b7d3 10939
55a08b3f
ML
10940 if (y < 0) {
10941 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10942 y = -y;
10943 }
10944 pos |= y << CURSOR_Y_SHIFT;
10945
10946 /* ILK+ do this automagically */
49cff963 10947 if (HAS_GMCH_DISPLAY(dev_priv) &&
f22aa143 10948 plane_state->base.rotation & DRM_ROTATE_180) {
55a08b3f
ML
10949 base += (plane_state->base.crtc_h *
10950 plane_state->base.crtc_w - 1) * 4;
10951 }
cda4b7d3 10952 }
cda4b7d3 10953
5efb3e28
VS
10954 I915_WRITE(CURPOS(pipe), pos);
10955
2a307c2e 10956 if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
55a08b3f 10957 i845_update_cursor(crtc, base, plane_state);
5efb3e28 10958 else
55a08b3f 10959 i9xx_update_cursor(crtc, base, plane_state);
cda4b7d3
CW
10960}
10961
50a0bc90 10962static bool cursor_size_ok(struct drm_i915_private *dev_priv,
dc41c154
VS
10963 uint32_t width, uint32_t height)
10964{
10965 if (width == 0 || height == 0)
10966 return false;
10967
10968 /*
10969 * 845g/865g are special in that they are only limited by
10970 * the width of their cursors, the height is arbitrary up to
10971 * the precision of the register. Everything else requires
10972 * square cursors, limited to a few power-of-two sizes.
10973 */
2a307c2e 10974 if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
dc41c154
VS
10975 if ((width & 63) != 0)
10976 return false;
10977
2a307c2e 10978 if (width > (IS_I845G(dev_priv) ? 64 : 512))
dc41c154
VS
10979 return false;
10980
10981 if (height > 1023)
10982 return false;
10983 } else {
10984 switch (width | height) {
10985 case 256:
10986 case 128:
50a0bc90 10987 if (IS_GEN2(dev_priv))
dc41c154
VS
10988 return false;
10989 case 64:
10990 break;
10991 default:
10992 return false;
10993 }
10994 }
10995
10996 return true;
10997}
10998
79e53945
JB
10999/* VESA 640x480x72Hz mode to set on the pipe */
11000static struct drm_display_mode load_detect_mode = {
11001 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
11002 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
11003};
11004
a8bb6818
DV
11005struct drm_framebuffer *
11006__intel_framebuffer_create(struct drm_device *dev,
11007 struct drm_mode_fb_cmd2 *mode_cmd,
11008 struct drm_i915_gem_object *obj)
d2dff872
CW
11009{
11010 struct intel_framebuffer *intel_fb;
11011 int ret;
11012
11013 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
dcb1394e 11014 if (!intel_fb)
d2dff872 11015 return ERR_PTR(-ENOMEM);
d2dff872
CW
11016
11017 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
dd4916c5
DV
11018 if (ret)
11019 goto err;
d2dff872
CW
11020
11021 return &intel_fb->base;
dcb1394e 11022
dd4916c5 11023err:
dd4916c5 11024 kfree(intel_fb);
dd4916c5 11025 return ERR_PTR(ret);
d2dff872
CW
11026}
11027
b5ea642a 11028static struct drm_framebuffer *
a8bb6818
DV
11029intel_framebuffer_create(struct drm_device *dev,
11030 struct drm_mode_fb_cmd2 *mode_cmd,
11031 struct drm_i915_gem_object *obj)
11032{
11033 struct drm_framebuffer *fb;
11034 int ret;
11035
11036 ret = i915_mutex_lock_interruptible(dev);
11037 if (ret)
11038 return ERR_PTR(ret);
11039 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
11040 mutex_unlock(&dev->struct_mutex);
11041
11042 return fb;
11043}
11044
d2dff872
CW
11045static u32
11046intel_framebuffer_pitch_for_width(int width, int bpp)
11047{
11048 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
11049 return ALIGN(pitch, 64);
11050}
11051
11052static u32
11053intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
11054{
11055 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
1267a26b 11056 return PAGE_ALIGN(pitch * mode->vdisplay);
d2dff872
CW
11057}
11058
11059static struct drm_framebuffer *
11060intel_framebuffer_create_for_mode(struct drm_device *dev,
11061 struct drm_display_mode *mode,
11062 int depth, int bpp)
11063{
dcb1394e 11064 struct drm_framebuffer *fb;
d2dff872 11065 struct drm_i915_gem_object *obj;
0fed39bd 11066 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872 11067
12d79d78 11068 obj = i915_gem_object_create(to_i915(dev),
d2dff872 11069 intel_framebuffer_size_for_mode(mode, bpp));
fe3db79b
CW
11070 if (IS_ERR(obj))
11071 return ERR_CAST(obj);
d2dff872
CW
11072
11073 mode_cmd.width = mode->hdisplay;
11074 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
11075 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
11076 bpp);
5ca0c34a 11077 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872 11078
dcb1394e
LW
11079 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
11080 if (IS_ERR(fb))
f0cd5182 11081 i915_gem_object_put(obj);
dcb1394e
LW
11082
11083 return fb;
d2dff872
CW
11084}
11085
11086static struct drm_framebuffer *
11087mode_fits_in_fbdev(struct drm_device *dev,
11088 struct drm_display_mode *mode)
11089{
0695726e 11090#ifdef CONFIG_DRM_FBDEV_EMULATION
fac5e23e 11091 struct drm_i915_private *dev_priv = to_i915(dev);
d2dff872
CW
11092 struct drm_i915_gem_object *obj;
11093 struct drm_framebuffer *fb;
11094
4c0e5528 11095 if (!dev_priv->fbdev)
d2dff872
CW
11096 return NULL;
11097
4c0e5528 11098 if (!dev_priv->fbdev->fb)
d2dff872
CW
11099 return NULL;
11100
4c0e5528
DV
11101 obj = dev_priv->fbdev->fb->obj;
11102 BUG_ON(!obj);
11103
8bcd4553 11104 fb = &dev_priv->fbdev->fb->base;
01f2c773 11105 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
272725c7 11106 fb->format->cpp[0] * 8))
d2dff872
CW
11107 return NULL;
11108
01f2c773 11109 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
11110 return NULL;
11111
edde3617 11112 drm_framebuffer_reference(fb);
d2dff872 11113 return fb;
4520f53a
DV
11114#else
11115 return NULL;
11116#endif
d2dff872
CW
11117}
11118
d3a40d1b
ACO
11119static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
11120 struct drm_crtc *crtc,
11121 struct drm_display_mode *mode,
11122 struct drm_framebuffer *fb,
11123 int x, int y)
11124{
11125 struct drm_plane_state *plane_state;
11126 int hdisplay, vdisplay;
11127 int ret;
11128
11129 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
11130 if (IS_ERR(plane_state))
11131 return PTR_ERR(plane_state);
11132
11133 if (mode)
196cd5d3 11134 drm_mode_get_hv_timing(mode, &hdisplay, &vdisplay);
d3a40d1b
ACO
11135 else
11136 hdisplay = vdisplay = 0;
11137
11138 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
11139 if (ret)
11140 return ret;
11141 drm_atomic_set_fb_for_plane(plane_state, fb);
11142 plane_state->crtc_x = 0;
11143 plane_state->crtc_y = 0;
11144 plane_state->crtc_w = hdisplay;
11145 plane_state->crtc_h = vdisplay;
11146 plane_state->src_x = x << 16;
11147 plane_state->src_y = y << 16;
11148 plane_state->src_w = hdisplay << 16;
11149 plane_state->src_h = vdisplay << 16;
11150
11151 return 0;
11152}
11153
d2434ab7 11154bool intel_get_load_detect_pipe(struct drm_connector *connector,
7173188d 11155 struct drm_display_mode *mode,
51fd371b
RC
11156 struct intel_load_detect_pipe *old,
11157 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
11158{
11159 struct intel_crtc *intel_crtc;
d2434ab7
DV
11160 struct intel_encoder *intel_encoder =
11161 intel_attached_encoder(connector);
79e53945 11162 struct drm_crtc *possible_crtc;
4ef69c7a 11163 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
11164 struct drm_crtc *crtc = NULL;
11165 struct drm_device *dev = encoder->dev;
0f0f74bc 11166 struct drm_i915_private *dev_priv = to_i915(dev);
94352cf9 11167 struct drm_framebuffer *fb;
51fd371b 11168 struct drm_mode_config *config = &dev->mode_config;
edde3617 11169 struct drm_atomic_state *state = NULL, *restore_state = NULL;
944b0c76 11170 struct drm_connector_state *connector_state;
4be07317 11171 struct intel_crtc_state *crtc_state;
51fd371b 11172 int ret, i = -1;
79e53945 11173
d2dff872 11174 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 11175 connector->base.id, connector->name,
8e329a03 11176 encoder->base.id, encoder->name);
d2dff872 11177
edde3617
ML
11178 old->restore_state = NULL;
11179
51fd371b
RC
11180retry:
11181 ret = drm_modeset_lock(&config->connection_mutex, ctx);
11182 if (ret)
ad3c558f 11183 goto fail;
6e9f798d 11184
79e53945
JB
11185 /*
11186 * Algorithm gets a little messy:
7a5e4805 11187 *
79e53945
JB
11188 * - if the connector already has an assigned crtc, use it (but make
11189 * sure it's on first)
7a5e4805 11190 *
79e53945
JB
11191 * - try to find the first unused crtc that can drive this connector,
11192 * and use that if we find one
79e53945
JB
11193 */
11194
11195 /* See if we already have a CRTC for this connector */
edde3617
ML
11196 if (connector->state->crtc) {
11197 crtc = connector->state->crtc;
8261b191 11198
51fd371b 11199 ret = drm_modeset_lock(&crtc->mutex, ctx);
4d02e2de 11200 if (ret)
ad3c558f 11201 goto fail;
8261b191
CW
11202
11203 /* Make sure the crtc and connector are running */
edde3617 11204 goto found;
79e53945
JB
11205 }
11206
11207 /* Find an unused one (if possible) */
70e1e0ec 11208 for_each_crtc(dev, possible_crtc) {
79e53945
JB
11209 i++;
11210 if (!(encoder->possible_crtcs & (1 << i)))
11211 continue;
edde3617
ML
11212
11213 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
11214 if (ret)
11215 goto fail;
11216
11217 if (possible_crtc->state->enable) {
11218 drm_modeset_unlock(&possible_crtc->mutex);
a459249c 11219 continue;
edde3617 11220 }
a459249c
VS
11221
11222 crtc = possible_crtc;
11223 break;
79e53945
JB
11224 }
11225
11226 /*
11227 * If we didn't find an unused CRTC, don't use any.
11228 */
11229 if (!crtc) {
7173188d 11230 DRM_DEBUG_KMS("no pipe available for load-detect\n");
ad3c558f 11231 goto fail;
79e53945
JB
11232 }
11233
edde3617
ML
11234found:
11235 intel_crtc = to_intel_crtc(crtc);
11236
4d02e2de
DV
11237 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
11238 if (ret)
ad3c558f 11239 goto fail;
79e53945 11240
83a57153 11241 state = drm_atomic_state_alloc(dev);
edde3617
ML
11242 restore_state = drm_atomic_state_alloc(dev);
11243 if (!state || !restore_state) {
11244 ret = -ENOMEM;
11245 goto fail;
11246 }
83a57153
ACO
11247
11248 state->acquire_ctx = ctx;
edde3617 11249 restore_state->acquire_ctx = ctx;
83a57153 11250
944b0c76
ACO
11251 connector_state = drm_atomic_get_connector_state(state, connector);
11252 if (IS_ERR(connector_state)) {
11253 ret = PTR_ERR(connector_state);
11254 goto fail;
11255 }
11256
edde3617
ML
11257 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
11258 if (ret)
11259 goto fail;
944b0c76 11260
4be07317
ACO
11261 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
11262 if (IS_ERR(crtc_state)) {
11263 ret = PTR_ERR(crtc_state);
11264 goto fail;
11265 }
11266
49d6fa21 11267 crtc_state->base.active = crtc_state->base.enable = true;
4be07317 11268
6492711d
CW
11269 if (!mode)
11270 mode = &load_detect_mode;
79e53945 11271
d2dff872
CW
11272 /* We need a framebuffer large enough to accommodate all accesses
11273 * that the plane may generate whilst we perform load detection.
11274 * We can not rely on the fbcon either being present (we get called
11275 * during its initialisation to detect all boot displays, or it may
11276 * not even exist) or that it is large enough to satisfy the
11277 * requested mode.
11278 */
94352cf9
DV
11279 fb = mode_fits_in_fbdev(dev, mode);
11280 if (fb == NULL) {
d2dff872 11281 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9 11282 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
d2dff872
CW
11283 } else
11284 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 11285 if (IS_ERR(fb)) {
d2dff872 11286 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
412b61d8 11287 goto fail;
79e53945 11288 }
79e53945 11289
d3a40d1b
ACO
11290 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
11291 if (ret)
11292 goto fail;
11293
edde3617
ML
11294 drm_framebuffer_unreference(fb);
11295
11296 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
11297 if (ret)
11298 goto fail;
11299
11300 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
11301 if (!ret)
11302 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
11303 if (!ret)
11304 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
11305 if (ret) {
11306 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
11307 goto fail;
11308 }
8c7b5ccb 11309
3ba86073
ML
11310 ret = drm_atomic_commit(state);
11311 if (ret) {
6492711d 11312 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
412b61d8 11313 goto fail;
79e53945 11314 }
edde3617
ML
11315
11316 old->restore_state = restore_state;
7abbd11f 11317 drm_atomic_state_put(state);
7173188d 11318
79e53945 11319 /* let the connector get through one full cycle before testing */
0f0f74bc 11320 intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
7173188d 11321 return true;
412b61d8 11322
ad3c558f 11323fail:
7fb71c8f
CW
11324 if (state) {
11325 drm_atomic_state_put(state);
11326 state = NULL;
11327 }
11328 if (restore_state) {
11329 drm_atomic_state_put(restore_state);
11330 restore_state = NULL;
11331 }
83a57153 11332
51fd371b
RC
11333 if (ret == -EDEADLK) {
11334 drm_modeset_backoff(ctx);
11335 goto retry;
11336 }
11337
412b61d8 11338 return false;
79e53945
JB
11339}
11340
d2434ab7 11341void intel_release_load_detect_pipe(struct drm_connector *connector,
49172fee
ACO
11342 struct intel_load_detect_pipe *old,
11343 struct drm_modeset_acquire_ctx *ctx)
79e53945 11344{
d2434ab7
DV
11345 struct intel_encoder *intel_encoder =
11346 intel_attached_encoder(connector);
4ef69c7a 11347 struct drm_encoder *encoder = &intel_encoder->base;
edde3617 11348 struct drm_atomic_state *state = old->restore_state;
d3a40d1b 11349 int ret;
79e53945 11350
d2dff872 11351 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 11352 connector->base.id, connector->name,
8e329a03 11353 encoder->base.id, encoder->name);
d2dff872 11354
edde3617 11355 if (!state)
0622a53c 11356 return;
79e53945 11357
edde3617 11358 ret = drm_atomic_commit(state);
0853695c 11359 if (ret)
edde3617 11360 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
0853695c 11361 drm_atomic_state_put(state);
79e53945
JB
11362}
11363
da4a1efa 11364static int i9xx_pll_refclk(struct drm_device *dev,
5cec258b 11365 const struct intel_crtc_state *pipe_config)
da4a1efa 11366{
fac5e23e 11367 struct drm_i915_private *dev_priv = to_i915(dev);
da4a1efa
VS
11368 u32 dpll = pipe_config->dpll_hw_state.dpll;
11369
11370 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 11371 return dev_priv->vbt.lvds_ssc_freq;
6e266956 11372 else if (HAS_PCH_SPLIT(dev_priv))
da4a1efa 11373 return 120000;
5db94019 11374 else if (!IS_GEN2(dev_priv))
da4a1efa
VS
11375 return 96000;
11376 else
11377 return 48000;
11378}
11379
79e53945 11380/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc 11381static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 11382 struct intel_crtc_state *pipe_config)
79e53945 11383{
f1f644dc 11384 struct drm_device *dev = crtc->base.dev;
fac5e23e 11385 struct drm_i915_private *dev_priv = to_i915(dev);
f1f644dc 11386 int pipe = pipe_config->cpu_transcoder;
293623f7 11387 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945 11388 u32 fp;
9e2c8475 11389 struct dpll clock;
dccbea3b 11390 int port_clock;
da4a1efa 11391 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
11392
11393 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 11394 fp = pipe_config->dpll_hw_state.fp0;
79e53945 11395 else
293623f7 11396 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
11397
11398 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
9b1e14f4 11399 if (IS_PINEVIEW(dev_priv)) {
f2b115e6
AJ
11400 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
11401 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
11402 } else {
11403 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
11404 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
11405 }
11406
5db94019 11407 if (!IS_GEN2(dev_priv)) {
9b1e14f4 11408 if (IS_PINEVIEW(dev_priv))
f2b115e6
AJ
11409 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
11410 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
11411 else
11412 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
11413 DPLL_FPA01_P1_POST_DIV_SHIFT);
11414
11415 switch (dpll & DPLL_MODE_MASK) {
11416 case DPLLB_MODE_DAC_SERIAL:
11417 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
11418 5 : 10;
11419 break;
11420 case DPLLB_MODE_LVDS:
11421 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
11422 7 : 14;
11423 break;
11424 default:
28c97730 11425 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 11426 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 11427 return;
79e53945
JB
11428 }
11429
9b1e14f4 11430 if (IS_PINEVIEW(dev_priv))
dccbea3b 11431 port_clock = pnv_calc_dpll_params(refclk, &clock);
ac58c3f0 11432 else
dccbea3b 11433 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945 11434 } else {
50a0bc90 11435 u32 lvds = IS_I830(dev_priv) ? 0 : I915_READ(LVDS);
b1c560d1 11436 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
11437
11438 if (is_lvds) {
11439 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
11440 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
11441
11442 if (lvds & LVDS_CLKB_POWER_UP)
11443 clock.p2 = 7;
11444 else
11445 clock.p2 = 14;
79e53945
JB
11446 } else {
11447 if (dpll & PLL_P1_DIVIDE_BY_TWO)
11448 clock.p1 = 2;
11449 else {
11450 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
11451 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
11452 }
11453 if (dpll & PLL_P2_DIVIDE_BY_4)
11454 clock.p2 = 4;
11455 else
11456 clock.p2 = 2;
79e53945 11457 }
da4a1efa 11458
dccbea3b 11459 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945
JB
11460 }
11461
18442d08
VS
11462 /*
11463 * This value includes pixel_multiplier. We will use
241bfc38 11464 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
11465 * encoder's get_config() function.
11466 */
dccbea3b 11467 pipe_config->port_clock = port_clock;
f1f644dc
JB
11468}
11469
6878da05
VS
11470int intel_dotclock_calculate(int link_freq,
11471 const struct intel_link_m_n *m_n)
f1f644dc 11472{
f1f644dc
JB
11473 /*
11474 * The calculation for the data clock is:
1041a02f 11475 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 11476 * But we want to avoid losing precison if possible, so:
1041a02f 11477 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
11478 *
11479 * and the link clock is simpler:
1041a02f 11480 * link_clock = (m * link_clock) / n
f1f644dc
JB
11481 */
11482
6878da05
VS
11483 if (!m_n->link_n)
11484 return 0;
f1f644dc 11485
6878da05
VS
11486 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
11487}
f1f644dc 11488
18442d08 11489static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 11490 struct intel_crtc_state *pipe_config)
6878da05 11491{
e3b247da 11492 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
79e53945 11493
18442d08
VS
11494 /* read out port_clock from the DPLL */
11495 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 11496
f1f644dc 11497 /*
e3b247da
VS
11498 * In case there is an active pipe without active ports,
11499 * we may need some idea for the dotclock anyway.
11500 * Calculate one based on the FDI configuration.
79e53945 11501 */
2d112de7 11502 pipe_config->base.adjusted_mode.crtc_clock =
21a727b3 11503 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
18442d08 11504 &pipe_config->fdi_m_n);
79e53945
JB
11505}
11506
11507/** Returns the currently programmed mode of the given pipe. */
11508struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
11509 struct drm_crtc *crtc)
11510{
fac5e23e 11511 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 11512 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 11513 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
79e53945 11514 struct drm_display_mode *mode;
3f36b937 11515 struct intel_crtc_state *pipe_config;
fe2b8f9d
PZ
11516 int htot = I915_READ(HTOTAL(cpu_transcoder));
11517 int hsync = I915_READ(HSYNC(cpu_transcoder));
11518 int vtot = I915_READ(VTOTAL(cpu_transcoder));
11519 int vsync = I915_READ(VSYNC(cpu_transcoder));
293623f7 11520 enum pipe pipe = intel_crtc->pipe;
79e53945
JB
11521
11522 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
11523 if (!mode)
11524 return NULL;
11525
3f36b937
TU
11526 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
11527 if (!pipe_config) {
11528 kfree(mode);
11529 return NULL;
11530 }
11531
f1f644dc
JB
11532 /*
11533 * Construct a pipe_config sufficient for getting the clock info
11534 * back out of crtc_clock_get.
11535 *
11536 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
11537 * to use a real value here instead.
11538 */
3f36b937
TU
11539 pipe_config->cpu_transcoder = (enum transcoder) pipe;
11540 pipe_config->pixel_multiplier = 1;
11541 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
11542 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
11543 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
11544 i9xx_crtc_clock_get(intel_crtc, pipe_config);
11545
11546 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
79e53945
JB
11547 mode->hdisplay = (htot & 0xffff) + 1;
11548 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
11549 mode->hsync_start = (hsync & 0xffff) + 1;
11550 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
11551 mode->vdisplay = (vtot & 0xffff) + 1;
11552 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
11553 mode->vsync_start = (vsync & 0xffff) + 1;
11554 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
11555
11556 drm_mode_set_name(mode);
79e53945 11557
3f36b937
TU
11558 kfree(pipe_config);
11559
79e53945
JB
11560 return mode;
11561}
11562
11563static void intel_crtc_destroy(struct drm_crtc *crtc)
11564{
11565 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
67e77c5a 11566 struct drm_device *dev = crtc->dev;
51cbaf01 11567 struct intel_flip_work *work;
67e77c5a 11568
5e2d7afc 11569 spin_lock_irq(&dev->event_lock);
5a21b665
DV
11570 work = intel_crtc->flip_work;
11571 intel_crtc->flip_work = NULL;
11572 spin_unlock_irq(&dev->event_lock);
67e77c5a 11573
5a21b665 11574 if (work) {
51cbaf01
ML
11575 cancel_work_sync(&work->mmio_work);
11576 cancel_work_sync(&work->unpin_work);
5a21b665 11577 kfree(work);
67e77c5a 11578 }
79e53945
JB
11579
11580 drm_crtc_cleanup(crtc);
67e77c5a 11581
79e53945
JB
11582 kfree(intel_crtc);
11583}
11584
6b95a207
KH
11585static void intel_unpin_work_fn(struct work_struct *__work)
11586{
51cbaf01
ML
11587 struct intel_flip_work *work =
11588 container_of(__work, struct intel_flip_work, unpin_work);
5a21b665
DV
11589 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
11590 struct drm_device *dev = crtc->base.dev;
11591 struct drm_plane *primary = crtc->base.primary;
03f476e1 11592
5a21b665
DV
11593 if (is_mmio_work(work))
11594 flush_work(&work->mmio_work);
03f476e1 11595
5a21b665 11596 mutex_lock(&dev->struct_mutex);
be1e3415 11597 intel_unpin_fb_vma(work->old_vma);
f8c417cd 11598 i915_gem_object_put(work->pending_flip_obj);
5a21b665 11599 mutex_unlock(&dev->struct_mutex);
143f73b3 11600
e8a261ea
CW
11601 i915_gem_request_put(work->flip_queued_req);
11602
5748b6a1
CW
11603 intel_frontbuffer_flip_complete(to_i915(dev),
11604 to_intel_plane(primary)->frontbuffer_bit);
5a21b665
DV
11605 intel_fbc_post_update(crtc);
11606 drm_framebuffer_unreference(work->old_fb);
143f73b3 11607
5a21b665
DV
11608 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
11609 atomic_dec(&crtc->unpin_work_count);
a6747b73 11610
5a21b665
DV
11611 kfree(work);
11612}
d9e86c0e 11613
5a21b665
DV
11614/* Is 'a' after or equal to 'b'? */
11615static bool g4x_flip_count_after_eq(u32 a, u32 b)
11616{
11617 return !((a - b) & 0x80000000);
11618}
143f73b3 11619
5a21b665
DV
11620static bool __pageflip_finished_cs(struct intel_crtc *crtc,
11621 struct intel_flip_work *work)
11622{
11623 struct drm_device *dev = crtc->base.dev;
fac5e23e 11624 struct drm_i915_private *dev_priv = to_i915(dev);
143f73b3 11625
8af29b0c 11626 if (abort_flip_on_reset(crtc))
5a21b665 11627 return true;
143f73b3 11628
5a21b665
DV
11629 /*
11630 * The relevant registers doen't exist on pre-ctg.
11631 * As the flip done interrupt doesn't trigger for mmio
11632 * flips on gmch platforms, a flip count check isn't
11633 * really needed there. But since ctg has the registers,
11634 * include it in the check anyway.
11635 */
9beb5fea 11636 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
5a21b665 11637 return true;
b4a98e57 11638
5a21b665
DV
11639 /*
11640 * BDW signals flip done immediately if the plane
11641 * is disabled, even if the plane enable is already
11642 * armed to occur at the next vblank :(
11643 */
f99d7069 11644
5a21b665
DV
11645 /*
11646 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
11647 * used the same base address. In that case the mmio flip might
11648 * have completed, but the CS hasn't even executed the flip yet.
11649 *
11650 * A flip count check isn't enough as the CS might have updated
11651 * the base address just after start of vblank, but before we
11652 * managed to process the interrupt. This means we'd complete the
11653 * CS flip too soon.
11654 *
11655 * Combining both checks should get us a good enough result. It may
11656 * still happen that the CS flip has been executed, but has not
11657 * yet actually completed. But in case the base address is the same
11658 * anyway, we don't really care.
11659 */
11660 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
11661 crtc->flip_work->gtt_offset &&
11662 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
11663 crtc->flip_work->flip_count);
11664}
b4a98e57 11665
5a21b665
DV
11666static bool
11667__pageflip_finished_mmio(struct intel_crtc *crtc,
11668 struct intel_flip_work *work)
11669{
11670 /*
11671 * MMIO work completes when vblank is different from
11672 * flip_queued_vblank.
11673 *
11674 * Reset counter value doesn't matter, this is handled by
11675 * i915_wait_request finishing early, so no need to handle
11676 * reset here.
11677 */
11678 return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
6b95a207
KH
11679}
11680
51cbaf01
ML
11681
11682static bool pageflip_finished(struct intel_crtc *crtc,
11683 struct intel_flip_work *work)
11684{
11685 if (!atomic_read(&work->pending))
11686 return false;
11687
11688 smp_rmb();
11689
5a21b665
DV
11690 if (is_mmio_work(work))
11691 return __pageflip_finished_mmio(crtc, work);
11692 else
11693 return __pageflip_finished_cs(crtc, work);
11694}
11695
11696void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
11697{
91c8a326 11698 struct drm_device *dev = &dev_priv->drm;
98187836 11699 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
5a21b665
DV
11700 struct intel_flip_work *work;
11701 unsigned long flags;
11702
11703 /* Ignore early vblank irqs */
11704 if (!crtc)
11705 return;
11706
51cbaf01 11707 /*
5a21b665
DV
11708 * This is called both by irq handlers and the reset code (to complete
11709 * lost pageflips) so needs the full irqsave spinlocks.
51cbaf01 11710 */
5a21b665 11711 spin_lock_irqsave(&dev->event_lock, flags);
e2af48c6 11712 work = crtc->flip_work;
5a21b665
DV
11713
11714 if (work != NULL &&
11715 !is_mmio_work(work) &&
e2af48c6
VS
11716 pageflip_finished(crtc, work))
11717 page_flip_completed(crtc);
5a21b665
DV
11718
11719 spin_unlock_irqrestore(&dev->event_lock, flags);
75f7f3ec
VS
11720}
11721
51cbaf01 11722void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
6b95a207 11723{
91c8a326 11724 struct drm_device *dev = &dev_priv->drm;
98187836 11725 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
51cbaf01 11726 struct intel_flip_work *work;
6b95a207
KH
11727 unsigned long flags;
11728
5251f04e
ML
11729 /* Ignore early vblank irqs */
11730 if (!crtc)
11731 return;
f326038a
DV
11732
11733 /*
11734 * This is called both by irq handlers and the reset code (to complete
11735 * lost pageflips) so needs the full irqsave spinlocks.
e7d841ca 11736 */
6b95a207 11737 spin_lock_irqsave(&dev->event_lock, flags);
e2af48c6 11738 work = crtc->flip_work;
5251f04e 11739
5a21b665
DV
11740 if (work != NULL &&
11741 is_mmio_work(work) &&
e2af48c6
VS
11742 pageflip_finished(crtc, work))
11743 page_flip_completed(crtc);
5251f04e 11744
6b95a207
KH
11745 spin_unlock_irqrestore(&dev->event_lock, flags);
11746}
11747
5a21b665
DV
11748static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
11749 struct intel_flip_work *work)
84c33a64 11750{
5a21b665 11751 work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
84c33a64 11752
5a21b665
DV
11753 /* Ensure that the work item is consistent when activating it ... */
11754 smp_mb__before_atomic();
11755 atomic_set(&work->pending, 1);
11756}
a6747b73 11757
5a21b665
DV
11758static int intel_gen2_queue_flip(struct drm_device *dev,
11759 struct drm_crtc *crtc,
11760 struct drm_framebuffer *fb,
11761 struct drm_i915_gem_object *obj,
11762 struct drm_i915_gem_request *req,
11763 uint32_t flags)
11764{
7e37f889 11765 struct intel_ring *ring = req->ring;
5a21b665
DV
11766 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11767 u32 flip_mask;
11768 int ret;
143f73b3 11769
5a21b665
DV
11770 ret = intel_ring_begin(req, 6);
11771 if (ret)
11772 return ret;
143f73b3 11773
5a21b665
DV
11774 /* Can't queue multiple flips, so wait for the previous
11775 * one to finish before executing the next.
11776 */
11777 if (intel_crtc->plane)
11778 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11779 else
11780 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
b5321f30
CW
11781 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11782 intel_ring_emit(ring, MI_NOOP);
11783 intel_ring_emit(ring, MI_DISPLAY_FLIP |
5a21b665 11784 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
b5321f30
CW
11785 intel_ring_emit(ring, fb->pitches[0]);
11786 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
11787 intel_ring_emit(ring, 0); /* aux display base address, unused */
143f73b3 11788
5a21b665
DV
11789 return 0;
11790}
84c33a64 11791
5a21b665
DV
11792static int intel_gen3_queue_flip(struct drm_device *dev,
11793 struct drm_crtc *crtc,
11794 struct drm_framebuffer *fb,
11795 struct drm_i915_gem_object *obj,
11796 struct drm_i915_gem_request *req,
11797 uint32_t flags)
11798{
7e37f889 11799 struct intel_ring *ring = req->ring;
5a21b665
DV
11800 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11801 u32 flip_mask;
11802 int ret;
d55dbd06 11803
5a21b665
DV
11804 ret = intel_ring_begin(req, 6);
11805 if (ret)
11806 return ret;
d55dbd06 11807
5a21b665
DV
11808 if (intel_crtc->plane)
11809 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11810 else
11811 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
b5321f30
CW
11812 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11813 intel_ring_emit(ring, MI_NOOP);
11814 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
5a21b665 11815 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
b5321f30
CW
11816 intel_ring_emit(ring, fb->pitches[0]);
11817 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
11818 intel_ring_emit(ring, MI_NOOP);
fd8e058a 11819
5a21b665
DV
11820 return 0;
11821}
84c33a64 11822
5a21b665
DV
11823static int intel_gen4_queue_flip(struct drm_device *dev,
11824 struct drm_crtc *crtc,
11825 struct drm_framebuffer *fb,
11826 struct drm_i915_gem_object *obj,
11827 struct drm_i915_gem_request *req,
11828 uint32_t flags)
11829{
7e37f889 11830 struct intel_ring *ring = req->ring;
fac5e23e 11831 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
DV
11832 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11833 uint32_t pf, pipesrc;
11834 int ret;
143f73b3 11835
5a21b665
DV
11836 ret = intel_ring_begin(req, 4);
11837 if (ret)
11838 return ret;
143f73b3 11839
5a21b665
DV
11840 /* i965+ uses the linear or tiled offsets from the
11841 * Display Registers (which do not change across a page-flip)
11842 * so we need only reprogram the base address.
11843 */
b5321f30 11844 intel_ring_emit(ring, MI_DISPLAY_FLIP |
5a21b665 11845 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
b5321f30
CW
11846 intel_ring_emit(ring, fb->pitches[0]);
11847 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset |
bae781b2 11848 intel_fb_modifier_to_tiling(fb->modifier));
5a21b665
DV
11849
11850 /* XXX Enabling the panel-fitter across page-flip is so far
11851 * untested on non-native modes, so ignore it for now.
11852 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11853 */
11854 pf = 0;
11855 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
b5321f30 11856 intel_ring_emit(ring, pf | pipesrc);
143f73b3 11857
5a21b665 11858 return 0;
8c9f3aaf
JB
11859}
11860
5a21b665
DV
11861static int intel_gen6_queue_flip(struct drm_device *dev,
11862 struct drm_crtc *crtc,
11863 struct drm_framebuffer *fb,
11864 struct drm_i915_gem_object *obj,
11865 struct drm_i915_gem_request *req,
11866 uint32_t flags)
da20eabd 11867{
7e37f889 11868 struct intel_ring *ring = req->ring;
fac5e23e 11869 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
DV
11870 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11871 uint32_t pf, pipesrc;
11872 int ret;
d21fbe87 11873
5a21b665
DV
11874 ret = intel_ring_begin(req, 4);
11875 if (ret)
11876 return ret;
92826fcd 11877
b5321f30 11878 intel_ring_emit(ring, MI_DISPLAY_FLIP |
5a21b665 11879 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
72618ebf 11880 intel_ring_emit(ring, fb->pitches[0] |
bae781b2 11881 intel_fb_modifier_to_tiling(fb->modifier));
b5321f30 11882 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
92826fcd 11883
5a21b665
DV
11884 /* Contrary to the suggestions in the documentation,
11885 * "Enable Panel Fitter" does not seem to be required when page
11886 * flipping with a non-native mode, and worse causes a normal
11887 * modeset to fail.
11888 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11889 */
11890 pf = 0;
11891 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
b5321f30 11892 intel_ring_emit(ring, pf | pipesrc);
7809e5ae 11893
5a21b665 11894 return 0;
7809e5ae
MR
11895}
11896
5a21b665
DV
11897static int intel_gen7_queue_flip(struct drm_device *dev,
11898 struct drm_crtc *crtc,
11899 struct drm_framebuffer *fb,
11900 struct drm_i915_gem_object *obj,
11901 struct drm_i915_gem_request *req,
11902 uint32_t flags)
d21fbe87 11903{
5db94019 11904 struct drm_i915_private *dev_priv = to_i915(dev);
7e37f889 11905 struct intel_ring *ring = req->ring;
5a21b665
DV
11906 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11907 uint32_t plane_bit = 0;
11908 int len, ret;
d21fbe87 11909
5a21b665
DV
11910 switch (intel_crtc->plane) {
11911 case PLANE_A:
11912 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11913 break;
11914 case PLANE_B:
11915 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11916 break;
11917 case PLANE_C:
11918 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11919 break;
11920 default:
11921 WARN_ONCE(1, "unknown plane in flip command\n");
11922 return -ENODEV;
11923 }
11924
11925 len = 4;
b5321f30 11926 if (req->engine->id == RCS) {
5a21b665
DV
11927 len += 6;
11928 /*
11929 * On Gen 8, SRM is now taking an extra dword to accommodate
11930 * 48bits addresses, and we need a NOOP for the batch size to
11931 * stay even.
11932 */
5db94019 11933 if (IS_GEN8(dev_priv))
5a21b665
DV
11934 len += 2;
11935 }
11936
11937 /*
11938 * BSpec MI_DISPLAY_FLIP for IVB:
11939 * "The full packet must be contained within the same cache line."
11940 *
11941 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11942 * cacheline, if we ever start emitting more commands before
11943 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11944 * then do the cacheline alignment, and finally emit the
11945 * MI_DISPLAY_FLIP.
11946 */
11947 ret = intel_ring_cacheline_align(req);
11948 if (ret)
11949 return ret;
11950
11951 ret = intel_ring_begin(req, len);
11952 if (ret)
11953 return ret;
11954
11955 /* Unmask the flip-done completion message. Note that the bspec says that
11956 * we should do this for both the BCS and RCS, and that we must not unmask
11957 * more than one flip event at any time (or ensure that one flip message
11958 * can be sent by waiting for flip-done prior to queueing new flips).
11959 * Experimentation says that BCS works despite DERRMR masking all
11960 * flip-done completion events and that unmasking all planes at once
11961 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11962 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11963 */
b5321f30
CW
11964 if (req->engine->id == RCS) {
11965 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
11966 intel_ring_emit_reg(ring, DERRMR);
11967 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
5a21b665
DV
11968 DERRMR_PIPEB_PRI_FLIP_DONE |
11969 DERRMR_PIPEC_PRI_FLIP_DONE));
5db94019 11970 if (IS_GEN8(dev_priv))
b5321f30 11971 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8 |
5a21b665
DV
11972 MI_SRM_LRM_GLOBAL_GTT);
11973 else
b5321f30 11974 intel_ring_emit(ring, MI_STORE_REGISTER_MEM |
5a21b665 11975 MI_SRM_LRM_GLOBAL_GTT);
b5321f30 11976 intel_ring_emit_reg(ring, DERRMR);
bde13ebd
CW
11977 intel_ring_emit(ring,
11978 i915_ggtt_offset(req->engine->scratch) + 256);
5db94019 11979 if (IS_GEN8(dev_priv)) {
b5321f30
CW
11980 intel_ring_emit(ring, 0);
11981 intel_ring_emit(ring, MI_NOOP);
5a21b665
DV
11982 }
11983 }
11984
b5321f30 11985 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
72618ebf 11986 intel_ring_emit(ring, fb->pitches[0] |
bae781b2 11987 intel_fb_modifier_to_tiling(fb->modifier));
b5321f30
CW
11988 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
11989 intel_ring_emit(ring, (MI_NOOP));
5a21b665
DV
11990
11991 return 0;
11992}
11993
11994static bool use_mmio_flip(struct intel_engine_cs *engine,
11995 struct drm_i915_gem_object *obj)
11996{
11997 /*
11998 * This is not being used for older platforms, because
11999 * non-availability of flip done interrupt forces us to use
12000 * CS flips. Older platforms derive flip done using some clever
12001 * tricks involving the flip_pending status bits and vblank irqs.
12002 * So using MMIO flips there would disrupt this mechanism.
12003 */
12004
12005 if (engine == NULL)
12006 return true;
12007
12008 if (INTEL_GEN(engine->i915) < 5)
12009 return false;
12010
12011 if (i915.use_mmio_flip < 0)
12012 return false;
12013 else if (i915.use_mmio_flip > 0)
12014 return true;
12015 else if (i915.enable_execlists)
12016 return true;
c37efb99 12017
d07f0e59 12018 return engine != i915_gem_object_last_write_engine(obj);
5a21b665
DV
12019}
12020
12021static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
12022 unsigned int rotation,
12023 struct intel_flip_work *work)
12024{
12025 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 12026 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
DV
12027 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
12028 const enum pipe pipe = intel_crtc->pipe;
d2196774 12029 u32 ctl, stride = skl_plane_stride(fb, 0, rotation);
5a21b665
DV
12030
12031 ctl = I915_READ(PLANE_CTL(pipe, 0));
12032 ctl &= ~PLANE_CTL_TILED_MASK;
bae781b2 12033 switch (fb->modifier) {
5a21b665
DV
12034 case DRM_FORMAT_MOD_NONE:
12035 break;
12036 case I915_FORMAT_MOD_X_TILED:
12037 ctl |= PLANE_CTL_TILED_X;
12038 break;
12039 case I915_FORMAT_MOD_Y_TILED:
12040 ctl |= PLANE_CTL_TILED_Y;
12041 break;
12042 case I915_FORMAT_MOD_Yf_TILED:
12043 ctl |= PLANE_CTL_TILED_YF;
12044 break;
12045 default:
bae781b2 12046 MISSING_CASE(fb->modifier);
5a21b665
DV
12047 }
12048
5a21b665
DV
12049 /*
12050 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
12051 * PLANE_SURF updates, the update is then guaranteed to be atomic.
12052 */
12053 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
12054 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
12055
12056 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
12057 POSTING_READ(PLANE_SURF(pipe, 0));
12058}
12059
12060static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
12061 struct intel_flip_work *work)
12062{
12063 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 12064 struct drm_i915_private *dev_priv = to_i915(dev);
72618ebf 12065 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
5a21b665
DV
12066 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
12067 u32 dspcntr;
12068
12069 dspcntr = I915_READ(reg);
12070
bae781b2 12071 if (fb->modifier == I915_FORMAT_MOD_X_TILED)
5a21b665
DV
12072 dspcntr |= DISPPLANE_TILED;
12073 else
12074 dspcntr &= ~DISPPLANE_TILED;
12075
12076 I915_WRITE(reg, dspcntr);
12077
12078 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
12079 POSTING_READ(DSPSURF(intel_crtc->plane));
12080}
12081
12082static void intel_mmio_flip_work_func(struct work_struct *w)
12083{
12084 struct intel_flip_work *work =
12085 container_of(w, struct intel_flip_work, mmio_work);
12086 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
12087 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
12088 struct intel_framebuffer *intel_fb =
12089 to_intel_framebuffer(crtc->base.primary->fb);
12090 struct drm_i915_gem_object *obj = intel_fb->obj;
12091
d07f0e59 12092 WARN_ON(i915_gem_object_wait(obj, 0, MAX_SCHEDULE_TIMEOUT, NULL) < 0);
5a21b665
DV
12093
12094 intel_pipe_update_start(crtc);
12095
12096 if (INTEL_GEN(dev_priv) >= 9)
12097 skl_do_mmio_flip(crtc, work->rotation, work);
12098 else
12099 /* use_mmio_flip() retricts MMIO flips to ilk+ */
12100 ilk_do_mmio_flip(crtc, work);
12101
12102 intel_pipe_update_end(crtc, work);
12103}
12104
12105static int intel_default_queue_flip(struct drm_device *dev,
12106 struct drm_crtc *crtc,
12107 struct drm_framebuffer *fb,
12108 struct drm_i915_gem_object *obj,
12109 struct drm_i915_gem_request *req,
12110 uint32_t flags)
12111{
12112 return -ENODEV;
12113}
12114
12115static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
12116 struct intel_crtc *intel_crtc,
12117 struct intel_flip_work *work)
12118{
12119 u32 addr, vblank;
12120
12121 if (!atomic_read(&work->pending))
12122 return false;
12123
12124 smp_rmb();
12125
12126 vblank = intel_crtc_get_vblank_counter(intel_crtc);
12127 if (work->flip_ready_vblank == 0) {
12128 if (work->flip_queued_req &&
f69a02c9 12129 !i915_gem_request_completed(work->flip_queued_req))
5a21b665
DV
12130 return false;
12131
12132 work->flip_ready_vblank = vblank;
12133 }
12134
12135 if (vblank - work->flip_ready_vblank < 3)
12136 return false;
12137
12138 /* Potential stall - if we see that the flip has happened,
12139 * assume a missed interrupt. */
12140 if (INTEL_GEN(dev_priv) >= 4)
12141 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
12142 else
12143 addr = I915_READ(DSPADDR(intel_crtc->plane));
12144
12145 /* There is a potential issue here with a false positive after a flip
12146 * to the same address. We could address this by checking for a
12147 * non-incrementing frame counter.
12148 */
12149 return addr == work->gtt_offset;
12150}
12151
12152void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
12153{
91c8a326 12154 struct drm_device *dev = &dev_priv->drm;
98187836 12155 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
5a21b665
DV
12156 struct intel_flip_work *work;
12157
12158 WARN_ON(!in_interrupt());
12159
12160 if (crtc == NULL)
12161 return;
12162
12163 spin_lock(&dev->event_lock);
e2af48c6 12164 work = crtc->flip_work;
5a21b665
DV
12165
12166 if (work != NULL && !is_mmio_work(work) &&
e2af48c6 12167 __pageflip_stall_check_cs(dev_priv, crtc, work)) {
5a21b665
DV
12168 WARN_ONCE(1,
12169 "Kicking stuck page flip: queued at %d, now %d\n",
e2af48c6
VS
12170 work->flip_queued_vblank, intel_crtc_get_vblank_counter(crtc));
12171 page_flip_completed(crtc);
5a21b665
DV
12172 work = NULL;
12173 }
12174
12175 if (work != NULL && !is_mmio_work(work) &&
e2af48c6 12176 intel_crtc_get_vblank_counter(crtc) - work->flip_queued_vblank > 1)
5a21b665
DV
12177 intel_queue_rps_boost_for_request(work->flip_queued_req);
12178 spin_unlock(&dev->event_lock);
12179}
12180
4c01ded5 12181__maybe_unused
5a21b665
DV
12182static int intel_crtc_page_flip(struct drm_crtc *crtc,
12183 struct drm_framebuffer *fb,
12184 struct drm_pending_vblank_event *event,
12185 uint32_t page_flip_flags)
12186{
12187 struct drm_device *dev = crtc->dev;
fac5e23e 12188 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
DV
12189 struct drm_framebuffer *old_fb = crtc->primary->fb;
12190 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
12191 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12192 struct drm_plane *primary = crtc->primary;
12193 enum pipe pipe = intel_crtc->pipe;
12194 struct intel_flip_work *work;
12195 struct intel_engine_cs *engine;
12196 bool mmio_flip;
8e637178 12197 struct drm_i915_gem_request *request;
058d88c4 12198 struct i915_vma *vma;
5a21b665
DV
12199 int ret;
12200
12201 /*
12202 * drm_mode_page_flip_ioctl() should already catch this, but double
12203 * check to be safe. In the future we may enable pageflipping from
12204 * a disabled primary plane.
12205 */
12206 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
12207 return -EBUSY;
12208
12209 /* Can't change pixel format via MI display flips. */
dbd4d576 12210 if (fb->format != crtc->primary->fb->format)
5a21b665
DV
12211 return -EINVAL;
12212
12213 /*
12214 * TILEOFF/LINOFF registers can't be changed via MI display flips.
12215 * Note that pitch changes could also affect these register.
12216 */
6315b5d3 12217 if (INTEL_GEN(dev_priv) > 3 &&
5a21b665
DV
12218 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
12219 fb->pitches[0] != crtc->primary->fb->pitches[0]))
12220 return -EINVAL;
12221
12222 if (i915_terminally_wedged(&dev_priv->gpu_error))
12223 goto out_hang;
12224
12225 work = kzalloc(sizeof(*work), GFP_KERNEL);
12226 if (work == NULL)
12227 return -ENOMEM;
12228
12229 work->event = event;
12230 work->crtc = crtc;
12231 work->old_fb = old_fb;
12232 INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
12233
12234 ret = drm_crtc_vblank_get(crtc);
12235 if (ret)
12236 goto free_work;
12237
12238 /* We borrow the event spin lock for protecting flip_work */
12239 spin_lock_irq(&dev->event_lock);
12240 if (intel_crtc->flip_work) {
12241 /* Before declaring the flip queue wedged, check if
12242 * the hardware completed the operation behind our backs.
12243 */
12244 if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
12245 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
12246 page_flip_completed(intel_crtc);
12247 } else {
12248 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
12249 spin_unlock_irq(&dev->event_lock);
12250
12251 drm_crtc_vblank_put(crtc);
12252 kfree(work);
12253 return -EBUSY;
12254 }
12255 }
12256 intel_crtc->flip_work = work;
12257 spin_unlock_irq(&dev->event_lock);
12258
12259 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
12260 flush_workqueue(dev_priv->wq);
12261
12262 /* Reference the objects for the scheduled work. */
12263 drm_framebuffer_reference(work->old_fb);
5a21b665
DV
12264
12265 crtc->primary->fb = fb;
12266 update_state_fb(crtc->primary);
faf68d92 12267
25dc556a 12268 work->pending_flip_obj = i915_gem_object_get(obj);
5a21b665
DV
12269
12270 ret = i915_mutex_lock_interruptible(dev);
12271 if (ret)
12272 goto cleanup;
12273
8af29b0c
CW
12274 intel_crtc->reset_count = i915_reset_count(&dev_priv->gpu_error);
12275 if (i915_reset_in_progress_or_wedged(&dev_priv->gpu_error)) {
5a21b665 12276 ret = -EIO;
ddbb271a 12277 goto unlock;
5a21b665
DV
12278 }
12279
12280 atomic_inc(&intel_crtc->unpin_work_count);
12281
9beb5fea 12282 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
5a21b665
DV
12283 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
12284
920a14b2 12285 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
3b3f1650 12286 engine = dev_priv->engine[BCS];
bae781b2 12287 if (fb->modifier != old_fb->modifier)
5a21b665
DV
12288 /* vlv: DISPLAY_FLIP fails to change tiling */
12289 engine = NULL;
fd6b8f43 12290 } else if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
3b3f1650 12291 engine = dev_priv->engine[BCS];
6315b5d3 12292 } else if (INTEL_GEN(dev_priv) >= 7) {
d07f0e59 12293 engine = i915_gem_object_last_write_engine(obj);
5a21b665 12294 if (engine == NULL || engine->id != RCS)
3b3f1650 12295 engine = dev_priv->engine[BCS];
5a21b665 12296 } else {
3b3f1650 12297 engine = dev_priv->engine[RCS];
5a21b665
DV
12298 }
12299
12300 mmio_flip = use_mmio_flip(engine, obj);
12301
058d88c4
CW
12302 vma = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
12303 if (IS_ERR(vma)) {
12304 ret = PTR_ERR(vma);
5a21b665 12305 goto cleanup_pending;
058d88c4 12306 }
5a21b665 12307
be1e3415
CW
12308 work->old_vma = to_intel_plane_state(primary->state)->vma;
12309 to_intel_plane_state(primary->state)->vma = vma;
12310
12311 work->gtt_offset = i915_ggtt_offset(vma) + intel_crtc->dspaddr_offset;
5a21b665
DV
12312 work->rotation = crtc->primary->state->rotation;
12313
1f061316
PZ
12314 /*
12315 * There's the potential that the next frame will not be compatible with
12316 * FBC, so we want to call pre_update() before the actual page flip.
12317 * The problem is that pre_update() caches some information about the fb
12318 * object, so we want to do this only after the object is pinned. Let's
12319 * be on the safe side and do this immediately before scheduling the
12320 * flip.
12321 */
12322 intel_fbc_pre_update(intel_crtc, intel_crtc->config,
12323 to_intel_plane_state(primary->state));
12324
5a21b665
DV
12325 if (mmio_flip) {
12326 INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
6277c8d0 12327 queue_work(system_unbound_wq, &work->mmio_work);
5a21b665 12328 } else {
e8a9c58f
CW
12329 request = i915_gem_request_alloc(engine,
12330 dev_priv->kernel_context);
8e637178
CW
12331 if (IS_ERR(request)) {
12332 ret = PTR_ERR(request);
12333 goto cleanup_unpin;
12334 }
12335
a2bc4695 12336 ret = i915_gem_request_await_object(request, obj, false);
8e637178
CW
12337 if (ret)
12338 goto cleanup_request;
12339
5a21b665
DV
12340 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
12341 page_flip_flags);
12342 if (ret)
8e637178 12343 goto cleanup_request;
5a21b665
DV
12344
12345 intel_mark_page_flip_active(intel_crtc, work);
12346
8e637178 12347 work->flip_queued_req = i915_gem_request_get(request);
5a21b665
DV
12348 i915_add_request_no_flush(request);
12349 }
12350
92117f0b 12351 i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
5a21b665
DV
12352 i915_gem_track_fb(intel_fb_obj(old_fb), obj,
12353 to_intel_plane(primary)->frontbuffer_bit);
12354 mutex_unlock(&dev->struct_mutex);
12355
5748b6a1 12356 intel_frontbuffer_flip_prepare(to_i915(dev),
5a21b665
DV
12357 to_intel_plane(primary)->frontbuffer_bit);
12358
12359 trace_i915_flip_request(intel_crtc->plane, obj);
12360
12361 return 0;
12362
8e637178
CW
12363cleanup_request:
12364 i915_add_request_no_flush(request);
5a21b665 12365cleanup_unpin:
be1e3415
CW
12366 to_intel_plane_state(primary->state)->vma = work->old_vma;
12367 intel_unpin_fb_vma(vma);
5a21b665 12368cleanup_pending:
5a21b665 12369 atomic_dec(&intel_crtc->unpin_work_count);
ddbb271a 12370unlock:
5a21b665
DV
12371 mutex_unlock(&dev->struct_mutex);
12372cleanup:
12373 crtc->primary->fb = old_fb;
12374 update_state_fb(crtc->primary);
12375
f0cd5182 12376 i915_gem_object_put(obj);
5a21b665
DV
12377 drm_framebuffer_unreference(work->old_fb);
12378
12379 spin_lock_irq(&dev->event_lock);
12380 intel_crtc->flip_work = NULL;
12381 spin_unlock_irq(&dev->event_lock);
12382
12383 drm_crtc_vblank_put(crtc);
12384free_work:
12385 kfree(work);
12386
12387 if (ret == -EIO) {
12388 struct drm_atomic_state *state;
12389 struct drm_plane_state *plane_state;
12390
12391out_hang:
12392 state = drm_atomic_state_alloc(dev);
12393 if (!state)
12394 return -ENOMEM;
12395 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
12396
12397retry:
12398 plane_state = drm_atomic_get_plane_state(state, primary);
12399 ret = PTR_ERR_OR_ZERO(plane_state);
12400 if (!ret) {
12401 drm_atomic_set_fb_for_plane(plane_state, fb);
12402
12403 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
12404 if (!ret)
12405 ret = drm_atomic_commit(state);
12406 }
12407
12408 if (ret == -EDEADLK) {
12409 drm_modeset_backoff(state->acquire_ctx);
12410 drm_atomic_state_clear(state);
12411 goto retry;
12412 }
12413
0853695c 12414 drm_atomic_state_put(state);
5a21b665
DV
12415
12416 if (ret == 0 && event) {
12417 spin_lock_irq(&dev->event_lock);
12418 drm_crtc_send_vblank_event(crtc, event);
12419 spin_unlock_irq(&dev->event_lock);
12420 }
12421 }
12422 return ret;
12423}
12424
12425
12426/**
12427 * intel_wm_need_update - Check whether watermarks need updating
12428 * @plane: drm plane
12429 * @state: new plane state
12430 *
12431 * Check current plane state versus the new one to determine whether
12432 * watermarks need to be recalculated.
12433 *
12434 * Returns true or false.
12435 */
12436static bool intel_wm_need_update(struct drm_plane *plane,
12437 struct drm_plane_state *state)
12438{
12439 struct intel_plane_state *new = to_intel_plane_state(state);
12440 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
12441
12442 /* Update watermarks on tiling or size changes. */
936e71e3 12443 if (new->base.visible != cur->base.visible)
5a21b665
DV
12444 return true;
12445
12446 if (!cur->base.fb || !new->base.fb)
12447 return false;
12448
bae781b2 12449 if (cur->base.fb->modifier != new->base.fb->modifier ||
5a21b665 12450 cur->base.rotation != new->base.rotation ||
936e71e3
VS
12451 drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
12452 drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
12453 drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
12454 drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
5a21b665
DV
12455 return true;
12456
12457 return false;
12458}
12459
12460static bool needs_scaling(struct intel_plane_state *state)
12461{
936e71e3
VS
12462 int src_w = drm_rect_width(&state->base.src) >> 16;
12463 int src_h = drm_rect_height(&state->base.src) >> 16;
12464 int dst_w = drm_rect_width(&state->base.dst);
12465 int dst_h = drm_rect_height(&state->base.dst);
5a21b665
DV
12466
12467 return (src_w != dst_w || src_h != dst_h);
12468}
d21fbe87 12469
da20eabd
ML
12470int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
12471 struct drm_plane_state *plane_state)
12472{
ab1d3a0e 12473 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
da20eabd
ML
12474 struct drm_crtc *crtc = crtc_state->crtc;
12475 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12476 struct drm_plane *plane = plane_state->plane;
12477 struct drm_device *dev = crtc->dev;
ed4a6a7c 12478 struct drm_i915_private *dev_priv = to_i915(dev);
da20eabd
ML
12479 struct intel_plane_state *old_plane_state =
12480 to_intel_plane_state(plane->state);
da20eabd
ML
12481 bool mode_changed = needs_modeset(crtc_state);
12482 bool was_crtc_enabled = crtc->state->active;
12483 bool is_crtc_enabled = crtc_state->active;
da20eabd
ML
12484 bool turn_off, turn_on, visible, was_visible;
12485 struct drm_framebuffer *fb = plane_state->fb;
78108b7c 12486 int ret;
da20eabd 12487
55b8f2a7 12488 if (INTEL_GEN(dev_priv) >= 9 && plane->type != DRM_PLANE_TYPE_CURSOR) {
da20eabd
ML
12489 ret = skl_update_scaler_plane(
12490 to_intel_crtc_state(crtc_state),
12491 to_intel_plane_state(plane_state));
12492 if (ret)
12493 return ret;
12494 }
12495
936e71e3 12496 was_visible = old_plane_state->base.visible;
1d4258db 12497 visible = plane_state->visible;
da20eabd
ML
12498
12499 if (!was_crtc_enabled && WARN_ON(was_visible))
12500 was_visible = false;
12501
35c08f43
ML
12502 /*
12503 * Visibility is calculated as if the crtc was on, but
12504 * after scaler setup everything depends on it being off
12505 * when the crtc isn't active.
f818ffea
VS
12506 *
12507 * FIXME this is wrong for watermarks. Watermarks should also
12508 * be computed as if the pipe would be active. Perhaps move
12509 * per-plane wm computation to the .check_plane() hook, and
12510 * only combine the results from all planes in the current place?
35c08f43
ML
12511 */
12512 if (!is_crtc_enabled)
1d4258db 12513 plane_state->visible = visible = false;
da20eabd
ML
12514
12515 if (!was_visible && !visible)
12516 return 0;
12517
e8861675
ML
12518 if (fb != old_plane_state->base.fb)
12519 pipe_config->fb_changed = true;
12520
da20eabd
ML
12521 turn_off = was_visible && (!visible || mode_changed);
12522 turn_on = visible && (!was_visible || mode_changed);
12523
72660ce0 12524 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
78108b7c
VS
12525 intel_crtc->base.base.id,
12526 intel_crtc->base.name,
72660ce0
VS
12527 plane->base.id, plane->name,
12528 fb ? fb->base.id : -1);
da20eabd 12529
72660ce0
VS
12530 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
12531 plane->base.id, plane->name,
12532 was_visible, visible,
da20eabd
ML
12533 turn_off, turn_on, mode_changed);
12534
caed361d
VS
12535 if (turn_on) {
12536 pipe_config->update_wm_pre = true;
12537
12538 /* must disable cxsr around plane enable/disable */
12539 if (plane->type != DRM_PLANE_TYPE_CURSOR)
12540 pipe_config->disable_cxsr = true;
12541 } else if (turn_off) {
12542 pipe_config->update_wm_post = true;
92826fcd 12543
852eb00d 12544 /* must disable cxsr around plane enable/disable */
e8861675 12545 if (plane->type != DRM_PLANE_TYPE_CURSOR)
ab1d3a0e 12546 pipe_config->disable_cxsr = true;
852eb00d 12547 } else if (intel_wm_need_update(plane, plane_state)) {
caed361d
VS
12548 /* FIXME bollocks */
12549 pipe_config->update_wm_pre = true;
12550 pipe_config->update_wm_post = true;
852eb00d 12551 }
da20eabd 12552
ed4a6a7c 12553 /* Pre-gen9 platforms need two-step watermark updates */
caed361d 12554 if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
6315b5d3 12555 INTEL_GEN(dev_priv) < 9 && dev_priv->display.optimize_watermarks)
ed4a6a7c
MR
12556 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
12557
8be6ca85 12558 if (visible || was_visible)
cd202f69 12559 pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
a9ff8714 12560
31ae71fc
ML
12561 /*
12562 * WaCxSRDisabledForSpriteScaling:ivb
12563 *
12564 * cstate->update_wm was already set above, so this flag will
12565 * take effect when we commit and program watermarks.
12566 */
fd6b8f43 12567 if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev_priv) &&
31ae71fc
ML
12568 needs_scaling(to_intel_plane_state(plane_state)) &&
12569 !needs_scaling(old_plane_state))
12570 pipe_config->disable_lp_wm = true;
d21fbe87 12571
da20eabd
ML
12572 return 0;
12573}
12574
6d3a1ce7
ML
12575static bool encoders_cloneable(const struct intel_encoder *a,
12576 const struct intel_encoder *b)
12577{
12578 /* masks could be asymmetric, so check both ways */
12579 return a == b || (a->cloneable & (1 << b->type) &&
12580 b->cloneable & (1 << a->type));
12581}
12582
12583static bool check_single_encoder_cloning(struct drm_atomic_state *state,
12584 struct intel_crtc *crtc,
12585 struct intel_encoder *encoder)
12586{
12587 struct intel_encoder *source_encoder;
12588 struct drm_connector *connector;
12589 struct drm_connector_state *connector_state;
12590 int i;
12591
12592 for_each_connector_in_state(state, connector, connector_state, i) {
12593 if (connector_state->crtc != &crtc->base)
12594 continue;
12595
12596 source_encoder =
12597 to_intel_encoder(connector_state->best_encoder);
12598 if (!encoders_cloneable(encoder, source_encoder))
12599 return false;
12600 }
12601
12602 return true;
12603}
12604
6d3a1ce7
ML
12605static int intel_crtc_atomic_check(struct drm_crtc *crtc,
12606 struct drm_crtc_state *crtc_state)
12607{
cf5a15be 12608 struct drm_device *dev = crtc->dev;
fac5e23e 12609 struct drm_i915_private *dev_priv = to_i915(dev);
6d3a1ce7 12610 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cf5a15be
ML
12611 struct intel_crtc_state *pipe_config =
12612 to_intel_crtc_state(crtc_state);
6d3a1ce7 12613 struct drm_atomic_state *state = crtc_state->state;
4d20cd86 12614 int ret;
6d3a1ce7
ML
12615 bool mode_changed = needs_modeset(crtc_state);
12616
852eb00d 12617 if (mode_changed && !crtc_state->active)
caed361d 12618 pipe_config->update_wm_post = true;
eddfcbcd 12619
ad421372
ML
12620 if (mode_changed && crtc_state->enable &&
12621 dev_priv->display.crtc_compute_clock &&
8106ddbd 12622 !WARN_ON(pipe_config->shared_dpll)) {
ad421372
ML
12623 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
12624 pipe_config);
12625 if (ret)
12626 return ret;
12627 }
12628
82cf435b
LL
12629 if (crtc_state->color_mgmt_changed) {
12630 ret = intel_color_check(crtc, crtc_state);
12631 if (ret)
12632 return ret;
e7852a4b
LL
12633
12634 /*
12635 * Changing color management on Intel hardware is
12636 * handled as part of planes update.
12637 */
12638 crtc_state->planes_changed = true;
82cf435b
LL
12639 }
12640
e435d6e5 12641 ret = 0;
86c8bbbe 12642 if (dev_priv->display.compute_pipe_wm) {
e3bddded 12643 ret = dev_priv->display.compute_pipe_wm(pipe_config);
ed4a6a7c
MR
12644 if (ret) {
12645 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
12646 return ret;
12647 }
12648 }
12649
12650 if (dev_priv->display.compute_intermediate_wm &&
12651 !to_intel_atomic_state(state)->skip_intermediate_wm) {
12652 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
12653 return 0;
12654
12655 /*
12656 * Calculate 'intermediate' watermarks that satisfy both the
12657 * old state and the new state. We can program these
12658 * immediately.
12659 */
6315b5d3 12660 ret = dev_priv->display.compute_intermediate_wm(dev,
ed4a6a7c
MR
12661 intel_crtc,
12662 pipe_config);
12663 if (ret) {
12664 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
86c8bbbe 12665 return ret;
ed4a6a7c 12666 }
e3d5457c
VS
12667 } else if (dev_priv->display.compute_intermediate_wm) {
12668 if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
12669 pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
86c8bbbe
MR
12670 }
12671
6315b5d3 12672 if (INTEL_GEN(dev_priv) >= 9) {
e435d6e5
ML
12673 if (mode_changed)
12674 ret = skl_update_scaler_crtc(pipe_config);
12675
12676 if (!ret)
12677 ret = intel_atomic_setup_scalers(dev, intel_crtc,
12678 pipe_config);
12679 }
12680
12681 return ret;
6d3a1ce7
ML
12682}
12683
65b38e0d 12684static const struct drm_crtc_helper_funcs intel_helper_funcs = {
f6e5b160 12685 .mode_set_base_atomic = intel_pipe_set_base_atomic,
5a21b665
DV
12686 .atomic_begin = intel_begin_crtc_commit,
12687 .atomic_flush = intel_finish_crtc_commit,
6d3a1ce7 12688 .atomic_check = intel_crtc_atomic_check,
f6e5b160
CW
12689};
12690
d29b2f9d
ACO
12691static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
12692{
12693 struct intel_connector *connector;
12694
12695 for_each_intel_connector(dev, connector) {
8863dc7f
DV
12696 if (connector->base.state->crtc)
12697 drm_connector_unreference(&connector->base);
12698
d29b2f9d
ACO
12699 if (connector->base.encoder) {
12700 connector->base.state->best_encoder =
12701 connector->base.encoder;
12702 connector->base.state->crtc =
12703 connector->base.encoder->crtc;
8863dc7f
DV
12704
12705 drm_connector_reference(&connector->base);
d29b2f9d
ACO
12706 } else {
12707 connector->base.state->best_encoder = NULL;
12708 connector->base.state->crtc = NULL;
12709 }
12710 }
12711}
12712
050f7aeb 12713static void
eba905b2 12714connected_sink_compute_bpp(struct intel_connector *connector,
5cec258b 12715 struct intel_crtc_state *pipe_config)
050f7aeb 12716{
6a2a5c5d 12717 const struct drm_display_info *info = &connector->base.display_info;
050f7aeb
DV
12718 int bpp = pipe_config->pipe_bpp;
12719
12720 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
6a2a5c5d
VS
12721 connector->base.base.id,
12722 connector->base.name);
050f7aeb
DV
12723
12724 /* Don't use an invalid EDID bpc value */
6a2a5c5d 12725 if (info->bpc != 0 && info->bpc * 3 < bpp) {
050f7aeb 12726 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
6a2a5c5d
VS
12727 bpp, info->bpc * 3);
12728 pipe_config->pipe_bpp = info->bpc * 3;
050f7aeb
DV
12729 }
12730
196f954e 12731 /* Clamp bpp to 8 on screens without EDID 1.4 */
6a2a5c5d 12732 if (info->bpc == 0 && bpp > 24) {
196f954e
MK
12733 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
12734 bpp);
12735 pipe_config->pipe_bpp = 24;
050f7aeb
DV
12736 }
12737}
12738
4e53c2e0 12739static int
050f7aeb 12740compute_baseline_pipe_bpp(struct intel_crtc *crtc,
5cec258b 12741 struct intel_crtc_state *pipe_config)
4e53c2e0 12742{
9beb5fea 12743 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1486017f 12744 struct drm_atomic_state *state;
da3ced29
ACO
12745 struct drm_connector *connector;
12746 struct drm_connector_state *connector_state;
1486017f 12747 int bpp, i;
4e53c2e0 12748
9beb5fea
TU
12749 if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
12750 IS_CHERRYVIEW(dev_priv)))
4e53c2e0 12751 bpp = 10*3;
9beb5fea 12752 else if (INTEL_GEN(dev_priv) >= 5)
d328c9d7
DV
12753 bpp = 12*3;
12754 else
12755 bpp = 8*3;
12756
4e53c2e0 12757
4e53c2e0
DV
12758 pipe_config->pipe_bpp = bpp;
12759
1486017f
ACO
12760 state = pipe_config->base.state;
12761
4e53c2e0 12762 /* Clamp display bpp to EDID value */
da3ced29
ACO
12763 for_each_connector_in_state(state, connector, connector_state, i) {
12764 if (connector_state->crtc != &crtc->base)
4e53c2e0
DV
12765 continue;
12766
da3ced29
ACO
12767 connected_sink_compute_bpp(to_intel_connector(connector),
12768 pipe_config);
4e53c2e0
DV
12769 }
12770
12771 return bpp;
12772}
12773
644db711
DV
12774static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
12775{
12776 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
12777 "type: 0x%x flags: 0x%x\n",
1342830c 12778 mode->crtc_clock,
644db711
DV
12779 mode->crtc_hdisplay, mode->crtc_hsync_start,
12780 mode->crtc_hsync_end, mode->crtc_htotal,
12781 mode->crtc_vdisplay, mode->crtc_vsync_start,
12782 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
12783}
12784
f6982332
TU
12785static inline void
12786intel_dump_m_n_config(struct intel_crtc_state *pipe_config, char *id,
a4309657 12787 unsigned int lane_count, struct intel_link_m_n *m_n)
f6982332 12788{
a4309657
TU
12789 DRM_DEBUG_KMS("%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12790 id, lane_count,
f6982332
TU
12791 m_n->gmch_m, m_n->gmch_n,
12792 m_n->link_m, m_n->link_n, m_n->tu);
12793}
12794
c0b03411 12795static void intel_dump_pipe_config(struct intel_crtc *crtc,
5cec258b 12796 struct intel_crtc_state *pipe_config,
c0b03411
DV
12797 const char *context)
12798{
6a60cd87 12799 struct drm_device *dev = crtc->base.dev;
4f8036a2 12800 struct drm_i915_private *dev_priv = to_i915(dev);
6a60cd87
CK
12801 struct drm_plane *plane;
12802 struct intel_plane *intel_plane;
12803 struct intel_plane_state *state;
12804 struct drm_framebuffer *fb;
12805
66766e4f
TU
12806 DRM_DEBUG_KMS("[CRTC:%d:%s]%s\n",
12807 crtc->base.base.id, crtc->base.name, context);
c0b03411 12808
2c89429e
TU
12809 DRM_DEBUG_KMS("cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
12810 transcoder_name(pipe_config->cpu_transcoder),
c0b03411 12811 pipe_config->pipe_bpp, pipe_config->dither);
a4309657
TU
12812
12813 if (pipe_config->has_pch_encoder)
12814 intel_dump_m_n_config(pipe_config, "fdi",
12815 pipe_config->fdi_lanes,
12816 &pipe_config->fdi_m_n);
f6982332
TU
12817
12818 if (intel_crtc_has_dp_encoder(pipe_config)) {
a4309657
TU
12819 intel_dump_m_n_config(pipe_config, "dp m_n",
12820 pipe_config->lane_count, &pipe_config->dp_m_n);
d806e682
TU
12821 if (pipe_config->has_drrs)
12822 intel_dump_m_n_config(pipe_config, "dp m2_n2",
12823 pipe_config->lane_count,
12824 &pipe_config->dp_m2_n2);
f6982332 12825 }
b95af8be 12826
55072d19 12827 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
2c89429e 12828 pipe_config->has_audio, pipe_config->has_infoframe);
55072d19 12829
c0b03411 12830 DRM_DEBUG_KMS("requested mode:\n");
2d112de7 12831 drm_mode_debug_printmodeline(&pipe_config->base.mode);
c0b03411 12832 DRM_DEBUG_KMS("adjusted mode:\n");
2d112de7
ACO
12833 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12834 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
2c89429e
TU
12835 DRM_DEBUG_KMS("port clock: %d, pipe src size: %dx%d\n",
12836 pipe_config->port_clock,
37327abd 12837 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
dd2f616d
TU
12838
12839 if (INTEL_GEN(dev_priv) >= 9)
12840 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12841 crtc->num_scalers,
12842 pipe_config->scaler_state.scaler_users,
12843 pipe_config->scaler_state.scaler_id);
a74f8375
TU
12844
12845 if (HAS_GMCH_DISPLAY(dev_priv))
12846 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12847 pipe_config->gmch_pfit.control,
12848 pipe_config->gmch_pfit.pgm_ratios,
12849 pipe_config->gmch_pfit.lvds_border_bits);
12850 else
12851 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
12852 pipe_config->pch_pfit.pos,
12853 pipe_config->pch_pfit.size,
08c4d7fc 12854 enableddisabled(pipe_config->pch_pfit.enabled));
a74f8375 12855
2c89429e
TU
12856 DRM_DEBUG_KMS("ips: %i, double wide: %i\n",
12857 pipe_config->ips_enabled, pipe_config->double_wide);
6a60cd87 12858
f50b79f0 12859 intel_dpll_dump_hw_state(dev_priv, &pipe_config->dpll_hw_state);
415ff0f6 12860
6a60cd87
CK
12861 DRM_DEBUG_KMS("planes on this crtc\n");
12862 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
b3c11ac2 12863 struct drm_format_name_buf format_name;
6a60cd87
CK
12864 intel_plane = to_intel_plane(plane);
12865 if (intel_plane->pipe != crtc->pipe)
12866 continue;
12867
12868 state = to_intel_plane_state(plane->state);
12869 fb = state->base.fb;
12870 if (!fb) {
1d577e02
VS
12871 DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
12872 plane->base.id, plane->name, state->scaler_id);
6a60cd87
CK
12873 continue;
12874 }
12875
dd2f616d
TU
12876 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d, fb = %ux%u format = %s\n",
12877 plane->base.id, plane->name,
b3c11ac2 12878 fb->base.id, fb->width, fb->height,
438b74a5 12879 drm_get_format_name(fb->format->format, &format_name));
dd2f616d
TU
12880 if (INTEL_GEN(dev_priv) >= 9)
12881 DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
12882 state->scaler_id,
12883 state->base.src.x1 >> 16,
12884 state->base.src.y1 >> 16,
12885 drm_rect_width(&state->base.src) >> 16,
12886 drm_rect_height(&state->base.src) >> 16,
12887 state->base.dst.x1, state->base.dst.y1,
12888 drm_rect_width(&state->base.dst),
12889 drm_rect_height(&state->base.dst));
6a60cd87 12890 }
c0b03411
DV
12891}
12892
5448a00d 12893static bool check_digital_port_conflicts(struct drm_atomic_state *state)
00f0b378 12894{
5448a00d 12895 struct drm_device *dev = state->dev;
da3ced29 12896 struct drm_connector *connector;
00f0b378 12897 unsigned int used_ports = 0;
477321e0 12898 unsigned int used_mst_ports = 0;
00f0b378
VS
12899
12900 /*
12901 * Walk the connector list instead of the encoder
12902 * list to detect the problem on ddi platforms
12903 * where there's just one encoder per digital port.
12904 */
0bff4858
VS
12905 drm_for_each_connector(connector, dev) {
12906 struct drm_connector_state *connector_state;
12907 struct intel_encoder *encoder;
12908
12909 connector_state = drm_atomic_get_existing_connector_state(state, connector);
12910 if (!connector_state)
12911 connector_state = connector->state;
12912
5448a00d 12913 if (!connector_state->best_encoder)
00f0b378
VS
12914 continue;
12915
5448a00d
ACO
12916 encoder = to_intel_encoder(connector_state->best_encoder);
12917
12918 WARN_ON(!connector_state->crtc);
00f0b378
VS
12919
12920 switch (encoder->type) {
12921 unsigned int port_mask;
12922 case INTEL_OUTPUT_UNKNOWN:
4f8036a2 12923 if (WARN_ON(!HAS_DDI(to_i915(dev))))
00f0b378 12924 break;
cca0502b 12925 case INTEL_OUTPUT_DP:
00f0b378
VS
12926 case INTEL_OUTPUT_HDMI:
12927 case INTEL_OUTPUT_EDP:
12928 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12929
12930 /* the same port mustn't appear more than once */
12931 if (used_ports & port_mask)
12932 return false;
12933
12934 used_ports |= port_mask;
477321e0
VS
12935 break;
12936 case INTEL_OUTPUT_DP_MST:
12937 used_mst_ports |=
12938 1 << enc_to_mst(&encoder->base)->primary->port;
12939 break;
00f0b378
VS
12940 default:
12941 break;
12942 }
12943 }
12944
477321e0
VS
12945 /* can't mix MST and SST/HDMI on the same port */
12946 if (used_ports & used_mst_ports)
12947 return false;
12948
00f0b378
VS
12949 return true;
12950}
12951
83a57153
ACO
12952static void
12953clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12954{
12955 struct drm_crtc_state tmp_state;
663a3640 12956 struct intel_crtc_scaler_state scaler_state;
4978cc93 12957 struct intel_dpll_hw_state dpll_hw_state;
8106ddbd 12958 struct intel_shared_dpll *shared_dpll;
c4e2d043 12959 bool force_thru;
83a57153 12960
7546a384
ACO
12961 /* FIXME: before the switch to atomic started, a new pipe_config was
12962 * kzalloc'd. Code that depends on any field being zero should be
12963 * fixed, so that the crtc_state can be safely duplicated. For now,
12964 * only fields that are know to not cause problems are preserved. */
12965
83a57153 12966 tmp_state = crtc_state->base;
663a3640 12967 scaler_state = crtc_state->scaler_state;
4978cc93
ACO
12968 shared_dpll = crtc_state->shared_dpll;
12969 dpll_hw_state = crtc_state->dpll_hw_state;
c4e2d043 12970 force_thru = crtc_state->pch_pfit.force_thru;
4978cc93 12971
83a57153 12972 memset(crtc_state, 0, sizeof *crtc_state);
4978cc93 12973
83a57153 12974 crtc_state->base = tmp_state;
663a3640 12975 crtc_state->scaler_state = scaler_state;
4978cc93
ACO
12976 crtc_state->shared_dpll = shared_dpll;
12977 crtc_state->dpll_hw_state = dpll_hw_state;
c4e2d043 12978 crtc_state->pch_pfit.force_thru = force_thru;
83a57153
ACO
12979}
12980
548ee15b 12981static int
b8cecdf5 12982intel_modeset_pipe_config(struct drm_crtc *crtc,
b359283a 12983 struct intel_crtc_state *pipe_config)
ee7b9f93 12984{
b359283a 12985 struct drm_atomic_state *state = pipe_config->base.state;
7758a113 12986 struct intel_encoder *encoder;
da3ced29 12987 struct drm_connector *connector;
0b901879 12988 struct drm_connector_state *connector_state;
d328c9d7 12989 int base_bpp, ret = -EINVAL;
0b901879 12990 int i;
e29c22c0 12991 bool retry = true;
ee7b9f93 12992
83a57153 12993 clear_intel_crtc_state(pipe_config);
7758a113 12994
e143a21c
DV
12995 pipe_config->cpu_transcoder =
12996 (enum transcoder) to_intel_crtc(crtc)->pipe;
b8cecdf5 12997
2960bc9c
ID
12998 /*
12999 * Sanitize sync polarity flags based on requested ones. If neither
13000 * positive or negative polarity is requested, treat this as meaning
13001 * negative polarity.
13002 */
2d112de7 13003 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 13004 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
2d112de7 13005 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
2960bc9c 13006
2d112de7 13007 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 13008 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
2d112de7 13009 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
2960bc9c 13010
d328c9d7
DV
13011 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
13012 pipe_config);
13013 if (base_bpp < 0)
4e53c2e0
DV
13014 goto fail;
13015
e41a56be
VS
13016 /*
13017 * Determine the real pipe dimensions. Note that stereo modes can
13018 * increase the actual pipe size due to the frame doubling and
13019 * insertion of additional space for blanks between the frame. This
13020 * is stored in the crtc timings. We use the requested mode to do this
13021 * computation to clearly distinguish it from the adjusted mode, which
13022 * can be changed by the connectors in the below retry loop.
13023 */
196cd5d3 13024 drm_mode_get_hv_timing(&pipe_config->base.mode,
ecb7e16b
GP
13025 &pipe_config->pipe_src_w,
13026 &pipe_config->pipe_src_h);
e41a56be 13027
253c84c8
VS
13028 for_each_connector_in_state(state, connector, connector_state, i) {
13029 if (connector_state->crtc != crtc)
13030 continue;
13031
13032 encoder = to_intel_encoder(connector_state->best_encoder);
13033
e25148d0
VS
13034 if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
13035 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
13036 goto fail;
13037 }
13038
253c84c8
VS
13039 /*
13040 * Determine output_types before calling the .compute_config()
13041 * hooks so that the hooks can use this information safely.
13042 */
13043 pipe_config->output_types |= 1 << encoder->type;
13044 }
13045
e29c22c0 13046encoder_retry:
ef1b460d 13047 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 13048 pipe_config->port_clock = 0;
ef1b460d 13049 pipe_config->pixel_multiplier = 1;
ff9a6750 13050
135c81b8 13051 /* Fill in default crtc timings, allow encoders to overwrite them. */
2d112de7
ACO
13052 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
13053 CRTC_STEREO_DOUBLE);
135c81b8 13054
7758a113
DV
13055 /* Pass our mode to the connectors and the CRTC to give them a chance to
13056 * adjust it according to limitations or connector properties, and also
13057 * a chance to reject the mode entirely.
47f1c6c9 13058 */
da3ced29 13059 for_each_connector_in_state(state, connector, connector_state, i) {
0b901879 13060 if (connector_state->crtc != crtc)
7758a113 13061 continue;
7ae89233 13062
0b901879
ACO
13063 encoder = to_intel_encoder(connector_state->best_encoder);
13064
0a478c27 13065 if (!(encoder->compute_config(encoder, pipe_config, connector_state))) {
efea6e8e 13066 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
DV
13067 goto fail;
13068 }
ee7b9f93 13069 }
47f1c6c9 13070
ff9a6750
DV
13071 /* Set default port clock if not overwritten by the encoder. Needs to be
13072 * done afterwards in case the encoder adjusts the mode. */
13073 if (!pipe_config->port_clock)
2d112de7 13074 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
241bfc38 13075 * pipe_config->pixel_multiplier;
ff9a6750 13076
a43f6e0f 13077 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 13078 if (ret < 0) {
7758a113
DV
13079 DRM_DEBUG_KMS("CRTC fixup failed\n");
13080 goto fail;
ee7b9f93 13081 }
e29c22c0
DV
13082
13083 if (ret == RETRY) {
13084 if (WARN(!retry, "loop in pipe configuration computation\n")) {
13085 ret = -EINVAL;
13086 goto fail;
13087 }
13088
13089 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
13090 retry = false;
13091 goto encoder_retry;
13092 }
13093
e8fa4270 13094 /* Dithering seems to not pass-through bits correctly when it should, so
611032bf
MN
13095 * only enable it on 6bpc panels and when its not a compliance
13096 * test requesting 6bpc video pattern.
13097 */
13098 pipe_config->dither = (pipe_config->pipe_bpp == 6*3) &&
13099 !pipe_config->dither_force_disable;
62f0ace5 13100 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
d328c9d7 13101 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
4e53c2e0 13102
7758a113 13103fail:
548ee15b 13104 return ret;
ee7b9f93 13105}
47f1c6c9 13106
ea9d758d 13107static void
4740b0f2 13108intel_modeset_update_crtc_state(struct drm_atomic_state *state)
ea9d758d 13109{
0a9ab303
ACO
13110 struct drm_crtc *crtc;
13111 struct drm_crtc_state *crtc_state;
8a75d157 13112 int i;
ea9d758d 13113
7668851f 13114 /* Double check state. */
8a75d157 13115 for_each_crtc_in_state(state, crtc, crtc_state, i) {
3cb480bc 13116 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
fc467a22
ML
13117
13118 /* Update hwmode for vblank functions */
13119 if (crtc->state->active)
13120 crtc->hwmode = crtc->state->adjusted_mode;
13121 else
13122 crtc->hwmode.crtc_clock = 0;
61067a5e
ML
13123
13124 /*
13125 * Update legacy state to satisfy fbc code. This can
13126 * be removed when fbc uses the atomic state.
13127 */
13128 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
13129 struct drm_plane_state *plane_state = crtc->primary->state;
13130
13131 crtc->primary->fb = plane_state->fb;
13132 crtc->x = plane_state->src_x >> 16;
13133 crtc->y = plane_state->src_y >> 16;
13134 }
ea9d758d 13135 }
ea9d758d
DV
13136}
13137
3bd26263 13138static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 13139{
3bd26263 13140 int diff;
f1f644dc
JB
13141
13142 if (clock1 == clock2)
13143 return true;
13144
13145 if (!clock1 || !clock2)
13146 return false;
13147
13148 diff = abs(clock1 - clock2);
13149
13150 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
13151 return true;
13152
13153 return false;
13154}
13155
cfb23ed6
ML
13156static bool
13157intel_compare_m_n(unsigned int m, unsigned int n,
13158 unsigned int m2, unsigned int n2,
13159 bool exact)
13160{
13161 if (m == m2 && n == n2)
13162 return true;
13163
13164 if (exact || !m || !n || !m2 || !n2)
13165 return false;
13166
13167 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
13168
31d10b57
ML
13169 if (n > n2) {
13170 while (n > n2) {
cfb23ed6
ML
13171 m2 <<= 1;
13172 n2 <<= 1;
13173 }
31d10b57
ML
13174 } else if (n < n2) {
13175 while (n < n2) {
cfb23ed6
ML
13176 m <<= 1;
13177 n <<= 1;
13178 }
13179 }
13180
31d10b57
ML
13181 if (n != n2)
13182 return false;
13183
13184 return intel_fuzzy_clock_check(m, m2);
cfb23ed6
ML
13185}
13186
13187static bool
13188intel_compare_link_m_n(const struct intel_link_m_n *m_n,
13189 struct intel_link_m_n *m2_n2,
13190 bool adjust)
13191{
13192 if (m_n->tu == m2_n2->tu &&
13193 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
13194 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
13195 intel_compare_m_n(m_n->link_m, m_n->link_n,
13196 m2_n2->link_m, m2_n2->link_n, !adjust)) {
13197 if (adjust)
13198 *m2_n2 = *m_n;
13199
13200 return true;
13201 }
13202
13203 return false;
13204}
13205
4e8048f8
TU
13206static void __printf(3, 4)
13207pipe_config_err(bool adjust, const char *name, const char *format, ...)
13208{
13209 char *level;
13210 unsigned int category;
13211 struct va_format vaf;
13212 va_list args;
13213
13214 if (adjust) {
13215 level = KERN_DEBUG;
13216 category = DRM_UT_KMS;
13217 } else {
13218 level = KERN_ERR;
13219 category = DRM_UT_NONE;
13220 }
13221
13222 va_start(args, format);
13223 vaf.fmt = format;
13224 vaf.va = &args;
13225
13226 drm_printk(level, category, "mismatch in %s %pV", name, &vaf);
13227
13228 va_end(args);
13229}
13230
0e8ffe1b 13231static bool
6315b5d3 13232intel_pipe_config_compare(struct drm_i915_private *dev_priv,
5cec258b 13233 struct intel_crtc_state *current_config,
cfb23ed6
ML
13234 struct intel_crtc_state *pipe_config,
13235 bool adjust)
0e8ffe1b 13236{
cfb23ed6
ML
13237 bool ret = true;
13238
66e985c0
DV
13239#define PIPE_CONF_CHECK_X(name) \
13240 if (current_config->name != pipe_config->name) { \
4e8048f8 13241 pipe_config_err(adjust, __stringify(name), \
66e985c0
DV
13242 "(expected 0x%08x, found 0x%08x)\n", \
13243 current_config->name, \
13244 pipe_config->name); \
cfb23ed6 13245 ret = false; \
66e985c0
DV
13246 }
13247
08a24034
DV
13248#define PIPE_CONF_CHECK_I(name) \
13249 if (current_config->name != pipe_config->name) { \
4e8048f8 13250 pipe_config_err(adjust, __stringify(name), \
08a24034
DV
13251 "(expected %i, found %i)\n", \
13252 current_config->name, \
13253 pipe_config->name); \
cfb23ed6
ML
13254 ret = false; \
13255 }
13256
8106ddbd
ACO
13257#define PIPE_CONF_CHECK_P(name) \
13258 if (current_config->name != pipe_config->name) { \
4e8048f8 13259 pipe_config_err(adjust, __stringify(name), \
8106ddbd
ACO
13260 "(expected %p, found %p)\n", \
13261 current_config->name, \
13262 pipe_config->name); \
13263 ret = false; \
13264 }
13265
cfb23ed6
ML
13266#define PIPE_CONF_CHECK_M_N(name) \
13267 if (!intel_compare_link_m_n(&current_config->name, \
13268 &pipe_config->name,\
13269 adjust)) { \
4e8048f8 13270 pipe_config_err(adjust, __stringify(name), \
cfb23ed6
ML
13271 "(expected tu %i gmch %i/%i link %i/%i, " \
13272 "found tu %i, gmch %i/%i link %i/%i)\n", \
13273 current_config->name.tu, \
13274 current_config->name.gmch_m, \
13275 current_config->name.gmch_n, \
13276 current_config->name.link_m, \
13277 current_config->name.link_n, \
13278 pipe_config->name.tu, \
13279 pipe_config->name.gmch_m, \
13280 pipe_config->name.gmch_n, \
13281 pipe_config->name.link_m, \
13282 pipe_config->name.link_n); \
13283 ret = false; \
13284 }
13285
55c561a7
DV
13286/* This is required for BDW+ where there is only one set of registers for
13287 * switching between high and low RR.
13288 * This macro can be used whenever a comparison has to be made between one
13289 * hw state and multiple sw state variables.
13290 */
cfb23ed6
ML
13291#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
13292 if (!intel_compare_link_m_n(&current_config->name, \
13293 &pipe_config->name, adjust) && \
13294 !intel_compare_link_m_n(&current_config->alt_name, \
13295 &pipe_config->name, adjust)) { \
4e8048f8 13296 pipe_config_err(adjust, __stringify(name), \
cfb23ed6
ML
13297 "(expected tu %i gmch %i/%i link %i/%i, " \
13298 "or tu %i gmch %i/%i link %i/%i, " \
13299 "found tu %i, gmch %i/%i link %i/%i)\n", \
13300 current_config->name.tu, \
13301 current_config->name.gmch_m, \
13302 current_config->name.gmch_n, \
13303 current_config->name.link_m, \
13304 current_config->name.link_n, \
13305 current_config->alt_name.tu, \
13306 current_config->alt_name.gmch_m, \
13307 current_config->alt_name.gmch_n, \
13308 current_config->alt_name.link_m, \
13309 current_config->alt_name.link_n, \
13310 pipe_config->name.tu, \
13311 pipe_config->name.gmch_m, \
13312 pipe_config->name.gmch_n, \
13313 pipe_config->name.link_m, \
13314 pipe_config->name.link_n); \
13315 ret = false; \
88adfff1
DV
13316 }
13317
1bd1bd80
DV
13318#define PIPE_CONF_CHECK_FLAGS(name, mask) \
13319 if ((current_config->name ^ pipe_config->name) & (mask)) { \
4e8048f8
TU
13320 pipe_config_err(adjust, __stringify(name), \
13321 "(%x) (expected %i, found %i)\n", \
13322 (mask), \
1bd1bd80
DV
13323 current_config->name & (mask), \
13324 pipe_config->name & (mask)); \
cfb23ed6 13325 ret = false; \
1bd1bd80
DV
13326 }
13327
5e550656
VS
13328#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
13329 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
4e8048f8 13330 pipe_config_err(adjust, __stringify(name), \
5e550656
VS
13331 "(expected %i, found %i)\n", \
13332 current_config->name, \
13333 pipe_config->name); \
cfb23ed6 13334 ret = false; \
5e550656
VS
13335 }
13336
bb760063
DV
13337#define PIPE_CONF_QUIRK(quirk) \
13338 ((current_config->quirks | pipe_config->quirks) & (quirk))
13339
eccb140b
DV
13340 PIPE_CONF_CHECK_I(cpu_transcoder);
13341
08a24034
DV
13342 PIPE_CONF_CHECK_I(has_pch_encoder);
13343 PIPE_CONF_CHECK_I(fdi_lanes);
cfb23ed6 13344 PIPE_CONF_CHECK_M_N(fdi_m_n);
08a24034 13345
90a6b7b0 13346 PIPE_CONF_CHECK_I(lane_count);
95a7a2ae 13347 PIPE_CONF_CHECK_X(lane_lat_optim_mask);
b95af8be 13348
6315b5d3 13349 if (INTEL_GEN(dev_priv) < 8) {
cfb23ed6
ML
13350 PIPE_CONF_CHECK_M_N(dp_m_n);
13351
cfb23ed6
ML
13352 if (current_config->has_drrs)
13353 PIPE_CONF_CHECK_M_N(dp_m2_n2);
13354 } else
13355 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
eb14cb74 13356
253c84c8 13357 PIPE_CONF_CHECK_X(output_types);
a65347ba 13358
2d112de7
ACO
13359 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
13360 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
13361 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
13362 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
13363 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
13364 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
1bd1bd80 13365
2d112de7
ACO
13366 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
13367 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
13368 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
13369 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
13370 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
13371 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
1bd1bd80 13372
c93f54cf 13373 PIPE_CONF_CHECK_I(pixel_multiplier);
6897b4b5 13374 PIPE_CONF_CHECK_I(has_hdmi_sink);
772c2a51 13375 if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
920a14b2 13376 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
b5a9fa09 13377 PIPE_CONF_CHECK_I(limited_color_range);
e43823ec 13378 PIPE_CONF_CHECK_I(has_infoframe);
6c49f241 13379
9ed109a7
DV
13380 PIPE_CONF_CHECK_I(has_audio);
13381
2d112de7 13382 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
1bd1bd80
DV
13383 DRM_MODE_FLAG_INTERLACE);
13384
bb760063 13385 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
2d112de7 13386 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 13387 DRM_MODE_FLAG_PHSYNC);
2d112de7 13388 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 13389 DRM_MODE_FLAG_NHSYNC);
2d112de7 13390 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 13391 DRM_MODE_FLAG_PVSYNC);
2d112de7 13392 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063
DV
13393 DRM_MODE_FLAG_NVSYNC);
13394 }
045ac3b5 13395
333b8ca8 13396 PIPE_CONF_CHECK_X(gmch_pfit.control);
e2ff2d4a 13397 /* pfit ratios are autocomputed by the hw on gen4+ */
6315b5d3 13398 if (INTEL_GEN(dev_priv) < 4)
7f7d8dd6 13399 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
333b8ca8 13400 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
9953599b 13401
bfd16b2a
ML
13402 if (!adjust) {
13403 PIPE_CONF_CHECK_I(pipe_src_w);
13404 PIPE_CONF_CHECK_I(pipe_src_h);
13405
13406 PIPE_CONF_CHECK_I(pch_pfit.enabled);
13407 if (current_config->pch_pfit.enabled) {
13408 PIPE_CONF_CHECK_X(pch_pfit.pos);
13409 PIPE_CONF_CHECK_X(pch_pfit.size);
13410 }
2fa2fe9a 13411
7aefe2b5
ML
13412 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
13413 }
a1b2278e 13414
e59150dc 13415 /* BDW+ don't expose a synchronous way to read the state */
772c2a51 13416 if (IS_HASWELL(dev_priv))
e59150dc 13417 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 13418
282740f7
VS
13419 PIPE_CONF_CHECK_I(double_wide);
13420
8106ddbd 13421 PIPE_CONF_CHECK_P(shared_dpll);
66e985c0 13422 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 13423 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
DV
13424 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
13425 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
d452c5b6 13426 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
00490c22 13427 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
3f4cd19f
DL
13428 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
13429 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
13430 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
c0d43d62 13431
47eacbab
VS
13432 PIPE_CONF_CHECK_X(dsi_pll.ctrl);
13433 PIPE_CONF_CHECK_X(dsi_pll.div);
13434
9beb5fea 13435 if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
42571aef
VS
13436 PIPE_CONF_CHECK_I(pipe_bpp);
13437
2d112de7 13438 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
a9a7e98a 13439 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 13440
66e985c0 13441#undef PIPE_CONF_CHECK_X
08a24034 13442#undef PIPE_CONF_CHECK_I
8106ddbd 13443#undef PIPE_CONF_CHECK_P
1bd1bd80 13444#undef PIPE_CONF_CHECK_FLAGS
5e550656 13445#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 13446#undef PIPE_CONF_QUIRK
88adfff1 13447
cfb23ed6 13448 return ret;
0e8ffe1b
DV
13449}
13450
e3b247da
VS
13451static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
13452 const struct intel_crtc_state *pipe_config)
13453{
13454 if (pipe_config->has_pch_encoder) {
21a727b3 13455 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
e3b247da
VS
13456 &pipe_config->fdi_m_n);
13457 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
13458
13459 /*
13460 * FDI already provided one idea for the dotclock.
13461 * Yell if the encoder disagrees.
13462 */
13463 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
13464 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
13465 fdi_dotclock, dotclock);
13466 }
13467}
13468
c0ead703
ML
13469static void verify_wm_state(struct drm_crtc *crtc,
13470 struct drm_crtc_state *new_state)
08db6652 13471{
6315b5d3 13472 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
08db6652 13473 struct skl_ddb_allocation hw_ddb, *sw_ddb;
3de8a14c 13474 struct skl_pipe_wm hw_wm, *sw_wm;
13475 struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
13476 struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
e7c84544
ML
13477 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13478 const enum pipe pipe = intel_crtc->pipe;
3de8a14c 13479 int plane, level, max_level = ilk_wm_max_level(dev_priv);
08db6652 13480
6315b5d3 13481 if (INTEL_GEN(dev_priv) < 9 || !new_state->active)
08db6652
DL
13482 return;
13483
3de8a14c 13484 skl_pipe_wm_get_hw_state(crtc, &hw_wm);
03af79e0 13485 sw_wm = &to_intel_crtc_state(new_state)->wm.skl.optimal;
3de8a14c 13486
08db6652
DL
13487 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
13488 sw_ddb = &dev_priv->wm.skl_hw.ddb;
13489
e7c84544 13490 /* planes */
8b364b41 13491 for_each_universal_plane(dev_priv, pipe, plane) {
3de8a14c 13492 hw_plane_wm = &hw_wm.planes[plane];
13493 sw_plane_wm = &sw_wm->planes[plane];
08db6652 13494
3de8a14c 13495 /* Watermarks */
13496 for (level = 0; level <= max_level; level++) {
13497 if (skl_wm_level_equals(&hw_plane_wm->wm[level],
13498 &sw_plane_wm->wm[level]))
13499 continue;
13500
13501 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",
13502 pipe_name(pipe), plane + 1, level,
13503 sw_plane_wm->wm[level].plane_en,
13504 sw_plane_wm->wm[level].plane_res_b,
13505 sw_plane_wm->wm[level].plane_res_l,
13506 hw_plane_wm->wm[level].plane_en,
13507 hw_plane_wm->wm[level].plane_res_b,
13508 hw_plane_wm->wm[level].plane_res_l);
13509 }
08db6652 13510
3de8a14c 13511 if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
13512 &sw_plane_wm->trans_wm)) {
13513 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",
13514 pipe_name(pipe), plane + 1,
13515 sw_plane_wm->trans_wm.plane_en,
13516 sw_plane_wm->trans_wm.plane_res_b,
13517 sw_plane_wm->trans_wm.plane_res_l,
13518 hw_plane_wm->trans_wm.plane_en,
13519 hw_plane_wm->trans_wm.plane_res_b,
13520 hw_plane_wm->trans_wm.plane_res_l);
13521 }
13522
13523 /* DDB */
13524 hw_ddb_entry = &hw_ddb.plane[pipe][plane];
13525 sw_ddb_entry = &sw_ddb->plane[pipe][plane];
13526
13527 if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
faccd994 13528 DRM_ERROR("mismatch in DDB state pipe %c plane %d (expected (%u,%u), found (%u,%u))\n",
3de8a14c 13529 pipe_name(pipe), plane + 1,
13530 sw_ddb_entry->start, sw_ddb_entry->end,
13531 hw_ddb_entry->start, hw_ddb_entry->end);
13532 }
e7c84544 13533 }
08db6652 13534
27082493
L
13535 /*
13536 * cursor
13537 * If the cursor plane isn't active, we may not have updated it's ddb
13538 * allocation. In that case since the ddb allocation will be updated
13539 * once the plane becomes visible, we can skip this check
13540 */
13541 if (intel_crtc->cursor_addr) {
3de8a14c 13542 hw_plane_wm = &hw_wm.planes[PLANE_CURSOR];
13543 sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
13544
13545 /* Watermarks */
13546 for (level = 0; level <= max_level; level++) {
13547 if (skl_wm_level_equals(&hw_plane_wm->wm[level],
13548 &sw_plane_wm->wm[level]))
13549 continue;
13550
13551 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",
13552 pipe_name(pipe), level,
13553 sw_plane_wm->wm[level].plane_en,
13554 sw_plane_wm->wm[level].plane_res_b,
13555 sw_plane_wm->wm[level].plane_res_l,
13556 hw_plane_wm->wm[level].plane_en,
13557 hw_plane_wm->wm[level].plane_res_b,
13558 hw_plane_wm->wm[level].plane_res_l);
13559 }
13560
13561 if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
13562 &sw_plane_wm->trans_wm)) {
13563 DRM_ERROR("mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
13564 pipe_name(pipe),
13565 sw_plane_wm->trans_wm.plane_en,
13566 sw_plane_wm->trans_wm.plane_res_b,
13567 sw_plane_wm->trans_wm.plane_res_l,
13568 hw_plane_wm->trans_wm.plane_en,
13569 hw_plane_wm->trans_wm.plane_res_b,
13570 hw_plane_wm->trans_wm.plane_res_l);
13571 }
13572
13573 /* DDB */
13574 hw_ddb_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
13575 sw_ddb_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
27082493 13576
3de8a14c 13577 if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
faccd994 13578 DRM_ERROR("mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
27082493 13579 pipe_name(pipe),
3de8a14c 13580 sw_ddb_entry->start, sw_ddb_entry->end,
13581 hw_ddb_entry->start, hw_ddb_entry->end);
27082493 13582 }
08db6652
DL
13583 }
13584}
13585
91d1b4bd 13586static void
677100ce
ML
13587verify_connector_state(struct drm_device *dev,
13588 struct drm_atomic_state *state,
13589 struct drm_crtc *crtc)
8af6cf88 13590{
35dd3c64 13591 struct drm_connector *connector;
677100ce
ML
13592 struct drm_connector_state *old_conn_state;
13593 int i;
8af6cf88 13594
677100ce 13595 for_each_connector_in_state(state, connector, old_conn_state, i) {
35dd3c64
ML
13596 struct drm_encoder *encoder = connector->encoder;
13597 struct drm_connector_state *state = connector->state;
ad3c558f 13598
e7c84544
ML
13599 if (state->crtc != crtc)
13600 continue;
13601
5a21b665 13602 intel_connector_verify_state(to_intel_connector(connector));
8af6cf88 13603
ad3c558f 13604 I915_STATE_WARN(state->best_encoder != encoder,
35dd3c64 13605 "connector's atomic encoder doesn't match legacy encoder\n");
8af6cf88 13606 }
91d1b4bd
DV
13607}
13608
13609static void
c0ead703 13610verify_encoder_state(struct drm_device *dev)
91d1b4bd
DV
13611{
13612 struct intel_encoder *encoder;
13613 struct intel_connector *connector;
8af6cf88 13614
b2784e15 13615 for_each_intel_encoder(dev, encoder) {
8af6cf88 13616 bool enabled = false;
4d20cd86 13617 enum pipe pipe;
8af6cf88
DV
13618
13619 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
13620 encoder->base.base.id,
8e329a03 13621 encoder->base.name);
8af6cf88 13622
3a3371ff 13623 for_each_intel_connector(dev, connector) {
4d20cd86 13624 if (connector->base.state->best_encoder != &encoder->base)
8af6cf88
DV
13625 continue;
13626 enabled = true;
ad3c558f
ML
13627
13628 I915_STATE_WARN(connector->base.state->crtc !=
13629 encoder->base.crtc,
13630 "connector's crtc doesn't match encoder crtc\n");
8af6cf88 13631 }
0e32b39c 13632
e2c719b7 13633 I915_STATE_WARN(!!encoder->base.crtc != enabled,
8af6cf88
DV
13634 "encoder's enabled state mismatch "
13635 "(expected %i, found %i)\n",
13636 !!encoder->base.crtc, enabled);
7c60d198
ML
13637
13638 if (!encoder->base.crtc) {
4d20cd86 13639 bool active;
7c60d198 13640
4d20cd86
ML
13641 active = encoder->get_hw_state(encoder, &pipe);
13642 I915_STATE_WARN(active,
13643 "encoder detached but still enabled on pipe %c.\n",
13644 pipe_name(pipe));
7c60d198 13645 }
8af6cf88 13646 }
91d1b4bd
DV
13647}
13648
13649static void
c0ead703
ML
13650verify_crtc_state(struct drm_crtc *crtc,
13651 struct drm_crtc_state *old_crtc_state,
13652 struct drm_crtc_state *new_crtc_state)
91d1b4bd 13653{
e7c84544 13654 struct drm_device *dev = crtc->dev;
fac5e23e 13655 struct drm_i915_private *dev_priv = to_i915(dev);
91d1b4bd 13656 struct intel_encoder *encoder;
e7c84544
ML
13657 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13658 struct intel_crtc_state *pipe_config, *sw_config;
13659 struct drm_atomic_state *old_state;
13660 bool active;
045ac3b5 13661
e7c84544 13662 old_state = old_crtc_state->state;
ec2dc6a0 13663 __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
e7c84544
ML
13664 pipe_config = to_intel_crtc_state(old_crtc_state);
13665 memset(pipe_config, 0, sizeof(*pipe_config));
13666 pipe_config->base.crtc = crtc;
13667 pipe_config->base.state = old_state;
8af6cf88 13668
78108b7c 13669 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
8af6cf88 13670
e7c84544 13671 active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
d62cf62a 13672
e7c84544
ML
13673 /* hw state is inconsistent with the pipe quirk */
13674 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
13675 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
13676 active = new_crtc_state->active;
6c49f241 13677
e7c84544
ML
13678 I915_STATE_WARN(new_crtc_state->active != active,
13679 "crtc active state doesn't match with hw state "
13680 "(expected %i, found %i)\n", new_crtc_state->active, active);
0e8ffe1b 13681
e7c84544
ML
13682 I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
13683 "transitional active state does not match atomic hw state "
13684 "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
4d20cd86 13685
e7c84544
ML
13686 for_each_encoder_on_crtc(dev, crtc, encoder) {
13687 enum pipe pipe;
4d20cd86 13688
e7c84544
ML
13689 active = encoder->get_hw_state(encoder, &pipe);
13690 I915_STATE_WARN(active != new_crtc_state->active,
13691 "[ENCODER:%i] active %i with crtc active %i\n",
13692 encoder->base.base.id, active, new_crtc_state->active);
4d20cd86 13693
e7c84544
ML
13694 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
13695 "Encoder connected to wrong pipe %c\n",
13696 pipe_name(pipe));
4d20cd86 13697
253c84c8
VS
13698 if (active) {
13699 pipe_config->output_types |= 1 << encoder->type;
e7c84544 13700 encoder->get_config(encoder, pipe_config);
253c84c8 13701 }
e7c84544 13702 }
53d9f4e9 13703
e7c84544
ML
13704 if (!new_crtc_state->active)
13705 return;
cfb23ed6 13706
e7c84544 13707 intel_pipe_config_sanity_check(dev_priv, pipe_config);
e3b247da 13708
e7c84544 13709 sw_config = to_intel_crtc_state(crtc->state);
6315b5d3 13710 if (!intel_pipe_config_compare(dev_priv, sw_config,
e7c84544
ML
13711 pipe_config, false)) {
13712 I915_STATE_WARN(1, "pipe state doesn't match!\n");
13713 intel_dump_pipe_config(intel_crtc, pipe_config,
13714 "[hw state]");
13715 intel_dump_pipe_config(intel_crtc, sw_config,
13716 "[sw state]");
8af6cf88
DV
13717 }
13718}
13719
91d1b4bd 13720static void
c0ead703
ML
13721verify_single_dpll_state(struct drm_i915_private *dev_priv,
13722 struct intel_shared_dpll *pll,
13723 struct drm_crtc *crtc,
13724 struct drm_crtc_state *new_state)
91d1b4bd 13725{
91d1b4bd 13726 struct intel_dpll_hw_state dpll_hw_state;
e7c84544
ML
13727 unsigned crtc_mask;
13728 bool active;
5358901f 13729
e7c84544 13730 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
5358901f 13731
e7c84544 13732 DRM_DEBUG_KMS("%s\n", pll->name);
5358901f 13733
e7c84544 13734 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
5358901f 13735
e7c84544
ML
13736 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
13737 I915_STATE_WARN(!pll->on && pll->active_mask,
13738 "pll in active use but not on in sw tracking\n");
13739 I915_STATE_WARN(pll->on && !pll->active_mask,
13740 "pll is on but not used by any active crtc\n");
13741 I915_STATE_WARN(pll->on != active,
13742 "pll on state mismatch (expected %i, found %i)\n",
13743 pll->on, active);
13744 }
5358901f 13745
e7c84544 13746 if (!crtc) {
2c42e535 13747 I915_STATE_WARN(pll->active_mask & ~pll->state.crtc_mask,
e7c84544 13748 "more active pll users than references: %x vs %x\n",
2c42e535 13749 pll->active_mask, pll->state.crtc_mask);
5358901f 13750
e7c84544
ML
13751 return;
13752 }
13753
13754 crtc_mask = 1 << drm_crtc_index(crtc);
13755
13756 if (new_state->active)
13757 I915_STATE_WARN(!(pll->active_mask & crtc_mask),
13758 "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
13759 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
13760 else
13761 I915_STATE_WARN(pll->active_mask & crtc_mask,
13762 "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
13763 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
2dd66ebd 13764
2c42e535 13765 I915_STATE_WARN(!(pll->state.crtc_mask & crtc_mask),
e7c84544 13766 "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
2c42e535 13767 crtc_mask, pll->state.crtc_mask);
66e985c0 13768
2c42e535 13769 I915_STATE_WARN(pll->on && memcmp(&pll->state.hw_state,
e7c84544
ML
13770 &dpll_hw_state,
13771 sizeof(dpll_hw_state)),
13772 "pll hw state mismatch\n");
13773}
13774
13775static void
c0ead703
ML
13776verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
13777 struct drm_crtc_state *old_crtc_state,
13778 struct drm_crtc_state *new_crtc_state)
e7c84544 13779{
fac5e23e 13780 struct drm_i915_private *dev_priv = to_i915(dev);
e7c84544
ML
13781 struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
13782 struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
13783
13784 if (new_state->shared_dpll)
c0ead703 13785 verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
e7c84544
ML
13786
13787 if (old_state->shared_dpll &&
13788 old_state->shared_dpll != new_state->shared_dpll) {
13789 unsigned crtc_mask = 1 << drm_crtc_index(crtc);
13790 struct intel_shared_dpll *pll = old_state->shared_dpll;
13791
13792 I915_STATE_WARN(pll->active_mask & crtc_mask,
13793 "pll active mismatch (didn't expect pipe %c in active mask)\n",
13794 pipe_name(drm_crtc_index(crtc)));
2c42e535 13795 I915_STATE_WARN(pll->state.crtc_mask & crtc_mask,
e7c84544
ML
13796 "pll enabled crtcs mismatch (found %x in enabled mask)\n",
13797 pipe_name(drm_crtc_index(crtc)));
5358901f 13798 }
8af6cf88
DV
13799}
13800
e7c84544 13801static void
c0ead703 13802intel_modeset_verify_crtc(struct drm_crtc *crtc,
677100ce
ML
13803 struct drm_atomic_state *state,
13804 struct drm_crtc_state *old_state,
13805 struct drm_crtc_state *new_state)
e7c84544 13806{
5a21b665
DV
13807 if (!needs_modeset(new_state) &&
13808 !to_intel_crtc_state(new_state)->update_pipe)
13809 return;
13810
c0ead703 13811 verify_wm_state(crtc, new_state);
677100ce 13812 verify_connector_state(crtc->dev, state, crtc);
c0ead703
ML
13813 verify_crtc_state(crtc, old_state, new_state);
13814 verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
e7c84544
ML
13815}
13816
13817static void
c0ead703 13818verify_disabled_dpll_state(struct drm_device *dev)
e7c84544 13819{
fac5e23e 13820 struct drm_i915_private *dev_priv = to_i915(dev);
e7c84544
ML
13821 int i;
13822
13823 for (i = 0; i < dev_priv->num_shared_dpll; i++)
c0ead703 13824 verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
e7c84544
ML
13825}
13826
13827static void
677100ce
ML
13828intel_modeset_verify_disabled(struct drm_device *dev,
13829 struct drm_atomic_state *state)
e7c84544 13830{
c0ead703 13831 verify_encoder_state(dev);
677100ce 13832 verify_connector_state(dev, state, NULL);
c0ead703 13833 verify_disabled_dpll_state(dev);
e7c84544
ML
13834}
13835
80715b2f
VS
13836static void update_scanline_offset(struct intel_crtc *crtc)
13837{
4f8036a2 13838 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
80715b2f
VS
13839
13840 /*
13841 * The scanline counter increments at the leading edge of hsync.
13842 *
13843 * On most platforms it starts counting from vtotal-1 on the
13844 * first active line. That means the scanline counter value is
13845 * always one less than what we would expect. Ie. just after
13846 * start of vblank, which also occurs at start of hsync (on the
13847 * last active line), the scanline counter will read vblank_start-1.
13848 *
13849 * On gen2 the scanline counter starts counting from 1 instead
13850 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
13851 * to keep the value positive), instead of adding one.
13852 *
13853 * On HSW+ the behaviour of the scanline counter depends on the output
13854 * type. For DP ports it behaves like most other platforms, but on HDMI
13855 * there's an extra 1 line difference. So we need to add two instead of
13856 * one to the value.
13857 */
4f8036a2 13858 if (IS_GEN2(dev_priv)) {
124abe07 13859 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
80715b2f
VS
13860 int vtotal;
13861
124abe07
VS
13862 vtotal = adjusted_mode->crtc_vtotal;
13863 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
80715b2f
VS
13864 vtotal /= 2;
13865
13866 crtc->scanline_offset = vtotal - 1;
4f8036a2 13867 } else if (HAS_DDI(dev_priv) &&
2d84d2b3 13868 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
80715b2f
VS
13869 crtc->scanline_offset = 2;
13870 } else
13871 crtc->scanline_offset = 1;
13872}
13873
ad421372 13874static void intel_modeset_clear_plls(struct drm_atomic_state *state)
ed6739ef 13875{
225da59b 13876 struct drm_device *dev = state->dev;
ed6739ef 13877 struct drm_i915_private *dev_priv = to_i915(dev);
0a9ab303
ACO
13878 struct drm_crtc *crtc;
13879 struct drm_crtc_state *crtc_state;
0a9ab303 13880 int i;
ed6739ef
ACO
13881
13882 if (!dev_priv->display.crtc_compute_clock)
ad421372 13883 return;
ed6739ef 13884
0a9ab303 13885 for_each_crtc_in_state(state, crtc, crtc_state, i) {
fb1a38a9 13886 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8106ddbd
ACO
13887 struct intel_shared_dpll *old_dpll =
13888 to_intel_crtc_state(crtc->state)->shared_dpll;
0a9ab303 13889
fb1a38a9 13890 if (!needs_modeset(crtc_state))
225da59b
ACO
13891 continue;
13892
8106ddbd 13893 to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
fb1a38a9 13894
8106ddbd 13895 if (!old_dpll)
fb1a38a9 13896 continue;
0a9ab303 13897
a1c414ee 13898 intel_release_shared_dpll(old_dpll, intel_crtc, state);
ad421372 13899 }
ed6739ef
ACO
13900}
13901
99d736a2
ML
13902/*
13903 * This implements the workaround described in the "notes" section of the mode
13904 * set sequence documentation. When going from no pipes or single pipe to
13905 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13906 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13907 */
13908static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13909{
13910 struct drm_crtc_state *crtc_state;
13911 struct intel_crtc *intel_crtc;
13912 struct drm_crtc *crtc;
13913 struct intel_crtc_state *first_crtc_state = NULL;
13914 struct intel_crtc_state *other_crtc_state = NULL;
13915 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13916 int i;
13917
13918 /* look at all crtc's that are going to be enabled in during modeset */
13919 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13920 intel_crtc = to_intel_crtc(crtc);
13921
13922 if (!crtc_state->active || !needs_modeset(crtc_state))
13923 continue;
13924
13925 if (first_crtc_state) {
13926 other_crtc_state = to_intel_crtc_state(crtc_state);
13927 break;
13928 } else {
13929 first_crtc_state = to_intel_crtc_state(crtc_state);
13930 first_pipe = intel_crtc->pipe;
13931 }
13932 }
13933
13934 /* No workaround needed? */
13935 if (!first_crtc_state)
13936 return 0;
13937
13938 /* w/a possibly needed, check how many crtc's are already enabled. */
13939 for_each_intel_crtc(state->dev, intel_crtc) {
13940 struct intel_crtc_state *pipe_config;
13941
13942 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13943 if (IS_ERR(pipe_config))
13944 return PTR_ERR(pipe_config);
13945
13946 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13947
13948 if (!pipe_config->base.active ||
13949 needs_modeset(&pipe_config->base))
13950 continue;
13951
13952 /* 2 or more enabled crtcs means no need for w/a */
13953 if (enabled_pipe != INVALID_PIPE)
13954 return 0;
13955
13956 enabled_pipe = intel_crtc->pipe;
13957 }
13958
13959 if (enabled_pipe != INVALID_PIPE)
13960 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13961 else if (other_crtc_state)
13962 other_crtc_state->hsw_workaround_pipe = first_pipe;
13963
13964 return 0;
13965}
13966
8d96561a
VS
13967static int intel_lock_all_pipes(struct drm_atomic_state *state)
13968{
13969 struct drm_crtc *crtc;
13970
13971 /* Add all pipes to the state */
13972 for_each_crtc(state->dev, crtc) {
13973 struct drm_crtc_state *crtc_state;
13974
13975 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13976 if (IS_ERR(crtc_state))
13977 return PTR_ERR(crtc_state);
13978 }
13979
13980 return 0;
13981}
13982
27c329ed
ML
13983static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13984{
13985 struct drm_crtc *crtc;
27c329ed 13986
8d96561a
VS
13987 /*
13988 * Add all pipes to the state, and force
13989 * a modeset on all the active ones.
13990 */
27c329ed 13991 for_each_crtc(state->dev, crtc) {
9780aad5
VS
13992 struct drm_crtc_state *crtc_state;
13993 int ret;
13994
27c329ed
ML
13995 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13996 if (IS_ERR(crtc_state))
13997 return PTR_ERR(crtc_state);
13998
13999 if (!crtc_state->active || needs_modeset(crtc_state))
14000 continue;
14001
14002 crtc_state->mode_changed = true;
14003
14004 ret = drm_atomic_add_affected_connectors(state, crtc);
14005 if (ret)
9780aad5 14006 return ret;
27c329ed
ML
14007
14008 ret = drm_atomic_add_affected_planes(state, crtc);
14009 if (ret)
9780aad5 14010 return ret;
27c329ed
ML
14011 }
14012
9780aad5 14013 return 0;
27c329ed
ML
14014}
14015
c347a676 14016static int intel_modeset_checks(struct drm_atomic_state *state)
054518dd 14017{
565602d7 14018 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 14019 struct drm_i915_private *dev_priv = to_i915(state->dev);
565602d7
ML
14020 struct drm_crtc *crtc;
14021 struct drm_crtc_state *crtc_state;
14022 int ret = 0, i;
054518dd 14023
b359283a
ML
14024 if (!check_digital_port_conflicts(state)) {
14025 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
14026 return -EINVAL;
14027 }
14028
565602d7
ML
14029 intel_state->modeset = true;
14030 intel_state->active_crtcs = dev_priv->active_crtcs;
14031
14032 for_each_crtc_in_state(state, crtc, crtc_state, i) {
14033 if (crtc_state->active)
14034 intel_state->active_crtcs |= 1 << i;
14035 else
14036 intel_state->active_crtcs &= ~(1 << i);
8b4a7d05
MR
14037
14038 if (crtc_state->active != crtc->state->active)
14039 intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
565602d7
ML
14040 }
14041
054518dd
ACO
14042 /*
14043 * See if the config requires any additional preparation, e.g.
14044 * to adjust global state with pipes off. We need to do this
14045 * here so we can get the modeset_pipe updated config for the new
14046 * mode set on this crtc. For other crtcs we need to use the
14047 * adjusted_mode bits in the crtc directly.
14048 */
27c329ed 14049 if (dev_priv->display.modeset_calc_cdclk) {
c89e39f3 14050 if (!intel_state->cdclk_pll_vco)
63911d72 14051 intel_state->cdclk_pll_vco = dev_priv->cdclk_pll.vco;
b2045352
VS
14052 if (!intel_state->cdclk_pll_vco)
14053 intel_state->cdclk_pll_vco = dev_priv->skl_preferred_vco_freq;
c89e39f3 14054
27c329ed 14055 ret = dev_priv->display.modeset_calc_cdclk(state);
c89e39f3
CT
14056 if (ret < 0)
14057 return ret;
27c329ed 14058
8d96561a
VS
14059 /*
14060 * Writes to dev_priv->atomic_cdclk_freq must protected by
14061 * holding all the crtc locks, even if we don't end up
14062 * touching the hardware
14063 */
14064 if (intel_state->cdclk != dev_priv->atomic_cdclk_freq) {
14065 ret = intel_lock_all_pipes(state);
14066 if (ret < 0)
14067 return ret;
14068 }
14069
14070 /* All pipes must be switched off while we change the cdclk. */
c89e39f3 14071 if (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
8d96561a 14072 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco) {
27c329ed 14073 ret = intel_modeset_all_pipes(state);
8d96561a
VS
14074 if (ret < 0)
14075 return ret;
14076 }
e8788cbc
ML
14077
14078 DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
14079 intel_state->cdclk, intel_state->dev_cdclk);
e0ca7a6b 14080 } else {
1a617b77 14081 to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
e0ca7a6b 14082 }
054518dd 14083
ad421372 14084 intel_modeset_clear_plls(state);
054518dd 14085
565602d7 14086 if (IS_HASWELL(dev_priv))
ad421372 14087 return haswell_mode_set_planes_workaround(state);
99d736a2 14088
ad421372 14089 return 0;
c347a676
ACO
14090}
14091
aa363136
MR
14092/*
14093 * Handle calculation of various watermark data at the end of the atomic check
14094 * phase. The code here should be run after the per-crtc and per-plane 'check'
14095 * handlers to ensure that all derived state has been updated.
14096 */
55994c2c 14097static int calc_watermark_data(struct drm_atomic_state *state)
aa363136
MR
14098{
14099 struct drm_device *dev = state->dev;
98d39494 14100 struct drm_i915_private *dev_priv = to_i915(dev);
98d39494
MR
14101
14102 /* Is there platform-specific watermark information to calculate? */
14103 if (dev_priv->display.compute_global_watermarks)
55994c2c
MR
14104 return dev_priv->display.compute_global_watermarks(state);
14105
14106 return 0;
aa363136
MR
14107}
14108
74c090b1
ML
14109/**
14110 * intel_atomic_check - validate state object
14111 * @dev: drm device
14112 * @state: state to validate
14113 */
14114static int intel_atomic_check(struct drm_device *dev,
14115 struct drm_atomic_state *state)
c347a676 14116{
dd8b3bdb 14117 struct drm_i915_private *dev_priv = to_i915(dev);
aa363136 14118 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
c347a676
ACO
14119 struct drm_crtc *crtc;
14120 struct drm_crtc_state *crtc_state;
14121 int ret, i;
61333b60 14122 bool any_ms = false;
c347a676 14123
74c090b1 14124 ret = drm_atomic_helper_check_modeset(dev, state);
054518dd
ACO
14125 if (ret)
14126 return ret;
14127
c347a676 14128 for_each_crtc_in_state(state, crtc, crtc_state, i) {
cfb23ed6
ML
14129 struct intel_crtc_state *pipe_config =
14130 to_intel_crtc_state(crtc_state);
1ed51de9
DV
14131
14132 /* Catch I915_MODE_FLAG_INHERITED */
14133 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
14134 crtc_state->mode_changed = true;
cfb23ed6 14135
af4a879e 14136 if (!needs_modeset(crtc_state))
c347a676
ACO
14137 continue;
14138
af4a879e
DV
14139 if (!crtc_state->enable) {
14140 any_ms = true;
cfb23ed6 14141 continue;
af4a879e 14142 }
cfb23ed6 14143
26495481
DV
14144 /* FIXME: For only active_changed we shouldn't need to do any
14145 * state recomputation at all. */
14146
1ed51de9
DV
14147 ret = drm_atomic_add_affected_connectors(state, crtc);
14148 if (ret)
14149 return ret;
b359283a 14150
cfb23ed6 14151 ret = intel_modeset_pipe_config(crtc, pipe_config);
25aa1c39
ML
14152 if (ret) {
14153 intel_dump_pipe_config(to_intel_crtc(crtc),
14154 pipe_config, "[failed]");
c347a676 14155 return ret;
25aa1c39 14156 }
c347a676 14157
73831236 14158 if (i915.fastboot &&
6315b5d3 14159 intel_pipe_config_compare(dev_priv,
cfb23ed6 14160 to_intel_crtc_state(crtc->state),
1ed51de9 14161 pipe_config, true)) {
26495481 14162 crtc_state->mode_changed = false;
bfd16b2a 14163 to_intel_crtc_state(crtc_state)->update_pipe = true;
26495481
DV
14164 }
14165
af4a879e 14166 if (needs_modeset(crtc_state))
26495481 14167 any_ms = true;
cfb23ed6 14168
af4a879e
DV
14169 ret = drm_atomic_add_affected_planes(state, crtc);
14170 if (ret)
14171 return ret;
61333b60 14172
26495481
DV
14173 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
14174 needs_modeset(crtc_state) ?
14175 "[modeset]" : "[fastset]");
c347a676
ACO
14176 }
14177
61333b60
ML
14178 if (any_ms) {
14179 ret = intel_modeset_checks(state);
14180
14181 if (ret)
14182 return ret;
e0ca7a6b
VS
14183 } else {
14184 intel_state->cdclk = dev_priv->atomic_cdclk_freq;
14185 }
76305b1a 14186
dd8b3bdb 14187 ret = drm_atomic_helper_check_planes(dev, state);
aa363136
MR
14188 if (ret)
14189 return ret;
14190
f51be2e0 14191 intel_fbc_choose_crtc(dev_priv, state);
55994c2c 14192 return calc_watermark_data(state);
054518dd
ACO
14193}
14194
5008e874 14195static int intel_atomic_prepare_commit(struct drm_device *dev,
d07f0e59 14196 struct drm_atomic_state *state)
5008e874 14197{
fac5e23e 14198 struct drm_i915_private *dev_priv = to_i915(dev);
5008e874
ML
14199 struct drm_crtc_state *crtc_state;
14200 struct drm_crtc *crtc;
14201 int i, ret;
14202
5a21b665
DV
14203 for_each_crtc_in_state(state, crtc, crtc_state, i) {
14204 if (state->legacy_cursor_update)
a6747b73
ML
14205 continue;
14206
5a21b665
DV
14207 ret = intel_crtc_wait_for_pending_flips(crtc);
14208 if (ret)
14209 return ret;
5008e874 14210
5a21b665
DV
14211 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
14212 flush_workqueue(dev_priv->wq);
d55dbd06
ML
14213 }
14214
f935675f
ML
14215 ret = mutex_lock_interruptible(&dev->struct_mutex);
14216 if (ret)
14217 return ret;
14218
5008e874 14219 ret = drm_atomic_helper_prepare_planes(dev, state);
f7e5838b 14220 mutex_unlock(&dev->struct_mutex);
7580d774 14221
5008e874
ML
14222 return ret;
14223}
14224
a2991414
ML
14225u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
14226{
14227 struct drm_device *dev = crtc->base.dev;
14228
14229 if (!dev->max_vblank_count)
14230 return drm_accurate_vblank_count(&crtc->base);
14231
14232 return dev->driver->get_vblank_counter(dev, crtc->pipe);
14233}
14234
5a21b665
DV
14235static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
14236 struct drm_i915_private *dev_priv,
14237 unsigned crtc_mask)
e8861675 14238{
5a21b665
DV
14239 unsigned last_vblank_count[I915_MAX_PIPES];
14240 enum pipe pipe;
14241 int ret;
e8861675 14242
5a21b665
DV
14243 if (!crtc_mask)
14244 return;
e8861675 14245
5a21b665 14246 for_each_pipe(dev_priv, pipe) {
98187836
VS
14247 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
14248 pipe);
e8861675 14249
5a21b665 14250 if (!((1 << pipe) & crtc_mask))
e8861675
ML
14251 continue;
14252
e2af48c6 14253 ret = drm_crtc_vblank_get(&crtc->base);
5a21b665
DV
14254 if (WARN_ON(ret != 0)) {
14255 crtc_mask &= ~(1 << pipe);
14256 continue;
e8861675
ML
14257 }
14258
e2af48c6 14259 last_vblank_count[pipe] = drm_crtc_vblank_count(&crtc->base);
e8861675
ML
14260 }
14261
5a21b665 14262 for_each_pipe(dev_priv, pipe) {
98187836
VS
14263 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
14264 pipe);
5a21b665 14265 long lret;
e8861675 14266
5a21b665
DV
14267 if (!((1 << pipe) & crtc_mask))
14268 continue;
d55dbd06 14269
5a21b665
DV
14270 lret = wait_event_timeout(dev->vblank[pipe].queue,
14271 last_vblank_count[pipe] !=
e2af48c6 14272 drm_crtc_vblank_count(&crtc->base),
5a21b665 14273 msecs_to_jiffies(50));
d55dbd06 14274
5a21b665 14275 WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
d55dbd06 14276
e2af48c6 14277 drm_crtc_vblank_put(&crtc->base);
d55dbd06
ML
14278 }
14279}
14280
5a21b665 14281static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
a6747b73 14282{
5a21b665
DV
14283 /* fb updated, need to unpin old fb */
14284 if (crtc_state->fb_changed)
14285 return true;
a6747b73 14286
5a21b665
DV
14287 /* wm changes, need vblank before final wm's */
14288 if (crtc_state->update_wm_post)
14289 return true;
a6747b73 14290
5a21b665
DV
14291 /*
14292 * cxsr is re-enabled after vblank.
14293 * This is already handled by crtc_state->update_wm_post,
14294 * but added for clarity.
14295 */
14296 if (crtc_state->disable_cxsr)
14297 return true;
a6747b73 14298
5a21b665 14299 return false;
e8861675
ML
14300}
14301
896e5bb0
L
14302static void intel_update_crtc(struct drm_crtc *crtc,
14303 struct drm_atomic_state *state,
14304 struct drm_crtc_state *old_crtc_state,
14305 unsigned int *crtc_vblank_mask)
14306{
14307 struct drm_device *dev = crtc->dev;
14308 struct drm_i915_private *dev_priv = to_i915(dev);
14309 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14310 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc->state);
14311 bool modeset = needs_modeset(crtc->state);
14312
14313 if (modeset) {
14314 update_scanline_offset(intel_crtc);
14315 dev_priv->display.crtc_enable(pipe_config, state);
14316 } else {
14317 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
14318 }
14319
14320 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
14321 intel_fbc_enable(
14322 intel_crtc, pipe_config,
14323 to_intel_plane_state(crtc->primary->state));
14324 }
14325
14326 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
14327
14328 if (needs_vblank_wait(pipe_config))
14329 *crtc_vblank_mask |= drm_crtc_mask(crtc);
14330}
14331
14332static void intel_update_crtcs(struct drm_atomic_state *state,
14333 unsigned int *crtc_vblank_mask)
14334{
14335 struct drm_crtc *crtc;
14336 struct drm_crtc_state *old_crtc_state;
14337 int i;
14338
14339 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
14340 if (!crtc->state->active)
14341 continue;
14342
14343 intel_update_crtc(crtc, state, old_crtc_state,
14344 crtc_vblank_mask);
14345 }
14346}
14347
27082493
L
14348static void skl_update_crtcs(struct drm_atomic_state *state,
14349 unsigned int *crtc_vblank_mask)
14350{
0f0f74bc 14351 struct drm_i915_private *dev_priv = to_i915(state->dev);
27082493
L
14352 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
14353 struct drm_crtc *crtc;
ce0ba283 14354 struct intel_crtc *intel_crtc;
27082493 14355 struct drm_crtc_state *old_crtc_state;
ce0ba283 14356 struct intel_crtc_state *cstate;
27082493
L
14357 unsigned int updated = 0;
14358 bool progress;
14359 enum pipe pipe;
5eff503b
ML
14360 int i;
14361
14362 const struct skl_ddb_entry *entries[I915_MAX_PIPES] = {};
14363
14364 for_each_crtc_in_state(state, crtc, old_crtc_state, i)
14365 /* ignore allocations for crtc's that have been turned off. */
14366 if (crtc->state->active)
14367 entries[i] = &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb;
27082493
L
14368
14369 /*
14370 * Whenever the number of active pipes changes, we need to make sure we
14371 * update the pipes in the right order so that their ddb allocations
14372 * never overlap with eachother inbetween CRTC updates. Otherwise we'll
14373 * cause pipe underruns and other bad stuff.
14374 */
14375 do {
27082493
L
14376 progress = false;
14377
14378 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
14379 bool vbl_wait = false;
14380 unsigned int cmask = drm_crtc_mask(crtc);
ce0ba283
L
14381
14382 intel_crtc = to_intel_crtc(crtc);
14383 cstate = to_intel_crtc_state(crtc->state);
14384 pipe = intel_crtc->pipe;
27082493 14385
5eff503b 14386 if (updated & cmask || !cstate->base.active)
27082493 14387 continue;
5eff503b
ML
14388
14389 if (skl_ddb_allocation_overlaps(entries, &cstate->wm.skl.ddb, i))
27082493
L
14390 continue;
14391
14392 updated |= cmask;
5eff503b 14393 entries[i] = &cstate->wm.skl.ddb;
27082493
L
14394
14395 /*
14396 * If this is an already active pipe, it's DDB changed,
14397 * and this isn't the last pipe that needs updating
14398 * then we need to wait for a vblank to pass for the
14399 * new ddb allocation to take effect.
14400 */
ce0ba283 14401 if (!skl_ddb_entry_equal(&cstate->wm.skl.ddb,
512b5527 14402 &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb) &&
27082493
L
14403 !crtc->state->active_changed &&
14404 intel_state->wm_results.dirty_pipes != updated)
14405 vbl_wait = true;
14406
14407 intel_update_crtc(crtc, state, old_crtc_state,
14408 crtc_vblank_mask);
14409
14410 if (vbl_wait)
0f0f74bc 14411 intel_wait_for_vblank(dev_priv, pipe);
27082493
L
14412
14413 progress = true;
14414 }
14415 } while (progress);
14416}
14417
ba318c61
CW
14418static void intel_atomic_helper_free_state(struct drm_i915_private *dev_priv)
14419{
14420 struct intel_atomic_state *state, *next;
14421 struct llist_node *freed;
14422
14423 freed = llist_del_all(&dev_priv->atomic_helper.free_list);
14424 llist_for_each_entry_safe(state, next, freed, freed)
14425 drm_atomic_state_put(&state->base);
14426}
14427
14428static void intel_atomic_helper_free_state_worker(struct work_struct *work)
14429{
14430 struct drm_i915_private *dev_priv =
14431 container_of(work, typeof(*dev_priv), atomic_helper.free_work);
14432
14433 intel_atomic_helper_free_state(dev_priv);
14434}
14435
94f05024 14436static void intel_atomic_commit_tail(struct drm_atomic_state *state)
a6778b3c 14437{
94f05024 14438 struct drm_device *dev = state->dev;
565602d7 14439 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 14440 struct drm_i915_private *dev_priv = to_i915(dev);
29ceb0e6 14441 struct drm_crtc_state *old_crtc_state;
7580d774 14442 struct drm_crtc *crtc;
5a21b665 14443 struct intel_crtc_state *intel_cstate;
5a21b665
DV
14444 bool hw_check = intel_state->modeset;
14445 unsigned long put_domains[I915_MAX_PIPES] = {};
14446 unsigned crtc_vblank_mask = 0;
e95433c7 14447 int i;
a6778b3c 14448
ea0000f0
DV
14449 drm_atomic_helper_wait_for_dependencies(state);
14450
c3b32658 14451 if (intel_state->modeset)
5a21b665 14452 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
565602d7 14453
29ceb0e6 14454 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
a539205a
ML
14455 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14456
5a21b665
DV
14457 if (needs_modeset(crtc->state) ||
14458 to_intel_crtc_state(crtc->state)->update_pipe) {
14459 hw_check = true;
14460
14461 put_domains[to_intel_crtc(crtc)->pipe] =
14462 modeset_get_crtc_power_domains(crtc,
14463 to_intel_crtc_state(crtc->state));
14464 }
14465
61333b60
ML
14466 if (!needs_modeset(crtc->state))
14467 continue;
14468
29ceb0e6 14469 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
460da916 14470
29ceb0e6
VS
14471 if (old_crtc_state->active) {
14472 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
4a806558 14473 dev_priv->display.crtc_disable(to_intel_crtc_state(old_crtc_state), state);
eddfcbcd 14474 intel_crtc->active = false;
58f9c0bc 14475 intel_fbc_disable(intel_crtc);
eddfcbcd 14476 intel_disable_shared_dpll(intel_crtc);
9bbc8258
VS
14477
14478 /*
14479 * Underruns don't always raise
14480 * interrupts, so check manually.
14481 */
14482 intel_check_cpu_fifo_underruns(dev_priv);
14483 intel_check_pch_fifo_underruns(dev_priv);
b9001114 14484
e62929b3
ML
14485 if (!crtc->state->active) {
14486 /*
14487 * Make sure we don't call initial_watermarks
14488 * for ILK-style watermark updates.
14489 */
14490 if (dev_priv->display.atomic_update_watermarks)
14491 dev_priv->display.initial_watermarks(intel_state,
14492 to_intel_crtc_state(crtc->state));
14493 else
14494 intel_update_watermarks(intel_crtc);
14495 }
a539205a 14496 }
b8cecdf5 14497 }
7758a113 14498
ea9d758d
DV
14499 /* Only after disabling all output pipelines that will be changed can we
14500 * update the the output configuration. */
4740b0f2 14501 intel_modeset_update_crtc_state(state);
f6e5b160 14502
565602d7 14503 if (intel_state->modeset) {
4740b0f2 14504 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
33c8df89
ML
14505
14506 if (dev_priv->display.modeset_commit_cdclk &&
c89e39f3 14507 (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
63911d72 14508 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco))
33c8df89 14509 dev_priv->display.modeset_commit_cdclk(state);
f6d1973d 14510
656d1b89
L
14511 /*
14512 * SKL workaround: bspec recommends we disable the SAGV when we
14513 * have more then one pipe enabled
14514 */
56feca91 14515 if (!intel_can_enable_sagv(state))
16dcdc4e 14516 intel_disable_sagv(dev_priv);
656d1b89 14517
677100ce 14518 intel_modeset_verify_disabled(dev, state);
4740b0f2 14519 }
47fab737 14520
896e5bb0 14521 /* Complete the events for pipes that have now been disabled */
29ceb0e6 14522 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
f6ac4b2a 14523 bool modeset = needs_modeset(crtc->state);
80715b2f 14524
1f7528c4
DV
14525 /* Complete events for now disable pipes here. */
14526 if (modeset && !crtc->state->active && crtc->state->event) {
14527 spin_lock_irq(&dev->event_lock);
14528 drm_crtc_send_vblank_event(crtc, crtc->state->event);
14529 spin_unlock_irq(&dev->event_lock);
14530
14531 crtc->state->event = NULL;
14532 }
177246a8
MR
14533 }
14534
896e5bb0
L
14535 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
14536 dev_priv->display.update_crtcs(state, &crtc_vblank_mask);
14537
94f05024
DV
14538 /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
14539 * already, but still need the state for the delayed optimization. To
14540 * fix this:
14541 * - wrap the optimization/post_plane_update stuff into a per-crtc work.
14542 * - schedule that vblank worker _before_ calling hw_done
14543 * - at the start of commit_tail, cancel it _synchrously
14544 * - switch over to the vblank wait helper in the core after that since
14545 * we don't need out special handling any more.
14546 */
5a21b665
DV
14547 if (!state->legacy_cursor_update)
14548 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
14549
14550 /*
14551 * Now that the vblank has passed, we can go ahead and program the
14552 * optimal watermarks on platforms that need two-step watermark
14553 * programming.
14554 *
14555 * TODO: Move this (and other cleanup) to an async worker eventually.
14556 */
14557 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
14558 intel_cstate = to_intel_crtc_state(crtc->state);
14559
14560 if (dev_priv->display.optimize_watermarks)
ccf010fb
ML
14561 dev_priv->display.optimize_watermarks(intel_state,
14562 intel_cstate);
5a21b665
DV
14563 }
14564
14565 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
14566 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
14567
14568 if (put_domains[i])
14569 modeset_put_power_domains(dev_priv, put_domains[i]);
14570
677100ce 14571 intel_modeset_verify_crtc(crtc, state, old_crtc_state, crtc->state);
5a21b665
DV
14572 }
14573
56feca91 14574 if (intel_state->modeset && intel_can_enable_sagv(state))
16dcdc4e 14575 intel_enable_sagv(dev_priv);
656d1b89 14576
94f05024
DV
14577 drm_atomic_helper_commit_hw_done(state);
14578
5a21b665
DV
14579 if (intel_state->modeset)
14580 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
14581
14582 mutex_lock(&dev->struct_mutex);
14583 drm_atomic_helper_cleanup_planes(dev, state);
14584 mutex_unlock(&dev->struct_mutex);
14585
ea0000f0
DV
14586 drm_atomic_helper_commit_cleanup_done(state);
14587
0853695c 14588 drm_atomic_state_put(state);
f30da187 14589
75714940
MK
14590 /* As one of the primary mmio accessors, KMS has a high likelihood
14591 * of triggering bugs in unclaimed access. After we finish
14592 * modesetting, see if an error has been flagged, and if so
14593 * enable debugging for the next modeset - and hope we catch
14594 * the culprit.
14595 *
14596 * XXX note that we assume display power is on at this point.
14597 * This might hold true now but we need to add pm helper to check
14598 * unclaimed only when the hardware is on, as atomic commits
14599 * can happen also when the device is completely off.
14600 */
14601 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
ba318c61
CW
14602
14603 intel_atomic_helper_free_state(dev_priv);
94f05024
DV
14604}
14605
14606static void intel_atomic_commit_work(struct work_struct *work)
14607{
c004a90b
CW
14608 struct drm_atomic_state *state =
14609 container_of(work, struct drm_atomic_state, commit_work);
14610
94f05024
DV
14611 intel_atomic_commit_tail(state);
14612}
14613
c004a90b
CW
14614static int __i915_sw_fence_call
14615intel_atomic_commit_ready(struct i915_sw_fence *fence,
14616 enum i915_sw_fence_notify notify)
14617{
14618 struct intel_atomic_state *state =
14619 container_of(fence, struct intel_atomic_state, commit_ready);
14620
14621 switch (notify) {
14622 case FENCE_COMPLETE:
14623 if (state->base.commit_work.func)
14624 queue_work(system_unbound_wq, &state->base.commit_work);
14625 break;
14626
14627 case FENCE_FREE:
eb955eee
CW
14628 {
14629 struct intel_atomic_helper *helper =
14630 &to_i915(state->base.dev)->atomic_helper;
14631
14632 if (llist_add(&state->freed, &helper->free_list))
14633 schedule_work(&helper->free_work);
14634 break;
14635 }
c004a90b
CW
14636 }
14637
14638 return NOTIFY_DONE;
14639}
14640
6c9c1b38
DV
14641static void intel_atomic_track_fbs(struct drm_atomic_state *state)
14642{
14643 struct drm_plane_state *old_plane_state;
14644 struct drm_plane *plane;
6c9c1b38
DV
14645 int i;
14646
faf5bf0a
CW
14647 for_each_plane_in_state(state, plane, old_plane_state, i)
14648 i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
14649 intel_fb_obj(plane->state->fb),
14650 to_intel_plane(plane)->frontbuffer_bit);
6c9c1b38
DV
14651}
14652
94f05024
DV
14653/**
14654 * intel_atomic_commit - commit validated state object
14655 * @dev: DRM device
14656 * @state: the top-level driver state object
14657 * @nonblock: nonblocking commit
14658 *
14659 * This function commits a top-level state object that has been validated
14660 * with drm_atomic_helper_check().
14661 *
94f05024
DV
14662 * RETURNS
14663 * Zero for success or -errno.
14664 */
14665static int intel_atomic_commit(struct drm_device *dev,
14666 struct drm_atomic_state *state,
14667 bool nonblock)
14668{
14669 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 14670 struct drm_i915_private *dev_priv = to_i915(dev);
94f05024
DV
14671 int ret = 0;
14672
94f05024
DV
14673 ret = drm_atomic_helper_setup_commit(state, nonblock);
14674 if (ret)
14675 return ret;
14676
c004a90b
CW
14677 drm_atomic_state_get(state);
14678 i915_sw_fence_init(&intel_state->commit_ready,
14679 intel_atomic_commit_ready);
94f05024 14680
d07f0e59 14681 ret = intel_atomic_prepare_commit(dev, state);
94f05024
DV
14682 if (ret) {
14683 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
c004a90b 14684 i915_sw_fence_commit(&intel_state->commit_ready);
94f05024
DV
14685 return ret;
14686 }
14687
14688 drm_atomic_helper_swap_state(state, true);
14689 dev_priv->wm.distrust_bios_wm = false;
3c0fb588 14690 intel_shared_dpll_swap_state(state);
6c9c1b38 14691 intel_atomic_track_fbs(state);
94f05024 14692
c3b32658
ML
14693 if (intel_state->modeset) {
14694 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
14695 sizeof(intel_state->min_pixclk));
14696 dev_priv->active_crtcs = intel_state->active_crtcs;
14697 dev_priv->atomic_cdclk_freq = intel_state->cdclk;
14698 }
14699
0853695c 14700 drm_atomic_state_get(state);
c004a90b
CW
14701 INIT_WORK(&state->commit_work,
14702 nonblock ? intel_atomic_commit_work : NULL);
14703
14704 i915_sw_fence_commit(&intel_state->commit_ready);
14705 if (!nonblock) {
14706 i915_sw_fence_wait(&intel_state->commit_ready);
94f05024 14707 intel_atomic_commit_tail(state);
c004a90b 14708 }
75714940 14709
74c090b1 14710 return 0;
7f27126e
JB
14711}
14712
c0c36b94
CW
14713void intel_crtc_restore_mode(struct drm_crtc *crtc)
14714{
83a57153
ACO
14715 struct drm_device *dev = crtc->dev;
14716 struct drm_atomic_state *state;
e694eb02 14717 struct drm_crtc_state *crtc_state;
2bfb4627 14718 int ret;
83a57153
ACO
14719
14720 state = drm_atomic_state_alloc(dev);
14721 if (!state) {
78108b7c
VS
14722 DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
14723 crtc->base.id, crtc->name);
83a57153
ACO
14724 return;
14725 }
14726
e694eb02 14727 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
83a57153 14728
e694eb02
ML
14729retry:
14730 crtc_state = drm_atomic_get_crtc_state(state, crtc);
14731 ret = PTR_ERR_OR_ZERO(crtc_state);
14732 if (!ret) {
14733 if (!crtc_state->active)
14734 goto out;
83a57153 14735
e694eb02 14736 crtc_state->mode_changed = true;
74c090b1 14737 ret = drm_atomic_commit(state);
83a57153
ACO
14738 }
14739
e694eb02
ML
14740 if (ret == -EDEADLK) {
14741 drm_atomic_state_clear(state);
14742 drm_modeset_backoff(state->acquire_ctx);
14743 goto retry;
4ed9fb37 14744 }
4be07317 14745
e694eb02 14746out:
0853695c 14747 drm_atomic_state_put(state);
c0c36b94
CW
14748}
14749
a8784875
BP
14750/*
14751 * FIXME: Remove this once i915 is fully DRIVER_ATOMIC by calling
14752 * drm_atomic_helper_legacy_gamma_set() directly.
14753 */
14754static int intel_atomic_legacy_gamma_set(struct drm_crtc *crtc,
14755 u16 *red, u16 *green, u16 *blue,
14756 uint32_t size)
14757{
14758 struct drm_device *dev = crtc->dev;
14759 struct drm_mode_config *config = &dev->mode_config;
14760 struct drm_crtc_state *state;
14761 int ret;
14762
14763 ret = drm_atomic_helper_legacy_gamma_set(crtc, red, green, blue, size);
14764 if (ret)
14765 return ret;
14766
14767 /*
14768 * Make sure we update the legacy properties so this works when
14769 * atomic is not enabled.
14770 */
14771
14772 state = crtc->state;
14773
14774 drm_object_property_set_value(&crtc->base,
14775 config->degamma_lut_property,
14776 (state->degamma_lut) ?
14777 state->degamma_lut->base.id : 0);
14778
14779 drm_object_property_set_value(&crtc->base,
14780 config->ctm_property,
14781 (state->ctm) ?
14782 state->ctm->base.id : 0);
14783
14784 drm_object_property_set_value(&crtc->base,
14785 config->gamma_lut_property,
14786 (state->gamma_lut) ?
14787 state->gamma_lut->base.id : 0);
14788
14789 return 0;
14790}
14791
f6e5b160 14792static const struct drm_crtc_funcs intel_crtc_funcs = {
a8784875 14793 .gamma_set = intel_atomic_legacy_gamma_set,
74c090b1 14794 .set_config = drm_atomic_helper_set_config,
82cf435b 14795 .set_property = drm_atomic_helper_crtc_set_property,
f6e5b160 14796 .destroy = intel_crtc_destroy,
4c01ded5 14797 .page_flip = drm_atomic_helper_page_flip,
1356837e
MR
14798 .atomic_duplicate_state = intel_crtc_duplicate_state,
14799 .atomic_destroy_state = intel_crtc_destroy_state,
8c6b709d 14800 .set_crc_source = intel_crtc_set_crc_source,
f6e5b160
CW
14801};
14802
6beb8c23
MR
14803/**
14804 * intel_prepare_plane_fb - Prepare fb for usage on plane
14805 * @plane: drm plane to prepare for
14806 * @fb: framebuffer to prepare for presentation
14807 *
14808 * Prepares a framebuffer for usage on a display plane. Generally this
14809 * involves pinning the underlying object and updating the frontbuffer tracking
14810 * bits. Some older platforms need special physical address handling for
14811 * cursor planes.
14812 *
f935675f
ML
14813 * Must be called with struct_mutex held.
14814 *
6beb8c23
MR
14815 * Returns 0 on success, negative error code on failure.
14816 */
14817int
14818intel_prepare_plane_fb(struct drm_plane *plane,
1832040d 14819 struct drm_plane_state *new_state)
465c120c 14820{
c004a90b
CW
14821 struct intel_atomic_state *intel_state =
14822 to_intel_atomic_state(new_state->state);
b7f05d4a 14823 struct drm_i915_private *dev_priv = to_i915(plane->dev);
844f9111 14824 struct drm_framebuffer *fb = new_state->fb;
6beb8c23 14825 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
1ee49399 14826 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
c004a90b 14827 int ret;
465c120c 14828
1ee49399 14829 if (!obj && !old_obj)
465c120c
MR
14830 return 0;
14831
5008e874
ML
14832 if (old_obj) {
14833 struct drm_crtc_state *crtc_state =
c004a90b
CW
14834 drm_atomic_get_existing_crtc_state(new_state->state,
14835 plane->state->crtc);
5008e874
ML
14836
14837 /* Big Hammer, we also need to ensure that any pending
14838 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
14839 * current scanout is retired before unpinning the old
14840 * framebuffer. Note that we rely on userspace rendering
14841 * into the buffer attached to the pipe they are waiting
14842 * on. If not, userspace generates a GPU hang with IPEHR
14843 * point to the MI_WAIT_FOR_EVENT.
14844 *
14845 * This should only fail upon a hung GPU, in which case we
14846 * can safely continue.
14847 */
c004a90b
CW
14848 if (needs_modeset(crtc_state)) {
14849 ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
14850 old_obj->resv, NULL,
14851 false, 0,
14852 GFP_KERNEL);
14853 if (ret < 0)
14854 return ret;
f4457ae7 14855 }
5008e874
ML
14856 }
14857
c004a90b
CW
14858 if (new_state->fence) { /* explicit fencing */
14859 ret = i915_sw_fence_await_dma_fence(&intel_state->commit_ready,
14860 new_state->fence,
14861 I915_FENCE_TIMEOUT,
14862 GFP_KERNEL);
14863 if (ret < 0)
14864 return ret;
14865 }
14866
c37efb99
CW
14867 if (!obj)
14868 return 0;
14869
c004a90b
CW
14870 if (!new_state->fence) { /* implicit fencing */
14871 ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
14872 obj->resv, NULL,
14873 false, I915_FENCE_TIMEOUT,
14874 GFP_KERNEL);
14875 if (ret < 0)
14876 return ret;
6b5e90f5
CW
14877
14878 i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
c004a90b 14879 }
5a21b665 14880
c37efb99 14881 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
b7f05d4a 14882 INTEL_INFO(dev_priv)->cursor_needs_physical) {
50a0bc90 14883 int align = IS_I830(dev_priv) ? 16 * 1024 : 256;
6beb8c23 14884 ret = i915_gem_object_attach_phys(obj, align);
d07f0e59 14885 if (ret) {
6beb8c23 14886 DRM_DEBUG_KMS("failed to attach phys object\n");
d07f0e59
CW
14887 return ret;
14888 }
6beb8c23 14889 } else {
058d88c4
CW
14890 struct i915_vma *vma;
14891
14892 vma = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
d07f0e59
CW
14893 if (IS_ERR(vma)) {
14894 DRM_DEBUG_KMS("failed to pin object\n");
14895 return PTR_ERR(vma);
14896 }
be1e3415
CW
14897
14898 to_intel_plane_state(new_state)->vma = vma;
7580d774 14899 }
fdd508a6 14900
d07f0e59 14901 return 0;
6beb8c23
MR
14902}
14903
38f3ce3a
MR
14904/**
14905 * intel_cleanup_plane_fb - Cleans up an fb after plane use
14906 * @plane: drm plane to clean up for
14907 * @fb: old framebuffer that was on plane
14908 *
14909 * Cleans up a framebuffer that has just been removed from a plane.
f935675f
ML
14910 *
14911 * Must be called with struct_mutex held.
38f3ce3a
MR
14912 */
14913void
14914intel_cleanup_plane_fb(struct drm_plane *plane,
1832040d 14915 struct drm_plane_state *old_state)
38f3ce3a 14916{
be1e3415 14917 struct i915_vma *vma;
38f3ce3a 14918
be1e3415
CW
14919 /* Should only be called after a successful intel_prepare_plane_fb()! */
14920 vma = fetch_and_zero(&to_intel_plane_state(old_state)->vma);
14921 if (vma)
14922 intel_unpin_fb_vma(vma);
465c120c
MR
14923}
14924
6156a456
CK
14925int
14926skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
14927{
14928 int max_scale;
6156a456
CK
14929 int crtc_clock, cdclk;
14930
bf8a0af0 14931 if (!intel_crtc || !crtc_state->base.enable)
6156a456
CK
14932 return DRM_PLANE_HELPER_NO_SCALING;
14933
6156a456 14934 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
27c329ed 14935 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
6156a456 14936
54bf1ce6 14937 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
6156a456
CK
14938 return DRM_PLANE_HELPER_NO_SCALING;
14939
14940 /*
14941 * skl max scale is lower of:
14942 * close to 3 but not 3, -1 is for that purpose
14943 * or
14944 * cdclk/crtc_clock
14945 */
14946 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
14947
14948 return max_scale;
14949}
14950
465c120c 14951static int
3c692a41 14952intel_check_primary_plane(struct drm_plane *plane,
061e4b8d 14953 struct intel_crtc_state *crtc_state,
3c692a41
GP
14954 struct intel_plane_state *state)
14955{
b63a16f6 14956 struct drm_i915_private *dev_priv = to_i915(plane->dev);
2b875c22 14957 struct drm_crtc *crtc = state->base.crtc;
6156a456 14958 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
061e4b8d
ML
14959 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
14960 bool can_position = false;
b63a16f6 14961 int ret;
465c120c 14962
b63a16f6 14963 if (INTEL_GEN(dev_priv) >= 9) {
693bdc28
VS
14964 /* use scaler when colorkey is not required */
14965 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
14966 min_scale = 1;
14967 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
14968 }
d8106366 14969 can_position = true;
6156a456 14970 }
d8106366 14971
cc926387
DV
14972 ret = drm_plane_helper_check_state(&state->base,
14973 &state->clip,
14974 min_scale, max_scale,
14975 can_position, true);
b63a16f6
VS
14976 if (ret)
14977 return ret;
14978
cc926387 14979 if (!state->base.fb)
b63a16f6
VS
14980 return 0;
14981
14982 if (INTEL_GEN(dev_priv) >= 9) {
14983 ret = skl_check_plane_surface(state);
14984 if (ret)
14985 return ret;
14986 }
14987
14988 return 0;
14af293f
GP
14989}
14990
5a21b665
DV
14991static void intel_begin_crtc_commit(struct drm_crtc *crtc,
14992 struct drm_crtc_state *old_crtc_state)
14993{
14994 struct drm_device *dev = crtc->dev;
62e0fb88 14995 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665 14996 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b707aa50
L
14997 struct intel_crtc_state *intel_cstate =
14998 to_intel_crtc_state(crtc->state);
ccf010fb 14999 struct intel_crtc_state *old_intel_cstate =
5a21b665 15000 to_intel_crtc_state(old_crtc_state);
ccf010fb
ML
15001 struct intel_atomic_state *old_intel_state =
15002 to_intel_atomic_state(old_crtc_state->state);
5a21b665
DV
15003 bool modeset = needs_modeset(crtc->state);
15004
15005 /* Perform vblank evasion around commit operation */
15006 intel_pipe_update_start(intel_crtc);
15007
15008 if (modeset)
e62929b3 15009 goto out;
5a21b665
DV
15010
15011 if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
15012 intel_color_set_csc(crtc->state);
15013 intel_color_load_luts(crtc->state);
15014 }
15015
ccf010fb
ML
15016 if (intel_cstate->update_pipe)
15017 intel_update_pipe_config(intel_crtc, old_intel_cstate);
15018 else if (INTEL_GEN(dev_priv) >= 9)
5a21b665 15019 skl_detach_scalers(intel_crtc);
62e0fb88 15020
e62929b3 15021out:
ccf010fb
ML
15022 if (dev_priv->display.atomic_update_watermarks)
15023 dev_priv->display.atomic_update_watermarks(old_intel_state,
15024 intel_cstate);
5a21b665
DV
15025}
15026
15027static void intel_finish_crtc_commit(struct drm_crtc *crtc,
15028 struct drm_crtc_state *old_crtc_state)
15029{
15030 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
15031
15032 intel_pipe_update_end(intel_crtc, NULL);
15033}
15034
cf4c7c12 15035/**
4a3b8769
MR
15036 * intel_plane_destroy - destroy a plane
15037 * @plane: plane to destroy
cf4c7c12 15038 *
4a3b8769
MR
15039 * Common destruction function for all types of planes (primary, cursor,
15040 * sprite).
cf4c7c12 15041 */
4a3b8769 15042void intel_plane_destroy(struct drm_plane *plane)
465c120c 15043{
465c120c 15044 drm_plane_cleanup(plane);
69ae561f 15045 kfree(to_intel_plane(plane));
465c120c
MR
15046}
15047
65a3fea0 15048const struct drm_plane_funcs intel_plane_funcs = {
70a101f8
MR
15049 .update_plane = drm_atomic_helper_update_plane,
15050 .disable_plane = drm_atomic_helper_disable_plane,
3d7d6510 15051 .destroy = intel_plane_destroy,
c196e1d6 15052 .set_property = drm_atomic_helper_plane_set_property,
a98b3431
MR
15053 .atomic_get_property = intel_plane_atomic_get_property,
15054 .atomic_set_property = intel_plane_atomic_set_property,
ea2c67bb
MR
15055 .atomic_duplicate_state = intel_plane_duplicate_state,
15056 .atomic_destroy_state = intel_plane_destroy_state,
465c120c
MR
15057};
15058
f79f2692
ML
15059static int
15060intel_legacy_cursor_update(struct drm_plane *plane,
15061 struct drm_crtc *crtc,
15062 struct drm_framebuffer *fb,
15063 int crtc_x, int crtc_y,
15064 unsigned int crtc_w, unsigned int crtc_h,
15065 uint32_t src_x, uint32_t src_y,
15066 uint32_t src_w, uint32_t src_h)
15067{
15068 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
15069 int ret;
15070 struct drm_plane_state *old_plane_state, *new_plane_state;
15071 struct intel_plane *intel_plane = to_intel_plane(plane);
15072 struct drm_framebuffer *old_fb;
15073 struct drm_crtc_state *crtc_state = crtc->state;
be1e3415 15074 struct i915_vma *old_vma;
f79f2692
ML
15075
15076 /*
15077 * When crtc is inactive or there is a modeset pending,
15078 * wait for it to complete in the slowpath
15079 */
15080 if (!crtc_state->active || needs_modeset(crtc_state) ||
15081 to_intel_crtc_state(crtc_state)->update_pipe)
15082 goto slow;
15083
15084 old_plane_state = plane->state;
15085
15086 /*
15087 * If any parameters change that may affect watermarks,
15088 * take the slowpath. Only changing fb or position should be
15089 * in the fastpath.
15090 */
15091 if (old_plane_state->crtc != crtc ||
15092 old_plane_state->src_w != src_w ||
15093 old_plane_state->src_h != src_h ||
15094 old_plane_state->crtc_w != crtc_w ||
15095 old_plane_state->crtc_h != crtc_h ||
15096 !old_plane_state->visible ||
15097 old_plane_state->fb->modifier != fb->modifier)
15098 goto slow;
15099
15100 new_plane_state = intel_plane_duplicate_state(plane);
15101 if (!new_plane_state)
15102 return -ENOMEM;
15103
15104 drm_atomic_set_fb_for_plane(new_plane_state, fb);
15105
15106 new_plane_state->src_x = src_x;
15107 new_plane_state->src_y = src_y;
15108 new_plane_state->src_w = src_w;
15109 new_plane_state->src_h = src_h;
15110 new_plane_state->crtc_x = crtc_x;
15111 new_plane_state->crtc_y = crtc_y;
15112 new_plane_state->crtc_w = crtc_w;
15113 new_plane_state->crtc_h = crtc_h;
15114
15115 ret = intel_plane_atomic_check_with_state(to_intel_crtc_state(crtc->state),
15116 to_intel_plane_state(new_plane_state));
15117 if (ret)
15118 goto out_free;
15119
15120 /* Visibility changed, must take slowpath. */
15121 if (!new_plane_state->visible)
15122 goto slow_free;
15123
15124 ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
15125 if (ret)
15126 goto out_free;
15127
15128 if (INTEL_INFO(dev_priv)->cursor_needs_physical) {
15129 int align = IS_I830(dev_priv) ? 16 * 1024 : 256;
15130
15131 ret = i915_gem_object_attach_phys(intel_fb_obj(fb), align);
15132 if (ret) {
15133 DRM_DEBUG_KMS("failed to attach phys object\n");
15134 goto out_unlock;
15135 }
15136 } else {
15137 struct i915_vma *vma;
15138
15139 vma = intel_pin_and_fence_fb_obj(fb, new_plane_state->rotation);
15140 if (IS_ERR(vma)) {
15141 DRM_DEBUG_KMS("failed to pin object\n");
15142
15143 ret = PTR_ERR(vma);
15144 goto out_unlock;
15145 }
be1e3415
CW
15146
15147 to_intel_plane_state(new_plane_state)->vma = vma;
f79f2692
ML
15148 }
15149
15150 old_fb = old_plane_state->fb;
be1e3415 15151 old_vma = to_intel_plane_state(old_plane_state)->vma;
f79f2692
ML
15152
15153 i915_gem_track_fb(intel_fb_obj(old_fb), intel_fb_obj(fb),
15154 intel_plane->frontbuffer_bit);
15155
15156 /* Swap plane state */
15157 new_plane_state->fence = old_plane_state->fence;
15158 *to_intel_plane_state(old_plane_state) = *to_intel_plane_state(new_plane_state);
15159 new_plane_state->fence = NULL;
15160 new_plane_state->fb = old_fb;
be1e3415 15161 to_intel_plane_state(new_plane_state)->vma = old_vma;
f79f2692
ML
15162
15163 intel_plane->update_plane(plane,
15164 to_intel_crtc_state(crtc->state),
15165 to_intel_plane_state(plane->state));
15166
15167 intel_cleanup_plane_fb(plane, new_plane_state);
15168
15169out_unlock:
15170 mutex_unlock(&dev_priv->drm.struct_mutex);
15171out_free:
15172 intel_plane_destroy_state(plane, new_plane_state);
15173 return ret;
15174
15175slow_free:
15176 intel_plane_destroy_state(plane, new_plane_state);
15177slow:
15178 return drm_atomic_helper_update_plane(plane, crtc, fb,
15179 crtc_x, crtc_y, crtc_w, crtc_h,
15180 src_x, src_y, src_w, src_h);
15181}
15182
15183static const struct drm_plane_funcs intel_cursor_plane_funcs = {
15184 .update_plane = intel_legacy_cursor_update,
15185 .disable_plane = drm_atomic_helper_disable_plane,
15186 .destroy = intel_plane_destroy,
15187 .set_property = drm_atomic_helper_plane_set_property,
15188 .atomic_get_property = intel_plane_atomic_get_property,
15189 .atomic_set_property = intel_plane_atomic_set_property,
15190 .atomic_duplicate_state = intel_plane_duplicate_state,
15191 .atomic_destroy_state = intel_plane_destroy_state,
15192};
15193
b079bd17 15194static struct intel_plane *
580503c7 15195intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
465c120c 15196{
fca0ce2a
VS
15197 struct intel_plane *primary = NULL;
15198 struct intel_plane_state *state = NULL;
465c120c 15199 const uint32_t *intel_primary_formats;
93ca7e00 15200 unsigned int supported_rotations;
45e3743a 15201 unsigned int num_formats;
fca0ce2a 15202 int ret;
465c120c
MR
15203
15204 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
b079bd17
VS
15205 if (!primary) {
15206 ret = -ENOMEM;
fca0ce2a 15207 goto fail;
b079bd17 15208 }
465c120c 15209
8e7d688b 15210 state = intel_create_plane_state(&primary->base);
b079bd17
VS
15211 if (!state) {
15212 ret = -ENOMEM;
fca0ce2a 15213 goto fail;
b079bd17
VS
15214 }
15215
8e7d688b 15216 primary->base.state = &state->base;
ea2c67bb 15217
465c120c
MR
15218 primary->can_scale = false;
15219 primary->max_downscale = 1;
580503c7 15220 if (INTEL_GEN(dev_priv) >= 9) {
6156a456 15221 primary->can_scale = true;
af99ceda 15222 state->scaler_id = -1;
6156a456 15223 }
465c120c 15224 primary->pipe = pipe;
e3c566df
VS
15225 /*
15226 * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
15227 * port is hooked to pipe B. Hence we want plane A feeding pipe B.
15228 */
15229 if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4)
15230 primary->plane = (enum plane) !pipe;
15231 else
15232 primary->plane = (enum plane) pipe;
b14e5848 15233 primary->id = PLANE_PRIMARY;
a9ff8714 15234 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
c59cb179 15235 primary->check_plane = intel_check_primary_plane;
465c120c 15236
580503c7 15237 if (INTEL_GEN(dev_priv) >= 9) {
6c0fd451
DL
15238 intel_primary_formats = skl_primary_formats;
15239 num_formats = ARRAY_SIZE(skl_primary_formats);
a8d201af
ML
15240
15241 primary->update_plane = skylake_update_primary_plane;
15242 primary->disable_plane = skylake_disable_primary_plane;
6e266956 15243 } else if (HAS_PCH_SPLIT(dev_priv)) {
a8d201af
ML
15244 intel_primary_formats = i965_primary_formats;
15245 num_formats = ARRAY_SIZE(i965_primary_formats);
15246
15247 primary->update_plane = ironlake_update_primary_plane;
15248 primary->disable_plane = i9xx_disable_primary_plane;
580503c7 15249 } else if (INTEL_GEN(dev_priv) >= 4) {
568db4f2
DL
15250 intel_primary_formats = i965_primary_formats;
15251 num_formats = ARRAY_SIZE(i965_primary_formats);
a8d201af
ML
15252
15253 primary->update_plane = i9xx_update_primary_plane;
15254 primary->disable_plane = i9xx_disable_primary_plane;
6c0fd451
DL
15255 } else {
15256 intel_primary_formats = i8xx_primary_formats;
15257 num_formats = ARRAY_SIZE(i8xx_primary_formats);
a8d201af
ML
15258
15259 primary->update_plane = i9xx_update_primary_plane;
15260 primary->disable_plane = i9xx_disable_primary_plane;
465c120c
MR
15261 }
15262
580503c7
VS
15263 if (INTEL_GEN(dev_priv) >= 9)
15264 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
15265 0, &intel_plane_funcs,
38573dc1
VS
15266 intel_primary_formats, num_formats,
15267 DRM_PLANE_TYPE_PRIMARY,
15268 "plane 1%c", pipe_name(pipe));
9beb5fea 15269 else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
580503c7
VS
15270 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
15271 0, &intel_plane_funcs,
38573dc1
VS
15272 intel_primary_formats, num_formats,
15273 DRM_PLANE_TYPE_PRIMARY,
15274 "primary %c", pipe_name(pipe));
15275 else
580503c7
VS
15276 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
15277 0, &intel_plane_funcs,
38573dc1
VS
15278 intel_primary_formats, num_formats,
15279 DRM_PLANE_TYPE_PRIMARY,
15280 "plane %c", plane_name(primary->plane));
fca0ce2a
VS
15281 if (ret)
15282 goto fail;
48404c1e 15283
5481e27f 15284 if (INTEL_GEN(dev_priv) >= 9) {
93ca7e00
VS
15285 supported_rotations =
15286 DRM_ROTATE_0 | DRM_ROTATE_90 |
15287 DRM_ROTATE_180 | DRM_ROTATE_270;
4ea7be2b
VS
15288 } else if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
15289 supported_rotations =
15290 DRM_ROTATE_0 | DRM_ROTATE_180 |
15291 DRM_REFLECT_X;
5481e27f 15292 } else if (INTEL_GEN(dev_priv) >= 4) {
93ca7e00
VS
15293 supported_rotations =
15294 DRM_ROTATE_0 | DRM_ROTATE_180;
15295 } else {
15296 supported_rotations = DRM_ROTATE_0;
15297 }
15298
5481e27f 15299 if (INTEL_GEN(dev_priv) >= 4)
93ca7e00
VS
15300 drm_plane_create_rotation_property(&primary->base,
15301 DRM_ROTATE_0,
15302 supported_rotations);
48404c1e 15303
ea2c67bb
MR
15304 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
15305
b079bd17 15306 return primary;
fca0ce2a
VS
15307
15308fail:
15309 kfree(state);
15310 kfree(primary);
15311
b079bd17 15312 return ERR_PTR(ret);
465c120c
MR
15313}
15314
3d7d6510 15315static int
852e787c 15316intel_check_cursor_plane(struct drm_plane *plane,
061e4b8d 15317 struct intel_crtc_state *crtc_state,
852e787c 15318 struct intel_plane_state *state)
3d7d6510 15319{
2b875c22 15320 struct drm_framebuffer *fb = state->base.fb;
757f9a3e 15321 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
b29ec92c 15322 enum pipe pipe = to_intel_plane(plane)->pipe;
757f9a3e
GP
15323 unsigned stride;
15324 int ret;
3d7d6510 15325
f8856a44
VS
15326 ret = drm_plane_helper_check_state(&state->base,
15327 &state->clip,
15328 DRM_PLANE_HELPER_NO_SCALING,
15329 DRM_PLANE_HELPER_NO_SCALING,
15330 true, true);
757f9a3e
GP
15331 if (ret)
15332 return ret;
15333
757f9a3e
GP
15334 /* if we want to turn off the cursor ignore width and height */
15335 if (!obj)
da20eabd 15336 return 0;
757f9a3e 15337
757f9a3e 15338 /* Check for which cursor types we support */
50a0bc90
TU
15339 if (!cursor_size_ok(to_i915(plane->dev), state->base.crtc_w,
15340 state->base.crtc_h)) {
ea2c67bb
MR
15341 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
15342 state->base.crtc_w, state->base.crtc_h);
757f9a3e
GP
15343 return -EINVAL;
15344 }
15345
ea2c67bb
MR
15346 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
15347 if (obj->base.size < stride * state->base.crtc_h) {
757f9a3e
GP
15348 DRM_DEBUG_KMS("buffer is too small\n");
15349 return -ENOMEM;
15350 }
15351
bae781b2 15352 if (fb->modifier != DRM_FORMAT_MOD_NONE) {
757f9a3e 15353 DRM_DEBUG_KMS("cursor cannot be tiled\n");
da20eabd 15354 return -EINVAL;
32b7eeec
MR
15355 }
15356
b29ec92c
VS
15357 /*
15358 * There's something wrong with the cursor on CHV pipe C.
15359 * If it straddles the left edge of the screen then
15360 * moving it away from the edge or disabling it often
15361 * results in a pipe underrun, and often that can lead to
15362 * dead pipe (constant underrun reported, and it scans
15363 * out just a solid color). To recover from that, the
15364 * display power well must be turned off and on again.
15365 * Refuse the put the cursor into that compromised position.
15366 */
920a14b2 15367 if (IS_CHERRYVIEW(to_i915(plane->dev)) && pipe == PIPE_C &&
936e71e3 15368 state->base.visible && state->base.crtc_x < 0) {
b29ec92c
VS
15369 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
15370 return -EINVAL;
15371 }
15372
da20eabd 15373 return 0;
852e787c 15374}
3d7d6510 15375
a8ad0d8e
ML
15376static void
15377intel_disable_cursor_plane(struct drm_plane *plane,
7fabf5ef 15378 struct drm_crtc *crtc)
a8ad0d8e 15379{
f2858021
ML
15380 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
15381
15382 intel_crtc->cursor_addr = 0;
55a08b3f 15383 intel_crtc_update_cursor(crtc, NULL);
a8ad0d8e
ML
15384}
15385
f4a2cf29 15386static void
55a08b3f
ML
15387intel_update_cursor_plane(struct drm_plane *plane,
15388 const struct intel_crtc_state *crtc_state,
15389 const struct intel_plane_state *state)
852e787c 15390{
55a08b3f
ML
15391 struct drm_crtc *crtc = crtc_state->base.crtc;
15392 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b7f05d4a 15393 struct drm_i915_private *dev_priv = to_i915(plane->dev);
2b875c22 15394 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
a912f12f 15395 uint32_t addr;
852e787c 15396
f4a2cf29 15397 if (!obj)
a912f12f 15398 addr = 0;
b7f05d4a 15399 else if (!INTEL_INFO(dev_priv)->cursor_needs_physical)
be1e3415 15400 addr = intel_plane_ggtt_offset(state);
f4a2cf29 15401 else
a912f12f 15402 addr = obj->phys_handle->busaddr;
852e787c 15403
a912f12f 15404 intel_crtc->cursor_addr = addr;
55a08b3f 15405 intel_crtc_update_cursor(crtc, state);
852e787c
GP
15406}
15407
b079bd17 15408static struct intel_plane *
580503c7 15409intel_cursor_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
3d7d6510 15410{
fca0ce2a
VS
15411 struct intel_plane *cursor = NULL;
15412 struct intel_plane_state *state = NULL;
15413 int ret;
3d7d6510
MR
15414
15415 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
b079bd17
VS
15416 if (!cursor) {
15417 ret = -ENOMEM;
fca0ce2a 15418 goto fail;
b079bd17 15419 }
3d7d6510 15420
8e7d688b 15421 state = intel_create_plane_state(&cursor->base);
b079bd17
VS
15422 if (!state) {
15423 ret = -ENOMEM;
fca0ce2a 15424 goto fail;
b079bd17
VS
15425 }
15426
8e7d688b 15427 cursor->base.state = &state->base;
ea2c67bb 15428
3d7d6510
MR
15429 cursor->can_scale = false;
15430 cursor->max_downscale = 1;
15431 cursor->pipe = pipe;
15432 cursor->plane = pipe;
b14e5848 15433 cursor->id = PLANE_CURSOR;
a9ff8714 15434 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
c59cb179 15435 cursor->check_plane = intel_check_cursor_plane;
55a08b3f 15436 cursor->update_plane = intel_update_cursor_plane;
a8ad0d8e 15437 cursor->disable_plane = intel_disable_cursor_plane;
3d7d6510 15438
580503c7 15439 ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
f79f2692 15440 0, &intel_cursor_plane_funcs,
fca0ce2a
VS
15441 intel_cursor_formats,
15442 ARRAY_SIZE(intel_cursor_formats),
38573dc1
VS
15443 DRM_PLANE_TYPE_CURSOR,
15444 "cursor %c", pipe_name(pipe));
fca0ce2a
VS
15445 if (ret)
15446 goto fail;
4398ad45 15447
5481e27f 15448 if (INTEL_GEN(dev_priv) >= 4)
93ca7e00
VS
15449 drm_plane_create_rotation_property(&cursor->base,
15450 DRM_ROTATE_0,
15451 DRM_ROTATE_0 |
15452 DRM_ROTATE_180);
4398ad45 15453
580503c7 15454 if (INTEL_GEN(dev_priv) >= 9)
af99ceda
CK
15455 state->scaler_id = -1;
15456
ea2c67bb
MR
15457 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
15458
b079bd17 15459 return cursor;
fca0ce2a
VS
15460
15461fail:
15462 kfree(state);
15463 kfree(cursor);
15464
b079bd17 15465 return ERR_PTR(ret);
3d7d6510
MR
15466}
15467
1c74eeaf
NM
15468static void intel_crtc_init_scalers(struct intel_crtc *crtc,
15469 struct intel_crtc_state *crtc_state)
549e2bfb 15470{
65edccce
VS
15471 struct intel_crtc_scaler_state *scaler_state =
15472 &crtc_state->scaler_state;
1c74eeaf 15473 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
549e2bfb 15474 int i;
549e2bfb 15475
1c74eeaf
NM
15476 crtc->num_scalers = dev_priv->info.num_scalers[crtc->pipe];
15477 if (!crtc->num_scalers)
15478 return;
15479
65edccce
VS
15480 for (i = 0; i < crtc->num_scalers; i++) {
15481 struct intel_scaler *scaler = &scaler_state->scalers[i];
15482
15483 scaler->in_use = 0;
15484 scaler->mode = PS_SCALER_MODE_DYN;
549e2bfb
CK
15485 }
15486
15487 scaler_state->scaler_id = -1;
15488}
15489
5ab0d85b 15490static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
79e53945
JB
15491{
15492 struct intel_crtc *intel_crtc;
f5de6e07 15493 struct intel_crtc_state *crtc_state = NULL;
b079bd17
VS
15494 struct intel_plane *primary = NULL;
15495 struct intel_plane *cursor = NULL;
a81d6fa0 15496 int sprite, ret;
79e53945 15497
955382f3 15498 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
b079bd17
VS
15499 if (!intel_crtc)
15500 return -ENOMEM;
79e53945 15501
f5de6e07 15502 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
b079bd17
VS
15503 if (!crtc_state) {
15504 ret = -ENOMEM;
f5de6e07 15505 goto fail;
b079bd17 15506 }
550acefd
ACO
15507 intel_crtc->config = crtc_state;
15508 intel_crtc->base.state = &crtc_state->base;
07878248 15509 crtc_state->base.crtc = &intel_crtc->base;
f5de6e07 15510
580503c7 15511 primary = intel_primary_plane_create(dev_priv, pipe);
b079bd17
VS
15512 if (IS_ERR(primary)) {
15513 ret = PTR_ERR(primary);
3d7d6510 15514 goto fail;
b079bd17 15515 }
d97d7b48 15516 intel_crtc->plane_ids_mask |= BIT(primary->id);
3d7d6510 15517
a81d6fa0 15518 for_each_sprite(dev_priv, pipe, sprite) {
b079bd17
VS
15519 struct intel_plane *plane;
15520
580503c7 15521 plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
d2b2cbce 15522 if (IS_ERR(plane)) {
b079bd17
VS
15523 ret = PTR_ERR(plane);
15524 goto fail;
15525 }
d97d7b48 15526 intel_crtc->plane_ids_mask |= BIT(plane->id);
a81d6fa0
VS
15527 }
15528
580503c7 15529 cursor = intel_cursor_plane_create(dev_priv, pipe);
d2b2cbce 15530 if (IS_ERR(cursor)) {
b079bd17 15531 ret = PTR_ERR(cursor);
3d7d6510 15532 goto fail;
b079bd17 15533 }
d97d7b48 15534 intel_crtc->plane_ids_mask |= BIT(cursor->id);
3d7d6510 15535
5ab0d85b 15536 ret = drm_crtc_init_with_planes(&dev_priv->drm, &intel_crtc->base,
b079bd17
VS
15537 &primary->base, &cursor->base,
15538 &intel_crtc_funcs,
4d5d72b7 15539 "pipe %c", pipe_name(pipe));
3d7d6510
MR
15540 if (ret)
15541 goto fail;
79e53945 15542
80824003 15543 intel_crtc->pipe = pipe;
e3c566df 15544 intel_crtc->plane = primary->plane;
80824003 15545
4b0e333e
CW
15546 intel_crtc->cursor_base = ~0;
15547 intel_crtc->cursor_cntl = ~0;
dc41c154 15548 intel_crtc->cursor_size = ~0;
8d7849db 15549
852eb00d
VS
15550 intel_crtc->wm.cxsr_allowed = true;
15551
1c74eeaf
NM
15552 /* initialize shared scalers */
15553 intel_crtc_init_scalers(intel_crtc, crtc_state);
15554
22fd0fab
JB
15555 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
15556 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
e2af48c6
VS
15557 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = intel_crtc;
15558 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = intel_crtc;
22fd0fab 15559
79e53945 15560 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
87b6b101 15561
8563b1e8
LL
15562 intel_color_init(&intel_crtc->base);
15563
87b6b101 15564 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
b079bd17
VS
15565
15566 return 0;
3d7d6510
MR
15567
15568fail:
b079bd17
VS
15569 /*
15570 * drm_mode_config_cleanup() will free up any
15571 * crtcs/planes already initialized.
15572 */
f5de6e07 15573 kfree(crtc_state);
3d7d6510 15574 kfree(intel_crtc);
b079bd17
VS
15575
15576 return ret;
79e53945
JB
15577}
15578
752aa88a
JB
15579enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
15580{
15581 struct drm_encoder *encoder = connector->base.encoder;
6e9f798d 15582 struct drm_device *dev = connector->base.dev;
752aa88a 15583
51fd371b 15584 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
752aa88a 15585
d3babd3f 15586 if (!encoder || WARN_ON(!encoder->crtc))
752aa88a
JB
15587 return INVALID_PIPE;
15588
15589 return to_intel_crtc(encoder->crtc)->pipe;
15590}
15591
08d7b3d1 15592int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 15593 struct drm_file *file)
08d7b3d1 15594{
08d7b3d1 15595 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7707e653 15596 struct drm_crtc *drmmode_crtc;
c05422d5 15597 struct intel_crtc *crtc;
08d7b3d1 15598
7707e653 15599 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
71240ed2 15600 if (!drmmode_crtc)
3f2c2057 15601 return -ENOENT;
08d7b3d1 15602
7707e653 15603 crtc = to_intel_crtc(drmmode_crtc);
c05422d5 15604 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 15605
c05422d5 15606 return 0;
08d7b3d1
CW
15607}
15608
66a9278e 15609static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 15610{
66a9278e
DV
15611 struct drm_device *dev = encoder->base.dev;
15612 struct intel_encoder *source_encoder;
79e53945 15613 int index_mask = 0;
79e53945
JB
15614 int entry = 0;
15615
b2784e15 15616 for_each_intel_encoder(dev, source_encoder) {
bc079e8b 15617 if (encoders_cloneable(encoder, source_encoder))
66a9278e
DV
15618 index_mask |= (1 << entry);
15619
79e53945
JB
15620 entry++;
15621 }
4ef69c7a 15622
79e53945
JB
15623 return index_mask;
15624}
15625
646d5772 15626static bool has_edp_a(struct drm_i915_private *dev_priv)
4d302442 15627{
646d5772 15628 if (!IS_MOBILE(dev_priv))
4d302442
CW
15629 return false;
15630
15631 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
15632 return false;
15633
5db94019 15634 if (IS_GEN5(dev_priv) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
15635 return false;
15636
15637 return true;
15638}
15639
6315b5d3 15640static bool intel_crt_present(struct drm_i915_private *dev_priv)
84b4e042 15641{
6315b5d3 15642 if (INTEL_GEN(dev_priv) >= 9)
884497ed
DL
15643 return false;
15644
50a0bc90 15645 if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
84b4e042
JB
15646 return false;
15647
920a14b2 15648 if (IS_CHERRYVIEW(dev_priv))
84b4e042
JB
15649 return false;
15650
4f8036a2
TU
15651 if (HAS_PCH_LPT_H(dev_priv) &&
15652 I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
65e472e4
VS
15653 return false;
15654
70ac54d0 15655 /* DDI E can't be used if DDI A requires 4 lanes */
4f8036a2 15656 if (HAS_DDI(dev_priv) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
70ac54d0
VS
15657 return false;
15658
e4abb733 15659 if (!dev_priv->vbt.int_crt_support)
84b4e042
JB
15660 return false;
15661
15662 return true;
15663}
15664
8090ba8c
ID
15665void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
15666{
15667 int pps_num;
15668 int pps_idx;
15669
15670 if (HAS_DDI(dev_priv))
15671 return;
15672 /*
15673 * This w/a is needed at least on CPT/PPT, but to be sure apply it
15674 * everywhere where registers can be write protected.
15675 */
15676 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
15677 pps_num = 2;
15678 else
15679 pps_num = 1;
15680
15681 for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
15682 u32 val = I915_READ(PP_CONTROL(pps_idx));
15683
15684 val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
15685 I915_WRITE(PP_CONTROL(pps_idx), val);
15686 }
15687}
15688
44cb734c
ID
15689static void intel_pps_init(struct drm_i915_private *dev_priv)
15690{
cc3f90f0 15691 if (HAS_PCH_SPLIT(dev_priv) || IS_GEN9_LP(dev_priv))
44cb734c
ID
15692 dev_priv->pps_mmio_base = PCH_PPS_BASE;
15693 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
15694 dev_priv->pps_mmio_base = VLV_PPS_BASE;
15695 else
15696 dev_priv->pps_mmio_base = PPS_BASE;
8090ba8c
ID
15697
15698 intel_pps_unlock_regs_wa(dev_priv);
44cb734c
ID
15699}
15700
c39055b0 15701static void intel_setup_outputs(struct drm_i915_private *dev_priv)
79e53945 15702{
4ef69c7a 15703 struct intel_encoder *encoder;
cb0953d7 15704 bool dpd_is_edp = false;
79e53945 15705
44cb734c
ID
15706 intel_pps_init(dev_priv);
15707
97a824e1
ID
15708 /*
15709 * intel_edp_init_connector() depends on this completing first, to
15710 * prevent the registeration of both eDP and LVDS and the incorrect
15711 * sharing of the PPS.
15712 */
c39055b0 15713 intel_lvds_init(dev_priv);
79e53945 15714
6315b5d3 15715 if (intel_crt_present(dev_priv))
c39055b0 15716 intel_crt_init(dev_priv);
cb0953d7 15717
cc3f90f0 15718 if (IS_GEN9_LP(dev_priv)) {
c776eb2e
VK
15719 /*
15720 * FIXME: Broxton doesn't support port detection via the
15721 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
15722 * detect the ports.
15723 */
c39055b0
ACO
15724 intel_ddi_init(dev_priv, PORT_A);
15725 intel_ddi_init(dev_priv, PORT_B);
15726 intel_ddi_init(dev_priv, PORT_C);
c6c794a2 15727
c39055b0 15728 intel_dsi_init(dev_priv);
4f8036a2 15729 } else if (HAS_DDI(dev_priv)) {
0e72a5b5
ED
15730 int found;
15731
de31facd
JB
15732 /*
15733 * Haswell uses DDI functions to detect digital outputs.
15734 * On SKL pre-D0 the strap isn't connected, so we assume
15735 * it's there.
15736 */
77179400 15737 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
de31facd 15738 /* WaIgnoreDDIAStrap: skl */
b976dc53 15739 if (found || IS_GEN9_BC(dev_priv))
c39055b0 15740 intel_ddi_init(dev_priv, PORT_A);
0e72a5b5
ED
15741
15742 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
15743 * register */
15744 found = I915_READ(SFUSE_STRAP);
15745
15746 if (found & SFUSE_STRAP_DDIB_DETECTED)
c39055b0 15747 intel_ddi_init(dev_priv, PORT_B);
0e72a5b5 15748 if (found & SFUSE_STRAP_DDIC_DETECTED)
c39055b0 15749 intel_ddi_init(dev_priv, PORT_C);
0e72a5b5 15750 if (found & SFUSE_STRAP_DDID_DETECTED)
c39055b0 15751 intel_ddi_init(dev_priv, PORT_D);
2800e4c2
RV
15752 /*
15753 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
15754 */
b976dc53 15755 if (IS_GEN9_BC(dev_priv) &&
2800e4c2
RV
15756 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
15757 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
15758 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
c39055b0 15759 intel_ddi_init(dev_priv, PORT_E);
2800e4c2 15760
6e266956 15761 } else if (HAS_PCH_SPLIT(dev_priv)) {
cb0953d7 15762 int found;
dd11bc10 15763 dpd_is_edp = intel_dp_is_edp(dev_priv, PORT_D);
270b3042 15764
646d5772 15765 if (has_edp_a(dev_priv))
c39055b0 15766 intel_dp_init(dev_priv, DP_A, PORT_A);
cb0953d7 15767
dc0fa718 15768 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 15769 /* PCH SDVOB multiplex with HDMIB */
c39055b0 15770 found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
30ad48b7 15771 if (!found)
c39055b0 15772 intel_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
5eb08b69 15773 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
c39055b0 15774 intel_dp_init(dev_priv, PCH_DP_B, PORT_B);
30ad48b7
ZW
15775 }
15776
dc0fa718 15777 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
c39055b0 15778 intel_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
30ad48b7 15779
dc0fa718 15780 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
c39055b0 15781 intel_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
30ad48b7 15782
5eb08b69 15783 if (I915_READ(PCH_DP_C) & DP_DETECTED)
c39055b0 15784 intel_dp_init(dev_priv, PCH_DP_C, PORT_C);
5eb08b69 15785
270b3042 15786 if (I915_READ(PCH_DP_D) & DP_DETECTED)
c39055b0 15787 intel_dp_init(dev_priv, PCH_DP_D, PORT_D);
920a14b2 15788 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
22f35042 15789 bool has_edp, has_port;
457c52d8 15790
e17ac6db
VS
15791 /*
15792 * The DP_DETECTED bit is the latched state of the DDC
15793 * SDA pin at boot. However since eDP doesn't require DDC
15794 * (no way to plug in a DP->HDMI dongle) the DDC pins for
15795 * eDP ports may have been muxed to an alternate function.
15796 * Thus we can't rely on the DP_DETECTED bit alone to detect
15797 * eDP ports. Consult the VBT as well as DP_DETECTED to
15798 * detect eDP ports.
22f35042
VS
15799 *
15800 * Sadly the straps seem to be missing sometimes even for HDMI
15801 * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
15802 * and VBT for the presence of the port. Additionally we can't
15803 * trust the port type the VBT declares as we've seen at least
15804 * HDMI ports that the VBT claim are DP or eDP.
e17ac6db 15805 */
dd11bc10 15806 has_edp = intel_dp_is_edp(dev_priv, PORT_B);
22f35042
VS
15807 has_port = intel_bios_is_port_present(dev_priv, PORT_B);
15808 if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
c39055b0 15809 has_edp &= intel_dp_init(dev_priv, VLV_DP_B, PORT_B);
22f35042 15810 if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
c39055b0 15811 intel_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
585a94b8 15812
dd11bc10 15813 has_edp = intel_dp_is_edp(dev_priv, PORT_C);
22f35042
VS
15814 has_port = intel_bios_is_port_present(dev_priv, PORT_C);
15815 if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
c39055b0 15816 has_edp &= intel_dp_init(dev_priv, VLV_DP_C, PORT_C);
22f35042 15817 if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
c39055b0 15818 intel_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
19c03924 15819
920a14b2 15820 if (IS_CHERRYVIEW(dev_priv)) {
22f35042
VS
15821 /*
15822 * eDP not supported on port D,
15823 * so no need to worry about it
15824 */
15825 has_port = intel_bios_is_port_present(dev_priv, PORT_D);
15826 if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
c39055b0 15827 intel_dp_init(dev_priv, CHV_DP_D, PORT_D);
22f35042 15828 if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
c39055b0 15829 intel_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
9418c1f1
VS
15830 }
15831
c39055b0 15832 intel_dsi_init(dev_priv);
5db94019 15833 } else if (!IS_GEN2(dev_priv) && !IS_PINEVIEW(dev_priv)) {
27185ae1 15834 bool found = false;
7d57382e 15835
e2debe91 15836 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 15837 DRM_DEBUG_KMS("probing SDVOB\n");
c39055b0 15838 found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
9beb5fea 15839 if (!found && IS_G4X(dev_priv)) {
b01f2c3a 15840 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
c39055b0 15841 intel_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
b01f2c3a 15842 }
27185ae1 15843
9beb5fea 15844 if (!found && IS_G4X(dev_priv))
c39055b0 15845 intel_dp_init(dev_priv, DP_B, PORT_B);
725e30ad 15846 }
13520b05
KH
15847
15848 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 15849
e2debe91 15850 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 15851 DRM_DEBUG_KMS("probing SDVOC\n");
c39055b0 15852 found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
b01f2c3a 15853 }
27185ae1 15854
e2debe91 15855 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 15856
9beb5fea 15857 if (IS_G4X(dev_priv)) {
b01f2c3a 15858 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
c39055b0 15859 intel_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
b01f2c3a 15860 }
9beb5fea 15861 if (IS_G4X(dev_priv))
c39055b0 15862 intel_dp_init(dev_priv, DP_C, PORT_C);
725e30ad 15863 }
27185ae1 15864
9beb5fea 15865 if (IS_G4X(dev_priv) && (I915_READ(DP_D) & DP_DETECTED))
c39055b0 15866 intel_dp_init(dev_priv, DP_D, PORT_D);
5db94019 15867 } else if (IS_GEN2(dev_priv))
c39055b0 15868 intel_dvo_init(dev_priv);
79e53945 15869
56b857a5 15870 if (SUPPORTS_TV(dev_priv))
c39055b0 15871 intel_tv_init(dev_priv);
79e53945 15872
c39055b0 15873 intel_psr_init(dev_priv);
7c8f8a70 15874
c39055b0 15875 for_each_intel_encoder(&dev_priv->drm, encoder) {
4ef69c7a
CW
15876 encoder->base.possible_crtcs = encoder->crtc_mask;
15877 encoder->base.possible_clones =
66a9278e 15878 intel_encoder_clones(encoder);
79e53945 15879 }
47356eb6 15880
c39055b0 15881 intel_init_pch_refclk(dev_priv);
270b3042 15882
c39055b0 15883 drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
79e53945
JB
15884}
15885
15886static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
15887{
60a5ca01 15888 struct drm_device *dev = fb->dev;
79e53945 15889 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 15890
ef2d633e 15891 drm_framebuffer_cleanup(fb);
60a5ca01 15892 mutex_lock(&dev->struct_mutex);
ef2d633e 15893 WARN_ON(!intel_fb->obj->framebuffer_references--);
f8c417cd 15894 i915_gem_object_put(intel_fb->obj);
60a5ca01 15895 mutex_unlock(&dev->struct_mutex);
79e53945
JB
15896 kfree(intel_fb);
15897}
15898
15899static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 15900 struct drm_file *file,
79e53945
JB
15901 unsigned int *handle)
15902{
15903 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 15904 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 15905
cc917ab4
CW
15906 if (obj->userptr.mm) {
15907 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
15908 return -EINVAL;
15909 }
15910
05394f39 15911 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
15912}
15913
86c98588
RV
15914static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
15915 struct drm_file *file,
15916 unsigned flags, unsigned color,
15917 struct drm_clip_rect *clips,
15918 unsigned num_clips)
15919{
15920 struct drm_device *dev = fb->dev;
15921 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
15922 struct drm_i915_gem_object *obj = intel_fb->obj;
15923
15924 mutex_lock(&dev->struct_mutex);
a6a7cc4b
CW
15925 if (obj->pin_display && obj->cache_dirty)
15926 i915_gem_clflush_object(obj, true);
74b4ea1e 15927 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
86c98588
RV
15928 mutex_unlock(&dev->struct_mutex);
15929
15930 return 0;
15931}
15932
79e53945
JB
15933static const struct drm_framebuffer_funcs intel_fb_funcs = {
15934 .destroy = intel_user_framebuffer_destroy,
15935 .create_handle = intel_user_framebuffer_create_handle,
86c98588 15936 .dirty = intel_user_framebuffer_dirty,
79e53945
JB
15937};
15938
b321803d 15939static
920a14b2
TU
15940u32 intel_fb_pitch_limit(struct drm_i915_private *dev_priv,
15941 uint64_t fb_modifier, uint32_t pixel_format)
b321803d 15942{
920a14b2 15943 u32 gen = INTEL_INFO(dev_priv)->gen;
b321803d
DL
15944
15945 if (gen >= 9) {
ac484963
VS
15946 int cpp = drm_format_plane_cpp(pixel_format, 0);
15947
b321803d
DL
15948 /* "The stride in bytes must not exceed the of the size of 8K
15949 * pixels and 32K bytes."
15950 */
ac484963 15951 return min(8192 * cpp, 32768);
920a14b2
TU
15952 } else if (gen >= 5 && !IS_VALLEYVIEW(dev_priv) &&
15953 !IS_CHERRYVIEW(dev_priv)) {
b321803d
DL
15954 return 32*1024;
15955 } else if (gen >= 4) {
15956 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
15957 return 16*1024;
15958 else
15959 return 32*1024;
15960 } else if (gen >= 3) {
15961 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
15962 return 8*1024;
15963 else
15964 return 16*1024;
15965 } else {
15966 /* XXX DSPC is limited to 4k tiled */
15967 return 8*1024;
15968 }
15969}
15970
b5ea642a
DV
15971static int intel_framebuffer_init(struct drm_device *dev,
15972 struct intel_framebuffer *intel_fb,
15973 struct drm_mode_fb_cmd2 *mode_cmd,
15974 struct drm_i915_gem_object *obj)
79e53945 15975{
7b49f948 15976 struct drm_i915_private *dev_priv = to_i915(dev);
c2ff7370 15977 unsigned int tiling = i915_gem_object_get_tiling(obj);
79e53945 15978 int ret;
b321803d 15979 u32 pitch_limit, stride_alignment;
b3c11ac2 15980 struct drm_format_name_buf format_name;
79e53945 15981
dd4916c5
DV
15982 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
15983
2a80eada 15984 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
c2ff7370
VS
15985 /*
15986 * If there's a fence, enforce that
15987 * the fb modifier and tiling mode match.
15988 */
15989 if (tiling != I915_TILING_NONE &&
15990 tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
2a80eada
DV
15991 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
15992 return -EINVAL;
15993 }
15994 } else {
c2ff7370 15995 if (tiling == I915_TILING_X) {
2a80eada 15996 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
c2ff7370 15997 } else if (tiling == I915_TILING_Y) {
2a80eada
DV
15998 DRM_DEBUG("No Y tiling for legacy addfb\n");
15999 return -EINVAL;
16000 }
16001 }
16002
9a8f0a12
TU
16003 /* Passed in modifier sanity checking. */
16004 switch (mode_cmd->modifier[0]) {
16005 case I915_FORMAT_MOD_Y_TILED:
16006 case I915_FORMAT_MOD_Yf_TILED:
6315b5d3 16007 if (INTEL_GEN(dev_priv) < 9) {
9a8f0a12
TU
16008 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
16009 mode_cmd->modifier[0]);
16010 return -EINVAL;
16011 }
16012 case DRM_FORMAT_MOD_NONE:
16013 case I915_FORMAT_MOD_X_TILED:
16014 break;
16015 default:
c0f40428
JB
16016 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
16017 mode_cmd->modifier[0]);
57cd6508 16018 return -EINVAL;
c16ed4be 16019 }
57cd6508 16020
c2ff7370
VS
16021 /*
16022 * gen2/3 display engine uses the fence if present,
16023 * so the tiling mode must match the fb modifier exactly.
16024 */
16025 if (INTEL_INFO(dev_priv)->gen < 4 &&
16026 tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
16027 DRM_DEBUG("tiling_mode must match fb modifier exactly on gen2/3\n");
16028 return -EINVAL;
16029 }
16030
7b49f948
VS
16031 stride_alignment = intel_fb_stride_alignment(dev_priv,
16032 mode_cmd->modifier[0],
b321803d
DL
16033 mode_cmd->pixel_format);
16034 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
16035 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
16036 mode_cmd->pitches[0], stride_alignment);
57cd6508 16037 return -EINVAL;
c16ed4be 16038 }
57cd6508 16039
920a14b2 16040 pitch_limit = intel_fb_pitch_limit(dev_priv, mode_cmd->modifier[0],
b321803d 16041 mode_cmd->pixel_format);
a35cdaa0 16042 if (mode_cmd->pitches[0] > pitch_limit) {
b321803d
DL
16043 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
16044 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
2a80eada 16045 "tiled" : "linear",
a35cdaa0 16046 mode_cmd->pitches[0], pitch_limit);
5d7bd705 16047 return -EINVAL;
c16ed4be 16048 }
5d7bd705 16049
c2ff7370
VS
16050 /*
16051 * If there's a fence, enforce that
16052 * the fb pitch and fence stride match.
16053 */
16054 if (tiling != I915_TILING_NONE &&
3e510a8e 16055 mode_cmd->pitches[0] != i915_gem_object_get_stride(obj)) {
c16ed4be 16056 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
3e510a8e
CW
16057 mode_cmd->pitches[0],
16058 i915_gem_object_get_stride(obj));
5d7bd705 16059 return -EINVAL;
c16ed4be 16060 }
5d7bd705 16061
57779d06 16062 /* Reject formats not supported by any plane early. */
308e5bcb 16063 switch (mode_cmd->pixel_format) {
57779d06 16064 case DRM_FORMAT_C8:
04b3924d
VS
16065 case DRM_FORMAT_RGB565:
16066 case DRM_FORMAT_XRGB8888:
16067 case DRM_FORMAT_ARGB8888:
57779d06
VS
16068 break;
16069 case DRM_FORMAT_XRGB1555:
6315b5d3 16070 if (INTEL_GEN(dev_priv) > 3) {
b3c11ac2
EE
16071 DRM_DEBUG("unsupported pixel format: %s\n",
16072 drm_get_format_name(mode_cmd->pixel_format, &format_name));
57779d06 16073 return -EINVAL;
c16ed4be 16074 }
57779d06 16075 break;
57779d06 16076 case DRM_FORMAT_ABGR8888:
920a14b2 16077 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
6315b5d3 16078 INTEL_GEN(dev_priv) < 9) {
b3c11ac2
EE
16079 DRM_DEBUG("unsupported pixel format: %s\n",
16080 drm_get_format_name(mode_cmd->pixel_format, &format_name));
6c0fd451
DL
16081 return -EINVAL;
16082 }
16083 break;
16084 case DRM_FORMAT_XBGR8888:
04b3924d 16085 case DRM_FORMAT_XRGB2101010:
57779d06 16086 case DRM_FORMAT_XBGR2101010:
6315b5d3 16087 if (INTEL_GEN(dev_priv) < 4) {
b3c11ac2
EE
16088 DRM_DEBUG("unsupported pixel format: %s\n",
16089 drm_get_format_name(mode_cmd->pixel_format, &format_name));
57779d06 16090 return -EINVAL;
c16ed4be 16091 }
b5626747 16092 break;
7531208b 16093 case DRM_FORMAT_ABGR2101010:
920a14b2 16094 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
b3c11ac2
EE
16095 DRM_DEBUG("unsupported pixel format: %s\n",
16096 drm_get_format_name(mode_cmd->pixel_format, &format_name));
7531208b
DL
16097 return -EINVAL;
16098 }
16099 break;
04b3924d
VS
16100 case DRM_FORMAT_YUYV:
16101 case DRM_FORMAT_UYVY:
16102 case DRM_FORMAT_YVYU:
16103 case DRM_FORMAT_VYUY:
6315b5d3 16104 if (INTEL_GEN(dev_priv) < 5) {
b3c11ac2
EE
16105 DRM_DEBUG("unsupported pixel format: %s\n",
16106 drm_get_format_name(mode_cmd->pixel_format, &format_name));
57779d06 16107 return -EINVAL;
c16ed4be 16108 }
57cd6508
CW
16109 break;
16110 default:
b3c11ac2
EE
16111 DRM_DEBUG("unsupported pixel format: %s\n",
16112 drm_get_format_name(mode_cmd->pixel_format, &format_name));
57cd6508
CW
16113 return -EINVAL;
16114 }
16115
90f9a336
VS
16116 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
16117 if (mode_cmd->offsets[0] != 0)
16118 return -EINVAL;
16119
a3f913ca 16120 drm_helper_mode_fill_fb_struct(dev, &intel_fb->base, mode_cmd);
c7d73f6a
DV
16121 intel_fb->obj = obj;
16122
6687c906
VS
16123 ret = intel_fill_fb_info(dev_priv, &intel_fb->base);
16124 if (ret)
16125 return ret;
2d7a215f 16126
79e53945
JB
16127 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
16128 if (ret) {
16129 DRM_ERROR("framebuffer init failed %d\n", ret);
16130 return ret;
16131 }
16132
0b05e1e0
VS
16133 intel_fb->obj->framebuffer_references++;
16134
79e53945
JB
16135 return 0;
16136}
16137
79e53945
JB
16138static struct drm_framebuffer *
16139intel_user_framebuffer_create(struct drm_device *dev,
16140 struct drm_file *filp,
1eb83451 16141 const struct drm_mode_fb_cmd2 *user_mode_cmd)
79e53945 16142{
dcb1394e 16143 struct drm_framebuffer *fb;
05394f39 16144 struct drm_i915_gem_object *obj;
76dc3769 16145 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
79e53945 16146
03ac0642
CW
16147 obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
16148 if (!obj)
cce13ff7 16149 return ERR_PTR(-ENOENT);
79e53945 16150
92907cbb 16151 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
dcb1394e 16152 if (IS_ERR(fb))
f0cd5182 16153 i915_gem_object_put(obj);
dcb1394e
LW
16154
16155 return fb;
79e53945
JB
16156}
16157
778e23a9
CW
16158static void intel_atomic_state_free(struct drm_atomic_state *state)
16159{
16160 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
16161
16162 drm_atomic_state_default_release(state);
16163
16164 i915_sw_fence_fini(&intel_state->commit_ready);
16165
16166 kfree(state);
16167}
16168
79e53945 16169static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 16170 .fb_create = intel_user_framebuffer_create,
0632fef6 16171 .output_poll_changed = intel_fbdev_output_poll_changed,
5ee67f1c
MR
16172 .atomic_check = intel_atomic_check,
16173 .atomic_commit = intel_atomic_commit,
de419ab6
ML
16174 .atomic_state_alloc = intel_atomic_state_alloc,
16175 .atomic_state_clear = intel_atomic_state_clear,
778e23a9 16176 .atomic_state_free = intel_atomic_state_free,
79e53945
JB
16177};
16178
88212941
ID
16179/**
16180 * intel_init_display_hooks - initialize the display modesetting hooks
16181 * @dev_priv: device private
16182 */
16183void intel_init_display_hooks(struct drm_i915_private *dev_priv)
e70236a8 16184{
88212941 16185 if (INTEL_INFO(dev_priv)->gen >= 9) {
bc8d7dff 16186 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
16187 dev_priv->display.get_initial_plane_config =
16188 skylake_get_initial_plane_config;
bc8d7dff
DL
16189 dev_priv->display.crtc_compute_clock =
16190 haswell_crtc_compute_clock;
16191 dev_priv->display.crtc_enable = haswell_crtc_enable;
16192 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 16193 } else if (HAS_DDI(dev_priv)) {
0e8ffe1b 16194 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
16195 dev_priv->display.get_initial_plane_config =
16196 ironlake_get_initial_plane_config;
797d0259
ACO
16197 dev_priv->display.crtc_compute_clock =
16198 haswell_crtc_compute_clock;
4f771f10
PZ
16199 dev_priv->display.crtc_enable = haswell_crtc_enable;
16200 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 16201 } else if (HAS_PCH_SPLIT(dev_priv)) {
0e8ffe1b 16202 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
5724dbd1
DL
16203 dev_priv->display.get_initial_plane_config =
16204 ironlake_get_initial_plane_config;
3fb37703
ACO
16205 dev_priv->display.crtc_compute_clock =
16206 ironlake_crtc_compute_clock;
76e5a89c
DV
16207 dev_priv->display.crtc_enable = ironlake_crtc_enable;
16208 dev_priv->display.crtc_disable = ironlake_crtc_disable;
65b3d6a9 16209 } else if (IS_CHERRYVIEW(dev_priv)) {
89b667f8 16210 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
16211 dev_priv->display.get_initial_plane_config =
16212 i9xx_get_initial_plane_config;
65b3d6a9
ACO
16213 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
16214 dev_priv->display.crtc_enable = valleyview_crtc_enable;
16215 dev_priv->display.crtc_disable = i9xx_crtc_disable;
16216 } else if (IS_VALLEYVIEW(dev_priv)) {
16217 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
16218 dev_priv->display.get_initial_plane_config =
16219 i9xx_get_initial_plane_config;
16220 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
89b667f8
JB
16221 dev_priv->display.crtc_enable = valleyview_crtc_enable;
16222 dev_priv->display.crtc_disable = i9xx_crtc_disable;
19ec6693
ACO
16223 } else if (IS_G4X(dev_priv)) {
16224 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
16225 dev_priv->display.get_initial_plane_config =
16226 i9xx_get_initial_plane_config;
16227 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
16228 dev_priv->display.crtc_enable = i9xx_crtc_enable;
16229 dev_priv->display.crtc_disable = i9xx_crtc_disable;
70e8aa21
ACO
16230 } else if (IS_PINEVIEW(dev_priv)) {
16231 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
16232 dev_priv->display.get_initial_plane_config =
16233 i9xx_get_initial_plane_config;
16234 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
16235 dev_priv->display.crtc_enable = i9xx_crtc_enable;
16236 dev_priv->display.crtc_disable = i9xx_crtc_disable;
81c97f52 16237 } else if (!IS_GEN2(dev_priv)) {
0e8ffe1b 16238 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
16239 dev_priv->display.get_initial_plane_config =
16240 i9xx_get_initial_plane_config;
d6dfee7a 16241 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
76e5a89c
DV
16242 dev_priv->display.crtc_enable = i9xx_crtc_enable;
16243 dev_priv->display.crtc_disable = i9xx_crtc_disable;
81c97f52
ACO
16244 } else {
16245 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
16246 dev_priv->display.get_initial_plane_config =
16247 i9xx_get_initial_plane_config;
16248 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
16249 dev_priv->display.crtc_enable = i9xx_crtc_enable;
16250 dev_priv->display.crtc_disable = i9xx_crtc_disable;
f564048e 16251 }
e70236a8 16252
e70236a8 16253 /* Returns the core display clock speed */
b976dc53 16254 if (IS_GEN9_BC(dev_priv))
1652d19e
VS
16255 dev_priv->display.get_display_clock_speed =
16256 skylake_get_display_clock_speed;
89b3c3c7 16257 else if (IS_GEN9_LP(dev_priv))
acd3f3d3
BP
16258 dev_priv->display.get_display_clock_speed =
16259 broxton_get_display_clock_speed;
88212941 16260 else if (IS_BROADWELL(dev_priv))
1652d19e
VS
16261 dev_priv->display.get_display_clock_speed =
16262 broadwell_get_display_clock_speed;
88212941 16263 else if (IS_HASWELL(dev_priv))
1652d19e
VS
16264 dev_priv->display.get_display_clock_speed =
16265 haswell_get_display_clock_speed;
88212941 16266 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
25eb05fc
JB
16267 dev_priv->display.get_display_clock_speed =
16268 valleyview_get_display_clock_speed;
88212941 16269 else if (IS_GEN5(dev_priv))
b37a6434
VS
16270 dev_priv->display.get_display_clock_speed =
16271 ilk_get_display_clock_speed;
c0f86832 16272 else if (IS_I945G(dev_priv) || IS_I965G(dev_priv) ||
88212941 16273 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
e70236a8
JB
16274 dev_priv->display.get_display_clock_speed =
16275 i945_get_display_clock_speed;
88212941 16276 else if (IS_GM45(dev_priv))
34edce2f
VS
16277 dev_priv->display.get_display_clock_speed =
16278 gm45_get_display_clock_speed;
c0f86832 16279 else if (IS_I965GM(dev_priv))
34edce2f
VS
16280 dev_priv->display.get_display_clock_speed =
16281 i965gm_get_display_clock_speed;
88212941 16282 else if (IS_PINEVIEW(dev_priv))
34edce2f
VS
16283 dev_priv->display.get_display_clock_speed =
16284 pnv_get_display_clock_speed;
88212941 16285 else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
34edce2f
VS
16286 dev_priv->display.get_display_clock_speed =
16287 g33_get_display_clock_speed;
88212941 16288 else if (IS_I915G(dev_priv))
e70236a8
JB
16289 dev_priv->display.get_display_clock_speed =
16290 i915_get_display_clock_speed;
6248017a 16291 else if (IS_I845G(dev_priv))
e70236a8
JB
16292 dev_priv->display.get_display_clock_speed =
16293 i9xx_misc_get_display_clock_speed;
6248017a
AH
16294 else if (IS_I945GM(dev_priv))
16295 dev_priv->display.get_display_clock_speed =
16296 i945gm_get_display_clock_speed;
88212941 16297 else if (IS_I915GM(dev_priv))
e70236a8
JB
16298 dev_priv->display.get_display_clock_speed =
16299 i915gm_get_display_clock_speed;
88212941 16300 else if (IS_I865G(dev_priv))
e70236a8
JB
16301 dev_priv->display.get_display_clock_speed =
16302 i865_get_display_clock_speed;
88212941 16303 else if (IS_I85X(dev_priv))
e70236a8 16304 dev_priv->display.get_display_clock_speed =
1b1d2716 16305 i85x_get_display_clock_speed;
623e01e5 16306 else { /* 830 */
88212941 16307 WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
e70236a8
JB
16308 dev_priv->display.get_display_clock_speed =
16309 i830_get_display_clock_speed;
623e01e5 16310 }
e70236a8 16311
88212941 16312 if (IS_GEN5(dev_priv)) {
3bb11b53 16313 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
88212941 16314 } else if (IS_GEN6(dev_priv)) {
3bb11b53 16315 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
88212941 16316 } else if (IS_IVYBRIDGE(dev_priv)) {
3bb11b53
SJ
16317 /* FIXME: detect B0+ stepping and use auto training */
16318 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
88212941 16319 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
3bb11b53 16320 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
445e780b
VS
16321 }
16322
16323 if (IS_BROADWELL(dev_priv)) {
16324 dev_priv->display.modeset_commit_cdclk =
16325 broadwell_modeset_commit_cdclk;
16326 dev_priv->display.modeset_calc_cdclk =
16327 broadwell_modeset_calc_cdclk;
88212941 16328 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
27c329ed
ML
16329 dev_priv->display.modeset_commit_cdclk =
16330 valleyview_modeset_commit_cdclk;
16331 dev_priv->display.modeset_calc_cdclk =
16332 valleyview_modeset_calc_cdclk;
89b3c3c7 16333 } else if (IS_GEN9_LP(dev_priv)) {
27c329ed 16334 dev_priv->display.modeset_commit_cdclk =
324513c0 16335 bxt_modeset_commit_cdclk;
27c329ed 16336 dev_priv->display.modeset_calc_cdclk =
324513c0 16337 bxt_modeset_calc_cdclk;
b976dc53 16338 } else if (IS_GEN9_BC(dev_priv)) {
c89e39f3
CT
16339 dev_priv->display.modeset_commit_cdclk =
16340 skl_modeset_commit_cdclk;
16341 dev_priv->display.modeset_calc_cdclk =
16342 skl_modeset_calc_cdclk;
e70236a8 16343 }
5a21b665 16344
27082493
L
16345 if (dev_priv->info.gen >= 9)
16346 dev_priv->display.update_crtcs = skl_update_crtcs;
16347 else
16348 dev_priv->display.update_crtcs = intel_update_crtcs;
16349
5a21b665
DV
16350 switch (INTEL_INFO(dev_priv)->gen) {
16351 case 2:
16352 dev_priv->display.queue_flip = intel_gen2_queue_flip;
16353 break;
16354
16355 case 3:
16356 dev_priv->display.queue_flip = intel_gen3_queue_flip;
16357 break;
16358
16359 case 4:
16360 case 5:
16361 dev_priv->display.queue_flip = intel_gen4_queue_flip;
16362 break;
16363
16364 case 6:
16365 dev_priv->display.queue_flip = intel_gen6_queue_flip;
16366 break;
16367 case 7:
16368 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
16369 dev_priv->display.queue_flip = intel_gen7_queue_flip;
16370 break;
16371 case 9:
16372 /* Drop through - unsupported since execlist only. */
16373 default:
16374 /* Default just returns -ENODEV to indicate unsupported */
16375 dev_priv->display.queue_flip = intel_default_queue_flip;
16376 }
e70236a8
JB
16377}
16378
b690e96c
JB
16379/*
16380 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
16381 * resume, or other times. This quirk makes sure that's the case for
16382 * affected systems.
16383 */
0206e353 16384static void quirk_pipea_force(struct drm_device *dev)
b690e96c 16385{
fac5e23e 16386 struct drm_i915_private *dev_priv = to_i915(dev);
b690e96c
JB
16387
16388 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
bc0daf48 16389 DRM_INFO("applying pipe a force quirk\n");
b690e96c
JB
16390}
16391
b6b5d049
VS
16392static void quirk_pipeb_force(struct drm_device *dev)
16393{
fac5e23e 16394 struct drm_i915_private *dev_priv = to_i915(dev);
b6b5d049
VS
16395
16396 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
16397 DRM_INFO("applying pipe b force quirk\n");
16398}
16399
435793df
KP
16400/*
16401 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
16402 */
16403static void quirk_ssc_force_disable(struct drm_device *dev)
16404{
fac5e23e 16405 struct drm_i915_private *dev_priv = to_i915(dev);
435793df 16406 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 16407 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
16408}
16409
4dca20ef 16410/*
5a15ab5b
CE
16411 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
16412 * brightness value
4dca20ef
CE
16413 */
16414static void quirk_invert_brightness(struct drm_device *dev)
16415{
fac5e23e 16416 struct drm_i915_private *dev_priv = to_i915(dev);
4dca20ef 16417 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 16418 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
16419}
16420
9c72cc6f
SD
16421/* Some VBT's incorrectly indicate no backlight is present */
16422static void quirk_backlight_present(struct drm_device *dev)
16423{
fac5e23e 16424 struct drm_i915_private *dev_priv = to_i915(dev);
9c72cc6f
SD
16425 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
16426 DRM_INFO("applying backlight present quirk\n");
16427}
16428
b690e96c
JB
16429struct intel_quirk {
16430 int device;
16431 int subsystem_vendor;
16432 int subsystem_device;
16433 void (*hook)(struct drm_device *dev);
16434};
16435
5f85f176
EE
16436/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
16437struct intel_dmi_quirk {
16438 void (*hook)(struct drm_device *dev);
16439 const struct dmi_system_id (*dmi_id_list)[];
16440};
16441
16442static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
16443{
16444 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
16445 return 1;
16446}
16447
16448static const struct intel_dmi_quirk intel_dmi_quirks[] = {
16449 {
16450 .dmi_id_list = &(const struct dmi_system_id[]) {
16451 {
16452 .callback = intel_dmi_reverse_brightness,
16453 .ident = "NCR Corporation",
16454 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
16455 DMI_MATCH(DMI_PRODUCT_NAME, ""),
16456 },
16457 },
16458 { } /* terminating entry */
16459 },
16460 .hook = quirk_invert_brightness,
16461 },
16462};
16463
c43b5634 16464static struct intel_quirk intel_quirks[] = {
b690e96c
JB
16465 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
16466 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
16467
b690e96c
JB
16468 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
16469 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
16470
5f080c0f
VS
16471 /* 830 needs to leave pipe A & dpll A up */
16472 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
16473
b6b5d049
VS
16474 /* 830 needs to leave pipe B & dpll B up */
16475 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
16476
435793df
KP
16477 /* Lenovo U160 cannot use SSC on LVDS */
16478 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
16479
16480 /* Sony Vaio Y cannot use SSC on LVDS */
16481 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 16482
be505f64
AH
16483 /* Acer Aspire 5734Z must invert backlight brightness */
16484 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
16485
16486 /* Acer/eMachines G725 */
16487 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
16488
16489 /* Acer/eMachines e725 */
16490 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
16491
16492 /* Acer/Packard Bell NCL20 */
16493 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
16494
16495 /* Acer Aspire 4736Z */
16496 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
16497
16498 /* Acer Aspire 5336 */
16499 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
2e93a1aa
SD
16500
16501 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
16502 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
d4967d8c 16503
dfb3d47b
SD
16504 /* Acer C720 Chromebook (Core i3 4005U) */
16505 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
16506
b2a9601c 16507 /* Apple Macbook 2,1 (Core 2 T7400) */
16508 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
16509
1b9448b0
JN
16510 /* Apple Macbook 4,1 */
16511 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
16512
d4967d8c
SD
16513 /* Toshiba CB35 Chromebook (Celeron 2955U) */
16514 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
724cb06f
SD
16515
16516 /* HP Chromebook 14 (Celeron 2955U) */
16517 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
cf6f0af9
JN
16518
16519 /* Dell Chromebook 11 */
16520 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
9be64eee
JN
16521
16522 /* Dell Chromebook 11 (2015 version) */
16523 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
b690e96c
JB
16524};
16525
16526static void intel_init_quirks(struct drm_device *dev)
16527{
16528 struct pci_dev *d = dev->pdev;
16529 int i;
16530
16531 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
16532 struct intel_quirk *q = &intel_quirks[i];
16533
16534 if (d->device == q->device &&
16535 (d->subsystem_vendor == q->subsystem_vendor ||
16536 q->subsystem_vendor == PCI_ANY_ID) &&
16537 (d->subsystem_device == q->subsystem_device ||
16538 q->subsystem_device == PCI_ANY_ID))
16539 q->hook(dev);
16540 }
5f85f176
EE
16541 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
16542 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
16543 intel_dmi_quirks[i].hook(dev);
16544 }
b690e96c
JB
16545}
16546
9cce37f4 16547/* Disable the VGA plane that we never use */
29b74b7f 16548static void i915_disable_vga(struct drm_i915_private *dev_priv)
9cce37f4 16549{
52a05c30 16550 struct pci_dev *pdev = dev_priv->drm.pdev;
9cce37f4 16551 u8 sr1;
920a14b2 16552 i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
9cce37f4 16553
2b37c616 16554 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
52a05c30 16555 vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 16556 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
16557 sr1 = inb(VGA_SR_DATA);
16558 outb(sr1 | 1<<5, VGA_SR_DATA);
52a05c30 16559 vga_put(pdev, VGA_RSRC_LEGACY_IO);
9cce37f4
JB
16560 udelay(300);
16561
01f5a626 16562 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9cce37f4
JB
16563 POSTING_READ(vga_reg);
16564}
16565
f817586c
DV
16566void intel_modeset_init_hw(struct drm_device *dev)
16567{
fac5e23e 16568 struct drm_i915_private *dev_priv = to_i915(dev);
1a617b77 16569
4c75b940 16570 intel_update_cdclk(dev_priv);
1a617b77
ML
16571
16572 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
16573
46f16e63 16574 intel_init_clock_gating(dev_priv);
f817586c
DV
16575}
16576
d93c0372
MR
16577/*
16578 * Calculate what we think the watermarks should be for the state we've read
16579 * out of the hardware and then immediately program those watermarks so that
16580 * we ensure the hardware settings match our internal state.
16581 *
16582 * We can calculate what we think WM's should be by creating a duplicate of the
16583 * current state (which was constructed during hardware readout) and running it
16584 * through the atomic check code to calculate new watermark values in the
16585 * state object.
16586 */
16587static void sanitize_watermarks(struct drm_device *dev)
16588{
16589 struct drm_i915_private *dev_priv = to_i915(dev);
16590 struct drm_atomic_state *state;
ccf010fb 16591 struct intel_atomic_state *intel_state;
d93c0372
MR
16592 struct drm_crtc *crtc;
16593 struct drm_crtc_state *cstate;
16594 struct drm_modeset_acquire_ctx ctx;
16595 int ret;
16596 int i;
16597
16598 /* Only supported on platforms that use atomic watermark design */
ed4a6a7c 16599 if (!dev_priv->display.optimize_watermarks)
d93c0372
MR
16600 return;
16601
16602 /*
16603 * We need to hold connection_mutex before calling duplicate_state so
16604 * that the connector loop is protected.
16605 */
16606 drm_modeset_acquire_init(&ctx, 0);
16607retry:
0cd1262d 16608 ret = drm_modeset_lock_all_ctx(dev, &ctx);
d93c0372
MR
16609 if (ret == -EDEADLK) {
16610 drm_modeset_backoff(&ctx);
16611 goto retry;
16612 } else if (WARN_ON(ret)) {
0cd1262d 16613 goto fail;
d93c0372
MR
16614 }
16615
16616 state = drm_atomic_helper_duplicate_state(dev, &ctx);
16617 if (WARN_ON(IS_ERR(state)))
0cd1262d 16618 goto fail;
d93c0372 16619
ccf010fb
ML
16620 intel_state = to_intel_atomic_state(state);
16621
ed4a6a7c
MR
16622 /*
16623 * Hardware readout is the only time we don't want to calculate
16624 * intermediate watermarks (since we don't trust the current
16625 * watermarks).
16626 */
ccf010fb 16627 intel_state->skip_intermediate_wm = true;
ed4a6a7c 16628
d93c0372
MR
16629 ret = intel_atomic_check(dev, state);
16630 if (ret) {
16631 /*
16632 * If we fail here, it means that the hardware appears to be
16633 * programmed in a way that shouldn't be possible, given our
16634 * understanding of watermark requirements. This might mean a
16635 * mistake in the hardware readout code or a mistake in the
16636 * watermark calculations for a given platform. Raise a WARN
16637 * so that this is noticeable.
16638 *
16639 * If this actually happens, we'll have to just leave the
16640 * BIOS-programmed watermarks untouched and hope for the best.
16641 */
16642 WARN(true, "Could not determine valid watermarks for inherited state\n");
b9a1b717 16643 goto put_state;
d93c0372
MR
16644 }
16645
16646 /* Write calculated watermark values back */
d93c0372
MR
16647 for_each_crtc_in_state(state, crtc, cstate, i) {
16648 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
16649
ed4a6a7c 16650 cs->wm.need_postvbl_update = true;
ccf010fb 16651 dev_priv->display.optimize_watermarks(intel_state, cs);
d93c0372
MR
16652 }
16653
b9a1b717 16654put_state:
0853695c 16655 drm_atomic_state_put(state);
0cd1262d 16656fail:
d93c0372
MR
16657 drm_modeset_drop_locks(&ctx);
16658 drm_modeset_acquire_fini(&ctx);
16659}
16660
b079bd17 16661int intel_modeset_init(struct drm_device *dev)
79e53945 16662{
72e96d64
JL
16663 struct drm_i915_private *dev_priv = to_i915(dev);
16664 struct i915_ggtt *ggtt = &dev_priv->ggtt;
8cc87b75 16665 enum pipe pipe;
46f297fb 16666 struct intel_crtc *crtc;
79e53945
JB
16667
16668 drm_mode_config_init(dev);
16669
16670 dev->mode_config.min_width = 0;
16671 dev->mode_config.min_height = 0;
16672
019d96cb
DA
16673 dev->mode_config.preferred_depth = 24;
16674 dev->mode_config.prefer_shadow = 1;
16675
25bab385
TU
16676 dev->mode_config.allow_fb_modifiers = true;
16677
e6ecefaa 16678 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 16679
eb955eee 16680 INIT_WORK(&dev_priv->atomic_helper.free_work,
ba318c61 16681 intel_atomic_helper_free_state_worker);
eb955eee 16682
b690e96c
JB
16683 intel_init_quirks(dev);
16684
62d75df7 16685 intel_init_pm(dev_priv);
1fa61106 16686
b7f05d4a 16687 if (INTEL_INFO(dev_priv)->num_pipes == 0)
b079bd17 16688 return 0;
e3c74757 16689
69f92f67
LW
16690 /*
16691 * There may be no VBT; and if the BIOS enabled SSC we can
16692 * just keep using it to avoid unnecessary flicker. Whereas if the
16693 * BIOS isn't using it, don't assume it will work even if the VBT
16694 * indicates as much.
16695 */
6e266956 16696 if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
69f92f67
LW
16697 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
16698 DREF_SSC1_ENABLE);
16699
16700 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
16701 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
16702 bios_lvds_use_ssc ? "en" : "dis",
16703 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
16704 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
16705 }
16706 }
16707
5db94019 16708 if (IS_GEN2(dev_priv)) {
a6c45cf0
CW
16709 dev->mode_config.max_width = 2048;
16710 dev->mode_config.max_height = 2048;
5db94019 16711 } else if (IS_GEN3(dev_priv)) {
5e4d6fa7
KP
16712 dev->mode_config.max_width = 4096;
16713 dev->mode_config.max_height = 4096;
79e53945 16714 } else {
a6c45cf0
CW
16715 dev->mode_config.max_width = 8192;
16716 dev->mode_config.max_height = 8192;
79e53945 16717 }
068be561 16718
2a307c2e
JN
16719 if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
16720 dev->mode_config.cursor_width = IS_I845G(dev_priv) ? 64 : 512;
dc41c154 16721 dev->mode_config.cursor_height = 1023;
5db94019 16722 } else if (IS_GEN2(dev_priv)) {
068be561
DL
16723 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
16724 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
16725 } else {
16726 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
16727 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
16728 }
16729
72e96d64 16730 dev->mode_config.fb_base = ggtt->mappable_base;
79e53945 16731
28c97730 16732 DRM_DEBUG_KMS("%d display pipe%s available.\n",
b7f05d4a
TU
16733 INTEL_INFO(dev_priv)->num_pipes,
16734 INTEL_INFO(dev_priv)->num_pipes > 1 ? "s" : "");
79e53945 16735
055e393f 16736 for_each_pipe(dev_priv, pipe) {
b079bd17
VS
16737 int ret;
16738
5ab0d85b 16739 ret = intel_crtc_init(dev_priv, pipe);
b079bd17
VS
16740 if (ret) {
16741 drm_mode_config_cleanup(dev);
16742 return ret;
16743 }
79e53945
JB
16744 }
16745
bfa7df01 16746 intel_update_czclk(dev_priv);
4c75b940 16747 intel_update_cdclk(dev_priv);
6a259b1f 16748 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
bfa7df01 16749
e72f9fbf 16750 intel_shared_dpll_init(dev);
ee7b9f93 16751
b2045352 16752 if (dev_priv->max_cdclk_freq == 0)
4c75b940 16753 intel_update_max_cdclk(dev_priv);
b2045352 16754
9cce37f4 16755 /* Just disable it once at startup */
29b74b7f 16756 i915_disable_vga(dev_priv);
c39055b0 16757 intel_setup_outputs(dev_priv);
11be49eb 16758
6e9f798d 16759 drm_modeset_lock_all(dev);
043e9bda 16760 intel_modeset_setup_hw_state(dev);
6e9f798d 16761 drm_modeset_unlock_all(dev);
46f297fb 16762
d3fcc808 16763 for_each_intel_crtc(dev, crtc) {
eeebeac5
ML
16764 struct intel_initial_plane_config plane_config = {};
16765
46f297fb
JB
16766 if (!crtc->active)
16767 continue;
16768
46f297fb 16769 /*
46f297fb
JB
16770 * Note that reserving the BIOS fb up front prevents us
16771 * from stuffing other stolen allocations like the ring
16772 * on top. This prevents some ugliness at boot time, and
16773 * can even allow for smooth boot transitions if the BIOS
16774 * fb is large enough for the active pipe configuration.
16775 */
eeebeac5
ML
16776 dev_priv->display.get_initial_plane_config(crtc,
16777 &plane_config);
16778
16779 /*
16780 * If the fb is shared between multiple heads, we'll
16781 * just get the first one.
16782 */
16783 intel_find_initial_plane_obj(crtc, &plane_config);
46f297fb 16784 }
d93c0372
MR
16785
16786 /*
16787 * Make sure hardware watermarks really match the state we read out.
16788 * Note that we need to do this after reconstructing the BIOS fb's
16789 * since the watermark calculation done here will use pstate->fb.
16790 */
16791 sanitize_watermarks(dev);
b079bd17
VS
16792
16793 return 0;
2c7111db
CW
16794}
16795
7fad798e
DV
16796static void intel_enable_pipe_a(struct drm_device *dev)
16797{
16798 struct intel_connector *connector;
16799 struct drm_connector *crt = NULL;
16800 struct intel_load_detect_pipe load_detect_temp;
208bf9fd 16801 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
7fad798e
DV
16802
16803 /* We can't just switch on the pipe A, we need to set things up with a
16804 * proper mode and output configuration. As a gross hack, enable pipe A
16805 * by enabling the load detect pipe once. */
3a3371ff 16806 for_each_intel_connector(dev, connector) {
7fad798e
DV
16807 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
16808 crt = &connector->base;
16809 break;
16810 }
16811 }
16812
16813 if (!crt)
16814 return;
16815
208bf9fd 16816 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
49172fee 16817 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
7fad798e
DV
16818}
16819
fa555837
DV
16820static bool
16821intel_check_plane_mapping(struct intel_crtc *crtc)
16822{
b7f05d4a 16823 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
649636ef 16824 u32 val;
fa555837 16825
b7f05d4a 16826 if (INTEL_INFO(dev_priv)->num_pipes == 1)
fa555837
DV
16827 return true;
16828
649636ef 16829 val = I915_READ(DSPCNTR(!crtc->plane));
fa555837
DV
16830
16831 if ((val & DISPLAY_PLANE_ENABLE) &&
16832 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
16833 return false;
16834
16835 return true;
16836}
16837
02e93c35
VS
16838static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
16839{
16840 struct drm_device *dev = crtc->base.dev;
16841 struct intel_encoder *encoder;
16842
16843 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
16844 return true;
16845
16846 return false;
16847}
16848
496b0fc3
ML
16849static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
16850{
16851 struct drm_device *dev = encoder->base.dev;
16852 struct intel_connector *connector;
16853
16854 for_each_connector_on_encoder(dev, &encoder->base, connector)
16855 return connector;
16856
16857 return NULL;
16858}
16859
a168f5b3
VS
16860static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
16861 enum transcoder pch_transcoder)
16862{
16863 return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
16864 (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == TRANSCODER_A);
16865}
16866
24929352
DV
16867static void intel_sanitize_crtc(struct intel_crtc *crtc)
16868{
16869 struct drm_device *dev = crtc->base.dev;
fac5e23e 16870 struct drm_i915_private *dev_priv = to_i915(dev);
4d1de975 16871 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
24929352 16872
24929352 16873 /* Clear any frame start delays used for debugging left by the BIOS */
4d1de975
JN
16874 if (!transcoder_is_dsi(cpu_transcoder)) {
16875 i915_reg_t reg = PIPECONF(cpu_transcoder);
16876
16877 I915_WRITE(reg,
16878 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
16879 }
24929352 16880
d3eaf884 16881 /* restore vblank interrupts to correct state */
9625604c 16882 drm_crtc_vblank_reset(&crtc->base);
d297e103 16883 if (crtc->active) {
f9cd7b88
VS
16884 struct intel_plane *plane;
16885
9625604c 16886 drm_crtc_vblank_on(&crtc->base);
f9cd7b88
VS
16887
16888 /* Disable everything but the primary plane */
16889 for_each_intel_plane_on_crtc(dev, crtc, plane) {
16890 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
16891 continue;
16892
16893 plane->disable_plane(&plane->base, &crtc->base);
16894 }
9625604c 16895 }
d3eaf884 16896
24929352 16897 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
DV
16898 * disable the crtc (and hence change the state) if it is wrong. Note
16899 * that gen4+ has a fixed plane -> pipe mapping. */
6315b5d3 16900 if (INTEL_GEN(dev_priv) < 4 && !intel_check_plane_mapping(crtc)) {
24929352
DV
16901 bool plane;
16902
78108b7c
VS
16903 DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
16904 crtc->base.base.id, crtc->base.name);
24929352
DV
16905
16906 /* Pipe has the wrong plane attached and the plane is active.
16907 * Temporarily change the plane mapping and disable everything
16908 * ... */
16909 plane = crtc->plane;
1d4258db 16910 crtc->base.primary->state->visible = true;
24929352 16911 crtc->plane = !plane;
b17d48e2 16912 intel_crtc_disable_noatomic(&crtc->base);
24929352 16913 crtc->plane = plane;
24929352 16914 }
24929352 16915
7fad798e
DV
16916 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
16917 crtc->pipe == PIPE_A && !crtc->active) {
16918 /* BIOS forgot to enable pipe A, this mostly happens after
16919 * resume. Force-enable the pipe to fix this, the update_dpms
16920 * call below we restore the pipe to the right state, but leave
16921 * the required bits on. */
16922 intel_enable_pipe_a(dev);
16923 }
16924
24929352
DV
16925 /* Adjust the state of the output pipe according to whether we
16926 * have active connectors/encoders. */
842e0307 16927 if (crtc->active && !intel_crtc_has_encoders(crtc))
b17d48e2 16928 intel_crtc_disable_noatomic(&crtc->base);
24929352 16929
49cff963 16930 if (crtc->active || HAS_GMCH_DISPLAY(dev_priv)) {
4cc31489
DV
16931 /*
16932 * We start out with underrun reporting disabled to avoid races.
16933 * For correct bookkeeping mark this on active crtcs.
16934 *
c5ab3bc0
DV
16935 * Also on gmch platforms we dont have any hardware bits to
16936 * disable the underrun reporting. Which means we need to start
16937 * out with underrun reporting disabled also on inactive pipes,
16938 * since otherwise we'll complain about the garbage we read when
16939 * e.g. coming up after runtime pm.
16940 *
4cc31489
DV
16941 * No protection against concurrent access is required - at
16942 * worst a fifo underrun happens which also sets this to false.
16943 */
16944 crtc->cpu_fifo_underrun_disabled = true;
a168f5b3
VS
16945 /*
16946 * We track the PCH trancoder underrun reporting state
16947 * within the crtc. With crtc for pipe A housing the underrun
16948 * reporting state for PCH transcoder A, crtc for pipe B housing
16949 * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
16950 * and marking underrun reporting as disabled for the non-existing
16951 * PCH transcoders B and C would prevent enabling the south
16952 * error interrupt (see cpt_can_enable_serr_int()).
16953 */
16954 if (has_pch_trancoder(dev_priv, (enum transcoder)crtc->pipe))
16955 crtc->pch_fifo_underrun_disabled = true;
4cc31489 16956 }
24929352
DV
16957}
16958
16959static void intel_sanitize_encoder(struct intel_encoder *encoder)
16960{
16961 struct intel_connector *connector;
24929352
DV
16962
16963 /* We need to check both for a crtc link (meaning that the
16964 * encoder is active and trying to read from a pipe) and the
16965 * pipe itself being active. */
16966 bool has_active_crtc = encoder->base.crtc &&
16967 to_intel_crtc(encoder->base.crtc)->active;
16968
496b0fc3
ML
16969 connector = intel_encoder_find_connector(encoder);
16970 if (connector && !has_active_crtc) {
24929352
DV
16971 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
16972 encoder->base.base.id,
8e329a03 16973 encoder->base.name);
24929352
DV
16974
16975 /* Connector is active, but has no active pipe. This is
16976 * fallout from our resume register restoring. Disable
16977 * the encoder manually again. */
16978 if (encoder->base.crtc) {
fd6bbda9
ML
16979 struct drm_crtc_state *crtc_state = encoder->base.crtc->state;
16980
24929352
DV
16981 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
16982 encoder->base.base.id,
8e329a03 16983 encoder->base.name);
fd6bbda9 16984 encoder->disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
a62d1497 16985 if (encoder->post_disable)
fd6bbda9 16986 encoder->post_disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
24929352 16987 }
7f1950fb 16988 encoder->base.crtc = NULL;
24929352
DV
16989
16990 /* Inconsistent output/port/pipe state happens presumably due to
16991 * a bug in one of the get_hw_state functions. Or someplace else
16992 * in our code, like the register restore mess on resume. Clamp
16993 * things to off as a safer default. */
fd6bbda9
ML
16994
16995 connector->base.dpms = DRM_MODE_DPMS_OFF;
16996 connector->base.encoder = NULL;
24929352
DV
16997 }
16998 /* Enabled encoders without active connectors will be fixed in
16999 * the crtc fixup. */
17000}
17001
29b74b7f 17002void i915_redisable_vga_power_on(struct drm_i915_private *dev_priv)
0fde901f 17003{
920a14b2 17004 i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
0fde901f 17005
04098753
ID
17006 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
17007 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
29b74b7f 17008 i915_disable_vga(dev_priv);
04098753
ID
17009 }
17010}
17011
29b74b7f 17012void i915_redisable_vga(struct drm_i915_private *dev_priv)
04098753 17013{
8dc8a27c
PZ
17014 /* This function can be called both from intel_modeset_setup_hw_state or
17015 * at a very early point in our resume sequence, where the power well
17016 * structures are not yet restored. Since this function is at a very
17017 * paranoid "someone might have enabled VGA while we were not looking"
17018 * level, just check if the power well is enabled instead of trying to
17019 * follow the "don't touch the power well if we don't need it" policy
17020 * the rest of the driver uses. */
6392f847 17021 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
17022 return;
17023
29b74b7f 17024 i915_redisable_vga_power_on(dev_priv);
6392f847
ID
17025
17026 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
0fde901f
KM
17027}
17028
f9cd7b88 17029static bool primary_get_hw_state(struct intel_plane *plane)
98ec7739 17030{
f9cd7b88 17031 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
98ec7739 17032
f9cd7b88 17033 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
d032ffa0
ML
17034}
17035
f9cd7b88
VS
17036/* FIXME read out full plane state for all planes */
17037static void readout_plane_state(struct intel_crtc *crtc)
d032ffa0 17038{
b26d3ea3 17039 struct drm_plane *primary = crtc->base.primary;
f9cd7b88 17040 struct intel_plane_state *plane_state =
b26d3ea3 17041 to_intel_plane_state(primary->state);
d032ffa0 17042
936e71e3 17043 plane_state->base.visible = crtc->active &&
b26d3ea3
ML
17044 primary_get_hw_state(to_intel_plane(primary));
17045
936e71e3 17046 if (plane_state->base.visible)
b26d3ea3 17047 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
98ec7739
VS
17048}
17049
30e984df 17050static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352 17051{
fac5e23e 17052 struct drm_i915_private *dev_priv = to_i915(dev);
24929352 17053 enum pipe pipe;
24929352
DV
17054 struct intel_crtc *crtc;
17055 struct intel_encoder *encoder;
17056 struct intel_connector *connector;
5358901f 17057 int i;
24929352 17058
565602d7
ML
17059 dev_priv->active_crtcs = 0;
17060
d3fcc808 17061 for_each_intel_crtc(dev, crtc) {
a8cd6da0
VS
17062 struct intel_crtc_state *crtc_state =
17063 to_intel_crtc_state(crtc->base.state);
3b117c8f 17064
ec2dc6a0 17065 __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
565602d7
ML
17066 memset(crtc_state, 0, sizeof(*crtc_state));
17067 crtc_state->base.crtc = &crtc->base;
24929352 17068
565602d7
ML
17069 crtc_state->base.active = crtc_state->base.enable =
17070 dev_priv->display.get_pipe_config(crtc, crtc_state);
17071
17072 crtc->base.enabled = crtc_state->base.enable;
17073 crtc->active = crtc_state->base.active;
17074
aca1ebf4 17075 if (crtc_state->base.active)
565602d7
ML
17076 dev_priv->active_crtcs |= 1 << crtc->pipe;
17077
f9cd7b88 17078 readout_plane_state(crtc);
24929352 17079
78108b7c
VS
17080 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
17081 crtc->base.base.id, crtc->base.name,
a8cd6da0 17082 enableddisabled(crtc_state->base.active));
24929352
DV
17083 }
17084
5358901f
DV
17085 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
17086 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
17087
2edd6443 17088 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
2c42e535
ACO
17089 &pll->state.hw_state);
17090 pll->state.crtc_mask = 0;
d3fcc808 17091 for_each_intel_crtc(dev, crtc) {
a8cd6da0
VS
17092 struct intel_crtc_state *crtc_state =
17093 to_intel_crtc_state(crtc->base.state);
17094
17095 if (crtc_state->base.active &&
17096 crtc_state->shared_dpll == pll)
2c42e535 17097 pll->state.crtc_mask |= 1 << crtc->pipe;
5358901f 17098 }
2c42e535 17099 pll->active_mask = pll->state.crtc_mask;
5358901f 17100
1e6f2ddc 17101 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
2c42e535 17102 pll->name, pll->state.crtc_mask, pll->on);
5358901f
DV
17103 }
17104
b2784e15 17105 for_each_intel_encoder(dev, encoder) {
24929352
DV
17106 pipe = 0;
17107
17108 if (encoder->get_hw_state(encoder, &pipe)) {
a8cd6da0
VS
17109 struct intel_crtc_state *crtc_state;
17110
98187836 17111 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
a8cd6da0 17112 crtc_state = to_intel_crtc_state(crtc->base.state);
e2af48c6 17113
045ac3b5 17114 encoder->base.crtc = &crtc->base;
a8cd6da0
VS
17115 crtc_state->output_types |= 1 << encoder->type;
17116 encoder->get_config(encoder, crtc_state);
24929352
DV
17117 } else {
17118 encoder->base.crtc = NULL;
17119 }
17120
6f2bcceb 17121 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
08c4d7fc
TU
17122 encoder->base.base.id, encoder->base.name,
17123 enableddisabled(encoder->base.crtc),
6f2bcceb 17124 pipe_name(pipe));
24929352
DV
17125 }
17126
3a3371ff 17127 for_each_intel_connector(dev, connector) {
24929352
DV
17128 if (connector->get_hw_state(connector)) {
17129 connector->base.dpms = DRM_MODE_DPMS_ON;
2aa974c9
ML
17130
17131 encoder = connector->encoder;
17132 connector->base.encoder = &encoder->base;
17133
17134 if (encoder->base.crtc &&
17135 encoder->base.crtc->state->active) {
17136 /*
17137 * This has to be done during hardware readout
17138 * because anything calling .crtc_disable may
17139 * rely on the connector_mask being accurate.
17140 */
17141 encoder->base.crtc->state->connector_mask |=
17142 1 << drm_connector_index(&connector->base);
e87a52b3
ML
17143 encoder->base.crtc->state->encoder_mask |=
17144 1 << drm_encoder_index(&encoder->base);
2aa974c9
ML
17145 }
17146
24929352
DV
17147 } else {
17148 connector->base.dpms = DRM_MODE_DPMS_OFF;
17149 connector->base.encoder = NULL;
17150 }
17151 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
08c4d7fc
TU
17152 connector->base.base.id, connector->base.name,
17153 enableddisabled(connector->base.encoder));
24929352 17154 }
7f4c6284
VS
17155
17156 for_each_intel_crtc(dev, crtc) {
a8cd6da0
VS
17157 struct intel_crtc_state *crtc_state =
17158 to_intel_crtc_state(crtc->base.state);
aca1ebf4
VS
17159 int pixclk = 0;
17160
a8cd6da0 17161 crtc->base.hwmode = crtc_state->base.adjusted_mode;
7f4c6284
VS
17162
17163 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
a8cd6da0
VS
17164 if (crtc_state->base.active) {
17165 intel_mode_from_pipe_config(&crtc->base.mode, crtc_state);
17166 intel_mode_from_pipe_config(&crtc_state->base.adjusted_mode, crtc_state);
7f4c6284
VS
17167 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
17168
17169 /*
17170 * The initial mode needs to be set in order to keep
17171 * the atomic core happy. It wants a valid mode if the
17172 * crtc's enabled, so we do the above call.
17173 *
7800fb69
DV
17174 * But we don't set all the derived state fully, hence
17175 * set a flag to indicate that a full recalculation is
17176 * needed on the next commit.
7f4c6284 17177 */
a8cd6da0 17178 crtc_state->base.mode.private_flags = I915_MODE_FLAG_INHERITED;
9eca6832 17179
aca1ebf4 17180 if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
a8cd6da0 17181 pixclk = ilk_pipe_pixel_rate(crtc_state);
aca1ebf4 17182 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
a8cd6da0 17183 pixclk = crtc_state->base.adjusted_mode.crtc_clock;
aca1ebf4
VS
17184 else
17185 WARN_ON(dev_priv->display.modeset_calc_cdclk);
17186
17187 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
a8cd6da0 17188 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
aca1ebf4
VS
17189 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
17190
9eca6832
VS
17191 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
17192 update_scanline_offset(crtc);
7f4c6284 17193 }
e3b247da 17194
aca1ebf4
VS
17195 dev_priv->min_pixclk[crtc->pipe] = pixclk;
17196
a8cd6da0 17197 intel_pipe_config_sanity_check(dev_priv, crtc_state);
7f4c6284 17198 }
30e984df
DV
17199}
17200
043e9bda
ML
17201/* Scan out the current hw modeset state,
17202 * and sanitizes it to the current state
17203 */
17204static void
17205intel_modeset_setup_hw_state(struct drm_device *dev)
30e984df 17206{
fac5e23e 17207 struct drm_i915_private *dev_priv = to_i915(dev);
30e984df 17208 enum pipe pipe;
30e984df
DV
17209 struct intel_crtc *crtc;
17210 struct intel_encoder *encoder;
35c95375 17211 int i;
30e984df
DV
17212
17213 intel_modeset_readout_hw_state(dev);
24929352
DV
17214
17215 /* HW state is read out, now we need to sanitize this mess. */
b2784e15 17216 for_each_intel_encoder(dev, encoder) {
24929352
DV
17217 intel_sanitize_encoder(encoder);
17218 }
17219
055e393f 17220 for_each_pipe(dev_priv, pipe) {
98187836 17221 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
e2af48c6 17222
24929352 17223 intel_sanitize_crtc(crtc);
6e3c9717
ACO
17224 intel_dump_pipe_config(crtc, crtc->config,
17225 "[setup_hw_state]");
24929352 17226 }
9a935856 17227
d29b2f9d
ACO
17228 intel_modeset_update_connector_atomic_state(dev);
17229
35c95375
DV
17230 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
17231 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
17232
2dd66ebd 17233 if (!pll->on || pll->active_mask)
35c95375
DV
17234 continue;
17235
17236 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
17237
2edd6443 17238 pll->funcs.disable(dev_priv, pll);
35c95375
DV
17239 pll->on = false;
17240 }
17241
920a14b2 17242 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
6eb1a681 17243 vlv_wm_get_hw_state(dev);
5db94019 17244 else if (IS_GEN9(dev_priv))
3078999f 17245 skl_wm_get_hw_state(dev);
6e266956 17246 else if (HAS_PCH_SPLIT(dev_priv))
243e6a44 17247 ilk_wm_get_hw_state(dev);
292b990e
ML
17248
17249 for_each_intel_crtc(dev, crtc) {
17250 unsigned long put_domains;
17251
74bff5f9 17252 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
292b990e
ML
17253 if (WARN_ON(put_domains))
17254 modeset_put_power_domains(dev_priv, put_domains);
17255 }
17256 intel_display_set_init_power(dev_priv, false);
010cf73d
PZ
17257
17258 intel_fbc_init_pipe_state(dev_priv);
043e9bda 17259}
7d0bc1ea 17260
043e9bda
ML
17261void intel_display_resume(struct drm_device *dev)
17262{
e2c8b870
ML
17263 struct drm_i915_private *dev_priv = to_i915(dev);
17264 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
17265 struct drm_modeset_acquire_ctx ctx;
043e9bda 17266 int ret;
f30da187 17267
e2c8b870 17268 dev_priv->modeset_restore_state = NULL;
73974893
ML
17269 if (state)
17270 state->acquire_ctx = &ctx;
043e9bda 17271
ea49c9ac
ML
17272 /*
17273 * This is a cludge because with real atomic modeset mode_config.mutex
17274 * won't be taken. Unfortunately some probed state like
17275 * audio_codec_enable is still protected by mode_config.mutex, so lock
17276 * it here for now.
17277 */
17278 mutex_lock(&dev->mode_config.mutex);
e2c8b870 17279 drm_modeset_acquire_init(&ctx, 0);
043e9bda 17280
73974893
ML
17281 while (1) {
17282 ret = drm_modeset_lock_all_ctx(dev, &ctx);
17283 if (ret != -EDEADLK)
17284 break;
043e9bda 17285
e2c8b870 17286 drm_modeset_backoff(&ctx);
e2c8b870 17287 }
043e9bda 17288
73974893
ML
17289 if (!ret)
17290 ret = __intel_display_resume(dev, state);
17291
e2c8b870
ML
17292 drm_modeset_drop_locks(&ctx);
17293 drm_modeset_acquire_fini(&ctx);
ea49c9ac 17294 mutex_unlock(&dev->mode_config.mutex);
043e9bda 17295
0853695c 17296 if (ret)
e2c8b870 17297 DRM_ERROR("Restoring old state failed with %i\n", ret);
3c5e37f1
CW
17298 if (state)
17299 drm_atomic_state_put(state);
2c7111db
CW
17300}
17301
17302void intel_modeset_gem_init(struct drm_device *dev)
17303{
dc97997a 17304 struct drm_i915_private *dev_priv = to_i915(dev);
484b41dd 17305
dc97997a 17306 intel_init_gt_powersave(dev_priv);
ae48434c 17307
1833b134 17308 intel_modeset_init_hw(dev);
02e792fb 17309
1ee8da6d 17310 intel_setup_overlay(dev_priv);
1ebaa0b9
CW
17311}
17312
17313int intel_connector_register(struct drm_connector *connector)
17314{
17315 struct intel_connector *intel_connector = to_intel_connector(connector);
17316 int ret;
17317
17318 ret = intel_backlight_device_register(intel_connector);
17319 if (ret)
17320 goto err;
17321
17322 return 0;
0962c3c9 17323
1ebaa0b9
CW
17324err:
17325 return ret;
79e53945
JB
17326}
17327
c191eca1 17328void intel_connector_unregister(struct drm_connector *connector)
4932e2c3 17329{
e63d87c0 17330 struct intel_connector *intel_connector = to_intel_connector(connector);
4932e2c3 17331
e63d87c0 17332 intel_backlight_device_unregister(intel_connector);
4932e2c3 17333 intel_panel_destroy_backlight(connector);
4932e2c3
ID
17334}
17335
79e53945
JB
17336void intel_modeset_cleanup(struct drm_device *dev)
17337{
fac5e23e 17338 struct drm_i915_private *dev_priv = to_i915(dev);
652c393a 17339
eb955eee
CW
17340 flush_work(&dev_priv->atomic_helper.free_work);
17341 WARN_ON(!llist_empty(&dev_priv->atomic_helper.free_list));
17342
dc97997a 17343 intel_disable_gt_powersave(dev_priv);
2eb5252e 17344
fd0c0642
DV
17345 /*
17346 * Interrupts and polling as the first thing to avoid creating havoc.
2eb5252e 17347 * Too much stuff here (turning of connectors, ...) would
fd0c0642
DV
17348 * experience fancy races otherwise.
17349 */
2aeb7d3a 17350 intel_irq_uninstall(dev_priv);
eb21b92b 17351
fd0c0642
DV
17352 /*
17353 * Due to the hpd irq storm handling the hotplug work can re-arm the
17354 * poll handlers. Hence disable polling after hpd handling is shut down.
17355 */
f87ea761 17356 drm_kms_helper_poll_fini(dev);
fd0c0642 17357
723bfd70
JB
17358 intel_unregister_dsm_handler();
17359
c937ab3e 17360 intel_fbc_global_disable(dev_priv);
69341a5e 17361
1630fe75
CW
17362 /* flush any delayed tasks or pending work */
17363 flush_scheduled_work();
17364
79e53945 17365 drm_mode_config_cleanup(dev);
4d7bb011 17366
1ee8da6d 17367 intel_cleanup_overlay(dev_priv);
ae48434c 17368
dc97997a 17369 intel_cleanup_gt_powersave(dev_priv);
f5949141 17370
40196446 17371 intel_teardown_gmbus(dev_priv);
79e53945
JB
17372}
17373
df0e9248
CW
17374void intel_connector_attach_encoder(struct intel_connector *connector,
17375 struct intel_encoder *encoder)
17376{
17377 connector->encoder = encoder;
17378 drm_mode_connector_attach_encoder(&connector->base,
17379 &encoder->base);
79e53945 17380}
28d52043
DA
17381
17382/*
17383 * set vga decode state - true == enable VGA decode
17384 */
6315b5d3 17385int intel_modeset_vga_set_state(struct drm_i915_private *dev_priv, bool state)
28d52043 17386{
6315b5d3 17387 unsigned reg = INTEL_GEN(dev_priv) >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
17388 u16 gmch_ctrl;
17389
75fa041d
CW
17390 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
17391 DRM_ERROR("failed to read control word\n");
17392 return -EIO;
17393 }
17394
c0cc8a55
CW
17395 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
17396 return 0;
17397
28d52043
DA
17398 if (state)
17399 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
17400 else
17401 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
17402
17403 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
17404 DRM_ERROR("failed to write control word\n");
17405 return -EIO;
17406 }
17407
28d52043
DA
17408 return 0;
17409}
c4a1d9e4 17410
98a2f411
CW
17411#if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
17412
c4a1d9e4 17413struct intel_display_error_state {
ff57f1b0
PZ
17414
17415 u32 power_well_driver;
17416
63b66e5b
CW
17417 int num_transcoders;
17418
c4a1d9e4
CW
17419 struct intel_cursor_error_state {
17420 u32 control;
17421 u32 position;
17422 u32 base;
17423 u32 size;
52331309 17424 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
17425
17426 struct intel_pipe_error_state {
ddf9c536 17427 bool power_domain_on;
c4a1d9e4 17428 u32 source;
f301b1e1 17429 u32 stat;
52331309 17430 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
17431
17432 struct intel_plane_error_state {
17433 u32 control;
17434 u32 stride;
17435 u32 size;
17436 u32 pos;
17437 u32 addr;
17438 u32 surface;
17439 u32 tile_offset;
52331309 17440 } plane[I915_MAX_PIPES];
63b66e5b
CW
17441
17442 struct intel_transcoder_error_state {
ddf9c536 17443 bool power_domain_on;
63b66e5b
CW
17444 enum transcoder cpu_transcoder;
17445
17446 u32 conf;
17447
17448 u32 htotal;
17449 u32 hblank;
17450 u32 hsync;
17451 u32 vtotal;
17452 u32 vblank;
17453 u32 vsync;
17454 } transcoder[4];
c4a1d9e4
CW
17455};
17456
17457struct intel_display_error_state *
c033666a 17458intel_display_capture_error_state(struct drm_i915_private *dev_priv)
c4a1d9e4 17459{
c4a1d9e4 17460 struct intel_display_error_state *error;
63b66e5b
CW
17461 int transcoders[] = {
17462 TRANSCODER_A,
17463 TRANSCODER_B,
17464 TRANSCODER_C,
17465 TRANSCODER_EDP,
17466 };
c4a1d9e4
CW
17467 int i;
17468
c033666a 17469 if (INTEL_INFO(dev_priv)->num_pipes == 0)
63b66e5b
CW
17470 return NULL;
17471
9d1cb914 17472 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
17473 if (error == NULL)
17474 return NULL;
17475
c033666a 17476 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
ff57f1b0
PZ
17477 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
17478
055e393f 17479 for_each_pipe(dev_priv, i) {
ddf9c536 17480 error->pipe[i].power_domain_on =
f458ebbc
DV
17481 __intel_display_power_is_enabled(dev_priv,
17482 POWER_DOMAIN_PIPE(i));
ddf9c536 17483 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
17484 continue;
17485
5efb3e28
VS
17486 error->cursor[i].control = I915_READ(CURCNTR(i));
17487 error->cursor[i].position = I915_READ(CURPOS(i));
17488 error->cursor[i].base = I915_READ(CURBASE(i));
c4a1d9e4
CW
17489
17490 error->plane[i].control = I915_READ(DSPCNTR(i));
17491 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
c033666a 17492 if (INTEL_GEN(dev_priv) <= 3) {
51889b35 17493 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
17494 error->plane[i].pos = I915_READ(DSPPOS(i));
17495 }
c033666a 17496 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
ca291363 17497 error->plane[i].addr = I915_READ(DSPADDR(i));
c033666a 17498 if (INTEL_GEN(dev_priv) >= 4) {
c4a1d9e4
CW
17499 error->plane[i].surface = I915_READ(DSPSURF(i));
17500 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
17501 }
17502
c4a1d9e4 17503 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1 17504
c033666a 17505 if (HAS_GMCH_DISPLAY(dev_priv))
f301b1e1 17506 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
17507 }
17508
4d1de975 17509 /* Note: this does not include DSI transcoders. */
c033666a 17510 error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
2d1fe073 17511 if (HAS_DDI(dev_priv))
63b66e5b
CW
17512 error->num_transcoders++; /* Account for eDP. */
17513
17514 for (i = 0; i < error->num_transcoders; i++) {
17515 enum transcoder cpu_transcoder = transcoders[i];
17516
ddf9c536 17517 error->transcoder[i].power_domain_on =
f458ebbc 17518 __intel_display_power_is_enabled(dev_priv,
38cc1daf 17519 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 17520 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
17521 continue;
17522
63b66e5b
CW
17523 error->transcoder[i].cpu_transcoder = cpu_transcoder;
17524
17525 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
17526 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
17527 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
17528 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
17529 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
17530 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
17531 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
17532 }
17533
17534 return error;
17535}
17536
edc3d884
MK
17537#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
17538
c4a1d9e4 17539void
edc3d884 17540intel_display_print_error_state(struct drm_i915_error_state_buf *m,
5f56d5f9 17541 struct drm_i915_private *dev_priv,
c4a1d9e4
CW
17542 struct intel_display_error_state *error)
17543{
17544 int i;
17545
63b66e5b
CW
17546 if (!error)
17547 return;
17548
b7f05d4a 17549 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev_priv)->num_pipes);
8652744b 17550 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
edc3d884 17551 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 17552 error->power_well_driver);
055e393f 17553 for_each_pipe(dev_priv, i) {
edc3d884 17554 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536 17555 err_printf(m, " Power: %s\n",
87ad3212 17556 onoff(error->pipe[i].power_domain_on));
edc3d884 17557 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 17558 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
17559
17560 err_printf(m, "Plane [%d]:\n", i);
17561 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
17562 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
5f56d5f9 17563 if (INTEL_GEN(dev_priv) <= 3) {
edc3d884
MK
17564 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
17565 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 17566 }
772c2a51 17567 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
edc3d884 17568 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
5f56d5f9 17569 if (INTEL_GEN(dev_priv) >= 4) {
edc3d884
MK
17570 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
17571 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
17572 }
17573
edc3d884
MK
17574 err_printf(m, "Cursor [%d]:\n", i);
17575 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
17576 err_printf(m, " POS: %08x\n", error->cursor[i].position);
17577 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 17578 }
63b66e5b
CW
17579
17580 for (i = 0; i < error->num_transcoders; i++) {
da205630 17581 err_printf(m, "CPU transcoder: %s\n",
63b66e5b 17582 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536 17583 err_printf(m, " Power: %s\n",
87ad3212 17584 onoff(error->transcoder[i].power_domain_on));
63b66e5b
CW
17585 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
17586 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
17587 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
17588 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
17589 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
17590 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
17591 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
17592 }
c4a1d9e4 17593}
98a2f411
CW
17594
17595#endif