]> Git Repo - linux.git/blame - drivers/gpu/drm/i915/intel_display.c
drm/i915: STOP_MACHINE is no more, stop selecting it
[linux.git] / drivers / gpu / drm / i915 / intel_display.c
CommitLineData
79e53945
JB
1/*
2 * Copyright © 2006-2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <[email protected]>
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"
c37efb99 40#include "i915_gem_dmabuf.h"
db18b6a6 41#include "intel_dsi.h"
e5510fac 42#include "i915_trace.h"
319c1d42 43#include <drm/drm_atomic.h>
c196e1d6 44#include <drm/drm_atomic_helper.h>
760285e7
DH
45#include <drm/drm_dp_helper.h>
46#include <drm/drm_crtc_helper.h>
465c120c
MR
47#include <drm/drm_plane_helper.h>
48#include <drm/drm_rect.h>
c0f372b3 49#include <linux/dma_remapping.h>
fd8e058a 50#include <linux/reservation.h>
79e53945 51
5a21b665
SV
52static bool is_mmio_work(struct intel_flip_work *work)
53{
54 return work->mmio_work.func;
55}
56
465c120c 57/* Primary plane formats for gen <= 3 */
568db4f2 58static const uint32_t i8xx_primary_formats[] = {
67fe7dc5
DL
59 DRM_FORMAT_C8,
60 DRM_FORMAT_RGB565,
465c120c 61 DRM_FORMAT_XRGB1555,
67fe7dc5 62 DRM_FORMAT_XRGB8888,
465c120c
MR
63};
64
65/* Primary plane formats for gen >= 4 */
568db4f2 66static const uint32_t i965_primary_formats[] = {
6c0fd451
DL
67 DRM_FORMAT_C8,
68 DRM_FORMAT_RGB565,
69 DRM_FORMAT_XRGB8888,
70 DRM_FORMAT_XBGR8888,
71 DRM_FORMAT_XRGB2101010,
72 DRM_FORMAT_XBGR2101010,
73};
74
75static const uint32_t skl_primary_formats[] = {
67fe7dc5
DL
76 DRM_FORMAT_C8,
77 DRM_FORMAT_RGB565,
78 DRM_FORMAT_XRGB8888,
465c120c 79 DRM_FORMAT_XBGR8888,
67fe7dc5 80 DRM_FORMAT_ARGB8888,
465c120c
MR
81 DRM_FORMAT_ABGR8888,
82 DRM_FORMAT_XRGB2101010,
465c120c 83 DRM_FORMAT_XBGR2101010,
ea916ea0
KM
84 DRM_FORMAT_YUYV,
85 DRM_FORMAT_YVYU,
86 DRM_FORMAT_UYVY,
87 DRM_FORMAT_VYUY,
465c120c
MR
88};
89
3d7d6510
MR
90/* Cursor formats */
91static const uint32_t intel_cursor_formats[] = {
92 DRM_FORMAT_ARGB8888,
93};
94
f1f644dc 95static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 96 struct intel_crtc_state *pipe_config);
18442d08 97static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 98 struct intel_crtc_state *pipe_config);
f1f644dc 99
eb1bfe80
JB
100static int intel_framebuffer_init(struct drm_device *dev,
101 struct intel_framebuffer *ifb,
102 struct drm_mode_fb_cmd2 *mode_cmd,
103 struct drm_i915_gem_object *obj);
5b18e57c
SV
104static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
105static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
bc58be60 106static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
29407aab 107static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
108 struct intel_link_m_n *m_n,
109 struct intel_link_m_n *m2_n2);
29407aab 110static void ironlake_set_pipeconf(struct drm_crtc *crtc);
229fca97 111static void haswell_set_pipeconf(struct drm_crtc *crtc);
391bf048 112static void haswell_set_pipemisc(struct drm_crtc *crtc);
d288f65f 113static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 114 const struct intel_crtc_state *pipe_config);
d288f65f 115static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 116 const struct intel_crtc_state *pipe_config);
5a21b665
SV
117static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
118static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
549e2bfb
CK
119static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
120 struct intel_crtc_state *crtc_state);
bfd16b2a
ML
121static void skylake_pfit_enable(struct intel_crtc *crtc);
122static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
123static void ironlake_pfit_enable(struct intel_crtc *crtc);
043e9bda 124static void intel_modeset_setup_hw_state(struct drm_device *dev);
2622a081 125static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
4e5ca60f 126static int ilk_max_pixel_rate(struct drm_atomic_state *state);
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
SV
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
SV
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
SV
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
TU
616 if (!IS_PINEVIEW(dev_priv) && !IS_VALLEYVIEW(dev_priv) &&
617 !IS_CHERRYVIEW(dev_priv) && !IS_BROXTON(dev_priv))
f01b7962
VS
618 if (clock->m1 <= clock->m2)
619 INTELPllInvalid("m1 <= m2\n");
620
e2d214ae
TU
621 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
622 !IS_BROXTON(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
SV
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
SV
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
20ddf665
VS
1011bool intel_crtc_active(struct drm_crtc *crtc)
1012{
1013 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1014
1015 /* Be paranoid as we can arrive here with only partial
1016 * state retrieved from the hardware during setup.
1017 *
241bfc38 1018 * We can ditch the adjusted_mode.crtc_clock check as soon
20ddf665
VS
1019 * as Haswell has gained clock readout/fastboot support.
1020 *
66e514c1 1021 * We can ditch the crtc->primary->fb check as soon as we can
20ddf665 1022 * properly reconstruct framebuffers.
c3d1f436
MR
1023 *
1024 * FIXME: The intel_crtc->active here should be switched to
1025 * crtc->state->active once we have proper CRTC states wired up
1026 * for atomic.
20ddf665 1027 */
c3d1f436 1028 return intel_crtc->active && crtc->primary->state->fb &&
6e3c9717 1029 intel_crtc->config->base.adjusted_mode.crtc_clock;
20ddf665
VS
1030}
1031
a5c961d1
PZ
1032enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1033 enum pipe pipe)
1034{
1035 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1036 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1037
6e3c9717 1038 return intel_crtc->config->cpu_transcoder;
a5c961d1
PZ
1039}
1040
fbf49ea2
VS
1041static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1042{
fac5e23e 1043 struct drm_i915_private *dev_priv = to_i915(dev);
f0f59a00 1044 i915_reg_t reg = PIPEDSL(pipe);
fbf49ea2
VS
1045 u32 line1, line2;
1046 u32 line_mask;
1047
5db94019 1048 if (IS_GEN2(dev_priv))
fbf49ea2
VS
1049 line_mask = DSL_LINEMASK_GEN2;
1050 else
1051 line_mask = DSL_LINEMASK_GEN3;
1052
1053 line1 = I915_READ(reg) & line_mask;
6adfb1ef 1054 msleep(5);
fbf49ea2
VS
1055 line2 = I915_READ(reg) & line_mask;
1056
1057 return line1 == line2;
1058}
1059
ab7ad7f6
KP
1060/*
1061 * intel_wait_for_pipe_off - wait for pipe to turn off
575f7ab7 1062 * @crtc: crtc whose pipe to wait for
9d0498a2
JB
1063 *
1064 * After disabling a pipe, we can't wait for vblank in the usual way,
1065 * spinning on the vblank interrupt status bit, since we won't actually
1066 * see an interrupt when the pipe is disabled.
1067 *
ab7ad7f6
KP
1068 * On Gen4 and above:
1069 * wait for the pipe register state bit to turn off
1070 *
1071 * Otherwise:
1072 * wait for the display line value to settle (it usually
1073 * ends up stopping at the start of the next frame).
58e10eb9 1074 *
9d0498a2 1075 */
575f7ab7 1076static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
9d0498a2 1077{
575f7ab7 1078 struct drm_device *dev = crtc->base.dev;
fac5e23e 1079 struct drm_i915_private *dev_priv = to_i915(dev);
6e3c9717 1080 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 1081 enum pipe pipe = crtc->pipe;
ab7ad7f6
KP
1082
1083 if (INTEL_INFO(dev)->gen >= 4) {
f0f59a00 1084 i915_reg_t reg = PIPECONF(cpu_transcoder);
ab7ad7f6
KP
1085
1086 /* Wait for the Pipe State to go off */
b8511f53
CW
1087 if (intel_wait_for_register(dev_priv,
1088 reg, I965_PIPECONF_ACTIVE, 0,
1089 100))
284637d9 1090 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1091 } else {
ab7ad7f6 1092 /* Wait for the display line to settle */
fbf49ea2 1093 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
284637d9 1094 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1095 }
79e53945
JB
1096}
1097
b24e7179 1098/* Only for pre-ILK configs */
55607e8a
SV
1099void assert_pll(struct drm_i915_private *dev_priv,
1100 enum pipe pipe, bool state)
b24e7179 1101{
b24e7179
JB
1102 u32 val;
1103 bool cur_state;
1104
649636ef 1105 val = I915_READ(DPLL(pipe));
b24e7179 1106 cur_state = !!(val & DPLL_VCO_ENABLE);
e2c719b7 1107 I915_STATE_WARN(cur_state != state,
b24e7179 1108 "PLL state assertion failure (expected %s, current %s)\n",
87ad3212 1109 onoff(state), onoff(cur_state));
b24e7179 1110}
b24e7179 1111
23538ef1 1112/* XXX: the dsi pll is shared between MIPI DSI ports */
8563b1e8 1113void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
23538ef1
JN
1114{
1115 u32 val;
1116 bool cur_state;
1117
a580516d 1118 mutex_lock(&dev_priv->sb_lock);
23538ef1 1119 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
a580516d 1120 mutex_unlock(&dev_priv->sb_lock);
23538ef1
JN
1121
1122 cur_state = val & DSI_PLL_VCO_EN;
e2c719b7 1123 I915_STATE_WARN(cur_state != state,
23538ef1 1124 "DSI PLL state assertion failure (expected %s, current %s)\n",
87ad3212 1125 onoff(state), onoff(cur_state));
23538ef1 1126}
23538ef1 1127
040484af
JB
1128static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1129 enum pipe pipe, bool state)
1130{
040484af 1131 bool cur_state;
ad80a810
PZ
1132 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1133 pipe);
040484af 1134
2d1fe073 1135 if (HAS_DDI(dev_priv)) {
affa9354 1136 /* DDI does not have a specific FDI_TX register */
649636ef 1137 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
ad80a810 1138 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
bf507ef7 1139 } else {
649636ef 1140 u32 val = I915_READ(FDI_TX_CTL(pipe));
bf507ef7
ED
1141 cur_state = !!(val & FDI_TX_ENABLE);
1142 }
e2c719b7 1143 I915_STATE_WARN(cur_state != state,
040484af 1144 "FDI TX state assertion failure (expected %s, current %s)\n",
87ad3212 1145 onoff(state), onoff(cur_state));
040484af
JB
1146}
1147#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1148#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1149
1150static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1151 enum pipe pipe, bool state)
1152{
040484af
JB
1153 u32 val;
1154 bool cur_state;
1155
649636ef 1156 val = I915_READ(FDI_RX_CTL(pipe));
d63fa0dc 1157 cur_state = !!(val & FDI_RX_ENABLE);
e2c719b7 1158 I915_STATE_WARN(cur_state != state,
040484af 1159 "FDI RX state assertion failure (expected %s, current %s)\n",
87ad3212 1160 onoff(state), onoff(cur_state));
040484af
JB
1161}
1162#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1163#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1164
1165static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1166 enum pipe pipe)
1167{
040484af
JB
1168 u32 val;
1169
1170 /* ILK FDI PLL is always enabled */
7e22dbbb 1171 if (IS_GEN5(dev_priv))
040484af
JB
1172 return;
1173
bf507ef7 1174 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
2d1fe073 1175 if (HAS_DDI(dev_priv))
bf507ef7
ED
1176 return;
1177
649636ef 1178 val = I915_READ(FDI_TX_CTL(pipe));
e2c719b7 1179 I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
040484af
JB
1180}
1181
55607e8a
SV
1182void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1183 enum pipe pipe, bool state)
040484af 1184{
040484af 1185 u32 val;
55607e8a 1186 bool cur_state;
040484af 1187
649636ef 1188 val = I915_READ(FDI_RX_CTL(pipe));
55607e8a 1189 cur_state = !!(val & FDI_RX_PLL_ENABLE);
e2c719b7 1190 I915_STATE_WARN(cur_state != state,
55607e8a 1191 "FDI RX PLL assertion failure (expected %s, current %s)\n",
87ad3212 1192 onoff(state), onoff(cur_state));
040484af
JB
1193}
1194
4f8036a2 1195void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
ea0760cf 1196{
f0f59a00 1197 i915_reg_t pp_reg;
ea0760cf
JB
1198 u32 val;
1199 enum pipe panel_pipe = PIPE_A;
0de3b485 1200 bool locked = true;
ea0760cf 1201
4f8036a2 1202 if (WARN_ON(HAS_DDI(dev_priv)))
bedd4dba
JN
1203 return;
1204
4f8036a2 1205 if (HAS_PCH_SPLIT(dev_priv)) {
bedd4dba
JN
1206 u32 port_sel;
1207
44cb734c
ID
1208 pp_reg = PP_CONTROL(0);
1209 port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
bedd4dba
JN
1210
1211 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1212 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1213 panel_pipe = PIPE_B;
1214 /* XXX: else fix for eDP */
4f8036a2 1215 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
bedd4dba 1216 /* presumably write lock depends on pipe, not port select */
44cb734c 1217 pp_reg = PP_CONTROL(pipe);
bedd4dba 1218 panel_pipe = pipe;
ea0760cf 1219 } else {
44cb734c 1220 pp_reg = PP_CONTROL(0);
bedd4dba
JN
1221 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1222 panel_pipe = PIPE_B;
ea0760cf
JB
1223 }
1224
1225 val = I915_READ(pp_reg);
1226 if (!(val & PANEL_POWER_ON) ||
ec49ba2d 1227 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
ea0760cf
JB
1228 locked = false;
1229
e2c719b7 1230 I915_STATE_WARN(panel_pipe == pipe && locked,
ea0760cf 1231 "panel assertion failure, pipe %c regs locked\n",
9db4a9c7 1232 pipe_name(pipe));
ea0760cf
JB
1233}
1234
93ce0ba6
JN
1235static void assert_cursor(struct drm_i915_private *dev_priv,
1236 enum pipe pipe, bool state)
1237{
93ce0ba6
JN
1238 bool cur_state;
1239
50a0bc90 1240 if (IS_845G(dev_priv) || IS_I865G(dev_priv))
0b87c24e 1241 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
d9d82081 1242 else
5efb3e28 1243 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
93ce0ba6 1244
e2c719b7 1245 I915_STATE_WARN(cur_state != state,
93ce0ba6 1246 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
87ad3212 1247 pipe_name(pipe), onoff(state), onoff(cur_state));
93ce0ba6
JN
1248}
1249#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1250#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1251
b840d907
JB
1252void assert_pipe(struct drm_i915_private *dev_priv,
1253 enum pipe pipe, bool state)
b24e7179 1254{
63d7bbe9 1255 bool cur_state;
702e7a56
PZ
1256 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1257 pipe);
4feed0eb 1258 enum intel_display_power_domain power_domain;
b24e7179 1259
b6b5d049
VS
1260 /* if we need the pipe quirk it must be always on */
1261 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1262 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
8e636784
SV
1263 state = true;
1264
4feed0eb
ID
1265 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1266 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
649636ef 1267 u32 val = I915_READ(PIPECONF(cpu_transcoder));
69310161 1268 cur_state = !!(val & PIPECONF_ENABLE);
4feed0eb
ID
1269
1270 intel_display_power_put(dev_priv, power_domain);
1271 } else {
1272 cur_state = false;
69310161
PZ
1273 }
1274
e2c719b7 1275 I915_STATE_WARN(cur_state != state,
63d7bbe9 1276 "pipe %c assertion failure (expected %s, current %s)\n",
87ad3212 1277 pipe_name(pipe), onoff(state), onoff(cur_state));
b24e7179
JB
1278}
1279
931872fc
CW
1280static void assert_plane(struct drm_i915_private *dev_priv,
1281 enum plane plane, bool state)
b24e7179 1282{
b24e7179 1283 u32 val;
931872fc 1284 bool cur_state;
b24e7179 1285
649636ef 1286 val = I915_READ(DSPCNTR(plane));
931872fc 1287 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
e2c719b7 1288 I915_STATE_WARN(cur_state != state,
931872fc 1289 "plane %c assertion failure (expected %s, current %s)\n",
87ad3212 1290 plane_name(plane), onoff(state), onoff(cur_state));
b24e7179
JB
1291}
1292
931872fc
CW
1293#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1294#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1295
b24e7179
JB
1296static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1297 enum pipe pipe)
1298{
91c8a326 1299 struct drm_device *dev = &dev_priv->drm;
649636ef 1300 int i;
b24e7179 1301
653e1026
VS
1302 /* Primary planes are fixed to pipes on gen4+ */
1303 if (INTEL_INFO(dev)->gen >= 4) {
649636ef 1304 u32 val = I915_READ(DSPCNTR(pipe));
e2c719b7 1305 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
28c05794
AJ
1306 "plane %c assertion failure, should be disabled but not\n",
1307 plane_name(pipe));
19ec1358 1308 return;
28c05794 1309 }
19ec1358 1310
b24e7179 1311 /* Need to check both planes against the pipe */
055e393f 1312 for_each_pipe(dev_priv, i) {
649636ef
VS
1313 u32 val = I915_READ(DSPCNTR(i));
1314 enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
b24e7179 1315 DISPPLANE_SEL_PIPE_SHIFT;
e2c719b7 1316 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
9db4a9c7
JB
1317 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1318 plane_name(i), pipe_name(pipe));
b24e7179
JB
1319 }
1320}
1321
19332d7a
JB
1322static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1323 enum pipe pipe)
1324{
91c8a326 1325 struct drm_device *dev = &dev_priv->drm;
649636ef 1326 int sprite;
19332d7a 1327
7feb8b88 1328 if (INTEL_INFO(dev)->gen >= 9) {
3bdcfc0c 1329 for_each_sprite(dev_priv, pipe, sprite) {
649636ef 1330 u32 val = I915_READ(PLANE_CTL(pipe, sprite));
e2c719b7 1331 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
7feb8b88
DL
1332 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1333 sprite, pipe_name(pipe));
1334 }
920a14b2 1335 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
3bdcfc0c 1336 for_each_sprite(dev_priv, pipe, sprite) {
649636ef 1337 u32 val = I915_READ(SPCNTR(pipe, sprite));
e2c719b7 1338 I915_STATE_WARN(val & SP_ENABLE,
20674eef 1339 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1fe47785 1340 sprite_name(pipe, sprite), pipe_name(pipe));
20674eef
VS
1341 }
1342 } else if (INTEL_INFO(dev)->gen >= 7) {
649636ef 1343 u32 val = I915_READ(SPRCTL(pipe));
e2c719b7 1344 I915_STATE_WARN(val & SPRITE_ENABLE,
06da8da2 1345 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef
VS
1346 plane_name(pipe), pipe_name(pipe));
1347 } else if (INTEL_INFO(dev)->gen >= 5) {
649636ef 1348 u32 val = I915_READ(DVSCNTR(pipe));
e2c719b7 1349 I915_STATE_WARN(val & DVS_ENABLE,
06da8da2 1350 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef 1351 plane_name(pipe), pipe_name(pipe));
19332d7a
JB
1352 }
1353}
1354
08c71e5e
VS
1355static void assert_vblank_disabled(struct drm_crtc *crtc)
1356{
e2c719b7 1357 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
08c71e5e
VS
1358 drm_crtc_vblank_put(crtc);
1359}
1360
7abd4b35
ACO
1361void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1362 enum pipe pipe)
92f2584a 1363{
92f2584a
JB
1364 u32 val;
1365 bool enabled;
1366
649636ef 1367 val = I915_READ(PCH_TRANSCONF(pipe));
92f2584a 1368 enabled = !!(val & TRANS_ENABLE);
e2c719b7 1369 I915_STATE_WARN(enabled,
9db4a9c7
JB
1370 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1371 pipe_name(pipe));
92f2584a
JB
1372}
1373
4e634389
KP
1374static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1375 enum pipe pipe, u32 port_sel, u32 val)
f0575e92
KP
1376{
1377 if ((val & DP_PORT_EN) == 0)
1378 return false;
1379
2d1fe073 1380 if (HAS_PCH_CPT(dev_priv)) {
f0f59a00 1381 u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
f0575e92
KP
1382 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1383 return false;
2d1fe073 1384 } else if (IS_CHERRYVIEW(dev_priv)) {
44f37d1f
CML
1385 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1386 return false;
f0575e92
KP
1387 } else {
1388 if ((val & DP_PIPE_MASK) != (pipe << 30))
1389 return false;
1390 }
1391 return true;
1392}
1393
1519b995
KP
1394static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1395 enum pipe pipe, u32 val)
1396{
dc0fa718 1397 if ((val & SDVO_ENABLE) == 0)
1519b995
KP
1398 return false;
1399
2d1fe073 1400 if (HAS_PCH_CPT(dev_priv)) {
dc0fa718 1401 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1519b995 1402 return false;
2d1fe073 1403 } else if (IS_CHERRYVIEW(dev_priv)) {
44f37d1f
CML
1404 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1405 return false;
1519b995 1406 } else {
dc0fa718 1407 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1519b995
KP
1408 return false;
1409 }
1410 return true;
1411}
1412
1413static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1414 enum pipe pipe, u32 val)
1415{
1416 if ((val & LVDS_PORT_EN) == 0)
1417 return false;
1418
2d1fe073 1419 if (HAS_PCH_CPT(dev_priv)) {
1519b995
KP
1420 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1421 return false;
1422 } else {
1423 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1424 return false;
1425 }
1426 return true;
1427}
1428
1429static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1430 enum pipe pipe, u32 val)
1431{
1432 if ((val & ADPA_DAC_ENABLE) == 0)
1433 return false;
2d1fe073 1434 if (HAS_PCH_CPT(dev_priv)) {
1519b995
KP
1435 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1436 return false;
1437 } else {
1438 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1439 return false;
1440 }
1441 return true;
1442}
1443
291906f1 1444static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
f0f59a00
VS
1445 enum pipe pipe, i915_reg_t reg,
1446 u32 port_sel)
291906f1 1447{
47a05eca 1448 u32 val = I915_READ(reg);
e2c719b7 1449 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
291906f1 1450 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
f0f59a00 1451 i915_mmio_reg_offset(reg), pipe_name(pipe));
de9a35ab 1452
2d1fe073 1453 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
75c5da27 1454 && (val & DP_PIPEB_SELECT),
de9a35ab 1455 "IBX PCH dp port still using transcoder B\n");
291906f1
JB
1456}
1457
1458static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
f0f59a00 1459 enum pipe pipe, i915_reg_t reg)
291906f1 1460{
47a05eca 1461 u32 val = I915_READ(reg);
e2c719b7 1462 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
23c99e77 1463 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
f0f59a00 1464 i915_mmio_reg_offset(reg), pipe_name(pipe));
de9a35ab 1465
2d1fe073 1466 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
75c5da27 1467 && (val & SDVO_PIPE_B_SELECT),
de9a35ab 1468 "IBX PCH hdmi port still using transcoder B\n");
291906f1
JB
1469}
1470
1471static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1472 enum pipe pipe)
1473{
291906f1 1474 u32 val;
291906f1 1475
f0575e92
KP
1476 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1477 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1478 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
291906f1 1479
649636ef 1480 val = I915_READ(PCH_ADPA);
e2c719b7 1481 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
291906f1 1482 "PCH VGA enabled on transcoder %c, should be disabled\n",
9db4a9c7 1483 pipe_name(pipe));
291906f1 1484
649636ef 1485 val = I915_READ(PCH_LVDS);
e2c719b7 1486 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
291906f1 1487 "PCH LVDS enabled on transcoder %c, should be disabled\n",
9db4a9c7 1488 pipe_name(pipe));
291906f1 1489
e2debe91
PZ
1490 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1491 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1492 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
291906f1
JB
1493}
1494
cd2d34d9
VS
1495static void _vlv_enable_pll(struct intel_crtc *crtc,
1496 const struct intel_crtc_state *pipe_config)
1497{
1498 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1499 enum pipe pipe = crtc->pipe;
1500
1501 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1502 POSTING_READ(DPLL(pipe));
1503 udelay(150);
1504
2c30b43b
CW
1505 if (intel_wait_for_register(dev_priv,
1506 DPLL(pipe),
1507 DPLL_LOCK_VLV,
1508 DPLL_LOCK_VLV,
1509 1))
cd2d34d9
VS
1510 DRM_ERROR("DPLL %d failed to lock\n", pipe);
1511}
1512
d288f65f 1513static void vlv_enable_pll(struct intel_crtc *crtc,
5cec258b 1514 const struct intel_crtc_state *pipe_config)
87442f73 1515{
cd2d34d9 1516 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8bd3f301 1517 enum pipe pipe = crtc->pipe;
87442f73 1518
8bd3f301 1519 assert_pipe_disabled(dev_priv, pipe);
87442f73 1520
87442f73 1521 /* PLL is protected by panel, make sure we can write it */
7d1a83cb 1522 assert_panel_unlocked(dev_priv, pipe);
87442f73 1523
cd2d34d9
VS
1524 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1525 _vlv_enable_pll(crtc, pipe_config);
426115cf 1526
8bd3f301
VS
1527 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1528 POSTING_READ(DPLL_MD(pipe));
87442f73
SV
1529}
1530
cd2d34d9
VS
1531
1532static void _chv_enable_pll(struct intel_crtc *crtc,
1533 const struct intel_crtc_state *pipe_config)
9d556c99 1534{
cd2d34d9 1535 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8bd3f301 1536 enum pipe pipe = crtc->pipe;
9d556c99 1537 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9d556c99
CML
1538 u32 tmp;
1539
a580516d 1540 mutex_lock(&dev_priv->sb_lock);
9d556c99
CML
1541
1542 /* Enable back the 10bit clock to display controller */
1543 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1544 tmp |= DPIO_DCLKP_EN;
1545 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1546
54433e91
VS
1547 mutex_unlock(&dev_priv->sb_lock);
1548
9d556c99
CML
1549 /*
1550 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1551 */
1552 udelay(1);
1553
1554 /* Enable PLL */
d288f65f 1555 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
9d556c99
CML
1556
1557 /* Check PLL is locked */
6b18826a
CW
1558 if (intel_wait_for_register(dev_priv,
1559 DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
1560 1))
9d556c99 1561 DRM_ERROR("PLL %d failed to lock\n", pipe);
cd2d34d9
VS
1562}
1563
1564static void chv_enable_pll(struct intel_crtc *crtc,
1565 const struct intel_crtc_state *pipe_config)
1566{
1567 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1568 enum pipe pipe = crtc->pipe;
1569
1570 assert_pipe_disabled(dev_priv, pipe);
1571
1572 /* PLL is protected by panel, make sure we can write it */
1573 assert_panel_unlocked(dev_priv, pipe);
1574
1575 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1576 _chv_enable_pll(crtc, pipe_config);
9d556c99 1577
c231775c
VS
1578 if (pipe != PIPE_A) {
1579 /*
1580 * WaPixelRepeatModeFixForC0:chv
1581 *
1582 * DPLLCMD is AWOL. Use chicken bits to propagate
1583 * the value from DPLLBMD to either pipe B or C.
1584 */
1585 I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
1586 I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
1587 I915_WRITE(CBR4_VLV, 0);
1588 dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
1589
1590 /*
1591 * DPLLB VGA mode also seems to cause problems.
1592 * We should always have it disabled.
1593 */
1594 WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
1595 } else {
1596 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1597 POSTING_READ(DPLL_MD(pipe));
1598 }
9d556c99
CML
1599}
1600
1c4e0274
VS
1601static int intel_num_dvo_pipes(struct drm_device *dev)
1602{
1603 struct intel_crtc *crtc;
1604 int count = 0;
1605
2d84d2b3 1606 for_each_intel_crtc(dev, crtc) {
3538b9df 1607 count += crtc->base.state->active &&
2d84d2b3
VS
1608 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
1609 }
1c4e0274
VS
1610
1611 return count;
1612}
1613
66e3d5c0 1614static void i9xx_enable_pll(struct intel_crtc *crtc)
63d7bbe9 1615{
66e3d5c0 1616 struct drm_device *dev = crtc->base.dev;
fac5e23e 1617 struct drm_i915_private *dev_priv = to_i915(dev);
f0f59a00 1618 i915_reg_t reg = DPLL(crtc->pipe);
6e3c9717 1619 u32 dpll = crtc->config->dpll_hw_state.dpll;
63d7bbe9 1620
66e3d5c0 1621 assert_pipe_disabled(dev_priv, crtc->pipe);
58c6eaa2 1622
63d7bbe9 1623 /* PLL is protected by panel, make sure we can write it */
50a0bc90 1624 if (IS_MOBILE(dev_priv) && !IS_I830(dev_priv))
66e3d5c0 1625 assert_panel_unlocked(dev_priv, crtc->pipe);
63d7bbe9 1626
1c4e0274 1627 /* Enable DVO 2x clock on both PLLs if necessary */
50a0bc90 1628 if (IS_I830(dev_priv) && intel_num_dvo_pipes(dev) > 0) {
1c4e0274
VS
1629 /*
1630 * It appears to be important that we don't enable this
1631 * for the current pipe before otherwise configuring the
1632 * PLL. No idea how this should be handled if multiple
1633 * DVO outputs are enabled simultaneosly.
1634 */
1635 dpll |= DPLL_DVO_2X_MODE;
1636 I915_WRITE(DPLL(!crtc->pipe),
1637 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1638 }
66e3d5c0 1639
c2b63374
VS
1640 /*
1641 * Apparently we need to have VGA mode enabled prior to changing
1642 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1643 * dividers, even though the register value does change.
1644 */
1645 I915_WRITE(reg, 0);
1646
8e7a65aa
VS
1647 I915_WRITE(reg, dpll);
1648
66e3d5c0
SV
1649 /* Wait for the clocks to stabilize. */
1650 POSTING_READ(reg);
1651 udelay(150);
1652
1653 if (INTEL_INFO(dev)->gen >= 4) {
1654 I915_WRITE(DPLL_MD(crtc->pipe),
6e3c9717 1655 crtc->config->dpll_hw_state.dpll_md);
66e3d5c0
SV
1656 } else {
1657 /* The pixel multiplier can only be updated once the
1658 * DPLL is enabled and the clocks are stable.
1659 *
1660 * So write it again.
1661 */
1662 I915_WRITE(reg, dpll);
1663 }
63d7bbe9
JB
1664
1665 /* We do this three times for luck */
66e3d5c0 1666 I915_WRITE(reg, dpll);
63d7bbe9
JB
1667 POSTING_READ(reg);
1668 udelay(150); /* wait for warmup */
66e3d5c0 1669 I915_WRITE(reg, dpll);
63d7bbe9
JB
1670 POSTING_READ(reg);
1671 udelay(150); /* wait for warmup */
66e3d5c0 1672 I915_WRITE(reg, dpll);
63d7bbe9
JB
1673 POSTING_READ(reg);
1674 udelay(150); /* wait for warmup */
1675}
1676
1677/**
50b44a44 1678 * i9xx_disable_pll - disable a PLL
63d7bbe9
JB
1679 * @dev_priv: i915 private structure
1680 * @pipe: pipe PLL to disable
1681 *
1682 * Disable the PLL for @pipe, making sure the pipe is off first.
1683 *
1684 * Note! This is for pre-ILK only.
1685 */
1c4e0274 1686static void i9xx_disable_pll(struct intel_crtc *crtc)
63d7bbe9 1687{
1c4e0274 1688 struct drm_device *dev = crtc->base.dev;
fac5e23e 1689 struct drm_i915_private *dev_priv = to_i915(dev);
1c4e0274
VS
1690 enum pipe pipe = crtc->pipe;
1691
1692 /* Disable DVO 2x clock on both PLLs if necessary */
50a0bc90 1693 if (IS_I830(dev_priv) &&
2d84d2b3 1694 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
3538b9df 1695 !intel_num_dvo_pipes(dev)) {
1c4e0274
VS
1696 I915_WRITE(DPLL(PIPE_B),
1697 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1698 I915_WRITE(DPLL(PIPE_A),
1699 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1700 }
1701
b6b5d049
VS
1702 /* Don't disable pipe or pipe PLLs if needed */
1703 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1704 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
63d7bbe9
JB
1705 return;
1706
1707 /* Make sure the pipe isn't still relying on us */
1708 assert_pipe_disabled(dev_priv, pipe);
1709
b8afb911 1710 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
50b44a44 1711 POSTING_READ(DPLL(pipe));
63d7bbe9
JB
1712}
1713
f6071166
JB
1714static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1715{
b8afb911 1716 u32 val;
f6071166
JB
1717
1718 /* Make sure the pipe isn't still relying on us */
1719 assert_pipe_disabled(dev_priv, pipe);
1720
03ed5cbf
VS
1721 val = DPLL_INTEGRATED_REF_CLK_VLV |
1722 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1723 if (pipe != PIPE_A)
1724 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1725
f6071166
JB
1726 I915_WRITE(DPLL(pipe), val);
1727 POSTING_READ(DPLL(pipe));
076ed3b2
CML
1728}
1729
1730static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1731{
d752048d 1732 enum dpio_channel port = vlv_pipe_to_channel(pipe);
076ed3b2
CML
1733 u32 val;
1734
a11b0703
VS
1735 /* Make sure the pipe isn't still relying on us */
1736 assert_pipe_disabled(dev_priv, pipe);
076ed3b2 1737
60bfe44f
VS
1738 val = DPLL_SSC_REF_CLK_CHV |
1739 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
a11b0703
VS
1740 if (pipe != PIPE_A)
1741 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
03ed5cbf 1742
a11b0703
VS
1743 I915_WRITE(DPLL(pipe), val);
1744 POSTING_READ(DPLL(pipe));
d752048d 1745
a580516d 1746 mutex_lock(&dev_priv->sb_lock);
d752048d
VS
1747
1748 /* Disable 10bit clock to display controller */
1749 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1750 val &= ~DPIO_DCLKP_EN;
1751 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1752
a580516d 1753 mutex_unlock(&dev_priv->sb_lock);
f6071166
JB
1754}
1755
e4607fcf 1756void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
9b6de0a1
VS
1757 struct intel_digital_port *dport,
1758 unsigned int expected_mask)
89b667f8
JB
1759{
1760 u32 port_mask;
f0f59a00 1761 i915_reg_t dpll_reg;
89b667f8 1762
e4607fcf
CML
1763 switch (dport->port) {
1764 case PORT_B:
89b667f8 1765 port_mask = DPLL_PORTB_READY_MASK;
00fc31b7 1766 dpll_reg = DPLL(0);
e4607fcf
CML
1767 break;
1768 case PORT_C:
89b667f8 1769 port_mask = DPLL_PORTC_READY_MASK;
00fc31b7 1770 dpll_reg = DPLL(0);
9b6de0a1 1771 expected_mask <<= 4;
00fc31b7
CML
1772 break;
1773 case PORT_D:
1774 port_mask = DPLL_PORTD_READY_MASK;
1775 dpll_reg = DPIO_PHY_STATUS;
e4607fcf
CML
1776 break;
1777 default:
1778 BUG();
1779 }
89b667f8 1780
370004d3
CW
1781 if (intel_wait_for_register(dev_priv,
1782 dpll_reg, port_mask, expected_mask,
1783 1000))
9b6de0a1
VS
1784 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1785 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
89b667f8
JB
1786}
1787
b8a4f404
PZ
1788static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1789 enum pipe pipe)
040484af 1790{
7c26e5c6 1791 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
e2b78267 1792 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
f0f59a00
VS
1793 i915_reg_t reg;
1794 uint32_t val, pipeconf_val;
040484af 1795
040484af 1796 /* Make sure PCH DPLL is enabled */
8106ddbd 1797 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
040484af
JB
1798
1799 /* FDI must be feeding us bits for PCH ports */
1800 assert_fdi_tx_enabled(dev_priv, pipe);
1801 assert_fdi_rx_enabled(dev_priv, pipe);
1802
6e266956 1803 if (HAS_PCH_CPT(dev_priv)) {
23670b32
SV
1804 /* Workaround: Set the timing override bit before enabling the
1805 * pch transcoder. */
1806 reg = TRANS_CHICKEN2(pipe);
1807 val = I915_READ(reg);
1808 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1809 I915_WRITE(reg, val);
59c859d6 1810 }
23670b32 1811
ab9412ba 1812 reg = PCH_TRANSCONF(pipe);
040484af 1813 val = I915_READ(reg);
5f7f726d 1814 pipeconf_val = I915_READ(PIPECONF(pipe));
e9bcff5c 1815
2d1fe073 1816 if (HAS_PCH_IBX(dev_priv)) {
e9bcff5c 1817 /*
c5de7c6f
VS
1818 * Make the BPC in transcoder be consistent with
1819 * that in pipeconf reg. For HDMI we must use 8bpc
1820 * here for both 8bpc and 12bpc.
e9bcff5c 1821 */
dfd07d72 1822 val &= ~PIPECONF_BPC_MASK;
2d84d2b3 1823 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
c5de7c6f
VS
1824 val |= PIPECONF_8BPC;
1825 else
1826 val |= pipeconf_val & PIPECONF_BPC_MASK;
e9bcff5c 1827 }
5f7f726d
PZ
1828
1829 val &= ~TRANS_INTERLACE_MASK;
1830 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
2d1fe073 1831 if (HAS_PCH_IBX(dev_priv) &&
2d84d2b3 1832 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
7c26e5c6
PZ
1833 val |= TRANS_LEGACY_INTERLACED_ILK;
1834 else
1835 val |= TRANS_INTERLACED;
5f7f726d
PZ
1836 else
1837 val |= TRANS_PROGRESSIVE;
1838
040484af 1839 I915_WRITE(reg, val | TRANS_ENABLE);
650fbd84
CW
1840 if (intel_wait_for_register(dev_priv,
1841 reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
1842 100))
4bb6f1f3 1843 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
040484af
JB
1844}
1845
8fb033d7 1846static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
937bb610 1847 enum transcoder cpu_transcoder)
040484af 1848{
8fb033d7 1849 u32 val, pipeconf_val;
8fb033d7 1850
8fb033d7 1851 /* FDI must be feeding us bits for PCH ports */
1a240d4d 1852 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
937bb610 1853 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
8fb033d7 1854
223a6fdf 1855 /* Workaround: set timing override bit. */
36c0d0cf 1856 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
23670b32 1857 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
36c0d0cf 1858 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
223a6fdf 1859
25f3ef11 1860 val = TRANS_ENABLE;
937bb610 1861 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
8fb033d7 1862
9a76b1c6
PZ
1863 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1864 PIPECONF_INTERLACED_ILK)
a35f2679 1865 val |= TRANS_INTERLACED;
8fb033d7
PZ
1866 else
1867 val |= TRANS_PROGRESSIVE;
1868
ab9412ba 1869 I915_WRITE(LPT_TRANSCONF, val);
d9f96244
CW
1870 if (intel_wait_for_register(dev_priv,
1871 LPT_TRANSCONF,
1872 TRANS_STATE_ENABLE,
1873 TRANS_STATE_ENABLE,
1874 100))
937bb610 1875 DRM_ERROR("Failed to enable PCH transcoder\n");
8fb033d7
PZ
1876}
1877
b8a4f404
PZ
1878static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1879 enum pipe pipe)
040484af 1880{
f0f59a00
VS
1881 i915_reg_t reg;
1882 uint32_t val;
040484af
JB
1883
1884 /* FDI relies on the transcoder */
1885 assert_fdi_tx_disabled(dev_priv, pipe);
1886 assert_fdi_rx_disabled(dev_priv, pipe);
1887
291906f1
JB
1888 /* Ports must be off as well */
1889 assert_pch_ports_disabled(dev_priv, pipe);
1890
ab9412ba 1891 reg = PCH_TRANSCONF(pipe);
040484af
JB
1892 val = I915_READ(reg);
1893 val &= ~TRANS_ENABLE;
1894 I915_WRITE(reg, val);
1895 /* wait for PCH transcoder off, transcoder state */
a7d04662
CW
1896 if (intel_wait_for_register(dev_priv,
1897 reg, TRANS_STATE_ENABLE, 0,
1898 50))
4bb6f1f3 1899 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
23670b32 1900
6e266956 1901 if (HAS_PCH_CPT(dev_priv)) {
23670b32
SV
1902 /* Workaround: Clear the timing override chicken bit again. */
1903 reg = TRANS_CHICKEN2(pipe);
1904 val = I915_READ(reg);
1905 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1906 I915_WRITE(reg, val);
1907 }
040484af
JB
1908}
1909
b7076546 1910void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
8fb033d7 1911{
8fb033d7
PZ
1912 u32 val;
1913
ab9412ba 1914 val = I915_READ(LPT_TRANSCONF);
8fb033d7 1915 val &= ~TRANS_ENABLE;
ab9412ba 1916 I915_WRITE(LPT_TRANSCONF, val);
8fb033d7 1917 /* wait for PCH transcoder off, transcoder state */
dfdb4749
CW
1918 if (intel_wait_for_register(dev_priv,
1919 LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
1920 50))
8a52fd9f 1921 DRM_ERROR("Failed to disable PCH transcoder\n");
223a6fdf
PZ
1922
1923 /* Workaround: clear timing override bit. */
36c0d0cf 1924 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
23670b32 1925 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
36c0d0cf 1926 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
040484af
JB
1927}
1928
65f2130c
VS
1929enum transcoder intel_crtc_pch_transcoder(struct intel_crtc *crtc)
1930{
1931 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1932
1933 WARN_ON(!crtc->config->has_pch_encoder);
1934
1935 if (HAS_PCH_LPT(dev_priv))
1936 return TRANSCODER_A;
1937 else
1938 return (enum transcoder) crtc->pipe;
1939}
1940
b24e7179 1941/**
309cfea8 1942 * intel_enable_pipe - enable a pipe, asserting requirements
0372264a 1943 * @crtc: crtc responsible for the pipe
b24e7179 1944 *
0372264a 1945 * Enable @crtc's pipe, making sure that various hardware specific requirements
b24e7179 1946 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
b24e7179 1947 */
e1fdc473 1948static void intel_enable_pipe(struct intel_crtc *crtc)
b24e7179 1949{
0372264a 1950 struct drm_device *dev = crtc->base.dev;
fac5e23e 1951 struct drm_i915_private *dev_priv = to_i915(dev);
0372264a 1952 enum pipe pipe = crtc->pipe;
1a70a728 1953 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
f0f59a00 1954 i915_reg_t reg;
b24e7179
JB
1955 u32 val;
1956
9e2ee2dd
VS
1957 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1958
58c6eaa2 1959 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 1960 assert_cursor_disabled(dev_priv, pipe);
58c6eaa2
SV
1961 assert_sprites_disabled(dev_priv, pipe);
1962
b24e7179
JB
1963 /*
1964 * A pipe without a PLL won't actually be able to drive bits from
1965 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1966 * need the check.
1967 */
09fa8bb9 1968 if (HAS_GMCH_DISPLAY(dev_priv)) {
d7edc4e5 1969 if (intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DSI))
23538ef1
JN
1970 assert_dsi_pll_enabled(dev_priv);
1971 else
1972 assert_pll_enabled(dev_priv, pipe);
09fa8bb9 1973 } else {
6e3c9717 1974 if (crtc->config->has_pch_encoder) {
040484af 1975 /* if driving the PCH, we need FDI enabled */
65f2130c
VS
1976 assert_fdi_rx_pll_enabled(dev_priv,
1977 (enum pipe) intel_crtc_pch_transcoder(crtc));
1a240d4d
SV
1978 assert_fdi_tx_pll_enabled(dev_priv,
1979 (enum pipe) cpu_transcoder);
040484af
JB
1980 }
1981 /* FIXME: assert CPU port conditions for SNB+ */
1982 }
b24e7179 1983
702e7a56 1984 reg = PIPECONF(cpu_transcoder);
b24e7179 1985 val = I915_READ(reg);
7ad25d48 1986 if (val & PIPECONF_ENABLE) {
b6b5d049
VS
1987 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1988 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
00d70b15 1989 return;
7ad25d48 1990 }
00d70b15
CW
1991
1992 I915_WRITE(reg, val | PIPECONF_ENABLE);
851855d8 1993 POSTING_READ(reg);
b7792d8b
VS
1994
1995 /*
1996 * Until the pipe starts DSL will read as 0, which would cause
1997 * an apparent vblank timestamp jump, which messes up also the
1998 * frame count when it's derived from the timestamps. So let's
1999 * wait for the pipe to start properly before we call
2000 * drm_crtc_vblank_on()
2001 */
2002 if (dev->max_vblank_count == 0 &&
2003 wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
2004 DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
b24e7179
JB
2005}
2006
2007/**
309cfea8 2008 * intel_disable_pipe - disable a pipe, asserting requirements
575f7ab7 2009 * @crtc: crtc whose pipes is to be disabled
b24e7179 2010 *
575f7ab7
VS
2011 * Disable the pipe of @crtc, making sure that various hardware
2012 * specific requirements are met, if applicable, e.g. plane
2013 * disabled, panel fitter off, etc.
b24e7179
JB
2014 *
2015 * Will wait until the pipe has shut down before returning.
2016 */
575f7ab7 2017static void intel_disable_pipe(struct intel_crtc *crtc)
b24e7179 2018{
fac5e23e 2019 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6e3c9717 2020 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 2021 enum pipe pipe = crtc->pipe;
f0f59a00 2022 i915_reg_t reg;
b24e7179
JB
2023 u32 val;
2024
9e2ee2dd
VS
2025 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2026
b24e7179
JB
2027 /*
2028 * Make sure planes won't keep trying to pump pixels to us,
2029 * or we might hang the display.
2030 */
2031 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2032 assert_cursor_disabled(dev_priv, pipe);
19332d7a 2033 assert_sprites_disabled(dev_priv, pipe);
b24e7179 2034
702e7a56 2035 reg = PIPECONF(cpu_transcoder);
b24e7179 2036 val = I915_READ(reg);
00d70b15
CW
2037 if ((val & PIPECONF_ENABLE) == 0)
2038 return;
2039
67adc644
VS
2040 /*
2041 * Double wide has implications for planes
2042 * so best keep it disabled when not needed.
2043 */
6e3c9717 2044 if (crtc->config->double_wide)
67adc644
VS
2045 val &= ~PIPECONF_DOUBLE_WIDE;
2046
2047 /* Don't disable pipe or pipe PLLs if needed */
b6b5d049
VS
2048 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2049 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
67adc644
VS
2050 val &= ~PIPECONF_ENABLE;
2051
2052 I915_WRITE(reg, val);
2053 if ((val & PIPECONF_ENABLE) == 0)
2054 intel_wait_for_pipe_off(crtc);
b24e7179
JB
2055}
2056
832be82f
VS
2057static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
2058{
2059 return IS_GEN2(dev_priv) ? 2048 : 4096;
2060}
2061
27ba3910
VS
2062static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
2063 uint64_t fb_modifier, unsigned int cpp)
7b49f948
VS
2064{
2065 switch (fb_modifier) {
2066 case DRM_FORMAT_MOD_NONE:
2067 return cpp;
2068 case I915_FORMAT_MOD_X_TILED:
2069 if (IS_GEN2(dev_priv))
2070 return 128;
2071 else
2072 return 512;
2073 case I915_FORMAT_MOD_Y_TILED:
2074 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2075 return 128;
2076 else
2077 return 512;
2078 case I915_FORMAT_MOD_Yf_TILED:
2079 switch (cpp) {
2080 case 1:
2081 return 64;
2082 case 2:
2083 case 4:
2084 return 128;
2085 case 8:
2086 case 16:
2087 return 256;
2088 default:
2089 MISSING_CASE(cpp);
2090 return cpp;
2091 }
2092 break;
2093 default:
2094 MISSING_CASE(fb_modifier);
2095 return cpp;
2096 }
2097}
2098
832be82f
VS
2099unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
2100 uint64_t fb_modifier, unsigned int cpp)
a57ce0b2 2101{
832be82f
VS
2102 if (fb_modifier == DRM_FORMAT_MOD_NONE)
2103 return 1;
2104 else
2105 return intel_tile_size(dev_priv) /
27ba3910 2106 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
6761dd31
TU
2107}
2108
8d0deca8
VS
2109/* Return the tile dimensions in pixel units */
2110static void intel_tile_dims(const struct drm_i915_private *dev_priv,
2111 unsigned int *tile_width,
2112 unsigned int *tile_height,
2113 uint64_t fb_modifier,
2114 unsigned int cpp)
2115{
2116 unsigned int tile_width_bytes =
2117 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
2118
2119 *tile_width = tile_width_bytes / cpp;
2120 *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
2121}
2122
6761dd31
TU
2123unsigned int
2124intel_fb_align_height(struct drm_device *dev, unsigned int height,
832be82f 2125 uint32_t pixel_format, uint64_t fb_modifier)
6761dd31 2126{
832be82f
VS
2127 unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
2128 unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
2129
2130 return ALIGN(height, tile_height);
a57ce0b2
JB
2131}
2132
1663b9d6
VS
2133unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2134{
2135 unsigned int size = 0;
2136 int i;
2137
2138 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2139 size += rot_info->plane[i].width * rot_info->plane[i].height;
2140
2141 return size;
2142}
2143
75c82a53 2144static void
3465c580
VS
2145intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2146 const struct drm_framebuffer *fb,
2147 unsigned int rotation)
f64b98cd 2148{
2d7a215f
VS
2149 if (intel_rotation_90_or_270(rotation)) {
2150 *view = i915_ggtt_view_rotated;
2151 view->params.rotated = to_intel_framebuffer(fb)->rot_info;
2152 } else {
2153 *view = i915_ggtt_view_normal;
2154 }
2155}
50470bb0 2156
603525d7 2157static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
4e9a86b6
VS
2158{
2159 if (INTEL_INFO(dev_priv)->gen >= 9)
2160 return 256 * 1024;
985b8bb4 2161 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
666a4537 2162 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
4e9a86b6
VS
2163 return 128 * 1024;
2164 else if (INTEL_INFO(dev_priv)->gen >= 4)
2165 return 4 * 1024;
2166 else
44c5905e 2167 return 0;
4e9a86b6
VS
2168}
2169
603525d7
VS
2170static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
2171 uint64_t fb_modifier)
2172{
2173 switch (fb_modifier) {
2174 case DRM_FORMAT_MOD_NONE:
2175 return intel_linear_alignment(dev_priv);
2176 case I915_FORMAT_MOD_X_TILED:
2177 if (INTEL_INFO(dev_priv)->gen >= 9)
2178 return 256 * 1024;
2179 return 0;
2180 case I915_FORMAT_MOD_Y_TILED:
2181 case I915_FORMAT_MOD_Yf_TILED:
2182 return 1 * 1024 * 1024;
2183 default:
2184 MISSING_CASE(fb_modifier);
2185 return 0;
2186 }
2187}
2188
058d88c4
CW
2189struct i915_vma *
2190intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
6b95a207 2191{
850c4cdc 2192 struct drm_device *dev = fb->dev;
fac5e23e 2193 struct drm_i915_private *dev_priv = to_i915(dev);
850c4cdc 2194 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2195 struct i915_ggtt_view view;
058d88c4 2196 struct i915_vma *vma;
6b95a207 2197 u32 alignment;
6b95a207 2198
ebcdd39e
MR
2199 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2200
603525d7 2201 alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
6b95a207 2202
3465c580 2203 intel_fill_fb_ggtt_view(&view, fb, rotation);
f64b98cd 2204
693db184
CW
2205 /* Note that the w/a also requires 64 PTE of padding following the
2206 * bo. We currently fill all unused PTE with the shadow page and so
2207 * we should always have valid PTE following the scanout preventing
2208 * the VT-d warning.
2209 */
48f112fe 2210 if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
693db184
CW
2211 alignment = 256 * 1024;
2212
d6dd6843
PZ
2213 /*
2214 * Global gtt pte registers are special registers which actually forward
2215 * writes to a chunk of system memory. Which means that there is no risk
2216 * that the register values disappear as soon as we call
2217 * intel_runtime_pm_put(), so it is correct to wrap only the
2218 * pin/unpin/fence and not more.
2219 */
2220 intel_runtime_pm_get(dev_priv);
2221
058d88c4 2222 vma = i915_gem_object_pin_to_display_plane(obj, alignment, &view);
49ef5294
CW
2223 if (IS_ERR(vma))
2224 goto err;
6b95a207 2225
05a20d09 2226 if (i915_vma_is_map_and_fenceable(vma)) {
49ef5294
CW
2227 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2228 * fence, whereas 965+ only requires a fence if using
2229 * framebuffer compression. For simplicity, we always, when
2230 * possible, install a fence as the cost is not that onerous.
2231 *
2232 * If we fail to fence the tiled scanout, then either the
2233 * modeset will reject the change (which is highly unlikely as
2234 * the affected systems, all but one, do not have unmappable
2235 * space) or we will not be able to enable full powersaving
2236 * techniques (also likely not to apply due to various limits
2237 * FBC and the like impose on the size of the buffer, which
2238 * presumably we violated anyway with this unmappable buffer).
2239 * Anyway, it is presumably better to stumble onwards with
2240 * something and try to run the system in a "less than optimal"
2241 * mode that matches the user configuration.
2242 */
2243 if (i915_vma_get_fence(vma) == 0)
2244 i915_vma_pin_fence(vma);
9807216f 2245 }
6b95a207 2246
49ef5294 2247err:
d6dd6843 2248 intel_runtime_pm_put(dev_priv);
058d88c4 2249 return vma;
6b95a207
KH
2250}
2251
fb4b8ce1 2252void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
1690e1eb 2253{
82bc3b2d 2254 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2255 struct i915_ggtt_view view;
058d88c4 2256 struct i915_vma *vma;
82bc3b2d 2257
ebcdd39e
MR
2258 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2259
3465c580 2260 intel_fill_fb_ggtt_view(&view, fb, rotation);
05a20d09 2261 vma = i915_gem_object_to_ggtt(obj, &view);
f64b98cd 2262
49ef5294 2263 i915_vma_unpin_fence(vma);
058d88c4 2264 i915_gem_object_unpin_from_display_plane(vma);
1690e1eb
CW
2265}
2266
ef78ec94
VS
2267static int intel_fb_pitch(const struct drm_framebuffer *fb, int plane,
2268 unsigned int rotation)
2269{
2270 if (intel_rotation_90_or_270(rotation))
2271 return to_intel_framebuffer(fb)->rotated[plane].pitch;
2272 else
2273 return fb->pitches[plane];
2274}
2275
6687c906
VS
2276/*
2277 * Convert the x/y offsets into a linear offset.
2278 * Only valid with 0/180 degree rotation, which is fine since linear
2279 * offset is only used with linear buffers on pre-hsw and tiled buffers
2280 * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
2281 */
2282u32 intel_fb_xy_to_linear(int x, int y,
2949056c
VS
2283 const struct intel_plane_state *state,
2284 int plane)
6687c906 2285{
2949056c 2286 const struct drm_framebuffer *fb = state->base.fb;
6687c906
VS
2287 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
2288 unsigned int pitch = fb->pitches[plane];
2289
2290 return y * pitch + x * cpp;
2291}
2292
2293/*
2294 * Add the x/y offsets derived from fb->offsets[] to the user
2295 * specified plane src x/y offsets. The resulting x/y offsets
2296 * specify the start of scanout from the beginning of the gtt mapping.
2297 */
2298void intel_add_fb_offsets(int *x, int *y,
2949056c
VS
2299 const struct intel_plane_state *state,
2300 int plane)
6687c906
VS
2301
2302{
2949056c
VS
2303 const struct intel_framebuffer *intel_fb = to_intel_framebuffer(state->base.fb);
2304 unsigned int rotation = state->base.rotation;
6687c906
VS
2305
2306 if (intel_rotation_90_or_270(rotation)) {
2307 *x += intel_fb->rotated[plane].x;
2308 *y += intel_fb->rotated[plane].y;
2309 } else {
2310 *x += intel_fb->normal[plane].x;
2311 *y += intel_fb->normal[plane].y;
2312 }
2313}
2314
29cf9491 2315/*
29cf9491
VS
2316 * Input tile dimensions and pitch must already be
2317 * rotated to match x and y, and in pixel units.
2318 */
66a2d927
VS
2319static u32 _intel_adjust_tile_offset(int *x, int *y,
2320 unsigned int tile_width,
2321 unsigned int tile_height,
2322 unsigned int tile_size,
2323 unsigned int pitch_tiles,
2324 u32 old_offset,
2325 u32 new_offset)
29cf9491 2326{
b9b24038 2327 unsigned int pitch_pixels = pitch_tiles * tile_width;
29cf9491
VS
2328 unsigned int tiles;
2329
2330 WARN_ON(old_offset & (tile_size - 1));
2331 WARN_ON(new_offset & (tile_size - 1));
2332 WARN_ON(new_offset > old_offset);
2333
2334 tiles = (old_offset - new_offset) / tile_size;
2335
2336 *y += tiles / pitch_tiles * tile_height;
2337 *x += tiles % pitch_tiles * tile_width;
2338
b9b24038
VS
2339 /* minimize x in case it got needlessly big */
2340 *y += *x / pitch_pixels * tile_height;
2341 *x %= pitch_pixels;
2342
29cf9491
VS
2343 return new_offset;
2344}
2345
66a2d927
VS
2346/*
2347 * Adjust the tile offset by moving the difference into
2348 * the x/y offsets.
2349 */
2350static u32 intel_adjust_tile_offset(int *x, int *y,
2351 const struct intel_plane_state *state, int plane,
2352 u32 old_offset, u32 new_offset)
2353{
2354 const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
2355 const struct drm_framebuffer *fb = state->base.fb;
2356 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
2357 unsigned int rotation = state->base.rotation;
2358 unsigned int pitch = intel_fb_pitch(fb, plane, rotation);
2359
2360 WARN_ON(new_offset > old_offset);
2361
2362 if (fb->modifier[plane] != DRM_FORMAT_MOD_NONE) {
2363 unsigned int tile_size, tile_width, tile_height;
2364 unsigned int pitch_tiles;
2365
2366 tile_size = intel_tile_size(dev_priv);
2367 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2368 fb->modifier[plane], cpp);
2369
2370 if (intel_rotation_90_or_270(rotation)) {
2371 pitch_tiles = pitch / tile_height;
2372 swap(tile_width, tile_height);
2373 } else {
2374 pitch_tiles = pitch / (tile_width * cpp);
2375 }
2376
2377 _intel_adjust_tile_offset(x, y, tile_width, tile_height,
2378 tile_size, pitch_tiles,
2379 old_offset, new_offset);
2380 } else {
2381 old_offset += *y * pitch + *x * cpp;
2382
2383 *y = (old_offset - new_offset) / pitch;
2384 *x = ((old_offset - new_offset) - *y * pitch) / cpp;
2385 }
2386
2387 return new_offset;
2388}
2389
8d0deca8
VS
2390/*
2391 * Computes the linear offset to the base tile and adjusts
2392 * x, y. bytes per pixel is assumed to be a power-of-two.
2393 *
2394 * In the 90/270 rotated case, x and y are assumed
2395 * to be already rotated to match the rotated GTT view, and
2396 * pitch is the tile_height aligned framebuffer height.
6687c906
VS
2397 *
2398 * This function is used when computing the derived information
2399 * under intel_framebuffer, so using any of that information
2400 * here is not allowed. Anything under drm_framebuffer can be
2401 * used. This is why the user has to pass in the pitch since it
2402 * is specified in the rotated orientation.
8d0deca8 2403 */
6687c906
VS
2404static u32 _intel_compute_tile_offset(const struct drm_i915_private *dev_priv,
2405 int *x, int *y,
2406 const struct drm_framebuffer *fb, int plane,
2407 unsigned int pitch,
2408 unsigned int rotation,
2409 u32 alignment)
c2c75131 2410{
4f2d9934
VS
2411 uint64_t fb_modifier = fb->modifier[plane];
2412 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
6687c906 2413 u32 offset, offset_aligned;
29cf9491 2414
29cf9491
VS
2415 if (alignment)
2416 alignment--;
2417
b5c65338 2418 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
8d0deca8
VS
2419 unsigned int tile_size, tile_width, tile_height;
2420 unsigned int tile_rows, tiles, pitch_tiles;
c2c75131 2421
d843310d 2422 tile_size = intel_tile_size(dev_priv);
8d0deca8
VS
2423 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2424 fb_modifier, cpp);
2425
2426 if (intel_rotation_90_or_270(rotation)) {
2427 pitch_tiles = pitch / tile_height;
2428 swap(tile_width, tile_height);
2429 } else {
2430 pitch_tiles = pitch / (tile_width * cpp);
2431 }
d843310d
VS
2432
2433 tile_rows = *y / tile_height;
2434 *y %= tile_height;
c2c75131 2435
8d0deca8
VS
2436 tiles = *x / tile_width;
2437 *x %= tile_width;
bc752862 2438
29cf9491
VS
2439 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2440 offset_aligned = offset & ~alignment;
bc752862 2441
66a2d927
VS
2442 _intel_adjust_tile_offset(x, y, tile_width, tile_height,
2443 tile_size, pitch_tiles,
2444 offset, offset_aligned);
29cf9491 2445 } else {
bc752862 2446 offset = *y * pitch + *x * cpp;
29cf9491
VS
2447 offset_aligned = offset & ~alignment;
2448
4e9a86b6
VS
2449 *y = (offset & alignment) / pitch;
2450 *x = ((offset & alignment) - *y * pitch) / cpp;
bc752862 2451 }
29cf9491
VS
2452
2453 return offset_aligned;
c2c75131
SV
2454}
2455
6687c906 2456u32 intel_compute_tile_offset(int *x, int *y,
2949056c
VS
2457 const struct intel_plane_state *state,
2458 int plane)
6687c906 2459{
2949056c
VS
2460 const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
2461 const struct drm_framebuffer *fb = state->base.fb;
2462 unsigned int rotation = state->base.rotation;
ef78ec94 2463 int pitch = intel_fb_pitch(fb, plane, rotation);
8d970654
VS
2464 u32 alignment;
2465
2466 /* AUX_DIST needs only 4K alignment */
2467 if (fb->pixel_format == DRM_FORMAT_NV12 && plane == 1)
2468 alignment = 4096;
2469 else
2470 alignment = intel_surf_alignment(dev_priv, fb->modifier[plane]);
6687c906
VS
2471
2472 return _intel_compute_tile_offset(dev_priv, x, y, fb, plane, pitch,
2473 rotation, alignment);
2474}
2475
2476/* Convert the fb->offset[] linear offset into x/y offsets */
2477static void intel_fb_offset_to_xy(int *x, int *y,
2478 const struct drm_framebuffer *fb, int plane)
2479{
2480 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
2481 unsigned int pitch = fb->pitches[plane];
2482 u32 linear_offset = fb->offsets[plane];
2483
2484 *y = linear_offset / pitch;
2485 *x = linear_offset % pitch / cpp;
2486}
2487
72618ebf
VS
2488static unsigned int intel_fb_modifier_to_tiling(uint64_t fb_modifier)
2489{
2490 switch (fb_modifier) {
2491 case I915_FORMAT_MOD_X_TILED:
2492 return I915_TILING_X;
2493 case I915_FORMAT_MOD_Y_TILED:
2494 return I915_TILING_Y;
2495 default:
2496 return I915_TILING_NONE;
2497 }
2498}
2499
6687c906
VS
2500static int
2501intel_fill_fb_info(struct drm_i915_private *dev_priv,
2502 struct drm_framebuffer *fb)
2503{
2504 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
2505 struct intel_rotation_info *rot_info = &intel_fb->rot_info;
2506 u32 gtt_offset_rotated = 0;
2507 unsigned int max_size = 0;
2508 uint32_t format = fb->pixel_format;
2509 int i, num_planes = drm_format_num_planes(format);
2510 unsigned int tile_size = intel_tile_size(dev_priv);
2511
2512 for (i = 0; i < num_planes; i++) {
2513 unsigned int width, height;
2514 unsigned int cpp, size;
2515 u32 offset;
2516 int x, y;
2517
2518 cpp = drm_format_plane_cpp(format, i);
2519 width = drm_format_plane_width(fb->width, format, i);
2520 height = drm_format_plane_height(fb->height, format, i);
2521
2522 intel_fb_offset_to_xy(&x, &y, fb, i);
2523
60d5f2a4
VS
2524 /*
2525 * The fence (if used) is aligned to the start of the object
2526 * so having the framebuffer wrap around across the edge of the
2527 * fenced region doesn't really work. We have no API to configure
2528 * the fence start offset within the object (nor could we probably
2529 * on gen2/3). So it's just easier if we just require that the
2530 * fb layout agrees with the fence layout. We already check that the
2531 * fb stride matches the fence stride elsewhere.
2532 */
2533 if (i915_gem_object_is_tiled(intel_fb->obj) &&
2534 (x + width) * cpp > fb->pitches[i]) {
2535 DRM_DEBUG("bad fb plane %d offset: 0x%x\n",
2536 i, fb->offsets[i]);
2537 return -EINVAL;
2538 }
2539
6687c906
VS
2540 /*
2541 * First pixel of the framebuffer from
2542 * the start of the normal gtt mapping.
2543 */
2544 intel_fb->normal[i].x = x;
2545 intel_fb->normal[i].y = y;
2546
2547 offset = _intel_compute_tile_offset(dev_priv, &x, &y,
2548 fb, 0, fb->pitches[i],
cc926387 2549 DRM_ROTATE_0, tile_size);
6687c906
VS
2550 offset /= tile_size;
2551
2552 if (fb->modifier[i] != DRM_FORMAT_MOD_NONE) {
2553 unsigned int tile_width, tile_height;
2554 unsigned int pitch_tiles;
2555 struct drm_rect r;
2556
2557 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2558 fb->modifier[i], cpp);
2559
2560 rot_info->plane[i].offset = offset;
2561 rot_info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i], tile_width * cpp);
2562 rot_info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
2563 rot_info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
2564
2565 intel_fb->rotated[i].pitch =
2566 rot_info->plane[i].height * tile_height;
2567
2568 /* how many tiles does this plane need */
2569 size = rot_info->plane[i].stride * rot_info->plane[i].height;
2570 /*
2571 * If the plane isn't horizontally tile aligned,
2572 * we need one more tile.
2573 */
2574 if (x != 0)
2575 size++;
2576
2577 /* rotate the x/y offsets to match the GTT view */
2578 r.x1 = x;
2579 r.y1 = y;
2580 r.x2 = x + width;
2581 r.y2 = y + height;
2582 drm_rect_rotate(&r,
2583 rot_info->plane[i].width * tile_width,
2584 rot_info->plane[i].height * tile_height,
cc926387 2585 DRM_ROTATE_270);
6687c906
VS
2586 x = r.x1;
2587 y = r.y1;
2588
2589 /* rotate the tile dimensions to match the GTT view */
2590 pitch_tiles = intel_fb->rotated[i].pitch / tile_height;
2591 swap(tile_width, tile_height);
2592
2593 /*
2594 * We only keep the x/y offsets, so push all of the
2595 * gtt offset into the x/y offsets.
2596 */
66a2d927
VS
2597 _intel_adjust_tile_offset(&x, &y, tile_size,
2598 tile_width, tile_height, pitch_tiles,
2599 gtt_offset_rotated * tile_size, 0);
6687c906
VS
2600
2601 gtt_offset_rotated += rot_info->plane[i].width * rot_info->plane[i].height;
2602
2603 /*
2604 * First pixel of the framebuffer from
2605 * the start of the rotated gtt mapping.
2606 */
2607 intel_fb->rotated[i].x = x;
2608 intel_fb->rotated[i].y = y;
2609 } else {
2610 size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
2611 x * cpp, tile_size);
2612 }
2613
2614 /* how many tiles in total needed in the bo */
2615 max_size = max(max_size, offset + size);
2616 }
2617
2618 if (max_size * tile_size > to_intel_framebuffer(fb)->obj->base.size) {
2619 DRM_DEBUG("fb too big for bo (need %u bytes, have %zu bytes)\n",
2620 max_size * tile_size, to_intel_framebuffer(fb)->obj->base.size);
2621 return -EINVAL;
2622 }
2623
2624 return 0;
2625}
2626
b35d63fa 2627static int i9xx_format_to_fourcc(int format)
46f297fb
JB
2628{
2629 switch (format) {
2630 case DISPPLANE_8BPP:
2631 return DRM_FORMAT_C8;
2632 case DISPPLANE_BGRX555:
2633 return DRM_FORMAT_XRGB1555;
2634 case DISPPLANE_BGRX565:
2635 return DRM_FORMAT_RGB565;
2636 default:
2637 case DISPPLANE_BGRX888:
2638 return DRM_FORMAT_XRGB8888;
2639 case DISPPLANE_RGBX888:
2640 return DRM_FORMAT_XBGR8888;
2641 case DISPPLANE_BGRX101010:
2642 return DRM_FORMAT_XRGB2101010;
2643 case DISPPLANE_RGBX101010:
2644 return DRM_FORMAT_XBGR2101010;
2645 }
2646}
2647
bc8d7dff
DL
2648static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2649{
2650 switch (format) {
2651 case PLANE_CTL_FORMAT_RGB_565:
2652 return DRM_FORMAT_RGB565;
2653 default:
2654 case PLANE_CTL_FORMAT_XRGB_8888:
2655 if (rgb_order) {
2656 if (alpha)
2657 return DRM_FORMAT_ABGR8888;
2658 else
2659 return DRM_FORMAT_XBGR8888;
2660 } else {
2661 if (alpha)
2662 return DRM_FORMAT_ARGB8888;
2663 else
2664 return DRM_FORMAT_XRGB8888;
2665 }
2666 case PLANE_CTL_FORMAT_XRGB_2101010:
2667 if (rgb_order)
2668 return DRM_FORMAT_XBGR2101010;
2669 else
2670 return DRM_FORMAT_XRGB2101010;
2671 }
2672}
2673
5724dbd1 2674static bool
f6936e29
SV
2675intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2676 struct intel_initial_plane_config *plane_config)
46f297fb
JB
2677{
2678 struct drm_device *dev = crtc->base.dev;
3badb49f 2679 struct drm_i915_private *dev_priv = to_i915(dev);
72e96d64 2680 struct i915_ggtt *ggtt = &dev_priv->ggtt;
46f297fb
JB
2681 struct drm_i915_gem_object *obj = NULL;
2682 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2d14030b 2683 struct drm_framebuffer *fb = &plane_config->fb->base;
f37b5c2b
SV
2684 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2685 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2686 PAGE_SIZE);
2687
2688 size_aligned -= base_aligned;
46f297fb 2689
ff2652ea
CW
2690 if (plane_config->size == 0)
2691 return false;
2692
3badb49f
PZ
2693 /* If the FB is too big, just don't use it since fbdev is not very
2694 * important and we should probably use that space with FBC or other
2695 * features. */
72e96d64 2696 if (size_aligned * 2 > ggtt->stolen_usable_size)
3badb49f
PZ
2697 return false;
2698
12c83d99
TU
2699 mutex_lock(&dev->struct_mutex);
2700
f37b5c2b
SV
2701 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2702 base_aligned,
2703 base_aligned,
2704 size_aligned);
12c83d99
TU
2705 if (!obj) {
2706 mutex_unlock(&dev->struct_mutex);
484b41dd 2707 return false;
12c83d99 2708 }
46f297fb 2709
3e510a8e
CW
2710 if (plane_config->tiling == I915_TILING_X)
2711 obj->tiling_and_stride = fb->pitches[0] | I915_TILING_X;
46f297fb 2712
6bf129df
DL
2713 mode_cmd.pixel_format = fb->pixel_format;
2714 mode_cmd.width = fb->width;
2715 mode_cmd.height = fb->height;
2716 mode_cmd.pitches[0] = fb->pitches[0];
18c5247e
SV
2717 mode_cmd.modifier[0] = fb->modifier[0];
2718 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
46f297fb 2719
6bf129df 2720 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
484b41dd 2721 &mode_cmd, obj)) {
46f297fb
JB
2722 DRM_DEBUG_KMS("intel fb init failed\n");
2723 goto out_unref_obj;
2724 }
12c83d99 2725
46f297fb 2726 mutex_unlock(&dev->struct_mutex);
484b41dd 2727
f6936e29 2728 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
484b41dd 2729 return true;
46f297fb
JB
2730
2731out_unref_obj:
f8c417cd 2732 i915_gem_object_put(obj);
46f297fb 2733 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2734 return false;
2735}
2736
5a21b665
SV
2737/* Update plane->state->fb to match plane->fb after driver-internal updates */
2738static void
2739update_state_fb(struct drm_plane *plane)
2740{
2741 if (plane->fb == plane->state->fb)
2742 return;
2743
2744 if (plane->state->fb)
2745 drm_framebuffer_unreference(plane->state->fb);
2746 plane->state->fb = plane->fb;
2747 if (plane->state->fb)
2748 drm_framebuffer_reference(plane->state->fb);
2749}
2750
5724dbd1 2751static void
f6936e29
SV
2752intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2753 struct intel_initial_plane_config *plane_config)
484b41dd
JB
2754{
2755 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 2756 struct drm_i915_private *dev_priv = to_i915(dev);
484b41dd
JB
2757 struct drm_crtc *c;
2758 struct intel_crtc *i;
2ff8fde1 2759 struct drm_i915_gem_object *obj;
88595ac9 2760 struct drm_plane *primary = intel_crtc->base.primary;
be5651f2 2761 struct drm_plane_state *plane_state = primary->state;
200757f5
MR
2762 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2763 struct intel_plane *intel_plane = to_intel_plane(primary);
0a8d8a86
MR
2764 struct intel_plane_state *intel_state =
2765 to_intel_plane_state(plane_state);
88595ac9 2766 struct drm_framebuffer *fb;
484b41dd 2767
2d14030b 2768 if (!plane_config->fb)
484b41dd
JB
2769 return;
2770
f6936e29 2771 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
88595ac9
SV
2772 fb = &plane_config->fb->base;
2773 goto valid_fb;
f55548b5 2774 }
484b41dd 2775
2d14030b 2776 kfree(plane_config->fb);
484b41dd
JB
2777
2778 /*
2779 * Failed to alloc the obj, check to see if we should share
2780 * an fb with another CRTC instead
2781 */
70e1e0ec 2782 for_each_crtc(dev, c) {
484b41dd
JB
2783 i = to_intel_crtc(c);
2784
2785 if (c == &intel_crtc->base)
2786 continue;
2787
2ff8fde1
MR
2788 if (!i->active)
2789 continue;
2790
88595ac9
SV
2791 fb = c->primary->fb;
2792 if (!fb)
484b41dd
JB
2793 continue;
2794
88595ac9 2795 obj = intel_fb_obj(fb);
058d88c4 2796 if (i915_gem_object_ggtt_offset(obj, NULL) == plane_config->base) {
88595ac9
SV
2797 drm_framebuffer_reference(fb);
2798 goto valid_fb;
484b41dd
JB
2799 }
2800 }
88595ac9 2801
200757f5
MR
2802 /*
2803 * We've failed to reconstruct the BIOS FB. Current display state
2804 * indicates that the primary plane is visible, but has a NULL FB,
2805 * which will lead to problems later if we don't fix it up. The
2806 * simplest solution is to just disable the primary plane now and
2807 * pretend the BIOS never had it enabled.
2808 */
936e71e3 2809 to_intel_plane_state(plane_state)->base.visible = false;
200757f5 2810 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
2622a081 2811 intel_pre_disable_primary_noatomic(&intel_crtc->base);
200757f5
MR
2812 intel_plane->disable_plane(primary, &intel_crtc->base);
2813
88595ac9
SV
2814 return;
2815
2816valid_fb:
f44e2659
VS
2817 plane_state->src_x = 0;
2818 plane_state->src_y = 0;
be5651f2
ML
2819 plane_state->src_w = fb->width << 16;
2820 plane_state->src_h = fb->height << 16;
2821
f44e2659
VS
2822 plane_state->crtc_x = 0;
2823 plane_state->crtc_y = 0;
be5651f2
ML
2824 plane_state->crtc_w = fb->width;
2825 plane_state->crtc_h = fb->height;
2826
936e71e3
VS
2827 intel_state->base.src.x1 = plane_state->src_x;
2828 intel_state->base.src.y1 = plane_state->src_y;
2829 intel_state->base.src.x2 = plane_state->src_x + plane_state->src_w;
2830 intel_state->base.src.y2 = plane_state->src_y + plane_state->src_h;
2831 intel_state->base.dst.x1 = plane_state->crtc_x;
2832 intel_state->base.dst.y1 = plane_state->crtc_y;
2833 intel_state->base.dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
2834 intel_state->base.dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
0a8d8a86 2835
88595ac9 2836 obj = intel_fb_obj(fb);
3e510a8e 2837 if (i915_gem_object_is_tiled(obj))
88595ac9
SV
2838 dev_priv->preserve_bios_swizzle = true;
2839
be5651f2
ML
2840 drm_framebuffer_reference(fb);
2841 primary->fb = primary->state->fb = fb;
36750f28 2842 primary->crtc = primary->state->crtc = &intel_crtc->base;
36750f28 2843 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
faf5bf0a
CW
2844 atomic_or(to_intel_plane(primary)->frontbuffer_bit,
2845 &obj->frontbuffer_bits);
46f297fb
JB
2846}
2847
b63a16f6
VS
2848static int skl_max_plane_width(const struct drm_framebuffer *fb, int plane,
2849 unsigned int rotation)
2850{
2851 int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
2852
2853 switch (fb->modifier[plane]) {
2854 case DRM_FORMAT_MOD_NONE:
2855 case I915_FORMAT_MOD_X_TILED:
2856 switch (cpp) {
2857 case 8:
2858 return 4096;
2859 case 4:
2860 case 2:
2861 case 1:
2862 return 8192;
2863 default:
2864 MISSING_CASE(cpp);
2865 break;
2866 }
2867 break;
2868 case I915_FORMAT_MOD_Y_TILED:
2869 case I915_FORMAT_MOD_Yf_TILED:
2870 switch (cpp) {
2871 case 8:
2872 return 2048;
2873 case 4:
2874 return 4096;
2875 case 2:
2876 case 1:
2877 return 8192;
2878 default:
2879 MISSING_CASE(cpp);
2880 break;
2881 }
2882 break;
2883 default:
2884 MISSING_CASE(fb->modifier[plane]);
2885 }
2886
2887 return 2048;
2888}
2889
2890static int skl_check_main_surface(struct intel_plane_state *plane_state)
2891{
2892 const struct drm_i915_private *dev_priv = to_i915(plane_state->base.plane->dev);
2893 const struct drm_framebuffer *fb = plane_state->base.fb;
2894 unsigned int rotation = plane_state->base.rotation;
cc926387
SV
2895 int x = plane_state->base.src.x1 >> 16;
2896 int y = plane_state->base.src.y1 >> 16;
2897 int w = drm_rect_width(&plane_state->base.src) >> 16;
2898 int h = drm_rect_height(&plane_state->base.src) >> 16;
b63a16f6
VS
2899 int max_width = skl_max_plane_width(fb, 0, rotation);
2900 int max_height = 4096;
8d970654 2901 u32 alignment, offset, aux_offset = plane_state->aux.offset;
b63a16f6
VS
2902
2903 if (w > max_width || h > max_height) {
2904 DRM_DEBUG_KMS("requested Y/RGB source size %dx%d too big (limit %dx%d)\n",
2905 w, h, max_width, max_height);
2906 return -EINVAL;
2907 }
2908
2909 intel_add_fb_offsets(&x, &y, plane_state, 0);
2910 offset = intel_compute_tile_offset(&x, &y, plane_state, 0);
2911
2912 alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
2913
8d970654
VS
2914 /*
2915 * AUX surface offset is specified as the distance from the
2916 * main surface offset, and it must be non-negative. Make
2917 * sure that is what we will get.
2918 */
2919 if (offset > aux_offset)
2920 offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
2921 offset, aux_offset & ~(alignment - 1));
2922
b63a16f6
VS
2923 /*
2924 * When using an X-tiled surface, the plane blows up
2925 * if the x offset + width exceed the stride.
2926 *
2927 * TODO: linear and Y-tiled seem fine, Yf untested,
2928 */
2929 if (fb->modifier[0] == I915_FORMAT_MOD_X_TILED) {
2930 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
2931
2932 while ((x + w) * cpp > fb->pitches[0]) {
2933 if (offset == 0) {
2934 DRM_DEBUG_KMS("Unable to find suitable display surface offset\n");
2935 return -EINVAL;
2936 }
2937
2938 offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
2939 offset, offset - alignment);
2940 }
2941 }
2942
2943 plane_state->main.offset = offset;
2944 plane_state->main.x = x;
2945 plane_state->main.y = y;
2946
2947 return 0;
2948}
2949
8d970654
VS
2950static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
2951{
2952 const struct drm_framebuffer *fb = plane_state->base.fb;
2953 unsigned int rotation = plane_state->base.rotation;
2954 int max_width = skl_max_plane_width(fb, 1, rotation);
2955 int max_height = 4096;
cc926387
SV
2956 int x = plane_state->base.src.x1 >> 17;
2957 int y = plane_state->base.src.y1 >> 17;
2958 int w = drm_rect_width(&plane_state->base.src) >> 17;
2959 int h = drm_rect_height(&plane_state->base.src) >> 17;
8d970654
VS
2960 u32 offset;
2961
2962 intel_add_fb_offsets(&x, &y, plane_state, 1);
2963 offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
2964
2965 /* FIXME not quite sure how/if these apply to the chroma plane */
2966 if (w > max_width || h > max_height) {
2967 DRM_DEBUG_KMS("CbCr source size %dx%d too big (limit %dx%d)\n",
2968 w, h, max_width, max_height);
2969 return -EINVAL;
2970 }
2971
2972 plane_state->aux.offset = offset;
2973 plane_state->aux.x = x;
2974 plane_state->aux.y = y;
2975
2976 return 0;
2977}
2978
b63a16f6
VS
2979int skl_check_plane_surface(struct intel_plane_state *plane_state)
2980{
2981 const struct drm_framebuffer *fb = plane_state->base.fb;
2982 unsigned int rotation = plane_state->base.rotation;
2983 int ret;
2984
2985 /* Rotate src coordinates to match rotated GTT view */
2986 if (intel_rotation_90_or_270(rotation))
cc926387
SV
2987 drm_rect_rotate(&plane_state->base.src,
2988 fb->width, fb->height, DRM_ROTATE_270);
b63a16f6 2989
8d970654
VS
2990 /*
2991 * Handle the AUX surface first since
2992 * the main surface setup depends on it.
2993 */
2994 if (fb->pixel_format == DRM_FORMAT_NV12) {
2995 ret = skl_check_nv12_aux_surface(plane_state);
2996 if (ret)
2997 return ret;
2998 } else {
2999 plane_state->aux.offset = ~0xfff;
3000 plane_state->aux.x = 0;
3001 plane_state->aux.y = 0;
3002 }
3003
b63a16f6
VS
3004 ret = skl_check_main_surface(plane_state);
3005 if (ret)
3006 return ret;
3007
3008 return 0;
3009}
3010
a8d201af
ML
3011static void i9xx_update_primary_plane(struct drm_plane *primary,
3012 const struct intel_crtc_state *crtc_state,
3013 const struct intel_plane_state *plane_state)
81255565 3014{
a8d201af 3015 struct drm_device *dev = primary->dev;
fac5e23e 3016 struct drm_i915_private *dev_priv = to_i915(dev);
a8d201af
ML
3017 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3018 struct drm_framebuffer *fb = plane_state->base.fb;
3019 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
81255565 3020 int plane = intel_crtc->plane;
54ea9da8 3021 u32 linear_offset;
81255565 3022 u32 dspcntr;
f0f59a00 3023 i915_reg_t reg = DSPCNTR(plane);
8d0deca8 3024 unsigned int rotation = plane_state->base.rotation;
936e71e3
VS
3025 int x = plane_state->base.src.x1 >> 16;
3026 int y = plane_state->base.src.y1 >> 16;
c9ba6fad 3027
f45651ba
VS
3028 dspcntr = DISPPLANE_GAMMA_ENABLE;
3029
fdd508a6 3030 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
3031
3032 if (INTEL_INFO(dev)->gen < 4) {
3033 if (intel_crtc->pipe == PIPE_B)
3034 dspcntr |= DISPPLANE_SEL_PIPE_B;
3035
3036 /* pipesrc and dspsize control the size that is scaled from,
3037 * which should always be the user's requested size.
3038 */
3039 I915_WRITE(DSPSIZE(plane),
a8d201af
ML
3040 ((crtc_state->pipe_src_h - 1) << 16) |
3041 (crtc_state->pipe_src_w - 1));
f45651ba 3042 I915_WRITE(DSPPOS(plane), 0);
920a14b2 3043 } else if (IS_CHERRYVIEW(dev_priv) && plane == PLANE_B) {
c14b0485 3044 I915_WRITE(PRIMSIZE(plane),
a8d201af
ML
3045 ((crtc_state->pipe_src_h - 1) << 16) |
3046 (crtc_state->pipe_src_w - 1));
c14b0485
VS
3047 I915_WRITE(PRIMPOS(plane), 0);
3048 I915_WRITE(PRIMCNSTALPHA(plane), 0);
f45651ba 3049 }
81255565 3050
57779d06
VS
3051 switch (fb->pixel_format) {
3052 case DRM_FORMAT_C8:
81255565
JB
3053 dspcntr |= DISPPLANE_8BPP;
3054 break;
57779d06 3055 case DRM_FORMAT_XRGB1555:
57779d06 3056 dspcntr |= DISPPLANE_BGRX555;
81255565 3057 break;
57779d06
VS
3058 case DRM_FORMAT_RGB565:
3059 dspcntr |= DISPPLANE_BGRX565;
3060 break;
3061 case DRM_FORMAT_XRGB8888:
57779d06
VS
3062 dspcntr |= DISPPLANE_BGRX888;
3063 break;
3064 case DRM_FORMAT_XBGR8888:
57779d06
VS
3065 dspcntr |= DISPPLANE_RGBX888;
3066 break;
3067 case DRM_FORMAT_XRGB2101010:
57779d06
VS
3068 dspcntr |= DISPPLANE_BGRX101010;
3069 break;
3070 case DRM_FORMAT_XBGR2101010:
57779d06 3071 dspcntr |= DISPPLANE_RGBX101010;
81255565
JB
3072 break;
3073 default:
baba133a 3074 BUG();
81255565 3075 }
57779d06 3076
72618ebf
VS
3077 if (INTEL_GEN(dev_priv) >= 4 &&
3078 fb->modifier[0] == I915_FORMAT_MOD_X_TILED)
f45651ba 3079 dspcntr |= DISPPLANE_TILED;
81255565 3080
9beb5fea 3081 if (IS_G4X(dev_priv))
de1aa629
VS
3082 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
3083
2949056c 3084 intel_add_fb_offsets(&x, &y, plane_state, 0);
81255565 3085
6687c906 3086 if (INTEL_INFO(dev)->gen >= 4)
c2c75131 3087 intel_crtc->dspaddr_offset =
2949056c 3088 intel_compute_tile_offset(&x, &y, plane_state, 0);
e506a0c6 3089
31ad61e4 3090 if (rotation == DRM_ROTATE_180) {
48404c1e
SJ
3091 dspcntr |= DISPPLANE_ROTATE_180;
3092
a8d201af
ML
3093 x += (crtc_state->pipe_src_w - 1);
3094 y += (crtc_state->pipe_src_h - 1);
48404c1e
SJ
3095 }
3096
2949056c 3097 linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
6687c906
VS
3098
3099 if (INTEL_INFO(dev)->gen < 4)
3100 intel_crtc->dspaddr_offset = linear_offset;
3101
2db3366b
PZ
3102 intel_crtc->adjusted_x = x;
3103 intel_crtc->adjusted_y = y;
3104
48404c1e
SJ
3105 I915_WRITE(reg, dspcntr);
3106
01f2c773 3107 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
a6c45cf0 3108 if (INTEL_INFO(dev)->gen >= 4) {
85ba7b7d 3109 I915_WRITE(DSPSURF(plane),
6687c906
VS
3110 intel_fb_gtt_offset(fb, rotation) +
3111 intel_crtc->dspaddr_offset);
5eddb70b 3112 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
e506a0c6 3113 I915_WRITE(DSPLINOFF(plane), linear_offset);
5eddb70b 3114 } else
058d88c4 3115 I915_WRITE(DSPADDR(plane), i915_gem_object_ggtt_offset(obj, NULL) + linear_offset);
5eddb70b 3116 POSTING_READ(reg);
17638cd6
JB
3117}
3118
a8d201af
ML
3119static void i9xx_disable_primary_plane(struct drm_plane *primary,
3120 struct drm_crtc *crtc)
17638cd6
JB
3121{
3122 struct drm_device *dev = crtc->dev;
fac5e23e 3123 struct drm_i915_private *dev_priv = to_i915(dev);
17638cd6 3124 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
17638cd6 3125 int plane = intel_crtc->plane;
f45651ba 3126
a8d201af
ML
3127 I915_WRITE(DSPCNTR(plane), 0);
3128 if (INTEL_INFO(dev_priv)->gen >= 4)
fdd508a6 3129 I915_WRITE(DSPSURF(plane), 0);
a8d201af
ML
3130 else
3131 I915_WRITE(DSPADDR(plane), 0);
3132 POSTING_READ(DSPCNTR(plane));
3133}
c9ba6fad 3134
a8d201af
ML
3135static void ironlake_update_primary_plane(struct drm_plane *primary,
3136 const struct intel_crtc_state *crtc_state,
3137 const struct intel_plane_state *plane_state)
3138{
3139 struct drm_device *dev = primary->dev;
fac5e23e 3140 struct drm_i915_private *dev_priv = to_i915(dev);
a8d201af
ML
3141 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3142 struct drm_framebuffer *fb = plane_state->base.fb;
a8d201af 3143 int plane = intel_crtc->plane;
54ea9da8 3144 u32 linear_offset;
a8d201af
ML
3145 u32 dspcntr;
3146 i915_reg_t reg = DSPCNTR(plane);
8d0deca8 3147 unsigned int rotation = plane_state->base.rotation;
936e71e3
VS
3148 int x = plane_state->base.src.x1 >> 16;
3149 int y = plane_state->base.src.y1 >> 16;
c9ba6fad 3150
f45651ba 3151 dspcntr = DISPPLANE_GAMMA_ENABLE;
fdd508a6 3152 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba 3153
8652744b 3154 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
f45651ba 3155 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
17638cd6 3156
57779d06
VS
3157 switch (fb->pixel_format) {
3158 case DRM_FORMAT_C8:
17638cd6
JB
3159 dspcntr |= DISPPLANE_8BPP;
3160 break;
57779d06
VS
3161 case DRM_FORMAT_RGB565:
3162 dspcntr |= DISPPLANE_BGRX565;
17638cd6 3163 break;
57779d06 3164 case DRM_FORMAT_XRGB8888:
57779d06
VS
3165 dspcntr |= DISPPLANE_BGRX888;
3166 break;
3167 case DRM_FORMAT_XBGR8888:
57779d06
VS
3168 dspcntr |= DISPPLANE_RGBX888;
3169 break;
3170 case DRM_FORMAT_XRGB2101010:
57779d06
VS
3171 dspcntr |= DISPPLANE_BGRX101010;
3172 break;
3173 case DRM_FORMAT_XBGR2101010:
57779d06 3174 dspcntr |= DISPPLANE_RGBX101010;
17638cd6
JB
3175 break;
3176 default:
baba133a 3177 BUG();
17638cd6
JB
3178 }
3179
72618ebf 3180 if (fb->modifier[0] == I915_FORMAT_MOD_X_TILED)
17638cd6 3181 dspcntr |= DISPPLANE_TILED;
17638cd6 3182
8652744b 3183 if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv))
1f5d76db 3184 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
17638cd6 3185
2949056c 3186 intel_add_fb_offsets(&x, &y, plane_state, 0);
6687c906 3187
c2c75131 3188 intel_crtc->dspaddr_offset =
2949056c 3189 intel_compute_tile_offset(&x, &y, plane_state, 0);
6687c906 3190
31ad61e4 3191 if (rotation == DRM_ROTATE_180) {
48404c1e
SJ
3192 dspcntr |= DISPPLANE_ROTATE_180;
3193
8652744b 3194 if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv)) {
a8d201af
ML
3195 x += (crtc_state->pipe_src_w - 1);
3196 y += (crtc_state->pipe_src_h - 1);
48404c1e
SJ
3197 }
3198 }
3199
2949056c 3200 linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
6687c906 3201
2db3366b
PZ
3202 intel_crtc->adjusted_x = x;
3203 intel_crtc->adjusted_y = y;
3204
48404c1e 3205 I915_WRITE(reg, dspcntr);
17638cd6 3206
01f2c773 3207 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
85ba7b7d 3208 I915_WRITE(DSPSURF(plane),
6687c906
VS
3209 intel_fb_gtt_offset(fb, rotation) +
3210 intel_crtc->dspaddr_offset);
8652744b 3211 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
bc1c91eb
DL
3212 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
3213 } else {
3214 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
3215 I915_WRITE(DSPLINOFF(plane), linear_offset);
3216 }
17638cd6 3217 POSTING_READ(reg);
17638cd6
JB
3218}
3219
7b49f948
VS
3220u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
3221 uint64_t fb_modifier, uint32_t pixel_format)
b321803d 3222{
7b49f948 3223 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
b321803d 3224 return 64;
7b49f948
VS
3225 } else {
3226 int cpp = drm_format_plane_cpp(pixel_format, 0);
3227
27ba3910 3228 return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
b321803d
DL
3229 }
3230}
3231
6687c906
VS
3232u32 intel_fb_gtt_offset(struct drm_framebuffer *fb,
3233 unsigned int rotation)
121920fa 3234{
6687c906 3235 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
ce7f1728 3236 struct i915_ggtt_view view;
058d88c4 3237 struct i915_vma *vma;
121920fa 3238
6687c906 3239 intel_fill_fb_ggtt_view(&view, fb, rotation);
dedf278c 3240
058d88c4
CW
3241 vma = i915_gem_object_to_ggtt(obj, &view);
3242 if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
3243 view.type))
3244 return -1;
3245
bde13ebd 3246 return i915_ggtt_offset(vma);
121920fa
TU
3247}
3248
e435d6e5
ML
3249static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
3250{
3251 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 3252 struct drm_i915_private *dev_priv = to_i915(dev);
e435d6e5
ML
3253
3254 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
3255 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
3256 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
e435d6e5
ML
3257}
3258
a1b2278e
CK
3259/*
3260 * This function detaches (aka. unbinds) unused scalers in hardware
3261 */
0583236e 3262static void skl_detach_scalers(struct intel_crtc *intel_crtc)
a1b2278e 3263{
a1b2278e
CK
3264 struct intel_crtc_scaler_state *scaler_state;
3265 int i;
3266
a1b2278e
CK
3267 scaler_state = &intel_crtc->config->scaler_state;
3268
3269 /* loop through and disable scalers that aren't in use */
3270 for (i = 0; i < intel_crtc->num_scalers; i++) {
e435d6e5
ML
3271 if (!scaler_state->scalers[i].in_use)
3272 skl_detach_scaler(intel_crtc, i);
a1b2278e
CK
3273 }
3274}
3275
d2196774
VS
3276u32 skl_plane_stride(const struct drm_framebuffer *fb, int plane,
3277 unsigned int rotation)
3278{
3279 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
3280 u32 stride = intel_fb_pitch(fb, plane, rotation);
3281
3282 /*
3283 * The stride is either expressed as a multiple of 64 bytes chunks for
3284 * linear buffers or in number of tiles for tiled buffers.
3285 */
3286 if (intel_rotation_90_or_270(rotation)) {
3287 int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
3288
3289 stride /= intel_tile_height(dev_priv, fb->modifier[0], cpp);
3290 } else {
3291 stride /= intel_fb_stride_alignment(dev_priv, fb->modifier[0],
3292 fb->pixel_format);
3293 }
3294
3295 return stride;
3296}
3297
6156a456 3298u32 skl_plane_ctl_format(uint32_t pixel_format)
70d21f0e 3299{
6156a456 3300 switch (pixel_format) {
d161cf7a 3301 case DRM_FORMAT_C8:
c34ce3d1 3302 return PLANE_CTL_FORMAT_INDEXED;
70d21f0e 3303 case DRM_FORMAT_RGB565:
c34ce3d1 3304 return PLANE_CTL_FORMAT_RGB_565;
70d21f0e 3305 case DRM_FORMAT_XBGR8888:
c34ce3d1 3306 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
6156a456 3307 case DRM_FORMAT_XRGB8888:
c34ce3d1 3308 return PLANE_CTL_FORMAT_XRGB_8888;
6156a456
CK
3309 /*
3310 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
3311 * to be already pre-multiplied. We need to add a knob (or a different
3312 * DRM_FORMAT) for user-space to configure that.
3313 */
f75fb42a 3314 case DRM_FORMAT_ABGR8888:
c34ce3d1 3315 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
6156a456 3316 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
6156a456 3317 case DRM_FORMAT_ARGB8888:
c34ce3d1 3318 return PLANE_CTL_FORMAT_XRGB_8888 |
6156a456 3319 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
70d21f0e 3320 case DRM_FORMAT_XRGB2101010:
c34ce3d1 3321 return PLANE_CTL_FORMAT_XRGB_2101010;
70d21f0e 3322 case DRM_FORMAT_XBGR2101010:
c34ce3d1 3323 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
6156a456 3324 case DRM_FORMAT_YUYV:
c34ce3d1 3325 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
6156a456 3326 case DRM_FORMAT_YVYU:
c34ce3d1 3327 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
6156a456 3328 case DRM_FORMAT_UYVY:
c34ce3d1 3329 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
6156a456 3330 case DRM_FORMAT_VYUY:
c34ce3d1 3331 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
70d21f0e 3332 default:
4249eeef 3333 MISSING_CASE(pixel_format);
70d21f0e 3334 }
8cfcba41 3335
c34ce3d1 3336 return 0;
6156a456 3337}
70d21f0e 3338
6156a456
CK
3339u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
3340{
6156a456 3341 switch (fb_modifier) {
30af77c4 3342 case DRM_FORMAT_MOD_NONE:
70d21f0e 3343 break;
30af77c4 3344 case I915_FORMAT_MOD_X_TILED:
c34ce3d1 3345 return PLANE_CTL_TILED_X;
b321803d 3346 case I915_FORMAT_MOD_Y_TILED:
c34ce3d1 3347 return PLANE_CTL_TILED_Y;
b321803d 3348 case I915_FORMAT_MOD_Yf_TILED:
c34ce3d1 3349 return PLANE_CTL_TILED_YF;
70d21f0e 3350 default:
6156a456 3351 MISSING_CASE(fb_modifier);
70d21f0e 3352 }
8cfcba41 3353
c34ce3d1 3354 return 0;
6156a456 3355}
70d21f0e 3356
6156a456
CK
3357u32 skl_plane_ctl_rotation(unsigned int rotation)
3358{
3b7a5119 3359 switch (rotation) {
31ad61e4 3360 case DRM_ROTATE_0:
6156a456 3361 break;
1e8df167
SJ
3362 /*
3363 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
3364 * while i915 HW rotation is clockwise, thats why this swapping.
3365 */
31ad61e4 3366 case DRM_ROTATE_90:
1e8df167 3367 return PLANE_CTL_ROTATE_270;
31ad61e4 3368 case DRM_ROTATE_180:
c34ce3d1 3369 return PLANE_CTL_ROTATE_180;
31ad61e4 3370 case DRM_ROTATE_270:
1e8df167 3371 return PLANE_CTL_ROTATE_90;
6156a456
CK
3372 default:
3373 MISSING_CASE(rotation);
3374 }
3375
c34ce3d1 3376 return 0;
6156a456
CK
3377}
3378
a8d201af
ML
3379static void skylake_update_primary_plane(struct drm_plane *plane,
3380 const struct intel_crtc_state *crtc_state,
3381 const struct intel_plane_state *plane_state)
6156a456 3382{
a8d201af 3383 struct drm_device *dev = plane->dev;
fac5e23e 3384 struct drm_i915_private *dev_priv = to_i915(dev);
a8d201af
ML
3385 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3386 struct drm_framebuffer *fb = plane_state->base.fb;
62e0fb88 3387 const struct skl_wm_values *wm = &dev_priv->wm.skl_results;
6156a456 3388 int pipe = intel_crtc->pipe;
d2196774 3389 u32 plane_ctl;
a8d201af 3390 unsigned int rotation = plane_state->base.rotation;
d2196774 3391 u32 stride = skl_plane_stride(fb, 0, rotation);
b63a16f6 3392 u32 surf_addr = plane_state->main.offset;
a8d201af 3393 int scaler_id = plane_state->scaler_id;
b63a16f6
VS
3394 int src_x = plane_state->main.x;
3395 int src_y = plane_state->main.y;
936e71e3
VS
3396 int src_w = drm_rect_width(&plane_state->base.src) >> 16;
3397 int src_h = drm_rect_height(&plane_state->base.src) >> 16;
3398 int dst_x = plane_state->base.dst.x1;
3399 int dst_y = plane_state->base.dst.y1;
3400 int dst_w = drm_rect_width(&plane_state->base.dst);
3401 int dst_h = drm_rect_height(&plane_state->base.dst);
70d21f0e 3402
6156a456
CK
3403 plane_ctl = PLANE_CTL_ENABLE |
3404 PLANE_CTL_PIPE_GAMMA_ENABLE |
3405 PLANE_CTL_PIPE_CSC_ENABLE;
3406
3407 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3408 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
3409 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
6156a456
CK
3410 plane_ctl |= skl_plane_ctl_rotation(rotation);
3411
6687c906
VS
3412 /* Sizes are 0 based */
3413 src_w--;
3414 src_h--;
3415 dst_w--;
3416 dst_h--;
3417
4c0b8a8b
PZ
3418 intel_crtc->dspaddr_offset = surf_addr;
3419
6687c906
VS
3420 intel_crtc->adjusted_x = src_x;
3421 intel_crtc->adjusted_y = src_y;
2db3366b 3422
62e0fb88
L
3423 if (wm->dirty_pipes & drm_crtc_mask(&intel_crtc->base))
3424 skl_write_plane_wm(intel_crtc, wm, 0);
3425
70d21f0e 3426 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
6687c906 3427 I915_WRITE(PLANE_OFFSET(pipe, 0), (src_y << 16) | src_x);
ef78ec94 3428 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
6687c906 3429 I915_WRITE(PLANE_SIZE(pipe, 0), (src_h << 16) | src_w);
6156a456
CK
3430
3431 if (scaler_id >= 0) {
3432 uint32_t ps_ctrl = 0;
3433
3434 WARN_ON(!dst_w || !dst_h);
3435 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3436 crtc_state->scaler_state.scalers[scaler_id].mode;
3437 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3438 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3439 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3440 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3441 I915_WRITE(PLANE_POS(pipe, 0), 0);
3442 } else {
3443 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3444 }
3445
6687c906
VS
3446 I915_WRITE(PLANE_SURF(pipe, 0),
3447 intel_fb_gtt_offset(fb, rotation) + surf_addr);
70d21f0e
DL
3448
3449 POSTING_READ(PLANE_SURF(pipe, 0));
3450}
3451
a8d201af
ML
3452static void skylake_disable_primary_plane(struct drm_plane *primary,
3453 struct drm_crtc *crtc)
17638cd6
JB
3454{
3455 struct drm_device *dev = crtc->dev;
fac5e23e 3456 struct drm_i915_private *dev_priv = to_i915(dev);
62e0fb88
L
3457 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3458 int pipe = intel_crtc->pipe;
3459
ccebc23b
L
3460 /*
3461 * We only populate skl_results on watermark updates, and if the
3462 * plane's visiblity isn't actually changing neither is its watermarks.
3463 */
3464 if (!crtc->primary->state->visible)
3465 skl_write_plane_wm(intel_crtc, &dev_priv->wm.skl_results, 0);
17638cd6 3466
a8d201af
ML
3467 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3468 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3469 POSTING_READ(PLANE_SURF(pipe, 0));
3470}
29b9bde6 3471
a8d201af
ML
3472/* Assume fb object is pinned & idle & fenced and just update base pointers */
3473static int
3474intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3475 int x, int y, enum mode_set_atomic state)
3476{
3477 /* Support for kgdboc is disabled, this needs a major rework. */
3478 DRM_ERROR("legacy panic handler not supported any more.\n");
3479
3480 return -ENODEV;
81255565
JB
3481}
3482
5a21b665
SV
3483static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
3484{
3485 struct intel_crtc *crtc;
3486
91c8a326 3487 for_each_intel_crtc(&dev_priv->drm, crtc)
5a21b665
SV
3488 intel_finish_page_flip_cs(dev_priv, crtc->pipe);
3489}
3490
7514747d
VS
3491static void intel_update_primary_planes(struct drm_device *dev)
3492{
7514747d 3493 struct drm_crtc *crtc;
96a02917 3494
70e1e0ec 3495 for_each_crtc(dev, crtc) {
11c22da6 3496 struct intel_plane *plane = to_intel_plane(crtc->primary);
73974893
ML
3497 struct intel_plane_state *plane_state =
3498 to_intel_plane_state(plane->base.state);
11c22da6 3499
936e71e3 3500 if (plane_state->base.visible)
a8d201af
ML
3501 plane->update_plane(&plane->base,
3502 to_intel_crtc_state(crtc->state),
3503 plane_state);
73974893
ML
3504 }
3505}
3506
3507static int
3508__intel_display_resume(struct drm_device *dev,
3509 struct drm_atomic_state *state)
3510{
3511 struct drm_crtc_state *crtc_state;
3512 struct drm_crtc *crtc;
3513 int i, ret;
11c22da6 3514
73974893
ML
3515 intel_modeset_setup_hw_state(dev);
3516 i915_redisable_vga(dev);
3517
3518 if (!state)
3519 return 0;
3520
3521 for_each_crtc_in_state(state, crtc, crtc_state, i) {
3522 /*
3523 * Force recalculation even if we restore
3524 * current state. With fast modeset this may not result
3525 * in a modeset when the state is compatible.
3526 */
3527 crtc_state->mode_changed = true;
96a02917 3528 }
73974893
ML
3529
3530 /* ignore any reset values/BIOS leftovers in the WM registers */
3531 to_intel_atomic_state(state)->skip_intermediate_wm = true;
3532
3533 ret = drm_atomic_commit(state);
3534
3535 WARN_ON(ret == -EDEADLK);
3536 return ret;
96a02917
VS
3537}
3538
4ac2ba2f
VS
3539static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
3540{
ae98104b
VS
3541 return intel_has_gpu_reset(dev_priv) &&
3542 INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
4ac2ba2f
VS
3543}
3544
c033666a 3545void intel_prepare_reset(struct drm_i915_private *dev_priv)
7514747d 3546{
73974893
ML
3547 struct drm_device *dev = &dev_priv->drm;
3548 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
3549 struct drm_atomic_state *state;
3550 int ret;
3551
73974893
ML
3552 /*
3553 * Need mode_config.mutex so that we don't
3554 * trample ongoing ->detect() and whatnot.
3555 */
3556 mutex_lock(&dev->mode_config.mutex);
3557 drm_modeset_acquire_init(ctx, 0);
3558 while (1) {
3559 ret = drm_modeset_lock_all_ctx(dev, ctx);
3560 if (ret != -EDEADLK)
3561 break;
3562
3563 drm_modeset_backoff(ctx);
3564 }
3565
3566 /* reset doesn't touch the display, but flips might get nuked anyway, */
522a63de 3567 if (!i915.force_reset_modeset_test &&
4ac2ba2f 3568 !gpu_reset_clobbers_display(dev_priv))
7514747d
VS
3569 return;
3570
f98ce92f
VS
3571 /*
3572 * Disabling the crtcs gracefully seems nicer. Also the
3573 * g33 docs say we should at least disable all the planes.
3574 */
73974893
ML
3575 state = drm_atomic_helper_duplicate_state(dev, ctx);
3576 if (IS_ERR(state)) {
3577 ret = PTR_ERR(state);
3578 state = NULL;
3579 DRM_ERROR("Duplicating state failed with %i\n", ret);
3580 goto err;
3581 }
3582
3583 ret = drm_atomic_helper_disable_all(dev, ctx);
3584 if (ret) {
3585 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
3586 goto err;
3587 }
3588
3589 dev_priv->modeset_restore_state = state;
3590 state->acquire_ctx = ctx;
3591 return;
3592
3593err:
3594 drm_atomic_state_free(state);
7514747d
VS
3595}
3596
c033666a 3597void intel_finish_reset(struct drm_i915_private *dev_priv)
7514747d 3598{
73974893
ML
3599 struct drm_device *dev = &dev_priv->drm;
3600 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
3601 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
3602 int ret;
3603
5a21b665
SV
3604 /*
3605 * Flips in the rings will be nuked by the reset,
3606 * so complete all pending flips so that user space
3607 * will get its events and not get stuck.
3608 */
3609 intel_complete_page_flips(dev_priv);
3610
73974893
ML
3611 dev_priv->modeset_restore_state = NULL;
3612
7514747d 3613 /* reset doesn't touch the display */
4ac2ba2f 3614 if (!gpu_reset_clobbers_display(dev_priv)) {
522a63de
ML
3615 if (!state) {
3616 /*
3617 * Flips in the rings have been nuked by the reset,
3618 * so update the base address of all primary
3619 * planes to the the last fb to make sure we're
3620 * showing the correct fb after a reset.
3621 *
3622 * FIXME: Atomic will make this obsolete since we won't schedule
3623 * CS-based flips (which might get lost in gpu resets) any more.
3624 */
3625 intel_update_primary_planes(dev);
3626 } else {
3627 ret = __intel_display_resume(dev, state);
3628 if (ret)
3629 DRM_ERROR("Restoring old state failed with %i\n", ret);
3630 }
73974893
ML
3631 } else {
3632 /*
3633 * The display has been reset as well,
3634 * so need a full re-initialization.
3635 */
3636 intel_runtime_pm_disable_interrupts(dev_priv);
3637 intel_runtime_pm_enable_interrupts(dev_priv);
7514747d 3638
51f59205 3639 intel_pps_unlock_regs_wa(dev_priv);
73974893 3640 intel_modeset_init_hw(dev);
7514747d 3641
73974893
ML
3642 spin_lock_irq(&dev_priv->irq_lock);
3643 if (dev_priv->display.hpd_irq_setup)
3644 dev_priv->display.hpd_irq_setup(dev_priv);
3645 spin_unlock_irq(&dev_priv->irq_lock);
7514747d 3646
73974893
ML
3647 ret = __intel_display_resume(dev, state);
3648 if (ret)
3649 DRM_ERROR("Restoring old state failed with %i\n", ret);
7514747d 3650
73974893
ML
3651 intel_hpd_init(dev_priv);
3652 }
7514747d 3653
73974893
ML
3654 drm_modeset_drop_locks(ctx);
3655 drm_modeset_acquire_fini(ctx);
3656 mutex_unlock(&dev->mode_config.mutex);
7514747d
VS
3657}
3658
8af29b0c
CW
3659static bool abort_flip_on_reset(struct intel_crtc *crtc)
3660{
3661 struct i915_gpu_error *error = &to_i915(crtc->base.dev)->gpu_error;
3662
3663 if (i915_reset_in_progress(error))
3664 return true;
3665
3666 if (crtc->reset_count != i915_reset_count(error))
3667 return true;
3668
3669 return false;
3670}
3671
7d5e3799
CW
3672static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3673{
5a21b665
SV
3674 struct drm_device *dev = crtc->dev;
3675 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5a21b665
SV
3676 bool pending;
3677
8af29b0c 3678 if (abort_flip_on_reset(intel_crtc))
5a21b665
SV
3679 return false;
3680
3681 spin_lock_irq(&dev->event_lock);
3682 pending = to_intel_crtc(crtc)->flip_work != NULL;
3683 spin_unlock_irq(&dev->event_lock);
3684
3685 return pending;
7d5e3799
CW
3686}
3687
bfd16b2a
ML
3688static void intel_update_pipe_config(struct intel_crtc *crtc,
3689 struct intel_crtc_state *old_crtc_state)
e30e8f75
GP
3690{
3691 struct drm_device *dev = crtc->base.dev;
fac5e23e 3692 struct drm_i915_private *dev_priv = to_i915(dev);
bfd16b2a
ML
3693 struct intel_crtc_state *pipe_config =
3694 to_intel_crtc_state(crtc->base.state);
e30e8f75 3695
bfd16b2a
ML
3696 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3697 crtc->base.mode = crtc->base.state->mode;
3698
3699 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3700 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3701 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
e30e8f75
GP
3702
3703 /*
3704 * Update pipe size and adjust fitter if needed: the reason for this is
3705 * that in compute_mode_changes we check the native mode (not the pfit
3706 * mode) to see if we can flip rather than do a full mode set. In the
3707 * fastboot case, we'll flip, but if we don't update the pipesrc and
3708 * pfit state, we'll end up with a big fb scanned out into the wrong
3709 * sized surface.
e30e8f75
GP
3710 */
3711
e30e8f75 3712 I915_WRITE(PIPESRC(crtc->pipe),
bfd16b2a
ML
3713 ((pipe_config->pipe_src_w - 1) << 16) |
3714 (pipe_config->pipe_src_h - 1));
3715
3716 /* on skylake this is done by detaching scalers */
3717 if (INTEL_INFO(dev)->gen >= 9) {
3718 skl_detach_scalers(crtc);
3719
3720 if (pipe_config->pch_pfit.enabled)
3721 skylake_pfit_enable(crtc);
6e266956 3722 } else if (HAS_PCH_SPLIT(dev_priv)) {
bfd16b2a
ML
3723 if (pipe_config->pch_pfit.enabled)
3724 ironlake_pfit_enable(crtc);
3725 else if (old_crtc_state->pch_pfit.enabled)
3726 ironlake_pfit_disable(crtc, true);
e30e8f75 3727 }
e30e8f75
GP
3728}
3729
5e84e1a4
ZW
3730static void intel_fdi_normal_train(struct drm_crtc *crtc)
3731{
3732 struct drm_device *dev = crtc->dev;
fac5e23e 3733 struct drm_i915_private *dev_priv = to_i915(dev);
5e84e1a4
ZW
3734 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3735 int pipe = intel_crtc->pipe;
f0f59a00
VS
3736 i915_reg_t reg;
3737 u32 temp;
5e84e1a4
ZW
3738
3739 /* enable normal train */
3740 reg = FDI_TX_CTL(pipe);
3741 temp = I915_READ(reg);
fd6b8f43 3742 if (IS_IVYBRIDGE(dev_priv)) {
357555c0
JB
3743 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3744 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
3745 } else {
3746 temp &= ~FDI_LINK_TRAIN_NONE;
3747 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 3748 }
5e84e1a4
ZW
3749 I915_WRITE(reg, temp);
3750
3751 reg = FDI_RX_CTL(pipe);
3752 temp = I915_READ(reg);
6e266956 3753 if (HAS_PCH_CPT(dev_priv)) {
5e84e1a4
ZW
3754 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3755 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3756 } else {
3757 temp &= ~FDI_LINK_TRAIN_NONE;
3758 temp |= FDI_LINK_TRAIN_NONE;
3759 }
3760 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3761
3762 /* wait one idle pattern time */
3763 POSTING_READ(reg);
3764 udelay(1000);
357555c0
JB
3765
3766 /* IVB wants error correction enabled */
fd6b8f43 3767 if (IS_IVYBRIDGE(dev_priv))
357555c0
JB
3768 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3769 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
3770}
3771
8db9d77b
ZW
3772/* The FDI link training functions for ILK/Ibexpeak. */
3773static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3774{
3775 struct drm_device *dev = crtc->dev;
fac5e23e 3776 struct drm_i915_private *dev_priv = to_i915(dev);
8db9d77b
ZW
3777 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3778 int pipe = intel_crtc->pipe;
f0f59a00
VS
3779 i915_reg_t reg;
3780 u32 temp, tries;
8db9d77b 3781
1c8562f6 3782 /* FDI needs bits from pipe first */
0fc932b8 3783 assert_pipe_enabled(dev_priv, pipe);
0fc932b8 3784
e1a44743
AJ
3785 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3786 for train result */
5eddb70b
CW
3787 reg = FDI_RX_IMR(pipe);
3788 temp = I915_READ(reg);
e1a44743
AJ
3789 temp &= ~FDI_RX_SYMBOL_LOCK;
3790 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3791 I915_WRITE(reg, temp);
3792 I915_READ(reg);
e1a44743
AJ
3793 udelay(150);
3794
8db9d77b 3795 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3796 reg = FDI_TX_CTL(pipe);
3797 temp = I915_READ(reg);
627eb5a3 3798 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3799 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3800 temp &= ~FDI_LINK_TRAIN_NONE;
3801 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 3802 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3803
5eddb70b
CW
3804 reg = FDI_RX_CTL(pipe);
3805 temp = I915_READ(reg);
8db9d77b
ZW
3806 temp &= ~FDI_LINK_TRAIN_NONE;
3807 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b
CW
3808 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3809
3810 POSTING_READ(reg);
8db9d77b
ZW
3811 udelay(150);
3812
5b2adf89 3813 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
SV
3814 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3815 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3816 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 3817
5eddb70b 3818 reg = FDI_RX_IIR(pipe);
e1a44743 3819 for (tries = 0; tries < 5; tries++) {
5eddb70b 3820 temp = I915_READ(reg);
8db9d77b
ZW
3821 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3822
3823 if ((temp & FDI_RX_BIT_LOCK)) {
3824 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 3825 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
3826 break;
3827 }
8db9d77b 3828 }
e1a44743 3829 if (tries == 5)
5eddb70b 3830 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3831
3832 /* Train 2 */
5eddb70b
CW
3833 reg = FDI_TX_CTL(pipe);
3834 temp = I915_READ(reg);
8db9d77b
ZW
3835 temp &= ~FDI_LINK_TRAIN_NONE;
3836 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3837 I915_WRITE(reg, temp);
8db9d77b 3838
5eddb70b
CW
3839 reg = FDI_RX_CTL(pipe);
3840 temp = I915_READ(reg);
8db9d77b
ZW
3841 temp &= ~FDI_LINK_TRAIN_NONE;
3842 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3843 I915_WRITE(reg, temp);
8db9d77b 3844
5eddb70b
CW
3845 POSTING_READ(reg);
3846 udelay(150);
8db9d77b 3847
5eddb70b 3848 reg = FDI_RX_IIR(pipe);
e1a44743 3849 for (tries = 0; tries < 5; tries++) {
5eddb70b 3850 temp = I915_READ(reg);
8db9d77b
ZW
3851 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3852
3853 if (temp & FDI_RX_SYMBOL_LOCK) {
5eddb70b 3854 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
3855 DRM_DEBUG_KMS("FDI train 2 done.\n");
3856 break;
3857 }
8db9d77b 3858 }
e1a44743 3859 if (tries == 5)
5eddb70b 3860 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3861
3862 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 3863
8db9d77b
ZW
3864}
3865
0206e353 3866static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
3867 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3868 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3869 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3870 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3871};
3872
3873/* The FDI link training functions for SNB/Cougarpoint. */
3874static void gen6_fdi_link_train(struct drm_crtc *crtc)
3875{
3876 struct drm_device *dev = crtc->dev;
fac5e23e 3877 struct drm_i915_private *dev_priv = to_i915(dev);
8db9d77b
ZW
3878 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3879 int pipe = intel_crtc->pipe;
f0f59a00
VS
3880 i915_reg_t reg;
3881 u32 temp, i, retry;
8db9d77b 3882
e1a44743
AJ
3883 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3884 for train result */
5eddb70b
CW
3885 reg = FDI_RX_IMR(pipe);
3886 temp = I915_READ(reg);
e1a44743
AJ
3887 temp &= ~FDI_RX_SYMBOL_LOCK;
3888 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3889 I915_WRITE(reg, temp);
3890
3891 POSTING_READ(reg);
e1a44743
AJ
3892 udelay(150);
3893
8db9d77b 3894 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3895 reg = FDI_TX_CTL(pipe);
3896 temp = I915_READ(reg);
627eb5a3 3897 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3898 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3899 temp &= ~FDI_LINK_TRAIN_NONE;
3900 temp |= FDI_LINK_TRAIN_PATTERN_1;
3901 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3902 /* SNB-B */
3903 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 3904 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3905
d74cf324
SV
3906 I915_WRITE(FDI_RX_MISC(pipe),
3907 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3908
5eddb70b
CW
3909 reg = FDI_RX_CTL(pipe);
3910 temp = I915_READ(reg);
6e266956 3911 if (HAS_PCH_CPT(dev_priv)) {
8db9d77b
ZW
3912 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3913 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3914 } else {
3915 temp &= ~FDI_LINK_TRAIN_NONE;
3916 temp |= FDI_LINK_TRAIN_PATTERN_1;
3917 }
5eddb70b
CW
3918 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3919
3920 POSTING_READ(reg);
8db9d77b
ZW
3921 udelay(150);
3922
0206e353 3923 for (i = 0; i < 4; i++) {
5eddb70b
CW
3924 reg = FDI_TX_CTL(pipe);
3925 temp = I915_READ(reg);
8db9d77b
ZW
3926 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3927 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3928 I915_WRITE(reg, temp);
3929
3930 POSTING_READ(reg);
8db9d77b
ZW
3931 udelay(500);
3932
fa37d39e
SP
3933 for (retry = 0; retry < 5; retry++) {
3934 reg = FDI_RX_IIR(pipe);
3935 temp = I915_READ(reg);
3936 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3937 if (temp & FDI_RX_BIT_LOCK) {
3938 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3939 DRM_DEBUG_KMS("FDI train 1 done.\n");
3940 break;
3941 }
3942 udelay(50);
8db9d77b 3943 }
fa37d39e
SP
3944 if (retry < 5)
3945 break;
8db9d77b
ZW
3946 }
3947 if (i == 4)
5eddb70b 3948 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3949
3950 /* Train 2 */
5eddb70b
CW
3951 reg = FDI_TX_CTL(pipe);
3952 temp = I915_READ(reg);
8db9d77b
ZW
3953 temp &= ~FDI_LINK_TRAIN_NONE;
3954 temp |= FDI_LINK_TRAIN_PATTERN_2;
5db94019 3955 if (IS_GEN6(dev_priv)) {
8db9d77b
ZW
3956 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3957 /* SNB-B */
3958 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3959 }
5eddb70b 3960 I915_WRITE(reg, temp);
8db9d77b 3961
5eddb70b
CW
3962 reg = FDI_RX_CTL(pipe);
3963 temp = I915_READ(reg);
6e266956 3964 if (HAS_PCH_CPT(dev_priv)) {
8db9d77b
ZW
3965 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3966 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3967 } else {
3968 temp &= ~FDI_LINK_TRAIN_NONE;
3969 temp |= FDI_LINK_TRAIN_PATTERN_2;
3970 }
5eddb70b
CW
3971 I915_WRITE(reg, temp);
3972
3973 POSTING_READ(reg);
8db9d77b
ZW
3974 udelay(150);
3975
0206e353 3976 for (i = 0; i < 4; i++) {
5eddb70b
CW
3977 reg = FDI_TX_CTL(pipe);
3978 temp = I915_READ(reg);
8db9d77b
ZW
3979 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3980 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3981 I915_WRITE(reg, temp);
3982
3983 POSTING_READ(reg);
8db9d77b
ZW
3984 udelay(500);
3985
fa37d39e
SP
3986 for (retry = 0; retry < 5; retry++) {
3987 reg = FDI_RX_IIR(pipe);
3988 temp = I915_READ(reg);
3989 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3990 if (temp & FDI_RX_SYMBOL_LOCK) {
3991 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3992 DRM_DEBUG_KMS("FDI train 2 done.\n");
3993 break;
3994 }
3995 udelay(50);
8db9d77b 3996 }
fa37d39e
SP
3997 if (retry < 5)
3998 break;
8db9d77b
ZW
3999 }
4000 if (i == 4)
5eddb70b 4001 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
4002
4003 DRM_DEBUG_KMS("FDI train done.\n");
4004}
4005
357555c0
JB
4006/* Manual link training for Ivy Bridge A0 parts */
4007static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
4008{
4009 struct drm_device *dev = crtc->dev;
fac5e23e 4010 struct drm_i915_private *dev_priv = to_i915(dev);
357555c0
JB
4011 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4012 int pipe = intel_crtc->pipe;
f0f59a00
VS
4013 i915_reg_t reg;
4014 u32 temp, i, j;
357555c0
JB
4015
4016 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
4017 for train result */
4018 reg = FDI_RX_IMR(pipe);
4019 temp = I915_READ(reg);
4020 temp &= ~FDI_RX_SYMBOL_LOCK;
4021 temp &= ~FDI_RX_BIT_LOCK;
4022 I915_WRITE(reg, temp);
4023
4024 POSTING_READ(reg);
4025 udelay(150);
4026
01a415fd
SV
4027 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
4028 I915_READ(FDI_RX_IIR(pipe)));
4029
139ccd3f
JB
4030 /* Try each vswing and preemphasis setting twice before moving on */
4031 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
4032 /* disable first in case we need to retry */
4033 reg = FDI_TX_CTL(pipe);
4034 temp = I915_READ(reg);
4035 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
4036 temp &= ~FDI_TX_ENABLE;
4037 I915_WRITE(reg, temp);
357555c0 4038
139ccd3f
JB
4039 reg = FDI_RX_CTL(pipe);
4040 temp = I915_READ(reg);
4041 temp &= ~FDI_LINK_TRAIN_AUTO;
4042 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4043 temp &= ~FDI_RX_ENABLE;
4044 I915_WRITE(reg, temp);
357555c0 4045
139ccd3f 4046 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
4047 reg = FDI_TX_CTL(pipe);
4048 temp = I915_READ(reg);
139ccd3f 4049 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 4050 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
139ccd3f 4051 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 4052 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
4053 temp |= snb_b_fdi_train_param[j/2];
4054 temp |= FDI_COMPOSITE_SYNC;
4055 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 4056
139ccd3f
JB
4057 I915_WRITE(FDI_RX_MISC(pipe),
4058 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 4059
139ccd3f 4060 reg = FDI_RX_CTL(pipe);
357555c0 4061 temp = I915_READ(reg);
139ccd3f
JB
4062 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
4063 temp |= FDI_COMPOSITE_SYNC;
4064 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 4065
139ccd3f
JB
4066 POSTING_READ(reg);
4067 udelay(1); /* should be 0.5us */
357555c0 4068
139ccd3f
JB
4069 for (i = 0; i < 4; i++) {
4070 reg = FDI_RX_IIR(pipe);
4071 temp = I915_READ(reg);
4072 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 4073
139ccd3f
JB
4074 if (temp & FDI_RX_BIT_LOCK ||
4075 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
4076 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
4077 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
4078 i);
4079 break;
4080 }
4081 udelay(1); /* should be 0.5us */
4082 }
4083 if (i == 4) {
4084 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
4085 continue;
4086 }
357555c0 4087
139ccd3f 4088 /* Train 2 */
357555c0
JB
4089 reg = FDI_TX_CTL(pipe);
4090 temp = I915_READ(reg);
139ccd3f
JB
4091 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
4092 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
4093 I915_WRITE(reg, temp);
4094
4095 reg = FDI_RX_CTL(pipe);
4096 temp = I915_READ(reg);
4097 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4098 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
4099 I915_WRITE(reg, temp);
4100
4101 POSTING_READ(reg);
139ccd3f 4102 udelay(2); /* should be 1.5us */
357555c0 4103
139ccd3f
JB
4104 for (i = 0; i < 4; i++) {
4105 reg = FDI_RX_IIR(pipe);
4106 temp = I915_READ(reg);
4107 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 4108
139ccd3f
JB
4109 if (temp & FDI_RX_SYMBOL_LOCK ||
4110 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
4111 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
4112 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
4113 i);
4114 goto train_done;
4115 }
4116 udelay(2); /* should be 1.5us */
357555c0 4117 }
139ccd3f
JB
4118 if (i == 4)
4119 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 4120 }
357555c0 4121
139ccd3f 4122train_done:
357555c0
JB
4123 DRM_DEBUG_KMS("FDI train done.\n");
4124}
4125
88cefb6c 4126static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 4127{
88cefb6c 4128 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 4129 struct drm_i915_private *dev_priv = to_i915(dev);
2c07245f 4130 int pipe = intel_crtc->pipe;
f0f59a00
VS
4131 i915_reg_t reg;
4132 u32 temp;
c64e311e 4133
c98e9dcf 4134 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
4135 reg = FDI_RX_CTL(pipe);
4136 temp = I915_READ(reg);
627eb5a3 4137 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
6e3c9717 4138 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
dfd07d72 4139 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
4140 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
4141
4142 POSTING_READ(reg);
c98e9dcf
JB
4143 udelay(200);
4144
4145 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
4146 temp = I915_READ(reg);
4147 I915_WRITE(reg, temp | FDI_PCDCLK);
4148
4149 POSTING_READ(reg);
c98e9dcf
JB
4150 udelay(200);
4151
20749730
PZ
4152 /* Enable CPU FDI TX PLL, always on for Ironlake */
4153 reg = FDI_TX_CTL(pipe);
4154 temp = I915_READ(reg);
4155 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
4156 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 4157
20749730
PZ
4158 POSTING_READ(reg);
4159 udelay(100);
6be4a607 4160 }
0e23b99d
JB
4161}
4162
88cefb6c
SV
4163static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
4164{
4165 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 4166 struct drm_i915_private *dev_priv = to_i915(dev);
88cefb6c 4167 int pipe = intel_crtc->pipe;
f0f59a00
VS
4168 i915_reg_t reg;
4169 u32 temp;
88cefb6c
SV
4170
4171 /* Switch from PCDclk to Rawclk */
4172 reg = FDI_RX_CTL(pipe);
4173 temp = I915_READ(reg);
4174 I915_WRITE(reg, temp & ~FDI_PCDCLK);
4175
4176 /* Disable CPU FDI TX PLL */
4177 reg = FDI_TX_CTL(pipe);
4178 temp = I915_READ(reg);
4179 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
4180
4181 POSTING_READ(reg);
4182 udelay(100);
4183
4184 reg = FDI_RX_CTL(pipe);
4185 temp = I915_READ(reg);
4186 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
4187
4188 /* Wait for the clocks to turn off. */
4189 POSTING_READ(reg);
4190 udelay(100);
4191}
4192
0fc932b8
JB
4193static void ironlake_fdi_disable(struct drm_crtc *crtc)
4194{
4195 struct drm_device *dev = crtc->dev;
fac5e23e 4196 struct drm_i915_private *dev_priv = to_i915(dev);
0fc932b8
JB
4197 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4198 int pipe = intel_crtc->pipe;
f0f59a00
VS
4199 i915_reg_t reg;
4200 u32 temp;
0fc932b8
JB
4201
4202 /* disable CPU FDI tx and PCH FDI rx */
4203 reg = FDI_TX_CTL(pipe);
4204 temp = I915_READ(reg);
4205 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
4206 POSTING_READ(reg);
4207
4208 reg = FDI_RX_CTL(pipe);
4209 temp = I915_READ(reg);
4210 temp &= ~(0x7 << 16);
dfd07d72 4211 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
4212 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
4213
4214 POSTING_READ(reg);
4215 udelay(100);
4216
4217 /* Ironlake workaround, disable clock pointer after downing FDI */
6e266956 4218 if (HAS_PCH_IBX(dev_priv))
6f06ce18 4219 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
0fc932b8
JB
4220
4221 /* still set train pattern 1 */
4222 reg = FDI_TX_CTL(pipe);
4223 temp = I915_READ(reg);
4224 temp &= ~FDI_LINK_TRAIN_NONE;
4225 temp |= FDI_LINK_TRAIN_PATTERN_1;
4226 I915_WRITE(reg, temp);
4227
4228 reg = FDI_RX_CTL(pipe);
4229 temp = I915_READ(reg);
6e266956 4230 if (HAS_PCH_CPT(dev_priv)) {
0fc932b8
JB
4231 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4232 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
4233 } else {
4234 temp &= ~FDI_LINK_TRAIN_NONE;
4235 temp |= FDI_LINK_TRAIN_PATTERN_1;
4236 }
4237 /* BPC in FDI rx is consistent with that in PIPECONF */
4238 temp &= ~(0x07 << 16);
dfd07d72 4239 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
4240 I915_WRITE(reg, temp);
4241
4242 POSTING_READ(reg);
4243 udelay(100);
4244}
4245
5dce5b93
CW
4246bool intel_has_pending_fb_unpin(struct drm_device *dev)
4247{
4248 struct intel_crtc *crtc;
4249
4250 /* Note that we don't need to be called with mode_config.lock here
4251 * as our list of CRTC objects is static for the lifetime of the
4252 * device and so cannot disappear as we iterate. Similarly, we can
4253 * happily treat the predicates as racy, atomic checks as userspace
4254 * cannot claim and pin a new fb without at least acquring the
4255 * struct_mutex and so serialising with us.
4256 */
d3fcc808 4257 for_each_intel_crtc(dev, crtc) {
5dce5b93
CW
4258 if (atomic_read(&crtc->unpin_work_count) == 0)
4259 continue;
4260
5a21b665 4261 if (crtc->flip_work)
5dce5b93
CW
4262 intel_wait_for_vblank(dev, crtc->pipe);
4263
4264 return true;
4265 }
4266
4267 return false;
4268}
4269
5a21b665 4270static void page_flip_completed(struct intel_crtc *intel_crtc)
d6bbafa1
CW
4271{
4272 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
5a21b665
SV
4273 struct intel_flip_work *work = intel_crtc->flip_work;
4274
4275 intel_crtc->flip_work = NULL;
d6bbafa1
CW
4276
4277 if (work->event)
560ce1dc 4278 drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
d6bbafa1
CW
4279
4280 drm_crtc_vblank_put(&intel_crtc->base);
4281
5a21b665 4282 wake_up_all(&dev_priv->pending_flip_queue);
143f73b3 4283 queue_work(dev_priv->wq, &work->unpin_work);
5a21b665
SV
4284
4285 trace_i915_flip_complete(intel_crtc->plane,
4286 work->pending_flip_obj);
d6bbafa1
CW
4287}
4288
5008e874 4289static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
e6c3a2a6 4290{
0f91128d 4291 struct drm_device *dev = crtc->dev;
fac5e23e 4292 struct drm_i915_private *dev_priv = to_i915(dev);
5008e874 4293 long ret;
e6c3a2a6 4294
2c10d571 4295 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
5008e874
ML
4296
4297 ret = wait_event_interruptible_timeout(
4298 dev_priv->pending_flip_queue,
4299 !intel_crtc_has_pending_flip(crtc),
4300 60*HZ);
4301
4302 if (ret < 0)
4303 return ret;
4304
5a21b665
SV
4305 if (ret == 0) {
4306 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4307 struct intel_flip_work *work;
4308
4309 spin_lock_irq(&dev->event_lock);
4310 work = intel_crtc->flip_work;
4311 if (work && !is_mmio_work(work)) {
4312 WARN_ONCE(1, "Removing stuck page flip\n");
4313 page_flip_completed(intel_crtc);
4314 }
4315 spin_unlock_irq(&dev->event_lock);
4316 }
5bb61643 4317
5008e874 4318 return 0;
e6c3a2a6
CW
4319}
4320
b7076546 4321void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
060f02d8
VS
4322{
4323 u32 temp;
4324
4325 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
4326
4327 mutex_lock(&dev_priv->sb_lock);
4328
4329 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4330 temp |= SBI_SSCCTL_DISABLE;
4331 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
4332
4333 mutex_unlock(&dev_priv->sb_lock);
4334}
4335
e615efe4
ED
4336/* Program iCLKIP clock to the desired frequency */
4337static void lpt_program_iclkip(struct drm_crtc *crtc)
4338{
64b46a06 4339 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6e3c9717 4340 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
e615efe4
ED
4341 u32 divsel, phaseinc, auxdiv, phasedir = 0;
4342 u32 temp;
4343
060f02d8 4344 lpt_disable_iclkip(dev_priv);
e615efe4 4345
64b46a06
VS
4346 /* The iCLK virtual clock root frequency is in MHz,
4347 * but the adjusted_mode->crtc_clock in in KHz. To get the
4348 * divisors, it is necessary to divide one by another, so we
4349 * convert the virtual clock precision to KHz here for higher
4350 * precision.
4351 */
4352 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
e615efe4
ED
4353 u32 iclk_virtual_root_freq = 172800 * 1000;
4354 u32 iclk_pi_range = 64;
64b46a06 4355 u32 desired_divisor;
e615efe4 4356
64b46a06
VS
4357 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4358 clock << auxdiv);
4359 divsel = (desired_divisor / iclk_pi_range) - 2;
4360 phaseinc = desired_divisor % iclk_pi_range;
e615efe4 4361
64b46a06
VS
4362 /*
4363 * Near 20MHz is a corner case which is
4364 * out of range for the 7-bit divisor
4365 */
4366 if (divsel <= 0x7f)
4367 break;
e615efe4
ED
4368 }
4369
4370 /* This should not happen with any sane values */
4371 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
4372 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
4373 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
4374 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
4375
4376 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 4377 clock,
e615efe4
ED
4378 auxdiv,
4379 divsel,
4380 phasedir,
4381 phaseinc);
4382
060f02d8
VS
4383 mutex_lock(&dev_priv->sb_lock);
4384
e615efe4 4385 /* Program SSCDIVINTPHASE6 */
988d6ee8 4386 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
4387 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
4388 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
4389 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
4390 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
4391 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
4392 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 4393 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
4394
4395 /* Program SSCAUXDIV */
988d6ee8 4396 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
4397 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
4398 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 4399 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
4400
4401 /* Enable modulator and associated divider */
988d6ee8 4402 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 4403 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 4404 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4 4405
060f02d8
VS
4406 mutex_unlock(&dev_priv->sb_lock);
4407
e615efe4
ED
4408 /* Wait for initialization time */
4409 udelay(24);
4410
4411 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
4412}
4413
8802e5b6
VS
4414int lpt_get_iclkip(struct drm_i915_private *dev_priv)
4415{
4416 u32 divsel, phaseinc, auxdiv;
4417 u32 iclk_virtual_root_freq = 172800 * 1000;
4418 u32 iclk_pi_range = 64;
4419 u32 desired_divisor;
4420 u32 temp;
4421
4422 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
4423 return 0;
4424
4425 mutex_lock(&dev_priv->sb_lock);
4426
4427 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4428 if (temp & SBI_SSCCTL_DISABLE) {
4429 mutex_unlock(&dev_priv->sb_lock);
4430 return 0;
4431 }
4432
4433 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
4434 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
4435 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
4436 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
4437 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
4438
4439 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
4440 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
4441 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
4442
4443 mutex_unlock(&dev_priv->sb_lock);
4444
4445 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
4446
4447 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4448 desired_divisor << auxdiv);
4449}
4450
275f01b2
SV
4451static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4452 enum pipe pch_transcoder)
4453{
4454 struct drm_device *dev = crtc->base.dev;
fac5e23e 4455 struct drm_i915_private *dev_priv = to_i915(dev);
6e3c9717 4456 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
275f01b2
SV
4457
4458 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4459 I915_READ(HTOTAL(cpu_transcoder)));
4460 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4461 I915_READ(HBLANK(cpu_transcoder)));
4462 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4463 I915_READ(HSYNC(cpu_transcoder)));
4464
4465 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4466 I915_READ(VTOTAL(cpu_transcoder)));
4467 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4468 I915_READ(VBLANK(cpu_transcoder)));
4469 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4470 I915_READ(VSYNC(cpu_transcoder)));
4471 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4472 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4473}
4474
003632d9 4475static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
1fbc0d78 4476{
fac5e23e 4477 struct drm_i915_private *dev_priv = to_i915(dev);
1fbc0d78
SV
4478 uint32_t temp;
4479
4480 temp = I915_READ(SOUTH_CHICKEN1);
003632d9 4481 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
1fbc0d78
SV
4482 return;
4483
4484 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4485 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4486
003632d9
ACO
4487 temp &= ~FDI_BC_BIFURCATION_SELECT;
4488 if (enable)
4489 temp |= FDI_BC_BIFURCATION_SELECT;
4490
4491 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
1fbc0d78
SV
4492 I915_WRITE(SOUTH_CHICKEN1, temp);
4493 POSTING_READ(SOUTH_CHICKEN1);
4494}
4495
4496static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4497{
4498 struct drm_device *dev = intel_crtc->base.dev;
1fbc0d78
SV
4499
4500 switch (intel_crtc->pipe) {
4501 case PIPE_A:
4502 break;
4503 case PIPE_B:
6e3c9717 4504 if (intel_crtc->config->fdi_lanes > 2)
003632d9 4505 cpt_set_fdi_bc_bifurcation(dev, false);
1fbc0d78 4506 else
003632d9 4507 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
SV
4508
4509 break;
4510 case PIPE_C:
003632d9 4511 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
SV
4512
4513 break;
4514 default:
4515 BUG();
4516 }
4517}
4518
c48b5305
VS
4519/* Return which DP Port should be selected for Transcoder DP control */
4520static enum port
4521intel_trans_dp_port_sel(struct drm_crtc *crtc)
4522{
4523 struct drm_device *dev = crtc->dev;
4524 struct intel_encoder *encoder;
4525
4526 for_each_encoder_on_crtc(dev, crtc, encoder) {
cca0502b 4527 if (encoder->type == INTEL_OUTPUT_DP ||
c48b5305
VS
4528 encoder->type == INTEL_OUTPUT_EDP)
4529 return enc_to_dig_port(&encoder->base)->port;
4530 }
4531
4532 return -1;
4533}
4534
f67a559d
JB
4535/*
4536 * Enable PCH resources required for PCH ports:
4537 * - PCH PLLs
4538 * - FDI training & RX/TX
4539 * - update transcoder timings
4540 * - DP transcoding bits
4541 * - transcoder
4542 */
4543static void ironlake_pch_enable(struct drm_crtc *crtc)
0e23b99d
JB
4544{
4545 struct drm_device *dev = crtc->dev;
fac5e23e 4546 struct drm_i915_private *dev_priv = to_i915(dev);
0e23b99d
JB
4547 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4548 int pipe = intel_crtc->pipe;
f0f59a00 4549 u32 temp;
2c07245f 4550
ab9412ba 4551 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 4552
fd6b8f43 4553 if (IS_IVYBRIDGE(dev_priv))
1fbc0d78
SV
4554 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4555
cd986abb
SV
4556 /* Write the TU size bits before fdi link training, so that error
4557 * detection works. */
4558 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4559 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4560
c98e9dcf 4561 /* For PCH output, training FDI link */
674cf967 4562 dev_priv->display.fdi_link_train(crtc);
2c07245f 4563
3ad8a208
SV
4564 /* We need to program the right clock selection before writing the pixel
4565 * mutliplier into the DPLL. */
6e266956 4566 if (HAS_PCH_CPT(dev_priv)) {
ee7b9f93 4567 u32 sel;
4b645f14 4568
c98e9dcf 4569 temp = I915_READ(PCH_DPLL_SEL);
11887397
SV
4570 temp |= TRANS_DPLL_ENABLE(pipe);
4571 sel = TRANS_DPLLB_SEL(pipe);
8106ddbd
ACO
4572 if (intel_crtc->config->shared_dpll ==
4573 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
ee7b9f93
JB
4574 temp |= sel;
4575 else
4576 temp &= ~sel;
c98e9dcf 4577 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 4578 }
5eddb70b 4579
3ad8a208
SV
4580 /* XXX: pch pll's can be enabled any time before we enable the PCH
4581 * transcoder, and we actually should do this to not upset any PCH
4582 * transcoder that already use the clock when we share it.
4583 *
4584 * Note that enable_shared_dpll tries to do the right thing, but
4585 * get_shared_dpll unconditionally resets the pll - we need that to have
4586 * the right LVDS enable sequence. */
85b3894f 4587 intel_enable_shared_dpll(intel_crtc);
3ad8a208 4588
d9b6cb56
JB
4589 /* set transcoder timing, panel must allow it */
4590 assert_panel_unlocked(dev_priv, pipe);
275f01b2 4591 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
8db9d77b 4592
303b81e0 4593 intel_fdi_normal_train(crtc);
5e84e1a4 4594
c98e9dcf 4595 /* For PCH DP, enable TRANS_DP_CTL */
6e266956
TU
4596 if (HAS_PCH_CPT(dev_priv) &&
4597 intel_crtc_has_dp_encoder(intel_crtc->config)) {
9c4edaee
VS
4598 const struct drm_display_mode *adjusted_mode =
4599 &intel_crtc->config->base.adjusted_mode;
dfd07d72 4600 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
f0f59a00 4601 i915_reg_t reg = TRANS_DP_CTL(pipe);
5eddb70b
CW
4602 temp = I915_READ(reg);
4603 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
4604 TRANS_DP_SYNC_MASK |
4605 TRANS_DP_BPC_MASK);
e3ef4479 4606 temp |= TRANS_DP_OUTPUT_ENABLE;
9325c9f0 4607 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf 4608
9c4edaee 4609 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 4610 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
9c4edaee 4611 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 4612 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf
JB
4613
4614 switch (intel_trans_dp_port_sel(crtc)) {
c48b5305 4615 case PORT_B:
5eddb70b 4616 temp |= TRANS_DP_PORT_SEL_B;
c98e9dcf 4617 break;
c48b5305 4618 case PORT_C:
5eddb70b 4619 temp |= TRANS_DP_PORT_SEL_C;
c98e9dcf 4620 break;
c48b5305 4621 case PORT_D:
5eddb70b 4622 temp |= TRANS_DP_PORT_SEL_D;
c98e9dcf
JB
4623 break;
4624 default:
e95d41e1 4625 BUG();
32f9d658 4626 }
2c07245f 4627
5eddb70b 4628 I915_WRITE(reg, temp);
6be4a607 4629 }
b52eb4dc 4630
b8a4f404 4631 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
4632}
4633
1507e5bd
PZ
4634static void lpt_pch_enable(struct drm_crtc *crtc)
4635{
4636 struct drm_device *dev = crtc->dev;
fac5e23e 4637 struct drm_i915_private *dev_priv = to_i915(dev);
1507e5bd 4638 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 4639 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
1507e5bd 4640
ab9412ba 4641 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
1507e5bd 4642
8c52b5e8 4643 lpt_program_iclkip(crtc);
1507e5bd 4644
0540e488 4645 /* Set transcoder timing. */
275f01b2 4646 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
1507e5bd 4647
937bb610 4648 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
4649}
4650
a1520318 4651static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57 4652{
fac5e23e 4653 struct drm_i915_private *dev_priv = to_i915(dev);
f0f59a00 4654 i915_reg_t dslreg = PIPEDSL(pipe);
d4270e57
JB
4655 u32 temp;
4656
4657 temp = I915_READ(dslreg);
4658 udelay(500);
4659 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 4660 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 4661 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
4662 }
4663}
4664
86adf9d7
ML
4665static int
4666skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4667 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4668 int src_w, int src_h, int dst_w, int dst_h)
a1b2278e 4669{
86adf9d7
ML
4670 struct intel_crtc_scaler_state *scaler_state =
4671 &crtc_state->scaler_state;
4672 struct intel_crtc *intel_crtc =
4673 to_intel_crtc(crtc_state->base.crtc);
a1b2278e 4674 int need_scaling;
6156a456
CK
4675
4676 need_scaling = intel_rotation_90_or_270(rotation) ?
4677 (src_h != dst_w || src_w != dst_h):
4678 (src_w != dst_w || src_h != dst_h);
a1b2278e
CK
4679
4680 /*
4681 * if plane is being disabled or scaler is no more required or force detach
4682 * - free scaler binded to this plane/crtc
4683 * - in order to do this, update crtc->scaler_usage
4684 *
4685 * Here scaler state in crtc_state is set free so that
4686 * scaler can be assigned to other user. Actual register
4687 * update to free the scaler is done in plane/panel-fit programming.
4688 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4689 */
86adf9d7 4690 if (force_detach || !need_scaling) {
a1b2278e 4691 if (*scaler_id >= 0) {
86adf9d7 4692 scaler_state->scaler_users &= ~(1 << scaler_user);
a1b2278e
CK
4693 scaler_state->scalers[*scaler_id].in_use = 0;
4694
86adf9d7
ML
4695 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4696 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4697 intel_crtc->pipe, scaler_user, *scaler_id,
a1b2278e
CK
4698 scaler_state->scaler_users);
4699 *scaler_id = -1;
4700 }
4701 return 0;
4702 }
4703
4704 /* range checks */
4705 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4706 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4707
4708 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4709 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
86adf9d7 4710 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
a1b2278e 4711 "size is out of scaler range\n",
86adf9d7 4712 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
a1b2278e
CK
4713 return -EINVAL;
4714 }
4715
86adf9d7
ML
4716 /* mark this plane as a scaler user in crtc_state */
4717 scaler_state->scaler_users |= (1 << scaler_user);
4718 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4719 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4720 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4721 scaler_state->scaler_users);
4722
4723 return 0;
4724}
4725
4726/**
4727 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4728 *
4729 * @state: crtc's scaler state
86adf9d7
ML
4730 *
4731 * Return
4732 * 0 - scaler_usage updated successfully
4733 * error - requested scaling cannot be supported or other error condition
4734 */
e435d6e5 4735int skl_update_scaler_crtc(struct intel_crtc_state *state)
86adf9d7
ML
4736{
4737 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
7c5f93b0 4738 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
86adf9d7 4739
78108b7c
VS
4740 DRM_DEBUG_KMS("Updating scaler for [CRTC:%d:%s] scaler_user index %u.%u\n",
4741 intel_crtc->base.base.id, intel_crtc->base.name,
4742 intel_crtc->pipe, SKL_CRTC_INDEX);
86adf9d7 4743
e435d6e5 4744 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
31ad61e4 4745 &state->scaler_state.scaler_id, DRM_ROTATE_0,
86adf9d7 4746 state->pipe_src_w, state->pipe_src_h,
aad941d5 4747 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
86adf9d7
ML
4748}
4749
4750/**
4751 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4752 *
4753 * @state: crtc's scaler state
86adf9d7
ML
4754 * @plane_state: atomic plane state to update
4755 *
4756 * Return
4757 * 0 - scaler_usage updated successfully
4758 * error - requested scaling cannot be supported or other error condition
4759 */
da20eabd
ML
4760static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4761 struct intel_plane_state *plane_state)
86adf9d7
ML
4762{
4763
4764 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
da20eabd
ML
4765 struct intel_plane *intel_plane =
4766 to_intel_plane(plane_state->base.plane);
86adf9d7
ML
4767 struct drm_framebuffer *fb = plane_state->base.fb;
4768 int ret;
4769
936e71e3 4770 bool force_detach = !fb || !plane_state->base.visible;
86adf9d7 4771
72660ce0
VS
4772 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d:%s] scaler_user index %u.%u\n",
4773 intel_plane->base.base.id, intel_plane->base.name,
4774 intel_crtc->pipe, drm_plane_index(&intel_plane->base));
86adf9d7
ML
4775
4776 ret = skl_update_scaler(crtc_state, force_detach,
4777 drm_plane_index(&intel_plane->base),
4778 &plane_state->scaler_id,
4779 plane_state->base.rotation,
936e71e3
VS
4780 drm_rect_width(&plane_state->base.src) >> 16,
4781 drm_rect_height(&plane_state->base.src) >> 16,
4782 drm_rect_width(&plane_state->base.dst),
4783 drm_rect_height(&plane_state->base.dst));
86adf9d7
ML
4784
4785 if (ret || plane_state->scaler_id < 0)
4786 return ret;
4787
a1b2278e 4788 /* check colorkey */
818ed961 4789 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
72660ce0
VS
4790 DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
4791 intel_plane->base.base.id,
4792 intel_plane->base.name);
a1b2278e
CK
4793 return -EINVAL;
4794 }
4795
4796 /* Check src format */
86adf9d7
ML
4797 switch (fb->pixel_format) {
4798 case DRM_FORMAT_RGB565:
4799 case DRM_FORMAT_XBGR8888:
4800 case DRM_FORMAT_XRGB8888:
4801 case DRM_FORMAT_ABGR8888:
4802 case DRM_FORMAT_ARGB8888:
4803 case DRM_FORMAT_XRGB2101010:
4804 case DRM_FORMAT_XBGR2101010:
4805 case DRM_FORMAT_YUYV:
4806 case DRM_FORMAT_YVYU:
4807 case DRM_FORMAT_UYVY:
4808 case DRM_FORMAT_VYUY:
4809 break;
4810 default:
72660ce0
VS
4811 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
4812 intel_plane->base.base.id, intel_plane->base.name,
4813 fb->base.id, fb->pixel_format);
86adf9d7 4814 return -EINVAL;
a1b2278e
CK
4815 }
4816
a1b2278e
CK
4817 return 0;
4818}
4819
e435d6e5
ML
4820static void skylake_scaler_disable(struct intel_crtc *crtc)
4821{
4822 int i;
4823
4824 for (i = 0; i < crtc->num_scalers; i++)
4825 skl_detach_scaler(crtc, i);
4826}
4827
4828static void skylake_pfit_enable(struct intel_crtc *crtc)
bd2e244f
JB
4829{
4830 struct drm_device *dev = crtc->base.dev;
fac5e23e 4831 struct drm_i915_private *dev_priv = to_i915(dev);
bd2e244f 4832 int pipe = crtc->pipe;
a1b2278e
CK
4833 struct intel_crtc_scaler_state *scaler_state =
4834 &crtc->config->scaler_state;
4835
4836 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4837
6e3c9717 4838 if (crtc->config->pch_pfit.enabled) {
a1b2278e
CK
4839 int id;
4840
4841 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4842 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4843 return;
4844 }
4845
4846 id = scaler_state->scaler_id;
4847 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4848 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4849 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4850 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4851
4852 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
bd2e244f
JB
4853 }
4854}
4855
b074cec8
JB
4856static void ironlake_pfit_enable(struct intel_crtc *crtc)
4857{
4858 struct drm_device *dev = crtc->base.dev;
fac5e23e 4859 struct drm_i915_private *dev_priv = to_i915(dev);
b074cec8
JB
4860 int pipe = crtc->pipe;
4861
6e3c9717 4862 if (crtc->config->pch_pfit.enabled) {
b074cec8
JB
4863 /* Force use of hard-coded filter coefficients
4864 * as some pre-programmed values are broken,
4865 * e.g. x201.
4866 */
fd6b8f43 4867 if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
b074cec8
JB
4868 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4869 PF_PIPE_SEL_IVB(pipe));
4870 else
4871 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
6e3c9717
ACO
4872 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4873 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
d4270e57
JB
4874 }
4875}
4876
20bc8673 4877void hsw_enable_ips(struct intel_crtc *crtc)
d77e4531 4878{
cea165c3 4879 struct drm_device *dev = crtc->base.dev;
fac5e23e 4880 struct drm_i915_private *dev_priv = to_i915(dev);
d77e4531 4881
6e3c9717 4882 if (!crtc->config->ips_enabled)
d77e4531
PZ
4883 return;
4884
307e4498
ML
4885 /*
4886 * We can only enable IPS after we enable a plane and wait for a vblank
4887 * This function is called from post_plane_update, which is run after
4888 * a vblank wait.
4889 */
cea165c3 4890
d77e4531 4891 assert_plane_enabled(dev_priv, crtc->plane);
8652744b 4892 if (IS_BROADWELL(dev_priv)) {
2a114cc1
BW
4893 mutex_lock(&dev_priv->rps.hw_lock);
4894 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4895 mutex_unlock(&dev_priv->rps.hw_lock);
4896 /* Quoting Art Runyan: "its not safe to expect any particular
4897 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
4898 * mailbox." Moreover, the mailbox may return a bogus state,
4899 * so we need to just enable it and continue on.
2a114cc1
BW
4900 */
4901 } else {
4902 I915_WRITE(IPS_CTL, IPS_ENABLE);
4903 /* The bit only becomes 1 in the next vblank, so this wait here
4904 * is essentially intel_wait_for_vblank. If we don't have this
4905 * and don't wait for vblanks until the end of crtc_enable, then
4906 * the HW state readout code will complain that the expected
4907 * IPS_CTL value is not the one we read. */
2ec9ba3c
CW
4908 if (intel_wait_for_register(dev_priv,
4909 IPS_CTL, IPS_ENABLE, IPS_ENABLE,
4910 50))
2a114cc1
BW
4911 DRM_ERROR("Timed out waiting for IPS enable\n");
4912 }
d77e4531
PZ
4913}
4914
20bc8673 4915void hsw_disable_ips(struct intel_crtc *crtc)
d77e4531
PZ
4916{
4917 struct drm_device *dev = crtc->base.dev;
fac5e23e 4918 struct drm_i915_private *dev_priv = to_i915(dev);
d77e4531 4919
6e3c9717 4920 if (!crtc->config->ips_enabled)
d77e4531
PZ
4921 return;
4922
4923 assert_plane_enabled(dev_priv, crtc->plane);
8652744b 4924 if (IS_BROADWELL(dev_priv)) {
2a114cc1
BW
4925 mutex_lock(&dev_priv->rps.hw_lock);
4926 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4927 mutex_unlock(&dev_priv->rps.hw_lock);
23d0b130 4928 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
b85c1ecf
CW
4929 if (intel_wait_for_register(dev_priv,
4930 IPS_CTL, IPS_ENABLE, 0,
4931 42))
23d0b130 4932 DRM_ERROR("Timed out waiting for IPS disable\n");
e59150dc 4933 } else {
2a114cc1 4934 I915_WRITE(IPS_CTL, 0);
e59150dc
JB
4935 POSTING_READ(IPS_CTL);
4936 }
d77e4531
PZ
4937
4938 /* We need to wait for a vblank before we can disable the plane. */
4939 intel_wait_for_vblank(dev, crtc->pipe);
4940}
4941
7cac945f 4942static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
d3eedb1a 4943{
7cac945f 4944 if (intel_crtc->overlay) {
d3eedb1a 4945 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 4946 struct drm_i915_private *dev_priv = to_i915(dev);
d3eedb1a
VS
4947
4948 mutex_lock(&dev->struct_mutex);
4949 dev_priv->mm.interruptible = false;
4950 (void) intel_overlay_switch_off(intel_crtc->overlay);
4951 dev_priv->mm.interruptible = true;
4952 mutex_unlock(&dev->struct_mutex);
4953 }
4954
4955 /* Let userspace switch the overlay on again. In most cases userspace
4956 * has to recompute where to put it anyway.
4957 */
4958}
4959
87d4300a
ML
4960/**
4961 * intel_post_enable_primary - Perform operations after enabling primary plane
4962 * @crtc: the CRTC whose primary plane was just enabled
4963 *
4964 * Performs potentially sleeping operations that must be done after the primary
4965 * plane is enabled, such as updating FBC and IPS. Note that this may be
4966 * called due to an explicit primary plane update, or due to an implicit
4967 * re-enable that is caused when a sprite plane is updated to no longer
4968 * completely hide the primary plane.
4969 */
4970static void
4971intel_post_enable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4972{
4973 struct drm_device *dev = crtc->dev;
fac5e23e 4974 struct drm_i915_private *dev_priv = to_i915(dev);
a5c4d7bc
VS
4975 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4976 int pipe = intel_crtc->pipe;
a5c4d7bc 4977
87d4300a
ML
4978 /*
4979 * FIXME IPS should be fine as long as one plane is
4980 * enabled, but in practice it seems to have problems
4981 * when going from primary only to sprite only and vice
4982 * versa.
4983 */
a5c4d7bc
VS
4984 hsw_enable_ips(intel_crtc);
4985
f99d7069 4986 /*
87d4300a
ML
4987 * Gen2 reports pipe underruns whenever all planes are disabled.
4988 * So don't enable underrun reporting before at least some planes
4989 * are enabled.
4990 * FIXME: Need to fix the logic to work when we turn off all planes
4991 * but leave the pipe running.
f99d7069 4992 */
5db94019 4993 if (IS_GEN2(dev_priv))
87d4300a
ML
4994 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4995
aca7b684
VS
4996 /* Underruns don't always raise interrupts, so check manually. */
4997 intel_check_cpu_fifo_underruns(dev_priv);
4998 intel_check_pch_fifo_underruns(dev_priv);
a5c4d7bc
VS
4999}
5000
2622a081 5001/* FIXME move all this to pre_plane_update() with proper state tracking */
87d4300a
ML
5002static void
5003intel_pre_disable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
5004{
5005 struct drm_device *dev = crtc->dev;
fac5e23e 5006 struct drm_i915_private *dev_priv = to_i915(dev);
a5c4d7bc
VS
5007 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5008 int pipe = intel_crtc->pipe;
a5c4d7bc 5009
87d4300a
ML
5010 /*
5011 * Gen2 reports pipe underruns whenever all planes are disabled.
5012 * So diasble underrun reporting before all the planes get disabled.
5013 * FIXME: Need to fix the logic to work when we turn off all planes
5014 * but leave the pipe running.
5015 */
5db94019 5016 if (IS_GEN2(dev_priv))
87d4300a 5017 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
a5c4d7bc 5018
2622a081
VS
5019 /*
5020 * FIXME IPS should be fine as long as one plane is
5021 * enabled, but in practice it seems to have problems
5022 * when going from primary only to sprite only and vice
5023 * versa.
5024 */
5025 hsw_disable_ips(intel_crtc);
5026}
5027
5028/* FIXME get rid of this and use pre_plane_update */
5029static void
5030intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
5031{
5032 struct drm_device *dev = crtc->dev;
fac5e23e 5033 struct drm_i915_private *dev_priv = to_i915(dev);
2622a081
VS
5034 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5035 int pipe = intel_crtc->pipe;
5036
5037 intel_pre_disable_primary(crtc);
5038
87d4300a
ML
5039 /*
5040 * Vblank time updates from the shadow to live plane control register
5041 * are blocked if the memory self-refresh mode is active at that
5042 * moment. So to make sure the plane gets truly disabled, disable
5043 * first the self-refresh mode. The self-refresh enable bit in turn
5044 * will be checked/applied by the HW only at the next frame start
5045 * event which is after the vblank start event, so we need to have a
5046 * wait-for-vblank between disabling the plane and the pipe.
5047 */
49cff963 5048 if (HAS_GMCH_DISPLAY(dev_priv)) {
87d4300a 5049 intel_set_memory_cxsr(dev_priv, false);
262cd2e1
VS
5050 dev_priv->wm.vlv.cxsr = false;
5051 intel_wait_for_vblank(dev, pipe);
5052 }
87d4300a
ML
5053}
5054
5a21b665
SV
5055static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
5056{
5057 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
5058 struct drm_atomic_state *old_state = old_crtc_state->base.state;
5059 struct intel_crtc_state *pipe_config =
5060 to_intel_crtc_state(crtc->base.state);
5a21b665
SV
5061 struct drm_plane *primary = crtc->base.primary;
5062 struct drm_plane_state *old_pri_state =
5063 drm_atomic_get_existing_plane_state(old_state, primary);
5064
5748b6a1 5065 intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
5a21b665
SV
5066
5067 crtc->wm.cxsr_allowed = true;
5068
5069 if (pipe_config->update_wm_post && pipe_config->base.active)
5070 intel_update_watermarks(&crtc->base);
5071
5072 if (old_pri_state) {
5073 struct intel_plane_state *primary_state =
5074 to_intel_plane_state(primary->state);
5075 struct intel_plane_state *old_primary_state =
5076 to_intel_plane_state(old_pri_state);
5077
5078 intel_fbc_post_update(crtc);
5079
936e71e3 5080 if (primary_state->base.visible &&
5a21b665 5081 (needs_modeset(&pipe_config->base) ||
936e71e3 5082 !old_primary_state->base.visible))
5a21b665
SV
5083 intel_post_enable_primary(&crtc->base);
5084 }
5085}
5086
5c74cd73 5087static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
ac21b225 5088{
5c74cd73 5089 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
ac21b225 5090 struct drm_device *dev = crtc->base.dev;
fac5e23e 5091 struct drm_i915_private *dev_priv = to_i915(dev);
ab1d3a0e
ML
5092 struct intel_crtc_state *pipe_config =
5093 to_intel_crtc_state(crtc->base.state);
5c74cd73
ML
5094 struct drm_atomic_state *old_state = old_crtc_state->base.state;
5095 struct drm_plane *primary = crtc->base.primary;
5096 struct drm_plane_state *old_pri_state =
5097 drm_atomic_get_existing_plane_state(old_state, primary);
5098 bool modeset = needs_modeset(&pipe_config->base);
ac21b225 5099
5c74cd73
ML
5100 if (old_pri_state) {
5101 struct intel_plane_state *primary_state =
5102 to_intel_plane_state(primary->state);
5103 struct intel_plane_state *old_primary_state =
5104 to_intel_plane_state(old_pri_state);
5105
faf68d92 5106 intel_fbc_pre_update(crtc, pipe_config, primary_state);
31ae71fc 5107
936e71e3
VS
5108 if (old_primary_state->base.visible &&
5109 (modeset || !primary_state->base.visible))
5c74cd73
ML
5110 intel_pre_disable_primary(&crtc->base);
5111 }
852eb00d 5112
49cff963 5113 if (pipe_config->disable_cxsr && HAS_GMCH_DISPLAY(dev_priv)) {
852eb00d 5114 crtc->wm.cxsr_allowed = false;
2dfd178d 5115
2622a081
VS
5116 /*
5117 * Vblank time updates from the shadow to live plane control register
5118 * are blocked if the memory self-refresh mode is active at that
5119 * moment. So to make sure the plane gets truly disabled, disable
5120 * first the self-refresh mode. The self-refresh enable bit in turn
5121 * will be checked/applied by the HW only at the next frame start
5122 * event which is after the vblank start event, so we need to have a
5123 * wait-for-vblank between disabling the plane and the pipe.
5124 */
5125 if (old_crtc_state->base.active) {
2dfd178d 5126 intel_set_memory_cxsr(dev_priv, false);
2622a081
VS
5127 dev_priv->wm.vlv.cxsr = false;
5128 intel_wait_for_vblank(dev, crtc->pipe);
5129 }
852eb00d 5130 }
92826fcd 5131
ed4a6a7c
MR
5132 /*
5133 * IVB workaround: must disable low power watermarks for at least
5134 * one frame before enabling scaling. LP watermarks can be re-enabled
5135 * when scaling is disabled.
5136 *
5137 * WaCxSRDisabledForSpriteScaling:ivb
5138 */
5139 if (pipe_config->disable_lp_wm) {
5140 ilk_disable_lp_wm(dev);
5141 intel_wait_for_vblank(dev, crtc->pipe);
5142 }
5143
5144 /*
5145 * If we're doing a modeset, we're done. No need to do any pre-vblank
5146 * watermark programming here.
5147 */
5148 if (needs_modeset(&pipe_config->base))
5149 return;
5150
5151 /*
5152 * For platforms that support atomic watermarks, program the
5153 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
5154 * will be the intermediate values that are safe for both pre- and
5155 * post- vblank; when vblank happens, the 'active' values will be set
5156 * to the final 'target' values and we'll do this again to get the
5157 * optimal watermarks. For gen9+ platforms, the values we program here
5158 * will be the final target values which will get automatically latched
5159 * at vblank time; no further programming will be necessary.
5160 *
5161 * If a platform hasn't been transitioned to atomic watermarks yet,
5162 * we'll continue to update watermarks the old way, if flags tell
5163 * us to.
5164 */
5165 if (dev_priv->display.initial_watermarks != NULL)
5166 dev_priv->display.initial_watermarks(pipe_config);
caed361d 5167 else if (pipe_config->update_wm_pre)
92826fcd 5168 intel_update_watermarks(&crtc->base);
ac21b225
ML
5169}
5170
d032ffa0 5171static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
87d4300a
ML
5172{
5173 struct drm_device *dev = crtc->dev;
5174 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
d032ffa0 5175 struct drm_plane *p;
87d4300a
ML
5176 int pipe = intel_crtc->pipe;
5177
7cac945f 5178 intel_crtc_dpms_overlay_disable(intel_crtc);
27321ae8 5179
d032ffa0
ML
5180 drm_for_each_plane_mask(p, dev, plane_mask)
5181 to_intel_plane(p)->disable_plane(p, crtc);
f98551ae 5182
f99d7069
SV
5183 /*
5184 * FIXME: Once we grow proper nuclear flip support out of this we need
5185 * to compute the mask of flip planes precisely. For the time being
5186 * consider this a flip to a NULL plane.
5187 */
5748b6a1 5188 intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
5189}
5190
fb1c98b1 5191static void intel_encoders_pre_pll_enable(struct drm_crtc *crtc,
fd6bbda9 5192 struct intel_crtc_state *crtc_state,
fb1c98b1
ML
5193 struct drm_atomic_state *old_state)
5194{
5195 struct drm_connector_state *old_conn_state;
5196 struct drm_connector *conn;
5197 int i;
5198
5199 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5200 struct drm_connector_state *conn_state = conn->state;
5201 struct intel_encoder *encoder =
5202 to_intel_encoder(conn_state->best_encoder);
5203
5204 if (conn_state->crtc != crtc)
5205 continue;
5206
5207 if (encoder->pre_pll_enable)
fd6bbda9 5208 encoder->pre_pll_enable(encoder, crtc_state, conn_state);
fb1c98b1
ML
5209 }
5210}
5211
5212static void intel_encoders_pre_enable(struct drm_crtc *crtc,
fd6bbda9 5213 struct intel_crtc_state *crtc_state,
fb1c98b1
ML
5214 struct drm_atomic_state *old_state)
5215{
5216 struct drm_connector_state *old_conn_state;
5217 struct drm_connector *conn;
5218 int i;
5219
5220 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5221 struct drm_connector_state *conn_state = conn->state;
5222 struct intel_encoder *encoder =
5223 to_intel_encoder(conn_state->best_encoder);
5224
5225 if (conn_state->crtc != crtc)
5226 continue;
5227
5228 if (encoder->pre_enable)
fd6bbda9 5229 encoder->pre_enable(encoder, crtc_state, conn_state);
fb1c98b1
ML
5230 }
5231}
5232
5233static void intel_encoders_enable(struct drm_crtc *crtc,
fd6bbda9 5234 struct intel_crtc_state *crtc_state,
fb1c98b1
ML
5235 struct drm_atomic_state *old_state)
5236{
5237 struct drm_connector_state *old_conn_state;
5238 struct drm_connector *conn;
5239 int i;
5240
5241 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5242 struct drm_connector_state *conn_state = conn->state;
5243 struct intel_encoder *encoder =
5244 to_intel_encoder(conn_state->best_encoder);
5245
5246 if (conn_state->crtc != crtc)
5247 continue;
5248
fd6bbda9 5249 encoder->enable(encoder, crtc_state, conn_state);
fb1c98b1
ML
5250 intel_opregion_notify_encoder(encoder, true);
5251 }
5252}
5253
5254static void intel_encoders_disable(struct drm_crtc *crtc,
fd6bbda9 5255 struct intel_crtc_state *old_crtc_state,
fb1c98b1
ML
5256 struct drm_atomic_state *old_state)
5257{
5258 struct drm_connector_state *old_conn_state;
5259 struct drm_connector *conn;
5260 int i;
5261
5262 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5263 struct intel_encoder *encoder =
5264 to_intel_encoder(old_conn_state->best_encoder);
5265
5266 if (old_conn_state->crtc != crtc)
5267 continue;
5268
5269 intel_opregion_notify_encoder(encoder, false);
fd6bbda9 5270 encoder->disable(encoder, old_crtc_state, old_conn_state);
fb1c98b1
ML
5271 }
5272}
5273
5274static void intel_encoders_post_disable(struct drm_crtc *crtc,
fd6bbda9 5275 struct intel_crtc_state *old_crtc_state,
fb1c98b1
ML
5276 struct drm_atomic_state *old_state)
5277{
5278 struct drm_connector_state *old_conn_state;
5279 struct drm_connector *conn;
5280 int i;
5281
5282 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5283 struct intel_encoder *encoder =
5284 to_intel_encoder(old_conn_state->best_encoder);
5285
5286 if (old_conn_state->crtc != crtc)
5287 continue;
5288
5289 if (encoder->post_disable)
fd6bbda9 5290 encoder->post_disable(encoder, old_crtc_state, old_conn_state);
fb1c98b1
ML
5291 }
5292}
5293
5294static void intel_encoders_post_pll_disable(struct drm_crtc *crtc,
fd6bbda9 5295 struct intel_crtc_state *old_crtc_state,
fb1c98b1
ML
5296 struct drm_atomic_state *old_state)
5297{
5298 struct drm_connector_state *old_conn_state;
5299 struct drm_connector *conn;
5300 int i;
5301
5302 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5303 struct intel_encoder *encoder =
5304 to_intel_encoder(old_conn_state->best_encoder);
5305
5306 if (old_conn_state->crtc != crtc)
5307 continue;
5308
5309 if (encoder->post_pll_disable)
fd6bbda9 5310 encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
fb1c98b1
ML
5311 }
5312}
5313
4a806558
ML
5314static void ironlake_crtc_enable(struct intel_crtc_state *pipe_config,
5315 struct drm_atomic_state *old_state)
f67a559d 5316{
4a806558 5317 struct drm_crtc *crtc = pipe_config->base.crtc;
f67a559d 5318 struct drm_device *dev = crtc->dev;
fac5e23e 5319 struct drm_i915_private *dev_priv = to_i915(dev);
f67a559d
JB
5320 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5321 int pipe = intel_crtc->pipe;
f67a559d 5322
53d9f4e9 5323 if (WARN_ON(intel_crtc->active))
f67a559d
JB
5324 return;
5325
b2c0593a
VS
5326 /*
5327 * Sometimes spurious CPU pipe underruns happen during FDI
5328 * training, at least with VGA+HDMI cloning. Suppress them.
5329 *
5330 * On ILK we get an occasional spurious CPU pipe underruns
5331 * between eDP port A enable and vdd enable. Also PCH port
5332 * enable seems to result in the occasional CPU pipe underrun.
5333 *
5334 * Spurious PCH underruns also occur during PCH enabling.
5335 */
5336 if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
5337 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
81b088ca
VS
5338 if (intel_crtc->config->has_pch_encoder)
5339 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
5340
6e3c9717 5341 if (intel_crtc->config->has_pch_encoder)
b14b1055
SV
5342 intel_prepare_shared_dpll(intel_crtc);
5343
37a5650b 5344 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 5345 intel_dp_set_m_n(intel_crtc, M1_N1);
29407aab
SV
5346
5347 intel_set_pipe_timings(intel_crtc);
bc58be60 5348 intel_set_pipe_src_size(intel_crtc);
29407aab 5349
6e3c9717 5350 if (intel_crtc->config->has_pch_encoder) {
29407aab 5351 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 5352 &intel_crtc->config->fdi_m_n, NULL);
29407aab
SV
5353 }
5354
5355 ironlake_set_pipeconf(crtc);
5356
f67a559d 5357 intel_crtc->active = true;
8664281b 5358
fd6bbda9 5359 intel_encoders_pre_enable(crtc, pipe_config, old_state);
f67a559d 5360
6e3c9717 5361 if (intel_crtc->config->has_pch_encoder) {
fff367c7
SV
5362 /* Note: FDI PLL enabling _must_ be done before we enable the
5363 * cpu pipes, hence this is separate from all the other fdi/pch
5364 * enabling. */
88cefb6c 5365 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
SV
5366 } else {
5367 assert_fdi_tx_disabled(dev_priv, pipe);
5368 assert_fdi_rx_disabled(dev_priv, pipe);
5369 }
f67a559d 5370
b074cec8 5371 ironlake_pfit_enable(intel_crtc);
f67a559d 5372
9c54c0dd
JB
5373 /*
5374 * On ILK+ LUT must be loaded before the pipe is running but with
5375 * clocks enabled
5376 */
b95c5321 5377 intel_color_load_luts(&pipe_config->base);
9c54c0dd 5378
1d5bf5d9
ID
5379 if (dev_priv->display.initial_watermarks != NULL)
5380 dev_priv->display.initial_watermarks(intel_crtc->config);
e1fdc473 5381 intel_enable_pipe(intel_crtc);
f67a559d 5382
6e3c9717 5383 if (intel_crtc->config->has_pch_encoder)
f67a559d 5384 ironlake_pch_enable(crtc);
c98e9dcf 5385
f9b61ff6
SV
5386 assert_vblank_disabled(crtc);
5387 drm_crtc_vblank_on(crtc);
5388
fd6bbda9 5389 intel_encoders_enable(crtc, pipe_config, old_state);
61b77ddd 5390
6e266956 5391 if (HAS_PCH_CPT(dev_priv))
a1520318 5392 cpt_verify_modeset(dev, intel_crtc->pipe);
37ca8d4c
VS
5393
5394 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
5395 if (intel_crtc->config->has_pch_encoder)
5396 intel_wait_for_vblank(dev, pipe);
b2c0593a 5397 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
37ca8d4c 5398 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607
JB
5399}
5400
42db64ef
PZ
5401/* IPS only exists on ULT machines and is tied to pipe A. */
5402static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
5403{
50a0bc90 5404 return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
42db64ef
PZ
5405}
5406
4a806558
ML
5407static void haswell_crtc_enable(struct intel_crtc_state *pipe_config,
5408 struct drm_atomic_state *old_state)
4f771f10 5409{
4a806558 5410 struct drm_crtc *crtc = pipe_config->base.crtc;
4f771f10 5411 struct drm_device *dev = crtc->dev;
fac5e23e 5412 struct drm_i915_private *dev_priv = to_i915(dev);
4f771f10 5413 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
99d736a2 5414 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
4d1de975 5415 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
4f771f10 5416
53d9f4e9 5417 if (WARN_ON(intel_crtc->active))
4f771f10
PZ
5418 return;
5419
81b088ca
VS
5420 if (intel_crtc->config->has_pch_encoder)
5421 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5422 false);
5423
fd6bbda9 5424 intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
95a7a2ae 5425
8106ddbd 5426 if (intel_crtc->config->shared_dpll)
df8ad70c
SV
5427 intel_enable_shared_dpll(intel_crtc);
5428
37a5650b 5429 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 5430 intel_dp_set_m_n(intel_crtc, M1_N1);
229fca97 5431
d7edc4e5 5432 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975
JN
5433 intel_set_pipe_timings(intel_crtc);
5434
bc58be60 5435 intel_set_pipe_src_size(intel_crtc);
229fca97 5436
4d1de975
JN
5437 if (cpu_transcoder != TRANSCODER_EDP &&
5438 !transcoder_is_dsi(cpu_transcoder)) {
5439 I915_WRITE(PIPE_MULT(cpu_transcoder),
6e3c9717 5440 intel_crtc->config->pixel_multiplier - 1);
ebb69c95
CT
5441 }
5442
6e3c9717 5443 if (intel_crtc->config->has_pch_encoder) {
229fca97 5444 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 5445 &intel_crtc->config->fdi_m_n, NULL);
229fca97
SV
5446 }
5447
d7edc4e5 5448 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975
JN
5449 haswell_set_pipeconf(crtc);
5450
391bf048 5451 haswell_set_pipemisc(crtc);
229fca97 5452
b95c5321 5453 intel_color_set_csc(&pipe_config->base);
229fca97 5454
4f771f10 5455 intel_crtc->active = true;
8664281b 5456
6b698516
SV
5457 if (intel_crtc->config->has_pch_encoder)
5458 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
5459 else
5460 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5461
fd6bbda9 5462 intel_encoders_pre_enable(crtc, pipe_config, old_state);
4f771f10 5463
d2d65408 5464 if (intel_crtc->config->has_pch_encoder)
4fe9467d 5465 dev_priv->display.fdi_link_train(crtc);
4fe9467d 5466
d7edc4e5 5467 if (!transcoder_is_dsi(cpu_transcoder))
7d4aefd0 5468 intel_ddi_enable_pipe_clock(intel_crtc);
4f771f10 5469
1c132b44 5470 if (INTEL_INFO(dev)->gen >= 9)
e435d6e5 5471 skylake_pfit_enable(intel_crtc);
ff6d9f55 5472 else
1c132b44 5473 ironlake_pfit_enable(intel_crtc);
4f771f10
PZ
5474
5475 /*
5476 * On ILK+ LUT must be loaded before the pipe is running but with
5477 * clocks enabled
5478 */
b95c5321 5479 intel_color_load_luts(&pipe_config->base);
4f771f10 5480
1f544388 5481 intel_ddi_set_pipe_settings(crtc);
d7edc4e5 5482 if (!transcoder_is_dsi(cpu_transcoder))
7d4aefd0 5483 intel_ddi_enable_transcoder_func(crtc);
4f771f10 5484
1d5bf5d9
ID
5485 if (dev_priv->display.initial_watermarks != NULL)
5486 dev_priv->display.initial_watermarks(pipe_config);
5487 else
5488 intel_update_watermarks(crtc);
4d1de975
JN
5489
5490 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
d7edc4e5 5491 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975 5492 intel_enable_pipe(intel_crtc);
42db64ef 5493
6e3c9717 5494 if (intel_crtc->config->has_pch_encoder)
1507e5bd 5495 lpt_pch_enable(crtc);
4f771f10 5496
a65347ba 5497 if (intel_crtc->config->dp_encoder_is_mst)
0e32b39c
DA
5498 intel_ddi_set_vc_payload_alloc(crtc, true);
5499
f9b61ff6
SV
5500 assert_vblank_disabled(crtc);
5501 drm_crtc_vblank_on(crtc);
5502
fd6bbda9 5503 intel_encoders_enable(crtc, pipe_config, old_state);
4f771f10 5504
6b698516
SV
5505 if (intel_crtc->config->has_pch_encoder) {
5506 intel_wait_for_vblank(dev, pipe);
5507 intel_wait_for_vblank(dev, pipe);
5508 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
d2d65408
VS
5509 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5510 true);
6b698516 5511 }
d2d65408 5512
e4916946
PZ
5513 /* If we change the relative order between pipe/planes enabling, we need
5514 * to change the workaround. */
99d736a2 5515 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
772c2a51 5516 if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
99d736a2
ML
5517 intel_wait_for_vblank(dev, hsw_workaround_pipe);
5518 intel_wait_for_vblank(dev, hsw_workaround_pipe);
5519 }
4f771f10
PZ
5520}
5521
bfd16b2a 5522static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
3f8dce3a
SV
5523{
5524 struct drm_device *dev = crtc->base.dev;
fac5e23e 5525 struct drm_i915_private *dev_priv = to_i915(dev);
3f8dce3a
SV
5526 int pipe = crtc->pipe;
5527
5528 /* To avoid upsetting the power well on haswell only disable the pfit if
5529 * it's in use. The hw state code will make sure we get this right. */
bfd16b2a 5530 if (force || crtc->config->pch_pfit.enabled) {
3f8dce3a
SV
5531 I915_WRITE(PF_CTL(pipe), 0);
5532 I915_WRITE(PF_WIN_POS(pipe), 0);
5533 I915_WRITE(PF_WIN_SZ(pipe), 0);
5534 }
5535}
5536
4a806558
ML
5537static void ironlake_crtc_disable(struct intel_crtc_state *old_crtc_state,
5538 struct drm_atomic_state *old_state)
6be4a607 5539{
4a806558 5540 struct drm_crtc *crtc = old_crtc_state->base.crtc;
6be4a607 5541 struct drm_device *dev = crtc->dev;
fac5e23e 5542 struct drm_i915_private *dev_priv = to_i915(dev);
6be4a607
JB
5543 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5544 int pipe = intel_crtc->pipe;
b52eb4dc 5545
b2c0593a
VS
5546 /*
5547 * Sometimes spurious CPU pipe underruns happen when the
5548 * pipe is already disabled, but FDI RX/TX is still enabled.
5549 * Happens at least with VGA+HDMI cloning. Suppress them.
5550 */
5551 if (intel_crtc->config->has_pch_encoder) {
5552 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
37ca8d4c 5553 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
b2c0593a 5554 }
37ca8d4c 5555
fd6bbda9 5556 intel_encoders_disable(crtc, old_crtc_state, old_state);
ea9d758d 5557
f9b61ff6
SV
5558 drm_crtc_vblank_off(crtc);
5559 assert_vblank_disabled(crtc);
5560
575f7ab7 5561 intel_disable_pipe(intel_crtc);
32f9d658 5562
bfd16b2a 5563 ironlake_pfit_disable(intel_crtc, false);
2c07245f 5564
b2c0593a 5565 if (intel_crtc->config->has_pch_encoder)
5a74f70a
VS
5566 ironlake_fdi_disable(crtc);
5567
fd6bbda9 5568 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
2c07245f 5569
6e3c9717 5570 if (intel_crtc->config->has_pch_encoder) {
d925c59a 5571 ironlake_disable_pch_transcoder(dev_priv, pipe);
6be4a607 5572
6e266956 5573 if (HAS_PCH_CPT(dev_priv)) {
f0f59a00
VS
5574 i915_reg_t reg;
5575 u32 temp;
5576
d925c59a
SV
5577 /* disable TRANS_DP_CTL */
5578 reg = TRANS_DP_CTL(pipe);
5579 temp = I915_READ(reg);
5580 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5581 TRANS_DP_PORT_SEL_MASK);
5582 temp |= TRANS_DP_PORT_SEL_NONE;
5583 I915_WRITE(reg, temp);
5584
5585 /* disable DPLL_SEL */
5586 temp = I915_READ(PCH_DPLL_SEL);
11887397 5587 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 5588 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 5589 }
e3421a18 5590
d925c59a
SV
5591 ironlake_fdi_pll_disable(intel_crtc);
5592 }
81b088ca 5593
b2c0593a 5594 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
81b088ca 5595 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607 5596}
1b3c7a47 5597
4a806558
ML
5598static void haswell_crtc_disable(struct intel_crtc_state *old_crtc_state,
5599 struct drm_atomic_state *old_state)
ee7b9f93 5600{
4a806558 5601 struct drm_crtc *crtc = old_crtc_state->base.crtc;
4f771f10 5602 struct drm_device *dev = crtc->dev;
fac5e23e 5603 struct drm_i915_private *dev_priv = to_i915(dev);
ee7b9f93 5604 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 5605 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee7b9f93 5606
d2d65408
VS
5607 if (intel_crtc->config->has_pch_encoder)
5608 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5609 false);
5610
fd6bbda9 5611 intel_encoders_disable(crtc, old_crtc_state, old_state);
4f771f10 5612
f9b61ff6
SV
5613 drm_crtc_vblank_off(crtc);
5614 assert_vblank_disabled(crtc);
5615
4d1de975 5616 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
d7edc4e5 5617 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975 5618 intel_disable_pipe(intel_crtc);
4f771f10 5619
6e3c9717 5620 if (intel_crtc->config->dp_encoder_is_mst)
a4bf214f
VS
5621 intel_ddi_set_vc_payload_alloc(crtc, false);
5622
d7edc4e5 5623 if (!transcoder_is_dsi(cpu_transcoder))
7d4aefd0 5624 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4f771f10 5625
1c132b44 5626 if (INTEL_INFO(dev)->gen >= 9)
e435d6e5 5627 skylake_scaler_disable(intel_crtc);
ff6d9f55 5628 else
bfd16b2a 5629 ironlake_pfit_disable(intel_crtc, false);
4f771f10 5630
d7edc4e5 5631 if (!transcoder_is_dsi(cpu_transcoder))
7d4aefd0 5632 intel_ddi_disable_pipe_clock(intel_crtc);
4f771f10 5633
fd6bbda9 5634 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
81b088ca 5635
b7076546 5636 if (old_crtc_state->has_pch_encoder)
81b088ca
VS
5637 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5638 true);
4f771f10
PZ
5639}
5640
2dd24552
JB
5641static void i9xx_pfit_enable(struct intel_crtc *crtc)
5642{
5643 struct drm_device *dev = crtc->base.dev;
fac5e23e 5644 struct drm_i915_private *dev_priv = to_i915(dev);
6e3c9717 5645 struct intel_crtc_state *pipe_config = crtc->config;
2dd24552 5646
681a8504 5647 if (!pipe_config->gmch_pfit.control)
2dd24552
JB
5648 return;
5649
2dd24552 5650 /*
c0b03411
SV
5651 * The panel fitter should only be adjusted whilst the pipe is disabled,
5652 * according to register description and PRM.
2dd24552 5653 */
c0b03411
SV
5654 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5655 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 5656
b074cec8
JB
5657 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5658 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
SV
5659
5660 /* Border color in case we don't scale up to the full screen. Black by
5661 * default, change to something else for debugging. */
5662 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
5663}
5664
d05410f9
DA
5665static enum intel_display_power_domain port_to_power_domain(enum port port)
5666{
5667 switch (port) {
5668 case PORT_A:
6331a704 5669 return POWER_DOMAIN_PORT_DDI_A_LANES;
d05410f9 5670 case PORT_B:
6331a704 5671 return POWER_DOMAIN_PORT_DDI_B_LANES;
d05410f9 5672 case PORT_C:
6331a704 5673 return POWER_DOMAIN_PORT_DDI_C_LANES;
d05410f9 5674 case PORT_D:
6331a704 5675 return POWER_DOMAIN_PORT_DDI_D_LANES;
d8e19f99 5676 case PORT_E:
6331a704 5677 return POWER_DOMAIN_PORT_DDI_E_LANES;
d05410f9 5678 default:
b9fec167 5679 MISSING_CASE(port);
d05410f9
DA
5680 return POWER_DOMAIN_PORT_OTHER;
5681 }
5682}
5683
25f78f58
VS
5684static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5685{
5686 switch (port) {
5687 case PORT_A:
5688 return POWER_DOMAIN_AUX_A;
5689 case PORT_B:
5690 return POWER_DOMAIN_AUX_B;
5691 case PORT_C:
5692 return POWER_DOMAIN_AUX_C;
5693 case PORT_D:
5694 return POWER_DOMAIN_AUX_D;
5695 case PORT_E:
5696 /* FIXME: Check VBT for actual wiring of PORT E */
5697 return POWER_DOMAIN_AUX_D;
5698 default:
b9fec167 5699 MISSING_CASE(port);
25f78f58
VS
5700 return POWER_DOMAIN_AUX_A;
5701 }
5702}
5703
319be8ae
ID
5704enum intel_display_power_domain
5705intel_display_port_power_domain(struct intel_encoder *intel_encoder)
5706{
4f8036a2 5707 struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
319be8ae
ID
5708 struct intel_digital_port *intel_dig_port;
5709
5710 switch (intel_encoder->type) {
5711 case INTEL_OUTPUT_UNKNOWN:
5712 /* Only DDI platforms should ever use this output type */
4f8036a2 5713 WARN_ON_ONCE(!HAS_DDI(dev_priv));
cca0502b 5714 case INTEL_OUTPUT_DP:
319be8ae
ID
5715 case INTEL_OUTPUT_HDMI:
5716 case INTEL_OUTPUT_EDP:
5717 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
d05410f9 5718 return port_to_power_domain(intel_dig_port->port);
0e32b39c
DA
5719 case INTEL_OUTPUT_DP_MST:
5720 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5721 return port_to_power_domain(intel_dig_port->port);
319be8ae
ID
5722 case INTEL_OUTPUT_ANALOG:
5723 return POWER_DOMAIN_PORT_CRT;
5724 case INTEL_OUTPUT_DSI:
5725 return POWER_DOMAIN_PORT_DSI;
5726 default:
5727 return POWER_DOMAIN_PORT_OTHER;
5728 }
5729}
5730
25f78f58
VS
5731enum intel_display_power_domain
5732intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5733{
4f8036a2 5734 struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
25f78f58
VS
5735 struct intel_digital_port *intel_dig_port;
5736
5737 switch (intel_encoder->type) {
5738 case INTEL_OUTPUT_UNKNOWN:
651174a4
ID
5739 case INTEL_OUTPUT_HDMI:
5740 /*
5741 * Only DDI platforms should ever use these output types.
5742 * We can get here after the HDMI detect code has already set
5743 * the type of the shared encoder. Since we can't be sure
5744 * what's the status of the given connectors, play safe and
5745 * run the DP detection too.
5746 */
4f8036a2 5747 WARN_ON_ONCE(!HAS_DDI(dev_priv));
cca0502b 5748 case INTEL_OUTPUT_DP:
25f78f58
VS
5749 case INTEL_OUTPUT_EDP:
5750 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5751 return port_to_aux_power_domain(intel_dig_port->port);
5752 case INTEL_OUTPUT_DP_MST:
5753 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5754 return port_to_aux_power_domain(intel_dig_port->port);
5755 default:
b9fec167 5756 MISSING_CASE(intel_encoder->type);
25f78f58
VS
5757 return POWER_DOMAIN_AUX_A;
5758 }
5759}
5760
74bff5f9
ML
5761static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
5762 struct intel_crtc_state *crtc_state)
77d22dca 5763{
319be8ae 5764 struct drm_device *dev = crtc->dev;
74bff5f9 5765 struct drm_encoder *encoder;
319be8ae
ID
5766 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5767 enum pipe pipe = intel_crtc->pipe;
77d22dca 5768 unsigned long mask;
74bff5f9 5769 enum transcoder transcoder = crtc_state->cpu_transcoder;
77d22dca 5770
74bff5f9 5771 if (!crtc_state->base.active)
292b990e
ML
5772 return 0;
5773
77d22dca
ID
5774 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5775 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
74bff5f9
ML
5776 if (crtc_state->pch_pfit.enabled ||
5777 crtc_state->pch_pfit.force_thru)
77d22dca
ID
5778 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5779
74bff5f9
ML
5780 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5781 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5782
319be8ae 5783 mask |= BIT(intel_display_port_power_domain(intel_encoder));
74bff5f9 5784 }
319be8ae 5785
15e7ec29
ML
5786 if (crtc_state->shared_dpll)
5787 mask |= BIT(POWER_DOMAIN_PLLS);
5788
77d22dca
ID
5789 return mask;
5790}
5791
74bff5f9
ML
5792static unsigned long
5793modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5794 struct intel_crtc_state *crtc_state)
77d22dca 5795{
fac5e23e 5796 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
292b990e
ML
5797 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5798 enum intel_display_power_domain domain;
5a21b665 5799 unsigned long domains, new_domains, old_domains;
77d22dca 5800
292b990e 5801 old_domains = intel_crtc->enabled_power_domains;
74bff5f9
ML
5802 intel_crtc->enabled_power_domains = new_domains =
5803 get_crtc_power_domains(crtc, crtc_state);
77d22dca 5804
5a21b665 5805 domains = new_domains & ~old_domains;
292b990e
ML
5806
5807 for_each_power_domain(domain, domains)
5808 intel_display_power_get(dev_priv, domain);
5809
5a21b665 5810 return old_domains & ~new_domains;
292b990e
ML
5811}
5812
5813static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5814 unsigned long domains)
5815{
5816 enum intel_display_power_domain domain;
5817
5818 for_each_power_domain(domain, domains)
5819 intel_display_power_put(dev_priv, domain);
5820}
77d22dca 5821
adafdc6f
MK
5822static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5823{
5824 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5825
5826 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5827 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5828 return max_cdclk_freq;
5829 else if (IS_CHERRYVIEW(dev_priv))
5830 return max_cdclk_freq*95/100;
5831 else if (INTEL_INFO(dev_priv)->gen < 4)
5832 return 2*max_cdclk_freq*90/100;
5833 else
5834 return max_cdclk_freq*90/100;
5835}
5836
b2045352
VS
5837static int skl_calc_cdclk(int max_pixclk, int vco);
5838
560a7ae4
DL
5839static void intel_update_max_cdclk(struct drm_device *dev)
5840{
fac5e23e 5841 struct drm_i915_private *dev_priv = to_i915(dev);
560a7ae4 5842
0853723b 5843 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
560a7ae4 5844 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
b2045352
VS
5845 int max_cdclk, vco;
5846
5847 vco = dev_priv->skl_preferred_vco_freq;
63911d72 5848 WARN_ON(vco != 8100000 && vco != 8640000);
560a7ae4 5849
b2045352
VS
5850 /*
5851 * Use the lower (vco 8640) cdclk values as a
5852 * first guess. skl_calc_cdclk() will correct it
5853 * if the preferred vco is 8100 instead.
5854 */
560a7ae4 5855 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
487ed2e4 5856 max_cdclk = 617143;
560a7ae4 5857 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
b2045352 5858 max_cdclk = 540000;
560a7ae4 5859 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
b2045352 5860 max_cdclk = 432000;
560a7ae4 5861 else
487ed2e4 5862 max_cdclk = 308571;
b2045352
VS
5863
5864 dev_priv->max_cdclk_freq = skl_calc_cdclk(max_cdclk, vco);
e2d214ae 5865 } else if (IS_BROXTON(dev_priv)) {
281c114f 5866 dev_priv->max_cdclk_freq = 624000;
8652744b 5867 } else if (IS_BROADWELL(dev_priv)) {
560a7ae4
DL
5868 /*
5869 * FIXME with extra cooling we can allow
5870 * 540 MHz for ULX and 675 Mhz for ULT.
5871 * How can we know if extra cooling is
5872 * available? PCI ID, VTB, something else?
5873 */
5874 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5875 dev_priv->max_cdclk_freq = 450000;
50a0bc90 5876 else if (IS_BDW_ULX(dev_priv))
560a7ae4 5877 dev_priv->max_cdclk_freq = 450000;
50a0bc90 5878 else if (IS_BDW_ULT(dev_priv))
560a7ae4
DL
5879 dev_priv->max_cdclk_freq = 540000;
5880 else
5881 dev_priv->max_cdclk_freq = 675000;
920a14b2 5882 } else if (IS_CHERRYVIEW(dev_priv)) {
0904deaf 5883 dev_priv->max_cdclk_freq = 320000;
11a914c2 5884 } else if (IS_VALLEYVIEW(dev_priv)) {
560a7ae4
DL
5885 dev_priv->max_cdclk_freq = 400000;
5886 } else {
5887 /* otherwise assume cdclk is fixed */
5888 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5889 }
5890
adafdc6f
MK
5891 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5892
560a7ae4
DL
5893 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5894 dev_priv->max_cdclk_freq);
adafdc6f
MK
5895
5896 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5897 dev_priv->max_dotclk_freq);
560a7ae4
DL
5898}
5899
5900static void intel_update_cdclk(struct drm_device *dev)
5901{
fac5e23e 5902 struct drm_i915_private *dev_priv = to_i915(dev);
560a7ae4
DL
5903
5904 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
2f2a121a 5905
83d7c81f 5906 if (INTEL_GEN(dev_priv) >= 9)
709e05c3
VS
5907 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz, VCO: %d kHz, ref: %d kHz\n",
5908 dev_priv->cdclk_freq, dev_priv->cdclk_pll.vco,
5909 dev_priv->cdclk_pll.ref);
2f2a121a
VS
5910 else
5911 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5912 dev_priv->cdclk_freq);
560a7ae4
DL
5913
5914 /*
b5d99ff9
VS
5915 * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
5916 * Programmng [sic] note: bit[9:2] should be programmed to the number
5917 * of cdclk that generates 4MHz reference clock freq which is used to
5918 * generate GMBus clock. This will vary with the cdclk freq.
560a7ae4 5919 */
b5d99ff9 5920 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
560a7ae4 5921 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
560a7ae4
DL
5922}
5923
92891e45
VS
5924/* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5925static int skl_cdclk_decimal(int cdclk)
5926{
5927 return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
5928}
5929
5f199dfa
VS
5930static int bxt_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
5931{
5932 int ratio;
5933
5934 if (cdclk == dev_priv->cdclk_pll.ref)
5935 return 0;
5936
5937 switch (cdclk) {
5938 default:
5939 MISSING_CASE(cdclk);
5940 case 144000:
5941 case 288000:
5942 case 384000:
5943 case 576000:
5944 ratio = 60;
5945 break;
5946 case 624000:
5947 ratio = 65;
5948 break;
5949 }
5950
5951 return dev_priv->cdclk_pll.ref * ratio;
5952}
5953
2b73001e
VS
5954static void bxt_de_pll_disable(struct drm_i915_private *dev_priv)
5955{
5956 I915_WRITE(BXT_DE_PLL_ENABLE, 0);
5957
5958 /* Timeout 200us */
95cac283
CW
5959 if (intel_wait_for_register(dev_priv,
5960 BXT_DE_PLL_ENABLE, BXT_DE_PLL_LOCK, 0,
5961 1))
2b73001e 5962 DRM_ERROR("timeout waiting for DE PLL unlock\n");
83d7c81f
VS
5963
5964 dev_priv->cdclk_pll.vco = 0;
2b73001e
VS
5965}
5966
5f199dfa 5967static void bxt_de_pll_enable(struct drm_i915_private *dev_priv, int vco)
2b73001e 5968{
5f199dfa 5969 int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->cdclk_pll.ref);
2b73001e
VS
5970 u32 val;
5971
5972 val = I915_READ(BXT_DE_PLL_CTL);
5973 val &= ~BXT_DE_PLL_RATIO_MASK;
5f199dfa 5974 val |= BXT_DE_PLL_RATIO(ratio);
2b73001e
VS
5975 I915_WRITE(BXT_DE_PLL_CTL, val);
5976
5977 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5978
5979 /* Timeout 200us */
e084e1b9
CW
5980 if (intel_wait_for_register(dev_priv,
5981 BXT_DE_PLL_ENABLE,
5982 BXT_DE_PLL_LOCK,
5983 BXT_DE_PLL_LOCK,
5984 1))
2b73001e 5985 DRM_ERROR("timeout waiting for DE PLL lock\n");
83d7c81f 5986
5f199dfa 5987 dev_priv->cdclk_pll.vco = vco;
2b73001e
VS
5988}
5989
324513c0 5990static void bxt_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
f8437dd1 5991{
5f199dfa
VS
5992 u32 val, divider;
5993 int vco, ret;
f8437dd1 5994
5f199dfa
VS
5995 vco = bxt_de_pll_vco(dev_priv, cdclk);
5996
5997 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
5998
5999 /* cdclk = vco / 2 / div{1,1.5,2,4} */
6000 switch (DIV_ROUND_CLOSEST(vco, cdclk)) {
6001 case 8:
f8437dd1 6002 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
f8437dd1 6003 break;
5f199dfa 6004 case 4:
f8437dd1 6005 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
f8437dd1 6006 break;
5f199dfa 6007 case 3:
f8437dd1 6008 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
f8437dd1 6009 break;
5f199dfa 6010 case 2:
f8437dd1 6011 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
f8437dd1
VK
6012 break;
6013 default:
5f199dfa
VS
6014 WARN_ON(cdclk != dev_priv->cdclk_pll.ref);
6015 WARN_ON(vco != 0);
f8437dd1 6016
5f199dfa
VS
6017 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
6018 break;
f8437dd1
VK
6019 }
6020
f8437dd1 6021 /* Inform power controller of upcoming frequency change */
5f199dfa 6022 mutex_lock(&dev_priv->rps.hw_lock);
f8437dd1
VK
6023 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
6024 0x80000000);
6025 mutex_unlock(&dev_priv->rps.hw_lock);
6026
6027 if (ret) {
6028 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
9ef56154 6029 ret, cdclk);
f8437dd1
VK
6030 return;
6031 }
6032
5f199dfa
VS
6033 if (dev_priv->cdclk_pll.vco != 0 &&
6034 dev_priv->cdclk_pll.vco != vco)
2b73001e 6035 bxt_de_pll_disable(dev_priv);
f8437dd1 6036
5f199dfa
VS
6037 if (dev_priv->cdclk_pll.vco != vco)
6038 bxt_de_pll_enable(dev_priv, vco);
f8437dd1 6039
5f199dfa
VS
6040 val = divider | skl_cdclk_decimal(cdclk);
6041 /*
6042 * FIXME if only the cd2x divider needs changing, it could be done
6043 * without shutting off the pipe (if only one pipe is active).
6044 */
6045 val |= BXT_CDCLK_CD2X_PIPE_NONE;
6046 /*
6047 * Disable SSA Precharge when CD clock frequency < 500 MHz,
6048 * enable otherwise.
6049 */
6050 if (cdclk >= 500000)
6051 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
6052 I915_WRITE(CDCLK_CTL, val);
f8437dd1
VK
6053
6054 mutex_lock(&dev_priv->rps.hw_lock);
6055 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
9ef56154 6056 DIV_ROUND_UP(cdclk, 25000));
f8437dd1
VK
6057 mutex_unlock(&dev_priv->rps.hw_lock);
6058
6059 if (ret) {
6060 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
9ef56154 6061 ret, cdclk);
f8437dd1
VK
6062 return;
6063 }
6064
91c8a326 6065 intel_update_cdclk(&dev_priv->drm);
f8437dd1
VK
6066}
6067
d66a2194 6068static void bxt_sanitize_cdclk(struct drm_i915_private *dev_priv)
f8437dd1 6069{
d66a2194
ID
6070 u32 cdctl, expected;
6071
91c8a326 6072 intel_update_cdclk(&dev_priv->drm);
f8437dd1 6073
d66a2194
ID
6074 if (dev_priv->cdclk_pll.vco == 0 ||
6075 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
6076 goto sanitize;
6077
6078 /* DPLL okay; verify the cdclock
6079 *
6080 * Some BIOS versions leave an incorrect decimal frequency value and
6081 * set reserved MBZ bits in CDCLK_CTL at least during exiting from S4,
6082 * so sanitize this register.
6083 */
6084 cdctl = I915_READ(CDCLK_CTL);
6085 /*
6086 * Let's ignore the pipe field, since BIOS could have configured the
6087 * dividers both synching to an active pipe, or asynchronously
6088 * (PIPE_NONE).
6089 */
6090 cdctl &= ~BXT_CDCLK_CD2X_PIPE_NONE;
6091
6092 expected = (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) |
6093 skl_cdclk_decimal(dev_priv->cdclk_freq);
6094 /*
6095 * Disable SSA Precharge when CD clock frequency < 500 MHz,
6096 * enable otherwise.
6097 */
6098 if (dev_priv->cdclk_freq >= 500000)
6099 expected |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
6100
6101 if (cdctl == expected)
6102 /* All well; nothing to sanitize */
6103 return;
6104
6105sanitize:
6106 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
6107
6108 /* force cdclk programming */
6109 dev_priv->cdclk_freq = 0;
6110
6111 /* force full PLL disable + enable */
6112 dev_priv->cdclk_pll.vco = -1;
6113}
6114
324513c0 6115void bxt_init_cdclk(struct drm_i915_private *dev_priv)
d66a2194
ID
6116{
6117 bxt_sanitize_cdclk(dev_priv);
6118
6119 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0)
089c6fd5 6120 return;
c2e001ef 6121
f8437dd1
VK
6122 /*
6123 * FIXME:
6124 * - The initial CDCLK needs to be read from VBT.
6125 * Need to make this change after VBT has changes for BXT.
f8437dd1 6126 */
324513c0 6127 bxt_set_cdclk(dev_priv, bxt_calc_cdclk(0));
f8437dd1
VK
6128}
6129
324513c0 6130void bxt_uninit_cdclk(struct drm_i915_private *dev_priv)
f8437dd1 6131{
324513c0 6132 bxt_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref);
f8437dd1
VK
6133}
6134
a8ca4934
VS
6135static int skl_calc_cdclk(int max_pixclk, int vco)
6136{
63911d72 6137 if (vco == 8640000) {
a8ca4934 6138 if (max_pixclk > 540000)
487ed2e4 6139 return 617143;
a8ca4934
VS
6140 else if (max_pixclk > 432000)
6141 return 540000;
487ed2e4 6142 else if (max_pixclk > 308571)
a8ca4934
VS
6143 return 432000;
6144 else
487ed2e4 6145 return 308571;
a8ca4934 6146 } else {
a8ca4934
VS
6147 if (max_pixclk > 540000)
6148 return 675000;
6149 else if (max_pixclk > 450000)
6150 return 540000;
6151 else if (max_pixclk > 337500)
6152 return 450000;
6153 else
6154 return 337500;
6155 }
6156}
6157
ea61791e
VS
6158static void
6159skl_dpll0_update(struct drm_i915_private *dev_priv)
5d96d8af 6160{
ea61791e 6161 u32 val;
5d96d8af 6162
709e05c3 6163 dev_priv->cdclk_pll.ref = 24000;
1c3f7700 6164 dev_priv->cdclk_pll.vco = 0;
709e05c3 6165
ea61791e 6166 val = I915_READ(LCPLL1_CTL);
1c3f7700 6167 if ((val & LCPLL_PLL_ENABLE) == 0)
ea61791e 6168 return;
5d96d8af 6169
1c3f7700
ID
6170 if (WARN_ON((val & LCPLL_PLL_LOCK) == 0))
6171 return;
9f7eb31a 6172
ea61791e
VS
6173 val = I915_READ(DPLL_CTRL1);
6174
1c3f7700
ID
6175 if (WARN_ON((val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
6176 DPLL_CTRL1_SSC(SKL_DPLL0) |
6177 DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
6178 DPLL_CTRL1_OVERRIDE(SKL_DPLL0)))
6179 return;
9f7eb31a 6180
ea61791e
VS
6181 switch (val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) {
6182 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0):
6183 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, SKL_DPLL0):
6184 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, SKL_DPLL0):
6185 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, SKL_DPLL0):
63911d72 6186 dev_priv->cdclk_pll.vco = 8100000;
ea61791e
VS
6187 break;
6188 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0):
6189 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, SKL_DPLL0):
63911d72 6190 dev_priv->cdclk_pll.vco = 8640000;
ea61791e
VS
6191 break;
6192 default:
6193 MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
ea61791e
VS
6194 break;
6195 }
5d96d8af
DL
6196}
6197
b2045352
VS
6198void skl_set_preferred_cdclk_vco(struct drm_i915_private *dev_priv, int vco)
6199{
6200 bool changed = dev_priv->skl_preferred_vco_freq != vco;
6201
6202 dev_priv->skl_preferred_vco_freq = vco;
6203
6204 if (changed)
91c8a326 6205 intel_update_max_cdclk(&dev_priv->drm);
b2045352
VS
6206}
6207
5d96d8af 6208static void
3861fc60 6209skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
5d96d8af 6210{
a8ca4934 6211 int min_cdclk = skl_calc_cdclk(0, vco);
5d96d8af
DL
6212 u32 val;
6213
63911d72 6214 WARN_ON(vco != 8100000 && vco != 8640000);
b2045352 6215
5d96d8af 6216 /* select the minimum CDCLK before enabling DPLL 0 */
9ef56154 6217 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_cdclk);
5d96d8af
DL
6218 I915_WRITE(CDCLK_CTL, val);
6219 POSTING_READ(CDCLK_CTL);
6220
6221 /*
6222 * We always enable DPLL0 with the lowest link rate possible, but still
6223 * taking into account the VCO required to operate the eDP panel at the
6224 * desired frequency. The usual DP link rates operate with a VCO of
6225 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
6226 * The modeset code is responsible for the selection of the exact link
6227 * rate later on, with the constraint of choosing a frequency that
a8ca4934 6228 * works with vco.
5d96d8af
DL
6229 */
6230 val = I915_READ(DPLL_CTRL1);
6231
6232 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
6233 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
6234 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
63911d72 6235 if (vco == 8640000)
5d96d8af
DL
6236 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
6237 SKL_DPLL0);
6238 else
6239 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
6240 SKL_DPLL0);
6241
6242 I915_WRITE(DPLL_CTRL1, val);
6243 POSTING_READ(DPLL_CTRL1);
6244
6245 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
6246
e24ca054
CW
6247 if (intel_wait_for_register(dev_priv,
6248 LCPLL1_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
6249 5))
5d96d8af 6250 DRM_ERROR("DPLL0 not locked\n");
1cd593e0 6251
63911d72 6252 dev_priv->cdclk_pll.vco = vco;
b2045352
VS
6253
6254 /* We'll want to keep using the current vco from now on. */
6255 skl_set_preferred_cdclk_vco(dev_priv, vco);
5d96d8af
DL
6256}
6257
430e05de
VS
6258static void
6259skl_dpll0_disable(struct drm_i915_private *dev_priv)
6260{
6261 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
8ad32a05
CW
6262 if (intel_wait_for_register(dev_priv,
6263 LCPLL1_CTL, LCPLL_PLL_LOCK, 0,
6264 1))
430e05de 6265 DRM_ERROR("Couldn't disable DPLL0\n");
1cd593e0 6266
63911d72 6267 dev_priv->cdclk_pll.vco = 0;
430e05de
VS
6268}
6269
5d96d8af
DL
6270static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
6271{
6272 int ret;
6273 u32 val;
6274
6275 /* inform PCU we want to change CDCLK */
6276 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
6277 mutex_lock(&dev_priv->rps.hw_lock);
6278 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
6279 mutex_unlock(&dev_priv->rps.hw_lock);
6280
6281 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
6282}
6283
6284static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
6285{
848496e5 6286 return _wait_for(skl_cdclk_pcu_ready(dev_priv), 3000, 10) == 0;
5d96d8af
DL
6287}
6288
1cd593e0 6289static void skl_set_cdclk(struct drm_i915_private *dev_priv, int cdclk, int vco)
5d96d8af 6290{
91c8a326 6291 struct drm_device *dev = &dev_priv->drm;
5d96d8af
DL
6292 u32 freq_select, pcu_ack;
6293
1cd593e0
VS
6294 WARN_ON((cdclk == 24000) != (vco == 0));
6295
63911d72 6296 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
5d96d8af
DL
6297
6298 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
6299 DRM_ERROR("failed to inform PCU about cdclk change\n");
6300 return;
6301 }
6302
6303 /* set CDCLK_CTL */
9ef56154 6304 switch (cdclk) {
5d96d8af
DL
6305 case 450000:
6306 case 432000:
6307 freq_select = CDCLK_FREQ_450_432;
6308 pcu_ack = 1;
6309 break;
6310 case 540000:
6311 freq_select = CDCLK_FREQ_540;
6312 pcu_ack = 2;
6313 break;
487ed2e4 6314 case 308571:
5d96d8af
DL
6315 case 337500:
6316 default:
6317 freq_select = CDCLK_FREQ_337_308;
6318 pcu_ack = 0;
6319 break;
487ed2e4 6320 case 617143:
5d96d8af
DL
6321 case 675000:
6322 freq_select = CDCLK_FREQ_675_617;
6323 pcu_ack = 3;
6324 break;
6325 }
6326
63911d72
VS
6327 if (dev_priv->cdclk_pll.vco != 0 &&
6328 dev_priv->cdclk_pll.vco != vco)
1cd593e0
VS
6329 skl_dpll0_disable(dev_priv);
6330
63911d72 6331 if (dev_priv->cdclk_pll.vco != vco)
1cd593e0
VS
6332 skl_dpll0_enable(dev_priv, vco);
6333
9ef56154 6334 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(cdclk));
5d96d8af
DL
6335 POSTING_READ(CDCLK_CTL);
6336
6337 /* inform PCU of the change */
6338 mutex_lock(&dev_priv->rps.hw_lock);
6339 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
6340 mutex_unlock(&dev_priv->rps.hw_lock);
560a7ae4
DL
6341
6342 intel_update_cdclk(dev);
5d96d8af
DL
6343}
6344
9f7eb31a
VS
6345static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv);
6346
5d96d8af
DL
6347void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
6348{
709e05c3 6349 skl_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref, 0);
5d96d8af
DL
6350}
6351
6352void skl_init_cdclk(struct drm_i915_private *dev_priv)
6353{
9f7eb31a
VS
6354 int cdclk, vco;
6355
6356 skl_sanitize_cdclk(dev_priv);
5d96d8af 6357
63911d72 6358 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0) {
9f7eb31a
VS
6359 /*
6360 * Use the current vco as our initial
6361 * guess as to what the preferred vco is.
6362 */
6363 if (dev_priv->skl_preferred_vco_freq == 0)
6364 skl_set_preferred_cdclk_vco(dev_priv,
63911d72 6365 dev_priv->cdclk_pll.vco);
70c2c184 6366 return;
1cd593e0 6367 }
5d96d8af 6368
70c2c184
VS
6369 vco = dev_priv->skl_preferred_vco_freq;
6370 if (vco == 0)
63911d72 6371 vco = 8100000;
70c2c184 6372 cdclk = skl_calc_cdclk(0, vco);
5d96d8af 6373
70c2c184 6374 skl_set_cdclk(dev_priv, cdclk, vco);
5d96d8af
DL
6375}
6376
9f7eb31a 6377static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
c73666f3 6378{
09492498 6379 uint32_t cdctl, expected;
c73666f3 6380
f1b391a5
SK
6381 /*
6382 * check if the pre-os intialized the display
6383 * There is SWF18 scratchpad register defined which is set by the
6384 * pre-os which can be used by the OS drivers to check the status
6385 */
6386 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
6387 goto sanitize;
6388
91c8a326 6389 intel_update_cdclk(&dev_priv->drm);
c73666f3 6390 /* Is PLL enabled and locked ? */
1c3f7700
ID
6391 if (dev_priv->cdclk_pll.vco == 0 ||
6392 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
c73666f3
SK
6393 goto sanitize;
6394
6395 /* DPLL okay; verify the cdclock
6396 *
6397 * Noticed in some instances that the freq selection is correct but
6398 * decimal part is programmed wrong from BIOS where pre-os does not
6399 * enable display. Verify the same as well.
6400 */
09492498
VS
6401 cdctl = I915_READ(CDCLK_CTL);
6402 expected = (cdctl & CDCLK_FREQ_SEL_MASK) |
6403 skl_cdclk_decimal(dev_priv->cdclk_freq);
6404 if (cdctl == expected)
c73666f3 6405 /* All well; nothing to sanitize */
9f7eb31a 6406 return;
c89e39f3 6407
9f7eb31a
VS
6408sanitize:
6409 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
c73666f3 6410
9f7eb31a
VS
6411 /* force cdclk programming */
6412 dev_priv->cdclk_freq = 0;
6413 /* force full PLL disable + enable */
63911d72 6414 dev_priv->cdclk_pll.vco = -1;
c73666f3
SK
6415}
6416
30a970c6
JB
6417/* Adjust CDclk dividers to allow high res or save power if possible */
6418static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
6419{
fac5e23e 6420 struct drm_i915_private *dev_priv = to_i915(dev);
30a970c6
JB
6421 u32 val, cmd;
6422
164dfd28
VK
6423 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
6424 != dev_priv->cdclk_freq);
d60c4473 6425
dfcab17e 6426 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
30a970c6 6427 cmd = 2;
dfcab17e 6428 else if (cdclk == 266667)
30a970c6
JB
6429 cmd = 1;
6430 else
6431 cmd = 0;
6432
6433 mutex_lock(&dev_priv->rps.hw_lock);
6434 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
6435 val &= ~DSPFREQGUAR_MASK;
6436 val |= (cmd << DSPFREQGUAR_SHIFT);
6437 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
6438 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
6439 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
6440 50)) {
6441 DRM_ERROR("timed out waiting for CDclk change\n");
6442 }
6443 mutex_unlock(&dev_priv->rps.hw_lock);
6444
54433e91
VS
6445 mutex_lock(&dev_priv->sb_lock);
6446
dfcab17e 6447 if (cdclk == 400000) {
6bcda4f0 6448 u32 divider;
30a970c6 6449
6bcda4f0 6450 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
30a970c6 6451
30a970c6
JB
6452 /* adjust cdclk divider */
6453 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
87d5d259 6454 val &= ~CCK_FREQUENCY_VALUES;
30a970c6
JB
6455 val |= divider;
6456 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
a877e801
VS
6457
6458 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
87d5d259 6459 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
a877e801
VS
6460 50))
6461 DRM_ERROR("timed out waiting for CDclk change\n");
30a970c6
JB
6462 }
6463
30a970c6
JB
6464 /* adjust self-refresh exit latency value */
6465 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
6466 val &= ~0x7f;
6467
6468 /*
6469 * For high bandwidth configs, we set a higher latency in the bunit
6470 * so that the core display fetch happens in time to avoid underruns.
6471 */
dfcab17e 6472 if (cdclk == 400000)
30a970c6
JB
6473 val |= 4500 / 250; /* 4.5 usec */
6474 else
6475 val |= 3000 / 250; /* 3.0 usec */
6476 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
54433e91 6477
a580516d 6478 mutex_unlock(&dev_priv->sb_lock);
30a970c6 6479
b6283055 6480 intel_update_cdclk(dev);
30a970c6
JB
6481}
6482
383c5a6a
VS
6483static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
6484{
fac5e23e 6485 struct drm_i915_private *dev_priv = to_i915(dev);
383c5a6a
VS
6486 u32 val, cmd;
6487
164dfd28
VK
6488 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
6489 != dev_priv->cdclk_freq);
383c5a6a
VS
6490
6491 switch (cdclk) {
383c5a6a
VS
6492 case 333333:
6493 case 320000:
383c5a6a 6494 case 266667:
383c5a6a 6495 case 200000:
383c5a6a
VS
6496 break;
6497 default:
5f77eeb0 6498 MISSING_CASE(cdclk);
383c5a6a
VS
6499 return;
6500 }
6501
9d0d3fda
VS
6502 /*
6503 * Specs are full of misinformation, but testing on actual
6504 * hardware has shown that we just need to write the desired
6505 * CCK divider into the Punit register.
6506 */
6507 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
6508
383c5a6a
VS
6509 mutex_lock(&dev_priv->rps.hw_lock);
6510 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
6511 val &= ~DSPFREQGUAR_MASK_CHV;
6512 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
6513 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
6514 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
6515 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
6516 50)) {
6517 DRM_ERROR("timed out waiting for CDclk change\n");
6518 }
6519 mutex_unlock(&dev_priv->rps.hw_lock);
6520
b6283055 6521 intel_update_cdclk(dev);
383c5a6a
VS
6522}
6523
30a970c6
JB
6524static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
6525 int max_pixclk)
6526{
6bcda4f0 6527 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
6cca3195 6528 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
29dc7ef3 6529
30a970c6
JB
6530 /*
6531 * Really only a few cases to deal with, as only 4 CDclks are supported:
6532 * 200MHz
6533 * 267MHz
29dc7ef3 6534 * 320/333MHz (depends on HPLL freq)
6cca3195
VS
6535 * 400MHz (VLV only)
6536 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
6537 * of the lower bin and adjust if needed.
e37c67a1
VS
6538 *
6539 * We seem to get an unstable or solid color picture at 200MHz.
6540 * Not sure what's wrong. For now use 200MHz only when all pipes
6541 * are off.
30a970c6 6542 */
6cca3195
VS
6543 if (!IS_CHERRYVIEW(dev_priv) &&
6544 max_pixclk > freq_320*limit/100)
dfcab17e 6545 return 400000;
6cca3195 6546 else if (max_pixclk > 266667*limit/100)
29dc7ef3 6547 return freq_320;
e37c67a1 6548 else if (max_pixclk > 0)
dfcab17e 6549 return 266667;
e37c67a1
VS
6550 else
6551 return 200000;
30a970c6
JB
6552}
6553
324513c0 6554static int bxt_calc_cdclk(int max_pixclk)
f8437dd1 6555{
760e1477 6556 if (max_pixclk > 576000)
f8437dd1 6557 return 624000;
760e1477 6558 else if (max_pixclk > 384000)
f8437dd1 6559 return 576000;
760e1477 6560 else if (max_pixclk > 288000)
f8437dd1 6561 return 384000;
760e1477 6562 else if (max_pixclk > 144000)
f8437dd1
VK
6563 return 288000;
6564 else
6565 return 144000;
6566}
6567
e8788cbc 6568/* Compute the max pixel clock for new configuration. */
a821fc46
ACO
6569static int intel_mode_max_pixclk(struct drm_device *dev,
6570 struct drm_atomic_state *state)
30a970c6 6571{
565602d7 6572 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 6573 struct drm_i915_private *dev_priv = to_i915(dev);
565602d7
ML
6574 struct drm_crtc *crtc;
6575 struct drm_crtc_state *crtc_state;
6576 unsigned max_pixclk = 0, i;
6577 enum pipe pipe;
30a970c6 6578
565602d7
ML
6579 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
6580 sizeof(intel_state->min_pixclk));
304603f4 6581
565602d7
ML
6582 for_each_crtc_in_state(state, crtc, crtc_state, i) {
6583 int pixclk = 0;
6584
6585 if (crtc_state->enable)
6586 pixclk = crtc_state->adjusted_mode.crtc_clock;
304603f4 6587
565602d7 6588 intel_state->min_pixclk[i] = pixclk;
30a970c6
JB
6589 }
6590
565602d7
ML
6591 for_each_pipe(dev_priv, pipe)
6592 max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
6593
30a970c6
JB
6594 return max_pixclk;
6595}
6596
27c329ed 6597static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
30a970c6 6598{
27c329ed 6599 struct drm_device *dev = state->dev;
fac5e23e 6600 struct drm_i915_private *dev_priv = to_i915(dev);
27c329ed 6601 int max_pixclk = intel_mode_max_pixclk(dev, state);
1a617b77
ML
6602 struct intel_atomic_state *intel_state =
6603 to_intel_atomic_state(state);
30a970c6 6604
1a617b77 6605 intel_state->cdclk = intel_state->dev_cdclk =
27c329ed 6606 valleyview_calc_cdclk(dev_priv, max_pixclk);
0a9ab303 6607
1a617b77
ML
6608 if (!intel_state->active_crtcs)
6609 intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
6610
27c329ed
ML
6611 return 0;
6612}
304603f4 6613
324513c0 6614static int bxt_modeset_calc_cdclk(struct drm_atomic_state *state)
27c329ed 6615{
4e5ca60f 6616 int max_pixclk = ilk_max_pixel_rate(state);
1a617b77
ML
6617 struct intel_atomic_state *intel_state =
6618 to_intel_atomic_state(state);
85a96e7a 6619
1a617b77 6620 intel_state->cdclk = intel_state->dev_cdclk =
324513c0 6621 bxt_calc_cdclk(max_pixclk);
85a96e7a 6622
1a617b77 6623 if (!intel_state->active_crtcs)
324513c0 6624 intel_state->dev_cdclk = bxt_calc_cdclk(0);
1a617b77 6625
27c329ed 6626 return 0;
30a970c6
JB
6627}
6628
1e69cd74
VS
6629static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
6630{
6631 unsigned int credits, default_credits;
6632
6633 if (IS_CHERRYVIEW(dev_priv))
6634 default_credits = PFI_CREDIT(12);
6635 else
6636 default_credits = PFI_CREDIT(8);
6637
bfa7df01 6638 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
1e69cd74
VS
6639 /* CHV suggested value is 31 or 63 */
6640 if (IS_CHERRYVIEW(dev_priv))
fcc0008f 6641 credits = PFI_CREDIT_63;
1e69cd74
VS
6642 else
6643 credits = PFI_CREDIT(15);
6644 } else {
6645 credits = default_credits;
6646 }
6647
6648 /*
6649 * WA - write default credits before re-programming
6650 * FIXME: should we also set the resend bit here?
6651 */
6652 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6653 default_credits);
6654
6655 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6656 credits | PFI_CREDIT_RESEND);
6657
6658 /*
6659 * FIXME is this guaranteed to clear
6660 * immediately or should we poll for it?
6661 */
6662 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6663}
6664
27c329ed 6665static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
30a970c6 6666{
a821fc46 6667 struct drm_device *dev = old_state->dev;
fac5e23e 6668 struct drm_i915_private *dev_priv = to_i915(dev);
1a617b77
ML
6669 struct intel_atomic_state *old_intel_state =
6670 to_intel_atomic_state(old_state);
6671 unsigned req_cdclk = old_intel_state->dev_cdclk;
30a970c6 6672
27c329ed
ML
6673 /*
6674 * FIXME: We can end up here with all power domains off, yet
6675 * with a CDCLK frequency other than the minimum. To account
6676 * for this take the PIPE-A power domain, which covers the HW
6677 * blocks needed for the following programming. This can be
6678 * removed once it's guaranteed that we get here either with
6679 * the minimum CDCLK set, or the required power domains
6680 * enabled.
6681 */
6682 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
738c05c0 6683
920a14b2 6684 if (IS_CHERRYVIEW(dev_priv))
27c329ed
ML
6685 cherryview_set_cdclk(dev, req_cdclk);
6686 else
6687 valleyview_set_cdclk(dev, req_cdclk);
738c05c0 6688
27c329ed 6689 vlv_program_pfi_credits(dev_priv);
1e69cd74 6690
27c329ed 6691 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
30a970c6
JB
6692}
6693
4a806558
ML
6694static void valleyview_crtc_enable(struct intel_crtc_state *pipe_config,
6695 struct drm_atomic_state *old_state)
89b667f8 6696{
4a806558 6697 struct drm_crtc *crtc = pipe_config->base.crtc;
89b667f8 6698 struct drm_device *dev = crtc->dev;
a72e4c9f 6699 struct drm_i915_private *dev_priv = to_i915(dev);
89b667f8 6700 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
89b667f8 6701 int pipe = intel_crtc->pipe;
89b667f8 6702
53d9f4e9 6703 if (WARN_ON(intel_crtc->active))
89b667f8
JB
6704 return;
6705
37a5650b 6706 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 6707 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
SV
6708
6709 intel_set_pipe_timings(intel_crtc);
bc58be60 6710 intel_set_pipe_src_size(intel_crtc);
5b18e57c 6711
920a14b2 6712 if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
fac5e23e 6713 struct drm_i915_private *dev_priv = to_i915(dev);
c14b0485
VS
6714
6715 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6716 I915_WRITE(CHV_CANVAS(pipe), 0);
6717 }
6718
5b18e57c
SV
6719 i9xx_set_pipeconf(intel_crtc);
6720
89b667f8 6721 intel_crtc->active = true;
89b667f8 6722
a72e4c9f 6723 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6724
fd6bbda9 6725 intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
89b667f8 6726
920a14b2 6727 if (IS_CHERRYVIEW(dev_priv)) {
cd2d34d9
VS
6728 chv_prepare_pll(intel_crtc, intel_crtc->config);
6729 chv_enable_pll(intel_crtc, intel_crtc->config);
6730 } else {
6731 vlv_prepare_pll(intel_crtc, intel_crtc->config);
6732 vlv_enable_pll(intel_crtc, intel_crtc->config);
9d556c99 6733 }
89b667f8 6734
fd6bbda9 6735 intel_encoders_pre_enable(crtc, pipe_config, old_state);
89b667f8 6736
2dd24552
JB
6737 i9xx_pfit_enable(intel_crtc);
6738
b95c5321 6739 intel_color_load_luts(&pipe_config->base);
63cbb074 6740
caed361d 6741 intel_update_watermarks(crtc);
e1fdc473 6742 intel_enable_pipe(intel_crtc);
be6a6f8e 6743
4b3a9526
VS
6744 assert_vblank_disabled(crtc);
6745 drm_crtc_vblank_on(crtc);
6746
fd6bbda9 6747 intel_encoders_enable(crtc, pipe_config, old_state);
89b667f8
JB
6748}
6749
f13c2ef3
SV
6750static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6751{
6752 struct drm_device *dev = crtc->base.dev;
fac5e23e 6753 struct drm_i915_private *dev_priv = to_i915(dev);
f13c2ef3 6754
6e3c9717
ACO
6755 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6756 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
f13c2ef3
SV
6757}
6758
4a806558
ML
6759static void i9xx_crtc_enable(struct intel_crtc_state *pipe_config,
6760 struct drm_atomic_state *old_state)
79e53945 6761{
4a806558 6762 struct drm_crtc *crtc = pipe_config->base.crtc;
79e53945 6763 struct drm_device *dev = crtc->dev;
a72e4c9f 6764 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 6765 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cd2d34d9 6766 enum pipe pipe = intel_crtc->pipe;
79e53945 6767
53d9f4e9 6768 if (WARN_ON(intel_crtc->active))
f7abfe8b
CW
6769 return;
6770
f13c2ef3
SV
6771 i9xx_set_pll_dividers(intel_crtc);
6772
37a5650b 6773 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 6774 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
SV
6775
6776 intel_set_pipe_timings(intel_crtc);
bc58be60 6777 intel_set_pipe_src_size(intel_crtc);
5b18e57c 6778
5b18e57c
SV
6779 i9xx_set_pipeconf(intel_crtc);
6780
f7abfe8b 6781 intel_crtc->active = true;
6b383a7f 6782
5db94019 6783 if (!IS_GEN2(dev_priv))
a72e4c9f 6784 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6785
fd6bbda9 6786 intel_encoders_pre_enable(crtc, pipe_config, old_state);
9d6d9f19 6787
f6736a1a
SV
6788 i9xx_enable_pll(intel_crtc);
6789
2dd24552
JB
6790 i9xx_pfit_enable(intel_crtc);
6791
b95c5321 6792 intel_color_load_luts(&pipe_config->base);
63cbb074 6793
f37fcc2a 6794 intel_update_watermarks(crtc);
e1fdc473 6795 intel_enable_pipe(intel_crtc);
be6a6f8e 6796
4b3a9526
VS
6797 assert_vblank_disabled(crtc);
6798 drm_crtc_vblank_on(crtc);
6799
fd6bbda9 6800 intel_encoders_enable(crtc, pipe_config, old_state);
0b8765c6 6801}
79e53945 6802
87476d63
SV
6803static void i9xx_pfit_disable(struct intel_crtc *crtc)
6804{
6805 struct drm_device *dev = crtc->base.dev;
fac5e23e 6806 struct drm_i915_private *dev_priv = to_i915(dev);
87476d63 6807
6e3c9717 6808 if (!crtc->config->gmch_pfit.control)
328d8e82 6809 return;
87476d63 6810
328d8e82 6811 assert_pipe_disabled(dev_priv, crtc->pipe);
87476d63 6812
328d8e82
SV
6813 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6814 I915_READ(PFIT_CONTROL));
6815 I915_WRITE(PFIT_CONTROL, 0);
87476d63
SV
6816}
6817
4a806558
ML
6818static void i9xx_crtc_disable(struct intel_crtc_state *old_crtc_state,
6819 struct drm_atomic_state *old_state)
0b8765c6 6820{
4a806558 6821 struct drm_crtc *crtc = old_crtc_state->base.crtc;
0b8765c6 6822 struct drm_device *dev = crtc->dev;
fac5e23e 6823 struct drm_i915_private *dev_priv = to_i915(dev);
0b8765c6
JB
6824 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6825 int pipe = intel_crtc->pipe;
ef9c3aee 6826
6304cd91
VS
6827 /*
6828 * On gen2 planes are double buffered but the pipe isn't, so we must
6829 * wait for planes to fully turn off before disabling the pipe.
6830 */
5db94019 6831 if (IS_GEN2(dev_priv))
90e83e53 6832 intel_wait_for_vblank(dev, pipe);
6304cd91 6833
fd6bbda9 6834 intel_encoders_disable(crtc, old_crtc_state, old_state);
4b3a9526 6835
f9b61ff6
SV
6836 drm_crtc_vblank_off(crtc);
6837 assert_vblank_disabled(crtc);
6838
575f7ab7 6839 intel_disable_pipe(intel_crtc);
24a1f16d 6840
87476d63 6841 i9xx_pfit_disable(intel_crtc);
24a1f16d 6842
fd6bbda9 6843 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
89b667f8 6844
d7edc4e5 6845 if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
920a14b2 6846 if (IS_CHERRYVIEW(dev_priv))
076ed3b2 6847 chv_disable_pll(dev_priv, pipe);
11a914c2 6848 else if (IS_VALLEYVIEW(dev_priv))
076ed3b2
CML
6849 vlv_disable_pll(dev_priv, pipe);
6850 else
1c4e0274 6851 i9xx_disable_pll(intel_crtc);
076ed3b2 6852 }
0b8765c6 6853
fd6bbda9 6854 intel_encoders_post_pll_disable(crtc, old_crtc_state, old_state);
d6db995f 6855
5db94019 6856 if (!IS_GEN2(dev_priv))
a72e4c9f 6857 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
0b8765c6
JB
6858}
6859
b17d48e2
ML
6860static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
6861{
842e0307 6862 struct intel_encoder *encoder;
b17d48e2
ML
6863 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6864 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6865 enum intel_display_power_domain domain;
6866 unsigned long domains;
4a806558
ML
6867 struct drm_atomic_state *state;
6868 struct intel_crtc_state *crtc_state;
6869 int ret;
b17d48e2
ML
6870
6871 if (!intel_crtc->active)
6872 return;
6873
936e71e3 6874 if (to_intel_plane_state(crtc->primary->state)->base.visible) {
5a21b665 6875 WARN_ON(intel_crtc->flip_work);
fc32b1fd 6876
2622a081 6877 intel_pre_disable_primary_noatomic(crtc);
54a41961
ML
6878
6879 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
936e71e3 6880 to_intel_plane_state(crtc->primary->state)->base.visible = false;
a539205a
ML
6881 }
6882
4a806558
ML
6883 state = drm_atomic_state_alloc(crtc->dev);
6884 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
6885
6886 /* Everything's already locked, -EDEADLK can't happen. */
6887 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
6888 ret = drm_atomic_add_affected_connectors(state, crtc);
6889
6890 WARN_ON(IS_ERR(crtc_state) || ret);
6891
6892 dev_priv->display.crtc_disable(crtc_state, state);
6893
6894 drm_atomic_state_free(state);
842e0307 6895
78108b7c
VS
6896 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
6897 crtc->base.id, crtc->name);
842e0307
ML
6898
6899 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6900 crtc->state->active = false;
37d9078b 6901 intel_crtc->active = false;
842e0307
ML
6902 crtc->enabled = false;
6903 crtc->state->connector_mask = 0;
6904 crtc->state->encoder_mask = 0;
6905
6906 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6907 encoder->base.crtc = NULL;
6908
58f9c0bc 6909 intel_fbc_disable(intel_crtc);
37d9078b 6910 intel_update_watermarks(crtc);
1f7457b1 6911 intel_disable_shared_dpll(intel_crtc);
b17d48e2
ML
6912
6913 domains = intel_crtc->enabled_power_domains;
6914 for_each_power_domain(domain, domains)
6915 intel_display_power_put(dev_priv, domain);
6916 intel_crtc->enabled_power_domains = 0;
565602d7
ML
6917
6918 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
6919 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
b17d48e2
ML
6920}
6921
6b72d486
ML
6922/*
6923 * turn all crtc's off, but do not adjust state
6924 * This has to be paired with a call to intel_modeset_setup_hw_state.
6925 */
70e0bd74 6926int intel_display_suspend(struct drm_device *dev)
ee7b9f93 6927{
e2c8b870 6928 struct drm_i915_private *dev_priv = to_i915(dev);
70e0bd74 6929 struct drm_atomic_state *state;
e2c8b870 6930 int ret;
70e0bd74 6931
e2c8b870
ML
6932 state = drm_atomic_helper_suspend(dev);
6933 ret = PTR_ERR_OR_ZERO(state);
70e0bd74
ML
6934 if (ret)
6935 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
e2c8b870
ML
6936 else
6937 dev_priv->modeset_restore_state = state;
70e0bd74 6938 return ret;
ee7b9f93
JB
6939}
6940
ea5b213a 6941void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 6942{
4ef69c7a 6943 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 6944
ea5b213a
CW
6945 drm_encoder_cleanup(encoder);
6946 kfree(intel_encoder);
7e7d76c3
JB
6947}
6948
0a91ca29
SV
6949/* Cross check the actual hw state with our own modeset state tracking (and it's
6950 * internal consistency). */
5a21b665 6951static void intel_connector_verify_state(struct intel_connector *connector)
79e53945 6952{
5a21b665 6953 struct drm_crtc *crtc = connector->base.state->crtc;
35dd3c64
ML
6954
6955 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6956 connector->base.base.id,
6957 connector->base.name);
6958
0a91ca29 6959 if (connector->get_hw_state(connector)) {
e85376cb 6960 struct intel_encoder *encoder = connector->encoder;
5a21b665 6961 struct drm_connector_state *conn_state = connector->base.state;
0a91ca29 6962
35dd3c64
ML
6963 I915_STATE_WARN(!crtc,
6964 "connector enabled without attached crtc\n");
0a91ca29 6965
35dd3c64
ML
6966 if (!crtc)
6967 return;
6968
6969 I915_STATE_WARN(!crtc->state->active,
6970 "connector is active, but attached crtc isn't\n");
6971
e85376cb 6972 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
35dd3c64
ML
6973 return;
6974
e85376cb 6975 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
35dd3c64
ML
6976 "atomic encoder doesn't match attached encoder\n");
6977
e85376cb 6978 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
35dd3c64
ML
6979 "attached encoder crtc differs from connector crtc\n");
6980 } else {
4d688a2a
ML
6981 I915_STATE_WARN(crtc && crtc->state->active,
6982 "attached crtc is active, but connector isn't\n");
5a21b665 6983 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
35dd3c64 6984 "best encoder set without crtc!\n");
0a91ca29 6985 }
79e53945
JB
6986}
6987
08d9bc92
ACO
6988int intel_connector_init(struct intel_connector *connector)
6989{
5350a031 6990 drm_atomic_helper_connector_reset(&connector->base);
08d9bc92 6991
5350a031 6992 if (!connector->base.state)
08d9bc92
ACO
6993 return -ENOMEM;
6994
08d9bc92
ACO
6995 return 0;
6996}
6997
6998struct intel_connector *intel_connector_alloc(void)
6999{
7000 struct intel_connector *connector;
7001
7002 connector = kzalloc(sizeof *connector, GFP_KERNEL);
7003 if (!connector)
7004 return NULL;
7005
7006 if (intel_connector_init(connector) < 0) {
7007 kfree(connector);
7008 return NULL;
7009 }
7010
7011 return connector;
7012}
7013
f0947c37
SV
7014/* Simple connector->get_hw_state implementation for encoders that support only
7015 * one connector and no cloning and hence the encoder state determines the state
7016 * of the connector. */
7017bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 7018{
24929352 7019 enum pipe pipe = 0;
f0947c37 7020 struct intel_encoder *encoder = connector->encoder;
ea5b213a 7021
f0947c37 7022 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
7023}
7024
6d293983 7025static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
d272ddfa 7026{
6d293983
ACO
7027 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
7028 return crtc_state->fdi_lanes;
d272ddfa
VS
7029
7030 return 0;
7031}
7032
6d293983 7033static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5cec258b 7034 struct intel_crtc_state *pipe_config)
1857e1da 7035{
8652744b 7036 struct drm_i915_private *dev_priv = to_i915(dev);
6d293983
ACO
7037 struct drm_atomic_state *state = pipe_config->base.state;
7038 struct intel_crtc *other_crtc;
7039 struct intel_crtc_state *other_crtc_state;
7040
1857e1da
SV
7041 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
7042 pipe_name(pipe), pipe_config->fdi_lanes);
7043 if (pipe_config->fdi_lanes > 4) {
7044 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
7045 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 7046 return -EINVAL;
1857e1da
SV
7047 }
7048
8652744b 7049 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
1857e1da
SV
7050 if (pipe_config->fdi_lanes > 2) {
7051 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
7052 pipe_config->fdi_lanes);
6d293983 7053 return -EINVAL;
1857e1da 7054 } else {
6d293983 7055 return 0;
1857e1da
SV
7056 }
7057 }
7058
7059 if (INTEL_INFO(dev)->num_pipes == 2)
6d293983 7060 return 0;
1857e1da
SV
7061
7062 /* Ivybridge 3 pipe is really complicated */
7063 switch (pipe) {
7064 case PIPE_A:
6d293983 7065 return 0;
1857e1da 7066 case PIPE_B:
6d293983
ACO
7067 if (pipe_config->fdi_lanes <= 2)
7068 return 0;
7069
7070 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
7071 other_crtc_state =
7072 intel_atomic_get_crtc_state(state, other_crtc);
7073 if (IS_ERR(other_crtc_state))
7074 return PTR_ERR(other_crtc_state);
7075
7076 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
1857e1da
SV
7077 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
7078 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 7079 return -EINVAL;
1857e1da 7080 }
6d293983 7081 return 0;
1857e1da 7082 case PIPE_C:
251cc67c
VS
7083 if (pipe_config->fdi_lanes > 2) {
7084 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
7085 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 7086 return -EINVAL;
251cc67c 7087 }
6d293983
ACO
7088
7089 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
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) > 2) {
1857e1da 7096 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
6d293983 7097 return -EINVAL;
1857e1da 7098 }
6d293983 7099 return 0;
1857e1da
SV
7100 default:
7101 BUG();
7102 }
7103}
7104
e29c22c0
SV
7105#define RETRY 1
7106static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5cec258b 7107 struct intel_crtc_state *pipe_config)
877d48d5 7108{
1857e1da 7109 struct drm_device *dev = intel_crtc->base.dev;
7c5f93b0 7110 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6d293983
ACO
7111 int lane, link_bw, fdi_dotclock, ret;
7112 bool needs_recompute = false;
877d48d5 7113
e29c22c0 7114retry:
877d48d5
SV
7115 /* FDI is a binary signal running at ~2.7GHz, encoding
7116 * each output octet as 10 bits. The actual frequency
7117 * is stored as a divider into a 100MHz clock, and the
7118 * mode pixel clock is stored in units of 1KHz.
7119 * Hence the bw of each lane in terms of the mode signal
7120 * is:
7121 */
21a727b3 7122 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
877d48d5 7123
241bfc38 7124 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 7125
2bd89a07 7126 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
SV
7127 pipe_config->pipe_bpp);
7128
7129 pipe_config->fdi_lanes = lane;
7130
2bd89a07 7131 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
877d48d5 7132 link_bw, &pipe_config->fdi_m_n);
1857e1da 7133
e3b247da 7134 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
6d293983 7135 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
e29c22c0
SV
7136 pipe_config->pipe_bpp -= 2*3;
7137 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
7138 pipe_config->pipe_bpp);
7139 needs_recompute = true;
7140 pipe_config->bw_constrained = true;
7141
7142 goto retry;
7143 }
7144
7145 if (needs_recompute)
7146 return RETRY;
7147
6d293983 7148 return ret;
877d48d5
SV
7149}
7150
8cfb3407
VS
7151static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
7152 struct intel_crtc_state *pipe_config)
7153{
7154 if (pipe_config->pipe_bpp > 24)
7155 return false;
7156
7157 /* HSW can handle pixel rate up to cdclk? */
2d1fe073 7158 if (IS_HASWELL(dev_priv))
8cfb3407
VS
7159 return true;
7160
7161 /*
b432e5cf
VS
7162 * We compare against max which means we must take
7163 * the increased cdclk requirement into account when
7164 * calculating the new cdclk.
7165 *
7166 * Should measure whether using a lower cdclk w/o IPS
8cfb3407
VS
7167 */
7168 return ilk_pipe_pixel_rate(pipe_config) <=
7169 dev_priv->max_cdclk_freq * 95 / 100;
7170}
7171
42db64ef 7172static void hsw_compute_ips_config(struct intel_crtc *crtc,
5cec258b 7173 struct intel_crtc_state *pipe_config)
42db64ef 7174{
8cfb3407 7175 struct drm_device *dev = crtc->base.dev;
fac5e23e 7176 struct drm_i915_private *dev_priv = to_i915(dev);
8cfb3407 7177
d330a953 7178 pipe_config->ips_enabled = i915.enable_ips &&
8cfb3407
VS
7179 hsw_crtc_supports_ips(crtc) &&
7180 pipe_config_supports_ips(dev_priv, pipe_config);
42db64ef
PZ
7181}
7182
39acb4aa
VS
7183static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
7184{
7185 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7186
7187 /* GDG double wide on either pipe, otherwise pipe A only */
7188 return INTEL_INFO(dev_priv)->gen < 4 &&
7189 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
7190}
7191
a43f6e0f 7192static int intel_crtc_compute_config(struct intel_crtc *crtc,
5cec258b 7193 struct intel_crtc_state *pipe_config)
79e53945 7194{
a43f6e0f 7195 struct drm_device *dev = crtc->base.dev;
fac5e23e 7196 struct drm_i915_private *dev_priv = to_i915(dev);
7c5f93b0 7197 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
f3261156 7198 int clock_limit = dev_priv->max_dotclk_freq;
89749350 7199
cf532bb2 7200 if (INTEL_INFO(dev)->gen < 4) {
f3261156 7201 clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
cf532bb2
VS
7202
7203 /*
39acb4aa 7204 * Enable double wide mode when the dot clock
cf532bb2 7205 * is > 90% of the (display) core speed.
cf532bb2 7206 */
39acb4aa
VS
7207 if (intel_crtc_supports_double_wide(crtc) &&
7208 adjusted_mode->crtc_clock > clock_limit) {
f3261156 7209 clock_limit = dev_priv->max_dotclk_freq;
cf532bb2 7210 pipe_config->double_wide = true;
ad3a4479 7211 }
f3261156 7212 }
ad3a4479 7213
f3261156
VS
7214 if (adjusted_mode->crtc_clock > clock_limit) {
7215 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
7216 adjusted_mode->crtc_clock, clock_limit,
7217 yesno(pipe_config->double_wide));
7218 return -EINVAL;
2c07245f 7219 }
89749350 7220
1d1d0e27
VS
7221 /*
7222 * Pipe horizontal size must be even in:
7223 * - DVO ganged mode
7224 * - LVDS dual channel mode
7225 * - Double wide pipe
7226 */
2d84d2b3 7227 if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
1d1d0e27
VS
7228 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
7229 pipe_config->pipe_src_w &= ~1;
7230
8693a824
DL
7231 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
7232 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
44f46b42 7233 */
9beb5fea 7234 if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
aad941d5 7235 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
e29c22c0 7236 return -EINVAL;
44f46b42 7237
50a0bc90 7238 if (HAS_IPS(dev_priv))
a43f6e0f
SV
7239 hsw_compute_ips_config(crtc, pipe_config);
7240
877d48d5 7241 if (pipe_config->has_pch_encoder)
a43f6e0f 7242 return ironlake_fdi_compute_config(crtc, pipe_config);
877d48d5 7243
cf5a15be 7244 return 0;
79e53945
JB
7245}
7246
1652d19e
VS
7247static int skylake_get_display_clock_speed(struct drm_device *dev)
7248{
7249 struct drm_i915_private *dev_priv = to_i915(dev);
ea61791e 7250 uint32_t cdctl;
1652d19e 7251
ea61791e 7252 skl_dpll0_update(dev_priv);
1652d19e 7253
63911d72 7254 if (dev_priv->cdclk_pll.vco == 0)
709e05c3 7255 return dev_priv->cdclk_pll.ref;
1652d19e 7256
ea61791e 7257 cdctl = I915_READ(CDCLK_CTL);
1652d19e 7258
63911d72 7259 if (dev_priv->cdclk_pll.vco == 8640000) {
1652d19e
VS
7260 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
7261 case CDCLK_FREQ_450_432:
7262 return 432000;
7263 case CDCLK_FREQ_337_308:
487ed2e4 7264 return 308571;
ea61791e
VS
7265 case CDCLK_FREQ_540:
7266 return 540000;
1652d19e 7267 case CDCLK_FREQ_675_617:
487ed2e4 7268 return 617143;
1652d19e 7269 default:
ea61791e 7270 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
1652d19e
VS
7271 }
7272 } else {
1652d19e
VS
7273 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
7274 case CDCLK_FREQ_450_432:
7275 return 450000;
7276 case CDCLK_FREQ_337_308:
7277 return 337500;
ea61791e
VS
7278 case CDCLK_FREQ_540:
7279 return 540000;
1652d19e
VS
7280 case CDCLK_FREQ_675_617:
7281 return 675000;
7282 default:
ea61791e 7283 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
1652d19e
VS
7284 }
7285 }
7286
709e05c3 7287 return dev_priv->cdclk_pll.ref;
1652d19e
VS
7288}
7289
83d7c81f
VS
7290static void bxt_de_pll_update(struct drm_i915_private *dev_priv)
7291{
7292 u32 val;
7293
7294 dev_priv->cdclk_pll.ref = 19200;
1c3f7700 7295 dev_priv->cdclk_pll.vco = 0;
83d7c81f
VS
7296
7297 val = I915_READ(BXT_DE_PLL_ENABLE);
1c3f7700 7298 if ((val & BXT_DE_PLL_PLL_ENABLE) == 0)
83d7c81f 7299 return;
83d7c81f 7300
1c3f7700
ID
7301 if (WARN_ON((val & BXT_DE_PLL_LOCK) == 0))
7302 return;
83d7c81f
VS
7303
7304 val = I915_READ(BXT_DE_PLL_CTL);
7305 dev_priv->cdclk_pll.vco = (val & BXT_DE_PLL_RATIO_MASK) *
7306 dev_priv->cdclk_pll.ref;
7307}
7308
acd3f3d3
BP
7309static int broxton_get_display_clock_speed(struct drm_device *dev)
7310{
7311 struct drm_i915_private *dev_priv = to_i915(dev);
f5986242
VS
7312 u32 divider;
7313 int div, vco;
acd3f3d3 7314
83d7c81f
VS
7315 bxt_de_pll_update(dev_priv);
7316
f5986242
VS
7317 vco = dev_priv->cdclk_pll.vco;
7318 if (vco == 0)
7319 return dev_priv->cdclk_pll.ref;
acd3f3d3 7320
f5986242 7321 divider = I915_READ(CDCLK_CTL) & BXT_CDCLK_CD2X_DIV_SEL_MASK;
acd3f3d3 7322
f5986242 7323 switch (divider) {
acd3f3d3 7324 case BXT_CDCLK_CD2X_DIV_SEL_1:
f5986242
VS
7325 div = 2;
7326 break;
acd3f3d3 7327 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
f5986242
VS
7328 div = 3;
7329 break;
acd3f3d3 7330 case BXT_CDCLK_CD2X_DIV_SEL_2:
f5986242
VS
7331 div = 4;
7332 break;
acd3f3d3 7333 case BXT_CDCLK_CD2X_DIV_SEL_4:
f5986242
VS
7334 div = 8;
7335 break;
7336 default:
7337 MISSING_CASE(divider);
7338 return dev_priv->cdclk_pll.ref;
acd3f3d3
BP
7339 }
7340
f5986242 7341 return DIV_ROUND_CLOSEST(vco, div);
acd3f3d3
BP
7342}
7343
1652d19e
VS
7344static int broadwell_get_display_clock_speed(struct drm_device *dev)
7345{
fac5e23e 7346 struct drm_i915_private *dev_priv = to_i915(dev);
1652d19e
VS
7347 uint32_t lcpll = I915_READ(LCPLL_CTL);
7348 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
7349
7350 if (lcpll & LCPLL_CD_SOURCE_FCLK)
7351 return 800000;
7352 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
7353 return 450000;
7354 else if (freq == LCPLL_CLK_FREQ_450)
7355 return 450000;
7356 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
7357 return 540000;
7358 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
7359 return 337500;
7360 else
7361 return 675000;
7362}
7363
7364static int haswell_get_display_clock_speed(struct drm_device *dev)
7365{
fac5e23e 7366 struct drm_i915_private *dev_priv = to_i915(dev);
1652d19e
VS
7367 uint32_t lcpll = I915_READ(LCPLL_CTL);
7368 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
7369
7370 if (lcpll & LCPLL_CD_SOURCE_FCLK)
7371 return 800000;
7372 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
7373 return 450000;
7374 else if (freq == LCPLL_CLK_FREQ_450)
7375 return 450000;
50a0bc90 7376 else if (IS_HSW_ULT(dev_priv))
1652d19e
VS
7377 return 337500;
7378 else
7379 return 540000;
79e53945
JB
7380}
7381
25eb05fc
JB
7382static int valleyview_get_display_clock_speed(struct drm_device *dev)
7383{
bfa7df01
VS
7384 return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
7385 CCK_DISPLAY_CLOCK_CONTROL);
25eb05fc
JB
7386}
7387
b37a6434
VS
7388static int ilk_get_display_clock_speed(struct drm_device *dev)
7389{
7390 return 450000;
7391}
7392
e70236a8
JB
7393static int i945_get_display_clock_speed(struct drm_device *dev)
7394{
7395 return 400000;
7396}
79e53945 7397
e70236a8 7398static int i915_get_display_clock_speed(struct drm_device *dev)
79e53945 7399{
e907f170 7400 return 333333;
e70236a8 7401}
79e53945 7402
e70236a8
JB
7403static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
7404{
7405 return 200000;
7406}
79e53945 7407
257a7ffc
SV
7408static int pnv_get_display_clock_speed(struct drm_device *dev)
7409{
52a05c30 7410 struct pci_dev *pdev = dev->pdev;
257a7ffc
SV
7411 u16 gcfgc = 0;
7412
52a05c30 7413 pci_read_config_word(pdev, GCFGC, &gcfgc);
257a7ffc
SV
7414
7415 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
7416 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
e907f170 7417 return 266667;
257a7ffc 7418 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
e907f170 7419 return 333333;
257a7ffc 7420 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
e907f170 7421 return 444444;
257a7ffc
SV
7422 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
7423 return 200000;
7424 default:
7425 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
7426 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
e907f170 7427 return 133333;
257a7ffc 7428 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
e907f170 7429 return 166667;
257a7ffc
SV
7430 }
7431}
7432
e70236a8
JB
7433static int i915gm_get_display_clock_speed(struct drm_device *dev)
7434{
52a05c30 7435 struct pci_dev *pdev = dev->pdev;
e70236a8 7436 u16 gcfgc = 0;
79e53945 7437
52a05c30 7438 pci_read_config_word(pdev, GCFGC, &gcfgc);
e70236a8
JB
7439
7440 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
e907f170 7441 return 133333;
e70236a8
JB
7442 else {
7443 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
7444 case GC_DISPLAY_CLOCK_333_MHZ:
e907f170 7445 return 333333;
e70236a8
JB
7446 default:
7447 case GC_DISPLAY_CLOCK_190_200_MHZ:
7448 return 190000;
79e53945 7449 }
e70236a8
JB
7450 }
7451}
7452
7453static int i865_get_display_clock_speed(struct drm_device *dev)
7454{
e907f170 7455 return 266667;
e70236a8
JB
7456}
7457
1b1d2716 7458static int i85x_get_display_clock_speed(struct drm_device *dev)
e70236a8 7459{
52a05c30 7460 struct pci_dev *pdev = dev->pdev;
e70236a8 7461 u16 hpllcc = 0;
1b1d2716 7462
65cd2b3f
VS
7463 /*
7464 * 852GM/852GMV only supports 133 MHz and the HPLLCC
7465 * encoding is different :(
7466 * FIXME is this the right way to detect 852GM/852GMV?
7467 */
52a05c30 7468 if (pdev->revision == 0x1)
65cd2b3f
VS
7469 return 133333;
7470
52a05c30 7471 pci_bus_read_config_word(pdev->bus,
1b1d2716
VS
7472 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
7473
e70236a8
JB
7474 /* Assume that the hardware is in the high speed state. This
7475 * should be the default.
7476 */
7477 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
7478 case GC_CLOCK_133_200:
1b1d2716 7479 case GC_CLOCK_133_200_2:
e70236a8
JB
7480 case GC_CLOCK_100_200:
7481 return 200000;
7482 case GC_CLOCK_166_250:
7483 return 250000;
7484 case GC_CLOCK_100_133:
e907f170 7485 return 133333;
1b1d2716
VS
7486 case GC_CLOCK_133_266:
7487 case GC_CLOCK_133_266_2:
7488 case GC_CLOCK_166_266:
7489 return 266667;
e70236a8 7490 }
79e53945 7491
e70236a8
JB
7492 /* Shouldn't happen */
7493 return 0;
7494}
79e53945 7495
e70236a8
JB
7496static int i830_get_display_clock_speed(struct drm_device *dev)
7497{
e907f170 7498 return 133333;
79e53945
JB
7499}
7500
34edce2f
VS
7501static unsigned int intel_hpll_vco(struct drm_device *dev)
7502{
fac5e23e 7503 struct drm_i915_private *dev_priv = to_i915(dev);
34edce2f
VS
7504 static const unsigned int blb_vco[8] = {
7505 [0] = 3200000,
7506 [1] = 4000000,
7507 [2] = 5333333,
7508 [3] = 4800000,
7509 [4] = 6400000,
7510 };
7511 static const unsigned int pnv_vco[8] = {
7512 [0] = 3200000,
7513 [1] = 4000000,
7514 [2] = 5333333,
7515 [3] = 4800000,
7516 [4] = 2666667,
7517 };
7518 static const unsigned int cl_vco[8] = {
7519 [0] = 3200000,
7520 [1] = 4000000,
7521 [2] = 5333333,
7522 [3] = 6400000,
7523 [4] = 3333333,
7524 [5] = 3566667,
7525 [6] = 4266667,
7526 };
7527 static const unsigned int elk_vco[8] = {
7528 [0] = 3200000,
7529 [1] = 4000000,
7530 [2] = 5333333,
7531 [3] = 4800000,
7532 };
7533 static const unsigned int ctg_vco[8] = {
7534 [0] = 3200000,
7535 [1] = 4000000,
7536 [2] = 5333333,
7537 [3] = 6400000,
7538 [4] = 2666667,
7539 [5] = 4266667,
7540 };
7541 const unsigned int *vco_table;
7542 unsigned int vco;
7543 uint8_t tmp = 0;
7544
7545 /* FIXME other chipsets? */
50a0bc90 7546 if (IS_GM45(dev_priv))
34edce2f 7547 vco_table = ctg_vco;
9beb5fea 7548 else if (IS_G4X(dev_priv))
34edce2f
VS
7549 vco_table = elk_vco;
7550 else if (IS_CRESTLINE(dev))
7551 vco_table = cl_vco;
7552 else if (IS_PINEVIEW(dev))
7553 vco_table = pnv_vco;
7554 else if (IS_G33(dev))
7555 vco_table = blb_vco;
7556 else
7557 return 0;
7558
7559 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
7560
7561 vco = vco_table[tmp & 0x7];
7562 if (vco == 0)
7563 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
7564 else
7565 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
7566
7567 return vco;
7568}
7569
7570static int gm45_get_display_clock_speed(struct drm_device *dev)
7571{
52a05c30 7572 struct pci_dev *pdev = dev->pdev;
34edce2f
VS
7573 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7574 uint16_t tmp = 0;
7575
52a05c30 7576 pci_read_config_word(pdev, GCFGC, &tmp);
34edce2f
VS
7577
7578 cdclk_sel = (tmp >> 12) & 0x1;
7579
7580 switch (vco) {
7581 case 2666667:
7582 case 4000000:
7583 case 5333333:
7584 return cdclk_sel ? 333333 : 222222;
7585 case 3200000:
7586 return cdclk_sel ? 320000 : 228571;
7587 default:
7588 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
7589 return 222222;
7590 }
7591}
7592
7593static int i965gm_get_display_clock_speed(struct drm_device *dev)
7594{
52a05c30 7595 struct pci_dev *pdev = dev->pdev;
34edce2f
VS
7596 static const uint8_t div_3200[] = { 16, 10, 8 };
7597 static const uint8_t div_4000[] = { 20, 12, 10 };
7598 static const uint8_t div_5333[] = { 24, 16, 14 };
7599 const uint8_t *div_table;
7600 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7601 uint16_t tmp = 0;
7602
52a05c30 7603 pci_read_config_word(pdev, GCFGC, &tmp);
34edce2f
VS
7604
7605 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
7606
7607 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7608 goto fail;
7609
7610 switch (vco) {
7611 case 3200000:
7612 div_table = div_3200;
7613 break;
7614 case 4000000:
7615 div_table = div_4000;
7616 break;
7617 case 5333333:
7618 div_table = div_5333;
7619 break;
7620 default:
7621 goto fail;
7622 }
7623
7624 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7625
caf4e252 7626fail:
34edce2f
VS
7627 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
7628 return 200000;
7629}
7630
7631static int g33_get_display_clock_speed(struct drm_device *dev)
7632{
52a05c30 7633 struct pci_dev *pdev = dev->pdev;
34edce2f
VS
7634 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
7635 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
7636 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
7637 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
7638 const uint8_t *div_table;
7639 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7640 uint16_t tmp = 0;
7641
52a05c30 7642 pci_read_config_word(pdev, GCFGC, &tmp);
34edce2f
VS
7643
7644 cdclk_sel = (tmp >> 4) & 0x7;
7645
7646 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7647 goto fail;
7648
7649 switch (vco) {
7650 case 3200000:
7651 div_table = div_3200;
7652 break;
7653 case 4000000:
7654 div_table = div_4000;
7655 break;
7656 case 4800000:
7657 div_table = div_4800;
7658 break;
7659 case 5333333:
7660 div_table = div_5333;
7661 break;
7662 default:
7663 goto fail;
7664 }
7665
7666 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7667
caf4e252 7668fail:
34edce2f
VS
7669 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7670 return 190476;
7671}
7672
2c07245f 7673static void
a65851af 7674intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 7675{
a65851af
VS
7676 while (*num > DATA_LINK_M_N_MASK ||
7677 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
7678 *num >>= 1;
7679 *den >>= 1;
7680 }
7681}
7682
a65851af
VS
7683static void compute_m_n(unsigned int m, unsigned int n,
7684 uint32_t *ret_m, uint32_t *ret_n)
7685{
7686 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7687 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7688 intel_reduce_m_n_ratio(ret_m, ret_n);
7689}
7690
e69d0bc1
SV
7691void
7692intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7693 int pixel_clock, int link_clock,
7694 struct intel_link_m_n *m_n)
2c07245f 7695{
e69d0bc1 7696 m_n->tu = 64;
a65851af
VS
7697
7698 compute_m_n(bits_per_pixel * pixel_clock,
7699 link_clock * nlanes * 8,
7700 &m_n->gmch_m, &m_n->gmch_n);
7701
7702 compute_m_n(pixel_clock, link_clock,
7703 &m_n->link_m, &m_n->link_n);
2c07245f
ZW
7704}
7705
a7615030
CW
7706static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7707{
d330a953
JN
7708 if (i915.panel_use_ssc >= 0)
7709 return i915.panel_use_ssc != 0;
41aa3448 7710 return dev_priv->vbt.lvds_use_ssc
435793df 7711 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
7712}
7713
7429e9d4 7714static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 7715{
7df00d7a 7716 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 7717}
f47709a9 7718
7429e9d4
SV
7719static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7720{
7721 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
7722}
7723
f47709a9 7724static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
190f68c5 7725 struct intel_crtc_state *crtc_state,
9e2c8475 7726 struct dpll *reduced_clock)
a7516a05 7727{
f47709a9 7728 struct drm_device *dev = crtc->base.dev;
a7516a05
JB
7729 u32 fp, fp2 = 0;
7730
7731 if (IS_PINEVIEW(dev)) {
190f68c5 7732 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7733 if (reduced_clock)
7429e9d4 7734 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 7735 } else {
190f68c5 7736 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7737 if (reduced_clock)
7429e9d4 7738 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
7739 }
7740
190f68c5 7741 crtc_state->dpll_hw_state.fp0 = fp;
a7516a05 7742
f47709a9 7743 crtc->lowfreq_avail = false;
2d84d2b3 7744 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ab585dea 7745 reduced_clock) {
190f68c5 7746 crtc_state->dpll_hw_state.fp1 = fp2;
f47709a9 7747 crtc->lowfreq_avail = true;
a7516a05 7748 } else {
190f68c5 7749 crtc_state->dpll_hw_state.fp1 = fp;
a7516a05
JB
7750 }
7751}
7752
5e69f97f
CML
7753static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7754 pipe)
89b667f8
JB
7755{
7756 u32 reg_val;
7757
7758 /*
7759 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7760 * and set it to a reasonable value instead.
7761 */
ab3c759a 7762 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
7763 reg_val &= 0xffffff00;
7764 reg_val |= 0x00000030;
ab3c759a 7765 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7766
ab3c759a 7767 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7768 reg_val &= 0x8cffffff;
7769 reg_val = 0x8c000000;
ab3c759a 7770 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 7771
ab3c759a 7772 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 7773 reg_val &= 0xffffff00;
ab3c759a 7774 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7775
ab3c759a 7776 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7777 reg_val &= 0x00ffffff;
7778 reg_val |= 0xb0000000;
ab3c759a 7779 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
7780}
7781
b551842d
SV
7782static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7783 struct intel_link_m_n *m_n)
7784{
7785 struct drm_device *dev = crtc->base.dev;
fac5e23e 7786 struct drm_i915_private *dev_priv = to_i915(dev);
b551842d
SV
7787 int pipe = crtc->pipe;
7788
e3b95f1e
SV
7789 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7790 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7791 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7792 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
SV
7793}
7794
7795static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
7796 struct intel_link_m_n *m_n,
7797 struct intel_link_m_n *m2_n2)
b551842d
SV
7798{
7799 struct drm_device *dev = crtc->base.dev;
fac5e23e 7800 struct drm_i915_private *dev_priv = to_i915(dev);
b551842d 7801 int pipe = crtc->pipe;
6e3c9717 7802 enum transcoder transcoder = crtc->config->cpu_transcoder;
b551842d
SV
7803
7804 if (INTEL_INFO(dev)->gen >= 5) {
7805 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7806 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7807 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7808 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
f769cd24
VK
7809 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7810 * for gen < 8) and if DRRS is supported (to make sure the
7811 * registers are not unnecessarily accessed).
7812 */
920a14b2
TU
7813 if (m2_n2 && (IS_CHERRYVIEW(dev_priv) ||
7814 INTEL_GEN(dev_priv) < 8) && crtc->config->has_drrs) {
f769cd24
VK
7815 I915_WRITE(PIPE_DATA_M2(transcoder),
7816 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7817 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7818 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7819 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7820 }
b551842d 7821 } else {
e3b95f1e
SV
7822 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7823 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7824 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7825 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
SV
7826 }
7827}
7828
fe3cd48d 7829void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
03afc4a2 7830{
fe3cd48d
R
7831 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7832
7833 if (m_n == M1_N1) {
7834 dp_m_n = &crtc->config->dp_m_n;
7835 dp_m2_n2 = &crtc->config->dp_m2_n2;
7836 } else if (m_n == M2_N2) {
7837
7838 /*
7839 * M2_N2 registers are not supported. Hence m2_n2 divider value
7840 * needs to be programmed into M1_N1.
7841 */
7842 dp_m_n = &crtc->config->dp_m2_n2;
7843 } else {
7844 DRM_ERROR("Unsupported divider value\n");
7845 return;
7846 }
7847
6e3c9717
ACO
7848 if (crtc->config->has_pch_encoder)
7849 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
03afc4a2 7850 else
fe3cd48d 7851 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
03afc4a2
SV
7852}
7853
251ac862
SV
7854static void vlv_compute_dpll(struct intel_crtc *crtc,
7855 struct intel_crtc_state *pipe_config)
bdd4b6a6 7856{
03ed5cbf 7857 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
cd2d34d9 7858 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
03ed5cbf
VS
7859 if (crtc->pipe != PIPE_A)
7860 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
bdd4b6a6 7861
cd2d34d9 7862 /* DPLL not used with DSI, but still need the rest set up */
d7edc4e5 7863 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
cd2d34d9
VS
7864 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
7865 DPLL_EXT_BUFFER_ENABLE_VLV;
7866
03ed5cbf
VS
7867 pipe_config->dpll_hw_state.dpll_md =
7868 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
7869}
bdd4b6a6 7870
03ed5cbf
VS
7871static void chv_compute_dpll(struct intel_crtc *crtc,
7872 struct intel_crtc_state *pipe_config)
7873{
7874 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
cd2d34d9 7875 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
03ed5cbf
VS
7876 if (crtc->pipe != PIPE_A)
7877 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7878
cd2d34d9 7879 /* DPLL not used with DSI, but still need the rest set up */
d7edc4e5 7880 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
cd2d34d9
VS
7881 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
7882
03ed5cbf
VS
7883 pipe_config->dpll_hw_state.dpll_md =
7884 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
bdd4b6a6
SV
7885}
7886
d288f65f 7887static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7888 const struct intel_crtc_state *pipe_config)
a0c4da24 7889{
f47709a9 7890 struct drm_device *dev = crtc->base.dev;
fac5e23e 7891 struct drm_i915_private *dev_priv = to_i915(dev);
cd2d34d9 7892 enum pipe pipe = crtc->pipe;
bdd4b6a6 7893 u32 mdiv;
a0c4da24 7894 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 7895 u32 coreclk, reg_val;
a0c4da24 7896
cd2d34d9
VS
7897 /* Enable Refclk */
7898 I915_WRITE(DPLL(pipe),
7899 pipe_config->dpll_hw_state.dpll &
7900 ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
7901
7902 /* No need to actually set up the DPLL with DSI */
7903 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7904 return;
7905
a580516d 7906 mutex_lock(&dev_priv->sb_lock);
09153000 7907
d288f65f
VS
7908 bestn = pipe_config->dpll.n;
7909 bestm1 = pipe_config->dpll.m1;
7910 bestm2 = pipe_config->dpll.m2;
7911 bestp1 = pipe_config->dpll.p1;
7912 bestp2 = pipe_config->dpll.p2;
a0c4da24 7913
89b667f8
JB
7914 /* See eDP HDMI DPIO driver vbios notes doc */
7915
7916 /* PLL B needs special handling */
bdd4b6a6 7917 if (pipe == PIPE_B)
5e69f97f 7918 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
7919
7920 /* Set up Tx target for periodic Rcomp update */
ab3c759a 7921 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
7922
7923 /* Disable target IRef on PLL */
ab3c759a 7924 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 7925 reg_val &= 0x00ffffff;
ab3c759a 7926 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
7927
7928 /* Disable fast lock */
ab3c759a 7929 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
7930
7931 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
7932 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7933 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7934 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 7935 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
7936
7937 /*
7938 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7939 * but we don't support that).
7940 * Note: don't use the DAC post divider as it seems unstable.
7941 */
7942 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 7943 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7944
a0c4da24 7945 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 7946 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7947
89b667f8 7948 /* Set HBR and RBR LPF coefficients */
d288f65f 7949 if (pipe_config->port_clock == 162000 ||
2d84d2b3
VS
7950 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
7951 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
ab3c759a 7952 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 7953 0x009f0003);
89b667f8 7954 else
ab3c759a 7955 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
7956 0x00d0000f);
7957
37a5650b 7958 if (intel_crtc_has_dp_encoder(pipe_config)) {
89b667f8 7959 /* Use SSC source */
bdd4b6a6 7960 if (pipe == PIPE_A)
ab3c759a 7961 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7962 0x0df40000);
7963 else
ab3c759a 7964 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7965 0x0df70000);
7966 } else { /* HDMI or VGA */
7967 /* Use bend source */
bdd4b6a6 7968 if (pipe == PIPE_A)
ab3c759a 7969 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7970 0x0df70000);
7971 else
ab3c759a 7972 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7973 0x0df40000);
7974 }
a0c4da24 7975
ab3c759a 7976 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8 7977 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
2210ce7f 7978 if (intel_crtc_has_dp_encoder(crtc->config))
89b667f8 7979 coreclk |= 0x01000000;
ab3c759a 7980 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 7981
ab3c759a 7982 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
a580516d 7983 mutex_unlock(&dev_priv->sb_lock);
a0c4da24
JB
7984}
7985
d288f65f 7986static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7987 const struct intel_crtc_state *pipe_config)
9d556c99
CML
7988{
7989 struct drm_device *dev = crtc->base.dev;
fac5e23e 7990 struct drm_i915_private *dev_priv = to_i915(dev);
cd2d34d9 7991 enum pipe pipe = crtc->pipe;
9d556c99 7992 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9cbe40c1 7993 u32 loopfilter, tribuf_calcntr;
9d556c99 7994 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
a945ce7e 7995 u32 dpio_val;
9cbe40c1 7996 int vco;
9d556c99 7997
cd2d34d9
VS
7998 /* Enable Refclk and SSC */
7999 I915_WRITE(DPLL(pipe),
8000 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
8001
8002 /* No need to actually set up the DPLL with DSI */
8003 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8004 return;
8005
d288f65f
VS
8006 bestn = pipe_config->dpll.n;
8007 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
8008 bestm1 = pipe_config->dpll.m1;
8009 bestm2 = pipe_config->dpll.m2 >> 22;
8010 bestp1 = pipe_config->dpll.p1;
8011 bestp2 = pipe_config->dpll.p2;
9cbe40c1 8012 vco = pipe_config->dpll.vco;
a945ce7e 8013 dpio_val = 0;
9cbe40c1 8014 loopfilter = 0;
9d556c99 8015
a580516d 8016 mutex_lock(&dev_priv->sb_lock);
9d556c99 8017
9d556c99
CML
8018 /* p1 and p2 divider */
8019 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
8020 5 << DPIO_CHV_S1_DIV_SHIFT |
8021 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
8022 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
8023 1 << DPIO_CHV_K_DIV_SHIFT);
8024
8025 /* Feedback post-divider - m2 */
8026 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
8027
8028 /* Feedback refclk divider - n and m1 */
8029 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
8030 DPIO_CHV_M1_DIV_BY_2 |
8031 1 << DPIO_CHV_N_DIV_SHIFT);
8032
8033 /* M2 fraction division */
25a25dfc 8034 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
9d556c99
CML
8035
8036 /* M2 fraction division enable */
a945ce7e
VP
8037 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
8038 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
8039 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
8040 if (bestm2_frac)
8041 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
8042 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
9d556c99 8043
de3a0fde
VP
8044 /* Program digital lock detect threshold */
8045 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
8046 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
8047 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
8048 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
8049 if (!bestm2_frac)
8050 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
8051 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
8052
9d556c99 8053 /* Loop filter */
9cbe40c1
VP
8054 if (vco == 5400000) {
8055 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
8056 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
8057 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
8058 tribuf_calcntr = 0x9;
8059 } else if (vco <= 6200000) {
8060 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
8061 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
8062 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
8063 tribuf_calcntr = 0x9;
8064 } else if (vco <= 6480000) {
8065 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
8066 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
8067 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
8068 tribuf_calcntr = 0x8;
8069 } else {
8070 /* Not supported. Apply the same limits as in the max case */
8071 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
8072 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
8073 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
8074 tribuf_calcntr = 0;
8075 }
9d556c99
CML
8076 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
8077
968040b2 8078 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
9cbe40c1
VP
8079 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
8080 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
8081 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
8082
9d556c99
CML
8083 /* AFC Recal */
8084 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
8085 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
8086 DPIO_AFC_RECAL);
8087
a580516d 8088 mutex_unlock(&dev_priv->sb_lock);
9d556c99
CML
8089}
8090
d288f65f
VS
8091/**
8092 * vlv_force_pll_on - forcibly enable just the PLL
8093 * @dev_priv: i915 private structure
8094 * @pipe: pipe PLL to enable
8095 * @dpll: PLL configuration
8096 *
8097 * Enable the PLL for @pipe using the supplied @dpll config. To be used
8098 * in cases where we need the PLL enabled even when @pipe is not going to
8099 * be enabled.
8100 */
3f36b937
TU
8101int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
8102 const struct dpll *dpll)
d288f65f
VS
8103{
8104 struct intel_crtc *crtc =
8105 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
3f36b937
TU
8106 struct intel_crtc_state *pipe_config;
8107
8108 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
8109 if (!pipe_config)
8110 return -ENOMEM;
8111
8112 pipe_config->base.crtc = &crtc->base;
8113 pipe_config->pixel_multiplier = 1;
8114 pipe_config->dpll = *dpll;
d288f65f 8115
920a14b2 8116 if (IS_CHERRYVIEW(to_i915(dev))) {
3f36b937
TU
8117 chv_compute_dpll(crtc, pipe_config);
8118 chv_prepare_pll(crtc, pipe_config);
8119 chv_enable_pll(crtc, pipe_config);
d288f65f 8120 } else {
3f36b937
TU
8121 vlv_compute_dpll(crtc, pipe_config);
8122 vlv_prepare_pll(crtc, pipe_config);
8123 vlv_enable_pll(crtc, pipe_config);
d288f65f 8124 }
3f36b937
TU
8125
8126 kfree(pipe_config);
8127
8128 return 0;
d288f65f
VS
8129}
8130
8131/**
8132 * vlv_force_pll_off - forcibly disable just the PLL
8133 * @dev_priv: i915 private structure
8134 * @pipe: pipe PLL to disable
8135 *
8136 * Disable the PLL for @pipe. To be used in cases where we need
8137 * the PLL enabled even when @pipe is not going to be enabled.
8138 */
8139void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
8140{
920a14b2 8141 if (IS_CHERRYVIEW(to_i915(dev)))
d288f65f
VS
8142 chv_disable_pll(to_i915(dev), pipe);
8143 else
8144 vlv_disable_pll(to_i915(dev), pipe);
8145}
8146
251ac862
SV
8147static void i9xx_compute_dpll(struct intel_crtc *crtc,
8148 struct intel_crtc_state *crtc_state,
9e2c8475 8149 struct dpll *reduced_clock)
eb1cbe48 8150{
f47709a9 8151 struct drm_device *dev = crtc->base.dev;
fac5e23e 8152 struct drm_i915_private *dev_priv = to_i915(dev);
eb1cbe48 8153 u32 dpll;
190f68c5 8154 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 8155
190f68c5 8156 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 8157
eb1cbe48
SV
8158 dpll = DPLL_VGA_MODE_DIS;
8159
2d84d2b3 8160 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
eb1cbe48
SV
8161 dpll |= DPLLB_MODE_LVDS;
8162 else
8163 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 8164
50a0bc90 8165 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) || IS_G33(dev_priv)) {
190f68c5 8166 dpll |= (crtc_state->pixel_multiplier - 1)
198a037f 8167 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 8168 }
198a037f 8169
3d6e9ee0
VS
8170 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
8171 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
4a33e48d 8172 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 8173
37a5650b 8174 if (intel_crtc_has_dp_encoder(crtc_state))
4a33e48d 8175 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
SV
8176
8177 /* compute bitmask from p1 value */
8178 if (IS_PINEVIEW(dev))
8179 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
8180 else {
8181 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
9beb5fea 8182 if (IS_G4X(dev_priv) && reduced_clock)
eb1cbe48
SV
8183 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
8184 }
8185 switch (clock->p2) {
8186 case 5:
8187 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8188 break;
8189 case 7:
8190 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8191 break;
8192 case 10:
8193 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8194 break;
8195 case 14:
8196 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8197 break;
8198 }
8199 if (INTEL_INFO(dev)->gen >= 4)
8200 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
8201
190f68c5 8202 if (crtc_state->sdvo_tv_clock)
eb1cbe48 8203 dpll |= PLL_REF_INPUT_TVCLKINBC;
2d84d2b3 8204 else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 8205 intel_panel_use_ssc(dev_priv))
eb1cbe48
SV
8206 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
8207 else
8208 dpll |= PLL_REF_INPUT_DREFCLK;
8209
8210 dpll |= DPLL_VCO_ENABLE;
190f68c5 8211 crtc_state->dpll_hw_state.dpll = dpll;
8bcc2795 8212
eb1cbe48 8213 if (INTEL_INFO(dev)->gen >= 4) {
190f68c5 8214 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
ef1b460d 8215 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
190f68c5 8216 crtc_state->dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
SV
8217 }
8218}
8219
251ac862
SV
8220static void i8xx_compute_dpll(struct intel_crtc *crtc,
8221 struct intel_crtc_state *crtc_state,
9e2c8475 8222 struct dpll *reduced_clock)
eb1cbe48 8223{
f47709a9 8224 struct drm_device *dev = crtc->base.dev;
fac5e23e 8225 struct drm_i915_private *dev_priv = to_i915(dev);
eb1cbe48 8226 u32 dpll;
190f68c5 8227 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 8228
190f68c5 8229 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 8230
eb1cbe48
SV
8231 dpll = DPLL_VGA_MODE_DIS;
8232
2d84d2b3 8233 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
eb1cbe48
SV
8234 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
8235 } else {
8236 if (clock->p1 == 2)
8237 dpll |= PLL_P1_DIVIDE_BY_TWO;
8238 else
8239 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
8240 if (clock->p2 == 4)
8241 dpll |= PLL_P2_DIVIDE_BY_4;
8242 }
8243
50a0bc90
TU
8244 if (!IS_I830(dev_priv) &&
8245 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
4a33e48d
SV
8246 dpll |= DPLL_DVO_2X_MODE;
8247
2d84d2b3 8248 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 8249 intel_panel_use_ssc(dev_priv))
eb1cbe48
SV
8250 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
8251 else
8252 dpll |= PLL_REF_INPUT_DREFCLK;
8253
8254 dpll |= DPLL_VCO_ENABLE;
190f68c5 8255 crtc_state->dpll_hw_state.dpll = dpll;
eb1cbe48
SV
8256}
8257
8a654f3b 8258static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c
PZ
8259{
8260 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 8261 struct drm_i915_private *dev_priv = to_i915(dev);
b0e77b9c 8262 enum pipe pipe = intel_crtc->pipe;
6e3c9717 8263 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
7c5f93b0 8264 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
1caea6e9
VS
8265 uint32_t crtc_vtotal, crtc_vblank_end;
8266 int vsyncshift = 0;
4d8a62ea
SV
8267
8268 /* We need to be careful not to changed the adjusted mode, for otherwise
8269 * the hw state checker will get angry at the mismatch. */
8270 crtc_vtotal = adjusted_mode->crtc_vtotal;
8271 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 8272
609aeaca 8273 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 8274 /* the chip adds 2 halflines automatically */
4d8a62ea
SV
8275 crtc_vtotal -= 1;
8276 crtc_vblank_end -= 1;
609aeaca 8277
2d84d2b3 8278 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
609aeaca
VS
8279 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
8280 else
8281 vsyncshift = adjusted_mode->crtc_hsync_start -
8282 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
8283 if (vsyncshift < 0)
8284 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
8285 }
8286
8287 if (INTEL_INFO(dev)->gen > 3)
fe2b8f9d 8288 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 8289
fe2b8f9d 8290 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
8291 (adjusted_mode->crtc_hdisplay - 1) |
8292 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 8293 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
8294 (adjusted_mode->crtc_hblank_start - 1) |
8295 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 8296 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
8297 (adjusted_mode->crtc_hsync_start - 1) |
8298 ((adjusted_mode->crtc_hsync_end - 1) << 16));
8299
fe2b8f9d 8300 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 8301 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 8302 ((crtc_vtotal - 1) << 16));
fe2b8f9d 8303 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 8304 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 8305 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 8306 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
8307 (adjusted_mode->crtc_vsync_start - 1) |
8308 ((adjusted_mode->crtc_vsync_end - 1) << 16));
8309
b5e508d4
PZ
8310 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
8311 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
8312 * documented on the DDI_FUNC_CTL register description, EDP Input Select
8313 * bits. */
772c2a51 8314 if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
b5e508d4
PZ
8315 (pipe == PIPE_B || pipe == PIPE_C))
8316 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
8317
bc58be60
JN
8318}
8319
8320static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
8321{
8322 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 8323 struct drm_i915_private *dev_priv = to_i915(dev);
bc58be60
JN
8324 enum pipe pipe = intel_crtc->pipe;
8325
b0e77b9c
PZ
8326 /* pipesrc controls the size that is scaled from, which should
8327 * always be the user's requested size.
8328 */
8329 I915_WRITE(PIPESRC(pipe),
6e3c9717
ACO
8330 ((intel_crtc->config->pipe_src_w - 1) << 16) |
8331 (intel_crtc->config->pipe_src_h - 1));
b0e77b9c
PZ
8332}
8333
1bd1bd80 8334static void intel_get_pipe_timings(struct intel_crtc *crtc,
5cec258b 8335 struct intel_crtc_state *pipe_config)
1bd1bd80
SV
8336{
8337 struct drm_device *dev = crtc->base.dev;
fac5e23e 8338 struct drm_i915_private *dev_priv = to_i915(dev);
1bd1bd80
SV
8339 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
8340 uint32_t tmp;
8341
8342 tmp = I915_READ(HTOTAL(cpu_transcoder));
2d112de7
ACO
8343 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
8344 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 8345 tmp = I915_READ(HBLANK(cpu_transcoder));
2d112de7
ACO
8346 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
8347 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 8348 tmp = I915_READ(HSYNC(cpu_transcoder));
2d112de7
ACO
8349 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
8350 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
SV
8351
8352 tmp = I915_READ(VTOTAL(cpu_transcoder));
2d112de7
ACO
8353 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
8354 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 8355 tmp = I915_READ(VBLANK(cpu_transcoder));
2d112de7
ACO
8356 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
8357 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 8358 tmp = I915_READ(VSYNC(cpu_transcoder));
2d112de7
ACO
8359 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
8360 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
SV
8361
8362 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
2d112de7
ACO
8363 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
8364 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
8365 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
1bd1bd80 8366 }
bc58be60
JN
8367}
8368
8369static void intel_get_pipe_src_size(struct intel_crtc *crtc,
8370 struct intel_crtc_state *pipe_config)
8371{
8372 struct drm_device *dev = crtc->base.dev;
fac5e23e 8373 struct drm_i915_private *dev_priv = to_i915(dev);
bc58be60 8374 u32 tmp;
1bd1bd80
SV
8375
8376 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
8377 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
8378 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
8379
2d112de7
ACO
8380 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
8381 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
SV
8382}
8383
f6a83288 8384void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5cec258b 8385 struct intel_crtc_state *pipe_config)
babea61d 8386{
2d112de7
ACO
8387 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
8388 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
8389 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
8390 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
babea61d 8391
2d112de7
ACO
8392 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
8393 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
8394 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
8395 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
babea61d 8396
2d112de7 8397 mode->flags = pipe_config->base.adjusted_mode.flags;
cd13f5ab 8398 mode->type = DRM_MODE_TYPE_DRIVER;
babea61d 8399
2d112de7
ACO
8400 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
8401 mode->flags |= pipe_config->base.adjusted_mode.flags;
cd13f5ab
ML
8402
8403 mode->hsync = drm_mode_hsync(mode);
8404 mode->vrefresh = drm_mode_vrefresh(mode);
8405 drm_mode_set_name(mode);
babea61d
JB
8406}
8407
84b046f3
SV
8408static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
8409{
8410 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 8411 struct drm_i915_private *dev_priv = to_i915(dev);
84b046f3
SV
8412 uint32_t pipeconf;
8413
9f11a9e4 8414 pipeconf = 0;
84b046f3 8415
b6b5d049
VS
8416 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
8417 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
8418 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
67c72a12 8419
6e3c9717 8420 if (intel_crtc->config->double_wide)
cf532bb2 8421 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 8422
ff9ce46e 8423 /* only g4x and later have fancy bpc/dither controls */
9beb5fea
TU
8424 if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
8425 IS_CHERRYVIEW(dev_priv)) {
ff9ce46e 8426 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6e3c9717 8427 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
ff9ce46e 8428 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 8429 PIPECONF_DITHER_TYPE_SP;
84b046f3 8430
6e3c9717 8431 switch (intel_crtc->config->pipe_bpp) {
ff9ce46e
SV
8432 case 18:
8433 pipeconf |= PIPECONF_6BPC;
8434 break;
8435 case 24:
8436 pipeconf |= PIPECONF_8BPC;
8437 break;
8438 case 30:
8439 pipeconf |= PIPECONF_10BPC;
8440 break;
8441 default:
8442 /* Case prevented by intel_choose_pipe_bpp_dither. */
8443 BUG();
84b046f3
SV
8444 }
8445 }
8446
8447 if (HAS_PIPE_CXSR(dev)) {
8448 if (intel_crtc->lowfreq_avail) {
8449 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
8450 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
8451 } else {
8452 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
84b046f3
SV
8453 }
8454 }
8455
6e3c9717 8456 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
efc2cfff 8457 if (INTEL_INFO(dev)->gen < 4 ||
2d84d2b3 8458 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
efc2cfff
VS
8459 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
8460 else
8461 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
8462 } else
84b046f3
SV
8463 pipeconf |= PIPECONF_PROGRESSIVE;
8464
920a14b2 8465 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
666a4537 8466 intel_crtc->config->limited_color_range)
9f11a9e4 8467 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 8468
84b046f3
SV
8469 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
8470 POSTING_READ(PIPECONF(intel_crtc->pipe));
8471}
8472
81c97f52
ACO
8473static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
8474 struct intel_crtc_state *crtc_state)
8475{
8476 struct drm_device *dev = crtc->base.dev;
fac5e23e 8477 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 8478 const struct intel_limit *limit;
81c97f52
ACO
8479 int refclk = 48000;
8480
8481 memset(&crtc_state->dpll_hw_state, 0,
8482 sizeof(crtc_state->dpll_hw_state));
8483
2d84d2b3 8484 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
81c97f52
ACO
8485 if (intel_panel_use_ssc(dev_priv)) {
8486 refclk = dev_priv->vbt.lvds_ssc_freq;
8487 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8488 }
8489
8490 limit = &intel_limits_i8xx_lvds;
2d84d2b3 8491 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
81c97f52
ACO
8492 limit = &intel_limits_i8xx_dvo;
8493 } else {
8494 limit = &intel_limits_i8xx_dac;
8495 }
8496
8497 if (!crtc_state->clock_set &&
8498 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8499 refclk, NULL, &crtc_state->dpll)) {
8500 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8501 return -EINVAL;
8502 }
8503
8504 i8xx_compute_dpll(crtc, crtc_state, NULL);
8505
8506 return 0;
8507}
8508
19ec6693
ACO
8509static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
8510 struct intel_crtc_state *crtc_state)
8511{
8512 struct drm_device *dev = crtc->base.dev;
fac5e23e 8513 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 8514 const struct intel_limit *limit;
19ec6693
ACO
8515 int refclk = 96000;
8516
8517 memset(&crtc_state->dpll_hw_state, 0,
8518 sizeof(crtc_state->dpll_hw_state));
8519
2d84d2b3 8520 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
19ec6693
ACO
8521 if (intel_panel_use_ssc(dev_priv)) {
8522 refclk = dev_priv->vbt.lvds_ssc_freq;
8523 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8524 }
8525
8526 if (intel_is_dual_link_lvds(dev))
8527 limit = &intel_limits_g4x_dual_channel_lvds;
8528 else
8529 limit = &intel_limits_g4x_single_channel_lvds;
2d84d2b3
VS
8530 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
8531 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
19ec6693 8532 limit = &intel_limits_g4x_hdmi;
2d84d2b3 8533 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
19ec6693
ACO
8534 limit = &intel_limits_g4x_sdvo;
8535 } else {
8536 /* The option is for other outputs */
8537 limit = &intel_limits_i9xx_sdvo;
8538 }
8539
8540 if (!crtc_state->clock_set &&
8541 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8542 refclk, NULL, &crtc_state->dpll)) {
8543 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8544 return -EINVAL;
8545 }
8546
8547 i9xx_compute_dpll(crtc, crtc_state, NULL);
8548
8549 return 0;
8550}
8551
70e8aa21
ACO
8552static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
8553 struct intel_crtc_state *crtc_state)
8554{
8555 struct drm_device *dev = crtc->base.dev;
fac5e23e 8556 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 8557 const struct intel_limit *limit;
70e8aa21
ACO
8558 int refclk = 96000;
8559
8560 memset(&crtc_state->dpll_hw_state, 0,
8561 sizeof(crtc_state->dpll_hw_state));
8562
2d84d2b3 8563 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
70e8aa21
ACO
8564 if (intel_panel_use_ssc(dev_priv)) {
8565 refclk = dev_priv->vbt.lvds_ssc_freq;
8566 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8567 }
8568
8569 limit = &intel_limits_pineview_lvds;
8570 } else {
8571 limit = &intel_limits_pineview_sdvo;
8572 }
8573
8574 if (!crtc_state->clock_set &&
8575 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8576 refclk, NULL, &crtc_state->dpll)) {
8577 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8578 return -EINVAL;
8579 }
8580
8581 i9xx_compute_dpll(crtc, crtc_state, NULL);
8582
8583 return 0;
8584}
8585
190f68c5
ACO
8586static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
8587 struct intel_crtc_state *crtc_state)
79e53945 8588{
c7653199 8589 struct drm_device *dev = crtc->base.dev;
fac5e23e 8590 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 8591 const struct intel_limit *limit;
81c97f52 8592 int refclk = 96000;
79e53945 8593
dd3cd74a
ACO
8594 memset(&crtc_state->dpll_hw_state, 0,
8595 sizeof(crtc_state->dpll_hw_state));
8596
2d84d2b3 8597 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
70e8aa21
ACO
8598 if (intel_panel_use_ssc(dev_priv)) {
8599 refclk = dev_priv->vbt.lvds_ssc_freq;
8600 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8601 }
43565a06 8602
70e8aa21
ACO
8603 limit = &intel_limits_i9xx_lvds;
8604 } else {
8605 limit = &intel_limits_i9xx_sdvo;
81c97f52 8606 }
79e53945 8607
70e8aa21
ACO
8608 if (!crtc_state->clock_set &&
8609 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8610 refclk, NULL, &crtc_state->dpll)) {
8611 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8612 return -EINVAL;
f47709a9 8613 }
7026d4ac 8614
81c97f52 8615 i9xx_compute_dpll(crtc, crtc_state, NULL);
79e53945 8616
c8f7a0db 8617 return 0;
f564048e
EA
8618}
8619
65b3d6a9
ACO
8620static int chv_crtc_compute_clock(struct intel_crtc *crtc,
8621 struct intel_crtc_state *crtc_state)
8622{
8623 int refclk = 100000;
1b6f4958 8624 const struct intel_limit *limit = &intel_limits_chv;
65b3d6a9
ACO
8625
8626 memset(&crtc_state->dpll_hw_state, 0,
8627 sizeof(crtc_state->dpll_hw_state));
8628
65b3d6a9
ACO
8629 if (!crtc_state->clock_set &&
8630 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8631 refclk, NULL, &crtc_state->dpll)) {
8632 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8633 return -EINVAL;
8634 }
8635
8636 chv_compute_dpll(crtc, crtc_state);
8637
8638 return 0;
8639}
8640
8641static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
8642 struct intel_crtc_state *crtc_state)
8643{
8644 int refclk = 100000;
1b6f4958 8645 const struct intel_limit *limit = &intel_limits_vlv;
65b3d6a9
ACO
8646
8647 memset(&crtc_state->dpll_hw_state, 0,
8648 sizeof(crtc_state->dpll_hw_state));
8649
65b3d6a9
ACO
8650 if (!crtc_state->clock_set &&
8651 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8652 refclk, NULL, &crtc_state->dpll)) {
8653 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8654 return -EINVAL;
8655 }
8656
8657 vlv_compute_dpll(crtc, crtc_state);
8658
8659 return 0;
8660}
8661
2fa2fe9a 8662static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5cec258b 8663 struct intel_crtc_state *pipe_config)
2fa2fe9a
SV
8664{
8665 struct drm_device *dev = crtc->base.dev;
fac5e23e 8666 struct drm_i915_private *dev_priv = to_i915(dev);
2fa2fe9a
SV
8667 uint32_t tmp;
8668
50a0bc90
TU
8669 if (INTEL_GEN(dev_priv) <= 3 &&
8670 (IS_I830(dev_priv) || !IS_MOBILE(dev_priv)))
dc9e7dec
VS
8671 return;
8672
2fa2fe9a 8673 tmp = I915_READ(PFIT_CONTROL);
06922821
SV
8674 if (!(tmp & PFIT_ENABLE))
8675 return;
2fa2fe9a 8676
06922821 8677 /* Check whether the pfit is attached to our pipe. */
2fa2fe9a
SV
8678 if (INTEL_INFO(dev)->gen < 4) {
8679 if (crtc->pipe != PIPE_B)
8680 return;
2fa2fe9a
SV
8681 } else {
8682 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
8683 return;
8684 }
8685
06922821 8686 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a 8687 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
2fa2fe9a
SV
8688}
8689
acbec814 8690static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 8691 struct intel_crtc_state *pipe_config)
acbec814
JB
8692{
8693 struct drm_device *dev = crtc->base.dev;
fac5e23e 8694 struct drm_i915_private *dev_priv = to_i915(dev);
acbec814 8695 int pipe = pipe_config->cpu_transcoder;
9e2c8475 8696 struct dpll clock;
acbec814 8697 u32 mdiv;
662c6ecb 8698 int refclk = 100000;
acbec814 8699
b521973b
VS
8700 /* In case of DSI, DPLL will not be used */
8701 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
f573de5a
SK
8702 return;
8703
a580516d 8704 mutex_lock(&dev_priv->sb_lock);
ab3c759a 8705 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
a580516d 8706 mutex_unlock(&dev_priv->sb_lock);
acbec814
JB
8707
8708 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8709 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8710 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8711 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8712 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8713
dccbea3b 8714 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
acbec814
JB
8715}
8716
5724dbd1
DL
8717static void
8718i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8719 struct intel_initial_plane_config *plane_config)
1ad292b5
JB
8720{
8721 struct drm_device *dev = crtc->base.dev;
fac5e23e 8722 struct drm_i915_private *dev_priv = to_i915(dev);
1ad292b5
JB
8723 u32 val, base, offset;
8724 int pipe = crtc->pipe, plane = crtc->plane;
8725 int fourcc, pixel_format;
6761dd31 8726 unsigned int aligned_height;
b113d5ee 8727 struct drm_framebuffer *fb;
1b842c89 8728 struct intel_framebuffer *intel_fb;
1ad292b5 8729
42a7b088
DL
8730 val = I915_READ(DSPCNTR(plane));
8731 if (!(val & DISPLAY_PLANE_ENABLE))
8732 return;
8733
d9806c9f 8734 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 8735 if (!intel_fb) {
1ad292b5
JB
8736 DRM_DEBUG_KMS("failed to alloc fb\n");
8737 return;
8738 }
8739
1b842c89
DL
8740 fb = &intel_fb->base;
8741
18c5247e
SV
8742 if (INTEL_INFO(dev)->gen >= 4) {
8743 if (val & DISPPLANE_TILED) {
49af449b 8744 plane_config->tiling = I915_TILING_X;
18c5247e
SV
8745 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8746 }
8747 }
1ad292b5
JB
8748
8749 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 8750 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
8751 fb->pixel_format = fourcc;
8752 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
1ad292b5
JB
8753
8754 if (INTEL_INFO(dev)->gen >= 4) {
49af449b 8755 if (plane_config->tiling)
1ad292b5
JB
8756 offset = I915_READ(DSPTILEOFF(plane));
8757 else
8758 offset = I915_READ(DSPLINOFF(plane));
8759 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8760 } else {
8761 base = I915_READ(DSPADDR(plane));
8762 }
8763 plane_config->base = base;
8764
8765 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
8766 fb->width = ((val >> 16) & 0xfff) + 1;
8767 fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
8768
8769 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 8770 fb->pitches[0] = val & 0xffffffc0;
1ad292b5 8771
b113d5ee 8772 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
SV
8773 fb->pixel_format,
8774 fb->modifier[0]);
1ad292b5 8775
f37b5c2b 8776 plane_config->size = fb->pitches[0] * aligned_height;
1ad292b5 8777
2844a921
DL
8778 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8779 pipe_name(pipe), plane, fb->width, fb->height,
8780 fb->bits_per_pixel, base, fb->pitches[0],
8781 plane_config->size);
1ad292b5 8782
2d14030b 8783 plane_config->fb = intel_fb;
1ad292b5
JB
8784}
8785
70b23a98 8786static void chv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 8787 struct intel_crtc_state *pipe_config)
70b23a98
VS
8788{
8789 struct drm_device *dev = crtc->base.dev;
fac5e23e 8790 struct drm_i915_private *dev_priv = to_i915(dev);
70b23a98
VS
8791 int pipe = pipe_config->cpu_transcoder;
8792 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9e2c8475 8793 struct dpll clock;
0d7b6b11 8794 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
70b23a98
VS
8795 int refclk = 100000;
8796
b521973b
VS
8797 /* In case of DSI, DPLL will not be used */
8798 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8799 return;
8800
a580516d 8801 mutex_lock(&dev_priv->sb_lock);
70b23a98
VS
8802 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8803 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8804 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8805 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
0d7b6b11 8806 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
a580516d 8807 mutex_unlock(&dev_priv->sb_lock);
70b23a98
VS
8808
8809 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
0d7b6b11
ID
8810 clock.m2 = (pll_dw0 & 0xff) << 22;
8811 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8812 clock.m2 |= pll_dw2 & 0x3fffff;
70b23a98
VS
8813 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8814 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8815 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8816
dccbea3b 8817 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
70b23a98
VS
8818}
8819
0e8ffe1b 8820static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5cec258b 8821 struct intel_crtc_state *pipe_config)
0e8ffe1b
SV
8822{
8823 struct drm_device *dev = crtc->base.dev;
fac5e23e 8824 struct drm_i915_private *dev_priv = to_i915(dev);
1729050e 8825 enum intel_display_power_domain power_domain;
0e8ffe1b 8826 uint32_t tmp;
1729050e 8827 bool ret;
0e8ffe1b 8828
1729050e
ID
8829 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8830 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0
ID
8831 return false;
8832
e143a21c 8833 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 8834 pipe_config->shared_dpll = NULL;
eccb140b 8835
1729050e
ID
8836 ret = false;
8837
0e8ffe1b
SV
8838 tmp = I915_READ(PIPECONF(crtc->pipe));
8839 if (!(tmp & PIPECONF_ENABLE))
1729050e 8840 goto out;
0e8ffe1b 8841
9beb5fea
TU
8842 if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
8843 IS_CHERRYVIEW(dev_priv)) {
42571aef
VS
8844 switch (tmp & PIPECONF_BPC_MASK) {
8845 case PIPECONF_6BPC:
8846 pipe_config->pipe_bpp = 18;
8847 break;
8848 case PIPECONF_8BPC:
8849 pipe_config->pipe_bpp = 24;
8850 break;
8851 case PIPECONF_10BPC:
8852 pipe_config->pipe_bpp = 30;
8853 break;
8854 default:
8855 break;
8856 }
8857 }
8858
920a14b2 8859 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
666a4537 8860 (tmp & PIPECONF_COLOR_RANGE_SELECT))
b5a9fa09
SV
8861 pipe_config->limited_color_range = true;
8862
282740f7
VS
8863 if (INTEL_INFO(dev)->gen < 4)
8864 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8865
1bd1bd80 8866 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 8867 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 8868
2fa2fe9a
SV
8869 i9xx_get_pfit_config(crtc, pipe_config);
8870
6c49f241 8871 if (INTEL_INFO(dev)->gen >= 4) {
c231775c 8872 /* No way to read it out on pipes B and C */
920a14b2 8873 if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
c231775c
VS
8874 tmp = dev_priv->chv_dpll_md[crtc->pipe];
8875 else
8876 tmp = I915_READ(DPLL_MD(crtc->pipe));
6c49f241
SV
8877 pipe_config->pixel_multiplier =
8878 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8879 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 8880 pipe_config->dpll_hw_state.dpll_md = tmp;
50a0bc90
TU
8881 } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
8882 IS_G33(dev_priv)) {
6c49f241
SV
8883 tmp = I915_READ(DPLL(crtc->pipe));
8884 pipe_config->pixel_multiplier =
8885 ((tmp & SDVO_MULTIPLIER_MASK)
8886 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8887 } else {
8888 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8889 * port and will be fixed up in the encoder->get_config
8890 * function. */
8891 pipe_config->pixel_multiplier = 1;
8892 }
8bcc2795 8893 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
920a14b2 8894 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
1c4e0274
VS
8895 /*
8896 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8897 * on 830. Filter it out here so that we don't
8898 * report errors due to that.
8899 */
50a0bc90 8900 if (IS_I830(dev_priv))
1c4e0274
VS
8901 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8902
8bcc2795
SV
8903 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8904 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
8905 } else {
8906 /* Mask out read-only status bits. */
8907 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8908 DPLL_PORTC_READY_MASK |
8909 DPLL_PORTB_READY_MASK);
8bcc2795 8910 }
6c49f241 8911
920a14b2 8912 if (IS_CHERRYVIEW(dev_priv))
70b23a98 8913 chv_crtc_clock_get(crtc, pipe_config);
11a914c2 8914 else if (IS_VALLEYVIEW(dev_priv))
acbec814
JB
8915 vlv_crtc_clock_get(crtc, pipe_config);
8916 else
8917 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 8918
0f64614d
VS
8919 /*
8920 * Normally the dotclock is filled in by the encoder .get_config()
8921 * but in case the pipe is enabled w/o any ports we need a sane
8922 * default.
8923 */
8924 pipe_config->base.adjusted_mode.crtc_clock =
8925 pipe_config->port_clock / pipe_config->pixel_multiplier;
8926
1729050e
ID
8927 ret = true;
8928
8929out:
8930 intel_display_power_put(dev_priv, power_domain);
8931
8932 return ret;
0e8ffe1b
SV
8933}
8934
dde86e2d 8935static void ironlake_init_pch_refclk(struct drm_device *dev)
13d83a67 8936{
fac5e23e 8937 struct drm_i915_private *dev_priv = to_i915(dev);
13d83a67 8938 struct intel_encoder *encoder;
1c1a24d2 8939 int i;
74cfd7ac 8940 u32 val, final;
13d83a67 8941 bool has_lvds = false;
199e5d79 8942 bool has_cpu_edp = false;
199e5d79 8943 bool has_panel = false;
99eb6a01
KP
8944 bool has_ck505 = false;
8945 bool can_ssc = false;
1c1a24d2 8946 bool using_ssc_source = false;
13d83a67
JB
8947
8948 /* We need to take the global config into account */
b2784e15 8949 for_each_intel_encoder(dev, encoder) {
199e5d79
KP
8950 switch (encoder->type) {
8951 case INTEL_OUTPUT_LVDS:
8952 has_panel = true;
8953 has_lvds = true;
8954 break;
8955 case INTEL_OUTPUT_EDP:
8956 has_panel = true;
2de6905f 8957 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
8958 has_cpu_edp = true;
8959 break;
6847d71b
PZ
8960 default:
8961 break;
13d83a67
JB
8962 }
8963 }
8964
6e266956 8965 if (HAS_PCH_IBX(dev_priv)) {
41aa3448 8966 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
8967 can_ssc = has_ck505;
8968 } else {
8969 has_ck505 = false;
8970 can_ssc = true;
8971 }
8972
1c1a24d2
L
8973 /* Check if any DPLLs are using the SSC source */
8974 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
8975 u32 temp = I915_READ(PCH_DPLL(i));
8976
8977 if (!(temp & DPLL_VCO_ENABLE))
8978 continue;
8979
8980 if ((temp & PLL_REF_INPUT_MASK) ==
8981 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
8982 using_ssc_source = true;
8983 break;
8984 }
8985 }
8986
8987 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
8988 has_panel, has_lvds, has_ck505, using_ssc_source);
13d83a67
JB
8989
8990 /* Ironlake: try to setup display ref clock before DPLL
8991 * enabling. This is only under driver's control after
8992 * PCH B stepping, previous chipset stepping should be
8993 * ignoring this setting.
8994 */
74cfd7ac
CW
8995 val = I915_READ(PCH_DREF_CONTROL);
8996
8997 /* As we must carefully and slowly disable/enable each source in turn,
8998 * compute the final state we want first and check if we need to
8999 * make any changes at all.
9000 */
9001 final = val;
9002 final &= ~DREF_NONSPREAD_SOURCE_MASK;
9003 if (has_ck505)
9004 final |= DREF_NONSPREAD_CK505_ENABLE;
9005 else
9006 final |= DREF_NONSPREAD_SOURCE_ENABLE;
9007
8c07eb68 9008 final &= ~DREF_SSC_SOURCE_MASK;
74cfd7ac 9009 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8c07eb68 9010 final &= ~DREF_SSC1_ENABLE;
74cfd7ac
CW
9011
9012 if (has_panel) {
9013 final |= DREF_SSC_SOURCE_ENABLE;
9014
9015 if (intel_panel_use_ssc(dev_priv) && can_ssc)
9016 final |= DREF_SSC1_ENABLE;
9017
9018 if (has_cpu_edp) {
9019 if (intel_panel_use_ssc(dev_priv) && can_ssc)
9020 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
9021 else
9022 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
9023 } else
9024 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
1c1a24d2
L
9025 } else if (using_ssc_source) {
9026 final |= DREF_SSC_SOURCE_ENABLE;
9027 final |= DREF_SSC1_ENABLE;
74cfd7ac
CW
9028 }
9029
9030 if (final == val)
9031 return;
9032
13d83a67 9033 /* Always enable nonspread source */
74cfd7ac 9034 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 9035
99eb6a01 9036 if (has_ck505)
74cfd7ac 9037 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 9038 else
74cfd7ac 9039 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 9040
199e5d79 9041 if (has_panel) {
74cfd7ac
CW
9042 val &= ~DREF_SSC_SOURCE_MASK;
9043 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 9044
199e5d79 9045 /* SSC must be turned on before enabling the CPU output */
99eb6a01 9046 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 9047 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 9048 val |= DREF_SSC1_ENABLE;
e77166b5 9049 } else
74cfd7ac 9050 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
9051
9052 /* Get SSC going before enabling the outputs */
74cfd7ac 9053 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
9054 POSTING_READ(PCH_DREF_CONTROL);
9055 udelay(200);
9056
74cfd7ac 9057 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
9058
9059 /* Enable CPU source on CPU attached eDP */
199e5d79 9060 if (has_cpu_edp) {
99eb6a01 9061 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 9062 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 9063 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 9064 } else
74cfd7ac 9065 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 9066 } else
74cfd7ac 9067 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 9068
74cfd7ac 9069 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
9070 POSTING_READ(PCH_DREF_CONTROL);
9071 udelay(200);
9072 } else {
1c1a24d2 9073 DRM_DEBUG_KMS("Disabling CPU source output\n");
199e5d79 9074
74cfd7ac 9075 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
9076
9077 /* Turn off CPU output */
74cfd7ac 9078 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 9079
74cfd7ac 9080 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
9081 POSTING_READ(PCH_DREF_CONTROL);
9082 udelay(200);
9083
1c1a24d2
L
9084 if (!using_ssc_source) {
9085 DRM_DEBUG_KMS("Disabling SSC source\n");
199e5d79 9086
1c1a24d2
L
9087 /* Turn off the SSC source */
9088 val &= ~DREF_SSC_SOURCE_MASK;
9089 val |= DREF_SSC_SOURCE_DISABLE;
f165d283 9090
1c1a24d2
L
9091 /* Turn off SSC1 */
9092 val &= ~DREF_SSC1_ENABLE;
9093
9094 I915_WRITE(PCH_DREF_CONTROL, val);
9095 POSTING_READ(PCH_DREF_CONTROL);
9096 udelay(200);
9097 }
13d83a67 9098 }
74cfd7ac
CW
9099
9100 BUG_ON(val != final);
13d83a67
JB
9101}
9102
f31f2d55 9103static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 9104{
f31f2d55 9105 uint32_t tmp;
dde86e2d 9106
0ff066a9
PZ
9107 tmp = I915_READ(SOUTH_CHICKEN2);
9108 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
9109 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 9110
cf3598c2
ID
9111 if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
9112 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
0ff066a9 9113 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 9114
0ff066a9
PZ
9115 tmp = I915_READ(SOUTH_CHICKEN2);
9116 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
9117 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 9118
cf3598c2
ID
9119 if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
9120 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
0ff066a9 9121 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
9122}
9123
9124/* WaMPhyProgramming:hsw */
9125static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
9126{
9127 uint32_t tmp;
dde86e2d
PZ
9128
9129 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
9130 tmp &= ~(0xFF << 24);
9131 tmp |= (0x12 << 24);
9132 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
9133
dde86e2d
PZ
9134 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
9135 tmp |= (1 << 11);
9136 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
9137
9138 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
9139 tmp |= (1 << 11);
9140 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
9141
dde86e2d
PZ
9142 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
9143 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
9144 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
9145
9146 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
9147 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
9148 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
9149
0ff066a9
PZ
9150 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
9151 tmp &= ~(7 << 13);
9152 tmp |= (5 << 13);
9153 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 9154
0ff066a9
PZ
9155 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
9156 tmp &= ~(7 << 13);
9157 tmp |= (5 << 13);
9158 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
9159
9160 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
9161 tmp &= ~0xFF;
9162 tmp |= 0x1C;
9163 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
9164
9165 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
9166 tmp &= ~0xFF;
9167 tmp |= 0x1C;
9168 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
9169
9170 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
9171 tmp &= ~(0xFF << 16);
9172 tmp |= (0x1C << 16);
9173 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
9174
9175 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
9176 tmp &= ~(0xFF << 16);
9177 tmp |= (0x1C << 16);
9178 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
9179
0ff066a9
PZ
9180 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
9181 tmp |= (1 << 27);
9182 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 9183
0ff066a9
PZ
9184 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
9185 tmp |= (1 << 27);
9186 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 9187
0ff066a9
PZ
9188 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
9189 tmp &= ~(0xF << 28);
9190 tmp |= (4 << 28);
9191 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 9192
0ff066a9
PZ
9193 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
9194 tmp &= ~(0xF << 28);
9195 tmp |= (4 << 28);
9196 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
9197}
9198
2fa86a1f
PZ
9199/* Implements 3 different sequences from BSpec chapter "Display iCLK
9200 * Programming" based on the parameters passed:
9201 * - Sequence to enable CLKOUT_DP
9202 * - Sequence to enable CLKOUT_DP without spread
9203 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
9204 */
9205static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
9206 bool with_fdi)
f31f2d55 9207{
fac5e23e 9208 struct drm_i915_private *dev_priv = to_i915(dev);
2fa86a1f
PZ
9209 uint32_t reg, tmp;
9210
9211 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
9212 with_spread = true;
4f8036a2
TU
9213 if (WARN(HAS_PCH_LPT_LP(dev_priv) &&
9214 with_fdi, "LP PCH doesn't have FDI\n"))
2fa86a1f 9215 with_fdi = false;
f31f2d55 9216
a580516d 9217 mutex_lock(&dev_priv->sb_lock);
f31f2d55
PZ
9218
9219 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
9220 tmp &= ~SBI_SSCCTL_DISABLE;
9221 tmp |= SBI_SSCCTL_PATHALT;
9222 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
9223
9224 udelay(24);
9225
2fa86a1f
PZ
9226 if (with_spread) {
9227 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
9228 tmp &= ~SBI_SSCCTL_PATHALT;
9229 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 9230
2fa86a1f
PZ
9231 if (with_fdi) {
9232 lpt_reset_fdi_mphy(dev_priv);
9233 lpt_program_fdi_mphy(dev_priv);
9234 }
9235 }
dde86e2d 9236
4f8036a2 9237 reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
2fa86a1f
PZ
9238 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
9239 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
9240 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246 9241
a580516d 9242 mutex_unlock(&dev_priv->sb_lock);
dde86e2d
PZ
9243}
9244
47701c3b
PZ
9245/* Sequence to disable CLKOUT_DP */
9246static void lpt_disable_clkout_dp(struct drm_device *dev)
9247{
fac5e23e 9248 struct drm_i915_private *dev_priv = to_i915(dev);
47701c3b
PZ
9249 uint32_t reg, tmp;
9250
a580516d 9251 mutex_lock(&dev_priv->sb_lock);
47701c3b 9252
4f8036a2 9253 reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
47701c3b
PZ
9254 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
9255 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
9256 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
9257
9258 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
9259 if (!(tmp & SBI_SSCCTL_DISABLE)) {
9260 if (!(tmp & SBI_SSCCTL_PATHALT)) {
9261 tmp |= SBI_SSCCTL_PATHALT;
9262 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
9263 udelay(32);
9264 }
9265 tmp |= SBI_SSCCTL_DISABLE;
9266 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
9267 }
9268
a580516d 9269 mutex_unlock(&dev_priv->sb_lock);
47701c3b
PZ
9270}
9271
f7be2c21
VS
9272#define BEND_IDX(steps) ((50 + (steps)) / 5)
9273
9274static const uint16_t sscdivintphase[] = {
9275 [BEND_IDX( 50)] = 0x3B23,
9276 [BEND_IDX( 45)] = 0x3B23,
9277 [BEND_IDX( 40)] = 0x3C23,
9278 [BEND_IDX( 35)] = 0x3C23,
9279 [BEND_IDX( 30)] = 0x3D23,
9280 [BEND_IDX( 25)] = 0x3D23,
9281 [BEND_IDX( 20)] = 0x3E23,
9282 [BEND_IDX( 15)] = 0x3E23,
9283 [BEND_IDX( 10)] = 0x3F23,
9284 [BEND_IDX( 5)] = 0x3F23,
9285 [BEND_IDX( 0)] = 0x0025,
9286 [BEND_IDX( -5)] = 0x0025,
9287 [BEND_IDX(-10)] = 0x0125,
9288 [BEND_IDX(-15)] = 0x0125,
9289 [BEND_IDX(-20)] = 0x0225,
9290 [BEND_IDX(-25)] = 0x0225,
9291 [BEND_IDX(-30)] = 0x0325,
9292 [BEND_IDX(-35)] = 0x0325,
9293 [BEND_IDX(-40)] = 0x0425,
9294 [BEND_IDX(-45)] = 0x0425,
9295 [BEND_IDX(-50)] = 0x0525,
9296};
9297
9298/*
9299 * Bend CLKOUT_DP
9300 * steps -50 to 50 inclusive, in steps of 5
9301 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
9302 * change in clock period = -(steps / 10) * 5.787 ps
9303 */
9304static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
9305{
9306 uint32_t tmp;
9307 int idx = BEND_IDX(steps);
9308
9309 if (WARN_ON(steps % 5 != 0))
9310 return;
9311
9312 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
9313 return;
9314
9315 mutex_lock(&dev_priv->sb_lock);
9316
9317 if (steps % 10 != 0)
9318 tmp = 0xAAAAAAAB;
9319 else
9320 tmp = 0x00000000;
9321 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
9322
9323 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
9324 tmp &= 0xffff0000;
9325 tmp |= sscdivintphase[idx];
9326 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
9327
9328 mutex_unlock(&dev_priv->sb_lock);
9329}
9330
9331#undef BEND_IDX
9332
bf8fa3d3
PZ
9333static void lpt_init_pch_refclk(struct drm_device *dev)
9334{
bf8fa3d3
PZ
9335 struct intel_encoder *encoder;
9336 bool has_vga = false;
9337
b2784e15 9338 for_each_intel_encoder(dev, encoder) {
bf8fa3d3
PZ
9339 switch (encoder->type) {
9340 case INTEL_OUTPUT_ANALOG:
9341 has_vga = true;
9342 break;
6847d71b
PZ
9343 default:
9344 break;
bf8fa3d3
PZ
9345 }
9346 }
9347
f7be2c21
VS
9348 if (has_vga) {
9349 lpt_bend_clkout_dp(to_i915(dev), 0);
47701c3b 9350 lpt_enable_clkout_dp(dev, true, true);
f7be2c21 9351 } else {
47701c3b 9352 lpt_disable_clkout_dp(dev);
f7be2c21 9353 }
bf8fa3d3
PZ
9354}
9355
dde86e2d
PZ
9356/*
9357 * Initialize reference clocks when the driver loads
9358 */
9359void intel_init_pch_refclk(struct drm_device *dev)
9360{
6e266956
TU
9361 struct drm_i915_private *dev_priv = to_i915(dev);
9362
9363 if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
dde86e2d 9364 ironlake_init_pch_refclk(dev);
6e266956 9365 else if (HAS_PCH_LPT(dev_priv))
dde86e2d
PZ
9366 lpt_init_pch_refclk(dev);
9367}
9368
6ff93609 9369static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 9370{
fac5e23e 9371 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
79e53945
JB
9372 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9373 int pipe = intel_crtc->pipe;
c8203565
PZ
9374 uint32_t val;
9375
78114071 9376 val = 0;
c8203565 9377
6e3c9717 9378 switch (intel_crtc->config->pipe_bpp) {
c8203565 9379 case 18:
dfd07d72 9380 val |= PIPECONF_6BPC;
c8203565
PZ
9381 break;
9382 case 24:
dfd07d72 9383 val |= PIPECONF_8BPC;
c8203565
PZ
9384 break;
9385 case 30:
dfd07d72 9386 val |= PIPECONF_10BPC;
c8203565
PZ
9387 break;
9388 case 36:
dfd07d72 9389 val |= PIPECONF_12BPC;
c8203565
PZ
9390 break;
9391 default:
cc769b62
PZ
9392 /* Case prevented by intel_choose_pipe_bpp_dither. */
9393 BUG();
c8203565
PZ
9394 }
9395
6e3c9717 9396 if (intel_crtc->config->dither)
c8203565
PZ
9397 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
9398
6e3c9717 9399 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
9400 val |= PIPECONF_INTERLACED_ILK;
9401 else
9402 val |= PIPECONF_PROGRESSIVE;
9403
6e3c9717 9404 if (intel_crtc->config->limited_color_range)
3685a8f3 9405 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 9406
c8203565
PZ
9407 I915_WRITE(PIPECONF(pipe), val);
9408 POSTING_READ(PIPECONF(pipe));
9409}
9410
6ff93609 9411static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 9412{
fac5e23e 9413 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
ee2b0b38 9414 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 9415 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
391bf048 9416 u32 val = 0;
ee2b0b38 9417
391bf048 9418 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
ee2b0b38
PZ
9419 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
9420
6e3c9717 9421 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
9422 val |= PIPECONF_INTERLACED_ILK;
9423 else
9424 val |= PIPECONF_PROGRESSIVE;
9425
702e7a56
PZ
9426 I915_WRITE(PIPECONF(cpu_transcoder), val);
9427 POSTING_READ(PIPECONF(cpu_transcoder));
391bf048
JN
9428}
9429
391bf048
JN
9430static void haswell_set_pipemisc(struct drm_crtc *crtc)
9431{
fac5e23e 9432 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
391bf048 9433 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756f85cf 9434
391bf048
JN
9435 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
9436 u32 val = 0;
756f85cf 9437
6e3c9717 9438 switch (intel_crtc->config->pipe_bpp) {
756f85cf
PZ
9439 case 18:
9440 val |= PIPEMISC_DITHER_6_BPC;
9441 break;
9442 case 24:
9443 val |= PIPEMISC_DITHER_8_BPC;
9444 break;
9445 case 30:
9446 val |= PIPEMISC_DITHER_10_BPC;
9447 break;
9448 case 36:
9449 val |= PIPEMISC_DITHER_12_BPC;
9450 break;
9451 default:
9452 /* Case prevented by pipe_config_set_bpp. */
9453 BUG();
9454 }
9455
6e3c9717 9456 if (intel_crtc->config->dither)
756f85cf
PZ
9457 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
9458
391bf048 9459 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
756f85cf 9460 }
ee2b0b38
PZ
9461}
9462
d4b1931c
PZ
9463int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
9464{
9465 /*
9466 * Account for spread spectrum to avoid
9467 * oversubscribing the link. Max center spread
9468 * is 2.5%; use 5% for safety's sake.
9469 */
9470 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 9471 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
9472}
9473
7429e9d4 9474static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 9475{
7429e9d4 9476 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
9477}
9478
b75ca6f6
ACO
9479static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
9480 struct intel_crtc_state *crtc_state,
9e2c8475 9481 struct dpll *reduced_clock)
79e53945 9482{
de13a2e3 9483 struct drm_crtc *crtc = &intel_crtc->base;
79e53945 9484 struct drm_device *dev = crtc->dev;
fac5e23e 9485 struct drm_i915_private *dev_priv = to_i915(dev);
b75ca6f6 9486 u32 dpll, fp, fp2;
3d6e9ee0 9487 int factor;
79e53945 9488
c1858123 9489 /* Enable autotuning of the PLL clock (if permissible) */
8febb297 9490 factor = 21;
3d6e9ee0 9491 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8febb297 9492 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 9493 dev_priv->vbt.lvds_ssc_freq == 100000) ||
6e266956 9494 (HAS_PCH_IBX(dev_priv) && intel_is_dual_link_lvds(dev)))
8febb297 9495 factor = 25;
190f68c5 9496 } else if (crtc_state->sdvo_tv_clock)
8febb297 9497 factor = 20;
c1858123 9498
b75ca6f6
ACO
9499 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
9500
190f68c5 9501 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
b75ca6f6
ACO
9502 fp |= FP_CB_TUNE;
9503
9504 if (reduced_clock) {
9505 fp2 = i9xx_dpll_compute_fp(reduced_clock);
2c07245f 9506
b75ca6f6
ACO
9507 if (reduced_clock->m < factor * reduced_clock->n)
9508 fp2 |= FP_CB_TUNE;
9509 } else {
9510 fp2 = fp;
9511 }
9a7c7890 9512
5eddb70b 9513 dpll = 0;
2c07245f 9514
3d6e9ee0 9515 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
a07d6787
EA
9516 dpll |= DPLLB_MODE_LVDS;
9517 else
9518 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 9519
190f68c5 9520 dpll |= (crtc_state->pixel_multiplier - 1)
ef1b460d 9521 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f 9522
3d6e9ee0
VS
9523 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
9524 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
4a33e48d 9525 dpll |= DPLL_SDVO_HIGH_SPEED;
3d6e9ee0 9526
37a5650b 9527 if (intel_crtc_has_dp_encoder(crtc_state))
4a33e48d 9528 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 9529
7d7f8633
VS
9530 /*
9531 * The high speed IO clock is only really required for
9532 * SDVO/HDMI/DP, but we also enable it for CRT to make it
9533 * possible to share the DPLL between CRT and HDMI. Enabling
9534 * the clock needlessly does no real harm, except use up a
9535 * bit of power potentially.
9536 *
9537 * We'll limit this to IVB with 3 pipes, since it has only two
9538 * DPLLs and so DPLL sharing is the only way to get three pipes
9539 * driving PCH ports at the same time. On SNB we could do this,
9540 * and potentially avoid enabling the second DPLL, but it's not
9541 * clear if it''s a win or loss power wise. No point in doing
9542 * this on ILK at all since it has a fixed DPLL<->pipe mapping.
9543 */
9544 if (INTEL_INFO(dev_priv)->num_pipes == 3 &&
9545 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
9546 dpll |= DPLL_SDVO_HIGH_SPEED;
9547
a07d6787 9548 /* compute bitmask from p1 value */
190f68c5 9549 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 9550 /* also FPA1 */
190f68c5 9551 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 9552
190f68c5 9553 switch (crtc_state->dpll.p2) {
a07d6787
EA
9554 case 5:
9555 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
9556 break;
9557 case 7:
9558 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
9559 break;
9560 case 10:
9561 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
9562 break;
9563 case 14:
9564 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
9565 break;
79e53945
JB
9566 }
9567
3d6e9ee0
VS
9568 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
9569 intel_panel_use_ssc(dev_priv))
43565a06 9570 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
9571 else
9572 dpll |= PLL_REF_INPUT_DREFCLK;
9573
b75ca6f6
ACO
9574 dpll |= DPLL_VCO_ENABLE;
9575
9576 crtc_state->dpll_hw_state.dpll = dpll;
9577 crtc_state->dpll_hw_state.fp0 = fp;
9578 crtc_state->dpll_hw_state.fp1 = fp2;
de13a2e3
PZ
9579}
9580
190f68c5
ACO
9581static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
9582 struct intel_crtc_state *crtc_state)
de13a2e3 9583{
997c030c 9584 struct drm_device *dev = crtc->base.dev;
fac5e23e 9585 struct drm_i915_private *dev_priv = to_i915(dev);
9e2c8475 9586 struct dpll reduced_clock;
7ed9f894 9587 bool has_reduced_clock = false;
e2b78267 9588 struct intel_shared_dpll *pll;
1b6f4958 9589 const struct intel_limit *limit;
997c030c 9590 int refclk = 120000;
de13a2e3 9591
dd3cd74a
ACO
9592 memset(&crtc_state->dpll_hw_state, 0,
9593 sizeof(crtc_state->dpll_hw_state));
9594
ded220e2
ACO
9595 crtc->lowfreq_avail = false;
9596
9597 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
9598 if (!crtc_state->has_pch_encoder)
9599 return 0;
79e53945 9600
2d84d2b3 9601 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
997c030c
ACO
9602 if (intel_panel_use_ssc(dev_priv)) {
9603 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
9604 dev_priv->vbt.lvds_ssc_freq);
9605 refclk = dev_priv->vbt.lvds_ssc_freq;
9606 }
9607
9608 if (intel_is_dual_link_lvds(dev)) {
9609 if (refclk == 100000)
9610 limit = &intel_limits_ironlake_dual_lvds_100m;
9611 else
9612 limit = &intel_limits_ironlake_dual_lvds;
9613 } else {
9614 if (refclk == 100000)
9615 limit = &intel_limits_ironlake_single_lvds_100m;
9616 else
9617 limit = &intel_limits_ironlake_single_lvds;
9618 }
9619 } else {
9620 limit = &intel_limits_ironlake_dac;
9621 }
9622
364ee29d 9623 if (!crtc_state->clock_set &&
997c030c
ACO
9624 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9625 refclk, NULL, &crtc_state->dpll)) {
364ee29d
ACO
9626 DRM_ERROR("Couldn't find PLL settings for mode!\n");
9627 return -EINVAL;
f47709a9 9628 }
79e53945 9629
b75ca6f6
ACO
9630 ironlake_compute_dpll(crtc, crtc_state,
9631 has_reduced_clock ? &reduced_clock : NULL);
66e985c0 9632
ded220e2
ACO
9633 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
9634 if (pll == NULL) {
9635 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
9636 pipe_name(crtc->pipe));
9637 return -EINVAL;
3fb37703 9638 }
79e53945 9639
2d84d2b3 9640 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ded220e2 9641 has_reduced_clock)
c7653199 9642 crtc->lowfreq_avail = true;
e2b78267 9643
c8f7a0db 9644 return 0;
79e53945
JB
9645}
9646
eb14cb74
VS
9647static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
9648 struct intel_link_m_n *m_n)
9649{
9650 struct drm_device *dev = crtc->base.dev;
fac5e23e 9651 struct drm_i915_private *dev_priv = to_i915(dev);
eb14cb74
VS
9652 enum pipe pipe = crtc->pipe;
9653
9654 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
9655 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
9656 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
9657 & ~TU_SIZE_MASK;
9658 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
9659 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
9660 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9661}
9662
9663static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
9664 enum transcoder transcoder,
b95af8be
VK
9665 struct intel_link_m_n *m_n,
9666 struct intel_link_m_n *m2_n2)
72419203
SV
9667{
9668 struct drm_device *dev = crtc->base.dev;
fac5e23e 9669 struct drm_i915_private *dev_priv = to_i915(dev);
eb14cb74 9670 enum pipe pipe = crtc->pipe;
72419203 9671
eb14cb74
VS
9672 if (INTEL_INFO(dev)->gen >= 5) {
9673 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
9674 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
9675 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
9676 & ~TU_SIZE_MASK;
9677 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
9678 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
9679 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
b95af8be
VK
9680 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
9681 * gen < 8) and if DRRS is supported (to make sure the
9682 * registers are not unnecessarily read).
9683 */
9684 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
6e3c9717 9685 crtc->config->has_drrs) {
b95af8be
VK
9686 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
9687 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
9688 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
9689 & ~TU_SIZE_MASK;
9690 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
9691 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
9692 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9693 }
eb14cb74
VS
9694 } else {
9695 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
9696 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
9697 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
9698 & ~TU_SIZE_MASK;
9699 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
9700 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
9701 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9702 }
9703}
9704
9705void intel_dp_get_m_n(struct intel_crtc *crtc,
5cec258b 9706 struct intel_crtc_state *pipe_config)
eb14cb74 9707{
681a8504 9708 if (pipe_config->has_pch_encoder)
eb14cb74
VS
9709 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
9710 else
9711 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be
VK
9712 &pipe_config->dp_m_n,
9713 &pipe_config->dp_m2_n2);
eb14cb74 9714}
72419203 9715
eb14cb74 9716static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5cec258b 9717 struct intel_crtc_state *pipe_config)
eb14cb74
VS
9718{
9719 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be 9720 &pipe_config->fdi_m_n, NULL);
72419203
SV
9721}
9722
bd2e244f 9723static void skylake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9724 struct intel_crtc_state *pipe_config)
bd2e244f
JB
9725{
9726 struct drm_device *dev = crtc->base.dev;
fac5e23e 9727 struct drm_i915_private *dev_priv = to_i915(dev);
a1b2278e
CK
9728 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9729 uint32_t ps_ctrl = 0;
9730 int id = -1;
9731 int i;
bd2e244f 9732
a1b2278e
CK
9733 /* find scaler attached to this pipe */
9734 for (i = 0; i < crtc->num_scalers; i++) {
9735 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9736 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9737 id = i;
9738 pipe_config->pch_pfit.enabled = true;
9739 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9740 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9741 break;
9742 }
9743 }
bd2e244f 9744
a1b2278e
CK
9745 scaler_state->scaler_id = id;
9746 if (id >= 0) {
9747 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9748 } else {
9749 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
bd2e244f
JB
9750 }
9751}
9752
5724dbd1
DL
9753static void
9754skylake_get_initial_plane_config(struct intel_crtc *crtc,
9755 struct intel_initial_plane_config *plane_config)
bc8d7dff
DL
9756{
9757 struct drm_device *dev = crtc->base.dev;
fac5e23e 9758 struct drm_i915_private *dev_priv = to_i915(dev);
40f46283 9759 u32 val, base, offset, stride_mult, tiling;
bc8d7dff
DL
9760 int pipe = crtc->pipe;
9761 int fourcc, pixel_format;
6761dd31 9762 unsigned int aligned_height;
bc8d7dff 9763 struct drm_framebuffer *fb;
1b842c89 9764 struct intel_framebuffer *intel_fb;
bc8d7dff 9765
d9806c9f 9766 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9767 if (!intel_fb) {
bc8d7dff
DL
9768 DRM_DEBUG_KMS("failed to alloc fb\n");
9769 return;
9770 }
9771
1b842c89
DL
9772 fb = &intel_fb->base;
9773
bc8d7dff 9774 val = I915_READ(PLANE_CTL(pipe, 0));
42a7b088
DL
9775 if (!(val & PLANE_CTL_ENABLE))
9776 goto error;
9777
bc8d7dff
DL
9778 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9779 fourcc = skl_format_to_fourcc(pixel_format,
9780 val & PLANE_CTL_ORDER_RGBX,
9781 val & PLANE_CTL_ALPHA_MASK);
9782 fb->pixel_format = fourcc;
9783 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9784
40f46283
DL
9785 tiling = val & PLANE_CTL_TILED_MASK;
9786 switch (tiling) {
9787 case PLANE_CTL_TILED_LINEAR:
9788 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9789 break;
9790 case PLANE_CTL_TILED_X:
9791 plane_config->tiling = I915_TILING_X;
9792 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9793 break;
9794 case PLANE_CTL_TILED_Y:
9795 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9796 break;
9797 case PLANE_CTL_TILED_YF:
9798 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9799 break;
9800 default:
9801 MISSING_CASE(tiling);
9802 goto error;
9803 }
9804
bc8d7dff
DL
9805 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9806 plane_config->base = base;
9807
9808 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9809
9810 val = I915_READ(PLANE_SIZE(pipe, 0));
9811 fb->height = ((val >> 16) & 0xfff) + 1;
9812 fb->width = ((val >> 0) & 0x1fff) + 1;
9813
9814 val = I915_READ(PLANE_STRIDE(pipe, 0));
7b49f948 9815 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
40f46283 9816 fb->pixel_format);
bc8d7dff
DL
9817 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9818
9819 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
SV
9820 fb->pixel_format,
9821 fb->modifier[0]);
bc8d7dff 9822
f37b5c2b 9823 plane_config->size = fb->pitches[0] * aligned_height;
bc8d7dff
DL
9824
9825 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9826 pipe_name(pipe), fb->width, fb->height,
9827 fb->bits_per_pixel, base, fb->pitches[0],
9828 plane_config->size);
9829
2d14030b 9830 plane_config->fb = intel_fb;
bc8d7dff
DL
9831 return;
9832
9833error:
d1a3a036 9834 kfree(intel_fb);
bc8d7dff
DL
9835}
9836
2fa2fe9a 9837static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9838 struct intel_crtc_state *pipe_config)
2fa2fe9a
SV
9839{
9840 struct drm_device *dev = crtc->base.dev;
fac5e23e 9841 struct drm_i915_private *dev_priv = to_i915(dev);
2fa2fe9a
SV
9842 uint32_t tmp;
9843
9844 tmp = I915_READ(PF_CTL(crtc->pipe));
9845
9846 if (tmp & PF_ENABLE) {
fd4daa9c 9847 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
SV
9848 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9849 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
SV
9850
9851 /* We currently do not free assignements of panel fitters on
9852 * ivb/hsw (since we don't use the higher upscaling modes which
9853 * differentiates them) so just WARN about this case for now. */
5db94019 9854 if (IS_GEN7(dev_priv)) {
cb8b2a30
SV
9855 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9856 PF_PIPE_SEL_IVB(crtc->pipe));
9857 }
2fa2fe9a 9858 }
79e53945
JB
9859}
9860
5724dbd1
DL
9861static void
9862ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9863 struct intel_initial_plane_config *plane_config)
4c6baa59
JB
9864{
9865 struct drm_device *dev = crtc->base.dev;
fac5e23e 9866 struct drm_i915_private *dev_priv = to_i915(dev);
4c6baa59 9867 u32 val, base, offset;
aeee5a49 9868 int pipe = crtc->pipe;
4c6baa59 9869 int fourcc, pixel_format;
6761dd31 9870 unsigned int aligned_height;
b113d5ee 9871 struct drm_framebuffer *fb;
1b842c89 9872 struct intel_framebuffer *intel_fb;
4c6baa59 9873
42a7b088
DL
9874 val = I915_READ(DSPCNTR(pipe));
9875 if (!(val & DISPLAY_PLANE_ENABLE))
9876 return;
9877
d9806c9f 9878 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9879 if (!intel_fb) {
4c6baa59
JB
9880 DRM_DEBUG_KMS("failed to alloc fb\n");
9881 return;
9882 }
9883
1b842c89
DL
9884 fb = &intel_fb->base;
9885
18c5247e
SV
9886 if (INTEL_INFO(dev)->gen >= 4) {
9887 if (val & DISPPLANE_TILED) {
49af449b 9888 plane_config->tiling = I915_TILING_X;
18c5247e
SV
9889 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9890 }
9891 }
4c6baa59
JB
9892
9893 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 9894 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
9895 fb->pixel_format = fourcc;
9896 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
4c6baa59 9897
aeee5a49 9898 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
8652744b 9899 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
aeee5a49 9900 offset = I915_READ(DSPOFFSET(pipe));
4c6baa59 9901 } else {
49af449b 9902 if (plane_config->tiling)
aeee5a49 9903 offset = I915_READ(DSPTILEOFF(pipe));
4c6baa59 9904 else
aeee5a49 9905 offset = I915_READ(DSPLINOFF(pipe));
4c6baa59
JB
9906 }
9907 plane_config->base = base;
9908
9909 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
9910 fb->width = ((val >> 16) & 0xfff) + 1;
9911 fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
9912
9913 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 9914 fb->pitches[0] = val & 0xffffffc0;
4c6baa59 9915
b113d5ee 9916 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
SV
9917 fb->pixel_format,
9918 fb->modifier[0]);
4c6baa59 9919
f37b5c2b 9920 plane_config->size = fb->pitches[0] * aligned_height;
4c6baa59 9921
2844a921
DL
9922 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9923 pipe_name(pipe), fb->width, fb->height,
9924 fb->bits_per_pixel, base, fb->pitches[0],
9925 plane_config->size);
b113d5ee 9926
2d14030b 9927 plane_config->fb = intel_fb;
4c6baa59
JB
9928}
9929
0e8ffe1b 9930static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9931 struct intel_crtc_state *pipe_config)
0e8ffe1b
SV
9932{
9933 struct drm_device *dev = crtc->base.dev;
fac5e23e 9934 struct drm_i915_private *dev_priv = to_i915(dev);
1729050e 9935 enum intel_display_power_domain power_domain;
0e8ffe1b 9936 uint32_t tmp;
1729050e 9937 bool ret;
0e8ffe1b 9938
1729050e
ID
9939 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9940 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
930e8c9e
PZ
9941 return false;
9942
e143a21c 9943 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 9944 pipe_config->shared_dpll = NULL;
eccb140b 9945
1729050e 9946 ret = false;
0e8ffe1b
SV
9947 tmp = I915_READ(PIPECONF(crtc->pipe));
9948 if (!(tmp & PIPECONF_ENABLE))
1729050e 9949 goto out;
0e8ffe1b 9950
42571aef
VS
9951 switch (tmp & PIPECONF_BPC_MASK) {
9952 case PIPECONF_6BPC:
9953 pipe_config->pipe_bpp = 18;
9954 break;
9955 case PIPECONF_8BPC:
9956 pipe_config->pipe_bpp = 24;
9957 break;
9958 case PIPECONF_10BPC:
9959 pipe_config->pipe_bpp = 30;
9960 break;
9961 case PIPECONF_12BPC:
9962 pipe_config->pipe_bpp = 36;
9963 break;
9964 default:
9965 break;
9966 }
9967
b5a9fa09
SV
9968 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9969 pipe_config->limited_color_range = true;
9970
ab9412ba 9971 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0 9972 struct intel_shared_dpll *pll;
8106ddbd 9973 enum intel_dpll_id pll_id;
66e985c0 9974
88adfff1
SV
9975 pipe_config->has_pch_encoder = true;
9976
627eb5a3
SV
9977 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9978 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9979 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
SV
9980
9981 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 9982
2d1fe073 9983 if (HAS_PCH_IBX(dev_priv)) {
d9a7bc67
ID
9984 /*
9985 * The pipe->pch transcoder and pch transcoder->pll
9986 * mapping is fixed.
9987 */
8106ddbd 9988 pll_id = (enum intel_dpll_id) crtc->pipe;
c0d43d62
SV
9989 } else {
9990 tmp = I915_READ(PCH_DPLL_SEL);
9991 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
8106ddbd 9992 pll_id = DPLL_ID_PCH_PLL_B;
c0d43d62 9993 else
8106ddbd 9994 pll_id= DPLL_ID_PCH_PLL_A;
c0d43d62 9995 }
66e985c0 9996
8106ddbd
ACO
9997 pipe_config->shared_dpll =
9998 intel_get_shared_dpll_by_id(dev_priv, pll_id);
9999 pll = pipe_config->shared_dpll;
66e985c0 10000
2edd6443
ACO
10001 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
10002 &pipe_config->dpll_hw_state));
c93f54cf
SV
10003
10004 tmp = pipe_config->dpll_hw_state.dpll;
10005 pipe_config->pixel_multiplier =
10006 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
10007 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
10008
10009 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
SV
10010 } else {
10011 pipe_config->pixel_multiplier = 1;
627eb5a3
SV
10012 }
10013
1bd1bd80 10014 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 10015 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 10016
2fa2fe9a
SV
10017 ironlake_get_pfit_config(crtc, pipe_config);
10018
1729050e
ID
10019 ret = true;
10020
10021out:
10022 intel_display_power_put(dev_priv, power_domain);
10023
10024 return ret;
0e8ffe1b
SV
10025}
10026
be256dc7
PZ
10027static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
10028{
91c8a326 10029 struct drm_device *dev = &dev_priv->drm;
be256dc7 10030 struct intel_crtc *crtc;
be256dc7 10031
d3fcc808 10032 for_each_intel_crtc(dev, crtc)
e2c719b7 10033 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
10034 pipe_name(crtc->pipe));
10035
e2c719b7
RC
10036 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
10037 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
01403de3
VS
10038 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
10039 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
44cb734c 10040 I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
e2c719b7 10041 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
be256dc7 10042 "CPU PWM1 enabled\n");
772c2a51 10043 if (IS_HASWELL(dev_priv))
e2c719b7 10044 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
c5107b87 10045 "CPU PWM2 enabled\n");
e2c719b7 10046 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
be256dc7 10047 "PCH PWM1 enabled\n");
e2c719b7 10048 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
be256dc7 10049 "Utility pin enabled\n");
e2c719b7 10050 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
be256dc7 10051
9926ada1
PZ
10052 /*
10053 * In theory we can still leave IRQs enabled, as long as only the HPD
10054 * interrupts remain enabled. We used to check for that, but since it's
10055 * gen-specific and since we only disable LCPLL after we fully disable
10056 * the interrupts, the check below should be enough.
10057 */
e2c719b7 10058 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
be256dc7
PZ
10059}
10060
9ccd5aeb
PZ
10061static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
10062{
772c2a51 10063 if (IS_HASWELL(dev_priv))
9ccd5aeb
PZ
10064 return I915_READ(D_COMP_HSW);
10065 else
10066 return I915_READ(D_COMP_BDW);
10067}
10068
3c4c9b81
PZ
10069static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
10070{
772c2a51 10071 if (IS_HASWELL(dev_priv)) {
3c4c9b81
PZ
10072 mutex_lock(&dev_priv->rps.hw_lock);
10073 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
10074 val))
79cf219a 10075 DRM_DEBUG_KMS("Failed to write to D_COMP\n");
3c4c9b81
PZ
10076 mutex_unlock(&dev_priv->rps.hw_lock);
10077 } else {
9ccd5aeb
PZ
10078 I915_WRITE(D_COMP_BDW, val);
10079 POSTING_READ(D_COMP_BDW);
3c4c9b81 10080 }
be256dc7
PZ
10081}
10082
10083/*
10084 * This function implements pieces of two sequences from BSpec:
10085 * - Sequence for display software to disable LCPLL
10086 * - Sequence for display software to allow package C8+
10087 * The steps implemented here are just the steps that actually touch the LCPLL
10088 * register. Callers should take care of disabling all the display engine
10089 * functions, doing the mode unset, fixing interrupts, etc.
10090 */
6ff58d53
PZ
10091static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
10092 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
10093{
10094 uint32_t val;
10095
10096 assert_can_disable_lcpll(dev_priv);
10097
10098 val = I915_READ(LCPLL_CTL);
10099
10100 if (switch_to_fclk) {
10101 val |= LCPLL_CD_SOURCE_FCLK;
10102 I915_WRITE(LCPLL_CTL, val);
10103
f53dd63f
ID
10104 if (wait_for_us(I915_READ(LCPLL_CTL) &
10105 LCPLL_CD_SOURCE_FCLK_DONE, 1))
be256dc7
PZ
10106 DRM_ERROR("Switching to FCLK failed\n");
10107
10108 val = I915_READ(LCPLL_CTL);
10109 }
10110
10111 val |= LCPLL_PLL_DISABLE;
10112 I915_WRITE(LCPLL_CTL, val);
10113 POSTING_READ(LCPLL_CTL);
10114
24d8441d 10115 if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
be256dc7
PZ
10116 DRM_ERROR("LCPLL still locked\n");
10117
9ccd5aeb 10118 val = hsw_read_dcomp(dev_priv);
be256dc7 10119 val |= D_COMP_COMP_DISABLE;
3c4c9b81 10120 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
10121 ndelay(100);
10122
9ccd5aeb
PZ
10123 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
10124 1))
be256dc7
PZ
10125 DRM_ERROR("D_COMP RCOMP still in progress\n");
10126
10127 if (allow_power_down) {
10128 val = I915_READ(LCPLL_CTL);
10129 val |= LCPLL_POWER_DOWN_ALLOW;
10130 I915_WRITE(LCPLL_CTL, val);
10131 POSTING_READ(LCPLL_CTL);
10132 }
10133}
10134
10135/*
10136 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
10137 * source.
10138 */
6ff58d53 10139static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
10140{
10141 uint32_t val;
10142
10143 val = I915_READ(LCPLL_CTL);
10144
10145 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
10146 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
10147 return;
10148
a8a8bd54
PZ
10149 /*
10150 * Make sure we're not on PC8 state before disabling PC8, otherwise
10151 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
a8a8bd54 10152 */
59bad947 10153 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
215733fa 10154
be256dc7
PZ
10155 if (val & LCPLL_POWER_DOWN_ALLOW) {
10156 val &= ~LCPLL_POWER_DOWN_ALLOW;
10157 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 10158 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
10159 }
10160
9ccd5aeb 10161 val = hsw_read_dcomp(dev_priv);
be256dc7
PZ
10162 val |= D_COMP_COMP_FORCE;
10163 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 10164 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
10165
10166 val = I915_READ(LCPLL_CTL);
10167 val &= ~LCPLL_PLL_DISABLE;
10168 I915_WRITE(LCPLL_CTL, val);
10169
93220c08
CW
10170 if (intel_wait_for_register(dev_priv,
10171 LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
10172 5))
be256dc7
PZ
10173 DRM_ERROR("LCPLL not locked yet\n");
10174
10175 if (val & LCPLL_CD_SOURCE_FCLK) {
10176 val = I915_READ(LCPLL_CTL);
10177 val &= ~LCPLL_CD_SOURCE_FCLK;
10178 I915_WRITE(LCPLL_CTL, val);
10179
f53dd63f
ID
10180 if (wait_for_us((I915_READ(LCPLL_CTL) &
10181 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
be256dc7
PZ
10182 DRM_ERROR("Switching back to LCPLL failed\n");
10183 }
215733fa 10184
59bad947 10185 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
91c8a326 10186 intel_update_cdclk(&dev_priv->drm);
be256dc7
PZ
10187}
10188
765dab67
PZ
10189/*
10190 * Package states C8 and deeper are really deep PC states that can only be
10191 * reached when all the devices on the system allow it, so even if the graphics
10192 * device allows PC8+, it doesn't mean the system will actually get to these
10193 * states. Our driver only allows PC8+ when going into runtime PM.
10194 *
10195 * The requirements for PC8+ are that all the outputs are disabled, the power
10196 * well is disabled and most interrupts are disabled, and these are also
10197 * requirements for runtime PM. When these conditions are met, we manually do
10198 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
10199 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
10200 * hang the machine.
10201 *
10202 * When we really reach PC8 or deeper states (not just when we allow it) we lose
10203 * the state of some registers, so when we come back from PC8+ we need to
10204 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
10205 * need to take care of the registers kept by RC6. Notice that this happens even
10206 * if we don't put the device in PCI D3 state (which is what currently happens
10207 * because of the runtime PM support).
10208 *
10209 * For more, read "Display Sequences for Package C8" on the hardware
10210 * documentation.
10211 */
a14cb6fc 10212void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 10213{
91c8a326 10214 struct drm_device *dev = &dev_priv->drm;
c67a470b
PZ
10215 uint32_t val;
10216
c67a470b
PZ
10217 DRM_DEBUG_KMS("Enabling package C8+\n");
10218
4f8036a2 10219 if (HAS_PCH_LPT_LP(dev_priv)) {
c67a470b
PZ
10220 val = I915_READ(SOUTH_DSPCLK_GATE_D);
10221 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
10222 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
10223 }
10224
10225 lpt_disable_clkout_dp(dev);
c67a470b
PZ
10226 hsw_disable_lcpll(dev_priv, true, true);
10227}
10228
a14cb6fc 10229void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b 10230{
91c8a326 10231 struct drm_device *dev = &dev_priv->drm;
c67a470b
PZ
10232 uint32_t val;
10233
c67a470b
PZ
10234 DRM_DEBUG_KMS("Disabling package C8+\n");
10235
10236 hsw_restore_lcpll(dev_priv);
c67a470b
PZ
10237 lpt_init_pch_refclk(dev);
10238
4f8036a2 10239 if (HAS_PCH_LPT_LP(dev_priv)) {
c67a470b
PZ
10240 val = I915_READ(SOUTH_DSPCLK_GATE_D);
10241 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
10242 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
10243 }
c67a470b
PZ
10244}
10245
324513c0 10246static void bxt_modeset_commit_cdclk(struct drm_atomic_state *old_state)
f8437dd1 10247{
a821fc46 10248 struct drm_device *dev = old_state->dev;
1a617b77
ML
10249 struct intel_atomic_state *old_intel_state =
10250 to_intel_atomic_state(old_state);
10251 unsigned int req_cdclk = old_intel_state->dev_cdclk;
f8437dd1 10252
324513c0 10253 bxt_set_cdclk(to_i915(dev), req_cdclk);
f8437dd1
VK
10254}
10255
b432e5cf 10256/* compute the max rate for new configuration */
27c329ed 10257static int ilk_max_pixel_rate(struct drm_atomic_state *state)
b432e5cf 10258{
565602d7 10259 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 10260 struct drm_i915_private *dev_priv = to_i915(state->dev);
565602d7
ML
10261 struct drm_crtc *crtc;
10262 struct drm_crtc_state *cstate;
27c329ed 10263 struct intel_crtc_state *crtc_state;
565602d7
ML
10264 unsigned max_pixel_rate = 0, i;
10265 enum pipe pipe;
b432e5cf 10266
565602d7
ML
10267 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
10268 sizeof(intel_state->min_pixclk));
27c329ed 10269
565602d7
ML
10270 for_each_crtc_in_state(state, crtc, cstate, i) {
10271 int pixel_rate;
27c329ed 10272
565602d7
ML
10273 crtc_state = to_intel_crtc_state(cstate);
10274 if (!crtc_state->base.enable) {
10275 intel_state->min_pixclk[i] = 0;
b432e5cf 10276 continue;
565602d7 10277 }
b432e5cf 10278
27c329ed 10279 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
b432e5cf
VS
10280
10281 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
565602d7 10282 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
b432e5cf
VS
10283 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
10284
565602d7 10285 intel_state->min_pixclk[i] = pixel_rate;
b432e5cf
VS
10286 }
10287
565602d7
ML
10288 for_each_pipe(dev_priv, pipe)
10289 max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
10290
b432e5cf
VS
10291 return max_pixel_rate;
10292}
10293
10294static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
10295{
fac5e23e 10296 struct drm_i915_private *dev_priv = to_i915(dev);
b432e5cf
VS
10297 uint32_t val, data;
10298 int ret;
10299
10300 if (WARN((I915_READ(LCPLL_CTL) &
10301 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
10302 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
10303 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
10304 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
10305 "trying to change cdclk frequency with cdclk not enabled\n"))
10306 return;
10307
10308 mutex_lock(&dev_priv->rps.hw_lock);
10309 ret = sandybridge_pcode_write(dev_priv,
10310 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
10311 mutex_unlock(&dev_priv->rps.hw_lock);
10312 if (ret) {
10313 DRM_ERROR("failed to inform pcode about cdclk change\n");
10314 return;
10315 }
10316
10317 val = I915_READ(LCPLL_CTL);
10318 val |= LCPLL_CD_SOURCE_FCLK;
10319 I915_WRITE(LCPLL_CTL, val);
10320
5ba00178
TU
10321 if (wait_for_us(I915_READ(LCPLL_CTL) &
10322 LCPLL_CD_SOURCE_FCLK_DONE, 1))
b432e5cf
VS
10323 DRM_ERROR("Switching to FCLK failed\n");
10324
10325 val = I915_READ(LCPLL_CTL);
10326 val &= ~LCPLL_CLK_FREQ_MASK;
10327
10328 switch (cdclk) {
10329 case 450000:
10330 val |= LCPLL_CLK_FREQ_450;
10331 data = 0;
10332 break;
10333 case 540000:
10334 val |= LCPLL_CLK_FREQ_54O_BDW;
10335 data = 1;
10336 break;
10337 case 337500:
10338 val |= LCPLL_CLK_FREQ_337_5_BDW;
10339 data = 2;
10340 break;
10341 case 675000:
10342 val |= LCPLL_CLK_FREQ_675_BDW;
10343 data = 3;
10344 break;
10345 default:
10346 WARN(1, "invalid cdclk frequency\n");
10347 return;
10348 }
10349
10350 I915_WRITE(LCPLL_CTL, val);
10351
10352 val = I915_READ(LCPLL_CTL);
10353 val &= ~LCPLL_CD_SOURCE_FCLK;
10354 I915_WRITE(LCPLL_CTL, val);
10355
5ba00178
TU
10356 if (wait_for_us((I915_READ(LCPLL_CTL) &
10357 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
b432e5cf
VS
10358 DRM_ERROR("Switching back to LCPLL failed\n");
10359
10360 mutex_lock(&dev_priv->rps.hw_lock);
10361 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
10362 mutex_unlock(&dev_priv->rps.hw_lock);
10363
7f1052a8
VS
10364 I915_WRITE(CDCLK_FREQ, DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
10365
b432e5cf
VS
10366 intel_update_cdclk(dev);
10367
10368 WARN(cdclk != dev_priv->cdclk_freq,
10369 "cdclk requested %d kHz but got %d kHz\n",
10370 cdclk, dev_priv->cdclk_freq);
10371}
10372
587c7914
VS
10373static int broadwell_calc_cdclk(int max_pixclk)
10374{
10375 if (max_pixclk > 540000)
10376 return 675000;
10377 else if (max_pixclk > 450000)
10378 return 540000;
10379 else if (max_pixclk > 337500)
10380 return 450000;
10381 else
10382 return 337500;
10383}
10384
27c329ed 10385static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
b432e5cf 10386{
27c329ed 10387 struct drm_i915_private *dev_priv = to_i915(state->dev);
1a617b77 10388 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
27c329ed 10389 int max_pixclk = ilk_max_pixel_rate(state);
b432e5cf
VS
10390 int cdclk;
10391
10392 /*
10393 * FIXME should also account for plane ratio
10394 * once 64bpp pixel formats are supported.
10395 */
587c7914 10396 cdclk = broadwell_calc_cdclk(max_pixclk);
b432e5cf 10397
b432e5cf 10398 if (cdclk > dev_priv->max_cdclk_freq) {
63ba534e
ML
10399 DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
10400 cdclk, dev_priv->max_cdclk_freq);
10401 return -EINVAL;
b432e5cf
VS
10402 }
10403
1a617b77
ML
10404 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
10405 if (!intel_state->active_crtcs)
587c7914 10406 intel_state->dev_cdclk = broadwell_calc_cdclk(0);
b432e5cf
VS
10407
10408 return 0;
10409}
10410
27c329ed 10411static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
b432e5cf 10412{
27c329ed 10413 struct drm_device *dev = old_state->dev;
1a617b77
ML
10414 struct intel_atomic_state *old_intel_state =
10415 to_intel_atomic_state(old_state);
10416 unsigned req_cdclk = old_intel_state->dev_cdclk;
b432e5cf 10417
27c329ed 10418 broadwell_set_cdclk(dev, req_cdclk);
b432e5cf
VS
10419}
10420
c89e39f3
CT
10421static int skl_modeset_calc_cdclk(struct drm_atomic_state *state)
10422{
10423 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
10424 struct drm_i915_private *dev_priv = to_i915(state->dev);
10425 const int max_pixclk = ilk_max_pixel_rate(state);
a8ca4934 10426 int vco = intel_state->cdclk_pll_vco;
c89e39f3
CT
10427 int cdclk;
10428
10429 /*
10430 * FIXME should also account for plane ratio
10431 * once 64bpp pixel formats are supported.
10432 */
a8ca4934 10433 cdclk = skl_calc_cdclk(max_pixclk, vco);
c89e39f3
CT
10434
10435 /*
10436 * FIXME move the cdclk caclulation to
10437 * compute_config() so we can fail gracegully.
10438 */
10439 if (cdclk > dev_priv->max_cdclk_freq) {
10440 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
10441 cdclk, dev_priv->max_cdclk_freq);
10442 cdclk = dev_priv->max_cdclk_freq;
10443 }
10444
10445 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
10446 if (!intel_state->active_crtcs)
a8ca4934 10447 intel_state->dev_cdclk = skl_calc_cdclk(0, vco);
c89e39f3
CT
10448
10449 return 0;
10450}
10451
10452static void skl_modeset_commit_cdclk(struct drm_atomic_state *old_state)
10453{
1cd593e0
VS
10454 struct drm_i915_private *dev_priv = to_i915(old_state->dev);
10455 struct intel_atomic_state *intel_state = to_intel_atomic_state(old_state);
10456 unsigned int req_cdclk = intel_state->dev_cdclk;
10457 unsigned int req_vco = intel_state->cdclk_pll_vco;
c89e39f3 10458
1cd593e0 10459 skl_set_cdclk(dev_priv, req_cdclk, req_vco);
c89e39f3
CT
10460}
10461
190f68c5
ACO
10462static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
10463 struct intel_crtc_state *crtc_state)
09b4ddf9 10464{
d7edc4e5 10465 if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
af3997b5
MK
10466 if (!intel_ddi_pll_select(crtc, crtc_state))
10467 return -EINVAL;
10468 }
716c2e55 10469
c7653199 10470 crtc->lowfreq_avail = false;
644cef34 10471
c8f7a0db 10472 return 0;
79e53945
JB
10473}
10474
3760b59c
S
10475static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
10476 enum port port,
10477 struct intel_crtc_state *pipe_config)
10478{
8106ddbd
ACO
10479 enum intel_dpll_id id;
10480
3760b59c
S
10481 switch (port) {
10482 case PORT_A:
08250c4b 10483 id = DPLL_ID_SKL_DPLL0;
3760b59c
S
10484 break;
10485 case PORT_B:
08250c4b 10486 id = DPLL_ID_SKL_DPLL1;
3760b59c
S
10487 break;
10488 case PORT_C:
08250c4b 10489 id = DPLL_ID_SKL_DPLL2;
3760b59c
S
10490 break;
10491 default:
10492 DRM_ERROR("Incorrect port type\n");
8106ddbd 10493 return;
3760b59c 10494 }
8106ddbd
ACO
10495
10496 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
3760b59c
S
10497}
10498
96b7dfb7
S
10499static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
10500 enum port port,
5cec258b 10501 struct intel_crtc_state *pipe_config)
96b7dfb7 10502{
8106ddbd 10503 enum intel_dpll_id id;
a3c988ea 10504 u32 temp;
96b7dfb7
S
10505
10506 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
c856052a 10507 id = temp >> (port * 3 + 1);
96b7dfb7 10508
c856052a 10509 if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL3))
8106ddbd 10510 return;
8106ddbd
ACO
10511
10512 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
96b7dfb7
S
10513}
10514
7d2c8175
DL
10515static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
10516 enum port port,
5cec258b 10517 struct intel_crtc_state *pipe_config)
7d2c8175 10518{
8106ddbd 10519 enum intel_dpll_id id;
c856052a 10520 uint32_t ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
8106ddbd 10521
c856052a 10522 switch (ddi_pll_sel) {
7d2c8175 10523 case PORT_CLK_SEL_WRPLL1:
8106ddbd 10524 id = DPLL_ID_WRPLL1;
7d2c8175
DL
10525 break;
10526 case PORT_CLK_SEL_WRPLL2:
8106ddbd 10527 id = DPLL_ID_WRPLL2;
7d2c8175 10528 break;
00490c22 10529 case PORT_CLK_SEL_SPLL:
8106ddbd 10530 id = DPLL_ID_SPLL;
79bd23da 10531 break;
9d16da65
ACO
10532 case PORT_CLK_SEL_LCPLL_810:
10533 id = DPLL_ID_LCPLL_810;
10534 break;
10535 case PORT_CLK_SEL_LCPLL_1350:
10536 id = DPLL_ID_LCPLL_1350;
10537 break;
10538 case PORT_CLK_SEL_LCPLL_2700:
10539 id = DPLL_ID_LCPLL_2700;
10540 break;
8106ddbd 10541 default:
c856052a 10542 MISSING_CASE(ddi_pll_sel);
8106ddbd
ACO
10543 /* fall through */
10544 case PORT_CLK_SEL_NONE:
8106ddbd 10545 return;
7d2c8175 10546 }
8106ddbd
ACO
10547
10548 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
7d2c8175
DL
10549}
10550
cf30429e
JN
10551static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
10552 struct intel_crtc_state *pipe_config,
10553 unsigned long *power_domain_mask)
10554{
10555 struct drm_device *dev = crtc->base.dev;
fac5e23e 10556 struct drm_i915_private *dev_priv = to_i915(dev);
cf30429e
JN
10557 enum intel_display_power_domain power_domain;
10558 u32 tmp;
10559
d9a7bc67
ID
10560 /*
10561 * The pipe->transcoder mapping is fixed with the exception of the eDP
10562 * transcoder handled below.
10563 */
cf30429e
JN
10564 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
10565
10566 /*
10567 * XXX: Do intel_display_power_get_if_enabled before reading this (for
10568 * consistency and less surprising code; it's in always on power).
10569 */
10570 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
10571 if (tmp & TRANS_DDI_FUNC_ENABLE) {
10572 enum pipe trans_edp_pipe;
10573 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
10574 default:
10575 WARN(1, "unknown pipe linked to edp transcoder\n");
10576 case TRANS_DDI_EDP_INPUT_A_ONOFF:
10577 case TRANS_DDI_EDP_INPUT_A_ON:
10578 trans_edp_pipe = PIPE_A;
10579 break;
10580 case TRANS_DDI_EDP_INPUT_B_ONOFF:
10581 trans_edp_pipe = PIPE_B;
10582 break;
10583 case TRANS_DDI_EDP_INPUT_C_ONOFF:
10584 trans_edp_pipe = PIPE_C;
10585 break;
10586 }
10587
10588 if (trans_edp_pipe == crtc->pipe)
10589 pipe_config->cpu_transcoder = TRANSCODER_EDP;
10590 }
10591
10592 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
10593 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10594 return false;
10595 *power_domain_mask |= BIT(power_domain);
10596
10597 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
10598
10599 return tmp & PIPECONF_ENABLE;
10600}
10601
4d1de975
JN
10602static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
10603 struct intel_crtc_state *pipe_config,
10604 unsigned long *power_domain_mask)
10605{
10606 struct drm_device *dev = crtc->base.dev;
fac5e23e 10607 struct drm_i915_private *dev_priv = to_i915(dev);
4d1de975
JN
10608 enum intel_display_power_domain power_domain;
10609 enum port port;
10610 enum transcoder cpu_transcoder;
10611 u32 tmp;
10612
4d1de975
JN
10613 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
10614 if (port == PORT_A)
10615 cpu_transcoder = TRANSCODER_DSI_A;
10616 else
10617 cpu_transcoder = TRANSCODER_DSI_C;
10618
10619 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
10620 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10621 continue;
10622 *power_domain_mask |= BIT(power_domain);
10623
db18b6a6
ID
10624 /*
10625 * The PLL needs to be enabled with a valid divider
10626 * configuration, otherwise accessing DSI registers will hang
10627 * the machine. See BSpec North Display Engine
10628 * registers/MIPI[BXT]. We can break out here early, since we
10629 * need the same DSI PLL to be enabled for both DSI ports.
10630 */
10631 if (!intel_dsi_pll_is_enabled(dev_priv))
10632 break;
10633
4d1de975
JN
10634 /* XXX: this works for video mode only */
10635 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
10636 if (!(tmp & DPI_ENABLE))
10637 continue;
10638
10639 tmp = I915_READ(MIPI_CTRL(port));
10640 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
10641 continue;
10642
10643 pipe_config->cpu_transcoder = cpu_transcoder;
4d1de975
JN
10644 break;
10645 }
10646
d7edc4e5 10647 return transcoder_is_dsi(pipe_config->cpu_transcoder);
4d1de975
JN
10648}
10649
26804afd 10650static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
5cec258b 10651 struct intel_crtc_state *pipe_config)
26804afd
SV
10652{
10653 struct drm_device *dev = crtc->base.dev;
fac5e23e 10654 struct drm_i915_private *dev_priv = to_i915(dev);
d452c5b6 10655 struct intel_shared_dpll *pll;
26804afd
SV
10656 enum port port;
10657 uint32_t tmp;
10658
10659 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
10660
10661 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
10662
0853723b 10663 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
96b7dfb7 10664 skylake_get_ddi_pll(dev_priv, port, pipe_config);
e2d214ae 10665 else if (IS_BROXTON(dev_priv))
3760b59c 10666 bxt_get_ddi_pll(dev_priv, port, pipe_config);
96b7dfb7
S
10667 else
10668 haswell_get_ddi_pll(dev_priv, port, pipe_config);
9cd86933 10669
8106ddbd
ACO
10670 pll = pipe_config->shared_dpll;
10671 if (pll) {
2edd6443
ACO
10672 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
10673 &pipe_config->dpll_hw_state));
d452c5b6
SV
10674 }
10675
26804afd
SV
10676 /*
10677 * Haswell has only FDI/PCH transcoder A. It is which is connected to
10678 * DDI E. So just check whether this pipe is wired to DDI E and whether
10679 * the PCH transcoder is on.
10680 */
ca370455
DL
10681 if (INTEL_INFO(dev)->gen < 9 &&
10682 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
26804afd
SV
10683 pipe_config->has_pch_encoder = true;
10684
10685 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
10686 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
10687 FDI_DP_PORT_WIDTH_SHIFT) + 1;
10688
10689 ironlake_get_fdi_m_n_config(crtc, pipe_config);
10690 }
10691}
10692
0e8ffe1b 10693static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5cec258b 10694 struct intel_crtc_state *pipe_config)
0e8ffe1b
SV
10695{
10696 struct drm_device *dev = crtc->base.dev;
fac5e23e 10697 struct drm_i915_private *dev_priv = to_i915(dev);
1729050e
ID
10698 enum intel_display_power_domain power_domain;
10699 unsigned long power_domain_mask;
cf30429e 10700 bool active;
0e8ffe1b 10701
1729050e
ID
10702 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
10703 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0 10704 return false;
1729050e
ID
10705 power_domain_mask = BIT(power_domain);
10706
8106ddbd 10707 pipe_config->shared_dpll = NULL;
c0d43d62 10708
cf30429e 10709 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
eccb140b 10710
d7edc4e5
VS
10711 if (IS_BROXTON(dev_priv) &&
10712 bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
10713 WARN_ON(active);
10714 active = true;
4d1de975
JN
10715 }
10716
cf30429e 10717 if (!active)
1729050e 10718 goto out;
0e8ffe1b 10719
d7edc4e5 10720 if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
4d1de975
JN
10721 haswell_get_ddi_port_state(crtc, pipe_config);
10722 intel_get_pipe_timings(crtc, pipe_config);
10723 }
627eb5a3 10724
bc58be60 10725 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 10726
05dc698c
LL
10727 pipe_config->gamma_mode =
10728 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
10729
a1b2278e
CK
10730 if (INTEL_INFO(dev)->gen >= 9) {
10731 skl_init_scalers(dev, crtc, pipe_config);
10732 }
10733
af99ceda
CK
10734 if (INTEL_INFO(dev)->gen >= 9) {
10735 pipe_config->scaler_state.scaler_id = -1;
10736 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
10737 }
10738
1729050e
ID
10739 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
10740 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
10741 power_domain_mask |= BIT(power_domain);
1c132b44 10742 if (INTEL_INFO(dev)->gen >= 9)
bd2e244f 10743 skylake_get_pfit_config(crtc, pipe_config);
ff6d9f55 10744 else
1c132b44 10745 ironlake_get_pfit_config(crtc, pipe_config);
bd2e244f 10746 }
88adfff1 10747
772c2a51 10748 if (IS_HASWELL(dev_priv))
e59150dc
JB
10749 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
10750 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 10751
4d1de975
JN
10752 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
10753 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
ebb69c95
CT
10754 pipe_config->pixel_multiplier =
10755 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
10756 } else {
10757 pipe_config->pixel_multiplier = 1;
10758 }
6c49f241 10759
1729050e
ID
10760out:
10761 for_each_power_domain(power_domain, power_domain_mask)
10762 intel_display_power_put(dev_priv, power_domain);
10763
cf30429e 10764 return active;
0e8ffe1b
SV
10765}
10766
55a08b3f
ML
10767static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
10768 const struct intel_plane_state *plane_state)
560b85bb
CW
10769{
10770 struct drm_device *dev = crtc->dev;
fac5e23e 10771 struct drm_i915_private *dev_priv = to_i915(dev);
560b85bb 10772 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
dc41c154 10773 uint32_t cntl = 0, size = 0;
560b85bb 10774
936e71e3 10775 if (plane_state && plane_state->base.visible) {
55a08b3f
ML
10776 unsigned int width = plane_state->base.crtc_w;
10777 unsigned int height = plane_state->base.crtc_h;
dc41c154
VS
10778 unsigned int stride = roundup_pow_of_two(width) * 4;
10779
10780 switch (stride) {
10781 default:
10782 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
10783 width, stride);
10784 stride = 256;
10785 /* fallthrough */
10786 case 256:
10787 case 512:
10788 case 1024:
10789 case 2048:
10790 break;
4b0e333e
CW
10791 }
10792
dc41c154
VS
10793 cntl |= CURSOR_ENABLE |
10794 CURSOR_GAMMA_ENABLE |
10795 CURSOR_FORMAT_ARGB |
10796 CURSOR_STRIDE(stride);
10797
10798 size = (height << 12) | width;
4b0e333e 10799 }
560b85bb 10800
dc41c154
VS
10801 if (intel_crtc->cursor_cntl != 0 &&
10802 (intel_crtc->cursor_base != base ||
10803 intel_crtc->cursor_size != size ||
10804 intel_crtc->cursor_cntl != cntl)) {
10805 /* On these chipsets we can only modify the base/size/stride
10806 * whilst the cursor is disabled.
10807 */
0b87c24e
VS
10808 I915_WRITE(CURCNTR(PIPE_A), 0);
10809 POSTING_READ(CURCNTR(PIPE_A));
dc41c154 10810 intel_crtc->cursor_cntl = 0;
4b0e333e 10811 }
560b85bb 10812
99d1f387 10813 if (intel_crtc->cursor_base != base) {
0b87c24e 10814 I915_WRITE(CURBASE(PIPE_A), base);
99d1f387
VS
10815 intel_crtc->cursor_base = base;
10816 }
4726e0b0 10817
dc41c154
VS
10818 if (intel_crtc->cursor_size != size) {
10819 I915_WRITE(CURSIZE, size);
10820 intel_crtc->cursor_size = size;
4b0e333e 10821 }
560b85bb 10822
4b0e333e 10823 if (intel_crtc->cursor_cntl != cntl) {
0b87c24e
VS
10824 I915_WRITE(CURCNTR(PIPE_A), cntl);
10825 POSTING_READ(CURCNTR(PIPE_A));
4b0e333e 10826 intel_crtc->cursor_cntl = cntl;
560b85bb 10827 }
560b85bb
CW
10828}
10829
55a08b3f
ML
10830static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
10831 const struct intel_plane_state *plane_state)
65a21cd6
JB
10832{
10833 struct drm_device *dev = crtc->dev;
fac5e23e 10834 struct drm_i915_private *dev_priv = to_i915(dev);
65a21cd6 10835 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
62e0fb88 10836 const struct skl_wm_values *wm = &dev_priv->wm.skl_results;
65a21cd6 10837 int pipe = intel_crtc->pipe;
663f3122 10838 uint32_t cntl = 0;
4b0e333e 10839
62e0fb88
L
10840 if (INTEL_GEN(dev_priv) >= 9 && wm->dirty_pipes & drm_crtc_mask(crtc))
10841 skl_write_cursor_wm(intel_crtc, wm);
10842
936e71e3 10843 if (plane_state && plane_state->base.visible) {
4b0e333e 10844 cntl = MCURSOR_GAMMA_ENABLE;
55a08b3f 10845 switch (plane_state->base.crtc_w) {
4726e0b0
SK
10846 case 64:
10847 cntl |= CURSOR_MODE_64_ARGB_AX;
10848 break;
10849 case 128:
10850 cntl |= CURSOR_MODE_128_ARGB_AX;
10851 break;
10852 case 256:
10853 cntl |= CURSOR_MODE_256_ARGB_AX;
10854 break;
10855 default:
55a08b3f 10856 MISSING_CASE(plane_state->base.crtc_w);
4726e0b0 10857 return;
65a21cd6 10858 }
4b0e333e 10859 cntl |= pipe << 28; /* Connect to correct pipe */
47bf17a7 10860
4f8036a2 10861 if (HAS_DDI(dev_priv))
47bf17a7 10862 cntl |= CURSOR_PIPE_CSC_ENABLE;
65a21cd6 10863
31ad61e4 10864 if (plane_state->base.rotation == DRM_ROTATE_180)
55a08b3f
ML
10865 cntl |= CURSOR_ROTATE_180;
10866 }
4398ad45 10867
4b0e333e
CW
10868 if (intel_crtc->cursor_cntl != cntl) {
10869 I915_WRITE(CURCNTR(pipe), cntl);
10870 POSTING_READ(CURCNTR(pipe));
10871 intel_crtc->cursor_cntl = cntl;
65a21cd6 10872 }
4b0e333e 10873
65a21cd6 10874 /* and commit changes on next vblank */
5efb3e28
VS
10875 I915_WRITE(CURBASE(pipe), base);
10876 POSTING_READ(CURBASE(pipe));
99d1f387
VS
10877
10878 intel_crtc->cursor_base = base;
65a21cd6
JB
10879}
10880
cda4b7d3 10881/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6b383a7f 10882static void intel_crtc_update_cursor(struct drm_crtc *crtc,
55a08b3f 10883 const struct intel_plane_state *plane_state)
cda4b7d3
CW
10884{
10885 struct drm_device *dev = crtc->dev;
fac5e23e 10886 struct drm_i915_private *dev_priv = to_i915(dev);
cda4b7d3
CW
10887 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10888 int pipe = intel_crtc->pipe;
55a08b3f
ML
10889 u32 base = intel_crtc->cursor_addr;
10890 u32 pos = 0;
cda4b7d3 10891
55a08b3f
ML
10892 if (plane_state) {
10893 int x = plane_state->base.crtc_x;
10894 int y = plane_state->base.crtc_y;
cda4b7d3 10895
55a08b3f
ML
10896 if (x < 0) {
10897 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10898 x = -x;
10899 }
10900 pos |= x << CURSOR_X_SHIFT;
cda4b7d3 10901
55a08b3f
ML
10902 if (y < 0) {
10903 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10904 y = -y;
10905 }
10906 pos |= y << CURSOR_Y_SHIFT;
10907
10908 /* ILK+ do this automagically */
49cff963 10909 if (HAS_GMCH_DISPLAY(dev_priv) &&
31ad61e4 10910 plane_state->base.rotation == DRM_ROTATE_180) {
55a08b3f
ML
10911 base += (plane_state->base.crtc_h *
10912 plane_state->base.crtc_w - 1) * 4;
10913 }
cda4b7d3 10914 }
cda4b7d3 10915
5efb3e28
VS
10916 I915_WRITE(CURPOS(pipe), pos);
10917
50a0bc90 10918 if (IS_845G(dev_priv) || IS_I865G(dev_priv))
55a08b3f 10919 i845_update_cursor(crtc, base, plane_state);
5efb3e28 10920 else
55a08b3f 10921 i9xx_update_cursor(crtc, base, plane_state);
cda4b7d3
CW
10922}
10923
50a0bc90 10924static bool cursor_size_ok(struct drm_i915_private *dev_priv,
dc41c154
VS
10925 uint32_t width, uint32_t height)
10926{
10927 if (width == 0 || height == 0)
10928 return false;
10929
10930 /*
10931 * 845g/865g are special in that they are only limited by
10932 * the width of their cursors, the height is arbitrary up to
10933 * the precision of the register. Everything else requires
10934 * square cursors, limited to a few power-of-two sizes.
10935 */
50a0bc90 10936 if (IS_845G(dev_priv) || IS_I865G(dev_priv)) {
dc41c154
VS
10937 if ((width & 63) != 0)
10938 return false;
10939
50a0bc90 10940 if (width > (IS_845G(dev_priv) ? 64 : 512))
dc41c154
VS
10941 return false;
10942
10943 if (height > 1023)
10944 return false;
10945 } else {
10946 switch (width | height) {
10947 case 256:
10948 case 128:
50a0bc90 10949 if (IS_GEN2(dev_priv))
dc41c154
VS
10950 return false;
10951 case 64:
10952 break;
10953 default:
10954 return false;
10955 }
10956 }
10957
10958 return true;
10959}
10960
79e53945
JB
10961/* VESA 640x480x72Hz mode to set on the pipe */
10962static struct drm_display_mode load_detect_mode = {
10963 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10964 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10965};
10966
a8bb6818
SV
10967struct drm_framebuffer *
10968__intel_framebuffer_create(struct drm_device *dev,
10969 struct drm_mode_fb_cmd2 *mode_cmd,
10970 struct drm_i915_gem_object *obj)
d2dff872
CW
10971{
10972 struct intel_framebuffer *intel_fb;
10973 int ret;
10974
10975 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
dcb1394e 10976 if (!intel_fb)
d2dff872 10977 return ERR_PTR(-ENOMEM);
d2dff872
CW
10978
10979 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
dd4916c5
SV
10980 if (ret)
10981 goto err;
d2dff872
CW
10982
10983 return &intel_fb->base;
dcb1394e 10984
dd4916c5 10985err:
dd4916c5 10986 kfree(intel_fb);
dd4916c5 10987 return ERR_PTR(ret);
d2dff872
CW
10988}
10989
b5ea642a 10990static struct drm_framebuffer *
a8bb6818
SV
10991intel_framebuffer_create(struct drm_device *dev,
10992 struct drm_mode_fb_cmd2 *mode_cmd,
10993 struct drm_i915_gem_object *obj)
10994{
10995 struct drm_framebuffer *fb;
10996 int ret;
10997
10998 ret = i915_mutex_lock_interruptible(dev);
10999 if (ret)
11000 return ERR_PTR(ret);
11001 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
11002 mutex_unlock(&dev->struct_mutex);
11003
11004 return fb;
11005}
11006
d2dff872
CW
11007static u32
11008intel_framebuffer_pitch_for_width(int width, int bpp)
11009{
11010 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
11011 return ALIGN(pitch, 64);
11012}
11013
11014static u32
11015intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
11016{
11017 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
1267a26b 11018 return PAGE_ALIGN(pitch * mode->vdisplay);
d2dff872
CW
11019}
11020
11021static struct drm_framebuffer *
11022intel_framebuffer_create_for_mode(struct drm_device *dev,
11023 struct drm_display_mode *mode,
11024 int depth, int bpp)
11025{
dcb1394e 11026 struct drm_framebuffer *fb;
d2dff872 11027 struct drm_i915_gem_object *obj;
0fed39bd 11028 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872 11029
d37cd8a8 11030 obj = i915_gem_object_create(dev,
d2dff872 11031 intel_framebuffer_size_for_mode(mode, bpp));
fe3db79b
CW
11032 if (IS_ERR(obj))
11033 return ERR_CAST(obj);
d2dff872
CW
11034
11035 mode_cmd.width = mode->hdisplay;
11036 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
11037 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
11038 bpp);
5ca0c34a 11039 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872 11040
dcb1394e
LW
11041 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
11042 if (IS_ERR(fb))
34911fd3 11043 i915_gem_object_put_unlocked(obj);
dcb1394e
LW
11044
11045 return fb;
d2dff872
CW
11046}
11047
11048static struct drm_framebuffer *
11049mode_fits_in_fbdev(struct drm_device *dev,
11050 struct drm_display_mode *mode)
11051{
0695726e 11052#ifdef CONFIG_DRM_FBDEV_EMULATION
fac5e23e 11053 struct drm_i915_private *dev_priv = to_i915(dev);
d2dff872
CW
11054 struct drm_i915_gem_object *obj;
11055 struct drm_framebuffer *fb;
11056
4c0e5528 11057 if (!dev_priv->fbdev)
d2dff872
CW
11058 return NULL;
11059
4c0e5528 11060 if (!dev_priv->fbdev->fb)
d2dff872
CW
11061 return NULL;
11062
4c0e5528
SV
11063 obj = dev_priv->fbdev->fb->obj;
11064 BUG_ON(!obj);
11065
8bcd4553 11066 fb = &dev_priv->fbdev->fb->base;
01f2c773
VS
11067 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
11068 fb->bits_per_pixel))
d2dff872
CW
11069 return NULL;
11070
01f2c773 11071 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
11072 return NULL;
11073
edde3617 11074 drm_framebuffer_reference(fb);
d2dff872 11075 return fb;
4520f53a
SV
11076#else
11077 return NULL;
11078#endif
d2dff872
CW
11079}
11080
d3a40d1b
ACO
11081static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
11082 struct drm_crtc *crtc,
11083 struct drm_display_mode *mode,
11084 struct drm_framebuffer *fb,
11085 int x, int y)
11086{
11087 struct drm_plane_state *plane_state;
11088 int hdisplay, vdisplay;
11089 int ret;
11090
11091 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
11092 if (IS_ERR(plane_state))
11093 return PTR_ERR(plane_state);
11094
11095 if (mode)
11096 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
11097 else
11098 hdisplay = vdisplay = 0;
11099
11100 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
11101 if (ret)
11102 return ret;
11103 drm_atomic_set_fb_for_plane(plane_state, fb);
11104 plane_state->crtc_x = 0;
11105 plane_state->crtc_y = 0;
11106 plane_state->crtc_w = hdisplay;
11107 plane_state->crtc_h = vdisplay;
11108 plane_state->src_x = x << 16;
11109 plane_state->src_y = y << 16;
11110 plane_state->src_w = hdisplay << 16;
11111 plane_state->src_h = vdisplay << 16;
11112
11113 return 0;
11114}
11115
d2434ab7 11116bool intel_get_load_detect_pipe(struct drm_connector *connector,
7173188d 11117 struct drm_display_mode *mode,
51fd371b
RC
11118 struct intel_load_detect_pipe *old,
11119 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
11120{
11121 struct intel_crtc *intel_crtc;
d2434ab7
SV
11122 struct intel_encoder *intel_encoder =
11123 intel_attached_encoder(connector);
79e53945 11124 struct drm_crtc *possible_crtc;
4ef69c7a 11125 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
11126 struct drm_crtc *crtc = NULL;
11127 struct drm_device *dev = encoder->dev;
94352cf9 11128 struct drm_framebuffer *fb;
51fd371b 11129 struct drm_mode_config *config = &dev->mode_config;
edde3617 11130 struct drm_atomic_state *state = NULL, *restore_state = NULL;
944b0c76 11131 struct drm_connector_state *connector_state;
4be07317 11132 struct intel_crtc_state *crtc_state;
51fd371b 11133 int ret, i = -1;
79e53945 11134
d2dff872 11135 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 11136 connector->base.id, connector->name,
8e329a03 11137 encoder->base.id, encoder->name);
d2dff872 11138
edde3617
ML
11139 old->restore_state = NULL;
11140
51fd371b
RC
11141retry:
11142 ret = drm_modeset_lock(&config->connection_mutex, ctx);
11143 if (ret)
ad3c558f 11144 goto fail;
6e9f798d 11145
79e53945
JB
11146 /*
11147 * Algorithm gets a little messy:
7a5e4805 11148 *
79e53945
JB
11149 * - if the connector already has an assigned crtc, use it (but make
11150 * sure it's on first)
7a5e4805 11151 *
79e53945
JB
11152 * - try to find the first unused crtc that can drive this connector,
11153 * and use that if we find one
79e53945
JB
11154 */
11155
11156 /* See if we already have a CRTC for this connector */
edde3617
ML
11157 if (connector->state->crtc) {
11158 crtc = connector->state->crtc;
8261b191 11159
51fd371b 11160 ret = drm_modeset_lock(&crtc->mutex, ctx);
4d02e2de 11161 if (ret)
ad3c558f 11162 goto fail;
8261b191
CW
11163
11164 /* Make sure the crtc and connector are running */
edde3617 11165 goto found;
79e53945
JB
11166 }
11167
11168 /* Find an unused one (if possible) */
70e1e0ec 11169 for_each_crtc(dev, possible_crtc) {
79e53945
JB
11170 i++;
11171 if (!(encoder->possible_crtcs & (1 << i)))
11172 continue;
edde3617
ML
11173
11174 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
11175 if (ret)
11176 goto fail;
11177
11178 if (possible_crtc->state->enable) {
11179 drm_modeset_unlock(&possible_crtc->mutex);
a459249c 11180 continue;
edde3617 11181 }
a459249c
VS
11182
11183 crtc = possible_crtc;
11184 break;
79e53945
JB
11185 }
11186
11187 /*
11188 * If we didn't find an unused CRTC, don't use any.
11189 */
11190 if (!crtc) {
7173188d 11191 DRM_DEBUG_KMS("no pipe available for load-detect\n");
ad3c558f 11192 goto fail;
79e53945
JB
11193 }
11194
edde3617
ML
11195found:
11196 intel_crtc = to_intel_crtc(crtc);
11197
4d02e2de
SV
11198 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
11199 if (ret)
ad3c558f 11200 goto fail;
79e53945 11201
83a57153 11202 state = drm_atomic_state_alloc(dev);
edde3617
ML
11203 restore_state = drm_atomic_state_alloc(dev);
11204 if (!state || !restore_state) {
11205 ret = -ENOMEM;
11206 goto fail;
11207 }
83a57153
ACO
11208
11209 state->acquire_ctx = ctx;
edde3617 11210 restore_state->acquire_ctx = ctx;
83a57153 11211
944b0c76
ACO
11212 connector_state = drm_atomic_get_connector_state(state, connector);
11213 if (IS_ERR(connector_state)) {
11214 ret = PTR_ERR(connector_state);
11215 goto fail;
11216 }
11217
edde3617
ML
11218 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
11219 if (ret)
11220 goto fail;
944b0c76 11221
4be07317
ACO
11222 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
11223 if (IS_ERR(crtc_state)) {
11224 ret = PTR_ERR(crtc_state);
11225 goto fail;
11226 }
11227
49d6fa21 11228 crtc_state->base.active = crtc_state->base.enable = true;
4be07317 11229
6492711d
CW
11230 if (!mode)
11231 mode = &load_detect_mode;
79e53945 11232
d2dff872
CW
11233 /* We need a framebuffer large enough to accommodate all accesses
11234 * that the plane may generate whilst we perform load detection.
11235 * We can not rely on the fbcon either being present (we get called
11236 * during its initialisation to detect all boot displays, or it may
11237 * not even exist) or that it is large enough to satisfy the
11238 * requested mode.
11239 */
94352cf9
SV
11240 fb = mode_fits_in_fbdev(dev, mode);
11241 if (fb == NULL) {
d2dff872 11242 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9 11243 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
d2dff872
CW
11244 } else
11245 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 11246 if (IS_ERR(fb)) {
d2dff872 11247 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
412b61d8 11248 goto fail;
79e53945 11249 }
79e53945 11250
d3a40d1b
ACO
11251 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
11252 if (ret)
11253 goto fail;
11254
edde3617
ML
11255 drm_framebuffer_unreference(fb);
11256
11257 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
11258 if (ret)
11259 goto fail;
11260
11261 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
11262 if (!ret)
11263 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
11264 if (!ret)
11265 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
11266 if (ret) {
11267 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
11268 goto fail;
11269 }
8c7b5ccb 11270
3ba86073
ML
11271 ret = drm_atomic_commit(state);
11272 if (ret) {
6492711d 11273 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
412b61d8 11274 goto fail;
79e53945 11275 }
edde3617
ML
11276
11277 old->restore_state = restore_state;
7173188d 11278
79e53945 11279 /* let the connector get through one full cycle before testing */
9d0498a2 11280 intel_wait_for_vblank(dev, intel_crtc->pipe);
7173188d 11281 return true;
412b61d8 11282
ad3c558f 11283fail:
e5d958ef 11284 drm_atomic_state_free(state);
edde3617
ML
11285 drm_atomic_state_free(restore_state);
11286 restore_state = state = NULL;
83a57153 11287
51fd371b
RC
11288 if (ret == -EDEADLK) {
11289 drm_modeset_backoff(ctx);
11290 goto retry;
11291 }
11292
412b61d8 11293 return false;
79e53945
JB
11294}
11295
d2434ab7 11296void intel_release_load_detect_pipe(struct drm_connector *connector,
49172fee
ACO
11297 struct intel_load_detect_pipe *old,
11298 struct drm_modeset_acquire_ctx *ctx)
79e53945 11299{
d2434ab7
SV
11300 struct intel_encoder *intel_encoder =
11301 intel_attached_encoder(connector);
4ef69c7a 11302 struct drm_encoder *encoder = &intel_encoder->base;
edde3617 11303 struct drm_atomic_state *state = old->restore_state;
d3a40d1b 11304 int ret;
79e53945 11305
d2dff872 11306 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 11307 connector->base.id, connector->name,
8e329a03 11308 encoder->base.id, encoder->name);
d2dff872 11309
edde3617 11310 if (!state)
0622a53c 11311 return;
79e53945 11312
edde3617
ML
11313 ret = drm_atomic_commit(state);
11314 if (ret) {
11315 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
11316 drm_atomic_state_free(state);
11317 }
79e53945
JB
11318}
11319
da4a1efa 11320static int i9xx_pll_refclk(struct drm_device *dev,
5cec258b 11321 const struct intel_crtc_state *pipe_config)
da4a1efa 11322{
fac5e23e 11323 struct drm_i915_private *dev_priv = to_i915(dev);
da4a1efa
VS
11324 u32 dpll = pipe_config->dpll_hw_state.dpll;
11325
11326 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 11327 return dev_priv->vbt.lvds_ssc_freq;
6e266956 11328 else if (HAS_PCH_SPLIT(dev_priv))
da4a1efa 11329 return 120000;
5db94019 11330 else if (!IS_GEN2(dev_priv))
da4a1efa
VS
11331 return 96000;
11332 else
11333 return 48000;
11334}
11335
79e53945 11336/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc 11337static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 11338 struct intel_crtc_state *pipe_config)
79e53945 11339{
f1f644dc 11340 struct drm_device *dev = crtc->base.dev;
fac5e23e 11341 struct drm_i915_private *dev_priv = to_i915(dev);
f1f644dc 11342 int pipe = pipe_config->cpu_transcoder;
293623f7 11343 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945 11344 u32 fp;
9e2c8475 11345 struct dpll clock;
dccbea3b 11346 int port_clock;
da4a1efa 11347 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
11348
11349 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 11350 fp = pipe_config->dpll_hw_state.fp0;
79e53945 11351 else
293623f7 11352 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
11353
11354 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
f2b115e6
AJ
11355 if (IS_PINEVIEW(dev)) {
11356 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
11357 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
11358 } else {
11359 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
11360 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
11361 }
11362
5db94019 11363 if (!IS_GEN2(dev_priv)) {
f2b115e6
AJ
11364 if (IS_PINEVIEW(dev))
11365 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
11366 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
11367 else
11368 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
11369 DPLL_FPA01_P1_POST_DIV_SHIFT);
11370
11371 switch (dpll & DPLL_MODE_MASK) {
11372 case DPLLB_MODE_DAC_SERIAL:
11373 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
11374 5 : 10;
11375 break;
11376 case DPLLB_MODE_LVDS:
11377 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
11378 7 : 14;
11379 break;
11380 default:
28c97730 11381 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 11382 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 11383 return;
79e53945
JB
11384 }
11385
ac58c3f0 11386 if (IS_PINEVIEW(dev))
dccbea3b 11387 port_clock = pnv_calc_dpll_params(refclk, &clock);
ac58c3f0 11388 else
dccbea3b 11389 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945 11390 } else {
50a0bc90 11391 u32 lvds = IS_I830(dev_priv) ? 0 : I915_READ(LVDS);
b1c560d1 11392 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
11393
11394 if (is_lvds) {
11395 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
11396 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
11397
11398 if (lvds & LVDS_CLKB_POWER_UP)
11399 clock.p2 = 7;
11400 else
11401 clock.p2 = 14;
79e53945
JB
11402 } else {
11403 if (dpll & PLL_P1_DIVIDE_BY_TWO)
11404 clock.p1 = 2;
11405 else {
11406 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
11407 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
11408 }
11409 if (dpll & PLL_P2_DIVIDE_BY_4)
11410 clock.p2 = 4;
11411 else
11412 clock.p2 = 2;
79e53945 11413 }
da4a1efa 11414
dccbea3b 11415 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945
JB
11416 }
11417
18442d08
VS
11418 /*
11419 * This value includes pixel_multiplier. We will use
241bfc38 11420 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
11421 * encoder's get_config() function.
11422 */
dccbea3b 11423 pipe_config->port_clock = port_clock;
f1f644dc
JB
11424}
11425
6878da05
VS
11426int intel_dotclock_calculate(int link_freq,
11427 const struct intel_link_m_n *m_n)
f1f644dc 11428{
f1f644dc
JB
11429 /*
11430 * The calculation for the data clock is:
1041a02f 11431 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 11432 * But we want to avoid losing precison if possible, so:
1041a02f 11433 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
11434 *
11435 * and the link clock is simpler:
1041a02f 11436 * link_clock = (m * link_clock) / n
f1f644dc
JB
11437 */
11438
6878da05
VS
11439 if (!m_n->link_n)
11440 return 0;
f1f644dc 11441
6878da05
VS
11442 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
11443}
f1f644dc 11444
18442d08 11445static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 11446 struct intel_crtc_state *pipe_config)
6878da05 11447{
e3b247da 11448 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
79e53945 11449
18442d08
VS
11450 /* read out port_clock from the DPLL */
11451 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 11452
f1f644dc 11453 /*
e3b247da
VS
11454 * In case there is an active pipe without active ports,
11455 * we may need some idea for the dotclock anyway.
11456 * Calculate one based on the FDI configuration.
79e53945 11457 */
2d112de7 11458 pipe_config->base.adjusted_mode.crtc_clock =
21a727b3 11459 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
18442d08 11460 &pipe_config->fdi_m_n);
79e53945
JB
11461}
11462
11463/** Returns the currently programmed mode of the given pipe. */
11464struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
11465 struct drm_crtc *crtc)
11466{
fac5e23e 11467 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 11468 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 11469 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
79e53945 11470 struct drm_display_mode *mode;
3f36b937 11471 struct intel_crtc_state *pipe_config;
fe2b8f9d
PZ
11472 int htot = I915_READ(HTOTAL(cpu_transcoder));
11473 int hsync = I915_READ(HSYNC(cpu_transcoder));
11474 int vtot = I915_READ(VTOTAL(cpu_transcoder));
11475 int vsync = I915_READ(VSYNC(cpu_transcoder));
293623f7 11476 enum pipe pipe = intel_crtc->pipe;
79e53945
JB
11477
11478 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
11479 if (!mode)
11480 return NULL;
11481
3f36b937
TU
11482 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
11483 if (!pipe_config) {
11484 kfree(mode);
11485 return NULL;
11486 }
11487
f1f644dc
JB
11488 /*
11489 * Construct a pipe_config sufficient for getting the clock info
11490 * back out of crtc_clock_get.
11491 *
11492 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
11493 * to use a real value here instead.
11494 */
3f36b937
TU
11495 pipe_config->cpu_transcoder = (enum transcoder) pipe;
11496 pipe_config->pixel_multiplier = 1;
11497 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
11498 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
11499 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
11500 i9xx_crtc_clock_get(intel_crtc, pipe_config);
11501
11502 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
79e53945
JB
11503 mode->hdisplay = (htot & 0xffff) + 1;
11504 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
11505 mode->hsync_start = (hsync & 0xffff) + 1;
11506 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
11507 mode->vdisplay = (vtot & 0xffff) + 1;
11508 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
11509 mode->vsync_start = (vsync & 0xffff) + 1;
11510 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
11511
11512 drm_mode_set_name(mode);
79e53945 11513
3f36b937
TU
11514 kfree(pipe_config);
11515
79e53945
JB
11516 return mode;
11517}
11518
11519static void intel_crtc_destroy(struct drm_crtc *crtc)
11520{
11521 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
67e77c5a 11522 struct drm_device *dev = crtc->dev;
51cbaf01 11523 struct intel_flip_work *work;
67e77c5a 11524
5e2d7afc 11525 spin_lock_irq(&dev->event_lock);
5a21b665
SV
11526 work = intel_crtc->flip_work;
11527 intel_crtc->flip_work = NULL;
11528 spin_unlock_irq(&dev->event_lock);
67e77c5a 11529
5a21b665 11530 if (work) {
51cbaf01
ML
11531 cancel_work_sync(&work->mmio_work);
11532 cancel_work_sync(&work->unpin_work);
5a21b665 11533 kfree(work);
67e77c5a 11534 }
79e53945
JB
11535
11536 drm_crtc_cleanup(crtc);
67e77c5a 11537
79e53945
JB
11538 kfree(intel_crtc);
11539}
11540
6b95a207
KH
11541static void intel_unpin_work_fn(struct work_struct *__work)
11542{
51cbaf01
ML
11543 struct intel_flip_work *work =
11544 container_of(__work, struct intel_flip_work, unpin_work);
5a21b665
SV
11545 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
11546 struct drm_device *dev = crtc->base.dev;
11547 struct drm_plane *primary = crtc->base.primary;
03f476e1 11548
5a21b665
SV
11549 if (is_mmio_work(work))
11550 flush_work(&work->mmio_work);
03f476e1 11551
5a21b665
SV
11552 mutex_lock(&dev->struct_mutex);
11553 intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
f8c417cd 11554 i915_gem_object_put(work->pending_flip_obj);
5a21b665 11555 mutex_unlock(&dev->struct_mutex);
143f73b3 11556
e8a261ea
CW
11557 i915_gem_request_put(work->flip_queued_req);
11558
5748b6a1
CW
11559 intel_frontbuffer_flip_complete(to_i915(dev),
11560 to_intel_plane(primary)->frontbuffer_bit);
5a21b665
SV
11561 intel_fbc_post_update(crtc);
11562 drm_framebuffer_unreference(work->old_fb);
143f73b3 11563
5a21b665
SV
11564 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
11565 atomic_dec(&crtc->unpin_work_count);
a6747b73 11566
5a21b665
SV
11567 kfree(work);
11568}
d9e86c0e 11569
5a21b665
SV
11570/* Is 'a' after or equal to 'b'? */
11571static bool g4x_flip_count_after_eq(u32 a, u32 b)
11572{
11573 return !((a - b) & 0x80000000);
11574}
143f73b3 11575
5a21b665
SV
11576static bool __pageflip_finished_cs(struct intel_crtc *crtc,
11577 struct intel_flip_work *work)
11578{
11579 struct drm_device *dev = crtc->base.dev;
fac5e23e 11580 struct drm_i915_private *dev_priv = to_i915(dev);
143f73b3 11581
8af29b0c 11582 if (abort_flip_on_reset(crtc))
5a21b665 11583 return true;
143f73b3 11584
5a21b665
SV
11585 /*
11586 * The relevant registers doen't exist on pre-ctg.
11587 * As the flip done interrupt doesn't trigger for mmio
11588 * flips on gmch platforms, a flip count check isn't
11589 * really needed there. But since ctg has the registers,
11590 * include it in the check anyway.
11591 */
9beb5fea 11592 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
5a21b665 11593 return true;
b4a98e57 11594
5a21b665
SV
11595 /*
11596 * BDW signals flip done immediately if the plane
11597 * is disabled, even if the plane enable is already
11598 * armed to occur at the next vblank :(
11599 */
f99d7069 11600
5a21b665
SV
11601 /*
11602 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
11603 * used the same base address. In that case the mmio flip might
11604 * have completed, but the CS hasn't even executed the flip yet.
11605 *
11606 * A flip count check isn't enough as the CS might have updated
11607 * the base address just after start of vblank, but before we
11608 * managed to process the interrupt. This means we'd complete the
11609 * CS flip too soon.
11610 *
11611 * Combining both checks should get us a good enough result. It may
11612 * still happen that the CS flip has been executed, but has not
11613 * yet actually completed. But in case the base address is the same
11614 * anyway, we don't really care.
11615 */
11616 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
11617 crtc->flip_work->gtt_offset &&
11618 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
11619 crtc->flip_work->flip_count);
11620}
b4a98e57 11621
5a21b665
SV
11622static bool
11623__pageflip_finished_mmio(struct intel_crtc *crtc,
11624 struct intel_flip_work *work)
11625{
11626 /*
11627 * MMIO work completes when vblank is different from
11628 * flip_queued_vblank.
11629 *
11630 * Reset counter value doesn't matter, this is handled by
11631 * i915_wait_request finishing early, so no need to handle
11632 * reset here.
11633 */
11634 return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
6b95a207
KH
11635}
11636
51cbaf01
ML
11637
11638static bool pageflip_finished(struct intel_crtc *crtc,
11639 struct intel_flip_work *work)
11640{
11641 if (!atomic_read(&work->pending))
11642 return false;
11643
11644 smp_rmb();
11645
5a21b665
SV
11646 if (is_mmio_work(work))
11647 return __pageflip_finished_mmio(crtc, work);
11648 else
11649 return __pageflip_finished_cs(crtc, work);
11650}
11651
11652void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
11653{
91c8a326 11654 struct drm_device *dev = &dev_priv->drm;
5a21b665
SV
11655 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11656 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11657 struct intel_flip_work *work;
11658 unsigned long flags;
11659
11660 /* Ignore early vblank irqs */
11661 if (!crtc)
11662 return;
11663
51cbaf01 11664 /*
5a21b665
SV
11665 * This is called both by irq handlers and the reset code (to complete
11666 * lost pageflips) so needs the full irqsave spinlocks.
51cbaf01 11667 */
5a21b665
SV
11668 spin_lock_irqsave(&dev->event_lock, flags);
11669 work = intel_crtc->flip_work;
11670
11671 if (work != NULL &&
11672 !is_mmio_work(work) &&
11673 pageflip_finished(intel_crtc, work))
11674 page_flip_completed(intel_crtc);
11675
11676 spin_unlock_irqrestore(&dev->event_lock, flags);
75f7f3ec
VS
11677}
11678
51cbaf01 11679void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
6b95a207 11680{
91c8a326 11681 struct drm_device *dev = &dev_priv->drm;
5251f04e
ML
11682 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11683 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
51cbaf01 11684 struct intel_flip_work *work;
6b95a207
KH
11685 unsigned long flags;
11686
5251f04e
ML
11687 /* Ignore early vblank irqs */
11688 if (!crtc)
11689 return;
f326038a
SV
11690
11691 /*
11692 * This is called both by irq handlers and the reset code (to complete
11693 * lost pageflips) so needs the full irqsave spinlocks.
e7d841ca 11694 */
6b95a207 11695 spin_lock_irqsave(&dev->event_lock, flags);
5a21b665 11696 work = intel_crtc->flip_work;
5251f04e 11697
5a21b665
SV
11698 if (work != NULL &&
11699 is_mmio_work(work) &&
11700 pageflip_finished(intel_crtc, work))
11701 page_flip_completed(intel_crtc);
5251f04e 11702
6b95a207
KH
11703 spin_unlock_irqrestore(&dev->event_lock, flags);
11704}
11705
5a21b665
SV
11706static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
11707 struct intel_flip_work *work)
84c33a64 11708{
5a21b665 11709 work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
84c33a64 11710
5a21b665
SV
11711 /* Ensure that the work item is consistent when activating it ... */
11712 smp_mb__before_atomic();
11713 atomic_set(&work->pending, 1);
11714}
a6747b73 11715
5a21b665
SV
11716static int intel_gen2_queue_flip(struct drm_device *dev,
11717 struct drm_crtc *crtc,
11718 struct drm_framebuffer *fb,
11719 struct drm_i915_gem_object *obj,
11720 struct drm_i915_gem_request *req,
11721 uint32_t flags)
11722{
7e37f889 11723 struct intel_ring *ring = req->ring;
5a21b665
SV
11724 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11725 u32 flip_mask;
11726 int ret;
143f73b3 11727
5a21b665
SV
11728 ret = intel_ring_begin(req, 6);
11729 if (ret)
11730 return ret;
143f73b3 11731
5a21b665
SV
11732 /* Can't queue multiple flips, so wait for the previous
11733 * one to finish before executing the next.
11734 */
11735 if (intel_crtc->plane)
11736 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11737 else
11738 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
b5321f30
CW
11739 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11740 intel_ring_emit(ring, MI_NOOP);
11741 intel_ring_emit(ring, MI_DISPLAY_FLIP |
5a21b665 11742 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
b5321f30
CW
11743 intel_ring_emit(ring, fb->pitches[0]);
11744 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
11745 intel_ring_emit(ring, 0); /* aux display base address, unused */
143f73b3 11746
5a21b665
SV
11747 return 0;
11748}
84c33a64 11749
5a21b665
SV
11750static int intel_gen3_queue_flip(struct drm_device *dev,
11751 struct drm_crtc *crtc,
11752 struct drm_framebuffer *fb,
11753 struct drm_i915_gem_object *obj,
11754 struct drm_i915_gem_request *req,
11755 uint32_t flags)
11756{
7e37f889 11757 struct intel_ring *ring = req->ring;
5a21b665
SV
11758 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11759 u32 flip_mask;
11760 int ret;
d55dbd06 11761
5a21b665
SV
11762 ret = intel_ring_begin(req, 6);
11763 if (ret)
11764 return ret;
d55dbd06 11765
5a21b665
SV
11766 if (intel_crtc->plane)
11767 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11768 else
11769 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
b5321f30
CW
11770 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11771 intel_ring_emit(ring, MI_NOOP);
11772 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
5a21b665 11773 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
b5321f30
CW
11774 intel_ring_emit(ring, fb->pitches[0]);
11775 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
11776 intel_ring_emit(ring, MI_NOOP);
fd8e058a 11777
5a21b665
SV
11778 return 0;
11779}
84c33a64 11780
5a21b665
SV
11781static int intel_gen4_queue_flip(struct drm_device *dev,
11782 struct drm_crtc *crtc,
11783 struct drm_framebuffer *fb,
11784 struct drm_i915_gem_object *obj,
11785 struct drm_i915_gem_request *req,
11786 uint32_t flags)
11787{
7e37f889 11788 struct intel_ring *ring = req->ring;
fac5e23e 11789 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
SV
11790 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11791 uint32_t pf, pipesrc;
11792 int ret;
143f73b3 11793
5a21b665
SV
11794 ret = intel_ring_begin(req, 4);
11795 if (ret)
11796 return ret;
143f73b3 11797
5a21b665
SV
11798 /* i965+ uses the linear or tiled offsets from the
11799 * Display Registers (which do not change across a page-flip)
11800 * so we need only reprogram the base address.
11801 */
b5321f30 11802 intel_ring_emit(ring, MI_DISPLAY_FLIP |
5a21b665 11803 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
b5321f30
CW
11804 intel_ring_emit(ring, fb->pitches[0]);
11805 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset |
72618ebf 11806 intel_fb_modifier_to_tiling(fb->modifier[0]));
5a21b665
SV
11807
11808 /* XXX Enabling the panel-fitter across page-flip is so far
11809 * untested on non-native modes, so ignore it for now.
11810 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11811 */
11812 pf = 0;
11813 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
b5321f30 11814 intel_ring_emit(ring, pf | pipesrc);
143f73b3 11815
5a21b665 11816 return 0;
8c9f3aaf
JB
11817}
11818
5a21b665
SV
11819static int intel_gen6_queue_flip(struct drm_device *dev,
11820 struct drm_crtc *crtc,
11821 struct drm_framebuffer *fb,
11822 struct drm_i915_gem_object *obj,
11823 struct drm_i915_gem_request *req,
11824 uint32_t flags)
da20eabd 11825{
7e37f889 11826 struct intel_ring *ring = req->ring;
fac5e23e 11827 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
SV
11828 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11829 uint32_t pf, pipesrc;
11830 int ret;
d21fbe87 11831
5a21b665
SV
11832 ret = intel_ring_begin(req, 4);
11833 if (ret)
11834 return ret;
92826fcd 11835
b5321f30 11836 intel_ring_emit(ring, MI_DISPLAY_FLIP |
5a21b665 11837 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
72618ebf
VS
11838 intel_ring_emit(ring, fb->pitches[0] |
11839 intel_fb_modifier_to_tiling(fb->modifier[0]));
b5321f30 11840 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
92826fcd 11841
5a21b665
SV
11842 /* Contrary to the suggestions in the documentation,
11843 * "Enable Panel Fitter" does not seem to be required when page
11844 * flipping with a non-native mode, and worse causes a normal
11845 * modeset to fail.
11846 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11847 */
11848 pf = 0;
11849 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
b5321f30 11850 intel_ring_emit(ring, pf | pipesrc);
7809e5ae 11851
5a21b665 11852 return 0;
7809e5ae
MR
11853}
11854
5a21b665
SV
11855static int intel_gen7_queue_flip(struct drm_device *dev,
11856 struct drm_crtc *crtc,
11857 struct drm_framebuffer *fb,
11858 struct drm_i915_gem_object *obj,
11859 struct drm_i915_gem_request *req,
11860 uint32_t flags)
d21fbe87 11861{
5db94019 11862 struct drm_i915_private *dev_priv = to_i915(dev);
7e37f889 11863 struct intel_ring *ring = req->ring;
5a21b665
SV
11864 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11865 uint32_t plane_bit = 0;
11866 int len, ret;
d21fbe87 11867
5a21b665
SV
11868 switch (intel_crtc->plane) {
11869 case PLANE_A:
11870 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11871 break;
11872 case PLANE_B:
11873 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11874 break;
11875 case PLANE_C:
11876 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11877 break;
11878 default:
11879 WARN_ONCE(1, "unknown plane in flip command\n");
11880 return -ENODEV;
11881 }
11882
11883 len = 4;
b5321f30 11884 if (req->engine->id == RCS) {
5a21b665
SV
11885 len += 6;
11886 /*
11887 * On Gen 8, SRM is now taking an extra dword to accommodate
11888 * 48bits addresses, and we need a NOOP for the batch size to
11889 * stay even.
11890 */
5db94019 11891 if (IS_GEN8(dev_priv))
5a21b665
SV
11892 len += 2;
11893 }
11894
11895 /*
11896 * BSpec MI_DISPLAY_FLIP for IVB:
11897 * "The full packet must be contained within the same cache line."
11898 *
11899 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11900 * cacheline, if we ever start emitting more commands before
11901 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11902 * then do the cacheline alignment, and finally emit the
11903 * MI_DISPLAY_FLIP.
11904 */
11905 ret = intel_ring_cacheline_align(req);
11906 if (ret)
11907 return ret;
11908
11909 ret = intel_ring_begin(req, len);
11910 if (ret)
11911 return ret;
11912
11913 /* Unmask the flip-done completion message. Note that the bspec says that
11914 * we should do this for both the BCS and RCS, and that we must not unmask
11915 * more than one flip event at any time (or ensure that one flip message
11916 * can be sent by waiting for flip-done prior to queueing new flips).
11917 * Experimentation says that BCS works despite DERRMR masking all
11918 * flip-done completion events and that unmasking all planes at once
11919 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11920 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11921 */
b5321f30
CW
11922 if (req->engine->id == RCS) {
11923 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
11924 intel_ring_emit_reg(ring, DERRMR);
11925 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
5a21b665
SV
11926 DERRMR_PIPEB_PRI_FLIP_DONE |
11927 DERRMR_PIPEC_PRI_FLIP_DONE));
5db94019 11928 if (IS_GEN8(dev_priv))
b5321f30 11929 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8 |
5a21b665
SV
11930 MI_SRM_LRM_GLOBAL_GTT);
11931 else
b5321f30 11932 intel_ring_emit(ring, MI_STORE_REGISTER_MEM |
5a21b665 11933 MI_SRM_LRM_GLOBAL_GTT);
b5321f30 11934 intel_ring_emit_reg(ring, DERRMR);
bde13ebd
CW
11935 intel_ring_emit(ring,
11936 i915_ggtt_offset(req->engine->scratch) + 256);
5db94019 11937 if (IS_GEN8(dev_priv)) {
b5321f30
CW
11938 intel_ring_emit(ring, 0);
11939 intel_ring_emit(ring, MI_NOOP);
5a21b665
SV
11940 }
11941 }
11942
b5321f30 11943 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
72618ebf
VS
11944 intel_ring_emit(ring, fb->pitches[0] |
11945 intel_fb_modifier_to_tiling(fb->modifier[0]));
b5321f30
CW
11946 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
11947 intel_ring_emit(ring, (MI_NOOP));
5a21b665
SV
11948
11949 return 0;
11950}
11951
11952static bool use_mmio_flip(struct intel_engine_cs *engine,
11953 struct drm_i915_gem_object *obj)
11954{
c37efb99
CW
11955 struct reservation_object *resv;
11956
5a21b665
SV
11957 /*
11958 * This is not being used for older platforms, because
11959 * non-availability of flip done interrupt forces us to use
11960 * CS flips. Older platforms derive flip done using some clever
11961 * tricks involving the flip_pending status bits and vblank irqs.
11962 * So using MMIO flips there would disrupt this mechanism.
11963 */
11964
11965 if (engine == NULL)
11966 return true;
11967
11968 if (INTEL_GEN(engine->i915) < 5)
11969 return false;
11970
11971 if (i915.use_mmio_flip < 0)
11972 return false;
11973 else if (i915.use_mmio_flip > 0)
11974 return true;
11975 else if (i915.enable_execlists)
11976 return true;
c37efb99
CW
11977
11978 resv = i915_gem_object_get_dmabuf_resv(obj);
11979 if (resv && !reservation_object_test_signaled_rcu(resv, false))
5a21b665 11980 return true;
c37efb99 11981
d72d908b
CW
11982 return engine != i915_gem_active_get_engine(&obj->last_write,
11983 &obj->base.dev->struct_mutex);
5a21b665
SV
11984}
11985
11986static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
11987 unsigned int rotation,
11988 struct intel_flip_work *work)
11989{
11990 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 11991 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
SV
11992 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
11993 const enum pipe pipe = intel_crtc->pipe;
d2196774 11994 u32 ctl, stride = skl_plane_stride(fb, 0, rotation);
5a21b665
SV
11995
11996 ctl = I915_READ(PLANE_CTL(pipe, 0));
11997 ctl &= ~PLANE_CTL_TILED_MASK;
11998 switch (fb->modifier[0]) {
11999 case DRM_FORMAT_MOD_NONE:
12000 break;
12001 case I915_FORMAT_MOD_X_TILED:
12002 ctl |= PLANE_CTL_TILED_X;
12003 break;
12004 case I915_FORMAT_MOD_Y_TILED:
12005 ctl |= PLANE_CTL_TILED_Y;
12006 break;
12007 case I915_FORMAT_MOD_Yf_TILED:
12008 ctl |= PLANE_CTL_TILED_YF;
12009 break;
12010 default:
12011 MISSING_CASE(fb->modifier[0]);
12012 }
12013
5a21b665
SV
12014 /*
12015 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
12016 * PLANE_SURF updates, the update is then guaranteed to be atomic.
12017 */
12018 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
12019 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
12020
12021 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
12022 POSTING_READ(PLANE_SURF(pipe, 0));
12023}
12024
12025static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
12026 struct intel_flip_work *work)
12027{
12028 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 12029 struct drm_i915_private *dev_priv = to_i915(dev);
72618ebf 12030 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
5a21b665
SV
12031 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
12032 u32 dspcntr;
12033
12034 dspcntr = I915_READ(reg);
12035
72618ebf 12036 if (fb->modifier[0] == I915_FORMAT_MOD_X_TILED)
5a21b665
SV
12037 dspcntr |= DISPPLANE_TILED;
12038 else
12039 dspcntr &= ~DISPPLANE_TILED;
12040
12041 I915_WRITE(reg, dspcntr);
12042
12043 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
12044 POSTING_READ(DSPSURF(intel_crtc->plane));
12045}
12046
12047static void intel_mmio_flip_work_func(struct work_struct *w)
12048{
12049 struct intel_flip_work *work =
12050 container_of(w, struct intel_flip_work, mmio_work);
12051 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
12052 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
12053 struct intel_framebuffer *intel_fb =
12054 to_intel_framebuffer(crtc->base.primary->fb);
12055 struct drm_i915_gem_object *obj = intel_fb->obj;
c37efb99 12056 struct reservation_object *resv;
5a21b665
SV
12057
12058 if (work->flip_queued_req)
776f3236 12059 WARN_ON(i915_wait_request(work->flip_queued_req,
ea746f36 12060 0, NULL, NO_WAITBOOST));
5a21b665
SV
12061
12062 /* For framebuffer backed by dmabuf, wait for fence */
c37efb99
CW
12063 resv = i915_gem_object_get_dmabuf_resv(obj);
12064 if (resv)
12065 WARN_ON(reservation_object_wait_timeout_rcu(resv, false, false,
5a21b665
SV
12066 MAX_SCHEDULE_TIMEOUT) < 0);
12067
12068 intel_pipe_update_start(crtc);
12069
12070 if (INTEL_GEN(dev_priv) >= 9)
12071 skl_do_mmio_flip(crtc, work->rotation, work);
12072 else
12073 /* use_mmio_flip() retricts MMIO flips to ilk+ */
12074 ilk_do_mmio_flip(crtc, work);
12075
12076 intel_pipe_update_end(crtc, work);
12077}
12078
12079static int intel_default_queue_flip(struct drm_device *dev,
12080 struct drm_crtc *crtc,
12081 struct drm_framebuffer *fb,
12082 struct drm_i915_gem_object *obj,
12083 struct drm_i915_gem_request *req,
12084 uint32_t flags)
12085{
12086 return -ENODEV;
12087}
12088
12089static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
12090 struct intel_crtc *intel_crtc,
12091 struct intel_flip_work *work)
12092{
12093 u32 addr, vblank;
12094
12095 if (!atomic_read(&work->pending))
12096 return false;
12097
12098 smp_rmb();
12099
12100 vblank = intel_crtc_get_vblank_counter(intel_crtc);
12101 if (work->flip_ready_vblank == 0) {
12102 if (work->flip_queued_req &&
f69a02c9 12103 !i915_gem_request_completed(work->flip_queued_req))
5a21b665
SV
12104 return false;
12105
12106 work->flip_ready_vblank = vblank;
12107 }
12108
12109 if (vblank - work->flip_ready_vblank < 3)
12110 return false;
12111
12112 /* Potential stall - if we see that the flip has happened,
12113 * assume a missed interrupt. */
12114 if (INTEL_GEN(dev_priv) >= 4)
12115 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
12116 else
12117 addr = I915_READ(DSPADDR(intel_crtc->plane));
12118
12119 /* There is a potential issue here with a false positive after a flip
12120 * to the same address. We could address this by checking for a
12121 * non-incrementing frame counter.
12122 */
12123 return addr == work->gtt_offset;
12124}
12125
12126void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
12127{
91c8a326 12128 struct drm_device *dev = &dev_priv->drm;
5a21b665
SV
12129 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
12130 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12131 struct intel_flip_work *work;
12132
12133 WARN_ON(!in_interrupt());
12134
12135 if (crtc == NULL)
12136 return;
12137
12138 spin_lock(&dev->event_lock);
12139 work = intel_crtc->flip_work;
12140
12141 if (work != NULL && !is_mmio_work(work) &&
12142 __pageflip_stall_check_cs(dev_priv, intel_crtc, work)) {
12143 WARN_ONCE(1,
12144 "Kicking stuck page flip: queued at %d, now %d\n",
12145 work->flip_queued_vblank, intel_crtc_get_vblank_counter(intel_crtc));
12146 page_flip_completed(intel_crtc);
12147 work = NULL;
12148 }
12149
12150 if (work != NULL && !is_mmio_work(work) &&
12151 intel_crtc_get_vblank_counter(intel_crtc) - work->flip_queued_vblank > 1)
12152 intel_queue_rps_boost_for_request(work->flip_queued_req);
12153 spin_unlock(&dev->event_lock);
12154}
12155
12156static int intel_crtc_page_flip(struct drm_crtc *crtc,
12157 struct drm_framebuffer *fb,
12158 struct drm_pending_vblank_event *event,
12159 uint32_t page_flip_flags)
12160{
12161 struct drm_device *dev = crtc->dev;
fac5e23e 12162 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
SV
12163 struct drm_framebuffer *old_fb = crtc->primary->fb;
12164 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
12165 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12166 struct drm_plane *primary = crtc->primary;
12167 enum pipe pipe = intel_crtc->pipe;
12168 struct intel_flip_work *work;
12169 struct intel_engine_cs *engine;
12170 bool mmio_flip;
8e637178 12171 struct drm_i915_gem_request *request;
058d88c4 12172 struct i915_vma *vma;
5a21b665
SV
12173 int ret;
12174
12175 /*
12176 * drm_mode_page_flip_ioctl() should already catch this, but double
12177 * check to be safe. In the future we may enable pageflipping from
12178 * a disabled primary plane.
12179 */
12180 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
12181 return -EBUSY;
12182
12183 /* Can't change pixel format via MI display flips. */
12184 if (fb->pixel_format != crtc->primary->fb->pixel_format)
12185 return -EINVAL;
12186
12187 /*
12188 * TILEOFF/LINOFF registers can't be changed via MI display flips.
12189 * Note that pitch changes could also affect these register.
12190 */
12191 if (INTEL_INFO(dev)->gen > 3 &&
12192 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
12193 fb->pitches[0] != crtc->primary->fb->pitches[0]))
12194 return -EINVAL;
12195
12196 if (i915_terminally_wedged(&dev_priv->gpu_error))
12197 goto out_hang;
12198
12199 work = kzalloc(sizeof(*work), GFP_KERNEL);
12200 if (work == NULL)
12201 return -ENOMEM;
12202
12203 work->event = event;
12204 work->crtc = crtc;
12205 work->old_fb = old_fb;
12206 INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
12207
12208 ret = drm_crtc_vblank_get(crtc);
12209 if (ret)
12210 goto free_work;
12211
12212 /* We borrow the event spin lock for protecting flip_work */
12213 spin_lock_irq(&dev->event_lock);
12214 if (intel_crtc->flip_work) {
12215 /* Before declaring the flip queue wedged, check if
12216 * the hardware completed the operation behind our backs.
12217 */
12218 if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
12219 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
12220 page_flip_completed(intel_crtc);
12221 } else {
12222 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
12223 spin_unlock_irq(&dev->event_lock);
12224
12225 drm_crtc_vblank_put(crtc);
12226 kfree(work);
12227 return -EBUSY;
12228 }
12229 }
12230 intel_crtc->flip_work = work;
12231 spin_unlock_irq(&dev->event_lock);
12232
12233 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
12234 flush_workqueue(dev_priv->wq);
12235
12236 /* Reference the objects for the scheduled work. */
12237 drm_framebuffer_reference(work->old_fb);
5a21b665
SV
12238
12239 crtc->primary->fb = fb;
12240 update_state_fb(crtc->primary);
faf68d92 12241
25dc556a 12242 work->pending_flip_obj = i915_gem_object_get(obj);
5a21b665
SV
12243
12244 ret = i915_mutex_lock_interruptible(dev);
12245 if (ret)
12246 goto cleanup;
12247
8af29b0c
CW
12248 intel_crtc->reset_count = i915_reset_count(&dev_priv->gpu_error);
12249 if (i915_reset_in_progress_or_wedged(&dev_priv->gpu_error)) {
5a21b665
SV
12250 ret = -EIO;
12251 goto cleanup;
12252 }
12253
12254 atomic_inc(&intel_crtc->unpin_work_count);
12255
9beb5fea 12256 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
5a21b665
SV
12257 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
12258
920a14b2 12259 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
3b3f1650 12260 engine = dev_priv->engine[BCS];
72618ebf 12261 if (fb->modifier[0] != old_fb->modifier[0])
5a21b665
SV
12262 /* vlv: DISPLAY_FLIP fails to change tiling */
12263 engine = NULL;
fd6b8f43 12264 } else if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
3b3f1650 12265 engine = dev_priv->engine[BCS];
5a21b665 12266 } else if (INTEL_INFO(dev)->gen >= 7) {
d72d908b
CW
12267 engine = i915_gem_active_get_engine(&obj->last_write,
12268 &obj->base.dev->struct_mutex);
5a21b665 12269 if (engine == NULL || engine->id != RCS)
3b3f1650 12270 engine = dev_priv->engine[BCS];
5a21b665 12271 } else {
3b3f1650 12272 engine = dev_priv->engine[RCS];
5a21b665
SV
12273 }
12274
12275 mmio_flip = use_mmio_flip(engine, obj);
12276
058d88c4
CW
12277 vma = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
12278 if (IS_ERR(vma)) {
12279 ret = PTR_ERR(vma);
5a21b665 12280 goto cleanup_pending;
058d88c4 12281 }
5a21b665 12282
6687c906 12283 work->gtt_offset = intel_fb_gtt_offset(fb, primary->state->rotation);
5a21b665
SV
12284 work->gtt_offset += intel_crtc->dspaddr_offset;
12285 work->rotation = crtc->primary->state->rotation;
12286
1f061316
PZ
12287 /*
12288 * There's the potential that the next frame will not be compatible with
12289 * FBC, so we want to call pre_update() before the actual page flip.
12290 * The problem is that pre_update() caches some information about the fb
12291 * object, so we want to do this only after the object is pinned. Let's
12292 * be on the safe side and do this immediately before scheduling the
12293 * flip.
12294 */
12295 intel_fbc_pre_update(intel_crtc, intel_crtc->config,
12296 to_intel_plane_state(primary->state));
12297
5a21b665
SV
12298 if (mmio_flip) {
12299 INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
12300
d72d908b
CW
12301 work->flip_queued_req = i915_gem_active_get(&obj->last_write,
12302 &obj->base.dev->struct_mutex);
6277c8d0 12303 queue_work(system_unbound_wq, &work->mmio_work);
5a21b665 12304 } else {
8e637178
CW
12305 request = i915_gem_request_alloc(engine, engine->last_context);
12306 if (IS_ERR(request)) {
12307 ret = PTR_ERR(request);
12308 goto cleanup_unpin;
12309 }
12310
a2bc4695 12311 ret = i915_gem_request_await_object(request, obj, false);
8e637178
CW
12312 if (ret)
12313 goto cleanup_request;
12314
5a21b665
SV
12315 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
12316 page_flip_flags);
12317 if (ret)
8e637178 12318 goto cleanup_request;
5a21b665
SV
12319
12320 intel_mark_page_flip_active(intel_crtc, work);
12321
8e637178 12322 work->flip_queued_req = i915_gem_request_get(request);
5a21b665
SV
12323 i915_add_request_no_flush(request);
12324 }
12325
12326 i915_gem_track_fb(intel_fb_obj(old_fb), obj,
12327 to_intel_plane(primary)->frontbuffer_bit);
12328 mutex_unlock(&dev->struct_mutex);
12329
5748b6a1 12330 intel_frontbuffer_flip_prepare(to_i915(dev),
5a21b665
SV
12331 to_intel_plane(primary)->frontbuffer_bit);
12332
12333 trace_i915_flip_request(intel_crtc->plane, obj);
12334
12335 return 0;
12336
8e637178
CW
12337cleanup_request:
12338 i915_add_request_no_flush(request);
5a21b665
SV
12339cleanup_unpin:
12340 intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
12341cleanup_pending:
5a21b665
SV
12342 atomic_dec(&intel_crtc->unpin_work_count);
12343 mutex_unlock(&dev->struct_mutex);
12344cleanup:
12345 crtc->primary->fb = old_fb;
12346 update_state_fb(crtc->primary);
12347
34911fd3 12348 i915_gem_object_put_unlocked(obj);
5a21b665
SV
12349 drm_framebuffer_unreference(work->old_fb);
12350
12351 spin_lock_irq(&dev->event_lock);
12352 intel_crtc->flip_work = NULL;
12353 spin_unlock_irq(&dev->event_lock);
12354
12355 drm_crtc_vblank_put(crtc);
12356free_work:
12357 kfree(work);
12358
12359 if (ret == -EIO) {
12360 struct drm_atomic_state *state;
12361 struct drm_plane_state *plane_state;
12362
12363out_hang:
12364 state = drm_atomic_state_alloc(dev);
12365 if (!state)
12366 return -ENOMEM;
12367 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
12368
12369retry:
12370 plane_state = drm_atomic_get_plane_state(state, primary);
12371 ret = PTR_ERR_OR_ZERO(plane_state);
12372 if (!ret) {
12373 drm_atomic_set_fb_for_plane(plane_state, fb);
12374
12375 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
12376 if (!ret)
12377 ret = drm_atomic_commit(state);
12378 }
12379
12380 if (ret == -EDEADLK) {
12381 drm_modeset_backoff(state->acquire_ctx);
12382 drm_atomic_state_clear(state);
12383 goto retry;
12384 }
12385
12386 if (ret)
12387 drm_atomic_state_free(state);
12388
12389 if (ret == 0 && event) {
12390 spin_lock_irq(&dev->event_lock);
12391 drm_crtc_send_vblank_event(crtc, event);
12392 spin_unlock_irq(&dev->event_lock);
12393 }
12394 }
12395 return ret;
12396}
12397
12398
12399/**
12400 * intel_wm_need_update - Check whether watermarks need updating
12401 * @plane: drm plane
12402 * @state: new plane state
12403 *
12404 * Check current plane state versus the new one to determine whether
12405 * watermarks need to be recalculated.
12406 *
12407 * Returns true or false.
12408 */
12409static bool intel_wm_need_update(struct drm_plane *plane,
12410 struct drm_plane_state *state)
12411{
12412 struct intel_plane_state *new = to_intel_plane_state(state);
12413 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
12414
12415 /* Update watermarks on tiling or size changes. */
936e71e3 12416 if (new->base.visible != cur->base.visible)
5a21b665
SV
12417 return true;
12418
12419 if (!cur->base.fb || !new->base.fb)
12420 return false;
12421
12422 if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
12423 cur->base.rotation != new->base.rotation ||
936e71e3
VS
12424 drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
12425 drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
12426 drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
12427 drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
5a21b665
SV
12428 return true;
12429
12430 return false;
12431}
12432
12433static bool needs_scaling(struct intel_plane_state *state)
12434{
936e71e3
VS
12435 int src_w = drm_rect_width(&state->base.src) >> 16;
12436 int src_h = drm_rect_height(&state->base.src) >> 16;
12437 int dst_w = drm_rect_width(&state->base.dst);
12438 int dst_h = drm_rect_height(&state->base.dst);
5a21b665
SV
12439
12440 return (src_w != dst_w || src_h != dst_h);
12441}
d21fbe87 12442
da20eabd
ML
12443int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
12444 struct drm_plane_state *plane_state)
12445{
ab1d3a0e 12446 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
da20eabd
ML
12447 struct drm_crtc *crtc = crtc_state->crtc;
12448 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12449 struct drm_plane *plane = plane_state->plane;
12450 struct drm_device *dev = crtc->dev;
ed4a6a7c 12451 struct drm_i915_private *dev_priv = to_i915(dev);
da20eabd
ML
12452 struct intel_plane_state *old_plane_state =
12453 to_intel_plane_state(plane->state);
da20eabd
ML
12454 bool mode_changed = needs_modeset(crtc_state);
12455 bool was_crtc_enabled = crtc->state->active;
12456 bool is_crtc_enabled = crtc_state->active;
da20eabd
ML
12457 bool turn_off, turn_on, visible, was_visible;
12458 struct drm_framebuffer *fb = plane_state->fb;
78108b7c 12459 int ret;
da20eabd 12460
55b8f2a7 12461 if (INTEL_GEN(dev_priv) >= 9 && plane->type != DRM_PLANE_TYPE_CURSOR) {
da20eabd
ML
12462 ret = skl_update_scaler_plane(
12463 to_intel_crtc_state(crtc_state),
12464 to_intel_plane_state(plane_state));
12465 if (ret)
12466 return ret;
12467 }
12468
936e71e3
VS
12469 was_visible = old_plane_state->base.visible;
12470 visible = to_intel_plane_state(plane_state)->base.visible;
da20eabd
ML
12471
12472 if (!was_crtc_enabled && WARN_ON(was_visible))
12473 was_visible = false;
12474
35c08f43
ML
12475 /*
12476 * Visibility is calculated as if the crtc was on, but
12477 * after scaler setup everything depends on it being off
12478 * when the crtc isn't active.
f818ffea
VS
12479 *
12480 * FIXME this is wrong for watermarks. Watermarks should also
12481 * be computed as if the pipe would be active. Perhaps move
12482 * per-plane wm computation to the .check_plane() hook, and
12483 * only combine the results from all planes in the current place?
35c08f43
ML
12484 */
12485 if (!is_crtc_enabled)
936e71e3 12486 to_intel_plane_state(plane_state)->base.visible = visible = false;
da20eabd
ML
12487
12488 if (!was_visible && !visible)
12489 return 0;
12490
e8861675
ML
12491 if (fb != old_plane_state->base.fb)
12492 pipe_config->fb_changed = true;
12493
da20eabd
ML
12494 turn_off = was_visible && (!visible || mode_changed);
12495 turn_on = visible && (!was_visible || mode_changed);
12496
72660ce0 12497 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
78108b7c
VS
12498 intel_crtc->base.base.id,
12499 intel_crtc->base.name,
72660ce0
VS
12500 plane->base.id, plane->name,
12501 fb ? fb->base.id : -1);
da20eabd 12502
72660ce0
VS
12503 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
12504 plane->base.id, plane->name,
12505 was_visible, visible,
da20eabd
ML
12506 turn_off, turn_on, mode_changed);
12507
caed361d
VS
12508 if (turn_on) {
12509 pipe_config->update_wm_pre = true;
12510
12511 /* must disable cxsr around plane enable/disable */
12512 if (plane->type != DRM_PLANE_TYPE_CURSOR)
12513 pipe_config->disable_cxsr = true;
12514 } else if (turn_off) {
12515 pipe_config->update_wm_post = true;
92826fcd 12516
852eb00d 12517 /* must disable cxsr around plane enable/disable */
e8861675 12518 if (plane->type != DRM_PLANE_TYPE_CURSOR)
ab1d3a0e 12519 pipe_config->disable_cxsr = true;
852eb00d 12520 } else if (intel_wm_need_update(plane, plane_state)) {
caed361d
VS
12521 /* FIXME bollocks */
12522 pipe_config->update_wm_pre = true;
12523 pipe_config->update_wm_post = true;
852eb00d 12524 }
da20eabd 12525
ed4a6a7c 12526 /* Pre-gen9 platforms need two-step watermark updates */
caed361d
VS
12527 if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
12528 INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
ed4a6a7c
MR
12529 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
12530
8be6ca85 12531 if (visible || was_visible)
cd202f69 12532 pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
a9ff8714 12533
31ae71fc
ML
12534 /*
12535 * WaCxSRDisabledForSpriteScaling:ivb
12536 *
12537 * cstate->update_wm was already set above, so this flag will
12538 * take effect when we commit and program watermarks.
12539 */
fd6b8f43 12540 if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev_priv) &&
31ae71fc
ML
12541 needs_scaling(to_intel_plane_state(plane_state)) &&
12542 !needs_scaling(old_plane_state))
12543 pipe_config->disable_lp_wm = true;
d21fbe87 12544
da20eabd
ML
12545 return 0;
12546}
12547
6d3a1ce7
ML
12548static bool encoders_cloneable(const struct intel_encoder *a,
12549 const struct intel_encoder *b)
12550{
12551 /* masks could be asymmetric, so check both ways */
12552 return a == b || (a->cloneable & (1 << b->type) &&
12553 b->cloneable & (1 << a->type));
12554}
12555
12556static bool check_single_encoder_cloning(struct drm_atomic_state *state,
12557 struct intel_crtc *crtc,
12558 struct intel_encoder *encoder)
12559{
12560 struct intel_encoder *source_encoder;
12561 struct drm_connector *connector;
12562 struct drm_connector_state *connector_state;
12563 int i;
12564
12565 for_each_connector_in_state(state, connector, connector_state, i) {
12566 if (connector_state->crtc != &crtc->base)
12567 continue;
12568
12569 source_encoder =
12570 to_intel_encoder(connector_state->best_encoder);
12571 if (!encoders_cloneable(encoder, source_encoder))
12572 return false;
12573 }
12574
12575 return true;
12576}
12577
6d3a1ce7
ML
12578static int intel_crtc_atomic_check(struct drm_crtc *crtc,
12579 struct drm_crtc_state *crtc_state)
12580{
cf5a15be 12581 struct drm_device *dev = crtc->dev;
fac5e23e 12582 struct drm_i915_private *dev_priv = to_i915(dev);
6d3a1ce7 12583 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cf5a15be
ML
12584 struct intel_crtc_state *pipe_config =
12585 to_intel_crtc_state(crtc_state);
6d3a1ce7 12586 struct drm_atomic_state *state = crtc_state->state;
4d20cd86 12587 int ret;
6d3a1ce7
ML
12588 bool mode_changed = needs_modeset(crtc_state);
12589
852eb00d 12590 if (mode_changed && !crtc_state->active)
caed361d 12591 pipe_config->update_wm_post = true;
eddfcbcd 12592
ad421372
ML
12593 if (mode_changed && crtc_state->enable &&
12594 dev_priv->display.crtc_compute_clock &&
8106ddbd 12595 !WARN_ON(pipe_config->shared_dpll)) {
ad421372
ML
12596 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
12597 pipe_config);
12598 if (ret)
12599 return ret;
12600 }
12601
82cf435b
LL
12602 if (crtc_state->color_mgmt_changed) {
12603 ret = intel_color_check(crtc, crtc_state);
12604 if (ret)
12605 return ret;
e7852a4b
LL
12606
12607 /*
12608 * Changing color management on Intel hardware is
12609 * handled as part of planes update.
12610 */
12611 crtc_state->planes_changed = true;
82cf435b
LL
12612 }
12613
e435d6e5 12614 ret = 0;
86c8bbbe 12615 if (dev_priv->display.compute_pipe_wm) {
e3bddded 12616 ret = dev_priv->display.compute_pipe_wm(pipe_config);
ed4a6a7c
MR
12617 if (ret) {
12618 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
12619 return ret;
12620 }
12621 }
12622
12623 if (dev_priv->display.compute_intermediate_wm &&
12624 !to_intel_atomic_state(state)->skip_intermediate_wm) {
12625 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
12626 return 0;
12627
12628 /*
12629 * Calculate 'intermediate' watermarks that satisfy both the
12630 * old state and the new state. We can program these
12631 * immediately.
12632 */
12633 ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
12634 intel_crtc,
12635 pipe_config);
12636 if (ret) {
12637 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
86c8bbbe 12638 return ret;
ed4a6a7c 12639 }
e3d5457c
VS
12640 } else if (dev_priv->display.compute_intermediate_wm) {
12641 if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
12642 pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
86c8bbbe
MR
12643 }
12644
e435d6e5
ML
12645 if (INTEL_INFO(dev)->gen >= 9) {
12646 if (mode_changed)
12647 ret = skl_update_scaler_crtc(pipe_config);
12648
12649 if (!ret)
12650 ret = intel_atomic_setup_scalers(dev, intel_crtc,
12651 pipe_config);
12652 }
12653
12654 return ret;
6d3a1ce7
ML
12655}
12656
65b38e0d 12657static const struct drm_crtc_helper_funcs intel_helper_funcs = {
f6e5b160 12658 .mode_set_base_atomic = intel_pipe_set_base_atomic,
5a21b665
SV
12659 .atomic_begin = intel_begin_crtc_commit,
12660 .atomic_flush = intel_finish_crtc_commit,
6d3a1ce7 12661 .atomic_check = intel_crtc_atomic_check,
f6e5b160
CW
12662};
12663
d29b2f9d
ACO
12664static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
12665{
12666 struct intel_connector *connector;
12667
12668 for_each_intel_connector(dev, connector) {
8863dc7f
SV
12669 if (connector->base.state->crtc)
12670 drm_connector_unreference(&connector->base);
12671
d29b2f9d
ACO
12672 if (connector->base.encoder) {
12673 connector->base.state->best_encoder =
12674 connector->base.encoder;
12675 connector->base.state->crtc =
12676 connector->base.encoder->crtc;
8863dc7f
SV
12677
12678 drm_connector_reference(&connector->base);
d29b2f9d
ACO
12679 } else {
12680 connector->base.state->best_encoder = NULL;
12681 connector->base.state->crtc = NULL;
12682 }
12683 }
12684}
12685
050f7aeb 12686static void
eba905b2 12687connected_sink_compute_bpp(struct intel_connector *connector,
5cec258b 12688 struct intel_crtc_state *pipe_config)
050f7aeb 12689{
6a2a5c5d 12690 const struct drm_display_info *info = &connector->base.display_info;
050f7aeb
SV
12691 int bpp = pipe_config->pipe_bpp;
12692
12693 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
6a2a5c5d
VS
12694 connector->base.base.id,
12695 connector->base.name);
050f7aeb
SV
12696
12697 /* Don't use an invalid EDID bpc value */
6a2a5c5d 12698 if (info->bpc != 0 && info->bpc * 3 < bpp) {
050f7aeb 12699 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
6a2a5c5d
VS
12700 bpp, info->bpc * 3);
12701 pipe_config->pipe_bpp = info->bpc * 3;
050f7aeb
SV
12702 }
12703
196f954e 12704 /* Clamp bpp to 8 on screens without EDID 1.4 */
6a2a5c5d 12705 if (info->bpc == 0 && bpp > 24) {
196f954e
MK
12706 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
12707 bpp);
12708 pipe_config->pipe_bpp = 24;
050f7aeb
SV
12709 }
12710}
12711
4e53c2e0 12712static int
050f7aeb 12713compute_baseline_pipe_bpp(struct intel_crtc *crtc,
5cec258b 12714 struct intel_crtc_state *pipe_config)
4e53c2e0 12715{
9beb5fea 12716 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1486017f 12717 struct drm_atomic_state *state;
da3ced29
ACO
12718 struct drm_connector *connector;
12719 struct drm_connector_state *connector_state;
1486017f 12720 int bpp, i;
4e53c2e0 12721
9beb5fea
TU
12722 if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
12723 IS_CHERRYVIEW(dev_priv)))
4e53c2e0 12724 bpp = 10*3;
9beb5fea 12725 else if (INTEL_GEN(dev_priv) >= 5)
d328c9d7
SV
12726 bpp = 12*3;
12727 else
12728 bpp = 8*3;
12729
4e53c2e0 12730
4e53c2e0
SV
12731 pipe_config->pipe_bpp = bpp;
12732
1486017f
ACO
12733 state = pipe_config->base.state;
12734
4e53c2e0 12735 /* Clamp display bpp to EDID value */
da3ced29
ACO
12736 for_each_connector_in_state(state, connector, connector_state, i) {
12737 if (connector_state->crtc != &crtc->base)
4e53c2e0
SV
12738 continue;
12739
da3ced29
ACO
12740 connected_sink_compute_bpp(to_intel_connector(connector),
12741 pipe_config);
4e53c2e0
SV
12742 }
12743
12744 return bpp;
12745}
12746
644db711
SV
12747static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
12748{
12749 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
12750 "type: 0x%x flags: 0x%x\n",
1342830c 12751 mode->crtc_clock,
644db711
SV
12752 mode->crtc_hdisplay, mode->crtc_hsync_start,
12753 mode->crtc_hsync_end, mode->crtc_htotal,
12754 mode->crtc_vdisplay, mode->crtc_vsync_start,
12755 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
12756}
12757
c0b03411 12758static void intel_dump_pipe_config(struct intel_crtc *crtc,
5cec258b 12759 struct intel_crtc_state *pipe_config,
c0b03411
SV
12760 const char *context)
12761{
6a60cd87 12762 struct drm_device *dev = crtc->base.dev;
4f8036a2 12763 struct drm_i915_private *dev_priv = to_i915(dev);
6a60cd87
CK
12764 struct drm_plane *plane;
12765 struct intel_plane *intel_plane;
12766 struct intel_plane_state *state;
12767 struct drm_framebuffer *fb;
12768
78108b7c
VS
12769 DRM_DEBUG_KMS("[CRTC:%d:%s]%s config %p for pipe %c\n",
12770 crtc->base.base.id, crtc->base.name,
6a60cd87 12771 context, pipe_config, pipe_name(crtc->pipe));
c0b03411 12772
da205630 12773 DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
c0b03411
SV
12774 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
12775 pipe_config->pipe_bpp, pipe_config->dither);
12776 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12777 pipe_config->has_pch_encoder,
12778 pipe_config->fdi_lanes,
12779 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
12780 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
12781 pipe_config->fdi_m_n.tu);
90a6b7b0 12782 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
37a5650b 12783 intel_crtc_has_dp_encoder(pipe_config),
90a6b7b0 12784 pipe_config->lane_count,
eb14cb74
VS
12785 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
12786 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
12787 pipe_config->dp_m_n.tu);
b95af8be 12788
90a6b7b0 12789 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
37a5650b 12790 intel_crtc_has_dp_encoder(pipe_config),
90a6b7b0 12791 pipe_config->lane_count,
b95af8be
VK
12792 pipe_config->dp_m2_n2.gmch_m,
12793 pipe_config->dp_m2_n2.gmch_n,
12794 pipe_config->dp_m2_n2.link_m,
12795 pipe_config->dp_m2_n2.link_n,
12796 pipe_config->dp_m2_n2.tu);
12797
55072d19
SV
12798 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12799 pipe_config->has_audio,
12800 pipe_config->has_infoframe);
12801
c0b03411 12802 DRM_DEBUG_KMS("requested mode:\n");
2d112de7 12803 drm_mode_debug_printmodeline(&pipe_config->base.mode);
c0b03411 12804 DRM_DEBUG_KMS("adjusted mode:\n");
2d112de7
ACO
12805 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12806 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
d71b8d4a 12807 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
37327abd
VS
12808 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12809 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
0ec463d3
TU
12810 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12811 crtc->num_scalers,
12812 pipe_config->scaler_state.scaler_users,
12813 pipe_config->scaler_state.scaler_id);
c0b03411
SV
12814 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12815 pipe_config->gmch_pfit.control,
12816 pipe_config->gmch_pfit.pgm_ratios,
12817 pipe_config->gmch_pfit.lvds_border_bits);
fd4daa9c 12818 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
c0b03411 12819 pipe_config->pch_pfit.pos,
fd4daa9c
CW
12820 pipe_config->pch_pfit.size,
12821 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
42db64ef 12822 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
cf532bb2 12823 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
6a60cd87 12824
e2d214ae 12825 if (IS_BROXTON(dev_priv)) {
c856052a 12826 DRM_DEBUG_KMS("dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
415ff0f6 12827 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
c8453338 12828 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
415ff0f6 12829 pipe_config->dpll_hw_state.ebb0,
05712c15 12830 pipe_config->dpll_hw_state.ebb4,
415ff0f6
TU
12831 pipe_config->dpll_hw_state.pll0,
12832 pipe_config->dpll_hw_state.pll1,
12833 pipe_config->dpll_hw_state.pll2,
12834 pipe_config->dpll_hw_state.pll3,
12835 pipe_config->dpll_hw_state.pll6,
12836 pipe_config->dpll_hw_state.pll8,
05712c15 12837 pipe_config->dpll_hw_state.pll9,
c8453338 12838 pipe_config->dpll_hw_state.pll10,
415ff0f6 12839 pipe_config->dpll_hw_state.pcsdw12);
0853723b 12840 } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
c856052a 12841 DRM_DEBUG_KMS("dpll_hw_state: "
415ff0f6 12842 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
415ff0f6
TU
12843 pipe_config->dpll_hw_state.ctrl1,
12844 pipe_config->dpll_hw_state.cfgcr1,
12845 pipe_config->dpll_hw_state.cfgcr2);
4f8036a2 12846 } else if (HAS_DDI(dev_priv)) {
c856052a 12847 DRM_DEBUG_KMS("dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
00490c22
ML
12848 pipe_config->dpll_hw_state.wrpll,
12849 pipe_config->dpll_hw_state.spll);
415ff0f6
TU
12850 } else {
12851 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12852 "fp0: 0x%x, fp1: 0x%x\n",
12853 pipe_config->dpll_hw_state.dpll,
12854 pipe_config->dpll_hw_state.dpll_md,
12855 pipe_config->dpll_hw_state.fp0,
12856 pipe_config->dpll_hw_state.fp1);
12857 }
12858
6a60cd87
CK
12859 DRM_DEBUG_KMS("planes on this crtc\n");
12860 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
d3828147 12861 char *format_name;
6a60cd87
CK
12862 intel_plane = to_intel_plane(plane);
12863 if (intel_plane->pipe != crtc->pipe)
12864 continue;
12865
12866 state = to_intel_plane_state(plane->state);
12867 fb = state->base.fb;
12868 if (!fb) {
1d577e02
VS
12869 DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
12870 plane->base.id, plane->name, state->scaler_id);
6a60cd87
CK
12871 continue;
12872 }
12873
90844f00
EE
12874 format_name = drm_get_format_name(fb->pixel_format);
12875
1d577e02
VS
12876 DRM_DEBUG_KMS("[PLANE:%d:%s] enabled",
12877 plane->base.id, plane->name);
12878 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = %s",
90844f00 12879 fb->base.id, fb->width, fb->height, format_name);
1d577e02
VS
12880 DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
12881 state->scaler_id,
936e71e3
VS
12882 state->base.src.x1 >> 16,
12883 state->base.src.y1 >> 16,
12884 drm_rect_width(&state->base.src) >> 16,
12885 drm_rect_height(&state->base.src) >> 16,
12886 state->base.dst.x1, state->base.dst.y1,
12887 drm_rect_width(&state->base.dst),
12888 drm_rect_height(&state->base.dst));
90844f00
EE
12889
12890 kfree(format_name);
6a60cd87 12891 }
c0b03411
SV
12892}
12893
5448a00d 12894static bool check_digital_port_conflicts(struct drm_atomic_state *state)
00f0b378 12895{
5448a00d 12896 struct drm_device *dev = state->dev;
da3ced29 12897 struct drm_connector *connector;
00f0b378 12898 unsigned int used_ports = 0;
477321e0 12899 unsigned int used_mst_ports = 0;
00f0b378
VS
12900
12901 /*
12902 * Walk the connector list instead of the encoder
12903 * list to detect the problem on ddi platforms
12904 * where there's just one encoder per digital port.
12905 */
0bff4858
VS
12906 drm_for_each_connector(connector, dev) {
12907 struct drm_connector_state *connector_state;
12908 struct intel_encoder *encoder;
12909
12910 connector_state = drm_atomic_get_existing_connector_state(state, connector);
12911 if (!connector_state)
12912 connector_state = connector->state;
12913
5448a00d 12914 if (!connector_state->best_encoder)
00f0b378
VS
12915 continue;
12916
5448a00d
ACO
12917 encoder = to_intel_encoder(connector_state->best_encoder);
12918
12919 WARN_ON(!connector_state->crtc);
00f0b378
VS
12920
12921 switch (encoder->type) {
12922 unsigned int port_mask;
12923 case INTEL_OUTPUT_UNKNOWN:
4f8036a2 12924 if (WARN_ON(!HAS_DDI(to_i915(dev))))
00f0b378 12925 break;
cca0502b 12926 case INTEL_OUTPUT_DP:
00f0b378
VS
12927 case INTEL_OUTPUT_HDMI:
12928 case INTEL_OUTPUT_EDP:
12929 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12930
12931 /* the same port mustn't appear more than once */
12932 if (used_ports & port_mask)
12933 return false;
12934
12935 used_ports |= port_mask;
477321e0
VS
12936 break;
12937 case INTEL_OUTPUT_DP_MST:
12938 used_mst_ports |=
12939 1 << enc_to_mst(&encoder->base)->primary->port;
12940 break;
00f0b378
VS
12941 default:
12942 break;
12943 }
12944 }
12945
477321e0
VS
12946 /* can't mix MST and SST/HDMI on the same port */
12947 if (used_ports & used_mst_ports)
12948 return false;
12949
00f0b378
VS
12950 return true;
12951}
12952
83a57153
ACO
12953static void
12954clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12955{
12956 struct drm_crtc_state tmp_state;
663a3640 12957 struct intel_crtc_scaler_state scaler_state;
4978cc93 12958 struct intel_dpll_hw_state dpll_hw_state;
8106ddbd 12959 struct intel_shared_dpll *shared_dpll;
c4e2d043 12960 bool force_thru;
83a57153 12961
7546a384
ACO
12962 /* FIXME: before the switch to atomic started, a new pipe_config was
12963 * kzalloc'd. Code that depends on any field being zero should be
12964 * fixed, so that the crtc_state can be safely duplicated. For now,
12965 * only fields that are know to not cause problems are preserved. */
12966
83a57153 12967 tmp_state = crtc_state->base;
663a3640 12968 scaler_state = crtc_state->scaler_state;
4978cc93
ACO
12969 shared_dpll = crtc_state->shared_dpll;
12970 dpll_hw_state = crtc_state->dpll_hw_state;
c4e2d043 12971 force_thru = crtc_state->pch_pfit.force_thru;
4978cc93 12972
83a57153 12973 memset(crtc_state, 0, sizeof *crtc_state);
4978cc93 12974
83a57153 12975 crtc_state->base = tmp_state;
663a3640 12976 crtc_state->scaler_state = scaler_state;
4978cc93
ACO
12977 crtc_state->shared_dpll = shared_dpll;
12978 crtc_state->dpll_hw_state = dpll_hw_state;
c4e2d043 12979 crtc_state->pch_pfit.force_thru = force_thru;
83a57153
ACO
12980}
12981
548ee15b 12982static int
b8cecdf5 12983intel_modeset_pipe_config(struct drm_crtc *crtc,
b359283a 12984 struct intel_crtc_state *pipe_config)
ee7b9f93 12985{
b359283a 12986 struct drm_atomic_state *state = pipe_config->base.state;
7758a113 12987 struct intel_encoder *encoder;
da3ced29 12988 struct drm_connector *connector;
0b901879 12989 struct drm_connector_state *connector_state;
d328c9d7 12990 int base_bpp, ret = -EINVAL;
0b901879 12991 int i;
e29c22c0 12992 bool retry = true;
ee7b9f93 12993
83a57153 12994 clear_intel_crtc_state(pipe_config);
7758a113 12995
e143a21c
SV
12996 pipe_config->cpu_transcoder =
12997 (enum transcoder) to_intel_crtc(crtc)->pipe;
b8cecdf5 12998
2960bc9c
ID
12999 /*
13000 * Sanitize sync polarity flags based on requested ones. If neither
13001 * positive or negative polarity is requested, treat this as meaning
13002 * negative polarity.
13003 */
2d112de7 13004 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 13005 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
2d112de7 13006 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
2960bc9c 13007
2d112de7 13008 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 13009 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
2d112de7 13010 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
2960bc9c 13011
d328c9d7
SV
13012 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
13013 pipe_config);
13014 if (base_bpp < 0)
4e53c2e0
SV
13015 goto fail;
13016
e41a56be
VS
13017 /*
13018 * Determine the real pipe dimensions. Note that stereo modes can
13019 * increase the actual pipe size due to the frame doubling and
13020 * insertion of additional space for blanks between the frame. This
13021 * is stored in the crtc timings. We use the requested mode to do this
13022 * computation to clearly distinguish it from the adjusted mode, which
13023 * can be changed by the connectors in the below retry loop.
13024 */
2d112de7 13025 drm_crtc_get_hv_timing(&pipe_config->base.mode,
ecb7e16b
GP
13026 &pipe_config->pipe_src_w,
13027 &pipe_config->pipe_src_h);
e41a56be 13028
253c84c8
VS
13029 for_each_connector_in_state(state, connector, connector_state, i) {
13030 if (connector_state->crtc != crtc)
13031 continue;
13032
13033 encoder = to_intel_encoder(connector_state->best_encoder);
13034
e25148d0
VS
13035 if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
13036 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
13037 goto fail;
13038 }
13039
253c84c8
VS
13040 /*
13041 * Determine output_types before calling the .compute_config()
13042 * hooks so that the hooks can use this information safely.
13043 */
13044 pipe_config->output_types |= 1 << encoder->type;
13045 }
13046
e29c22c0 13047encoder_retry:
ef1b460d 13048 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 13049 pipe_config->port_clock = 0;
ef1b460d 13050 pipe_config->pixel_multiplier = 1;
ff9a6750 13051
135c81b8 13052 /* Fill in default crtc timings, allow encoders to overwrite them. */
2d112de7
ACO
13053 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
13054 CRTC_STEREO_DOUBLE);
135c81b8 13055
7758a113
SV
13056 /* Pass our mode to the connectors and the CRTC to give them a chance to
13057 * adjust it according to limitations or connector properties, and also
13058 * a chance to reject the mode entirely.
47f1c6c9 13059 */
da3ced29 13060 for_each_connector_in_state(state, connector, connector_state, i) {
0b901879 13061 if (connector_state->crtc != crtc)
7758a113 13062 continue;
7ae89233 13063
0b901879
ACO
13064 encoder = to_intel_encoder(connector_state->best_encoder);
13065
0a478c27 13066 if (!(encoder->compute_config(encoder, pipe_config, connector_state))) {
efea6e8e 13067 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
SV
13068 goto fail;
13069 }
ee7b9f93 13070 }
47f1c6c9 13071
ff9a6750
SV
13072 /* Set default port clock if not overwritten by the encoder. Needs to be
13073 * done afterwards in case the encoder adjusts the mode. */
13074 if (!pipe_config->port_clock)
2d112de7 13075 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
241bfc38 13076 * pipe_config->pixel_multiplier;
ff9a6750 13077
a43f6e0f 13078 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 13079 if (ret < 0) {
7758a113
SV
13080 DRM_DEBUG_KMS("CRTC fixup failed\n");
13081 goto fail;
ee7b9f93 13082 }
e29c22c0
SV
13083
13084 if (ret == RETRY) {
13085 if (WARN(!retry, "loop in pipe configuration computation\n")) {
13086 ret = -EINVAL;
13087 goto fail;
13088 }
13089
13090 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
13091 retry = false;
13092 goto encoder_retry;
13093 }
13094
e8fa4270
SV
13095 /* Dithering seems to not pass-through bits correctly when it should, so
13096 * only enable it on 6bpc panels. */
13097 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
62f0ace5 13098 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
d328c9d7 13099 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
4e53c2e0 13100
7758a113 13101fail:
548ee15b 13102 return ret;
ee7b9f93 13103}
47f1c6c9 13104
ea9d758d 13105static void
4740b0f2 13106intel_modeset_update_crtc_state(struct drm_atomic_state *state)
ea9d758d 13107{
0a9ab303
ACO
13108 struct drm_crtc *crtc;
13109 struct drm_crtc_state *crtc_state;
8a75d157 13110 int i;
ea9d758d 13111
7668851f 13112 /* Double check state. */
8a75d157 13113 for_each_crtc_in_state(state, crtc, crtc_state, i) {
3cb480bc 13114 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
fc467a22
ML
13115
13116 /* Update hwmode for vblank functions */
13117 if (crtc->state->active)
13118 crtc->hwmode = crtc->state->adjusted_mode;
13119 else
13120 crtc->hwmode.crtc_clock = 0;
61067a5e
ML
13121
13122 /*
13123 * Update legacy state to satisfy fbc code. This can
13124 * be removed when fbc uses the atomic state.
13125 */
13126 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
13127 struct drm_plane_state *plane_state = crtc->primary->state;
13128
13129 crtc->primary->fb = plane_state->fb;
13130 crtc->x = plane_state->src_x >> 16;
13131 crtc->y = plane_state->src_y >> 16;
13132 }
ea9d758d 13133 }
ea9d758d
SV
13134}
13135
3bd26263 13136static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 13137{
3bd26263 13138 int diff;
f1f644dc
JB
13139
13140 if (clock1 == clock2)
13141 return true;
13142
13143 if (!clock1 || !clock2)
13144 return false;
13145
13146 diff = abs(clock1 - clock2);
13147
13148 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
13149 return true;
13150
13151 return false;
13152}
13153
cfb23ed6
ML
13154static bool
13155intel_compare_m_n(unsigned int m, unsigned int n,
13156 unsigned int m2, unsigned int n2,
13157 bool exact)
13158{
13159 if (m == m2 && n == n2)
13160 return true;
13161
13162 if (exact || !m || !n || !m2 || !n2)
13163 return false;
13164
13165 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
13166
31d10b57
ML
13167 if (n > n2) {
13168 while (n > n2) {
cfb23ed6
ML
13169 m2 <<= 1;
13170 n2 <<= 1;
13171 }
31d10b57
ML
13172 } else if (n < n2) {
13173 while (n < n2) {
cfb23ed6
ML
13174 m <<= 1;
13175 n <<= 1;
13176 }
13177 }
13178
31d10b57
ML
13179 if (n != n2)
13180 return false;
13181
13182 return intel_fuzzy_clock_check(m, m2);
cfb23ed6
ML
13183}
13184
13185static bool
13186intel_compare_link_m_n(const struct intel_link_m_n *m_n,
13187 struct intel_link_m_n *m2_n2,
13188 bool adjust)
13189{
13190 if (m_n->tu == m2_n2->tu &&
13191 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
13192 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
13193 intel_compare_m_n(m_n->link_m, m_n->link_n,
13194 m2_n2->link_m, m2_n2->link_n, !adjust)) {
13195 if (adjust)
13196 *m2_n2 = *m_n;
13197
13198 return true;
13199 }
13200
13201 return false;
13202}
13203
0e8ffe1b 13204static bool
2fa2fe9a 13205intel_pipe_config_compare(struct drm_device *dev,
5cec258b 13206 struct intel_crtc_state *current_config,
cfb23ed6
ML
13207 struct intel_crtc_state *pipe_config,
13208 bool adjust)
0e8ffe1b 13209{
772c2a51 13210 struct drm_i915_private *dev_priv = to_i915(dev);
cfb23ed6
ML
13211 bool ret = true;
13212
13213#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
13214 do { \
13215 if (!adjust) \
13216 DRM_ERROR(fmt, ##__VA_ARGS__); \
13217 else \
13218 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
13219 } while (0)
13220
66e985c0
SV
13221#define PIPE_CONF_CHECK_X(name) \
13222 if (current_config->name != pipe_config->name) { \
cfb23ed6 13223 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
66e985c0
SV
13224 "(expected 0x%08x, found 0x%08x)\n", \
13225 current_config->name, \
13226 pipe_config->name); \
cfb23ed6 13227 ret = false; \
66e985c0
SV
13228 }
13229
08a24034
SV
13230#define PIPE_CONF_CHECK_I(name) \
13231 if (current_config->name != pipe_config->name) { \
cfb23ed6 13232 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
08a24034
SV
13233 "(expected %i, found %i)\n", \
13234 current_config->name, \
13235 pipe_config->name); \
cfb23ed6
ML
13236 ret = false; \
13237 }
13238
8106ddbd
ACO
13239#define PIPE_CONF_CHECK_P(name) \
13240 if (current_config->name != pipe_config->name) { \
13241 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
13242 "(expected %p, found %p)\n", \
13243 current_config->name, \
13244 pipe_config->name); \
13245 ret = false; \
13246 }
13247
cfb23ed6
ML
13248#define PIPE_CONF_CHECK_M_N(name) \
13249 if (!intel_compare_link_m_n(&current_config->name, \
13250 &pipe_config->name,\
13251 adjust)) { \
13252 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
13253 "(expected tu %i gmch %i/%i link %i/%i, " \
13254 "found tu %i, gmch %i/%i link %i/%i)\n", \
13255 current_config->name.tu, \
13256 current_config->name.gmch_m, \
13257 current_config->name.gmch_n, \
13258 current_config->name.link_m, \
13259 current_config->name.link_n, \
13260 pipe_config->name.tu, \
13261 pipe_config->name.gmch_m, \
13262 pipe_config->name.gmch_n, \
13263 pipe_config->name.link_m, \
13264 pipe_config->name.link_n); \
13265 ret = false; \
13266 }
13267
55c561a7
SV
13268/* This is required for BDW+ where there is only one set of registers for
13269 * switching between high and low RR.
13270 * This macro can be used whenever a comparison has to be made between one
13271 * hw state and multiple sw state variables.
13272 */
cfb23ed6
ML
13273#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
13274 if (!intel_compare_link_m_n(&current_config->name, \
13275 &pipe_config->name, adjust) && \
13276 !intel_compare_link_m_n(&current_config->alt_name, \
13277 &pipe_config->name, adjust)) { \
13278 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
13279 "(expected tu %i gmch %i/%i link %i/%i, " \
13280 "or tu %i gmch %i/%i link %i/%i, " \
13281 "found tu %i, gmch %i/%i link %i/%i)\n", \
13282 current_config->name.tu, \
13283 current_config->name.gmch_m, \
13284 current_config->name.gmch_n, \
13285 current_config->name.link_m, \
13286 current_config->name.link_n, \
13287 current_config->alt_name.tu, \
13288 current_config->alt_name.gmch_m, \
13289 current_config->alt_name.gmch_n, \
13290 current_config->alt_name.link_m, \
13291 current_config->alt_name.link_n, \
13292 pipe_config->name.tu, \
13293 pipe_config->name.gmch_m, \
13294 pipe_config->name.gmch_n, \
13295 pipe_config->name.link_m, \
13296 pipe_config->name.link_n); \
13297 ret = false; \
88adfff1
SV
13298 }
13299
1bd1bd80
SV
13300#define PIPE_CONF_CHECK_FLAGS(name, mask) \
13301 if ((current_config->name ^ pipe_config->name) & (mask)) { \
cfb23ed6 13302 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
1bd1bd80
SV
13303 "(expected %i, found %i)\n", \
13304 current_config->name & (mask), \
13305 pipe_config->name & (mask)); \
cfb23ed6 13306 ret = false; \
1bd1bd80
SV
13307 }
13308
5e550656
VS
13309#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
13310 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
cfb23ed6 13311 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
5e550656
VS
13312 "(expected %i, found %i)\n", \
13313 current_config->name, \
13314 pipe_config->name); \
cfb23ed6 13315 ret = false; \
5e550656
VS
13316 }
13317
bb760063
SV
13318#define PIPE_CONF_QUIRK(quirk) \
13319 ((current_config->quirks | pipe_config->quirks) & (quirk))
13320
eccb140b
SV
13321 PIPE_CONF_CHECK_I(cpu_transcoder);
13322
08a24034
SV
13323 PIPE_CONF_CHECK_I(has_pch_encoder);
13324 PIPE_CONF_CHECK_I(fdi_lanes);
cfb23ed6 13325 PIPE_CONF_CHECK_M_N(fdi_m_n);
08a24034 13326
90a6b7b0 13327 PIPE_CONF_CHECK_I(lane_count);
95a7a2ae 13328 PIPE_CONF_CHECK_X(lane_lat_optim_mask);
b95af8be
VK
13329
13330 if (INTEL_INFO(dev)->gen < 8) {
cfb23ed6
ML
13331 PIPE_CONF_CHECK_M_N(dp_m_n);
13332
cfb23ed6
ML
13333 if (current_config->has_drrs)
13334 PIPE_CONF_CHECK_M_N(dp_m2_n2);
13335 } else
13336 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
eb14cb74 13337
253c84c8 13338 PIPE_CONF_CHECK_X(output_types);
a65347ba 13339
2d112de7
ACO
13340 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
13341 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
13342 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
13343 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
13344 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
13345 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
1bd1bd80 13346
2d112de7
ACO
13347 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
13348 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
13349 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
13350 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
13351 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
13352 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
1bd1bd80 13353
c93f54cf 13354 PIPE_CONF_CHECK_I(pixel_multiplier);
6897b4b5 13355 PIPE_CONF_CHECK_I(has_hdmi_sink);
772c2a51 13356 if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
920a14b2 13357 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
b5a9fa09 13358 PIPE_CONF_CHECK_I(limited_color_range);
e43823ec 13359 PIPE_CONF_CHECK_I(has_infoframe);
6c49f241 13360
9ed109a7
SV
13361 PIPE_CONF_CHECK_I(has_audio);
13362
2d112de7 13363 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
1bd1bd80
SV
13364 DRM_MODE_FLAG_INTERLACE);
13365
bb760063 13366 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
2d112de7 13367 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 13368 DRM_MODE_FLAG_PHSYNC);
2d112de7 13369 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 13370 DRM_MODE_FLAG_NHSYNC);
2d112de7 13371 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 13372 DRM_MODE_FLAG_PVSYNC);
2d112de7 13373 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063
SV
13374 DRM_MODE_FLAG_NVSYNC);
13375 }
045ac3b5 13376
333b8ca8 13377 PIPE_CONF_CHECK_X(gmch_pfit.control);
e2ff2d4a
SV
13378 /* pfit ratios are autocomputed by the hw on gen4+ */
13379 if (INTEL_INFO(dev)->gen < 4)
7f7d8dd6 13380 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
333b8ca8 13381 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
9953599b 13382
bfd16b2a
ML
13383 if (!adjust) {
13384 PIPE_CONF_CHECK_I(pipe_src_w);
13385 PIPE_CONF_CHECK_I(pipe_src_h);
13386
13387 PIPE_CONF_CHECK_I(pch_pfit.enabled);
13388 if (current_config->pch_pfit.enabled) {
13389 PIPE_CONF_CHECK_X(pch_pfit.pos);
13390 PIPE_CONF_CHECK_X(pch_pfit.size);
13391 }
2fa2fe9a 13392
7aefe2b5
ML
13393 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
13394 }
a1b2278e 13395
e59150dc 13396 /* BDW+ don't expose a synchronous way to read the state */
772c2a51 13397 if (IS_HASWELL(dev_priv))
e59150dc 13398 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 13399
282740f7
VS
13400 PIPE_CONF_CHECK_I(double_wide);
13401
8106ddbd 13402 PIPE_CONF_CHECK_P(shared_dpll);
66e985c0 13403 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 13404 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
SV
13405 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
13406 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
d452c5b6 13407 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
00490c22 13408 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
3f4cd19f
DL
13409 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
13410 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
13411 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
c0d43d62 13412
47eacbab
VS
13413 PIPE_CONF_CHECK_X(dsi_pll.ctrl);
13414 PIPE_CONF_CHECK_X(dsi_pll.div);
13415
9beb5fea 13416 if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
42571aef
VS
13417 PIPE_CONF_CHECK_I(pipe_bpp);
13418
2d112de7 13419 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
a9a7e98a 13420 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 13421
66e985c0 13422#undef PIPE_CONF_CHECK_X
08a24034 13423#undef PIPE_CONF_CHECK_I
8106ddbd 13424#undef PIPE_CONF_CHECK_P
1bd1bd80 13425#undef PIPE_CONF_CHECK_FLAGS
5e550656 13426#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 13427#undef PIPE_CONF_QUIRK
cfb23ed6 13428#undef INTEL_ERR_OR_DBG_KMS
88adfff1 13429
cfb23ed6 13430 return ret;
0e8ffe1b
SV
13431}
13432
e3b247da
VS
13433static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
13434 const struct intel_crtc_state *pipe_config)
13435{
13436 if (pipe_config->has_pch_encoder) {
21a727b3 13437 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
e3b247da
VS
13438 &pipe_config->fdi_m_n);
13439 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
13440
13441 /*
13442 * FDI already provided one idea for the dotclock.
13443 * Yell if the encoder disagrees.
13444 */
13445 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
13446 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
13447 fdi_dotclock, dotclock);
13448 }
13449}
13450
c0ead703
ML
13451static void verify_wm_state(struct drm_crtc *crtc,
13452 struct drm_crtc_state *new_state)
08db6652 13453{
e7c84544 13454 struct drm_device *dev = crtc->dev;
fac5e23e 13455 struct drm_i915_private *dev_priv = to_i915(dev);
08db6652 13456 struct skl_ddb_allocation hw_ddb, *sw_ddb;
e7c84544
ML
13457 struct skl_ddb_entry *hw_entry, *sw_entry;
13458 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13459 const enum pipe pipe = intel_crtc->pipe;
08db6652
DL
13460 int plane;
13461
e7c84544 13462 if (INTEL_INFO(dev)->gen < 9 || !new_state->active)
08db6652
DL
13463 return;
13464
13465 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
13466 sw_ddb = &dev_priv->wm.skl_hw.ddb;
13467
e7c84544
ML
13468 /* planes */
13469 for_each_plane(dev_priv, pipe, plane) {
13470 hw_entry = &hw_ddb.plane[pipe][plane];
13471 sw_entry = &sw_ddb->plane[pipe][plane];
08db6652 13472
e7c84544 13473 if (skl_ddb_entry_equal(hw_entry, sw_entry))
08db6652
DL
13474 continue;
13475
e7c84544
ML
13476 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
13477 "(expected (%u,%u), found (%u,%u))\n",
13478 pipe_name(pipe), plane + 1,
13479 sw_entry->start, sw_entry->end,
13480 hw_entry->start, hw_entry->end);
13481 }
08db6652 13482
27082493
L
13483 /*
13484 * cursor
13485 * If the cursor plane isn't active, we may not have updated it's ddb
13486 * allocation. In that case since the ddb allocation will be updated
13487 * once the plane becomes visible, we can skip this check
13488 */
13489 if (intel_crtc->cursor_addr) {
13490 hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
13491 sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
13492
13493 if (!skl_ddb_entry_equal(hw_entry, sw_entry)) {
13494 DRM_ERROR("mismatch in DDB state pipe %c cursor "
13495 "(expected (%u,%u), found (%u,%u))\n",
13496 pipe_name(pipe),
13497 sw_entry->start, sw_entry->end,
13498 hw_entry->start, hw_entry->end);
13499 }
08db6652
DL
13500 }
13501}
13502
91d1b4bd 13503static void
c0ead703 13504verify_connector_state(struct drm_device *dev, struct drm_crtc *crtc)
8af6cf88 13505{
35dd3c64 13506 struct drm_connector *connector;
8af6cf88 13507
e7c84544 13508 drm_for_each_connector(connector, dev) {
35dd3c64
ML
13509 struct drm_encoder *encoder = connector->encoder;
13510 struct drm_connector_state *state = connector->state;
ad3c558f 13511
e7c84544
ML
13512 if (state->crtc != crtc)
13513 continue;
13514
5a21b665 13515 intel_connector_verify_state(to_intel_connector(connector));
8af6cf88 13516
ad3c558f 13517 I915_STATE_WARN(state->best_encoder != encoder,
35dd3c64 13518 "connector's atomic encoder doesn't match legacy encoder\n");
8af6cf88 13519 }
91d1b4bd
SV
13520}
13521
13522static void
c0ead703 13523verify_encoder_state(struct drm_device *dev)
91d1b4bd
SV
13524{
13525 struct intel_encoder *encoder;
13526 struct intel_connector *connector;
8af6cf88 13527
b2784e15 13528 for_each_intel_encoder(dev, encoder) {
8af6cf88 13529 bool enabled = false;
4d20cd86 13530 enum pipe pipe;
8af6cf88
SV
13531
13532 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
13533 encoder->base.base.id,
8e329a03 13534 encoder->base.name);
8af6cf88 13535
3a3371ff 13536 for_each_intel_connector(dev, connector) {
4d20cd86 13537 if (connector->base.state->best_encoder != &encoder->base)
8af6cf88
SV
13538 continue;
13539 enabled = true;
ad3c558f
ML
13540
13541 I915_STATE_WARN(connector->base.state->crtc !=
13542 encoder->base.crtc,
13543 "connector's crtc doesn't match encoder crtc\n");
8af6cf88 13544 }
0e32b39c 13545
e2c719b7 13546 I915_STATE_WARN(!!encoder->base.crtc != enabled,
8af6cf88
SV
13547 "encoder's enabled state mismatch "
13548 "(expected %i, found %i)\n",
13549 !!encoder->base.crtc, enabled);
7c60d198
ML
13550
13551 if (!encoder->base.crtc) {
4d20cd86 13552 bool active;
7c60d198 13553
4d20cd86
ML
13554 active = encoder->get_hw_state(encoder, &pipe);
13555 I915_STATE_WARN(active,
13556 "encoder detached but still enabled on pipe %c.\n",
13557 pipe_name(pipe));
7c60d198 13558 }
8af6cf88 13559 }
91d1b4bd
SV
13560}
13561
13562static void
c0ead703
ML
13563verify_crtc_state(struct drm_crtc *crtc,
13564 struct drm_crtc_state *old_crtc_state,
13565 struct drm_crtc_state *new_crtc_state)
91d1b4bd 13566{
e7c84544 13567 struct drm_device *dev = crtc->dev;
fac5e23e 13568 struct drm_i915_private *dev_priv = to_i915(dev);
91d1b4bd 13569 struct intel_encoder *encoder;
e7c84544
ML
13570 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13571 struct intel_crtc_state *pipe_config, *sw_config;
13572 struct drm_atomic_state *old_state;
13573 bool active;
045ac3b5 13574
e7c84544 13575 old_state = old_crtc_state->state;
ec2dc6a0 13576 __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
e7c84544
ML
13577 pipe_config = to_intel_crtc_state(old_crtc_state);
13578 memset(pipe_config, 0, sizeof(*pipe_config));
13579 pipe_config->base.crtc = crtc;
13580 pipe_config->base.state = old_state;
8af6cf88 13581
78108b7c 13582 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
8af6cf88 13583
e7c84544 13584 active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
d62cf62a 13585
e7c84544
ML
13586 /* hw state is inconsistent with the pipe quirk */
13587 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
13588 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
13589 active = new_crtc_state->active;
6c49f241 13590
e7c84544
ML
13591 I915_STATE_WARN(new_crtc_state->active != active,
13592 "crtc active state doesn't match with hw state "
13593 "(expected %i, found %i)\n", new_crtc_state->active, active);
0e8ffe1b 13594
e7c84544
ML
13595 I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
13596 "transitional active state does not match atomic hw state "
13597 "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
4d20cd86 13598
e7c84544
ML
13599 for_each_encoder_on_crtc(dev, crtc, encoder) {
13600 enum pipe pipe;
4d20cd86 13601
e7c84544
ML
13602 active = encoder->get_hw_state(encoder, &pipe);
13603 I915_STATE_WARN(active != new_crtc_state->active,
13604 "[ENCODER:%i] active %i with crtc active %i\n",
13605 encoder->base.base.id, active, new_crtc_state->active);
4d20cd86 13606
e7c84544
ML
13607 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
13608 "Encoder connected to wrong pipe %c\n",
13609 pipe_name(pipe));
4d20cd86 13610
253c84c8
VS
13611 if (active) {
13612 pipe_config->output_types |= 1 << encoder->type;
e7c84544 13613 encoder->get_config(encoder, pipe_config);
253c84c8 13614 }
e7c84544 13615 }
53d9f4e9 13616
e7c84544
ML
13617 if (!new_crtc_state->active)
13618 return;
cfb23ed6 13619
e7c84544 13620 intel_pipe_config_sanity_check(dev_priv, pipe_config);
e3b247da 13621
e7c84544
ML
13622 sw_config = to_intel_crtc_state(crtc->state);
13623 if (!intel_pipe_config_compare(dev, sw_config,
13624 pipe_config, false)) {
13625 I915_STATE_WARN(1, "pipe state doesn't match!\n");
13626 intel_dump_pipe_config(intel_crtc, pipe_config,
13627 "[hw state]");
13628 intel_dump_pipe_config(intel_crtc, sw_config,
13629 "[sw state]");
8af6cf88
SV
13630 }
13631}
13632
91d1b4bd 13633static void
c0ead703
ML
13634verify_single_dpll_state(struct drm_i915_private *dev_priv,
13635 struct intel_shared_dpll *pll,
13636 struct drm_crtc *crtc,
13637 struct drm_crtc_state *new_state)
91d1b4bd 13638{
91d1b4bd 13639 struct intel_dpll_hw_state dpll_hw_state;
e7c84544
ML
13640 unsigned crtc_mask;
13641 bool active;
5358901f 13642
e7c84544 13643 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
5358901f 13644
e7c84544 13645 DRM_DEBUG_KMS("%s\n", pll->name);
5358901f 13646
e7c84544 13647 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
5358901f 13648
e7c84544
ML
13649 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
13650 I915_STATE_WARN(!pll->on && pll->active_mask,
13651 "pll in active use but not on in sw tracking\n");
13652 I915_STATE_WARN(pll->on && !pll->active_mask,
13653 "pll is on but not used by any active crtc\n");
13654 I915_STATE_WARN(pll->on != active,
13655 "pll on state mismatch (expected %i, found %i)\n",
13656 pll->on, active);
13657 }
5358901f 13658
e7c84544 13659 if (!crtc) {
2dd66ebd 13660 I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
e7c84544
ML
13661 "more active pll users than references: %x vs %x\n",
13662 pll->active_mask, pll->config.crtc_mask);
5358901f 13663
e7c84544
ML
13664 return;
13665 }
13666
13667 crtc_mask = 1 << drm_crtc_index(crtc);
13668
13669 if (new_state->active)
13670 I915_STATE_WARN(!(pll->active_mask & crtc_mask),
13671 "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
13672 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
13673 else
13674 I915_STATE_WARN(pll->active_mask & crtc_mask,
13675 "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
13676 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
2dd66ebd 13677
e7c84544
ML
13678 I915_STATE_WARN(!(pll->config.crtc_mask & crtc_mask),
13679 "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
13680 crtc_mask, pll->config.crtc_mask);
66e985c0 13681
e7c84544
ML
13682 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state,
13683 &dpll_hw_state,
13684 sizeof(dpll_hw_state)),
13685 "pll hw state mismatch\n");
13686}
13687
13688static void
c0ead703
ML
13689verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
13690 struct drm_crtc_state *old_crtc_state,
13691 struct drm_crtc_state *new_crtc_state)
e7c84544 13692{
fac5e23e 13693 struct drm_i915_private *dev_priv = to_i915(dev);
e7c84544
ML
13694 struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
13695 struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
13696
13697 if (new_state->shared_dpll)
c0ead703 13698 verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
e7c84544
ML
13699
13700 if (old_state->shared_dpll &&
13701 old_state->shared_dpll != new_state->shared_dpll) {
13702 unsigned crtc_mask = 1 << drm_crtc_index(crtc);
13703 struct intel_shared_dpll *pll = old_state->shared_dpll;
13704
13705 I915_STATE_WARN(pll->active_mask & crtc_mask,
13706 "pll active mismatch (didn't expect pipe %c in active mask)\n",
13707 pipe_name(drm_crtc_index(crtc)));
13708 I915_STATE_WARN(pll->config.crtc_mask & crtc_mask,
13709 "pll enabled crtcs mismatch (found %x in enabled mask)\n",
13710 pipe_name(drm_crtc_index(crtc)));
5358901f 13711 }
8af6cf88
SV
13712}
13713
e7c84544 13714static void
c0ead703 13715intel_modeset_verify_crtc(struct drm_crtc *crtc,
e7c84544
ML
13716 struct drm_crtc_state *old_state,
13717 struct drm_crtc_state *new_state)
13718{
5a21b665
SV
13719 if (!needs_modeset(new_state) &&
13720 !to_intel_crtc_state(new_state)->update_pipe)
13721 return;
13722
c0ead703 13723 verify_wm_state(crtc, new_state);
5a21b665 13724 verify_connector_state(crtc->dev, crtc);
c0ead703
ML
13725 verify_crtc_state(crtc, old_state, new_state);
13726 verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
e7c84544
ML
13727}
13728
13729static void
c0ead703 13730verify_disabled_dpll_state(struct drm_device *dev)
e7c84544 13731{
fac5e23e 13732 struct drm_i915_private *dev_priv = to_i915(dev);
e7c84544
ML
13733 int i;
13734
13735 for (i = 0; i < dev_priv->num_shared_dpll; i++)
c0ead703 13736 verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
e7c84544
ML
13737}
13738
13739static void
c0ead703 13740intel_modeset_verify_disabled(struct drm_device *dev)
e7c84544 13741{
c0ead703
ML
13742 verify_encoder_state(dev);
13743 verify_connector_state(dev, NULL);
13744 verify_disabled_dpll_state(dev);
e7c84544
ML
13745}
13746
80715b2f
VS
13747static void update_scanline_offset(struct intel_crtc *crtc)
13748{
4f8036a2 13749 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
80715b2f
VS
13750
13751 /*
13752 * The scanline counter increments at the leading edge of hsync.
13753 *
13754 * On most platforms it starts counting from vtotal-1 on the
13755 * first active line. That means the scanline counter value is
13756 * always one less than what we would expect. Ie. just after
13757 * start of vblank, which also occurs at start of hsync (on the
13758 * last active line), the scanline counter will read vblank_start-1.
13759 *
13760 * On gen2 the scanline counter starts counting from 1 instead
13761 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
13762 * to keep the value positive), instead of adding one.
13763 *
13764 * On HSW+ the behaviour of the scanline counter depends on the output
13765 * type. For DP ports it behaves like most other platforms, but on HDMI
13766 * there's an extra 1 line difference. So we need to add two instead of
13767 * one to the value.
13768 */
4f8036a2 13769 if (IS_GEN2(dev_priv)) {
124abe07 13770 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
80715b2f
VS
13771 int vtotal;
13772
124abe07
VS
13773 vtotal = adjusted_mode->crtc_vtotal;
13774 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
80715b2f
VS
13775 vtotal /= 2;
13776
13777 crtc->scanline_offset = vtotal - 1;
4f8036a2 13778 } else if (HAS_DDI(dev_priv) &&
2d84d2b3 13779 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
80715b2f
VS
13780 crtc->scanline_offset = 2;
13781 } else
13782 crtc->scanline_offset = 1;
13783}
13784
ad421372 13785static void intel_modeset_clear_plls(struct drm_atomic_state *state)
ed6739ef 13786{
225da59b 13787 struct drm_device *dev = state->dev;
ed6739ef 13788 struct drm_i915_private *dev_priv = to_i915(dev);
ad421372 13789 struct intel_shared_dpll_config *shared_dpll = NULL;
0a9ab303
ACO
13790 struct drm_crtc *crtc;
13791 struct drm_crtc_state *crtc_state;
0a9ab303 13792 int i;
ed6739ef
ACO
13793
13794 if (!dev_priv->display.crtc_compute_clock)
ad421372 13795 return;
ed6739ef 13796
0a9ab303 13797 for_each_crtc_in_state(state, crtc, crtc_state, i) {
fb1a38a9 13798 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8106ddbd
ACO
13799 struct intel_shared_dpll *old_dpll =
13800 to_intel_crtc_state(crtc->state)->shared_dpll;
0a9ab303 13801
fb1a38a9 13802 if (!needs_modeset(crtc_state))
225da59b
ACO
13803 continue;
13804
8106ddbd 13805 to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
fb1a38a9 13806
8106ddbd 13807 if (!old_dpll)
fb1a38a9 13808 continue;
0a9ab303 13809
ad421372
ML
13810 if (!shared_dpll)
13811 shared_dpll = intel_atomic_get_shared_dpll_state(state);
ed6739ef 13812
8106ddbd 13813 intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
ad421372 13814 }
ed6739ef
ACO
13815}
13816
99d736a2
ML
13817/*
13818 * This implements the workaround described in the "notes" section of the mode
13819 * set sequence documentation. When going from no pipes or single pipe to
13820 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13821 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13822 */
13823static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13824{
13825 struct drm_crtc_state *crtc_state;
13826 struct intel_crtc *intel_crtc;
13827 struct drm_crtc *crtc;
13828 struct intel_crtc_state *first_crtc_state = NULL;
13829 struct intel_crtc_state *other_crtc_state = NULL;
13830 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13831 int i;
13832
13833 /* look at all crtc's that are going to be enabled in during modeset */
13834 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13835 intel_crtc = to_intel_crtc(crtc);
13836
13837 if (!crtc_state->active || !needs_modeset(crtc_state))
13838 continue;
13839
13840 if (first_crtc_state) {
13841 other_crtc_state = to_intel_crtc_state(crtc_state);
13842 break;
13843 } else {
13844 first_crtc_state = to_intel_crtc_state(crtc_state);
13845 first_pipe = intel_crtc->pipe;
13846 }
13847 }
13848
13849 /* No workaround needed? */
13850 if (!first_crtc_state)
13851 return 0;
13852
13853 /* w/a possibly needed, check how many crtc's are already enabled. */
13854 for_each_intel_crtc(state->dev, intel_crtc) {
13855 struct intel_crtc_state *pipe_config;
13856
13857 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13858 if (IS_ERR(pipe_config))
13859 return PTR_ERR(pipe_config);
13860
13861 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13862
13863 if (!pipe_config->base.active ||
13864 needs_modeset(&pipe_config->base))
13865 continue;
13866
13867 /* 2 or more enabled crtcs means no need for w/a */
13868 if (enabled_pipe != INVALID_PIPE)
13869 return 0;
13870
13871 enabled_pipe = intel_crtc->pipe;
13872 }
13873
13874 if (enabled_pipe != INVALID_PIPE)
13875 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13876 else if (other_crtc_state)
13877 other_crtc_state->hsw_workaround_pipe = first_pipe;
13878
13879 return 0;
13880}
13881
27c329ed
ML
13882static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13883{
13884 struct drm_crtc *crtc;
13885 struct drm_crtc_state *crtc_state;
13886 int ret = 0;
13887
13888 /* add all active pipes to the state */
13889 for_each_crtc(state->dev, crtc) {
13890 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13891 if (IS_ERR(crtc_state))
13892 return PTR_ERR(crtc_state);
13893
13894 if (!crtc_state->active || needs_modeset(crtc_state))
13895 continue;
13896
13897 crtc_state->mode_changed = true;
13898
13899 ret = drm_atomic_add_affected_connectors(state, crtc);
13900 if (ret)
13901 break;
13902
13903 ret = drm_atomic_add_affected_planes(state, crtc);
13904 if (ret)
13905 break;
13906 }
13907
13908 return ret;
13909}
13910
c347a676 13911static int intel_modeset_checks(struct drm_atomic_state *state)
054518dd 13912{
565602d7 13913 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 13914 struct drm_i915_private *dev_priv = to_i915(state->dev);
565602d7
ML
13915 struct drm_crtc *crtc;
13916 struct drm_crtc_state *crtc_state;
13917 int ret = 0, i;
054518dd 13918
b359283a
ML
13919 if (!check_digital_port_conflicts(state)) {
13920 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13921 return -EINVAL;
13922 }
13923
565602d7
ML
13924 intel_state->modeset = true;
13925 intel_state->active_crtcs = dev_priv->active_crtcs;
13926
13927 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13928 if (crtc_state->active)
13929 intel_state->active_crtcs |= 1 << i;
13930 else
13931 intel_state->active_crtcs &= ~(1 << i);
8b4a7d05
MR
13932
13933 if (crtc_state->active != crtc->state->active)
13934 intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
565602d7
ML
13935 }
13936
054518dd
ACO
13937 /*
13938 * See if the config requires any additional preparation, e.g.
13939 * to adjust global state with pipes off. We need to do this
13940 * here so we can get the modeset_pipe updated config for the new
13941 * mode set on this crtc. For other crtcs we need to use the
13942 * adjusted_mode bits in the crtc directly.
13943 */
27c329ed 13944 if (dev_priv->display.modeset_calc_cdclk) {
c89e39f3 13945 if (!intel_state->cdclk_pll_vco)
63911d72 13946 intel_state->cdclk_pll_vco = dev_priv->cdclk_pll.vco;
b2045352
VS
13947 if (!intel_state->cdclk_pll_vco)
13948 intel_state->cdclk_pll_vco = dev_priv->skl_preferred_vco_freq;
c89e39f3 13949
27c329ed 13950 ret = dev_priv->display.modeset_calc_cdclk(state);
c89e39f3
CT
13951 if (ret < 0)
13952 return ret;
27c329ed 13953
c89e39f3 13954 if (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
63911d72 13955 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco)
27c329ed
ML
13956 ret = intel_modeset_all_pipes(state);
13957
13958 if (ret < 0)
054518dd 13959 return ret;
e8788cbc
ML
13960
13961 DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
13962 intel_state->cdclk, intel_state->dev_cdclk);
27c329ed 13963 } else
1a617b77 13964 to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
054518dd 13965
ad421372 13966 intel_modeset_clear_plls(state);
054518dd 13967
565602d7 13968 if (IS_HASWELL(dev_priv))
ad421372 13969 return haswell_mode_set_planes_workaround(state);
99d736a2 13970
ad421372 13971 return 0;
c347a676
ACO
13972}
13973
aa363136
MR
13974/*
13975 * Handle calculation of various watermark data at the end of the atomic check
13976 * phase. The code here should be run after the per-crtc and per-plane 'check'
13977 * handlers to ensure that all derived state has been updated.
13978 */
55994c2c 13979static int calc_watermark_data(struct drm_atomic_state *state)
aa363136
MR
13980{
13981 struct drm_device *dev = state->dev;
98d39494 13982 struct drm_i915_private *dev_priv = to_i915(dev);
98d39494
MR
13983
13984 /* Is there platform-specific watermark information to calculate? */
13985 if (dev_priv->display.compute_global_watermarks)
55994c2c
MR
13986 return dev_priv->display.compute_global_watermarks(state);
13987
13988 return 0;
aa363136
MR
13989}
13990
74c090b1
ML
13991/**
13992 * intel_atomic_check - validate state object
13993 * @dev: drm device
13994 * @state: state to validate
13995 */
13996static int intel_atomic_check(struct drm_device *dev,
13997 struct drm_atomic_state *state)
c347a676 13998{
dd8b3bdb 13999 struct drm_i915_private *dev_priv = to_i915(dev);
aa363136 14000 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
c347a676
ACO
14001 struct drm_crtc *crtc;
14002 struct drm_crtc_state *crtc_state;
14003 int ret, i;
61333b60 14004 bool any_ms = false;
c347a676 14005
74c090b1 14006 ret = drm_atomic_helper_check_modeset(dev, state);
054518dd
ACO
14007 if (ret)
14008 return ret;
14009
c347a676 14010 for_each_crtc_in_state(state, crtc, crtc_state, i) {
cfb23ed6
ML
14011 struct intel_crtc_state *pipe_config =
14012 to_intel_crtc_state(crtc_state);
1ed51de9
SV
14013
14014 /* Catch I915_MODE_FLAG_INHERITED */
14015 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
14016 crtc_state->mode_changed = true;
cfb23ed6 14017
af4a879e 14018 if (!needs_modeset(crtc_state))
c347a676
ACO
14019 continue;
14020
af4a879e
SV
14021 if (!crtc_state->enable) {
14022 any_ms = true;
cfb23ed6 14023 continue;
af4a879e 14024 }
cfb23ed6 14025
26495481
SV
14026 /* FIXME: For only active_changed we shouldn't need to do any
14027 * state recomputation at all. */
14028
1ed51de9
SV
14029 ret = drm_atomic_add_affected_connectors(state, crtc);
14030 if (ret)
14031 return ret;
b359283a 14032
cfb23ed6 14033 ret = intel_modeset_pipe_config(crtc, pipe_config);
25aa1c39
ML
14034 if (ret) {
14035 intel_dump_pipe_config(to_intel_crtc(crtc),
14036 pipe_config, "[failed]");
c347a676 14037 return ret;
25aa1c39 14038 }
c347a676 14039
73831236 14040 if (i915.fastboot &&
dd8b3bdb 14041 intel_pipe_config_compare(dev,
cfb23ed6 14042 to_intel_crtc_state(crtc->state),
1ed51de9 14043 pipe_config, true)) {
26495481 14044 crtc_state->mode_changed = false;
bfd16b2a 14045 to_intel_crtc_state(crtc_state)->update_pipe = true;
26495481
SV
14046 }
14047
af4a879e 14048 if (needs_modeset(crtc_state))
26495481 14049 any_ms = true;
cfb23ed6 14050
af4a879e
SV
14051 ret = drm_atomic_add_affected_planes(state, crtc);
14052 if (ret)
14053 return ret;
61333b60 14054
26495481
SV
14055 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
14056 needs_modeset(crtc_state) ?
14057 "[modeset]" : "[fastset]");
c347a676
ACO
14058 }
14059
61333b60
ML
14060 if (any_ms) {
14061 ret = intel_modeset_checks(state);
14062
14063 if (ret)
14064 return ret;
27c329ed 14065 } else
dd8b3bdb 14066 intel_state->cdclk = dev_priv->cdclk_freq;
76305b1a 14067
dd8b3bdb 14068 ret = drm_atomic_helper_check_planes(dev, state);
aa363136
MR
14069 if (ret)
14070 return ret;
14071
f51be2e0 14072 intel_fbc_choose_crtc(dev_priv, state);
55994c2c 14073 return calc_watermark_data(state);
054518dd
ACO
14074}
14075
5008e874
ML
14076static int intel_atomic_prepare_commit(struct drm_device *dev,
14077 struct drm_atomic_state *state,
81072bfd 14078 bool nonblock)
5008e874 14079{
fac5e23e 14080 struct drm_i915_private *dev_priv = to_i915(dev);
7580d774 14081 struct drm_plane_state *plane_state;
5008e874 14082 struct drm_crtc_state *crtc_state;
7580d774 14083 struct drm_plane *plane;
5008e874
ML
14084 struct drm_crtc *crtc;
14085 int i, ret;
14086
5a21b665
SV
14087 for_each_crtc_in_state(state, crtc, crtc_state, i) {
14088 if (state->legacy_cursor_update)
a6747b73
ML
14089 continue;
14090
5a21b665
SV
14091 ret = intel_crtc_wait_for_pending_flips(crtc);
14092 if (ret)
14093 return ret;
5008e874 14094
5a21b665
SV
14095 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
14096 flush_workqueue(dev_priv->wq);
d55dbd06
ML
14097 }
14098
f935675f
ML
14099 ret = mutex_lock_interruptible(&dev->struct_mutex);
14100 if (ret)
14101 return ret;
14102
5008e874 14103 ret = drm_atomic_helper_prepare_planes(dev, state);
f7e5838b 14104 mutex_unlock(&dev->struct_mutex);
7580d774 14105
21daaeee 14106 if (!ret && !nonblock) {
7580d774
ML
14107 for_each_plane_in_state(state, plane, plane_state, i) {
14108 struct intel_plane_state *intel_plane_state =
14109 to_intel_plane_state(plane_state);
14110
14111 if (!intel_plane_state->wait_req)
14112 continue;
14113
776f3236 14114 ret = i915_wait_request(intel_plane_state->wait_req,
ea746f36
CW
14115 I915_WAIT_INTERRUPTIBLE,
14116 NULL, NULL);
f7e5838b 14117 if (ret) {
f4457ae7
CW
14118 /* Any hang should be swallowed by the wait */
14119 WARN_ON(ret == -EIO);
f7e5838b
CW
14120 mutex_lock(&dev->struct_mutex);
14121 drm_atomic_helper_cleanup_planes(dev, state);
14122 mutex_unlock(&dev->struct_mutex);
7580d774 14123 break;
f7e5838b 14124 }
7580d774 14125 }
7580d774 14126 }
5008e874
ML
14127
14128 return ret;
14129}
14130
a2991414
ML
14131u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
14132{
14133 struct drm_device *dev = crtc->base.dev;
14134
14135 if (!dev->max_vblank_count)
14136 return drm_accurate_vblank_count(&crtc->base);
14137
14138 return dev->driver->get_vblank_counter(dev, crtc->pipe);
14139}
14140
5a21b665
SV
14141static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
14142 struct drm_i915_private *dev_priv,
14143 unsigned crtc_mask)
e8861675 14144{
5a21b665
SV
14145 unsigned last_vblank_count[I915_MAX_PIPES];
14146 enum pipe pipe;
14147 int ret;
e8861675 14148
5a21b665
SV
14149 if (!crtc_mask)
14150 return;
e8861675 14151
5a21b665
SV
14152 for_each_pipe(dev_priv, pipe) {
14153 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
e8861675 14154
5a21b665 14155 if (!((1 << pipe) & crtc_mask))
e8861675
ML
14156 continue;
14157
5a21b665
SV
14158 ret = drm_crtc_vblank_get(crtc);
14159 if (WARN_ON(ret != 0)) {
14160 crtc_mask &= ~(1 << pipe);
14161 continue;
e8861675
ML
14162 }
14163
5a21b665 14164 last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
e8861675
ML
14165 }
14166
5a21b665
SV
14167 for_each_pipe(dev_priv, pipe) {
14168 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
14169 long lret;
e8861675 14170
5a21b665
SV
14171 if (!((1 << pipe) & crtc_mask))
14172 continue;
d55dbd06 14173
5a21b665
SV
14174 lret = wait_event_timeout(dev->vblank[pipe].queue,
14175 last_vblank_count[pipe] !=
14176 drm_crtc_vblank_count(crtc),
14177 msecs_to_jiffies(50));
d55dbd06 14178
5a21b665 14179 WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
d55dbd06 14180
5a21b665 14181 drm_crtc_vblank_put(crtc);
d55dbd06
ML
14182 }
14183}
14184
5a21b665 14185static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
a6747b73 14186{
5a21b665
SV
14187 /* fb updated, need to unpin old fb */
14188 if (crtc_state->fb_changed)
14189 return true;
a6747b73 14190
5a21b665
SV
14191 /* wm changes, need vblank before final wm's */
14192 if (crtc_state->update_wm_post)
14193 return true;
a6747b73 14194
5a21b665
SV
14195 /*
14196 * cxsr is re-enabled after vblank.
14197 * This is already handled by crtc_state->update_wm_post,
14198 * but added for clarity.
14199 */
14200 if (crtc_state->disable_cxsr)
14201 return true;
a6747b73 14202
5a21b665 14203 return false;
e8861675
ML
14204}
14205
896e5bb0
L
14206static void intel_update_crtc(struct drm_crtc *crtc,
14207 struct drm_atomic_state *state,
14208 struct drm_crtc_state *old_crtc_state,
14209 unsigned int *crtc_vblank_mask)
14210{
14211 struct drm_device *dev = crtc->dev;
14212 struct drm_i915_private *dev_priv = to_i915(dev);
14213 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14214 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc->state);
14215 bool modeset = needs_modeset(crtc->state);
14216
14217 if (modeset) {
14218 update_scanline_offset(intel_crtc);
14219 dev_priv->display.crtc_enable(pipe_config, state);
14220 } else {
14221 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
14222 }
14223
14224 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
14225 intel_fbc_enable(
14226 intel_crtc, pipe_config,
14227 to_intel_plane_state(crtc->primary->state));
14228 }
14229
14230 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
14231
14232 if (needs_vblank_wait(pipe_config))
14233 *crtc_vblank_mask |= drm_crtc_mask(crtc);
14234}
14235
14236static void intel_update_crtcs(struct drm_atomic_state *state,
14237 unsigned int *crtc_vblank_mask)
14238{
14239 struct drm_crtc *crtc;
14240 struct drm_crtc_state *old_crtc_state;
14241 int i;
14242
14243 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
14244 if (!crtc->state->active)
14245 continue;
14246
14247 intel_update_crtc(crtc, state, old_crtc_state,
14248 crtc_vblank_mask);
14249 }
14250}
14251
27082493
L
14252static void skl_update_crtcs(struct drm_atomic_state *state,
14253 unsigned int *crtc_vblank_mask)
14254{
14255 struct drm_device *dev = state->dev;
27082493
L
14256 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
14257 struct drm_crtc *crtc;
ce0ba283 14258 struct intel_crtc *intel_crtc;
27082493 14259 struct drm_crtc_state *old_crtc_state;
ce0ba283 14260 struct intel_crtc_state *cstate;
27082493
L
14261 unsigned int updated = 0;
14262 bool progress;
14263 enum pipe pipe;
14264
14265 /*
14266 * Whenever the number of active pipes changes, we need to make sure we
14267 * update the pipes in the right order so that their ddb allocations
14268 * never overlap with eachother inbetween CRTC updates. Otherwise we'll
14269 * cause pipe underruns and other bad stuff.
14270 */
14271 do {
14272 int i;
14273 progress = false;
14274
14275 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
14276 bool vbl_wait = false;
14277 unsigned int cmask = drm_crtc_mask(crtc);
ce0ba283
L
14278
14279 intel_crtc = to_intel_crtc(crtc);
14280 cstate = to_intel_crtc_state(crtc->state);
14281 pipe = intel_crtc->pipe;
27082493
L
14282
14283 if (updated & cmask || !crtc->state->active)
14284 continue;
ce0ba283 14285 if (skl_ddb_allocation_overlaps(state, intel_crtc))
27082493
L
14286 continue;
14287
14288 updated |= cmask;
14289
14290 /*
14291 * If this is an already active pipe, it's DDB changed,
14292 * and this isn't the last pipe that needs updating
14293 * then we need to wait for a vblank to pass for the
14294 * new ddb allocation to take effect.
14295 */
ce0ba283
L
14296 if (!skl_ddb_entry_equal(&cstate->wm.skl.ddb,
14297 &intel_crtc->hw_ddb) &&
27082493
L
14298 !crtc->state->active_changed &&
14299 intel_state->wm_results.dirty_pipes != updated)
14300 vbl_wait = true;
14301
14302 intel_update_crtc(crtc, state, old_crtc_state,
14303 crtc_vblank_mask);
14304
14305 if (vbl_wait)
14306 intel_wait_for_vblank(dev, pipe);
14307
14308 progress = true;
14309 }
14310 } while (progress);
14311}
14312
94f05024 14313static void intel_atomic_commit_tail(struct drm_atomic_state *state)
a6778b3c 14314{
94f05024 14315 struct drm_device *dev = state->dev;
565602d7 14316 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 14317 struct drm_i915_private *dev_priv = to_i915(dev);
29ceb0e6 14318 struct drm_crtc_state *old_crtc_state;
7580d774 14319 struct drm_crtc *crtc;
5a21b665 14320 struct intel_crtc_state *intel_cstate;
94f05024
SV
14321 struct drm_plane *plane;
14322 struct drm_plane_state *plane_state;
5a21b665
SV
14323 bool hw_check = intel_state->modeset;
14324 unsigned long put_domains[I915_MAX_PIPES] = {};
14325 unsigned crtc_vblank_mask = 0;
94f05024 14326 int i, ret;
a6778b3c 14327
94f05024
SV
14328 for_each_plane_in_state(state, plane, plane_state, i) {
14329 struct intel_plane_state *intel_plane_state =
14330 to_intel_plane_state(plane_state);
ea0000f0 14331
94f05024
SV
14332 if (!intel_plane_state->wait_req)
14333 continue;
d4afb8cc 14334
776f3236 14335 ret = i915_wait_request(intel_plane_state->wait_req,
ea746f36 14336 0, NULL, NULL);
94f05024
SV
14337 /* EIO should be eaten, and we can't get interrupted in the
14338 * worker, and blocking commits have waited already. */
14339 WARN_ON(ret);
14340 }
1c5e19f8 14341
ea0000f0
SV
14342 drm_atomic_helper_wait_for_dependencies(state);
14343
565602d7
ML
14344 if (intel_state->modeset) {
14345 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
14346 sizeof(intel_state->min_pixclk));
14347 dev_priv->active_crtcs = intel_state->active_crtcs;
1a617b77 14348 dev_priv->atomic_cdclk_freq = intel_state->cdclk;
5a21b665
SV
14349
14350 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
565602d7
ML
14351 }
14352
29ceb0e6 14353 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
a539205a
ML
14354 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14355
5a21b665
SV
14356 if (needs_modeset(crtc->state) ||
14357 to_intel_crtc_state(crtc->state)->update_pipe) {
14358 hw_check = true;
14359
14360 put_domains[to_intel_crtc(crtc)->pipe] =
14361 modeset_get_crtc_power_domains(crtc,
14362 to_intel_crtc_state(crtc->state));
14363 }
14364
61333b60
ML
14365 if (!needs_modeset(crtc->state))
14366 continue;
14367
29ceb0e6 14368 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
460da916 14369
29ceb0e6
VS
14370 if (old_crtc_state->active) {
14371 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
4a806558 14372 dev_priv->display.crtc_disable(to_intel_crtc_state(old_crtc_state), state);
eddfcbcd 14373 intel_crtc->active = false;
58f9c0bc 14374 intel_fbc_disable(intel_crtc);
eddfcbcd 14375 intel_disable_shared_dpll(intel_crtc);
9bbc8258
VS
14376
14377 /*
14378 * Underruns don't always raise
14379 * interrupts, so check manually.
14380 */
14381 intel_check_cpu_fifo_underruns(dev_priv);
14382 intel_check_pch_fifo_underruns(dev_priv);
b9001114
ML
14383
14384 if (!crtc->state->active)
14385 intel_update_watermarks(crtc);
a539205a 14386 }
b8cecdf5 14387 }
7758a113 14388
ea9d758d
SV
14389 /* Only after disabling all output pipelines that will be changed can we
14390 * update the the output configuration. */
4740b0f2 14391 intel_modeset_update_crtc_state(state);
f6e5b160 14392
565602d7 14393 if (intel_state->modeset) {
4740b0f2 14394 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
33c8df89
ML
14395
14396 if (dev_priv->display.modeset_commit_cdclk &&
c89e39f3 14397 (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
63911d72 14398 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco))
33c8df89 14399 dev_priv->display.modeset_commit_cdclk(state);
f6d1973d 14400
656d1b89
L
14401 /*
14402 * SKL workaround: bspec recommends we disable the SAGV when we
14403 * have more then one pipe enabled
14404 */
56feca91 14405 if (!intel_can_enable_sagv(state))
16dcdc4e 14406 intel_disable_sagv(dev_priv);
656d1b89 14407
c0ead703 14408 intel_modeset_verify_disabled(dev);
4740b0f2 14409 }
47fab737 14410
896e5bb0 14411 /* Complete the events for pipes that have now been disabled */
29ceb0e6 14412 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
f6ac4b2a 14413 bool modeset = needs_modeset(crtc->state);
80715b2f 14414
1f7528c4
SV
14415 /* Complete events for now disable pipes here. */
14416 if (modeset && !crtc->state->active && crtc->state->event) {
14417 spin_lock_irq(&dev->event_lock);
14418 drm_crtc_send_vblank_event(crtc, crtc->state->event);
14419 spin_unlock_irq(&dev->event_lock);
14420
14421 crtc->state->event = NULL;
14422 }
177246a8
MR
14423 }
14424
896e5bb0
L
14425 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
14426 dev_priv->display.update_crtcs(state, &crtc_vblank_mask);
14427
94f05024
SV
14428 /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
14429 * already, but still need the state for the delayed optimization. To
14430 * fix this:
14431 * - wrap the optimization/post_plane_update stuff into a per-crtc work.
14432 * - schedule that vblank worker _before_ calling hw_done
14433 * - at the start of commit_tail, cancel it _synchrously
14434 * - switch over to the vblank wait helper in the core after that since
14435 * we don't need out special handling any more.
14436 */
5a21b665
SV
14437 if (!state->legacy_cursor_update)
14438 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
14439
14440 /*
14441 * Now that the vblank has passed, we can go ahead and program the
14442 * optimal watermarks on platforms that need two-step watermark
14443 * programming.
14444 *
14445 * TODO: Move this (and other cleanup) to an async worker eventually.
14446 */
14447 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
14448 intel_cstate = to_intel_crtc_state(crtc->state);
14449
14450 if (dev_priv->display.optimize_watermarks)
14451 dev_priv->display.optimize_watermarks(intel_cstate);
14452 }
14453
14454 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
14455 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
14456
14457 if (put_domains[i])
14458 modeset_put_power_domains(dev_priv, put_domains[i]);
14459
14460 intel_modeset_verify_crtc(crtc, old_crtc_state, crtc->state);
14461 }
14462
56feca91 14463 if (intel_state->modeset && intel_can_enable_sagv(state))
16dcdc4e 14464 intel_enable_sagv(dev_priv);
656d1b89 14465
94f05024
SV
14466 drm_atomic_helper_commit_hw_done(state);
14467
5a21b665
SV
14468 if (intel_state->modeset)
14469 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
14470
14471 mutex_lock(&dev->struct_mutex);
14472 drm_atomic_helper_cleanup_planes(dev, state);
14473 mutex_unlock(&dev->struct_mutex);
14474
ea0000f0
SV
14475 drm_atomic_helper_commit_cleanup_done(state);
14476
ee165b1a 14477 drm_atomic_state_free(state);
f30da187 14478
75714940
MK
14479 /* As one of the primary mmio accessors, KMS has a high likelihood
14480 * of triggering bugs in unclaimed access. After we finish
14481 * modesetting, see if an error has been flagged, and if so
14482 * enable debugging for the next modeset - and hope we catch
14483 * the culprit.
14484 *
14485 * XXX note that we assume display power is on at this point.
14486 * This might hold true now but we need to add pm helper to check
14487 * unclaimed only when the hardware is on, as atomic commits
14488 * can happen also when the device is completely off.
14489 */
14490 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
94f05024
SV
14491}
14492
14493static void intel_atomic_commit_work(struct work_struct *work)
14494{
14495 struct drm_atomic_state *state = container_of(work,
14496 struct drm_atomic_state,
14497 commit_work);
14498 intel_atomic_commit_tail(state);
14499}
14500
6c9c1b38
SV
14501static void intel_atomic_track_fbs(struct drm_atomic_state *state)
14502{
14503 struct drm_plane_state *old_plane_state;
14504 struct drm_plane *plane;
6c9c1b38
SV
14505 int i;
14506
faf5bf0a
CW
14507 for_each_plane_in_state(state, plane, old_plane_state, i)
14508 i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
14509 intel_fb_obj(plane->state->fb),
14510 to_intel_plane(plane)->frontbuffer_bit);
6c9c1b38
SV
14511}
14512
94f05024
SV
14513/**
14514 * intel_atomic_commit - commit validated state object
14515 * @dev: DRM device
14516 * @state: the top-level driver state object
14517 * @nonblock: nonblocking commit
14518 *
14519 * This function commits a top-level state object that has been validated
14520 * with drm_atomic_helper_check().
14521 *
14522 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
14523 * nonblocking commits are only safe for pure plane updates. Everything else
14524 * should work though.
14525 *
14526 * RETURNS
14527 * Zero for success or -errno.
14528 */
14529static int intel_atomic_commit(struct drm_device *dev,
14530 struct drm_atomic_state *state,
14531 bool nonblock)
14532{
14533 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 14534 struct drm_i915_private *dev_priv = to_i915(dev);
94f05024
SV
14535 int ret = 0;
14536
14537 if (intel_state->modeset && nonblock) {
14538 DRM_DEBUG_KMS("nonblocking commit for modeset not yet implemented.\n");
14539 return -EINVAL;
14540 }
14541
14542 ret = drm_atomic_helper_setup_commit(state, nonblock);
14543 if (ret)
14544 return ret;
14545
14546 INIT_WORK(&state->commit_work, intel_atomic_commit_work);
14547
14548 ret = intel_atomic_prepare_commit(dev, state, nonblock);
14549 if (ret) {
14550 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
14551 return ret;
14552 }
14553
14554 drm_atomic_helper_swap_state(state, true);
14555 dev_priv->wm.distrust_bios_wm = false;
14556 dev_priv->wm.skl_results = intel_state->wm_results;
14557 intel_shared_dpll_commit(state);
6c9c1b38 14558 intel_atomic_track_fbs(state);
94f05024
SV
14559
14560 if (nonblock)
14561 queue_work(system_unbound_wq, &state->commit_work);
14562 else
14563 intel_atomic_commit_tail(state);
75714940 14564
74c090b1 14565 return 0;
7f27126e
JB
14566}
14567
c0c36b94
CW
14568void intel_crtc_restore_mode(struct drm_crtc *crtc)
14569{
83a57153
ACO
14570 struct drm_device *dev = crtc->dev;
14571 struct drm_atomic_state *state;
e694eb02 14572 struct drm_crtc_state *crtc_state;
2bfb4627 14573 int ret;
83a57153
ACO
14574
14575 state = drm_atomic_state_alloc(dev);
14576 if (!state) {
78108b7c
VS
14577 DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
14578 crtc->base.id, crtc->name);
83a57153
ACO
14579 return;
14580 }
14581
e694eb02 14582 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
83a57153 14583
e694eb02
ML
14584retry:
14585 crtc_state = drm_atomic_get_crtc_state(state, crtc);
14586 ret = PTR_ERR_OR_ZERO(crtc_state);
14587 if (!ret) {
14588 if (!crtc_state->active)
14589 goto out;
83a57153 14590
e694eb02 14591 crtc_state->mode_changed = true;
74c090b1 14592 ret = drm_atomic_commit(state);
83a57153
ACO
14593 }
14594
e694eb02
ML
14595 if (ret == -EDEADLK) {
14596 drm_atomic_state_clear(state);
14597 drm_modeset_backoff(state->acquire_ctx);
14598 goto retry;
4ed9fb37 14599 }
4be07317 14600
2bfb4627 14601 if (ret)
e694eb02 14602out:
2bfb4627 14603 drm_atomic_state_free(state);
c0c36b94
CW
14604}
14605
a8784875
BP
14606/*
14607 * FIXME: Remove this once i915 is fully DRIVER_ATOMIC by calling
14608 * drm_atomic_helper_legacy_gamma_set() directly.
14609 */
14610static int intel_atomic_legacy_gamma_set(struct drm_crtc *crtc,
14611 u16 *red, u16 *green, u16 *blue,
14612 uint32_t size)
14613{
14614 struct drm_device *dev = crtc->dev;
14615 struct drm_mode_config *config = &dev->mode_config;
14616 struct drm_crtc_state *state;
14617 int ret;
14618
14619 ret = drm_atomic_helper_legacy_gamma_set(crtc, red, green, blue, size);
14620 if (ret)
14621 return ret;
14622
14623 /*
14624 * Make sure we update the legacy properties so this works when
14625 * atomic is not enabled.
14626 */
14627
14628 state = crtc->state;
14629
14630 drm_object_property_set_value(&crtc->base,
14631 config->degamma_lut_property,
14632 (state->degamma_lut) ?
14633 state->degamma_lut->base.id : 0);
14634
14635 drm_object_property_set_value(&crtc->base,
14636 config->ctm_property,
14637 (state->ctm) ?
14638 state->ctm->base.id : 0);
14639
14640 drm_object_property_set_value(&crtc->base,
14641 config->gamma_lut_property,
14642 (state->gamma_lut) ?
14643 state->gamma_lut->base.id : 0);
14644
14645 return 0;
14646}
14647
f6e5b160 14648static const struct drm_crtc_funcs intel_crtc_funcs = {
a8784875 14649 .gamma_set = intel_atomic_legacy_gamma_set,
74c090b1 14650 .set_config = drm_atomic_helper_set_config,
82cf435b 14651 .set_property = drm_atomic_helper_crtc_set_property,
f6e5b160 14652 .destroy = intel_crtc_destroy,
527b6abe 14653 .page_flip = intel_crtc_page_flip,
1356837e
MR
14654 .atomic_duplicate_state = intel_crtc_duplicate_state,
14655 .atomic_destroy_state = intel_crtc_destroy_state,
f6e5b160
CW
14656};
14657
6beb8c23
MR
14658/**
14659 * intel_prepare_plane_fb - Prepare fb for usage on plane
14660 * @plane: drm plane to prepare for
14661 * @fb: framebuffer to prepare for presentation
14662 *
14663 * Prepares a framebuffer for usage on a display plane. Generally this
14664 * involves pinning the underlying object and updating the frontbuffer tracking
14665 * bits. Some older platforms need special physical address handling for
14666 * cursor planes.
14667 *
f935675f
ML
14668 * Must be called with struct_mutex held.
14669 *
6beb8c23
MR
14670 * Returns 0 on success, negative error code on failure.
14671 */
14672int
14673intel_prepare_plane_fb(struct drm_plane *plane,
1832040d 14674 struct drm_plane_state *new_state)
465c120c
MR
14675{
14676 struct drm_device *dev = plane->dev;
50a0bc90 14677 struct drm_i915_private *dev_priv = to_i915(dev);
844f9111 14678 struct drm_framebuffer *fb = new_state->fb;
6beb8c23 14679 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
1ee49399 14680 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
c37efb99 14681 struct reservation_object *resv;
6beb8c23 14682 int ret = 0;
465c120c 14683
1ee49399 14684 if (!obj && !old_obj)
465c120c
MR
14685 return 0;
14686
5008e874
ML
14687 if (old_obj) {
14688 struct drm_crtc_state *crtc_state =
14689 drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
14690
14691 /* Big Hammer, we also need to ensure that any pending
14692 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
14693 * current scanout is retired before unpinning the old
14694 * framebuffer. Note that we rely on userspace rendering
14695 * into the buffer attached to the pipe they are waiting
14696 * on. If not, userspace generates a GPU hang with IPEHR
14697 * point to the MI_WAIT_FOR_EVENT.
14698 *
14699 * This should only fail upon a hung GPU, in which case we
14700 * can safely continue.
14701 */
14702 if (needs_modeset(crtc_state))
14703 ret = i915_gem_object_wait_rendering(old_obj, true);
f4457ae7
CW
14704 if (ret) {
14705 /* GPU hangs should have been swallowed by the wait */
14706 WARN_ON(ret == -EIO);
f935675f 14707 return ret;
f4457ae7 14708 }
5008e874
ML
14709 }
14710
c37efb99
CW
14711 if (!obj)
14712 return 0;
14713
5a21b665 14714 /* For framebuffer backed by dmabuf, wait for fence */
c37efb99
CW
14715 resv = i915_gem_object_get_dmabuf_resv(obj);
14716 if (resv) {
5a21b665
SV
14717 long lret;
14718
c37efb99 14719 lret = reservation_object_wait_timeout_rcu(resv, false, true,
5a21b665
SV
14720 MAX_SCHEDULE_TIMEOUT);
14721 if (lret == -ERESTARTSYS)
14722 return lret;
14723
14724 WARN(lret < 0, "waiting returns %li\n", lret);
14725 }
14726
c37efb99 14727 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
6beb8c23 14728 INTEL_INFO(dev)->cursor_needs_physical) {
50a0bc90 14729 int align = IS_I830(dev_priv) ? 16 * 1024 : 256;
6beb8c23
MR
14730 ret = i915_gem_object_attach_phys(obj, align);
14731 if (ret)
14732 DRM_DEBUG_KMS("failed to attach phys object\n");
14733 } else {
058d88c4
CW
14734 struct i915_vma *vma;
14735
14736 vma = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
14737 if (IS_ERR(vma))
14738 ret = PTR_ERR(vma);
6beb8c23 14739 }
465c120c 14740
c37efb99 14741 if (ret == 0) {
27c01aae 14742 to_intel_plane_state(new_state)->wait_req =
d72d908b
CW
14743 i915_gem_active_get(&obj->last_write,
14744 &obj->base.dev->struct_mutex);
7580d774 14745 }
fdd508a6 14746
6beb8c23
MR
14747 return ret;
14748}
14749
38f3ce3a
MR
14750/**
14751 * intel_cleanup_plane_fb - Cleans up an fb after plane use
14752 * @plane: drm plane to clean up for
14753 * @fb: old framebuffer that was on plane
14754 *
14755 * Cleans up a framebuffer that has just been removed from a plane.
f935675f
ML
14756 *
14757 * Must be called with struct_mutex held.
38f3ce3a
MR
14758 */
14759void
14760intel_cleanup_plane_fb(struct drm_plane *plane,
1832040d 14761 struct drm_plane_state *old_state)
38f3ce3a
MR
14762{
14763 struct drm_device *dev = plane->dev;
7580d774 14764 struct intel_plane_state *old_intel_state;
84978257 14765 struct intel_plane_state *intel_state = to_intel_plane_state(plane->state);
1ee49399
ML
14766 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
14767 struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
38f3ce3a 14768
7580d774
ML
14769 old_intel_state = to_intel_plane_state(old_state);
14770
1ee49399 14771 if (!obj && !old_obj)
38f3ce3a
MR
14772 return;
14773
1ee49399
ML
14774 if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
14775 !INTEL_INFO(dev)->cursor_needs_physical))
3465c580 14776 intel_unpin_fb_obj(old_state->fb, old_state->rotation);
1ee49399 14777
84978257 14778 i915_gem_request_assign(&intel_state->wait_req, NULL);
7580d774 14779 i915_gem_request_assign(&old_intel_state->wait_req, NULL);
465c120c
MR
14780}
14781
6156a456
CK
14782int
14783skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
14784{
14785 int max_scale;
6156a456
CK
14786 int crtc_clock, cdclk;
14787
bf8a0af0 14788 if (!intel_crtc || !crtc_state->base.enable)
6156a456
CK
14789 return DRM_PLANE_HELPER_NO_SCALING;
14790
6156a456 14791 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
27c329ed 14792 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
6156a456 14793
54bf1ce6 14794 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
6156a456
CK
14795 return DRM_PLANE_HELPER_NO_SCALING;
14796
14797 /*
14798 * skl max scale is lower of:
14799 * close to 3 but not 3, -1 is for that purpose
14800 * or
14801 * cdclk/crtc_clock
14802 */
14803 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
14804
14805 return max_scale;
14806}
14807
465c120c 14808static int
3c692a41 14809intel_check_primary_plane(struct drm_plane *plane,
061e4b8d 14810 struct intel_crtc_state *crtc_state,
3c692a41
GP
14811 struct intel_plane_state *state)
14812{
b63a16f6 14813 struct drm_i915_private *dev_priv = to_i915(plane->dev);
2b875c22 14814 struct drm_crtc *crtc = state->base.crtc;
6156a456 14815 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
061e4b8d
ML
14816 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
14817 bool can_position = false;
b63a16f6 14818 int ret;
465c120c 14819
b63a16f6 14820 if (INTEL_GEN(dev_priv) >= 9) {
693bdc28
VS
14821 /* use scaler when colorkey is not required */
14822 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
14823 min_scale = 1;
14824 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
14825 }
d8106366 14826 can_position = true;
6156a456 14827 }
d8106366 14828
cc926387
SV
14829 ret = drm_plane_helper_check_state(&state->base,
14830 &state->clip,
14831 min_scale, max_scale,
14832 can_position, true);
b63a16f6
VS
14833 if (ret)
14834 return ret;
14835
cc926387 14836 if (!state->base.fb)
b63a16f6
VS
14837 return 0;
14838
14839 if (INTEL_GEN(dev_priv) >= 9) {
14840 ret = skl_check_plane_surface(state);
14841 if (ret)
14842 return ret;
14843 }
14844
14845 return 0;
14af293f
GP
14846}
14847
5a21b665
SV
14848static void intel_begin_crtc_commit(struct drm_crtc *crtc,
14849 struct drm_crtc_state *old_crtc_state)
14850{
14851 struct drm_device *dev = crtc->dev;
62e0fb88 14852 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665 14853 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b707aa50
L
14854 struct intel_crtc_state *intel_cstate =
14855 to_intel_crtc_state(crtc->state);
5a21b665
SV
14856 struct intel_crtc_state *old_intel_state =
14857 to_intel_crtc_state(old_crtc_state);
14858 bool modeset = needs_modeset(crtc->state);
62e0fb88 14859 enum pipe pipe = intel_crtc->pipe;
5a21b665
SV
14860
14861 /* Perform vblank evasion around commit operation */
14862 intel_pipe_update_start(intel_crtc);
14863
14864 if (modeset)
14865 return;
14866
14867 if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
14868 intel_color_set_csc(crtc->state);
14869 intel_color_load_luts(crtc->state);
14870 }
14871
b707aa50 14872 if (intel_cstate->update_pipe) {
5a21b665 14873 intel_update_pipe_config(intel_crtc, old_intel_state);
b707aa50 14874 } else if (INTEL_GEN(dev_priv) >= 9) {
5a21b665 14875 skl_detach_scalers(intel_crtc);
62e0fb88
L
14876
14877 I915_WRITE(PIPE_WM_LINETIME(pipe),
b707aa50 14878 intel_cstate->wm.skl.optimal.linetime);
62e0fb88 14879 }
5a21b665
SV
14880}
14881
14882static void intel_finish_crtc_commit(struct drm_crtc *crtc,
14883 struct drm_crtc_state *old_crtc_state)
14884{
14885 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14886
14887 intel_pipe_update_end(intel_crtc, NULL);
14888}
14889
cf4c7c12 14890/**
4a3b8769
MR
14891 * intel_plane_destroy - destroy a plane
14892 * @plane: plane to destroy
cf4c7c12 14893 *
4a3b8769
MR
14894 * Common destruction function for all types of planes (primary, cursor,
14895 * sprite).
cf4c7c12 14896 */
4a3b8769 14897void intel_plane_destroy(struct drm_plane *plane)
465c120c 14898{
69ae561f
VS
14899 if (!plane)
14900 return;
14901
465c120c 14902 drm_plane_cleanup(plane);
69ae561f 14903 kfree(to_intel_plane(plane));
465c120c
MR
14904}
14905
65a3fea0 14906const struct drm_plane_funcs intel_plane_funcs = {
70a101f8
MR
14907 .update_plane = drm_atomic_helper_update_plane,
14908 .disable_plane = drm_atomic_helper_disable_plane,
3d7d6510 14909 .destroy = intel_plane_destroy,
c196e1d6 14910 .set_property = drm_atomic_helper_plane_set_property,
a98b3431
MR
14911 .atomic_get_property = intel_plane_atomic_get_property,
14912 .atomic_set_property = intel_plane_atomic_set_property,
ea2c67bb
MR
14913 .atomic_duplicate_state = intel_plane_duplicate_state,
14914 .atomic_destroy_state = intel_plane_destroy_state,
14915
465c120c
MR
14916};
14917
14918static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
14919 int pipe)
14920{
6e266956 14921 struct drm_i915_private *dev_priv = to_i915(dev);
fca0ce2a
VS
14922 struct intel_plane *primary = NULL;
14923 struct intel_plane_state *state = NULL;
465c120c 14924 const uint32_t *intel_primary_formats;
45e3743a 14925 unsigned int num_formats;
fca0ce2a 14926 int ret;
465c120c
MR
14927
14928 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
fca0ce2a
VS
14929 if (!primary)
14930 goto fail;
465c120c 14931
8e7d688b 14932 state = intel_create_plane_state(&primary->base);
fca0ce2a
VS
14933 if (!state)
14934 goto fail;
8e7d688b 14935 primary->base.state = &state->base;
ea2c67bb 14936
465c120c
MR
14937 primary->can_scale = false;
14938 primary->max_downscale = 1;
6156a456
CK
14939 if (INTEL_INFO(dev)->gen >= 9) {
14940 primary->can_scale = true;
af99ceda 14941 state->scaler_id = -1;
6156a456 14942 }
465c120c
MR
14943 primary->pipe = pipe;
14944 primary->plane = pipe;
a9ff8714 14945 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
c59cb179 14946 primary->check_plane = intel_check_primary_plane;
465c120c
MR
14947 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
14948 primary->plane = !pipe;
14949
6c0fd451
DL
14950 if (INTEL_INFO(dev)->gen >= 9) {
14951 intel_primary_formats = skl_primary_formats;
14952 num_formats = ARRAY_SIZE(skl_primary_formats);
a8d201af
ML
14953
14954 primary->update_plane = skylake_update_primary_plane;
14955 primary->disable_plane = skylake_disable_primary_plane;
6e266956 14956 } else if (HAS_PCH_SPLIT(dev_priv)) {
a8d201af
ML
14957 intel_primary_formats = i965_primary_formats;
14958 num_formats = ARRAY_SIZE(i965_primary_formats);
14959
14960 primary->update_plane = ironlake_update_primary_plane;
14961 primary->disable_plane = i9xx_disable_primary_plane;
6c0fd451 14962 } else if (INTEL_INFO(dev)->gen >= 4) {
568db4f2
DL
14963 intel_primary_formats = i965_primary_formats;
14964 num_formats = ARRAY_SIZE(i965_primary_formats);
a8d201af
ML
14965
14966 primary->update_plane = i9xx_update_primary_plane;
14967 primary->disable_plane = i9xx_disable_primary_plane;
6c0fd451
DL
14968 } else {
14969 intel_primary_formats = i8xx_primary_formats;
14970 num_formats = ARRAY_SIZE(i8xx_primary_formats);
a8d201af
ML
14971
14972 primary->update_plane = i9xx_update_primary_plane;
14973 primary->disable_plane = i9xx_disable_primary_plane;
465c120c
MR
14974 }
14975
38573dc1
VS
14976 if (INTEL_INFO(dev)->gen >= 9)
14977 ret = drm_universal_plane_init(dev, &primary->base, 0,
14978 &intel_plane_funcs,
14979 intel_primary_formats, num_formats,
14980 DRM_PLANE_TYPE_PRIMARY,
14981 "plane 1%c", pipe_name(pipe));
9beb5fea 14982 else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
38573dc1
VS
14983 ret = drm_universal_plane_init(dev, &primary->base, 0,
14984 &intel_plane_funcs,
14985 intel_primary_formats, num_formats,
14986 DRM_PLANE_TYPE_PRIMARY,
14987 "primary %c", pipe_name(pipe));
14988 else
14989 ret = drm_universal_plane_init(dev, &primary->base, 0,
14990 &intel_plane_funcs,
14991 intel_primary_formats, num_formats,
14992 DRM_PLANE_TYPE_PRIMARY,
14993 "plane %c", plane_name(primary->plane));
fca0ce2a
VS
14994 if (ret)
14995 goto fail;
48404c1e 14996
3b7a5119
SJ
14997 if (INTEL_INFO(dev)->gen >= 4)
14998 intel_create_rotation_property(dev, primary);
48404c1e 14999
ea2c67bb
MR
15000 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
15001
465c120c 15002 return &primary->base;
fca0ce2a
VS
15003
15004fail:
15005 kfree(state);
15006 kfree(primary);
15007
15008 return NULL;
465c120c
MR
15009}
15010
3b7a5119
SJ
15011void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
15012{
15013 if (!dev->mode_config.rotation_property) {
31ad61e4
JL
15014 unsigned long flags = DRM_ROTATE_0 |
15015 DRM_ROTATE_180;
3b7a5119
SJ
15016
15017 if (INTEL_INFO(dev)->gen >= 9)
31ad61e4 15018 flags |= DRM_ROTATE_90 | DRM_ROTATE_270;
3b7a5119
SJ
15019
15020 dev->mode_config.rotation_property =
15021 drm_mode_create_rotation_property(dev, flags);
15022 }
15023 if (dev->mode_config.rotation_property)
15024 drm_object_attach_property(&plane->base.base,
15025 dev->mode_config.rotation_property,
15026 plane->base.state->rotation);
15027}
15028
3d7d6510 15029static int
852e787c 15030intel_check_cursor_plane(struct drm_plane *plane,
061e4b8d 15031 struct intel_crtc_state *crtc_state,
852e787c 15032 struct intel_plane_state *state)
3d7d6510 15033{
2b875c22 15034 struct drm_framebuffer *fb = state->base.fb;
757f9a3e 15035 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
b29ec92c 15036 enum pipe pipe = to_intel_plane(plane)->pipe;
757f9a3e
GP
15037 unsigned stride;
15038 int ret;
3d7d6510 15039
f8856a44
VS
15040 ret = drm_plane_helper_check_state(&state->base,
15041 &state->clip,
15042 DRM_PLANE_HELPER_NO_SCALING,
15043 DRM_PLANE_HELPER_NO_SCALING,
15044 true, true);
757f9a3e
GP
15045 if (ret)
15046 return ret;
15047
757f9a3e
GP
15048 /* if we want to turn off the cursor ignore width and height */
15049 if (!obj)
da20eabd 15050 return 0;
757f9a3e 15051
757f9a3e 15052 /* Check for which cursor types we support */
50a0bc90
TU
15053 if (!cursor_size_ok(to_i915(plane->dev), state->base.crtc_w,
15054 state->base.crtc_h)) {
ea2c67bb
MR
15055 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
15056 state->base.crtc_w, state->base.crtc_h);
757f9a3e
GP
15057 return -EINVAL;
15058 }
15059
ea2c67bb
MR
15060 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
15061 if (obj->base.size < stride * state->base.crtc_h) {
757f9a3e
GP
15062 DRM_DEBUG_KMS("buffer is too small\n");
15063 return -ENOMEM;
15064 }
15065
3a656b54 15066 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
757f9a3e 15067 DRM_DEBUG_KMS("cursor cannot be tiled\n");
da20eabd 15068 return -EINVAL;
32b7eeec
MR
15069 }
15070
b29ec92c
VS
15071 /*
15072 * There's something wrong with the cursor on CHV pipe C.
15073 * If it straddles the left edge of the screen then
15074 * moving it away from the edge or disabling it often
15075 * results in a pipe underrun, and often that can lead to
15076 * dead pipe (constant underrun reported, and it scans
15077 * out just a solid color). To recover from that, the
15078 * display power well must be turned off and on again.
15079 * Refuse the put the cursor into that compromised position.
15080 */
920a14b2 15081 if (IS_CHERRYVIEW(to_i915(plane->dev)) && pipe == PIPE_C &&
936e71e3 15082 state->base.visible && state->base.crtc_x < 0) {
b29ec92c
VS
15083 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
15084 return -EINVAL;
15085 }
15086
da20eabd 15087 return 0;
852e787c 15088}
3d7d6510 15089
a8ad0d8e
ML
15090static void
15091intel_disable_cursor_plane(struct drm_plane *plane,
7fabf5ef 15092 struct drm_crtc *crtc)
a8ad0d8e 15093{
f2858021
ML
15094 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
15095
15096 intel_crtc->cursor_addr = 0;
55a08b3f 15097 intel_crtc_update_cursor(crtc, NULL);
a8ad0d8e
ML
15098}
15099
f4a2cf29 15100static void
55a08b3f
ML
15101intel_update_cursor_plane(struct drm_plane *plane,
15102 const struct intel_crtc_state *crtc_state,
15103 const struct intel_plane_state *state)
852e787c 15104{
55a08b3f
ML
15105 struct drm_crtc *crtc = crtc_state->base.crtc;
15106 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ea2c67bb 15107 struct drm_device *dev = plane->dev;
2b875c22 15108 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
a912f12f 15109 uint32_t addr;
852e787c 15110
f4a2cf29 15111 if (!obj)
a912f12f 15112 addr = 0;
f4a2cf29 15113 else if (!INTEL_INFO(dev)->cursor_needs_physical)
058d88c4 15114 addr = i915_gem_object_ggtt_offset(obj, NULL);
f4a2cf29 15115 else
a912f12f 15116 addr = obj->phys_handle->busaddr;
852e787c 15117
a912f12f 15118 intel_crtc->cursor_addr = addr;
55a08b3f 15119 intel_crtc_update_cursor(crtc, state);
852e787c
GP
15120}
15121
3d7d6510
MR
15122static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
15123 int pipe)
15124{
fca0ce2a
VS
15125 struct intel_plane *cursor = NULL;
15126 struct intel_plane_state *state = NULL;
15127 int ret;
3d7d6510
MR
15128
15129 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
fca0ce2a
VS
15130 if (!cursor)
15131 goto fail;
3d7d6510 15132
8e7d688b 15133 state = intel_create_plane_state(&cursor->base);
fca0ce2a
VS
15134 if (!state)
15135 goto fail;
8e7d688b 15136 cursor->base.state = &state->base;
ea2c67bb 15137
3d7d6510
MR
15138 cursor->can_scale = false;
15139 cursor->max_downscale = 1;
15140 cursor->pipe = pipe;
15141 cursor->plane = pipe;
a9ff8714 15142 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
c59cb179 15143 cursor->check_plane = intel_check_cursor_plane;
55a08b3f 15144 cursor->update_plane = intel_update_cursor_plane;
a8ad0d8e 15145 cursor->disable_plane = intel_disable_cursor_plane;
3d7d6510 15146
fca0ce2a
VS
15147 ret = drm_universal_plane_init(dev, &cursor->base, 0,
15148 &intel_plane_funcs,
15149 intel_cursor_formats,
15150 ARRAY_SIZE(intel_cursor_formats),
38573dc1
VS
15151 DRM_PLANE_TYPE_CURSOR,
15152 "cursor %c", pipe_name(pipe));
fca0ce2a
VS
15153 if (ret)
15154 goto fail;
4398ad45
VS
15155
15156 if (INTEL_INFO(dev)->gen >= 4) {
15157 if (!dev->mode_config.rotation_property)
15158 dev->mode_config.rotation_property =
15159 drm_mode_create_rotation_property(dev,
31ad61e4
JL
15160 DRM_ROTATE_0 |
15161 DRM_ROTATE_180);
4398ad45
VS
15162 if (dev->mode_config.rotation_property)
15163 drm_object_attach_property(&cursor->base.base,
15164 dev->mode_config.rotation_property,
8e7d688b 15165 state->base.rotation);
4398ad45
VS
15166 }
15167
af99ceda
CK
15168 if (INTEL_INFO(dev)->gen >=9)
15169 state->scaler_id = -1;
15170
ea2c67bb
MR
15171 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
15172
3d7d6510 15173 return &cursor->base;
fca0ce2a
VS
15174
15175fail:
15176 kfree(state);
15177 kfree(cursor);
15178
15179 return NULL;
3d7d6510
MR
15180}
15181
549e2bfb
CK
15182static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
15183 struct intel_crtc_state *crtc_state)
15184{
15185 int i;
15186 struct intel_scaler *intel_scaler;
15187 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
15188
15189 for (i = 0; i < intel_crtc->num_scalers; i++) {
15190 intel_scaler = &scaler_state->scalers[i];
15191 intel_scaler->in_use = 0;
549e2bfb
CK
15192 intel_scaler->mode = PS_SCALER_MODE_DYN;
15193 }
15194
15195 scaler_state->scaler_id = -1;
15196}
15197
b358d0a6 15198static void intel_crtc_init(struct drm_device *dev, int pipe)
79e53945 15199{
fac5e23e 15200 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 15201 struct intel_crtc *intel_crtc;
f5de6e07 15202 struct intel_crtc_state *crtc_state = NULL;
3d7d6510
MR
15203 struct drm_plane *primary = NULL;
15204 struct drm_plane *cursor = NULL;
8563b1e8 15205 int ret;
79e53945 15206
955382f3 15207 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
79e53945
JB
15208 if (intel_crtc == NULL)
15209 return;
15210
f5de6e07
ACO
15211 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
15212 if (!crtc_state)
15213 goto fail;
550acefd
ACO
15214 intel_crtc->config = crtc_state;
15215 intel_crtc->base.state = &crtc_state->base;
07878248 15216 crtc_state->base.crtc = &intel_crtc->base;
f5de6e07 15217
549e2bfb
CK
15218 /* initialize shared scalers */
15219 if (INTEL_INFO(dev)->gen >= 9) {
15220 if (pipe == PIPE_C)
15221 intel_crtc->num_scalers = 1;
15222 else
15223 intel_crtc->num_scalers = SKL_NUM_SCALERS;
15224
15225 skl_init_scalers(dev, intel_crtc, crtc_state);
15226 }
15227
465c120c 15228 primary = intel_primary_plane_create(dev, pipe);
3d7d6510
MR
15229 if (!primary)
15230 goto fail;
15231
15232 cursor = intel_cursor_plane_create(dev, pipe);
15233 if (!cursor)
15234 goto fail;
15235
465c120c 15236 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
4d5d72b7
VS
15237 cursor, &intel_crtc_funcs,
15238 "pipe %c", pipe_name(pipe));
3d7d6510
MR
15239 if (ret)
15240 goto fail;
79e53945 15241
1f1c2e24
VS
15242 /*
15243 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
8c0f92e1 15244 * is hooked to pipe B. Hence we want plane A feeding pipe B.
1f1c2e24 15245 */
80824003
JB
15246 intel_crtc->pipe = pipe;
15247 intel_crtc->plane = pipe;
3a77c4c4 15248 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
28c97730 15249 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
e2e767ab 15250 intel_crtc->plane = !pipe;
80824003
JB
15251 }
15252
4b0e333e
CW
15253 intel_crtc->cursor_base = ~0;
15254 intel_crtc->cursor_cntl = ~0;
dc41c154 15255 intel_crtc->cursor_size = ~0;
8d7849db 15256
852eb00d
VS
15257 intel_crtc->wm.cxsr_allowed = true;
15258
22fd0fab
JB
15259 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
15260 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
15261 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
15262 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
15263
79e53945 15264 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
87b6b101 15265
8563b1e8
LL
15266 intel_color_init(&intel_crtc->base);
15267
87b6b101 15268 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
3d7d6510
MR
15269 return;
15270
15271fail:
69ae561f
VS
15272 intel_plane_destroy(primary);
15273 intel_plane_destroy(cursor);
f5de6e07 15274 kfree(crtc_state);
3d7d6510 15275 kfree(intel_crtc);
79e53945
JB
15276}
15277
752aa88a
JB
15278enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
15279{
15280 struct drm_encoder *encoder = connector->base.encoder;
6e9f798d 15281 struct drm_device *dev = connector->base.dev;
752aa88a 15282
51fd371b 15283 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
752aa88a 15284
d3babd3f 15285 if (!encoder || WARN_ON(!encoder->crtc))
752aa88a
JB
15286 return INVALID_PIPE;
15287
15288 return to_intel_crtc(encoder->crtc)->pipe;
15289}
15290
08d7b3d1 15291int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 15292 struct drm_file *file)
08d7b3d1 15293{
08d7b3d1 15294 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7707e653 15295 struct drm_crtc *drmmode_crtc;
c05422d5 15296 struct intel_crtc *crtc;
08d7b3d1 15297
7707e653 15298 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
71240ed2 15299 if (!drmmode_crtc)
3f2c2057 15300 return -ENOENT;
08d7b3d1 15301
7707e653 15302 crtc = to_intel_crtc(drmmode_crtc);
c05422d5 15303 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 15304
c05422d5 15305 return 0;
08d7b3d1
CW
15306}
15307
66a9278e 15308static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 15309{
66a9278e
SV
15310 struct drm_device *dev = encoder->base.dev;
15311 struct intel_encoder *source_encoder;
79e53945 15312 int index_mask = 0;
79e53945
JB
15313 int entry = 0;
15314
b2784e15 15315 for_each_intel_encoder(dev, source_encoder) {
bc079e8b 15316 if (encoders_cloneable(encoder, source_encoder))
66a9278e
SV
15317 index_mask |= (1 << entry);
15318
79e53945
JB
15319 entry++;
15320 }
4ef69c7a 15321
79e53945
JB
15322 return index_mask;
15323}
15324
4d302442
CW
15325static bool has_edp_a(struct drm_device *dev)
15326{
fac5e23e 15327 struct drm_i915_private *dev_priv = to_i915(dev);
4d302442
CW
15328
15329 if (!IS_MOBILE(dev))
15330 return false;
15331
15332 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
15333 return false;
15334
5db94019 15335 if (IS_GEN5(dev_priv) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
15336 return false;
15337
15338 return true;
15339}
15340
84b4e042
JB
15341static bool intel_crt_present(struct drm_device *dev)
15342{
fac5e23e 15343 struct drm_i915_private *dev_priv = to_i915(dev);
84b4e042 15344
884497ed
DL
15345 if (INTEL_INFO(dev)->gen >= 9)
15346 return false;
15347
50a0bc90 15348 if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
84b4e042
JB
15349 return false;
15350
920a14b2 15351 if (IS_CHERRYVIEW(dev_priv))
84b4e042
JB
15352 return false;
15353
4f8036a2
TU
15354 if (HAS_PCH_LPT_H(dev_priv) &&
15355 I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
65e472e4
VS
15356 return false;
15357
70ac54d0 15358 /* DDI E can't be used if DDI A requires 4 lanes */
4f8036a2 15359 if (HAS_DDI(dev_priv) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
70ac54d0
VS
15360 return false;
15361
e4abb733 15362 if (!dev_priv->vbt.int_crt_support)
84b4e042
JB
15363 return false;
15364
15365 return true;
15366}
15367
8090ba8c
ID
15368void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
15369{
15370 int pps_num;
15371 int pps_idx;
15372
15373 if (HAS_DDI(dev_priv))
15374 return;
15375 /*
15376 * This w/a is needed at least on CPT/PPT, but to be sure apply it
15377 * everywhere where registers can be write protected.
15378 */
15379 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
15380 pps_num = 2;
15381 else
15382 pps_num = 1;
15383
15384 for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
15385 u32 val = I915_READ(PP_CONTROL(pps_idx));
15386
15387 val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
15388 I915_WRITE(PP_CONTROL(pps_idx), val);
15389 }
15390}
15391
44cb734c
ID
15392static void intel_pps_init(struct drm_i915_private *dev_priv)
15393{
15394 if (HAS_PCH_SPLIT(dev_priv) || IS_BROXTON(dev_priv))
15395 dev_priv->pps_mmio_base = PCH_PPS_BASE;
15396 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
15397 dev_priv->pps_mmio_base = VLV_PPS_BASE;
15398 else
15399 dev_priv->pps_mmio_base = PPS_BASE;
8090ba8c
ID
15400
15401 intel_pps_unlock_regs_wa(dev_priv);
44cb734c
ID
15402}
15403
79e53945
JB
15404static void intel_setup_outputs(struct drm_device *dev)
15405{
fac5e23e 15406 struct drm_i915_private *dev_priv = to_i915(dev);
4ef69c7a 15407 struct intel_encoder *encoder;
cb0953d7 15408 bool dpd_is_edp = false;
79e53945 15409
44cb734c
ID
15410 intel_pps_init(dev_priv);
15411
97a824e1
ID
15412 /*
15413 * intel_edp_init_connector() depends on this completing first, to
15414 * prevent the registeration of both eDP and LVDS and the incorrect
15415 * sharing of the PPS.
15416 */
c9093354 15417 intel_lvds_init(dev);
79e53945 15418
84b4e042 15419 if (intel_crt_present(dev))
79935fca 15420 intel_crt_init(dev);
cb0953d7 15421
e2d214ae 15422 if (IS_BROXTON(dev_priv)) {
c776eb2e
VK
15423 /*
15424 * FIXME: Broxton doesn't support port detection via the
15425 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
15426 * detect the ports.
15427 */
15428 intel_ddi_init(dev, PORT_A);
15429 intel_ddi_init(dev, PORT_B);
15430 intel_ddi_init(dev, PORT_C);
c6c794a2
SS
15431
15432 intel_dsi_init(dev);
4f8036a2 15433 } else if (HAS_DDI(dev_priv)) {
0e72a5b5
ED
15434 int found;
15435
de31facd
JB
15436 /*
15437 * Haswell uses DDI functions to detect digital outputs.
15438 * On SKL pre-D0 the strap isn't connected, so we assume
15439 * it's there.
15440 */
77179400 15441 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
de31facd 15442 /* WaIgnoreDDIAStrap: skl */
0853723b 15443 if (found || IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
0e72a5b5
ED
15444 intel_ddi_init(dev, PORT_A);
15445
15446 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
15447 * register */
15448 found = I915_READ(SFUSE_STRAP);
15449
15450 if (found & SFUSE_STRAP_DDIB_DETECTED)
15451 intel_ddi_init(dev, PORT_B);
15452 if (found & SFUSE_STRAP_DDIC_DETECTED)
15453 intel_ddi_init(dev, PORT_C);
15454 if (found & SFUSE_STRAP_DDID_DETECTED)
15455 intel_ddi_init(dev, PORT_D);
2800e4c2
RV
15456 /*
15457 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
15458 */
0853723b 15459 if ((IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) &&
2800e4c2
RV
15460 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
15461 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
15462 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
15463 intel_ddi_init(dev, PORT_E);
15464
6e266956 15465 } else if (HAS_PCH_SPLIT(dev_priv)) {
cb0953d7 15466 int found;
5d8a7752 15467 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
270b3042
SV
15468
15469 if (has_edp_a(dev))
15470 intel_dp_init(dev, DP_A, PORT_A);
cb0953d7 15471
dc0fa718 15472 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 15473 /* PCH SDVOB multiplex with HDMIB */
2a5c0832 15474 found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
30ad48b7 15475 if (!found)
e2debe91 15476 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
5eb08b69 15477 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
ab9d7c30 15478 intel_dp_init(dev, PCH_DP_B, PORT_B);
30ad48b7
ZW
15479 }
15480
dc0fa718 15481 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
e2debe91 15482 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
30ad48b7 15483
dc0fa718 15484 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
e2debe91 15485 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
30ad48b7 15486
5eb08b69 15487 if (I915_READ(PCH_DP_C) & DP_DETECTED)
ab9d7c30 15488 intel_dp_init(dev, PCH_DP_C, PORT_C);
5eb08b69 15489
270b3042 15490 if (I915_READ(PCH_DP_D) & DP_DETECTED)
ab9d7c30 15491 intel_dp_init(dev, PCH_DP_D, PORT_D);
920a14b2 15492 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
22f35042 15493 bool has_edp, has_port;
457c52d8 15494
e17ac6db
VS
15495 /*
15496 * The DP_DETECTED bit is the latched state of the DDC
15497 * SDA pin at boot. However since eDP doesn't require DDC
15498 * (no way to plug in a DP->HDMI dongle) the DDC pins for
15499 * eDP ports may have been muxed to an alternate function.
15500 * Thus we can't rely on the DP_DETECTED bit alone to detect
15501 * eDP ports. Consult the VBT as well as DP_DETECTED to
15502 * detect eDP ports.
22f35042
VS
15503 *
15504 * Sadly the straps seem to be missing sometimes even for HDMI
15505 * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
15506 * and VBT for the presence of the port. Additionally we can't
15507 * trust the port type the VBT declares as we've seen at least
15508 * HDMI ports that the VBT claim are DP or eDP.
e17ac6db 15509 */
457c52d8 15510 has_edp = intel_dp_is_edp(dev, PORT_B);
22f35042
VS
15511 has_port = intel_bios_is_port_present(dev_priv, PORT_B);
15512 if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
457c52d8 15513 has_edp &= intel_dp_init(dev, VLV_DP_B, PORT_B);
22f35042 15514 if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
e66eb81d 15515 intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
585a94b8 15516
457c52d8 15517 has_edp = intel_dp_is_edp(dev, PORT_C);
22f35042
VS
15518 has_port = intel_bios_is_port_present(dev_priv, PORT_C);
15519 if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
457c52d8 15520 has_edp &= intel_dp_init(dev, VLV_DP_C, PORT_C);
22f35042 15521 if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
e66eb81d 15522 intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
19c03924 15523
920a14b2 15524 if (IS_CHERRYVIEW(dev_priv)) {
22f35042
VS
15525 /*
15526 * eDP not supported on port D,
15527 * so no need to worry about it
15528 */
15529 has_port = intel_bios_is_port_present(dev_priv, PORT_D);
15530 if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
e66eb81d 15531 intel_dp_init(dev, CHV_DP_D, PORT_D);
22f35042
VS
15532 if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
15533 intel_hdmi_init(dev, CHV_HDMID, PORT_D);
9418c1f1
VS
15534 }
15535
3cfca973 15536 intel_dsi_init(dev);
5db94019 15537 } else if (!IS_GEN2(dev_priv) && !IS_PINEVIEW(dev_priv)) {
27185ae1 15538 bool found = false;
7d57382e 15539
e2debe91 15540 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 15541 DRM_DEBUG_KMS("probing SDVOB\n");
2a5c0832 15542 found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
9beb5fea 15543 if (!found && IS_G4X(dev_priv)) {
b01f2c3a 15544 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
e2debe91 15545 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
b01f2c3a 15546 }
27185ae1 15547
9beb5fea 15548 if (!found && IS_G4X(dev_priv))
ab9d7c30 15549 intel_dp_init(dev, DP_B, PORT_B);
725e30ad 15550 }
13520b05
KH
15551
15552 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 15553
e2debe91 15554 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 15555 DRM_DEBUG_KMS("probing SDVOC\n");
2a5c0832 15556 found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
b01f2c3a 15557 }
27185ae1 15558
e2debe91 15559 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 15560
9beb5fea 15561 if (IS_G4X(dev_priv)) {
b01f2c3a 15562 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
e2debe91 15563 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
b01f2c3a 15564 }
9beb5fea 15565 if (IS_G4X(dev_priv))
ab9d7c30 15566 intel_dp_init(dev, DP_C, PORT_C);
725e30ad 15567 }
27185ae1 15568
9beb5fea 15569 if (IS_G4X(dev_priv) && (I915_READ(DP_D) & DP_DETECTED))
ab9d7c30 15570 intel_dp_init(dev, DP_D, PORT_D);
5db94019 15571 } else if (IS_GEN2(dev_priv))
79e53945
JB
15572 intel_dvo_init(dev);
15573
103a196f 15574 if (SUPPORTS_TV(dev))
79e53945
JB
15575 intel_tv_init(dev);
15576
0bc12bcb 15577 intel_psr_init(dev);
7c8f8a70 15578
b2784e15 15579 for_each_intel_encoder(dev, encoder) {
4ef69c7a
CW
15580 encoder->base.possible_crtcs = encoder->crtc_mask;
15581 encoder->base.possible_clones =
66a9278e 15582 intel_encoder_clones(encoder);
79e53945 15583 }
47356eb6 15584
dde86e2d 15585 intel_init_pch_refclk(dev);
270b3042
SV
15586
15587 drm_helper_move_panel_connectors_to_head(dev);
79e53945
JB
15588}
15589
15590static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
15591{
60a5ca01 15592 struct drm_device *dev = fb->dev;
79e53945 15593 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 15594
ef2d633e 15595 drm_framebuffer_cleanup(fb);
60a5ca01 15596 mutex_lock(&dev->struct_mutex);
ef2d633e 15597 WARN_ON(!intel_fb->obj->framebuffer_references--);
f8c417cd 15598 i915_gem_object_put(intel_fb->obj);
60a5ca01 15599 mutex_unlock(&dev->struct_mutex);
79e53945
JB
15600 kfree(intel_fb);
15601}
15602
15603static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 15604 struct drm_file *file,
79e53945
JB
15605 unsigned int *handle)
15606{
15607 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 15608 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 15609
cc917ab4
CW
15610 if (obj->userptr.mm) {
15611 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
15612 return -EINVAL;
15613 }
15614
05394f39 15615 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
15616}
15617
86c98588
RV
15618static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
15619 struct drm_file *file,
15620 unsigned flags, unsigned color,
15621 struct drm_clip_rect *clips,
15622 unsigned num_clips)
15623{
15624 struct drm_device *dev = fb->dev;
15625 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
15626 struct drm_i915_gem_object *obj = intel_fb->obj;
15627
15628 mutex_lock(&dev->struct_mutex);
74b4ea1e 15629 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
86c98588
RV
15630 mutex_unlock(&dev->struct_mutex);
15631
15632 return 0;
15633}
15634
79e53945
JB
15635static const struct drm_framebuffer_funcs intel_fb_funcs = {
15636 .destroy = intel_user_framebuffer_destroy,
15637 .create_handle = intel_user_framebuffer_create_handle,
86c98588 15638 .dirty = intel_user_framebuffer_dirty,
79e53945
JB
15639};
15640
b321803d 15641static
920a14b2
TU
15642u32 intel_fb_pitch_limit(struct drm_i915_private *dev_priv,
15643 uint64_t fb_modifier, uint32_t pixel_format)
b321803d 15644{
920a14b2 15645 u32 gen = INTEL_INFO(dev_priv)->gen;
b321803d
DL
15646
15647 if (gen >= 9) {
ac484963
VS
15648 int cpp = drm_format_plane_cpp(pixel_format, 0);
15649
b321803d
DL
15650 /* "The stride in bytes must not exceed the of the size of 8K
15651 * pixels and 32K bytes."
15652 */
ac484963 15653 return min(8192 * cpp, 32768);
920a14b2
TU
15654 } else if (gen >= 5 && !IS_VALLEYVIEW(dev_priv) &&
15655 !IS_CHERRYVIEW(dev_priv)) {
b321803d
DL
15656 return 32*1024;
15657 } else if (gen >= 4) {
15658 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
15659 return 16*1024;
15660 else
15661 return 32*1024;
15662 } else if (gen >= 3) {
15663 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
15664 return 8*1024;
15665 else
15666 return 16*1024;
15667 } else {
15668 /* XXX DSPC is limited to 4k tiled */
15669 return 8*1024;
15670 }
15671}
15672
b5ea642a
SV
15673static int intel_framebuffer_init(struct drm_device *dev,
15674 struct intel_framebuffer *intel_fb,
15675 struct drm_mode_fb_cmd2 *mode_cmd,
15676 struct drm_i915_gem_object *obj)
79e53945 15677{
7b49f948 15678 struct drm_i915_private *dev_priv = to_i915(dev);
c2ff7370 15679 unsigned int tiling = i915_gem_object_get_tiling(obj);
79e53945 15680 int ret;
b321803d 15681 u32 pitch_limit, stride_alignment;
d3828147 15682 char *format_name;
79e53945 15683
dd4916c5
SV
15684 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
15685
2a80eada 15686 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
c2ff7370
VS
15687 /*
15688 * If there's a fence, enforce that
15689 * the fb modifier and tiling mode match.
15690 */
15691 if (tiling != I915_TILING_NONE &&
15692 tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
2a80eada
SV
15693 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
15694 return -EINVAL;
15695 }
15696 } else {
c2ff7370 15697 if (tiling == I915_TILING_X) {
2a80eada 15698 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
c2ff7370 15699 } else if (tiling == I915_TILING_Y) {
2a80eada
SV
15700 DRM_DEBUG("No Y tiling for legacy addfb\n");
15701 return -EINVAL;
15702 }
15703 }
15704
9a8f0a12
TU
15705 /* Passed in modifier sanity checking. */
15706 switch (mode_cmd->modifier[0]) {
15707 case I915_FORMAT_MOD_Y_TILED:
15708 case I915_FORMAT_MOD_Yf_TILED:
15709 if (INTEL_INFO(dev)->gen < 9) {
15710 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
15711 mode_cmd->modifier[0]);
15712 return -EINVAL;
15713 }
15714 case DRM_FORMAT_MOD_NONE:
15715 case I915_FORMAT_MOD_X_TILED:
15716 break;
15717 default:
c0f40428
JB
15718 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
15719 mode_cmd->modifier[0]);
57cd6508 15720 return -EINVAL;
c16ed4be 15721 }
57cd6508 15722
c2ff7370
VS
15723 /*
15724 * gen2/3 display engine uses the fence if present,
15725 * so the tiling mode must match the fb modifier exactly.
15726 */
15727 if (INTEL_INFO(dev_priv)->gen < 4 &&
15728 tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
15729 DRM_DEBUG("tiling_mode must match fb modifier exactly on gen2/3\n");
15730 return -EINVAL;
15731 }
15732
7b49f948
VS
15733 stride_alignment = intel_fb_stride_alignment(dev_priv,
15734 mode_cmd->modifier[0],
b321803d
DL
15735 mode_cmd->pixel_format);
15736 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
15737 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
15738 mode_cmd->pitches[0], stride_alignment);
57cd6508 15739 return -EINVAL;
c16ed4be 15740 }
57cd6508 15741
920a14b2 15742 pitch_limit = intel_fb_pitch_limit(dev_priv, mode_cmd->modifier[0],
b321803d 15743 mode_cmd->pixel_format);
a35cdaa0 15744 if (mode_cmd->pitches[0] > pitch_limit) {
b321803d
DL
15745 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
15746 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
2a80eada 15747 "tiled" : "linear",
a35cdaa0 15748 mode_cmd->pitches[0], pitch_limit);
5d7bd705 15749 return -EINVAL;
c16ed4be 15750 }
5d7bd705 15751
c2ff7370
VS
15752 /*
15753 * If there's a fence, enforce that
15754 * the fb pitch and fence stride match.
15755 */
15756 if (tiling != I915_TILING_NONE &&
3e510a8e 15757 mode_cmd->pitches[0] != i915_gem_object_get_stride(obj)) {
c16ed4be 15758 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
3e510a8e
CW
15759 mode_cmd->pitches[0],
15760 i915_gem_object_get_stride(obj));
5d7bd705 15761 return -EINVAL;
c16ed4be 15762 }
5d7bd705 15763
57779d06 15764 /* Reject formats not supported by any plane early. */
308e5bcb 15765 switch (mode_cmd->pixel_format) {
57779d06 15766 case DRM_FORMAT_C8:
04b3924d
VS
15767 case DRM_FORMAT_RGB565:
15768 case DRM_FORMAT_XRGB8888:
15769 case DRM_FORMAT_ARGB8888:
57779d06
VS
15770 break;
15771 case DRM_FORMAT_XRGB1555:
c16ed4be 15772 if (INTEL_INFO(dev)->gen > 3) {
90844f00
EE
15773 format_name = drm_get_format_name(mode_cmd->pixel_format);
15774 DRM_DEBUG("unsupported pixel format: %s\n", format_name);
15775 kfree(format_name);
57779d06 15776 return -EINVAL;
c16ed4be 15777 }
57779d06 15778 break;
57779d06 15779 case DRM_FORMAT_ABGR8888:
920a14b2 15780 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
666a4537 15781 INTEL_INFO(dev)->gen < 9) {
90844f00
EE
15782 format_name = drm_get_format_name(mode_cmd->pixel_format);
15783 DRM_DEBUG("unsupported pixel format: %s\n", format_name);
15784 kfree(format_name);
6c0fd451
DL
15785 return -EINVAL;
15786 }
15787 break;
15788 case DRM_FORMAT_XBGR8888:
04b3924d 15789 case DRM_FORMAT_XRGB2101010:
57779d06 15790 case DRM_FORMAT_XBGR2101010:
c16ed4be 15791 if (INTEL_INFO(dev)->gen < 4) {
90844f00
EE
15792 format_name = drm_get_format_name(mode_cmd->pixel_format);
15793 DRM_DEBUG("unsupported pixel format: %s\n", format_name);
15794 kfree(format_name);
57779d06 15795 return -EINVAL;
c16ed4be 15796 }
b5626747 15797 break;
7531208b 15798 case DRM_FORMAT_ABGR2101010:
920a14b2 15799 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
90844f00
EE
15800 format_name = drm_get_format_name(mode_cmd->pixel_format);
15801 DRM_DEBUG("unsupported pixel format: %s\n", format_name);
15802 kfree(format_name);
7531208b
DL
15803 return -EINVAL;
15804 }
15805 break;
04b3924d
VS
15806 case DRM_FORMAT_YUYV:
15807 case DRM_FORMAT_UYVY:
15808 case DRM_FORMAT_YVYU:
15809 case DRM_FORMAT_VYUY:
c16ed4be 15810 if (INTEL_INFO(dev)->gen < 5) {
90844f00
EE
15811 format_name = drm_get_format_name(mode_cmd->pixel_format);
15812 DRM_DEBUG("unsupported pixel format: %s\n", format_name);
15813 kfree(format_name);
57779d06 15814 return -EINVAL;
c16ed4be 15815 }
57cd6508
CW
15816 break;
15817 default:
90844f00
EE
15818 format_name = drm_get_format_name(mode_cmd->pixel_format);
15819 DRM_DEBUG("unsupported pixel format: %s\n", format_name);
15820 kfree(format_name);
57cd6508
CW
15821 return -EINVAL;
15822 }
15823
90f9a336
VS
15824 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
15825 if (mode_cmd->offsets[0] != 0)
15826 return -EINVAL;
15827
c7d73f6a
SV
15828 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
15829 intel_fb->obj = obj;
15830
6687c906
VS
15831 ret = intel_fill_fb_info(dev_priv, &intel_fb->base);
15832 if (ret)
15833 return ret;
2d7a215f 15834
79e53945
JB
15835 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
15836 if (ret) {
15837 DRM_ERROR("framebuffer init failed %d\n", ret);
15838 return ret;
15839 }
15840
0b05e1e0
VS
15841 intel_fb->obj->framebuffer_references++;
15842
79e53945
JB
15843 return 0;
15844}
15845
79e53945
JB
15846static struct drm_framebuffer *
15847intel_user_framebuffer_create(struct drm_device *dev,
15848 struct drm_file *filp,
1eb83451 15849 const struct drm_mode_fb_cmd2 *user_mode_cmd)
79e53945 15850{
dcb1394e 15851 struct drm_framebuffer *fb;
05394f39 15852 struct drm_i915_gem_object *obj;
76dc3769 15853 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
79e53945 15854
03ac0642
CW
15855 obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
15856 if (!obj)
cce13ff7 15857 return ERR_PTR(-ENOENT);
79e53945 15858
92907cbb 15859 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
dcb1394e 15860 if (IS_ERR(fb))
34911fd3 15861 i915_gem_object_put_unlocked(obj);
dcb1394e
LW
15862
15863 return fb;
79e53945
JB
15864}
15865
79e53945 15866static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 15867 .fb_create = intel_user_framebuffer_create,
0632fef6 15868 .output_poll_changed = intel_fbdev_output_poll_changed,
5ee67f1c
MR
15869 .atomic_check = intel_atomic_check,
15870 .atomic_commit = intel_atomic_commit,
de419ab6
ML
15871 .atomic_state_alloc = intel_atomic_state_alloc,
15872 .atomic_state_clear = intel_atomic_state_clear,
79e53945
JB
15873};
15874
88212941
ID
15875/**
15876 * intel_init_display_hooks - initialize the display modesetting hooks
15877 * @dev_priv: device private
15878 */
15879void intel_init_display_hooks(struct drm_i915_private *dev_priv)
e70236a8 15880{
88212941 15881 if (INTEL_INFO(dev_priv)->gen >= 9) {
bc8d7dff 15882 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
15883 dev_priv->display.get_initial_plane_config =
15884 skylake_get_initial_plane_config;
bc8d7dff
DL
15885 dev_priv->display.crtc_compute_clock =
15886 haswell_crtc_compute_clock;
15887 dev_priv->display.crtc_enable = haswell_crtc_enable;
15888 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 15889 } else if (HAS_DDI(dev_priv)) {
0e8ffe1b 15890 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
15891 dev_priv->display.get_initial_plane_config =
15892 ironlake_get_initial_plane_config;
797d0259
ACO
15893 dev_priv->display.crtc_compute_clock =
15894 haswell_crtc_compute_clock;
4f771f10
PZ
15895 dev_priv->display.crtc_enable = haswell_crtc_enable;
15896 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 15897 } else if (HAS_PCH_SPLIT(dev_priv)) {
0e8ffe1b 15898 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
5724dbd1
DL
15899 dev_priv->display.get_initial_plane_config =
15900 ironlake_get_initial_plane_config;
3fb37703
ACO
15901 dev_priv->display.crtc_compute_clock =
15902 ironlake_crtc_compute_clock;
76e5a89c
SV
15903 dev_priv->display.crtc_enable = ironlake_crtc_enable;
15904 dev_priv->display.crtc_disable = ironlake_crtc_disable;
65b3d6a9 15905 } else if (IS_CHERRYVIEW(dev_priv)) {
89b667f8 15906 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
15907 dev_priv->display.get_initial_plane_config =
15908 i9xx_get_initial_plane_config;
65b3d6a9
ACO
15909 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
15910 dev_priv->display.crtc_enable = valleyview_crtc_enable;
15911 dev_priv->display.crtc_disable = i9xx_crtc_disable;
15912 } else if (IS_VALLEYVIEW(dev_priv)) {
15913 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15914 dev_priv->display.get_initial_plane_config =
15915 i9xx_get_initial_plane_config;
15916 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
89b667f8
JB
15917 dev_priv->display.crtc_enable = valleyview_crtc_enable;
15918 dev_priv->display.crtc_disable = i9xx_crtc_disable;
19ec6693
ACO
15919 } else if (IS_G4X(dev_priv)) {
15920 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15921 dev_priv->display.get_initial_plane_config =
15922 i9xx_get_initial_plane_config;
15923 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
15924 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15925 dev_priv->display.crtc_disable = i9xx_crtc_disable;
70e8aa21
ACO
15926 } else if (IS_PINEVIEW(dev_priv)) {
15927 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15928 dev_priv->display.get_initial_plane_config =
15929 i9xx_get_initial_plane_config;
15930 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
15931 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15932 dev_priv->display.crtc_disable = i9xx_crtc_disable;
81c97f52 15933 } else if (!IS_GEN2(dev_priv)) {
0e8ffe1b 15934 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
15935 dev_priv->display.get_initial_plane_config =
15936 i9xx_get_initial_plane_config;
d6dfee7a 15937 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
76e5a89c
SV
15938 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15939 dev_priv->display.crtc_disable = i9xx_crtc_disable;
81c97f52
ACO
15940 } else {
15941 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15942 dev_priv->display.get_initial_plane_config =
15943 i9xx_get_initial_plane_config;
15944 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
15945 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15946 dev_priv->display.crtc_disable = i9xx_crtc_disable;
f564048e 15947 }
e70236a8 15948
e70236a8 15949 /* Returns the core display clock speed */
88212941 15950 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
1652d19e
VS
15951 dev_priv->display.get_display_clock_speed =
15952 skylake_get_display_clock_speed;
88212941 15953 else if (IS_BROXTON(dev_priv))
acd3f3d3
BP
15954 dev_priv->display.get_display_clock_speed =
15955 broxton_get_display_clock_speed;
88212941 15956 else if (IS_BROADWELL(dev_priv))
1652d19e
VS
15957 dev_priv->display.get_display_clock_speed =
15958 broadwell_get_display_clock_speed;
88212941 15959 else if (IS_HASWELL(dev_priv))
1652d19e
VS
15960 dev_priv->display.get_display_clock_speed =
15961 haswell_get_display_clock_speed;
88212941 15962 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
25eb05fc
JB
15963 dev_priv->display.get_display_clock_speed =
15964 valleyview_get_display_clock_speed;
88212941 15965 else if (IS_GEN5(dev_priv))
b37a6434
VS
15966 dev_priv->display.get_display_clock_speed =
15967 ilk_get_display_clock_speed;
88212941
ID
15968 else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
15969 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
e70236a8
JB
15970 dev_priv->display.get_display_clock_speed =
15971 i945_get_display_clock_speed;
88212941 15972 else if (IS_GM45(dev_priv))
34edce2f
VS
15973 dev_priv->display.get_display_clock_speed =
15974 gm45_get_display_clock_speed;
88212941 15975 else if (IS_CRESTLINE(dev_priv))
34edce2f
VS
15976 dev_priv->display.get_display_clock_speed =
15977 i965gm_get_display_clock_speed;
88212941 15978 else if (IS_PINEVIEW(dev_priv))
34edce2f
VS
15979 dev_priv->display.get_display_clock_speed =
15980 pnv_get_display_clock_speed;
88212941 15981 else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
34edce2f
VS
15982 dev_priv->display.get_display_clock_speed =
15983 g33_get_display_clock_speed;
88212941 15984 else if (IS_I915G(dev_priv))
e70236a8
JB
15985 dev_priv->display.get_display_clock_speed =
15986 i915_get_display_clock_speed;
88212941 15987 else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
e70236a8
JB
15988 dev_priv->display.get_display_clock_speed =
15989 i9xx_misc_get_display_clock_speed;
88212941 15990 else if (IS_I915GM(dev_priv))
e70236a8
JB
15991 dev_priv->display.get_display_clock_speed =
15992 i915gm_get_display_clock_speed;
88212941 15993 else if (IS_I865G(dev_priv))
e70236a8
JB
15994 dev_priv->display.get_display_clock_speed =
15995 i865_get_display_clock_speed;
88212941 15996 else if (IS_I85X(dev_priv))
e70236a8 15997 dev_priv->display.get_display_clock_speed =
1b1d2716 15998 i85x_get_display_clock_speed;
623e01e5 15999 else { /* 830 */
88212941 16000 WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
e70236a8
JB
16001 dev_priv->display.get_display_clock_speed =
16002 i830_get_display_clock_speed;
623e01e5 16003 }
e70236a8 16004
88212941 16005 if (IS_GEN5(dev_priv)) {
3bb11b53 16006 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
88212941 16007 } else if (IS_GEN6(dev_priv)) {
3bb11b53 16008 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
88212941 16009 } else if (IS_IVYBRIDGE(dev_priv)) {
3bb11b53
SJ
16010 /* FIXME: detect B0+ stepping and use auto training */
16011 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
88212941 16012 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
3bb11b53 16013 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
445e780b
VS
16014 }
16015
16016 if (IS_BROADWELL(dev_priv)) {
16017 dev_priv->display.modeset_commit_cdclk =
16018 broadwell_modeset_commit_cdclk;
16019 dev_priv->display.modeset_calc_cdclk =
16020 broadwell_modeset_calc_cdclk;
88212941 16021 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
27c329ed
ML
16022 dev_priv->display.modeset_commit_cdclk =
16023 valleyview_modeset_commit_cdclk;
16024 dev_priv->display.modeset_calc_cdclk =
16025 valleyview_modeset_calc_cdclk;
88212941 16026 } else if (IS_BROXTON(dev_priv)) {
27c329ed 16027 dev_priv->display.modeset_commit_cdclk =
324513c0 16028 bxt_modeset_commit_cdclk;
27c329ed 16029 dev_priv->display.modeset_calc_cdclk =
324513c0 16030 bxt_modeset_calc_cdclk;
c89e39f3
CT
16031 } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
16032 dev_priv->display.modeset_commit_cdclk =
16033 skl_modeset_commit_cdclk;
16034 dev_priv->display.modeset_calc_cdclk =
16035 skl_modeset_calc_cdclk;
e70236a8 16036 }
5a21b665 16037
27082493
L
16038 if (dev_priv->info.gen >= 9)
16039 dev_priv->display.update_crtcs = skl_update_crtcs;
16040 else
16041 dev_priv->display.update_crtcs = intel_update_crtcs;
16042
5a21b665
SV
16043 switch (INTEL_INFO(dev_priv)->gen) {
16044 case 2:
16045 dev_priv->display.queue_flip = intel_gen2_queue_flip;
16046 break;
16047
16048 case 3:
16049 dev_priv->display.queue_flip = intel_gen3_queue_flip;
16050 break;
16051
16052 case 4:
16053 case 5:
16054 dev_priv->display.queue_flip = intel_gen4_queue_flip;
16055 break;
16056
16057 case 6:
16058 dev_priv->display.queue_flip = intel_gen6_queue_flip;
16059 break;
16060 case 7:
16061 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
16062 dev_priv->display.queue_flip = intel_gen7_queue_flip;
16063 break;
16064 case 9:
16065 /* Drop through - unsupported since execlist only. */
16066 default:
16067 /* Default just returns -ENODEV to indicate unsupported */
16068 dev_priv->display.queue_flip = intel_default_queue_flip;
16069 }
e70236a8
JB
16070}
16071
b690e96c
JB
16072/*
16073 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
16074 * resume, or other times. This quirk makes sure that's the case for
16075 * affected systems.
16076 */
0206e353 16077static void quirk_pipea_force(struct drm_device *dev)
b690e96c 16078{
fac5e23e 16079 struct drm_i915_private *dev_priv = to_i915(dev);
b690e96c
JB
16080
16081 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
bc0daf48 16082 DRM_INFO("applying pipe a force quirk\n");
b690e96c
JB
16083}
16084
b6b5d049
VS
16085static void quirk_pipeb_force(struct drm_device *dev)
16086{
fac5e23e 16087 struct drm_i915_private *dev_priv = to_i915(dev);
b6b5d049
VS
16088
16089 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
16090 DRM_INFO("applying pipe b force quirk\n");
16091}
16092
435793df
KP
16093/*
16094 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
16095 */
16096static void quirk_ssc_force_disable(struct drm_device *dev)
16097{
fac5e23e 16098 struct drm_i915_private *dev_priv = to_i915(dev);
435793df 16099 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 16100 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
16101}
16102
4dca20ef 16103/*
5a15ab5b
CE
16104 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
16105 * brightness value
4dca20ef
CE
16106 */
16107static void quirk_invert_brightness(struct drm_device *dev)
16108{
fac5e23e 16109 struct drm_i915_private *dev_priv = to_i915(dev);
4dca20ef 16110 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 16111 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
16112}
16113
9c72cc6f
SD
16114/* Some VBT's incorrectly indicate no backlight is present */
16115static void quirk_backlight_present(struct drm_device *dev)
16116{
fac5e23e 16117 struct drm_i915_private *dev_priv = to_i915(dev);
9c72cc6f
SD
16118 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
16119 DRM_INFO("applying backlight present quirk\n");
16120}
16121
b690e96c
JB
16122struct intel_quirk {
16123 int device;
16124 int subsystem_vendor;
16125 int subsystem_device;
16126 void (*hook)(struct drm_device *dev);
16127};
16128
5f85f176
EE
16129/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
16130struct intel_dmi_quirk {
16131 void (*hook)(struct drm_device *dev);
16132 const struct dmi_system_id (*dmi_id_list)[];
16133};
16134
16135static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
16136{
16137 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
16138 return 1;
16139}
16140
16141static const struct intel_dmi_quirk intel_dmi_quirks[] = {
16142 {
16143 .dmi_id_list = &(const struct dmi_system_id[]) {
16144 {
16145 .callback = intel_dmi_reverse_brightness,
16146 .ident = "NCR Corporation",
16147 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
16148 DMI_MATCH(DMI_PRODUCT_NAME, ""),
16149 },
16150 },
16151 { } /* terminating entry */
16152 },
16153 .hook = quirk_invert_brightness,
16154 },
16155};
16156
c43b5634 16157static struct intel_quirk intel_quirks[] = {
b690e96c
JB
16158 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
16159 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
16160
b690e96c
JB
16161 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
16162 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
16163
5f080c0f
VS
16164 /* 830 needs to leave pipe A & dpll A up */
16165 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
16166
b6b5d049
VS
16167 /* 830 needs to leave pipe B & dpll B up */
16168 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
16169
435793df
KP
16170 /* Lenovo U160 cannot use SSC on LVDS */
16171 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
16172
16173 /* Sony Vaio Y cannot use SSC on LVDS */
16174 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 16175
be505f64
AH
16176 /* Acer Aspire 5734Z must invert backlight brightness */
16177 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
16178
16179 /* Acer/eMachines G725 */
16180 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
16181
16182 /* Acer/eMachines e725 */
16183 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
16184
16185 /* Acer/Packard Bell NCL20 */
16186 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
16187
16188 /* Acer Aspire 4736Z */
16189 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
16190
16191 /* Acer Aspire 5336 */
16192 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
2e93a1aa
SD
16193
16194 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
16195 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
d4967d8c 16196
dfb3d47b
SD
16197 /* Acer C720 Chromebook (Core i3 4005U) */
16198 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
16199
b2a9601c 16200 /* Apple Macbook 2,1 (Core 2 T7400) */
16201 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
16202
1b9448b0
JN
16203 /* Apple Macbook 4,1 */
16204 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
16205
d4967d8c
SD
16206 /* Toshiba CB35 Chromebook (Celeron 2955U) */
16207 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
724cb06f
SD
16208
16209 /* HP Chromebook 14 (Celeron 2955U) */
16210 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
cf6f0af9
JN
16211
16212 /* Dell Chromebook 11 */
16213 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
9be64eee
JN
16214
16215 /* Dell Chromebook 11 (2015 version) */
16216 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
b690e96c
JB
16217};
16218
16219static void intel_init_quirks(struct drm_device *dev)
16220{
16221 struct pci_dev *d = dev->pdev;
16222 int i;
16223
16224 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
16225 struct intel_quirk *q = &intel_quirks[i];
16226
16227 if (d->device == q->device &&
16228 (d->subsystem_vendor == q->subsystem_vendor ||
16229 q->subsystem_vendor == PCI_ANY_ID) &&
16230 (d->subsystem_device == q->subsystem_device ||
16231 q->subsystem_device == PCI_ANY_ID))
16232 q->hook(dev);
16233 }
5f85f176
EE
16234 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
16235 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
16236 intel_dmi_quirks[i].hook(dev);
16237 }
b690e96c
JB
16238}
16239
9cce37f4
JB
16240/* Disable the VGA plane that we never use */
16241static void i915_disable_vga(struct drm_device *dev)
16242{
fac5e23e 16243 struct drm_i915_private *dev_priv = to_i915(dev);
52a05c30 16244 struct pci_dev *pdev = dev_priv->drm.pdev;
9cce37f4 16245 u8 sr1;
920a14b2 16246 i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
9cce37f4 16247
2b37c616 16248 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
52a05c30 16249 vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 16250 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
16251 sr1 = inb(VGA_SR_DATA);
16252 outb(sr1 | 1<<5, VGA_SR_DATA);
52a05c30 16253 vga_put(pdev, VGA_RSRC_LEGACY_IO);
9cce37f4
JB
16254 udelay(300);
16255
01f5a626 16256 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9cce37f4
JB
16257 POSTING_READ(vga_reg);
16258}
16259
f817586c
SV
16260void intel_modeset_init_hw(struct drm_device *dev)
16261{
fac5e23e 16262 struct drm_i915_private *dev_priv = to_i915(dev);
1a617b77 16263
b6283055 16264 intel_update_cdclk(dev);
1a617b77
ML
16265
16266 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
16267
f817586c 16268 intel_init_clock_gating(dev);
f817586c
SV
16269}
16270
d93c0372
MR
16271/*
16272 * Calculate what we think the watermarks should be for the state we've read
16273 * out of the hardware and then immediately program those watermarks so that
16274 * we ensure the hardware settings match our internal state.
16275 *
16276 * We can calculate what we think WM's should be by creating a duplicate of the
16277 * current state (which was constructed during hardware readout) and running it
16278 * through the atomic check code to calculate new watermark values in the
16279 * state object.
16280 */
16281static void sanitize_watermarks(struct drm_device *dev)
16282{
16283 struct drm_i915_private *dev_priv = to_i915(dev);
16284 struct drm_atomic_state *state;
16285 struct drm_crtc *crtc;
16286 struct drm_crtc_state *cstate;
16287 struct drm_modeset_acquire_ctx ctx;
16288 int ret;
16289 int i;
16290
16291 /* Only supported on platforms that use atomic watermark design */
ed4a6a7c 16292 if (!dev_priv->display.optimize_watermarks)
d93c0372
MR
16293 return;
16294
16295 /*
16296 * We need to hold connection_mutex before calling duplicate_state so
16297 * that the connector loop is protected.
16298 */
16299 drm_modeset_acquire_init(&ctx, 0);
16300retry:
0cd1262d 16301 ret = drm_modeset_lock_all_ctx(dev, &ctx);
d93c0372
MR
16302 if (ret == -EDEADLK) {
16303 drm_modeset_backoff(&ctx);
16304 goto retry;
16305 } else if (WARN_ON(ret)) {
0cd1262d 16306 goto fail;
d93c0372
MR
16307 }
16308
16309 state = drm_atomic_helper_duplicate_state(dev, &ctx);
16310 if (WARN_ON(IS_ERR(state)))
0cd1262d 16311 goto fail;
d93c0372 16312
ed4a6a7c
MR
16313 /*
16314 * Hardware readout is the only time we don't want to calculate
16315 * intermediate watermarks (since we don't trust the current
16316 * watermarks).
16317 */
16318 to_intel_atomic_state(state)->skip_intermediate_wm = true;
16319
d93c0372
MR
16320 ret = intel_atomic_check(dev, state);
16321 if (ret) {
16322 /*
16323 * If we fail here, it means that the hardware appears to be
16324 * programmed in a way that shouldn't be possible, given our
16325 * understanding of watermark requirements. This might mean a
16326 * mistake in the hardware readout code or a mistake in the
16327 * watermark calculations for a given platform. Raise a WARN
16328 * so that this is noticeable.
16329 *
16330 * If this actually happens, we'll have to just leave the
16331 * BIOS-programmed watermarks untouched and hope for the best.
16332 */
16333 WARN(true, "Could not determine valid watermarks for inherited state\n");
0cd1262d 16334 goto fail;
d93c0372
MR
16335 }
16336
16337 /* Write calculated watermark values back */
d93c0372
MR
16338 for_each_crtc_in_state(state, crtc, cstate, i) {
16339 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
16340
ed4a6a7c
MR
16341 cs->wm.need_postvbl_update = true;
16342 dev_priv->display.optimize_watermarks(cs);
d93c0372
MR
16343 }
16344
16345 drm_atomic_state_free(state);
0cd1262d 16346fail:
d93c0372
MR
16347 drm_modeset_drop_locks(&ctx);
16348 drm_modeset_acquire_fini(&ctx);
16349}
16350
79e53945
JB
16351void intel_modeset_init(struct drm_device *dev)
16352{
72e96d64
JL
16353 struct drm_i915_private *dev_priv = to_i915(dev);
16354 struct i915_ggtt *ggtt = &dev_priv->ggtt;
1fe47785 16355 int sprite, ret;
8cc87b75 16356 enum pipe pipe;
46f297fb 16357 struct intel_crtc *crtc;
79e53945
JB
16358
16359 drm_mode_config_init(dev);
16360
16361 dev->mode_config.min_width = 0;
16362 dev->mode_config.min_height = 0;
16363
019d96cb
DA
16364 dev->mode_config.preferred_depth = 24;
16365 dev->mode_config.prefer_shadow = 1;
16366
25bab385
TU
16367 dev->mode_config.allow_fb_modifiers = true;
16368
e6ecefaa 16369 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 16370
b690e96c
JB
16371 intel_init_quirks(dev);
16372
1fa61106
ED
16373 intel_init_pm(dev);
16374
e3c74757
BW
16375 if (INTEL_INFO(dev)->num_pipes == 0)
16376 return;
16377
69f92f67
LW
16378 /*
16379 * There may be no VBT; and if the BIOS enabled SSC we can
16380 * just keep using it to avoid unnecessary flicker. Whereas if the
16381 * BIOS isn't using it, don't assume it will work even if the VBT
16382 * indicates as much.
16383 */
6e266956 16384 if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
69f92f67
LW
16385 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
16386 DREF_SSC1_ENABLE);
16387
16388 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
16389 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
16390 bios_lvds_use_ssc ? "en" : "dis",
16391 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
16392 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
16393 }
16394 }
16395
5db94019 16396 if (IS_GEN2(dev_priv)) {
a6c45cf0
CW
16397 dev->mode_config.max_width = 2048;
16398 dev->mode_config.max_height = 2048;
5db94019 16399 } else if (IS_GEN3(dev_priv)) {
5e4d6fa7
KP
16400 dev->mode_config.max_width = 4096;
16401 dev->mode_config.max_height = 4096;
79e53945 16402 } else {
a6c45cf0
CW
16403 dev->mode_config.max_width = 8192;
16404 dev->mode_config.max_height = 8192;
79e53945 16405 }
068be561 16406
50a0bc90
TU
16407 if (IS_845G(dev_priv) || IS_I865G(dev_priv)) {
16408 dev->mode_config.cursor_width = IS_845G(dev_priv) ? 64 : 512;
dc41c154 16409 dev->mode_config.cursor_height = 1023;
5db94019 16410 } else if (IS_GEN2(dev_priv)) {
068be561
DL
16411 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
16412 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
16413 } else {
16414 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
16415 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
16416 }
16417
72e96d64 16418 dev->mode_config.fb_base = ggtt->mappable_base;
79e53945 16419
28c97730 16420 DRM_DEBUG_KMS("%d display pipe%s available.\n",
7eb552ae
BW
16421 INTEL_INFO(dev)->num_pipes,
16422 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
79e53945 16423
055e393f 16424 for_each_pipe(dev_priv, pipe) {
8cc87b75 16425 intel_crtc_init(dev, pipe);
3bdcfc0c 16426 for_each_sprite(dev_priv, pipe, sprite) {
1fe47785 16427 ret = intel_plane_init(dev, pipe, sprite);
7f1f3851 16428 if (ret)
06da8da2 16429 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
1fe47785 16430 pipe_name(pipe), sprite_name(pipe, sprite), ret);
7f1f3851 16431 }
79e53945
JB
16432 }
16433
bfa7df01
VS
16434 intel_update_czclk(dev_priv);
16435 intel_update_cdclk(dev);
16436
e72f9fbf 16437 intel_shared_dpll_init(dev);
ee7b9f93 16438
b2045352
VS
16439 if (dev_priv->max_cdclk_freq == 0)
16440 intel_update_max_cdclk(dev);
16441
9cce37f4
JB
16442 /* Just disable it once at startup */
16443 i915_disable_vga(dev);
79e53945 16444 intel_setup_outputs(dev);
11be49eb 16445
6e9f798d 16446 drm_modeset_lock_all(dev);
043e9bda 16447 intel_modeset_setup_hw_state(dev);
6e9f798d 16448 drm_modeset_unlock_all(dev);
46f297fb 16449
d3fcc808 16450 for_each_intel_crtc(dev, crtc) {
eeebeac5
ML
16451 struct intel_initial_plane_config plane_config = {};
16452
46f297fb
JB
16453 if (!crtc->active)
16454 continue;
16455
46f297fb 16456 /*
46f297fb
JB
16457 * Note that reserving the BIOS fb up front prevents us
16458 * from stuffing other stolen allocations like the ring
16459 * on top. This prevents some ugliness at boot time, and
16460 * can even allow for smooth boot transitions if the BIOS
16461 * fb is large enough for the active pipe configuration.
16462 */
eeebeac5
ML
16463 dev_priv->display.get_initial_plane_config(crtc,
16464 &plane_config);
16465
16466 /*
16467 * If the fb is shared between multiple heads, we'll
16468 * just get the first one.
16469 */
16470 intel_find_initial_plane_obj(crtc, &plane_config);
46f297fb 16471 }
d93c0372
MR
16472
16473 /*
16474 * Make sure hardware watermarks really match the state we read out.
16475 * Note that we need to do this after reconstructing the BIOS fb's
16476 * since the watermark calculation done here will use pstate->fb.
16477 */
16478 sanitize_watermarks(dev);
2c7111db
CW
16479}
16480
7fad798e
SV
16481static void intel_enable_pipe_a(struct drm_device *dev)
16482{
16483 struct intel_connector *connector;
16484 struct drm_connector *crt = NULL;
16485 struct intel_load_detect_pipe load_detect_temp;
208bf9fd 16486 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
7fad798e
SV
16487
16488 /* We can't just switch on the pipe A, we need to set things up with a
16489 * proper mode and output configuration. As a gross hack, enable pipe A
16490 * by enabling the load detect pipe once. */
3a3371ff 16491 for_each_intel_connector(dev, connector) {
7fad798e
SV
16492 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
16493 crt = &connector->base;
16494 break;
16495 }
16496 }
16497
16498 if (!crt)
16499 return;
16500
208bf9fd 16501 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
49172fee 16502 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
7fad798e
SV
16503}
16504
fa555837
SV
16505static bool
16506intel_check_plane_mapping(struct intel_crtc *crtc)
16507{
7eb552ae 16508 struct drm_device *dev = crtc->base.dev;
fac5e23e 16509 struct drm_i915_private *dev_priv = to_i915(dev);
649636ef 16510 u32 val;
fa555837 16511
7eb552ae 16512 if (INTEL_INFO(dev)->num_pipes == 1)
fa555837
SV
16513 return true;
16514
649636ef 16515 val = I915_READ(DSPCNTR(!crtc->plane));
fa555837
SV
16516
16517 if ((val & DISPLAY_PLANE_ENABLE) &&
16518 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
16519 return false;
16520
16521 return true;
16522}
16523
02e93c35
VS
16524static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
16525{
16526 struct drm_device *dev = crtc->base.dev;
16527 struct intel_encoder *encoder;
16528
16529 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
16530 return true;
16531
16532 return false;
16533}
16534
496b0fc3
ML
16535static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
16536{
16537 struct drm_device *dev = encoder->base.dev;
16538 struct intel_connector *connector;
16539
16540 for_each_connector_on_encoder(dev, &encoder->base, connector)
16541 return connector;
16542
16543 return NULL;
16544}
16545
a168f5b3
VS
16546static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
16547 enum transcoder pch_transcoder)
16548{
16549 return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
16550 (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == TRANSCODER_A);
16551}
16552
24929352
SV
16553static void intel_sanitize_crtc(struct intel_crtc *crtc)
16554{
16555 struct drm_device *dev = crtc->base.dev;
fac5e23e 16556 struct drm_i915_private *dev_priv = to_i915(dev);
4d1de975 16557 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
24929352 16558
24929352 16559 /* Clear any frame start delays used for debugging left by the BIOS */
4d1de975
JN
16560 if (!transcoder_is_dsi(cpu_transcoder)) {
16561 i915_reg_t reg = PIPECONF(cpu_transcoder);
16562
16563 I915_WRITE(reg,
16564 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
16565 }
24929352 16566
d3eaf884 16567 /* restore vblank interrupts to correct state */
9625604c 16568 drm_crtc_vblank_reset(&crtc->base);
d297e103 16569 if (crtc->active) {
f9cd7b88
VS
16570 struct intel_plane *plane;
16571
9625604c 16572 drm_crtc_vblank_on(&crtc->base);
f9cd7b88
VS
16573
16574 /* Disable everything but the primary plane */
16575 for_each_intel_plane_on_crtc(dev, crtc, plane) {
16576 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
16577 continue;
16578
16579 plane->disable_plane(&plane->base, &crtc->base);
16580 }
9625604c 16581 }
d3eaf884 16582
24929352 16583 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
SV
16584 * disable the crtc (and hence change the state) if it is wrong. Note
16585 * that gen4+ has a fixed plane -> pipe mapping. */
16586 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
24929352
SV
16587 bool plane;
16588
78108b7c
VS
16589 DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
16590 crtc->base.base.id, crtc->base.name);
24929352
SV
16591
16592 /* Pipe has the wrong plane attached and the plane is active.
16593 * Temporarily change the plane mapping and disable everything
16594 * ... */
16595 plane = crtc->plane;
936e71e3 16596 to_intel_plane_state(crtc->base.primary->state)->base.visible = true;
24929352 16597 crtc->plane = !plane;
b17d48e2 16598 intel_crtc_disable_noatomic(&crtc->base);
24929352 16599 crtc->plane = plane;
24929352 16600 }
24929352 16601
7fad798e
SV
16602 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
16603 crtc->pipe == PIPE_A && !crtc->active) {
16604 /* BIOS forgot to enable pipe A, this mostly happens after
16605 * resume. Force-enable the pipe to fix this, the update_dpms
16606 * call below we restore the pipe to the right state, but leave
16607 * the required bits on. */
16608 intel_enable_pipe_a(dev);
16609 }
16610
24929352
SV
16611 /* Adjust the state of the output pipe according to whether we
16612 * have active connectors/encoders. */
842e0307 16613 if (crtc->active && !intel_crtc_has_encoders(crtc))
b17d48e2 16614 intel_crtc_disable_noatomic(&crtc->base);
24929352 16615
49cff963 16616 if (crtc->active || HAS_GMCH_DISPLAY(dev_priv)) {
4cc31489
SV
16617 /*
16618 * We start out with underrun reporting disabled to avoid races.
16619 * For correct bookkeeping mark this on active crtcs.
16620 *
c5ab3bc0
SV
16621 * Also on gmch platforms we dont have any hardware bits to
16622 * disable the underrun reporting. Which means we need to start
16623 * out with underrun reporting disabled also on inactive pipes,
16624 * since otherwise we'll complain about the garbage we read when
16625 * e.g. coming up after runtime pm.
16626 *
4cc31489
SV
16627 * No protection against concurrent access is required - at
16628 * worst a fifo underrun happens which also sets this to false.
16629 */
16630 crtc->cpu_fifo_underrun_disabled = true;
a168f5b3
VS
16631 /*
16632 * We track the PCH trancoder underrun reporting state
16633 * within the crtc. With crtc for pipe A housing the underrun
16634 * reporting state for PCH transcoder A, crtc for pipe B housing
16635 * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
16636 * and marking underrun reporting as disabled for the non-existing
16637 * PCH transcoders B and C would prevent enabling the south
16638 * error interrupt (see cpt_can_enable_serr_int()).
16639 */
16640 if (has_pch_trancoder(dev_priv, (enum transcoder)crtc->pipe))
16641 crtc->pch_fifo_underrun_disabled = true;
4cc31489 16642 }
24929352
SV
16643}
16644
16645static void intel_sanitize_encoder(struct intel_encoder *encoder)
16646{
16647 struct intel_connector *connector;
24929352
SV
16648
16649 /* We need to check both for a crtc link (meaning that the
16650 * encoder is active and trying to read from a pipe) and the
16651 * pipe itself being active. */
16652 bool has_active_crtc = encoder->base.crtc &&
16653 to_intel_crtc(encoder->base.crtc)->active;
16654
496b0fc3
ML
16655 connector = intel_encoder_find_connector(encoder);
16656 if (connector && !has_active_crtc) {
24929352
SV
16657 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
16658 encoder->base.base.id,
8e329a03 16659 encoder->base.name);
24929352
SV
16660
16661 /* Connector is active, but has no active pipe. This is
16662 * fallout from our resume register restoring. Disable
16663 * the encoder manually again. */
16664 if (encoder->base.crtc) {
fd6bbda9
ML
16665 struct drm_crtc_state *crtc_state = encoder->base.crtc->state;
16666
24929352
SV
16667 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
16668 encoder->base.base.id,
8e329a03 16669 encoder->base.name);
fd6bbda9 16670 encoder->disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
a62d1497 16671 if (encoder->post_disable)
fd6bbda9 16672 encoder->post_disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
24929352 16673 }
7f1950fb 16674 encoder->base.crtc = NULL;
24929352
SV
16675
16676 /* Inconsistent output/port/pipe state happens presumably due to
16677 * a bug in one of the get_hw_state functions. Or someplace else
16678 * in our code, like the register restore mess on resume. Clamp
16679 * things to off as a safer default. */
fd6bbda9
ML
16680
16681 connector->base.dpms = DRM_MODE_DPMS_OFF;
16682 connector->base.encoder = NULL;
24929352
SV
16683 }
16684 /* Enabled encoders without active connectors will be fixed in
16685 * the crtc fixup. */
16686}
16687
04098753 16688void i915_redisable_vga_power_on(struct drm_device *dev)
0fde901f 16689{
fac5e23e 16690 struct drm_i915_private *dev_priv = to_i915(dev);
920a14b2 16691 i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
0fde901f 16692
04098753
ID
16693 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
16694 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
16695 i915_disable_vga(dev);
16696 }
16697}
16698
16699void i915_redisable_vga(struct drm_device *dev)
16700{
fac5e23e 16701 struct drm_i915_private *dev_priv = to_i915(dev);
04098753 16702
8dc8a27c
PZ
16703 /* This function can be called both from intel_modeset_setup_hw_state or
16704 * at a very early point in our resume sequence, where the power well
16705 * structures are not yet restored. Since this function is at a very
16706 * paranoid "someone might have enabled VGA while we were not looking"
16707 * level, just check if the power well is enabled instead of trying to
16708 * follow the "don't touch the power well if we don't need it" policy
16709 * the rest of the driver uses. */
6392f847 16710 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
16711 return;
16712
04098753 16713 i915_redisable_vga_power_on(dev);
6392f847
ID
16714
16715 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
0fde901f
KM
16716}
16717
f9cd7b88 16718static bool primary_get_hw_state(struct intel_plane *plane)
98ec7739 16719{
f9cd7b88 16720 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
98ec7739 16721
f9cd7b88 16722 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
d032ffa0
ML
16723}
16724
f9cd7b88
VS
16725/* FIXME read out full plane state for all planes */
16726static void readout_plane_state(struct intel_crtc *crtc)
d032ffa0 16727{
b26d3ea3 16728 struct drm_plane *primary = crtc->base.primary;
f9cd7b88 16729 struct intel_plane_state *plane_state =
b26d3ea3 16730 to_intel_plane_state(primary->state);
d032ffa0 16731
936e71e3 16732 plane_state->base.visible = crtc->active &&
b26d3ea3
ML
16733 primary_get_hw_state(to_intel_plane(primary));
16734
936e71e3 16735 if (plane_state->base.visible)
b26d3ea3 16736 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
98ec7739
VS
16737}
16738
30e984df 16739static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352 16740{
fac5e23e 16741 struct drm_i915_private *dev_priv = to_i915(dev);
24929352 16742 enum pipe pipe;
24929352
SV
16743 struct intel_crtc *crtc;
16744 struct intel_encoder *encoder;
16745 struct intel_connector *connector;
5358901f 16746 int i;
24929352 16747
565602d7
ML
16748 dev_priv->active_crtcs = 0;
16749
d3fcc808 16750 for_each_intel_crtc(dev, crtc) {
565602d7
ML
16751 struct intel_crtc_state *crtc_state = crtc->config;
16752 int pixclk = 0;
3b117c8f 16753
ec2dc6a0 16754 __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
565602d7
ML
16755 memset(crtc_state, 0, sizeof(*crtc_state));
16756 crtc_state->base.crtc = &crtc->base;
24929352 16757
565602d7
ML
16758 crtc_state->base.active = crtc_state->base.enable =
16759 dev_priv->display.get_pipe_config(crtc, crtc_state);
16760
16761 crtc->base.enabled = crtc_state->base.enable;
16762 crtc->active = crtc_state->base.active;
16763
16764 if (crtc_state->base.active) {
16765 dev_priv->active_crtcs |= 1 << crtc->pipe;
16766
c89e39f3 16767 if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
565602d7 16768 pixclk = ilk_pipe_pixel_rate(crtc_state);
9558d15d 16769 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
565602d7
ML
16770 pixclk = crtc_state->base.adjusted_mode.crtc_clock;
16771 else
16772 WARN_ON(dev_priv->display.modeset_calc_cdclk);
9558d15d
VS
16773
16774 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
16775 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
16776 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
565602d7
ML
16777 }
16778
16779 dev_priv->min_pixclk[crtc->pipe] = pixclk;
b70709a6 16780
f9cd7b88 16781 readout_plane_state(crtc);
24929352 16782
78108b7c
VS
16783 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
16784 crtc->base.base.id, crtc->base.name,
24929352
SV
16785 crtc->active ? "enabled" : "disabled");
16786 }
16787
5358901f
SV
16788 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
16789 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
16790
2edd6443
ACO
16791 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
16792 &pll->config.hw_state);
3e369b76 16793 pll->config.crtc_mask = 0;
d3fcc808 16794 for_each_intel_crtc(dev, crtc) {
2dd66ebd 16795 if (crtc->active && crtc->config->shared_dpll == pll)
3e369b76 16796 pll->config.crtc_mask |= 1 << crtc->pipe;
5358901f 16797 }
2dd66ebd 16798 pll->active_mask = pll->config.crtc_mask;
5358901f 16799
1e6f2ddc 16800 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
3e369b76 16801 pll->name, pll->config.crtc_mask, pll->on);
5358901f
SV
16802 }
16803
b2784e15 16804 for_each_intel_encoder(dev, encoder) {
24929352
SV
16805 pipe = 0;
16806
16807 if (encoder->get_hw_state(encoder, &pipe)) {
045ac3b5
JB
16808 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
16809 encoder->base.crtc = &crtc->base;
253c84c8 16810 crtc->config->output_types |= 1 << encoder->type;
6e3c9717 16811 encoder->get_config(encoder, crtc->config);
24929352
SV
16812 } else {
16813 encoder->base.crtc = NULL;
16814 }
16815
6f2bcceb 16816 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
24929352 16817 encoder->base.base.id,
8e329a03 16818 encoder->base.name,
24929352 16819 encoder->base.crtc ? "enabled" : "disabled",
6f2bcceb 16820 pipe_name(pipe));
24929352
SV
16821 }
16822
3a3371ff 16823 for_each_intel_connector(dev, connector) {
24929352
SV
16824 if (connector->get_hw_state(connector)) {
16825 connector->base.dpms = DRM_MODE_DPMS_ON;
2aa974c9
ML
16826
16827 encoder = connector->encoder;
16828 connector->base.encoder = &encoder->base;
16829
16830 if (encoder->base.crtc &&
16831 encoder->base.crtc->state->active) {
16832 /*
16833 * This has to be done during hardware readout
16834 * because anything calling .crtc_disable may
16835 * rely on the connector_mask being accurate.
16836 */
16837 encoder->base.crtc->state->connector_mask |=
16838 1 << drm_connector_index(&connector->base);
e87a52b3
ML
16839 encoder->base.crtc->state->encoder_mask |=
16840 1 << drm_encoder_index(&encoder->base);
2aa974c9
ML
16841 }
16842
24929352
SV
16843 } else {
16844 connector->base.dpms = DRM_MODE_DPMS_OFF;
16845 connector->base.encoder = NULL;
16846 }
16847 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
16848 connector->base.base.id,
c23cc417 16849 connector->base.name,
24929352
SV
16850 connector->base.encoder ? "enabled" : "disabled");
16851 }
7f4c6284
VS
16852
16853 for_each_intel_crtc(dev, crtc) {
16854 crtc->base.hwmode = crtc->config->base.adjusted_mode;
16855
16856 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
16857 if (crtc->base.state->active) {
16858 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
16859 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
16860 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
16861
16862 /*
16863 * The initial mode needs to be set in order to keep
16864 * the atomic core happy. It wants a valid mode if the
16865 * crtc's enabled, so we do the above call.
16866 *
16867 * At this point some state updated by the connectors
16868 * in their ->detect() callback has not run yet, so
16869 * no recalculation can be done yet.
16870 *
16871 * Even if we could do a recalculation and modeset
16872 * right now it would cause a double modeset if
16873 * fbdev or userspace chooses a different initial mode.
16874 *
16875 * If that happens, someone indicated they wanted a
16876 * mode change, which means it's safe to do a full
16877 * recalculation.
16878 */
16879 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
9eca6832
VS
16880
16881 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
16882 update_scanline_offset(crtc);
7f4c6284 16883 }
e3b247da
VS
16884
16885 intel_pipe_config_sanity_check(dev_priv, crtc->config);
7f4c6284 16886 }
30e984df
SV
16887}
16888
043e9bda
ML
16889/* Scan out the current hw modeset state,
16890 * and sanitizes it to the current state
16891 */
16892static void
16893intel_modeset_setup_hw_state(struct drm_device *dev)
30e984df 16894{
fac5e23e 16895 struct drm_i915_private *dev_priv = to_i915(dev);
30e984df 16896 enum pipe pipe;
30e984df
SV
16897 struct intel_crtc *crtc;
16898 struct intel_encoder *encoder;
35c95375 16899 int i;
30e984df
SV
16900
16901 intel_modeset_readout_hw_state(dev);
24929352
SV
16902
16903 /* HW state is read out, now we need to sanitize this mess. */
b2784e15 16904 for_each_intel_encoder(dev, encoder) {
24929352
SV
16905 intel_sanitize_encoder(encoder);
16906 }
16907
055e393f 16908 for_each_pipe(dev_priv, pipe) {
24929352
SV
16909 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
16910 intel_sanitize_crtc(crtc);
6e3c9717
ACO
16911 intel_dump_pipe_config(crtc, crtc->config,
16912 "[setup_hw_state]");
24929352 16913 }
9a935856 16914
d29b2f9d
ACO
16915 intel_modeset_update_connector_atomic_state(dev);
16916
35c95375
SV
16917 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
16918 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
16919
2dd66ebd 16920 if (!pll->on || pll->active_mask)
35c95375
SV
16921 continue;
16922
16923 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
16924
2edd6443 16925 pll->funcs.disable(dev_priv, pll);
35c95375
SV
16926 pll->on = false;
16927 }
16928
920a14b2 16929 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
6eb1a681 16930 vlv_wm_get_hw_state(dev);
5db94019 16931 else if (IS_GEN9(dev_priv))
3078999f 16932 skl_wm_get_hw_state(dev);
6e266956 16933 else if (HAS_PCH_SPLIT(dev_priv))
243e6a44 16934 ilk_wm_get_hw_state(dev);
292b990e
ML
16935
16936 for_each_intel_crtc(dev, crtc) {
16937 unsigned long put_domains;
16938
74bff5f9 16939 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
292b990e
ML
16940 if (WARN_ON(put_domains))
16941 modeset_put_power_domains(dev_priv, put_domains);
16942 }
16943 intel_display_set_init_power(dev_priv, false);
010cf73d
PZ
16944
16945 intel_fbc_init_pipe_state(dev_priv);
043e9bda 16946}
7d0bc1ea 16947
043e9bda
ML
16948void intel_display_resume(struct drm_device *dev)
16949{
e2c8b870
ML
16950 struct drm_i915_private *dev_priv = to_i915(dev);
16951 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
16952 struct drm_modeset_acquire_ctx ctx;
043e9bda 16953 int ret;
f30da187 16954
e2c8b870 16955 dev_priv->modeset_restore_state = NULL;
73974893
ML
16956 if (state)
16957 state->acquire_ctx = &ctx;
043e9bda 16958
ea49c9ac
ML
16959 /*
16960 * This is a cludge because with real atomic modeset mode_config.mutex
16961 * won't be taken. Unfortunately some probed state like
16962 * audio_codec_enable is still protected by mode_config.mutex, so lock
16963 * it here for now.
16964 */
16965 mutex_lock(&dev->mode_config.mutex);
e2c8b870 16966 drm_modeset_acquire_init(&ctx, 0);
043e9bda 16967
73974893
ML
16968 while (1) {
16969 ret = drm_modeset_lock_all_ctx(dev, &ctx);
16970 if (ret != -EDEADLK)
16971 break;
043e9bda 16972
e2c8b870 16973 drm_modeset_backoff(&ctx);
e2c8b870 16974 }
043e9bda 16975
73974893
ML
16976 if (!ret)
16977 ret = __intel_display_resume(dev, state);
16978
e2c8b870
ML
16979 drm_modeset_drop_locks(&ctx);
16980 drm_modeset_acquire_fini(&ctx);
ea49c9ac 16981 mutex_unlock(&dev->mode_config.mutex);
043e9bda 16982
e2c8b870
ML
16983 if (ret) {
16984 DRM_ERROR("Restoring old state failed with %i\n", ret);
16985 drm_atomic_state_free(state);
16986 }
2c7111db
CW
16987}
16988
16989void intel_modeset_gem_init(struct drm_device *dev)
16990{
dc97997a 16991 struct drm_i915_private *dev_priv = to_i915(dev);
484b41dd 16992 struct drm_crtc *c;
2ff8fde1 16993 struct drm_i915_gem_object *obj;
484b41dd 16994
dc97997a 16995 intel_init_gt_powersave(dev_priv);
ae48434c 16996
1833b134 16997 intel_modeset_init_hw(dev);
02e792fb 16998
1ee8da6d 16999 intel_setup_overlay(dev_priv);
484b41dd
JB
17000
17001 /*
17002 * Make sure any fbs we allocated at startup are properly
17003 * pinned & fenced. When we do the allocation it's too early
17004 * for this.
17005 */
70e1e0ec 17006 for_each_crtc(dev, c) {
058d88c4
CW
17007 struct i915_vma *vma;
17008
2ff8fde1
MR
17009 obj = intel_fb_obj(c->primary->fb);
17010 if (obj == NULL)
484b41dd
JB
17011 continue;
17012
e0d6149b 17013 mutex_lock(&dev->struct_mutex);
058d88c4 17014 vma = intel_pin_and_fence_fb_obj(c->primary->fb,
3465c580 17015 c->primary->state->rotation);
e0d6149b 17016 mutex_unlock(&dev->struct_mutex);
058d88c4 17017 if (IS_ERR(vma)) {
484b41dd
JB
17018 DRM_ERROR("failed to pin boot fb on pipe %d\n",
17019 to_intel_crtc(c)->pipe);
66e514c1 17020 drm_framebuffer_unreference(c->primary->fb);
5a21b665 17021 c->primary->fb = NULL;
36750f28 17022 c->primary->crtc = c->primary->state->crtc = NULL;
5a21b665 17023 update_state_fb(c->primary);
36750f28 17024 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
484b41dd
JB
17025 }
17026 }
1ebaa0b9
CW
17027}
17028
17029int intel_connector_register(struct drm_connector *connector)
17030{
17031 struct intel_connector *intel_connector = to_intel_connector(connector);
17032 int ret;
17033
17034 ret = intel_backlight_device_register(intel_connector);
17035 if (ret)
17036 goto err;
17037
17038 return 0;
0962c3c9 17039
1ebaa0b9
CW
17040err:
17041 return ret;
79e53945
JB
17042}
17043
c191eca1 17044void intel_connector_unregister(struct drm_connector *connector)
4932e2c3 17045{
e63d87c0 17046 struct intel_connector *intel_connector = to_intel_connector(connector);
4932e2c3 17047
e63d87c0 17048 intel_backlight_device_unregister(intel_connector);
4932e2c3 17049 intel_panel_destroy_backlight(connector);
4932e2c3
ID
17050}
17051
79e53945
JB
17052void intel_modeset_cleanup(struct drm_device *dev)
17053{
fac5e23e 17054 struct drm_i915_private *dev_priv = to_i915(dev);
652c393a 17055
dc97997a 17056 intel_disable_gt_powersave(dev_priv);
2eb5252e 17057
fd0c0642
SV
17058 /*
17059 * Interrupts and polling as the first thing to avoid creating havoc.
2eb5252e 17060 * Too much stuff here (turning of connectors, ...) would
fd0c0642
SV
17061 * experience fancy races otherwise.
17062 */
2aeb7d3a 17063 intel_irq_uninstall(dev_priv);
eb21b92b 17064
fd0c0642
SV
17065 /*
17066 * Due to the hpd irq storm handling the hotplug work can re-arm the
17067 * poll handlers. Hence disable polling after hpd handling is shut down.
17068 */
f87ea761 17069 drm_kms_helper_poll_fini(dev);
fd0c0642 17070
723bfd70
JB
17071 intel_unregister_dsm_handler();
17072
c937ab3e 17073 intel_fbc_global_disable(dev_priv);
69341a5e 17074
1630fe75
CW
17075 /* flush any delayed tasks or pending work */
17076 flush_scheduled_work();
17077
79e53945 17078 drm_mode_config_cleanup(dev);
4d7bb011 17079
1ee8da6d 17080 intel_cleanup_overlay(dev_priv);
ae48434c 17081
dc97997a 17082 intel_cleanup_gt_powersave(dev_priv);
f5949141
SV
17083
17084 intel_teardown_gmbus(dev);
79e53945
JB
17085}
17086
df0e9248
CW
17087void intel_connector_attach_encoder(struct intel_connector *connector,
17088 struct intel_encoder *encoder)
17089{
17090 connector->encoder = encoder;
17091 drm_mode_connector_attach_encoder(&connector->base,
17092 &encoder->base);
79e53945 17093}
28d52043
DA
17094
17095/*
17096 * set vga decode state - true == enable VGA decode
17097 */
17098int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
17099{
fac5e23e 17100 struct drm_i915_private *dev_priv = to_i915(dev);
a885b3cc 17101 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
17102 u16 gmch_ctrl;
17103
75fa041d
CW
17104 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
17105 DRM_ERROR("failed to read control word\n");
17106 return -EIO;
17107 }
17108
c0cc8a55
CW
17109 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
17110 return 0;
17111
28d52043
DA
17112 if (state)
17113 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
17114 else
17115 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
17116
17117 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
17118 DRM_ERROR("failed to write control word\n");
17119 return -EIO;
17120 }
17121
28d52043
DA
17122 return 0;
17123}
c4a1d9e4 17124
98a2f411
CW
17125#if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
17126
c4a1d9e4 17127struct intel_display_error_state {
ff57f1b0
PZ
17128
17129 u32 power_well_driver;
17130
63b66e5b
CW
17131 int num_transcoders;
17132
c4a1d9e4
CW
17133 struct intel_cursor_error_state {
17134 u32 control;
17135 u32 position;
17136 u32 base;
17137 u32 size;
52331309 17138 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
17139
17140 struct intel_pipe_error_state {
ddf9c536 17141 bool power_domain_on;
c4a1d9e4 17142 u32 source;
f301b1e1 17143 u32 stat;
52331309 17144 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
17145
17146 struct intel_plane_error_state {
17147 u32 control;
17148 u32 stride;
17149 u32 size;
17150 u32 pos;
17151 u32 addr;
17152 u32 surface;
17153 u32 tile_offset;
52331309 17154 } plane[I915_MAX_PIPES];
63b66e5b
CW
17155
17156 struct intel_transcoder_error_state {
ddf9c536 17157 bool power_domain_on;
63b66e5b
CW
17158 enum transcoder cpu_transcoder;
17159
17160 u32 conf;
17161
17162 u32 htotal;
17163 u32 hblank;
17164 u32 hsync;
17165 u32 vtotal;
17166 u32 vblank;
17167 u32 vsync;
17168 } transcoder[4];
c4a1d9e4
CW
17169};
17170
17171struct intel_display_error_state *
c033666a 17172intel_display_capture_error_state(struct drm_i915_private *dev_priv)
c4a1d9e4 17173{
c4a1d9e4 17174 struct intel_display_error_state *error;
63b66e5b
CW
17175 int transcoders[] = {
17176 TRANSCODER_A,
17177 TRANSCODER_B,
17178 TRANSCODER_C,
17179 TRANSCODER_EDP,
17180 };
c4a1d9e4
CW
17181 int i;
17182
c033666a 17183 if (INTEL_INFO(dev_priv)->num_pipes == 0)
63b66e5b
CW
17184 return NULL;
17185
9d1cb914 17186 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
17187 if (error == NULL)
17188 return NULL;
17189
c033666a 17190 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
ff57f1b0
PZ
17191 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
17192
055e393f 17193 for_each_pipe(dev_priv, i) {
ddf9c536 17194 error->pipe[i].power_domain_on =
f458ebbc
SV
17195 __intel_display_power_is_enabled(dev_priv,
17196 POWER_DOMAIN_PIPE(i));
ddf9c536 17197 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
17198 continue;
17199
5efb3e28
VS
17200 error->cursor[i].control = I915_READ(CURCNTR(i));
17201 error->cursor[i].position = I915_READ(CURPOS(i));
17202 error->cursor[i].base = I915_READ(CURBASE(i));
c4a1d9e4
CW
17203
17204 error->plane[i].control = I915_READ(DSPCNTR(i));
17205 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
c033666a 17206 if (INTEL_GEN(dev_priv) <= 3) {
51889b35 17207 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
17208 error->plane[i].pos = I915_READ(DSPPOS(i));
17209 }
c033666a 17210 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
ca291363 17211 error->plane[i].addr = I915_READ(DSPADDR(i));
c033666a 17212 if (INTEL_GEN(dev_priv) >= 4) {
c4a1d9e4
CW
17213 error->plane[i].surface = I915_READ(DSPSURF(i));
17214 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
17215 }
17216
c4a1d9e4 17217 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1 17218
c033666a 17219 if (HAS_GMCH_DISPLAY(dev_priv))
f301b1e1 17220 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
17221 }
17222
4d1de975 17223 /* Note: this does not include DSI transcoders. */
c033666a 17224 error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
2d1fe073 17225 if (HAS_DDI(dev_priv))
63b66e5b
CW
17226 error->num_transcoders++; /* Account for eDP. */
17227
17228 for (i = 0; i < error->num_transcoders; i++) {
17229 enum transcoder cpu_transcoder = transcoders[i];
17230
ddf9c536 17231 error->transcoder[i].power_domain_on =
f458ebbc 17232 __intel_display_power_is_enabled(dev_priv,
38cc1daf 17233 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 17234 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
17235 continue;
17236
63b66e5b
CW
17237 error->transcoder[i].cpu_transcoder = cpu_transcoder;
17238
17239 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
17240 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
17241 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
17242 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
17243 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
17244 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
17245 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
17246 }
17247
17248 return error;
17249}
17250
edc3d884
MK
17251#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
17252
c4a1d9e4 17253void
edc3d884 17254intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
17255 struct drm_device *dev,
17256 struct intel_display_error_state *error)
17257{
fac5e23e 17258 struct drm_i915_private *dev_priv = to_i915(dev);
c4a1d9e4
CW
17259 int i;
17260
63b66e5b
CW
17261 if (!error)
17262 return;
17263
edc3d884 17264 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
8652744b 17265 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
edc3d884 17266 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 17267 error->power_well_driver);
055e393f 17268 for_each_pipe(dev_priv, i) {
edc3d884 17269 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536 17270 err_printf(m, " Power: %s\n",
87ad3212 17271 onoff(error->pipe[i].power_domain_on));
edc3d884 17272 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 17273 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
17274
17275 err_printf(m, "Plane [%d]:\n", i);
17276 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
17277 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
80ca378b 17278 if (INTEL_INFO(dev)->gen <= 3) {
edc3d884
MK
17279 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
17280 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 17281 }
772c2a51 17282 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
edc3d884 17283 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
c4a1d9e4 17284 if (INTEL_INFO(dev)->gen >= 4) {
edc3d884
MK
17285 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
17286 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
17287 }
17288
edc3d884
MK
17289 err_printf(m, "Cursor [%d]:\n", i);
17290 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
17291 err_printf(m, " POS: %08x\n", error->cursor[i].position);
17292 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 17293 }
63b66e5b
CW
17294
17295 for (i = 0; i < error->num_transcoders; i++) {
da205630 17296 err_printf(m, "CPU transcoder: %s\n",
63b66e5b 17297 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536 17298 err_printf(m, " Power: %s\n",
87ad3212 17299 onoff(error->transcoder[i].power_domain_on));
63b66e5b
CW
17300 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
17301 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
17302 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
17303 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
17304 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
17305 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
17306 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
17307 }
c4a1d9e4 17308}
98a2f411
CW
17309
17310#endif
This page took 7.517281 seconds and 4 git commands to generate.