drm/i915: Remove vblank wait from hsw_enable_ips, v2.
[deliverable/linux.git] / drivers / gpu / drm / i915 / intel_display.c
CommitLineData
79e53945
JB
1/*
2 * Copyright © 2006-2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 */
26
618563e3 27#include <linux/dmi.h>
c1c7af60
JB
28#include <linux/module.h>
29#include <linux/input.h>
79e53945 30#include <linux/i2c.h>
7662c8bd 31#include <linux/kernel.h>
5a0e3ad6 32#include <linux/slab.h>
9cce37f4 33#include <linux/vgaarb.h>
e0dac65e 34#include <drm/drm_edid.h>
760285e7 35#include <drm/drmP.h>
79e53945 36#include "intel_drv.h"
760285e7 37#include <drm/i915_drm.h>
79e53945 38#include "i915_drv.h"
e5510fac 39#include "i915_trace.h"
319c1d42 40#include <drm/drm_atomic.h>
c196e1d6 41#include <drm/drm_atomic_helper.h>
760285e7
DH
42#include <drm/drm_dp_helper.h>
43#include <drm/drm_crtc_helper.h>
465c120c
MR
44#include <drm/drm_plane_helper.h>
45#include <drm/drm_rect.h>
c0f372b3 46#include <linux/dma_remapping.h>
fd8e058a
AG
47#include <linux/reservation.h>
48#include <linux/dma-buf.h>
79e53945 49
465c120c 50/* Primary plane formats for gen <= 3 */
568db4f2 51static const uint32_t i8xx_primary_formats[] = {
67fe7dc5
DL
52 DRM_FORMAT_C8,
53 DRM_FORMAT_RGB565,
465c120c 54 DRM_FORMAT_XRGB1555,
67fe7dc5 55 DRM_FORMAT_XRGB8888,
465c120c
MR
56};
57
58/* Primary plane formats for gen >= 4 */
568db4f2 59static const uint32_t i965_primary_formats[] = {
6c0fd451
DL
60 DRM_FORMAT_C8,
61 DRM_FORMAT_RGB565,
62 DRM_FORMAT_XRGB8888,
63 DRM_FORMAT_XBGR8888,
64 DRM_FORMAT_XRGB2101010,
65 DRM_FORMAT_XBGR2101010,
66};
67
68static const uint32_t skl_primary_formats[] = {
67fe7dc5
DL
69 DRM_FORMAT_C8,
70 DRM_FORMAT_RGB565,
71 DRM_FORMAT_XRGB8888,
465c120c 72 DRM_FORMAT_XBGR8888,
67fe7dc5 73 DRM_FORMAT_ARGB8888,
465c120c
MR
74 DRM_FORMAT_ABGR8888,
75 DRM_FORMAT_XRGB2101010,
465c120c 76 DRM_FORMAT_XBGR2101010,
ea916ea0
KM
77 DRM_FORMAT_YUYV,
78 DRM_FORMAT_YVYU,
79 DRM_FORMAT_UYVY,
80 DRM_FORMAT_VYUY,
465c120c
MR
81};
82
3d7d6510
MR
83/* Cursor formats */
84static const uint32_t intel_cursor_formats[] = {
85 DRM_FORMAT_ARGB8888,
86};
87
f1f644dc 88static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 89 struct intel_crtc_state *pipe_config);
18442d08 90static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 91 struct intel_crtc_state *pipe_config);
f1f644dc 92
eb1bfe80
JB
93static int intel_framebuffer_init(struct drm_device *dev,
94 struct intel_framebuffer *ifb,
95 struct drm_mode_fb_cmd2 *mode_cmd,
96 struct drm_i915_gem_object *obj);
5b18e57c
DV
97static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
98static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
bc58be60 99static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
29407aab 100static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
101 struct intel_link_m_n *m_n,
102 struct intel_link_m_n *m2_n2);
29407aab 103static void ironlake_set_pipeconf(struct drm_crtc *crtc);
229fca97 104static void haswell_set_pipeconf(struct drm_crtc *crtc);
391bf048 105static void haswell_set_pipemisc(struct drm_crtc *crtc);
d288f65f 106static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 107 const struct intel_crtc_state *pipe_config);
d288f65f 108static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 109 const struct intel_crtc_state *pipe_config);
613d2b27
ML
110static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
111static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
549e2bfb
CK
112static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
113 struct intel_crtc_state *crtc_state);
bfd16b2a
ML
114static void skylake_pfit_enable(struct intel_crtc *crtc);
115static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
116static void ironlake_pfit_enable(struct intel_crtc *crtc);
043e9bda 117static void intel_modeset_setup_hw_state(struct drm_device *dev);
2622a081 118static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
e7457a9a 119
79e53945 120typedef struct {
0206e353 121 int min, max;
79e53945
JB
122} intel_range_t;
123
124typedef struct {
0206e353
AJ
125 int dot_limit;
126 int p2_slow, p2_fast;
79e53945
JB
127} intel_p2_t;
128
d4906093
ML
129typedef struct intel_limit intel_limit_t;
130struct intel_limit {
0206e353
AJ
131 intel_range_t dot, vco, n, m, m1, m2, p, p1;
132 intel_p2_t p2;
d4906093 133};
79e53945 134
bfa7df01
VS
135/* returns HPLL frequency in kHz */
136static int valleyview_get_vco(struct drm_i915_private *dev_priv)
137{
138 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
139
140 /* Obtain SKU information */
141 mutex_lock(&dev_priv->sb_lock);
142 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
143 CCK_FUSE_HPLL_FREQ_MASK;
144 mutex_unlock(&dev_priv->sb_lock);
145
146 return vco_freq[hpll_freq] * 1000;
147}
148
149static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
150 const char *name, u32 reg)
151{
152 u32 val;
153 int divider;
154
155 if (dev_priv->hpll_freq == 0)
156 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
157
158 mutex_lock(&dev_priv->sb_lock);
159 val = vlv_cck_read(dev_priv, reg);
160 mutex_unlock(&dev_priv->sb_lock);
161
162 divider = val & CCK_FREQUENCY_VALUES;
163
164 WARN((val & CCK_FREQUENCY_STATUS) !=
165 (divider << CCK_FREQUENCY_STATUS_SHIFT),
166 "%s change in progress\n", name);
167
168 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
169}
170
e7dc33f3
VS
171static int
172intel_pch_rawclk(struct drm_i915_private *dev_priv)
d2acd215 173{
e7dc33f3
VS
174 return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
175}
d2acd215 176
e7dc33f3
VS
177static int
178intel_vlv_hrawclk(struct drm_i915_private *dev_priv)
179{
35d38d1f
VS
180 return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
181 CCK_DISPLAY_REF_CLOCK_CONTROL);
d2acd215
DV
182}
183
e7dc33f3
VS
184static int
185intel_g4x_hrawclk(struct drm_i915_private *dev_priv)
79e50a4f 186{
79e50a4f
JN
187 uint32_t clkcfg;
188
e7dc33f3 189 /* hrawclock is 1/4 the FSB frequency */
79e50a4f
JN
190 clkcfg = I915_READ(CLKCFG);
191 switch (clkcfg & CLKCFG_FSB_MASK) {
192 case CLKCFG_FSB_400:
e7dc33f3 193 return 100000;
79e50a4f 194 case CLKCFG_FSB_533:
e7dc33f3 195 return 133333;
79e50a4f 196 case CLKCFG_FSB_667:
e7dc33f3 197 return 166667;
79e50a4f 198 case CLKCFG_FSB_800:
e7dc33f3 199 return 200000;
79e50a4f 200 case CLKCFG_FSB_1067:
e7dc33f3 201 return 266667;
79e50a4f 202 case CLKCFG_FSB_1333:
e7dc33f3 203 return 333333;
79e50a4f
JN
204 /* these two are just a guess; one of them might be right */
205 case CLKCFG_FSB_1600:
206 case CLKCFG_FSB_1600_ALT:
e7dc33f3 207 return 400000;
79e50a4f 208 default:
e7dc33f3 209 return 133333;
79e50a4f
JN
210 }
211}
212
e7dc33f3
VS
213static void intel_update_rawclk(struct drm_i915_private *dev_priv)
214{
215 if (HAS_PCH_SPLIT(dev_priv))
216 dev_priv->rawclk_freq = intel_pch_rawclk(dev_priv);
217 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
218 dev_priv->rawclk_freq = intel_vlv_hrawclk(dev_priv);
219 else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
220 dev_priv->rawclk_freq = intel_g4x_hrawclk(dev_priv);
221 else
222 return; /* no rawclk on other platforms, or no need to know it */
223
224 DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
225}
226
bfa7df01
VS
227static void intel_update_czclk(struct drm_i915_private *dev_priv)
228{
666a4537 229 if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
bfa7df01
VS
230 return;
231
232 dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
233 CCK_CZ_CLOCK_CONTROL);
234
235 DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
236}
237
021357ac 238static inline u32 /* units of 100MHz */
21a727b3
VS
239intel_fdi_link_freq(struct drm_i915_private *dev_priv,
240 const struct intel_crtc_state *pipe_config)
021357ac 241{
21a727b3
VS
242 if (HAS_DDI(dev_priv))
243 return pipe_config->port_clock; /* SPLL */
244 else if (IS_GEN5(dev_priv))
245 return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
e3b247da 246 else
21a727b3 247 return 270000;
021357ac
CW
248}
249
5d536e28 250static const intel_limit_t intel_limits_i8xx_dac = {
0206e353 251 .dot = { .min = 25000, .max = 350000 },
9c333719 252 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 253 .n = { .min = 2, .max = 16 },
0206e353
AJ
254 .m = { .min = 96, .max = 140 },
255 .m1 = { .min = 18, .max = 26 },
256 .m2 = { .min = 6, .max = 16 },
257 .p = { .min = 4, .max = 128 },
258 .p1 = { .min = 2, .max = 33 },
273e27ca
EA
259 .p2 = { .dot_limit = 165000,
260 .p2_slow = 4, .p2_fast = 2 },
e4b36699
KP
261};
262
5d536e28
DV
263static const intel_limit_t intel_limits_i8xx_dvo = {
264 .dot = { .min = 25000, .max = 350000 },
9c333719 265 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 266 .n = { .min = 2, .max = 16 },
5d536e28
DV
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 },
272 .p2 = { .dot_limit = 165000,
273 .p2_slow = 4, .p2_fast = 4 },
274};
275
e4b36699 276static const intel_limit_t intel_limits_i8xx_lvds = {
0206e353 277 .dot = { .min = 25000, .max = 350000 },
9c333719 278 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 279 .n = { .min = 2, .max = 16 },
0206e353
AJ
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 = 1, .max = 6 },
273e27ca
EA
285 .p2 = { .dot_limit = 165000,
286 .p2_slow = 14, .p2_fast = 7 },
e4b36699 287};
273e27ca 288
e4b36699 289static const intel_limit_t intel_limits_i9xx_sdvo = {
0206e353
AJ
290 .dot = { .min = 20000, .max = 400000 },
291 .vco = { .min = 1400000, .max = 2800000 },
292 .n = { .min = 1, .max = 6 },
293 .m = { .min = 70, .max = 120 },
4f7dfb67
PJ
294 .m1 = { .min = 8, .max = 18 },
295 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
296 .p = { .min = 5, .max = 80 },
297 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
298 .p2 = { .dot_limit = 200000,
299 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
300};
301
302static const intel_limit_t intel_limits_i9xx_lvds = {
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 },
53a7d2d1
PJ
307 .m1 = { .min = 8, .max = 18 },
308 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
309 .p = { .min = 7, .max = 98 },
310 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
311 .p2 = { .dot_limit = 112000,
312 .p2_slow = 14, .p2_fast = 7 },
e4b36699
KP
313};
314
273e27ca 315
e4b36699 316static const intel_limit_t intel_limits_g4x_sdvo = {
273e27ca
EA
317 .dot = { .min = 25000, .max = 270000 },
318 .vco = { .min = 1750000, .max = 3500000},
319 .n = { .min = 1, .max = 4 },
320 .m = { .min = 104, .max = 138 },
321 .m1 = { .min = 17, .max = 23 },
322 .m2 = { .min = 5, .max = 11 },
323 .p = { .min = 10, .max = 30 },
324 .p1 = { .min = 1, .max = 3},
325 .p2 = { .dot_limit = 270000,
326 .p2_slow = 10,
327 .p2_fast = 10
044c7c41 328 },
e4b36699
KP
329};
330
331static const intel_limit_t intel_limits_g4x_hdmi = {
273e27ca
EA
332 .dot = { .min = 22000, .max = 400000 },
333 .vco = { .min = 1750000, .max = 3500000},
334 .n = { .min = 1, .max = 4 },
335 .m = { .min = 104, .max = 138 },
336 .m1 = { .min = 16, .max = 23 },
337 .m2 = { .min = 5, .max = 11 },
338 .p = { .min = 5, .max = 80 },
339 .p1 = { .min = 1, .max = 8},
340 .p2 = { .dot_limit = 165000,
341 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
342};
343
344static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
273e27ca
EA
345 .dot = { .min = 20000, .max = 115000 },
346 .vco = { .min = 1750000, .max = 3500000 },
347 .n = { .min = 1, .max = 3 },
348 .m = { .min = 104, .max = 138 },
349 .m1 = { .min = 17, .max = 23 },
350 .m2 = { .min = 5, .max = 11 },
351 .p = { .min = 28, .max = 112 },
352 .p1 = { .min = 2, .max = 8 },
353 .p2 = { .dot_limit = 0,
354 .p2_slow = 14, .p2_fast = 14
044c7c41 355 },
e4b36699
KP
356};
357
358static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
273e27ca
EA
359 .dot = { .min = 80000, .max = 224000 },
360 .vco = { .min = 1750000, .max = 3500000 },
361 .n = { .min = 1, .max = 3 },
362 .m = { .min = 104, .max = 138 },
363 .m1 = { .min = 17, .max = 23 },
364 .m2 = { .min = 5, .max = 11 },
365 .p = { .min = 14, .max = 42 },
366 .p1 = { .min = 2, .max = 6 },
367 .p2 = { .dot_limit = 0,
368 .p2_slow = 7, .p2_fast = 7
044c7c41 369 },
e4b36699
KP
370};
371
f2b115e6 372static const intel_limit_t intel_limits_pineview_sdvo = {
0206e353
AJ
373 .dot = { .min = 20000, .max = 400000},
374 .vco = { .min = 1700000, .max = 3500000 },
273e27ca 375 /* Pineview's Ncounter is a ring counter */
0206e353
AJ
376 .n = { .min = 3, .max = 6 },
377 .m = { .min = 2, .max = 256 },
273e27ca 378 /* Pineview only has one combined m divider, which we treat as m2. */
0206e353
AJ
379 .m1 = { .min = 0, .max = 0 },
380 .m2 = { .min = 0, .max = 254 },
381 .p = { .min = 5, .max = 80 },
382 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
383 .p2 = { .dot_limit = 200000,
384 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
385};
386
f2b115e6 387static const intel_limit_t intel_limits_pineview_lvds = {
0206e353
AJ
388 .dot = { .min = 20000, .max = 400000 },
389 .vco = { .min = 1700000, .max = 3500000 },
390 .n = { .min = 3, .max = 6 },
391 .m = { .min = 2, .max = 256 },
392 .m1 = { .min = 0, .max = 0 },
393 .m2 = { .min = 0, .max = 254 },
394 .p = { .min = 7, .max = 112 },
395 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
396 .p2 = { .dot_limit = 112000,
397 .p2_slow = 14, .p2_fast = 14 },
e4b36699
KP
398};
399
273e27ca
EA
400/* Ironlake / Sandybridge
401 *
402 * We calculate clock using (register_value + 2) for N/M1/M2, so here
403 * the range value for them is (actual_value - 2).
404 */
b91ad0ec 405static const intel_limit_t intel_limits_ironlake_dac = {
273e27ca
EA
406 .dot = { .min = 25000, .max = 350000 },
407 .vco = { .min = 1760000, .max = 3510000 },
408 .n = { .min = 1, .max = 5 },
409 .m = { .min = 79, .max = 127 },
410 .m1 = { .min = 12, .max = 22 },
411 .m2 = { .min = 5, .max = 9 },
412 .p = { .min = 5, .max = 80 },
413 .p1 = { .min = 1, .max = 8 },
414 .p2 = { .dot_limit = 225000,
415 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
416};
417
b91ad0ec 418static const intel_limit_t intel_limits_ironlake_single_lvds = {
273e27ca
EA
419 .dot = { .min = 25000, .max = 350000 },
420 .vco = { .min = 1760000, .max = 3510000 },
421 .n = { .min = 1, .max = 3 },
422 .m = { .min = 79, .max = 118 },
423 .m1 = { .min = 12, .max = 22 },
424 .m2 = { .min = 5, .max = 9 },
425 .p = { .min = 28, .max = 112 },
426 .p1 = { .min = 2, .max = 8 },
427 .p2 = { .dot_limit = 225000,
428 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
429};
430
431static const intel_limit_t intel_limits_ironlake_dual_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 = 127 },
436 .m1 = { .min = 12, .max = 22 },
437 .m2 = { .min = 5, .max = 9 },
438 .p = { .min = 14, .max = 56 },
439 .p1 = { .min = 2, .max = 8 },
440 .p2 = { .dot_limit = 225000,
441 .p2_slow = 7, .p2_fast = 7 },
b91ad0ec
ZW
442};
443
273e27ca 444/* LVDS 100mhz refclk limits. */
b91ad0ec 445static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
273e27ca
EA
446 .dot = { .min = 25000, .max = 350000 },
447 .vco = { .min = 1760000, .max = 3510000 },
448 .n = { .min = 1, .max = 2 },
449 .m = { .min = 79, .max = 126 },
450 .m1 = { .min = 12, .max = 22 },
451 .m2 = { .min = 5, .max = 9 },
452 .p = { .min = 28, .max = 112 },
0206e353 453 .p1 = { .min = 2, .max = 8 },
273e27ca
EA
454 .p2 = { .dot_limit = 225000,
455 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
456};
457
458static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
273e27ca
EA
459 .dot = { .min = 25000, .max = 350000 },
460 .vco = { .min = 1760000, .max = 3510000 },
461 .n = { .min = 1, .max = 3 },
462 .m = { .min = 79, .max = 126 },
463 .m1 = { .min = 12, .max = 22 },
464 .m2 = { .min = 5, .max = 9 },
465 .p = { .min = 14, .max = 42 },
0206e353 466 .p1 = { .min = 2, .max = 6 },
273e27ca
EA
467 .p2 = { .dot_limit = 225000,
468 .p2_slow = 7, .p2_fast = 7 },
4547668a
ZY
469};
470
dc730512 471static const intel_limit_t intel_limits_vlv = {
f01b7962
VS
472 /*
473 * These are the data rate limits (measured in fast clocks)
474 * since those are the strictest limits we have. The fast
475 * clock and actual rate limits are more relaxed, so checking
476 * them would make no difference.
477 */
478 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
75e53986 479 .vco = { .min = 4000000, .max = 6000000 },
a0c4da24 480 .n = { .min = 1, .max = 7 },
a0c4da24
JB
481 .m1 = { .min = 2, .max = 3 },
482 .m2 = { .min = 11, .max = 156 },
b99ab663 483 .p1 = { .min = 2, .max = 3 },
5fdc9c49 484 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
a0c4da24
JB
485};
486
ef9348c8
CML
487static const intel_limit_t intel_limits_chv = {
488 /*
489 * These are the data rate limits (measured in fast clocks)
490 * since those are the strictest limits we have. The fast
491 * clock and actual rate limits are more relaxed, so checking
492 * them would make no difference.
493 */
494 .dot = { .min = 25000 * 5, .max = 540000 * 5},
17fe1021 495 .vco = { .min = 4800000, .max = 6480000 },
ef9348c8
CML
496 .n = { .min = 1, .max = 1 },
497 .m1 = { .min = 2, .max = 2 },
498 .m2 = { .min = 24 << 22, .max = 175 << 22 },
499 .p1 = { .min = 2, .max = 4 },
500 .p2 = { .p2_slow = 1, .p2_fast = 14 },
501};
502
5ab7b0b7
ID
503static const intel_limit_t intel_limits_bxt = {
504 /* FIXME: find real dot limits */
505 .dot = { .min = 0, .max = INT_MAX },
e6292556 506 .vco = { .min = 4800000, .max = 6700000 },
5ab7b0b7
ID
507 .n = { .min = 1, .max = 1 },
508 .m1 = { .min = 2, .max = 2 },
509 /* FIXME: find real m2 limits */
510 .m2 = { .min = 2 << 22, .max = 255 << 22 },
511 .p1 = { .min = 2, .max = 4 },
512 .p2 = { .p2_slow = 1, .p2_fast = 20 },
513};
514
cdba954e
ACO
515static bool
516needs_modeset(struct drm_crtc_state *state)
517{
fc596660 518 return drm_atomic_crtc_needs_modeset(state);
cdba954e
ACO
519}
520
e0638cdf
PZ
521/**
522 * Returns whether any output on the specified pipe is of the specified type
523 */
4093561b 524bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
e0638cdf 525{
409ee761 526 struct drm_device *dev = crtc->base.dev;
e0638cdf
PZ
527 struct intel_encoder *encoder;
528
409ee761 529 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
e0638cdf
PZ
530 if (encoder->type == type)
531 return true;
532
533 return false;
534}
535
d0737e1d
ACO
536/**
537 * Returns whether any output on the specified pipe will have the specified
538 * type after a staged modeset is complete, i.e., the same as
539 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
540 * encoder->crtc.
541 */
a93e255f
ACO
542static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
543 int type)
d0737e1d 544{
a93e255f 545 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 546 struct drm_connector *connector;
a93e255f 547 struct drm_connector_state *connector_state;
d0737e1d 548 struct intel_encoder *encoder;
a93e255f
ACO
549 int i, num_connectors = 0;
550
da3ced29 551 for_each_connector_in_state(state, connector, connector_state, i) {
a93e255f
ACO
552 if (connector_state->crtc != crtc_state->base.crtc)
553 continue;
554
555 num_connectors++;
d0737e1d 556
a93e255f
ACO
557 encoder = to_intel_encoder(connector_state->best_encoder);
558 if (encoder->type == type)
d0737e1d 559 return true;
a93e255f
ACO
560 }
561
562 WARN_ON(num_connectors == 0);
d0737e1d
ACO
563
564 return false;
565}
566
dccbea3b
ID
567/*
568 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
569 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
570 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
571 * The helpers' return value is the rate of the clock that is fed to the
572 * display engine's pipe which can be the above fast dot clock rate or a
573 * divided-down version of it.
574 */
f2b115e6 575/* m1 is reserved as 0 in Pineview, n is a ring counter */
dccbea3b 576static int pnv_calc_dpll_params(int refclk, intel_clock_t *clock)
79e53945 577{
2177832f
SL
578 clock->m = clock->m2 + 2;
579 clock->p = clock->p1 * clock->p2;
ed5ca77e 580 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 581 return 0;
fb03ac01
VS
582 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
583 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
584
585 return clock->dot;
2177832f
SL
586}
587
7429e9d4
DV
588static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
589{
590 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
591}
592
dccbea3b 593static int i9xx_calc_dpll_params(int refclk, intel_clock_t *clock)
2177832f 594{
7429e9d4 595 clock->m = i9xx_dpll_compute_m(clock);
79e53945 596 clock->p = clock->p1 * clock->p2;
ed5ca77e 597 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
dccbea3b 598 return 0;
fb03ac01
VS
599 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
600 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
601
602 return clock->dot;
79e53945
JB
603}
604
dccbea3b 605static int vlv_calc_dpll_params(int refclk, intel_clock_t *clock)
589eca67
ID
606{
607 clock->m = clock->m1 * clock->m2;
608 clock->p = clock->p1 * clock->p2;
609 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 610 return 0;
589eca67
ID
611 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
612 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
613
614 return clock->dot / 5;
589eca67
ID
615}
616
dccbea3b 617int chv_calc_dpll_params(int refclk, intel_clock_t *clock)
ef9348c8
CML
618{
619 clock->m = clock->m1 * clock->m2;
620 clock->p = clock->p1 * clock->p2;
621 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 622 return 0;
ef9348c8
CML
623 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
624 clock->n << 22);
625 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
626
627 return clock->dot / 5;
ef9348c8
CML
628}
629
7c04d1d9 630#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
79e53945
JB
631/**
632 * Returns whether the given set of divisors are valid for a given refclk with
633 * the given connectors.
634 */
635
1b894b59
CW
636static bool intel_PLL_is_valid(struct drm_device *dev,
637 const intel_limit_t *limit,
638 const intel_clock_t *clock)
79e53945 639{
f01b7962
VS
640 if (clock->n < limit->n.min || limit->n.max < clock->n)
641 INTELPllInvalid("n out of range\n");
79e53945 642 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
0206e353 643 INTELPllInvalid("p1 out of range\n");
79e53945 644 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
0206e353 645 INTELPllInvalid("m2 out of range\n");
79e53945 646 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
0206e353 647 INTELPllInvalid("m1 out of range\n");
f01b7962 648
666a4537
WB
649 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) &&
650 !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev))
f01b7962
VS
651 if (clock->m1 <= clock->m2)
652 INTELPllInvalid("m1 <= m2\n");
653
666a4537 654 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev)) {
f01b7962
VS
655 if (clock->p < limit->p.min || limit->p.max < clock->p)
656 INTELPllInvalid("p out of range\n");
657 if (clock->m < limit->m.min || limit->m.max < clock->m)
658 INTELPllInvalid("m out of range\n");
659 }
660
79e53945 661 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
0206e353 662 INTELPllInvalid("vco out of range\n");
79e53945
JB
663 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
664 * connector, etc., rather than just a single range.
665 */
666 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
0206e353 667 INTELPllInvalid("dot out of range\n");
79e53945
JB
668
669 return true;
670}
671
3b1429d9
VS
672static int
673i9xx_select_p2_div(const intel_limit_t *limit,
674 const struct intel_crtc_state *crtc_state,
675 int target)
79e53945 676{
3b1429d9 677 struct drm_device *dev = crtc_state->base.crtc->dev;
79e53945 678
a93e255f 679 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
79e53945 680 /*
a210b028
DV
681 * For LVDS just rely on its current settings for dual-channel.
682 * We haven't figured out how to reliably set up different
683 * single/dual channel state, if we even can.
79e53945 684 */
1974cad0 685 if (intel_is_dual_link_lvds(dev))
3b1429d9 686 return limit->p2.p2_fast;
79e53945 687 else
3b1429d9 688 return limit->p2.p2_slow;
79e53945
JB
689 } else {
690 if (target < limit->p2.dot_limit)
3b1429d9 691 return limit->p2.p2_slow;
79e53945 692 else
3b1429d9 693 return limit->p2.p2_fast;
79e53945 694 }
3b1429d9
VS
695}
696
70e8aa21
ACO
697/*
698 * Returns a set of divisors for the desired target clock with the given
699 * refclk, or FALSE. The returned values represent the clock equation:
700 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
701 *
702 * Target and reference clocks are specified in kHz.
703 *
704 * If match_clock is provided, then best_clock P divider must match the P
705 * divider from @match_clock used for LVDS downclocking.
706 */
3b1429d9
VS
707static bool
708i9xx_find_best_dpll(const intel_limit_t *limit,
709 struct intel_crtc_state *crtc_state,
710 int target, int refclk, intel_clock_t *match_clock,
711 intel_clock_t *best_clock)
712{
713 struct drm_device *dev = crtc_state->base.crtc->dev;
714 intel_clock_t clock;
715 int err = target;
79e53945 716
0206e353 717 memset(best_clock, 0, sizeof(*best_clock));
79e53945 718
3b1429d9
VS
719 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
720
42158660
ZY
721 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
722 clock.m1++) {
723 for (clock.m2 = limit->m2.min;
724 clock.m2 <= limit->m2.max; clock.m2++) {
c0efc387 725 if (clock.m2 >= clock.m1)
42158660
ZY
726 break;
727 for (clock.n = limit->n.min;
728 clock.n <= limit->n.max; clock.n++) {
729 for (clock.p1 = limit->p1.min;
730 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
731 int this_err;
732
dccbea3b 733 i9xx_calc_dpll_params(refclk, &clock);
ac58c3f0
DV
734 if (!intel_PLL_is_valid(dev, limit,
735 &clock))
736 continue;
737 if (match_clock &&
738 clock.p != match_clock->p)
739 continue;
740
741 this_err = abs(clock.dot - target);
742 if (this_err < err) {
743 *best_clock = clock;
744 err = this_err;
745 }
746 }
747 }
748 }
749 }
750
751 return (err != target);
752}
753
70e8aa21
ACO
754/*
755 * Returns a set of divisors for the desired target clock with the given
756 * refclk, or FALSE. The returned values represent the clock equation:
757 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
758 *
759 * Target and reference clocks are specified in kHz.
760 *
761 * If match_clock is provided, then best_clock P divider must match the P
762 * divider from @match_clock used for LVDS downclocking.
763 */
ac58c3f0 764static bool
a93e255f
ACO
765pnv_find_best_dpll(const intel_limit_t *limit,
766 struct intel_crtc_state *crtc_state,
ee9300bb
DV
767 int target, int refclk, intel_clock_t *match_clock,
768 intel_clock_t *best_clock)
79e53945 769{
3b1429d9 770 struct drm_device *dev = crtc_state->base.crtc->dev;
79e53945 771 intel_clock_t clock;
79e53945
JB
772 int err = target;
773
0206e353 774 memset(best_clock, 0, sizeof(*best_clock));
79e53945 775
3b1429d9
VS
776 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
777
42158660
ZY
778 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
779 clock.m1++) {
780 for (clock.m2 = limit->m2.min;
781 clock.m2 <= limit->m2.max; clock.m2++) {
42158660
ZY
782 for (clock.n = limit->n.min;
783 clock.n <= limit->n.max; clock.n++) {
784 for (clock.p1 = limit->p1.min;
785 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
786 int this_err;
787
dccbea3b 788 pnv_calc_dpll_params(refclk, &clock);
1b894b59
CW
789 if (!intel_PLL_is_valid(dev, limit,
790 &clock))
79e53945 791 continue;
cec2f356
SP
792 if (match_clock &&
793 clock.p != match_clock->p)
794 continue;
79e53945
JB
795
796 this_err = abs(clock.dot - target);
797 if (this_err < err) {
798 *best_clock = clock;
799 err = this_err;
800 }
801 }
802 }
803 }
804 }
805
806 return (err != target);
807}
808
997c030c
ACO
809/*
810 * Returns a set of divisors for the desired target clock with the given
811 * refclk, or FALSE. The returned values represent the clock equation:
812 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
70e8aa21
ACO
813 *
814 * Target and reference clocks are specified in kHz.
815 *
816 * If match_clock is provided, then best_clock P divider must match the P
817 * divider from @match_clock used for LVDS downclocking.
997c030c 818 */
d4906093 819static bool
a93e255f
ACO
820g4x_find_best_dpll(const intel_limit_t *limit,
821 struct intel_crtc_state *crtc_state,
ee9300bb
DV
822 int target, int refclk, intel_clock_t *match_clock,
823 intel_clock_t *best_clock)
d4906093 824{
3b1429d9 825 struct drm_device *dev = crtc_state->base.crtc->dev;
d4906093
ML
826 intel_clock_t clock;
827 int max_n;
3b1429d9 828 bool found = false;
6ba770dc
AJ
829 /* approximately equals target * 0.00585 */
830 int err_most = (target >> 8) + (target >> 9);
d4906093
ML
831
832 memset(best_clock, 0, sizeof(*best_clock));
3b1429d9
VS
833
834 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
835
d4906093 836 max_n = limit->n.max;
f77f13e2 837 /* based on hardware requirement, prefer smaller n to precision */
d4906093 838 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
f77f13e2 839 /* based on hardware requirement, prefere larger m1,m2 */
d4906093
ML
840 for (clock.m1 = limit->m1.max;
841 clock.m1 >= limit->m1.min; clock.m1--) {
842 for (clock.m2 = limit->m2.max;
843 clock.m2 >= limit->m2.min; clock.m2--) {
844 for (clock.p1 = limit->p1.max;
845 clock.p1 >= limit->p1.min; clock.p1--) {
846 int this_err;
847
dccbea3b 848 i9xx_calc_dpll_params(refclk, &clock);
1b894b59
CW
849 if (!intel_PLL_is_valid(dev, limit,
850 &clock))
d4906093 851 continue;
1b894b59
CW
852
853 this_err = abs(clock.dot - target);
d4906093
ML
854 if (this_err < err_most) {
855 *best_clock = clock;
856 err_most = this_err;
857 max_n = clock.n;
858 found = true;
859 }
860 }
861 }
862 }
863 }
2c07245f
ZW
864 return found;
865}
866
d5dd62bd
ID
867/*
868 * Check if the calculated PLL configuration is more optimal compared to the
869 * best configuration and error found so far. Return the calculated error.
870 */
871static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
872 const intel_clock_t *calculated_clock,
873 const intel_clock_t *best_clock,
874 unsigned int best_error_ppm,
875 unsigned int *error_ppm)
876{
9ca3ba01
ID
877 /*
878 * For CHV ignore the error and consider only the P value.
879 * Prefer a bigger P value based on HW requirements.
880 */
881 if (IS_CHERRYVIEW(dev)) {
882 *error_ppm = 0;
883
884 return calculated_clock->p > best_clock->p;
885 }
886
24be4e46
ID
887 if (WARN_ON_ONCE(!target_freq))
888 return false;
889
d5dd62bd
ID
890 *error_ppm = div_u64(1000000ULL *
891 abs(target_freq - calculated_clock->dot),
892 target_freq);
893 /*
894 * Prefer a better P value over a better (smaller) error if the error
895 * is small. Ensure this preference for future configurations too by
896 * setting the error to 0.
897 */
898 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
899 *error_ppm = 0;
900
901 return true;
902 }
903
904 return *error_ppm + 10 < best_error_ppm;
905}
906
65b3d6a9
ACO
907/*
908 * Returns a set of divisors for the desired target clock with the given
909 * refclk, or FALSE. The returned values represent the clock equation:
910 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
911 */
a0c4da24 912static bool
a93e255f
ACO
913vlv_find_best_dpll(const intel_limit_t *limit,
914 struct intel_crtc_state *crtc_state,
ee9300bb
DV
915 int target, int refclk, intel_clock_t *match_clock,
916 intel_clock_t *best_clock)
a0c4da24 917{
a93e255f 918 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 919 struct drm_device *dev = crtc->base.dev;
6b4bf1c4 920 intel_clock_t clock;
69e4f900 921 unsigned int bestppm = 1000000;
27e639bf
VS
922 /* min update 19.2 MHz */
923 int max_n = min(limit->n.max, refclk / 19200);
49e497ef 924 bool found = false;
a0c4da24 925
6b4bf1c4
VS
926 target *= 5; /* fast clock */
927
928 memset(best_clock, 0, sizeof(*best_clock));
a0c4da24
JB
929
930 /* based on hardware requirement, prefer smaller n to precision */
27e639bf 931 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
811bbf05 932 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
889059d8 933 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
c1a9ae43 934 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
6b4bf1c4 935 clock.p = clock.p1 * clock.p2;
a0c4da24 936 /* based on hardware requirement, prefer bigger m1,m2 values */
6b4bf1c4 937 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
d5dd62bd 938 unsigned int ppm;
69e4f900 939
6b4bf1c4
VS
940 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
941 refclk * clock.m1);
942
dccbea3b 943 vlv_calc_dpll_params(refclk, &clock);
43b0ac53 944
f01b7962
VS
945 if (!intel_PLL_is_valid(dev, limit,
946 &clock))
43b0ac53
VS
947 continue;
948
d5dd62bd
ID
949 if (!vlv_PLL_is_optimal(dev, target,
950 &clock,
951 best_clock,
952 bestppm, &ppm))
953 continue;
6b4bf1c4 954
d5dd62bd
ID
955 *best_clock = clock;
956 bestppm = ppm;
957 found = true;
a0c4da24
JB
958 }
959 }
960 }
961 }
a0c4da24 962
49e497ef 963 return found;
a0c4da24 964}
a4fc5ed6 965
65b3d6a9
ACO
966/*
967 * Returns a set of divisors for the desired target clock with the given
968 * refclk, or FALSE. The returned values represent the clock equation:
969 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
970 */
ef9348c8 971static bool
a93e255f
ACO
972chv_find_best_dpll(const intel_limit_t *limit,
973 struct intel_crtc_state *crtc_state,
ef9348c8
CML
974 int target, int refclk, intel_clock_t *match_clock,
975 intel_clock_t *best_clock)
976{
a93e255f 977 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 978 struct drm_device *dev = crtc->base.dev;
9ca3ba01 979 unsigned int best_error_ppm;
ef9348c8
CML
980 intel_clock_t clock;
981 uint64_t m2;
982 int found = false;
983
984 memset(best_clock, 0, sizeof(*best_clock));
9ca3ba01 985 best_error_ppm = 1000000;
ef9348c8
CML
986
987 /*
988 * Based on hardware doc, the n always set to 1, and m1 always
989 * set to 2. If requires to support 200Mhz refclk, we need to
990 * revisit this because n may not 1 anymore.
991 */
992 clock.n = 1, clock.m1 = 2;
993 target *= 5; /* fast clock */
994
995 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
996 for (clock.p2 = limit->p2.p2_fast;
997 clock.p2 >= limit->p2.p2_slow;
998 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
9ca3ba01 999 unsigned int error_ppm;
ef9348c8
CML
1000
1001 clock.p = clock.p1 * clock.p2;
1002
1003 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
1004 clock.n) << 22, refclk * clock.m1);
1005
1006 if (m2 > INT_MAX/clock.m1)
1007 continue;
1008
1009 clock.m2 = m2;
1010
dccbea3b 1011 chv_calc_dpll_params(refclk, &clock);
ef9348c8
CML
1012
1013 if (!intel_PLL_is_valid(dev, limit, &clock))
1014 continue;
1015
9ca3ba01
ID
1016 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
1017 best_error_ppm, &error_ppm))
1018 continue;
1019
1020 *best_clock = clock;
1021 best_error_ppm = error_ppm;
1022 found = true;
ef9348c8
CML
1023 }
1024 }
1025
1026 return found;
1027}
1028
5ab7b0b7
ID
1029bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
1030 intel_clock_t *best_clock)
1031{
65b3d6a9
ACO
1032 int refclk = 100000;
1033 const intel_limit_t *limit = &intel_limits_bxt;
5ab7b0b7 1034
65b3d6a9 1035 return chv_find_best_dpll(limit, crtc_state,
5ab7b0b7
ID
1036 target_clock, refclk, NULL, best_clock);
1037}
1038
20ddf665
VS
1039bool intel_crtc_active(struct drm_crtc *crtc)
1040{
1041 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1042
1043 /* Be paranoid as we can arrive here with only partial
1044 * state retrieved from the hardware during setup.
1045 *
241bfc38 1046 * We can ditch the adjusted_mode.crtc_clock check as soon
20ddf665
VS
1047 * as Haswell has gained clock readout/fastboot support.
1048 *
66e514c1 1049 * We can ditch the crtc->primary->fb check as soon as we can
20ddf665 1050 * properly reconstruct framebuffers.
c3d1f436
MR
1051 *
1052 * FIXME: The intel_crtc->active here should be switched to
1053 * crtc->state->active once we have proper CRTC states wired up
1054 * for atomic.
20ddf665 1055 */
c3d1f436 1056 return intel_crtc->active && crtc->primary->state->fb &&
6e3c9717 1057 intel_crtc->config->base.adjusted_mode.crtc_clock;
20ddf665
VS
1058}
1059
a5c961d1
PZ
1060enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1061 enum pipe pipe)
1062{
1063 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1064 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1065
6e3c9717 1066 return intel_crtc->config->cpu_transcoder;
a5c961d1
PZ
1067}
1068
fbf49ea2
VS
1069static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1070{
1071 struct drm_i915_private *dev_priv = dev->dev_private;
f0f59a00 1072 i915_reg_t reg = PIPEDSL(pipe);
fbf49ea2
VS
1073 u32 line1, line2;
1074 u32 line_mask;
1075
1076 if (IS_GEN2(dev))
1077 line_mask = DSL_LINEMASK_GEN2;
1078 else
1079 line_mask = DSL_LINEMASK_GEN3;
1080
1081 line1 = I915_READ(reg) & line_mask;
6adfb1ef 1082 msleep(5);
fbf49ea2
VS
1083 line2 = I915_READ(reg) & line_mask;
1084
1085 return line1 == line2;
1086}
1087
ab7ad7f6
KP
1088/*
1089 * intel_wait_for_pipe_off - wait for pipe to turn off
575f7ab7 1090 * @crtc: crtc whose pipe to wait for
9d0498a2
JB
1091 *
1092 * After disabling a pipe, we can't wait for vblank in the usual way,
1093 * spinning on the vblank interrupt status bit, since we won't actually
1094 * see an interrupt when the pipe is disabled.
1095 *
ab7ad7f6
KP
1096 * On Gen4 and above:
1097 * wait for the pipe register state bit to turn off
1098 *
1099 * Otherwise:
1100 * wait for the display line value to settle (it usually
1101 * ends up stopping at the start of the next frame).
58e10eb9 1102 *
9d0498a2 1103 */
575f7ab7 1104static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
9d0498a2 1105{
575f7ab7 1106 struct drm_device *dev = crtc->base.dev;
9d0498a2 1107 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 1108 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 1109 enum pipe pipe = crtc->pipe;
ab7ad7f6
KP
1110
1111 if (INTEL_INFO(dev)->gen >= 4) {
f0f59a00 1112 i915_reg_t reg = PIPECONF(cpu_transcoder);
ab7ad7f6
KP
1113
1114 /* Wait for the Pipe State to go off */
58e10eb9
CW
1115 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1116 100))
284637d9 1117 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1118 } else {
ab7ad7f6 1119 /* Wait for the display line to settle */
fbf49ea2 1120 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
284637d9 1121 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1122 }
79e53945
JB
1123}
1124
b24e7179 1125/* Only for pre-ILK configs */
55607e8a
DV
1126void assert_pll(struct drm_i915_private *dev_priv,
1127 enum pipe pipe, bool state)
b24e7179 1128{
b24e7179
JB
1129 u32 val;
1130 bool cur_state;
1131
649636ef 1132 val = I915_READ(DPLL(pipe));
b24e7179 1133 cur_state = !!(val & DPLL_VCO_ENABLE);
e2c719b7 1134 I915_STATE_WARN(cur_state != state,
b24e7179 1135 "PLL state assertion failure (expected %s, current %s)\n",
87ad3212 1136 onoff(state), onoff(cur_state));
b24e7179 1137}
b24e7179 1138
23538ef1 1139/* XXX: the dsi pll is shared between MIPI DSI ports */
8563b1e8 1140void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
23538ef1
JN
1141{
1142 u32 val;
1143 bool cur_state;
1144
a580516d 1145 mutex_lock(&dev_priv->sb_lock);
23538ef1 1146 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
a580516d 1147 mutex_unlock(&dev_priv->sb_lock);
23538ef1
JN
1148
1149 cur_state = val & DSI_PLL_VCO_EN;
e2c719b7 1150 I915_STATE_WARN(cur_state != state,
23538ef1 1151 "DSI PLL state assertion failure (expected %s, current %s)\n",
87ad3212 1152 onoff(state), onoff(cur_state));
23538ef1 1153}
23538ef1 1154
040484af
JB
1155static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1156 enum pipe pipe, bool state)
1157{
040484af 1158 bool cur_state;
ad80a810
PZ
1159 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1160 pipe);
040484af 1161
affa9354
PZ
1162 if (HAS_DDI(dev_priv->dev)) {
1163 /* DDI does not have a specific FDI_TX register */
649636ef 1164 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
ad80a810 1165 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
bf507ef7 1166 } else {
649636ef 1167 u32 val = I915_READ(FDI_TX_CTL(pipe));
bf507ef7
ED
1168 cur_state = !!(val & FDI_TX_ENABLE);
1169 }
e2c719b7 1170 I915_STATE_WARN(cur_state != state,
040484af 1171 "FDI TX state assertion failure (expected %s, current %s)\n",
87ad3212 1172 onoff(state), onoff(cur_state));
040484af
JB
1173}
1174#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1175#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1176
1177static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1178 enum pipe pipe, bool state)
1179{
040484af
JB
1180 u32 val;
1181 bool cur_state;
1182
649636ef 1183 val = I915_READ(FDI_RX_CTL(pipe));
d63fa0dc 1184 cur_state = !!(val & FDI_RX_ENABLE);
e2c719b7 1185 I915_STATE_WARN(cur_state != state,
040484af 1186 "FDI RX state assertion failure (expected %s, current %s)\n",
87ad3212 1187 onoff(state), onoff(cur_state));
040484af
JB
1188}
1189#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1190#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1191
1192static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1193 enum pipe pipe)
1194{
040484af
JB
1195 u32 val;
1196
1197 /* ILK FDI PLL is always enabled */
3d13ef2e 1198 if (INTEL_INFO(dev_priv->dev)->gen == 5)
040484af
JB
1199 return;
1200
bf507ef7 1201 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
affa9354 1202 if (HAS_DDI(dev_priv->dev))
bf507ef7
ED
1203 return;
1204
649636ef 1205 val = I915_READ(FDI_TX_CTL(pipe));
e2c719b7 1206 I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
040484af
JB
1207}
1208
55607e8a
DV
1209void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1210 enum pipe pipe, bool state)
040484af 1211{
040484af 1212 u32 val;
55607e8a 1213 bool cur_state;
040484af 1214
649636ef 1215 val = I915_READ(FDI_RX_CTL(pipe));
55607e8a 1216 cur_state = !!(val & FDI_RX_PLL_ENABLE);
e2c719b7 1217 I915_STATE_WARN(cur_state != state,
55607e8a 1218 "FDI RX PLL assertion failure (expected %s, current %s)\n",
87ad3212 1219 onoff(state), onoff(cur_state));
040484af
JB
1220}
1221
b680c37a
DV
1222void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1223 enum pipe pipe)
ea0760cf 1224{
bedd4dba 1225 struct drm_device *dev = dev_priv->dev;
f0f59a00 1226 i915_reg_t pp_reg;
ea0760cf
JB
1227 u32 val;
1228 enum pipe panel_pipe = PIPE_A;
0de3b485 1229 bool locked = true;
ea0760cf 1230
bedd4dba
JN
1231 if (WARN_ON(HAS_DDI(dev)))
1232 return;
1233
1234 if (HAS_PCH_SPLIT(dev)) {
1235 u32 port_sel;
1236
ea0760cf 1237 pp_reg = PCH_PP_CONTROL;
bedd4dba
JN
1238 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1239
1240 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1241 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1242 panel_pipe = PIPE_B;
1243 /* XXX: else fix for eDP */
666a4537 1244 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
bedd4dba
JN
1245 /* presumably write lock depends on pipe, not port select */
1246 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1247 panel_pipe = pipe;
ea0760cf
JB
1248 } else {
1249 pp_reg = PP_CONTROL;
bedd4dba
JN
1250 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1251 panel_pipe = PIPE_B;
ea0760cf
JB
1252 }
1253
1254 val = I915_READ(pp_reg);
1255 if (!(val & PANEL_POWER_ON) ||
ec49ba2d 1256 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
ea0760cf
JB
1257 locked = false;
1258
e2c719b7 1259 I915_STATE_WARN(panel_pipe == pipe && locked,
ea0760cf 1260 "panel assertion failure, pipe %c regs locked\n",
9db4a9c7 1261 pipe_name(pipe));
ea0760cf
JB
1262}
1263
93ce0ba6
JN
1264static void assert_cursor(struct drm_i915_private *dev_priv,
1265 enum pipe pipe, bool state)
1266{
1267 struct drm_device *dev = dev_priv->dev;
1268 bool cur_state;
1269
d9d82081 1270 if (IS_845G(dev) || IS_I865G(dev))
0b87c24e 1271 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
d9d82081 1272 else
5efb3e28 1273 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
93ce0ba6 1274
e2c719b7 1275 I915_STATE_WARN(cur_state != state,
93ce0ba6 1276 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
87ad3212 1277 pipe_name(pipe), onoff(state), onoff(cur_state));
93ce0ba6
JN
1278}
1279#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1280#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1281
b840d907
JB
1282void assert_pipe(struct drm_i915_private *dev_priv,
1283 enum pipe pipe, bool state)
b24e7179 1284{
63d7bbe9 1285 bool cur_state;
702e7a56
PZ
1286 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1287 pipe);
4feed0eb 1288 enum intel_display_power_domain power_domain;
b24e7179 1289
b6b5d049
VS
1290 /* if we need the pipe quirk it must be always on */
1291 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1292 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
8e636784
DV
1293 state = true;
1294
4feed0eb
ID
1295 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1296 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
649636ef 1297 u32 val = I915_READ(PIPECONF(cpu_transcoder));
69310161 1298 cur_state = !!(val & PIPECONF_ENABLE);
4feed0eb
ID
1299
1300 intel_display_power_put(dev_priv, power_domain);
1301 } else {
1302 cur_state = false;
69310161
PZ
1303 }
1304
e2c719b7 1305 I915_STATE_WARN(cur_state != state,
63d7bbe9 1306 "pipe %c assertion failure (expected %s, current %s)\n",
87ad3212 1307 pipe_name(pipe), onoff(state), onoff(cur_state));
b24e7179
JB
1308}
1309
931872fc
CW
1310static void assert_plane(struct drm_i915_private *dev_priv,
1311 enum plane plane, bool state)
b24e7179 1312{
b24e7179 1313 u32 val;
931872fc 1314 bool cur_state;
b24e7179 1315
649636ef 1316 val = I915_READ(DSPCNTR(plane));
931872fc 1317 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
e2c719b7 1318 I915_STATE_WARN(cur_state != state,
931872fc 1319 "plane %c assertion failure (expected %s, current %s)\n",
87ad3212 1320 plane_name(plane), onoff(state), onoff(cur_state));
b24e7179
JB
1321}
1322
931872fc
CW
1323#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1324#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1325
b24e7179
JB
1326static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1327 enum pipe pipe)
1328{
653e1026 1329 struct drm_device *dev = dev_priv->dev;
649636ef 1330 int i;
b24e7179 1331
653e1026
VS
1332 /* Primary planes are fixed to pipes on gen4+ */
1333 if (INTEL_INFO(dev)->gen >= 4) {
649636ef 1334 u32 val = I915_READ(DSPCNTR(pipe));
e2c719b7 1335 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
28c05794
AJ
1336 "plane %c assertion failure, should be disabled but not\n",
1337 plane_name(pipe));
19ec1358 1338 return;
28c05794 1339 }
19ec1358 1340
b24e7179 1341 /* Need to check both planes against the pipe */
055e393f 1342 for_each_pipe(dev_priv, i) {
649636ef
VS
1343 u32 val = I915_READ(DSPCNTR(i));
1344 enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
b24e7179 1345 DISPPLANE_SEL_PIPE_SHIFT;
e2c719b7 1346 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
9db4a9c7
JB
1347 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1348 plane_name(i), pipe_name(pipe));
b24e7179
JB
1349 }
1350}
1351
19332d7a
JB
1352static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1353 enum pipe pipe)
1354{
20674eef 1355 struct drm_device *dev = dev_priv->dev;
649636ef 1356 int sprite;
19332d7a 1357
7feb8b88 1358 if (INTEL_INFO(dev)->gen >= 9) {
3bdcfc0c 1359 for_each_sprite(dev_priv, pipe, sprite) {
649636ef 1360 u32 val = I915_READ(PLANE_CTL(pipe, sprite));
e2c719b7 1361 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
7feb8b88
DL
1362 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1363 sprite, pipe_name(pipe));
1364 }
666a4537 1365 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
3bdcfc0c 1366 for_each_sprite(dev_priv, pipe, sprite) {
649636ef 1367 u32 val = I915_READ(SPCNTR(pipe, sprite));
e2c719b7 1368 I915_STATE_WARN(val & SP_ENABLE,
20674eef 1369 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1fe47785 1370 sprite_name(pipe, sprite), pipe_name(pipe));
20674eef
VS
1371 }
1372 } else if (INTEL_INFO(dev)->gen >= 7) {
649636ef 1373 u32 val = I915_READ(SPRCTL(pipe));
e2c719b7 1374 I915_STATE_WARN(val & SPRITE_ENABLE,
06da8da2 1375 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef
VS
1376 plane_name(pipe), pipe_name(pipe));
1377 } else if (INTEL_INFO(dev)->gen >= 5) {
649636ef 1378 u32 val = I915_READ(DVSCNTR(pipe));
e2c719b7 1379 I915_STATE_WARN(val & DVS_ENABLE,
06da8da2 1380 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef 1381 plane_name(pipe), pipe_name(pipe));
19332d7a
JB
1382 }
1383}
1384
08c71e5e
VS
1385static void assert_vblank_disabled(struct drm_crtc *crtc)
1386{
e2c719b7 1387 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
08c71e5e
VS
1388 drm_crtc_vblank_put(crtc);
1389}
1390
7abd4b35
ACO
1391void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1392 enum pipe pipe)
92f2584a 1393{
92f2584a
JB
1394 u32 val;
1395 bool enabled;
1396
649636ef 1397 val = I915_READ(PCH_TRANSCONF(pipe));
92f2584a 1398 enabled = !!(val & TRANS_ENABLE);
e2c719b7 1399 I915_STATE_WARN(enabled,
9db4a9c7
JB
1400 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1401 pipe_name(pipe));
92f2584a
JB
1402}
1403
4e634389
KP
1404static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1405 enum pipe pipe, u32 port_sel, u32 val)
f0575e92
KP
1406{
1407 if ((val & DP_PORT_EN) == 0)
1408 return false;
1409
1410 if (HAS_PCH_CPT(dev_priv->dev)) {
f0f59a00 1411 u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
f0575e92
KP
1412 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1413 return false;
44f37d1f
CML
1414 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1415 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1416 return false;
f0575e92
KP
1417 } else {
1418 if ((val & DP_PIPE_MASK) != (pipe << 30))
1419 return false;
1420 }
1421 return true;
1422}
1423
1519b995
KP
1424static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1425 enum pipe pipe, u32 val)
1426{
dc0fa718 1427 if ((val & SDVO_ENABLE) == 0)
1519b995
KP
1428 return false;
1429
1430 if (HAS_PCH_CPT(dev_priv->dev)) {
dc0fa718 1431 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1519b995 1432 return false;
44f37d1f
CML
1433 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1434 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1435 return false;
1519b995 1436 } else {
dc0fa718 1437 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1519b995
KP
1438 return false;
1439 }
1440 return true;
1441}
1442
1443static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1444 enum pipe pipe, u32 val)
1445{
1446 if ((val & LVDS_PORT_EN) == 0)
1447 return false;
1448
1449 if (HAS_PCH_CPT(dev_priv->dev)) {
1450 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1451 return false;
1452 } else {
1453 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1454 return false;
1455 }
1456 return true;
1457}
1458
1459static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1460 enum pipe pipe, u32 val)
1461{
1462 if ((val & ADPA_DAC_ENABLE) == 0)
1463 return false;
1464 if (HAS_PCH_CPT(dev_priv->dev)) {
1465 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1466 return false;
1467 } else {
1468 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1469 return false;
1470 }
1471 return true;
1472}
1473
291906f1 1474static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
f0f59a00
VS
1475 enum pipe pipe, i915_reg_t reg,
1476 u32 port_sel)
291906f1 1477{
47a05eca 1478 u32 val = I915_READ(reg);
e2c719b7 1479 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
291906f1 1480 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
f0f59a00 1481 i915_mmio_reg_offset(reg), pipe_name(pipe));
de9a35ab 1482
e2c719b7 1483 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
75c5da27 1484 && (val & DP_PIPEB_SELECT),
de9a35ab 1485 "IBX PCH dp port still using transcoder B\n");
291906f1
JB
1486}
1487
1488static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
f0f59a00 1489 enum pipe pipe, i915_reg_t reg)
291906f1 1490{
47a05eca 1491 u32 val = I915_READ(reg);
e2c719b7 1492 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
23c99e77 1493 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
f0f59a00 1494 i915_mmio_reg_offset(reg), pipe_name(pipe));
de9a35ab 1495
e2c719b7 1496 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
75c5da27 1497 && (val & SDVO_PIPE_B_SELECT),
de9a35ab 1498 "IBX PCH hdmi port still using transcoder B\n");
291906f1
JB
1499}
1500
1501static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1502 enum pipe pipe)
1503{
291906f1 1504 u32 val;
291906f1 1505
f0575e92
KP
1506 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1507 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1508 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
291906f1 1509
649636ef 1510 val = I915_READ(PCH_ADPA);
e2c719b7 1511 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
291906f1 1512 "PCH VGA enabled on transcoder %c, should be disabled\n",
9db4a9c7 1513 pipe_name(pipe));
291906f1 1514
649636ef 1515 val = I915_READ(PCH_LVDS);
e2c719b7 1516 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
291906f1 1517 "PCH LVDS enabled on transcoder %c, should be disabled\n",
9db4a9c7 1518 pipe_name(pipe));
291906f1 1519
e2debe91
PZ
1520 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1521 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1522 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
291906f1
JB
1523}
1524
d288f65f 1525static void vlv_enable_pll(struct intel_crtc *crtc,
5cec258b 1526 const struct intel_crtc_state *pipe_config)
87442f73 1527{
426115cf
DV
1528 struct drm_device *dev = crtc->base.dev;
1529 struct drm_i915_private *dev_priv = dev->dev_private;
f0f59a00 1530 i915_reg_t reg = DPLL(crtc->pipe);
d288f65f 1531 u32 dpll = pipe_config->dpll_hw_state.dpll;
87442f73 1532
426115cf 1533 assert_pipe_disabled(dev_priv, crtc->pipe);
87442f73 1534
87442f73 1535 /* PLL is protected by panel, make sure we can write it */
6a9e7363 1536 if (IS_MOBILE(dev_priv->dev))
426115cf 1537 assert_panel_unlocked(dev_priv, crtc->pipe);
87442f73 1538
426115cf
DV
1539 I915_WRITE(reg, dpll);
1540 POSTING_READ(reg);
1541 udelay(150);
1542
1543 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1544 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1545
d288f65f 1546 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
426115cf 1547 POSTING_READ(DPLL_MD(crtc->pipe));
87442f73
DV
1548
1549 /* We do this three times for luck */
426115cf 1550 I915_WRITE(reg, dpll);
87442f73
DV
1551 POSTING_READ(reg);
1552 udelay(150); /* wait for warmup */
426115cf 1553 I915_WRITE(reg, dpll);
87442f73
DV
1554 POSTING_READ(reg);
1555 udelay(150); /* wait for warmup */
426115cf 1556 I915_WRITE(reg, dpll);
87442f73
DV
1557 POSTING_READ(reg);
1558 udelay(150); /* wait for warmup */
1559}
1560
d288f65f 1561static void chv_enable_pll(struct intel_crtc *crtc,
5cec258b 1562 const struct intel_crtc_state *pipe_config)
9d556c99
CML
1563{
1564 struct drm_device *dev = crtc->base.dev;
1565 struct drm_i915_private *dev_priv = dev->dev_private;
1566 int pipe = crtc->pipe;
1567 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9d556c99
CML
1568 u32 tmp;
1569
1570 assert_pipe_disabled(dev_priv, crtc->pipe);
1571
a580516d 1572 mutex_lock(&dev_priv->sb_lock);
9d556c99
CML
1573
1574 /* Enable back the 10bit clock to display controller */
1575 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1576 tmp |= DPIO_DCLKP_EN;
1577 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1578
54433e91
VS
1579 mutex_unlock(&dev_priv->sb_lock);
1580
9d556c99
CML
1581 /*
1582 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1583 */
1584 udelay(1);
1585
1586 /* Enable PLL */
d288f65f 1587 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
9d556c99
CML
1588
1589 /* Check PLL is locked */
a11b0703 1590 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
9d556c99
CML
1591 DRM_ERROR("PLL %d failed to lock\n", pipe);
1592
a11b0703 1593 /* not sure when this should be written */
d288f65f 1594 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
a11b0703 1595 POSTING_READ(DPLL_MD(pipe));
9d556c99
CML
1596}
1597
1c4e0274
VS
1598static int intel_num_dvo_pipes(struct drm_device *dev)
1599{
1600 struct intel_crtc *crtc;
1601 int count = 0;
1602
1603 for_each_intel_crtc(dev, crtc)
3538b9df 1604 count += crtc->base.state->active &&
409ee761 1605 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
1c4e0274
VS
1606
1607 return count;
1608}
1609
66e3d5c0 1610static void i9xx_enable_pll(struct intel_crtc *crtc)
63d7bbe9 1611{
66e3d5c0
DV
1612 struct drm_device *dev = crtc->base.dev;
1613 struct drm_i915_private *dev_priv = dev->dev_private;
f0f59a00 1614 i915_reg_t reg = DPLL(crtc->pipe);
6e3c9717 1615 u32 dpll = crtc->config->dpll_hw_state.dpll;
63d7bbe9 1616
66e3d5c0 1617 assert_pipe_disabled(dev_priv, crtc->pipe);
58c6eaa2 1618
63d7bbe9 1619 /* No really, not for ILK+ */
3d13ef2e 1620 BUG_ON(INTEL_INFO(dev)->gen >= 5);
63d7bbe9
JB
1621
1622 /* PLL is protected by panel, make sure we can write it */
66e3d5c0
DV
1623 if (IS_MOBILE(dev) && !IS_I830(dev))
1624 assert_panel_unlocked(dev_priv, crtc->pipe);
63d7bbe9 1625
1c4e0274
VS
1626 /* Enable DVO 2x clock on both PLLs if necessary */
1627 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1628 /*
1629 * It appears to be important that we don't enable this
1630 * for the current pipe before otherwise configuring the
1631 * PLL. No idea how this should be handled if multiple
1632 * DVO outputs are enabled simultaneosly.
1633 */
1634 dpll |= DPLL_DVO_2X_MODE;
1635 I915_WRITE(DPLL(!crtc->pipe),
1636 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1637 }
66e3d5c0 1638
c2b63374
VS
1639 /*
1640 * Apparently we need to have VGA mode enabled prior to changing
1641 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1642 * dividers, even though the register value does change.
1643 */
1644 I915_WRITE(reg, 0);
1645
8e7a65aa
VS
1646 I915_WRITE(reg, dpll);
1647
66e3d5c0
DV
1648 /* Wait for the clocks to stabilize. */
1649 POSTING_READ(reg);
1650 udelay(150);
1651
1652 if (INTEL_INFO(dev)->gen >= 4) {
1653 I915_WRITE(DPLL_MD(crtc->pipe),
6e3c9717 1654 crtc->config->dpll_hw_state.dpll_md);
66e3d5c0
DV
1655 } else {
1656 /* The pixel multiplier can only be updated once the
1657 * DPLL is enabled and the clocks are stable.
1658 *
1659 * So write it again.
1660 */
1661 I915_WRITE(reg, dpll);
1662 }
63d7bbe9
JB
1663
1664 /* We do this three times for luck */
66e3d5c0 1665 I915_WRITE(reg, dpll);
63d7bbe9
JB
1666 POSTING_READ(reg);
1667 udelay(150); /* wait for warmup */
66e3d5c0 1668 I915_WRITE(reg, dpll);
63d7bbe9
JB
1669 POSTING_READ(reg);
1670 udelay(150); /* wait for warmup */
66e3d5c0 1671 I915_WRITE(reg, dpll);
63d7bbe9
JB
1672 POSTING_READ(reg);
1673 udelay(150); /* wait for warmup */
1674}
1675
1676/**
50b44a44 1677 * i9xx_disable_pll - disable a PLL
63d7bbe9
JB
1678 * @dev_priv: i915 private structure
1679 * @pipe: pipe PLL to disable
1680 *
1681 * Disable the PLL for @pipe, making sure the pipe is off first.
1682 *
1683 * Note! This is for pre-ILK only.
1684 */
1c4e0274 1685static void i9xx_disable_pll(struct intel_crtc *crtc)
63d7bbe9 1686{
1c4e0274
VS
1687 struct drm_device *dev = crtc->base.dev;
1688 struct drm_i915_private *dev_priv = dev->dev_private;
1689 enum pipe pipe = crtc->pipe;
1690
1691 /* Disable DVO 2x clock on both PLLs if necessary */
1692 if (IS_I830(dev) &&
409ee761 1693 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
3538b9df 1694 !intel_num_dvo_pipes(dev)) {
1c4e0274
VS
1695 I915_WRITE(DPLL(PIPE_B),
1696 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1697 I915_WRITE(DPLL(PIPE_A),
1698 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1699 }
1700
b6b5d049
VS
1701 /* Don't disable pipe or pipe PLLs if needed */
1702 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1703 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
63d7bbe9
JB
1704 return;
1705
1706 /* Make sure the pipe isn't still relying on us */
1707 assert_pipe_disabled(dev_priv, pipe);
1708
b8afb911 1709 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
50b44a44 1710 POSTING_READ(DPLL(pipe));
63d7bbe9
JB
1711}
1712
f6071166
JB
1713static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1714{
b8afb911 1715 u32 val;
f6071166
JB
1716
1717 /* Make sure the pipe isn't still relying on us */
1718 assert_pipe_disabled(dev_priv, pipe);
1719
e5cbfbfb
ID
1720 /*
1721 * Leave integrated clock source and reference clock enabled for pipe B.
1722 * The latter is needed for VGA hotplug / manual detection.
1723 */
b8afb911 1724 val = DPLL_VGA_MODE_DIS;
f6071166 1725 if (pipe == PIPE_B)
60bfe44f 1726 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REF_CLK_ENABLE_VLV;
f6071166
JB
1727 I915_WRITE(DPLL(pipe), val);
1728 POSTING_READ(DPLL(pipe));
076ed3b2
CML
1729
1730}
1731
1732static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1733{
d752048d 1734 enum dpio_channel port = vlv_pipe_to_channel(pipe);
076ed3b2
CML
1735 u32 val;
1736
a11b0703
VS
1737 /* Make sure the pipe isn't still relying on us */
1738 assert_pipe_disabled(dev_priv, pipe);
076ed3b2 1739
a11b0703 1740 /* Set PLL en = 0 */
60bfe44f
VS
1741 val = DPLL_SSC_REF_CLK_CHV |
1742 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
a11b0703
VS
1743 if (pipe != PIPE_A)
1744 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1745 I915_WRITE(DPLL(pipe), val);
1746 POSTING_READ(DPLL(pipe));
d752048d 1747
a580516d 1748 mutex_lock(&dev_priv->sb_lock);
d752048d
VS
1749
1750 /* Disable 10bit clock to display controller */
1751 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1752 val &= ~DPIO_DCLKP_EN;
1753 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1754
a580516d 1755 mutex_unlock(&dev_priv->sb_lock);
f6071166
JB
1756}
1757
e4607fcf 1758void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
9b6de0a1
VS
1759 struct intel_digital_port *dport,
1760 unsigned int expected_mask)
89b667f8
JB
1761{
1762 u32 port_mask;
f0f59a00 1763 i915_reg_t dpll_reg;
89b667f8 1764
e4607fcf
CML
1765 switch (dport->port) {
1766 case PORT_B:
89b667f8 1767 port_mask = DPLL_PORTB_READY_MASK;
00fc31b7 1768 dpll_reg = DPLL(0);
e4607fcf
CML
1769 break;
1770 case PORT_C:
89b667f8 1771 port_mask = DPLL_PORTC_READY_MASK;
00fc31b7 1772 dpll_reg = DPLL(0);
9b6de0a1 1773 expected_mask <<= 4;
00fc31b7
CML
1774 break;
1775 case PORT_D:
1776 port_mask = DPLL_PORTD_READY_MASK;
1777 dpll_reg = DPIO_PHY_STATUS;
e4607fcf
CML
1778 break;
1779 default:
1780 BUG();
1781 }
89b667f8 1782
9b6de0a1
VS
1783 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
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{
23670b32 1791 struct drm_device *dev = dev_priv->dev;
7c26e5c6 1792 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
e2b78267 1793 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
f0f59a00
VS
1794 i915_reg_t reg;
1795 uint32_t val, pipeconf_val;
040484af
JB
1796
1797 /* PCH only available on ILK+ */
55522f37 1798 BUG_ON(!HAS_PCH_SPLIT(dev));
040484af
JB
1799
1800 /* Make sure PCH DPLL is enabled */
8106ddbd 1801 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
040484af
JB
1802
1803 /* FDI must be feeding us bits for PCH ports */
1804 assert_fdi_tx_enabled(dev_priv, pipe);
1805 assert_fdi_rx_enabled(dev_priv, pipe);
1806
23670b32
DV
1807 if (HAS_PCH_CPT(dev)) {
1808 /* Workaround: Set the timing override bit before enabling the
1809 * pch transcoder. */
1810 reg = TRANS_CHICKEN2(pipe);
1811 val = I915_READ(reg);
1812 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1813 I915_WRITE(reg, val);
59c859d6 1814 }
23670b32 1815
ab9412ba 1816 reg = PCH_TRANSCONF(pipe);
040484af 1817 val = I915_READ(reg);
5f7f726d 1818 pipeconf_val = I915_READ(PIPECONF(pipe));
e9bcff5c
JB
1819
1820 if (HAS_PCH_IBX(dev_priv->dev)) {
1821 /*
c5de7c6f
VS
1822 * Make the BPC in transcoder be consistent with
1823 * that in pipeconf reg. For HDMI we must use 8bpc
1824 * here for both 8bpc and 12bpc.
e9bcff5c 1825 */
dfd07d72 1826 val &= ~PIPECONF_BPC_MASK;
c5de7c6f
VS
1827 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
1828 val |= PIPECONF_8BPC;
1829 else
1830 val |= pipeconf_val & PIPECONF_BPC_MASK;
e9bcff5c 1831 }
5f7f726d
PZ
1832
1833 val &= ~TRANS_INTERLACE_MASK;
1834 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
7c26e5c6 1835 if (HAS_PCH_IBX(dev_priv->dev) &&
409ee761 1836 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
7c26e5c6
PZ
1837 val |= TRANS_LEGACY_INTERLACED_ILK;
1838 else
1839 val |= TRANS_INTERLACED;
5f7f726d
PZ
1840 else
1841 val |= TRANS_PROGRESSIVE;
1842
040484af
JB
1843 I915_WRITE(reg, val | TRANS_ENABLE);
1844 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
4bb6f1f3 1845 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
040484af
JB
1846}
1847
8fb033d7 1848static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
937bb610 1849 enum transcoder cpu_transcoder)
040484af 1850{
8fb033d7 1851 u32 val, pipeconf_val;
8fb033d7
PZ
1852
1853 /* PCH only available on ILK+ */
55522f37 1854 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
8fb033d7 1855
8fb033d7 1856 /* FDI must be feeding us bits for PCH ports */
1a240d4d 1857 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
937bb610 1858 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
8fb033d7 1859
223a6fdf 1860 /* Workaround: set timing override bit. */
36c0d0cf 1861 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
23670b32 1862 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
36c0d0cf 1863 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
223a6fdf 1864
25f3ef11 1865 val = TRANS_ENABLE;
937bb610 1866 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
8fb033d7 1867
9a76b1c6
PZ
1868 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1869 PIPECONF_INTERLACED_ILK)
a35f2679 1870 val |= TRANS_INTERLACED;
8fb033d7
PZ
1871 else
1872 val |= TRANS_PROGRESSIVE;
1873
ab9412ba
DV
1874 I915_WRITE(LPT_TRANSCONF, val);
1875 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
937bb610 1876 DRM_ERROR("Failed to enable PCH transcoder\n");
8fb033d7
PZ
1877}
1878
b8a4f404
PZ
1879static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1880 enum pipe pipe)
040484af 1881{
23670b32 1882 struct drm_device *dev = dev_priv->dev;
f0f59a00
VS
1883 i915_reg_t reg;
1884 uint32_t val;
040484af
JB
1885
1886 /* FDI relies on the transcoder */
1887 assert_fdi_tx_disabled(dev_priv, pipe);
1888 assert_fdi_rx_disabled(dev_priv, pipe);
1889
291906f1
JB
1890 /* Ports must be off as well */
1891 assert_pch_ports_disabled(dev_priv, pipe);
1892
ab9412ba 1893 reg = PCH_TRANSCONF(pipe);
040484af
JB
1894 val = I915_READ(reg);
1895 val &= ~TRANS_ENABLE;
1896 I915_WRITE(reg, val);
1897 /* wait for PCH transcoder off, transcoder state */
1898 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
4bb6f1f3 1899 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
23670b32 1900
c465613b 1901 if (HAS_PCH_CPT(dev)) {
23670b32
DV
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
ab4d966c 1910static void 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 */
ab9412ba 1918 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
8a52fd9f 1919 DRM_ERROR("Failed to disable PCH transcoder\n");
223a6fdf
PZ
1920
1921 /* Workaround: clear timing override bit. */
36c0d0cf 1922 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
23670b32 1923 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
36c0d0cf 1924 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
040484af
JB
1925}
1926
b24e7179 1927/**
309cfea8 1928 * intel_enable_pipe - enable a pipe, asserting requirements
0372264a 1929 * @crtc: crtc responsible for the pipe
b24e7179 1930 *
0372264a 1931 * Enable @crtc's pipe, making sure that various hardware specific requirements
b24e7179 1932 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
b24e7179 1933 */
e1fdc473 1934static void intel_enable_pipe(struct intel_crtc *crtc)
b24e7179 1935{
0372264a
PZ
1936 struct drm_device *dev = crtc->base.dev;
1937 struct drm_i915_private *dev_priv = dev->dev_private;
1938 enum pipe pipe = crtc->pipe;
1a70a728 1939 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
1a240d4d 1940 enum pipe pch_transcoder;
f0f59a00 1941 i915_reg_t reg;
b24e7179
JB
1942 u32 val;
1943
9e2ee2dd
VS
1944 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1945
58c6eaa2 1946 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 1947 assert_cursor_disabled(dev_priv, pipe);
58c6eaa2
DV
1948 assert_sprites_disabled(dev_priv, pipe);
1949
681e5811 1950 if (HAS_PCH_LPT(dev_priv->dev))
cc391bbb
PZ
1951 pch_transcoder = TRANSCODER_A;
1952 else
1953 pch_transcoder = pipe;
1954
b24e7179
JB
1955 /*
1956 * A pipe without a PLL won't actually be able to drive bits from
1957 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1958 * need the check.
1959 */
50360403 1960 if (HAS_GMCH_DISPLAY(dev_priv->dev))
a65347ba 1961 if (crtc->config->has_dsi_encoder)
23538ef1
JN
1962 assert_dsi_pll_enabled(dev_priv);
1963 else
1964 assert_pll_enabled(dev_priv, pipe);
040484af 1965 else {
6e3c9717 1966 if (crtc->config->has_pch_encoder) {
040484af 1967 /* if driving the PCH, we need FDI enabled */
cc391bbb 1968 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
1a240d4d
DV
1969 assert_fdi_tx_pll_enabled(dev_priv,
1970 (enum pipe) cpu_transcoder);
040484af
JB
1971 }
1972 /* FIXME: assert CPU port conditions for SNB+ */
1973 }
b24e7179 1974
702e7a56 1975 reg = PIPECONF(cpu_transcoder);
b24e7179 1976 val = I915_READ(reg);
7ad25d48 1977 if (val & PIPECONF_ENABLE) {
b6b5d049
VS
1978 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1979 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
00d70b15 1980 return;
7ad25d48 1981 }
00d70b15
CW
1982
1983 I915_WRITE(reg, val | PIPECONF_ENABLE);
851855d8 1984 POSTING_READ(reg);
b7792d8b
VS
1985
1986 /*
1987 * Until the pipe starts DSL will read as 0, which would cause
1988 * an apparent vblank timestamp jump, which messes up also the
1989 * frame count when it's derived from the timestamps. So let's
1990 * wait for the pipe to start properly before we call
1991 * drm_crtc_vblank_on()
1992 */
1993 if (dev->max_vblank_count == 0 &&
1994 wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
1995 DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
b24e7179
JB
1996}
1997
1998/**
309cfea8 1999 * intel_disable_pipe - disable a pipe, asserting requirements
575f7ab7 2000 * @crtc: crtc whose pipes is to be disabled
b24e7179 2001 *
575f7ab7
VS
2002 * Disable the pipe of @crtc, making sure that various hardware
2003 * specific requirements are met, if applicable, e.g. plane
2004 * disabled, panel fitter off, etc.
b24e7179
JB
2005 *
2006 * Will wait until the pipe has shut down before returning.
2007 */
575f7ab7 2008static void intel_disable_pipe(struct intel_crtc *crtc)
b24e7179 2009{
575f7ab7 2010 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
6e3c9717 2011 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 2012 enum pipe pipe = crtc->pipe;
f0f59a00 2013 i915_reg_t reg;
b24e7179
JB
2014 u32 val;
2015
9e2ee2dd
VS
2016 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2017
b24e7179
JB
2018 /*
2019 * Make sure planes won't keep trying to pump pixels to us,
2020 * or we might hang the display.
2021 */
2022 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2023 assert_cursor_disabled(dev_priv, pipe);
19332d7a 2024 assert_sprites_disabled(dev_priv, pipe);
b24e7179 2025
702e7a56 2026 reg = PIPECONF(cpu_transcoder);
b24e7179 2027 val = I915_READ(reg);
00d70b15
CW
2028 if ((val & PIPECONF_ENABLE) == 0)
2029 return;
2030
67adc644
VS
2031 /*
2032 * Double wide has implications for planes
2033 * so best keep it disabled when not needed.
2034 */
6e3c9717 2035 if (crtc->config->double_wide)
67adc644
VS
2036 val &= ~PIPECONF_DOUBLE_WIDE;
2037
2038 /* Don't disable pipe or pipe PLLs if needed */
b6b5d049
VS
2039 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2040 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
67adc644
VS
2041 val &= ~PIPECONF_ENABLE;
2042
2043 I915_WRITE(reg, val);
2044 if ((val & PIPECONF_ENABLE) == 0)
2045 intel_wait_for_pipe_off(crtc);
b24e7179
JB
2046}
2047
693db184
CW
2048static bool need_vtd_wa(struct drm_device *dev)
2049{
2050#ifdef CONFIG_INTEL_IOMMU
2051 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2052 return true;
2053#endif
2054 return false;
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
2d7a215f
VS
2157static void
2158intel_fill_fb_info(struct drm_i915_private *dev_priv,
2159 struct drm_framebuffer *fb)
2160{
2161 struct intel_rotation_info *info = &to_intel_framebuffer(fb)->rot_info;
2162 unsigned int tile_size, tile_width, tile_height, cpp;
50470bb0 2163
d9b3288e
VS
2164 tile_size = intel_tile_size(dev_priv);
2165
2166 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
8d0deca8
VS
2167 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2168 fb->modifier[0], cpp);
d9b3288e 2169
1663b9d6
VS
2170 info->plane[0].width = DIV_ROUND_UP(fb->pitches[0], tile_width * cpp);
2171 info->plane[0].height = DIV_ROUND_UP(fb->height, tile_height);
84fe03f7 2172
89e3e142 2173 if (info->pixel_format == DRM_FORMAT_NV12) {
832be82f 2174 cpp = drm_format_plane_cpp(fb->pixel_format, 1);
8d0deca8
VS
2175 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2176 fb->modifier[1], cpp);
d9b3288e 2177
2d7a215f 2178 info->uv_offset = fb->offsets[1];
1663b9d6
VS
2179 info->plane[1].width = DIV_ROUND_UP(fb->pitches[1], tile_width * cpp);
2180 info->plane[1].height = DIV_ROUND_UP(fb->height / 2, tile_height);
89e3e142 2181 }
f64b98cd
TU
2182}
2183
603525d7 2184static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
4e9a86b6
VS
2185{
2186 if (INTEL_INFO(dev_priv)->gen >= 9)
2187 return 256 * 1024;
985b8bb4 2188 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
666a4537 2189 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
4e9a86b6
VS
2190 return 128 * 1024;
2191 else if (INTEL_INFO(dev_priv)->gen >= 4)
2192 return 4 * 1024;
2193 else
44c5905e 2194 return 0;
4e9a86b6
VS
2195}
2196
603525d7
VS
2197static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
2198 uint64_t fb_modifier)
2199{
2200 switch (fb_modifier) {
2201 case DRM_FORMAT_MOD_NONE:
2202 return intel_linear_alignment(dev_priv);
2203 case I915_FORMAT_MOD_X_TILED:
2204 if (INTEL_INFO(dev_priv)->gen >= 9)
2205 return 256 * 1024;
2206 return 0;
2207 case I915_FORMAT_MOD_Y_TILED:
2208 case I915_FORMAT_MOD_Yf_TILED:
2209 return 1 * 1024 * 1024;
2210 default:
2211 MISSING_CASE(fb_modifier);
2212 return 0;
2213 }
2214}
2215
127bd2ac 2216int
3465c580
VS
2217intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
2218 unsigned int rotation)
6b95a207 2219{
850c4cdc 2220 struct drm_device *dev = fb->dev;
ce453d81 2221 struct drm_i915_private *dev_priv = dev->dev_private;
850c4cdc 2222 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2223 struct i915_ggtt_view view;
6b95a207
KH
2224 u32 alignment;
2225 int ret;
2226
ebcdd39e
MR
2227 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2228
603525d7 2229 alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
6b95a207 2230
3465c580 2231 intel_fill_fb_ggtt_view(&view, fb, rotation);
f64b98cd 2232
693db184
CW
2233 /* Note that the w/a also requires 64 PTE of padding following the
2234 * bo. We currently fill all unused PTE with the shadow page and so
2235 * we should always have valid PTE following the scanout preventing
2236 * the VT-d warning.
2237 */
2238 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2239 alignment = 256 * 1024;
2240
d6dd6843
PZ
2241 /*
2242 * Global gtt pte registers are special registers which actually forward
2243 * writes to a chunk of system memory. Which means that there is no risk
2244 * that the register values disappear as soon as we call
2245 * intel_runtime_pm_put(), so it is correct to wrap only the
2246 * pin/unpin/fence and not more.
2247 */
2248 intel_runtime_pm_get(dev_priv);
2249
7580d774
ML
2250 ret = i915_gem_object_pin_to_display_plane(obj, alignment,
2251 &view);
48b956c5 2252 if (ret)
b26a6b35 2253 goto err_pm;
6b95a207
KH
2254
2255 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2256 * fence, whereas 965+ only requires a fence if using
2257 * framebuffer compression. For simplicity, we always install
2258 * a fence as the cost is not that onerous.
2259 */
9807216f
VK
2260 if (view.type == I915_GGTT_VIEW_NORMAL) {
2261 ret = i915_gem_object_get_fence(obj);
2262 if (ret == -EDEADLK) {
2263 /*
2264 * -EDEADLK means there are no free fences
2265 * no pending flips.
2266 *
2267 * This is propagated to atomic, but it uses
2268 * -EDEADLK to force a locking recovery, so
2269 * change the returned error to -EBUSY.
2270 */
2271 ret = -EBUSY;
2272 goto err_unpin;
2273 } else if (ret)
2274 goto err_unpin;
1690e1eb 2275
9807216f
VK
2276 i915_gem_object_pin_fence(obj);
2277 }
6b95a207 2278
d6dd6843 2279 intel_runtime_pm_put(dev_priv);
6b95a207 2280 return 0;
48b956c5
CW
2281
2282err_unpin:
f64b98cd 2283 i915_gem_object_unpin_from_display_plane(obj, &view);
b26a6b35 2284err_pm:
d6dd6843 2285 intel_runtime_pm_put(dev_priv);
48b956c5 2286 return ret;
6b95a207
KH
2287}
2288
3465c580 2289static void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
1690e1eb 2290{
82bc3b2d 2291 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2292 struct i915_ggtt_view view;
82bc3b2d 2293
ebcdd39e
MR
2294 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2295
3465c580 2296 intel_fill_fb_ggtt_view(&view, fb, rotation);
f64b98cd 2297
9807216f
VK
2298 if (view.type == I915_GGTT_VIEW_NORMAL)
2299 i915_gem_object_unpin_fence(obj);
2300
f64b98cd 2301 i915_gem_object_unpin_from_display_plane(obj, &view);
1690e1eb
CW
2302}
2303
29cf9491
VS
2304/*
2305 * Adjust the tile offset by moving the difference into
2306 * the x/y offsets.
2307 *
2308 * Input tile dimensions and pitch must already be
2309 * rotated to match x and y, and in pixel units.
2310 */
2311static u32 intel_adjust_tile_offset(int *x, int *y,
2312 unsigned int tile_width,
2313 unsigned int tile_height,
2314 unsigned int tile_size,
2315 unsigned int pitch_tiles,
2316 u32 old_offset,
2317 u32 new_offset)
2318{
2319 unsigned int tiles;
2320
2321 WARN_ON(old_offset & (tile_size - 1));
2322 WARN_ON(new_offset & (tile_size - 1));
2323 WARN_ON(new_offset > old_offset);
2324
2325 tiles = (old_offset - new_offset) / tile_size;
2326
2327 *y += tiles / pitch_tiles * tile_height;
2328 *x += tiles % pitch_tiles * tile_width;
2329
2330 return new_offset;
2331}
2332
8d0deca8
VS
2333/*
2334 * Computes the linear offset to the base tile and adjusts
2335 * x, y. bytes per pixel is assumed to be a power-of-two.
2336 *
2337 * In the 90/270 rotated case, x and y are assumed
2338 * to be already rotated to match the rotated GTT view, and
2339 * pitch is the tile_height aligned framebuffer height.
2340 */
4f2d9934
VS
2341u32 intel_compute_tile_offset(int *x, int *y,
2342 const struct drm_framebuffer *fb, int plane,
8d0deca8
VS
2343 unsigned int pitch,
2344 unsigned int rotation)
c2c75131 2345{
4f2d9934
VS
2346 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
2347 uint64_t fb_modifier = fb->modifier[plane];
2348 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
29cf9491
VS
2349 u32 offset, offset_aligned, alignment;
2350
2351 alignment = intel_surf_alignment(dev_priv, fb_modifier);
2352 if (alignment)
2353 alignment--;
2354
b5c65338 2355 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
8d0deca8
VS
2356 unsigned int tile_size, tile_width, tile_height;
2357 unsigned int tile_rows, tiles, pitch_tiles;
c2c75131 2358
d843310d 2359 tile_size = intel_tile_size(dev_priv);
8d0deca8
VS
2360 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2361 fb_modifier, cpp);
2362
2363 if (intel_rotation_90_or_270(rotation)) {
2364 pitch_tiles = pitch / tile_height;
2365 swap(tile_width, tile_height);
2366 } else {
2367 pitch_tiles = pitch / (tile_width * cpp);
2368 }
d843310d
VS
2369
2370 tile_rows = *y / tile_height;
2371 *y %= tile_height;
c2c75131 2372
8d0deca8
VS
2373 tiles = *x / tile_width;
2374 *x %= tile_width;
bc752862 2375
29cf9491
VS
2376 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2377 offset_aligned = offset & ~alignment;
bc752862 2378
29cf9491
VS
2379 intel_adjust_tile_offset(x, y, tile_width, tile_height,
2380 tile_size, pitch_tiles,
2381 offset, offset_aligned);
2382 } else {
bc752862 2383 offset = *y * pitch + *x * cpp;
29cf9491
VS
2384 offset_aligned = offset & ~alignment;
2385
4e9a86b6
VS
2386 *y = (offset & alignment) / pitch;
2387 *x = ((offset & alignment) - *y * pitch) / cpp;
bc752862 2388 }
29cf9491
VS
2389
2390 return offset_aligned;
c2c75131
DV
2391}
2392
b35d63fa 2393static int i9xx_format_to_fourcc(int format)
46f297fb
JB
2394{
2395 switch (format) {
2396 case DISPPLANE_8BPP:
2397 return DRM_FORMAT_C8;
2398 case DISPPLANE_BGRX555:
2399 return DRM_FORMAT_XRGB1555;
2400 case DISPPLANE_BGRX565:
2401 return DRM_FORMAT_RGB565;
2402 default:
2403 case DISPPLANE_BGRX888:
2404 return DRM_FORMAT_XRGB8888;
2405 case DISPPLANE_RGBX888:
2406 return DRM_FORMAT_XBGR8888;
2407 case DISPPLANE_BGRX101010:
2408 return DRM_FORMAT_XRGB2101010;
2409 case DISPPLANE_RGBX101010:
2410 return DRM_FORMAT_XBGR2101010;
2411 }
2412}
2413
bc8d7dff
DL
2414static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2415{
2416 switch (format) {
2417 case PLANE_CTL_FORMAT_RGB_565:
2418 return DRM_FORMAT_RGB565;
2419 default:
2420 case PLANE_CTL_FORMAT_XRGB_8888:
2421 if (rgb_order) {
2422 if (alpha)
2423 return DRM_FORMAT_ABGR8888;
2424 else
2425 return DRM_FORMAT_XBGR8888;
2426 } else {
2427 if (alpha)
2428 return DRM_FORMAT_ARGB8888;
2429 else
2430 return DRM_FORMAT_XRGB8888;
2431 }
2432 case PLANE_CTL_FORMAT_XRGB_2101010:
2433 if (rgb_order)
2434 return DRM_FORMAT_XBGR2101010;
2435 else
2436 return DRM_FORMAT_XRGB2101010;
2437 }
2438}
2439
5724dbd1 2440static bool
f6936e29
DV
2441intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2442 struct intel_initial_plane_config *plane_config)
46f297fb
JB
2443{
2444 struct drm_device *dev = crtc->base.dev;
3badb49f 2445 struct drm_i915_private *dev_priv = to_i915(dev);
46f297fb
JB
2446 struct drm_i915_gem_object *obj = NULL;
2447 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2d14030b 2448 struct drm_framebuffer *fb = &plane_config->fb->base;
f37b5c2b
DV
2449 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2450 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2451 PAGE_SIZE);
2452
2453 size_aligned -= base_aligned;
46f297fb 2454
ff2652ea
CW
2455 if (plane_config->size == 0)
2456 return false;
2457
3badb49f
PZ
2458 /* If the FB is too big, just don't use it since fbdev is not very
2459 * important and we should probably use that space with FBC or other
2460 * features. */
62106b4f 2461 if (size_aligned * 2 > dev_priv->ggtt.stolen_usable_size)
3badb49f
PZ
2462 return false;
2463
12c83d99
TU
2464 mutex_lock(&dev->struct_mutex);
2465
f37b5c2b
DV
2466 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2467 base_aligned,
2468 base_aligned,
2469 size_aligned);
12c83d99
TU
2470 if (!obj) {
2471 mutex_unlock(&dev->struct_mutex);
484b41dd 2472 return false;
12c83d99 2473 }
46f297fb 2474
49af449b
DL
2475 obj->tiling_mode = plane_config->tiling;
2476 if (obj->tiling_mode == I915_TILING_X)
6bf129df 2477 obj->stride = fb->pitches[0];
46f297fb 2478
6bf129df
DL
2479 mode_cmd.pixel_format = fb->pixel_format;
2480 mode_cmd.width = fb->width;
2481 mode_cmd.height = fb->height;
2482 mode_cmd.pitches[0] = fb->pitches[0];
18c5247e
DV
2483 mode_cmd.modifier[0] = fb->modifier[0];
2484 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
46f297fb 2485
6bf129df 2486 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
484b41dd 2487 &mode_cmd, obj)) {
46f297fb
JB
2488 DRM_DEBUG_KMS("intel fb init failed\n");
2489 goto out_unref_obj;
2490 }
12c83d99 2491
46f297fb 2492 mutex_unlock(&dev->struct_mutex);
484b41dd 2493
f6936e29 2494 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
484b41dd 2495 return true;
46f297fb
JB
2496
2497out_unref_obj:
2498 drm_gem_object_unreference(&obj->base);
2499 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2500 return false;
2501}
2502
afd65eb4
MR
2503/* Update plane->state->fb to match plane->fb after driver-internal updates */
2504static void
2505update_state_fb(struct drm_plane *plane)
2506{
2507 if (plane->fb == plane->state->fb)
2508 return;
2509
2510 if (plane->state->fb)
2511 drm_framebuffer_unreference(plane->state->fb);
2512 plane->state->fb = plane->fb;
2513 if (plane->state->fb)
2514 drm_framebuffer_reference(plane->state->fb);
2515}
2516
5724dbd1 2517static void
f6936e29
DV
2518intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2519 struct intel_initial_plane_config *plane_config)
484b41dd
JB
2520{
2521 struct drm_device *dev = intel_crtc->base.dev;
d9ceb816 2522 struct drm_i915_private *dev_priv = dev->dev_private;
484b41dd
JB
2523 struct drm_crtc *c;
2524 struct intel_crtc *i;
2ff8fde1 2525 struct drm_i915_gem_object *obj;
88595ac9 2526 struct drm_plane *primary = intel_crtc->base.primary;
be5651f2 2527 struct drm_plane_state *plane_state = primary->state;
200757f5
MR
2528 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2529 struct intel_plane *intel_plane = to_intel_plane(primary);
0a8d8a86
MR
2530 struct intel_plane_state *intel_state =
2531 to_intel_plane_state(plane_state);
88595ac9 2532 struct drm_framebuffer *fb;
484b41dd 2533
2d14030b 2534 if (!plane_config->fb)
484b41dd
JB
2535 return;
2536
f6936e29 2537 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
88595ac9
DV
2538 fb = &plane_config->fb->base;
2539 goto valid_fb;
f55548b5 2540 }
484b41dd 2541
2d14030b 2542 kfree(plane_config->fb);
484b41dd
JB
2543
2544 /*
2545 * Failed to alloc the obj, check to see if we should share
2546 * an fb with another CRTC instead
2547 */
70e1e0ec 2548 for_each_crtc(dev, c) {
484b41dd
JB
2549 i = to_intel_crtc(c);
2550
2551 if (c == &intel_crtc->base)
2552 continue;
2553
2ff8fde1
MR
2554 if (!i->active)
2555 continue;
2556
88595ac9
DV
2557 fb = c->primary->fb;
2558 if (!fb)
484b41dd
JB
2559 continue;
2560
88595ac9 2561 obj = intel_fb_obj(fb);
2ff8fde1 2562 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
88595ac9
DV
2563 drm_framebuffer_reference(fb);
2564 goto valid_fb;
484b41dd
JB
2565 }
2566 }
88595ac9 2567
200757f5
MR
2568 /*
2569 * We've failed to reconstruct the BIOS FB. Current display state
2570 * indicates that the primary plane is visible, but has a NULL FB,
2571 * which will lead to problems later if we don't fix it up. The
2572 * simplest solution is to just disable the primary plane now and
2573 * pretend the BIOS never had it enabled.
2574 */
2575 to_intel_plane_state(plane_state)->visible = false;
2576 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
2622a081 2577 intel_pre_disable_primary_noatomic(&intel_crtc->base);
200757f5
MR
2578 intel_plane->disable_plane(primary, &intel_crtc->base);
2579
88595ac9
DV
2580 return;
2581
2582valid_fb:
f44e2659
VS
2583 plane_state->src_x = 0;
2584 plane_state->src_y = 0;
be5651f2
ML
2585 plane_state->src_w = fb->width << 16;
2586 plane_state->src_h = fb->height << 16;
2587
f44e2659
VS
2588 plane_state->crtc_x = 0;
2589 plane_state->crtc_y = 0;
be5651f2
ML
2590 plane_state->crtc_w = fb->width;
2591 plane_state->crtc_h = fb->height;
2592
0a8d8a86
MR
2593 intel_state->src.x1 = plane_state->src_x;
2594 intel_state->src.y1 = plane_state->src_y;
2595 intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
2596 intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
2597 intel_state->dst.x1 = plane_state->crtc_x;
2598 intel_state->dst.y1 = plane_state->crtc_y;
2599 intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
2600 intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
2601
88595ac9
DV
2602 obj = intel_fb_obj(fb);
2603 if (obj->tiling_mode != I915_TILING_NONE)
2604 dev_priv->preserve_bios_swizzle = true;
2605
be5651f2
ML
2606 drm_framebuffer_reference(fb);
2607 primary->fb = primary->state->fb = fb;
36750f28 2608 primary->crtc = primary->state->crtc = &intel_crtc->base;
36750f28 2609 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
a9ff8714 2610 obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
46f297fb
JB
2611}
2612
a8d201af
ML
2613static void i9xx_update_primary_plane(struct drm_plane *primary,
2614 const struct intel_crtc_state *crtc_state,
2615 const struct intel_plane_state *plane_state)
81255565 2616{
a8d201af 2617 struct drm_device *dev = primary->dev;
81255565 2618 struct drm_i915_private *dev_priv = dev->dev_private;
a8d201af
ML
2619 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2620 struct drm_framebuffer *fb = plane_state->base.fb;
2621 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
81255565 2622 int plane = intel_crtc->plane;
54ea9da8 2623 u32 linear_offset;
81255565 2624 u32 dspcntr;
f0f59a00 2625 i915_reg_t reg = DSPCNTR(plane);
8d0deca8 2626 unsigned int rotation = plane_state->base.rotation;
ac484963 2627 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
54ea9da8
VS
2628 int x = plane_state->src.x1 >> 16;
2629 int y = plane_state->src.y1 >> 16;
c9ba6fad 2630
f45651ba
VS
2631 dspcntr = DISPPLANE_GAMMA_ENABLE;
2632
fdd508a6 2633 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2634
2635 if (INTEL_INFO(dev)->gen < 4) {
2636 if (intel_crtc->pipe == PIPE_B)
2637 dspcntr |= DISPPLANE_SEL_PIPE_B;
2638
2639 /* pipesrc and dspsize control the size that is scaled from,
2640 * which should always be the user's requested size.
2641 */
2642 I915_WRITE(DSPSIZE(plane),
a8d201af
ML
2643 ((crtc_state->pipe_src_h - 1) << 16) |
2644 (crtc_state->pipe_src_w - 1));
f45651ba 2645 I915_WRITE(DSPPOS(plane), 0);
c14b0485
VS
2646 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2647 I915_WRITE(PRIMSIZE(plane),
a8d201af
ML
2648 ((crtc_state->pipe_src_h - 1) << 16) |
2649 (crtc_state->pipe_src_w - 1));
c14b0485
VS
2650 I915_WRITE(PRIMPOS(plane), 0);
2651 I915_WRITE(PRIMCNSTALPHA(plane), 0);
f45651ba 2652 }
81255565 2653
57779d06
VS
2654 switch (fb->pixel_format) {
2655 case DRM_FORMAT_C8:
81255565
JB
2656 dspcntr |= DISPPLANE_8BPP;
2657 break;
57779d06 2658 case DRM_FORMAT_XRGB1555:
57779d06 2659 dspcntr |= DISPPLANE_BGRX555;
81255565 2660 break;
57779d06
VS
2661 case DRM_FORMAT_RGB565:
2662 dspcntr |= DISPPLANE_BGRX565;
2663 break;
2664 case DRM_FORMAT_XRGB8888:
57779d06
VS
2665 dspcntr |= DISPPLANE_BGRX888;
2666 break;
2667 case DRM_FORMAT_XBGR8888:
57779d06
VS
2668 dspcntr |= DISPPLANE_RGBX888;
2669 break;
2670 case DRM_FORMAT_XRGB2101010:
57779d06
VS
2671 dspcntr |= DISPPLANE_BGRX101010;
2672 break;
2673 case DRM_FORMAT_XBGR2101010:
57779d06 2674 dspcntr |= DISPPLANE_RGBX101010;
81255565
JB
2675 break;
2676 default:
baba133a 2677 BUG();
81255565 2678 }
57779d06 2679
f45651ba
VS
2680 if (INTEL_INFO(dev)->gen >= 4 &&
2681 obj->tiling_mode != I915_TILING_NONE)
2682 dspcntr |= DISPPLANE_TILED;
81255565 2683
de1aa629
VS
2684 if (IS_G4X(dev))
2685 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2686
ac484963 2687 linear_offset = y * fb->pitches[0] + x * cpp;
81255565 2688
c2c75131
DV
2689 if (INTEL_INFO(dev)->gen >= 4) {
2690 intel_crtc->dspaddr_offset =
4f2d9934 2691 intel_compute_tile_offset(&x, &y, fb, 0,
8d0deca8 2692 fb->pitches[0], rotation);
c2c75131
DV
2693 linear_offset -= intel_crtc->dspaddr_offset;
2694 } else {
e506a0c6 2695 intel_crtc->dspaddr_offset = linear_offset;
c2c75131 2696 }
e506a0c6 2697
8d0deca8 2698 if (rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2699 dspcntr |= DISPPLANE_ROTATE_180;
2700
a8d201af
ML
2701 x += (crtc_state->pipe_src_w - 1);
2702 y += (crtc_state->pipe_src_h - 1);
48404c1e
SJ
2703
2704 /* Finding the last pixel of the last line of the display
2705 data and adding to linear_offset*/
2706 linear_offset +=
a8d201af 2707 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
ac484963 2708 (crtc_state->pipe_src_w - 1) * cpp;
48404c1e
SJ
2709 }
2710
2db3366b
PZ
2711 intel_crtc->adjusted_x = x;
2712 intel_crtc->adjusted_y = y;
2713
48404c1e
SJ
2714 I915_WRITE(reg, dspcntr);
2715
01f2c773 2716 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
a6c45cf0 2717 if (INTEL_INFO(dev)->gen >= 4) {
85ba7b7d
DV
2718 I915_WRITE(DSPSURF(plane),
2719 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
5eddb70b 2720 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
e506a0c6 2721 I915_WRITE(DSPLINOFF(plane), linear_offset);
5eddb70b 2722 } else
f343c5f6 2723 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
5eddb70b 2724 POSTING_READ(reg);
17638cd6
JB
2725}
2726
a8d201af
ML
2727static void i9xx_disable_primary_plane(struct drm_plane *primary,
2728 struct drm_crtc *crtc)
17638cd6
JB
2729{
2730 struct drm_device *dev = crtc->dev;
2731 struct drm_i915_private *dev_priv = dev->dev_private;
2732 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
17638cd6 2733 int plane = intel_crtc->plane;
f45651ba 2734
a8d201af
ML
2735 I915_WRITE(DSPCNTR(plane), 0);
2736 if (INTEL_INFO(dev_priv)->gen >= 4)
fdd508a6 2737 I915_WRITE(DSPSURF(plane), 0);
a8d201af
ML
2738 else
2739 I915_WRITE(DSPADDR(plane), 0);
2740 POSTING_READ(DSPCNTR(plane));
2741}
c9ba6fad 2742
a8d201af
ML
2743static void ironlake_update_primary_plane(struct drm_plane *primary,
2744 const struct intel_crtc_state *crtc_state,
2745 const struct intel_plane_state *plane_state)
2746{
2747 struct drm_device *dev = primary->dev;
2748 struct drm_i915_private *dev_priv = dev->dev_private;
2749 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2750 struct drm_framebuffer *fb = plane_state->base.fb;
2751 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
2752 int plane = intel_crtc->plane;
54ea9da8 2753 u32 linear_offset;
a8d201af
ML
2754 u32 dspcntr;
2755 i915_reg_t reg = DSPCNTR(plane);
8d0deca8 2756 unsigned int rotation = plane_state->base.rotation;
ac484963 2757 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
a8d201af
ML
2758 int x = plane_state->src.x1 >> 16;
2759 int y = plane_state->src.y1 >> 16;
c9ba6fad 2760
f45651ba 2761 dspcntr = DISPPLANE_GAMMA_ENABLE;
fdd508a6 2762 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2763
2764 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2765 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
17638cd6 2766
57779d06
VS
2767 switch (fb->pixel_format) {
2768 case DRM_FORMAT_C8:
17638cd6
JB
2769 dspcntr |= DISPPLANE_8BPP;
2770 break;
57779d06
VS
2771 case DRM_FORMAT_RGB565:
2772 dspcntr |= DISPPLANE_BGRX565;
17638cd6 2773 break;
57779d06 2774 case DRM_FORMAT_XRGB8888:
57779d06
VS
2775 dspcntr |= DISPPLANE_BGRX888;
2776 break;
2777 case DRM_FORMAT_XBGR8888:
57779d06
VS
2778 dspcntr |= DISPPLANE_RGBX888;
2779 break;
2780 case DRM_FORMAT_XRGB2101010:
57779d06
VS
2781 dspcntr |= DISPPLANE_BGRX101010;
2782 break;
2783 case DRM_FORMAT_XBGR2101010:
57779d06 2784 dspcntr |= DISPPLANE_RGBX101010;
17638cd6
JB
2785 break;
2786 default:
baba133a 2787 BUG();
17638cd6
JB
2788 }
2789
2790 if (obj->tiling_mode != I915_TILING_NONE)
2791 dspcntr |= DISPPLANE_TILED;
17638cd6 2792
f45651ba 2793 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
1f5d76db 2794 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
17638cd6 2795
ac484963 2796 linear_offset = y * fb->pitches[0] + x * cpp;
c2c75131 2797 intel_crtc->dspaddr_offset =
4f2d9934 2798 intel_compute_tile_offset(&x, &y, fb, 0,
8d0deca8 2799 fb->pitches[0], rotation);
c2c75131 2800 linear_offset -= intel_crtc->dspaddr_offset;
8d0deca8 2801 if (rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2802 dspcntr |= DISPPLANE_ROTATE_180;
2803
2804 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
a8d201af
ML
2805 x += (crtc_state->pipe_src_w - 1);
2806 y += (crtc_state->pipe_src_h - 1);
48404c1e
SJ
2807
2808 /* Finding the last pixel of the last line of the display
2809 data and adding to linear_offset*/
2810 linear_offset +=
a8d201af 2811 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
ac484963 2812 (crtc_state->pipe_src_w - 1) * cpp;
48404c1e
SJ
2813 }
2814 }
2815
2db3366b
PZ
2816 intel_crtc->adjusted_x = x;
2817 intel_crtc->adjusted_y = y;
2818
48404c1e 2819 I915_WRITE(reg, dspcntr);
17638cd6 2820
01f2c773 2821 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
85ba7b7d
DV
2822 I915_WRITE(DSPSURF(plane),
2823 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
b3dc685e 2824 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
bc1c91eb
DL
2825 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2826 } else {
2827 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2828 I915_WRITE(DSPLINOFF(plane), linear_offset);
2829 }
17638cd6 2830 POSTING_READ(reg);
17638cd6
JB
2831}
2832
7b49f948
VS
2833u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
2834 uint64_t fb_modifier, uint32_t pixel_format)
b321803d 2835{
7b49f948 2836 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
b321803d 2837 return 64;
7b49f948
VS
2838 } else {
2839 int cpp = drm_format_plane_cpp(pixel_format, 0);
2840
27ba3910 2841 return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
b321803d
DL
2842 }
2843}
2844
44eb0cb9
MK
2845u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
2846 struct drm_i915_gem_object *obj,
2847 unsigned int plane)
121920fa 2848{
ce7f1728 2849 struct i915_ggtt_view view;
dedf278c 2850 struct i915_vma *vma;
44eb0cb9 2851 u64 offset;
121920fa 2852
e7941294 2853 intel_fill_fb_ggtt_view(&view, intel_plane->base.state->fb,
3465c580 2854 intel_plane->base.state->rotation);
121920fa 2855
ce7f1728 2856 vma = i915_gem_obj_to_ggtt_view(obj, &view);
dedf278c 2857 if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
ce7f1728 2858 view.type))
dedf278c
TU
2859 return -1;
2860
44eb0cb9 2861 offset = vma->node.start;
dedf278c
TU
2862
2863 if (plane == 1) {
7723f47d 2864 offset += vma->ggtt_view.params.rotated.uv_start_page *
dedf278c
TU
2865 PAGE_SIZE;
2866 }
2867
44eb0cb9
MK
2868 WARN_ON(upper_32_bits(offset));
2869
2870 return lower_32_bits(offset);
121920fa
TU
2871}
2872
e435d6e5
ML
2873static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
2874{
2875 struct drm_device *dev = intel_crtc->base.dev;
2876 struct drm_i915_private *dev_priv = dev->dev_private;
2877
2878 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
2879 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
2880 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
e435d6e5
ML
2881}
2882
a1b2278e
CK
2883/*
2884 * This function detaches (aka. unbinds) unused scalers in hardware
2885 */
0583236e 2886static void skl_detach_scalers(struct intel_crtc *intel_crtc)
a1b2278e 2887{
a1b2278e
CK
2888 struct intel_crtc_scaler_state *scaler_state;
2889 int i;
2890
a1b2278e
CK
2891 scaler_state = &intel_crtc->config->scaler_state;
2892
2893 /* loop through and disable scalers that aren't in use */
2894 for (i = 0; i < intel_crtc->num_scalers; i++) {
e435d6e5
ML
2895 if (!scaler_state->scalers[i].in_use)
2896 skl_detach_scaler(intel_crtc, i);
a1b2278e
CK
2897 }
2898}
2899
6156a456 2900u32 skl_plane_ctl_format(uint32_t pixel_format)
70d21f0e 2901{
6156a456 2902 switch (pixel_format) {
d161cf7a 2903 case DRM_FORMAT_C8:
c34ce3d1 2904 return PLANE_CTL_FORMAT_INDEXED;
70d21f0e 2905 case DRM_FORMAT_RGB565:
c34ce3d1 2906 return PLANE_CTL_FORMAT_RGB_565;
70d21f0e 2907 case DRM_FORMAT_XBGR8888:
c34ce3d1 2908 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
6156a456 2909 case DRM_FORMAT_XRGB8888:
c34ce3d1 2910 return PLANE_CTL_FORMAT_XRGB_8888;
6156a456
CK
2911 /*
2912 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2913 * to be already pre-multiplied. We need to add a knob (or a different
2914 * DRM_FORMAT) for user-space to configure that.
2915 */
f75fb42a 2916 case DRM_FORMAT_ABGR8888:
c34ce3d1 2917 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
6156a456 2918 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
6156a456 2919 case DRM_FORMAT_ARGB8888:
c34ce3d1 2920 return PLANE_CTL_FORMAT_XRGB_8888 |
6156a456 2921 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
70d21f0e 2922 case DRM_FORMAT_XRGB2101010:
c34ce3d1 2923 return PLANE_CTL_FORMAT_XRGB_2101010;
70d21f0e 2924 case DRM_FORMAT_XBGR2101010:
c34ce3d1 2925 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
6156a456 2926 case DRM_FORMAT_YUYV:
c34ce3d1 2927 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
6156a456 2928 case DRM_FORMAT_YVYU:
c34ce3d1 2929 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
6156a456 2930 case DRM_FORMAT_UYVY:
c34ce3d1 2931 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
6156a456 2932 case DRM_FORMAT_VYUY:
c34ce3d1 2933 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
70d21f0e 2934 default:
4249eeef 2935 MISSING_CASE(pixel_format);
70d21f0e 2936 }
8cfcba41 2937
c34ce3d1 2938 return 0;
6156a456 2939}
70d21f0e 2940
6156a456
CK
2941u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2942{
6156a456 2943 switch (fb_modifier) {
30af77c4 2944 case DRM_FORMAT_MOD_NONE:
70d21f0e 2945 break;
30af77c4 2946 case I915_FORMAT_MOD_X_TILED:
c34ce3d1 2947 return PLANE_CTL_TILED_X;
b321803d 2948 case I915_FORMAT_MOD_Y_TILED:
c34ce3d1 2949 return PLANE_CTL_TILED_Y;
b321803d 2950 case I915_FORMAT_MOD_Yf_TILED:
c34ce3d1 2951 return PLANE_CTL_TILED_YF;
70d21f0e 2952 default:
6156a456 2953 MISSING_CASE(fb_modifier);
70d21f0e 2954 }
8cfcba41 2955
c34ce3d1 2956 return 0;
6156a456 2957}
70d21f0e 2958
6156a456
CK
2959u32 skl_plane_ctl_rotation(unsigned int rotation)
2960{
3b7a5119 2961 switch (rotation) {
6156a456
CK
2962 case BIT(DRM_ROTATE_0):
2963 break;
1e8df167
SJ
2964 /*
2965 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
2966 * while i915 HW rotation is clockwise, thats why this swapping.
2967 */
3b7a5119 2968 case BIT(DRM_ROTATE_90):
1e8df167 2969 return PLANE_CTL_ROTATE_270;
3b7a5119 2970 case BIT(DRM_ROTATE_180):
c34ce3d1 2971 return PLANE_CTL_ROTATE_180;
3b7a5119 2972 case BIT(DRM_ROTATE_270):
1e8df167 2973 return PLANE_CTL_ROTATE_90;
6156a456
CK
2974 default:
2975 MISSING_CASE(rotation);
2976 }
2977
c34ce3d1 2978 return 0;
6156a456
CK
2979}
2980
a8d201af
ML
2981static void skylake_update_primary_plane(struct drm_plane *plane,
2982 const struct intel_crtc_state *crtc_state,
2983 const struct intel_plane_state *plane_state)
6156a456 2984{
a8d201af 2985 struct drm_device *dev = plane->dev;
6156a456 2986 struct drm_i915_private *dev_priv = dev->dev_private;
a8d201af
ML
2987 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2988 struct drm_framebuffer *fb = plane_state->base.fb;
2989 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
6156a456
CK
2990 int pipe = intel_crtc->pipe;
2991 u32 plane_ctl, stride_div, stride;
2992 u32 tile_height, plane_offset, plane_size;
a8d201af 2993 unsigned int rotation = plane_state->base.rotation;
6156a456 2994 int x_offset, y_offset;
44eb0cb9 2995 u32 surf_addr;
a8d201af
ML
2996 int scaler_id = plane_state->scaler_id;
2997 int src_x = plane_state->src.x1 >> 16;
2998 int src_y = plane_state->src.y1 >> 16;
2999 int src_w = drm_rect_width(&plane_state->src) >> 16;
3000 int src_h = drm_rect_height(&plane_state->src) >> 16;
3001 int dst_x = plane_state->dst.x1;
3002 int dst_y = plane_state->dst.y1;
3003 int dst_w = drm_rect_width(&plane_state->dst);
3004 int dst_h = drm_rect_height(&plane_state->dst);
70d21f0e 3005
6156a456
CK
3006 plane_ctl = PLANE_CTL_ENABLE |
3007 PLANE_CTL_PIPE_GAMMA_ENABLE |
3008 PLANE_CTL_PIPE_CSC_ENABLE;
3009
3010 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3011 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
3012 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
6156a456
CK
3013 plane_ctl |= skl_plane_ctl_rotation(rotation);
3014
7b49f948 3015 stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
b321803d 3016 fb->pixel_format);
dedf278c 3017 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
3b7a5119 3018
a42e5a23
PZ
3019 WARN_ON(drm_rect_width(&plane_state->src) == 0);
3020
3b7a5119 3021 if (intel_rotation_90_or_270(rotation)) {
832be82f
VS
3022 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
3023
3b7a5119 3024 /* stride = Surface height in tiles */
832be82f 3025 tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
3b7a5119 3026 stride = DIV_ROUND_UP(fb->height, tile_height);
a8d201af
ML
3027 x_offset = stride * tile_height - src_y - src_h;
3028 y_offset = src_x;
6156a456 3029 plane_size = (src_w - 1) << 16 | (src_h - 1);
3b7a5119
SJ
3030 } else {
3031 stride = fb->pitches[0] / stride_div;
a8d201af
ML
3032 x_offset = src_x;
3033 y_offset = src_y;
6156a456 3034 plane_size = (src_h - 1) << 16 | (src_w - 1);
3b7a5119
SJ
3035 }
3036 plane_offset = y_offset << 16 | x_offset;
b321803d 3037
2db3366b
PZ
3038 intel_crtc->adjusted_x = x_offset;
3039 intel_crtc->adjusted_y = y_offset;
3040
70d21f0e 3041 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
3b7a5119
SJ
3042 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3043 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3044 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
6156a456
CK
3045
3046 if (scaler_id >= 0) {
3047 uint32_t ps_ctrl = 0;
3048
3049 WARN_ON(!dst_w || !dst_h);
3050 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3051 crtc_state->scaler_state.scalers[scaler_id].mode;
3052 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3053 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3054 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3055 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3056 I915_WRITE(PLANE_POS(pipe, 0), 0);
3057 } else {
3058 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3059 }
3060
121920fa 3061 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
70d21f0e
DL
3062
3063 POSTING_READ(PLANE_SURF(pipe, 0));
3064}
3065
a8d201af
ML
3066static void skylake_disable_primary_plane(struct drm_plane *primary,
3067 struct drm_crtc *crtc)
17638cd6
JB
3068{
3069 struct drm_device *dev = crtc->dev;
3070 struct drm_i915_private *dev_priv = dev->dev_private;
a8d201af 3071 int pipe = to_intel_crtc(crtc)->pipe;
17638cd6 3072
a8d201af
ML
3073 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3074 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3075 POSTING_READ(PLANE_SURF(pipe, 0));
3076}
29b9bde6 3077
a8d201af
ML
3078/* Assume fb object is pinned & idle & fenced and just update base pointers */
3079static int
3080intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3081 int x, int y, enum mode_set_atomic state)
3082{
3083 /* Support for kgdboc is disabled, this needs a major rework. */
3084 DRM_ERROR("legacy panic handler not supported any more.\n");
3085
3086 return -ENODEV;
81255565
JB
3087}
3088
7514747d 3089static void intel_complete_page_flips(struct drm_device *dev)
96a02917 3090{
96a02917
VS
3091 struct drm_crtc *crtc;
3092
70e1e0ec 3093 for_each_crtc(dev, crtc) {
96a02917
VS
3094 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3095 enum plane plane = intel_crtc->plane;
3096
3097 intel_prepare_page_flip(dev, plane);
3098 intel_finish_page_flip_plane(dev, plane);
3099 }
7514747d
VS
3100}
3101
3102static void intel_update_primary_planes(struct drm_device *dev)
3103{
7514747d 3104 struct drm_crtc *crtc;
96a02917 3105
70e1e0ec 3106 for_each_crtc(dev, crtc) {
11c22da6
ML
3107 struct intel_plane *plane = to_intel_plane(crtc->primary);
3108 struct intel_plane_state *plane_state;
96a02917 3109
11c22da6 3110 drm_modeset_lock_crtc(crtc, &plane->base);
11c22da6
ML
3111 plane_state = to_intel_plane_state(plane->base.state);
3112
a8d201af
ML
3113 if (plane_state->visible)
3114 plane->update_plane(&plane->base,
3115 to_intel_crtc_state(crtc->state),
3116 plane_state);
11c22da6
ML
3117
3118 drm_modeset_unlock_crtc(crtc);
96a02917
VS
3119 }
3120}
3121
7514747d
VS
3122void intel_prepare_reset(struct drm_device *dev)
3123{
3124 /* no reset support for gen2 */
3125 if (IS_GEN2(dev))
3126 return;
3127
3128 /* reset doesn't touch the display */
3129 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3130 return;
3131
3132 drm_modeset_lock_all(dev);
f98ce92f
VS
3133 /*
3134 * Disabling the crtcs gracefully seems nicer. Also the
3135 * g33 docs say we should at least disable all the planes.
3136 */
6b72d486 3137 intel_display_suspend(dev);
7514747d
VS
3138}
3139
3140void intel_finish_reset(struct drm_device *dev)
3141{
3142 struct drm_i915_private *dev_priv = to_i915(dev);
3143
3144 /*
3145 * Flips in the rings will be nuked by the reset,
3146 * so complete all pending flips so that user space
3147 * will get its events and not get stuck.
3148 */
3149 intel_complete_page_flips(dev);
3150
3151 /* no reset support for gen2 */
3152 if (IS_GEN2(dev))
3153 return;
3154
3155 /* reset doesn't touch the display */
3156 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3157 /*
3158 * Flips in the rings have been nuked by the reset,
3159 * so update the base address of all primary
3160 * planes to the the last fb to make sure we're
3161 * showing the correct fb after a reset.
11c22da6
ML
3162 *
3163 * FIXME: Atomic will make this obsolete since we won't schedule
3164 * CS-based flips (which might get lost in gpu resets) any more.
7514747d
VS
3165 */
3166 intel_update_primary_planes(dev);
3167 return;
3168 }
3169
3170 /*
3171 * The display has been reset as well,
3172 * so need a full re-initialization.
3173 */
3174 intel_runtime_pm_disable_interrupts(dev_priv);
3175 intel_runtime_pm_enable_interrupts(dev_priv);
3176
3177 intel_modeset_init_hw(dev);
3178
3179 spin_lock_irq(&dev_priv->irq_lock);
3180 if (dev_priv->display.hpd_irq_setup)
3181 dev_priv->display.hpd_irq_setup(dev);
3182 spin_unlock_irq(&dev_priv->irq_lock);
3183
043e9bda 3184 intel_display_resume(dev);
7514747d
VS
3185
3186 intel_hpd_init(dev_priv);
3187
3188 drm_modeset_unlock_all(dev);
3189}
3190
7d5e3799
CW
3191static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3192{
3193 struct drm_device *dev = crtc->dev;
3194 struct drm_i915_private *dev_priv = dev->dev_private;
3195 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7d5e3799
CW
3196 bool pending;
3197
3198 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3199 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3200 return false;
3201
5e2d7afc 3202 spin_lock_irq(&dev->event_lock);
7d5e3799 3203 pending = to_intel_crtc(crtc)->unpin_work != NULL;
5e2d7afc 3204 spin_unlock_irq(&dev->event_lock);
7d5e3799
CW
3205
3206 return pending;
3207}
3208
bfd16b2a
ML
3209static void intel_update_pipe_config(struct intel_crtc *crtc,
3210 struct intel_crtc_state *old_crtc_state)
e30e8f75
GP
3211{
3212 struct drm_device *dev = crtc->base.dev;
3213 struct drm_i915_private *dev_priv = dev->dev_private;
bfd16b2a
ML
3214 struct intel_crtc_state *pipe_config =
3215 to_intel_crtc_state(crtc->base.state);
e30e8f75 3216
bfd16b2a
ML
3217 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3218 crtc->base.mode = crtc->base.state->mode;
3219
3220 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3221 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3222 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
e30e8f75 3223
44522d85 3224 if (HAS_DDI(dev))
8563b1e8 3225 intel_color_set_csc(&crtc->base);
44522d85 3226
e30e8f75
GP
3227 /*
3228 * Update pipe size and adjust fitter if needed: the reason for this is
3229 * that in compute_mode_changes we check the native mode (not the pfit
3230 * mode) to see if we can flip rather than do a full mode set. In the
3231 * fastboot case, we'll flip, but if we don't update the pipesrc and
3232 * pfit state, we'll end up with a big fb scanned out into the wrong
3233 * sized surface.
e30e8f75
GP
3234 */
3235
e30e8f75 3236 I915_WRITE(PIPESRC(crtc->pipe),
bfd16b2a
ML
3237 ((pipe_config->pipe_src_w - 1) << 16) |
3238 (pipe_config->pipe_src_h - 1));
3239
3240 /* on skylake this is done by detaching scalers */
3241 if (INTEL_INFO(dev)->gen >= 9) {
3242 skl_detach_scalers(crtc);
3243
3244 if (pipe_config->pch_pfit.enabled)
3245 skylake_pfit_enable(crtc);
3246 } else if (HAS_PCH_SPLIT(dev)) {
3247 if (pipe_config->pch_pfit.enabled)
3248 ironlake_pfit_enable(crtc);
3249 else if (old_crtc_state->pch_pfit.enabled)
3250 ironlake_pfit_disable(crtc, true);
e30e8f75 3251 }
e30e8f75
GP
3252}
3253
5e84e1a4
ZW
3254static void intel_fdi_normal_train(struct drm_crtc *crtc)
3255{
3256 struct drm_device *dev = crtc->dev;
3257 struct drm_i915_private *dev_priv = dev->dev_private;
3258 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3259 int pipe = intel_crtc->pipe;
f0f59a00
VS
3260 i915_reg_t reg;
3261 u32 temp;
5e84e1a4
ZW
3262
3263 /* enable normal train */
3264 reg = FDI_TX_CTL(pipe);
3265 temp = I915_READ(reg);
61e499bf 3266 if (IS_IVYBRIDGE(dev)) {
357555c0
JB
3267 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3268 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
3269 } else {
3270 temp &= ~FDI_LINK_TRAIN_NONE;
3271 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 3272 }
5e84e1a4
ZW
3273 I915_WRITE(reg, temp);
3274
3275 reg = FDI_RX_CTL(pipe);
3276 temp = I915_READ(reg);
3277 if (HAS_PCH_CPT(dev)) {
3278 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3279 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3280 } else {
3281 temp &= ~FDI_LINK_TRAIN_NONE;
3282 temp |= FDI_LINK_TRAIN_NONE;
3283 }
3284 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3285
3286 /* wait one idle pattern time */
3287 POSTING_READ(reg);
3288 udelay(1000);
357555c0
JB
3289
3290 /* IVB wants error correction enabled */
3291 if (IS_IVYBRIDGE(dev))
3292 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3293 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
3294}
3295
8db9d77b
ZW
3296/* The FDI link training functions for ILK/Ibexpeak. */
3297static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3298{
3299 struct drm_device *dev = crtc->dev;
3300 struct drm_i915_private *dev_priv = dev->dev_private;
3301 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3302 int pipe = intel_crtc->pipe;
f0f59a00
VS
3303 i915_reg_t reg;
3304 u32 temp, tries;
8db9d77b 3305
1c8562f6 3306 /* FDI needs bits from pipe first */
0fc932b8 3307 assert_pipe_enabled(dev_priv, pipe);
0fc932b8 3308
e1a44743
AJ
3309 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3310 for train result */
5eddb70b
CW
3311 reg = FDI_RX_IMR(pipe);
3312 temp = I915_READ(reg);
e1a44743
AJ
3313 temp &= ~FDI_RX_SYMBOL_LOCK;
3314 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3315 I915_WRITE(reg, temp);
3316 I915_READ(reg);
e1a44743
AJ
3317 udelay(150);
3318
8db9d77b 3319 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3320 reg = FDI_TX_CTL(pipe);
3321 temp = I915_READ(reg);
627eb5a3 3322 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3323 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3324 temp &= ~FDI_LINK_TRAIN_NONE;
3325 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 3326 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3327
5eddb70b
CW
3328 reg = FDI_RX_CTL(pipe);
3329 temp = I915_READ(reg);
8db9d77b
ZW
3330 temp &= ~FDI_LINK_TRAIN_NONE;
3331 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b
CW
3332 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3333
3334 POSTING_READ(reg);
8db9d77b
ZW
3335 udelay(150);
3336
5b2adf89 3337 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
DV
3338 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3339 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3340 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 3341
5eddb70b 3342 reg = FDI_RX_IIR(pipe);
e1a44743 3343 for (tries = 0; tries < 5; tries++) {
5eddb70b 3344 temp = I915_READ(reg);
8db9d77b
ZW
3345 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3346
3347 if ((temp & FDI_RX_BIT_LOCK)) {
3348 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 3349 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
3350 break;
3351 }
8db9d77b 3352 }
e1a44743 3353 if (tries == 5)
5eddb70b 3354 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3355
3356 /* Train 2 */
5eddb70b
CW
3357 reg = FDI_TX_CTL(pipe);
3358 temp = I915_READ(reg);
8db9d77b
ZW
3359 temp &= ~FDI_LINK_TRAIN_NONE;
3360 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3361 I915_WRITE(reg, temp);
8db9d77b 3362
5eddb70b
CW
3363 reg = FDI_RX_CTL(pipe);
3364 temp = I915_READ(reg);
8db9d77b
ZW
3365 temp &= ~FDI_LINK_TRAIN_NONE;
3366 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3367 I915_WRITE(reg, temp);
8db9d77b 3368
5eddb70b
CW
3369 POSTING_READ(reg);
3370 udelay(150);
8db9d77b 3371
5eddb70b 3372 reg = FDI_RX_IIR(pipe);
e1a44743 3373 for (tries = 0; tries < 5; tries++) {
5eddb70b 3374 temp = I915_READ(reg);
8db9d77b
ZW
3375 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3376
3377 if (temp & FDI_RX_SYMBOL_LOCK) {
5eddb70b 3378 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
3379 DRM_DEBUG_KMS("FDI train 2 done.\n");
3380 break;
3381 }
8db9d77b 3382 }
e1a44743 3383 if (tries == 5)
5eddb70b 3384 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3385
3386 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 3387
8db9d77b
ZW
3388}
3389
0206e353 3390static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
3391 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3392 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3393 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3394 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3395};
3396
3397/* The FDI link training functions for SNB/Cougarpoint. */
3398static void gen6_fdi_link_train(struct drm_crtc *crtc)
3399{
3400 struct drm_device *dev = crtc->dev;
3401 struct drm_i915_private *dev_priv = dev->dev_private;
3402 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3403 int pipe = intel_crtc->pipe;
f0f59a00
VS
3404 i915_reg_t reg;
3405 u32 temp, i, retry;
8db9d77b 3406
e1a44743
AJ
3407 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3408 for train result */
5eddb70b
CW
3409 reg = FDI_RX_IMR(pipe);
3410 temp = I915_READ(reg);
e1a44743
AJ
3411 temp &= ~FDI_RX_SYMBOL_LOCK;
3412 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3413 I915_WRITE(reg, temp);
3414
3415 POSTING_READ(reg);
e1a44743
AJ
3416 udelay(150);
3417
8db9d77b 3418 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3419 reg = FDI_TX_CTL(pipe);
3420 temp = I915_READ(reg);
627eb5a3 3421 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3422 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3423 temp &= ~FDI_LINK_TRAIN_NONE;
3424 temp |= FDI_LINK_TRAIN_PATTERN_1;
3425 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3426 /* SNB-B */
3427 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 3428 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3429
d74cf324
DV
3430 I915_WRITE(FDI_RX_MISC(pipe),
3431 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3432
5eddb70b
CW
3433 reg = FDI_RX_CTL(pipe);
3434 temp = I915_READ(reg);
8db9d77b
ZW
3435 if (HAS_PCH_CPT(dev)) {
3436 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3437 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3438 } else {
3439 temp &= ~FDI_LINK_TRAIN_NONE;
3440 temp |= FDI_LINK_TRAIN_PATTERN_1;
3441 }
5eddb70b
CW
3442 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3443
3444 POSTING_READ(reg);
8db9d77b
ZW
3445 udelay(150);
3446
0206e353 3447 for (i = 0; i < 4; i++) {
5eddb70b
CW
3448 reg = FDI_TX_CTL(pipe);
3449 temp = I915_READ(reg);
8db9d77b
ZW
3450 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3451 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3452 I915_WRITE(reg, temp);
3453
3454 POSTING_READ(reg);
8db9d77b
ZW
3455 udelay(500);
3456
fa37d39e
SP
3457 for (retry = 0; retry < 5; retry++) {
3458 reg = FDI_RX_IIR(pipe);
3459 temp = I915_READ(reg);
3460 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3461 if (temp & FDI_RX_BIT_LOCK) {
3462 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3463 DRM_DEBUG_KMS("FDI train 1 done.\n");
3464 break;
3465 }
3466 udelay(50);
8db9d77b 3467 }
fa37d39e
SP
3468 if (retry < 5)
3469 break;
8db9d77b
ZW
3470 }
3471 if (i == 4)
5eddb70b 3472 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3473
3474 /* Train 2 */
5eddb70b
CW
3475 reg = FDI_TX_CTL(pipe);
3476 temp = I915_READ(reg);
8db9d77b
ZW
3477 temp &= ~FDI_LINK_TRAIN_NONE;
3478 temp |= FDI_LINK_TRAIN_PATTERN_2;
3479 if (IS_GEN6(dev)) {
3480 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3481 /* SNB-B */
3482 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3483 }
5eddb70b 3484 I915_WRITE(reg, temp);
8db9d77b 3485
5eddb70b
CW
3486 reg = FDI_RX_CTL(pipe);
3487 temp = I915_READ(reg);
8db9d77b
ZW
3488 if (HAS_PCH_CPT(dev)) {
3489 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3490 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3491 } else {
3492 temp &= ~FDI_LINK_TRAIN_NONE;
3493 temp |= FDI_LINK_TRAIN_PATTERN_2;
3494 }
5eddb70b
CW
3495 I915_WRITE(reg, temp);
3496
3497 POSTING_READ(reg);
8db9d77b
ZW
3498 udelay(150);
3499
0206e353 3500 for (i = 0; i < 4; i++) {
5eddb70b
CW
3501 reg = FDI_TX_CTL(pipe);
3502 temp = I915_READ(reg);
8db9d77b
ZW
3503 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3504 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3505 I915_WRITE(reg, temp);
3506
3507 POSTING_READ(reg);
8db9d77b
ZW
3508 udelay(500);
3509
fa37d39e
SP
3510 for (retry = 0; retry < 5; retry++) {
3511 reg = FDI_RX_IIR(pipe);
3512 temp = I915_READ(reg);
3513 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3514 if (temp & FDI_RX_SYMBOL_LOCK) {
3515 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3516 DRM_DEBUG_KMS("FDI train 2 done.\n");
3517 break;
3518 }
3519 udelay(50);
8db9d77b 3520 }
fa37d39e
SP
3521 if (retry < 5)
3522 break;
8db9d77b
ZW
3523 }
3524 if (i == 4)
5eddb70b 3525 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3526
3527 DRM_DEBUG_KMS("FDI train done.\n");
3528}
3529
357555c0
JB
3530/* Manual link training for Ivy Bridge A0 parts */
3531static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3532{
3533 struct drm_device *dev = crtc->dev;
3534 struct drm_i915_private *dev_priv = dev->dev_private;
3535 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3536 int pipe = intel_crtc->pipe;
f0f59a00
VS
3537 i915_reg_t reg;
3538 u32 temp, i, j;
357555c0
JB
3539
3540 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3541 for train result */
3542 reg = FDI_RX_IMR(pipe);
3543 temp = I915_READ(reg);
3544 temp &= ~FDI_RX_SYMBOL_LOCK;
3545 temp &= ~FDI_RX_BIT_LOCK;
3546 I915_WRITE(reg, temp);
3547
3548 POSTING_READ(reg);
3549 udelay(150);
3550
01a415fd
DV
3551 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3552 I915_READ(FDI_RX_IIR(pipe)));
3553
139ccd3f
JB
3554 /* Try each vswing and preemphasis setting twice before moving on */
3555 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3556 /* disable first in case we need to retry */
3557 reg = FDI_TX_CTL(pipe);
3558 temp = I915_READ(reg);
3559 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3560 temp &= ~FDI_TX_ENABLE;
3561 I915_WRITE(reg, temp);
357555c0 3562
139ccd3f
JB
3563 reg = FDI_RX_CTL(pipe);
3564 temp = I915_READ(reg);
3565 temp &= ~FDI_LINK_TRAIN_AUTO;
3566 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3567 temp &= ~FDI_RX_ENABLE;
3568 I915_WRITE(reg, temp);
357555c0 3569
139ccd3f 3570 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
3571 reg = FDI_TX_CTL(pipe);
3572 temp = I915_READ(reg);
139ccd3f 3573 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3574 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
139ccd3f 3575 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 3576 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
3577 temp |= snb_b_fdi_train_param[j/2];
3578 temp |= FDI_COMPOSITE_SYNC;
3579 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 3580
139ccd3f
JB
3581 I915_WRITE(FDI_RX_MISC(pipe),
3582 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 3583
139ccd3f 3584 reg = FDI_RX_CTL(pipe);
357555c0 3585 temp = I915_READ(reg);
139ccd3f
JB
3586 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3587 temp |= FDI_COMPOSITE_SYNC;
3588 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 3589
139ccd3f
JB
3590 POSTING_READ(reg);
3591 udelay(1); /* should be 0.5us */
357555c0 3592
139ccd3f
JB
3593 for (i = 0; i < 4; i++) {
3594 reg = FDI_RX_IIR(pipe);
3595 temp = I915_READ(reg);
3596 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3597
139ccd3f
JB
3598 if (temp & FDI_RX_BIT_LOCK ||
3599 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3600 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3601 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3602 i);
3603 break;
3604 }
3605 udelay(1); /* should be 0.5us */
3606 }
3607 if (i == 4) {
3608 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3609 continue;
3610 }
357555c0 3611
139ccd3f 3612 /* Train 2 */
357555c0
JB
3613 reg = FDI_TX_CTL(pipe);
3614 temp = I915_READ(reg);
139ccd3f
JB
3615 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3616 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3617 I915_WRITE(reg, temp);
3618
3619 reg = FDI_RX_CTL(pipe);
3620 temp = I915_READ(reg);
3621 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3622 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
3623 I915_WRITE(reg, temp);
3624
3625 POSTING_READ(reg);
139ccd3f 3626 udelay(2); /* should be 1.5us */
357555c0 3627
139ccd3f
JB
3628 for (i = 0; i < 4; i++) {
3629 reg = FDI_RX_IIR(pipe);
3630 temp = I915_READ(reg);
3631 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3632
139ccd3f
JB
3633 if (temp & FDI_RX_SYMBOL_LOCK ||
3634 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3635 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3636 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3637 i);
3638 goto train_done;
3639 }
3640 udelay(2); /* should be 1.5us */
357555c0 3641 }
139ccd3f
JB
3642 if (i == 4)
3643 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 3644 }
357555c0 3645
139ccd3f 3646train_done:
357555c0
JB
3647 DRM_DEBUG_KMS("FDI train done.\n");
3648}
3649
88cefb6c 3650static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 3651{
88cefb6c 3652 struct drm_device *dev = intel_crtc->base.dev;
2c07245f 3653 struct drm_i915_private *dev_priv = dev->dev_private;
2c07245f 3654 int pipe = intel_crtc->pipe;
f0f59a00
VS
3655 i915_reg_t reg;
3656 u32 temp;
c64e311e 3657
c98e9dcf 3658 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
3659 reg = FDI_RX_CTL(pipe);
3660 temp = I915_READ(reg);
627eb5a3 3661 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
6e3c9717 3662 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
dfd07d72 3663 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
3664 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3665
3666 POSTING_READ(reg);
c98e9dcf
JB
3667 udelay(200);
3668
3669 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
3670 temp = I915_READ(reg);
3671 I915_WRITE(reg, temp | FDI_PCDCLK);
3672
3673 POSTING_READ(reg);
c98e9dcf
JB
3674 udelay(200);
3675
20749730
PZ
3676 /* Enable CPU FDI TX PLL, always on for Ironlake */
3677 reg = FDI_TX_CTL(pipe);
3678 temp = I915_READ(reg);
3679 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3680 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 3681
20749730
PZ
3682 POSTING_READ(reg);
3683 udelay(100);
6be4a607 3684 }
0e23b99d
JB
3685}
3686
88cefb6c
DV
3687static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3688{
3689 struct drm_device *dev = intel_crtc->base.dev;
3690 struct drm_i915_private *dev_priv = dev->dev_private;
3691 int pipe = intel_crtc->pipe;
f0f59a00
VS
3692 i915_reg_t reg;
3693 u32 temp;
88cefb6c
DV
3694
3695 /* Switch from PCDclk to Rawclk */
3696 reg = FDI_RX_CTL(pipe);
3697 temp = I915_READ(reg);
3698 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3699
3700 /* Disable CPU FDI TX PLL */
3701 reg = FDI_TX_CTL(pipe);
3702 temp = I915_READ(reg);
3703 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3704
3705 POSTING_READ(reg);
3706 udelay(100);
3707
3708 reg = FDI_RX_CTL(pipe);
3709 temp = I915_READ(reg);
3710 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3711
3712 /* Wait for the clocks to turn off. */
3713 POSTING_READ(reg);
3714 udelay(100);
3715}
3716
0fc932b8
JB
3717static void ironlake_fdi_disable(struct drm_crtc *crtc)
3718{
3719 struct drm_device *dev = crtc->dev;
3720 struct drm_i915_private *dev_priv = dev->dev_private;
3721 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3722 int pipe = intel_crtc->pipe;
f0f59a00
VS
3723 i915_reg_t reg;
3724 u32 temp;
0fc932b8
JB
3725
3726 /* disable CPU FDI tx and PCH FDI rx */
3727 reg = FDI_TX_CTL(pipe);
3728 temp = I915_READ(reg);
3729 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3730 POSTING_READ(reg);
3731
3732 reg = FDI_RX_CTL(pipe);
3733 temp = I915_READ(reg);
3734 temp &= ~(0x7 << 16);
dfd07d72 3735 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3736 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3737
3738 POSTING_READ(reg);
3739 udelay(100);
3740
3741 /* Ironlake workaround, disable clock pointer after downing FDI */
eba905b2 3742 if (HAS_PCH_IBX(dev))
6f06ce18 3743 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
0fc932b8
JB
3744
3745 /* still set train pattern 1 */
3746 reg = FDI_TX_CTL(pipe);
3747 temp = I915_READ(reg);
3748 temp &= ~FDI_LINK_TRAIN_NONE;
3749 temp |= FDI_LINK_TRAIN_PATTERN_1;
3750 I915_WRITE(reg, temp);
3751
3752 reg = FDI_RX_CTL(pipe);
3753 temp = I915_READ(reg);
3754 if (HAS_PCH_CPT(dev)) {
3755 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3756 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3757 } else {
3758 temp &= ~FDI_LINK_TRAIN_NONE;
3759 temp |= FDI_LINK_TRAIN_PATTERN_1;
3760 }
3761 /* BPC in FDI rx is consistent with that in PIPECONF */
3762 temp &= ~(0x07 << 16);
dfd07d72 3763 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3764 I915_WRITE(reg, temp);
3765
3766 POSTING_READ(reg);
3767 udelay(100);
3768}
3769
5dce5b93
CW
3770bool intel_has_pending_fb_unpin(struct drm_device *dev)
3771{
3772 struct intel_crtc *crtc;
3773
3774 /* Note that we don't need to be called with mode_config.lock here
3775 * as our list of CRTC objects is static for the lifetime of the
3776 * device and so cannot disappear as we iterate. Similarly, we can
3777 * happily treat the predicates as racy, atomic checks as userspace
3778 * cannot claim and pin a new fb without at least acquring the
3779 * struct_mutex and so serialising with us.
3780 */
d3fcc808 3781 for_each_intel_crtc(dev, crtc) {
5dce5b93
CW
3782 if (atomic_read(&crtc->unpin_work_count) == 0)
3783 continue;
3784
3785 if (crtc->unpin_work)
3786 intel_wait_for_vblank(dev, crtc->pipe);
3787
3788 return true;
3789 }
3790
3791 return false;
3792}
3793
d6bbafa1
CW
3794static void page_flip_completed(struct intel_crtc *intel_crtc)
3795{
3796 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3797 struct intel_unpin_work *work = intel_crtc->unpin_work;
3798
3799 /* ensure that the unpin work is consistent wrt ->pending. */
3800 smp_rmb();
3801 intel_crtc->unpin_work = NULL;
3802
3803 if (work->event)
3804 drm_send_vblank_event(intel_crtc->base.dev,
3805 intel_crtc->pipe,
3806 work->event);
3807
3808 drm_crtc_vblank_put(&intel_crtc->base);
3809
3810 wake_up_all(&dev_priv->pending_flip_queue);
3811 queue_work(dev_priv->wq, &work->work);
3812
3813 trace_i915_flip_complete(intel_crtc->plane,
3814 work->pending_flip_obj);
3815}
3816
5008e874 3817static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
e6c3a2a6 3818{
0f91128d 3819 struct drm_device *dev = crtc->dev;
5bb61643 3820 struct drm_i915_private *dev_priv = dev->dev_private;
5008e874 3821 long ret;
e6c3a2a6 3822
2c10d571 3823 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
5008e874
ML
3824
3825 ret = wait_event_interruptible_timeout(
3826 dev_priv->pending_flip_queue,
3827 !intel_crtc_has_pending_flip(crtc),
3828 60*HZ);
3829
3830 if (ret < 0)
3831 return ret;
3832
3833 if (ret == 0) {
9c787942 3834 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2c10d571 3835
5e2d7afc 3836 spin_lock_irq(&dev->event_lock);
9c787942
CW
3837 if (intel_crtc->unpin_work) {
3838 WARN_ONCE(1, "Removing stuck page flip\n");
3839 page_flip_completed(intel_crtc);
3840 }
5e2d7afc 3841 spin_unlock_irq(&dev->event_lock);
9c787942 3842 }
5bb61643 3843
5008e874 3844 return 0;
e6c3a2a6
CW
3845}
3846
060f02d8
VS
3847static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
3848{
3849 u32 temp;
3850
3851 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3852
3853 mutex_lock(&dev_priv->sb_lock);
3854
3855 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3856 temp |= SBI_SSCCTL_DISABLE;
3857 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
3858
3859 mutex_unlock(&dev_priv->sb_lock);
3860}
3861
e615efe4
ED
3862/* Program iCLKIP clock to the desired frequency */
3863static void lpt_program_iclkip(struct drm_crtc *crtc)
3864{
64b46a06 3865 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6e3c9717 3866 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
e615efe4
ED
3867 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3868 u32 temp;
3869
060f02d8 3870 lpt_disable_iclkip(dev_priv);
e615efe4 3871
64b46a06
VS
3872 /* The iCLK virtual clock root frequency is in MHz,
3873 * but the adjusted_mode->crtc_clock in in KHz. To get the
3874 * divisors, it is necessary to divide one by another, so we
3875 * convert the virtual clock precision to KHz here for higher
3876 * precision.
3877 */
3878 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
e615efe4
ED
3879 u32 iclk_virtual_root_freq = 172800 * 1000;
3880 u32 iclk_pi_range = 64;
64b46a06 3881 u32 desired_divisor;
e615efe4 3882
64b46a06
VS
3883 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3884 clock << auxdiv);
3885 divsel = (desired_divisor / iclk_pi_range) - 2;
3886 phaseinc = desired_divisor % iclk_pi_range;
e615efe4 3887
64b46a06
VS
3888 /*
3889 * Near 20MHz is a corner case which is
3890 * out of range for the 7-bit divisor
3891 */
3892 if (divsel <= 0x7f)
3893 break;
e615efe4
ED
3894 }
3895
3896 /* This should not happen with any sane values */
3897 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3898 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3899 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3900 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3901
3902 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 3903 clock,
e615efe4
ED
3904 auxdiv,
3905 divsel,
3906 phasedir,
3907 phaseinc);
3908
060f02d8
VS
3909 mutex_lock(&dev_priv->sb_lock);
3910
e615efe4 3911 /* Program SSCDIVINTPHASE6 */
988d6ee8 3912 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
3913 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3914 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3915 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3916 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3917 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3918 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 3919 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
3920
3921 /* Program SSCAUXDIV */
988d6ee8 3922 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
3923 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3924 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 3925 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
3926
3927 /* Enable modulator and associated divider */
988d6ee8 3928 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 3929 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 3930 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4 3931
060f02d8
VS
3932 mutex_unlock(&dev_priv->sb_lock);
3933
e615efe4
ED
3934 /* Wait for initialization time */
3935 udelay(24);
3936
3937 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
3938}
3939
8802e5b6
VS
3940int lpt_get_iclkip(struct drm_i915_private *dev_priv)
3941{
3942 u32 divsel, phaseinc, auxdiv;
3943 u32 iclk_virtual_root_freq = 172800 * 1000;
3944 u32 iclk_pi_range = 64;
3945 u32 desired_divisor;
3946 u32 temp;
3947
3948 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
3949 return 0;
3950
3951 mutex_lock(&dev_priv->sb_lock);
3952
3953 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3954 if (temp & SBI_SSCCTL_DISABLE) {
3955 mutex_unlock(&dev_priv->sb_lock);
3956 return 0;
3957 }
3958
3959 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
3960 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
3961 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
3962 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
3963 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
3964
3965 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
3966 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
3967 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
3968
3969 mutex_unlock(&dev_priv->sb_lock);
3970
3971 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
3972
3973 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3974 desired_divisor << auxdiv);
3975}
3976
275f01b2
DV
3977static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3978 enum pipe pch_transcoder)
3979{
3980 struct drm_device *dev = crtc->base.dev;
3981 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 3982 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
275f01b2
DV
3983
3984 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3985 I915_READ(HTOTAL(cpu_transcoder)));
3986 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3987 I915_READ(HBLANK(cpu_transcoder)));
3988 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3989 I915_READ(HSYNC(cpu_transcoder)));
3990
3991 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3992 I915_READ(VTOTAL(cpu_transcoder)));
3993 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3994 I915_READ(VBLANK(cpu_transcoder)));
3995 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3996 I915_READ(VSYNC(cpu_transcoder)));
3997 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3998 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3999}
4000
003632d9 4001static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
1fbc0d78
DV
4002{
4003 struct drm_i915_private *dev_priv = dev->dev_private;
4004 uint32_t temp;
4005
4006 temp = I915_READ(SOUTH_CHICKEN1);
003632d9 4007 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
1fbc0d78
DV
4008 return;
4009
4010 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4011 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4012
003632d9
ACO
4013 temp &= ~FDI_BC_BIFURCATION_SELECT;
4014 if (enable)
4015 temp |= FDI_BC_BIFURCATION_SELECT;
4016
4017 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
1fbc0d78
DV
4018 I915_WRITE(SOUTH_CHICKEN1, temp);
4019 POSTING_READ(SOUTH_CHICKEN1);
4020}
4021
4022static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4023{
4024 struct drm_device *dev = intel_crtc->base.dev;
1fbc0d78
DV
4025
4026 switch (intel_crtc->pipe) {
4027 case PIPE_A:
4028 break;
4029 case PIPE_B:
6e3c9717 4030 if (intel_crtc->config->fdi_lanes > 2)
003632d9 4031 cpt_set_fdi_bc_bifurcation(dev, false);
1fbc0d78 4032 else
003632d9 4033 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4034
4035 break;
4036 case PIPE_C:
003632d9 4037 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4038
4039 break;
4040 default:
4041 BUG();
4042 }
4043}
4044
c48b5305
VS
4045/* Return which DP Port should be selected for Transcoder DP control */
4046static enum port
4047intel_trans_dp_port_sel(struct drm_crtc *crtc)
4048{
4049 struct drm_device *dev = crtc->dev;
4050 struct intel_encoder *encoder;
4051
4052 for_each_encoder_on_crtc(dev, crtc, encoder) {
4053 if (encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
4054 encoder->type == INTEL_OUTPUT_EDP)
4055 return enc_to_dig_port(&encoder->base)->port;
4056 }
4057
4058 return -1;
4059}
4060
f67a559d
JB
4061/*
4062 * Enable PCH resources required for PCH ports:
4063 * - PCH PLLs
4064 * - FDI training & RX/TX
4065 * - update transcoder timings
4066 * - DP transcoding bits
4067 * - transcoder
4068 */
4069static void ironlake_pch_enable(struct drm_crtc *crtc)
0e23b99d
JB
4070{
4071 struct drm_device *dev = crtc->dev;
4072 struct drm_i915_private *dev_priv = dev->dev_private;
4073 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4074 int pipe = intel_crtc->pipe;
f0f59a00 4075 u32 temp;
2c07245f 4076
ab9412ba 4077 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 4078
1fbc0d78
DV
4079 if (IS_IVYBRIDGE(dev))
4080 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4081
cd986abb
DV
4082 /* Write the TU size bits before fdi link training, so that error
4083 * detection works. */
4084 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4085 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4086
3860b2ec
VS
4087 /*
4088 * Sometimes spurious CPU pipe underruns happen during FDI
4089 * training, at least with VGA+HDMI cloning. Suppress them.
4090 */
4091 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4092
c98e9dcf 4093 /* For PCH output, training FDI link */
674cf967 4094 dev_priv->display.fdi_link_train(crtc);
2c07245f 4095
3ad8a208
DV
4096 /* We need to program the right clock selection before writing the pixel
4097 * mutliplier into the DPLL. */
303b81e0 4098 if (HAS_PCH_CPT(dev)) {
ee7b9f93 4099 u32 sel;
4b645f14 4100
c98e9dcf 4101 temp = I915_READ(PCH_DPLL_SEL);
11887397
DV
4102 temp |= TRANS_DPLL_ENABLE(pipe);
4103 sel = TRANS_DPLLB_SEL(pipe);
8106ddbd
ACO
4104 if (intel_crtc->config->shared_dpll ==
4105 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
ee7b9f93
JB
4106 temp |= sel;
4107 else
4108 temp &= ~sel;
c98e9dcf 4109 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 4110 }
5eddb70b 4111
3ad8a208
DV
4112 /* XXX: pch pll's can be enabled any time before we enable the PCH
4113 * transcoder, and we actually should do this to not upset any PCH
4114 * transcoder that already use the clock when we share it.
4115 *
4116 * Note that enable_shared_dpll tries to do the right thing, but
4117 * get_shared_dpll unconditionally resets the pll - we need that to have
4118 * the right LVDS enable sequence. */
85b3894f 4119 intel_enable_shared_dpll(intel_crtc);
3ad8a208 4120
d9b6cb56
JB
4121 /* set transcoder timing, panel must allow it */
4122 assert_panel_unlocked(dev_priv, pipe);
275f01b2 4123 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
8db9d77b 4124
303b81e0 4125 intel_fdi_normal_train(crtc);
5e84e1a4 4126
3860b2ec
VS
4127 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4128
c98e9dcf 4129 /* For PCH DP, enable TRANS_DP_CTL */
6e3c9717 4130 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
9c4edaee
VS
4131 const struct drm_display_mode *adjusted_mode =
4132 &intel_crtc->config->base.adjusted_mode;
dfd07d72 4133 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
f0f59a00 4134 i915_reg_t reg = TRANS_DP_CTL(pipe);
5eddb70b
CW
4135 temp = I915_READ(reg);
4136 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
4137 TRANS_DP_SYNC_MASK |
4138 TRANS_DP_BPC_MASK);
e3ef4479 4139 temp |= TRANS_DP_OUTPUT_ENABLE;
9325c9f0 4140 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf 4141
9c4edaee 4142 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 4143 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
9c4edaee 4144 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 4145 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf
JB
4146
4147 switch (intel_trans_dp_port_sel(crtc)) {
c48b5305 4148 case PORT_B:
5eddb70b 4149 temp |= TRANS_DP_PORT_SEL_B;
c98e9dcf 4150 break;
c48b5305 4151 case PORT_C:
5eddb70b 4152 temp |= TRANS_DP_PORT_SEL_C;
c98e9dcf 4153 break;
c48b5305 4154 case PORT_D:
5eddb70b 4155 temp |= TRANS_DP_PORT_SEL_D;
c98e9dcf
JB
4156 break;
4157 default:
e95d41e1 4158 BUG();
32f9d658 4159 }
2c07245f 4160
5eddb70b 4161 I915_WRITE(reg, temp);
6be4a607 4162 }
b52eb4dc 4163
b8a4f404 4164 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
4165}
4166
1507e5bd
PZ
4167static void lpt_pch_enable(struct drm_crtc *crtc)
4168{
4169 struct drm_device *dev = crtc->dev;
4170 struct drm_i915_private *dev_priv = dev->dev_private;
4171 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 4172 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
1507e5bd 4173
ab9412ba 4174 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
1507e5bd 4175
8c52b5e8 4176 lpt_program_iclkip(crtc);
1507e5bd 4177
0540e488 4178 /* Set transcoder timing. */
275f01b2 4179 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
1507e5bd 4180
937bb610 4181 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
4182}
4183
a1520318 4184static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57
JB
4185{
4186 struct drm_i915_private *dev_priv = dev->dev_private;
f0f59a00 4187 i915_reg_t dslreg = PIPEDSL(pipe);
d4270e57
JB
4188 u32 temp;
4189
4190 temp = I915_READ(dslreg);
4191 udelay(500);
4192 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 4193 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 4194 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
4195 }
4196}
4197
86adf9d7
ML
4198static int
4199skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4200 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4201 int src_w, int src_h, int dst_w, int dst_h)
a1b2278e 4202{
86adf9d7
ML
4203 struct intel_crtc_scaler_state *scaler_state =
4204 &crtc_state->scaler_state;
4205 struct intel_crtc *intel_crtc =
4206 to_intel_crtc(crtc_state->base.crtc);
a1b2278e 4207 int need_scaling;
6156a456
CK
4208
4209 need_scaling = intel_rotation_90_or_270(rotation) ?
4210 (src_h != dst_w || src_w != dst_h):
4211 (src_w != dst_w || src_h != dst_h);
a1b2278e
CK
4212
4213 /*
4214 * if plane is being disabled or scaler is no more required or force detach
4215 * - free scaler binded to this plane/crtc
4216 * - in order to do this, update crtc->scaler_usage
4217 *
4218 * Here scaler state in crtc_state is set free so that
4219 * scaler can be assigned to other user. Actual register
4220 * update to free the scaler is done in plane/panel-fit programming.
4221 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4222 */
86adf9d7 4223 if (force_detach || !need_scaling) {
a1b2278e 4224 if (*scaler_id >= 0) {
86adf9d7 4225 scaler_state->scaler_users &= ~(1 << scaler_user);
a1b2278e
CK
4226 scaler_state->scalers[*scaler_id].in_use = 0;
4227
86adf9d7
ML
4228 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4229 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4230 intel_crtc->pipe, scaler_user, *scaler_id,
a1b2278e
CK
4231 scaler_state->scaler_users);
4232 *scaler_id = -1;
4233 }
4234 return 0;
4235 }
4236
4237 /* range checks */
4238 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4239 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4240
4241 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4242 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
86adf9d7 4243 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
a1b2278e 4244 "size is out of scaler range\n",
86adf9d7 4245 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
a1b2278e
CK
4246 return -EINVAL;
4247 }
4248
86adf9d7
ML
4249 /* mark this plane as a scaler user in crtc_state */
4250 scaler_state->scaler_users |= (1 << scaler_user);
4251 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4252 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4253 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4254 scaler_state->scaler_users);
4255
4256 return 0;
4257}
4258
4259/**
4260 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4261 *
4262 * @state: crtc's scaler state
86adf9d7
ML
4263 *
4264 * Return
4265 * 0 - scaler_usage updated successfully
4266 * error - requested scaling cannot be supported or other error condition
4267 */
e435d6e5 4268int skl_update_scaler_crtc(struct intel_crtc_state *state)
86adf9d7
ML
4269{
4270 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
7c5f93b0 4271 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
86adf9d7
ML
4272
4273 DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
4274 intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
4275
e435d6e5 4276 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
fa5a7970 4277 &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
86adf9d7 4278 state->pipe_src_w, state->pipe_src_h,
aad941d5 4279 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
86adf9d7
ML
4280}
4281
4282/**
4283 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4284 *
4285 * @state: crtc's scaler state
86adf9d7
ML
4286 * @plane_state: atomic plane state to update
4287 *
4288 * Return
4289 * 0 - scaler_usage updated successfully
4290 * error - requested scaling cannot be supported or other error condition
4291 */
da20eabd
ML
4292static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4293 struct intel_plane_state *plane_state)
86adf9d7
ML
4294{
4295
4296 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
da20eabd
ML
4297 struct intel_plane *intel_plane =
4298 to_intel_plane(plane_state->base.plane);
86adf9d7
ML
4299 struct drm_framebuffer *fb = plane_state->base.fb;
4300 int ret;
4301
4302 bool force_detach = !fb || !plane_state->visible;
4303
4304 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
4305 intel_plane->base.base.id, intel_crtc->pipe,
4306 drm_plane_index(&intel_plane->base));
4307
4308 ret = skl_update_scaler(crtc_state, force_detach,
4309 drm_plane_index(&intel_plane->base),
4310 &plane_state->scaler_id,
4311 plane_state->base.rotation,
4312 drm_rect_width(&plane_state->src) >> 16,
4313 drm_rect_height(&plane_state->src) >> 16,
4314 drm_rect_width(&plane_state->dst),
4315 drm_rect_height(&plane_state->dst));
4316
4317 if (ret || plane_state->scaler_id < 0)
4318 return ret;
4319
a1b2278e 4320 /* check colorkey */
818ed961 4321 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
86adf9d7 4322 DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
818ed961 4323 intel_plane->base.base.id);
a1b2278e
CK
4324 return -EINVAL;
4325 }
4326
4327 /* Check src format */
86adf9d7
ML
4328 switch (fb->pixel_format) {
4329 case DRM_FORMAT_RGB565:
4330 case DRM_FORMAT_XBGR8888:
4331 case DRM_FORMAT_XRGB8888:
4332 case DRM_FORMAT_ABGR8888:
4333 case DRM_FORMAT_ARGB8888:
4334 case DRM_FORMAT_XRGB2101010:
4335 case DRM_FORMAT_XBGR2101010:
4336 case DRM_FORMAT_YUYV:
4337 case DRM_FORMAT_YVYU:
4338 case DRM_FORMAT_UYVY:
4339 case DRM_FORMAT_VYUY:
4340 break;
4341 default:
4342 DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
4343 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4344 return -EINVAL;
a1b2278e
CK
4345 }
4346
a1b2278e
CK
4347 return 0;
4348}
4349
e435d6e5
ML
4350static void skylake_scaler_disable(struct intel_crtc *crtc)
4351{
4352 int i;
4353
4354 for (i = 0; i < crtc->num_scalers; i++)
4355 skl_detach_scaler(crtc, i);
4356}
4357
4358static void skylake_pfit_enable(struct intel_crtc *crtc)
bd2e244f
JB
4359{
4360 struct drm_device *dev = crtc->base.dev;
4361 struct drm_i915_private *dev_priv = dev->dev_private;
4362 int pipe = crtc->pipe;
a1b2278e
CK
4363 struct intel_crtc_scaler_state *scaler_state =
4364 &crtc->config->scaler_state;
4365
4366 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4367
6e3c9717 4368 if (crtc->config->pch_pfit.enabled) {
a1b2278e
CK
4369 int id;
4370
4371 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4372 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4373 return;
4374 }
4375
4376 id = scaler_state->scaler_id;
4377 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4378 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4379 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4380 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4381
4382 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
bd2e244f
JB
4383 }
4384}
4385
b074cec8
JB
4386static void ironlake_pfit_enable(struct intel_crtc *crtc)
4387{
4388 struct drm_device *dev = crtc->base.dev;
4389 struct drm_i915_private *dev_priv = dev->dev_private;
4390 int pipe = crtc->pipe;
4391
6e3c9717 4392 if (crtc->config->pch_pfit.enabled) {
b074cec8
JB
4393 /* Force use of hard-coded filter coefficients
4394 * as some pre-programmed values are broken,
4395 * e.g. x201.
4396 */
4397 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4398 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4399 PF_PIPE_SEL_IVB(pipe));
4400 else
4401 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
6e3c9717
ACO
4402 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4403 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
d4270e57
JB
4404 }
4405}
4406
20bc8673 4407void hsw_enable_ips(struct intel_crtc *crtc)
d77e4531 4408{
cea165c3
VS
4409 struct drm_device *dev = crtc->base.dev;
4410 struct drm_i915_private *dev_priv = dev->dev_private;
d77e4531 4411
6e3c9717 4412 if (!crtc->config->ips_enabled)
d77e4531
PZ
4413 return;
4414
307e4498
ML
4415 /*
4416 * We can only enable IPS after we enable a plane and wait for a vblank
4417 * This function is called from post_plane_update, which is run after
4418 * a vblank wait.
4419 */
cea165c3 4420
d77e4531 4421 assert_plane_enabled(dev_priv, crtc->plane);
cea165c3 4422 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4423 mutex_lock(&dev_priv->rps.hw_lock);
4424 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4425 mutex_unlock(&dev_priv->rps.hw_lock);
4426 /* Quoting Art Runyan: "its not safe to expect any particular
4427 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
4428 * mailbox." Moreover, the mailbox may return a bogus state,
4429 * so we need to just enable it and continue on.
2a114cc1
BW
4430 */
4431 } else {
4432 I915_WRITE(IPS_CTL, IPS_ENABLE);
4433 /* The bit only becomes 1 in the next vblank, so this wait here
4434 * is essentially intel_wait_for_vblank. If we don't have this
4435 * and don't wait for vblanks until the end of crtc_enable, then
4436 * the HW state readout code will complain that the expected
4437 * IPS_CTL value is not the one we read. */
4438 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4439 DRM_ERROR("Timed out waiting for IPS enable\n");
4440 }
d77e4531
PZ
4441}
4442
20bc8673 4443void hsw_disable_ips(struct intel_crtc *crtc)
d77e4531
PZ
4444{
4445 struct drm_device *dev = crtc->base.dev;
4446 struct drm_i915_private *dev_priv = dev->dev_private;
4447
6e3c9717 4448 if (!crtc->config->ips_enabled)
d77e4531
PZ
4449 return;
4450
4451 assert_plane_enabled(dev_priv, crtc->plane);
23d0b130 4452 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4453 mutex_lock(&dev_priv->rps.hw_lock);
4454 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4455 mutex_unlock(&dev_priv->rps.hw_lock);
23d0b130
BW
4456 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4457 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4458 DRM_ERROR("Timed out waiting for IPS disable\n");
e59150dc 4459 } else {
2a114cc1 4460 I915_WRITE(IPS_CTL, 0);
e59150dc
JB
4461 POSTING_READ(IPS_CTL);
4462 }
d77e4531
PZ
4463
4464 /* We need to wait for a vblank before we can disable the plane. */
4465 intel_wait_for_vblank(dev, crtc->pipe);
4466}
4467
7cac945f 4468static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
d3eedb1a 4469{
7cac945f 4470 if (intel_crtc->overlay) {
d3eedb1a
VS
4471 struct drm_device *dev = intel_crtc->base.dev;
4472 struct drm_i915_private *dev_priv = dev->dev_private;
4473
4474 mutex_lock(&dev->struct_mutex);
4475 dev_priv->mm.interruptible = false;
4476 (void) intel_overlay_switch_off(intel_crtc->overlay);
4477 dev_priv->mm.interruptible = true;
4478 mutex_unlock(&dev->struct_mutex);
4479 }
4480
4481 /* Let userspace switch the overlay on again. In most cases userspace
4482 * has to recompute where to put it anyway.
4483 */
4484}
4485
87d4300a
ML
4486/**
4487 * intel_post_enable_primary - Perform operations after enabling primary plane
4488 * @crtc: the CRTC whose primary plane was just enabled
4489 *
4490 * Performs potentially sleeping operations that must be done after the primary
4491 * plane is enabled, such as updating FBC and IPS. Note that this may be
4492 * called due to an explicit primary plane update, or due to an implicit
4493 * re-enable that is caused when a sprite plane is updated to no longer
4494 * completely hide the primary plane.
4495 */
4496static void
4497intel_post_enable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4498{
4499 struct drm_device *dev = crtc->dev;
87d4300a 4500 struct drm_i915_private *dev_priv = dev->dev_private;
a5c4d7bc
VS
4501 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4502 int pipe = intel_crtc->pipe;
a5c4d7bc 4503
87d4300a
ML
4504 /*
4505 * FIXME IPS should be fine as long as one plane is
4506 * enabled, but in practice it seems to have problems
4507 * when going from primary only to sprite only and vice
4508 * versa.
4509 */
a5c4d7bc
VS
4510 hsw_enable_ips(intel_crtc);
4511
f99d7069 4512 /*
87d4300a
ML
4513 * Gen2 reports pipe underruns whenever all planes are disabled.
4514 * So don't enable underrun reporting before at least some planes
4515 * are enabled.
4516 * FIXME: Need to fix the logic to work when we turn off all planes
4517 * but leave the pipe running.
f99d7069 4518 */
87d4300a
ML
4519 if (IS_GEN2(dev))
4520 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4521
aca7b684
VS
4522 /* Underruns don't always raise interrupts, so check manually. */
4523 intel_check_cpu_fifo_underruns(dev_priv);
4524 intel_check_pch_fifo_underruns(dev_priv);
a5c4d7bc
VS
4525}
4526
2622a081 4527/* FIXME move all this to pre_plane_update() with proper state tracking */
87d4300a
ML
4528static void
4529intel_pre_disable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4530{
4531 struct drm_device *dev = crtc->dev;
4532 struct drm_i915_private *dev_priv = dev->dev_private;
4533 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4534 int pipe = intel_crtc->pipe;
a5c4d7bc 4535
87d4300a
ML
4536 /*
4537 * Gen2 reports pipe underruns whenever all planes are disabled.
4538 * So diasble underrun reporting before all the planes get disabled.
4539 * FIXME: Need to fix the logic to work when we turn off all planes
4540 * but leave the pipe running.
4541 */
4542 if (IS_GEN2(dev))
4543 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
a5c4d7bc 4544
2622a081
VS
4545 /*
4546 * FIXME IPS should be fine as long as one plane is
4547 * enabled, but in practice it seems to have problems
4548 * when going from primary only to sprite only and vice
4549 * versa.
4550 */
4551 hsw_disable_ips(intel_crtc);
4552}
4553
4554/* FIXME get rid of this and use pre_plane_update */
4555static void
4556intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
4557{
4558 struct drm_device *dev = crtc->dev;
4559 struct drm_i915_private *dev_priv = dev->dev_private;
4560 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4561 int pipe = intel_crtc->pipe;
4562
4563 intel_pre_disable_primary(crtc);
4564
87d4300a
ML
4565 /*
4566 * Vblank time updates from the shadow to live plane control register
4567 * are blocked if the memory self-refresh mode is active at that
4568 * moment. So to make sure the plane gets truly disabled, disable
4569 * first the self-refresh mode. The self-refresh enable bit in turn
4570 * will be checked/applied by the HW only at the next frame start
4571 * event which is after the vblank start event, so we need to have a
4572 * wait-for-vblank between disabling the plane and the pipe.
4573 */
262cd2e1 4574 if (HAS_GMCH_DISPLAY(dev)) {
87d4300a 4575 intel_set_memory_cxsr(dev_priv, false);
262cd2e1
VS
4576 dev_priv->wm.vlv.cxsr = false;
4577 intel_wait_for_vblank(dev, pipe);
4578 }
87d4300a
ML
4579}
4580
cd202f69 4581static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
ac21b225 4582{
cd202f69
ML
4583 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
4584 struct drm_atomic_state *old_state = old_crtc_state->base.state;
92826fcd
ML
4585 struct intel_crtc_state *pipe_config =
4586 to_intel_crtc_state(crtc->base.state);
ac21b225 4587 struct drm_device *dev = crtc->base.dev;
cd202f69
ML
4588 struct drm_plane *primary = crtc->base.primary;
4589 struct drm_plane_state *old_pri_state =
4590 drm_atomic_get_existing_plane_state(old_state, primary);
ac21b225 4591
cd202f69 4592 intel_frontbuffer_flip(dev, pipe_config->fb_bits);
ac21b225 4593
ab1d3a0e 4594 crtc->wm.cxsr_allowed = true;
852eb00d 4595
caed361d 4596 if (pipe_config->update_wm_post && pipe_config->base.active)
f015c551
VS
4597 intel_update_watermarks(&crtc->base);
4598
cd202f69
ML
4599 if (old_pri_state) {
4600 struct intel_plane_state *primary_state =
4601 to_intel_plane_state(primary->state);
4602 struct intel_plane_state *old_primary_state =
4603 to_intel_plane_state(old_pri_state);
4604
31ae71fc
ML
4605 intel_fbc_post_update(crtc);
4606
cd202f69
ML
4607 if (primary_state->visible &&
4608 (needs_modeset(&pipe_config->base) ||
4609 !old_primary_state->visible))
4610 intel_post_enable_primary(&crtc->base);
4611 }
ac21b225
ML
4612}
4613
5c74cd73 4614static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
ac21b225 4615{
5c74cd73 4616 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
ac21b225 4617 struct drm_device *dev = crtc->base.dev;
eddfcbcd 4618 struct drm_i915_private *dev_priv = dev->dev_private;
ab1d3a0e
ML
4619 struct intel_crtc_state *pipe_config =
4620 to_intel_crtc_state(crtc->base.state);
5c74cd73
ML
4621 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4622 struct drm_plane *primary = crtc->base.primary;
4623 struct drm_plane_state *old_pri_state =
4624 drm_atomic_get_existing_plane_state(old_state, primary);
4625 bool modeset = needs_modeset(&pipe_config->base);
ac21b225 4626
5c74cd73
ML
4627 if (old_pri_state) {
4628 struct intel_plane_state *primary_state =
4629 to_intel_plane_state(primary->state);
4630 struct intel_plane_state *old_primary_state =
4631 to_intel_plane_state(old_pri_state);
4632
31ae71fc
ML
4633 intel_fbc_pre_update(crtc);
4634
5c74cd73
ML
4635 if (old_primary_state->visible &&
4636 (modeset || !primary_state->visible))
4637 intel_pre_disable_primary(&crtc->base);
4638 }
852eb00d 4639
ab1d3a0e 4640 if (pipe_config->disable_cxsr) {
852eb00d 4641 crtc->wm.cxsr_allowed = false;
2dfd178d 4642
2622a081
VS
4643 /*
4644 * Vblank time updates from the shadow to live plane control register
4645 * are blocked if the memory self-refresh mode is active at that
4646 * moment. So to make sure the plane gets truly disabled, disable
4647 * first the self-refresh mode. The self-refresh enable bit in turn
4648 * will be checked/applied by the HW only at the next frame start
4649 * event which is after the vblank start event, so we need to have a
4650 * wait-for-vblank between disabling the plane and the pipe.
4651 */
4652 if (old_crtc_state->base.active) {
2dfd178d 4653 intel_set_memory_cxsr(dev_priv, false);
2622a081
VS
4654 dev_priv->wm.vlv.cxsr = false;
4655 intel_wait_for_vblank(dev, crtc->pipe);
4656 }
852eb00d 4657 }
92826fcd 4658
ed4a6a7c
MR
4659 /*
4660 * IVB workaround: must disable low power watermarks for at least
4661 * one frame before enabling scaling. LP watermarks can be re-enabled
4662 * when scaling is disabled.
4663 *
4664 * WaCxSRDisabledForSpriteScaling:ivb
4665 */
4666 if (pipe_config->disable_lp_wm) {
4667 ilk_disable_lp_wm(dev);
4668 intel_wait_for_vblank(dev, crtc->pipe);
4669 }
4670
4671 /*
4672 * If we're doing a modeset, we're done. No need to do any pre-vblank
4673 * watermark programming here.
4674 */
4675 if (needs_modeset(&pipe_config->base))
4676 return;
4677
4678 /*
4679 * For platforms that support atomic watermarks, program the
4680 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
4681 * will be the intermediate values that are safe for both pre- and
4682 * post- vblank; when vblank happens, the 'active' values will be set
4683 * to the final 'target' values and we'll do this again to get the
4684 * optimal watermarks. For gen9+ platforms, the values we program here
4685 * will be the final target values which will get automatically latched
4686 * at vblank time; no further programming will be necessary.
4687 *
4688 * If a platform hasn't been transitioned to atomic watermarks yet,
4689 * we'll continue to update watermarks the old way, if flags tell
4690 * us to.
4691 */
4692 if (dev_priv->display.initial_watermarks != NULL)
4693 dev_priv->display.initial_watermarks(pipe_config);
caed361d 4694 else if (pipe_config->update_wm_pre)
92826fcd 4695 intel_update_watermarks(&crtc->base);
ac21b225
ML
4696}
4697
d032ffa0 4698static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
87d4300a
ML
4699{
4700 struct drm_device *dev = crtc->dev;
4701 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
d032ffa0 4702 struct drm_plane *p;
87d4300a
ML
4703 int pipe = intel_crtc->pipe;
4704
7cac945f 4705 intel_crtc_dpms_overlay_disable(intel_crtc);
27321ae8 4706
d032ffa0
ML
4707 drm_for_each_plane_mask(p, dev, plane_mask)
4708 to_intel_plane(p)->disable_plane(p, crtc);
f98551ae 4709
f99d7069
DV
4710 /*
4711 * FIXME: Once we grow proper nuclear flip support out of this we need
4712 * to compute the mask of flip planes precisely. For the time being
4713 * consider this a flip to a NULL plane.
4714 */
4715 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
4716}
4717
f67a559d
JB
4718static void ironlake_crtc_enable(struct drm_crtc *crtc)
4719{
4720 struct drm_device *dev = crtc->dev;
4721 struct drm_i915_private *dev_priv = dev->dev_private;
4722 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4723 struct intel_encoder *encoder;
f67a559d 4724 int pipe = intel_crtc->pipe;
f67a559d 4725
53d9f4e9 4726 if (WARN_ON(intel_crtc->active))
f67a559d
JB
4727 return;
4728
81b088ca
VS
4729 if (intel_crtc->config->has_pch_encoder)
4730 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4731
6e3c9717 4732 if (intel_crtc->config->has_pch_encoder)
b14b1055
DV
4733 intel_prepare_shared_dpll(intel_crtc);
4734
6e3c9717 4735 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 4736 intel_dp_set_m_n(intel_crtc, M1_N1);
29407aab
DV
4737
4738 intel_set_pipe_timings(intel_crtc);
bc58be60 4739 intel_set_pipe_src_size(intel_crtc);
29407aab 4740
6e3c9717 4741 if (intel_crtc->config->has_pch_encoder) {
29407aab 4742 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4743 &intel_crtc->config->fdi_m_n, NULL);
29407aab
DV
4744 }
4745
4746 ironlake_set_pipeconf(crtc);
4747
f67a559d 4748 intel_crtc->active = true;
8664281b 4749
a72e4c9f 4750 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
8664281b 4751
f6736a1a 4752 for_each_encoder_on_crtc(dev, crtc, encoder)
952735ee
DV
4753 if (encoder->pre_enable)
4754 encoder->pre_enable(encoder);
f67a559d 4755
6e3c9717 4756 if (intel_crtc->config->has_pch_encoder) {
fff367c7
DV
4757 /* Note: FDI PLL enabling _must_ be done before we enable the
4758 * cpu pipes, hence this is separate from all the other fdi/pch
4759 * enabling. */
88cefb6c 4760 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
DV
4761 } else {
4762 assert_fdi_tx_disabled(dev_priv, pipe);
4763 assert_fdi_rx_disabled(dev_priv, pipe);
4764 }
f67a559d 4765
b074cec8 4766 ironlake_pfit_enable(intel_crtc);
f67a559d 4767
9c54c0dd
JB
4768 /*
4769 * On ILK+ LUT must be loaded before the pipe is running but with
4770 * clocks enabled
4771 */
8563b1e8 4772 intel_color_load_luts(crtc);
9c54c0dd 4773
1d5bf5d9
ID
4774 if (dev_priv->display.initial_watermarks != NULL)
4775 dev_priv->display.initial_watermarks(intel_crtc->config);
e1fdc473 4776 intel_enable_pipe(intel_crtc);
f67a559d 4777
6e3c9717 4778 if (intel_crtc->config->has_pch_encoder)
f67a559d 4779 ironlake_pch_enable(crtc);
c98e9dcf 4780
f9b61ff6
DV
4781 assert_vblank_disabled(crtc);
4782 drm_crtc_vblank_on(crtc);
4783
fa5c73b1
DV
4784 for_each_encoder_on_crtc(dev, crtc, encoder)
4785 encoder->enable(encoder);
61b77ddd
DV
4786
4787 if (HAS_PCH_CPT(dev))
a1520318 4788 cpt_verify_modeset(dev, intel_crtc->pipe);
37ca8d4c
VS
4789
4790 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
4791 if (intel_crtc->config->has_pch_encoder)
4792 intel_wait_for_vblank(dev, pipe);
4793 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607
JB
4794}
4795
42db64ef
PZ
4796/* IPS only exists on ULT machines and is tied to pipe A. */
4797static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4798{
f5adf94e 4799 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
42db64ef
PZ
4800}
4801
4f771f10
PZ
4802static void haswell_crtc_enable(struct drm_crtc *crtc)
4803{
4804 struct drm_device *dev = crtc->dev;
4805 struct drm_i915_private *dev_priv = dev->dev_private;
4806 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4807 struct intel_encoder *encoder;
99d736a2 4808 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
4d1de975 4809 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
99d736a2
ML
4810 struct intel_crtc_state *pipe_config =
4811 to_intel_crtc_state(crtc->state);
4f771f10 4812
53d9f4e9 4813 if (WARN_ON(intel_crtc->active))
4f771f10
PZ
4814 return;
4815
81b088ca
VS
4816 if (intel_crtc->config->has_pch_encoder)
4817 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4818 false);
4819
8106ddbd 4820 if (intel_crtc->config->shared_dpll)
df8ad70c
DV
4821 intel_enable_shared_dpll(intel_crtc);
4822
6e3c9717 4823 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 4824 intel_dp_set_m_n(intel_crtc, M1_N1);
229fca97 4825
4d1de975
JN
4826 if (!intel_crtc->config->has_dsi_encoder)
4827 intel_set_pipe_timings(intel_crtc);
4828
bc58be60 4829 intel_set_pipe_src_size(intel_crtc);
229fca97 4830
4d1de975
JN
4831 if (cpu_transcoder != TRANSCODER_EDP &&
4832 !transcoder_is_dsi(cpu_transcoder)) {
4833 I915_WRITE(PIPE_MULT(cpu_transcoder),
6e3c9717 4834 intel_crtc->config->pixel_multiplier - 1);
ebb69c95
CT
4835 }
4836
6e3c9717 4837 if (intel_crtc->config->has_pch_encoder) {
229fca97 4838 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4839 &intel_crtc->config->fdi_m_n, NULL);
229fca97
DV
4840 }
4841
4d1de975
JN
4842 if (!intel_crtc->config->has_dsi_encoder)
4843 haswell_set_pipeconf(crtc);
4844
391bf048 4845 haswell_set_pipemisc(crtc);
229fca97 4846
8563b1e8 4847 intel_color_set_csc(crtc);
229fca97 4848
4f771f10 4849 intel_crtc->active = true;
8664281b 4850
6b698516
DV
4851 if (intel_crtc->config->has_pch_encoder)
4852 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4853 else
4854 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4855
7d4aefd0 4856 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10
PZ
4857 if (encoder->pre_enable)
4858 encoder->pre_enable(encoder);
7d4aefd0 4859 }
4f771f10 4860
d2d65408 4861 if (intel_crtc->config->has_pch_encoder)
4fe9467d 4862 dev_priv->display.fdi_link_train(crtc);
4fe9467d 4863
a65347ba 4864 if (!intel_crtc->config->has_dsi_encoder)
7d4aefd0 4865 intel_ddi_enable_pipe_clock(intel_crtc);
4f771f10 4866
1c132b44 4867 if (INTEL_INFO(dev)->gen >= 9)
e435d6e5 4868 skylake_pfit_enable(intel_crtc);
ff6d9f55 4869 else
1c132b44 4870 ironlake_pfit_enable(intel_crtc);
4f771f10
PZ
4871
4872 /*
4873 * On ILK+ LUT must be loaded before the pipe is running but with
4874 * clocks enabled
4875 */
8563b1e8 4876 intel_color_load_luts(crtc);
4f771f10 4877
1f544388 4878 intel_ddi_set_pipe_settings(crtc);
a65347ba 4879 if (!intel_crtc->config->has_dsi_encoder)
7d4aefd0 4880 intel_ddi_enable_transcoder_func(crtc);
4f771f10 4881
1d5bf5d9
ID
4882 if (dev_priv->display.initial_watermarks != NULL)
4883 dev_priv->display.initial_watermarks(pipe_config);
4884 else
4885 intel_update_watermarks(crtc);
4d1de975
JN
4886
4887 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
4888 if (!intel_crtc->config->has_dsi_encoder)
4889 intel_enable_pipe(intel_crtc);
42db64ef 4890
6e3c9717 4891 if (intel_crtc->config->has_pch_encoder)
1507e5bd 4892 lpt_pch_enable(crtc);
4f771f10 4893
a65347ba 4894 if (intel_crtc->config->dp_encoder_is_mst)
0e32b39c
DA
4895 intel_ddi_set_vc_payload_alloc(crtc, true);
4896
f9b61ff6
DV
4897 assert_vblank_disabled(crtc);
4898 drm_crtc_vblank_on(crtc);
4899
8807e55b 4900 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10 4901 encoder->enable(encoder);
8807e55b
JN
4902 intel_opregion_notify_encoder(encoder, true);
4903 }
4f771f10 4904
6b698516
DV
4905 if (intel_crtc->config->has_pch_encoder) {
4906 intel_wait_for_vblank(dev, pipe);
4907 intel_wait_for_vblank(dev, pipe);
4908 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
d2d65408
VS
4909 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4910 true);
6b698516 4911 }
d2d65408 4912
e4916946
PZ
4913 /* If we change the relative order between pipe/planes enabling, we need
4914 * to change the workaround. */
99d736a2
ML
4915 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
4916 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
4917 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4918 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4919 }
4f771f10
PZ
4920}
4921
bfd16b2a 4922static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
3f8dce3a
DV
4923{
4924 struct drm_device *dev = crtc->base.dev;
4925 struct drm_i915_private *dev_priv = dev->dev_private;
4926 int pipe = crtc->pipe;
4927
4928 /* To avoid upsetting the power well on haswell only disable the pfit if
4929 * it's in use. The hw state code will make sure we get this right. */
bfd16b2a 4930 if (force || crtc->config->pch_pfit.enabled) {
3f8dce3a
DV
4931 I915_WRITE(PF_CTL(pipe), 0);
4932 I915_WRITE(PF_WIN_POS(pipe), 0);
4933 I915_WRITE(PF_WIN_SZ(pipe), 0);
4934 }
4935}
4936
6be4a607
JB
4937static void ironlake_crtc_disable(struct drm_crtc *crtc)
4938{
4939 struct drm_device *dev = crtc->dev;
4940 struct drm_i915_private *dev_priv = dev->dev_private;
4941 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4942 struct intel_encoder *encoder;
6be4a607 4943 int pipe = intel_crtc->pipe;
b52eb4dc 4944
37ca8d4c
VS
4945 if (intel_crtc->config->has_pch_encoder)
4946 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4947
ea9d758d
DV
4948 for_each_encoder_on_crtc(dev, crtc, encoder)
4949 encoder->disable(encoder);
4950
f9b61ff6
DV
4951 drm_crtc_vblank_off(crtc);
4952 assert_vblank_disabled(crtc);
4953
3860b2ec
VS
4954 /*
4955 * Sometimes spurious CPU pipe underruns happen when the
4956 * pipe is already disabled, but FDI RX/TX is still enabled.
4957 * Happens at least with VGA+HDMI cloning. Suppress them.
4958 */
4959 if (intel_crtc->config->has_pch_encoder)
4960 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4961
575f7ab7 4962 intel_disable_pipe(intel_crtc);
32f9d658 4963
bfd16b2a 4964 ironlake_pfit_disable(intel_crtc, false);
2c07245f 4965
3860b2ec 4966 if (intel_crtc->config->has_pch_encoder) {
5a74f70a 4967 ironlake_fdi_disable(crtc);
3860b2ec
VS
4968 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4969 }
5a74f70a 4970
bf49ec8c
DV
4971 for_each_encoder_on_crtc(dev, crtc, encoder)
4972 if (encoder->post_disable)
4973 encoder->post_disable(encoder);
2c07245f 4974
6e3c9717 4975 if (intel_crtc->config->has_pch_encoder) {
d925c59a 4976 ironlake_disable_pch_transcoder(dev_priv, pipe);
6be4a607 4977
d925c59a 4978 if (HAS_PCH_CPT(dev)) {
f0f59a00
VS
4979 i915_reg_t reg;
4980 u32 temp;
4981
d925c59a
DV
4982 /* disable TRANS_DP_CTL */
4983 reg = TRANS_DP_CTL(pipe);
4984 temp = I915_READ(reg);
4985 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
4986 TRANS_DP_PORT_SEL_MASK);
4987 temp |= TRANS_DP_PORT_SEL_NONE;
4988 I915_WRITE(reg, temp);
4989
4990 /* disable DPLL_SEL */
4991 temp = I915_READ(PCH_DPLL_SEL);
11887397 4992 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 4993 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 4994 }
e3421a18 4995
d925c59a
DV
4996 ironlake_fdi_pll_disable(intel_crtc);
4997 }
81b088ca
VS
4998
4999 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607 5000}
1b3c7a47 5001
4f771f10 5002static void haswell_crtc_disable(struct drm_crtc *crtc)
ee7b9f93 5003{
4f771f10
PZ
5004 struct drm_device *dev = crtc->dev;
5005 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93 5006 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4f771f10 5007 struct intel_encoder *encoder;
6e3c9717 5008 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee7b9f93 5009
d2d65408
VS
5010 if (intel_crtc->config->has_pch_encoder)
5011 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5012 false);
5013
8807e55b
JN
5014 for_each_encoder_on_crtc(dev, crtc, encoder) {
5015 intel_opregion_notify_encoder(encoder, false);
4f771f10 5016 encoder->disable(encoder);
8807e55b 5017 }
4f771f10 5018
f9b61ff6
DV
5019 drm_crtc_vblank_off(crtc);
5020 assert_vblank_disabled(crtc);
5021
4d1de975
JN
5022 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
5023 if (!intel_crtc->config->has_dsi_encoder)
5024 intel_disable_pipe(intel_crtc);
4f771f10 5025
6e3c9717 5026 if (intel_crtc->config->dp_encoder_is_mst)
a4bf214f
VS
5027 intel_ddi_set_vc_payload_alloc(crtc, false);
5028
a65347ba 5029 if (!intel_crtc->config->has_dsi_encoder)
7d4aefd0 5030 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4f771f10 5031
1c132b44 5032 if (INTEL_INFO(dev)->gen >= 9)
e435d6e5 5033 skylake_scaler_disable(intel_crtc);
ff6d9f55 5034 else
bfd16b2a 5035 ironlake_pfit_disable(intel_crtc, false);
4f771f10 5036
a65347ba 5037 if (!intel_crtc->config->has_dsi_encoder)
7d4aefd0 5038 intel_ddi_disable_pipe_clock(intel_crtc);
4f771f10 5039
97b040aa
ID
5040 for_each_encoder_on_crtc(dev, crtc, encoder)
5041 if (encoder->post_disable)
5042 encoder->post_disable(encoder);
81b088ca 5043
92966a37
VS
5044 if (intel_crtc->config->has_pch_encoder) {
5045 lpt_disable_pch_transcoder(dev_priv);
503a74e9 5046 lpt_disable_iclkip(dev_priv);
92966a37
VS
5047 intel_ddi_fdi_disable(crtc);
5048
81b088ca
VS
5049 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5050 true);
92966a37 5051 }
4f771f10
PZ
5052}
5053
2dd24552
JB
5054static void i9xx_pfit_enable(struct intel_crtc *crtc)
5055{
5056 struct drm_device *dev = crtc->base.dev;
5057 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 5058 struct intel_crtc_state *pipe_config = crtc->config;
2dd24552 5059
681a8504 5060 if (!pipe_config->gmch_pfit.control)
2dd24552
JB
5061 return;
5062
2dd24552 5063 /*
c0b03411
DV
5064 * The panel fitter should only be adjusted whilst the pipe is disabled,
5065 * according to register description and PRM.
2dd24552 5066 */
c0b03411
DV
5067 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5068 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 5069
b074cec8
JB
5070 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5071 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
DV
5072
5073 /* Border color in case we don't scale up to the full screen. Black by
5074 * default, change to something else for debugging. */
5075 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
5076}
5077
d05410f9
DA
5078static enum intel_display_power_domain port_to_power_domain(enum port port)
5079{
5080 switch (port) {
5081 case PORT_A:
6331a704 5082 return POWER_DOMAIN_PORT_DDI_A_LANES;
d05410f9 5083 case PORT_B:
6331a704 5084 return POWER_DOMAIN_PORT_DDI_B_LANES;
d05410f9 5085 case PORT_C:
6331a704 5086 return POWER_DOMAIN_PORT_DDI_C_LANES;
d05410f9 5087 case PORT_D:
6331a704 5088 return POWER_DOMAIN_PORT_DDI_D_LANES;
d8e19f99 5089 case PORT_E:
6331a704 5090 return POWER_DOMAIN_PORT_DDI_E_LANES;
d05410f9 5091 default:
b9fec167 5092 MISSING_CASE(port);
d05410f9
DA
5093 return POWER_DOMAIN_PORT_OTHER;
5094 }
5095}
5096
25f78f58
VS
5097static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5098{
5099 switch (port) {
5100 case PORT_A:
5101 return POWER_DOMAIN_AUX_A;
5102 case PORT_B:
5103 return POWER_DOMAIN_AUX_B;
5104 case PORT_C:
5105 return POWER_DOMAIN_AUX_C;
5106 case PORT_D:
5107 return POWER_DOMAIN_AUX_D;
5108 case PORT_E:
5109 /* FIXME: Check VBT for actual wiring of PORT E */
5110 return POWER_DOMAIN_AUX_D;
5111 default:
b9fec167 5112 MISSING_CASE(port);
25f78f58
VS
5113 return POWER_DOMAIN_AUX_A;
5114 }
5115}
5116
319be8ae
ID
5117enum intel_display_power_domain
5118intel_display_port_power_domain(struct intel_encoder *intel_encoder)
5119{
5120 struct drm_device *dev = intel_encoder->base.dev;
5121 struct intel_digital_port *intel_dig_port;
5122
5123 switch (intel_encoder->type) {
5124 case INTEL_OUTPUT_UNKNOWN:
5125 /* Only DDI platforms should ever use this output type */
5126 WARN_ON_ONCE(!HAS_DDI(dev));
5127 case INTEL_OUTPUT_DISPLAYPORT:
5128 case INTEL_OUTPUT_HDMI:
5129 case INTEL_OUTPUT_EDP:
5130 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
d05410f9 5131 return port_to_power_domain(intel_dig_port->port);
0e32b39c
DA
5132 case INTEL_OUTPUT_DP_MST:
5133 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5134 return port_to_power_domain(intel_dig_port->port);
319be8ae
ID
5135 case INTEL_OUTPUT_ANALOG:
5136 return POWER_DOMAIN_PORT_CRT;
5137 case INTEL_OUTPUT_DSI:
5138 return POWER_DOMAIN_PORT_DSI;
5139 default:
5140 return POWER_DOMAIN_PORT_OTHER;
5141 }
5142}
5143
25f78f58
VS
5144enum intel_display_power_domain
5145intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5146{
5147 struct drm_device *dev = intel_encoder->base.dev;
5148 struct intel_digital_port *intel_dig_port;
5149
5150 switch (intel_encoder->type) {
5151 case INTEL_OUTPUT_UNKNOWN:
651174a4
ID
5152 case INTEL_OUTPUT_HDMI:
5153 /*
5154 * Only DDI platforms should ever use these output types.
5155 * We can get here after the HDMI detect code has already set
5156 * the type of the shared encoder. Since we can't be sure
5157 * what's the status of the given connectors, play safe and
5158 * run the DP detection too.
5159 */
25f78f58
VS
5160 WARN_ON_ONCE(!HAS_DDI(dev));
5161 case INTEL_OUTPUT_DISPLAYPORT:
5162 case INTEL_OUTPUT_EDP:
5163 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5164 return port_to_aux_power_domain(intel_dig_port->port);
5165 case INTEL_OUTPUT_DP_MST:
5166 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5167 return port_to_aux_power_domain(intel_dig_port->port);
5168 default:
b9fec167 5169 MISSING_CASE(intel_encoder->type);
25f78f58
VS
5170 return POWER_DOMAIN_AUX_A;
5171 }
5172}
5173
74bff5f9
ML
5174static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
5175 struct intel_crtc_state *crtc_state)
77d22dca 5176{
319be8ae 5177 struct drm_device *dev = crtc->dev;
74bff5f9 5178 struct drm_encoder *encoder;
319be8ae
ID
5179 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5180 enum pipe pipe = intel_crtc->pipe;
77d22dca 5181 unsigned long mask;
74bff5f9 5182 enum transcoder transcoder = crtc_state->cpu_transcoder;
77d22dca 5183
74bff5f9 5184 if (!crtc_state->base.active)
292b990e
ML
5185 return 0;
5186
77d22dca
ID
5187 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5188 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
74bff5f9
ML
5189 if (crtc_state->pch_pfit.enabled ||
5190 crtc_state->pch_pfit.force_thru)
77d22dca
ID
5191 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5192
74bff5f9
ML
5193 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5194 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5195
319be8ae 5196 mask |= BIT(intel_display_port_power_domain(intel_encoder));
74bff5f9 5197 }
319be8ae 5198
15e7ec29
ML
5199 if (crtc_state->shared_dpll)
5200 mask |= BIT(POWER_DOMAIN_PLLS);
5201
77d22dca
ID
5202 return mask;
5203}
5204
74bff5f9
ML
5205static unsigned long
5206modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5207 struct intel_crtc_state *crtc_state)
77d22dca 5208{
292b990e
ML
5209 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5210 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5211 enum intel_display_power_domain domain;
5212 unsigned long domains, new_domains, old_domains;
77d22dca 5213
292b990e 5214 old_domains = intel_crtc->enabled_power_domains;
74bff5f9
ML
5215 intel_crtc->enabled_power_domains = new_domains =
5216 get_crtc_power_domains(crtc, crtc_state);
77d22dca 5217
292b990e
ML
5218 domains = new_domains & ~old_domains;
5219
5220 for_each_power_domain(domain, domains)
5221 intel_display_power_get(dev_priv, domain);
5222
5223 return old_domains & ~new_domains;
5224}
5225
5226static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5227 unsigned long domains)
5228{
5229 enum intel_display_power_domain domain;
5230
5231 for_each_power_domain(domain, domains)
5232 intel_display_power_put(dev_priv, domain);
5233}
77d22dca 5234
adafdc6f
MK
5235static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5236{
5237 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5238
5239 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5240 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5241 return max_cdclk_freq;
5242 else if (IS_CHERRYVIEW(dev_priv))
5243 return max_cdclk_freq*95/100;
5244 else if (INTEL_INFO(dev_priv)->gen < 4)
5245 return 2*max_cdclk_freq*90/100;
5246 else
5247 return max_cdclk_freq*90/100;
5248}
5249
560a7ae4
DL
5250static void intel_update_max_cdclk(struct drm_device *dev)
5251{
5252 struct drm_i915_private *dev_priv = dev->dev_private;
5253
ef11bdb3 5254 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
560a7ae4
DL
5255 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
5256
5257 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
5258 dev_priv->max_cdclk_freq = 675000;
5259 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
5260 dev_priv->max_cdclk_freq = 540000;
5261 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
5262 dev_priv->max_cdclk_freq = 450000;
5263 else
5264 dev_priv->max_cdclk_freq = 337500;
5265 } else if (IS_BROADWELL(dev)) {
5266 /*
5267 * FIXME with extra cooling we can allow
5268 * 540 MHz for ULX and 675 Mhz for ULT.
5269 * How can we know if extra cooling is
5270 * available? PCI ID, VTB, something else?
5271 */
5272 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5273 dev_priv->max_cdclk_freq = 450000;
5274 else if (IS_BDW_ULX(dev))
5275 dev_priv->max_cdclk_freq = 450000;
5276 else if (IS_BDW_ULT(dev))
5277 dev_priv->max_cdclk_freq = 540000;
5278 else
5279 dev_priv->max_cdclk_freq = 675000;
0904deaf
MK
5280 } else if (IS_CHERRYVIEW(dev)) {
5281 dev_priv->max_cdclk_freq = 320000;
560a7ae4
DL
5282 } else if (IS_VALLEYVIEW(dev)) {
5283 dev_priv->max_cdclk_freq = 400000;
5284 } else {
5285 /* otherwise assume cdclk is fixed */
5286 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5287 }
5288
adafdc6f
MK
5289 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5290
560a7ae4
DL
5291 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5292 dev_priv->max_cdclk_freq);
adafdc6f
MK
5293
5294 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5295 dev_priv->max_dotclk_freq);
560a7ae4
DL
5296}
5297
5298static void intel_update_cdclk(struct drm_device *dev)
5299{
5300 struct drm_i915_private *dev_priv = dev->dev_private;
5301
5302 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
5303 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5304 dev_priv->cdclk_freq);
5305
5306 /*
5307 * Program the gmbus_freq based on the cdclk frequency.
5308 * BSpec erroneously claims we should aim for 4MHz, but
5309 * in fact 1MHz is the correct frequency.
5310 */
666a4537 5311 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
560a7ae4
DL
5312 /*
5313 * Program the gmbus_freq based on the cdclk frequency.
5314 * BSpec erroneously claims we should aim for 4MHz, but
5315 * in fact 1MHz is the correct frequency.
5316 */
5317 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
5318 }
5319
5320 if (dev_priv->max_cdclk_freq == 0)
5321 intel_update_max_cdclk(dev);
5322}
5323
70d0c574 5324static void broxton_set_cdclk(struct drm_device *dev, int frequency)
f8437dd1
VK
5325{
5326 struct drm_i915_private *dev_priv = dev->dev_private;
5327 uint32_t divider;
5328 uint32_t ratio;
5329 uint32_t current_freq;
5330 int ret;
5331
5332 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5333 switch (frequency) {
5334 case 144000:
5335 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5336 ratio = BXT_DE_PLL_RATIO(60);
5337 break;
5338 case 288000:
5339 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5340 ratio = BXT_DE_PLL_RATIO(60);
5341 break;
5342 case 384000:
5343 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5344 ratio = BXT_DE_PLL_RATIO(60);
5345 break;
5346 case 576000:
5347 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5348 ratio = BXT_DE_PLL_RATIO(60);
5349 break;
5350 case 624000:
5351 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5352 ratio = BXT_DE_PLL_RATIO(65);
5353 break;
5354 case 19200:
5355 /*
5356 * Bypass frequency with DE PLL disabled. Init ratio, divider
5357 * to suppress GCC warning.
5358 */
5359 ratio = 0;
5360 divider = 0;
5361 break;
5362 default:
5363 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5364
5365 return;
5366 }
5367
5368 mutex_lock(&dev_priv->rps.hw_lock);
5369 /* Inform power controller of upcoming frequency change */
5370 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5371 0x80000000);
5372 mutex_unlock(&dev_priv->rps.hw_lock);
5373
5374 if (ret) {
5375 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5376 ret, frequency);
5377 return;
5378 }
5379
5380 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5381 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5382 current_freq = current_freq * 500 + 1000;
5383
5384 /*
5385 * DE PLL has to be disabled when
5386 * - setting to 19.2MHz (bypass, PLL isn't used)
5387 * - before setting to 624MHz (PLL needs toggling)
5388 * - before setting to any frequency from 624MHz (PLL needs toggling)
5389 */
5390 if (frequency == 19200 || frequency == 624000 ||
5391 current_freq == 624000) {
5392 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5393 /* Timeout 200us */
5394 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5395 1))
5396 DRM_ERROR("timout waiting for DE PLL unlock\n");
5397 }
5398
5399 if (frequency != 19200) {
5400 uint32_t val;
5401
5402 val = I915_READ(BXT_DE_PLL_CTL);
5403 val &= ~BXT_DE_PLL_RATIO_MASK;
5404 val |= ratio;
5405 I915_WRITE(BXT_DE_PLL_CTL, val);
5406
5407 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5408 /* Timeout 200us */
5409 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5410 DRM_ERROR("timeout waiting for DE PLL lock\n");
5411
5412 val = I915_READ(CDCLK_CTL);
5413 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5414 val |= divider;
5415 /*
5416 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5417 * enable otherwise.
5418 */
5419 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5420 if (frequency >= 500000)
5421 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5422
5423 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5424 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5425 val |= (frequency - 1000) / 500;
5426 I915_WRITE(CDCLK_CTL, val);
5427 }
5428
5429 mutex_lock(&dev_priv->rps.hw_lock);
5430 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5431 DIV_ROUND_UP(frequency, 25000));
5432 mutex_unlock(&dev_priv->rps.hw_lock);
5433
5434 if (ret) {
5435 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5436 ret, frequency);
5437 return;
5438 }
5439
a47871bd 5440 intel_update_cdclk(dev);
f8437dd1
VK
5441}
5442
5443void broxton_init_cdclk(struct drm_device *dev)
5444{
5445 struct drm_i915_private *dev_priv = dev->dev_private;
5446 uint32_t val;
5447
5448 /*
5449 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5450 * or else the reset will hang because there is no PCH to respond.
5451 * Move the handshake programming to initialization sequence.
5452 * Previously was left up to BIOS.
5453 */
5454 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5455 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5456 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5457
5458 /* Enable PG1 for cdclk */
5459 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5460
5461 /* check if cd clock is enabled */
5462 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5463 DRM_DEBUG_KMS("Display already initialized\n");
5464 return;
5465 }
5466
5467 /*
5468 * FIXME:
5469 * - The initial CDCLK needs to be read from VBT.
5470 * Need to make this change after VBT has changes for BXT.
5471 * - check if setting the max (or any) cdclk freq is really necessary
5472 * here, it belongs to modeset time
5473 */
5474 broxton_set_cdclk(dev, 624000);
5475
5476 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
22e02c0b
VS
5477 POSTING_READ(DBUF_CTL);
5478
f8437dd1
VK
5479 udelay(10);
5480
5481 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5482 DRM_ERROR("DBuf power enable timeout!\n");
5483}
5484
5485void broxton_uninit_cdclk(struct drm_device *dev)
5486{
5487 struct drm_i915_private *dev_priv = dev->dev_private;
5488
5489 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
22e02c0b
VS
5490 POSTING_READ(DBUF_CTL);
5491
f8437dd1
VK
5492 udelay(10);
5493
5494 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5495 DRM_ERROR("DBuf power disable timeout!\n");
5496
5497 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5498 broxton_set_cdclk(dev, 19200);
5499
5500 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5501}
5502
5d96d8af
DL
5503static const struct skl_cdclk_entry {
5504 unsigned int freq;
5505 unsigned int vco;
5506} skl_cdclk_frequencies[] = {
5507 { .freq = 308570, .vco = 8640 },
5508 { .freq = 337500, .vco = 8100 },
5509 { .freq = 432000, .vco = 8640 },
5510 { .freq = 450000, .vco = 8100 },
5511 { .freq = 540000, .vco = 8100 },
5512 { .freq = 617140, .vco = 8640 },
5513 { .freq = 675000, .vco = 8100 },
5514};
5515
5516static unsigned int skl_cdclk_decimal(unsigned int freq)
5517{
5518 return (freq - 1000) / 500;
5519}
5520
5521static unsigned int skl_cdclk_get_vco(unsigned int freq)
5522{
5523 unsigned int i;
5524
5525 for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
5526 const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
5527
5528 if (e->freq == freq)
5529 return e->vco;
5530 }
5531
5532 return 8100;
5533}
5534
5535static void
5536skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
5537{
5538 unsigned int min_freq;
5539 u32 val;
5540
5541 /* select the minimum CDCLK before enabling DPLL 0 */
5542 val = I915_READ(CDCLK_CTL);
5543 val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
5544 val |= CDCLK_FREQ_337_308;
5545
5546 if (required_vco == 8640)
5547 min_freq = 308570;
5548 else
5549 min_freq = 337500;
5550
5551 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
5552
5553 I915_WRITE(CDCLK_CTL, val);
5554 POSTING_READ(CDCLK_CTL);
5555
5556 /*
5557 * We always enable DPLL0 with the lowest link rate possible, but still
5558 * taking into account the VCO required to operate the eDP panel at the
5559 * desired frequency. The usual DP link rates operate with a VCO of
5560 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5561 * The modeset code is responsible for the selection of the exact link
5562 * rate later on, with the constraint of choosing a frequency that
5563 * works with required_vco.
5564 */
5565 val = I915_READ(DPLL_CTRL1);
5566
5567 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5568 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5569 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
5570 if (required_vco == 8640)
5571 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5572 SKL_DPLL0);
5573 else
5574 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5575 SKL_DPLL0);
5576
5577 I915_WRITE(DPLL_CTRL1, val);
5578 POSTING_READ(DPLL_CTRL1);
5579
5580 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5581
5582 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5583 DRM_ERROR("DPLL0 not locked\n");
5584}
5585
5586static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5587{
5588 int ret;
5589 u32 val;
5590
5591 /* inform PCU we want to change CDCLK */
5592 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5593 mutex_lock(&dev_priv->rps.hw_lock);
5594 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5595 mutex_unlock(&dev_priv->rps.hw_lock);
5596
5597 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5598}
5599
5600static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5601{
5602 unsigned int i;
5603
5604 for (i = 0; i < 15; i++) {
5605 if (skl_cdclk_pcu_ready(dev_priv))
5606 return true;
5607 udelay(10);
5608 }
5609
5610 return false;
5611}
5612
5613static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
5614{
560a7ae4 5615 struct drm_device *dev = dev_priv->dev;
5d96d8af
DL
5616 u32 freq_select, pcu_ack;
5617
5618 DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
5619
5620 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5621 DRM_ERROR("failed to inform PCU about cdclk change\n");
5622 return;
5623 }
5624
5625 /* set CDCLK_CTL */
5626 switch(freq) {
5627 case 450000:
5628 case 432000:
5629 freq_select = CDCLK_FREQ_450_432;
5630 pcu_ack = 1;
5631 break;
5632 case 540000:
5633 freq_select = CDCLK_FREQ_540;
5634 pcu_ack = 2;
5635 break;
5636 case 308570:
5637 case 337500:
5638 default:
5639 freq_select = CDCLK_FREQ_337_308;
5640 pcu_ack = 0;
5641 break;
5642 case 617140:
5643 case 675000:
5644 freq_select = CDCLK_FREQ_675_617;
5645 pcu_ack = 3;
5646 break;
5647 }
5648
5649 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
5650 POSTING_READ(CDCLK_CTL);
5651
5652 /* inform PCU of the change */
5653 mutex_lock(&dev_priv->rps.hw_lock);
5654 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5655 mutex_unlock(&dev_priv->rps.hw_lock);
560a7ae4
DL
5656
5657 intel_update_cdclk(dev);
5d96d8af
DL
5658}
5659
5660void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5661{
5662 /* disable DBUF power */
5663 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5664 POSTING_READ(DBUF_CTL);
5665
5666 udelay(10);
5667
5668 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5669 DRM_ERROR("DBuf power disable timeout\n");
5670
ab96c1ee
ID
5671 /* disable DPLL0 */
5672 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5673 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5674 DRM_ERROR("Couldn't disable DPLL0\n");
5d96d8af
DL
5675}
5676
5677void skl_init_cdclk(struct drm_i915_private *dev_priv)
5678{
5d96d8af
DL
5679 unsigned int required_vco;
5680
39d9b85a
GW
5681 /* DPLL0 not enabled (happens on early BIOS versions) */
5682 if (!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE)) {
5683 /* enable DPLL0 */
5684 required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
5685 skl_dpll0_enable(dev_priv, required_vco);
5d96d8af
DL
5686 }
5687
5d96d8af
DL
5688 /* set CDCLK to the frequency the BIOS chose */
5689 skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
5690
5691 /* enable DBUF power */
5692 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5693 POSTING_READ(DBUF_CTL);
5694
5695 udelay(10);
5696
5697 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5698 DRM_ERROR("DBuf power enable timeout\n");
5699}
5700
c73666f3
SK
5701int skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
5702{
5703 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
5704 uint32_t cdctl = I915_READ(CDCLK_CTL);
5705 int freq = dev_priv->skl_boot_cdclk;
5706
f1b391a5
SK
5707 /*
5708 * check if the pre-os intialized the display
5709 * There is SWF18 scratchpad register defined which is set by the
5710 * pre-os which can be used by the OS drivers to check the status
5711 */
5712 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
5713 goto sanitize;
5714
c73666f3
SK
5715 /* Is PLL enabled and locked ? */
5716 if (!((lcpll1 & LCPLL_PLL_ENABLE) && (lcpll1 & LCPLL_PLL_LOCK)))
5717 goto sanitize;
5718
5719 /* DPLL okay; verify the cdclock
5720 *
5721 * Noticed in some instances that the freq selection is correct but
5722 * decimal part is programmed wrong from BIOS where pre-os does not
5723 * enable display. Verify the same as well.
5724 */
5725 if (cdctl == ((cdctl & CDCLK_FREQ_SEL_MASK) | skl_cdclk_decimal(freq)))
5726 /* All well; nothing to sanitize */
5727 return false;
5728sanitize:
5729 /*
5730 * As of now initialize with max cdclk till
5731 * we get dynamic cdclk support
5732 * */
5733 dev_priv->skl_boot_cdclk = dev_priv->max_cdclk_freq;
5734 skl_init_cdclk(dev_priv);
5735
5736 /* we did have to sanitize */
5737 return true;
5738}
5739
30a970c6
JB
5740/* Adjust CDclk dividers to allow high res or save power if possible */
5741static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5742{
5743 struct drm_i915_private *dev_priv = dev->dev_private;
5744 u32 val, cmd;
5745
164dfd28
VK
5746 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5747 != dev_priv->cdclk_freq);
d60c4473 5748
dfcab17e 5749 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
30a970c6 5750 cmd = 2;
dfcab17e 5751 else if (cdclk == 266667)
30a970c6
JB
5752 cmd = 1;
5753 else
5754 cmd = 0;
5755
5756 mutex_lock(&dev_priv->rps.hw_lock);
5757 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5758 val &= ~DSPFREQGUAR_MASK;
5759 val |= (cmd << DSPFREQGUAR_SHIFT);
5760 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5761 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5762 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5763 50)) {
5764 DRM_ERROR("timed out waiting for CDclk change\n");
5765 }
5766 mutex_unlock(&dev_priv->rps.hw_lock);
5767
54433e91
VS
5768 mutex_lock(&dev_priv->sb_lock);
5769
dfcab17e 5770 if (cdclk == 400000) {
6bcda4f0 5771 u32 divider;
30a970c6 5772
6bcda4f0 5773 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
30a970c6 5774
30a970c6
JB
5775 /* adjust cdclk divider */
5776 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
87d5d259 5777 val &= ~CCK_FREQUENCY_VALUES;
30a970c6
JB
5778 val |= divider;
5779 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
a877e801
VS
5780
5781 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
87d5d259 5782 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
a877e801
VS
5783 50))
5784 DRM_ERROR("timed out waiting for CDclk change\n");
30a970c6
JB
5785 }
5786
30a970c6
JB
5787 /* adjust self-refresh exit latency value */
5788 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5789 val &= ~0x7f;
5790
5791 /*
5792 * For high bandwidth configs, we set a higher latency in the bunit
5793 * so that the core display fetch happens in time to avoid underruns.
5794 */
dfcab17e 5795 if (cdclk == 400000)
30a970c6
JB
5796 val |= 4500 / 250; /* 4.5 usec */
5797 else
5798 val |= 3000 / 250; /* 3.0 usec */
5799 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
54433e91 5800
a580516d 5801 mutex_unlock(&dev_priv->sb_lock);
30a970c6 5802
b6283055 5803 intel_update_cdclk(dev);
30a970c6
JB
5804}
5805
383c5a6a
VS
5806static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5807{
5808 struct drm_i915_private *dev_priv = dev->dev_private;
5809 u32 val, cmd;
5810
164dfd28
VK
5811 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5812 != dev_priv->cdclk_freq);
383c5a6a
VS
5813
5814 switch (cdclk) {
383c5a6a
VS
5815 case 333333:
5816 case 320000:
383c5a6a 5817 case 266667:
383c5a6a 5818 case 200000:
383c5a6a
VS
5819 break;
5820 default:
5f77eeb0 5821 MISSING_CASE(cdclk);
383c5a6a
VS
5822 return;
5823 }
5824
9d0d3fda
VS
5825 /*
5826 * Specs are full of misinformation, but testing on actual
5827 * hardware has shown that we just need to write the desired
5828 * CCK divider into the Punit register.
5829 */
5830 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5831
383c5a6a
VS
5832 mutex_lock(&dev_priv->rps.hw_lock);
5833 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5834 val &= ~DSPFREQGUAR_MASK_CHV;
5835 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5836 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5837 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5838 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5839 50)) {
5840 DRM_ERROR("timed out waiting for CDclk change\n");
5841 }
5842 mutex_unlock(&dev_priv->rps.hw_lock);
5843
b6283055 5844 intel_update_cdclk(dev);
383c5a6a
VS
5845}
5846
30a970c6
JB
5847static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5848 int max_pixclk)
5849{
6bcda4f0 5850 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
6cca3195 5851 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
29dc7ef3 5852
30a970c6
JB
5853 /*
5854 * Really only a few cases to deal with, as only 4 CDclks are supported:
5855 * 200MHz
5856 * 267MHz
29dc7ef3 5857 * 320/333MHz (depends on HPLL freq)
6cca3195
VS
5858 * 400MHz (VLV only)
5859 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5860 * of the lower bin and adjust if needed.
e37c67a1
VS
5861 *
5862 * We seem to get an unstable or solid color picture at 200MHz.
5863 * Not sure what's wrong. For now use 200MHz only when all pipes
5864 * are off.
30a970c6 5865 */
6cca3195
VS
5866 if (!IS_CHERRYVIEW(dev_priv) &&
5867 max_pixclk > freq_320*limit/100)
dfcab17e 5868 return 400000;
6cca3195 5869 else if (max_pixclk > 266667*limit/100)
29dc7ef3 5870 return freq_320;
e37c67a1 5871 else if (max_pixclk > 0)
dfcab17e 5872 return 266667;
e37c67a1
VS
5873 else
5874 return 200000;
30a970c6
JB
5875}
5876
f8437dd1
VK
5877static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5878 int max_pixclk)
5879{
5880 /*
5881 * FIXME:
5882 * - remove the guardband, it's not needed on BXT
5883 * - set 19.2MHz bypass frequency if there are no active pipes
5884 */
5885 if (max_pixclk > 576000*9/10)
5886 return 624000;
5887 else if (max_pixclk > 384000*9/10)
5888 return 576000;
5889 else if (max_pixclk > 288000*9/10)
5890 return 384000;
5891 else if (max_pixclk > 144000*9/10)
5892 return 288000;
5893 else
5894 return 144000;
5895}
5896
e8788cbc 5897/* Compute the max pixel clock for new configuration. */
a821fc46
ACO
5898static int intel_mode_max_pixclk(struct drm_device *dev,
5899 struct drm_atomic_state *state)
30a970c6 5900{
565602d7
ML
5901 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
5902 struct drm_i915_private *dev_priv = dev->dev_private;
5903 struct drm_crtc *crtc;
5904 struct drm_crtc_state *crtc_state;
5905 unsigned max_pixclk = 0, i;
5906 enum pipe pipe;
30a970c6 5907
565602d7
ML
5908 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
5909 sizeof(intel_state->min_pixclk));
304603f4 5910
565602d7
ML
5911 for_each_crtc_in_state(state, crtc, crtc_state, i) {
5912 int pixclk = 0;
5913
5914 if (crtc_state->enable)
5915 pixclk = crtc_state->adjusted_mode.crtc_clock;
304603f4 5916
565602d7 5917 intel_state->min_pixclk[i] = pixclk;
30a970c6
JB
5918 }
5919
565602d7
ML
5920 for_each_pipe(dev_priv, pipe)
5921 max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
5922
30a970c6
JB
5923 return max_pixclk;
5924}
5925
27c329ed 5926static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
30a970c6 5927{
27c329ed
ML
5928 struct drm_device *dev = state->dev;
5929 struct drm_i915_private *dev_priv = dev->dev_private;
5930 int max_pixclk = intel_mode_max_pixclk(dev, state);
1a617b77
ML
5931 struct intel_atomic_state *intel_state =
5932 to_intel_atomic_state(state);
30a970c6 5933
304603f4
ACO
5934 if (max_pixclk < 0)
5935 return max_pixclk;
30a970c6 5936
1a617b77 5937 intel_state->cdclk = intel_state->dev_cdclk =
27c329ed 5938 valleyview_calc_cdclk(dev_priv, max_pixclk);
0a9ab303 5939
1a617b77
ML
5940 if (!intel_state->active_crtcs)
5941 intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
5942
27c329ed
ML
5943 return 0;
5944}
304603f4 5945
27c329ed
ML
5946static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
5947{
5948 struct drm_device *dev = state->dev;
5949 struct drm_i915_private *dev_priv = dev->dev_private;
5950 int max_pixclk = intel_mode_max_pixclk(dev, state);
1a617b77
ML
5951 struct intel_atomic_state *intel_state =
5952 to_intel_atomic_state(state);
85a96e7a 5953
27c329ed
ML
5954 if (max_pixclk < 0)
5955 return max_pixclk;
85a96e7a 5956
1a617b77 5957 intel_state->cdclk = intel_state->dev_cdclk =
27c329ed 5958 broxton_calc_cdclk(dev_priv, max_pixclk);
85a96e7a 5959
1a617b77
ML
5960 if (!intel_state->active_crtcs)
5961 intel_state->dev_cdclk = broxton_calc_cdclk(dev_priv, 0);
5962
27c329ed 5963 return 0;
30a970c6
JB
5964}
5965
1e69cd74
VS
5966static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5967{
5968 unsigned int credits, default_credits;
5969
5970 if (IS_CHERRYVIEW(dev_priv))
5971 default_credits = PFI_CREDIT(12);
5972 else
5973 default_credits = PFI_CREDIT(8);
5974
bfa7df01 5975 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
1e69cd74
VS
5976 /* CHV suggested value is 31 or 63 */
5977 if (IS_CHERRYVIEW(dev_priv))
fcc0008f 5978 credits = PFI_CREDIT_63;
1e69cd74
VS
5979 else
5980 credits = PFI_CREDIT(15);
5981 } else {
5982 credits = default_credits;
5983 }
5984
5985 /*
5986 * WA - write default credits before re-programming
5987 * FIXME: should we also set the resend bit here?
5988 */
5989 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5990 default_credits);
5991
5992 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5993 credits | PFI_CREDIT_RESEND);
5994
5995 /*
5996 * FIXME is this guaranteed to clear
5997 * immediately or should we poll for it?
5998 */
5999 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6000}
6001
27c329ed 6002static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
30a970c6 6003{
a821fc46 6004 struct drm_device *dev = old_state->dev;
30a970c6 6005 struct drm_i915_private *dev_priv = dev->dev_private;
1a617b77
ML
6006 struct intel_atomic_state *old_intel_state =
6007 to_intel_atomic_state(old_state);
6008 unsigned req_cdclk = old_intel_state->dev_cdclk;
30a970c6 6009
27c329ed
ML
6010 /*
6011 * FIXME: We can end up here with all power domains off, yet
6012 * with a CDCLK frequency other than the minimum. To account
6013 * for this take the PIPE-A power domain, which covers the HW
6014 * blocks needed for the following programming. This can be
6015 * removed once it's guaranteed that we get here either with
6016 * the minimum CDCLK set, or the required power domains
6017 * enabled.
6018 */
6019 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
738c05c0 6020
27c329ed
ML
6021 if (IS_CHERRYVIEW(dev))
6022 cherryview_set_cdclk(dev, req_cdclk);
6023 else
6024 valleyview_set_cdclk(dev, req_cdclk);
738c05c0 6025
27c329ed 6026 vlv_program_pfi_credits(dev_priv);
1e69cd74 6027
27c329ed 6028 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
30a970c6
JB
6029}
6030
89b667f8
JB
6031static void valleyview_crtc_enable(struct drm_crtc *crtc)
6032{
6033 struct drm_device *dev = crtc->dev;
a72e4c9f 6034 struct drm_i915_private *dev_priv = to_i915(dev);
89b667f8
JB
6035 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6036 struct intel_encoder *encoder;
6037 int pipe = intel_crtc->pipe;
89b667f8 6038
53d9f4e9 6039 if (WARN_ON(intel_crtc->active))
89b667f8
JB
6040 return;
6041
6e3c9717 6042 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 6043 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
6044
6045 intel_set_pipe_timings(intel_crtc);
bc58be60 6046 intel_set_pipe_src_size(intel_crtc);
5b18e57c 6047
c14b0485
VS
6048 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6049 struct drm_i915_private *dev_priv = dev->dev_private;
6050
6051 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6052 I915_WRITE(CHV_CANVAS(pipe), 0);
6053 }
6054
5b18e57c
DV
6055 i9xx_set_pipeconf(intel_crtc);
6056
89b667f8 6057 intel_crtc->active = true;
89b667f8 6058
a72e4c9f 6059 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6060
89b667f8
JB
6061 for_each_encoder_on_crtc(dev, crtc, encoder)
6062 if (encoder->pre_pll_enable)
6063 encoder->pre_pll_enable(encoder);
6064
a65347ba 6065 if (!intel_crtc->config->has_dsi_encoder) {
c0b4c660
VS
6066 if (IS_CHERRYVIEW(dev)) {
6067 chv_prepare_pll(intel_crtc, intel_crtc->config);
6e3c9717 6068 chv_enable_pll(intel_crtc, intel_crtc->config);
c0b4c660
VS
6069 } else {
6070 vlv_prepare_pll(intel_crtc, intel_crtc->config);
6e3c9717 6071 vlv_enable_pll(intel_crtc, intel_crtc->config);
c0b4c660 6072 }
9d556c99 6073 }
89b667f8
JB
6074
6075 for_each_encoder_on_crtc(dev, crtc, encoder)
6076 if (encoder->pre_enable)
6077 encoder->pre_enable(encoder);
6078
2dd24552
JB
6079 i9xx_pfit_enable(intel_crtc);
6080
8563b1e8 6081 intel_color_load_luts(crtc);
63cbb074 6082
caed361d 6083 intel_update_watermarks(crtc);
e1fdc473 6084 intel_enable_pipe(intel_crtc);
be6a6f8e 6085
4b3a9526
VS
6086 assert_vblank_disabled(crtc);
6087 drm_crtc_vblank_on(crtc);
6088
f9b61ff6
DV
6089 for_each_encoder_on_crtc(dev, crtc, encoder)
6090 encoder->enable(encoder);
89b667f8
JB
6091}
6092
f13c2ef3
DV
6093static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6094{
6095 struct drm_device *dev = crtc->base.dev;
6096 struct drm_i915_private *dev_priv = dev->dev_private;
6097
6e3c9717
ACO
6098 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6099 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
f13c2ef3
DV
6100}
6101
0b8765c6 6102static void i9xx_crtc_enable(struct drm_crtc *crtc)
79e53945
JB
6103{
6104 struct drm_device *dev = crtc->dev;
a72e4c9f 6105 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 6106 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 6107 struct intel_encoder *encoder;
79e53945 6108 int pipe = intel_crtc->pipe;
79e53945 6109
53d9f4e9 6110 if (WARN_ON(intel_crtc->active))
f7abfe8b
CW
6111 return;
6112
f13c2ef3
DV
6113 i9xx_set_pll_dividers(intel_crtc);
6114
6e3c9717 6115 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 6116 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
6117
6118 intel_set_pipe_timings(intel_crtc);
bc58be60 6119 intel_set_pipe_src_size(intel_crtc);
5b18e57c 6120
5b18e57c
DV
6121 i9xx_set_pipeconf(intel_crtc);
6122
f7abfe8b 6123 intel_crtc->active = true;
6b383a7f 6124
4a3436e8 6125 if (!IS_GEN2(dev))
a72e4c9f 6126 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6127
9d6d9f19
MK
6128 for_each_encoder_on_crtc(dev, crtc, encoder)
6129 if (encoder->pre_enable)
6130 encoder->pre_enable(encoder);
6131
f6736a1a
DV
6132 i9xx_enable_pll(intel_crtc);
6133
2dd24552
JB
6134 i9xx_pfit_enable(intel_crtc);
6135
8563b1e8 6136 intel_color_load_luts(crtc);
63cbb074 6137
f37fcc2a 6138 intel_update_watermarks(crtc);
e1fdc473 6139 intel_enable_pipe(intel_crtc);
be6a6f8e 6140
4b3a9526
VS
6141 assert_vblank_disabled(crtc);
6142 drm_crtc_vblank_on(crtc);
6143
f9b61ff6
DV
6144 for_each_encoder_on_crtc(dev, crtc, encoder)
6145 encoder->enable(encoder);
0b8765c6 6146}
79e53945 6147
87476d63
DV
6148static void i9xx_pfit_disable(struct intel_crtc *crtc)
6149{
6150 struct drm_device *dev = crtc->base.dev;
6151 struct drm_i915_private *dev_priv = dev->dev_private;
87476d63 6152
6e3c9717 6153 if (!crtc->config->gmch_pfit.control)
328d8e82 6154 return;
87476d63 6155
328d8e82 6156 assert_pipe_disabled(dev_priv, crtc->pipe);
87476d63 6157
328d8e82
DV
6158 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6159 I915_READ(PFIT_CONTROL));
6160 I915_WRITE(PFIT_CONTROL, 0);
87476d63
DV
6161}
6162
0b8765c6
JB
6163static void i9xx_crtc_disable(struct drm_crtc *crtc)
6164{
6165 struct drm_device *dev = crtc->dev;
6166 struct drm_i915_private *dev_priv = dev->dev_private;
6167 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 6168 struct intel_encoder *encoder;
0b8765c6 6169 int pipe = intel_crtc->pipe;
ef9c3aee 6170
6304cd91
VS
6171 /*
6172 * On gen2 planes are double buffered but the pipe isn't, so we must
6173 * wait for planes to fully turn off before disabling the pipe.
6174 */
90e83e53
ACO
6175 if (IS_GEN2(dev))
6176 intel_wait_for_vblank(dev, pipe);
6304cd91 6177
4b3a9526
VS
6178 for_each_encoder_on_crtc(dev, crtc, encoder)
6179 encoder->disable(encoder);
6180
f9b61ff6
DV
6181 drm_crtc_vblank_off(crtc);
6182 assert_vblank_disabled(crtc);
6183
575f7ab7 6184 intel_disable_pipe(intel_crtc);
24a1f16d 6185
87476d63 6186 i9xx_pfit_disable(intel_crtc);
24a1f16d 6187
89b667f8
JB
6188 for_each_encoder_on_crtc(dev, crtc, encoder)
6189 if (encoder->post_disable)
6190 encoder->post_disable(encoder);
6191
a65347ba 6192 if (!intel_crtc->config->has_dsi_encoder) {
076ed3b2
CML
6193 if (IS_CHERRYVIEW(dev))
6194 chv_disable_pll(dev_priv, pipe);
6195 else if (IS_VALLEYVIEW(dev))
6196 vlv_disable_pll(dev_priv, pipe);
6197 else
1c4e0274 6198 i9xx_disable_pll(intel_crtc);
076ed3b2 6199 }
0b8765c6 6200
d6db995f
VS
6201 for_each_encoder_on_crtc(dev, crtc, encoder)
6202 if (encoder->post_pll_disable)
6203 encoder->post_pll_disable(encoder);
6204
4a3436e8 6205 if (!IS_GEN2(dev))
a72e4c9f 6206 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
0b8765c6
JB
6207}
6208
b17d48e2
ML
6209static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
6210{
842e0307 6211 struct intel_encoder *encoder;
b17d48e2
ML
6212 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6213 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6214 enum intel_display_power_domain domain;
6215 unsigned long domains;
6216
6217 if (!intel_crtc->active)
6218 return;
6219
a539205a 6220 if (to_intel_plane_state(crtc->primary->state)->visible) {
fc32b1fd
ML
6221 WARN_ON(intel_crtc->unpin_work);
6222
2622a081 6223 intel_pre_disable_primary_noatomic(crtc);
54a41961
ML
6224
6225 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
6226 to_intel_plane_state(crtc->primary->state)->visible = false;
a539205a
ML
6227 }
6228
b17d48e2 6229 dev_priv->display.crtc_disable(crtc);
842e0307
ML
6230
6231 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was enabled, now disabled\n",
6232 crtc->base.id);
6233
6234 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6235 crtc->state->active = false;
37d9078b 6236 intel_crtc->active = false;
842e0307
ML
6237 crtc->enabled = false;
6238 crtc->state->connector_mask = 0;
6239 crtc->state->encoder_mask = 0;
6240
6241 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6242 encoder->base.crtc = NULL;
6243
58f9c0bc 6244 intel_fbc_disable(intel_crtc);
37d9078b 6245 intel_update_watermarks(crtc);
1f7457b1 6246 intel_disable_shared_dpll(intel_crtc);
b17d48e2
ML
6247
6248 domains = intel_crtc->enabled_power_domains;
6249 for_each_power_domain(domain, domains)
6250 intel_display_power_put(dev_priv, domain);
6251 intel_crtc->enabled_power_domains = 0;
565602d7
ML
6252
6253 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
6254 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
b17d48e2
ML
6255}
6256
6b72d486
ML
6257/*
6258 * turn all crtc's off, but do not adjust state
6259 * This has to be paired with a call to intel_modeset_setup_hw_state.
6260 */
70e0bd74 6261int intel_display_suspend(struct drm_device *dev)
ee7b9f93 6262{
e2c8b870 6263 struct drm_i915_private *dev_priv = to_i915(dev);
70e0bd74 6264 struct drm_atomic_state *state;
e2c8b870 6265 int ret;
70e0bd74 6266
e2c8b870
ML
6267 state = drm_atomic_helper_suspend(dev);
6268 ret = PTR_ERR_OR_ZERO(state);
70e0bd74
ML
6269 if (ret)
6270 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
e2c8b870
ML
6271 else
6272 dev_priv->modeset_restore_state = state;
70e0bd74 6273 return ret;
ee7b9f93
JB
6274}
6275
ea5b213a 6276void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 6277{
4ef69c7a 6278 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 6279
ea5b213a
CW
6280 drm_encoder_cleanup(encoder);
6281 kfree(intel_encoder);
7e7d76c3
JB
6282}
6283
0a91ca29
DV
6284/* Cross check the actual hw state with our own modeset state tracking (and it's
6285 * internal consistency). */
b980514c 6286static void intel_connector_check_state(struct intel_connector *connector)
79e53945 6287{
35dd3c64
ML
6288 struct drm_crtc *crtc = connector->base.state->crtc;
6289
6290 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6291 connector->base.base.id,
6292 connector->base.name);
6293
0a91ca29 6294 if (connector->get_hw_state(connector)) {
e85376cb 6295 struct intel_encoder *encoder = connector->encoder;
35dd3c64 6296 struct drm_connector_state *conn_state = connector->base.state;
0a91ca29 6297
35dd3c64
ML
6298 I915_STATE_WARN(!crtc,
6299 "connector enabled without attached crtc\n");
0a91ca29 6300
35dd3c64
ML
6301 if (!crtc)
6302 return;
6303
6304 I915_STATE_WARN(!crtc->state->active,
6305 "connector is active, but attached crtc isn't\n");
6306
e85376cb 6307 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
35dd3c64
ML
6308 return;
6309
e85376cb 6310 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
35dd3c64
ML
6311 "atomic encoder doesn't match attached encoder\n");
6312
e85376cb 6313 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
35dd3c64
ML
6314 "attached encoder crtc differs from connector crtc\n");
6315 } else {
4d688a2a
ML
6316 I915_STATE_WARN(crtc && crtc->state->active,
6317 "attached crtc is active, but connector isn't\n");
35dd3c64
ML
6318 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
6319 "best encoder set without crtc!\n");
0a91ca29 6320 }
79e53945
JB
6321}
6322
08d9bc92
ACO
6323int intel_connector_init(struct intel_connector *connector)
6324{
5350a031 6325 drm_atomic_helper_connector_reset(&connector->base);
08d9bc92 6326
5350a031 6327 if (!connector->base.state)
08d9bc92
ACO
6328 return -ENOMEM;
6329
08d9bc92
ACO
6330 return 0;
6331}
6332
6333struct intel_connector *intel_connector_alloc(void)
6334{
6335 struct intel_connector *connector;
6336
6337 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6338 if (!connector)
6339 return NULL;
6340
6341 if (intel_connector_init(connector) < 0) {
6342 kfree(connector);
6343 return NULL;
6344 }
6345
6346 return connector;
6347}
6348
f0947c37
DV
6349/* Simple connector->get_hw_state implementation for encoders that support only
6350 * one connector and no cloning and hence the encoder state determines the state
6351 * of the connector. */
6352bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 6353{
24929352 6354 enum pipe pipe = 0;
f0947c37 6355 struct intel_encoder *encoder = connector->encoder;
ea5b213a 6356
f0947c37 6357 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
6358}
6359
6d293983 6360static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
d272ddfa 6361{
6d293983
ACO
6362 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6363 return crtc_state->fdi_lanes;
d272ddfa
VS
6364
6365 return 0;
6366}
6367
6d293983 6368static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5cec258b 6369 struct intel_crtc_state *pipe_config)
1857e1da 6370{
6d293983
ACO
6371 struct drm_atomic_state *state = pipe_config->base.state;
6372 struct intel_crtc *other_crtc;
6373 struct intel_crtc_state *other_crtc_state;
6374
1857e1da
DV
6375 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6376 pipe_name(pipe), pipe_config->fdi_lanes);
6377 if (pipe_config->fdi_lanes > 4) {
6378 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6379 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6380 return -EINVAL;
1857e1da
DV
6381 }
6382
bafb6553 6383 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
1857e1da
DV
6384 if (pipe_config->fdi_lanes > 2) {
6385 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6386 pipe_config->fdi_lanes);
6d293983 6387 return -EINVAL;
1857e1da 6388 } else {
6d293983 6389 return 0;
1857e1da
DV
6390 }
6391 }
6392
6393 if (INTEL_INFO(dev)->num_pipes == 2)
6d293983 6394 return 0;
1857e1da
DV
6395
6396 /* Ivybridge 3 pipe is really complicated */
6397 switch (pipe) {
6398 case PIPE_A:
6d293983 6399 return 0;
1857e1da 6400 case PIPE_B:
6d293983
ACO
6401 if (pipe_config->fdi_lanes <= 2)
6402 return 0;
6403
6404 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6405 other_crtc_state =
6406 intel_atomic_get_crtc_state(state, other_crtc);
6407 if (IS_ERR(other_crtc_state))
6408 return PTR_ERR(other_crtc_state);
6409
6410 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
1857e1da
DV
6411 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6412 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6413 return -EINVAL;
1857e1da 6414 }
6d293983 6415 return 0;
1857e1da 6416 case PIPE_C:
251cc67c
VS
6417 if (pipe_config->fdi_lanes > 2) {
6418 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6419 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6420 return -EINVAL;
251cc67c 6421 }
6d293983
ACO
6422
6423 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6424 other_crtc_state =
6425 intel_atomic_get_crtc_state(state, other_crtc);
6426 if (IS_ERR(other_crtc_state))
6427 return PTR_ERR(other_crtc_state);
6428
6429 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
1857e1da 6430 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
6d293983 6431 return -EINVAL;
1857e1da 6432 }
6d293983 6433 return 0;
1857e1da
DV
6434 default:
6435 BUG();
6436 }
6437}
6438
e29c22c0
DV
6439#define RETRY 1
6440static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5cec258b 6441 struct intel_crtc_state *pipe_config)
877d48d5 6442{
1857e1da 6443 struct drm_device *dev = intel_crtc->base.dev;
7c5f93b0 6444 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6d293983
ACO
6445 int lane, link_bw, fdi_dotclock, ret;
6446 bool needs_recompute = false;
877d48d5 6447
e29c22c0 6448retry:
877d48d5
DV
6449 /* FDI is a binary signal running at ~2.7GHz, encoding
6450 * each output octet as 10 bits. The actual frequency
6451 * is stored as a divider into a 100MHz clock, and the
6452 * mode pixel clock is stored in units of 1KHz.
6453 * Hence the bw of each lane in terms of the mode signal
6454 * is:
6455 */
21a727b3 6456 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
877d48d5 6457
241bfc38 6458 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 6459
2bd89a07 6460 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
DV
6461 pipe_config->pipe_bpp);
6462
6463 pipe_config->fdi_lanes = lane;
6464
2bd89a07 6465 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
877d48d5 6466 link_bw, &pipe_config->fdi_m_n);
1857e1da 6467
e3b247da 6468 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
6d293983 6469 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
e29c22c0
DV
6470 pipe_config->pipe_bpp -= 2*3;
6471 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6472 pipe_config->pipe_bpp);
6473 needs_recompute = true;
6474 pipe_config->bw_constrained = true;
6475
6476 goto retry;
6477 }
6478
6479 if (needs_recompute)
6480 return RETRY;
6481
6d293983 6482 return ret;
877d48d5
DV
6483}
6484
8cfb3407
VS
6485static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6486 struct intel_crtc_state *pipe_config)
6487{
6488 if (pipe_config->pipe_bpp > 24)
6489 return false;
6490
6491 /* HSW can handle pixel rate up to cdclk? */
6492 if (IS_HASWELL(dev_priv->dev))
6493 return true;
6494
6495 /*
b432e5cf
VS
6496 * We compare against max which means we must take
6497 * the increased cdclk requirement into account when
6498 * calculating the new cdclk.
6499 *
6500 * Should measure whether using a lower cdclk w/o IPS
8cfb3407
VS
6501 */
6502 return ilk_pipe_pixel_rate(pipe_config) <=
6503 dev_priv->max_cdclk_freq * 95 / 100;
6504}
6505
42db64ef 6506static void hsw_compute_ips_config(struct intel_crtc *crtc,
5cec258b 6507 struct intel_crtc_state *pipe_config)
42db64ef 6508{
8cfb3407
VS
6509 struct drm_device *dev = crtc->base.dev;
6510 struct drm_i915_private *dev_priv = dev->dev_private;
6511
d330a953 6512 pipe_config->ips_enabled = i915.enable_ips &&
8cfb3407
VS
6513 hsw_crtc_supports_ips(crtc) &&
6514 pipe_config_supports_ips(dev_priv, pipe_config);
42db64ef
PZ
6515}
6516
39acb4aa
VS
6517static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
6518{
6519 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6520
6521 /* GDG double wide on either pipe, otherwise pipe A only */
6522 return INTEL_INFO(dev_priv)->gen < 4 &&
6523 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
6524}
6525
a43f6e0f 6526static int intel_crtc_compute_config(struct intel_crtc *crtc,
5cec258b 6527 struct intel_crtc_state *pipe_config)
79e53945 6528{
a43f6e0f 6529 struct drm_device *dev = crtc->base.dev;
8bd31e67 6530 struct drm_i915_private *dev_priv = dev->dev_private;
7c5f93b0 6531 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
89749350 6532
ad3a4479 6533 /* FIXME should check pixel clock limits on all platforms */
cf532bb2 6534 if (INTEL_INFO(dev)->gen < 4) {
39acb4aa 6535 int clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
cf532bb2
VS
6536
6537 /*
39acb4aa 6538 * Enable double wide mode when the dot clock
cf532bb2 6539 * is > 90% of the (display) core speed.
cf532bb2 6540 */
39acb4aa
VS
6541 if (intel_crtc_supports_double_wide(crtc) &&
6542 adjusted_mode->crtc_clock > clock_limit) {
ad3a4479 6543 clock_limit *= 2;
cf532bb2 6544 pipe_config->double_wide = true;
ad3a4479
VS
6545 }
6546
39acb4aa
VS
6547 if (adjusted_mode->crtc_clock > clock_limit) {
6548 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6549 adjusted_mode->crtc_clock, clock_limit,
6550 yesno(pipe_config->double_wide));
e29c22c0 6551 return -EINVAL;
39acb4aa 6552 }
2c07245f 6553 }
89749350 6554
1d1d0e27
VS
6555 /*
6556 * Pipe horizontal size must be even in:
6557 * - DVO ganged mode
6558 * - LVDS dual channel mode
6559 * - Double wide pipe
6560 */
a93e255f 6561 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
1d1d0e27
VS
6562 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6563 pipe_config->pipe_src_w &= ~1;
6564
8693a824
DL
6565 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6566 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
44f46b42
CW
6567 */
6568 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
aad941d5 6569 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
e29c22c0 6570 return -EINVAL;
44f46b42 6571
f5adf94e 6572 if (HAS_IPS(dev))
a43f6e0f
DV
6573 hsw_compute_ips_config(crtc, pipe_config);
6574
877d48d5 6575 if (pipe_config->has_pch_encoder)
a43f6e0f 6576 return ironlake_fdi_compute_config(crtc, pipe_config);
877d48d5 6577
cf5a15be 6578 return 0;
79e53945
JB
6579}
6580
1652d19e
VS
6581static int skylake_get_display_clock_speed(struct drm_device *dev)
6582{
6583 struct drm_i915_private *dev_priv = to_i915(dev);
6584 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6585 uint32_t cdctl = I915_READ(CDCLK_CTL);
6586 uint32_t linkrate;
6587
414355a7 6588 if (!(lcpll1 & LCPLL_PLL_ENABLE))
1652d19e 6589 return 24000; /* 24MHz is the cd freq with NSSC ref */
1652d19e
VS
6590
6591 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6592 return 540000;
6593
6594 linkrate = (I915_READ(DPLL_CTRL1) &
71cd8423 6595 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
1652d19e 6596
71cd8423
DL
6597 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6598 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
1652d19e
VS
6599 /* vco 8640 */
6600 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6601 case CDCLK_FREQ_450_432:
6602 return 432000;
6603 case CDCLK_FREQ_337_308:
6604 return 308570;
6605 case CDCLK_FREQ_675_617:
6606 return 617140;
6607 default:
6608 WARN(1, "Unknown cd freq selection\n");
6609 }
6610 } else {
6611 /* vco 8100 */
6612 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6613 case CDCLK_FREQ_450_432:
6614 return 450000;
6615 case CDCLK_FREQ_337_308:
6616 return 337500;
6617 case CDCLK_FREQ_675_617:
6618 return 675000;
6619 default:
6620 WARN(1, "Unknown cd freq selection\n");
6621 }
6622 }
6623
6624 /* error case, do as if DPLL0 isn't enabled */
6625 return 24000;
6626}
6627
acd3f3d3
BP
6628static int broxton_get_display_clock_speed(struct drm_device *dev)
6629{
6630 struct drm_i915_private *dev_priv = to_i915(dev);
6631 uint32_t cdctl = I915_READ(CDCLK_CTL);
6632 uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
6633 uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
6634 int cdclk;
6635
6636 if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
6637 return 19200;
6638
6639 cdclk = 19200 * pll_ratio / 2;
6640
6641 switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
6642 case BXT_CDCLK_CD2X_DIV_SEL_1:
6643 return cdclk; /* 576MHz or 624MHz */
6644 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
6645 return cdclk * 2 / 3; /* 384MHz */
6646 case BXT_CDCLK_CD2X_DIV_SEL_2:
6647 return cdclk / 2; /* 288MHz */
6648 case BXT_CDCLK_CD2X_DIV_SEL_4:
6649 return cdclk / 4; /* 144MHz */
6650 }
6651
6652 /* error case, do as if DE PLL isn't enabled */
6653 return 19200;
6654}
6655
1652d19e
VS
6656static int broadwell_get_display_clock_speed(struct drm_device *dev)
6657{
6658 struct drm_i915_private *dev_priv = dev->dev_private;
6659 uint32_t lcpll = I915_READ(LCPLL_CTL);
6660 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6661
6662 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6663 return 800000;
6664 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6665 return 450000;
6666 else if (freq == LCPLL_CLK_FREQ_450)
6667 return 450000;
6668 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6669 return 540000;
6670 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6671 return 337500;
6672 else
6673 return 675000;
6674}
6675
6676static int haswell_get_display_clock_speed(struct drm_device *dev)
6677{
6678 struct drm_i915_private *dev_priv = dev->dev_private;
6679 uint32_t lcpll = I915_READ(LCPLL_CTL);
6680 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6681
6682 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6683 return 800000;
6684 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6685 return 450000;
6686 else if (freq == LCPLL_CLK_FREQ_450)
6687 return 450000;
6688 else if (IS_HSW_ULT(dev))
6689 return 337500;
6690 else
6691 return 540000;
79e53945
JB
6692}
6693
25eb05fc
JB
6694static int valleyview_get_display_clock_speed(struct drm_device *dev)
6695{
bfa7df01
VS
6696 return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
6697 CCK_DISPLAY_CLOCK_CONTROL);
25eb05fc
JB
6698}
6699
b37a6434
VS
6700static int ilk_get_display_clock_speed(struct drm_device *dev)
6701{
6702 return 450000;
6703}
6704
e70236a8
JB
6705static int i945_get_display_clock_speed(struct drm_device *dev)
6706{
6707 return 400000;
6708}
79e53945 6709
e70236a8 6710static int i915_get_display_clock_speed(struct drm_device *dev)
79e53945 6711{
e907f170 6712 return 333333;
e70236a8 6713}
79e53945 6714
e70236a8
JB
6715static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6716{
6717 return 200000;
6718}
79e53945 6719
257a7ffc
DV
6720static int pnv_get_display_clock_speed(struct drm_device *dev)
6721{
6722 u16 gcfgc = 0;
6723
6724 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6725
6726 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6727 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
e907f170 6728 return 266667;
257a7ffc 6729 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
e907f170 6730 return 333333;
257a7ffc 6731 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
e907f170 6732 return 444444;
257a7ffc
DV
6733 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6734 return 200000;
6735 default:
6736 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6737 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
e907f170 6738 return 133333;
257a7ffc 6739 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
e907f170 6740 return 166667;
257a7ffc
DV
6741 }
6742}
6743
e70236a8
JB
6744static int i915gm_get_display_clock_speed(struct drm_device *dev)
6745{
6746 u16 gcfgc = 0;
79e53945 6747
e70236a8
JB
6748 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6749
6750 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
e907f170 6751 return 133333;
e70236a8
JB
6752 else {
6753 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6754 case GC_DISPLAY_CLOCK_333_MHZ:
e907f170 6755 return 333333;
e70236a8
JB
6756 default:
6757 case GC_DISPLAY_CLOCK_190_200_MHZ:
6758 return 190000;
79e53945 6759 }
e70236a8
JB
6760 }
6761}
6762
6763static int i865_get_display_clock_speed(struct drm_device *dev)
6764{
e907f170 6765 return 266667;
e70236a8
JB
6766}
6767
1b1d2716 6768static int i85x_get_display_clock_speed(struct drm_device *dev)
e70236a8
JB
6769{
6770 u16 hpllcc = 0;
1b1d2716 6771
65cd2b3f
VS
6772 /*
6773 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6774 * encoding is different :(
6775 * FIXME is this the right way to detect 852GM/852GMV?
6776 */
6777 if (dev->pdev->revision == 0x1)
6778 return 133333;
6779
1b1d2716
VS
6780 pci_bus_read_config_word(dev->pdev->bus,
6781 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6782
e70236a8
JB
6783 /* Assume that the hardware is in the high speed state. This
6784 * should be the default.
6785 */
6786 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6787 case GC_CLOCK_133_200:
1b1d2716 6788 case GC_CLOCK_133_200_2:
e70236a8
JB
6789 case GC_CLOCK_100_200:
6790 return 200000;
6791 case GC_CLOCK_166_250:
6792 return 250000;
6793 case GC_CLOCK_100_133:
e907f170 6794 return 133333;
1b1d2716
VS
6795 case GC_CLOCK_133_266:
6796 case GC_CLOCK_133_266_2:
6797 case GC_CLOCK_166_266:
6798 return 266667;
e70236a8 6799 }
79e53945 6800
e70236a8
JB
6801 /* Shouldn't happen */
6802 return 0;
6803}
79e53945 6804
e70236a8
JB
6805static int i830_get_display_clock_speed(struct drm_device *dev)
6806{
e907f170 6807 return 133333;
79e53945
JB
6808}
6809
34edce2f
VS
6810static unsigned int intel_hpll_vco(struct drm_device *dev)
6811{
6812 struct drm_i915_private *dev_priv = dev->dev_private;
6813 static const unsigned int blb_vco[8] = {
6814 [0] = 3200000,
6815 [1] = 4000000,
6816 [2] = 5333333,
6817 [3] = 4800000,
6818 [4] = 6400000,
6819 };
6820 static const unsigned int pnv_vco[8] = {
6821 [0] = 3200000,
6822 [1] = 4000000,
6823 [2] = 5333333,
6824 [3] = 4800000,
6825 [4] = 2666667,
6826 };
6827 static const unsigned int cl_vco[8] = {
6828 [0] = 3200000,
6829 [1] = 4000000,
6830 [2] = 5333333,
6831 [3] = 6400000,
6832 [4] = 3333333,
6833 [5] = 3566667,
6834 [6] = 4266667,
6835 };
6836 static const unsigned int elk_vco[8] = {
6837 [0] = 3200000,
6838 [1] = 4000000,
6839 [2] = 5333333,
6840 [3] = 4800000,
6841 };
6842 static const unsigned int ctg_vco[8] = {
6843 [0] = 3200000,
6844 [1] = 4000000,
6845 [2] = 5333333,
6846 [3] = 6400000,
6847 [4] = 2666667,
6848 [5] = 4266667,
6849 };
6850 const unsigned int *vco_table;
6851 unsigned int vco;
6852 uint8_t tmp = 0;
6853
6854 /* FIXME other chipsets? */
6855 if (IS_GM45(dev))
6856 vco_table = ctg_vco;
6857 else if (IS_G4X(dev))
6858 vco_table = elk_vco;
6859 else if (IS_CRESTLINE(dev))
6860 vco_table = cl_vco;
6861 else if (IS_PINEVIEW(dev))
6862 vco_table = pnv_vco;
6863 else if (IS_G33(dev))
6864 vco_table = blb_vco;
6865 else
6866 return 0;
6867
6868 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6869
6870 vco = vco_table[tmp & 0x7];
6871 if (vco == 0)
6872 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6873 else
6874 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6875
6876 return vco;
6877}
6878
6879static int gm45_get_display_clock_speed(struct drm_device *dev)
6880{
6881 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6882 uint16_t tmp = 0;
6883
6884 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6885
6886 cdclk_sel = (tmp >> 12) & 0x1;
6887
6888 switch (vco) {
6889 case 2666667:
6890 case 4000000:
6891 case 5333333:
6892 return cdclk_sel ? 333333 : 222222;
6893 case 3200000:
6894 return cdclk_sel ? 320000 : 228571;
6895 default:
6896 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
6897 return 222222;
6898 }
6899}
6900
6901static int i965gm_get_display_clock_speed(struct drm_device *dev)
6902{
6903 static const uint8_t div_3200[] = { 16, 10, 8 };
6904 static const uint8_t div_4000[] = { 20, 12, 10 };
6905 static const uint8_t div_5333[] = { 24, 16, 14 };
6906 const uint8_t *div_table;
6907 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6908 uint16_t tmp = 0;
6909
6910 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6911
6912 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
6913
6914 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6915 goto fail;
6916
6917 switch (vco) {
6918 case 3200000:
6919 div_table = div_3200;
6920 break;
6921 case 4000000:
6922 div_table = div_4000;
6923 break;
6924 case 5333333:
6925 div_table = div_5333;
6926 break;
6927 default:
6928 goto fail;
6929 }
6930
6931 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6932
caf4e252 6933fail:
34edce2f
VS
6934 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
6935 return 200000;
6936}
6937
6938static int g33_get_display_clock_speed(struct drm_device *dev)
6939{
6940 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
6941 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
6942 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
6943 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
6944 const uint8_t *div_table;
6945 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6946 uint16_t tmp = 0;
6947
6948 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6949
6950 cdclk_sel = (tmp >> 4) & 0x7;
6951
6952 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6953 goto fail;
6954
6955 switch (vco) {
6956 case 3200000:
6957 div_table = div_3200;
6958 break;
6959 case 4000000:
6960 div_table = div_4000;
6961 break;
6962 case 4800000:
6963 div_table = div_4800;
6964 break;
6965 case 5333333:
6966 div_table = div_5333;
6967 break;
6968 default:
6969 goto fail;
6970 }
6971
6972 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6973
caf4e252 6974fail:
34edce2f
VS
6975 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
6976 return 190476;
6977}
6978
2c07245f 6979static void
a65851af 6980intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 6981{
a65851af
VS
6982 while (*num > DATA_LINK_M_N_MASK ||
6983 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
6984 *num >>= 1;
6985 *den >>= 1;
6986 }
6987}
6988
a65851af
VS
6989static void compute_m_n(unsigned int m, unsigned int n,
6990 uint32_t *ret_m, uint32_t *ret_n)
6991{
6992 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
6993 *ret_m = div_u64((uint64_t) m * *ret_n, n);
6994 intel_reduce_m_n_ratio(ret_m, ret_n);
6995}
6996
e69d0bc1
DV
6997void
6998intel_link_compute_m_n(int bits_per_pixel, int nlanes,
6999 int pixel_clock, int link_clock,
7000 struct intel_link_m_n *m_n)
2c07245f 7001{
e69d0bc1 7002 m_n->tu = 64;
a65851af
VS
7003
7004 compute_m_n(bits_per_pixel * pixel_clock,
7005 link_clock * nlanes * 8,
7006 &m_n->gmch_m, &m_n->gmch_n);
7007
7008 compute_m_n(pixel_clock, link_clock,
7009 &m_n->link_m, &m_n->link_n);
2c07245f
ZW
7010}
7011
a7615030
CW
7012static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7013{
d330a953
JN
7014 if (i915.panel_use_ssc >= 0)
7015 return i915.panel_use_ssc != 0;
41aa3448 7016 return dev_priv->vbt.lvds_use_ssc
435793df 7017 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
7018}
7019
7429e9d4 7020static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 7021{
7df00d7a 7022 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 7023}
f47709a9 7024
7429e9d4
DV
7025static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7026{
7027 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
7028}
7029
f47709a9 7030static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
190f68c5 7031 struct intel_crtc_state *crtc_state,
a7516a05
JB
7032 intel_clock_t *reduced_clock)
7033{
f47709a9 7034 struct drm_device *dev = crtc->base.dev;
a7516a05
JB
7035 u32 fp, fp2 = 0;
7036
7037 if (IS_PINEVIEW(dev)) {
190f68c5 7038 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7039 if (reduced_clock)
7429e9d4 7040 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 7041 } else {
190f68c5 7042 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7043 if (reduced_clock)
7429e9d4 7044 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
7045 }
7046
190f68c5 7047 crtc_state->dpll_hw_state.fp0 = fp;
a7516a05 7048
f47709a9 7049 crtc->lowfreq_avail = false;
a93e255f 7050 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ab585dea 7051 reduced_clock) {
190f68c5 7052 crtc_state->dpll_hw_state.fp1 = fp2;
f47709a9 7053 crtc->lowfreq_avail = true;
a7516a05 7054 } else {
190f68c5 7055 crtc_state->dpll_hw_state.fp1 = fp;
a7516a05
JB
7056 }
7057}
7058
5e69f97f
CML
7059static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7060 pipe)
89b667f8
JB
7061{
7062 u32 reg_val;
7063
7064 /*
7065 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7066 * and set it to a reasonable value instead.
7067 */
ab3c759a 7068 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
7069 reg_val &= 0xffffff00;
7070 reg_val |= 0x00000030;
ab3c759a 7071 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7072
ab3c759a 7073 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7074 reg_val &= 0x8cffffff;
7075 reg_val = 0x8c000000;
ab3c759a 7076 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 7077
ab3c759a 7078 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 7079 reg_val &= 0xffffff00;
ab3c759a 7080 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7081
ab3c759a 7082 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7083 reg_val &= 0x00ffffff;
7084 reg_val |= 0xb0000000;
ab3c759a 7085 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
7086}
7087
b551842d
DV
7088static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7089 struct intel_link_m_n *m_n)
7090{
7091 struct drm_device *dev = crtc->base.dev;
7092 struct drm_i915_private *dev_priv = dev->dev_private;
7093 int pipe = crtc->pipe;
7094
e3b95f1e
DV
7095 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7096 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7097 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7098 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
7099}
7100
7101static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
7102 struct intel_link_m_n *m_n,
7103 struct intel_link_m_n *m2_n2)
b551842d
DV
7104{
7105 struct drm_device *dev = crtc->base.dev;
7106 struct drm_i915_private *dev_priv = dev->dev_private;
7107 int pipe = crtc->pipe;
6e3c9717 7108 enum transcoder transcoder = crtc->config->cpu_transcoder;
b551842d
DV
7109
7110 if (INTEL_INFO(dev)->gen >= 5) {
7111 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7112 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7113 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7114 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
f769cd24
VK
7115 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7116 * for gen < 8) and if DRRS is supported (to make sure the
7117 * registers are not unnecessarily accessed).
7118 */
44395bfe 7119 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
6e3c9717 7120 crtc->config->has_drrs) {
f769cd24
VK
7121 I915_WRITE(PIPE_DATA_M2(transcoder),
7122 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7123 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7124 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7125 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7126 }
b551842d 7127 } else {
e3b95f1e
DV
7128 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7129 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7130 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7131 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
7132 }
7133}
7134
fe3cd48d 7135void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
03afc4a2 7136{
fe3cd48d
R
7137 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7138
7139 if (m_n == M1_N1) {
7140 dp_m_n = &crtc->config->dp_m_n;
7141 dp_m2_n2 = &crtc->config->dp_m2_n2;
7142 } else if (m_n == M2_N2) {
7143
7144 /*
7145 * M2_N2 registers are not supported. Hence m2_n2 divider value
7146 * needs to be programmed into M1_N1.
7147 */
7148 dp_m_n = &crtc->config->dp_m2_n2;
7149 } else {
7150 DRM_ERROR("Unsupported divider value\n");
7151 return;
7152 }
7153
6e3c9717
ACO
7154 if (crtc->config->has_pch_encoder)
7155 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
03afc4a2 7156 else
fe3cd48d 7157 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
03afc4a2
DV
7158}
7159
251ac862
DV
7160static void vlv_compute_dpll(struct intel_crtc *crtc,
7161 struct intel_crtc_state *pipe_config)
bdd4b6a6
DV
7162{
7163 u32 dpll, dpll_md;
7164
7165 /*
7166 * Enable DPIO clock input. We should never disable the reference
7167 * clock for pipe B, since VGA hotplug / manual detection depends
7168 * on it.
7169 */
60bfe44f
VS
7170 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REF_CLK_ENABLE_VLV |
7171 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_REF_CLK_VLV;
bdd4b6a6
DV
7172 /* We should never disable this, set it here for state tracking */
7173 if (crtc->pipe == PIPE_B)
7174 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7175 dpll |= DPLL_VCO_ENABLE;
d288f65f 7176 pipe_config->dpll_hw_state.dpll = dpll;
bdd4b6a6 7177
d288f65f 7178 dpll_md = (pipe_config->pixel_multiplier - 1)
bdd4b6a6 7179 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
d288f65f 7180 pipe_config->dpll_hw_state.dpll_md = dpll_md;
bdd4b6a6
DV
7181}
7182
d288f65f 7183static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7184 const struct intel_crtc_state *pipe_config)
a0c4da24 7185{
f47709a9 7186 struct drm_device *dev = crtc->base.dev;
a0c4da24 7187 struct drm_i915_private *dev_priv = dev->dev_private;
f47709a9 7188 int pipe = crtc->pipe;
bdd4b6a6 7189 u32 mdiv;
a0c4da24 7190 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 7191 u32 coreclk, reg_val;
a0c4da24 7192
a580516d 7193 mutex_lock(&dev_priv->sb_lock);
09153000 7194
d288f65f
VS
7195 bestn = pipe_config->dpll.n;
7196 bestm1 = pipe_config->dpll.m1;
7197 bestm2 = pipe_config->dpll.m2;
7198 bestp1 = pipe_config->dpll.p1;
7199 bestp2 = pipe_config->dpll.p2;
a0c4da24 7200
89b667f8
JB
7201 /* See eDP HDMI DPIO driver vbios notes doc */
7202
7203 /* PLL B needs special handling */
bdd4b6a6 7204 if (pipe == PIPE_B)
5e69f97f 7205 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
7206
7207 /* Set up Tx target for periodic Rcomp update */
ab3c759a 7208 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
7209
7210 /* Disable target IRef on PLL */
ab3c759a 7211 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 7212 reg_val &= 0x00ffffff;
ab3c759a 7213 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
7214
7215 /* Disable fast lock */
ab3c759a 7216 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
7217
7218 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
7219 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7220 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7221 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 7222 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
7223
7224 /*
7225 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7226 * but we don't support that).
7227 * Note: don't use the DAC post divider as it seems unstable.
7228 */
7229 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 7230 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7231
a0c4da24 7232 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 7233 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7234
89b667f8 7235 /* Set HBR and RBR LPF coefficients */
d288f65f 7236 if (pipe_config->port_clock == 162000 ||
409ee761
ACO
7237 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7238 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
ab3c759a 7239 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 7240 0x009f0003);
89b667f8 7241 else
ab3c759a 7242 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
7243 0x00d0000f);
7244
681a8504 7245 if (pipe_config->has_dp_encoder) {
89b667f8 7246 /* Use SSC source */
bdd4b6a6 7247 if (pipe == PIPE_A)
ab3c759a 7248 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7249 0x0df40000);
7250 else
ab3c759a 7251 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7252 0x0df70000);
7253 } else { /* HDMI or VGA */
7254 /* Use bend source */
bdd4b6a6 7255 if (pipe == PIPE_A)
ab3c759a 7256 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7257 0x0df70000);
7258 else
ab3c759a 7259 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7260 0x0df40000);
7261 }
a0c4da24 7262
ab3c759a 7263 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8 7264 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
409ee761
ACO
7265 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7266 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
89b667f8 7267 coreclk |= 0x01000000;
ab3c759a 7268 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 7269
ab3c759a 7270 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
a580516d 7271 mutex_unlock(&dev_priv->sb_lock);
a0c4da24
JB
7272}
7273
251ac862
DV
7274static void chv_compute_dpll(struct intel_crtc *crtc,
7275 struct intel_crtc_state *pipe_config)
1ae0d137 7276{
60bfe44f
VS
7277 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
7278 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
1ae0d137
VS
7279 DPLL_VCO_ENABLE;
7280 if (crtc->pipe != PIPE_A)
d288f65f 7281 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
1ae0d137 7282
d288f65f
VS
7283 pipe_config->dpll_hw_state.dpll_md =
7284 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
1ae0d137
VS
7285}
7286
d288f65f 7287static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7288 const struct intel_crtc_state *pipe_config)
9d556c99
CML
7289{
7290 struct drm_device *dev = crtc->base.dev;
7291 struct drm_i915_private *dev_priv = dev->dev_private;
7292 int pipe = crtc->pipe;
f0f59a00 7293 i915_reg_t dpll_reg = DPLL(crtc->pipe);
9d556c99 7294 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9cbe40c1 7295 u32 loopfilter, tribuf_calcntr;
9d556c99 7296 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
a945ce7e 7297 u32 dpio_val;
9cbe40c1 7298 int vco;
9d556c99 7299
d288f65f
VS
7300 bestn = pipe_config->dpll.n;
7301 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7302 bestm1 = pipe_config->dpll.m1;
7303 bestm2 = pipe_config->dpll.m2 >> 22;
7304 bestp1 = pipe_config->dpll.p1;
7305 bestp2 = pipe_config->dpll.p2;
9cbe40c1 7306 vco = pipe_config->dpll.vco;
a945ce7e 7307 dpio_val = 0;
9cbe40c1 7308 loopfilter = 0;
9d556c99
CML
7309
7310 /*
7311 * Enable Refclk and SSC
7312 */
a11b0703 7313 I915_WRITE(dpll_reg,
d288f65f 7314 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
a11b0703 7315
a580516d 7316 mutex_lock(&dev_priv->sb_lock);
9d556c99 7317
9d556c99
CML
7318 /* p1 and p2 divider */
7319 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7320 5 << DPIO_CHV_S1_DIV_SHIFT |
7321 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7322 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7323 1 << DPIO_CHV_K_DIV_SHIFT);
7324
7325 /* Feedback post-divider - m2 */
7326 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7327
7328 /* Feedback refclk divider - n and m1 */
7329 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7330 DPIO_CHV_M1_DIV_BY_2 |
7331 1 << DPIO_CHV_N_DIV_SHIFT);
7332
7333 /* M2 fraction division */
25a25dfc 7334 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
9d556c99
CML
7335
7336 /* M2 fraction division enable */
a945ce7e
VP
7337 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7338 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7339 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7340 if (bestm2_frac)
7341 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7342 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
9d556c99 7343
de3a0fde
VP
7344 /* Program digital lock detect threshold */
7345 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7346 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7347 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7348 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7349 if (!bestm2_frac)
7350 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7351 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7352
9d556c99 7353 /* Loop filter */
9cbe40c1
VP
7354 if (vco == 5400000) {
7355 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7356 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7357 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7358 tribuf_calcntr = 0x9;
7359 } else if (vco <= 6200000) {
7360 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7361 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7362 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7363 tribuf_calcntr = 0x9;
7364 } else if (vco <= 6480000) {
7365 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7366 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7367 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7368 tribuf_calcntr = 0x8;
7369 } else {
7370 /* Not supported. Apply the same limits as in the max case */
7371 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7372 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7373 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7374 tribuf_calcntr = 0;
7375 }
9d556c99
CML
7376 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7377
968040b2 7378 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
9cbe40c1
VP
7379 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7380 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7381 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7382
9d556c99
CML
7383 /* AFC Recal */
7384 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7385 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7386 DPIO_AFC_RECAL);
7387
a580516d 7388 mutex_unlock(&dev_priv->sb_lock);
9d556c99
CML
7389}
7390
d288f65f
VS
7391/**
7392 * vlv_force_pll_on - forcibly enable just the PLL
7393 * @dev_priv: i915 private structure
7394 * @pipe: pipe PLL to enable
7395 * @dpll: PLL configuration
7396 *
7397 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7398 * in cases where we need the PLL enabled even when @pipe is not going to
7399 * be enabled.
7400 */
3f36b937
TU
7401int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7402 const struct dpll *dpll)
d288f65f
VS
7403{
7404 struct intel_crtc *crtc =
7405 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
3f36b937
TU
7406 struct intel_crtc_state *pipe_config;
7407
7408 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7409 if (!pipe_config)
7410 return -ENOMEM;
7411
7412 pipe_config->base.crtc = &crtc->base;
7413 pipe_config->pixel_multiplier = 1;
7414 pipe_config->dpll = *dpll;
d288f65f
VS
7415
7416 if (IS_CHERRYVIEW(dev)) {
3f36b937
TU
7417 chv_compute_dpll(crtc, pipe_config);
7418 chv_prepare_pll(crtc, pipe_config);
7419 chv_enable_pll(crtc, pipe_config);
d288f65f 7420 } else {
3f36b937
TU
7421 vlv_compute_dpll(crtc, pipe_config);
7422 vlv_prepare_pll(crtc, pipe_config);
7423 vlv_enable_pll(crtc, pipe_config);
d288f65f 7424 }
3f36b937
TU
7425
7426 kfree(pipe_config);
7427
7428 return 0;
d288f65f
VS
7429}
7430
7431/**
7432 * vlv_force_pll_off - forcibly disable just the PLL
7433 * @dev_priv: i915 private structure
7434 * @pipe: pipe PLL to disable
7435 *
7436 * Disable the PLL for @pipe. To be used in cases where we need
7437 * the PLL enabled even when @pipe is not going to be enabled.
7438 */
7439void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7440{
7441 if (IS_CHERRYVIEW(dev))
7442 chv_disable_pll(to_i915(dev), pipe);
7443 else
7444 vlv_disable_pll(to_i915(dev), pipe);
7445}
7446
251ac862
DV
7447static void i9xx_compute_dpll(struct intel_crtc *crtc,
7448 struct intel_crtc_state *crtc_state,
ceb41007 7449 intel_clock_t *reduced_clock)
eb1cbe48 7450{
f47709a9 7451 struct drm_device *dev = crtc->base.dev;
eb1cbe48 7452 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48
DV
7453 u32 dpll;
7454 bool is_sdvo;
190f68c5 7455 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7456
190f68c5 7457 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7458
a93e255f
ACO
7459 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7460 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
eb1cbe48
DV
7461
7462 dpll = DPLL_VGA_MODE_DIS;
7463
a93e255f 7464 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
7465 dpll |= DPLLB_MODE_LVDS;
7466 else
7467 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 7468
ef1b460d 7469 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
190f68c5 7470 dpll |= (crtc_state->pixel_multiplier - 1)
198a037f 7471 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 7472 }
198a037f
DV
7473
7474 if (is_sdvo)
4a33e48d 7475 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 7476
190f68c5 7477 if (crtc_state->has_dp_encoder)
4a33e48d 7478 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
7479
7480 /* compute bitmask from p1 value */
7481 if (IS_PINEVIEW(dev))
7482 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7483 else {
7484 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7485 if (IS_G4X(dev) && reduced_clock)
7486 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7487 }
7488 switch (clock->p2) {
7489 case 5:
7490 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7491 break;
7492 case 7:
7493 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7494 break;
7495 case 10:
7496 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7497 break;
7498 case 14:
7499 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7500 break;
7501 }
7502 if (INTEL_INFO(dev)->gen >= 4)
7503 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7504
190f68c5 7505 if (crtc_state->sdvo_tv_clock)
eb1cbe48 7506 dpll |= PLL_REF_INPUT_TVCLKINBC;
a93e255f 7507 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 7508 intel_panel_use_ssc(dev_priv))
eb1cbe48
DV
7509 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7510 else
7511 dpll |= PLL_REF_INPUT_DREFCLK;
7512
7513 dpll |= DPLL_VCO_ENABLE;
190f68c5 7514 crtc_state->dpll_hw_state.dpll = dpll;
8bcc2795 7515
eb1cbe48 7516 if (INTEL_INFO(dev)->gen >= 4) {
190f68c5 7517 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
ef1b460d 7518 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
190f68c5 7519 crtc_state->dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
DV
7520 }
7521}
7522
251ac862
DV
7523static void i8xx_compute_dpll(struct intel_crtc *crtc,
7524 struct intel_crtc_state *crtc_state,
ceb41007 7525 intel_clock_t *reduced_clock)
eb1cbe48 7526{
f47709a9 7527 struct drm_device *dev = crtc->base.dev;
eb1cbe48 7528 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48 7529 u32 dpll;
190f68c5 7530 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7531
190f68c5 7532 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7533
eb1cbe48
DV
7534 dpll = DPLL_VGA_MODE_DIS;
7535
a93e255f 7536 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
7537 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7538 } else {
7539 if (clock->p1 == 2)
7540 dpll |= PLL_P1_DIVIDE_BY_TWO;
7541 else
7542 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7543 if (clock->p2 == 4)
7544 dpll |= PLL_P2_DIVIDE_BY_4;
7545 }
7546
a93e255f 7547 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
4a33e48d
DV
7548 dpll |= DPLL_DVO_2X_MODE;
7549
a93e255f 7550 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 7551 intel_panel_use_ssc(dev_priv))
eb1cbe48
DV
7552 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7553 else
7554 dpll |= PLL_REF_INPUT_DREFCLK;
7555
7556 dpll |= DPLL_VCO_ENABLE;
190f68c5 7557 crtc_state->dpll_hw_state.dpll = dpll;
eb1cbe48
DV
7558}
7559
8a654f3b 7560static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c
PZ
7561{
7562 struct drm_device *dev = intel_crtc->base.dev;
7563 struct drm_i915_private *dev_priv = dev->dev_private;
7564 enum pipe pipe = intel_crtc->pipe;
6e3c9717 7565 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
7c5f93b0 7566 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
1caea6e9
VS
7567 uint32_t crtc_vtotal, crtc_vblank_end;
7568 int vsyncshift = 0;
4d8a62ea
DV
7569
7570 /* We need to be careful not to changed the adjusted mode, for otherwise
7571 * the hw state checker will get angry at the mismatch. */
7572 crtc_vtotal = adjusted_mode->crtc_vtotal;
7573 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 7574
609aeaca 7575 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 7576 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
7577 crtc_vtotal -= 1;
7578 crtc_vblank_end -= 1;
609aeaca 7579
409ee761 7580 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
609aeaca
VS
7581 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7582 else
7583 vsyncshift = adjusted_mode->crtc_hsync_start -
7584 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
7585 if (vsyncshift < 0)
7586 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
7587 }
7588
7589 if (INTEL_INFO(dev)->gen > 3)
fe2b8f9d 7590 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 7591
fe2b8f9d 7592 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
7593 (adjusted_mode->crtc_hdisplay - 1) |
7594 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 7595 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
7596 (adjusted_mode->crtc_hblank_start - 1) |
7597 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 7598 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
7599 (adjusted_mode->crtc_hsync_start - 1) |
7600 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7601
fe2b8f9d 7602 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 7603 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 7604 ((crtc_vtotal - 1) << 16));
fe2b8f9d 7605 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 7606 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 7607 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 7608 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
7609 (adjusted_mode->crtc_vsync_start - 1) |
7610 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7611
b5e508d4
PZ
7612 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7613 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7614 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7615 * bits. */
7616 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7617 (pipe == PIPE_B || pipe == PIPE_C))
7618 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7619
bc58be60
JN
7620}
7621
7622static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
7623{
7624 struct drm_device *dev = intel_crtc->base.dev;
7625 struct drm_i915_private *dev_priv = dev->dev_private;
7626 enum pipe pipe = intel_crtc->pipe;
7627
b0e77b9c
PZ
7628 /* pipesrc controls the size that is scaled from, which should
7629 * always be the user's requested size.
7630 */
7631 I915_WRITE(PIPESRC(pipe),
6e3c9717
ACO
7632 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7633 (intel_crtc->config->pipe_src_h - 1));
b0e77b9c
PZ
7634}
7635
1bd1bd80 7636static void intel_get_pipe_timings(struct intel_crtc *crtc,
5cec258b 7637 struct intel_crtc_state *pipe_config)
1bd1bd80
DV
7638{
7639 struct drm_device *dev = crtc->base.dev;
7640 struct drm_i915_private *dev_priv = dev->dev_private;
7641 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7642 uint32_t tmp;
7643
7644 tmp = I915_READ(HTOTAL(cpu_transcoder));
2d112de7
ACO
7645 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7646 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7647 tmp = I915_READ(HBLANK(cpu_transcoder));
2d112de7
ACO
7648 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7649 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7650 tmp = I915_READ(HSYNC(cpu_transcoder));
2d112de7
ACO
7651 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7652 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7653
7654 tmp = I915_READ(VTOTAL(cpu_transcoder));
2d112de7
ACO
7655 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7656 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7657 tmp = I915_READ(VBLANK(cpu_transcoder));
2d112de7
ACO
7658 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7659 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7660 tmp = I915_READ(VSYNC(cpu_transcoder));
2d112de7
ACO
7661 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7662 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7663
7664 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
2d112de7
ACO
7665 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7666 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7667 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
1bd1bd80 7668 }
bc58be60
JN
7669}
7670
7671static void intel_get_pipe_src_size(struct intel_crtc *crtc,
7672 struct intel_crtc_state *pipe_config)
7673{
7674 struct drm_device *dev = crtc->base.dev;
7675 struct drm_i915_private *dev_priv = dev->dev_private;
7676 u32 tmp;
1bd1bd80
DV
7677
7678 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
7679 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7680 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7681
2d112de7
ACO
7682 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7683 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
DV
7684}
7685
f6a83288 7686void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5cec258b 7687 struct intel_crtc_state *pipe_config)
babea61d 7688{
2d112de7
ACO
7689 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7690 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7691 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7692 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
babea61d 7693
2d112de7
ACO
7694 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7695 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7696 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7697 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
babea61d 7698
2d112de7 7699 mode->flags = pipe_config->base.adjusted_mode.flags;
cd13f5ab 7700 mode->type = DRM_MODE_TYPE_DRIVER;
babea61d 7701
2d112de7
ACO
7702 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7703 mode->flags |= pipe_config->base.adjusted_mode.flags;
cd13f5ab
ML
7704
7705 mode->hsync = drm_mode_hsync(mode);
7706 mode->vrefresh = drm_mode_vrefresh(mode);
7707 drm_mode_set_name(mode);
babea61d
JB
7708}
7709
84b046f3
DV
7710static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7711{
7712 struct drm_device *dev = intel_crtc->base.dev;
7713 struct drm_i915_private *dev_priv = dev->dev_private;
7714 uint32_t pipeconf;
7715
9f11a9e4 7716 pipeconf = 0;
84b046f3 7717
b6b5d049
VS
7718 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7719 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7720 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
67c72a12 7721
6e3c9717 7722 if (intel_crtc->config->double_wide)
cf532bb2 7723 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 7724
ff9ce46e 7725 /* only g4x and later have fancy bpc/dither controls */
666a4537 7726 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
ff9ce46e 7727 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6e3c9717 7728 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
ff9ce46e 7729 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 7730 PIPECONF_DITHER_TYPE_SP;
84b046f3 7731
6e3c9717 7732 switch (intel_crtc->config->pipe_bpp) {
ff9ce46e
DV
7733 case 18:
7734 pipeconf |= PIPECONF_6BPC;
7735 break;
7736 case 24:
7737 pipeconf |= PIPECONF_8BPC;
7738 break;
7739 case 30:
7740 pipeconf |= PIPECONF_10BPC;
7741 break;
7742 default:
7743 /* Case prevented by intel_choose_pipe_bpp_dither. */
7744 BUG();
84b046f3
DV
7745 }
7746 }
7747
7748 if (HAS_PIPE_CXSR(dev)) {
7749 if (intel_crtc->lowfreq_avail) {
7750 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7751 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7752 } else {
7753 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
84b046f3
DV
7754 }
7755 }
7756
6e3c9717 7757 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
efc2cfff 7758 if (INTEL_INFO(dev)->gen < 4 ||
409ee761 7759 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
efc2cfff
VS
7760 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7761 else
7762 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7763 } else
84b046f3
DV
7764 pipeconf |= PIPECONF_PROGRESSIVE;
7765
666a4537
WB
7766 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
7767 intel_crtc->config->limited_color_range)
9f11a9e4 7768 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 7769
84b046f3
DV
7770 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7771 POSTING_READ(PIPECONF(intel_crtc->pipe));
7772}
7773
81c97f52
ACO
7774static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
7775 struct intel_crtc_state *crtc_state)
7776{
7777 struct drm_device *dev = crtc->base.dev;
7778 struct drm_i915_private *dev_priv = dev->dev_private;
7779 const intel_limit_t *limit;
7780 int refclk = 48000;
7781
7782 memset(&crtc_state->dpll_hw_state, 0,
7783 sizeof(crtc_state->dpll_hw_state));
7784
7785 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7786 if (intel_panel_use_ssc(dev_priv)) {
7787 refclk = dev_priv->vbt.lvds_ssc_freq;
7788 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7789 }
7790
7791 limit = &intel_limits_i8xx_lvds;
7792 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO)) {
7793 limit = &intel_limits_i8xx_dvo;
7794 } else {
7795 limit = &intel_limits_i8xx_dac;
7796 }
7797
7798 if (!crtc_state->clock_set &&
7799 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7800 refclk, NULL, &crtc_state->dpll)) {
7801 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7802 return -EINVAL;
7803 }
7804
7805 i8xx_compute_dpll(crtc, crtc_state, NULL);
7806
7807 return 0;
7808}
7809
19ec6693
ACO
7810static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
7811 struct intel_crtc_state *crtc_state)
7812{
7813 struct drm_device *dev = crtc->base.dev;
7814 struct drm_i915_private *dev_priv = dev->dev_private;
7815 const intel_limit_t *limit;
7816 int refclk = 96000;
7817
7818 memset(&crtc_state->dpll_hw_state, 0,
7819 sizeof(crtc_state->dpll_hw_state));
7820
7821 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7822 if (intel_panel_use_ssc(dev_priv)) {
7823 refclk = dev_priv->vbt.lvds_ssc_freq;
7824 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7825 }
7826
7827 if (intel_is_dual_link_lvds(dev))
7828 limit = &intel_limits_g4x_dual_channel_lvds;
7829 else
7830 limit = &intel_limits_g4x_single_channel_lvds;
7831 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
7832 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
7833 limit = &intel_limits_g4x_hdmi;
7834 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
7835 limit = &intel_limits_g4x_sdvo;
7836 } else {
7837 /* The option is for other outputs */
7838 limit = &intel_limits_i9xx_sdvo;
7839 }
7840
7841 if (!crtc_state->clock_set &&
7842 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7843 refclk, NULL, &crtc_state->dpll)) {
7844 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7845 return -EINVAL;
7846 }
7847
7848 i9xx_compute_dpll(crtc, crtc_state, NULL);
7849
7850 return 0;
7851}
7852
70e8aa21
ACO
7853static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
7854 struct intel_crtc_state *crtc_state)
7855{
7856 struct drm_device *dev = crtc->base.dev;
7857 struct drm_i915_private *dev_priv = dev->dev_private;
7858 const intel_limit_t *limit;
7859 int refclk = 96000;
7860
7861 memset(&crtc_state->dpll_hw_state, 0,
7862 sizeof(crtc_state->dpll_hw_state));
7863
7864 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7865 if (intel_panel_use_ssc(dev_priv)) {
7866 refclk = dev_priv->vbt.lvds_ssc_freq;
7867 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7868 }
7869
7870 limit = &intel_limits_pineview_lvds;
7871 } else {
7872 limit = &intel_limits_pineview_sdvo;
7873 }
7874
7875 if (!crtc_state->clock_set &&
7876 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7877 refclk, NULL, &crtc_state->dpll)) {
7878 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7879 return -EINVAL;
7880 }
7881
7882 i9xx_compute_dpll(crtc, crtc_state, NULL);
7883
7884 return 0;
7885}
7886
190f68c5
ACO
7887static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7888 struct intel_crtc_state *crtc_state)
79e53945 7889{
c7653199 7890 struct drm_device *dev = crtc->base.dev;
79e53945 7891 struct drm_i915_private *dev_priv = dev->dev_private;
d4906093 7892 const intel_limit_t *limit;
81c97f52 7893 int refclk = 96000;
79e53945 7894
dd3cd74a
ACO
7895 memset(&crtc_state->dpll_hw_state, 0,
7896 sizeof(crtc_state->dpll_hw_state));
7897
70e8aa21
ACO
7898 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7899 if (intel_panel_use_ssc(dev_priv)) {
7900 refclk = dev_priv->vbt.lvds_ssc_freq;
7901 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7902 }
43565a06 7903
70e8aa21
ACO
7904 limit = &intel_limits_i9xx_lvds;
7905 } else {
7906 limit = &intel_limits_i9xx_sdvo;
81c97f52 7907 }
79e53945 7908
70e8aa21
ACO
7909 if (!crtc_state->clock_set &&
7910 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7911 refclk, NULL, &crtc_state->dpll)) {
7912 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7913 return -EINVAL;
f47709a9 7914 }
7026d4ac 7915
81c97f52 7916 i9xx_compute_dpll(crtc, crtc_state, NULL);
79e53945 7917
c8f7a0db 7918 return 0;
f564048e
EA
7919}
7920
65b3d6a9
ACO
7921static int chv_crtc_compute_clock(struct intel_crtc *crtc,
7922 struct intel_crtc_state *crtc_state)
7923{
7924 int refclk = 100000;
7925 const intel_limit_t *limit = &intel_limits_chv;
7926
7927 memset(&crtc_state->dpll_hw_state, 0,
7928 sizeof(crtc_state->dpll_hw_state));
7929
7930 if (crtc_state->has_dsi_encoder)
7931 return 0;
7932
7933 if (!crtc_state->clock_set &&
7934 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7935 refclk, NULL, &crtc_state->dpll)) {
7936 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7937 return -EINVAL;
7938 }
7939
7940 chv_compute_dpll(crtc, crtc_state);
7941
7942 return 0;
7943}
7944
7945static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
7946 struct intel_crtc_state *crtc_state)
7947{
7948 int refclk = 100000;
7949 const intel_limit_t *limit = &intel_limits_vlv;
7950
7951 memset(&crtc_state->dpll_hw_state, 0,
7952 sizeof(crtc_state->dpll_hw_state));
7953
7954 if (crtc_state->has_dsi_encoder)
7955 return 0;
7956
7957 if (!crtc_state->clock_set &&
7958 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7959 refclk, NULL, &crtc_state->dpll)) {
7960 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7961 return -EINVAL;
7962 }
7963
7964 vlv_compute_dpll(crtc, crtc_state);
7965
7966 return 0;
7967}
7968
2fa2fe9a 7969static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5cec258b 7970 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
7971{
7972 struct drm_device *dev = crtc->base.dev;
7973 struct drm_i915_private *dev_priv = dev->dev_private;
7974 uint32_t tmp;
7975
dc9e7dec
VS
7976 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7977 return;
7978
2fa2fe9a 7979 tmp = I915_READ(PFIT_CONTROL);
06922821
DV
7980 if (!(tmp & PFIT_ENABLE))
7981 return;
2fa2fe9a 7982
06922821 7983 /* Check whether the pfit is attached to our pipe. */
2fa2fe9a
DV
7984 if (INTEL_INFO(dev)->gen < 4) {
7985 if (crtc->pipe != PIPE_B)
7986 return;
2fa2fe9a
DV
7987 } else {
7988 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7989 return;
7990 }
7991
06922821 7992 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a
DV
7993 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7994 if (INTEL_INFO(dev)->gen < 5)
7995 pipe_config->gmch_pfit.lvds_border_bits =
7996 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7997}
7998
acbec814 7999static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 8000 struct intel_crtc_state *pipe_config)
acbec814
JB
8001{
8002 struct drm_device *dev = crtc->base.dev;
8003 struct drm_i915_private *dev_priv = dev->dev_private;
8004 int pipe = pipe_config->cpu_transcoder;
8005 intel_clock_t clock;
8006 u32 mdiv;
662c6ecb 8007 int refclk = 100000;
acbec814 8008
f573de5a
SK
8009 /* In case of MIPI DPLL will not even be used */
8010 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
8011 return;
8012
a580516d 8013 mutex_lock(&dev_priv->sb_lock);
ab3c759a 8014 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
a580516d 8015 mutex_unlock(&dev_priv->sb_lock);
acbec814
JB
8016
8017 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8018 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8019 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8020 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8021 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8022
dccbea3b 8023 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
acbec814
JB
8024}
8025
5724dbd1
DL
8026static void
8027i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8028 struct intel_initial_plane_config *plane_config)
1ad292b5
JB
8029{
8030 struct drm_device *dev = crtc->base.dev;
8031 struct drm_i915_private *dev_priv = dev->dev_private;
8032 u32 val, base, offset;
8033 int pipe = crtc->pipe, plane = crtc->plane;
8034 int fourcc, pixel_format;
6761dd31 8035 unsigned int aligned_height;
b113d5ee 8036 struct drm_framebuffer *fb;
1b842c89 8037 struct intel_framebuffer *intel_fb;
1ad292b5 8038
42a7b088
DL
8039 val = I915_READ(DSPCNTR(plane));
8040 if (!(val & DISPLAY_PLANE_ENABLE))
8041 return;
8042
d9806c9f 8043 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 8044 if (!intel_fb) {
1ad292b5
JB
8045 DRM_DEBUG_KMS("failed to alloc fb\n");
8046 return;
8047 }
8048
1b842c89
DL
8049 fb = &intel_fb->base;
8050
18c5247e
DV
8051 if (INTEL_INFO(dev)->gen >= 4) {
8052 if (val & DISPPLANE_TILED) {
49af449b 8053 plane_config->tiling = I915_TILING_X;
18c5247e
DV
8054 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8055 }
8056 }
1ad292b5
JB
8057
8058 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 8059 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
8060 fb->pixel_format = fourcc;
8061 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
1ad292b5
JB
8062
8063 if (INTEL_INFO(dev)->gen >= 4) {
49af449b 8064 if (plane_config->tiling)
1ad292b5
JB
8065 offset = I915_READ(DSPTILEOFF(plane));
8066 else
8067 offset = I915_READ(DSPLINOFF(plane));
8068 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8069 } else {
8070 base = I915_READ(DSPADDR(plane));
8071 }
8072 plane_config->base = base;
8073
8074 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
8075 fb->width = ((val >> 16) & 0xfff) + 1;
8076 fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
8077
8078 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 8079 fb->pitches[0] = val & 0xffffffc0;
1ad292b5 8080
b113d5ee 8081 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
8082 fb->pixel_format,
8083 fb->modifier[0]);
1ad292b5 8084
f37b5c2b 8085 plane_config->size = fb->pitches[0] * aligned_height;
1ad292b5 8086
2844a921
DL
8087 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8088 pipe_name(pipe), plane, fb->width, fb->height,
8089 fb->bits_per_pixel, base, fb->pitches[0],
8090 plane_config->size);
1ad292b5 8091
2d14030b 8092 plane_config->fb = intel_fb;
1ad292b5
JB
8093}
8094
70b23a98 8095static void chv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 8096 struct intel_crtc_state *pipe_config)
70b23a98
VS
8097{
8098 struct drm_device *dev = crtc->base.dev;
8099 struct drm_i915_private *dev_priv = dev->dev_private;
8100 int pipe = pipe_config->cpu_transcoder;
8101 enum dpio_channel port = vlv_pipe_to_channel(pipe);
8102 intel_clock_t clock;
0d7b6b11 8103 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
70b23a98
VS
8104 int refclk = 100000;
8105
a580516d 8106 mutex_lock(&dev_priv->sb_lock);
70b23a98
VS
8107 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8108 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8109 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8110 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
0d7b6b11 8111 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
a580516d 8112 mutex_unlock(&dev_priv->sb_lock);
70b23a98
VS
8113
8114 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
0d7b6b11
ID
8115 clock.m2 = (pll_dw0 & 0xff) << 22;
8116 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8117 clock.m2 |= pll_dw2 & 0x3fffff;
70b23a98
VS
8118 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8119 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8120 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8121
dccbea3b 8122 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
70b23a98
VS
8123}
8124
0e8ffe1b 8125static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5cec258b 8126 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
8127{
8128 struct drm_device *dev = crtc->base.dev;
8129 struct drm_i915_private *dev_priv = dev->dev_private;
1729050e 8130 enum intel_display_power_domain power_domain;
0e8ffe1b 8131 uint32_t tmp;
1729050e 8132 bool ret;
0e8ffe1b 8133
1729050e
ID
8134 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8135 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0
ID
8136 return false;
8137
e143a21c 8138 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 8139 pipe_config->shared_dpll = NULL;
eccb140b 8140
1729050e
ID
8141 ret = false;
8142
0e8ffe1b
DV
8143 tmp = I915_READ(PIPECONF(crtc->pipe));
8144 if (!(tmp & PIPECONF_ENABLE))
1729050e 8145 goto out;
0e8ffe1b 8146
666a4537 8147 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
42571aef
VS
8148 switch (tmp & PIPECONF_BPC_MASK) {
8149 case PIPECONF_6BPC:
8150 pipe_config->pipe_bpp = 18;
8151 break;
8152 case PIPECONF_8BPC:
8153 pipe_config->pipe_bpp = 24;
8154 break;
8155 case PIPECONF_10BPC:
8156 pipe_config->pipe_bpp = 30;
8157 break;
8158 default:
8159 break;
8160 }
8161 }
8162
666a4537
WB
8163 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
8164 (tmp & PIPECONF_COLOR_RANGE_SELECT))
b5a9fa09
DV
8165 pipe_config->limited_color_range = true;
8166
282740f7
VS
8167 if (INTEL_INFO(dev)->gen < 4)
8168 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8169
1bd1bd80 8170 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 8171 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 8172
2fa2fe9a
DV
8173 i9xx_get_pfit_config(crtc, pipe_config);
8174
6c49f241
DV
8175 if (INTEL_INFO(dev)->gen >= 4) {
8176 tmp = I915_READ(DPLL_MD(crtc->pipe));
8177 pipe_config->pixel_multiplier =
8178 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8179 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 8180 pipe_config->dpll_hw_state.dpll_md = tmp;
6c49f241
DV
8181 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8182 tmp = I915_READ(DPLL(crtc->pipe));
8183 pipe_config->pixel_multiplier =
8184 ((tmp & SDVO_MULTIPLIER_MASK)
8185 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8186 } else {
8187 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8188 * port and will be fixed up in the encoder->get_config
8189 * function. */
8190 pipe_config->pixel_multiplier = 1;
8191 }
8bcc2795 8192 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
666a4537 8193 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
1c4e0274
VS
8194 /*
8195 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8196 * on 830. Filter it out here so that we don't
8197 * report errors due to that.
8198 */
8199 if (IS_I830(dev))
8200 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8201
8bcc2795
DV
8202 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8203 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
8204 } else {
8205 /* Mask out read-only status bits. */
8206 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8207 DPLL_PORTC_READY_MASK |
8208 DPLL_PORTB_READY_MASK);
8bcc2795 8209 }
6c49f241 8210
70b23a98
VS
8211 if (IS_CHERRYVIEW(dev))
8212 chv_crtc_clock_get(crtc, pipe_config);
8213 else if (IS_VALLEYVIEW(dev))
acbec814
JB
8214 vlv_crtc_clock_get(crtc, pipe_config);
8215 else
8216 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 8217
0f64614d
VS
8218 /*
8219 * Normally the dotclock is filled in by the encoder .get_config()
8220 * but in case the pipe is enabled w/o any ports we need a sane
8221 * default.
8222 */
8223 pipe_config->base.adjusted_mode.crtc_clock =
8224 pipe_config->port_clock / pipe_config->pixel_multiplier;
8225
1729050e
ID
8226 ret = true;
8227
8228out:
8229 intel_display_power_put(dev_priv, power_domain);
8230
8231 return ret;
0e8ffe1b
DV
8232}
8233
dde86e2d 8234static void ironlake_init_pch_refclk(struct drm_device *dev)
13d83a67
JB
8235{
8236 struct drm_i915_private *dev_priv = dev->dev_private;
13d83a67 8237 struct intel_encoder *encoder;
74cfd7ac 8238 u32 val, final;
13d83a67 8239 bool has_lvds = false;
199e5d79 8240 bool has_cpu_edp = false;
199e5d79 8241 bool has_panel = false;
99eb6a01
KP
8242 bool has_ck505 = false;
8243 bool can_ssc = false;
13d83a67
JB
8244
8245 /* We need to take the global config into account */
b2784e15 8246 for_each_intel_encoder(dev, encoder) {
199e5d79
KP
8247 switch (encoder->type) {
8248 case INTEL_OUTPUT_LVDS:
8249 has_panel = true;
8250 has_lvds = true;
8251 break;
8252 case INTEL_OUTPUT_EDP:
8253 has_panel = true;
2de6905f 8254 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
8255 has_cpu_edp = true;
8256 break;
6847d71b
PZ
8257 default:
8258 break;
13d83a67
JB
8259 }
8260 }
8261
99eb6a01 8262 if (HAS_PCH_IBX(dev)) {
41aa3448 8263 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
8264 can_ssc = has_ck505;
8265 } else {
8266 has_ck505 = false;
8267 can_ssc = true;
8268 }
8269
2de6905f
ID
8270 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8271 has_panel, has_lvds, has_ck505);
13d83a67
JB
8272
8273 /* Ironlake: try to setup display ref clock before DPLL
8274 * enabling. This is only under driver's control after
8275 * PCH B stepping, previous chipset stepping should be
8276 * ignoring this setting.
8277 */
74cfd7ac
CW
8278 val = I915_READ(PCH_DREF_CONTROL);
8279
8280 /* As we must carefully and slowly disable/enable each source in turn,
8281 * compute the final state we want first and check if we need to
8282 * make any changes at all.
8283 */
8284 final = val;
8285 final &= ~DREF_NONSPREAD_SOURCE_MASK;
8286 if (has_ck505)
8287 final |= DREF_NONSPREAD_CK505_ENABLE;
8288 else
8289 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8290
8291 final &= ~DREF_SSC_SOURCE_MASK;
8292 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8293 final &= ~DREF_SSC1_ENABLE;
8294
8295 if (has_panel) {
8296 final |= DREF_SSC_SOURCE_ENABLE;
8297
8298 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8299 final |= DREF_SSC1_ENABLE;
8300
8301 if (has_cpu_edp) {
8302 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8303 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8304 else
8305 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8306 } else
8307 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8308 } else {
8309 final |= DREF_SSC_SOURCE_DISABLE;
8310 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8311 }
8312
8313 if (final == val)
8314 return;
8315
13d83a67 8316 /* Always enable nonspread source */
74cfd7ac 8317 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 8318
99eb6a01 8319 if (has_ck505)
74cfd7ac 8320 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 8321 else
74cfd7ac 8322 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 8323
199e5d79 8324 if (has_panel) {
74cfd7ac
CW
8325 val &= ~DREF_SSC_SOURCE_MASK;
8326 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 8327
199e5d79 8328 /* SSC must be turned on before enabling the CPU output */
99eb6a01 8329 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 8330 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 8331 val |= DREF_SSC1_ENABLE;
e77166b5 8332 } else
74cfd7ac 8333 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
8334
8335 /* Get SSC going before enabling the outputs */
74cfd7ac 8336 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8337 POSTING_READ(PCH_DREF_CONTROL);
8338 udelay(200);
8339
74cfd7ac 8340 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
8341
8342 /* Enable CPU source on CPU attached eDP */
199e5d79 8343 if (has_cpu_edp) {
99eb6a01 8344 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 8345 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 8346 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 8347 } else
74cfd7ac 8348 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 8349 } else
74cfd7ac 8350 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 8351
74cfd7ac 8352 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8353 POSTING_READ(PCH_DREF_CONTROL);
8354 udelay(200);
8355 } else {
8356 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8357
74cfd7ac 8358 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
8359
8360 /* Turn off CPU output */
74cfd7ac 8361 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 8362
74cfd7ac 8363 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8364 POSTING_READ(PCH_DREF_CONTROL);
8365 udelay(200);
8366
8367 /* Turn off the SSC source */
74cfd7ac
CW
8368 val &= ~DREF_SSC_SOURCE_MASK;
8369 val |= DREF_SSC_SOURCE_DISABLE;
199e5d79
KP
8370
8371 /* Turn off SSC1 */
74cfd7ac 8372 val &= ~DREF_SSC1_ENABLE;
199e5d79 8373
74cfd7ac 8374 I915_WRITE(PCH_DREF_CONTROL, val);
13d83a67
JB
8375 POSTING_READ(PCH_DREF_CONTROL);
8376 udelay(200);
8377 }
74cfd7ac
CW
8378
8379 BUG_ON(val != final);
13d83a67
JB
8380}
8381
f31f2d55 8382static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 8383{
f31f2d55 8384 uint32_t tmp;
dde86e2d 8385
0ff066a9
PZ
8386 tmp = I915_READ(SOUTH_CHICKEN2);
8387 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8388 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 8389
0ff066a9
PZ
8390 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8391 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8392 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 8393
0ff066a9
PZ
8394 tmp = I915_READ(SOUTH_CHICKEN2);
8395 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8396 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 8397
0ff066a9
PZ
8398 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8399 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8400 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
8401}
8402
8403/* WaMPhyProgramming:hsw */
8404static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8405{
8406 uint32_t tmp;
dde86e2d
PZ
8407
8408 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8409 tmp &= ~(0xFF << 24);
8410 tmp |= (0x12 << 24);
8411 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8412
dde86e2d
PZ
8413 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8414 tmp |= (1 << 11);
8415 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8416
8417 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8418 tmp |= (1 << 11);
8419 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8420
dde86e2d
PZ
8421 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8422 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8423 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8424
8425 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8426 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8427 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8428
0ff066a9
PZ
8429 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8430 tmp &= ~(7 << 13);
8431 tmp |= (5 << 13);
8432 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 8433
0ff066a9
PZ
8434 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8435 tmp &= ~(7 << 13);
8436 tmp |= (5 << 13);
8437 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
8438
8439 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8440 tmp &= ~0xFF;
8441 tmp |= 0x1C;
8442 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8443
8444 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8445 tmp &= ~0xFF;
8446 tmp |= 0x1C;
8447 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8448
8449 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8450 tmp &= ~(0xFF << 16);
8451 tmp |= (0x1C << 16);
8452 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8453
8454 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8455 tmp &= ~(0xFF << 16);
8456 tmp |= (0x1C << 16);
8457 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8458
0ff066a9
PZ
8459 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8460 tmp |= (1 << 27);
8461 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 8462
0ff066a9
PZ
8463 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8464 tmp |= (1 << 27);
8465 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 8466
0ff066a9
PZ
8467 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8468 tmp &= ~(0xF << 28);
8469 tmp |= (4 << 28);
8470 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 8471
0ff066a9
PZ
8472 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8473 tmp &= ~(0xF << 28);
8474 tmp |= (4 << 28);
8475 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
8476}
8477
2fa86a1f
PZ
8478/* Implements 3 different sequences from BSpec chapter "Display iCLK
8479 * Programming" based on the parameters passed:
8480 * - Sequence to enable CLKOUT_DP
8481 * - Sequence to enable CLKOUT_DP without spread
8482 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8483 */
8484static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8485 bool with_fdi)
f31f2d55
PZ
8486{
8487 struct drm_i915_private *dev_priv = dev->dev_private;
2fa86a1f
PZ
8488 uint32_t reg, tmp;
8489
8490 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8491 with_spread = true;
c2699524 8492 if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
2fa86a1f 8493 with_fdi = false;
f31f2d55 8494
a580516d 8495 mutex_lock(&dev_priv->sb_lock);
f31f2d55
PZ
8496
8497 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8498 tmp &= ~SBI_SSCCTL_DISABLE;
8499 tmp |= SBI_SSCCTL_PATHALT;
8500 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8501
8502 udelay(24);
8503
2fa86a1f
PZ
8504 if (with_spread) {
8505 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8506 tmp &= ~SBI_SSCCTL_PATHALT;
8507 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 8508
2fa86a1f
PZ
8509 if (with_fdi) {
8510 lpt_reset_fdi_mphy(dev_priv);
8511 lpt_program_fdi_mphy(dev_priv);
8512 }
8513 }
dde86e2d 8514
c2699524 8515 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
2fa86a1f
PZ
8516 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8517 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8518 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246 8519
a580516d 8520 mutex_unlock(&dev_priv->sb_lock);
dde86e2d
PZ
8521}
8522
47701c3b
PZ
8523/* Sequence to disable CLKOUT_DP */
8524static void lpt_disable_clkout_dp(struct drm_device *dev)
8525{
8526 struct drm_i915_private *dev_priv = dev->dev_private;
8527 uint32_t reg, tmp;
8528
a580516d 8529 mutex_lock(&dev_priv->sb_lock);
47701c3b 8530
c2699524 8531 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
47701c3b
PZ
8532 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8533 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8534 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8535
8536 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8537 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8538 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8539 tmp |= SBI_SSCCTL_PATHALT;
8540 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8541 udelay(32);
8542 }
8543 tmp |= SBI_SSCCTL_DISABLE;
8544 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8545 }
8546
a580516d 8547 mutex_unlock(&dev_priv->sb_lock);
47701c3b
PZ
8548}
8549
f7be2c21
VS
8550#define BEND_IDX(steps) ((50 + (steps)) / 5)
8551
8552static const uint16_t sscdivintphase[] = {
8553 [BEND_IDX( 50)] = 0x3B23,
8554 [BEND_IDX( 45)] = 0x3B23,
8555 [BEND_IDX( 40)] = 0x3C23,
8556 [BEND_IDX( 35)] = 0x3C23,
8557 [BEND_IDX( 30)] = 0x3D23,
8558 [BEND_IDX( 25)] = 0x3D23,
8559 [BEND_IDX( 20)] = 0x3E23,
8560 [BEND_IDX( 15)] = 0x3E23,
8561 [BEND_IDX( 10)] = 0x3F23,
8562 [BEND_IDX( 5)] = 0x3F23,
8563 [BEND_IDX( 0)] = 0x0025,
8564 [BEND_IDX( -5)] = 0x0025,
8565 [BEND_IDX(-10)] = 0x0125,
8566 [BEND_IDX(-15)] = 0x0125,
8567 [BEND_IDX(-20)] = 0x0225,
8568 [BEND_IDX(-25)] = 0x0225,
8569 [BEND_IDX(-30)] = 0x0325,
8570 [BEND_IDX(-35)] = 0x0325,
8571 [BEND_IDX(-40)] = 0x0425,
8572 [BEND_IDX(-45)] = 0x0425,
8573 [BEND_IDX(-50)] = 0x0525,
8574};
8575
8576/*
8577 * Bend CLKOUT_DP
8578 * steps -50 to 50 inclusive, in steps of 5
8579 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
8580 * change in clock period = -(steps / 10) * 5.787 ps
8581 */
8582static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
8583{
8584 uint32_t tmp;
8585 int idx = BEND_IDX(steps);
8586
8587 if (WARN_ON(steps % 5 != 0))
8588 return;
8589
8590 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
8591 return;
8592
8593 mutex_lock(&dev_priv->sb_lock);
8594
8595 if (steps % 10 != 0)
8596 tmp = 0xAAAAAAAB;
8597 else
8598 tmp = 0x00000000;
8599 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
8600
8601 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
8602 tmp &= 0xffff0000;
8603 tmp |= sscdivintphase[idx];
8604 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
8605
8606 mutex_unlock(&dev_priv->sb_lock);
8607}
8608
8609#undef BEND_IDX
8610
bf8fa3d3
PZ
8611static void lpt_init_pch_refclk(struct drm_device *dev)
8612{
bf8fa3d3
PZ
8613 struct intel_encoder *encoder;
8614 bool has_vga = false;
8615
b2784e15 8616 for_each_intel_encoder(dev, encoder) {
bf8fa3d3
PZ
8617 switch (encoder->type) {
8618 case INTEL_OUTPUT_ANALOG:
8619 has_vga = true;
8620 break;
6847d71b
PZ
8621 default:
8622 break;
bf8fa3d3
PZ
8623 }
8624 }
8625
f7be2c21
VS
8626 if (has_vga) {
8627 lpt_bend_clkout_dp(to_i915(dev), 0);
47701c3b 8628 lpt_enable_clkout_dp(dev, true, true);
f7be2c21 8629 } else {
47701c3b 8630 lpt_disable_clkout_dp(dev);
f7be2c21 8631 }
bf8fa3d3
PZ
8632}
8633
dde86e2d
PZ
8634/*
8635 * Initialize reference clocks when the driver loads
8636 */
8637void intel_init_pch_refclk(struct drm_device *dev)
8638{
8639 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8640 ironlake_init_pch_refclk(dev);
8641 else if (HAS_PCH_LPT(dev))
8642 lpt_init_pch_refclk(dev);
8643}
8644
6ff93609 8645static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 8646{
c8203565 8647 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
79e53945
JB
8648 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8649 int pipe = intel_crtc->pipe;
c8203565
PZ
8650 uint32_t val;
8651
78114071 8652 val = 0;
c8203565 8653
6e3c9717 8654 switch (intel_crtc->config->pipe_bpp) {
c8203565 8655 case 18:
dfd07d72 8656 val |= PIPECONF_6BPC;
c8203565
PZ
8657 break;
8658 case 24:
dfd07d72 8659 val |= PIPECONF_8BPC;
c8203565
PZ
8660 break;
8661 case 30:
dfd07d72 8662 val |= PIPECONF_10BPC;
c8203565
PZ
8663 break;
8664 case 36:
dfd07d72 8665 val |= PIPECONF_12BPC;
c8203565
PZ
8666 break;
8667 default:
cc769b62
PZ
8668 /* Case prevented by intel_choose_pipe_bpp_dither. */
8669 BUG();
c8203565
PZ
8670 }
8671
6e3c9717 8672 if (intel_crtc->config->dither)
c8203565
PZ
8673 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8674
6e3c9717 8675 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
8676 val |= PIPECONF_INTERLACED_ILK;
8677 else
8678 val |= PIPECONF_PROGRESSIVE;
8679
6e3c9717 8680 if (intel_crtc->config->limited_color_range)
3685a8f3 8681 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 8682
c8203565
PZ
8683 I915_WRITE(PIPECONF(pipe), val);
8684 POSTING_READ(PIPECONF(pipe));
8685}
8686
6ff93609 8687static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 8688{
391bf048 8689 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
ee2b0b38 8690 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 8691 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
391bf048 8692 u32 val = 0;
ee2b0b38 8693
391bf048 8694 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
ee2b0b38
PZ
8695 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8696
6e3c9717 8697 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
8698 val |= PIPECONF_INTERLACED_ILK;
8699 else
8700 val |= PIPECONF_PROGRESSIVE;
8701
702e7a56
PZ
8702 I915_WRITE(PIPECONF(cpu_transcoder), val);
8703 POSTING_READ(PIPECONF(cpu_transcoder));
391bf048
JN
8704}
8705
391bf048
JN
8706static void haswell_set_pipemisc(struct drm_crtc *crtc)
8707{
8708 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8709 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756f85cf 8710
391bf048
JN
8711 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
8712 u32 val = 0;
756f85cf 8713
6e3c9717 8714 switch (intel_crtc->config->pipe_bpp) {
756f85cf
PZ
8715 case 18:
8716 val |= PIPEMISC_DITHER_6_BPC;
8717 break;
8718 case 24:
8719 val |= PIPEMISC_DITHER_8_BPC;
8720 break;
8721 case 30:
8722 val |= PIPEMISC_DITHER_10_BPC;
8723 break;
8724 case 36:
8725 val |= PIPEMISC_DITHER_12_BPC;
8726 break;
8727 default:
8728 /* Case prevented by pipe_config_set_bpp. */
8729 BUG();
8730 }
8731
6e3c9717 8732 if (intel_crtc->config->dither)
756f85cf
PZ
8733 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8734
391bf048 8735 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
756f85cf 8736 }
ee2b0b38
PZ
8737}
8738
d4b1931c
PZ
8739int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8740{
8741 /*
8742 * Account for spread spectrum to avoid
8743 * oversubscribing the link. Max center spread
8744 * is 2.5%; use 5% for safety's sake.
8745 */
8746 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 8747 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
8748}
8749
7429e9d4 8750static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 8751{
7429e9d4 8752 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
8753}
8754
b75ca6f6
ACO
8755static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
8756 struct intel_crtc_state *crtc_state,
8757 intel_clock_t *reduced_clock)
79e53945 8758{
de13a2e3 8759 struct drm_crtc *crtc = &intel_crtc->base;
79e53945
JB
8760 struct drm_device *dev = crtc->dev;
8761 struct drm_i915_private *dev_priv = dev->dev_private;
55bb9992 8762 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 8763 struct drm_connector *connector;
55bb9992
ACO
8764 struct drm_connector_state *connector_state;
8765 struct intel_encoder *encoder;
b75ca6f6 8766 u32 dpll, fp, fp2;
ceb41007 8767 int factor, i;
09ede541 8768 bool is_lvds = false, is_sdvo = false;
79e53945 8769
da3ced29 8770 for_each_connector_in_state(state, connector, connector_state, i) {
55bb9992
ACO
8771 if (connector_state->crtc != crtc_state->base.crtc)
8772 continue;
8773
8774 encoder = to_intel_encoder(connector_state->best_encoder);
8775
8776 switch (encoder->type) {
79e53945
JB
8777 case INTEL_OUTPUT_LVDS:
8778 is_lvds = true;
8779 break;
8780 case INTEL_OUTPUT_SDVO:
7d57382e 8781 case INTEL_OUTPUT_HDMI:
79e53945 8782 is_sdvo = true;
79e53945 8783 break;
6847d71b
PZ
8784 default:
8785 break;
79e53945
JB
8786 }
8787 }
79e53945 8788
c1858123 8789 /* Enable autotuning of the PLL clock (if permissible) */
8febb297
EA
8790 factor = 21;
8791 if (is_lvds) {
8792 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 8793 dev_priv->vbt.lvds_ssc_freq == 100000) ||
f0b44056 8794 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
8febb297 8795 factor = 25;
190f68c5 8796 } else if (crtc_state->sdvo_tv_clock)
8febb297 8797 factor = 20;
c1858123 8798
b75ca6f6
ACO
8799 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
8800
190f68c5 8801 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
b75ca6f6
ACO
8802 fp |= FP_CB_TUNE;
8803
8804 if (reduced_clock) {
8805 fp2 = i9xx_dpll_compute_fp(reduced_clock);
2c07245f 8806
b75ca6f6
ACO
8807 if (reduced_clock->m < factor * reduced_clock->n)
8808 fp2 |= FP_CB_TUNE;
8809 } else {
8810 fp2 = fp;
8811 }
9a7c7890 8812
5eddb70b 8813 dpll = 0;
2c07245f 8814
a07d6787
EA
8815 if (is_lvds)
8816 dpll |= DPLLB_MODE_LVDS;
8817 else
8818 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 8819
190f68c5 8820 dpll |= (crtc_state->pixel_multiplier - 1)
ef1b460d 8821 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f
DV
8822
8823 if (is_sdvo)
4a33e48d 8824 dpll |= DPLL_SDVO_HIGH_SPEED;
190f68c5 8825 if (crtc_state->has_dp_encoder)
4a33e48d 8826 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 8827
a07d6787 8828 /* compute bitmask from p1 value */
190f68c5 8829 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 8830 /* also FPA1 */
190f68c5 8831 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 8832
190f68c5 8833 switch (crtc_state->dpll.p2) {
a07d6787
EA
8834 case 5:
8835 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8836 break;
8837 case 7:
8838 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8839 break;
8840 case 10:
8841 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8842 break;
8843 case 14:
8844 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8845 break;
79e53945
JB
8846 }
8847
ceb41007 8848 if (is_lvds && intel_panel_use_ssc(dev_priv))
43565a06 8849 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
8850 else
8851 dpll |= PLL_REF_INPUT_DREFCLK;
8852
b75ca6f6
ACO
8853 dpll |= DPLL_VCO_ENABLE;
8854
8855 crtc_state->dpll_hw_state.dpll = dpll;
8856 crtc_state->dpll_hw_state.fp0 = fp;
8857 crtc_state->dpll_hw_state.fp1 = fp2;
de13a2e3
PZ
8858}
8859
190f68c5
ACO
8860static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8861 struct intel_crtc_state *crtc_state)
de13a2e3 8862{
997c030c
ACO
8863 struct drm_device *dev = crtc->base.dev;
8864 struct drm_i915_private *dev_priv = dev->dev_private;
364ee29d 8865 intel_clock_t reduced_clock;
7ed9f894 8866 bool has_reduced_clock = false;
e2b78267 8867 struct intel_shared_dpll *pll;
997c030c
ACO
8868 const intel_limit_t *limit;
8869 int refclk = 120000;
de13a2e3 8870
dd3cd74a
ACO
8871 memset(&crtc_state->dpll_hw_state, 0,
8872 sizeof(crtc_state->dpll_hw_state));
8873
ded220e2
ACO
8874 crtc->lowfreq_avail = false;
8875
8876 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
8877 if (!crtc_state->has_pch_encoder)
8878 return 0;
79e53945 8879
997c030c
ACO
8880 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8881 if (intel_panel_use_ssc(dev_priv)) {
8882 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
8883 dev_priv->vbt.lvds_ssc_freq);
8884 refclk = dev_priv->vbt.lvds_ssc_freq;
8885 }
8886
8887 if (intel_is_dual_link_lvds(dev)) {
8888 if (refclk == 100000)
8889 limit = &intel_limits_ironlake_dual_lvds_100m;
8890 else
8891 limit = &intel_limits_ironlake_dual_lvds;
8892 } else {
8893 if (refclk == 100000)
8894 limit = &intel_limits_ironlake_single_lvds_100m;
8895 else
8896 limit = &intel_limits_ironlake_single_lvds;
8897 }
8898 } else {
8899 limit = &intel_limits_ironlake_dac;
8900 }
8901
364ee29d 8902 if (!crtc_state->clock_set &&
997c030c
ACO
8903 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8904 refclk, NULL, &crtc_state->dpll)) {
364ee29d
ACO
8905 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8906 return -EINVAL;
f47709a9 8907 }
79e53945 8908
b75ca6f6
ACO
8909 ironlake_compute_dpll(crtc, crtc_state,
8910 has_reduced_clock ? &reduced_clock : NULL);
66e985c0 8911
ded220e2
ACO
8912 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
8913 if (pll == NULL) {
8914 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
8915 pipe_name(crtc->pipe));
8916 return -EINVAL;
3fb37703 8917 }
79e53945 8918
ded220e2
ACO
8919 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8920 has_reduced_clock)
c7653199 8921 crtc->lowfreq_avail = true;
e2b78267 8922
c8f7a0db 8923 return 0;
79e53945
JB
8924}
8925
eb14cb74
VS
8926static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8927 struct intel_link_m_n *m_n)
8928{
8929 struct drm_device *dev = crtc->base.dev;
8930 struct drm_i915_private *dev_priv = dev->dev_private;
8931 enum pipe pipe = crtc->pipe;
8932
8933 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8934 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8935 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8936 & ~TU_SIZE_MASK;
8937 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8938 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8939 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8940}
8941
8942static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8943 enum transcoder transcoder,
b95af8be
VK
8944 struct intel_link_m_n *m_n,
8945 struct intel_link_m_n *m2_n2)
72419203
DV
8946{
8947 struct drm_device *dev = crtc->base.dev;
8948 struct drm_i915_private *dev_priv = dev->dev_private;
eb14cb74 8949 enum pipe pipe = crtc->pipe;
72419203 8950
eb14cb74
VS
8951 if (INTEL_INFO(dev)->gen >= 5) {
8952 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8953 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8954 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8955 & ~TU_SIZE_MASK;
8956 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8957 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8958 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
b95af8be
VK
8959 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8960 * gen < 8) and if DRRS is supported (to make sure the
8961 * registers are not unnecessarily read).
8962 */
8963 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
6e3c9717 8964 crtc->config->has_drrs) {
b95af8be
VK
8965 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8966 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8967 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8968 & ~TU_SIZE_MASK;
8969 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8970 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8971 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8972 }
eb14cb74
VS
8973 } else {
8974 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8975 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8976 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8977 & ~TU_SIZE_MASK;
8978 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8979 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8980 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8981 }
8982}
8983
8984void intel_dp_get_m_n(struct intel_crtc *crtc,
5cec258b 8985 struct intel_crtc_state *pipe_config)
eb14cb74 8986{
681a8504 8987 if (pipe_config->has_pch_encoder)
eb14cb74
VS
8988 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8989 else
8990 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be
VK
8991 &pipe_config->dp_m_n,
8992 &pipe_config->dp_m2_n2);
eb14cb74 8993}
72419203 8994
eb14cb74 8995static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5cec258b 8996 struct intel_crtc_state *pipe_config)
eb14cb74
VS
8997{
8998 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be 8999 &pipe_config->fdi_m_n, NULL);
72419203
DV
9000}
9001
bd2e244f 9002static void skylake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9003 struct intel_crtc_state *pipe_config)
bd2e244f
JB
9004{
9005 struct drm_device *dev = crtc->base.dev;
9006 struct drm_i915_private *dev_priv = dev->dev_private;
a1b2278e
CK
9007 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9008 uint32_t ps_ctrl = 0;
9009 int id = -1;
9010 int i;
bd2e244f 9011
a1b2278e
CK
9012 /* find scaler attached to this pipe */
9013 for (i = 0; i < crtc->num_scalers; i++) {
9014 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9015 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9016 id = i;
9017 pipe_config->pch_pfit.enabled = true;
9018 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9019 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9020 break;
9021 }
9022 }
bd2e244f 9023
a1b2278e
CK
9024 scaler_state->scaler_id = id;
9025 if (id >= 0) {
9026 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9027 } else {
9028 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
bd2e244f
JB
9029 }
9030}
9031
5724dbd1
DL
9032static void
9033skylake_get_initial_plane_config(struct intel_crtc *crtc,
9034 struct intel_initial_plane_config *plane_config)
bc8d7dff
DL
9035{
9036 struct drm_device *dev = crtc->base.dev;
9037 struct drm_i915_private *dev_priv = dev->dev_private;
40f46283 9038 u32 val, base, offset, stride_mult, tiling;
bc8d7dff
DL
9039 int pipe = crtc->pipe;
9040 int fourcc, pixel_format;
6761dd31 9041 unsigned int aligned_height;
bc8d7dff 9042 struct drm_framebuffer *fb;
1b842c89 9043 struct intel_framebuffer *intel_fb;
bc8d7dff 9044
d9806c9f 9045 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9046 if (!intel_fb) {
bc8d7dff
DL
9047 DRM_DEBUG_KMS("failed to alloc fb\n");
9048 return;
9049 }
9050
1b842c89
DL
9051 fb = &intel_fb->base;
9052
bc8d7dff 9053 val = I915_READ(PLANE_CTL(pipe, 0));
42a7b088
DL
9054 if (!(val & PLANE_CTL_ENABLE))
9055 goto error;
9056
bc8d7dff
DL
9057 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9058 fourcc = skl_format_to_fourcc(pixel_format,
9059 val & PLANE_CTL_ORDER_RGBX,
9060 val & PLANE_CTL_ALPHA_MASK);
9061 fb->pixel_format = fourcc;
9062 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9063
40f46283
DL
9064 tiling = val & PLANE_CTL_TILED_MASK;
9065 switch (tiling) {
9066 case PLANE_CTL_TILED_LINEAR:
9067 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9068 break;
9069 case PLANE_CTL_TILED_X:
9070 plane_config->tiling = I915_TILING_X;
9071 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9072 break;
9073 case PLANE_CTL_TILED_Y:
9074 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9075 break;
9076 case PLANE_CTL_TILED_YF:
9077 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9078 break;
9079 default:
9080 MISSING_CASE(tiling);
9081 goto error;
9082 }
9083
bc8d7dff
DL
9084 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9085 plane_config->base = base;
9086
9087 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9088
9089 val = I915_READ(PLANE_SIZE(pipe, 0));
9090 fb->height = ((val >> 16) & 0xfff) + 1;
9091 fb->width = ((val >> 0) & 0x1fff) + 1;
9092
9093 val = I915_READ(PLANE_STRIDE(pipe, 0));
7b49f948 9094 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
40f46283 9095 fb->pixel_format);
bc8d7dff
DL
9096 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9097
9098 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
9099 fb->pixel_format,
9100 fb->modifier[0]);
bc8d7dff 9101
f37b5c2b 9102 plane_config->size = fb->pitches[0] * aligned_height;
bc8d7dff
DL
9103
9104 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9105 pipe_name(pipe), fb->width, fb->height,
9106 fb->bits_per_pixel, base, fb->pitches[0],
9107 plane_config->size);
9108
2d14030b 9109 plane_config->fb = intel_fb;
bc8d7dff
DL
9110 return;
9111
9112error:
9113 kfree(fb);
9114}
9115
2fa2fe9a 9116static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9117 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
9118{
9119 struct drm_device *dev = crtc->base.dev;
9120 struct drm_i915_private *dev_priv = dev->dev_private;
9121 uint32_t tmp;
9122
9123 tmp = I915_READ(PF_CTL(crtc->pipe));
9124
9125 if (tmp & PF_ENABLE) {
fd4daa9c 9126 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
DV
9127 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9128 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
DV
9129
9130 /* We currently do not free assignements of panel fitters on
9131 * ivb/hsw (since we don't use the higher upscaling modes which
9132 * differentiates them) so just WARN about this case for now. */
9133 if (IS_GEN7(dev)) {
9134 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9135 PF_PIPE_SEL_IVB(crtc->pipe));
9136 }
2fa2fe9a 9137 }
79e53945
JB
9138}
9139
5724dbd1
DL
9140static void
9141ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9142 struct intel_initial_plane_config *plane_config)
4c6baa59
JB
9143{
9144 struct drm_device *dev = crtc->base.dev;
9145 struct drm_i915_private *dev_priv = dev->dev_private;
9146 u32 val, base, offset;
aeee5a49 9147 int pipe = crtc->pipe;
4c6baa59 9148 int fourcc, pixel_format;
6761dd31 9149 unsigned int aligned_height;
b113d5ee 9150 struct drm_framebuffer *fb;
1b842c89 9151 struct intel_framebuffer *intel_fb;
4c6baa59 9152
42a7b088
DL
9153 val = I915_READ(DSPCNTR(pipe));
9154 if (!(val & DISPLAY_PLANE_ENABLE))
9155 return;
9156
d9806c9f 9157 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9158 if (!intel_fb) {
4c6baa59
JB
9159 DRM_DEBUG_KMS("failed to alloc fb\n");
9160 return;
9161 }
9162
1b842c89
DL
9163 fb = &intel_fb->base;
9164
18c5247e
DV
9165 if (INTEL_INFO(dev)->gen >= 4) {
9166 if (val & DISPPLANE_TILED) {
49af449b 9167 plane_config->tiling = I915_TILING_X;
18c5247e
DV
9168 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9169 }
9170 }
4c6baa59
JB
9171
9172 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 9173 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
9174 fb->pixel_format = fourcc;
9175 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
4c6baa59 9176
aeee5a49 9177 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
4c6baa59 9178 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
aeee5a49 9179 offset = I915_READ(DSPOFFSET(pipe));
4c6baa59 9180 } else {
49af449b 9181 if (plane_config->tiling)
aeee5a49 9182 offset = I915_READ(DSPTILEOFF(pipe));
4c6baa59 9183 else
aeee5a49 9184 offset = I915_READ(DSPLINOFF(pipe));
4c6baa59
JB
9185 }
9186 plane_config->base = base;
9187
9188 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
9189 fb->width = ((val >> 16) & 0xfff) + 1;
9190 fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
9191
9192 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 9193 fb->pitches[0] = val & 0xffffffc0;
4c6baa59 9194
b113d5ee 9195 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
9196 fb->pixel_format,
9197 fb->modifier[0]);
4c6baa59 9198
f37b5c2b 9199 plane_config->size = fb->pitches[0] * aligned_height;
4c6baa59 9200
2844a921
DL
9201 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9202 pipe_name(pipe), fb->width, fb->height,
9203 fb->bits_per_pixel, base, fb->pitches[0],
9204 plane_config->size);
b113d5ee 9205
2d14030b 9206 plane_config->fb = intel_fb;
4c6baa59
JB
9207}
9208
0e8ffe1b 9209static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9210 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
9211{
9212 struct drm_device *dev = crtc->base.dev;
9213 struct drm_i915_private *dev_priv = dev->dev_private;
1729050e 9214 enum intel_display_power_domain power_domain;
0e8ffe1b 9215 uint32_t tmp;
1729050e 9216 bool ret;
0e8ffe1b 9217
1729050e
ID
9218 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9219 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
930e8c9e
PZ
9220 return false;
9221
e143a21c 9222 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 9223 pipe_config->shared_dpll = NULL;
eccb140b 9224
1729050e 9225 ret = false;
0e8ffe1b
DV
9226 tmp = I915_READ(PIPECONF(crtc->pipe));
9227 if (!(tmp & PIPECONF_ENABLE))
1729050e 9228 goto out;
0e8ffe1b 9229
42571aef
VS
9230 switch (tmp & PIPECONF_BPC_MASK) {
9231 case PIPECONF_6BPC:
9232 pipe_config->pipe_bpp = 18;
9233 break;
9234 case PIPECONF_8BPC:
9235 pipe_config->pipe_bpp = 24;
9236 break;
9237 case PIPECONF_10BPC:
9238 pipe_config->pipe_bpp = 30;
9239 break;
9240 case PIPECONF_12BPC:
9241 pipe_config->pipe_bpp = 36;
9242 break;
9243 default:
9244 break;
9245 }
9246
b5a9fa09
DV
9247 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9248 pipe_config->limited_color_range = true;
9249
ab9412ba 9250 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0 9251 struct intel_shared_dpll *pll;
8106ddbd 9252 enum intel_dpll_id pll_id;
66e985c0 9253
88adfff1
DV
9254 pipe_config->has_pch_encoder = true;
9255
627eb5a3
DV
9256 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9257 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9258 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
9259
9260 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 9261
c0d43d62 9262 if (HAS_PCH_IBX(dev_priv->dev)) {
8106ddbd 9263 pll_id = (enum intel_dpll_id) crtc->pipe;
c0d43d62
DV
9264 } else {
9265 tmp = I915_READ(PCH_DPLL_SEL);
9266 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
8106ddbd 9267 pll_id = DPLL_ID_PCH_PLL_B;
c0d43d62 9268 else
8106ddbd 9269 pll_id= DPLL_ID_PCH_PLL_A;
c0d43d62 9270 }
66e985c0 9271
8106ddbd
ACO
9272 pipe_config->shared_dpll =
9273 intel_get_shared_dpll_by_id(dev_priv, pll_id);
9274 pll = pipe_config->shared_dpll;
66e985c0 9275
2edd6443
ACO
9276 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9277 &pipe_config->dpll_hw_state));
c93f54cf
DV
9278
9279 tmp = pipe_config->dpll_hw_state.dpll;
9280 pipe_config->pixel_multiplier =
9281 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9282 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
9283
9284 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
DV
9285 } else {
9286 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
9287 }
9288
1bd1bd80 9289 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 9290 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 9291
2fa2fe9a
DV
9292 ironlake_get_pfit_config(crtc, pipe_config);
9293
1729050e
ID
9294 ret = true;
9295
9296out:
9297 intel_display_power_put(dev_priv, power_domain);
9298
9299 return ret;
0e8ffe1b
DV
9300}
9301
be256dc7
PZ
9302static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9303{
9304 struct drm_device *dev = dev_priv->dev;
be256dc7 9305 struct intel_crtc *crtc;
be256dc7 9306
d3fcc808 9307 for_each_intel_crtc(dev, crtc)
e2c719b7 9308 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
9309 pipe_name(crtc->pipe));
9310
e2c719b7
RC
9311 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9312 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
01403de3
VS
9313 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9314 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
e2c719b7
RC
9315 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9316 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
be256dc7 9317 "CPU PWM1 enabled\n");
c5107b87 9318 if (IS_HASWELL(dev))
e2c719b7 9319 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
c5107b87 9320 "CPU PWM2 enabled\n");
e2c719b7 9321 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
be256dc7 9322 "PCH PWM1 enabled\n");
e2c719b7 9323 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
be256dc7 9324 "Utility pin enabled\n");
e2c719b7 9325 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
be256dc7 9326
9926ada1
PZ
9327 /*
9328 * In theory we can still leave IRQs enabled, as long as only the HPD
9329 * interrupts remain enabled. We used to check for that, but since it's
9330 * gen-specific and since we only disable LCPLL after we fully disable
9331 * the interrupts, the check below should be enough.
9332 */
e2c719b7 9333 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
be256dc7
PZ
9334}
9335
9ccd5aeb
PZ
9336static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9337{
9338 struct drm_device *dev = dev_priv->dev;
9339
9340 if (IS_HASWELL(dev))
9341 return I915_READ(D_COMP_HSW);
9342 else
9343 return I915_READ(D_COMP_BDW);
9344}
9345
3c4c9b81
PZ
9346static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9347{
9348 struct drm_device *dev = dev_priv->dev;
9349
9350 if (IS_HASWELL(dev)) {
9351 mutex_lock(&dev_priv->rps.hw_lock);
9352 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9353 val))
f475dadf 9354 DRM_ERROR("Failed to write to D_COMP\n");
3c4c9b81
PZ
9355 mutex_unlock(&dev_priv->rps.hw_lock);
9356 } else {
9ccd5aeb
PZ
9357 I915_WRITE(D_COMP_BDW, val);
9358 POSTING_READ(D_COMP_BDW);
3c4c9b81 9359 }
be256dc7
PZ
9360}
9361
9362/*
9363 * This function implements pieces of two sequences from BSpec:
9364 * - Sequence for display software to disable LCPLL
9365 * - Sequence for display software to allow package C8+
9366 * The steps implemented here are just the steps that actually touch the LCPLL
9367 * register. Callers should take care of disabling all the display engine
9368 * functions, doing the mode unset, fixing interrupts, etc.
9369 */
6ff58d53
PZ
9370static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9371 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
9372{
9373 uint32_t val;
9374
9375 assert_can_disable_lcpll(dev_priv);
9376
9377 val = I915_READ(LCPLL_CTL);
9378
9379 if (switch_to_fclk) {
9380 val |= LCPLL_CD_SOURCE_FCLK;
9381 I915_WRITE(LCPLL_CTL, val);
9382
9383 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9384 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9385 DRM_ERROR("Switching to FCLK failed\n");
9386
9387 val = I915_READ(LCPLL_CTL);
9388 }
9389
9390 val |= LCPLL_PLL_DISABLE;
9391 I915_WRITE(LCPLL_CTL, val);
9392 POSTING_READ(LCPLL_CTL);
9393
9394 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9395 DRM_ERROR("LCPLL still locked\n");
9396
9ccd5aeb 9397 val = hsw_read_dcomp(dev_priv);
be256dc7 9398 val |= D_COMP_COMP_DISABLE;
3c4c9b81 9399 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9400 ndelay(100);
9401
9ccd5aeb
PZ
9402 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9403 1))
be256dc7
PZ
9404 DRM_ERROR("D_COMP RCOMP still in progress\n");
9405
9406 if (allow_power_down) {
9407 val = I915_READ(LCPLL_CTL);
9408 val |= LCPLL_POWER_DOWN_ALLOW;
9409 I915_WRITE(LCPLL_CTL, val);
9410 POSTING_READ(LCPLL_CTL);
9411 }
9412}
9413
9414/*
9415 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9416 * source.
9417 */
6ff58d53 9418static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
9419{
9420 uint32_t val;
9421
9422 val = I915_READ(LCPLL_CTL);
9423
9424 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9425 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9426 return;
9427
a8a8bd54
PZ
9428 /*
9429 * Make sure we're not on PC8 state before disabling PC8, otherwise
9430 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
a8a8bd54 9431 */
59bad947 9432 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
215733fa 9433
be256dc7
PZ
9434 if (val & LCPLL_POWER_DOWN_ALLOW) {
9435 val &= ~LCPLL_POWER_DOWN_ALLOW;
9436 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 9437 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
9438 }
9439
9ccd5aeb 9440 val = hsw_read_dcomp(dev_priv);
be256dc7
PZ
9441 val |= D_COMP_COMP_FORCE;
9442 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 9443 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9444
9445 val = I915_READ(LCPLL_CTL);
9446 val &= ~LCPLL_PLL_DISABLE;
9447 I915_WRITE(LCPLL_CTL, val);
9448
9449 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9450 DRM_ERROR("LCPLL not locked yet\n");
9451
9452 if (val & LCPLL_CD_SOURCE_FCLK) {
9453 val = I915_READ(LCPLL_CTL);
9454 val &= ~LCPLL_CD_SOURCE_FCLK;
9455 I915_WRITE(LCPLL_CTL, val);
9456
9457 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9458 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9459 DRM_ERROR("Switching back to LCPLL failed\n");
9460 }
215733fa 9461
59bad947 9462 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
b6283055 9463 intel_update_cdclk(dev_priv->dev);
be256dc7
PZ
9464}
9465
765dab67
PZ
9466/*
9467 * Package states C8 and deeper are really deep PC states that can only be
9468 * reached when all the devices on the system allow it, so even if the graphics
9469 * device allows PC8+, it doesn't mean the system will actually get to these
9470 * states. Our driver only allows PC8+ when going into runtime PM.
9471 *
9472 * The requirements for PC8+ are that all the outputs are disabled, the power
9473 * well is disabled and most interrupts are disabled, and these are also
9474 * requirements for runtime PM. When these conditions are met, we manually do
9475 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9476 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9477 * hang the machine.
9478 *
9479 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9480 * the state of some registers, so when we come back from PC8+ we need to
9481 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9482 * need to take care of the registers kept by RC6. Notice that this happens even
9483 * if we don't put the device in PCI D3 state (which is what currently happens
9484 * because of the runtime PM support).
9485 *
9486 * For more, read "Display Sequences for Package C8" on the hardware
9487 * documentation.
9488 */
a14cb6fc 9489void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 9490{
c67a470b
PZ
9491 struct drm_device *dev = dev_priv->dev;
9492 uint32_t val;
9493
c67a470b
PZ
9494 DRM_DEBUG_KMS("Enabling package C8+\n");
9495
c2699524 9496 if (HAS_PCH_LPT_LP(dev)) {
c67a470b
PZ
9497 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9498 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9499 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9500 }
9501
9502 lpt_disable_clkout_dp(dev);
c67a470b
PZ
9503 hsw_disable_lcpll(dev_priv, true, true);
9504}
9505
a14cb6fc 9506void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b
PZ
9507{
9508 struct drm_device *dev = dev_priv->dev;
9509 uint32_t val;
9510
c67a470b
PZ
9511 DRM_DEBUG_KMS("Disabling package C8+\n");
9512
9513 hsw_restore_lcpll(dev_priv);
c67a470b
PZ
9514 lpt_init_pch_refclk(dev);
9515
c2699524 9516 if (HAS_PCH_LPT_LP(dev)) {
c67a470b
PZ
9517 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9518 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9519 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9520 }
c67a470b
PZ
9521}
9522
27c329ed 9523static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
f8437dd1 9524{
a821fc46 9525 struct drm_device *dev = old_state->dev;
1a617b77
ML
9526 struct intel_atomic_state *old_intel_state =
9527 to_intel_atomic_state(old_state);
9528 unsigned int req_cdclk = old_intel_state->dev_cdclk;
f8437dd1 9529
27c329ed 9530 broxton_set_cdclk(dev, req_cdclk);
f8437dd1
VK
9531}
9532
b432e5cf 9533/* compute the max rate for new configuration */
27c329ed 9534static int ilk_max_pixel_rate(struct drm_atomic_state *state)
b432e5cf 9535{
565602d7
ML
9536 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
9537 struct drm_i915_private *dev_priv = state->dev->dev_private;
9538 struct drm_crtc *crtc;
9539 struct drm_crtc_state *cstate;
27c329ed 9540 struct intel_crtc_state *crtc_state;
565602d7
ML
9541 unsigned max_pixel_rate = 0, i;
9542 enum pipe pipe;
b432e5cf 9543
565602d7
ML
9544 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
9545 sizeof(intel_state->min_pixclk));
27c329ed 9546
565602d7
ML
9547 for_each_crtc_in_state(state, crtc, cstate, i) {
9548 int pixel_rate;
27c329ed 9549
565602d7
ML
9550 crtc_state = to_intel_crtc_state(cstate);
9551 if (!crtc_state->base.enable) {
9552 intel_state->min_pixclk[i] = 0;
b432e5cf 9553 continue;
565602d7 9554 }
b432e5cf 9555
27c329ed 9556 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
b432e5cf
VS
9557
9558 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
565602d7 9559 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
b432e5cf
VS
9560 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9561
565602d7 9562 intel_state->min_pixclk[i] = pixel_rate;
b432e5cf
VS
9563 }
9564
565602d7
ML
9565 for_each_pipe(dev_priv, pipe)
9566 max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
9567
b432e5cf
VS
9568 return max_pixel_rate;
9569}
9570
9571static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9572{
9573 struct drm_i915_private *dev_priv = dev->dev_private;
9574 uint32_t val, data;
9575 int ret;
9576
9577 if (WARN((I915_READ(LCPLL_CTL) &
9578 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9579 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9580 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9581 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9582 "trying to change cdclk frequency with cdclk not enabled\n"))
9583 return;
9584
9585 mutex_lock(&dev_priv->rps.hw_lock);
9586 ret = sandybridge_pcode_write(dev_priv,
9587 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9588 mutex_unlock(&dev_priv->rps.hw_lock);
9589 if (ret) {
9590 DRM_ERROR("failed to inform pcode about cdclk change\n");
9591 return;
9592 }
9593
9594 val = I915_READ(LCPLL_CTL);
9595 val |= LCPLL_CD_SOURCE_FCLK;
9596 I915_WRITE(LCPLL_CTL, val);
9597
5ba00178
TU
9598 if (wait_for_us(I915_READ(LCPLL_CTL) &
9599 LCPLL_CD_SOURCE_FCLK_DONE, 1))
b432e5cf
VS
9600 DRM_ERROR("Switching to FCLK failed\n");
9601
9602 val = I915_READ(LCPLL_CTL);
9603 val &= ~LCPLL_CLK_FREQ_MASK;
9604
9605 switch (cdclk) {
9606 case 450000:
9607 val |= LCPLL_CLK_FREQ_450;
9608 data = 0;
9609 break;
9610 case 540000:
9611 val |= LCPLL_CLK_FREQ_54O_BDW;
9612 data = 1;
9613 break;
9614 case 337500:
9615 val |= LCPLL_CLK_FREQ_337_5_BDW;
9616 data = 2;
9617 break;
9618 case 675000:
9619 val |= LCPLL_CLK_FREQ_675_BDW;
9620 data = 3;
9621 break;
9622 default:
9623 WARN(1, "invalid cdclk frequency\n");
9624 return;
9625 }
9626
9627 I915_WRITE(LCPLL_CTL, val);
9628
9629 val = I915_READ(LCPLL_CTL);
9630 val &= ~LCPLL_CD_SOURCE_FCLK;
9631 I915_WRITE(LCPLL_CTL, val);
9632
5ba00178
TU
9633 if (wait_for_us((I915_READ(LCPLL_CTL) &
9634 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
b432e5cf
VS
9635 DRM_ERROR("Switching back to LCPLL failed\n");
9636
9637 mutex_lock(&dev_priv->rps.hw_lock);
9638 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9639 mutex_unlock(&dev_priv->rps.hw_lock);
9640
9641 intel_update_cdclk(dev);
9642
9643 WARN(cdclk != dev_priv->cdclk_freq,
9644 "cdclk requested %d kHz but got %d kHz\n",
9645 cdclk, dev_priv->cdclk_freq);
9646}
9647
27c329ed 9648static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
b432e5cf 9649{
27c329ed 9650 struct drm_i915_private *dev_priv = to_i915(state->dev);
1a617b77 9651 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
27c329ed 9652 int max_pixclk = ilk_max_pixel_rate(state);
b432e5cf
VS
9653 int cdclk;
9654
9655 /*
9656 * FIXME should also account for plane ratio
9657 * once 64bpp pixel formats are supported.
9658 */
27c329ed 9659 if (max_pixclk > 540000)
b432e5cf 9660 cdclk = 675000;
27c329ed 9661 else if (max_pixclk > 450000)
b432e5cf 9662 cdclk = 540000;
27c329ed 9663 else if (max_pixclk > 337500)
b432e5cf
VS
9664 cdclk = 450000;
9665 else
9666 cdclk = 337500;
9667
b432e5cf 9668 if (cdclk > dev_priv->max_cdclk_freq) {
63ba534e
ML
9669 DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9670 cdclk, dev_priv->max_cdclk_freq);
9671 return -EINVAL;
b432e5cf
VS
9672 }
9673
1a617b77
ML
9674 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9675 if (!intel_state->active_crtcs)
9676 intel_state->dev_cdclk = 337500;
b432e5cf
VS
9677
9678 return 0;
9679}
9680
27c329ed 9681static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
b432e5cf 9682{
27c329ed 9683 struct drm_device *dev = old_state->dev;
1a617b77
ML
9684 struct intel_atomic_state *old_intel_state =
9685 to_intel_atomic_state(old_state);
9686 unsigned req_cdclk = old_intel_state->dev_cdclk;
b432e5cf 9687
27c329ed 9688 broadwell_set_cdclk(dev, req_cdclk);
b432e5cf
VS
9689}
9690
190f68c5
ACO
9691static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9692 struct intel_crtc_state *crtc_state)
09b4ddf9 9693{
af3997b5
MK
9694 struct intel_encoder *intel_encoder =
9695 intel_ddi_get_crtc_new_encoder(crtc_state);
9696
9697 if (intel_encoder->type != INTEL_OUTPUT_DSI) {
9698 if (!intel_ddi_pll_select(crtc, crtc_state))
9699 return -EINVAL;
9700 }
716c2e55 9701
c7653199 9702 crtc->lowfreq_avail = false;
644cef34 9703
c8f7a0db 9704 return 0;
79e53945
JB
9705}
9706
3760b59c
S
9707static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9708 enum port port,
9709 struct intel_crtc_state *pipe_config)
9710{
8106ddbd
ACO
9711 enum intel_dpll_id id;
9712
3760b59c
S
9713 switch (port) {
9714 case PORT_A:
9715 pipe_config->ddi_pll_sel = SKL_DPLL0;
08250c4b 9716 id = DPLL_ID_SKL_DPLL0;
3760b59c
S
9717 break;
9718 case PORT_B:
9719 pipe_config->ddi_pll_sel = SKL_DPLL1;
08250c4b 9720 id = DPLL_ID_SKL_DPLL1;
3760b59c
S
9721 break;
9722 case PORT_C:
9723 pipe_config->ddi_pll_sel = SKL_DPLL2;
08250c4b 9724 id = DPLL_ID_SKL_DPLL2;
3760b59c
S
9725 break;
9726 default:
9727 DRM_ERROR("Incorrect port type\n");
8106ddbd 9728 return;
3760b59c 9729 }
8106ddbd
ACO
9730
9731 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
3760b59c
S
9732}
9733
96b7dfb7
S
9734static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9735 enum port port,
5cec258b 9736 struct intel_crtc_state *pipe_config)
96b7dfb7 9737{
8106ddbd 9738 enum intel_dpll_id id;
a3c988ea 9739 u32 temp;
96b7dfb7
S
9740
9741 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9742 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9743
9744 switch (pipe_config->ddi_pll_sel) {
3148ade7 9745 case SKL_DPLL0:
a3c988ea
ACO
9746 id = DPLL_ID_SKL_DPLL0;
9747 break;
96b7dfb7 9748 case SKL_DPLL1:
8106ddbd 9749 id = DPLL_ID_SKL_DPLL1;
96b7dfb7
S
9750 break;
9751 case SKL_DPLL2:
8106ddbd 9752 id = DPLL_ID_SKL_DPLL2;
96b7dfb7
S
9753 break;
9754 case SKL_DPLL3:
8106ddbd 9755 id = DPLL_ID_SKL_DPLL3;
96b7dfb7 9756 break;
8106ddbd
ACO
9757 default:
9758 MISSING_CASE(pipe_config->ddi_pll_sel);
9759 return;
96b7dfb7 9760 }
8106ddbd
ACO
9761
9762 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
96b7dfb7
S
9763}
9764
7d2c8175
DL
9765static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9766 enum port port,
5cec258b 9767 struct intel_crtc_state *pipe_config)
7d2c8175 9768{
8106ddbd
ACO
9769 enum intel_dpll_id id;
9770
7d2c8175
DL
9771 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9772
9773 switch (pipe_config->ddi_pll_sel) {
9774 case PORT_CLK_SEL_WRPLL1:
8106ddbd 9775 id = DPLL_ID_WRPLL1;
7d2c8175
DL
9776 break;
9777 case PORT_CLK_SEL_WRPLL2:
8106ddbd 9778 id = DPLL_ID_WRPLL2;
7d2c8175 9779 break;
00490c22 9780 case PORT_CLK_SEL_SPLL:
8106ddbd 9781 id = DPLL_ID_SPLL;
79bd23da 9782 break;
9d16da65
ACO
9783 case PORT_CLK_SEL_LCPLL_810:
9784 id = DPLL_ID_LCPLL_810;
9785 break;
9786 case PORT_CLK_SEL_LCPLL_1350:
9787 id = DPLL_ID_LCPLL_1350;
9788 break;
9789 case PORT_CLK_SEL_LCPLL_2700:
9790 id = DPLL_ID_LCPLL_2700;
9791 break;
8106ddbd
ACO
9792 default:
9793 MISSING_CASE(pipe_config->ddi_pll_sel);
9794 /* fall through */
9795 case PORT_CLK_SEL_NONE:
8106ddbd 9796 return;
7d2c8175 9797 }
8106ddbd
ACO
9798
9799 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
7d2c8175
DL
9800}
9801
cf30429e
JN
9802static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
9803 struct intel_crtc_state *pipe_config,
9804 unsigned long *power_domain_mask)
9805{
9806 struct drm_device *dev = crtc->base.dev;
9807 struct drm_i915_private *dev_priv = dev->dev_private;
9808 enum intel_display_power_domain power_domain;
9809 u32 tmp;
9810
9811 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
9812
9813 /*
9814 * XXX: Do intel_display_power_get_if_enabled before reading this (for
9815 * consistency and less surprising code; it's in always on power).
9816 */
9817 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9818 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9819 enum pipe trans_edp_pipe;
9820 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9821 default:
9822 WARN(1, "unknown pipe linked to edp transcoder\n");
9823 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9824 case TRANS_DDI_EDP_INPUT_A_ON:
9825 trans_edp_pipe = PIPE_A;
9826 break;
9827 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9828 trans_edp_pipe = PIPE_B;
9829 break;
9830 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9831 trans_edp_pipe = PIPE_C;
9832 break;
9833 }
9834
9835 if (trans_edp_pipe == crtc->pipe)
9836 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9837 }
9838
9839 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
9840 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9841 return false;
9842 *power_domain_mask |= BIT(power_domain);
9843
9844 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
9845
9846 return tmp & PIPECONF_ENABLE;
9847}
9848
4d1de975
JN
9849static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
9850 struct intel_crtc_state *pipe_config,
9851 unsigned long *power_domain_mask)
9852{
9853 struct drm_device *dev = crtc->base.dev;
9854 struct drm_i915_private *dev_priv = dev->dev_private;
9855 enum intel_display_power_domain power_domain;
9856 enum port port;
9857 enum transcoder cpu_transcoder;
9858 u32 tmp;
9859
9860 pipe_config->has_dsi_encoder = false;
9861
9862 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
9863 if (port == PORT_A)
9864 cpu_transcoder = TRANSCODER_DSI_A;
9865 else
9866 cpu_transcoder = TRANSCODER_DSI_C;
9867
9868 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
9869 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9870 continue;
9871 *power_domain_mask |= BIT(power_domain);
9872
9873 /* XXX: this works for video mode only */
9874 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
9875 if (!(tmp & DPI_ENABLE))
9876 continue;
9877
9878 tmp = I915_READ(MIPI_CTRL(port));
9879 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
9880 continue;
9881
9882 pipe_config->cpu_transcoder = cpu_transcoder;
9883 pipe_config->has_dsi_encoder = true;
9884 break;
9885 }
9886
9887 return pipe_config->has_dsi_encoder;
9888}
9889
26804afd 9890static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
5cec258b 9891 struct intel_crtc_state *pipe_config)
26804afd
DV
9892{
9893 struct drm_device *dev = crtc->base.dev;
9894 struct drm_i915_private *dev_priv = dev->dev_private;
d452c5b6 9895 struct intel_shared_dpll *pll;
26804afd
DV
9896 enum port port;
9897 uint32_t tmp;
9898
9899 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9900
9901 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9902
ef11bdb3 9903 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
96b7dfb7 9904 skylake_get_ddi_pll(dev_priv, port, pipe_config);
3760b59c
S
9905 else if (IS_BROXTON(dev))
9906 bxt_get_ddi_pll(dev_priv, port, pipe_config);
96b7dfb7
S
9907 else
9908 haswell_get_ddi_pll(dev_priv, port, pipe_config);
9cd86933 9909
8106ddbd
ACO
9910 pll = pipe_config->shared_dpll;
9911 if (pll) {
2edd6443
ACO
9912 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9913 &pipe_config->dpll_hw_state));
d452c5b6
DV
9914 }
9915
26804afd
DV
9916 /*
9917 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9918 * DDI E. So just check whether this pipe is wired to DDI E and whether
9919 * the PCH transcoder is on.
9920 */
ca370455
DL
9921 if (INTEL_INFO(dev)->gen < 9 &&
9922 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
26804afd
DV
9923 pipe_config->has_pch_encoder = true;
9924
9925 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9926 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9927 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9928
9929 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9930 }
9931}
9932
0e8ffe1b 9933static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9934 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
9935{
9936 struct drm_device *dev = crtc->base.dev;
9937 struct drm_i915_private *dev_priv = dev->dev_private;
1729050e
ID
9938 enum intel_display_power_domain power_domain;
9939 unsigned long power_domain_mask;
cf30429e 9940 bool active;
0e8ffe1b 9941
1729050e
ID
9942 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9943 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0 9944 return false;
1729050e
ID
9945 power_domain_mask = BIT(power_domain);
9946
8106ddbd 9947 pipe_config->shared_dpll = NULL;
c0d43d62 9948
cf30429e 9949 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
eccb140b 9950
4d1de975
JN
9951 if (IS_BROXTON(dev_priv)) {
9952 bxt_get_dsi_transcoder_state(crtc, pipe_config,
9953 &power_domain_mask);
9954 WARN_ON(active && pipe_config->has_dsi_encoder);
9955 if (pipe_config->has_dsi_encoder)
9956 active = true;
9957 }
9958
cf30429e 9959 if (!active)
1729050e 9960 goto out;
0e8ffe1b 9961
4d1de975
JN
9962 if (!pipe_config->has_dsi_encoder) {
9963 haswell_get_ddi_port_state(crtc, pipe_config);
9964 intel_get_pipe_timings(crtc, pipe_config);
9965 }
627eb5a3 9966
bc58be60 9967 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 9968
05dc698c
LL
9969 pipe_config->gamma_mode =
9970 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
9971
a1b2278e
CK
9972 if (INTEL_INFO(dev)->gen >= 9) {
9973 skl_init_scalers(dev, crtc, pipe_config);
9974 }
9975
af99ceda
CK
9976 if (INTEL_INFO(dev)->gen >= 9) {
9977 pipe_config->scaler_state.scaler_id = -1;
9978 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9979 }
9980
1729050e
ID
9981 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
9982 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
9983 power_domain_mask |= BIT(power_domain);
1c132b44 9984 if (INTEL_INFO(dev)->gen >= 9)
bd2e244f 9985 skylake_get_pfit_config(crtc, pipe_config);
ff6d9f55 9986 else
1c132b44 9987 ironlake_get_pfit_config(crtc, pipe_config);
bd2e244f 9988 }
88adfff1 9989
e59150dc
JB
9990 if (IS_HASWELL(dev))
9991 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9992 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 9993
4d1de975
JN
9994 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
9995 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
ebb69c95
CT
9996 pipe_config->pixel_multiplier =
9997 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9998 } else {
9999 pipe_config->pixel_multiplier = 1;
10000 }
6c49f241 10001
1729050e
ID
10002out:
10003 for_each_power_domain(power_domain, power_domain_mask)
10004 intel_display_power_put(dev_priv, power_domain);
10005
cf30429e 10006 return active;
0e8ffe1b
DV
10007}
10008
55a08b3f
ML
10009static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
10010 const struct intel_plane_state *plane_state)
560b85bb
CW
10011{
10012 struct drm_device *dev = crtc->dev;
10013 struct drm_i915_private *dev_priv = dev->dev_private;
10014 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
dc41c154 10015 uint32_t cntl = 0, size = 0;
560b85bb 10016
55a08b3f
ML
10017 if (plane_state && plane_state->visible) {
10018 unsigned int width = plane_state->base.crtc_w;
10019 unsigned int height = plane_state->base.crtc_h;
dc41c154
VS
10020 unsigned int stride = roundup_pow_of_two(width) * 4;
10021
10022 switch (stride) {
10023 default:
10024 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
10025 width, stride);
10026 stride = 256;
10027 /* fallthrough */
10028 case 256:
10029 case 512:
10030 case 1024:
10031 case 2048:
10032 break;
4b0e333e
CW
10033 }
10034
dc41c154
VS
10035 cntl |= CURSOR_ENABLE |
10036 CURSOR_GAMMA_ENABLE |
10037 CURSOR_FORMAT_ARGB |
10038 CURSOR_STRIDE(stride);
10039
10040 size = (height << 12) | width;
4b0e333e 10041 }
560b85bb 10042
dc41c154
VS
10043 if (intel_crtc->cursor_cntl != 0 &&
10044 (intel_crtc->cursor_base != base ||
10045 intel_crtc->cursor_size != size ||
10046 intel_crtc->cursor_cntl != cntl)) {
10047 /* On these chipsets we can only modify the base/size/stride
10048 * whilst the cursor is disabled.
10049 */
0b87c24e
VS
10050 I915_WRITE(CURCNTR(PIPE_A), 0);
10051 POSTING_READ(CURCNTR(PIPE_A));
dc41c154 10052 intel_crtc->cursor_cntl = 0;
4b0e333e 10053 }
560b85bb 10054
99d1f387 10055 if (intel_crtc->cursor_base != base) {
0b87c24e 10056 I915_WRITE(CURBASE(PIPE_A), base);
99d1f387
VS
10057 intel_crtc->cursor_base = base;
10058 }
4726e0b0 10059
dc41c154
VS
10060 if (intel_crtc->cursor_size != size) {
10061 I915_WRITE(CURSIZE, size);
10062 intel_crtc->cursor_size = size;
4b0e333e 10063 }
560b85bb 10064
4b0e333e 10065 if (intel_crtc->cursor_cntl != cntl) {
0b87c24e
VS
10066 I915_WRITE(CURCNTR(PIPE_A), cntl);
10067 POSTING_READ(CURCNTR(PIPE_A));
4b0e333e 10068 intel_crtc->cursor_cntl = cntl;
560b85bb 10069 }
560b85bb
CW
10070}
10071
55a08b3f
ML
10072static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
10073 const struct intel_plane_state *plane_state)
65a21cd6
JB
10074{
10075 struct drm_device *dev = crtc->dev;
10076 struct drm_i915_private *dev_priv = dev->dev_private;
10077 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10078 int pipe = intel_crtc->pipe;
663f3122 10079 uint32_t cntl = 0;
4b0e333e 10080
55a08b3f 10081 if (plane_state && plane_state->visible) {
4b0e333e 10082 cntl = MCURSOR_GAMMA_ENABLE;
55a08b3f 10083 switch (plane_state->base.crtc_w) {
4726e0b0
SK
10084 case 64:
10085 cntl |= CURSOR_MODE_64_ARGB_AX;
10086 break;
10087 case 128:
10088 cntl |= CURSOR_MODE_128_ARGB_AX;
10089 break;
10090 case 256:
10091 cntl |= CURSOR_MODE_256_ARGB_AX;
10092 break;
10093 default:
55a08b3f 10094 MISSING_CASE(plane_state->base.crtc_w);
4726e0b0 10095 return;
65a21cd6 10096 }
4b0e333e 10097 cntl |= pipe << 28; /* Connect to correct pipe */
47bf17a7 10098
fc6f93bc 10099 if (HAS_DDI(dev))
47bf17a7 10100 cntl |= CURSOR_PIPE_CSC_ENABLE;
65a21cd6 10101
55a08b3f
ML
10102 if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
10103 cntl |= CURSOR_ROTATE_180;
10104 }
4398ad45 10105
4b0e333e
CW
10106 if (intel_crtc->cursor_cntl != cntl) {
10107 I915_WRITE(CURCNTR(pipe), cntl);
10108 POSTING_READ(CURCNTR(pipe));
10109 intel_crtc->cursor_cntl = cntl;
65a21cd6 10110 }
4b0e333e 10111
65a21cd6 10112 /* and commit changes on next vblank */
5efb3e28
VS
10113 I915_WRITE(CURBASE(pipe), base);
10114 POSTING_READ(CURBASE(pipe));
99d1f387
VS
10115
10116 intel_crtc->cursor_base = base;
65a21cd6
JB
10117}
10118
cda4b7d3 10119/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6b383a7f 10120static void intel_crtc_update_cursor(struct drm_crtc *crtc,
55a08b3f 10121 const struct intel_plane_state *plane_state)
cda4b7d3
CW
10122{
10123 struct drm_device *dev = crtc->dev;
10124 struct drm_i915_private *dev_priv = dev->dev_private;
10125 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10126 int pipe = intel_crtc->pipe;
55a08b3f
ML
10127 u32 base = intel_crtc->cursor_addr;
10128 u32 pos = 0;
cda4b7d3 10129
55a08b3f
ML
10130 if (plane_state) {
10131 int x = plane_state->base.crtc_x;
10132 int y = plane_state->base.crtc_y;
cda4b7d3 10133
55a08b3f
ML
10134 if (x < 0) {
10135 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10136 x = -x;
10137 }
10138 pos |= x << CURSOR_X_SHIFT;
cda4b7d3 10139
55a08b3f
ML
10140 if (y < 0) {
10141 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10142 y = -y;
10143 }
10144 pos |= y << CURSOR_Y_SHIFT;
10145
10146 /* ILK+ do this automagically */
10147 if (HAS_GMCH_DISPLAY(dev) &&
10148 plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
10149 base += (plane_state->base.crtc_h *
10150 plane_state->base.crtc_w - 1) * 4;
10151 }
cda4b7d3 10152 }
cda4b7d3 10153
5efb3e28
VS
10154 I915_WRITE(CURPOS(pipe), pos);
10155
8ac54669 10156 if (IS_845G(dev) || IS_I865G(dev))
55a08b3f 10157 i845_update_cursor(crtc, base, plane_state);
5efb3e28 10158 else
55a08b3f 10159 i9xx_update_cursor(crtc, base, plane_state);
cda4b7d3
CW
10160}
10161
dc41c154
VS
10162static bool cursor_size_ok(struct drm_device *dev,
10163 uint32_t width, uint32_t height)
10164{
10165 if (width == 0 || height == 0)
10166 return false;
10167
10168 /*
10169 * 845g/865g are special in that they are only limited by
10170 * the width of their cursors, the height is arbitrary up to
10171 * the precision of the register. Everything else requires
10172 * square cursors, limited to a few power-of-two sizes.
10173 */
10174 if (IS_845G(dev) || IS_I865G(dev)) {
10175 if ((width & 63) != 0)
10176 return false;
10177
10178 if (width > (IS_845G(dev) ? 64 : 512))
10179 return false;
10180
10181 if (height > 1023)
10182 return false;
10183 } else {
10184 switch (width | height) {
10185 case 256:
10186 case 128:
10187 if (IS_GEN2(dev))
10188 return false;
10189 case 64:
10190 break;
10191 default:
10192 return false;
10193 }
10194 }
10195
10196 return true;
10197}
10198
79e53945
JB
10199/* VESA 640x480x72Hz mode to set on the pipe */
10200static struct drm_display_mode load_detect_mode = {
10201 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10202 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10203};
10204
a8bb6818
DV
10205struct drm_framebuffer *
10206__intel_framebuffer_create(struct drm_device *dev,
10207 struct drm_mode_fb_cmd2 *mode_cmd,
10208 struct drm_i915_gem_object *obj)
d2dff872
CW
10209{
10210 struct intel_framebuffer *intel_fb;
10211 int ret;
10212
10213 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
dcb1394e 10214 if (!intel_fb)
d2dff872 10215 return ERR_PTR(-ENOMEM);
d2dff872
CW
10216
10217 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
dd4916c5
DV
10218 if (ret)
10219 goto err;
d2dff872
CW
10220
10221 return &intel_fb->base;
dcb1394e 10222
dd4916c5 10223err:
dd4916c5 10224 kfree(intel_fb);
dd4916c5 10225 return ERR_PTR(ret);
d2dff872
CW
10226}
10227
b5ea642a 10228static struct drm_framebuffer *
a8bb6818
DV
10229intel_framebuffer_create(struct drm_device *dev,
10230 struct drm_mode_fb_cmd2 *mode_cmd,
10231 struct drm_i915_gem_object *obj)
10232{
10233 struct drm_framebuffer *fb;
10234 int ret;
10235
10236 ret = i915_mutex_lock_interruptible(dev);
10237 if (ret)
10238 return ERR_PTR(ret);
10239 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10240 mutex_unlock(&dev->struct_mutex);
10241
10242 return fb;
10243}
10244
d2dff872
CW
10245static u32
10246intel_framebuffer_pitch_for_width(int width, int bpp)
10247{
10248 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10249 return ALIGN(pitch, 64);
10250}
10251
10252static u32
10253intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10254{
10255 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
1267a26b 10256 return PAGE_ALIGN(pitch * mode->vdisplay);
d2dff872
CW
10257}
10258
10259static struct drm_framebuffer *
10260intel_framebuffer_create_for_mode(struct drm_device *dev,
10261 struct drm_display_mode *mode,
10262 int depth, int bpp)
10263{
dcb1394e 10264 struct drm_framebuffer *fb;
d2dff872 10265 struct drm_i915_gem_object *obj;
0fed39bd 10266 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872
CW
10267
10268 obj = i915_gem_alloc_object(dev,
10269 intel_framebuffer_size_for_mode(mode, bpp));
10270 if (obj == NULL)
10271 return ERR_PTR(-ENOMEM);
10272
10273 mode_cmd.width = mode->hdisplay;
10274 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
10275 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10276 bpp);
5ca0c34a 10277 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872 10278
dcb1394e
LW
10279 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
10280 if (IS_ERR(fb))
10281 drm_gem_object_unreference_unlocked(&obj->base);
10282
10283 return fb;
d2dff872
CW
10284}
10285
10286static struct drm_framebuffer *
10287mode_fits_in_fbdev(struct drm_device *dev,
10288 struct drm_display_mode *mode)
10289{
0695726e 10290#ifdef CONFIG_DRM_FBDEV_EMULATION
d2dff872
CW
10291 struct drm_i915_private *dev_priv = dev->dev_private;
10292 struct drm_i915_gem_object *obj;
10293 struct drm_framebuffer *fb;
10294
4c0e5528 10295 if (!dev_priv->fbdev)
d2dff872
CW
10296 return NULL;
10297
4c0e5528 10298 if (!dev_priv->fbdev->fb)
d2dff872
CW
10299 return NULL;
10300
4c0e5528
DV
10301 obj = dev_priv->fbdev->fb->obj;
10302 BUG_ON(!obj);
10303
8bcd4553 10304 fb = &dev_priv->fbdev->fb->base;
01f2c773
VS
10305 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10306 fb->bits_per_pixel))
d2dff872
CW
10307 return NULL;
10308
01f2c773 10309 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
10310 return NULL;
10311
edde3617 10312 drm_framebuffer_reference(fb);
d2dff872 10313 return fb;
4520f53a
DV
10314#else
10315 return NULL;
10316#endif
d2dff872
CW
10317}
10318
d3a40d1b
ACO
10319static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10320 struct drm_crtc *crtc,
10321 struct drm_display_mode *mode,
10322 struct drm_framebuffer *fb,
10323 int x, int y)
10324{
10325 struct drm_plane_state *plane_state;
10326 int hdisplay, vdisplay;
10327 int ret;
10328
10329 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10330 if (IS_ERR(plane_state))
10331 return PTR_ERR(plane_state);
10332
10333 if (mode)
10334 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10335 else
10336 hdisplay = vdisplay = 0;
10337
10338 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10339 if (ret)
10340 return ret;
10341 drm_atomic_set_fb_for_plane(plane_state, fb);
10342 plane_state->crtc_x = 0;
10343 plane_state->crtc_y = 0;
10344 plane_state->crtc_w = hdisplay;
10345 plane_state->crtc_h = vdisplay;
10346 plane_state->src_x = x << 16;
10347 plane_state->src_y = y << 16;
10348 plane_state->src_w = hdisplay << 16;
10349 plane_state->src_h = vdisplay << 16;
10350
10351 return 0;
10352}
10353
d2434ab7 10354bool intel_get_load_detect_pipe(struct drm_connector *connector,
7173188d 10355 struct drm_display_mode *mode,
51fd371b
RC
10356 struct intel_load_detect_pipe *old,
10357 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
10358{
10359 struct intel_crtc *intel_crtc;
d2434ab7
DV
10360 struct intel_encoder *intel_encoder =
10361 intel_attached_encoder(connector);
79e53945 10362 struct drm_crtc *possible_crtc;
4ef69c7a 10363 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
10364 struct drm_crtc *crtc = NULL;
10365 struct drm_device *dev = encoder->dev;
94352cf9 10366 struct drm_framebuffer *fb;
51fd371b 10367 struct drm_mode_config *config = &dev->mode_config;
edde3617 10368 struct drm_atomic_state *state = NULL, *restore_state = NULL;
944b0c76 10369 struct drm_connector_state *connector_state;
4be07317 10370 struct intel_crtc_state *crtc_state;
51fd371b 10371 int ret, i = -1;
79e53945 10372
d2dff872 10373 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 10374 connector->base.id, connector->name,
8e329a03 10375 encoder->base.id, encoder->name);
d2dff872 10376
edde3617
ML
10377 old->restore_state = NULL;
10378
51fd371b
RC
10379retry:
10380 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10381 if (ret)
ad3c558f 10382 goto fail;
6e9f798d 10383
79e53945
JB
10384 /*
10385 * Algorithm gets a little messy:
7a5e4805 10386 *
79e53945
JB
10387 * - if the connector already has an assigned crtc, use it (but make
10388 * sure it's on first)
7a5e4805 10389 *
79e53945
JB
10390 * - try to find the first unused crtc that can drive this connector,
10391 * and use that if we find one
79e53945
JB
10392 */
10393
10394 /* See if we already have a CRTC for this connector */
edde3617
ML
10395 if (connector->state->crtc) {
10396 crtc = connector->state->crtc;
8261b191 10397
51fd371b 10398 ret = drm_modeset_lock(&crtc->mutex, ctx);
4d02e2de 10399 if (ret)
ad3c558f 10400 goto fail;
8261b191
CW
10401
10402 /* Make sure the crtc and connector are running */
edde3617 10403 goto found;
79e53945
JB
10404 }
10405
10406 /* Find an unused one (if possible) */
70e1e0ec 10407 for_each_crtc(dev, possible_crtc) {
79e53945
JB
10408 i++;
10409 if (!(encoder->possible_crtcs & (1 << i)))
10410 continue;
edde3617
ML
10411
10412 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
10413 if (ret)
10414 goto fail;
10415
10416 if (possible_crtc->state->enable) {
10417 drm_modeset_unlock(&possible_crtc->mutex);
a459249c 10418 continue;
edde3617 10419 }
a459249c
VS
10420
10421 crtc = possible_crtc;
10422 break;
79e53945
JB
10423 }
10424
10425 /*
10426 * If we didn't find an unused CRTC, don't use any.
10427 */
10428 if (!crtc) {
7173188d 10429 DRM_DEBUG_KMS("no pipe available for load-detect\n");
ad3c558f 10430 goto fail;
79e53945
JB
10431 }
10432
edde3617
ML
10433found:
10434 intel_crtc = to_intel_crtc(crtc);
10435
4d02e2de
DV
10436 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10437 if (ret)
ad3c558f 10438 goto fail;
79e53945 10439
83a57153 10440 state = drm_atomic_state_alloc(dev);
edde3617
ML
10441 restore_state = drm_atomic_state_alloc(dev);
10442 if (!state || !restore_state) {
10443 ret = -ENOMEM;
10444 goto fail;
10445 }
83a57153
ACO
10446
10447 state->acquire_ctx = ctx;
edde3617 10448 restore_state->acquire_ctx = ctx;
83a57153 10449
944b0c76
ACO
10450 connector_state = drm_atomic_get_connector_state(state, connector);
10451 if (IS_ERR(connector_state)) {
10452 ret = PTR_ERR(connector_state);
10453 goto fail;
10454 }
10455
edde3617
ML
10456 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
10457 if (ret)
10458 goto fail;
944b0c76 10459
4be07317
ACO
10460 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10461 if (IS_ERR(crtc_state)) {
10462 ret = PTR_ERR(crtc_state);
10463 goto fail;
10464 }
10465
49d6fa21 10466 crtc_state->base.active = crtc_state->base.enable = true;
4be07317 10467
6492711d
CW
10468 if (!mode)
10469 mode = &load_detect_mode;
79e53945 10470
d2dff872
CW
10471 /* We need a framebuffer large enough to accommodate all accesses
10472 * that the plane may generate whilst we perform load detection.
10473 * We can not rely on the fbcon either being present (we get called
10474 * during its initialisation to detect all boot displays, or it may
10475 * not even exist) or that it is large enough to satisfy the
10476 * requested mode.
10477 */
94352cf9
DV
10478 fb = mode_fits_in_fbdev(dev, mode);
10479 if (fb == NULL) {
d2dff872 10480 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9 10481 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
d2dff872
CW
10482 } else
10483 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 10484 if (IS_ERR(fb)) {
d2dff872 10485 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
412b61d8 10486 goto fail;
79e53945 10487 }
79e53945 10488
d3a40d1b
ACO
10489 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10490 if (ret)
10491 goto fail;
10492
edde3617
ML
10493 drm_framebuffer_unreference(fb);
10494
10495 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
10496 if (ret)
10497 goto fail;
10498
10499 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
10500 if (!ret)
10501 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
10502 if (!ret)
10503 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
10504 if (ret) {
10505 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
10506 goto fail;
10507 }
8c7b5ccb 10508
3ba86073
ML
10509 ret = drm_atomic_commit(state);
10510 if (ret) {
6492711d 10511 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
412b61d8 10512 goto fail;
79e53945 10513 }
edde3617
ML
10514
10515 old->restore_state = restore_state;
7173188d 10516
79e53945 10517 /* let the connector get through one full cycle before testing */
9d0498a2 10518 intel_wait_for_vblank(dev, intel_crtc->pipe);
7173188d 10519 return true;
412b61d8 10520
ad3c558f 10521fail:
e5d958ef 10522 drm_atomic_state_free(state);
edde3617
ML
10523 drm_atomic_state_free(restore_state);
10524 restore_state = state = NULL;
83a57153 10525
51fd371b
RC
10526 if (ret == -EDEADLK) {
10527 drm_modeset_backoff(ctx);
10528 goto retry;
10529 }
10530
412b61d8 10531 return false;
79e53945
JB
10532}
10533
d2434ab7 10534void intel_release_load_detect_pipe(struct drm_connector *connector,
49172fee
ACO
10535 struct intel_load_detect_pipe *old,
10536 struct drm_modeset_acquire_ctx *ctx)
79e53945 10537{
d2434ab7
DV
10538 struct intel_encoder *intel_encoder =
10539 intel_attached_encoder(connector);
4ef69c7a 10540 struct drm_encoder *encoder = &intel_encoder->base;
edde3617 10541 struct drm_atomic_state *state = old->restore_state;
d3a40d1b 10542 int ret;
79e53945 10543
d2dff872 10544 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 10545 connector->base.id, connector->name,
8e329a03 10546 encoder->base.id, encoder->name);
d2dff872 10547
edde3617 10548 if (!state)
0622a53c 10549 return;
79e53945 10550
edde3617
ML
10551 ret = drm_atomic_commit(state);
10552 if (ret) {
10553 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
10554 drm_atomic_state_free(state);
10555 }
79e53945
JB
10556}
10557
da4a1efa 10558static int i9xx_pll_refclk(struct drm_device *dev,
5cec258b 10559 const struct intel_crtc_state *pipe_config)
da4a1efa
VS
10560{
10561 struct drm_i915_private *dev_priv = dev->dev_private;
10562 u32 dpll = pipe_config->dpll_hw_state.dpll;
10563
10564 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 10565 return dev_priv->vbt.lvds_ssc_freq;
da4a1efa
VS
10566 else if (HAS_PCH_SPLIT(dev))
10567 return 120000;
10568 else if (!IS_GEN2(dev))
10569 return 96000;
10570 else
10571 return 48000;
10572}
10573
79e53945 10574/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc 10575static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 10576 struct intel_crtc_state *pipe_config)
79e53945 10577{
f1f644dc 10578 struct drm_device *dev = crtc->base.dev;
79e53945 10579 struct drm_i915_private *dev_priv = dev->dev_private;
f1f644dc 10580 int pipe = pipe_config->cpu_transcoder;
293623f7 10581 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945
JB
10582 u32 fp;
10583 intel_clock_t clock;
dccbea3b 10584 int port_clock;
da4a1efa 10585 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
10586
10587 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 10588 fp = pipe_config->dpll_hw_state.fp0;
79e53945 10589 else
293623f7 10590 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
10591
10592 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
f2b115e6
AJ
10593 if (IS_PINEVIEW(dev)) {
10594 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10595 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
10596 } else {
10597 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10598 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10599 }
10600
a6c45cf0 10601 if (!IS_GEN2(dev)) {
f2b115e6
AJ
10602 if (IS_PINEVIEW(dev))
10603 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10604 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
10605 else
10606 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
10607 DPLL_FPA01_P1_POST_DIV_SHIFT);
10608
10609 switch (dpll & DPLL_MODE_MASK) {
10610 case DPLLB_MODE_DAC_SERIAL:
10611 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10612 5 : 10;
10613 break;
10614 case DPLLB_MODE_LVDS:
10615 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10616 7 : 14;
10617 break;
10618 default:
28c97730 10619 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 10620 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 10621 return;
79e53945
JB
10622 }
10623
ac58c3f0 10624 if (IS_PINEVIEW(dev))
dccbea3b 10625 port_clock = pnv_calc_dpll_params(refclk, &clock);
ac58c3f0 10626 else
dccbea3b 10627 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945 10628 } else {
0fb58223 10629 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
b1c560d1 10630 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
10631
10632 if (is_lvds) {
10633 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10634 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
10635
10636 if (lvds & LVDS_CLKB_POWER_UP)
10637 clock.p2 = 7;
10638 else
10639 clock.p2 = 14;
79e53945
JB
10640 } else {
10641 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10642 clock.p1 = 2;
10643 else {
10644 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10645 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10646 }
10647 if (dpll & PLL_P2_DIVIDE_BY_4)
10648 clock.p2 = 4;
10649 else
10650 clock.p2 = 2;
79e53945 10651 }
da4a1efa 10652
dccbea3b 10653 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945
JB
10654 }
10655
18442d08
VS
10656 /*
10657 * This value includes pixel_multiplier. We will use
241bfc38 10658 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
10659 * encoder's get_config() function.
10660 */
dccbea3b 10661 pipe_config->port_clock = port_clock;
f1f644dc
JB
10662}
10663
6878da05
VS
10664int intel_dotclock_calculate(int link_freq,
10665 const struct intel_link_m_n *m_n)
f1f644dc 10666{
f1f644dc
JB
10667 /*
10668 * The calculation for the data clock is:
1041a02f 10669 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 10670 * But we want to avoid losing precison if possible, so:
1041a02f 10671 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
10672 *
10673 * and the link clock is simpler:
1041a02f 10674 * link_clock = (m * link_clock) / n
f1f644dc
JB
10675 */
10676
6878da05
VS
10677 if (!m_n->link_n)
10678 return 0;
f1f644dc 10679
6878da05
VS
10680 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10681}
f1f644dc 10682
18442d08 10683static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 10684 struct intel_crtc_state *pipe_config)
6878da05 10685{
e3b247da 10686 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
79e53945 10687
18442d08
VS
10688 /* read out port_clock from the DPLL */
10689 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 10690
f1f644dc 10691 /*
e3b247da
VS
10692 * In case there is an active pipe without active ports,
10693 * we may need some idea for the dotclock anyway.
10694 * Calculate one based on the FDI configuration.
79e53945 10695 */
2d112de7 10696 pipe_config->base.adjusted_mode.crtc_clock =
21a727b3 10697 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
18442d08 10698 &pipe_config->fdi_m_n);
79e53945
JB
10699}
10700
10701/** Returns the currently programmed mode of the given pipe. */
10702struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10703 struct drm_crtc *crtc)
10704{
548f245b 10705 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 10706 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 10707 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
79e53945 10708 struct drm_display_mode *mode;
3f36b937 10709 struct intel_crtc_state *pipe_config;
fe2b8f9d
PZ
10710 int htot = I915_READ(HTOTAL(cpu_transcoder));
10711 int hsync = I915_READ(HSYNC(cpu_transcoder));
10712 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10713 int vsync = I915_READ(VSYNC(cpu_transcoder));
293623f7 10714 enum pipe pipe = intel_crtc->pipe;
79e53945
JB
10715
10716 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10717 if (!mode)
10718 return NULL;
10719
3f36b937
TU
10720 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10721 if (!pipe_config) {
10722 kfree(mode);
10723 return NULL;
10724 }
10725
f1f644dc
JB
10726 /*
10727 * Construct a pipe_config sufficient for getting the clock info
10728 * back out of crtc_clock_get.
10729 *
10730 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10731 * to use a real value here instead.
10732 */
3f36b937
TU
10733 pipe_config->cpu_transcoder = (enum transcoder) pipe;
10734 pipe_config->pixel_multiplier = 1;
10735 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10736 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10737 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
10738 i9xx_crtc_clock_get(intel_crtc, pipe_config);
10739
10740 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
79e53945
JB
10741 mode->hdisplay = (htot & 0xffff) + 1;
10742 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10743 mode->hsync_start = (hsync & 0xffff) + 1;
10744 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10745 mode->vdisplay = (vtot & 0xffff) + 1;
10746 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10747 mode->vsync_start = (vsync & 0xffff) + 1;
10748 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10749
10750 drm_mode_set_name(mode);
79e53945 10751
3f36b937
TU
10752 kfree(pipe_config);
10753
79e53945
JB
10754 return mode;
10755}
10756
f047e395
CW
10757void intel_mark_busy(struct drm_device *dev)
10758{
c67a470b
PZ
10759 struct drm_i915_private *dev_priv = dev->dev_private;
10760
f62a0076
CW
10761 if (dev_priv->mm.busy)
10762 return;
10763
43694d69 10764 intel_runtime_pm_get(dev_priv);
c67a470b 10765 i915_update_gfx_val(dev_priv);
43cf3bf0
CW
10766 if (INTEL_INFO(dev)->gen >= 6)
10767 gen6_rps_busy(dev_priv);
f62a0076 10768 dev_priv->mm.busy = true;
f047e395
CW
10769}
10770
10771void intel_mark_idle(struct drm_device *dev)
652c393a 10772{
c67a470b 10773 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 10774
f62a0076
CW
10775 if (!dev_priv->mm.busy)
10776 return;
10777
10778 dev_priv->mm.busy = false;
10779
3d13ef2e 10780 if (INTEL_INFO(dev)->gen >= 6)
b29c19b6 10781 gen6_rps_idle(dev->dev_private);
bb4cdd53 10782
43694d69 10783 intel_runtime_pm_put(dev_priv);
652c393a
JB
10784}
10785
79e53945
JB
10786static void intel_crtc_destroy(struct drm_crtc *crtc)
10787{
10788 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
67e77c5a
DV
10789 struct drm_device *dev = crtc->dev;
10790 struct intel_unpin_work *work;
67e77c5a 10791
5e2d7afc 10792 spin_lock_irq(&dev->event_lock);
67e77c5a
DV
10793 work = intel_crtc->unpin_work;
10794 intel_crtc->unpin_work = NULL;
5e2d7afc 10795 spin_unlock_irq(&dev->event_lock);
67e77c5a
DV
10796
10797 if (work) {
10798 cancel_work_sync(&work->work);
10799 kfree(work);
10800 }
79e53945
JB
10801
10802 drm_crtc_cleanup(crtc);
67e77c5a 10803
79e53945
JB
10804 kfree(intel_crtc);
10805}
10806
6b95a207
KH
10807static void intel_unpin_work_fn(struct work_struct *__work)
10808{
10809 struct intel_unpin_work *work =
10810 container_of(__work, struct intel_unpin_work, work);
a9ff8714
VS
10811 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10812 struct drm_device *dev = crtc->base.dev;
10813 struct drm_plane *primary = crtc->base.primary;
6b95a207 10814
b4a98e57 10815 mutex_lock(&dev->struct_mutex);
3465c580 10816 intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
05394f39 10817 drm_gem_object_unreference(&work->pending_flip_obj->base);
d9e86c0e 10818
f06cc1b9 10819 if (work->flip_queued_req)
146d84f0 10820 i915_gem_request_assign(&work->flip_queued_req, NULL);
b4a98e57
CW
10821 mutex_unlock(&dev->struct_mutex);
10822
a9ff8714 10823 intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
1eb52238 10824 intel_fbc_post_update(crtc);
89ed88ba 10825 drm_framebuffer_unreference(work->old_fb);
f99d7069 10826
a9ff8714
VS
10827 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
10828 atomic_dec(&crtc->unpin_work_count);
b4a98e57 10829
6b95a207
KH
10830 kfree(work);
10831}
10832
1afe3e9d 10833static void do_intel_finish_page_flip(struct drm_device *dev,
49b14a5c 10834 struct drm_crtc *crtc)
6b95a207 10835{
6b95a207
KH
10836 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10837 struct intel_unpin_work *work;
6b95a207
KH
10838 unsigned long flags;
10839
10840 /* Ignore early vblank irqs */
10841 if (intel_crtc == NULL)
10842 return;
10843
f326038a
DV
10844 /*
10845 * This is called both by irq handlers and the reset code (to complete
10846 * lost pageflips) so needs the full irqsave spinlocks.
10847 */
6b95a207
KH
10848 spin_lock_irqsave(&dev->event_lock, flags);
10849 work = intel_crtc->unpin_work;
e7d841ca
CW
10850
10851 /* Ensure we don't miss a work->pending update ... */
10852 smp_rmb();
10853
10854 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
6b95a207
KH
10855 spin_unlock_irqrestore(&dev->event_lock, flags);
10856 return;
10857 }
10858
d6bbafa1 10859 page_flip_completed(intel_crtc);
0af7e4df 10860
6b95a207 10861 spin_unlock_irqrestore(&dev->event_lock, flags);
6b95a207
KH
10862}
10863
1afe3e9d
JB
10864void intel_finish_page_flip(struct drm_device *dev, int pipe)
10865{
fbee40df 10866 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
10867 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10868
49b14a5c 10869 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
10870}
10871
10872void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10873{
fbee40df 10874 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
10875 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10876
49b14a5c 10877 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
10878}
10879
75f7f3ec
VS
10880/* Is 'a' after or equal to 'b'? */
10881static bool g4x_flip_count_after_eq(u32 a, u32 b)
10882{
10883 return !((a - b) & 0x80000000);
10884}
10885
10886static bool page_flip_finished(struct intel_crtc *crtc)
10887{
10888 struct drm_device *dev = crtc->base.dev;
10889 struct drm_i915_private *dev_priv = dev->dev_private;
10890
bdfa7542
VS
10891 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10892 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10893 return true;
10894
75f7f3ec
VS
10895 /*
10896 * The relevant registers doen't exist on pre-ctg.
10897 * As the flip done interrupt doesn't trigger for mmio
10898 * flips on gmch platforms, a flip count check isn't
10899 * really needed there. But since ctg has the registers,
10900 * include it in the check anyway.
10901 */
10902 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10903 return true;
10904
e8861675
ML
10905 /*
10906 * BDW signals flip done immediately if the plane
10907 * is disabled, even if the plane enable is already
10908 * armed to occur at the next vblank :(
10909 */
10910
75f7f3ec
VS
10911 /*
10912 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10913 * used the same base address. In that case the mmio flip might
10914 * have completed, but the CS hasn't even executed the flip yet.
10915 *
10916 * A flip count check isn't enough as the CS might have updated
10917 * the base address just after start of vblank, but before we
10918 * managed to process the interrupt. This means we'd complete the
10919 * CS flip too soon.
10920 *
10921 * Combining both checks should get us a good enough result. It may
10922 * still happen that the CS flip has been executed, but has not
10923 * yet actually completed. But in case the base address is the same
10924 * anyway, we don't really care.
10925 */
10926 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10927 crtc->unpin_work->gtt_offset &&
fd8f507c 10928 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
75f7f3ec
VS
10929 crtc->unpin_work->flip_count);
10930}
10931
6b95a207
KH
10932void intel_prepare_page_flip(struct drm_device *dev, int plane)
10933{
fbee40df 10934 struct drm_i915_private *dev_priv = dev->dev_private;
6b95a207
KH
10935 struct intel_crtc *intel_crtc =
10936 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10937 unsigned long flags;
10938
f326038a
DV
10939
10940 /*
10941 * This is called both by irq handlers and the reset code (to complete
10942 * lost pageflips) so needs the full irqsave spinlocks.
10943 *
10944 * NB: An MMIO update of the plane base pointer will also
e7d841ca
CW
10945 * generate a page-flip completion irq, i.e. every modeset
10946 * is also accompanied by a spurious intel_prepare_page_flip().
10947 */
6b95a207 10948 spin_lock_irqsave(&dev->event_lock, flags);
75f7f3ec 10949 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
e7d841ca 10950 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
6b95a207
KH
10951 spin_unlock_irqrestore(&dev->event_lock, flags);
10952}
10953
6042639c 10954static inline void intel_mark_page_flip_active(struct intel_unpin_work *work)
e7d841ca
CW
10955{
10956 /* Ensure that the work item is consistent when activating it ... */
10957 smp_wmb();
6042639c 10958 atomic_set(&work->pending, INTEL_FLIP_PENDING);
e7d841ca
CW
10959 /* and that it is marked active as soon as the irq could fire. */
10960 smp_wmb();
10961}
10962
8c9f3aaf
JB
10963static int intel_gen2_queue_flip(struct drm_device *dev,
10964 struct drm_crtc *crtc,
10965 struct drm_framebuffer *fb,
ed8d1975 10966 struct drm_i915_gem_object *obj,
6258fbe2 10967 struct drm_i915_gem_request *req,
ed8d1975 10968 uint32_t flags)
8c9f3aaf 10969{
4a570db5 10970 struct intel_engine_cs *engine = req->engine;
8c9f3aaf 10971 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
10972 u32 flip_mask;
10973 int ret;
10974
5fb9de1a 10975 ret = intel_ring_begin(req, 6);
8c9f3aaf 10976 if (ret)
4fa62c89 10977 return ret;
8c9f3aaf
JB
10978
10979 /* Can't queue multiple flips, so wait for the previous
10980 * one to finish before executing the next.
10981 */
10982 if (intel_crtc->plane)
10983 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10984 else
10985 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
e2f80391
TU
10986 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
10987 intel_ring_emit(engine, MI_NOOP);
10988 intel_ring_emit(engine, MI_DISPLAY_FLIP |
6d90c952 10989 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
e2f80391
TU
10990 intel_ring_emit(engine, fb->pitches[0]);
10991 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
10992 intel_ring_emit(engine, 0); /* aux display base address, unused */
e7d841ca 10993
6042639c 10994 intel_mark_page_flip_active(intel_crtc->unpin_work);
83d4092b 10995 return 0;
8c9f3aaf
JB
10996}
10997
10998static int intel_gen3_queue_flip(struct drm_device *dev,
10999 struct drm_crtc *crtc,
11000 struct drm_framebuffer *fb,
ed8d1975 11001 struct drm_i915_gem_object *obj,
6258fbe2 11002 struct drm_i915_gem_request *req,
ed8d1975 11003 uint32_t flags)
8c9f3aaf 11004{
4a570db5 11005 struct intel_engine_cs *engine = req->engine;
8c9f3aaf 11006 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
11007 u32 flip_mask;
11008 int ret;
11009
5fb9de1a 11010 ret = intel_ring_begin(req, 6);
8c9f3aaf 11011 if (ret)
4fa62c89 11012 return ret;
8c9f3aaf
JB
11013
11014 if (intel_crtc->plane)
11015 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11016 else
11017 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
e2f80391
TU
11018 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
11019 intel_ring_emit(engine, MI_NOOP);
11020 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 |
6d90c952 11021 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
e2f80391
TU
11022 intel_ring_emit(engine, fb->pitches[0]);
11023 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
11024 intel_ring_emit(engine, MI_NOOP);
6d90c952 11025
6042639c 11026 intel_mark_page_flip_active(intel_crtc->unpin_work);
83d4092b 11027 return 0;
8c9f3aaf
JB
11028}
11029
11030static int intel_gen4_queue_flip(struct drm_device *dev,
11031 struct drm_crtc *crtc,
11032 struct drm_framebuffer *fb,
ed8d1975 11033 struct drm_i915_gem_object *obj,
6258fbe2 11034 struct drm_i915_gem_request *req,
ed8d1975 11035 uint32_t flags)
8c9f3aaf 11036{
4a570db5 11037 struct intel_engine_cs *engine = req->engine;
8c9f3aaf
JB
11038 struct drm_i915_private *dev_priv = dev->dev_private;
11039 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11040 uint32_t pf, pipesrc;
11041 int ret;
11042
5fb9de1a 11043 ret = intel_ring_begin(req, 4);
8c9f3aaf 11044 if (ret)
4fa62c89 11045 return ret;
8c9f3aaf
JB
11046
11047 /* i965+ uses the linear or tiled offsets from the
11048 * Display Registers (which do not change across a page-flip)
11049 * so we need only reprogram the base address.
11050 */
e2f80391 11051 intel_ring_emit(engine, MI_DISPLAY_FLIP |
6d90c952 11052 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
e2f80391
TU
11053 intel_ring_emit(engine, fb->pitches[0]);
11054 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset |
c2c75131 11055 obj->tiling_mode);
8c9f3aaf
JB
11056
11057 /* XXX Enabling the panel-fitter across page-flip is so far
11058 * untested on non-native modes, so ignore it for now.
11059 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11060 */
11061 pf = 0;
11062 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
e2f80391 11063 intel_ring_emit(engine, pf | pipesrc);
e7d841ca 11064
6042639c 11065 intel_mark_page_flip_active(intel_crtc->unpin_work);
83d4092b 11066 return 0;
8c9f3aaf
JB
11067}
11068
11069static int intel_gen6_queue_flip(struct drm_device *dev,
11070 struct drm_crtc *crtc,
11071 struct drm_framebuffer *fb,
ed8d1975 11072 struct drm_i915_gem_object *obj,
6258fbe2 11073 struct drm_i915_gem_request *req,
ed8d1975 11074 uint32_t flags)
8c9f3aaf 11075{
4a570db5 11076 struct intel_engine_cs *engine = req->engine;
8c9f3aaf
JB
11077 struct drm_i915_private *dev_priv = dev->dev_private;
11078 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11079 uint32_t pf, pipesrc;
11080 int ret;
11081
5fb9de1a 11082 ret = intel_ring_begin(req, 4);
8c9f3aaf 11083 if (ret)
4fa62c89 11084 return ret;
8c9f3aaf 11085
e2f80391 11086 intel_ring_emit(engine, MI_DISPLAY_FLIP |
6d90c952 11087 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
e2f80391
TU
11088 intel_ring_emit(engine, fb->pitches[0] | obj->tiling_mode);
11089 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
8c9f3aaf 11090
dc257cf1
DV
11091 /* Contrary to the suggestions in the documentation,
11092 * "Enable Panel Fitter" does not seem to be required when page
11093 * flipping with a non-native mode, and worse causes a normal
11094 * modeset to fail.
11095 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11096 */
11097 pf = 0;
8c9f3aaf 11098 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
e2f80391 11099 intel_ring_emit(engine, pf | pipesrc);
e7d841ca 11100
6042639c 11101 intel_mark_page_flip_active(intel_crtc->unpin_work);
83d4092b 11102 return 0;
8c9f3aaf
JB
11103}
11104
7c9017e5
JB
11105static int intel_gen7_queue_flip(struct drm_device *dev,
11106 struct drm_crtc *crtc,
11107 struct drm_framebuffer *fb,
ed8d1975 11108 struct drm_i915_gem_object *obj,
6258fbe2 11109 struct drm_i915_gem_request *req,
ed8d1975 11110 uint32_t flags)
7c9017e5 11111{
4a570db5 11112 struct intel_engine_cs *engine = req->engine;
7c9017e5 11113 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cb05d8de 11114 uint32_t plane_bit = 0;
ffe74d75
CW
11115 int len, ret;
11116
eba905b2 11117 switch (intel_crtc->plane) {
cb05d8de
DV
11118 case PLANE_A:
11119 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11120 break;
11121 case PLANE_B:
11122 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11123 break;
11124 case PLANE_C:
11125 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11126 break;
11127 default:
11128 WARN_ONCE(1, "unknown plane in flip command\n");
4fa62c89 11129 return -ENODEV;
cb05d8de
DV
11130 }
11131
ffe74d75 11132 len = 4;
e2f80391 11133 if (engine->id == RCS) {
ffe74d75 11134 len += 6;
f476828a
DL
11135 /*
11136 * On Gen 8, SRM is now taking an extra dword to accommodate
11137 * 48bits addresses, and we need a NOOP for the batch size to
11138 * stay even.
11139 */
11140 if (IS_GEN8(dev))
11141 len += 2;
11142 }
ffe74d75 11143
f66fab8e
VS
11144 /*
11145 * BSpec MI_DISPLAY_FLIP for IVB:
11146 * "The full packet must be contained within the same cache line."
11147 *
11148 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11149 * cacheline, if we ever start emitting more commands before
11150 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11151 * then do the cacheline alignment, and finally emit the
11152 * MI_DISPLAY_FLIP.
11153 */
bba09b12 11154 ret = intel_ring_cacheline_align(req);
f66fab8e 11155 if (ret)
4fa62c89 11156 return ret;
f66fab8e 11157
5fb9de1a 11158 ret = intel_ring_begin(req, len);
7c9017e5 11159 if (ret)
4fa62c89 11160 return ret;
7c9017e5 11161
ffe74d75
CW
11162 /* Unmask the flip-done completion message. Note that the bspec says that
11163 * we should do this for both the BCS and RCS, and that we must not unmask
11164 * more than one flip event at any time (or ensure that one flip message
11165 * can be sent by waiting for flip-done prior to queueing new flips).
11166 * Experimentation says that BCS works despite DERRMR masking all
11167 * flip-done completion events and that unmasking all planes at once
11168 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11169 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11170 */
e2f80391
TU
11171 if (engine->id == RCS) {
11172 intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(1));
11173 intel_ring_emit_reg(engine, DERRMR);
11174 intel_ring_emit(engine, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11175 DERRMR_PIPEB_PRI_FLIP_DONE |
11176 DERRMR_PIPEC_PRI_FLIP_DONE));
f476828a 11177 if (IS_GEN8(dev))
e2f80391 11178 intel_ring_emit(engine, MI_STORE_REGISTER_MEM_GEN8 |
f476828a
DL
11179 MI_SRM_LRM_GLOBAL_GTT);
11180 else
e2f80391 11181 intel_ring_emit(engine, MI_STORE_REGISTER_MEM |
f476828a 11182 MI_SRM_LRM_GLOBAL_GTT);
e2f80391
TU
11183 intel_ring_emit_reg(engine, DERRMR);
11184 intel_ring_emit(engine, engine->scratch.gtt_offset + 256);
f476828a 11185 if (IS_GEN8(dev)) {
e2f80391
TU
11186 intel_ring_emit(engine, 0);
11187 intel_ring_emit(engine, MI_NOOP);
f476828a 11188 }
ffe74d75
CW
11189 }
11190
e2f80391
TU
11191 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 | plane_bit);
11192 intel_ring_emit(engine, (fb->pitches[0] | obj->tiling_mode));
11193 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
11194 intel_ring_emit(engine, (MI_NOOP));
e7d841ca 11195
6042639c 11196 intel_mark_page_flip_active(intel_crtc->unpin_work);
83d4092b 11197 return 0;
7c9017e5
JB
11198}
11199
0bc40be8 11200static bool use_mmio_flip(struct intel_engine_cs *engine,
84c33a64
SG
11201 struct drm_i915_gem_object *obj)
11202{
11203 /*
11204 * This is not being used for older platforms, because
11205 * non-availability of flip done interrupt forces us to use
11206 * CS flips. Older platforms derive flip done using some clever
11207 * tricks involving the flip_pending status bits and vblank irqs.
11208 * So using MMIO flips there would disrupt this mechanism.
11209 */
11210
0bc40be8 11211 if (engine == NULL)
8e09bf83
CW
11212 return true;
11213
0bc40be8 11214 if (INTEL_INFO(engine->dev)->gen < 5)
84c33a64
SG
11215 return false;
11216
11217 if (i915.use_mmio_flip < 0)
11218 return false;
11219 else if (i915.use_mmio_flip > 0)
11220 return true;
14bf993e
OM
11221 else if (i915.enable_execlists)
11222 return true;
fd8e058a
AG
11223 else if (obj->base.dma_buf &&
11224 !reservation_object_test_signaled_rcu(obj->base.dma_buf->resv,
11225 false))
11226 return true;
84c33a64 11227 else
666796da 11228 return engine != i915_gem_request_get_engine(obj->last_write_req);
84c33a64
SG
11229}
11230
6042639c 11231static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
86efe24a 11232 unsigned int rotation,
6042639c 11233 struct intel_unpin_work *work)
ff944564
DL
11234{
11235 struct drm_device *dev = intel_crtc->base.dev;
11236 struct drm_i915_private *dev_priv = dev->dev_private;
11237 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
ff944564 11238 const enum pipe pipe = intel_crtc->pipe;
86efe24a 11239 u32 ctl, stride, tile_height;
ff944564
DL
11240
11241 ctl = I915_READ(PLANE_CTL(pipe, 0));
11242 ctl &= ~PLANE_CTL_TILED_MASK;
2ebef630
TU
11243 switch (fb->modifier[0]) {
11244 case DRM_FORMAT_MOD_NONE:
11245 break;
11246 case I915_FORMAT_MOD_X_TILED:
ff944564 11247 ctl |= PLANE_CTL_TILED_X;
2ebef630
TU
11248 break;
11249 case I915_FORMAT_MOD_Y_TILED:
11250 ctl |= PLANE_CTL_TILED_Y;
11251 break;
11252 case I915_FORMAT_MOD_Yf_TILED:
11253 ctl |= PLANE_CTL_TILED_YF;
11254 break;
11255 default:
11256 MISSING_CASE(fb->modifier[0]);
11257 }
ff944564
DL
11258
11259 /*
11260 * The stride is either expressed as a multiple of 64 bytes chunks for
11261 * linear buffers or in number of tiles for tiled buffers.
11262 */
86efe24a
TU
11263 if (intel_rotation_90_or_270(rotation)) {
11264 /* stride = Surface height in tiles */
832be82f 11265 tile_height = intel_tile_height(dev_priv, fb->modifier[0], 0);
86efe24a
TU
11266 stride = DIV_ROUND_UP(fb->height, tile_height);
11267 } else {
11268 stride = fb->pitches[0] /
7b49f948
VS
11269 intel_fb_stride_alignment(dev_priv, fb->modifier[0],
11270 fb->pixel_format);
86efe24a 11271 }
ff944564
DL
11272
11273 /*
11274 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11275 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11276 */
11277 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11278 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11279
6042639c 11280 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
ff944564
DL
11281 POSTING_READ(PLANE_SURF(pipe, 0));
11282}
11283
6042639c
CW
11284static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
11285 struct intel_unpin_work *work)
84c33a64
SG
11286{
11287 struct drm_device *dev = intel_crtc->base.dev;
11288 struct drm_i915_private *dev_priv = dev->dev_private;
11289 struct intel_framebuffer *intel_fb =
11290 to_intel_framebuffer(intel_crtc->base.primary->fb);
11291 struct drm_i915_gem_object *obj = intel_fb->obj;
f0f59a00 11292 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
84c33a64 11293 u32 dspcntr;
84c33a64 11294
84c33a64
SG
11295 dspcntr = I915_READ(reg);
11296
c5d97472
DL
11297 if (obj->tiling_mode != I915_TILING_NONE)
11298 dspcntr |= DISPPLANE_TILED;
11299 else
11300 dspcntr &= ~DISPPLANE_TILED;
11301
84c33a64
SG
11302 I915_WRITE(reg, dspcntr);
11303
6042639c 11304 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
84c33a64 11305 POSTING_READ(DSPSURF(intel_crtc->plane));
ff944564
DL
11306}
11307
11308/*
11309 * XXX: This is the temporary way to update the plane registers until we get
11310 * around to using the usual plane update functions for MMIO flips
11311 */
6042639c 11312static void intel_do_mmio_flip(struct intel_mmio_flip *mmio_flip)
ff944564 11313{
6042639c
CW
11314 struct intel_crtc *crtc = mmio_flip->crtc;
11315 struct intel_unpin_work *work;
11316
11317 spin_lock_irq(&crtc->base.dev->event_lock);
11318 work = crtc->unpin_work;
11319 spin_unlock_irq(&crtc->base.dev->event_lock);
11320 if (work == NULL)
11321 return;
ff944564 11322
6042639c 11323 intel_mark_page_flip_active(work);
ff944564 11324
6042639c 11325 intel_pipe_update_start(crtc);
ff944564 11326
6042639c 11327 if (INTEL_INFO(mmio_flip->i915)->gen >= 9)
86efe24a 11328 skl_do_mmio_flip(crtc, mmio_flip->rotation, work);
ff944564
DL
11329 else
11330 /* use_mmio_flip() retricts MMIO flips to ilk+ */
6042639c 11331 ilk_do_mmio_flip(crtc, work);
ff944564 11332
6042639c 11333 intel_pipe_update_end(crtc);
84c33a64
SG
11334}
11335
9362c7c5 11336static void intel_mmio_flip_work_func(struct work_struct *work)
84c33a64 11337{
b2cfe0ab
CW
11338 struct intel_mmio_flip *mmio_flip =
11339 container_of(work, struct intel_mmio_flip, work);
fd8e058a
AG
11340 struct intel_framebuffer *intel_fb =
11341 to_intel_framebuffer(mmio_flip->crtc->base.primary->fb);
11342 struct drm_i915_gem_object *obj = intel_fb->obj;
84c33a64 11343
6042639c 11344 if (mmio_flip->req) {
eed29a5b 11345 WARN_ON(__i915_wait_request(mmio_flip->req,
b2cfe0ab 11346 mmio_flip->crtc->reset_counter,
bcafc4e3
CW
11347 false, NULL,
11348 &mmio_flip->i915->rps.mmioflips));
6042639c
CW
11349 i915_gem_request_unreference__unlocked(mmio_flip->req);
11350 }
84c33a64 11351
fd8e058a
AG
11352 /* For framebuffer backed by dmabuf, wait for fence */
11353 if (obj->base.dma_buf)
11354 WARN_ON(reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
11355 false, false,
11356 MAX_SCHEDULE_TIMEOUT) < 0);
11357
6042639c 11358 intel_do_mmio_flip(mmio_flip);
b2cfe0ab 11359 kfree(mmio_flip);
84c33a64
SG
11360}
11361
11362static int intel_queue_mmio_flip(struct drm_device *dev,
11363 struct drm_crtc *crtc,
86efe24a 11364 struct drm_i915_gem_object *obj)
84c33a64 11365{
b2cfe0ab
CW
11366 struct intel_mmio_flip *mmio_flip;
11367
11368 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
11369 if (mmio_flip == NULL)
11370 return -ENOMEM;
84c33a64 11371
bcafc4e3 11372 mmio_flip->i915 = to_i915(dev);
eed29a5b 11373 mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
b2cfe0ab 11374 mmio_flip->crtc = to_intel_crtc(crtc);
86efe24a 11375 mmio_flip->rotation = crtc->primary->state->rotation;
536f5b5e 11376
b2cfe0ab
CW
11377 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
11378 schedule_work(&mmio_flip->work);
84c33a64 11379
84c33a64
SG
11380 return 0;
11381}
11382
8c9f3aaf
JB
11383static int intel_default_queue_flip(struct drm_device *dev,
11384 struct drm_crtc *crtc,
11385 struct drm_framebuffer *fb,
ed8d1975 11386 struct drm_i915_gem_object *obj,
6258fbe2 11387 struct drm_i915_gem_request *req,
ed8d1975 11388 uint32_t flags)
8c9f3aaf
JB
11389{
11390 return -ENODEV;
11391}
11392
d6bbafa1
CW
11393static bool __intel_pageflip_stall_check(struct drm_device *dev,
11394 struct drm_crtc *crtc)
11395{
11396 struct drm_i915_private *dev_priv = dev->dev_private;
11397 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11398 struct intel_unpin_work *work = intel_crtc->unpin_work;
11399 u32 addr;
11400
11401 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
11402 return true;
11403
908565c2
CW
11404 if (atomic_read(&work->pending) < INTEL_FLIP_PENDING)
11405 return false;
11406
d6bbafa1
CW
11407 if (!work->enable_stall_check)
11408 return false;
11409
11410 if (work->flip_ready_vblank == 0) {
3a8a946e
DV
11411 if (work->flip_queued_req &&
11412 !i915_gem_request_completed(work->flip_queued_req, true))
d6bbafa1
CW
11413 return false;
11414
1e3feefd 11415 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
d6bbafa1
CW
11416 }
11417
1e3feefd 11418 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
d6bbafa1
CW
11419 return false;
11420
11421 /* Potential stall - if we see that the flip has happened,
11422 * assume a missed interrupt. */
11423 if (INTEL_INFO(dev)->gen >= 4)
11424 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11425 else
11426 addr = I915_READ(DSPADDR(intel_crtc->plane));
11427
11428 /* There is a potential issue here with a false positive after a flip
11429 * to the same address. We could address this by checking for a
11430 * non-incrementing frame counter.
11431 */
11432 return addr == work->gtt_offset;
11433}
11434
11435void intel_check_page_flip(struct drm_device *dev, int pipe)
11436{
11437 struct drm_i915_private *dev_priv = dev->dev_private;
11438 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11439 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6ad790c0 11440 struct intel_unpin_work *work;
f326038a 11441
6c51d46f 11442 WARN_ON(!in_interrupt());
d6bbafa1
CW
11443
11444 if (crtc == NULL)
11445 return;
11446
f326038a 11447 spin_lock(&dev->event_lock);
6ad790c0
CW
11448 work = intel_crtc->unpin_work;
11449 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
d6bbafa1 11450 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
6ad790c0 11451 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
d6bbafa1 11452 page_flip_completed(intel_crtc);
6ad790c0 11453 work = NULL;
d6bbafa1 11454 }
6ad790c0
CW
11455 if (work != NULL &&
11456 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
11457 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
f326038a 11458 spin_unlock(&dev->event_lock);
d6bbafa1
CW
11459}
11460
6b95a207
KH
11461static int intel_crtc_page_flip(struct drm_crtc *crtc,
11462 struct drm_framebuffer *fb,
ed8d1975
KP
11463 struct drm_pending_vblank_event *event,
11464 uint32_t page_flip_flags)
6b95a207
KH
11465{
11466 struct drm_device *dev = crtc->dev;
11467 struct drm_i915_private *dev_priv = dev->dev_private;
f4510a27 11468 struct drm_framebuffer *old_fb = crtc->primary->fb;
2ff8fde1 11469 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
6b95a207 11470 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
455a6808 11471 struct drm_plane *primary = crtc->primary;
a071fa00 11472 enum pipe pipe = intel_crtc->pipe;
6b95a207 11473 struct intel_unpin_work *work;
e2f80391 11474 struct intel_engine_cs *engine;
cf5d8a46 11475 bool mmio_flip;
91af127f 11476 struct drm_i915_gem_request *request = NULL;
52e68630 11477 int ret;
6b95a207 11478
2ff8fde1
MR
11479 /*
11480 * drm_mode_page_flip_ioctl() should already catch this, but double
11481 * check to be safe. In the future we may enable pageflipping from
11482 * a disabled primary plane.
11483 */
11484 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11485 return -EBUSY;
11486
e6a595d2 11487 /* Can't change pixel format via MI display flips. */
f4510a27 11488 if (fb->pixel_format != crtc->primary->fb->pixel_format)
e6a595d2
VS
11489 return -EINVAL;
11490
11491 /*
11492 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11493 * Note that pitch changes could also affect these register.
11494 */
11495 if (INTEL_INFO(dev)->gen > 3 &&
f4510a27
MR
11496 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11497 fb->pitches[0] != crtc->primary->fb->pitches[0]))
e6a595d2
VS
11498 return -EINVAL;
11499
f900db47
CW
11500 if (i915_terminally_wedged(&dev_priv->gpu_error))
11501 goto out_hang;
11502
b14c5679 11503 work = kzalloc(sizeof(*work), GFP_KERNEL);
6b95a207
KH
11504 if (work == NULL)
11505 return -ENOMEM;
11506
6b95a207 11507 work->event = event;
b4a98e57 11508 work->crtc = crtc;
ab8d6675 11509 work->old_fb = old_fb;
6b95a207
KH
11510 INIT_WORK(&work->work, intel_unpin_work_fn);
11511
87b6b101 11512 ret = drm_crtc_vblank_get(crtc);
7317c75e
JB
11513 if (ret)
11514 goto free_work;
11515
6b95a207 11516 /* We borrow the event spin lock for protecting unpin_work */
5e2d7afc 11517 spin_lock_irq(&dev->event_lock);
6b95a207 11518 if (intel_crtc->unpin_work) {
d6bbafa1
CW
11519 /* Before declaring the flip queue wedged, check if
11520 * the hardware completed the operation behind our backs.
11521 */
11522 if (__intel_pageflip_stall_check(dev, crtc)) {
11523 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11524 page_flip_completed(intel_crtc);
11525 } else {
11526 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
5e2d7afc 11527 spin_unlock_irq(&dev->event_lock);
468f0b44 11528
d6bbafa1
CW
11529 drm_crtc_vblank_put(crtc);
11530 kfree(work);
11531 return -EBUSY;
11532 }
6b95a207
KH
11533 }
11534 intel_crtc->unpin_work = work;
5e2d7afc 11535 spin_unlock_irq(&dev->event_lock);
6b95a207 11536
b4a98e57
CW
11537 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11538 flush_workqueue(dev_priv->wq);
11539
75dfca80 11540 /* Reference the objects for the scheduled work. */
ab8d6675 11541 drm_framebuffer_reference(work->old_fb);
05394f39 11542 drm_gem_object_reference(&obj->base);
6b95a207 11543
f4510a27 11544 crtc->primary->fb = fb;
afd65eb4 11545 update_state_fb(crtc->primary);
e8216e50 11546 intel_fbc_pre_update(intel_crtc);
1ed1f968 11547
e1f99ce6 11548 work->pending_flip_obj = obj;
e1f99ce6 11549
89ed88ba
CW
11550 ret = i915_mutex_lock_interruptible(dev);
11551 if (ret)
11552 goto cleanup;
11553
b4a98e57 11554 atomic_inc(&intel_crtc->unpin_work_count);
10d83730 11555 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
e1f99ce6 11556
75f7f3ec 11557 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
fd8f507c 11558 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
75f7f3ec 11559
666a4537 11560 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
4a570db5 11561 engine = &dev_priv->engine[BCS];
ab8d6675 11562 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
8e09bf83 11563 /* vlv: DISPLAY_FLIP fails to change tiling */
e2f80391 11564 engine = NULL;
48bf5b2d 11565 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
4a570db5 11566 engine = &dev_priv->engine[BCS];
4fa62c89 11567 } else if (INTEL_INFO(dev)->gen >= 7) {
666796da 11568 engine = i915_gem_request_get_engine(obj->last_write_req);
e2f80391 11569 if (engine == NULL || engine->id != RCS)
4a570db5 11570 engine = &dev_priv->engine[BCS];
4fa62c89 11571 } else {
4a570db5 11572 engine = &dev_priv->engine[RCS];
4fa62c89
VS
11573 }
11574
e2f80391 11575 mmio_flip = use_mmio_flip(engine, obj);
cf5d8a46
CW
11576
11577 /* When using CS flips, we want to emit semaphores between rings.
11578 * However, when using mmio flips we will create a task to do the
11579 * synchronisation, so all we want here is to pin the framebuffer
11580 * into the display plane and skip any waits.
11581 */
7580d774 11582 if (!mmio_flip) {
e2f80391 11583 ret = i915_gem_object_sync(obj, engine, &request);
7580d774
ML
11584 if (ret)
11585 goto cleanup_pending;
11586 }
11587
3465c580 11588 ret = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
8c9f3aaf
JB
11589 if (ret)
11590 goto cleanup_pending;
6b95a207 11591
dedf278c
TU
11592 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
11593 obj, 0);
11594 work->gtt_offset += intel_crtc->dspaddr_offset;
4fa62c89 11595
cf5d8a46 11596 if (mmio_flip) {
86efe24a 11597 ret = intel_queue_mmio_flip(dev, crtc, obj);
d6bbafa1
CW
11598 if (ret)
11599 goto cleanup_unpin;
11600
f06cc1b9
JH
11601 i915_gem_request_assign(&work->flip_queued_req,
11602 obj->last_write_req);
d6bbafa1 11603 } else {
6258fbe2 11604 if (!request) {
e2f80391 11605 request = i915_gem_request_alloc(engine, NULL);
26827088
DG
11606 if (IS_ERR(request)) {
11607 ret = PTR_ERR(request);
6258fbe2 11608 goto cleanup_unpin;
26827088 11609 }
6258fbe2
JH
11610 }
11611
11612 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
d6bbafa1
CW
11613 page_flip_flags);
11614 if (ret)
11615 goto cleanup_unpin;
11616
6258fbe2 11617 i915_gem_request_assign(&work->flip_queued_req, request);
d6bbafa1
CW
11618 }
11619
91af127f 11620 if (request)
75289874 11621 i915_add_request_no_flush(request);
91af127f 11622
1e3feefd 11623 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
d6bbafa1 11624 work->enable_stall_check = true;
4fa62c89 11625
ab8d6675 11626 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
a9ff8714 11627 to_intel_plane(primary)->frontbuffer_bit);
c80ac854 11628 mutex_unlock(&dev->struct_mutex);
a071fa00 11629
a9ff8714
VS
11630 intel_frontbuffer_flip_prepare(dev,
11631 to_intel_plane(primary)->frontbuffer_bit);
6b95a207 11632
e5510fac
JB
11633 trace_i915_flip_request(intel_crtc->plane, obj);
11634
6b95a207 11635 return 0;
96b099fd 11636
4fa62c89 11637cleanup_unpin:
3465c580 11638 intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
8c9f3aaf 11639cleanup_pending:
0aa498d5 11640 if (!IS_ERR_OR_NULL(request))
91af127f 11641 i915_gem_request_cancel(request);
b4a98e57 11642 atomic_dec(&intel_crtc->unpin_work_count);
89ed88ba
CW
11643 mutex_unlock(&dev->struct_mutex);
11644cleanup:
f4510a27 11645 crtc->primary->fb = old_fb;
afd65eb4 11646 update_state_fb(crtc->primary);
89ed88ba
CW
11647
11648 drm_gem_object_unreference_unlocked(&obj->base);
ab8d6675 11649 drm_framebuffer_unreference(work->old_fb);
96b099fd 11650
5e2d7afc 11651 spin_lock_irq(&dev->event_lock);
96b099fd 11652 intel_crtc->unpin_work = NULL;
5e2d7afc 11653 spin_unlock_irq(&dev->event_lock);
96b099fd 11654
87b6b101 11655 drm_crtc_vblank_put(crtc);
7317c75e 11656free_work:
96b099fd
CW
11657 kfree(work);
11658
f900db47 11659 if (ret == -EIO) {
02e0efb5
ML
11660 struct drm_atomic_state *state;
11661 struct drm_plane_state *plane_state;
11662
f900db47 11663out_hang:
02e0efb5
ML
11664 state = drm_atomic_state_alloc(dev);
11665 if (!state)
11666 return -ENOMEM;
11667 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11668
11669retry:
11670 plane_state = drm_atomic_get_plane_state(state, primary);
11671 ret = PTR_ERR_OR_ZERO(plane_state);
11672 if (!ret) {
11673 drm_atomic_set_fb_for_plane(plane_state, fb);
11674
11675 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11676 if (!ret)
11677 ret = drm_atomic_commit(state);
11678 }
11679
11680 if (ret == -EDEADLK) {
11681 drm_modeset_backoff(state->acquire_ctx);
11682 drm_atomic_state_clear(state);
11683 goto retry;
11684 }
11685
11686 if (ret)
11687 drm_atomic_state_free(state);
11688
f0d3dad3 11689 if (ret == 0 && event) {
5e2d7afc 11690 spin_lock_irq(&dev->event_lock);
a071fa00 11691 drm_send_vblank_event(dev, pipe, event);
5e2d7afc 11692 spin_unlock_irq(&dev->event_lock);
f0d3dad3 11693 }
f900db47 11694 }
96b099fd 11695 return ret;
6b95a207
KH
11696}
11697
da20eabd
ML
11698
11699/**
11700 * intel_wm_need_update - Check whether watermarks need updating
11701 * @plane: drm plane
11702 * @state: new plane state
11703 *
11704 * Check current plane state versus the new one to determine whether
11705 * watermarks need to be recalculated.
11706 *
11707 * Returns true or false.
11708 */
11709static bool intel_wm_need_update(struct drm_plane *plane,
11710 struct drm_plane_state *state)
11711{
d21fbe87
MR
11712 struct intel_plane_state *new = to_intel_plane_state(state);
11713 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
11714
11715 /* Update watermarks on tiling or size changes. */
92826fcd
ML
11716 if (new->visible != cur->visible)
11717 return true;
11718
11719 if (!cur->base.fb || !new->base.fb)
11720 return false;
11721
11722 if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
11723 cur->base.rotation != new->base.rotation ||
d21fbe87
MR
11724 drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
11725 drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
11726 drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
11727 drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
2791a16c 11728 return true;
7809e5ae 11729
2791a16c 11730 return false;
7809e5ae
MR
11731}
11732
d21fbe87
MR
11733static bool needs_scaling(struct intel_plane_state *state)
11734{
11735 int src_w = drm_rect_width(&state->src) >> 16;
11736 int src_h = drm_rect_height(&state->src) >> 16;
11737 int dst_w = drm_rect_width(&state->dst);
11738 int dst_h = drm_rect_height(&state->dst);
11739
11740 return (src_w != dst_w || src_h != dst_h);
11741}
11742
da20eabd
ML
11743int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11744 struct drm_plane_state *plane_state)
11745{
ab1d3a0e 11746 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
da20eabd
ML
11747 struct drm_crtc *crtc = crtc_state->crtc;
11748 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11749 struct drm_plane *plane = plane_state->plane;
11750 struct drm_device *dev = crtc->dev;
ed4a6a7c 11751 struct drm_i915_private *dev_priv = to_i915(dev);
da20eabd
ML
11752 struct intel_plane_state *old_plane_state =
11753 to_intel_plane_state(plane->state);
11754 int idx = intel_crtc->base.base.id, ret;
da20eabd
ML
11755 bool mode_changed = needs_modeset(crtc_state);
11756 bool was_crtc_enabled = crtc->state->active;
11757 bool is_crtc_enabled = crtc_state->active;
da20eabd
ML
11758 bool turn_off, turn_on, visible, was_visible;
11759 struct drm_framebuffer *fb = plane_state->fb;
11760
11761 if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
11762 plane->type != DRM_PLANE_TYPE_CURSOR) {
11763 ret = skl_update_scaler_plane(
11764 to_intel_crtc_state(crtc_state),
11765 to_intel_plane_state(plane_state));
11766 if (ret)
11767 return ret;
11768 }
11769
da20eabd
ML
11770 was_visible = old_plane_state->visible;
11771 visible = to_intel_plane_state(plane_state)->visible;
11772
11773 if (!was_crtc_enabled && WARN_ON(was_visible))
11774 was_visible = false;
11775
35c08f43
ML
11776 /*
11777 * Visibility is calculated as if the crtc was on, but
11778 * after scaler setup everything depends on it being off
11779 * when the crtc isn't active.
11780 */
11781 if (!is_crtc_enabled)
11782 to_intel_plane_state(plane_state)->visible = visible = false;
da20eabd
ML
11783
11784 if (!was_visible && !visible)
11785 return 0;
11786
e8861675
ML
11787 if (fb != old_plane_state->base.fb)
11788 pipe_config->fb_changed = true;
11789
da20eabd
ML
11790 turn_off = was_visible && (!visible || mode_changed);
11791 turn_on = visible && (!was_visible || mode_changed);
11792
11793 DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
11794 plane->base.id, fb ? fb->base.id : -1);
11795
11796 DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
11797 plane->base.id, was_visible, visible,
11798 turn_off, turn_on, mode_changed);
11799
caed361d
VS
11800 if (turn_on) {
11801 pipe_config->update_wm_pre = true;
11802
11803 /* must disable cxsr around plane enable/disable */
11804 if (plane->type != DRM_PLANE_TYPE_CURSOR)
11805 pipe_config->disable_cxsr = true;
11806 } else if (turn_off) {
11807 pipe_config->update_wm_post = true;
92826fcd 11808
852eb00d 11809 /* must disable cxsr around plane enable/disable */
e8861675 11810 if (plane->type != DRM_PLANE_TYPE_CURSOR)
ab1d3a0e 11811 pipe_config->disable_cxsr = true;
852eb00d 11812 } else if (intel_wm_need_update(plane, plane_state)) {
caed361d
VS
11813 /* FIXME bollocks */
11814 pipe_config->update_wm_pre = true;
11815 pipe_config->update_wm_post = true;
852eb00d 11816 }
da20eabd 11817
ed4a6a7c 11818 /* Pre-gen9 platforms need two-step watermark updates */
caed361d
VS
11819 if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
11820 INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
ed4a6a7c
MR
11821 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
11822
8be6ca85 11823 if (visible || was_visible)
cd202f69 11824 pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
a9ff8714 11825
31ae71fc
ML
11826 /*
11827 * WaCxSRDisabledForSpriteScaling:ivb
11828 *
11829 * cstate->update_wm was already set above, so this flag will
11830 * take effect when we commit and program watermarks.
11831 */
11832 if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev) &&
11833 needs_scaling(to_intel_plane_state(plane_state)) &&
11834 !needs_scaling(old_plane_state))
11835 pipe_config->disable_lp_wm = true;
d21fbe87 11836
da20eabd
ML
11837 return 0;
11838}
11839
6d3a1ce7
ML
11840static bool encoders_cloneable(const struct intel_encoder *a,
11841 const struct intel_encoder *b)
11842{
11843 /* masks could be asymmetric, so check both ways */
11844 return a == b || (a->cloneable & (1 << b->type) &&
11845 b->cloneable & (1 << a->type));
11846}
11847
11848static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11849 struct intel_crtc *crtc,
11850 struct intel_encoder *encoder)
11851{
11852 struct intel_encoder *source_encoder;
11853 struct drm_connector *connector;
11854 struct drm_connector_state *connector_state;
11855 int i;
11856
11857 for_each_connector_in_state(state, connector, connector_state, i) {
11858 if (connector_state->crtc != &crtc->base)
11859 continue;
11860
11861 source_encoder =
11862 to_intel_encoder(connector_state->best_encoder);
11863 if (!encoders_cloneable(encoder, source_encoder))
11864 return false;
11865 }
11866
11867 return true;
11868}
11869
11870static bool check_encoder_cloning(struct drm_atomic_state *state,
11871 struct intel_crtc *crtc)
11872{
11873 struct intel_encoder *encoder;
11874 struct drm_connector *connector;
11875 struct drm_connector_state *connector_state;
11876 int i;
11877
11878 for_each_connector_in_state(state, connector, connector_state, i) {
11879 if (connector_state->crtc != &crtc->base)
11880 continue;
11881
11882 encoder = to_intel_encoder(connector_state->best_encoder);
11883 if (!check_single_encoder_cloning(state, crtc, encoder))
11884 return false;
11885 }
11886
11887 return true;
11888}
11889
11890static int intel_crtc_atomic_check(struct drm_crtc *crtc,
11891 struct drm_crtc_state *crtc_state)
11892{
cf5a15be 11893 struct drm_device *dev = crtc->dev;
ad421372 11894 struct drm_i915_private *dev_priv = dev->dev_private;
6d3a1ce7 11895 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cf5a15be
ML
11896 struct intel_crtc_state *pipe_config =
11897 to_intel_crtc_state(crtc_state);
6d3a1ce7 11898 struct drm_atomic_state *state = crtc_state->state;
4d20cd86 11899 int ret;
6d3a1ce7
ML
11900 bool mode_changed = needs_modeset(crtc_state);
11901
11902 if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
11903 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11904 return -EINVAL;
11905 }
11906
852eb00d 11907 if (mode_changed && !crtc_state->active)
caed361d 11908 pipe_config->update_wm_post = true;
eddfcbcd 11909
ad421372
ML
11910 if (mode_changed && crtc_state->enable &&
11911 dev_priv->display.crtc_compute_clock &&
8106ddbd 11912 !WARN_ON(pipe_config->shared_dpll)) {
ad421372
ML
11913 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
11914 pipe_config);
11915 if (ret)
11916 return ret;
11917 }
11918
82cf435b
LL
11919 if (crtc_state->color_mgmt_changed) {
11920 ret = intel_color_check(crtc, crtc_state);
11921 if (ret)
11922 return ret;
11923 }
11924
e435d6e5 11925 ret = 0;
86c8bbbe 11926 if (dev_priv->display.compute_pipe_wm) {
e3bddded 11927 ret = dev_priv->display.compute_pipe_wm(pipe_config);
ed4a6a7c
MR
11928 if (ret) {
11929 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
11930 return ret;
11931 }
11932 }
11933
11934 if (dev_priv->display.compute_intermediate_wm &&
11935 !to_intel_atomic_state(state)->skip_intermediate_wm) {
11936 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
11937 return 0;
11938
11939 /*
11940 * Calculate 'intermediate' watermarks that satisfy both the
11941 * old state and the new state. We can program these
11942 * immediately.
11943 */
11944 ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
11945 intel_crtc,
11946 pipe_config);
11947 if (ret) {
11948 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
86c8bbbe 11949 return ret;
ed4a6a7c 11950 }
86c8bbbe
MR
11951 }
11952
e435d6e5
ML
11953 if (INTEL_INFO(dev)->gen >= 9) {
11954 if (mode_changed)
11955 ret = skl_update_scaler_crtc(pipe_config);
11956
11957 if (!ret)
11958 ret = intel_atomic_setup_scalers(dev, intel_crtc,
11959 pipe_config);
11960 }
11961
11962 return ret;
6d3a1ce7
ML
11963}
11964
65b38e0d 11965static const struct drm_crtc_helper_funcs intel_helper_funcs = {
f6e5b160 11966 .mode_set_base_atomic = intel_pipe_set_base_atomic,
ea2c67bb
MR
11967 .atomic_begin = intel_begin_crtc_commit,
11968 .atomic_flush = intel_finish_crtc_commit,
6d3a1ce7 11969 .atomic_check = intel_crtc_atomic_check,
f6e5b160
CW
11970};
11971
d29b2f9d
ACO
11972static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11973{
11974 struct intel_connector *connector;
11975
11976 for_each_intel_connector(dev, connector) {
11977 if (connector->base.encoder) {
11978 connector->base.state->best_encoder =
11979 connector->base.encoder;
11980 connector->base.state->crtc =
11981 connector->base.encoder->crtc;
11982 } else {
11983 connector->base.state->best_encoder = NULL;
11984 connector->base.state->crtc = NULL;
11985 }
11986 }
11987}
11988
050f7aeb 11989static void
eba905b2 11990connected_sink_compute_bpp(struct intel_connector *connector,
5cec258b 11991 struct intel_crtc_state *pipe_config)
050f7aeb
DV
11992{
11993 int bpp = pipe_config->pipe_bpp;
11994
11995 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
11996 connector->base.base.id,
c23cc417 11997 connector->base.name);
050f7aeb
DV
11998
11999 /* Don't use an invalid EDID bpc value */
12000 if (connector->base.display_info.bpc &&
12001 connector->base.display_info.bpc * 3 < bpp) {
12002 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
12003 bpp, connector->base.display_info.bpc*3);
12004 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
12005 }
12006
013dd9e0
JN
12007 /* Clamp bpp to default limit on screens without EDID 1.4 */
12008 if (connector->base.display_info.bpc == 0) {
12009 int type = connector->base.connector_type;
12010 int clamp_bpp = 24;
12011
12012 /* Fall back to 18 bpp when DP sink capability is unknown. */
12013 if (type == DRM_MODE_CONNECTOR_DisplayPort ||
12014 type == DRM_MODE_CONNECTOR_eDP)
12015 clamp_bpp = 18;
12016
12017 if (bpp > clamp_bpp) {
12018 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of %d\n",
12019 bpp, clamp_bpp);
12020 pipe_config->pipe_bpp = clamp_bpp;
12021 }
050f7aeb
DV
12022 }
12023}
12024
4e53c2e0 12025static int
050f7aeb 12026compute_baseline_pipe_bpp(struct intel_crtc *crtc,
5cec258b 12027 struct intel_crtc_state *pipe_config)
4e53c2e0 12028{
050f7aeb 12029 struct drm_device *dev = crtc->base.dev;
1486017f 12030 struct drm_atomic_state *state;
da3ced29
ACO
12031 struct drm_connector *connector;
12032 struct drm_connector_state *connector_state;
1486017f 12033 int bpp, i;
4e53c2e0 12034
666a4537 12035 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
4e53c2e0 12036 bpp = 10*3;
d328c9d7
DV
12037 else if (INTEL_INFO(dev)->gen >= 5)
12038 bpp = 12*3;
12039 else
12040 bpp = 8*3;
12041
4e53c2e0 12042
4e53c2e0
DV
12043 pipe_config->pipe_bpp = bpp;
12044
1486017f
ACO
12045 state = pipe_config->base.state;
12046
4e53c2e0 12047 /* Clamp display bpp to EDID value */
da3ced29
ACO
12048 for_each_connector_in_state(state, connector, connector_state, i) {
12049 if (connector_state->crtc != &crtc->base)
4e53c2e0
DV
12050 continue;
12051
da3ced29
ACO
12052 connected_sink_compute_bpp(to_intel_connector(connector),
12053 pipe_config);
4e53c2e0
DV
12054 }
12055
12056 return bpp;
12057}
12058
644db711
DV
12059static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
12060{
12061 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
12062 "type: 0x%x flags: 0x%x\n",
1342830c 12063 mode->crtc_clock,
644db711
DV
12064 mode->crtc_hdisplay, mode->crtc_hsync_start,
12065 mode->crtc_hsync_end, mode->crtc_htotal,
12066 mode->crtc_vdisplay, mode->crtc_vsync_start,
12067 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
12068}
12069
c0b03411 12070static void intel_dump_pipe_config(struct intel_crtc *crtc,
5cec258b 12071 struct intel_crtc_state *pipe_config,
c0b03411
DV
12072 const char *context)
12073{
6a60cd87
CK
12074 struct drm_device *dev = crtc->base.dev;
12075 struct drm_plane *plane;
12076 struct intel_plane *intel_plane;
12077 struct intel_plane_state *state;
12078 struct drm_framebuffer *fb;
12079
12080 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
12081 context, pipe_config, pipe_name(crtc->pipe));
c0b03411 12082
da205630 12083 DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
c0b03411
DV
12084 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
12085 pipe_config->pipe_bpp, pipe_config->dither);
12086 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12087 pipe_config->has_pch_encoder,
12088 pipe_config->fdi_lanes,
12089 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
12090 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
12091 pipe_config->fdi_m_n.tu);
90a6b7b0 12092 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
eb14cb74 12093 pipe_config->has_dp_encoder,
90a6b7b0 12094 pipe_config->lane_count,
eb14cb74
VS
12095 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
12096 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
12097 pipe_config->dp_m_n.tu);
b95af8be 12098
90a6b7b0 12099 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
b95af8be 12100 pipe_config->has_dp_encoder,
90a6b7b0 12101 pipe_config->lane_count,
b95af8be
VK
12102 pipe_config->dp_m2_n2.gmch_m,
12103 pipe_config->dp_m2_n2.gmch_n,
12104 pipe_config->dp_m2_n2.link_m,
12105 pipe_config->dp_m2_n2.link_n,
12106 pipe_config->dp_m2_n2.tu);
12107
55072d19
DV
12108 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12109 pipe_config->has_audio,
12110 pipe_config->has_infoframe);
12111
c0b03411 12112 DRM_DEBUG_KMS("requested mode:\n");
2d112de7 12113 drm_mode_debug_printmodeline(&pipe_config->base.mode);
c0b03411 12114 DRM_DEBUG_KMS("adjusted mode:\n");
2d112de7
ACO
12115 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12116 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
d71b8d4a 12117 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
37327abd
VS
12118 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12119 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
0ec463d3
TU
12120 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12121 crtc->num_scalers,
12122 pipe_config->scaler_state.scaler_users,
12123 pipe_config->scaler_state.scaler_id);
c0b03411
DV
12124 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12125 pipe_config->gmch_pfit.control,
12126 pipe_config->gmch_pfit.pgm_ratios,
12127 pipe_config->gmch_pfit.lvds_border_bits);
fd4daa9c 12128 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
c0b03411 12129 pipe_config->pch_pfit.pos,
fd4daa9c
CW
12130 pipe_config->pch_pfit.size,
12131 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
42db64ef 12132 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
cf532bb2 12133 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
6a60cd87 12134
415ff0f6 12135 if (IS_BROXTON(dev)) {
05712c15 12136 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
415ff0f6 12137 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
c8453338 12138 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
415ff0f6
TU
12139 pipe_config->ddi_pll_sel,
12140 pipe_config->dpll_hw_state.ebb0,
05712c15 12141 pipe_config->dpll_hw_state.ebb4,
415ff0f6
TU
12142 pipe_config->dpll_hw_state.pll0,
12143 pipe_config->dpll_hw_state.pll1,
12144 pipe_config->dpll_hw_state.pll2,
12145 pipe_config->dpll_hw_state.pll3,
12146 pipe_config->dpll_hw_state.pll6,
12147 pipe_config->dpll_hw_state.pll8,
05712c15 12148 pipe_config->dpll_hw_state.pll9,
c8453338 12149 pipe_config->dpll_hw_state.pll10,
415ff0f6 12150 pipe_config->dpll_hw_state.pcsdw12);
ef11bdb3 12151 } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
415ff0f6
TU
12152 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12153 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12154 pipe_config->ddi_pll_sel,
12155 pipe_config->dpll_hw_state.ctrl1,
12156 pipe_config->dpll_hw_state.cfgcr1,
12157 pipe_config->dpll_hw_state.cfgcr2);
12158 } else if (HAS_DDI(dev)) {
1260f07e 12159 DRM_DEBUG_KMS("ddi_pll_sel: 0x%x; dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
415ff0f6 12160 pipe_config->ddi_pll_sel,
00490c22
ML
12161 pipe_config->dpll_hw_state.wrpll,
12162 pipe_config->dpll_hw_state.spll);
415ff0f6
TU
12163 } else {
12164 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12165 "fp0: 0x%x, fp1: 0x%x\n",
12166 pipe_config->dpll_hw_state.dpll,
12167 pipe_config->dpll_hw_state.dpll_md,
12168 pipe_config->dpll_hw_state.fp0,
12169 pipe_config->dpll_hw_state.fp1);
12170 }
12171
6a60cd87
CK
12172 DRM_DEBUG_KMS("planes on this crtc\n");
12173 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12174 intel_plane = to_intel_plane(plane);
12175 if (intel_plane->pipe != crtc->pipe)
12176 continue;
12177
12178 state = to_intel_plane_state(plane->state);
12179 fb = state->base.fb;
12180 if (!fb) {
12181 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
12182 "disabled, scaler_id = %d\n",
12183 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12184 plane->base.id, intel_plane->pipe,
12185 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
12186 drm_plane_index(plane), state->scaler_id);
12187 continue;
12188 }
12189
12190 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
12191 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12192 plane->base.id, intel_plane->pipe,
12193 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
12194 drm_plane_index(plane));
12195 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
12196 fb->base.id, fb->width, fb->height, fb->pixel_format);
12197 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
12198 state->scaler_id,
12199 state->src.x1 >> 16, state->src.y1 >> 16,
12200 drm_rect_width(&state->src) >> 16,
12201 drm_rect_height(&state->src) >> 16,
12202 state->dst.x1, state->dst.y1,
12203 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
12204 }
c0b03411
DV
12205}
12206
5448a00d 12207static bool check_digital_port_conflicts(struct drm_atomic_state *state)
00f0b378 12208{
5448a00d 12209 struct drm_device *dev = state->dev;
da3ced29 12210 struct drm_connector *connector;
00f0b378
VS
12211 unsigned int used_ports = 0;
12212
12213 /*
12214 * Walk the connector list instead of the encoder
12215 * list to detect the problem on ddi platforms
12216 * where there's just one encoder per digital port.
12217 */
0bff4858
VS
12218 drm_for_each_connector(connector, dev) {
12219 struct drm_connector_state *connector_state;
12220 struct intel_encoder *encoder;
12221
12222 connector_state = drm_atomic_get_existing_connector_state(state, connector);
12223 if (!connector_state)
12224 connector_state = connector->state;
12225
5448a00d 12226 if (!connector_state->best_encoder)
00f0b378
VS
12227 continue;
12228
5448a00d
ACO
12229 encoder = to_intel_encoder(connector_state->best_encoder);
12230
12231 WARN_ON(!connector_state->crtc);
00f0b378
VS
12232
12233 switch (encoder->type) {
12234 unsigned int port_mask;
12235 case INTEL_OUTPUT_UNKNOWN:
12236 if (WARN_ON(!HAS_DDI(dev)))
12237 break;
12238 case INTEL_OUTPUT_DISPLAYPORT:
12239 case INTEL_OUTPUT_HDMI:
12240 case INTEL_OUTPUT_EDP:
12241 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12242
12243 /* the same port mustn't appear more than once */
12244 if (used_ports & port_mask)
12245 return false;
12246
12247 used_ports |= port_mask;
12248 default:
12249 break;
12250 }
12251 }
12252
12253 return true;
12254}
12255
83a57153
ACO
12256static void
12257clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12258{
12259 struct drm_crtc_state tmp_state;
663a3640 12260 struct intel_crtc_scaler_state scaler_state;
4978cc93 12261 struct intel_dpll_hw_state dpll_hw_state;
8106ddbd 12262 struct intel_shared_dpll *shared_dpll;
8504c74c 12263 uint32_t ddi_pll_sel;
c4e2d043 12264 bool force_thru;
83a57153 12265
7546a384
ACO
12266 /* FIXME: before the switch to atomic started, a new pipe_config was
12267 * kzalloc'd. Code that depends on any field being zero should be
12268 * fixed, so that the crtc_state can be safely duplicated. For now,
12269 * only fields that are know to not cause problems are preserved. */
12270
83a57153 12271 tmp_state = crtc_state->base;
663a3640 12272 scaler_state = crtc_state->scaler_state;
4978cc93
ACO
12273 shared_dpll = crtc_state->shared_dpll;
12274 dpll_hw_state = crtc_state->dpll_hw_state;
8504c74c 12275 ddi_pll_sel = crtc_state->ddi_pll_sel;
c4e2d043 12276 force_thru = crtc_state->pch_pfit.force_thru;
4978cc93 12277
83a57153 12278 memset(crtc_state, 0, sizeof *crtc_state);
4978cc93 12279
83a57153 12280 crtc_state->base = tmp_state;
663a3640 12281 crtc_state->scaler_state = scaler_state;
4978cc93
ACO
12282 crtc_state->shared_dpll = shared_dpll;
12283 crtc_state->dpll_hw_state = dpll_hw_state;
8504c74c 12284 crtc_state->ddi_pll_sel = ddi_pll_sel;
c4e2d043 12285 crtc_state->pch_pfit.force_thru = force_thru;
83a57153
ACO
12286}
12287
548ee15b 12288static int
b8cecdf5 12289intel_modeset_pipe_config(struct drm_crtc *crtc,
b359283a 12290 struct intel_crtc_state *pipe_config)
ee7b9f93 12291{
b359283a 12292 struct drm_atomic_state *state = pipe_config->base.state;
7758a113 12293 struct intel_encoder *encoder;
da3ced29 12294 struct drm_connector *connector;
0b901879 12295 struct drm_connector_state *connector_state;
d328c9d7 12296 int base_bpp, ret = -EINVAL;
0b901879 12297 int i;
e29c22c0 12298 bool retry = true;
ee7b9f93 12299
83a57153 12300 clear_intel_crtc_state(pipe_config);
7758a113 12301
e143a21c
DV
12302 pipe_config->cpu_transcoder =
12303 (enum transcoder) to_intel_crtc(crtc)->pipe;
b8cecdf5 12304
2960bc9c
ID
12305 /*
12306 * Sanitize sync polarity flags based on requested ones. If neither
12307 * positive or negative polarity is requested, treat this as meaning
12308 * negative polarity.
12309 */
2d112de7 12310 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 12311 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
2d112de7 12312 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
2960bc9c 12313
2d112de7 12314 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 12315 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
2d112de7 12316 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
2960bc9c 12317
d328c9d7
DV
12318 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12319 pipe_config);
12320 if (base_bpp < 0)
4e53c2e0
DV
12321 goto fail;
12322
e41a56be
VS
12323 /*
12324 * Determine the real pipe dimensions. Note that stereo modes can
12325 * increase the actual pipe size due to the frame doubling and
12326 * insertion of additional space for blanks between the frame. This
12327 * is stored in the crtc timings. We use the requested mode to do this
12328 * computation to clearly distinguish it from the adjusted mode, which
12329 * can be changed by the connectors in the below retry loop.
12330 */
2d112de7 12331 drm_crtc_get_hv_timing(&pipe_config->base.mode,
ecb7e16b
GP
12332 &pipe_config->pipe_src_w,
12333 &pipe_config->pipe_src_h);
e41a56be 12334
e29c22c0 12335encoder_retry:
ef1b460d 12336 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 12337 pipe_config->port_clock = 0;
ef1b460d 12338 pipe_config->pixel_multiplier = 1;
ff9a6750 12339
135c81b8 12340 /* Fill in default crtc timings, allow encoders to overwrite them. */
2d112de7
ACO
12341 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12342 CRTC_STEREO_DOUBLE);
135c81b8 12343
7758a113
DV
12344 /* Pass our mode to the connectors and the CRTC to give them a chance to
12345 * adjust it according to limitations or connector properties, and also
12346 * a chance to reject the mode entirely.
47f1c6c9 12347 */
da3ced29 12348 for_each_connector_in_state(state, connector, connector_state, i) {
0b901879 12349 if (connector_state->crtc != crtc)
7758a113 12350 continue;
7ae89233 12351
0b901879
ACO
12352 encoder = to_intel_encoder(connector_state->best_encoder);
12353
efea6e8e
DV
12354 if (!(encoder->compute_config(encoder, pipe_config))) {
12355 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
DV
12356 goto fail;
12357 }
ee7b9f93 12358 }
47f1c6c9 12359
ff9a6750
DV
12360 /* Set default port clock if not overwritten by the encoder. Needs to be
12361 * done afterwards in case the encoder adjusts the mode. */
12362 if (!pipe_config->port_clock)
2d112de7 12363 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
241bfc38 12364 * pipe_config->pixel_multiplier;
ff9a6750 12365
a43f6e0f 12366 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 12367 if (ret < 0) {
7758a113
DV
12368 DRM_DEBUG_KMS("CRTC fixup failed\n");
12369 goto fail;
ee7b9f93 12370 }
e29c22c0
DV
12371
12372 if (ret == RETRY) {
12373 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12374 ret = -EINVAL;
12375 goto fail;
12376 }
12377
12378 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12379 retry = false;
12380 goto encoder_retry;
12381 }
12382
e8fa4270
DV
12383 /* Dithering seems to not pass-through bits correctly when it should, so
12384 * only enable it on 6bpc panels. */
12385 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
62f0ace5 12386 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
d328c9d7 12387 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
4e53c2e0 12388
7758a113 12389fail:
548ee15b 12390 return ret;
ee7b9f93 12391}
47f1c6c9 12392
ea9d758d 12393static void
4740b0f2 12394intel_modeset_update_crtc_state(struct drm_atomic_state *state)
ea9d758d 12395{
0a9ab303
ACO
12396 struct drm_crtc *crtc;
12397 struct drm_crtc_state *crtc_state;
8a75d157 12398 int i;
ea9d758d 12399
7668851f 12400 /* Double check state. */
8a75d157 12401 for_each_crtc_in_state(state, crtc, crtc_state, i) {
3cb480bc 12402 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
fc467a22
ML
12403
12404 /* Update hwmode for vblank functions */
12405 if (crtc->state->active)
12406 crtc->hwmode = crtc->state->adjusted_mode;
12407 else
12408 crtc->hwmode.crtc_clock = 0;
61067a5e
ML
12409
12410 /*
12411 * Update legacy state to satisfy fbc code. This can
12412 * be removed when fbc uses the atomic state.
12413 */
12414 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
12415 struct drm_plane_state *plane_state = crtc->primary->state;
12416
12417 crtc->primary->fb = plane_state->fb;
12418 crtc->x = plane_state->src_x >> 16;
12419 crtc->y = plane_state->src_y >> 16;
12420 }
ea9d758d 12421 }
ea9d758d
DV
12422}
12423
3bd26263 12424static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 12425{
3bd26263 12426 int diff;
f1f644dc
JB
12427
12428 if (clock1 == clock2)
12429 return true;
12430
12431 if (!clock1 || !clock2)
12432 return false;
12433
12434 diff = abs(clock1 - clock2);
12435
12436 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12437 return true;
12438
12439 return false;
12440}
12441
25c5b266
DV
12442#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12443 list_for_each_entry((intel_crtc), \
12444 &(dev)->mode_config.crtc_list, \
12445 base.head) \
95150bdf 12446 for_each_if (mask & (1 <<(intel_crtc)->pipe))
25c5b266 12447
cfb23ed6
ML
12448static bool
12449intel_compare_m_n(unsigned int m, unsigned int n,
12450 unsigned int m2, unsigned int n2,
12451 bool exact)
12452{
12453 if (m == m2 && n == n2)
12454 return true;
12455
12456 if (exact || !m || !n || !m2 || !n2)
12457 return false;
12458
12459 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
12460
31d10b57
ML
12461 if (n > n2) {
12462 while (n > n2) {
cfb23ed6
ML
12463 m2 <<= 1;
12464 n2 <<= 1;
12465 }
31d10b57
ML
12466 } else if (n < n2) {
12467 while (n < n2) {
cfb23ed6
ML
12468 m <<= 1;
12469 n <<= 1;
12470 }
12471 }
12472
31d10b57
ML
12473 if (n != n2)
12474 return false;
12475
12476 return intel_fuzzy_clock_check(m, m2);
cfb23ed6
ML
12477}
12478
12479static bool
12480intel_compare_link_m_n(const struct intel_link_m_n *m_n,
12481 struct intel_link_m_n *m2_n2,
12482 bool adjust)
12483{
12484 if (m_n->tu == m2_n2->tu &&
12485 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
12486 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
12487 intel_compare_m_n(m_n->link_m, m_n->link_n,
12488 m2_n2->link_m, m2_n2->link_n, !adjust)) {
12489 if (adjust)
12490 *m2_n2 = *m_n;
12491
12492 return true;
12493 }
12494
12495 return false;
12496}
12497
0e8ffe1b 12498static bool
2fa2fe9a 12499intel_pipe_config_compare(struct drm_device *dev,
5cec258b 12500 struct intel_crtc_state *current_config,
cfb23ed6
ML
12501 struct intel_crtc_state *pipe_config,
12502 bool adjust)
0e8ffe1b 12503{
cfb23ed6
ML
12504 bool ret = true;
12505
12506#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
12507 do { \
12508 if (!adjust) \
12509 DRM_ERROR(fmt, ##__VA_ARGS__); \
12510 else \
12511 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
12512 } while (0)
12513
66e985c0
DV
12514#define PIPE_CONF_CHECK_X(name) \
12515 if (current_config->name != pipe_config->name) { \
cfb23ed6 12516 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
66e985c0
DV
12517 "(expected 0x%08x, found 0x%08x)\n", \
12518 current_config->name, \
12519 pipe_config->name); \
cfb23ed6 12520 ret = false; \
66e985c0
DV
12521 }
12522
08a24034
DV
12523#define PIPE_CONF_CHECK_I(name) \
12524 if (current_config->name != pipe_config->name) { \
cfb23ed6 12525 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
08a24034
DV
12526 "(expected %i, found %i)\n", \
12527 current_config->name, \
12528 pipe_config->name); \
cfb23ed6
ML
12529 ret = false; \
12530 }
12531
8106ddbd
ACO
12532#define PIPE_CONF_CHECK_P(name) \
12533 if (current_config->name != pipe_config->name) { \
12534 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12535 "(expected %p, found %p)\n", \
12536 current_config->name, \
12537 pipe_config->name); \
12538 ret = false; \
12539 }
12540
cfb23ed6
ML
12541#define PIPE_CONF_CHECK_M_N(name) \
12542 if (!intel_compare_link_m_n(&current_config->name, \
12543 &pipe_config->name,\
12544 adjust)) { \
12545 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12546 "(expected tu %i gmch %i/%i link %i/%i, " \
12547 "found tu %i, gmch %i/%i link %i/%i)\n", \
12548 current_config->name.tu, \
12549 current_config->name.gmch_m, \
12550 current_config->name.gmch_n, \
12551 current_config->name.link_m, \
12552 current_config->name.link_n, \
12553 pipe_config->name.tu, \
12554 pipe_config->name.gmch_m, \
12555 pipe_config->name.gmch_n, \
12556 pipe_config->name.link_m, \
12557 pipe_config->name.link_n); \
12558 ret = false; \
12559 }
12560
12561#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12562 if (!intel_compare_link_m_n(&current_config->name, \
12563 &pipe_config->name, adjust) && \
12564 !intel_compare_link_m_n(&current_config->alt_name, \
12565 &pipe_config->name, adjust)) { \
12566 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12567 "(expected tu %i gmch %i/%i link %i/%i, " \
12568 "or tu %i gmch %i/%i link %i/%i, " \
12569 "found tu %i, gmch %i/%i link %i/%i)\n", \
12570 current_config->name.tu, \
12571 current_config->name.gmch_m, \
12572 current_config->name.gmch_n, \
12573 current_config->name.link_m, \
12574 current_config->name.link_n, \
12575 current_config->alt_name.tu, \
12576 current_config->alt_name.gmch_m, \
12577 current_config->alt_name.gmch_n, \
12578 current_config->alt_name.link_m, \
12579 current_config->alt_name.link_n, \
12580 pipe_config->name.tu, \
12581 pipe_config->name.gmch_m, \
12582 pipe_config->name.gmch_n, \
12583 pipe_config->name.link_m, \
12584 pipe_config->name.link_n); \
12585 ret = false; \
88adfff1
DV
12586 }
12587
b95af8be
VK
12588/* This is required for BDW+ where there is only one set of registers for
12589 * switching between high and low RR.
12590 * This macro can be used whenever a comparison has to be made between one
12591 * hw state and multiple sw state variables.
12592 */
12593#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
12594 if ((current_config->name != pipe_config->name) && \
12595 (current_config->alt_name != pipe_config->name)) { \
cfb23ed6 12596 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
b95af8be
VK
12597 "(expected %i or %i, found %i)\n", \
12598 current_config->name, \
12599 current_config->alt_name, \
12600 pipe_config->name); \
cfb23ed6 12601 ret = false; \
b95af8be
VK
12602 }
12603
1bd1bd80
DV
12604#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12605 if ((current_config->name ^ pipe_config->name) & (mask)) { \
cfb23ed6 12606 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
1bd1bd80
DV
12607 "(expected %i, found %i)\n", \
12608 current_config->name & (mask), \
12609 pipe_config->name & (mask)); \
cfb23ed6 12610 ret = false; \
1bd1bd80
DV
12611 }
12612
5e550656
VS
12613#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12614 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
cfb23ed6 12615 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
5e550656
VS
12616 "(expected %i, found %i)\n", \
12617 current_config->name, \
12618 pipe_config->name); \
cfb23ed6 12619 ret = false; \
5e550656
VS
12620 }
12621
bb760063
DV
12622#define PIPE_CONF_QUIRK(quirk) \
12623 ((current_config->quirks | pipe_config->quirks) & (quirk))
12624
eccb140b
DV
12625 PIPE_CONF_CHECK_I(cpu_transcoder);
12626
08a24034
DV
12627 PIPE_CONF_CHECK_I(has_pch_encoder);
12628 PIPE_CONF_CHECK_I(fdi_lanes);
cfb23ed6 12629 PIPE_CONF_CHECK_M_N(fdi_m_n);
08a24034 12630
eb14cb74 12631 PIPE_CONF_CHECK_I(has_dp_encoder);
90a6b7b0 12632 PIPE_CONF_CHECK_I(lane_count);
b95af8be
VK
12633
12634 if (INTEL_INFO(dev)->gen < 8) {
cfb23ed6
ML
12635 PIPE_CONF_CHECK_M_N(dp_m_n);
12636
cfb23ed6
ML
12637 if (current_config->has_drrs)
12638 PIPE_CONF_CHECK_M_N(dp_m2_n2);
12639 } else
12640 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
eb14cb74 12641
a65347ba
JN
12642 PIPE_CONF_CHECK_I(has_dsi_encoder);
12643
2d112de7
ACO
12644 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12645 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12646 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12647 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12648 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12649 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
1bd1bd80 12650
2d112de7
ACO
12651 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12652 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12653 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12654 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12655 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12656 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
1bd1bd80 12657
c93f54cf 12658 PIPE_CONF_CHECK_I(pixel_multiplier);
6897b4b5 12659 PIPE_CONF_CHECK_I(has_hdmi_sink);
b5a9fa09 12660 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
666a4537 12661 IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
b5a9fa09 12662 PIPE_CONF_CHECK_I(limited_color_range);
e43823ec 12663 PIPE_CONF_CHECK_I(has_infoframe);
6c49f241 12664
9ed109a7
DV
12665 PIPE_CONF_CHECK_I(has_audio);
12666
2d112de7 12667 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
1bd1bd80
DV
12668 DRM_MODE_FLAG_INTERLACE);
12669
bb760063 12670 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
2d112de7 12671 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12672 DRM_MODE_FLAG_PHSYNC);
2d112de7 12673 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12674 DRM_MODE_FLAG_NHSYNC);
2d112de7 12675 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12676 DRM_MODE_FLAG_PVSYNC);
2d112de7 12677 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063
DV
12678 DRM_MODE_FLAG_NVSYNC);
12679 }
045ac3b5 12680
333b8ca8 12681 PIPE_CONF_CHECK_X(gmch_pfit.control);
e2ff2d4a
DV
12682 /* pfit ratios are autocomputed by the hw on gen4+ */
12683 if (INTEL_INFO(dev)->gen < 4)
12684 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
333b8ca8 12685 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
9953599b 12686
bfd16b2a
ML
12687 if (!adjust) {
12688 PIPE_CONF_CHECK_I(pipe_src_w);
12689 PIPE_CONF_CHECK_I(pipe_src_h);
12690
12691 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12692 if (current_config->pch_pfit.enabled) {
12693 PIPE_CONF_CHECK_X(pch_pfit.pos);
12694 PIPE_CONF_CHECK_X(pch_pfit.size);
12695 }
2fa2fe9a 12696
7aefe2b5
ML
12697 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12698 }
a1b2278e 12699
e59150dc
JB
12700 /* BDW+ don't expose a synchronous way to read the state */
12701 if (IS_HASWELL(dev))
12702 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 12703
282740f7
VS
12704 PIPE_CONF_CHECK_I(double_wide);
12705
26804afd
DV
12706 PIPE_CONF_CHECK_X(ddi_pll_sel);
12707
8106ddbd 12708 PIPE_CONF_CHECK_P(shared_dpll);
66e985c0 12709 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 12710 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
DV
12711 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12712 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
d452c5b6 12713 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
00490c22 12714 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
3f4cd19f
DL
12715 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12716 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12717 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
c0d43d62 12718
42571aef
VS
12719 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12720 PIPE_CONF_CHECK_I(pipe_bpp);
12721
2d112de7 12722 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
a9a7e98a 12723 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 12724
66e985c0 12725#undef PIPE_CONF_CHECK_X
08a24034 12726#undef PIPE_CONF_CHECK_I
8106ddbd 12727#undef PIPE_CONF_CHECK_P
b95af8be 12728#undef PIPE_CONF_CHECK_I_ALT
1bd1bd80 12729#undef PIPE_CONF_CHECK_FLAGS
5e550656 12730#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 12731#undef PIPE_CONF_QUIRK
cfb23ed6 12732#undef INTEL_ERR_OR_DBG_KMS
88adfff1 12733
cfb23ed6 12734 return ret;
0e8ffe1b
DV
12735}
12736
e3b247da
VS
12737static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
12738 const struct intel_crtc_state *pipe_config)
12739{
12740 if (pipe_config->has_pch_encoder) {
21a727b3 12741 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
e3b247da
VS
12742 &pipe_config->fdi_m_n);
12743 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
12744
12745 /*
12746 * FDI already provided one idea for the dotclock.
12747 * Yell if the encoder disagrees.
12748 */
12749 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
12750 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
12751 fdi_dotclock, dotclock);
12752 }
12753}
12754
08db6652
DL
12755static void check_wm_state(struct drm_device *dev)
12756{
12757 struct drm_i915_private *dev_priv = dev->dev_private;
12758 struct skl_ddb_allocation hw_ddb, *sw_ddb;
12759 struct intel_crtc *intel_crtc;
12760 int plane;
12761
12762 if (INTEL_INFO(dev)->gen < 9)
12763 return;
12764
12765 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12766 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12767
12768 for_each_intel_crtc(dev, intel_crtc) {
12769 struct skl_ddb_entry *hw_entry, *sw_entry;
12770 const enum pipe pipe = intel_crtc->pipe;
12771
12772 if (!intel_crtc->active)
12773 continue;
12774
12775 /* planes */
dd740780 12776 for_each_plane(dev_priv, pipe, plane) {
08db6652
DL
12777 hw_entry = &hw_ddb.plane[pipe][plane];
12778 sw_entry = &sw_ddb->plane[pipe][plane];
12779
12780 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12781 continue;
12782
12783 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12784 "(expected (%u,%u), found (%u,%u))\n",
12785 pipe_name(pipe), plane + 1,
12786 sw_entry->start, sw_entry->end,
12787 hw_entry->start, hw_entry->end);
12788 }
12789
12790 /* cursor */
4969d33e
MR
12791 hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
12792 sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
08db6652
DL
12793
12794 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12795 continue;
12796
12797 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12798 "(expected (%u,%u), found (%u,%u))\n",
12799 pipe_name(pipe),
12800 sw_entry->start, sw_entry->end,
12801 hw_entry->start, hw_entry->end);
12802 }
12803}
12804
91d1b4bd 12805static void
35dd3c64
ML
12806check_connector_state(struct drm_device *dev,
12807 struct drm_atomic_state *old_state)
8af6cf88 12808{
35dd3c64
ML
12809 struct drm_connector_state *old_conn_state;
12810 struct drm_connector *connector;
12811 int i;
8af6cf88 12812
35dd3c64
ML
12813 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
12814 struct drm_encoder *encoder = connector->encoder;
12815 struct drm_connector_state *state = connector->state;
ad3c558f 12816
8af6cf88
DV
12817 /* This also checks the encoder/connector hw state with the
12818 * ->get_hw_state callbacks. */
35dd3c64 12819 intel_connector_check_state(to_intel_connector(connector));
8af6cf88 12820
ad3c558f 12821 I915_STATE_WARN(state->best_encoder != encoder,
35dd3c64 12822 "connector's atomic encoder doesn't match legacy encoder\n");
8af6cf88 12823 }
91d1b4bd
DV
12824}
12825
12826static void
12827check_encoder_state(struct drm_device *dev)
12828{
12829 struct intel_encoder *encoder;
12830 struct intel_connector *connector;
8af6cf88 12831
b2784e15 12832 for_each_intel_encoder(dev, encoder) {
8af6cf88 12833 bool enabled = false;
4d20cd86 12834 enum pipe pipe;
8af6cf88
DV
12835
12836 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12837 encoder->base.base.id,
8e329a03 12838 encoder->base.name);
8af6cf88 12839
3a3371ff 12840 for_each_intel_connector(dev, connector) {
4d20cd86 12841 if (connector->base.state->best_encoder != &encoder->base)
8af6cf88
DV
12842 continue;
12843 enabled = true;
ad3c558f
ML
12844
12845 I915_STATE_WARN(connector->base.state->crtc !=
12846 encoder->base.crtc,
12847 "connector's crtc doesn't match encoder crtc\n");
8af6cf88 12848 }
0e32b39c 12849
e2c719b7 12850 I915_STATE_WARN(!!encoder->base.crtc != enabled,
8af6cf88
DV
12851 "encoder's enabled state mismatch "
12852 "(expected %i, found %i)\n",
12853 !!encoder->base.crtc, enabled);
7c60d198
ML
12854
12855 if (!encoder->base.crtc) {
4d20cd86 12856 bool active;
7c60d198 12857
4d20cd86
ML
12858 active = encoder->get_hw_state(encoder, &pipe);
12859 I915_STATE_WARN(active,
12860 "encoder detached but still enabled on pipe %c.\n",
12861 pipe_name(pipe));
7c60d198 12862 }
8af6cf88 12863 }
91d1b4bd
DV
12864}
12865
12866static void
4d20cd86 12867check_crtc_state(struct drm_device *dev, struct drm_atomic_state *old_state)
91d1b4bd 12868{
fbee40df 12869 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd 12870 struct intel_encoder *encoder;
4d20cd86
ML
12871 struct drm_crtc_state *old_crtc_state;
12872 struct drm_crtc *crtc;
12873 int i;
8af6cf88 12874
4d20cd86
ML
12875 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
12876 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12877 struct intel_crtc_state *pipe_config, *sw_config;
7b89b8de 12878 bool active;
8af6cf88 12879
bfd16b2a
ML
12880 if (!needs_modeset(crtc->state) &&
12881 !to_intel_crtc_state(crtc->state)->update_pipe)
4d20cd86 12882 continue;
045ac3b5 12883
4d20cd86
ML
12884 __drm_atomic_helper_crtc_destroy_state(crtc, old_crtc_state);
12885 pipe_config = to_intel_crtc_state(old_crtc_state);
12886 memset(pipe_config, 0, sizeof(*pipe_config));
12887 pipe_config->base.crtc = crtc;
12888 pipe_config->base.state = old_state;
8af6cf88 12889
4d20cd86
ML
12890 DRM_DEBUG_KMS("[CRTC:%d]\n",
12891 crtc->base.id);
8af6cf88 12892
4d20cd86
ML
12893 active = dev_priv->display.get_pipe_config(intel_crtc,
12894 pipe_config);
d62cf62a 12895
b6b5d049 12896 /* hw state is inconsistent with the pipe quirk */
4d20cd86
ML
12897 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12898 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
12899 active = crtc->state->active;
6c49f241 12900
4d20cd86 12901 I915_STATE_WARN(crtc->state->active != active,
0e8ffe1b 12902 "crtc active state doesn't match with hw state "
4d20cd86 12903 "(expected %i, found %i)\n", crtc->state->active, active);
0e8ffe1b 12904
4d20cd86 12905 I915_STATE_WARN(intel_crtc->active != crtc->state->active,
53d9f4e9 12906 "transitional active state does not match atomic hw state "
4d20cd86
ML
12907 "(expected %i, found %i)\n", crtc->state->active, intel_crtc->active);
12908
12909 for_each_encoder_on_crtc(dev, crtc, encoder) {
12910 enum pipe pipe;
12911
12912 active = encoder->get_hw_state(encoder, &pipe);
12913 I915_STATE_WARN(active != crtc->state->active,
12914 "[ENCODER:%i] active %i with crtc active %i\n",
12915 encoder->base.base.id, active, crtc->state->active);
12916
12917 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
12918 "Encoder connected to wrong pipe %c\n",
12919 pipe_name(pipe));
12920
12921 if (active)
12922 encoder->get_config(encoder, pipe_config);
12923 }
53d9f4e9 12924
4d20cd86 12925 if (!crtc->state->active)
cfb23ed6
ML
12926 continue;
12927
e3b247da
VS
12928 intel_pipe_config_sanity_check(dev_priv, pipe_config);
12929
4d20cd86
ML
12930 sw_config = to_intel_crtc_state(crtc->state);
12931 if (!intel_pipe_config_compare(dev, sw_config,
12932 pipe_config, false)) {
e2c719b7 12933 I915_STATE_WARN(1, "pipe state doesn't match!\n");
4d20cd86 12934 intel_dump_pipe_config(intel_crtc, pipe_config,
c0b03411 12935 "[hw state]");
4d20cd86 12936 intel_dump_pipe_config(intel_crtc, sw_config,
c0b03411
DV
12937 "[sw state]");
12938 }
8af6cf88
DV
12939 }
12940}
12941
91d1b4bd
DV
12942static void
12943check_shared_dpll_state(struct drm_device *dev)
12944{
fbee40df 12945 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd
DV
12946 struct intel_crtc *crtc;
12947 struct intel_dpll_hw_state dpll_hw_state;
12948 int i;
5358901f
DV
12949
12950 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
8106ddbd
ACO
12951 struct intel_shared_dpll *pll =
12952 intel_get_shared_dpll_by_id(dev_priv, i);
2dd66ebd 12953 unsigned enabled_crtcs = 0, active_crtcs = 0;
5358901f
DV
12954 bool active;
12955
12956 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12957
12958 DRM_DEBUG_KMS("%s\n", pll->name);
12959
2edd6443 12960 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
5358901f 12961
2dd66ebd
ML
12962 I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
12963 "more active pll users than references: %x vs %x\n",
12964 pll->active_mask, pll->config.crtc_mask);
9d16da65
ACO
12965
12966 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
2dd66ebd
ML
12967 I915_STATE_WARN(!pll->on && pll->active_mask,
12968 "pll in active use but not on in sw tracking\n");
12969 I915_STATE_WARN(pll->on && !pll->active_mask,
12970 "pll is on but not used by any active crtc\n");
9d16da65
ACO
12971 I915_STATE_WARN(pll->on != active,
12972 "pll on state mismatch (expected %i, found %i)\n",
12973 pll->on, active);
12974 }
5358901f 12975
d3fcc808 12976 for_each_intel_crtc(dev, crtc) {
8106ddbd 12977 if (crtc->base.state->enable && crtc->config->shared_dpll == pll)
2dd66ebd
ML
12978 enabled_crtcs |= 1 << drm_crtc_index(&crtc->base);
12979 if (crtc->base.state->active && crtc->config->shared_dpll == pll)
12980 active_crtcs |= 1 << drm_crtc_index(&crtc->base);
5358901f 12981 }
2dd66ebd
ML
12982
12983 I915_STATE_WARN(pll->active_mask != active_crtcs,
12984 "pll active crtcs mismatch (expected %x, found %x)\n",
12985 pll->active_mask, active_crtcs);
12986 I915_STATE_WARN(pll->config.crtc_mask != enabled_crtcs,
12987 "pll enabled crtcs mismatch (expected %x, found %x)\n",
12988 pll->config.crtc_mask, enabled_crtcs);
66e985c0 12989
e2c719b7 12990 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
66e985c0
DV
12991 sizeof(dpll_hw_state)),
12992 "pll hw state mismatch\n");
5358901f 12993 }
8af6cf88
DV
12994}
12995
ee165b1a
ML
12996static void
12997intel_modeset_check_state(struct drm_device *dev,
12998 struct drm_atomic_state *old_state)
91d1b4bd 12999{
08db6652 13000 check_wm_state(dev);
35dd3c64 13001 check_connector_state(dev, old_state);
91d1b4bd 13002 check_encoder_state(dev);
4d20cd86 13003 check_crtc_state(dev, old_state);
91d1b4bd
DV
13004 check_shared_dpll_state(dev);
13005}
13006
80715b2f
VS
13007static void update_scanline_offset(struct intel_crtc *crtc)
13008{
13009 struct drm_device *dev = crtc->base.dev;
13010
13011 /*
13012 * The scanline counter increments at the leading edge of hsync.
13013 *
13014 * On most platforms it starts counting from vtotal-1 on the
13015 * first active line. That means the scanline counter value is
13016 * always one less than what we would expect. Ie. just after
13017 * start of vblank, which also occurs at start of hsync (on the
13018 * last active line), the scanline counter will read vblank_start-1.
13019 *
13020 * On gen2 the scanline counter starts counting from 1 instead
13021 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
13022 * to keep the value positive), instead of adding one.
13023 *
13024 * On HSW+ the behaviour of the scanline counter depends on the output
13025 * type. For DP ports it behaves like most other platforms, but on HDMI
13026 * there's an extra 1 line difference. So we need to add two instead of
13027 * one to the value.
13028 */
13029 if (IS_GEN2(dev)) {
124abe07 13030 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
80715b2f
VS
13031 int vtotal;
13032
124abe07
VS
13033 vtotal = adjusted_mode->crtc_vtotal;
13034 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
80715b2f
VS
13035 vtotal /= 2;
13036
13037 crtc->scanline_offset = vtotal - 1;
13038 } else if (HAS_DDI(dev) &&
409ee761 13039 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
80715b2f
VS
13040 crtc->scanline_offset = 2;
13041 } else
13042 crtc->scanline_offset = 1;
13043}
13044
ad421372 13045static void intel_modeset_clear_plls(struct drm_atomic_state *state)
ed6739ef 13046{
225da59b 13047 struct drm_device *dev = state->dev;
ed6739ef 13048 struct drm_i915_private *dev_priv = to_i915(dev);
ad421372 13049 struct intel_shared_dpll_config *shared_dpll = NULL;
0a9ab303
ACO
13050 struct drm_crtc *crtc;
13051 struct drm_crtc_state *crtc_state;
0a9ab303 13052 int i;
ed6739ef
ACO
13053
13054 if (!dev_priv->display.crtc_compute_clock)
ad421372 13055 return;
ed6739ef 13056
0a9ab303 13057 for_each_crtc_in_state(state, crtc, crtc_state, i) {
fb1a38a9 13058 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8106ddbd
ACO
13059 struct intel_shared_dpll *old_dpll =
13060 to_intel_crtc_state(crtc->state)->shared_dpll;
0a9ab303 13061
fb1a38a9 13062 if (!needs_modeset(crtc_state))
225da59b
ACO
13063 continue;
13064
8106ddbd 13065 to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
fb1a38a9 13066
8106ddbd 13067 if (!old_dpll)
fb1a38a9 13068 continue;
0a9ab303 13069
ad421372
ML
13070 if (!shared_dpll)
13071 shared_dpll = intel_atomic_get_shared_dpll_state(state);
ed6739ef 13072
8106ddbd 13073 intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
ad421372 13074 }
ed6739ef
ACO
13075}
13076
99d736a2
ML
13077/*
13078 * This implements the workaround described in the "notes" section of the mode
13079 * set sequence documentation. When going from no pipes or single pipe to
13080 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13081 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13082 */
13083static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13084{
13085 struct drm_crtc_state *crtc_state;
13086 struct intel_crtc *intel_crtc;
13087 struct drm_crtc *crtc;
13088 struct intel_crtc_state *first_crtc_state = NULL;
13089 struct intel_crtc_state *other_crtc_state = NULL;
13090 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13091 int i;
13092
13093 /* look at all crtc's that are going to be enabled in during modeset */
13094 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13095 intel_crtc = to_intel_crtc(crtc);
13096
13097 if (!crtc_state->active || !needs_modeset(crtc_state))
13098 continue;
13099
13100 if (first_crtc_state) {
13101 other_crtc_state = to_intel_crtc_state(crtc_state);
13102 break;
13103 } else {
13104 first_crtc_state = to_intel_crtc_state(crtc_state);
13105 first_pipe = intel_crtc->pipe;
13106 }
13107 }
13108
13109 /* No workaround needed? */
13110 if (!first_crtc_state)
13111 return 0;
13112
13113 /* w/a possibly needed, check how many crtc's are already enabled. */
13114 for_each_intel_crtc(state->dev, intel_crtc) {
13115 struct intel_crtc_state *pipe_config;
13116
13117 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13118 if (IS_ERR(pipe_config))
13119 return PTR_ERR(pipe_config);
13120
13121 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13122
13123 if (!pipe_config->base.active ||
13124 needs_modeset(&pipe_config->base))
13125 continue;
13126
13127 /* 2 or more enabled crtcs means no need for w/a */
13128 if (enabled_pipe != INVALID_PIPE)
13129 return 0;
13130
13131 enabled_pipe = intel_crtc->pipe;
13132 }
13133
13134 if (enabled_pipe != INVALID_PIPE)
13135 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13136 else if (other_crtc_state)
13137 other_crtc_state->hsw_workaround_pipe = first_pipe;
13138
13139 return 0;
13140}
13141
27c329ed
ML
13142static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13143{
13144 struct drm_crtc *crtc;
13145 struct drm_crtc_state *crtc_state;
13146 int ret = 0;
13147
13148 /* add all active pipes to the state */
13149 for_each_crtc(state->dev, crtc) {
13150 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13151 if (IS_ERR(crtc_state))
13152 return PTR_ERR(crtc_state);
13153
13154 if (!crtc_state->active || needs_modeset(crtc_state))
13155 continue;
13156
13157 crtc_state->mode_changed = true;
13158
13159 ret = drm_atomic_add_affected_connectors(state, crtc);
13160 if (ret)
13161 break;
13162
13163 ret = drm_atomic_add_affected_planes(state, crtc);
13164 if (ret)
13165 break;
13166 }
13167
13168 return ret;
13169}
13170
c347a676 13171static int intel_modeset_checks(struct drm_atomic_state *state)
054518dd 13172{
565602d7
ML
13173 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13174 struct drm_i915_private *dev_priv = state->dev->dev_private;
13175 struct drm_crtc *crtc;
13176 struct drm_crtc_state *crtc_state;
13177 int ret = 0, i;
054518dd 13178
b359283a
ML
13179 if (!check_digital_port_conflicts(state)) {
13180 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13181 return -EINVAL;
13182 }
13183
565602d7
ML
13184 intel_state->modeset = true;
13185 intel_state->active_crtcs = dev_priv->active_crtcs;
13186
13187 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13188 if (crtc_state->active)
13189 intel_state->active_crtcs |= 1 << i;
13190 else
13191 intel_state->active_crtcs &= ~(1 << i);
13192 }
13193
054518dd
ACO
13194 /*
13195 * See if the config requires any additional preparation, e.g.
13196 * to adjust global state with pipes off. We need to do this
13197 * here so we can get the modeset_pipe updated config for the new
13198 * mode set on this crtc. For other crtcs we need to use the
13199 * adjusted_mode bits in the crtc directly.
13200 */
27c329ed 13201 if (dev_priv->display.modeset_calc_cdclk) {
27c329ed
ML
13202 ret = dev_priv->display.modeset_calc_cdclk(state);
13203
1a617b77 13204 if (!ret && intel_state->dev_cdclk != dev_priv->cdclk_freq)
27c329ed
ML
13205 ret = intel_modeset_all_pipes(state);
13206
13207 if (ret < 0)
054518dd 13208 return ret;
e8788cbc
ML
13209
13210 DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
13211 intel_state->cdclk, intel_state->dev_cdclk);
27c329ed 13212 } else
1a617b77 13213 to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
054518dd 13214
ad421372 13215 intel_modeset_clear_plls(state);
054518dd 13216
565602d7 13217 if (IS_HASWELL(dev_priv))
ad421372 13218 return haswell_mode_set_planes_workaround(state);
99d736a2 13219
ad421372 13220 return 0;
c347a676
ACO
13221}
13222
aa363136
MR
13223/*
13224 * Handle calculation of various watermark data at the end of the atomic check
13225 * phase. The code here should be run after the per-crtc and per-plane 'check'
13226 * handlers to ensure that all derived state has been updated.
13227 */
13228static void calc_watermark_data(struct drm_atomic_state *state)
13229{
13230 struct drm_device *dev = state->dev;
13231 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13232 struct drm_crtc *crtc;
13233 struct drm_crtc_state *cstate;
13234 struct drm_plane *plane;
13235 struct drm_plane_state *pstate;
13236
13237 /*
13238 * Calculate watermark configuration details now that derived
13239 * plane/crtc state is all properly updated.
13240 */
13241 drm_for_each_crtc(crtc, dev) {
13242 cstate = drm_atomic_get_existing_crtc_state(state, crtc) ?:
13243 crtc->state;
13244
13245 if (cstate->active)
13246 intel_state->wm_config.num_pipes_active++;
13247 }
13248 drm_for_each_legacy_plane(plane, dev) {
13249 pstate = drm_atomic_get_existing_plane_state(state, plane) ?:
13250 plane->state;
13251
13252 if (!to_intel_plane_state(pstate)->visible)
13253 continue;
13254
13255 intel_state->wm_config.sprites_enabled = true;
13256 if (pstate->crtc_w != pstate->src_w >> 16 ||
13257 pstate->crtc_h != pstate->src_h >> 16)
13258 intel_state->wm_config.sprites_scaled = true;
13259 }
13260}
13261
74c090b1
ML
13262/**
13263 * intel_atomic_check - validate state object
13264 * @dev: drm device
13265 * @state: state to validate
13266 */
13267static int intel_atomic_check(struct drm_device *dev,
13268 struct drm_atomic_state *state)
c347a676 13269{
dd8b3bdb 13270 struct drm_i915_private *dev_priv = to_i915(dev);
aa363136 13271 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
c347a676
ACO
13272 struct drm_crtc *crtc;
13273 struct drm_crtc_state *crtc_state;
13274 int ret, i;
61333b60 13275 bool any_ms = false;
c347a676 13276
74c090b1 13277 ret = drm_atomic_helper_check_modeset(dev, state);
054518dd
ACO
13278 if (ret)
13279 return ret;
13280
c347a676 13281 for_each_crtc_in_state(state, crtc, crtc_state, i) {
cfb23ed6
ML
13282 struct intel_crtc_state *pipe_config =
13283 to_intel_crtc_state(crtc_state);
1ed51de9
DV
13284
13285 /* Catch I915_MODE_FLAG_INHERITED */
13286 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
13287 crtc_state->mode_changed = true;
cfb23ed6 13288
61333b60
ML
13289 if (!crtc_state->enable) {
13290 if (needs_modeset(crtc_state))
13291 any_ms = true;
c347a676 13292 continue;
61333b60 13293 }
c347a676 13294
26495481 13295 if (!needs_modeset(crtc_state))
cfb23ed6
ML
13296 continue;
13297
26495481
DV
13298 /* FIXME: For only active_changed we shouldn't need to do any
13299 * state recomputation at all. */
13300
1ed51de9
DV
13301 ret = drm_atomic_add_affected_connectors(state, crtc);
13302 if (ret)
13303 return ret;
b359283a 13304
cfb23ed6 13305 ret = intel_modeset_pipe_config(crtc, pipe_config);
c347a676
ACO
13306 if (ret)
13307 return ret;
13308
73831236 13309 if (i915.fastboot &&
dd8b3bdb 13310 intel_pipe_config_compare(dev,
cfb23ed6 13311 to_intel_crtc_state(crtc->state),
1ed51de9 13312 pipe_config, true)) {
26495481 13313 crtc_state->mode_changed = false;
bfd16b2a 13314 to_intel_crtc_state(crtc_state)->update_pipe = true;
26495481
DV
13315 }
13316
13317 if (needs_modeset(crtc_state)) {
13318 any_ms = true;
cfb23ed6
ML
13319
13320 ret = drm_atomic_add_affected_planes(state, crtc);
13321 if (ret)
13322 return ret;
13323 }
61333b60 13324
26495481
DV
13325 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
13326 needs_modeset(crtc_state) ?
13327 "[modeset]" : "[fastset]");
c347a676
ACO
13328 }
13329
61333b60
ML
13330 if (any_ms) {
13331 ret = intel_modeset_checks(state);
13332
13333 if (ret)
13334 return ret;
27c329ed 13335 } else
dd8b3bdb 13336 intel_state->cdclk = dev_priv->cdclk_freq;
76305b1a 13337
dd8b3bdb 13338 ret = drm_atomic_helper_check_planes(dev, state);
aa363136
MR
13339 if (ret)
13340 return ret;
13341
f51be2e0 13342 intel_fbc_choose_crtc(dev_priv, state);
aa363136
MR
13343 calc_watermark_data(state);
13344
13345 return 0;
054518dd
ACO
13346}
13347
5008e874
ML
13348static int intel_atomic_prepare_commit(struct drm_device *dev,
13349 struct drm_atomic_state *state,
13350 bool async)
13351{
7580d774
ML
13352 struct drm_i915_private *dev_priv = dev->dev_private;
13353 struct drm_plane_state *plane_state;
5008e874 13354 struct drm_crtc_state *crtc_state;
7580d774 13355 struct drm_plane *plane;
5008e874
ML
13356 struct drm_crtc *crtc;
13357 int i, ret;
13358
13359 if (async) {
13360 DRM_DEBUG_KMS("i915 does not yet support async commit\n");
13361 return -EINVAL;
13362 }
13363
13364 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13365 ret = intel_crtc_wait_for_pending_flips(crtc);
13366 if (ret)
13367 return ret;
7580d774
ML
13368
13369 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
13370 flush_workqueue(dev_priv->wq);
5008e874
ML
13371 }
13372
f935675f
ML
13373 ret = mutex_lock_interruptible(&dev->struct_mutex);
13374 if (ret)
13375 return ret;
13376
5008e874 13377 ret = drm_atomic_helper_prepare_planes(dev, state);
7580d774
ML
13378 if (!ret && !async && !i915_reset_in_progress(&dev_priv->gpu_error)) {
13379 u32 reset_counter;
13380
13381 reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
13382 mutex_unlock(&dev->struct_mutex);
13383
13384 for_each_plane_in_state(state, plane, plane_state, i) {
13385 struct intel_plane_state *intel_plane_state =
13386 to_intel_plane_state(plane_state);
13387
13388 if (!intel_plane_state->wait_req)
13389 continue;
13390
13391 ret = __i915_wait_request(intel_plane_state->wait_req,
13392 reset_counter, true,
13393 NULL, NULL);
13394
13395 /* Swallow -EIO errors to allow updates during hw lockup. */
13396 if (ret == -EIO)
13397 ret = 0;
13398
13399 if (ret)
13400 break;
13401 }
13402
13403 if (!ret)
13404 return 0;
13405
13406 mutex_lock(&dev->struct_mutex);
13407 drm_atomic_helper_cleanup_planes(dev, state);
13408 }
5008e874 13409
f935675f 13410 mutex_unlock(&dev->struct_mutex);
5008e874
ML
13411 return ret;
13412}
13413
e8861675
ML
13414static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
13415 struct drm_i915_private *dev_priv,
13416 unsigned crtc_mask)
13417{
13418 unsigned last_vblank_count[I915_MAX_PIPES];
13419 enum pipe pipe;
13420 int ret;
13421
13422 if (!crtc_mask)
13423 return;
13424
13425 for_each_pipe(dev_priv, pipe) {
13426 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13427
13428 if (!((1 << pipe) & crtc_mask))
13429 continue;
13430
13431 ret = drm_crtc_vblank_get(crtc);
13432 if (WARN_ON(ret != 0)) {
13433 crtc_mask &= ~(1 << pipe);
13434 continue;
13435 }
13436
13437 last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
13438 }
13439
13440 for_each_pipe(dev_priv, pipe) {
13441 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13442 long lret;
13443
13444 if (!((1 << pipe) & crtc_mask))
13445 continue;
13446
13447 lret = wait_event_timeout(dev->vblank[pipe].queue,
13448 last_vblank_count[pipe] !=
13449 drm_crtc_vblank_count(crtc),
13450 msecs_to_jiffies(50));
13451
13452 WARN_ON(!lret);
13453
13454 drm_crtc_vblank_put(crtc);
13455 }
13456}
13457
13458static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
13459{
13460 /* fb updated, need to unpin old fb */
13461 if (crtc_state->fb_changed)
13462 return true;
13463
13464 /* wm changes, need vblank before final wm's */
caed361d 13465 if (crtc_state->update_wm_post)
e8861675
ML
13466 return true;
13467
13468 /*
13469 * cxsr is re-enabled after vblank.
caed361d 13470 * This is already handled by crtc_state->update_wm_post,
e8861675
ML
13471 * but added for clarity.
13472 */
13473 if (crtc_state->disable_cxsr)
13474 return true;
13475
13476 return false;
13477}
13478
74c090b1
ML
13479/**
13480 * intel_atomic_commit - commit validated state object
13481 * @dev: DRM device
13482 * @state: the top-level driver state object
13483 * @async: asynchronous commit
13484 *
13485 * This function commits a top-level state object that has been validated
13486 * with drm_atomic_helper_check().
13487 *
13488 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
13489 * we can only handle plane-related operations and do not yet support
13490 * asynchronous commit.
13491 *
13492 * RETURNS
13493 * Zero for success or -errno.
13494 */
13495static int intel_atomic_commit(struct drm_device *dev,
13496 struct drm_atomic_state *state,
13497 bool async)
a6778b3c 13498{
565602d7 13499 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fbee40df 13500 struct drm_i915_private *dev_priv = dev->dev_private;
29ceb0e6 13501 struct drm_crtc_state *old_crtc_state;
7580d774 13502 struct drm_crtc *crtc;
ed4a6a7c 13503 struct intel_crtc_state *intel_cstate;
565602d7
ML
13504 int ret = 0, i;
13505 bool hw_check = intel_state->modeset;
33c8df89 13506 unsigned long put_domains[I915_MAX_PIPES] = {};
e8861675 13507 unsigned crtc_vblank_mask = 0;
a6778b3c 13508
5008e874 13509 ret = intel_atomic_prepare_commit(dev, state, async);
7580d774
ML
13510 if (ret) {
13511 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
d4afb8cc 13512 return ret;
7580d774 13513 }
d4afb8cc 13514
1c5e19f8 13515 drm_atomic_helper_swap_state(dev, state);
a1475e77
ML
13516 dev_priv->wm.config = intel_state->wm_config;
13517 intel_shared_dpll_commit(state);
1c5e19f8 13518
565602d7
ML
13519 if (intel_state->modeset) {
13520 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
13521 sizeof(intel_state->min_pixclk));
13522 dev_priv->active_crtcs = intel_state->active_crtcs;
1a617b77 13523 dev_priv->atomic_cdclk_freq = intel_state->cdclk;
33c8df89
ML
13524
13525 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
565602d7
ML
13526 }
13527
29ceb0e6 13528 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
a539205a
ML
13529 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13530
33c8df89
ML
13531 if (needs_modeset(crtc->state) ||
13532 to_intel_crtc_state(crtc->state)->update_pipe) {
13533 hw_check = true;
13534
13535 put_domains[to_intel_crtc(crtc)->pipe] =
13536 modeset_get_crtc_power_domains(crtc,
13537 to_intel_crtc_state(crtc->state));
13538 }
13539
61333b60
ML
13540 if (!needs_modeset(crtc->state))
13541 continue;
13542
29ceb0e6 13543 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
460da916 13544
29ceb0e6
VS
13545 if (old_crtc_state->active) {
13546 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
a539205a 13547 dev_priv->display.crtc_disable(crtc);
eddfcbcd 13548 intel_crtc->active = false;
58f9c0bc 13549 intel_fbc_disable(intel_crtc);
eddfcbcd 13550 intel_disable_shared_dpll(intel_crtc);
9bbc8258
VS
13551
13552 /*
13553 * Underruns don't always raise
13554 * interrupts, so check manually.
13555 */
13556 intel_check_cpu_fifo_underruns(dev_priv);
13557 intel_check_pch_fifo_underruns(dev_priv);
b9001114
ML
13558
13559 if (!crtc->state->active)
13560 intel_update_watermarks(crtc);
a539205a 13561 }
b8cecdf5 13562 }
7758a113 13563
ea9d758d
DV
13564 /* Only after disabling all output pipelines that will be changed can we
13565 * update the the output configuration. */
4740b0f2 13566 intel_modeset_update_crtc_state(state);
f6e5b160 13567
565602d7 13568 if (intel_state->modeset) {
4740b0f2 13569 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
33c8df89
ML
13570
13571 if (dev_priv->display.modeset_commit_cdclk &&
13572 intel_state->dev_cdclk != dev_priv->cdclk_freq)
13573 dev_priv->display.modeset_commit_cdclk(state);
4740b0f2 13574 }
47fab737 13575
a6778b3c 13576 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
29ceb0e6 13577 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
f6ac4b2a
ML
13578 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13579 bool modeset = needs_modeset(crtc->state);
e8861675
ML
13580 struct intel_crtc_state *pipe_config =
13581 to_intel_crtc_state(crtc->state);
13582 bool update_pipe = !modeset && pipe_config->update_pipe;
9f836f90 13583
f6ac4b2a 13584 if (modeset && crtc->state->active) {
a539205a
ML
13585 update_scanline_offset(to_intel_crtc(crtc));
13586 dev_priv->display.crtc_enable(crtc);
13587 }
80715b2f 13588
82cf435b
LL
13589 if (!modeset &&
13590 crtc->state->active &&
13591 crtc->state->color_mgmt_changed) {
13592 /*
13593 * Only update color management when not doing
13594 * a modeset as this will be done by
13595 * crtc_enable already.
13596 */
13597 intel_color_set_csc(crtc);
13598 intel_color_load_luts(crtc);
13599 }
13600
f6ac4b2a 13601 if (!modeset)
29ceb0e6 13602 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
f6ac4b2a 13603
31ae71fc
ML
13604 if (crtc->state->active &&
13605 drm_atomic_get_existing_plane_state(state, crtc->primary))
49227c4a
PZ
13606 intel_fbc_enable(intel_crtc);
13607
6173ee28
ML
13608 if (crtc->state->active &&
13609 (crtc->state->planes_changed || update_pipe))
29ceb0e6 13610 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
bfd16b2a 13611
e8861675
ML
13612 if (pipe_config->base.active && needs_vblank_wait(pipe_config))
13613 crtc_vblank_mask |= 1 << i;
80715b2f 13614 }
a6778b3c 13615
a6778b3c 13616 /* FIXME: add subpixel order */
83a57153 13617
e8861675
ML
13618 if (!state->legacy_cursor_update)
13619 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
f935675f 13620
ed4a6a7c
MR
13621 /*
13622 * Now that the vblank has passed, we can go ahead and program the
13623 * optimal watermarks on platforms that need two-step watermark
13624 * programming.
13625 *
13626 * TODO: Move this (and other cleanup) to an async worker eventually.
13627 */
29ceb0e6 13628 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
ed4a6a7c
MR
13629 intel_cstate = to_intel_crtc_state(crtc->state);
13630
13631 if (dev_priv->display.optimize_watermarks)
13632 dev_priv->display.optimize_watermarks(intel_cstate);
13633 }
13634
177246a8
MR
13635 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
13636 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
13637
13638 if (put_domains[i])
13639 modeset_put_power_domains(dev_priv, put_domains[i]);
13640 }
13641
13642 if (intel_state->modeset)
13643 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
13644
f935675f 13645 mutex_lock(&dev->struct_mutex);
d4afb8cc 13646 drm_atomic_helper_cleanup_planes(dev, state);
f935675f 13647 mutex_unlock(&dev->struct_mutex);
2bfb4627 13648
565602d7 13649 if (hw_check)
ee165b1a
ML
13650 intel_modeset_check_state(dev, state);
13651
13652 drm_atomic_state_free(state);
f30da187 13653
75714940
MK
13654 /* As one of the primary mmio accessors, KMS has a high likelihood
13655 * of triggering bugs in unclaimed access. After we finish
13656 * modesetting, see if an error has been flagged, and if so
13657 * enable debugging for the next modeset - and hope we catch
13658 * the culprit.
13659 *
13660 * XXX note that we assume display power is on at this point.
13661 * This might hold true now but we need to add pm helper to check
13662 * unclaimed only when the hardware is on, as atomic commits
13663 * can happen also when the device is completely off.
13664 */
13665 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
13666
74c090b1 13667 return 0;
7f27126e
JB
13668}
13669
c0c36b94
CW
13670void intel_crtc_restore_mode(struct drm_crtc *crtc)
13671{
83a57153
ACO
13672 struct drm_device *dev = crtc->dev;
13673 struct drm_atomic_state *state;
e694eb02 13674 struct drm_crtc_state *crtc_state;
2bfb4627 13675 int ret;
83a57153
ACO
13676
13677 state = drm_atomic_state_alloc(dev);
13678 if (!state) {
e694eb02 13679 DRM_DEBUG_KMS("[CRTC:%d] crtc restore failed, out of memory",
83a57153
ACO
13680 crtc->base.id);
13681 return;
13682 }
13683
e694eb02 13684 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
83a57153 13685
e694eb02
ML
13686retry:
13687 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13688 ret = PTR_ERR_OR_ZERO(crtc_state);
13689 if (!ret) {
13690 if (!crtc_state->active)
13691 goto out;
83a57153 13692
e694eb02 13693 crtc_state->mode_changed = true;
74c090b1 13694 ret = drm_atomic_commit(state);
83a57153
ACO
13695 }
13696
e694eb02
ML
13697 if (ret == -EDEADLK) {
13698 drm_atomic_state_clear(state);
13699 drm_modeset_backoff(state->acquire_ctx);
13700 goto retry;
4ed9fb37 13701 }
4be07317 13702
2bfb4627 13703 if (ret)
e694eb02 13704out:
2bfb4627 13705 drm_atomic_state_free(state);
c0c36b94
CW
13706}
13707
25c5b266
DV
13708#undef for_each_intel_crtc_masked
13709
f6e5b160 13710static const struct drm_crtc_funcs intel_crtc_funcs = {
82cf435b 13711 .gamma_set = drm_atomic_helper_legacy_gamma_set,
74c090b1 13712 .set_config = drm_atomic_helper_set_config,
82cf435b 13713 .set_property = drm_atomic_helper_crtc_set_property,
f6e5b160
CW
13714 .destroy = intel_crtc_destroy,
13715 .page_flip = intel_crtc_page_flip,
1356837e
MR
13716 .atomic_duplicate_state = intel_crtc_duplicate_state,
13717 .atomic_destroy_state = intel_crtc_destroy_state,
f6e5b160
CW
13718};
13719
6beb8c23
MR
13720/**
13721 * intel_prepare_plane_fb - Prepare fb for usage on plane
13722 * @plane: drm plane to prepare for
13723 * @fb: framebuffer to prepare for presentation
13724 *
13725 * Prepares a framebuffer for usage on a display plane. Generally this
13726 * involves pinning the underlying object and updating the frontbuffer tracking
13727 * bits. Some older platforms need special physical address handling for
13728 * cursor planes.
13729 *
f935675f
ML
13730 * Must be called with struct_mutex held.
13731 *
6beb8c23
MR
13732 * Returns 0 on success, negative error code on failure.
13733 */
13734int
13735intel_prepare_plane_fb(struct drm_plane *plane,
d136dfee 13736 const struct drm_plane_state *new_state)
465c120c
MR
13737{
13738 struct drm_device *dev = plane->dev;
844f9111 13739 struct drm_framebuffer *fb = new_state->fb;
6beb8c23 13740 struct intel_plane *intel_plane = to_intel_plane(plane);
6beb8c23 13741 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
1ee49399 13742 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
6beb8c23 13743 int ret = 0;
465c120c 13744
1ee49399 13745 if (!obj && !old_obj)
465c120c
MR
13746 return 0;
13747
5008e874
ML
13748 if (old_obj) {
13749 struct drm_crtc_state *crtc_state =
13750 drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
13751
13752 /* Big Hammer, we also need to ensure that any pending
13753 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
13754 * current scanout is retired before unpinning the old
13755 * framebuffer. Note that we rely on userspace rendering
13756 * into the buffer attached to the pipe they are waiting
13757 * on. If not, userspace generates a GPU hang with IPEHR
13758 * point to the MI_WAIT_FOR_EVENT.
13759 *
13760 * This should only fail upon a hung GPU, in which case we
13761 * can safely continue.
13762 */
13763 if (needs_modeset(crtc_state))
13764 ret = i915_gem_object_wait_rendering(old_obj, true);
13765
13766 /* Swallow -EIO errors to allow updates during hw lockup. */
13767 if (ret && ret != -EIO)
f935675f 13768 return ret;
5008e874
ML
13769 }
13770
3c28ff22
AG
13771 /* For framebuffer backed by dmabuf, wait for fence */
13772 if (obj && obj->base.dma_buf) {
bcf8be27
ML
13773 long lret;
13774
13775 lret = reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
13776 false, true,
13777 MAX_SCHEDULE_TIMEOUT);
13778 if (lret == -ERESTARTSYS)
13779 return lret;
3c28ff22 13780
bcf8be27 13781 WARN(lret < 0, "waiting returns %li\n", lret);
3c28ff22
AG
13782 }
13783
1ee49399
ML
13784 if (!obj) {
13785 ret = 0;
13786 } else if (plane->type == DRM_PLANE_TYPE_CURSOR &&
6beb8c23
MR
13787 INTEL_INFO(dev)->cursor_needs_physical) {
13788 int align = IS_I830(dev) ? 16 * 1024 : 256;
13789 ret = i915_gem_object_attach_phys(obj, align);
13790 if (ret)
13791 DRM_DEBUG_KMS("failed to attach phys object\n");
13792 } else {
3465c580 13793 ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
6beb8c23 13794 }
465c120c 13795
7580d774
ML
13796 if (ret == 0) {
13797 if (obj) {
13798 struct intel_plane_state *plane_state =
13799 to_intel_plane_state(new_state);
13800
13801 i915_gem_request_assign(&plane_state->wait_req,
13802 obj->last_write_req);
13803 }
13804
a9ff8714 13805 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
7580d774 13806 }
fdd508a6 13807
6beb8c23
MR
13808 return ret;
13809}
13810
38f3ce3a
MR
13811/**
13812 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13813 * @plane: drm plane to clean up for
13814 * @fb: old framebuffer that was on plane
13815 *
13816 * Cleans up a framebuffer that has just been removed from a plane.
f935675f
ML
13817 *
13818 * Must be called with struct_mutex held.
38f3ce3a
MR
13819 */
13820void
13821intel_cleanup_plane_fb(struct drm_plane *plane,
d136dfee 13822 const struct drm_plane_state *old_state)
38f3ce3a
MR
13823{
13824 struct drm_device *dev = plane->dev;
1ee49399 13825 struct intel_plane *intel_plane = to_intel_plane(plane);
7580d774 13826 struct intel_plane_state *old_intel_state;
1ee49399
ML
13827 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
13828 struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
38f3ce3a 13829
7580d774
ML
13830 old_intel_state = to_intel_plane_state(old_state);
13831
1ee49399 13832 if (!obj && !old_obj)
38f3ce3a
MR
13833 return;
13834
1ee49399
ML
13835 if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
13836 !INTEL_INFO(dev)->cursor_needs_physical))
3465c580 13837 intel_unpin_fb_obj(old_state->fb, old_state->rotation);
1ee49399
ML
13838
13839 /* prepare_fb aborted? */
13840 if ((old_obj && (old_obj->frontbuffer_bits & intel_plane->frontbuffer_bit)) ||
13841 (obj && !(obj->frontbuffer_bits & intel_plane->frontbuffer_bit)))
13842 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
7580d774
ML
13843
13844 i915_gem_request_assign(&old_intel_state->wait_req, NULL);
465c120c
MR
13845}
13846
6156a456
CK
13847int
13848skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13849{
13850 int max_scale;
13851 struct drm_device *dev;
13852 struct drm_i915_private *dev_priv;
13853 int crtc_clock, cdclk;
13854
bf8a0af0 13855 if (!intel_crtc || !crtc_state->base.enable)
6156a456
CK
13856 return DRM_PLANE_HELPER_NO_SCALING;
13857
13858 dev = intel_crtc->base.dev;
13859 dev_priv = dev->dev_private;
13860 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
27c329ed 13861 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
6156a456 13862
54bf1ce6 13863 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
6156a456
CK
13864 return DRM_PLANE_HELPER_NO_SCALING;
13865
13866 /*
13867 * skl max scale is lower of:
13868 * close to 3 but not 3, -1 is for that purpose
13869 * or
13870 * cdclk/crtc_clock
13871 */
13872 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13873
13874 return max_scale;
13875}
13876
465c120c 13877static int
3c692a41 13878intel_check_primary_plane(struct drm_plane *plane,
061e4b8d 13879 struct intel_crtc_state *crtc_state,
3c692a41
GP
13880 struct intel_plane_state *state)
13881{
2b875c22
MR
13882 struct drm_crtc *crtc = state->base.crtc;
13883 struct drm_framebuffer *fb = state->base.fb;
6156a456 13884 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
061e4b8d
ML
13885 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13886 bool can_position = false;
465c120c 13887
693bdc28
VS
13888 if (INTEL_INFO(plane->dev)->gen >= 9) {
13889 /* use scaler when colorkey is not required */
13890 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
13891 min_scale = 1;
13892 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
13893 }
d8106366 13894 can_position = true;
6156a456 13895 }
d8106366 13896
061e4b8d
ML
13897 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13898 &state->dst, &state->clip,
da20eabd
ML
13899 min_scale, max_scale,
13900 can_position, true,
13901 &state->visible);
14af293f
GP
13902}
13903
613d2b27
ML
13904static void intel_begin_crtc_commit(struct drm_crtc *crtc,
13905 struct drm_crtc_state *old_crtc_state)
3c692a41 13906{
32b7eeec 13907 struct drm_device *dev = crtc->dev;
3c692a41 13908 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
bfd16b2a
ML
13909 struct intel_crtc_state *old_intel_state =
13910 to_intel_crtc_state(old_crtc_state);
13911 bool modeset = needs_modeset(crtc->state);
3c692a41 13912
c34c9ee4 13913 /* Perform vblank evasion around commit operation */
62852622 13914 intel_pipe_update_start(intel_crtc);
0583236e 13915
bfd16b2a
ML
13916 if (modeset)
13917 return;
13918
13919 if (to_intel_crtc_state(crtc->state)->update_pipe)
13920 intel_update_pipe_config(intel_crtc, old_intel_state);
13921 else if (INTEL_INFO(dev)->gen >= 9)
0583236e 13922 skl_detach_scalers(intel_crtc);
32b7eeec
MR
13923}
13924
613d2b27
ML
13925static void intel_finish_crtc_commit(struct drm_crtc *crtc,
13926 struct drm_crtc_state *old_crtc_state)
32b7eeec 13927{
32b7eeec 13928 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
32b7eeec 13929
62852622 13930 intel_pipe_update_end(intel_crtc);
3c692a41
GP
13931}
13932
cf4c7c12 13933/**
4a3b8769
MR
13934 * intel_plane_destroy - destroy a plane
13935 * @plane: plane to destroy
cf4c7c12 13936 *
4a3b8769
MR
13937 * Common destruction function for all types of planes (primary, cursor,
13938 * sprite).
cf4c7c12 13939 */
4a3b8769 13940void intel_plane_destroy(struct drm_plane *plane)
465c120c
MR
13941{
13942 struct intel_plane *intel_plane = to_intel_plane(plane);
13943 drm_plane_cleanup(plane);
13944 kfree(intel_plane);
13945}
13946
65a3fea0 13947const struct drm_plane_funcs intel_plane_funcs = {
70a101f8
MR
13948 .update_plane = drm_atomic_helper_update_plane,
13949 .disable_plane = drm_atomic_helper_disable_plane,
3d7d6510 13950 .destroy = intel_plane_destroy,
c196e1d6 13951 .set_property = drm_atomic_helper_plane_set_property,
a98b3431
MR
13952 .atomic_get_property = intel_plane_atomic_get_property,
13953 .atomic_set_property = intel_plane_atomic_set_property,
ea2c67bb
MR
13954 .atomic_duplicate_state = intel_plane_duplicate_state,
13955 .atomic_destroy_state = intel_plane_destroy_state,
13956
465c120c
MR
13957};
13958
13959static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13960 int pipe)
13961{
13962 struct intel_plane *primary;
8e7d688b 13963 struct intel_plane_state *state;
465c120c 13964 const uint32_t *intel_primary_formats;
45e3743a 13965 unsigned int num_formats;
465c120c
MR
13966
13967 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13968 if (primary == NULL)
13969 return NULL;
13970
8e7d688b
MR
13971 state = intel_create_plane_state(&primary->base);
13972 if (!state) {
ea2c67bb
MR
13973 kfree(primary);
13974 return NULL;
13975 }
8e7d688b 13976 primary->base.state = &state->base;
ea2c67bb 13977
465c120c
MR
13978 primary->can_scale = false;
13979 primary->max_downscale = 1;
6156a456
CK
13980 if (INTEL_INFO(dev)->gen >= 9) {
13981 primary->can_scale = true;
af99ceda 13982 state->scaler_id = -1;
6156a456 13983 }
465c120c
MR
13984 primary->pipe = pipe;
13985 primary->plane = pipe;
a9ff8714 13986 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
c59cb179 13987 primary->check_plane = intel_check_primary_plane;
465c120c
MR
13988 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13989 primary->plane = !pipe;
13990
6c0fd451
DL
13991 if (INTEL_INFO(dev)->gen >= 9) {
13992 intel_primary_formats = skl_primary_formats;
13993 num_formats = ARRAY_SIZE(skl_primary_formats);
a8d201af
ML
13994
13995 primary->update_plane = skylake_update_primary_plane;
13996 primary->disable_plane = skylake_disable_primary_plane;
13997 } else if (HAS_PCH_SPLIT(dev)) {
13998 intel_primary_formats = i965_primary_formats;
13999 num_formats = ARRAY_SIZE(i965_primary_formats);
14000
14001 primary->update_plane = ironlake_update_primary_plane;
14002 primary->disable_plane = i9xx_disable_primary_plane;
6c0fd451 14003 } else if (INTEL_INFO(dev)->gen >= 4) {
568db4f2
DL
14004 intel_primary_formats = i965_primary_formats;
14005 num_formats = ARRAY_SIZE(i965_primary_formats);
a8d201af
ML
14006
14007 primary->update_plane = i9xx_update_primary_plane;
14008 primary->disable_plane = i9xx_disable_primary_plane;
6c0fd451
DL
14009 } else {
14010 intel_primary_formats = i8xx_primary_formats;
14011 num_formats = ARRAY_SIZE(i8xx_primary_formats);
a8d201af
ML
14012
14013 primary->update_plane = i9xx_update_primary_plane;
14014 primary->disable_plane = i9xx_disable_primary_plane;
465c120c
MR
14015 }
14016
14017 drm_universal_plane_init(dev, &primary->base, 0,
65a3fea0 14018 &intel_plane_funcs,
465c120c 14019 intel_primary_formats, num_formats,
b0b3b795 14020 DRM_PLANE_TYPE_PRIMARY, NULL);
48404c1e 14021
3b7a5119
SJ
14022 if (INTEL_INFO(dev)->gen >= 4)
14023 intel_create_rotation_property(dev, primary);
48404c1e 14024
ea2c67bb
MR
14025 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
14026
465c120c
MR
14027 return &primary->base;
14028}
14029
3b7a5119
SJ
14030void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
14031{
14032 if (!dev->mode_config.rotation_property) {
14033 unsigned long flags = BIT(DRM_ROTATE_0) |
14034 BIT(DRM_ROTATE_180);
14035
14036 if (INTEL_INFO(dev)->gen >= 9)
14037 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
14038
14039 dev->mode_config.rotation_property =
14040 drm_mode_create_rotation_property(dev, flags);
14041 }
14042 if (dev->mode_config.rotation_property)
14043 drm_object_attach_property(&plane->base.base,
14044 dev->mode_config.rotation_property,
14045 plane->base.state->rotation);
14046}
14047
3d7d6510 14048static int
852e787c 14049intel_check_cursor_plane(struct drm_plane *plane,
061e4b8d 14050 struct intel_crtc_state *crtc_state,
852e787c 14051 struct intel_plane_state *state)
3d7d6510 14052{
061e4b8d 14053 struct drm_crtc *crtc = crtc_state->base.crtc;
2b875c22 14054 struct drm_framebuffer *fb = state->base.fb;
757f9a3e 14055 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
b29ec92c 14056 enum pipe pipe = to_intel_plane(plane)->pipe;
757f9a3e
GP
14057 unsigned stride;
14058 int ret;
3d7d6510 14059
061e4b8d
ML
14060 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14061 &state->dst, &state->clip,
3d7d6510
MR
14062 DRM_PLANE_HELPER_NO_SCALING,
14063 DRM_PLANE_HELPER_NO_SCALING,
852e787c 14064 true, true, &state->visible);
757f9a3e
GP
14065 if (ret)
14066 return ret;
14067
757f9a3e
GP
14068 /* if we want to turn off the cursor ignore width and height */
14069 if (!obj)
da20eabd 14070 return 0;
757f9a3e 14071
757f9a3e 14072 /* Check for which cursor types we support */
061e4b8d 14073 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
ea2c67bb
MR
14074 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
14075 state->base.crtc_w, state->base.crtc_h);
757f9a3e
GP
14076 return -EINVAL;
14077 }
14078
ea2c67bb
MR
14079 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
14080 if (obj->base.size < stride * state->base.crtc_h) {
757f9a3e
GP
14081 DRM_DEBUG_KMS("buffer is too small\n");
14082 return -ENOMEM;
14083 }
14084
3a656b54 14085 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
757f9a3e 14086 DRM_DEBUG_KMS("cursor cannot be tiled\n");
da20eabd 14087 return -EINVAL;
32b7eeec
MR
14088 }
14089
b29ec92c
VS
14090 /*
14091 * There's something wrong with the cursor on CHV pipe C.
14092 * If it straddles the left edge of the screen then
14093 * moving it away from the edge or disabling it often
14094 * results in a pipe underrun, and often that can lead to
14095 * dead pipe (constant underrun reported, and it scans
14096 * out just a solid color). To recover from that, the
14097 * display power well must be turned off and on again.
14098 * Refuse the put the cursor into that compromised position.
14099 */
14100 if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
14101 state->visible && state->base.crtc_x < 0) {
14102 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
14103 return -EINVAL;
14104 }
14105
da20eabd 14106 return 0;
852e787c 14107}
3d7d6510 14108
a8ad0d8e
ML
14109static void
14110intel_disable_cursor_plane(struct drm_plane *plane,
7fabf5ef 14111 struct drm_crtc *crtc)
a8ad0d8e 14112{
f2858021
ML
14113 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14114
14115 intel_crtc->cursor_addr = 0;
55a08b3f 14116 intel_crtc_update_cursor(crtc, NULL);
a8ad0d8e
ML
14117}
14118
f4a2cf29 14119static void
55a08b3f
ML
14120intel_update_cursor_plane(struct drm_plane *plane,
14121 const struct intel_crtc_state *crtc_state,
14122 const struct intel_plane_state *state)
852e787c 14123{
55a08b3f
ML
14124 struct drm_crtc *crtc = crtc_state->base.crtc;
14125 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ea2c67bb 14126 struct drm_device *dev = plane->dev;
2b875c22 14127 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
a912f12f 14128 uint32_t addr;
852e787c 14129
f4a2cf29 14130 if (!obj)
a912f12f 14131 addr = 0;
f4a2cf29 14132 else if (!INTEL_INFO(dev)->cursor_needs_physical)
a912f12f 14133 addr = i915_gem_obj_ggtt_offset(obj);
f4a2cf29 14134 else
a912f12f 14135 addr = obj->phys_handle->busaddr;
852e787c 14136
a912f12f 14137 intel_crtc->cursor_addr = addr;
55a08b3f 14138 intel_crtc_update_cursor(crtc, state);
852e787c
GP
14139}
14140
3d7d6510
MR
14141static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14142 int pipe)
14143{
14144 struct intel_plane *cursor;
8e7d688b 14145 struct intel_plane_state *state;
3d7d6510
MR
14146
14147 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
14148 if (cursor == NULL)
14149 return NULL;
14150
8e7d688b
MR
14151 state = intel_create_plane_state(&cursor->base);
14152 if (!state) {
ea2c67bb
MR
14153 kfree(cursor);
14154 return NULL;
14155 }
8e7d688b 14156 cursor->base.state = &state->base;
ea2c67bb 14157
3d7d6510
MR
14158 cursor->can_scale = false;
14159 cursor->max_downscale = 1;
14160 cursor->pipe = pipe;
14161 cursor->plane = pipe;
a9ff8714 14162 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
c59cb179 14163 cursor->check_plane = intel_check_cursor_plane;
55a08b3f 14164 cursor->update_plane = intel_update_cursor_plane;
a8ad0d8e 14165 cursor->disable_plane = intel_disable_cursor_plane;
3d7d6510
MR
14166
14167 drm_universal_plane_init(dev, &cursor->base, 0,
65a3fea0 14168 &intel_plane_funcs,
3d7d6510
MR
14169 intel_cursor_formats,
14170 ARRAY_SIZE(intel_cursor_formats),
b0b3b795 14171 DRM_PLANE_TYPE_CURSOR, NULL);
4398ad45
VS
14172
14173 if (INTEL_INFO(dev)->gen >= 4) {
14174 if (!dev->mode_config.rotation_property)
14175 dev->mode_config.rotation_property =
14176 drm_mode_create_rotation_property(dev,
14177 BIT(DRM_ROTATE_0) |
14178 BIT(DRM_ROTATE_180));
14179 if (dev->mode_config.rotation_property)
14180 drm_object_attach_property(&cursor->base.base,
14181 dev->mode_config.rotation_property,
8e7d688b 14182 state->base.rotation);
4398ad45
VS
14183 }
14184
af99ceda
CK
14185 if (INTEL_INFO(dev)->gen >=9)
14186 state->scaler_id = -1;
14187
ea2c67bb
MR
14188 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14189
3d7d6510
MR
14190 return &cursor->base;
14191}
14192
549e2bfb
CK
14193static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14194 struct intel_crtc_state *crtc_state)
14195{
14196 int i;
14197 struct intel_scaler *intel_scaler;
14198 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14199
14200 for (i = 0; i < intel_crtc->num_scalers; i++) {
14201 intel_scaler = &scaler_state->scalers[i];
14202 intel_scaler->in_use = 0;
549e2bfb
CK
14203 intel_scaler->mode = PS_SCALER_MODE_DYN;
14204 }
14205
14206 scaler_state->scaler_id = -1;
14207}
14208
b358d0a6 14209static void intel_crtc_init(struct drm_device *dev, int pipe)
79e53945 14210{
fbee40df 14211 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 14212 struct intel_crtc *intel_crtc;
f5de6e07 14213 struct intel_crtc_state *crtc_state = NULL;
3d7d6510
MR
14214 struct drm_plane *primary = NULL;
14215 struct drm_plane *cursor = NULL;
8563b1e8 14216 int ret;
79e53945 14217
955382f3 14218 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
79e53945
JB
14219 if (intel_crtc == NULL)
14220 return;
14221
f5de6e07
ACO
14222 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14223 if (!crtc_state)
14224 goto fail;
550acefd
ACO
14225 intel_crtc->config = crtc_state;
14226 intel_crtc->base.state = &crtc_state->base;
07878248 14227 crtc_state->base.crtc = &intel_crtc->base;
f5de6e07 14228
549e2bfb
CK
14229 /* initialize shared scalers */
14230 if (INTEL_INFO(dev)->gen >= 9) {
14231 if (pipe == PIPE_C)
14232 intel_crtc->num_scalers = 1;
14233 else
14234 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14235
14236 skl_init_scalers(dev, intel_crtc, crtc_state);
14237 }
14238
465c120c 14239 primary = intel_primary_plane_create(dev, pipe);
3d7d6510
MR
14240 if (!primary)
14241 goto fail;
14242
14243 cursor = intel_cursor_plane_create(dev, pipe);
14244 if (!cursor)
14245 goto fail;
14246
465c120c 14247 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
f9882876 14248 cursor, &intel_crtc_funcs, NULL);
3d7d6510
MR
14249 if (ret)
14250 goto fail;
79e53945 14251
1f1c2e24
VS
14252 /*
14253 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
8c0f92e1 14254 * is hooked to pipe B. Hence we want plane A feeding pipe B.
1f1c2e24 14255 */
80824003
JB
14256 intel_crtc->pipe = pipe;
14257 intel_crtc->plane = pipe;
3a77c4c4 14258 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
28c97730 14259 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
e2e767ab 14260 intel_crtc->plane = !pipe;
80824003
JB
14261 }
14262
4b0e333e
CW
14263 intel_crtc->cursor_base = ~0;
14264 intel_crtc->cursor_cntl = ~0;
dc41c154 14265 intel_crtc->cursor_size = ~0;
8d7849db 14266
852eb00d
VS
14267 intel_crtc->wm.cxsr_allowed = true;
14268
22fd0fab
JB
14269 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14270 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14271 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14272 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14273
79e53945 14274 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
87b6b101 14275
8563b1e8
LL
14276 intel_color_init(&intel_crtc->base);
14277
87b6b101 14278 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
3d7d6510
MR
14279 return;
14280
14281fail:
14282 if (primary)
14283 drm_plane_cleanup(primary);
14284 if (cursor)
14285 drm_plane_cleanup(cursor);
f5de6e07 14286 kfree(crtc_state);
3d7d6510 14287 kfree(intel_crtc);
79e53945
JB
14288}
14289
752aa88a
JB
14290enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14291{
14292 struct drm_encoder *encoder = connector->base.encoder;
6e9f798d 14293 struct drm_device *dev = connector->base.dev;
752aa88a 14294
51fd371b 14295 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
752aa88a 14296
d3babd3f 14297 if (!encoder || WARN_ON(!encoder->crtc))
752aa88a
JB
14298 return INVALID_PIPE;
14299
14300 return to_intel_crtc(encoder->crtc)->pipe;
14301}
14302
08d7b3d1 14303int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 14304 struct drm_file *file)
08d7b3d1 14305{
08d7b3d1 14306 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7707e653 14307 struct drm_crtc *drmmode_crtc;
c05422d5 14308 struct intel_crtc *crtc;
08d7b3d1 14309
7707e653 14310 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
08d7b3d1 14311
7707e653 14312 if (!drmmode_crtc) {
08d7b3d1 14313 DRM_ERROR("no such CRTC id\n");
3f2c2057 14314 return -ENOENT;
08d7b3d1
CW
14315 }
14316
7707e653 14317 crtc = to_intel_crtc(drmmode_crtc);
c05422d5 14318 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 14319
c05422d5 14320 return 0;
08d7b3d1
CW
14321}
14322
66a9278e 14323static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 14324{
66a9278e
DV
14325 struct drm_device *dev = encoder->base.dev;
14326 struct intel_encoder *source_encoder;
79e53945 14327 int index_mask = 0;
79e53945
JB
14328 int entry = 0;
14329
b2784e15 14330 for_each_intel_encoder(dev, source_encoder) {
bc079e8b 14331 if (encoders_cloneable(encoder, source_encoder))
66a9278e
DV
14332 index_mask |= (1 << entry);
14333
79e53945
JB
14334 entry++;
14335 }
4ef69c7a 14336
79e53945
JB
14337 return index_mask;
14338}
14339
4d302442
CW
14340static bool has_edp_a(struct drm_device *dev)
14341{
14342 struct drm_i915_private *dev_priv = dev->dev_private;
14343
14344 if (!IS_MOBILE(dev))
14345 return false;
14346
14347 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14348 return false;
14349
e3589908 14350 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
14351 return false;
14352
14353 return true;
14354}
14355
84b4e042
JB
14356static bool intel_crt_present(struct drm_device *dev)
14357{
14358 struct drm_i915_private *dev_priv = dev->dev_private;
14359
884497ed
DL
14360 if (INTEL_INFO(dev)->gen >= 9)
14361 return false;
14362
cf404ce4 14363 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
84b4e042
JB
14364 return false;
14365
14366 if (IS_CHERRYVIEW(dev))
14367 return false;
14368
65e472e4
VS
14369 if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
14370 return false;
14371
70ac54d0
VS
14372 /* DDI E can't be used if DDI A requires 4 lanes */
14373 if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
14374 return false;
14375
e4abb733 14376 if (!dev_priv->vbt.int_crt_support)
84b4e042
JB
14377 return false;
14378
14379 return true;
14380}
14381
79e53945
JB
14382static void intel_setup_outputs(struct drm_device *dev)
14383{
725e30ad 14384 struct drm_i915_private *dev_priv = dev->dev_private;
4ef69c7a 14385 struct intel_encoder *encoder;
cb0953d7 14386 bool dpd_is_edp = false;
79e53945 14387
c9093354 14388 intel_lvds_init(dev);
79e53945 14389
84b4e042 14390 if (intel_crt_present(dev))
79935fca 14391 intel_crt_init(dev);
cb0953d7 14392
c776eb2e
VK
14393 if (IS_BROXTON(dev)) {
14394 /*
14395 * FIXME: Broxton doesn't support port detection via the
14396 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14397 * detect the ports.
14398 */
14399 intel_ddi_init(dev, PORT_A);
14400 intel_ddi_init(dev, PORT_B);
14401 intel_ddi_init(dev, PORT_C);
c6c794a2
SS
14402
14403 intel_dsi_init(dev);
c776eb2e 14404 } else if (HAS_DDI(dev)) {
0e72a5b5
ED
14405 int found;
14406
de31facd
JB
14407 /*
14408 * Haswell uses DDI functions to detect digital outputs.
14409 * On SKL pre-D0 the strap isn't connected, so we assume
14410 * it's there.
14411 */
77179400 14412 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
de31facd 14413 /* WaIgnoreDDIAStrap: skl */
ef11bdb3 14414 if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
0e72a5b5
ED
14415 intel_ddi_init(dev, PORT_A);
14416
14417 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14418 * register */
14419 found = I915_READ(SFUSE_STRAP);
14420
14421 if (found & SFUSE_STRAP_DDIB_DETECTED)
14422 intel_ddi_init(dev, PORT_B);
14423 if (found & SFUSE_STRAP_DDIC_DETECTED)
14424 intel_ddi_init(dev, PORT_C);
14425 if (found & SFUSE_STRAP_DDID_DETECTED)
14426 intel_ddi_init(dev, PORT_D);
2800e4c2
RV
14427 /*
14428 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14429 */
ef11bdb3 14430 if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
2800e4c2
RV
14431 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14432 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14433 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
14434 intel_ddi_init(dev, PORT_E);
14435
0e72a5b5 14436 } else if (HAS_PCH_SPLIT(dev)) {
cb0953d7 14437 int found;
5d8a7752 14438 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
270b3042
DV
14439
14440 if (has_edp_a(dev))
14441 intel_dp_init(dev, DP_A, PORT_A);
cb0953d7 14442
dc0fa718 14443 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 14444 /* PCH SDVOB multiplex with HDMIB */
2a5c0832 14445 found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
30ad48b7 14446 if (!found)
e2debe91 14447 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
5eb08b69 14448 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
ab9d7c30 14449 intel_dp_init(dev, PCH_DP_B, PORT_B);
30ad48b7
ZW
14450 }
14451
dc0fa718 14452 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
e2debe91 14453 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
30ad48b7 14454
dc0fa718 14455 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
e2debe91 14456 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
30ad48b7 14457
5eb08b69 14458 if (I915_READ(PCH_DP_C) & DP_DETECTED)
ab9d7c30 14459 intel_dp_init(dev, PCH_DP_C, PORT_C);
5eb08b69 14460
270b3042 14461 if (I915_READ(PCH_DP_D) & DP_DETECTED)
ab9d7c30 14462 intel_dp_init(dev, PCH_DP_D, PORT_D);
666a4537 14463 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
e17ac6db
VS
14464 /*
14465 * The DP_DETECTED bit is the latched state of the DDC
14466 * SDA pin at boot. However since eDP doesn't require DDC
14467 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14468 * eDP ports may have been muxed to an alternate function.
14469 * Thus we can't rely on the DP_DETECTED bit alone to detect
14470 * eDP ports. Consult the VBT as well as DP_DETECTED to
14471 * detect eDP ports.
14472 */
e66eb81d 14473 if (I915_READ(VLV_HDMIB) & SDVO_DETECTED &&
d2182a66 14474 !intel_dp_is_edp(dev, PORT_B))
e66eb81d
VS
14475 intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
14476 if (I915_READ(VLV_DP_B) & DP_DETECTED ||
e17ac6db 14477 intel_dp_is_edp(dev, PORT_B))
e66eb81d 14478 intel_dp_init(dev, VLV_DP_B, PORT_B);
585a94b8 14479
e66eb81d 14480 if (I915_READ(VLV_HDMIC) & SDVO_DETECTED &&
d2182a66 14481 !intel_dp_is_edp(dev, PORT_C))
e66eb81d
VS
14482 intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
14483 if (I915_READ(VLV_DP_C) & DP_DETECTED ||
e17ac6db 14484 intel_dp_is_edp(dev, PORT_C))
e66eb81d 14485 intel_dp_init(dev, VLV_DP_C, PORT_C);
19c03924 14486
9418c1f1 14487 if (IS_CHERRYVIEW(dev)) {
e17ac6db 14488 /* eDP not supported on port D, so don't check VBT */
e66eb81d
VS
14489 if (I915_READ(CHV_HDMID) & SDVO_DETECTED)
14490 intel_hdmi_init(dev, CHV_HDMID, PORT_D);
14491 if (I915_READ(CHV_DP_D) & DP_DETECTED)
14492 intel_dp_init(dev, CHV_DP_D, PORT_D);
9418c1f1
VS
14493 }
14494
3cfca973 14495 intel_dsi_init(dev);
09da55dc 14496 } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
27185ae1 14497 bool found = false;
7d57382e 14498
e2debe91 14499 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14500 DRM_DEBUG_KMS("probing SDVOB\n");
2a5c0832 14501 found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
3fec3d2f 14502 if (!found && IS_G4X(dev)) {
b01f2c3a 14503 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
e2debe91 14504 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
b01f2c3a 14505 }
27185ae1 14506
3fec3d2f 14507 if (!found && IS_G4X(dev))
ab9d7c30 14508 intel_dp_init(dev, DP_B, PORT_B);
725e30ad 14509 }
13520b05
KH
14510
14511 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 14512
e2debe91 14513 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14514 DRM_DEBUG_KMS("probing SDVOC\n");
2a5c0832 14515 found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
b01f2c3a 14516 }
27185ae1 14517
e2debe91 14518 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 14519
3fec3d2f 14520 if (IS_G4X(dev)) {
b01f2c3a 14521 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
e2debe91 14522 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
b01f2c3a 14523 }
3fec3d2f 14524 if (IS_G4X(dev))
ab9d7c30 14525 intel_dp_init(dev, DP_C, PORT_C);
725e30ad 14526 }
27185ae1 14527
3fec3d2f 14528 if (IS_G4X(dev) &&
e7281eab 14529 (I915_READ(DP_D) & DP_DETECTED))
ab9d7c30 14530 intel_dp_init(dev, DP_D, PORT_D);
bad720ff 14531 } else if (IS_GEN2(dev))
79e53945
JB
14532 intel_dvo_init(dev);
14533
103a196f 14534 if (SUPPORTS_TV(dev))
79e53945
JB
14535 intel_tv_init(dev);
14536
0bc12bcb 14537 intel_psr_init(dev);
7c8f8a70 14538
b2784e15 14539 for_each_intel_encoder(dev, encoder) {
4ef69c7a
CW
14540 encoder->base.possible_crtcs = encoder->crtc_mask;
14541 encoder->base.possible_clones =
66a9278e 14542 intel_encoder_clones(encoder);
79e53945 14543 }
47356eb6 14544
dde86e2d 14545 intel_init_pch_refclk(dev);
270b3042
DV
14546
14547 drm_helper_move_panel_connectors_to_head(dev);
79e53945
JB
14548}
14549
14550static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14551{
60a5ca01 14552 struct drm_device *dev = fb->dev;
79e53945 14553 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 14554
ef2d633e 14555 drm_framebuffer_cleanup(fb);
60a5ca01 14556 mutex_lock(&dev->struct_mutex);
ef2d633e 14557 WARN_ON(!intel_fb->obj->framebuffer_references--);
60a5ca01
VS
14558 drm_gem_object_unreference(&intel_fb->obj->base);
14559 mutex_unlock(&dev->struct_mutex);
79e53945
JB
14560 kfree(intel_fb);
14561}
14562
14563static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 14564 struct drm_file *file,
79e53945
JB
14565 unsigned int *handle)
14566{
14567 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 14568 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 14569
cc917ab4
CW
14570 if (obj->userptr.mm) {
14571 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14572 return -EINVAL;
14573 }
14574
05394f39 14575 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
14576}
14577
86c98588
RV
14578static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14579 struct drm_file *file,
14580 unsigned flags, unsigned color,
14581 struct drm_clip_rect *clips,
14582 unsigned num_clips)
14583{
14584 struct drm_device *dev = fb->dev;
14585 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14586 struct drm_i915_gem_object *obj = intel_fb->obj;
14587
14588 mutex_lock(&dev->struct_mutex);
74b4ea1e 14589 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
86c98588
RV
14590 mutex_unlock(&dev->struct_mutex);
14591
14592 return 0;
14593}
14594
79e53945
JB
14595static const struct drm_framebuffer_funcs intel_fb_funcs = {
14596 .destroy = intel_user_framebuffer_destroy,
14597 .create_handle = intel_user_framebuffer_create_handle,
86c98588 14598 .dirty = intel_user_framebuffer_dirty,
79e53945
JB
14599};
14600
b321803d
DL
14601static
14602u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14603 uint32_t pixel_format)
14604{
14605 u32 gen = INTEL_INFO(dev)->gen;
14606
14607 if (gen >= 9) {
ac484963
VS
14608 int cpp = drm_format_plane_cpp(pixel_format, 0);
14609
b321803d
DL
14610 /* "The stride in bytes must not exceed the of the size of 8K
14611 * pixels and 32K bytes."
14612 */
ac484963 14613 return min(8192 * cpp, 32768);
666a4537 14614 } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
b321803d
DL
14615 return 32*1024;
14616 } else if (gen >= 4) {
14617 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14618 return 16*1024;
14619 else
14620 return 32*1024;
14621 } else if (gen >= 3) {
14622 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14623 return 8*1024;
14624 else
14625 return 16*1024;
14626 } else {
14627 /* XXX DSPC is limited to 4k tiled */
14628 return 8*1024;
14629 }
14630}
14631
b5ea642a
DV
14632static int intel_framebuffer_init(struct drm_device *dev,
14633 struct intel_framebuffer *intel_fb,
14634 struct drm_mode_fb_cmd2 *mode_cmd,
14635 struct drm_i915_gem_object *obj)
79e53945 14636{
7b49f948 14637 struct drm_i915_private *dev_priv = to_i915(dev);
6761dd31 14638 unsigned int aligned_height;
79e53945 14639 int ret;
b321803d 14640 u32 pitch_limit, stride_alignment;
79e53945 14641
dd4916c5
DV
14642 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14643
2a80eada
DV
14644 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14645 /* Enforce that fb modifier and tiling mode match, but only for
14646 * X-tiled. This is needed for FBC. */
14647 if (!!(obj->tiling_mode == I915_TILING_X) !=
14648 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14649 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14650 return -EINVAL;
14651 }
14652 } else {
14653 if (obj->tiling_mode == I915_TILING_X)
14654 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14655 else if (obj->tiling_mode == I915_TILING_Y) {
14656 DRM_DEBUG("No Y tiling for legacy addfb\n");
14657 return -EINVAL;
14658 }
14659 }
14660
9a8f0a12
TU
14661 /* Passed in modifier sanity checking. */
14662 switch (mode_cmd->modifier[0]) {
14663 case I915_FORMAT_MOD_Y_TILED:
14664 case I915_FORMAT_MOD_Yf_TILED:
14665 if (INTEL_INFO(dev)->gen < 9) {
14666 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14667 mode_cmd->modifier[0]);
14668 return -EINVAL;
14669 }
14670 case DRM_FORMAT_MOD_NONE:
14671 case I915_FORMAT_MOD_X_TILED:
14672 break;
14673 default:
c0f40428
JB
14674 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14675 mode_cmd->modifier[0]);
57cd6508 14676 return -EINVAL;
c16ed4be 14677 }
57cd6508 14678
7b49f948
VS
14679 stride_alignment = intel_fb_stride_alignment(dev_priv,
14680 mode_cmd->modifier[0],
b321803d
DL
14681 mode_cmd->pixel_format);
14682 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14683 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14684 mode_cmd->pitches[0], stride_alignment);
57cd6508 14685 return -EINVAL;
c16ed4be 14686 }
57cd6508 14687
b321803d
DL
14688 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14689 mode_cmd->pixel_format);
a35cdaa0 14690 if (mode_cmd->pitches[0] > pitch_limit) {
b321803d
DL
14691 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14692 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
2a80eada 14693 "tiled" : "linear",
a35cdaa0 14694 mode_cmd->pitches[0], pitch_limit);
5d7bd705 14695 return -EINVAL;
c16ed4be 14696 }
5d7bd705 14697
2a80eada 14698 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
c16ed4be
CW
14699 mode_cmd->pitches[0] != obj->stride) {
14700 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14701 mode_cmd->pitches[0], obj->stride);
5d7bd705 14702 return -EINVAL;
c16ed4be 14703 }
5d7bd705 14704
57779d06 14705 /* Reject formats not supported by any plane early. */
308e5bcb 14706 switch (mode_cmd->pixel_format) {
57779d06 14707 case DRM_FORMAT_C8:
04b3924d
VS
14708 case DRM_FORMAT_RGB565:
14709 case DRM_FORMAT_XRGB8888:
14710 case DRM_FORMAT_ARGB8888:
57779d06
VS
14711 break;
14712 case DRM_FORMAT_XRGB1555:
c16ed4be 14713 if (INTEL_INFO(dev)->gen > 3) {
4ee62c76
VS
14714 DRM_DEBUG("unsupported pixel format: %s\n",
14715 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14716 return -EINVAL;
c16ed4be 14717 }
57779d06 14718 break;
57779d06 14719 case DRM_FORMAT_ABGR8888:
666a4537
WB
14720 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
14721 INTEL_INFO(dev)->gen < 9) {
6c0fd451
DL
14722 DRM_DEBUG("unsupported pixel format: %s\n",
14723 drm_get_format_name(mode_cmd->pixel_format));
14724 return -EINVAL;
14725 }
14726 break;
14727 case DRM_FORMAT_XBGR8888:
04b3924d 14728 case DRM_FORMAT_XRGB2101010:
57779d06 14729 case DRM_FORMAT_XBGR2101010:
c16ed4be 14730 if (INTEL_INFO(dev)->gen < 4) {
4ee62c76
VS
14731 DRM_DEBUG("unsupported pixel format: %s\n",
14732 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14733 return -EINVAL;
c16ed4be 14734 }
b5626747 14735 break;
7531208b 14736 case DRM_FORMAT_ABGR2101010:
666a4537 14737 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
7531208b
DL
14738 DRM_DEBUG("unsupported pixel format: %s\n",
14739 drm_get_format_name(mode_cmd->pixel_format));
14740 return -EINVAL;
14741 }
14742 break;
04b3924d
VS
14743 case DRM_FORMAT_YUYV:
14744 case DRM_FORMAT_UYVY:
14745 case DRM_FORMAT_YVYU:
14746 case DRM_FORMAT_VYUY:
c16ed4be 14747 if (INTEL_INFO(dev)->gen < 5) {
4ee62c76
VS
14748 DRM_DEBUG("unsupported pixel format: %s\n",
14749 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14750 return -EINVAL;
c16ed4be 14751 }
57cd6508
CW
14752 break;
14753 default:
4ee62c76
VS
14754 DRM_DEBUG("unsupported pixel format: %s\n",
14755 drm_get_format_name(mode_cmd->pixel_format));
57cd6508
CW
14756 return -EINVAL;
14757 }
14758
90f9a336
VS
14759 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14760 if (mode_cmd->offsets[0] != 0)
14761 return -EINVAL;
14762
ec2c981e 14763 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
091df6cb
DV
14764 mode_cmd->pixel_format,
14765 mode_cmd->modifier[0]);
53155c0a
DV
14766 /* FIXME drm helper for size checks (especially planar formats)? */
14767 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14768 return -EINVAL;
14769
c7d73f6a
DV
14770 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14771 intel_fb->obj = obj;
14772
2d7a215f
VS
14773 intel_fill_fb_info(dev_priv, &intel_fb->base);
14774
79e53945
JB
14775 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14776 if (ret) {
14777 DRM_ERROR("framebuffer init failed %d\n", ret);
14778 return ret;
14779 }
14780
0b05e1e0
VS
14781 intel_fb->obj->framebuffer_references++;
14782
79e53945
JB
14783 return 0;
14784}
14785
79e53945
JB
14786static struct drm_framebuffer *
14787intel_user_framebuffer_create(struct drm_device *dev,
14788 struct drm_file *filp,
1eb83451 14789 const struct drm_mode_fb_cmd2 *user_mode_cmd)
79e53945 14790{
dcb1394e 14791 struct drm_framebuffer *fb;
05394f39 14792 struct drm_i915_gem_object *obj;
76dc3769 14793 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
79e53945 14794
308e5bcb 14795 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
76dc3769 14796 mode_cmd.handles[0]));
c8725226 14797 if (&obj->base == NULL)
cce13ff7 14798 return ERR_PTR(-ENOENT);
79e53945 14799
92907cbb 14800 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
dcb1394e
LW
14801 if (IS_ERR(fb))
14802 drm_gem_object_unreference_unlocked(&obj->base);
14803
14804 return fb;
79e53945
JB
14805}
14806
0695726e 14807#ifndef CONFIG_DRM_FBDEV_EMULATION
0632fef6 14808static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
4520f53a
DV
14809{
14810}
14811#endif
14812
79e53945 14813static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 14814 .fb_create = intel_user_framebuffer_create,
0632fef6 14815 .output_poll_changed = intel_fbdev_output_poll_changed,
5ee67f1c
MR
14816 .atomic_check = intel_atomic_check,
14817 .atomic_commit = intel_atomic_commit,
de419ab6
ML
14818 .atomic_state_alloc = intel_atomic_state_alloc,
14819 .atomic_state_clear = intel_atomic_state_clear,
79e53945
JB
14820};
14821
88212941
ID
14822/**
14823 * intel_init_display_hooks - initialize the display modesetting hooks
14824 * @dev_priv: device private
14825 */
14826void intel_init_display_hooks(struct drm_i915_private *dev_priv)
e70236a8 14827{
88212941 14828 if (INTEL_INFO(dev_priv)->gen >= 9) {
bc8d7dff 14829 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14830 dev_priv->display.get_initial_plane_config =
14831 skylake_get_initial_plane_config;
bc8d7dff
DL
14832 dev_priv->display.crtc_compute_clock =
14833 haswell_crtc_compute_clock;
14834 dev_priv->display.crtc_enable = haswell_crtc_enable;
14835 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 14836 } else if (HAS_DDI(dev_priv)) {
0e8ffe1b 14837 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14838 dev_priv->display.get_initial_plane_config =
14839 ironlake_get_initial_plane_config;
797d0259
ACO
14840 dev_priv->display.crtc_compute_clock =
14841 haswell_crtc_compute_clock;
4f771f10
PZ
14842 dev_priv->display.crtc_enable = haswell_crtc_enable;
14843 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 14844 } else if (HAS_PCH_SPLIT(dev_priv)) {
0e8ffe1b 14845 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
5724dbd1
DL
14846 dev_priv->display.get_initial_plane_config =
14847 ironlake_get_initial_plane_config;
3fb37703
ACO
14848 dev_priv->display.crtc_compute_clock =
14849 ironlake_crtc_compute_clock;
76e5a89c
DV
14850 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14851 dev_priv->display.crtc_disable = ironlake_crtc_disable;
65b3d6a9 14852 } else if (IS_CHERRYVIEW(dev_priv)) {
89b667f8 14853 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14854 dev_priv->display.get_initial_plane_config =
14855 i9xx_get_initial_plane_config;
65b3d6a9
ACO
14856 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
14857 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14858 dev_priv->display.crtc_disable = i9xx_crtc_disable;
14859 } else if (IS_VALLEYVIEW(dev_priv)) {
14860 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14861 dev_priv->display.get_initial_plane_config =
14862 i9xx_get_initial_plane_config;
14863 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
89b667f8
JB
14864 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14865 dev_priv->display.crtc_disable = i9xx_crtc_disable;
19ec6693
ACO
14866 } else if (IS_G4X(dev_priv)) {
14867 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14868 dev_priv->display.get_initial_plane_config =
14869 i9xx_get_initial_plane_config;
14870 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
14871 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14872 dev_priv->display.crtc_disable = i9xx_crtc_disable;
70e8aa21
ACO
14873 } else if (IS_PINEVIEW(dev_priv)) {
14874 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14875 dev_priv->display.get_initial_plane_config =
14876 i9xx_get_initial_plane_config;
14877 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
14878 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14879 dev_priv->display.crtc_disable = i9xx_crtc_disable;
81c97f52 14880 } else if (!IS_GEN2(dev_priv)) {
0e8ffe1b 14881 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14882 dev_priv->display.get_initial_plane_config =
14883 i9xx_get_initial_plane_config;
d6dfee7a 14884 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
76e5a89c
DV
14885 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14886 dev_priv->display.crtc_disable = i9xx_crtc_disable;
81c97f52
ACO
14887 } else {
14888 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14889 dev_priv->display.get_initial_plane_config =
14890 i9xx_get_initial_plane_config;
14891 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
14892 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14893 dev_priv->display.crtc_disable = i9xx_crtc_disable;
f564048e 14894 }
e70236a8 14895
e70236a8 14896 /* Returns the core display clock speed */
88212941 14897 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
1652d19e
VS
14898 dev_priv->display.get_display_clock_speed =
14899 skylake_get_display_clock_speed;
88212941 14900 else if (IS_BROXTON(dev_priv))
acd3f3d3
BP
14901 dev_priv->display.get_display_clock_speed =
14902 broxton_get_display_clock_speed;
88212941 14903 else if (IS_BROADWELL(dev_priv))
1652d19e
VS
14904 dev_priv->display.get_display_clock_speed =
14905 broadwell_get_display_clock_speed;
88212941 14906 else if (IS_HASWELL(dev_priv))
1652d19e
VS
14907 dev_priv->display.get_display_clock_speed =
14908 haswell_get_display_clock_speed;
88212941 14909 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
25eb05fc
JB
14910 dev_priv->display.get_display_clock_speed =
14911 valleyview_get_display_clock_speed;
88212941 14912 else if (IS_GEN5(dev_priv))
b37a6434
VS
14913 dev_priv->display.get_display_clock_speed =
14914 ilk_get_display_clock_speed;
88212941
ID
14915 else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
14916 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
e70236a8
JB
14917 dev_priv->display.get_display_clock_speed =
14918 i945_get_display_clock_speed;
88212941 14919 else if (IS_GM45(dev_priv))
34edce2f
VS
14920 dev_priv->display.get_display_clock_speed =
14921 gm45_get_display_clock_speed;
88212941 14922 else if (IS_CRESTLINE(dev_priv))
34edce2f
VS
14923 dev_priv->display.get_display_clock_speed =
14924 i965gm_get_display_clock_speed;
88212941 14925 else if (IS_PINEVIEW(dev_priv))
34edce2f
VS
14926 dev_priv->display.get_display_clock_speed =
14927 pnv_get_display_clock_speed;
88212941 14928 else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
34edce2f
VS
14929 dev_priv->display.get_display_clock_speed =
14930 g33_get_display_clock_speed;
88212941 14931 else if (IS_I915G(dev_priv))
e70236a8
JB
14932 dev_priv->display.get_display_clock_speed =
14933 i915_get_display_clock_speed;
88212941 14934 else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
e70236a8
JB
14935 dev_priv->display.get_display_clock_speed =
14936 i9xx_misc_get_display_clock_speed;
88212941 14937 else if (IS_I915GM(dev_priv))
e70236a8
JB
14938 dev_priv->display.get_display_clock_speed =
14939 i915gm_get_display_clock_speed;
88212941 14940 else if (IS_I865G(dev_priv))
e70236a8
JB
14941 dev_priv->display.get_display_clock_speed =
14942 i865_get_display_clock_speed;
88212941 14943 else if (IS_I85X(dev_priv))
e70236a8 14944 dev_priv->display.get_display_clock_speed =
1b1d2716 14945 i85x_get_display_clock_speed;
623e01e5 14946 else { /* 830 */
88212941 14947 WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
e70236a8
JB
14948 dev_priv->display.get_display_clock_speed =
14949 i830_get_display_clock_speed;
623e01e5 14950 }
e70236a8 14951
88212941 14952 if (IS_GEN5(dev_priv)) {
3bb11b53 14953 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
88212941 14954 } else if (IS_GEN6(dev_priv)) {
3bb11b53 14955 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
88212941 14956 } else if (IS_IVYBRIDGE(dev_priv)) {
3bb11b53
SJ
14957 /* FIXME: detect B0+ stepping and use auto training */
14958 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
88212941 14959 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
3bb11b53 14960 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
88212941 14961 if (IS_BROADWELL(dev_priv)) {
27c329ed
ML
14962 dev_priv->display.modeset_commit_cdclk =
14963 broadwell_modeset_commit_cdclk;
14964 dev_priv->display.modeset_calc_cdclk =
14965 broadwell_modeset_calc_cdclk;
14966 }
88212941 14967 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
27c329ed
ML
14968 dev_priv->display.modeset_commit_cdclk =
14969 valleyview_modeset_commit_cdclk;
14970 dev_priv->display.modeset_calc_cdclk =
14971 valleyview_modeset_calc_cdclk;
88212941 14972 } else if (IS_BROXTON(dev_priv)) {
27c329ed
ML
14973 dev_priv->display.modeset_commit_cdclk =
14974 broxton_modeset_commit_cdclk;
14975 dev_priv->display.modeset_calc_cdclk =
14976 broxton_modeset_calc_cdclk;
e70236a8 14977 }
8c9f3aaf 14978
88212941 14979 switch (INTEL_INFO(dev_priv)->gen) {
8c9f3aaf
JB
14980 case 2:
14981 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14982 break;
14983
14984 case 3:
14985 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14986 break;
14987
14988 case 4:
14989 case 5:
14990 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14991 break;
14992
14993 case 6:
14994 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14995 break;
7c9017e5 14996 case 7:
4e0bbc31 14997 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
7c9017e5
JB
14998 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14999 break;
830c81db 15000 case 9:
ba343e02
TU
15001 /* Drop through - unsupported since execlist only. */
15002 default:
15003 /* Default just returns -ENODEV to indicate unsupported */
15004 dev_priv->display.queue_flip = intel_default_queue_flip;
8c9f3aaf 15005 }
e70236a8
JB
15006}
15007
b690e96c
JB
15008/*
15009 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
15010 * resume, or other times. This quirk makes sure that's the case for
15011 * affected systems.
15012 */
0206e353 15013static void quirk_pipea_force(struct drm_device *dev)
b690e96c
JB
15014{
15015 struct drm_i915_private *dev_priv = dev->dev_private;
15016
15017 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
bc0daf48 15018 DRM_INFO("applying pipe a force quirk\n");
b690e96c
JB
15019}
15020
b6b5d049
VS
15021static void quirk_pipeb_force(struct drm_device *dev)
15022{
15023 struct drm_i915_private *dev_priv = dev->dev_private;
15024
15025 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
15026 DRM_INFO("applying pipe b force quirk\n");
15027}
15028
435793df
KP
15029/*
15030 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
15031 */
15032static void quirk_ssc_force_disable(struct drm_device *dev)
15033{
15034 struct drm_i915_private *dev_priv = dev->dev_private;
15035 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 15036 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
15037}
15038
4dca20ef 15039/*
5a15ab5b
CE
15040 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
15041 * brightness value
4dca20ef
CE
15042 */
15043static void quirk_invert_brightness(struct drm_device *dev)
15044{
15045 struct drm_i915_private *dev_priv = dev->dev_private;
15046 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 15047 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
15048}
15049
9c72cc6f
SD
15050/* Some VBT's incorrectly indicate no backlight is present */
15051static void quirk_backlight_present(struct drm_device *dev)
15052{
15053 struct drm_i915_private *dev_priv = dev->dev_private;
15054 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
15055 DRM_INFO("applying backlight present quirk\n");
15056}
15057
b690e96c
JB
15058struct intel_quirk {
15059 int device;
15060 int subsystem_vendor;
15061 int subsystem_device;
15062 void (*hook)(struct drm_device *dev);
15063};
15064
5f85f176
EE
15065/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
15066struct intel_dmi_quirk {
15067 void (*hook)(struct drm_device *dev);
15068 const struct dmi_system_id (*dmi_id_list)[];
15069};
15070
15071static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
15072{
15073 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
15074 return 1;
15075}
15076
15077static const struct intel_dmi_quirk intel_dmi_quirks[] = {
15078 {
15079 .dmi_id_list = &(const struct dmi_system_id[]) {
15080 {
15081 .callback = intel_dmi_reverse_brightness,
15082 .ident = "NCR Corporation",
15083 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
15084 DMI_MATCH(DMI_PRODUCT_NAME, ""),
15085 },
15086 },
15087 { } /* terminating entry */
15088 },
15089 .hook = quirk_invert_brightness,
15090 },
15091};
15092
c43b5634 15093static struct intel_quirk intel_quirks[] = {
b690e96c
JB
15094 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
15095 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
15096
b690e96c
JB
15097 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
15098 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
15099
5f080c0f
VS
15100 /* 830 needs to leave pipe A & dpll A up */
15101 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
15102
b6b5d049
VS
15103 /* 830 needs to leave pipe B & dpll B up */
15104 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
15105
435793df
KP
15106 /* Lenovo U160 cannot use SSC on LVDS */
15107 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
15108
15109 /* Sony Vaio Y cannot use SSC on LVDS */
15110 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 15111
be505f64
AH
15112 /* Acer Aspire 5734Z must invert backlight brightness */
15113 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
15114
15115 /* Acer/eMachines G725 */
15116 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
15117
15118 /* Acer/eMachines e725 */
15119 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
15120
15121 /* Acer/Packard Bell NCL20 */
15122 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
15123
15124 /* Acer Aspire 4736Z */
15125 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
15126
15127 /* Acer Aspire 5336 */
15128 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
2e93a1aa
SD
15129
15130 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
15131 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
d4967d8c 15132
dfb3d47b
SD
15133 /* Acer C720 Chromebook (Core i3 4005U) */
15134 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
15135
b2a9601c 15136 /* Apple Macbook 2,1 (Core 2 T7400) */
15137 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
15138
1b9448b0
JN
15139 /* Apple Macbook 4,1 */
15140 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
15141
d4967d8c
SD
15142 /* Toshiba CB35 Chromebook (Celeron 2955U) */
15143 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
724cb06f
SD
15144
15145 /* HP Chromebook 14 (Celeron 2955U) */
15146 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
cf6f0af9
JN
15147
15148 /* Dell Chromebook 11 */
15149 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
9be64eee
JN
15150
15151 /* Dell Chromebook 11 (2015 version) */
15152 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
b690e96c
JB
15153};
15154
15155static void intel_init_quirks(struct drm_device *dev)
15156{
15157 struct pci_dev *d = dev->pdev;
15158 int i;
15159
15160 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
15161 struct intel_quirk *q = &intel_quirks[i];
15162
15163 if (d->device == q->device &&
15164 (d->subsystem_vendor == q->subsystem_vendor ||
15165 q->subsystem_vendor == PCI_ANY_ID) &&
15166 (d->subsystem_device == q->subsystem_device ||
15167 q->subsystem_device == PCI_ANY_ID))
15168 q->hook(dev);
15169 }
5f85f176
EE
15170 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
15171 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
15172 intel_dmi_quirks[i].hook(dev);
15173 }
b690e96c
JB
15174}
15175
9cce37f4
JB
15176/* Disable the VGA plane that we never use */
15177static void i915_disable_vga(struct drm_device *dev)
15178{
15179 struct drm_i915_private *dev_priv = dev->dev_private;
15180 u8 sr1;
f0f59a00 15181 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
9cce37f4 15182
2b37c616 15183 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
9cce37f4 15184 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 15185 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
15186 sr1 = inb(VGA_SR_DATA);
15187 outb(sr1 | 1<<5, VGA_SR_DATA);
15188 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
15189 udelay(300);
15190
01f5a626 15191 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9cce37f4
JB
15192 POSTING_READ(vga_reg);
15193}
15194
f817586c
DV
15195void intel_modeset_init_hw(struct drm_device *dev)
15196{
1a617b77
ML
15197 struct drm_i915_private *dev_priv = dev->dev_private;
15198
b6283055 15199 intel_update_cdclk(dev);
1a617b77
ML
15200
15201 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
15202
f817586c 15203 intel_init_clock_gating(dev);
8090c6b9 15204 intel_enable_gt_powersave(dev);
f817586c
DV
15205}
15206
d93c0372
MR
15207/*
15208 * Calculate what we think the watermarks should be for the state we've read
15209 * out of the hardware and then immediately program those watermarks so that
15210 * we ensure the hardware settings match our internal state.
15211 *
15212 * We can calculate what we think WM's should be by creating a duplicate of the
15213 * current state (which was constructed during hardware readout) and running it
15214 * through the atomic check code to calculate new watermark values in the
15215 * state object.
15216 */
15217static void sanitize_watermarks(struct drm_device *dev)
15218{
15219 struct drm_i915_private *dev_priv = to_i915(dev);
15220 struct drm_atomic_state *state;
15221 struct drm_crtc *crtc;
15222 struct drm_crtc_state *cstate;
15223 struct drm_modeset_acquire_ctx ctx;
15224 int ret;
15225 int i;
15226
15227 /* Only supported on platforms that use atomic watermark design */
ed4a6a7c 15228 if (!dev_priv->display.optimize_watermarks)
d93c0372
MR
15229 return;
15230
15231 /*
15232 * We need to hold connection_mutex before calling duplicate_state so
15233 * that the connector loop is protected.
15234 */
15235 drm_modeset_acquire_init(&ctx, 0);
15236retry:
0cd1262d 15237 ret = drm_modeset_lock_all_ctx(dev, &ctx);
d93c0372
MR
15238 if (ret == -EDEADLK) {
15239 drm_modeset_backoff(&ctx);
15240 goto retry;
15241 } else if (WARN_ON(ret)) {
0cd1262d 15242 goto fail;
d93c0372
MR
15243 }
15244
15245 state = drm_atomic_helper_duplicate_state(dev, &ctx);
15246 if (WARN_ON(IS_ERR(state)))
0cd1262d 15247 goto fail;
d93c0372 15248
ed4a6a7c
MR
15249 /*
15250 * Hardware readout is the only time we don't want to calculate
15251 * intermediate watermarks (since we don't trust the current
15252 * watermarks).
15253 */
15254 to_intel_atomic_state(state)->skip_intermediate_wm = true;
15255
d93c0372
MR
15256 ret = intel_atomic_check(dev, state);
15257 if (ret) {
15258 /*
15259 * If we fail here, it means that the hardware appears to be
15260 * programmed in a way that shouldn't be possible, given our
15261 * understanding of watermark requirements. This might mean a
15262 * mistake in the hardware readout code or a mistake in the
15263 * watermark calculations for a given platform. Raise a WARN
15264 * so that this is noticeable.
15265 *
15266 * If this actually happens, we'll have to just leave the
15267 * BIOS-programmed watermarks untouched and hope for the best.
15268 */
15269 WARN(true, "Could not determine valid watermarks for inherited state\n");
0cd1262d 15270 goto fail;
d93c0372
MR
15271 }
15272
15273 /* Write calculated watermark values back */
15274 to_i915(dev)->wm.config = to_intel_atomic_state(state)->wm_config;
15275 for_each_crtc_in_state(state, crtc, cstate, i) {
15276 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
15277
ed4a6a7c
MR
15278 cs->wm.need_postvbl_update = true;
15279 dev_priv->display.optimize_watermarks(cs);
d93c0372
MR
15280 }
15281
15282 drm_atomic_state_free(state);
0cd1262d 15283fail:
d93c0372
MR
15284 drm_modeset_drop_locks(&ctx);
15285 drm_modeset_acquire_fini(&ctx);
15286}
15287
79e53945
JB
15288void intel_modeset_init(struct drm_device *dev)
15289{
652c393a 15290 struct drm_i915_private *dev_priv = dev->dev_private;
1fe47785 15291 int sprite, ret;
8cc87b75 15292 enum pipe pipe;
46f297fb 15293 struct intel_crtc *crtc;
79e53945
JB
15294
15295 drm_mode_config_init(dev);
15296
15297 dev->mode_config.min_width = 0;
15298 dev->mode_config.min_height = 0;
15299
019d96cb
DA
15300 dev->mode_config.preferred_depth = 24;
15301 dev->mode_config.prefer_shadow = 1;
15302
25bab385
TU
15303 dev->mode_config.allow_fb_modifiers = true;
15304
e6ecefaa 15305 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 15306
b690e96c
JB
15307 intel_init_quirks(dev);
15308
1fa61106
ED
15309 intel_init_pm(dev);
15310
e3c74757
BW
15311 if (INTEL_INFO(dev)->num_pipes == 0)
15312 return;
15313
69f92f67
LW
15314 /*
15315 * There may be no VBT; and if the BIOS enabled SSC we can
15316 * just keep using it to avoid unnecessary flicker. Whereas if the
15317 * BIOS isn't using it, don't assume it will work even if the VBT
15318 * indicates as much.
15319 */
15320 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
15321 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15322 DREF_SSC1_ENABLE);
15323
15324 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
15325 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
15326 bios_lvds_use_ssc ? "en" : "dis",
15327 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
15328 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
15329 }
15330 }
15331
a6c45cf0
CW
15332 if (IS_GEN2(dev)) {
15333 dev->mode_config.max_width = 2048;
15334 dev->mode_config.max_height = 2048;
15335 } else if (IS_GEN3(dev)) {
5e4d6fa7
KP
15336 dev->mode_config.max_width = 4096;
15337 dev->mode_config.max_height = 4096;
79e53945 15338 } else {
a6c45cf0
CW
15339 dev->mode_config.max_width = 8192;
15340 dev->mode_config.max_height = 8192;
79e53945 15341 }
068be561 15342
dc41c154
VS
15343 if (IS_845G(dev) || IS_I865G(dev)) {
15344 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15345 dev->mode_config.cursor_height = 1023;
15346 } else if (IS_GEN2(dev)) {
068be561
DL
15347 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15348 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15349 } else {
15350 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15351 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15352 }
15353
62106b4f 15354 dev->mode_config.fb_base = dev_priv->ggtt.mappable_base;
79e53945 15355
28c97730 15356 DRM_DEBUG_KMS("%d display pipe%s available.\n",
7eb552ae
BW
15357 INTEL_INFO(dev)->num_pipes,
15358 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
79e53945 15359
055e393f 15360 for_each_pipe(dev_priv, pipe) {
8cc87b75 15361 intel_crtc_init(dev, pipe);
3bdcfc0c 15362 for_each_sprite(dev_priv, pipe, sprite) {
1fe47785 15363 ret = intel_plane_init(dev, pipe, sprite);
7f1f3851 15364 if (ret)
06da8da2 15365 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
1fe47785 15366 pipe_name(pipe), sprite_name(pipe, sprite), ret);
7f1f3851 15367 }
79e53945
JB
15368 }
15369
bfa7df01 15370 intel_update_czclk(dev_priv);
e7dc33f3 15371 intel_update_rawclk(dev_priv);
bfa7df01
VS
15372 intel_update_cdclk(dev);
15373
e72f9fbf 15374 intel_shared_dpll_init(dev);
ee7b9f93 15375
9cce37f4
JB
15376 /* Just disable it once at startup */
15377 i915_disable_vga(dev);
79e53945 15378 intel_setup_outputs(dev);
11be49eb 15379
6e9f798d 15380 drm_modeset_lock_all(dev);
043e9bda 15381 intel_modeset_setup_hw_state(dev);
6e9f798d 15382 drm_modeset_unlock_all(dev);
46f297fb 15383
d3fcc808 15384 for_each_intel_crtc(dev, crtc) {
eeebeac5
ML
15385 struct intel_initial_plane_config plane_config = {};
15386
46f297fb
JB
15387 if (!crtc->active)
15388 continue;
15389
46f297fb 15390 /*
46f297fb
JB
15391 * Note that reserving the BIOS fb up front prevents us
15392 * from stuffing other stolen allocations like the ring
15393 * on top. This prevents some ugliness at boot time, and
15394 * can even allow for smooth boot transitions if the BIOS
15395 * fb is large enough for the active pipe configuration.
15396 */
eeebeac5
ML
15397 dev_priv->display.get_initial_plane_config(crtc,
15398 &plane_config);
15399
15400 /*
15401 * If the fb is shared between multiple heads, we'll
15402 * just get the first one.
15403 */
15404 intel_find_initial_plane_obj(crtc, &plane_config);
46f297fb 15405 }
d93c0372
MR
15406
15407 /*
15408 * Make sure hardware watermarks really match the state we read out.
15409 * Note that we need to do this after reconstructing the BIOS fb's
15410 * since the watermark calculation done here will use pstate->fb.
15411 */
15412 sanitize_watermarks(dev);
2c7111db
CW
15413}
15414
7fad798e
DV
15415static void intel_enable_pipe_a(struct drm_device *dev)
15416{
15417 struct intel_connector *connector;
15418 struct drm_connector *crt = NULL;
15419 struct intel_load_detect_pipe load_detect_temp;
208bf9fd 15420 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
7fad798e
DV
15421
15422 /* We can't just switch on the pipe A, we need to set things up with a
15423 * proper mode and output configuration. As a gross hack, enable pipe A
15424 * by enabling the load detect pipe once. */
3a3371ff 15425 for_each_intel_connector(dev, connector) {
7fad798e
DV
15426 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15427 crt = &connector->base;
15428 break;
15429 }
15430 }
15431
15432 if (!crt)
15433 return;
15434
208bf9fd 15435 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
49172fee 15436 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
7fad798e
DV
15437}
15438
fa555837
DV
15439static bool
15440intel_check_plane_mapping(struct intel_crtc *crtc)
15441{
7eb552ae
BW
15442 struct drm_device *dev = crtc->base.dev;
15443 struct drm_i915_private *dev_priv = dev->dev_private;
649636ef 15444 u32 val;
fa555837 15445
7eb552ae 15446 if (INTEL_INFO(dev)->num_pipes == 1)
fa555837
DV
15447 return true;
15448
649636ef 15449 val = I915_READ(DSPCNTR(!crtc->plane));
fa555837
DV
15450
15451 if ((val & DISPLAY_PLANE_ENABLE) &&
15452 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15453 return false;
15454
15455 return true;
15456}
15457
02e93c35
VS
15458static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15459{
15460 struct drm_device *dev = crtc->base.dev;
15461 struct intel_encoder *encoder;
15462
15463 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15464 return true;
15465
15466 return false;
15467}
15468
dd756198
VS
15469static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
15470{
15471 struct drm_device *dev = encoder->base.dev;
15472 struct intel_connector *connector;
15473
15474 for_each_connector_on_encoder(dev, &encoder->base, connector)
15475 return true;
15476
15477 return false;
15478}
15479
24929352
DV
15480static void intel_sanitize_crtc(struct intel_crtc *crtc)
15481{
15482 struct drm_device *dev = crtc->base.dev;
15483 struct drm_i915_private *dev_priv = dev->dev_private;
4d1de975 15484 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
24929352 15485
24929352 15486 /* Clear any frame start delays used for debugging left by the BIOS */
4d1de975
JN
15487 if (!transcoder_is_dsi(cpu_transcoder)) {
15488 i915_reg_t reg = PIPECONF(cpu_transcoder);
15489
15490 I915_WRITE(reg,
15491 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15492 }
24929352 15493
d3eaf884 15494 /* restore vblank interrupts to correct state */
9625604c 15495 drm_crtc_vblank_reset(&crtc->base);
d297e103 15496 if (crtc->active) {
f9cd7b88
VS
15497 struct intel_plane *plane;
15498
9625604c 15499 drm_crtc_vblank_on(&crtc->base);
f9cd7b88
VS
15500
15501 /* Disable everything but the primary plane */
15502 for_each_intel_plane_on_crtc(dev, crtc, plane) {
15503 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
15504 continue;
15505
15506 plane->disable_plane(&plane->base, &crtc->base);
15507 }
9625604c 15508 }
d3eaf884 15509
24929352 15510 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
DV
15511 * disable the crtc (and hence change the state) if it is wrong. Note
15512 * that gen4+ has a fixed plane -> pipe mapping. */
15513 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
24929352
DV
15514 bool plane;
15515
24929352
DV
15516 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
15517 crtc->base.base.id);
15518
15519 /* Pipe has the wrong plane attached and the plane is active.
15520 * Temporarily change the plane mapping and disable everything
15521 * ... */
15522 plane = crtc->plane;
b70709a6 15523 to_intel_plane_state(crtc->base.primary->state)->visible = true;
24929352 15524 crtc->plane = !plane;
b17d48e2 15525 intel_crtc_disable_noatomic(&crtc->base);
24929352 15526 crtc->plane = plane;
24929352 15527 }
24929352 15528
7fad798e
DV
15529 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15530 crtc->pipe == PIPE_A && !crtc->active) {
15531 /* BIOS forgot to enable pipe A, this mostly happens after
15532 * resume. Force-enable the pipe to fix this, the update_dpms
15533 * call below we restore the pipe to the right state, but leave
15534 * the required bits on. */
15535 intel_enable_pipe_a(dev);
15536 }
15537
24929352
DV
15538 /* Adjust the state of the output pipe according to whether we
15539 * have active connectors/encoders. */
842e0307 15540 if (crtc->active && !intel_crtc_has_encoders(crtc))
b17d48e2 15541 intel_crtc_disable_noatomic(&crtc->base);
24929352 15542
a3ed6aad 15543 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
4cc31489
DV
15544 /*
15545 * We start out with underrun reporting disabled to avoid races.
15546 * For correct bookkeeping mark this on active crtcs.
15547 *
c5ab3bc0
DV
15548 * Also on gmch platforms we dont have any hardware bits to
15549 * disable the underrun reporting. Which means we need to start
15550 * out with underrun reporting disabled also on inactive pipes,
15551 * since otherwise we'll complain about the garbage we read when
15552 * e.g. coming up after runtime pm.
15553 *
4cc31489
DV
15554 * No protection against concurrent access is required - at
15555 * worst a fifo underrun happens which also sets this to false.
15556 */
15557 crtc->cpu_fifo_underrun_disabled = true;
15558 crtc->pch_fifo_underrun_disabled = true;
15559 }
24929352
DV
15560}
15561
15562static void intel_sanitize_encoder(struct intel_encoder *encoder)
15563{
15564 struct intel_connector *connector;
15565 struct drm_device *dev = encoder->base.dev;
15566
15567 /* We need to check both for a crtc link (meaning that the
15568 * encoder is active and trying to read from a pipe) and the
15569 * pipe itself being active. */
15570 bool has_active_crtc = encoder->base.crtc &&
15571 to_intel_crtc(encoder->base.crtc)->active;
15572
dd756198 15573 if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
24929352
DV
15574 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15575 encoder->base.base.id,
8e329a03 15576 encoder->base.name);
24929352
DV
15577
15578 /* Connector is active, but has no active pipe. This is
15579 * fallout from our resume register restoring. Disable
15580 * the encoder manually again. */
15581 if (encoder->base.crtc) {
15582 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15583 encoder->base.base.id,
8e329a03 15584 encoder->base.name);
24929352 15585 encoder->disable(encoder);
a62d1497
VS
15586 if (encoder->post_disable)
15587 encoder->post_disable(encoder);
24929352 15588 }
7f1950fb 15589 encoder->base.crtc = NULL;
24929352
DV
15590
15591 /* Inconsistent output/port/pipe state happens presumably due to
15592 * a bug in one of the get_hw_state functions. Or someplace else
15593 * in our code, like the register restore mess on resume. Clamp
15594 * things to off as a safer default. */
3a3371ff 15595 for_each_intel_connector(dev, connector) {
24929352
DV
15596 if (connector->encoder != encoder)
15597 continue;
7f1950fb
EE
15598 connector->base.dpms = DRM_MODE_DPMS_OFF;
15599 connector->base.encoder = NULL;
24929352
DV
15600 }
15601 }
15602 /* Enabled encoders without active connectors will be fixed in
15603 * the crtc fixup. */
15604}
15605
04098753 15606void i915_redisable_vga_power_on(struct drm_device *dev)
0fde901f
KM
15607{
15608 struct drm_i915_private *dev_priv = dev->dev_private;
f0f59a00 15609 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
0fde901f 15610
04098753
ID
15611 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15612 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15613 i915_disable_vga(dev);
15614 }
15615}
15616
15617void i915_redisable_vga(struct drm_device *dev)
15618{
15619 struct drm_i915_private *dev_priv = dev->dev_private;
15620
8dc8a27c
PZ
15621 /* This function can be called both from intel_modeset_setup_hw_state or
15622 * at a very early point in our resume sequence, where the power well
15623 * structures are not yet restored. Since this function is at a very
15624 * paranoid "someone might have enabled VGA while we were not looking"
15625 * level, just check if the power well is enabled instead of trying to
15626 * follow the "don't touch the power well if we don't need it" policy
15627 * the rest of the driver uses. */
6392f847 15628 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
15629 return;
15630
04098753 15631 i915_redisable_vga_power_on(dev);
6392f847
ID
15632
15633 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
0fde901f
KM
15634}
15635
f9cd7b88 15636static bool primary_get_hw_state(struct intel_plane *plane)
98ec7739 15637{
f9cd7b88 15638 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
98ec7739 15639
f9cd7b88 15640 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
d032ffa0
ML
15641}
15642
f9cd7b88
VS
15643/* FIXME read out full plane state for all planes */
15644static void readout_plane_state(struct intel_crtc *crtc)
d032ffa0 15645{
b26d3ea3 15646 struct drm_plane *primary = crtc->base.primary;
f9cd7b88 15647 struct intel_plane_state *plane_state =
b26d3ea3 15648 to_intel_plane_state(primary->state);
d032ffa0 15649
19b8d387 15650 plane_state->visible = crtc->active &&
b26d3ea3
ML
15651 primary_get_hw_state(to_intel_plane(primary));
15652
15653 if (plane_state->visible)
15654 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
98ec7739
VS
15655}
15656
30e984df 15657static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352
DV
15658{
15659 struct drm_i915_private *dev_priv = dev->dev_private;
15660 enum pipe pipe;
24929352
DV
15661 struct intel_crtc *crtc;
15662 struct intel_encoder *encoder;
15663 struct intel_connector *connector;
5358901f 15664 int i;
24929352 15665
565602d7
ML
15666 dev_priv->active_crtcs = 0;
15667
d3fcc808 15668 for_each_intel_crtc(dev, crtc) {
565602d7
ML
15669 struct intel_crtc_state *crtc_state = crtc->config;
15670 int pixclk = 0;
3b117c8f 15671
565602d7
ML
15672 __drm_atomic_helper_crtc_destroy_state(&crtc->base, &crtc_state->base);
15673 memset(crtc_state, 0, sizeof(*crtc_state));
15674 crtc_state->base.crtc = &crtc->base;
24929352 15675
565602d7
ML
15676 crtc_state->base.active = crtc_state->base.enable =
15677 dev_priv->display.get_pipe_config(crtc, crtc_state);
15678
15679 crtc->base.enabled = crtc_state->base.enable;
15680 crtc->active = crtc_state->base.active;
15681
15682 if (crtc_state->base.active) {
15683 dev_priv->active_crtcs |= 1 << crtc->pipe;
15684
15685 if (IS_BROADWELL(dev_priv)) {
15686 pixclk = ilk_pipe_pixel_rate(crtc_state);
15687
15688 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
15689 if (crtc_state->ips_enabled)
15690 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
15691 } else if (IS_VALLEYVIEW(dev_priv) ||
15692 IS_CHERRYVIEW(dev_priv) ||
15693 IS_BROXTON(dev_priv))
15694 pixclk = crtc_state->base.adjusted_mode.crtc_clock;
15695 else
15696 WARN_ON(dev_priv->display.modeset_calc_cdclk);
15697 }
15698
15699 dev_priv->min_pixclk[crtc->pipe] = pixclk;
b70709a6 15700
f9cd7b88 15701 readout_plane_state(crtc);
24929352
DV
15702
15703 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15704 crtc->base.base.id,
15705 crtc->active ? "enabled" : "disabled");
15706 }
15707
5358901f
DV
15708 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15709 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15710
2edd6443
ACO
15711 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
15712 &pll->config.hw_state);
3e369b76 15713 pll->config.crtc_mask = 0;
d3fcc808 15714 for_each_intel_crtc(dev, crtc) {
2dd66ebd 15715 if (crtc->active && crtc->config->shared_dpll == pll)
3e369b76 15716 pll->config.crtc_mask |= 1 << crtc->pipe;
5358901f 15717 }
2dd66ebd 15718 pll->active_mask = pll->config.crtc_mask;
5358901f 15719
1e6f2ddc 15720 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
3e369b76 15721 pll->name, pll->config.crtc_mask, pll->on);
5358901f
DV
15722 }
15723
b2784e15 15724 for_each_intel_encoder(dev, encoder) {
24929352
DV
15725 pipe = 0;
15726
15727 if (encoder->get_hw_state(encoder, &pipe)) {
045ac3b5
JB
15728 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15729 encoder->base.crtc = &crtc->base;
6e3c9717 15730 encoder->get_config(encoder, crtc->config);
24929352
DV
15731 } else {
15732 encoder->base.crtc = NULL;
15733 }
15734
6f2bcceb 15735 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
24929352 15736 encoder->base.base.id,
8e329a03 15737 encoder->base.name,
24929352 15738 encoder->base.crtc ? "enabled" : "disabled",
6f2bcceb 15739 pipe_name(pipe));
24929352
DV
15740 }
15741
3a3371ff 15742 for_each_intel_connector(dev, connector) {
24929352
DV
15743 if (connector->get_hw_state(connector)) {
15744 connector->base.dpms = DRM_MODE_DPMS_ON;
2aa974c9
ML
15745
15746 encoder = connector->encoder;
15747 connector->base.encoder = &encoder->base;
15748
15749 if (encoder->base.crtc &&
15750 encoder->base.crtc->state->active) {
15751 /*
15752 * This has to be done during hardware readout
15753 * because anything calling .crtc_disable may
15754 * rely on the connector_mask being accurate.
15755 */
15756 encoder->base.crtc->state->connector_mask |=
15757 1 << drm_connector_index(&connector->base);
e87a52b3
ML
15758 encoder->base.crtc->state->encoder_mask |=
15759 1 << drm_encoder_index(&encoder->base);
2aa974c9
ML
15760 }
15761
24929352
DV
15762 } else {
15763 connector->base.dpms = DRM_MODE_DPMS_OFF;
15764 connector->base.encoder = NULL;
15765 }
15766 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15767 connector->base.base.id,
c23cc417 15768 connector->base.name,
24929352
DV
15769 connector->base.encoder ? "enabled" : "disabled");
15770 }
7f4c6284
VS
15771
15772 for_each_intel_crtc(dev, crtc) {
15773 crtc->base.hwmode = crtc->config->base.adjusted_mode;
15774
15775 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
15776 if (crtc->base.state->active) {
15777 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
15778 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
15779 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
15780
15781 /*
15782 * The initial mode needs to be set in order to keep
15783 * the atomic core happy. It wants a valid mode if the
15784 * crtc's enabled, so we do the above call.
15785 *
15786 * At this point some state updated by the connectors
15787 * in their ->detect() callback has not run yet, so
15788 * no recalculation can be done yet.
15789 *
15790 * Even if we could do a recalculation and modeset
15791 * right now it would cause a double modeset if
15792 * fbdev or userspace chooses a different initial mode.
15793 *
15794 * If that happens, someone indicated they wanted a
15795 * mode change, which means it's safe to do a full
15796 * recalculation.
15797 */
15798 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
9eca6832
VS
15799
15800 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
15801 update_scanline_offset(crtc);
7f4c6284 15802 }
e3b247da
VS
15803
15804 intel_pipe_config_sanity_check(dev_priv, crtc->config);
7f4c6284 15805 }
30e984df
DV
15806}
15807
043e9bda
ML
15808/* Scan out the current hw modeset state,
15809 * and sanitizes it to the current state
15810 */
15811static void
15812intel_modeset_setup_hw_state(struct drm_device *dev)
30e984df
DV
15813{
15814 struct drm_i915_private *dev_priv = dev->dev_private;
15815 enum pipe pipe;
30e984df
DV
15816 struct intel_crtc *crtc;
15817 struct intel_encoder *encoder;
35c95375 15818 int i;
30e984df
DV
15819
15820 intel_modeset_readout_hw_state(dev);
24929352
DV
15821
15822 /* HW state is read out, now we need to sanitize this mess. */
b2784e15 15823 for_each_intel_encoder(dev, encoder) {
24929352
DV
15824 intel_sanitize_encoder(encoder);
15825 }
15826
055e393f 15827 for_each_pipe(dev_priv, pipe) {
24929352
DV
15828 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15829 intel_sanitize_crtc(crtc);
6e3c9717
ACO
15830 intel_dump_pipe_config(crtc, crtc->config,
15831 "[setup_hw_state]");
24929352 15832 }
9a935856 15833
d29b2f9d
ACO
15834 intel_modeset_update_connector_atomic_state(dev);
15835
35c95375
DV
15836 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15837 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15838
2dd66ebd 15839 if (!pll->on || pll->active_mask)
35c95375
DV
15840 continue;
15841
15842 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15843
2edd6443 15844 pll->funcs.disable(dev_priv, pll);
35c95375
DV
15845 pll->on = false;
15846 }
15847
666a4537 15848 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
6eb1a681
VS
15849 vlv_wm_get_hw_state(dev);
15850 else if (IS_GEN9(dev))
3078999f
PB
15851 skl_wm_get_hw_state(dev);
15852 else if (HAS_PCH_SPLIT(dev))
243e6a44 15853 ilk_wm_get_hw_state(dev);
292b990e
ML
15854
15855 for_each_intel_crtc(dev, crtc) {
15856 unsigned long put_domains;
15857
74bff5f9 15858 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
292b990e
ML
15859 if (WARN_ON(put_domains))
15860 modeset_put_power_domains(dev_priv, put_domains);
15861 }
15862 intel_display_set_init_power(dev_priv, false);
010cf73d
PZ
15863
15864 intel_fbc_init_pipe_state(dev_priv);
043e9bda 15865}
7d0bc1ea 15866
043e9bda
ML
15867void intel_display_resume(struct drm_device *dev)
15868{
e2c8b870
ML
15869 struct drm_i915_private *dev_priv = to_i915(dev);
15870 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
15871 struct drm_modeset_acquire_ctx ctx;
043e9bda 15872 int ret;
e2c8b870 15873 bool setup = false;
f30da187 15874
e2c8b870 15875 dev_priv->modeset_restore_state = NULL;
043e9bda 15876
ea49c9ac
ML
15877 /*
15878 * This is a cludge because with real atomic modeset mode_config.mutex
15879 * won't be taken. Unfortunately some probed state like
15880 * audio_codec_enable is still protected by mode_config.mutex, so lock
15881 * it here for now.
15882 */
15883 mutex_lock(&dev->mode_config.mutex);
e2c8b870 15884 drm_modeset_acquire_init(&ctx, 0);
043e9bda 15885
e2c8b870
ML
15886retry:
15887 ret = drm_modeset_lock_all_ctx(dev, &ctx);
043e9bda 15888
e2c8b870
ML
15889 if (ret == 0 && !setup) {
15890 setup = true;
043e9bda 15891
e2c8b870
ML
15892 intel_modeset_setup_hw_state(dev);
15893 i915_redisable_vga(dev);
45e2b5f6 15894 }
8af6cf88 15895
e2c8b870
ML
15896 if (ret == 0 && state) {
15897 struct drm_crtc_state *crtc_state;
15898 struct drm_crtc *crtc;
15899 int i;
043e9bda 15900
e2c8b870
ML
15901 state->acquire_ctx = &ctx;
15902
15903 for_each_crtc_in_state(state, crtc, crtc_state, i) {
15904 /*
15905 * Force recalculation even if we restore
15906 * current state. With fast modeset this may not result
15907 * in a modeset when the state is compatible.
15908 */
15909 crtc_state->mode_changed = true;
15910 }
15911
15912 ret = drm_atomic_commit(state);
043e9bda
ML
15913 }
15914
e2c8b870
ML
15915 if (ret == -EDEADLK) {
15916 drm_modeset_backoff(&ctx);
15917 goto retry;
15918 }
043e9bda 15919
e2c8b870
ML
15920 drm_modeset_drop_locks(&ctx);
15921 drm_modeset_acquire_fini(&ctx);
ea49c9ac 15922 mutex_unlock(&dev->mode_config.mutex);
043e9bda 15923
e2c8b870
ML
15924 if (ret) {
15925 DRM_ERROR("Restoring old state failed with %i\n", ret);
15926 drm_atomic_state_free(state);
15927 }
2c7111db
CW
15928}
15929
15930void intel_modeset_gem_init(struct drm_device *dev)
15931{
484b41dd 15932 struct drm_crtc *c;
2ff8fde1 15933 struct drm_i915_gem_object *obj;
e0d6149b 15934 int ret;
484b41dd 15935
ae48434c 15936 intel_init_gt_powersave(dev);
ae48434c 15937
1833b134 15938 intel_modeset_init_hw(dev);
02e792fb
DV
15939
15940 intel_setup_overlay(dev);
484b41dd
JB
15941
15942 /*
15943 * Make sure any fbs we allocated at startup are properly
15944 * pinned & fenced. When we do the allocation it's too early
15945 * for this.
15946 */
70e1e0ec 15947 for_each_crtc(dev, c) {
2ff8fde1
MR
15948 obj = intel_fb_obj(c->primary->fb);
15949 if (obj == NULL)
484b41dd
JB
15950 continue;
15951
e0d6149b 15952 mutex_lock(&dev->struct_mutex);
3465c580
VS
15953 ret = intel_pin_and_fence_fb_obj(c->primary->fb,
15954 c->primary->state->rotation);
e0d6149b
TU
15955 mutex_unlock(&dev->struct_mutex);
15956 if (ret) {
484b41dd
JB
15957 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15958 to_intel_crtc(c)->pipe);
66e514c1
DA
15959 drm_framebuffer_unreference(c->primary->fb);
15960 c->primary->fb = NULL;
36750f28 15961 c->primary->crtc = c->primary->state->crtc = NULL;
afd65eb4 15962 update_state_fb(c->primary);
36750f28 15963 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
484b41dd
JB
15964 }
15965 }
0962c3c9
VS
15966
15967 intel_backlight_register(dev);
79e53945
JB
15968}
15969
4932e2c3
ID
15970void intel_connector_unregister(struct intel_connector *intel_connector)
15971{
15972 struct drm_connector *connector = &intel_connector->base;
15973
15974 intel_panel_destroy_backlight(connector);
34ea3d38 15975 drm_connector_unregister(connector);
4932e2c3
ID
15976}
15977
79e53945
JB
15978void intel_modeset_cleanup(struct drm_device *dev)
15979{
652c393a 15980 struct drm_i915_private *dev_priv = dev->dev_private;
19c8054c 15981 struct intel_connector *connector;
652c393a 15982
2eb5252e
ID
15983 intel_disable_gt_powersave(dev);
15984
0962c3c9
VS
15985 intel_backlight_unregister(dev);
15986
fd0c0642
DV
15987 /*
15988 * Interrupts and polling as the first thing to avoid creating havoc.
2eb5252e 15989 * Too much stuff here (turning of connectors, ...) would
fd0c0642
DV
15990 * experience fancy races otherwise.
15991 */
2aeb7d3a 15992 intel_irq_uninstall(dev_priv);
eb21b92b 15993
fd0c0642
DV
15994 /*
15995 * Due to the hpd irq storm handling the hotplug work can re-arm the
15996 * poll handlers. Hence disable polling after hpd handling is shut down.
15997 */
f87ea761 15998 drm_kms_helper_poll_fini(dev);
fd0c0642 15999
723bfd70
JB
16000 intel_unregister_dsm_handler();
16001
c937ab3e 16002 intel_fbc_global_disable(dev_priv);
69341a5e 16003
1630fe75
CW
16004 /* flush any delayed tasks or pending work */
16005 flush_scheduled_work();
16006
db31af1d 16007 /* destroy the backlight and sysfs files before encoders/connectors */
19c8054c
JN
16008 for_each_intel_connector(dev, connector)
16009 connector->unregister(connector);
d9255d57 16010
79e53945 16011 drm_mode_config_cleanup(dev);
4d7bb011
DV
16012
16013 intel_cleanup_overlay(dev);
ae48434c 16014
ae48434c 16015 intel_cleanup_gt_powersave(dev);
f5949141
DV
16016
16017 intel_teardown_gmbus(dev);
79e53945
JB
16018}
16019
f1c79df3
ZW
16020/*
16021 * Return which encoder is currently attached for connector.
16022 */
df0e9248 16023struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
79e53945 16024{
df0e9248
CW
16025 return &intel_attached_encoder(connector)->base;
16026}
f1c79df3 16027
df0e9248
CW
16028void intel_connector_attach_encoder(struct intel_connector *connector,
16029 struct intel_encoder *encoder)
16030{
16031 connector->encoder = encoder;
16032 drm_mode_connector_attach_encoder(&connector->base,
16033 &encoder->base);
79e53945 16034}
28d52043
DA
16035
16036/*
16037 * set vga decode state - true == enable VGA decode
16038 */
16039int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
16040{
16041 struct drm_i915_private *dev_priv = dev->dev_private;
a885b3cc 16042 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
16043 u16 gmch_ctrl;
16044
75fa041d
CW
16045 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
16046 DRM_ERROR("failed to read control word\n");
16047 return -EIO;
16048 }
16049
c0cc8a55
CW
16050 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
16051 return 0;
16052
28d52043
DA
16053 if (state)
16054 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
16055 else
16056 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
16057
16058 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
16059 DRM_ERROR("failed to write control word\n");
16060 return -EIO;
16061 }
16062
28d52043
DA
16063 return 0;
16064}
c4a1d9e4 16065
c4a1d9e4 16066struct intel_display_error_state {
ff57f1b0
PZ
16067
16068 u32 power_well_driver;
16069
63b66e5b
CW
16070 int num_transcoders;
16071
c4a1d9e4
CW
16072 struct intel_cursor_error_state {
16073 u32 control;
16074 u32 position;
16075 u32 base;
16076 u32 size;
52331309 16077 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
16078
16079 struct intel_pipe_error_state {
ddf9c536 16080 bool power_domain_on;
c4a1d9e4 16081 u32 source;
f301b1e1 16082 u32 stat;
52331309 16083 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
16084
16085 struct intel_plane_error_state {
16086 u32 control;
16087 u32 stride;
16088 u32 size;
16089 u32 pos;
16090 u32 addr;
16091 u32 surface;
16092 u32 tile_offset;
52331309 16093 } plane[I915_MAX_PIPES];
63b66e5b
CW
16094
16095 struct intel_transcoder_error_state {
ddf9c536 16096 bool power_domain_on;
63b66e5b
CW
16097 enum transcoder cpu_transcoder;
16098
16099 u32 conf;
16100
16101 u32 htotal;
16102 u32 hblank;
16103 u32 hsync;
16104 u32 vtotal;
16105 u32 vblank;
16106 u32 vsync;
16107 } transcoder[4];
c4a1d9e4
CW
16108};
16109
16110struct intel_display_error_state *
16111intel_display_capture_error_state(struct drm_device *dev)
16112{
fbee40df 16113 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4 16114 struct intel_display_error_state *error;
63b66e5b
CW
16115 int transcoders[] = {
16116 TRANSCODER_A,
16117 TRANSCODER_B,
16118 TRANSCODER_C,
16119 TRANSCODER_EDP,
16120 };
c4a1d9e4
CW
16121 int i;
16122
63b66e5b
CW
16123 if (INTEL_INFO(dev)->num_pipes == 0)
16124 return NULL;
16125
9d1cb914 16126 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
16127 if (error == NULL)
16128 return NULL;
16129
190be112 16130 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
ff57f1b0
PZ
16131 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
16132
055e393f 16133 for_each_pipe(dev_priv, i) {
ddf9c536 16134 error->pipe[i].power_domain_on =
f458ebbc
DV
16135 __intel_display_power_is_enabled(dev_priv,
16136 POWER_DOMAIN_PIPE(i));
ddf9c536 16137 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
16138 continue;
16139
5efb3e28
VS
16140 error->cursor[i].control = I915_READ(CURCNTR(i));
16141 error->cursor[i].position = I915_READ(CURPOS(i));
16142 error->cursor[i].base = I915_READ(CURBASE(i));
c4a1d9e4
CW
16143
16144 error->plane[i].control = I915_READ(DSPCNTR(i));
16145 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
80ca378b 16146 if (INTEL_INFO(dev)->gen <= 3) {
51889b35 16147 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
16148 error->plane[i].pos = I915_READ(DSPPOS(i));
16149 }
ca291363
PZ
16150 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
16151 error->plane[i].addr = I915_READ(DSPADDR(i));
c4a1d9e4
CW
16152 if (INTEL_INFO(dev)->gen >= 4) {
16153 error->plane[i].surface = I915_READ(DSPSURF(i));
16154 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
16155 }
16156
c4a1d9e4 16157 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1 16158
3abfce77 16159 if (HAS_GMCH_DISPLAY(dev))
f301b1e1 16160 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
16161 }
16162
4d1de975 16163 /* Note: this does not include DSI transcoders. */
63b66e5b
CW
16164 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
16165 if (HAS_DDI(dev_priv->dev))
16166 error->num_transcoders++; /* Account for eDP. */
16167
16168 for (i = 0; i < error->num_transcoders; i++) {
16169 enum transcoder cpu_transcoder = transcoders[i];
16170
ddf9c536 16171 error->transcoder[i].power_domain_on =
f458ebbc 16172 __intel_display_power_is_enabled(dev_priv,
38cc1daf 16173 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 16174 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
16175 continue;
16176
63b66e5b
CW
16177 error->transcoder[i].cpu_transcoder = cpu_transcoder;
16178
16179 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
16180 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
16181 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
16182 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
16183 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
16184 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
16185 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
16186 }
16187
16188 return error;
16189}
16190
edc3d884
MK
16191#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
16192
c4a1d9e4 16193void
edc3d884 16194intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
16195 struct drm_device *dev,
16196 struct intel_display_error_state *error)
16197{
055e393f 16198 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4
CW
16199 int i;
16200
63b66e5b
CW
16201 if (!error)
16202 return;
16203
edc3d884 16204 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
190be112 16205 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
edc3d884 16206 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 16207 error->power_well_driver);
055e393f 16208 for_each_pipe(dev_priv, i) {
edc3d884 16209 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536 16210 err_printf(m, " Power: %s\n",
87ad3212 16211 onoff(error->pipe[i].power_domain_on));
edc3d884 16212 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 16213 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
16214
16215 err_printf(m, "Plane [%d]:\n", i);
16216 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
16217 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
80ca378b 16218 if (INTEL_INFO(dev)->gen <= 3) {
edc3d884
MK
16219 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
16220 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 16221 }
4b71a570 16222 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
edc3d884 16223 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
c4a1d9e4 16224 if (INTEL_INFO(dev)->gen >= 4) {
edc3d884
MK
16225 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
16226 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
16227 }
16228
edc3d884
MK
16229 err_printf(m, "Cursor [%d]:\n", i);
16230 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
16231 err_printf(m, " POS: %08x\n", error->cursor[i].position);
16232 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 16233 }
63b66e5b
CW
16234
16235 for (i = 0; i < error->num_transcoders; i++) {
da205630 16236 err_printf(m, "CPU transcoder: %s\n",
63b66e5b 16237 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536 16238 err_printf(m, " Power: %s\n",
87ad3212 16239 onoff(error->transcoder[i].power_domain_on));
63b66e5b
CW
16240 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
16241 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
16242 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
16243 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
16244 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
16245 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
16246 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
16247 }
c4a1d9e4 16248}
This page took 7.163119 seconds and 5 git commands to generate.