drm/i915: Split CHV and VLV specific crtc_compute_clock() hooks
[linux-block.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
a93e255f
ACO
567static const intel_limit_t *
568intel_g4x_limit(struct intel_crtc_state *crtc_state)
044c7c41 569{
a93e255f 570 struct drm_device *dev = crtc_state->base.crtc->dev;
044c7c41
ML
571 const intel_limit_t *limit;
572
a93e255f 573 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
1974cad0 574 if (intel_is_dual_link_lvds(dev))
e4b36699 575 limit = &intel_limits_g4x_dual_channel_lvds;
044c7c41 576 else
e4b36699 577 limit = &intel_limits_g4x_single_channel_lvds;
a93e255f
ACO
578 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
579 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
e4b36699 580 limit = &intel_limits_g4x_hdmi;
a93e255f 581 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
e4b36699 582 limit = &intel_limits_g4x_sdvo;
044c7c41 583 } else /* The option is for other outputs */
e4b36699 584 limit = &intel_limits_i9xx_sdvo;
044c7c41
ML
585
586 return limit;
587}
588
a93e255f
ACO
589static const intel_limit_t *
590intel_limit(struct intel_crtc_state *crtc_state, int refclk)
79e53945 591{
a93e255f 592 struct drm_device *dev = crtc_state->base.crtc->dev;
79e53945
JB
593 const intel_limit_t *limit;
594
65b3d6a9
ACO
595 if (IS_BROXTON(dev) || IS_CHERRYVIEW(dev) || IS_VALLEYVIEW(dev) ||
596 HAS_PCH_SPLIT(dev))
8f0d5b9b 597 limit = NULL;
65b3d6a9
ACO
598
599 if (IS_G4X(dev)) {
a93e255f 600 limit = intel_g4x_limit(crtc_state);
f2b115e6 601 } else if (IS_PINEVIEW(dev)) {
a93e255f 602 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
f2b115e6 603 limit = &intel_limits_pineview_lvds;
2177832f 604 else
f2b115e6 605 limit = &intel_limits_pineview_sdvo;
a6c45cf0 606 } else if (!IS_GEN2(dev)) {
a93e255f 607 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
a6c45cf0
CW
608 limit = &intel_limits_i9xx_lvds;
609 else
610 limit = &intel_limits_i9xx_sdvo;
79e53945 611 } else {
a93e255f 612 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
e4b36699 613 limit = &intel_limits_i8xx_lvds;
a93e255f 614 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
e4b36699 615 limit = &intel_limits_i8xx_dvo;
5d536e28
DV
616 else
617 limit = &intel_limits_i8xx_dac;
79e53945 618 }
65b3d6a9
ACO
619
620 WARN_ON(limit == NULL);
621
79e53945
JB
622 return limit;
623}
624
dccbea3b
ID
625/*
626 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
627 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
628 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
629 * The helpers' return value is the rate of the clock that is fed to the
630 * display engine's pipe which can be the above fast dot clock rate or a
631 * divided-down version of it.
632 */
f2b115e6 633/* m1 is reserved as 0 in Pineview, n is a ring counter */
dccbea3b 634static int pnv_calc_dpll_params(int refclk, intel_clock_t *clock)
79e53945 635{
2177832f
SL
636 clock->m = clock->m2 + 2;
637 clock->p = clock->p1 * clock->p2;
ed5ca77e 638 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 639 return 0;
fb03ac01
VS
640 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
641 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
642
643 return clock->dot;
2177832f
SL
644}
645
7429e9d4
DV
646static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
647{
648 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
649}
650
dccbea3b 651static int i9xx_calc_dpll_params(int refclk, intel_clock_t *clock)
2177832f 652{
7429e9d4 653 clock->m = i9xx_dpll_compute_m(clock);
79e53945 654 clock->p = clock->p1 * clock->p2;
ed5ca77e 655 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
dccbea3b 656 return 0;
fb03ac01
VS
657 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
658 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
659
660 return clock->dot;
79e53945
JB
661}
662
dccbea3b 663static int vlv_calc_dpll_params(int refclk, intel_clock_t *clock)
589eca67
ID
664{
665 clock->m = clock->m1 * clock->m2;
666 clock->p = clock->p1 * clock->p2;
667 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 668 return 0;
589eca67
ID
669 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
670 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
671
672 return clock->dot / 5;
589eca67
ID
673}
674
dccbea3b 675int chv_calc_dpll_params(int refclk, intel_clock_t *clock)
ef9348c8
CML
676{
677 clock->m = clock->m1 * clock->m2;
678 clock->p = clock->p1 * clock->p2;
679 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 680 return 0;
ef9348c8
CML
681 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
682 clock->n << 22);
683 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
684
685 return clock->dot / 5;
ef9348c8
CML
686}
687
7c04d1d9 688#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
79e53945
JB
689/**
690 * Returns whether the given set of divisors are valid for a given refclk with
691 * the given connectors.
692 */
693
1b894b59
CW
694static bool intel_PLL_is_valid(struct drm_device *dev,
695 const intel_limit_t *limit,
696 const intel_clock_t *clock)
79e53945 697{
f01b7962
VS
698 if (clock->n < limit->n.min || limit->n.max < clock->n)
699 INTELPllInvalid("n out of range\n");
79e53945 700 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
0206e353 701 INTELPllInvalid("p1 out of range\n");
79e53945 702 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
0206e353 703 INTELPllInvalid("m2 out of range\n");
79e53945 704 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
0206e353 705 INTELPllInvalid("m1 out of range\n");
f01b7962 706
666a4537
WB
707 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) &&
708 !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev))
f01b7962
VS
709 if (clock->m1 <= clock->m2)
710 INTELPllInvalid("m1 <= m2\n");
711
666a4537 712 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev)) {
f01b7962
VS
713 if (clock->p < limit->p.min || limit->p.max < clock->p)
714 INTELPllInvalid("p out of range\n");
715 if (clock->m < limit->m.min || limit->m.max < clock->m)
716 INTELPllInvalid("m out of range\n");
717 }
718
79e53945 719 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
0206e353 720 INTELPllInvalid("vco out of range\n");
79e53945
JB
721 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
722 * connector, etc., rather than just a single range.
723 */
724 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
0206e353 725 INTELPllInvalid("dot out of range\n");
79e53945
JB
726
727 return true;
728}
729
3b1429d9
VS
730static int
731i9xx_select_p2_div(const intel_limit_t *limit,
732 const struct intel_crtc_state *crtc_state,
733 int target)
79e53945 734{
3b1429d9 735 struct drm_device *dev = crtc_state->base.crtc->dev;
79e53945 736
a93e255f 737 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
79e53945 738 /*
a210b028
DV
739 * For LVDS just rely on its current settings for dual-channel.
740 * We haven't figured out how to reliably set up different
741 * single/dual channel state, if we even can.
79e53945 742 */
1974cad0 743 if (intel_is_dual_link_lvds(dev))
3b1429d9 744 return limit->p2.p2_fast;
79e53945 745 else
3b1429d9 746 return limit->p2.p2_slow;
79e53945
JB
747 } else {
748 if (target < limit->p2.dot_limit)
3b1429d9 749 return limit->p2.p2_slow;
79e53945 750 else
3b1429d9 751 return limit->p2.p2_fast;
79e53945 752 }
3b1429d9
VS
753}
754
755static bool
756i9xx_find_best_dpll(const intel_limit_t *limit,
757 struct intel_crtc_state *crtc_state,
758 int target, int refclk, intel_clock_t *match_clock,
759 intel_clock_t *best_clock)
760{
761 struct drm_device *dev = crtc_state->base.crtc->dev;
762 intel_clock_t clock;
763 int err = target;
79e53945 764
0206e353 765 memset(best_clock, 0, sizeof(*best_clock));
79e53945 766
3b1429d9
VS
767 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
768
42158660
ZY
769 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
770 clock.m1++) {
771 for (clock.m2 = limit->m2.min;
772 clock.m2 <= limit->m2.max; clock.m2++) {
c0efc387 773 if (clock.m2 >= clock.m1)
42158660
ZY
774 break;
775 for (clock.n = limit->n.min;
776 clock.n <= limit->n.max; clock.n++) {
777 for (clock.p1 = limit->p1.min;
778 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
779 int this_err;
780
dccbea3b 781 i9xx_calc_dpll_params(refclk, &clock);
ac58c3f0
DV
782 if (!intel_PLL_is_valid(dev, limit,
783 &clock))
784 continue;
785 if (match_clock &&
786 clock.p != match_clock->p)
787 continue;
788
789 this_err = abs(clock.dot - target);
790 if (this_err < err) {
791 *best_clock = clock;
792 err = this_err;
793 }
794 }
795 }
796 }
797 }
798
799 return (err != target);
800}
801
802static bool
a93e255f
ACO
803pnv_find_best_dpll(const intel_limit_t *limit,
804 struct intel_crtc_state *crtc_state,
ee9300bb
DV
805 int target, int refclk, intel_clock_t *match_clock,
806 intel_clock_t *best_clock)
79e53945 807{
3b1429d9 808 struct drm_device *dev = crtc_state->base.crtc->dev;
79e53945 809 intel_clock_t clock;
79e53945
JB
810 int err = target;
811
0206e353 812 memset(best_clock, 0, sizeof(*best_clock));
79e53945 813
3b1429d9
VS
814 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
815
42158660
ZY
816 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
817 clock.m1++) {
818 for (clock.m2 = limit->m2.min;
819 clock.m2 <= limit->m2.max; clock.m2++) {
42158660
ZY
820 for (clock.n = limit->n.min;
821 clock.n <= limit->n.max; clock.n++) {
822 for (clock.p1 = limit->p1.min;
823 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
824 int this_err;
825
dccbea3b 826 pnv_calc_dpll_params(refclk, &clock);
1b894b59
CW
827 if (!intel_PLL_is_valid(dev, limit,
828 &clock))
79e53945 829 continue;
cec2f356
SP
830 if (match_clock &&
831 clock.p != match_clock->p)
832 continue;
79e53945
JB
833
834 this_err = abs(clock.dot - target);
835 if (this_err < err) {
836 *best_clock = clock;
837 err = this_err;
838 }
839 }
840 }
841 }
842 }
843
844 return (err != target);
845}
846
997c030c
ACO
847/*
848 * Returns a set of divisors for the desired target clock with the given
849 * refclk, or FALSE. The returned values represent the clock equation:
850 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
851 */
d4906093 852static bool
a93e255f
ACO
853g4x_find_best_dpll(const intel_limit_t *limit,
854 struct intel_crtc_state *crtc_state,
ee9300bb
DV
855 int target, int refclk, intel_clock_t *match_clock,
856 intel_clock_t *best_clock)
d4906093 857{
3b1429d9 858 struct drm_device *dev = crtc_state->base.crtc->dev;
d4906093
ML
859 intel_clock_t clock;
860 int max_n;
3b1429d9 861 bool found = false;
6ba770dc
AJ
862 /* approximately equals target * 0.00585 */
863 int err_most = (target >> 8) + (target >> 9);
d4906093
ML
864
865 memset(best_clock, 0, sizeof(*best_clock));
3b1429d9
VS
866
867 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
868
d4906093 869 max_n = limit->n.max;
f77f13e2 870 /* based on hardware requirement, prefer smaller n to precision */
d4906093 871 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
f77f13e2 872 /* based on hardware requirement, prefere larger m1,m2 */
d4906093
ML
873 for (clock.m1 = limit->m1.max;
874 clock.m1 >= limit->m1.min; clock.m1--) {
875 for (clock.m2 = limit->m2.max;
876 clock.m2 >= limit->m2.min; clock.m2--) {
877 for (clock.p1 = limit->p1.max;
878 clock.p1 >= limit->p1.min; clock.p1--) {
879 int this_err;
880
dccbea3b 881 i9xx_calc_dpll_params(refclk, &clock);
1b894b59
CW
882 if (!intel_PLL_is_valid(dev, limit,
883 &clock))
d4906093 884 continue;
1b894b59
CW
885
886 this_err = abs(clock.dot - target);
d4906093
ML
887 if (this_err < err_most) {
888 *best_clock = clock;
889 err_most = this_err;
890 max_n = clock.n;
891 found = true;
892 }
893 }
894 }
895 }
896 }
2c07245f
ZW
897 return found;
898}
899
d5dd62bd
ID
900/*
901 * Check if the calculated PLL configuration is more optimal compared to the
902 * best configuration and error found so far. Return the calculated error.
903 */
904static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
905 const intel_clock_t *calculated_clock,
906 const intel_clock_t *best_clock,
907 unsigned int best_error_ppm,
908 unsigned int *error_ppm)
909{
9ca3ba01
ID
910 /*
911 * For CHV ignore the error and consider only the P value.
912 * Prefer a bigger P value based on HW requirements.
913 */
914 if (IS_CHERRYVIEW(dev)) {
915 *error_ppm = 0;
916
917 return calculated_clock->p > best_clock->p;
918 }
919
24be4e46
ID
920 if (WARN_ON_ONCE(!target_freq))
921 return false;
922
d5dd62bd
ID
923 *error_ppm = div_u64(1000000ULL *
924 abs(target_freq - calculated_clock->dot),
925 target_freq);
926 /*
927 * Prefer a better P value over a better (smaller) error if the error
928 * is small. Ensure this preference for future configurations too by
929 * setting the error to 0.
930 */
931 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
932 *error_ppm = 0;
933
934 return true;
935 }
936
937 return *error_ppm + 10 < best_error_ppm;
938}
939
65b3d6a9
ACO
940/*
941 * Returns a set of divisors for the desired target clock with the given
942 * refclk, or FALSE. The returned values represent the clock equation:
943 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
944 */
a0c4da24 945static bool
a93e255f
ACO
946vlv_find_best_dpll(const intel_limit_t *limit,
947 struct intel_crtc_state *crtc_state,
ee9300bb
DV
948 int target, int refclk, intel_clock_t *match_clock,
949 intel_clock_t *best_clock)
a0c4da24 950{
a93e255f 951 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 952 struct drm_device *dev = crtc->base.dev;
6b4bf1c4 953 intel_clock_t clock;
69e4f900 954 unsigned int bestppm = 1000000;
27e639bf
VS
955 /* min update 19.2 MHz */
956 int max_n = min(limit->n.max, refclk / 19200);
49e497ef 957 bool found = false;
a0c4da24 958
6b4bf1c4
VS
959 target *= 5; /* fast clock */
960
961 memset(best_clock, 0, sizeof(*best_clock));
a0c4da24
JB
962
963 /* based on hardware requirement, prefer smaller n to precision */
27e639bf 964 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
811bbf05 965 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
889059d8 966 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
c1a9ae43 967 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
6b4bf1c4 968 clock.p = clock.p1 * clock.p2;
a0c4da24 969 /* based on hardware requirement, prefer bigger m1,m2 values */
6b4bf1c4 970 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
d5dd62bd 971 unsigned int ppm;
69e4f900 972
6b4bf1c4
VS
973 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
974 refclk * clock.m1);
975
dccbea3b 976 vlv_calc_dpll_params(refclk, &clock);
43b0ac53 977
f01b7962
VS
978 if (!intel_PLL_is_valid(dev, limit,
979 &clock))
43b0ac53
VS
980 continue;
981
d5dd62bd
ID
982 if (!vlv_PLL_is_optimal(dev, target,
983 &clock,
984 best_clock,
985 bestppm, &ppm))
986 continue;
6b4bf1c4 987
d5dd62bd
ID
988 *best_clock = clock;
989 bestppm = ppm;
990 found = true;
a0c4da24
JB
991 }
992 }
993 }
994 }
a0c4da24 995
49e497ef 996 return found;
a0c4da24 997}
a4fc5ed6 998
65b3d6a9
ACO
999/*
1000 * Returns a set of divisors for the desired target clock with the given
1001 * refclk, or FALSE. The returned values represent the clock equation:
1002 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
1003 */
ef9348c8 1004static bool
a93e255f
ACO
1005chv_find_best_dpll(const intel_limit_t *limit,
1006 struct intel_crtc_state *crtc_state,
ef9348c8
CML
1007 int target, int refclk, intel_clock_t *match_clock,
1008 intel_clock_t *best_clock)
1009{
a93e255f 1010 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 1011 struct drm_device *dev = crtc->base.dev;
9ca3ba01 1012 unsigned int best_error_ppm;
ef9348c8
CML
1013 intel_clock_t clock;
1014 uint64_t m2;
1015 int found = false;
1016
1017 memset(best_clock, 0, sizeof(*best_clock));
9ca3ba01 1018 best_error_ppm = 1000000;
ef9348c8
CML
1019
1020 /*
1021 * Based on hardware doc, the n always set to 1, and m1 always
1022 * set to 2. If requires to support 200Mhz refclk, we need to
1023 * revisit this because n may not 1 anymore.
1024 */
1025 clock.n = 1, clock.m1 = 2;
1026 target *= 5; /* fast clock */
1027
1028 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
1029 for (clock.p2 = limit->p2.p2_fast;
1030 clock.p2 >= limit->p2.p2_slow;
1031 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
9ca3ba01 1032 unsigned int error_ppm;
ef9348c8
CML
1033
1034 clock.p = clock.p1 * clock.p2;
1035
1036 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
1037 clock.n) << 22, refclk * clock.m1);
1038
1039 if (m2 > INT_MAX/clock.m1)
1040 continue;
1041
1042 clock.m2 = m2;
1043
dccbea3b 1044 chv_calc_dpll_params(refclk, &clock);
ef9348c8
CML
1045
1046 if (!intel_PLL_is_valid(dev, limit, &clock))
1047 continue;
1048
9ca3ba01
ID
1049 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
1050 best_error_ppm, &error_ppm))
1051 continue;
1052
1053 *best_clock = clock;
1054 best_error_ppm = error_ppm;
1055 found = true;
ef9348c8
CML
1056 }
1057 }
1058
1059 return found;
1060}
1061
5ab7b0b7
ID
1062bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
1063 intel_clock_t *best_clock)
1064{
65b3d6a9
ACO
1065 int refclk = 100000;
1066 const intel_limit_t *limit = &intel_limits_bxt;
5ab7b0b7 1067
65b3d6a9 1068 return chv_find_best_dpll(limit, crtc_state,
5ab7b0b7
ID
1069 target_clock, refclk, NULL, best_clock);
1070}
1071
20ddf665
VS
1072bool intel_crtc_active(struct drm_crtc *crtc)
1073{
1074 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1075
1076 /* Be paranoid as we can arrive here with only partial
1077 * state retrieved from the hardware during setup.
1078 *
241bfc38 1079 * We can ditch the adjusted_mode.crtc_clock check as soon
20ddf665
VS
1080 * as Haswell has gained clock readout/fastboot support.
1081 *
66e514c1 1082 * We can ditch the crtc->primary->fb check as soon as we can
20ddf665 1083 * properly reconstruct framebuffers.
c3d1f436
MR
1084 *
1085 * FIXME: The intel_crtc->active here should be switched to
1086 * crtc->state->active once we have proper CRTC states wired up
1087 * for atomic.
20ddf665 1088 */
c3d1f436 1089 return intel_crtc->active && crtc->primary->state->fb &&
6e3c9717 1090 intel_crtc->config->base.adjusted_mode.crtc_clock;
20ddf665
VS
1091}
1092
a5c961d1
PZ
1093enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1094 enum pipe pipe)
1095{
1096 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1097 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1098
6e3c9717 1099 return intel_crtc->config->cpu_transcoder;
a5c961d1
PZ
1100}
1101
fbf49ea2
VS
1102static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1103{
1104 struct drm_i915_private *dev_priv = dev->dev_private;
f0f59a00 1105 i915_reg_t reg = PIPEDSL(pipe);
fbf49ea2
VS
1106 u32 line1, line2;
1107 u32 line_mask;
1108
1109 if (IS_GEN2(dev))
1110 line_mask = DSL_LINEMASK_GEN2;
1111 else
1112 line_mask = DSL_LINEMASK_GEN3;
1113
1114 line1 = I915_READ(reg) & line_mask;
6adfb1ef 1115 msleep(5);
fbf49ea2
VS
1116 line2 = I915_READ(reg) & line_mask;
1117
1118 return line1 == line2;
1119}
1120
ab7ad7f6
KP
1121/*
1122 * intel_wait_for_pipe_off - wait for pipe to turn off
575f7ab7 1123 * @crtc: crtc whose pipe to wait for
9d0498a2
JB
1124 *
1125 * After disabling a pipe, we can't wait for vblank in the usual way,
1126 * spinning on the vblank interrupt status bit, since we won't actually
1127 * see an interrupt when the pipe is disabled.
1128 *
ab7ad7f6
KP
1129 * On Gen4 and above:
1130 * wait for the pipe register state bit to turn off
1131 *
1132 * Otherwise:
1133 * wait for the display line value to settle (it usually
1134 * ends up stopping at the start of the next frame).
58e10eb9 1135 *
9d0498a2 1136 */
575f7ab7 1137static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
9d0498a2 1138{
575f7ab7 1139 struct drm_device *dev = crtc->base.dev;
9d0498a2 1140 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 1141 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 1142 enum pipe pipe = crtc->pipe;
ab7ad7f6
KP
1143
1144 if (INTEL_INFO(dev)->gen >= 4) {
f0f59a00 1145 i915_reg_t reg = PIPECONF(cpu_transcoder);
ab7ad7f6
KP
1146
1147 /* Wait for the Pipe State to go off */
58e10eb9
CW
1148 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1149 100))
284637d9 1150 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1151 } else {
ab7ad7f6 1152 /* Wait for the display line to settle */
fbf49ea2 1153 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
284637d9 1154 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1155 }
79e53945
JB
1156}
1157
b24e7179 1158/* Only for pre-ILK configs */
55607e8a
DV
1159void assert_pll(struct drm_i915_private *dev_priv,
1160 enum pipe pipe, bool state)
b24e7179 1161{
b24e7179
JB
1162 u32 val;
1163 bool cur_state;
1164
649636ef 1165 val = I915_READ(DPLL(pipe));
b24e7179 1166 cur_state = !!(val & DPLL_VCO_ENABLE);
e2c719b7 1167 I915_STATE_WARN(cur_state != state,
b24e7179 1168 "PLL state assertion failure (expected %s, current %s)\n",
87ad3212 1169 onoff(state), onoff(cur_state));
b24e7179 1170}
b24e7179 1171
23538ef1 1172/* XXX: the dsi pll is shared between MIPI DSI ports */
8563b1e8 1173void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
23538ef1
JN
1174{
1175 u32 val;
1176 bool cur_state;
1177
a580516d 1178 mutex_lock(&dev_priv->sb_lock);
23538ef1 1179 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
a580516d 1180 mutex_unlock(&dev_priv->sb_lock);
23538ef1
JN
1181
1182 cur_state = val & DSI_PLL_VCO_EN;
e2c719b7 1183 I915_STATE_WARN(cur_state != state,
23538ef1 1184 "DSI PLL state assertion failure (expected %s, current %s)\n",
87ad3212 1185 onoff(state), onoff(cur_state));
23538ef1 1186}
23538ef1 1187
040484af
JB
1188static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1189 enum pipe pipe, bool state)
1190{
040484af 1191 bool cur_state;
ad80a810
PZ
1192 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1193 pipe);
040484af 1194
affa9354
PZ
1195 if (HAS_DDI(dev_priv->dev)) {
1196 /* DDI does not have a specific FDI_TX register */
649636ef 1197 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
ad80a810 1198 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
bf507ef7 1199 } else {
649636ef 1200 u32 val = I915_READ(FDI_TX_CTL(pipe));
bf507ef7
ED
1201 cur_state = !!(val & FDI_TX_ENABLE);
1202 }
e2c719b7 1203 I915_STATE_WARN(cur_state != state,
040484af 1204 "FDI TX state assertion failure (expected %s, current %s)\n",
87ad3212 1205 onoff(state), onoff(cur_state));
040484af
JB
1206}
1207#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1208#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1209
1210static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1211 enum pipe pipe, bool state)
1212{
040484af
JB
1213 u32 val;
1214 bool cur_state;
1215
649636ef 1216 val = I915_READ(FDI_RX_CTL(pipe));
d63fa0dc 1217 cur_state = !!(val & FDI_RX_ENABLE);
e2c719b7 1218 I915_STATE_WARN(cur_state != state,
040484af 1219 "FDI RX state assertion failure (expected %s, current %s)\n",
87ad3212 1220 onoff(state), onoff(cur_state));
040484af
JB
1221}
1222#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1223#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1224
1225static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1226 enum pipe pipe)
1227{
040484af
JB
1228 u32 val;
1229
1230 /* ILK FDI PLL is always enabled */
3d13ef2e 1231 if (INTEL_INFO(dev_priv->dev)->gen == 5)
040484af
JB
1232 return;
1233
bf507ef7 1234 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
affa9354 1235 if (HAS_DDI(dev_priv->dev))
bf507ef7
ED
1236 return;
1237
649636ef 1238 val = I915_READ(FDI_TX_CTL(pipe));
e2c719b7 1239 I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
040484af
JB
1240}
1241
55607e8a
DV
1242void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1243 enum pipe pipe, bool state)
040484af 1244{
040484af 1245 u32 val;
55607e8a 1246 bool cur_state;
040484af 1247
649636ef 1248 val = I915_READ(FDI_RX_CTL(pipe));
55607e8a 1249 cur_state = !!(val & FDI_RX_PLL_ENABLE);
e2c719b7 1250 I915_STATE_WARN(cur_state != state,
55607e8a 1251 "FDI RX PLL assertion failure (expected %s, current %s)\n",
87ad3212 1252 onoff(state), onoff(cur_state));
040484af
JB
1253}
1254
b680c37a
DV
1255void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1256 enum pipe pipe)
ea0760cf 1257{
bedd4dba 1258 struct drm_device *dev = dev_priv->dev;
f0f59a00 1259 i915_reg_t pp_reg;
ea0760cf
JB
1260 u32 val;
1261 enum pipe panel_pipe = PIPE_A;
0de3b485 1262 bool locked = true;
ea0760cf 1263
bedd4dba
JN
1264 if (WARN_ON(HAS_DDI(dev)))
1265 return;
1266
1267 if (HAS_PCH_SPLIT(dev)) {
1268 u32 port_sel;
1269
ea0760cf 1270 pp_reg = PCH_PP_CONTROL;
bedd4dba
JN
1271 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1272
1273 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1274 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1275 panel_pipe = PIPE_B;
1276 /* XXX: else fix for eDP */
666a4537 1277 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
bedd4dba
JN
1278 /* presumably write lock depends on pipe, not port select */
1279 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1280 panel_pipe = pipe;
ea0760cf
JB
1281 } else {
1282 pp_reg = PP_CONTROL;
bedd4dba
JN
1283 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1284 panel_pipe = PIPE_B;
ea0760cf
JB
1285 }
1286
1287 val = I915_READ(pp_reg);
1288 if (!(val & PANEL_POWER_ON) ||
ec49ba2d 1289 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
ea0760cf
JB
1290 locked = false;
1291
e2c719b7 1292 I915_STATE_WARN(panel_pipe == pipe && locked,
ea0760cf 1293 "panel assertion failure, pipe %c regs locked\n",
9db4a9c7 1294 pipe_name(pipe));
ea0760cf
JB
1295}
1296
93ce0ba6
JN
1297static void assert_cursor(struct drm_i915_private *dev_priv,
1298 enum pipe pipe, bool state)
1299{
1300 struct drm_device *dev = dev_priv->dev;
1301 bool cur_state;
1302
d9d82081 1303 if (IS_845G(dev) || IS_I865G(dev))
0b87c24e 1304 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
d9d82081 1305 else
5efb3e28 1306 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
93ce0ba6 1307
e2c719b7 1308 I915_STATE_WARN(cur_state != state,
93ce0ba6 1309 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
87ad3212 1310 pipe_name(pipe), onoff(state), onoff(cur_state));
93ce0ba6
JN
1311}
1312#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1313#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1314
b840d907
JB
1315void assert_pipe(struct drm_i915_private *dev_priv,
1316 enum pipe pipe, bool state)
b24e7179 1317{
63d7bbe9 1318 bool cur_state;
702e7a56
PZ
1319 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1320 pipe);
4feed0eb 1321 enum intel_display_power_domain power_domain;
b24e7179 1322
b6b5d049
VS
1323 /* if we need the pipe quirk it must be always on */
1324 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1325 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
8e636784
DV
1326 state = true;
1327
4feed0eb
ID
1328 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1329 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
649636ef 1330 u32 val = I915_READ(PIPECONF(cpu_transcoder));
69310161 1331 cur_state = !!(val & PIPECONF_ENABLE);
4feed0eb
ID
1332
1333 intel_display_power_put(dev_priv, power_domain);
1334 } else {
1335 cur_state = false;
69310161
PZ
1336 }
1337
e2c719b7 1338 I915_STATE_WARN(cur_state != state,
63d7bbe9 1339 "pipe %c assertion failure (expected %s, current %s)\n",
87ad3212 1340 pipe_name(pipe), onoff(state), onoff(cur_state));
b24e7179
JB
1341}
1342
931872fc
CW
1343static void assert_plane(struct drm_i915_private *dev_priv,
1344 enum plane plane, bool state)
b24e7179 1345{
b24e7179 1346 u32 val;
931872fc 1347 bool cur_state;
b24e7179 1348
649636ef 1349 val = I915_READ(DSPCNTR(plane));
931872fc 1350 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
e2c719b7 1351 I915_STATE_WARN(cur_state != state,
931872fc 1352 "plane %c assertion failure (expected %s, current %s)\n",
87ad3212 1353 plane_name(plane), onoff(state), onoff(cur_state));
b24e7179
JB
1354}
1355
931872fc
CW
1356#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1357#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1358
b24e7179
JB
1359static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1360 enum pipe pipe)
1361{
653e1026 1362 struct drm_device *dev = dev_priv->dev;
649636ef 1363 int i;
b24e7179 1364
653e1026
VS
1365 /* Primary planes are fixed to pipes on gen4+ */
1366 if (INTEL_INFO(dev)->gen >= 4) {
649636ef 1367 u32 val = I915_READ(DSPCNTR(pipe));
e2c719b7 1368 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
28c05794
AJ
1369 "plane %c assertion failure, should be disabled but not\n",
1370 plane_name(pipe));
19ec1358 1371 return;
28c05794 1372 }
19ec1358 1373
b24e7179 1374 /* Need to check both planes against the pipe */
055e393f 1375 for_each_pipe(dev_priv, i) {
649636ef
VS
1376 u32 val = I915_READ(DSPCNTR(i));
1377 enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
b24e7179 1378 DISPPLANE_SEL_PIPE_SHIFT;
e2c719b7 1379 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
9db4a9c7
JB
1380 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1381 plane_name(i), pipe_name(pipe));
b24e7179
JB
1382 }
1383}
1384
19332d7a
JB
1385static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1386 enum pipe pipe)
1387{
20674eef 1388 struct drm_device *dev = dev_priv->dev;
649636ef 1389 int sprite;
19332d7a 1390
7feb8b88 1391 if (INTEL_INFO(dev)->gen >= 9) {
3bdcfc0c 1392 for_each_sprite(dev_priv, pipe, sprite) {
649636ef 1393 u32 val = I915_READ(PLANE_CTL(pipe, sprite));
e2c719b7 1394 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
7feb8b88
DL
1395 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1396 sprite, pipe_name(pipe));
1397 }
666a4537 1398 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
3bdcfc0c 1399 for_each_sprite(dev_priv, pipe, sprite) {
649636ef 1400 u32 val = I915_READ(SPCNTR(pipe, sprite));
e2c719b7 1401 I915_STATE_WARN(val & SP_ENABLE,
20674eef 1402 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1fe47785 1403 sprite_name(pipe, sprite), pipe_name(pipe));
20674eef
VS
1404 }
1405 } else if (INTEL_INFO(dev)->gen >= 7) {
649636ef 1406 u32 val = I915_READ(SPRCTL(pipe));
e2c719b7 1407 I915_STATE_WARN(val & SPRITE_ENABLE,
06da8da2 1408 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef
VS
1409 plane_name(pipe), pipe_name(pipe));
1410 } else if (INTEL_INFO(dev)->gen >= 5) {
649636ef 1411 u32 val = I915_READ(DVSCNTR(pipe));
e2c719b7 1412 I915_STATE_WARN(val & DVS_ENABLE,
06da8da2 1413 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef 1414 plane_name(pipe), pipe_name(pipe));
19332d7a
JB
1415 }
1416}
1417
08c71e5e
VS
1418static void assert_vblank_disabled(struct drm_crtc *crtc)
1419{
e2c719b7 1420 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
08c71e5e
VS
1421 drm_crtc_vblank_put(crtc);
1422}
1423
7abd4b35
ACO
1424void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1425 enum pipe pipe)
92f2584a 1426{
92f2584a
JB
1427 u32 val;
1428 bool enabled;
1429
649636ef 1430 val = I915_READ(PCH_TRANSCONF(pipe));
92f2584a 1431 enabled = !!(val & TRANS_ENABLE);
e2c719b7 1432 I915_STATE_WARN(enabled,
9db4a9c7
JB
1433 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1434 pipe_name(pipe));
92f2584a
JB
1435}
1436
4e634389
KP
1437static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1438 enum pipe pipe, u32 port_sel, u32 val)
f0575e92
KP
1439{
1440 if ((val & DP_PORT_EN) == 0)
1441 return false;
1442
1443 if (HAS_PCH_CPT(dev_priv->dev)) {
f0f59a00 1444 u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
f0575e92
KP
1445 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1446 return false;
44f37d1f
CML
1447 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1448 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1449 return false;
f0575e92
KP
1450 } else {
1451 if ((val & DP_PIPE_MASK) != (pipe << 30))
1452 return false;
1453 }
1454 return true;
1455}
1456
1519b995
KP
1457static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1458 enum pipe pipe, u32 val)
1459{
dc0fa718 1460 if ((val & SDVO_ENABLE) == 0)
1519b995
KP
1461 return false;
1462
1463 if (HAS_PCH_CPT(dev_priv->dev)) {
dc0fa718 1464 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1519b995 1465 return false;
44f37d1f
CML
1466 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1467 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1468 return false;
1519b995 1469 } else {
dc0fa718 1470 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1519b995
KP
1471 return false;
1472 }
1473 return true;
1474}
1475
1476static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1477 enum pipe pipe, u32 val)
1478{
1479 if ((val & LVDS_PORT_EN) == 0)
1480 return false;
1481
1482 if (HAS_PCH_CPT(dev_priv->dev)) {
1483 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1484 return false;
1485 } else {
1486 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1487 return false;
1488 }
1489 return true;
1490}
1491
1492static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1493 enum pipe pipe, u32 val)
1494{
1495 if ((val & ADPA_DAC_ENABLE) == 0)
1496 return false;
1497 if (HAS_PCH_CPT(dev_priv->dev)) {
1498 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1499 return false;
1500 } else {
1501 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1502 return false;
1503 }
1504 return true;
1505}
1506
291906f1 1507static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
f0f59a00
VS
1508 enum pipe pipe, i915_reg_t reg,
1509 u32 port_sel)
291906f1 1510{
47a05eca 1511 u32 val = I915_READ(reg);
e2c719b7 1512 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
291906f1 1513 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
f0f59a00 1514 i915_mmio_reg_offset(reg), pipe_name(pipe));
de9a35ab 1515
e2c719b7 1516 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
75c5da27 1517 && (val & DP_PIPEB_SELECT),
de9a35ab 1518 "IBX PCH dp port still using transcoder B\n");
291906f1
JB
1519}
1520
1521static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
f0f59a00 1522 enum pipe pipe, i915_reg_t reg)
291906f1 1523{
47a05eca 1524 u32 val = I915_READ(reg);
e2c719b7 1525 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
23c99e77 1526 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
f0f59a00 1527 i915_mmio_reg_offset(reg), pipe_name(pipe));
de9a35ab 1528
e2c719b7 1529 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
75c5da27 1530 && (val & SDVO_PIPE_B_SELECT),
de9a35ab 1531 "IBX PCH hdmi port still using transcoder B\n");
291906f1
JB
1532}
1533
1534static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1535 enum pipe pipe)
1536{
291906f1 1537 u32 val;
291906f1 1538
f0575e92
KP
1539 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1540 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1541 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
291906f1 1542
649636ef 1543 val = I915_READ(PCH_ADPA);
e2c719b7 1544 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
291906f1 1545 "PCH VGA enabled on transcoder %c, should be disabled\n",
9db4a9c7 1546 pipe_name(pipe));
291906f1 1547
649636ef 1548 val = I915_READ(PCH_LVDS);
e2c719b7 1549 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
291906f1 1550 "PCH LVDS enabled on transcoder %c, should be disabled\n",
9db4a9c7 1551 pipe_name(pipe));
291906f1 1552
e2debe91
PZ
1553 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1554 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1555 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
291906f1
JB
1556}
1557
d288f65f 1558static void vlv_enable_pll(struct intel_crtc *crtc,
5cec258b 1559 const struct intel_crtc_state *pipe_config)
87442f73 1560{
426115cf
DV
1561 struct drm_device *dev = crtc->base.dev;
1562 struct drm_i915_private *dev_priv = dev->dev_private;
f0f59a00 1563 i915_reg_t reg = DPLL(crtc->pipe);
d288f65f 1564 u32 dpll = pipe_config->dpll_hw_state.dpll;
87442f73 1565
426115cf 1566 assert_pipe_disabled(dev_priv, crtc->pipe);
87442f73 1567
87442f73 1568 /* PLL is protected by panel, make sure we can write it */
6a9e7363 1569 if (IS_MOBILE(dev_priv->dev))
426115cf 1570 assert_panel_unlocked(dev_priv, crtc->pipe);
87442f73 1571
426115cf
DV
1572 I915_WRITE(reg, dpll);
1573 POSTING_READ(reg);
1574 udelay(150);
1575
1576 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1577 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1578
d288f65f 1579 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
426115cf 1580 POSTING_READ(DPLL_MD(crtc->pipe));
87442f73
DV
1581
1582 /* We do this three times for luck */
426115cf 1583 I915_WRITE(reg, dpll);
87442f73
DV
1584 POSTING_READ(reg);
1585 udelay(150); /* wait for warmup */
426115cf 1586 I915_WRITE(reg, dpll);
87442f73
DV
1587 POSTING_READ(reg);
1588 udelay(150); /* wait for warmup */
426115cf 1589 I915_WRITE(reg, dpll);
87442f73
DV
1590 POSTING_READ(reg);
1591 udelay(150); /* wait for warmup */
1592}
1593
d288f65f 1594static void chv_enable_pll(struct intel_crtc *crtc,
5cec258b 1595 const struct intel_crtc_state *pipe_config)
9d556c99
CML
1596{
1597 struct drm_device *dev = crtc->base.dev;
1598 struct drm_i915_private *dev_priv = dev->dev_private;
1599 int pipe = crtc->pipe;
1600 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9d556c99
CML
1601 u32 tmp;
1602
1603 assert_pipe_disabled(dev_priv, crtc->pipe);
1604
a580516d 1605 mutex_lock(&dev_priv->sb_lock);
9d556c99
CML
1606
1607 /* Enable back the 10bit clock to display controller */
1608 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1609 tmp |= DPIO_DCLKP_EN;
1610 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1611
54433e91
VS
1612 mutex_unlock(&dev_priv->sb_lock);
1613
9d556c99
CML
1614 /*
1615 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1616 */
1617 udelay(1);
1618
1619 /* Enable PLL */
d288f65f 1620 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
9d556c99
CML
1621
1622 /* Check PLL is locked */
a11b0703 1623 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
9d556c99
CML
1624 DRM_ERROR("PLL %d failed to lock\n", pipe);
1625
a11b0703 1626 /* not sure when this should be written */
d288f65f 1627 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
a11b0703 1628 POSTING_READ(DPLL_MD(pipe));
9d556c99
CML
1629}
1630
1c4e0274
VS
1631static int intel_num_dvo_pipes(struct drm_device *dev)
1632{
1633 struct intel_crtc *crtc;
1634 int count = 0;
1635
1636 for_each_intel_crtc(dev, crtc)
3538b9df 1637 count += crtc->base.state->active &&
409ee761 1638 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
1c4e0274
VS
1639
1640 return count;
1641}
1642
66e3d5c0 1643static void i9xx_enable_pll(struct intel_crtc *crtc)
63d7bbe9 1644{
66e3d5c0
DV
1645 struct drm_device *dev = crtc->base.dev;
1646 struct drm_i915_private *dev_priv = dev->dev_private;
f0f59a00 1647 i915_reg_t reg = DPLL(crtc->pipe);
6e3c9717 1648 u32 dpll = crtc->config->dpll_hw_state.dpll;
63d7bbe9 1649
66e3d5c0 1650 assert_pipe_disabled(dev_priv, crtc->pipe);
58c6eaa2 1651
63d7bbe9 1652 /* No really, not for ILK+ */
3d13ef2e 1653 BUG_ON(INTEL_INFO(dev)->gen >= 5);
63d7bbe9
JB
1654
1655 /* PLL is protected by panel, make sure we can write it */
66e3d5c0
DV
1656 if (IS_MOBILE(dev) && !IS_I830(dev))
1657 assert_panel_unlocked(dev_priv, crtc->pipe);
63d7bbe9 1658
1c4e0274
VS
1659 /* Enable DVO 2x clock on both PLLs if necessary */
1660 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1661 /*
1662 * It appears to be important that we don't enable this
1663 * for the current pipe before otherwise configuring the
1664 * PLL. No idea how this should be handled if multiple
1665 * DVO outputs are enabled simultaneosly.
1666 */
1667 dpll |= DPLL_DVO_2X_MODE;
1668 I915_WRITE(DPLL(!crtc->pipe),
1669 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1670 }
66e3d5c0 1671
c2b63374
VS
1672 /*
1673 * Apparently we need to have VGA mode enabled prior to changing
1674 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1675 * dividers, even though the register value does change.
1676 */
1677 I915_WRITE(reg, 0);
1678
8e7a65aa
VS
1679 I915_WRITE(reg, dpll);
1680
66e3d5c0
DV
1681 /* Wait for the clocks to stabilize. */
1682 POSTING_READ(reg);
1683 udelay(150);
1684
1685 if (INTEL_INFO(dev)->gen >= 4) {
1686 I915_WRITE(DPLL_MD(crtc->pipe),
6e3c9717 1687 crtc->config->dpll_hw_state.dpll_md);
66e3d5c0
DV
1688 } else {
1689 /* The pixel multiplier can only be updated once the
1690 * DPLL is enabled and the clocks are stable.
1691 *
1692 * So write it again.
1693 */
1694 I915_WRITE(reg, dpll);
1695 }
63d7bbe9
JB
1696
1697 /* We do this three times for luck */
66e3d5c0 1698 I915_WRITE(reg, dpll);
63d7bbe9
JB
1699 POSTING_READ(reg);
1700 udelay(150); /* wait for warmup */
66e3d5c0 1701 I915_WRITE(reg, dpll);
63d7bbe9
JB
1702 POSTING_READ(reg);
1703 udelay(150); /* wait for warmup */
66e3d5c0 1704 I915_WRITE(reg, dpll);
63d7bbe9
JB
1705 POSTING_READ(reg);
1706 udelay(150); /* wait for warmup */
1707}
1708
1709/**
50b44a44 1710 * i9xx_disable_pll - disable a PLL
63d7bbe9
JB
1711 * @dev_priv: i915 private structure
1712 * @pipe: pipe PLL to disable
1713 *
1714 * Disable the PLL for @pipe, making sure the pipe is off first.
1715 *
1716 * Note! This is for pre-ILK only.
1717 */
1c4e0274 1718static void i9xx_disable_pll(struct intel_crtc *crtc)
63d7bbe9 1719{
1c4e0274
VS
1720 struct drm_device *dev = crtc->base.dev;
1721 struct drm_i915_private *dev_priv = dev->dev_private;
1722 enum pipe pipe = crtc->pipe;
1723
1724 /* Disable DVO 2x clock on both PLLs if necessary */
1725 if (IS_I830(dev) &&
409ee761 1726 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
3538b9df 1727 !intel_num_dvo_pipes(dev)) {
1c4e0274
VS
1728 I915_WRITE(DPLL(PIPE_B),
1729 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1730 I915_WRITE(DPLL(PIPE_A),
1731 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1732 }
1733
b6b5d049
VS
1734 /* Don't disable pipe or pipe PLLs if needed */
1735 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1736 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
63d7bbe9
JB
1737 return;
1738
1739 /* Make sure the pipe isn't still relying on us */
1740 assert_pipe_disabled(dev_priv, pipe);
1741
b8afb911 1742 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
50b44a44 1743 POSTING_READ(DPLL(pipe));
63d7bbe9
JB
1744}
1745
f6071166
JB
1746static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1747{
b8afb911 1748 u32 val;
f6071166
JB
1749
1750 /* Make sure the pipe isn't still relying on us */
1751 assert_pipe_disabled(dev_priv, pipe);
1752
e5cbfbfb
ID
1753 /*
1754 * Leave integrated clock source and reference clock enabled for pipe B.
1755 * The latter is needed for VGA hotplug / manual detection.
1756 */
b8afb911 1757 val = DPLL_VGA_MODE_DIS;
f6071166 1758 if (pipe == PIPE_B)
60bfe44f 1759 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REF_CLK_ENABLE_VLV;
f6071166
JB
1760 I915_WRITE(DPLL(pipe), val);
1761 POSTING_READ(DPLL(pipe));
076ed3b2
CML
1762
1763}
1764
1765static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1766{
d752048d 1767 enum dpio_channel port = vlv_pipe_to_channel(pipe);
076ed3b2
CML
1768 u32 val;
1769
a11b0703
VS
1770 /* Make sure the pipe isn't still relying on us */
1771 assert_pipe_disabled(dev_priv, pipe);
076ed3b2 1772
a11b0703 1773 /* Set PLL en = 0 */
60bfe44f
VS
1774 val = DPLL_SSC_REF_CLK_CHV |
1775 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
a11b0703
VS
1776 if (pipe != PIPE_A)
1777 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1778 I915_WRITE(DPLL(pipe), val);
1779 POSTING_READ(DPLL(pipe));
d752048d 1780
a580516d 1781 mutex_lock(&dev_priv->sb_lock);
d752048d
VS
1782
1783 /* Disable 10bit clock to display controller */
1784 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1785 val &= ~DPIO_DCLKP_EN;
1786 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1787
a580516d 1788 mutex_unlock(&dev_priv->sb_lock);
f6071166
JB
1789}
1790
e4607fcf 1791void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
9b6de0a1
VS
1792 struct intel_digital_port *dport,
1793 unsigned int expected_mask)
89b667f8
JB
1794{
1795 u32 port_mask;
f0f59a00 1796 i915_reg_t dpll_reg;
89b667f8 1797
e4607fcf
CML
1798 switch (dport->port) {
1799 case PORT_B:
89b667f8 1800 port_mask = DPLL_PORTB_READY_MASK;
00fc31b7 1801 dpll_reg = DPLL(0);
e4607fcf
CML
1802 break;
1803 case PORT_C:
89b667f8 1804 port_mask = DPLL_PORTC_READY_MASK;
00fc31b7 1805 dpll_reg = DPLL(0);
9b6de0a1 1806 expected_mask <<= 4;
00fc31b7
CML
1807 break;
1808 case PORT_D:
1809 port_mask = DPLL_PORTD_READY_MASK;
1810 dpll_reg = DPIO_PHY_STATUS;
e4607fcf
CML
1811 break;
1812 default:
1813 BUG();
1814 }
89b667f8 1815
9b6de0a1
VS
1816 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1817 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1818 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
89b667f8
JB
1819}
1820
b8a4f404
PZ
1821static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1822 enum pipe pipe)
040484af 1823{
23670b32 1824 struct drm_device *dev = dev_priv->dev;
7c26e5c6 1825 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
e2b78267 1826 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
f0f59a00
VS
1827 i915_reg_t reg;
1828 uint32_t val, pipeconf_val;
040484af
JB
1829
1830 /* PCH only available on ILK+ */
55522f37 1831 BUG_ON(!HAS_PCH_SPLIT(dev));
040484af
JB
1832
1833 /* Make sure PCH DPLL is enabled */
8106ddbd 1834 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
040484af
JB
1835
1836 /* FDI must be feeding us bits for PCH ports */
1837 assert_fdi_tx_enabled(dev_priv, pipe);
1838 assert_fdi_rx_enabled(dev_priv, pipe);
1839
23670b32
DV
1840 if (HAS_PCH_CPT(dev)) {
1841 /* Workaround: Set the timing override bit before enabling the
1842 * pch transcoder. */
1843 reg = TRANS_CHICKEN2(pipe);
1844 val = I915_READ(reg);
1845 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1846 I915_WRITE(reg, val);
59c859d6 1847 }
23670b32 1848
ab9412ba 1849 reg = PCH_TRANSCONF(pipe);
040484af 1850 val = I915_READ(reg);
5f7f726d 1851 pipeconf_val = I915_READ(PIPECONF(pipe));
e9bcff5c
JB
1852
1853 if (HAS_PCH_IBX(dev_priv->dev)) {
1854 /*
c5de7c6f
VS
1855 * Make the BPC in transcoder be consistent with
1856 * that in pipeconf reg. For HDMI we must use 8bpc
1857 * here for both 8bpc and 12bpc.
e9bcff5c 1858 */
dfd07d72 1859 val &= ~PIPECONF_BPC_MASK;
c5de7c6f
VS
1860 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
1861 val |= PIPECONF_8BPC;
1862 else
1863 val |= pipeconf_val & PIPECONF_BPC_MASK;
e9bcff5c 1864 }
5f7f726d
PZ
1865
1866 val &= ~TRANS_INTERLACE_MASK;
1867 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
7c26e5c6 1868 if (HAS_PCH_IBX(dev_priv->dev) &&
409ee761 1869 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
7c26e5c6
PZ
1870 val |= TRANS_LEGACY_INTERLACED_ILK;
1871 else
1872 val |= TRANS_INTERLACED;
5f7f726d
PZ
1873 else
1874 val |= TRANS_PROGRESSIVE;
1875
040484af
JB
1876 I915_WRITE(reg, val | TRANS_ENABLE);
1877 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
4bb6f1f3 1878 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
040484af
JB
1879}
1880
8fb033d7 1881static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
937bb610 1882 enum transcoder cpu_transcoder)
040484af 1883{
8fb033d7 1884 u32 val, pipeconf_val;
8fb033d7
PZ
1885
1886 /* PCH only available on ILK+ */
55522f37 1887 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
8fb033d7 1888
8fb033d7 1889 /* FDI must be feeding us bits for PCH ports */
1a240d4d 1890 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
937bb610 1891 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
8fb033d7 1892
223a6fdf 1893 /* Workaround: set timing override bit. */
36c0d0cf 1894 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
23670b32 1895 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
36c0d0cf 1896 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
223a6fdf 1897
25f3ef11 1898 val = TRANS_ENABLE;
937bb610 1899 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
8fb033d7 1900
9a76b1c6
PZ
1901 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1902 PIPECONF_INTERLACED_ILK)
a35f2679 1903 val |= TRANS_INTERLACED;
8fb033d7
PZ
1904 else
1905 val |= TRANS_PROGRESSIVE;
1906
ab9412ba
DV
1907 I915_WRITE(LPT_TRANSCONF, val);
1908 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
937bb610 1909 DRM_ERROR("Failed to enable PCH transcoder\n");
8fb033d7
PZ
1910}
1911
b8a4f404
PZ
1912static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1913 enum pipe pipe)
040484af 1914{
23670b32 1915 struct drm_device *dev = dev_priv->dev;
f0f59a00
VS
1916 i915_reg_t reg;
1917 uint32_t val;
040484af
JB
1918
1919 /* FDI relies on the transcoder */
1920 assert_fdi_tx_disabled(dev_priv, pipe);
1921 assert_fdi_rx_disabled(dev_priv, pipe);
1922
291906f1
JB
1923 /* Ports must be off as well */
1924 assert_pch_ports_disabled(dev_priv, pipe);
1925
ab9412ba 1926 reg = PCH_TRANSCONF(pipe);
040484af
JB
1927 val = I915_READ(reg);
1928 val &= ~TRANS_ENABLE;
1929 I915_WRITE(reg, val);
1930 /* wait for PCH transcoder off, transcoder state */
1931 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
4bb6f1f3 1932 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
23670b32 1933
c465613b 1934 if (HAS_PCH_CPT(dev)) {
23670b32
DV
1935 /* Workaround: Clear the timing override chicken bit again. */
1936 reg = TRANS_CHICKEN2(pipe);
1937 val = I915_READ(reg);
1938 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1939 I915_WRITE(reg, val);
1940 }
040484af
JB
1941}
1942
ab4d966c 1943static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
8fb033d7 1944{
8fb033d7
PZ
1945 u32 val;
1946
ab9412ba 1947 val = I915_READ(LPT_TRANSCONF);
8fb033d7 1948 val &= ~TRANS_ENABLE;
ab9412ba 1949 I915_WRITE(LPT_TRANSCONF, val);
8fb033d7 1950 /* wait for PCH transcoder off, transcoder state */
ab9412ba 1951 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
8a52fd9f 1952 DRM_ERROR("Failed to disable PCH transcoder\n");
223a6fdf
PZ
1953
1954 /* Workaround: clear timing override bit. */
36c0d0cf 1955 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
23670b32 1956 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
36c0d0cf 1957 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
040484af
JB
1958}
1959
b24e7179 1960/**
309cfea8 1961 * intel_enable_pipe - enable a pipe, asserting requirements
0372264a 1962 * @crtc: crtc responsible for the pipe
b24e7179 1963 *
0372264a 1964 * Enable @crtc's pipe, making sure that various hardware specific requirements
b24e7179 1965 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
b24e7179 1966 */
e1fdc473 1967static void intel_enable_pipe(struct intel_crtc *crtc)
b24e7179 1968{
0372264a
PZ
1969 struct drm_device *dev = crtc->base.dev;
1970 struct drm_i915_private *dev_priv = dev->dev_private;
1971 enum pipe pipe = crtc->pipe;
1a70a728 1972 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
1a240d4d 1973 enum pipe pch_transcoder;
f0f59a00 1974 i915_reg_t reg;
b24e7179
JB
1975 u32 val;
1976
9e2ee2dd
VS
1977 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1978
58c6eaa2 1979 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 1980 assert_cursor_disabled(dev_priv, pipe);
58c6eaa2
DV
1981 assert_sprites_disabled(dev_priv, pipe);
1982
681e5811 1983 if (HAS_PCH_LPT(dev_priv->dev))
cc391bbb
PZ
1984 pch_transcoder = TRANSCODER_A;
1985 else
1986 pch_transcoder = pipe;
1987
b24e7179
JB
1988 /*
1989 * A pipe without a PLL won't actually be able to drive bits from
1990 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1991 * need the check.
1992 */
50360403 1993 if (HAS_GMCH_DISPLAY(dev_priv->dev))
a65347ba 1994 if (crtc->config->has_dsi_encoder)
23538ef1
JN
1995 assert_dsi_pll_enabled(dev_priv);
1996 else
1997 assert_pll_enabled(dev_priv, pipe);
040484af 1998 else {
6e3c9717 1999 if (crtc->config->has_pch_encoder) {
040484af 2000 /* if driving the PCH, we need FDI enabled */
cc391bbb 2001 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
1a240d4d
DV
2002 assert_fdi_tx_pll_enabled(dev_priv,
2003 (enum pipe) cpu_transcoder);
040484af
JB
2004 }
2005 /* FIXME: assert CPU port conditions for SNB+ */
2006 }
b24e7179 2007
702e7a56 2008 reg = PIPECONF(cpu_transcoder);
b24e7179 2009 val = I915_READ(reg);
7ad25d48 2010 if (val & PIPECONF_ENABLE) {
b6b5d049
VS
2011 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2012 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
00d70b15 2013 return;
7ad25d48 2014 }
00d70b15
CW
2015
2016 I915_WRITE(reg, val | PIPECONF_ENABLE);
851855d8 2017 POSTING_READ(reg);
b7792d8b
VS
2018
2019 /*
2020 * Until the pipe starts DSL will read as 0, which would cause
2021 * an apparent vblank timestamp jump, which messes up also the
2022 * frame count when it's derived from the timestamps. So let's
2023 * wait for the pipe to start properly before we call
2024 * drm_crtc_vblank_on()
2025 */
2026 if (dev->max_vblank_count == 0 &&
2027 wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
2028 DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
b24e7179
JB
2029}
2030
2031/**
309cfea8 2032 * intel_disable_pipe - disable a pipe, asserting requirements
575f7ab7 2033 * @crtc: crtc whose pipes is to be disabled
b24e7179 2034 *
575f7ab7
VS
2035 * Disable the pipe of @crtc, making sure that various hardware
2036 * specific requirements are met, if applicable, e.g. plane
2037 * disabled, panel fitter off, etc.
b24e7179
JB
2038 *
2039 * Will wait until the pipe has shut down before returning.
2040 */
575f7ab7 2041static void intel_disable_pipe(struct intel_crtc *crtc)
b24e7179 2042{
575f7ab7 2043 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
6e3c9717 2044 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 2045 enum pipe pipe = crtc->pipe;
f0f59a00 2046 i915_reg_t reg;
b24e7179
JB
2047 u32 val;
2048
9e2ee2dd
VS
2049 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2050
b24e7179
JB
2051 /*
2052 * Make sure planes won't keep trying to pump pixels to us,
2053 * or we might hang the display.
2054 */
2055 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2056 assert_cursor_disabled(dev_priv, pipe);
19332d7a 2057 assert_sprites_disabled(dev_priv, pipe);
b24e7179 2058
702e7a56 2059 reg = PIPECONF(cpu_transcoder);
b24e7179 2060 val = I915_READ(reg);
00d70b15
CW
2061 if ((val & PIPECONF_ENABLE) == 0)
2062 return;
2063
67adc644
VS
2064 /*
2065 * Double wide has implications for planes
2066 * so best keep it disabled when not needed.
2067 */
6e3c9717 2068 if (crtc->config->double_wide)
67adc644
VS
2069 val &= ~PIPECONF_DOUBLE_WIDE;
2070
2071 /* Don't disable pipe or pipe PLLs if needed */
b6b5d049
VS
2072 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2073 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
67adc644
VS
2074 val &= ~PIPECONF_ENABLE;
2075
2076 I915_WRITE(reg, val);
2077 if ((val & PIPECONF_ENABLE) == 0)
2078 intel_wait_for_pipe_off(crtc);
b24e7179
JB
2079}
2080
693db184
CW
2081static bool need_vtd_wa(struct drm_device *dev)
2082{
2083#ifdef CONFIG_INTEL_IOMMU
2084 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2085 return true;
2086#endif
2087 return false;
2088}
2089
832be82f
VS
2090static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
2091{
2092 return IS_GEN2(dev_priv) ? 2048 : 4096;
2093}
2094
27ba3910
VS
2095static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
2096 uint64_t fb_modifier, unsigned int cpp)
7b49f948
VS
2097{
2098 switch (fb_modifier) {
2099 case DRM_FORMAT_MOD_NONE:
2100 return cpp;
2101 case I915_FORMAT_MOD_X_TILED:
2102 if (IS_GEN2(dev_priv))
2103 return 128;
2104 else
2105 return 512;
2106 case I915_FORMAT_MOD_Y_TILED:
2107 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2108 return 128;
2109 else
2110 return 512;
2111 case I915_FORMAT_MOD_Yf_TILED:
2112 switch (cpp) {
2113 case 1:
2114 return 64;
2115 case 2:
2116 case 4:
2117 return 128;
2118 case 8:
2119 case 16:
2120 return 256;
2121 default:
2122 MISSING_CASE(cpp);
2123 return cpp;
2124 }
2125 break;
2126 default:
2127 MISSING_CASE(fb_modifier);
2128 return cpp;
2129 }
2130}
2131
832be82f
VS
2132unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
2133 uint64_t fb_modifier, unsigned int cpp)
a57ce0b2 2134{
832be82f
VS
2135 if (fb_modifier == DRM_FORMAT_MOD_NONE)
2136 return 1;
2137 else
2138 return intel_tile_size(dev_priv) /
27ba3910 2139 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
6761dd31
TU
2140}
2141
8d0deca8
VS
2142/* Return the tile dimensions in pixel units */
2143static void intel_tile_dims(const struct drm_i915_private *dev_priv,
2144 unsigned int *tile_width,
2145 unsigned int *tile_height,
2146 uint64_t fb_modifier,
2147 unsigned int cpp)
2148{
2149 unsigned int tile_width_bytes =
2150 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
2151
2152 *tile_width = tile_width_bytes / cpp;
2153 *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
2154}
2155
6761dd31
TU
2156unsigned int
2157intel_fb_align_height(struct drm_device *dev, unsigned int height,
832be82f 2158 uint32_t pixel_format, uint64_t fb_modifier)
6761dd31 2159{
832be82f
VS
2160 unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
2161 unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
2162
2163 return ALIGN(height, tile_height);
a57ce0b2
JB
2164}
2165
1663b9d6
VS
2166unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2167{
2168 unsigned int size = 0;
2169 int i;
2170
2171 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2172 size += rot_info->plane[i].width * rot_info->plane[i].height;
2173
2174 return size;
2175}
2176
75c82a53 2177static void
3465c580
VS
2178intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2179 const struct drm_framebuffer *fb,
2180 unsigned int rotation)
f64b98cd 2181{
2d7a215f
VS
2182 if (intel_rotation_90_or_270(rotation)) {
2183 *view = i915_ggtt_view_rotated;
2184 view->params.rotated = to_intel_framebuffer(fb)->rot_info;
2185 } else {
2186 *view = i915_ggtt_view_normal;
2187 }
2188}
50470bb0 2189
2d7a215f
VS
2190static void
2191intel_fill_fb_info(struct drm_i915_private *dev_priv,
2192 struct drm_framebuffer *fb)
2193{
2194 struct intel_rotation_info *info = &to_intel_framebuffer(fb)->rot_info;
2195 unsigned int tile_size, tile_width, tile_height, cpp;
50470bb0 2196
d9b3288e
VS
2197 tile_size = intel_tile_size(dev_priv);
2198
2199 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
8d0deca8
VS
2200 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2201 fb->modifier[0], cpp);
d9b3288e 2202
1663b9d6
VS
2203 info->plane[0].width = DIV_ROUND_UP(fb->pitches[0], tile_width * cpp);
2204 info->plane[0].height = DIV_ROUND_UP(fb->height, tile_height);
84fe03f7 2205
89e3e142 2206 if (info->pixel_format == DRM_FORMAT_NV12) {
832be82f 2207 cpp = drm_format_plane_cpp(fb->pixel_format, 1);
8d0deca8
VS
2208 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2209 fb->modifier[1], cpp);
d9b3288e 2210
2d7a215f 2211 info->uv_offset = fb->offsets[1];
1663b9d6
VS
2212 info->plane[1].width = DIV_ROUND_UP(fb->pitches[1], tile_width * cpp);
2213 info->plane[1].height = DIV_ROUND_UP(fb->height / 2, tile_height);
89e3e142 2214 }
f64b98cd
TU
2215}
2216
603525d7 2217static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
4e9a86b6
VS
2218{
2219 if (INTEL_INFO(dev_priv)->gen >= 9)
2220 return 256 * 1024;
985b8bb4 2221 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
666a4537 2222 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
4e9a86b6
VS
2223 return 128 * 1024;
2224 else if (INTEL_INFO(dev_priv)->gen >= 4)
2225 return 4 * 1024;
2226 else
44c5905e 2227 return 0;
4e9a86b6
VS
2228}
2229
603525d7
VS
2230static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
2231 uint64_t fb_modifier)
2232{
2233 switch (fb_modifier) {
2234 case DRM_FORMAT_MOD_NONE:
2235 return intel_linear_alignment(dev_priv);
2236 case I915_FORMAT_MOD_X_TILED:
2237 if (INTEL_INFO(dev_priv)->gen >= 9)
2238 return 256 * 1024;
2239 return 0;
2240 case I915_FORMAT_MOD_Y_TILED:
2241 case I915_FORMAT_MOD_Yf_TILED:
2242 return 1 * 1024 * 1024;
2243 default:
2244 MISSING_CASE(fb_modifier);
2245 return 0;
2246 }
2247}
2248
127bd2ac 2249int
3465c580
VS
2250intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
2251 unsigned int rotation)
6b95a207 2252{
850c4cdc 2253 struct drm_device *dev = fb->dev;
ce453d81 2254 struct drm_i915_private *dev_priv = dev->dev_private;
850c4cdc 2255 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2256 struct i915_ggtt_view view;
6b95a207
KH
2257 u32 alignment;
2258 int ret;
2259
ebcdd39e
MR
2260 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2261
603525d7 2262 alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
6b95a207 2263
3465c580 2264 intel_fill_fb_ggtt_view(&view, fb, rotation);
f64b98cd 2265
693db184
CW
2266 /* Note that the w/a also requires 64 PTE of padding following the
2267 * bo. We currently fill all unused PTE with the shadow page and so
2268 * we should always have valid PTE following the scanout preventing
2269 * the VT-d warning.
2270 */
2271 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2272 alignment = 256 * 1024;
2273
d6dd6843
PZ
2274 /*
2275 * Global gtt pte registers are special registers which actually forward
2276 * writes to a chunk of system memory. Which means that there is no risk
2277 * that the register values disappear as soon as we call
2278 * intel_runtime_pm_put(), so it is correct to wrap only the
2279 * pin/unpin/fence and not more.
2280 */
2281 intel_runtime_pm_get(dev_priv);
2282
7580d774
ML
2283 ret = i915_gem_object_pin_to_display_plane(obj, alignment,
2284 &view);
48b956c5 2285 if (ret)
b26a6b35 2286 goto err_pm;
6b95a207
KH
2287
2288 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2289 * fence, whereas 965+ only requires a fence if using
2290 * framebuffer compression. For simplicity, we always install
2291 * a fence as the cost is not that onerous.
2292 */
9807216f
VK
2293 if (view.type == I915_GGTT_VIEW_NORMAL) {
2294 ret = i915_gem_object_get_fence(obj);
2295 if (ret == -EDEADLK) {
2296 /*
2297 * -EDEADLK means there are no free fences
2298 * no pending flips.
2299 *
2300 * This is propagated to atomic, but it uses
2301 * -EDEADLK to force a locking recovery, so
2302 * change the returned error to -EBUSY.
2303 */
2304 ret = -EBUSY;
2305 goto err_unpin;
2306 } else if (ret)
2307 goto err_unpin;
1690e1eb 2308
9807216f
VK
2309 i915_gem_object_pin_fence(obj);
2310 }
6b95a207 2311
d6dd6843 2312 intel_runtime_pm_put(dev_priv);
6b95a207 2313 return 0;
48b956c5
CW
2314
2315err_unpin:
f64b98cd 2316 i915_gem_object_unpin_from_display_plane(obj, &view);
b26a6b35 2317err_pm:
d6dd6843 2318 intel_runtime_pm_put(dev_priv);
48b956c5 2319 return ret;
6b95a207
KH
2320}
2321
3465c580 2322static void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
1690e1eb 2323{
82bc3b2d 2324 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2325 struct i915_ggtt_view view;
82bc3b2d 2326
ebcdd39e
MR
2327 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2328
3465c580 2329 intel_fill_fb_ggtt_view(&view, fb, rotation);
f64b98cd 2330
9807216f
VK
2331 if (view.type == I915_GGTT_VIEW_NORMAL)
2332 i915_gem_object_unpin_fence(obj);
2333
f64b98cd 2334 i915_gem_object_unpin_from_display_plane(obj, &view);
1690e1eb
CW
2335}
2336
29cf9491
VS
2337/*
2338 * Adjust the tile offset by moving the difference into
2339 * the x/y offsets.
2340 *
2341 * Input tile dimensions and pitch must already be
2342 * rotated to match x and y, and in pixel units.
2343 */
2344static u32 intel_adjust_tile_offset(int *x, int *y,
2345 unsigned int tile_width,
2346 unsigned int tile_height,
2347 unsigned int tile_size,
2348 unsigned int pitch_tiles,
2349 u32 old_offset,
2350 u32 new_offset)
2351{
2352 unsigned int tiles;
2353
2354 WARN_ON(old_offset & (tile_size - 1));
2355 WARN_ON(new_offset & (tile_size - 1));
2356 WARN_ON(new_offset > old_offset);
2357
2358 tiles = (old_offset - new_offset) / tile_size;
2359
2360 *y += tiles / pitch_tiles * tile_height;
2361 *x += tiles % pitch_tiles * tile_width;
2362
2363 return new_offset;
2364}
2365
8d0deca8
VS
2366/*
2367 * Computes the linear offset to the base tile and adjusts
2368 * x, y. bytes per pixel is assumed to be a power-of-two.
2369 *
2370 * In the 90/270 rotated case, x and y are assumed
2371 * to be already rotated to match the rotated GTT view, and
2372 * pitch is the tile_height aligned framebuffer height.
2373 */
4f2d9934
VS
2374u32 intel_compute_tile_offset(int *x, int *y,
2375 const struct drm_framebuffer *fb, int plane,
8d0deca8
VS
2376 unsigned int pitch,
2377 unsigned int rotation)
c2c75131 2378{
4f2d9934
VS
2379 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
2380 uint64_t fb_modifier = fb->modifier[plane];
2381 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
29cf9491
VS
2382 u32 offset, offset_aligned, alignment;
2383
2384 alignment = intel_surf_alignment(dev_priv, fb_modifier);
2385 if (alignment)
2386 alignment--;
2387
b5c65338 2388 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
8d0deca8
VS
2389 unsigned int tile_size, tile_width, tile_height;
2390 unsigned int tile_rows, tiles, pitch_tiles;
c2c75131 2391
d843310d 2392 tile_size = intel_tile_size(dev_priv);
8d0deca8
VS
2393 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2394 fb_modifier, cpp);
2395
2396 if (intel_rotation_90_or_270(rotation)) {
2397 pitch_tiles = pitch / tile_height;
2398 swap(tile_width, tile_height);
2399 } else {
2400 pitch_tiles = pitch / (tile_width * cpp);
2401 }
d843310d
VS
2402
2403 tile_rows = *y / tile_height;
2404 *y %= tile_height;
c2c75131 2405
8d0deca8
VS
2406 tiles = *x / tile_width;
2407 *x %= tile_width;
bc752862 2408
29cf9491
VS
2409 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2410 offset_aligned = offset & ~alignment;
bc752862 2411
29cf9491
VS
2412 intel_adjust_tile_offset(x, y, tile_width, tile_height,
2413 tile_size, pitch_tiles,
2414 offset, offset_aligned);
2415 } else {
bc752862 2416 offset = *y * pitch + *x * cpp;
29cf9491
VS
2417 offset_aligned = offset & ~alignment;
2418
4e9a86b6
VS
2419 *y = (offset & alignment) / pitch;
2420 *x = ((offset & alignment) - *y * pitch) / cpp;
bc752862 2421 }
29cf9491
VS
2422
2423 return offset_aligned;
c2c75131
DV
2424}
2425
b35d63fa 2426static int i9xx_format_to_fourcc(int format)
46f297fb
JB
2427{
2428 switch (format) {
2429 case DISPPLANE_8BPP:
2430 return DRM_FORMAT_C8;
2431 case DISPPLANE_BGRX555:
2432 return DRM_FORMAT_XRGB1555;
2433 case DISPPLANE_BGRX565:
2434 return DRM_FORMAT_RGB565;
2435 default:
2436 case DISPPLANE_BGRX888:
2437 return DRM_FORMAT_XRGB8888;
2438 case DISPPLANE_RGBX888:
2439 return DRM_FORMAT_XBGR8888;
2440 case DISPPLANE_BGRX101010:
2441 return DRM_FORMAT_XRGB2101010;
2442 case DISPPLANE_RGBX101010:
2443 return DRM_FORMAT_XBGR2101010;
2444 }
2445}
2446
bc8d7dff
DL
2447static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2448{
2449 switch (format) {
2450 case PLANE_CTL_FORMAT_RGB_565:
2451 return DRM_FORMAT_RGB565;
2452 default:
2453 case PLANE_CTL_FORMAT_XRGB_8888:
2454 if (rgb_order) {
2455 if (alpha)
2456 return DRM_FORMAT_ABGR8888;
2457 else
2458 return DRM_FORMAT_XBGR8888;
2459 } else {
2460 if (alpha)
2461 return DRM_FORMAT_ARGB8888;
2462 else
2463 return DRM_FORMAT_XRGB8888;
2464 }
2465 case PLANE_CTL_FORMAT_XRGB_2101010:
2466 if (rgb_order)
2467 return DRM_FORMAT_XBGR2101010;
2468 else
2469 return DRM_FORMAT_XRGB2101010;
2470 }
2471}
2472
5724dbd1 2473static bool
f6936e29
DV
2474intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2475 struct intel_initial_plane_config *plane_config)
46f297fb
JB
2476{
2477 struct drm_device *dev = crtc->base.dev;
3badb49f 2478 struct drm_i915_private *dev_priv = to_i915(dev);
46f297fb
JB
2479 struct drm_i915_gem_object *obj = NULL;
2480 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2d14030b 2481 struct drm_framebuffer *fb = &plane_config->fb->base;
f37b5c2b
DV
2482 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2483 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2484 PAGE_SIZE);
2485
2486 size_aligned -= base_aligned;
46f297fb 2487
ff2652ea
CW
2488 if (plane_config->size == 0)
2489 return false;
2490
3badb49f
PZ
2491 /* If the FB is too big, just don't use it since fbdev is not very
2492 * important and we should probably use that space with FBC or other
2493 * features. */
62106b4f 2494 if (size_aligned * 2 > dev_priv->ggtt.stolen_usable_size)
3badb49f
PZ
2495 return false;
2496
12c83d99
TU
2497 mutex_lock(&dev->struct_mutex);
2498
f37b5c2b
DV
2499 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2500 base_aligned,
2501 base_aligned,
2502 size_aligned);
12c83d99
TU
2503 if (!obj) {
2504 mutex_unlock(&dev->struct_mutex);
484b41dd 2505 return false;
12c83d99 2506 }
46f297fb 2507
49af449b
DL
2508 obj->tiling_mode = plane_config->tiling;
2509 if (obj->tiling_mode == I915_TILING_X)
6bf129df 2510 obj->stride = fb->pitches[0];
46f297fb 2511
6bf129df
DL
2512 mode_cmd.pixel_format = fb->pixel_format;
2513 mode_cmd.width = fb->width;
2514 mode_cmd.height = fb->height;
2515 mode_cmd.pitches[0] = fb->pitches[0];
18c5247e
DV
2516 mode_cmd.modifier[0] = fb->modifier[0];
2517 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
46f297fb 2518
6bf129df 2519 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
484b41dd 2520 &mode_cmd, obj)) {
46f297fb
JB
2521 DRM_DEBUG_KMS("intel fb init failed\n");
2522 goto out_unref_obj;
2523 }
12c83d99 2524
46f297fb 2525 mutex_unlock(&dev->struct_mutex);
484b41dd 2526
f6936e29 2527 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
484b41dd 2528 return true;
46f297fb
JB
2529
2530out_unref_obj:
2531 drm_gem_object_unreference(&obj->base);
2532 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2533 return false;
2534}
2535
afd65eb4
MR
2536/* Update plane->state->fb to match plane->fb after driver-internal updates */
2537static void
2538update_state_fb(struct drm_plane *plane)
2539{
2540 if (plane->fb == plane->state->fb)
2541 return;
2542
2543 if (plane->state->fb)
2544 drm_framebuffer_unreference(plane->state->fb);
2545 plane->state->fb = plane->fb;
2546 if (plane->state->fb)
2547 drm_framebuffer_reference(plane->state->fb);
2548}
2549
5724dbd1 2550static void
f6936e29
DV
2551intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2552 struct intel_initial_plane_config *plane_config)
484b41dd
JB
2553{
2554 struct drm_device *dev = intel_crtc->base.dev;
d9ceb816 2555 struct drm_i915_private *dev_priv = dev->dev_private;
484b41dd
JB
2556 struct drm_crtc *c;
2557 struct intel_crtc *i;
2ff8fde1 2558 struct drm_i915_gem_object *obj;
88595ac9 2559 struct drm_plane *primary = intel_crtc->base.primary;
be5651f2 2560 struct drm_plane_state *plane_state = primary->state;
200757f5
MR
2561 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2562 struct intel_plane *intel_plane = to_intel_plane(primary);
0a8d8a86
MR
2563 struct intel_plane_state *intel_state =
2564 to_intel_plane_state(plane_state);
88595ac9 2565 struct drm_framebuffer *fb;
484b41dd 2566
2d14030b 2567 if (!plane_config->fb)
484b41dd
JB
2568 return;
2569
f6936e29 2570 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
88595ac9
DV
2571 fb = &plane_config->fb->base;
2572 goto valid_fb;
f55548b5 2573 }
484b41dd 2574
2d14030b 2575 kfree(plane_config->fb);
484b41dd
JB
2576
2577 /*
2578 * Failed to alloc the obj, check to see if we should share
2579 * an fb with another CRTC instead
2580 */
70e1e0ec 2581 for_each_crtc(dev, c) {
484b41dd
JB
2582 i = to_intel_crtc(c);
2583
2584 if (c == &intel_crtc->base)
2585 continue;
2586
2ff8fde1
MR
2587 if (!i->active)
2588 continue;
2589
88595ac9
DV
2590 fb = c->primary->fb;
2591 if (!fb)
484b41dd
JB
2592 continue;
2593
88595ac9 2594 obj = intel_fb_obj(fb);
2ff8fde1 2595 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
88595ac9
DV
2596 drm_framebuffer_reference(fb);
2597 goto valid_fb;
484b41dd
JB
2598 }
2599 }
88595ac9 2600
200757f5
MR
2601 /*
2602 * We've failed to reconstruct the BIOS FB. Current display state
2603 * indicates that the primary plane is visible, but has a NULL FB,
2604 * which will lead to problems later if we don't fix it up. The
2605 * simplest solution is to just disable the primary plane now and
2606 * pretend the BIOS never had it enabled.
2607 */
2608 to_intel_plane_state(plane_state)->visible = false;
2609 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
2622a081 2610 intel_pre_disable_primary_noatomic(&intel_crtc->base);
200757f5
MR
2611 intel_plane->disable_plane(primary, &intel_crtc->base);
2612
88595ac9
DV
2613 return;
2614
2615valid_fb:
f44e2659
VS
2616 plane_state->src_x = 0;
2617 plane_state->src_y = 0;
be5651f2
ML
2618 plane_state->src_w = fb->width << 16;
2619 plane_state->src_h = fb->height << 16;
2620
f44e2659
VS
2621 plane_state->crtc_x = 0;
2622 plane_state->crtc_y = 0;
be5651f2
ML
2623 plane_state->crtc_w = fb->width;
2624 plane_state->crtc_h = fb->height;
2625
0a8d8a86
MR
2626 intel_state->src.x1 = plane_state->src_x;
2627 intel_state->src.y1 = plane_state->src_y;
2628 intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
2629 intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
2630 intel_state->dst.x1 = plane_state->crtc_x;
2631 intel_state->dst.y1 = plane_state->crtc_y;
2632 intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
2633 intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
2634
88595ac9
DV
2635 obj = intel_fb_obj(fb);
2636 if (obj->tiling_mode != I915_TILING_NONE)
2637 dev_priv->preserve_bios_swizzle = true;
2638
be5651f2
ML
2639 drm_framebuffer_reference(fb);
2640 primary->fb = primary->state->fb = fb;
36750f28 2641 primary->crtc = primary->state->crtc = &intel_crtc->base;
36750f28 2642 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
a9ff8714 2643 obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
46f297fb
JB
2644}
2645
a8d201af
ML
2646static void i9xx_update_primary_plane(struct drm_plane *primary,
2647 const struct intel_crtc_state *crtc_state,
2648 const struct intel_plane_state *plane_state)
81255565 2649{
a8d201af 2650 struct drm_device *dev = primary->dev;
81255565 2651 struct drm_i915_private *dev_priv = dev->dev_private;
a8d201af
ML
2652 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2653 struct drm_framebuffer *fb = plane_state->base.fb;
2654 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
81255565 2655 int plane = intel_crtc->plane;
54ea9da8 2656 u32 linear_offset;
81255565 2657 u32 dspcntr;
f0f59a00 2658 i915_reg_t reg = DSPCNTR(plane);
8d0deca8 2659 unsigned int rotation = plane_state->base.rotation;
ac484963 2660 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
54ea9da8
VS
2661 int x = plane_state->src.x1 >> 16;
2662 int y = plane_state->src.y1 >> 16;
c9ba6fad 2663
f45651ba
VS
2664 dspcntr = DISPPLANE_GAMMA_ENABLE;
2665
fdd508a6 2666 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2667
2668 if (INTEL_INFO(dev)->gen < 4) {
2669 if (intel_crtc->pipe == PIPE_B)
2670 dspcntr |= DISPPLANE_SEL_PIPE_B;
2671
2672 /* pipesrc and dspsize control the size that is scaled from,
2673 * which should always be the user's requested size.
2674 */
2675 I915_WRITE(DSPSIZE(plane),
a8d201af
ML
2676 ((crtc_state->pipe_src_h - 1) << 16) |
2677 (crtc_state->pipe_src_w - 1));
f45651ba 2678 I915_WRITE(DSPPOS(plane), 0);
c14b0485
VS
2679 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2680 I915_WRITE(PRIMSIZE(plane),
a8d201af
ML
2681 ((crtc_state->pipe_src_h - 1) << 16) |
2682 (crtc_state->pipe_src_w - 1));
c14b0485
VS
2683 I915_WRITE(PRIMPOS(plane), 0);
2684 I915_WRITE(PRIMCNSTALPHA(plane), 0);
f45651ba 2685 }
81255565 2686
57779d06
VS
2687 switch (fb->pixel_format) {
2688 case DRM_FORMAT_C8:
81255565
JB
2689 dspcntr |= DISPPLANE_8BPP;
2690 break;
57779d06 2691 case DRM_FORMAT_XRGB1555:
57779d06 2692 dspcntr |= DISPPLANE_BGRX555;
81255565 2693 break;
57779d06
VS
2694 case DRM_FORMAT_RGB565:
2695 dspcntr |= DISPPLANE_BGRX565;
2696 break;
2697 case DRM_FORMAT_XRGB8888:
57779d06
VS
2698 dspcntr |= DISPPLANE_BGRX888;
2699 break;
2700 case DRM_FORMAT_XBGR8888:
57779d06
VS
2701 dspcntr |= DISPPLANE_RGBX888;
2702 break;
2703 case DRM_FORMAT_XRGB2101010:
57779d06
VS
2704 dspcntr |= DISPPLANE_BGRX101010;
2705 break;
2706 case DRM_FORMAT_XBGR2101010:
57779d06 2707 dspcntr |= DISPPLANE_RGBX101010;
81255565
JB
2708 break;
2709 default:
baba133a 2710 BUG();
81255565 2711 }
57779d06 2712
f45651ba
VS
2713 if (INTEL_INFO(dev)->gen >= 4 &&
2714 obj->tiling_mode != I915_TILING_NONE)
2715 dspcntr |= DISPPLANE_TILED;
81255565 2716
de1aa629
VS
2717 if (IS_G4X(dev))
2718 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2719
ac484963 2720 linear_offset = y * fb->pitches[0] + x * cpp;
81255565 2721
c2c75131
DV
2722 if (INTEL_INFO(dev)->gen >= 4) {
2723 intel_crtc->dspaddr_offset =
4f2d9934 2724 intel_compute_tile_offset(&x, &y, fb, 0,
8d0deca8 2725 fb->pitches[0], rotation);
c2c75131
DV
2726 linear_offset -= intel_crtc->dspaddr_offset;
2727 } else {
e506a0c6 2728 intel_crtc->dspaddr_offset = linear_offset;
c2c75131 2729 }
e506a0c6 2730
8d0deca8 2731 if (rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2732 dspcntr |= DISPPLANE_ROTATE_180;
2733
a8d201af
ML
2734 x += (crtc_state->pipe_src_w - 1);
2735 y += (crtc_state->pipe_src_h - 1);
48404c1e
SJ
2736
2737 /* Finding the last pixel of the last line of the display
2738 data and adding to linear_offset*/
2739 linear_offset +=
a8d201af 2740 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
ac484963 2741 (crtc_state->pipe_src_w - 1) * cpp;
48404c1e
SJ
2742 }
2743
2db3366b
PZ
2744 intel_crtc->adjusted_x = x;
2745 intel_crtc->adjusted_y = y;
2746
48404c1e
SJ
2747 I915_WRITE(reg, dspcntr);
2748
01f2c773 2749 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
a6c45cf0 2750 if (INTEL_INFO(dev)->gen >= 4) {
85ba7b7d
DV
2751 I915_WRITE(DSPSURF(plane),
2752 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
5eddb70b 2753 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
e506a0c6 2754 I915_WRITE(DSPLINOFF(plane), linear_offset);
5eddb70b 2755 } else
f343c5f6 2756 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
5eddb70b 2757 POSTING_READ(reg);
17638cd6
JB
2758}
2759
a8d201af
ML
2760static void i9xx_disable_primary_plane(struct drm_plane *primary,
2761 struct drm_crtc *crtc)
17638cd6
JB
2762{
2763 struct drm_device *dev = crtc->dev;
2764 struct drm_i915_private *dev_priv = dev->dev_private;
2765 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
17638cd6 2766 int plane = intel_crtc->plane;
f45651ba 2767
a8d201af
ML
2768 I915_WRITE(DSPCNTR(plane), 0);
2769 if (INTEL_INFO(dev_priv)->gen >= 4)
fdd508a6 2770 I915_WRITE(DSPSURF(plane), 0);
a8d201af
ML
2771 else
2772 I915_WRITE(DSPADDR(plane), 0);
2773 POSTING_READ(DSPCNTR(plane));
2774}
c9ba6fad 2775
a8d201af
ML
2776static void ironlake_update_primary_plane(struct drm_plane *primary,
2777 const struct intel_crtc_state *crtc_state,
2778 const struct intel_plane_state *plane_state)
2779{
2780 struct drm_device *dev = primary->dev;
2781 struct drm_i915_private *dev_priv = dev->dev_private;
2782 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2783 struct drm_framebuffer *fb = plane_state->base.fb;
2784 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
2785 int plane = intel_crtc->plane;
54ea9da8 2786 u32 linear_offset;
a8d201af
ML
2787 u32 dspcntr;
2788 i915_reg_t reg = DSPCNTR(plane);
8d0deca8 2789 unsigned int rotation = plane_state->base.rotation;
ac484963 2790 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
a8d201af
ML
2791 int x = plane_state->src.x1 >> 16;
2792 int y = plane_state->src.y1 >> 16;
c9ba6fad 2793
f45651ba 2794 dspcntr = DISPPLANE_GAMMA_ENABLE;
fdd508a6 2795 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2796
2797 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2798 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
17638cd6 2799
57779d06
VS
2800 switch (fb->pixel_format) {
2801 case DRM_FORMAT_C8:
17638cd6
JB
2802 dspcntr |= DISPPLANE_8BPP;
2803 break;
57779d06
VS
2804 case DRM_FORMAT_RGB565:
2805 dspcntr |= DISPPLANE_BGRX565;
17638cd6 2806 break;
57779d06 2807 case DRM_FORMAT_XRGB8888:
57779d06
VS
2808 dspcntr |= DISPPLANE_BGRX888;
2809 break;
2810 case DRM_FORMAT_XBGR8888:
57779d06
VS
2811 dspcntr |= DISPPLANE_RGBX888;
2812 break;
2813 case DRM_FORMAT_XRGB2101010:
57779d06
VS
2814 dspcntr |= DISPPLANE_BGRX101010;
2815 break;
2816 case DRM_FORMAT_XBGR2101010:
57779d06 2817 dspcntr |= DISPPLANE_RGBX101010;
17638cd6
JB
2818 break;
2819 default:
baba133a 2820 BUG();
17638cd6
JB
2821 }
2822
2823 if (obj->tiling_mode != I915_TILING_NONE)
2824 dspcntr |= DISPPLANE_TILED;
17638cd6 2825
f45651ba 2826 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
1f5d76db 2827 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
17638cd6 2828
ac484963 2829 linear_offset = y * fb->pitches[0] + x * cpp;
c2c75131 2830 intel_crtc->dspaddr_offset =
4f2d9934 2831 intel_compute_tile_offset(&x, &y, fb, 0,
8d0deca8 2832 fb->pitches[0], rotation);
c2c75131 2833 linear_offset -= intel_crtc->dspaddr_offset;
8d0deca8 2834 if (rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2835 dspcntr |= DISPPLANE_ROTATE_180;
2836
2837 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
a8d201af
ML
2838 x += (crtc_state->pipe_src_w - 1);
2839 y += (crtc_state->pipe_src_h - 1);
48404c1e
SJ
2840
2841 /* Finding the last pixel of the last line of the display
2842 data and adding to linear_offset*/
2843 linear_offset +=
a8d201af 2844 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
ac484963 2845 (crtc_state->pipe_src_w - 1) * cpp;
48404c1e
SJ
2846 }
2847 }
2848
2db3366b
PZ
2849 intel_crtc->adjusted_x = x;
2850 intel_crtc->adjusted_y = y;
2851
48404c1e 2852 I915_WRITE(reg, dspcntr);
17638cd6 2853
01f2c773 2854 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
85ba7b7d
DV
2855 I915_WRITE(DSPSURF(plane),
2856 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
b3dc685e 2857 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
bc1c91eb
DL
2858 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2859 } else {
2860 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2861 I915_WRITE(DSPLINOFF(plane), linear_offset);
2862 }
17638cd6 2863 POSTING_READ(reg);
17638cd6
JB
2864}
2865
7b49f948
VS
2866u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
2867 uint64_t fb_modifier, uint32_t pixel_format)
b321803d 2868{
7b49f948 2869 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
b321803d 2870 return 64;
7b49f948
VS
2871 } else {
2872 int cpp = drm_format_plane_cpp(pixel_format, 0);
2873
27ba3910 2874 return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
b321803d
DL
2875 }
2876}
2877
44eb0cb9
MK
2878u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
2879 struct drm_i915_gem_object *obj,
2880 unsigned int plane)
121920fa 2881{
ce7f1728 2882 struct i915_ggtt_view view;
dedf278c 2883 struct i915_vma *vma;
44eb0cb9 2884 u64 offset;
121920fa 2885
e7941294 2886 intel_fill_fb_ggtt_view(&view, intel_plane->base.state->fb,
3465c580 2887 intel_plane->base.state->rotation);
121920fa 2888
ce7f1728 2889 vma = i915_gem_obj_to_ggtt_view(obj, &view);
dedf278c 2890 if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
ce7f1728 2891 view.type))
dedf278c
TU
2892 return -1;
2893
44eb0cb9 2894 offset = vma->node.start;
dedf278c
TU
2895
2896 if (plane == 1) {
7723f47d 2897 offset += vma->ggtt_view.params.rotated.uv_start_page *
dedf278c
TU
2898 PAGE_SIZE;
2899 }
2900
44eb0cb9
MK
2901 WARN_ON(upper_32_bits(offset));
2902
2903 return lower_32_bits(offset);
121920fa
TU
2904}
2905
e435d6e5
ML
2906static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
2907{
2908 struct drm_device *dev = intel_crtc->base.dev;
2909 struct drm_i915_private *dev_priv = dev->dev_private;
2910
2911 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
2912 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
2913 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
e435d6e5
ML
2914}
2915
a1b2278e
CK
2916/*
2917 * This function detaches (aka. unbinds) unused scalers in hardware
2918 */
0583236e 2919static void skl_detach_scalers(struct intel_crtc *intel_crtc)
a1b2278e 2920{
a1b2278e
CK
2921 struct intel_crtc_scaler_state *scaler_state;
2922 int i;
2923
a1b2278e
CK
2924 scaler_state = &intel_crtc->config->scaler_state;
2925
2926 /* loop through and disable scalers that aren't in use */
2927 for (i = 0; i < intel_crtc->num_scalers; i++) {
e435d6e5
ML
2928 if (!scaler_state->scalers[i].in_use)
2929 skl_detach_scaler(intel_crtc, i);
a1b2278e
CK
2930 }
2931}
2932
6156a456 2933u32 skl_plane_ctl_format(uint32_t pixel_format)
70d21f0e 2934{
6156a456 2935 switch (pixel_format) {
d161cf7a 2936 case DRM_FORMAT_C8:
c34ce3d1 2937 return PLANE_CTL_FORMAT_INDEXED;
70d21f0e 2938 case DRM_FORMAT_RGB565:
c34ce3d1 2939 return PLANE_CTL_FORMAT_RGB_565;
70d21f0e 2940 case DRM_FORMAT_XBGR8888:
c34ce3d1 2941 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
6156a456 2942 case DRM_FORMAT_XRGB8888:
c34ce3d1 2943 return PLANE_CTL_FORMAT_XRGB_8888;
6156a456
CK
2944 /*
2945 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2946 * to be already pre-multiplied. We need to add a knob (or a different
2947 * DRM_FORMAT) for user-space to configure that.
2948 */
f75fb42a 2949 case DRM_FORMAT_ABGR8888:
c34ce3d1 2950 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
6156a456 2951 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
6156a456 2952 case DRM_FORMAT_ARGB8888:
c34ce3d1 2953 return PLANE_CTL_FORMAT_XRGB_8888 |
6156a456 2954 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
70d21f0e 2955 case DRM_FORMAT_XRGB2101010:
c34ce3d1 2956 return PLANE_CTL_FORMAT_XRGB_2101010;
70d21f0e 2957 case DRM_FORMAT_XBGR2101010:
c34ce3d1 2958 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
6156a456 2959 case DRM_FORMAT_YUYV:
c34ce3d1 2960 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
6156a456 2961 case DRM_FORMAT_YVYU:
c34ce3d1 2962 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
6156a456 2963 case DRM_FORMAT_UYVY:
c34ce3d1 2964 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
6156a456 2965 case DRM_FORMAT_VYUY:
c34ce3d1 2966 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
70d21f0e 2967 default:
4249eeef 2968 MISSING_CASE(pixel_format);
70d21f0e 2969 }
8cfcba41 2970
c34ce3d1 2971 return 0;
6156a456 2972}
70d21f0e 2973
6156a456
CK
2974u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2975{
6156a456 2976 switch (fb_modifier) {
30af77c4 2977 case DRM_FORMAT_MOD_NONE:
70d21f0e 2978 break;
30af77c4 2979 case I915_FORMAT_MOD_X_TILED:
c34ce3d1 2980 return PLANE_CTL_TILED_X;
b321803d 2981 case I915_FORMAT_MOD_Y_TILED:
c34ce3d1 2982 return PLANE_CTL_TILED_Y;
b321803d 2983 case I915_FORMAT_MOD_Yf_TILED:
c34ce3d1 2984 return PLANE_CTL_TILED_YF;
70d21f0e 2985 default:
6156a456 2986 MISSING_CASE(fb_modifier);
70d21f0e 2987 }
8cfcba41 2988
c34ce3d1 2989 return 0;
6156a456 2990}
70d21f0e 2991
6156a456
CK
2992u32 skl_plane_ctl_rotation(unsigned int rotation)
2993{
3b7a5119 2994 switch (rotation) {
6156a456
CK
2995 case BIT(DRM_ROTATE_0):
2996 break;
1e8df167
SJ
2997 /*
2998 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
2999 * while i915 HW rotation is clockwise, thats why this swapping.
3000 */
3b7a5119 3001 case BIT(DRM_ROTATE_90):
1e8df167 3002 return PLANE_CTL_ROTATE_270;
3b7a5119 3003 case BIT(DRM_ROTATE_180):
c34ce3d1 3004 return PLANE_CTL_ROTATE_180;
3b7a5119 3005 case BIT(DRM_ROTATE_270):
1e8df167 3006 return PLANE_CTL_ROTATE_90;
6156a456
CK
3007 default:
3008 MISSING_CASE(rotation);
3009 }
3010
c34ce3d1 3011 return 0;
6156a456
CK
3012}
3013
a8d201af
ML
3014static void skylake_update_primary_plane(struct drm_plane *plane,
3015 const struct intel_crtc_state *crtc_state,
3016 const struct intel_plane_state *plane_state)
6156a456 3017{
a8d201af 3018 struct drm_device *dev = plane->dev;
6156a456 3019 struct drm_i915_private *dev_priv = dev->dev_private;
a8d201af
ML
3020 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3021 struct drm_framebuffer *fb = plane_state->base.fb;
3022 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
6156a456
CK
3023 int pipe = intel_crtc->pipe;
3024 u32 plane_ctl, stride_div, stride;
3025 u32 tile_height, plane_offset, plane_size;
a8d201af 3026 unsigned int rotation = plane_state->base.rotation;
6156a456 3027 int x_offset, y_offset;
44eb0cb9 3028 u32 surf_addr;
a8d201af
ML
3029 int scaler_id = plane_state->scaler_id;
3030 int src_x = plane_state->src.x1 >> 16;
3031 int src_y = plane_state->src.y1 >> 16;
3032 int src_w = drm_rect_width(&plane_state->src) >> 16;
3033 int src_h = drm_rect_height(&plane_state->src) >> 16;
3034 int dst_x = plane_state->dst.x1;
3035 int dst_y = plane_state->dst.y1;
3036 int dst_w = drm_rect_width(&plane_state->dst);
3037 int dst_h = drm_rect_height(&plane_state->dst);
70d21f0e 3038
6156a456
CK
3039 plane_ctl = PLANE_CTL_ENABLE |
3040 PLANE_CTL_PIPE_GAMMA_ENABLE |
3041 PLANE_CTL_PIPE_CSC_ENABLE;
3042
3043 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3044 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
3045 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
6156a456
CK
3046 plane_ctl |= skl_plane_ctl_rotation(rotation);
3047
7b49f948 3048 stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
b321803d 3049 fb->pixel_format);
dedf278c 3050 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
3b7a5119 3051
a42e5a23
PZ
3052 WARN_ON(drm_rect_width(&plane_state->src) == 0);
3053
3b7a5119 3054 if (intel_rotation_90_or_270(rotation)) {
832be82f
VS
3055 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
3056
3b7a5119 3057 /* stride = Surface height in tiles */
832be82f 3058 tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
3b7a5119 3059 stride = DIV_ROUND_UP(fb->height, tile_height);
a8d201af
ML
3060 x_offset = stride * tile_height - src_y - src_h;
3061 y_offset = src_x;
6156a456 3062 plane_size = (src_w - 1) << 16 | (src_h - 1);
3b7a5119
SJ
3063 } else {
3064 stride = fb->pitches[0] / stride_div;
a8d201af
ML
3065 x_offset = src_x;
3066 y_offset = src_y;
6156a456 3067 plane_size = (src_h - 1) << 16 | (src_w - 1);
3b7a5119
SJ
3068 }
3069 plane_offset = y_offset << 16 | x_offset;
b321803d 3070
2db3366b
PZ
3071 intel_crtc->adjusted_x = x_offset;
3072 intel_crtc->adjusted_y = y_offset;
3073
70d21f0e 3074 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
3b7a5119
SJ
3075 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3076 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3077 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
6156a456
CK
3078
3079 if (scaler_id >= 0) {
3080 uint32_t ps_ctrl = 0;
3081
3082 WARN_ON(!dst_w || !dst_h);
3083 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3084 crtc_state->scaler_state.scalers[scaler_id].mode;
3085 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3086 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3087 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3088 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3089 I915_WRITE(PLANE_POS(pipe, 0), 0);
3090 } else {
3091 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3092 }
3093
121920fa 3094 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
70d21f0e
DL
3095
3096 POSTING_READ(PLANE_SURF(pipe, 0));
3097}
3098
a8d201af
ML
3099static void skylake_disable_primary_plane(struct drm_plane *primary,
3100 struct drm_crtc *crtc)
17638cd6
JB
3101{
3102 struct drm_device *dev = crtc->dev;
3103 struct drm_i915_private *dev_priv = dev->dev_private;
a8d201af 3104 int pipe = to_intel_crtc(crtc)->pipe;
17638cd6 3105
a8d201af
ML
3106 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3107 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3108 POSTING_READ(PLANE_SURF(pipe, 0));
3109}
29b9bde6 3110
a8d201af
ML
3111/* Assume fb object is pinned & idle & fenced and just update base pointers */
3112static int
3113intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3114 int x, int y, enum mode_set_atomic state)
3115{
3116 /* Support for kgdboc is disabled, this needs a major rework. */
3117 DRM_ERROR("legacy panic handler not supported any more.\n");
3118
3119 return -ENODEV;
81255565
JB
3120}
3121
7514747d 3122static void intel_complete_page_flips(struct drm_device *dev)
96a02917 3123{
96a02917
VS
3124 struct drm_crtc *crtc;
3125
70e1e0ec 3126 for_each_crtc(dev, crtc) {
96a02917
VS
3127 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3128 enum plane plane = intel_crtc->plane;
3129
3130 intel_prepare_page_flip(dev, plane);
3131 intel_finish_page_flip_plane(dev, plane);
3132 }
7514747d
VS
3133}
3134
3135static void intel_update_primary_planes(struct drm_device *dev)
3136{
7514747d 3137 struct drm_crtc *crtc;
96a02917 3138
70e1e0ec 3139 for_each_crtc(dev, crtc) {
11c22da6
ML
3140 struct intel_plane *plane = to_intel_plane(crtc->primary);
3141 struct intel_plane_state *plane_state;
96a02917 3142
11c22da6 3143 drm_modeset_lock_crtc(crtc, &plane->base);
11c22da6
ML
3144 plane_state = to_intel_plane_state(plane->base.state);
3145
a8d201af
ML
3146 if (plane_state->visible)
3147 plane->update_plane(&plane->base,
3148 to_intel_crtc_state(crtc->state),
3149 plane_state);
11c22da6
ML
3150
3151 drm_modeset_unlock_crtc(crtc);
96a02917
VS
3152 }
3153}
3154
7514747d
VS
3155void intel_prepare_reset(struct drm_device *dev)
3156{
3157 /* no reset support for gen2 */
3158 if (IS_GEN2(dev))
3159 return;
3160
3161 /* reset doesn't touch the display */
3162 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3163 return;
3164
3165 drm_modeset_lock_all(dev);
f98ce92f
VS
3166 /*
3167 * Disabling the crtcs gracefully seems nicer. Also the
3168 * g33 docs say we should at least disable all the planes.
3169 */
6b72d486 3170 intel_display_suspend(dev);
7514747d
VS
3171}
3172
3173void intel_finish_reset(struct drm_device *dev)
3174{
3175 struct drm_i915_private *dev_priv = to_i915(dev);
3176
3177 /*
3178 * Flips in the rings will be nuked by the reset,
3179 * so complete all pending flips so that user space
3180 * will get its events and not get stuck.
3181 */
3182 intel_complete_page_flips(dev);
3183
3184 /* no reset support for gen2 */
3185 if (IS_GEN2(dev))
3186 return;
3187
3188 /* reset doesn't touch the display */
3189 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3190 /*
3191 * Flips in the rings have been nuked by the reset,
3192 * so update the base address of all primary
3193 * planes to the the last fb to make sure we're
3194 * showing the correct fb after a reset.
11c22da6
ML
3195 *
3196 * FIXME: Atomic will make this obsolete since we won't schedule
3197 * CS-based flips (which might get lost in gpu resets) any more.
7514747d
VS
3198 */
3199 intel_update_primary_planes(dev);
3200 return;
3201 }
3202
3203 /*
3204 * The display has been reset as well,
3205 * so need a full re-initialization.
3206 */
3207 intel_runtime_pm_disable_interrupts(dev_priv);
3208 intel_runtime_pm_enable_interrupts(dev_priv);
3209
3210 intel_modeset_init_hw(dev);
3211
3212 spin_lock_irq(&dev_priv->irq_lock);
3213 if (dev_priv->display.hpd_irq_setup)
3214 dev_priv->display.hpd_irq_setup(dev);
3215 spin_unlock_irq(&dev_priv->irq_lock);
3216
043e9bda 3217 intel_display_resume(dev);
7514747d
VS
3218
3219 intel_hpd_init(dev_priv);
3220
3221 drm_modeset_unlock_all(dev);
3222}
3223
7d5e3799
CW
3224static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3225{
3226 struct drm_device *dev = crtc->dev;
3227 struct drm_i915_private *dev_priv = dev->dev_private;
3228 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7d5e3799
CW
3229 bool pending;
3230
3231 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3232 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3233 return false;
3234
5e2d7afc 3235 spin_lock_irq(&dev->event_lock);
7d5e3799 3236 pending = to_intel_crtc(crtc)->unpin_work != NULL;
5e2d7afc 3237 spin_unlock_irq(&dev->event_lock);
7d5e3799
CW
3238
3239 return pending;
3240}
3241
bfd16b2a
ML
3242static void intel_update_pipe_config(struct intel_crtc *crtc,
3243 struct intel_crtc_state *old_crtc_state)
e30e8f75
GP
3244{
3245 struct drm_device *dev = crtc->base.dev;
3246 struct drm_i915_private *dev_priv = dev->dev_private;
bfd16b2a
ML
3247 struct intel_crtc_state *pipe_config =
3248 to_intel_crtc_state(crtc->base.state);
e30e8f75 3249
bfd16b2a
ML
3250 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3251 crtc->base.mode = crtc->base.state->mode;
3252
3253 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3254 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3255 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
e30e8f75 3256
44522d85 3257 if (HAS_DDI(dev))
8563b1e8 3258 intel_color_set_csc(&crtc->base);
44522d85 3259
e30e8f75
GP
3260 /*
3261 * Update pipe size and adjust fitter if needed: the reason for this is
3262 * that in compute_mode_changes we check the native mode (not the pfit
3263 * mode) to see if we can flip rather than do a full mode set. In the
3264 * fastboot case, we'll flip, but if we don't update the pipesrc and
3265 * pfit state, we'll end up with a big fb scanned out into the wrong
3266 * sized surface.
e30e8f75
GP
3267 */
3268
e30e8f75 3269 I915_WRITE(PIPESRC(crtc->pipe),
bfd16b2a
ML
3270 ((pipe_config->pipe_src_w - 1) << 16) |
3271 (pipe_config->pipe_src_h - 1));
3272
3273 /* on skylake this is done by detaching scalers */
3274 if (INTEL_INFO(dev)->gen >= 9) {
3275 skl_detach_scalers(crtc);
3276
3277 if (pipe_config->pch_pfit.enabled)
3278 skylake_pfit_enable(crtc);
3279 } else if (HAS_PCH_SPLIT(dev)) {
3280 if (pipe_config->pch_pfit.enabled)
3281 ironlake_pfit_enable(crtc);
3282 else if (old_crtc_state->pch_pfit.enabled)
3283 ironlake_pfit_disable(crtc, true);
e30e8f75 3284 }
e30e8f75
GP
3285}
3286
5e84e1a4
ZW
3287static void intel_fdi_normal_train(struct drm_crtc *crtc)
3288{
3289 struct drm_device *dev = crtc->dev;
3290 struct drm_i915_private *dev_priv = dev->dev_private;
3291 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3292 int pipe = intel_crtc->pipe;
f0f59a00
VS
3293 i915_reg_t reg;
3294 u32 temp;
5e84e1a4
ZW
3295
3296 /* enable normal train */
3297 reg = FDI_TX_CTL(pipe);
3298 temp = I915_READ(reg);
61e499bf 3299 if (IS_IVYBRIDGE(dev)) {
357555c0
JB
3300 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3301 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
3302 } else {
3303 temp &= ~FDI_LINK_TRAIN_NONE;
3304 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 3305 }
5e84e1a4
ZW
3306 I915_WRITE(reg, temp);
3307
3308 reg = FDI_RX_CTL(pipe);
3309 temp = I915_READ(reg);
3310 if (HAS_PCH_CPT(dev)) {
3311 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3312 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3313 } else {
3314 temp &= ~FDI_LINK_TRAIN_NONE;
3315 temp |= FDI_LINK_TRAIN_NONE;
3316 }
3317 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3318
3319 /* wait one idle pattern time */
3320 POSTING_READ(reg);
3321 udelay(1000);
357555c0
JB
3322
3323 /* IVB wants error correction enabled */
3324 if (IS_IVYBRIDGE(dev))
3325 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3326 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
3327}
3328
8db9d77b
ZW
3329/* The FDI link training functions for ILK/Ibexpeak. */
3330static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3331{
3332 struct drm_device *dev = crtc->dev;
3333 struct drm_i915_private *dev_priv = dev->dev_private;
3334 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3335 int pipe = intel_crtc->pipe;
f0f59a00
VS
3336 i915_reg_t reg;
3337 u32 temp, tries;
8db9d77b 3338
1c8562f6 3339 /* FDI needs bits from pipe first */
0fc932b8 3340 assert_pipe_enabled(dev_priv, pipe);
0fc932b8 3341
e1a44743
AJ
3342 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3343 for train result */
5eddb70b
CW
3344 reg = FDI_RX_IMR(pipe);
3345 temp = I915_READ(reg);
e1a44743
AJ
3346 temp &= ~FDI_RX_SYMBOL_LOCK;
3347 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3348 I915_WRITE(reg, temp);
3349 I915_READ(reg);
e1a44743
AJ
3350 udelay(150);
3351
8db9d77b 3352 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3353 reg = FDI_TX_CTL(pipe);
3354 temp = I915_READ(reg);
627eb5a3 3355 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3356 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3357 temp &= ~FDI_LINK_TRAIN_NONE;
3358 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 3359 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3360
5eddb70b
CW
3361 reg = FDI_RX_CTL(pipe);
3362 temp = I915_READ(reg);
8db9d77b
ZW
3363 temp &= ~FDI_LINK_TRAIN_NONE;
3364 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b
CW
3365 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3366
3367 POSTING_READ(reg);
8db9d77b
ZW
3368 udelay(150);
3369
5b2adf89 3370 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
DV
3371 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3372 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3373 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 3374
5eddb70b 3375 reg = FDI_RX_IIR(pipe);
e1a44743 3376 for (tries = 0; tries < 5; tries++) {
5eddb70b 3377 temp = I915_READ(reg);
8db9d77b
ZW
3378 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3379
3380 if ((temp & FDI_RX_BIT_LOCK)) {
3381 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 3382 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
3383 break;
3384 }
8db9d77b 3385 }
e1a44743 3386 if (tries == 5)
5eddb70b 3387 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3388
3389 /* Train 2 */
5eddb70b
CW
3390 reg = FDI_TX_CTL(pipe);
3391 temp = I915_READ(reg);
8db9d77b
ZW
3392 temp &= ~FDI_LINK_TRAIN_NONE;
3393 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3394 I915_WRITE(reg, temp);
8db9d77b 3395
5eddb70b
CW
3396 reg = FDI_RX_CTL(pipe);
3397 temp = I915_READ(reg);
8db9d77b
ZW
3398 temp &= ~FDI_LINK_TRAIN_NONE;
3399 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3400 I915_WRITE(reg, temp);
8db9d77b 3401
5eddb70b
CW
3402 POSTING_READ(reg);
3403 udelay(150);
8db9d77b 3404
5eddb70b 3405 reg = FDI_RX_IIR(pipe);
e1a44743 3406 for (tries = 0; tries < 5; tries++) {
5eddb70b 3407 temp = I915_READ(reg);
8db9d77b
ZW
3408 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3409
3410 if (temp & FDI_RX_SYMBOL_LOCK) {
5eddb70b 3411 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
3412 DRM_DEBUG_KMS("FDI train 2 done.\n");
3413 break;
3414 }
8db9d77b 3415 }
e1a44743 3416 if (tries == 5)
5eddb70b 3417 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3418
3419 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 3420
8db9d77b
ZW
3421}
3422
0206e353 3423static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
3424 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3425 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3426 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3427 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3428};
3429
3430/* The FDI link training functions for SNB/Cougarpoint. */
3431static void gen6_fdi_link_train(struct drm_crtc *crtc)
3432{
3433 struct drm_device *dev = crtc->dev;
3434 struct drm_i915_private *dev_priv = dev->dev_private;
3435 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3436 int pipe = intel_crtc->pipe;
f0f59a00
VS
3437 i915_reg_t reg;
3438 u32 temp, i, retry;
8db9d77b 3439
e1a44743
AJ
3440 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3441 for train result */
5eddb70b
CW
3442 reg = FDI_RX_IMR(pipe);
3443 temp = I915_READ(reg);
e1a44743
AJ
3444 temp &= ~FDI_RX_SYMBOL_LOCK;
3445 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3446 I915_WRITE(reg, temp);
3447
3448 POSTING_READ(reg);
e1a44743
AJ
3449 udelay(150);
3450
8db9d77b 3451 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3452 reg = FDI_TX_CTL(pipe);
3453 temp = I915_READ(reg);
627eb5a3 3454 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3455 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3456 temp &= ~FDI_LINK_TRAIN_NONE;
3457 temp |= FDI_LINK_TRAIN_PATTERN_1;
3458 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3459 /* SNB-B */
3460 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 3461 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3462
d74cf324
DV
3463 I915_WRITE(FDI_RX_MISC(pipe),
3464 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3465
5eddb70b
CW
3466 reg = FDI_RX_CTL(pipe);
3467 temp = I915_READ(reg);
8db9d77b
ZW
3468 if (HAS_PCH_CPT(dev)) {
3469 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3470 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3471 } else {
3472 temp &= ~FDI_LINK_TRAIN_NONE;
3473 temp |= FDI_LINK_TRAIN_PATTERN_1;
3474 }
5eddb70b
CW
3475 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3476
3477 POSTING_READ(reg);
8db9d77b
ZW
3478 udelay(150);
3479
0206e353 3480 for (i = 0; i < 4; i++) {
5eddb70b
CW
3481 reg = FDI_TX_CTL(pipe);
3482 temp = I915_READ(reg);
8db9d77b
ZW
3483 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3484 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3485 I915_WRITE(reg, temp);
3486
3487 POSTING_READ(reg);
8db9d77b
ZW
3488 udelay(500);
3489
fa37d39e
SP
3490 for (retry = 0; retry < 5; retry++) {
3491 reg = FDI_RX_IIR(pipe);
3492 temp = I915_READ(reg);
3493 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3494 if (temp & FDI_RX_BIT_LOCK) {
3495 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3496 DRM_DEBUG_KMS("FDI train 1 done.\n");
3497 break;
3498 }
3499 udelay(50);
8db9d77b 3500 }
fa37d39e
SP
3501 if (retry < 5)
3502 break;
8db9d77b
ZW
3503 }
3504 if (i == 4)
5eddb70b 3505 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3506
3507 /* Train 2 */
5eddb70b
CW
3508 reg = FDI_TX_CTL(pipe);
3509 temp = I915_READ(reg);
8db9d77b
ZW
3510 temp &= ~FDI_LINK_TRAIN_NONE;
3511 temp |= FDI_LINK_TRAIN_PATTERN_2;
3512 if (IS_GEN6(dev)) {
3513 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3514 /* SNB-B */
3515 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3516 }
5eddb70b 3517 I915_WRITE(reg, temp);
8db9d77b 3518
5eddb70b
CW
3519 reg = FDI_RX_CTL(pipe);
3520 temp = I915_READ(reg);
8db9d77b
ZW
3521 if (HAS_PCH_CPT(dev)) {
3522 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3523 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3524 } else {
3525 temp &= ~FDI_LINK_TRAIN_NONE;
3526 temp |= FDI_LINK_TRAIN_PATTERN_2;
3527 }
5eddb70b
CW
3528 I915_WRITE(reg, temp);
3529
3530 POSTING_READ(reg);
8db9d77b
ZW
3531 udelay(150);
3532
0206e353 3533 for (i = 0; i < 4; i++) {
5eddb70b
CW
3534 reg = FDI_TX_CTL(pipe);
3535 temp = I915_READ(reg);
8db9d77b
ZW
3536 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3537 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3538 I915_WRITE(reg, temp);
3539
3540 POSTING_READ(reg);
8db9d77b
ZW
3541 udelay(500);
3542
fa37d39e
SP
3543 for (retry = 0; retry < 5; retry++) {
3544 reg = FDI_RX_IIR(pipe);
3545 temp = I915_READ(reg);
3546 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3547 if (temp & FDI_RX_SYMBOL_LOCK) {
3548 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3549 DRM_DEBUG_KMS("FDI train 2 done.\n");
3550 break;
3551 }
3552 udelay(50);
8db9d77b 3553 }
fa37d39e
SP
3554 if (retry < 5)
3555 break;
8db9d77b
ZW
3556 }
3557 if (i == 4)
5eddb70b 3558 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3559
3560 DRM_DEBUG_KMS("FDI train done.\n");
3561}
3562
357555c0
JB
3563/* Manual link training for Ivy Bridge A0 parts */
3564static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3565{
3566 struct drm_device *dev = crtc->dev;
3567 struct drm_i915_private *dev_priv = dev->dev_private;
3568 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3569 int pipe = intel_crtc->pipe;
f0f59a00
VS
3570 i915_reg_t reg;
3571 u32 temp, i, j;
357555c0
JB
3572
3573 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3574 for train result */
3575 reg = FDI_RX_IMR(pipe);
3576 temp = I915_READ(reg);
3577 temp &= ~FDI_RX_SYMBOL_LOCK;
3578 temp &= ~FDI_RX_BIT_LOCK;
3579 I915_WRITE(reg, temp);
3580
3581 POSTING_READ(reg);
3582 udelay(150);
3583
01a415fd
DV
3584 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3585 I915_READ(FDI_RX_IIR(pipe)));
3586
139ccd3f
JB
3587 /* Try each vswing and preemphasis setting twice before moving on */
3588 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3589 /* disable first in case we need to retry */
3590 reg = FDI_TX_CTL(pipe);
3591 temp = I915_READ(reg);
3592 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3593 temp &= ~FDI_TX_ENABLE;
3594 I915_WRITE(reg, temp);
357555c0 3595
139ccd3f
JB
3596 reg = FDI_RX_CTL(pipe);
3597 temp = I915_READ(reg);
3598 temp &= ~FDI_LINK_TRAIN_AUTO;
3599 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3600 temp &= ~FDI_RX_ENABLE;
3601 I915_WRITE(reg, temp);
357555c0 3602
139ccd3f 3603 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
3604 reg = FDI_TX_CTL(pipe);
3605 temp = I915_READ(reg);
139ccd3f 3606 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3607 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
139ccd3f 3608 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 3609 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
3610 temp |= snb_b_fdi_train_param[j/2];
3611 temp |= FDI_COMPOSITE_SYNC;
3612 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 3613
139ccd3f
JB
3614 I915_WRITE(FDI_RX_MISC(pipe),
3615 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 3616
139ccd3f 3617 reg = FDI_RX_CTL(pipe);
357555c0 3618 temp = I915_READ(reg);
139ccd3f
JB
3619 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3620 temp |= FDI_COMPOSITE_SYNC;
3621 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 3622
139ccd3f
JB
3623 POSTING_READ(reg);
3624 udelay(1); /* should be 0.5us */
357555c0 3625
139ccd3f
JB
3626 for (i = 0; i < 4; i++) {
3627 reg = FDI_RX_IIR(pipe);
3628 temp = I915_READ(reg);
3629 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3630
139ccd3f
JB
3631 if (temp & FDI_RX_BIT_LOCK ||
3632 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3633 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3634 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3635 i);
3636 break;
3637 }
3638 udelay(1); /* should be 0.5us */
3639 }
3640 if (i == 4) {
3641 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3642 continue;
3643 }
357555c0 3644
139ccd3f 3645 /* Train 2 */
357555c0
JB
3646 reg = FDI_TX_CTL(pipe);
3647 temp = I915_READ(reg);
139ccd3f
JB
3648 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3649 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3650 I915_WRITE(reg, temp);
3651
3652 reg = FDI_RX_CTL(pipe);
3653 temp = I915_READ(reg);
3654 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3655 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
3656 I915_WRITE(reg, temp);
3657
3658 POSTING_READ(reg);
139ccd3f 3659 udelay(2); /* should be 1.5us */
357555c0 3660
139ccd3f
JB
3661 for (i = 0; i < 4; i++) {
3662 reg = FDI_RX_IIR(pipe);
3663 temp = I915_READ(reg);
3664 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3665
139ccd3f
JB
3666 if (temp & FDI_RX_SYMBOL_LOCK ||
3667 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3668 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3669 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3670 i);
3671 goto train_done;
3672 }
3673 udelay(2); /* should be 1.5us */
357555c0 3674 }
139ccd3f
JB
3675 if (i == 4)
3676 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 3677 }
357555c0 3678
139ccd3f 3679train_done:
357555c0
JB
3680 DRM_DEBUG_KMS("FDI train done.\n");
3681}
3682
88cefb6c 3683static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 3684{
88cefb6c 3685 struct drm_device *dev = intel_crtc->base.dev;
2c07245f 3686 struct drm_i915_private *dev_priv = dev->dev_private;
2c07245f 3687 int pipe = intel_crtc->pipe;
f0f59a00
VS
3688 i915_reg_t reg;
3689 u32 temp;
c64e311e 3690
c98e9dcf 3691 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
3692 reg = FDI_RX_CTL(pipe);
3693 temp = I915_READ(reg);
627eb5a3 3694 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
6e3c9717 3695 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
dfd07d72 3696 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
3697 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3698
3699 POSTING_READ(reg);
c98e9dcf
JB
3700 udelay(200);
3701
3702 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
3703 temp = I915_READ(reg);
3704 I915_WRITE(reg, temp | FDI_PCDCLK);
3705
3706 POSTING_READ(reg);
c98e9dcf
JB
3707 udelay(200);
3708
20749730
PZ
3709 /* Enable CPU FDI TX PLL, always on for Ironlake */
3710 reg = FDI_TX_CTL(pipe);
3711 temp = I915_READ(reg);
3712 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3713 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 3714
20749730
PZ
3715 POSTING_READ(reg);
3716 udelay(100);
6be4a607 3717 }
0e23b99d
JB
3718}
3719
88cefb6c
DV
3720static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3721{
3722 struct drm_device *dev = intel_crtc->base.dev;
3723 struct drm_i915_private *dev_priv = dev->dev_private;
3724 int pipe = intel_crtc->pipe;
f0f59a00
VS
3725 i915_reg_t reg;
3726 u32 temp;
88cefb6c
DV
3727
3728 /* Switch from PCDclk to Rawclk */
3729 reg = FDI_RX_CTL(pipe);
3730 temp = I915_READ(reg);
3731 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3732
3733 /* Disable CPU FDI TX PLL */
3734 reg = FDI_TX_CTL(pipe);
3735 temp = I915_READ(reg);
3736 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3737
3738 POSTING_READ(reg);
3739 udelay(100);
3740
3741 reg = FDI_RX_CTL(pipe);
3742 temp = I915_READ(reg);
3743 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3744
3745 /* Wait for the clocks to turn off. */
3746 POSTING_READ(reg);
3747 udelay(100);
3748}
3749
0fc932b8
JB
3750static void ironlake_fdi_disable(struct drm_crtc *crtc)
3751{
3752 struct drm_device *dev = crtc->dev;
3753 struct drm_i915_private *dev_priv = dev->dev_private;
3754 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3755 int pipe = intel_crtc->pipe;
f0f59a00
VS
3756 i915_reg_t reg;
3757 u32 temp;
0fc932b8
JB
3758
3759 /* disable CPU FDI tx and PCH FDI rx */
3760 reg = FDI_TX_CTL(pipe);
3761 temp = I915_READ(reg);
3762 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3763 POSTING_READ(reg);
3764
3765 reg = FDI_RX_CTL(pipe);
3766 temp = I915_READ(reg);
3767 temp &= ~(0x7 << 16);
dfd07d72 3768 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3769 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3770
3771 POSTING_READ(reg);
3772 udelay(100);
3773
3774 /* Ironlake workaround, disable clock pointer after downing FDI */
eba905b2 3775 if (HAS_PCH_IBX(dev))
6f06ce18 3776 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
0fc932b8
JB
3777
3778 /* still set train pattern 1 */
3779 reg = FDI_TX_CTL(pipe);
3780 temp = I915_READ(reg);
3781 temp &= ~FDI_LINK_TRAIN_NONE;
3782 temp |= FDI_LINK_TRAIN_PATTERN_1;
3783 I915_WRITE(reg, temp);
3784
3785 reg = FDI_RX_CTL(pipe);
3786 temp = I915_READ(reg);
3787 if (HAS_PCH_CPT(dev)) {
3788 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3789 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3790 } else {
3791 temp &= ~FDI_LINK_TRAIN_NONE;
3792 temp |= FDI_LINK_TRAIN_PATTERN_1;
3793 }
3794 /* BPC in FDI rx is consistent with that in PIPECONF */
3795 temp &= ~(0x07 << 16);
dfd07d72 3796 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3797 I915_WRITE(reg, temp);
3798
3799 POSTING_READ(reg);
3800 udelay(100);
3801}
3802
5dce5b93
CW
3803bool intel_has_pending_fb_unpin(struct drm_device *dev)
3804{
3805 struct intel_crtc *crtc;
3806
3807 /* Note that we don't need to be called with mode_config.lock here
3808 * as our list of CRTC objects is static for the lifetime of the
3809 * device and so cannot disappear as we iterate. Similarly, we can
3810 * happily treat the predicates as racy, atomic checks as userspace
3811 * cannot claim and pin a new fb without at least acquring the
3812 * struct_mutex and so serialising with us.
3813 */
d3fcc808 3814 for_each_intel_crtc(dev, crtc) {
5dce5b93
CW
3815 if (atomic_read(&crtc->unpin_work_count) == 0)
3816 continue;
3817
3818 if (crtc->unpin_work)
3819 intel_wait_for_vblank(dev, crtc->pipe);
3820
3821 return true;
3822 }
3823
3824 return false;
3825}
3826
d6bbafa1
CW
3827static void page_flip_completed(struct intel_crtc *intel_crtc)
3828{
3829 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3830 struct intel_unpin_work *work = intel_crtc->unpin_work;
3831
3832 /* ensure that the unpin work is consistent wrt ->pending. */
3833 smp_rmb();
3834 intel_crtc->unpin_work = NULL;
3835
3836 if (work->event)
3837 drm_send_vblank_event(intel_crtc->base.dev,
3838 intel_crtc->pipe,
3839 work->event);
3840
3841 drm_crtc_vblank_put(&intel_crtc->base);
3842
3843 wake_up_all(&dev_priv->pending_flip_queue);
3844 queue_work(dev_priv->wq, &work->work);
3845
3846 trace_i915_flip_complete(intel_crtc->plane,
3847 work->pending_flip_obj);
3848}
3849
5008e874 3850static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
e6c3a2a6 3851{
0f91128d 3852 struct drm_device *dev = crtc->dev;
5bb61643 3853 struct drm_i915_private *dev_priv = dev->dev_private;
5008e874 3854 long ret;
e6c3a2a6 3855
2c10d571 3856 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
5008e874
ML
3857
3858 ret = wait_event_interruptible_timeout(
3859 dev_priv->pending_flip_queue,
3860 !intel_crtc_has_pending_flip(crtc),
3861 60*HZ);
3862
3863 if (ret < 0)
3864 return ret;
3865
3866 if (ret == 0) {
9c787942 3867 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2c10d571 3868
5e2d7afc 3869 spin_lock_irq(&dev->event_lock);
9c787942
CW
3870 if (intel_crtc->unpin_work) {
3871 WARN_ONCE(1, "Removing stuck page flip\n");
3872 page_flip_completed(intel_crtc);
3873 }
5e2d7afc 3874 spin_unlock_irq(&dev->event_lock);
9c787942 3875 }
5bb61643 3876
5008e874 3877 return 0;
e6c3a2a6
CW
3878}
3879
060f02d8
VS
3880static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
3881{
3882 u32 temp;
3883
3884 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3885
3886 mutex_lock(&dev_priv->sb_lock);
3887
3888 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3889 temp |= SBI_SSCCTL_DISABLE;
3890 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
3891
3892 mutex_unlock(&dev_priv->sb_lock);
3893}
3894
e615efe4
ED
3895/* Program iCLKIP clock to the desired frequency */
3896static void lpt_program_iclkip(struct drm_crtc *crtc)
3897{
64b46a06 3898 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6e3c9717 3899 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
e615efe4
ED
3900 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3901 u32 temp;
3902
060f02d8 3903 lpt_disable_iclkip(dev_priv);
e615efe4 3904
64b46a06
VS
3905 /* The iCLK virtual clock root frequency is in MHz,
3906 * but the adjusted_mode->crtc_clock in in KHz. To get the
3907 * divisors, it is necessary to divide one by another, so we
3908 * convert the virtual clock precision to KHz here for higher
3909 * precision.
3910 */
3911 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
e615efe4
ED
3912 u32 iclk_virtual_root_freq = 172800 * 1000;
3913 u32 iclk_pi_range = 64;
64b46a06 3914 u32 desired_divisor;
e615efe4 3915
64b46a06
VS
3916 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3917 clock << auxdiv);
3918 divsel = (desired_divisor / iclk_pi_range) - 2;
3919 phaseinc = desired_divisor % iclk_pi_range;
e615efe4 3920
64b46a06
VS
3921 /*
3922 * Near 20MHz is a corner case which is
3923 * out of range for the 7-bit divisor
3924 */
3925 if (divsel <= 0x7f)
3926 break;
e615efe4
ED
3927 }
3928
3929 /* This should not happen with any sane values */
3930 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3931 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3932 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3933 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3934
3935 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 3936 clock,
e615efe4
ED
3937 auxdiv,
3938 divsel,
3939 phasedir,
3940 phaseinc);
3941
060f02d8
VS
3942 mutex_lock(&dev_priv->sb_lock);
3943
e615efe4 3944 /* Program SSCDIVINTPHASE6 */
988d6ee8 3945 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
3946 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3947 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3948 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3949 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3950 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3951 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 3952 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
3953
3954 /* Program SSCAUXDIV */
988d6ee8 3955 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
3956 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3957 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 3958 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
3959
3960 /* Enable modulator and associated divider */
988d6ee8 3961 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 3962 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 3963 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4 3964
060f02d8
VS
3965 mutex_unlock(&dev_priv->sb_lock);
3966
e615efe4
ED
3967 /* Wait for initialization time */
3968 udelay(24);
3969
3970 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
3971}
3972
8802e5b6
VS
3973int lpt_get_iclkip(struct drm_i915_private *dev_priv)
3974{
3975 u32 divsel, phaseinc, auxdiv;
3976 u32 iclk_virtual_root_freq = 172800 * 1000;
3977 u32 iclk_pi_range = 64;
3978 u32 desired_divisor;
3979 u32 temp;
3980
3981 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
3982 return 0;
3983
3984 mutex_lock(&dev_priv->sb_lock);
3985
3986 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3987 if (temp & SBI_SSCCTL_DISABLE) {
3988 mutex_unlock(&dev_priv->sb_lock);
3989 return 0;
3990 }
3991
3992 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
3993 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
3994 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
3995 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
3996 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
3997
3998 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
3999 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
4000 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
4001
4002 mutex_unlock(&dev_priv->sb_lock);
4003
4004 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
4005
4006 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4007 desired_divisor << auxdiv);
4008}
4009
275f01b2
DV
4010static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4011 enum pipe pch_transcoder)
4012{
4013 struct drm_device *dev = crtc->base.dev;
4014 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 4015 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
275f01b2
DV
4016
4017 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4018 I915_READ(HTOTAL(cpu_transcoder)));
4019 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4020 I915_READ(HBLANK(cpu_transcoder)));
4021 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4022 I915_READ(HSYNC(cpu_transcoder)));
4023
4024 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4025 I915_READ(VTOTAL(cpu_transcoder)));
4026 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4027 I915_READ(VBLANK(cpu_transcoder)));
4028 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4029 I915_READ(VSYNC(cpu_transcoder)));
4030 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4031 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4032}
4033
003632d9 4034static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
1fbc0d78
DV
4035{
4036 struct drm_i915_private *dev_priv = dev->dev_private;
4037 uint32_t temp;
4038
4039 temp = I915_READ(SOUTH_CHICKEN1);
003632d9 4040 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
1fbc0d78
DV
4041 return;
4042
4043 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4044 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4045
003632d9
ACO
4046 temp &= ~FDI_BC_BIFURCATION_SELECT;
4047 if (enable)
4048 temp |= FDI_BC_BIFURCATION_SELECT;
4049
4050 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
1fbc0d78
DV
4051 I915_WRITE(SOUTH_CHICKEN1, temp);
4052 POSTING_READ(SOUTH_CHICKEN1);
4053}
4054
4055static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4056{
4057 struct drm_device *dev = intel_crtc->base.dev;
1fbc0d78
DV
4058
4059 switch (intel_crtc->pipe) {
4060 case PIPE_A:
4061 break;
4062 case PIPE_B:
6e3c9717 4063 if (intel_crtc->config->fdi_lanes > 2)
003632d9 4064 cpt_set_fdi_bc_bifurcation(dev, false);
1fbc0d78 4065 else
003632d9 4066 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4067
4068 break;
4069 case PIPE_C:
003632d9 4070 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4071
4072 break;
4073 default:
4074 BUG();
4075 }
4076}
4077
c48b5305
VS
4078/* Return which DP Port should be selected for Transcoder DP control */
4079static enum port
4080intel_trans_dp_port_sel(struct drm_crtc *crtc)
4081{
4082 struct drm_device *dev = crtc->dev;
4083 struct intel_encoder *encoder;
4084
4085 for_each_encoder_on_crtc(dev, crtc, encoder) {
4086 if (encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
4087 encoder->type == INTEL_OUTPUT_EDP)
4088 return enc_to_dig_port(&encoder->base)->port;
4089 }
4090
4091 return -1;
4092}
4093
f67a559d
JB
4094/*
4095 * Enable PCH resources required for PCH ports:
4096 * - PCH PLLs
4097 * - FDI training & RX/TX
4098 * - update transcoder timings
4099 * - DP transcoding bits
4100 * - transcoder
4101 */
4102static void ironlake_pch_enable(struct drm_crtc *crtc)
0e23b99d
JB
4103{
4104 struct drm_device *dev = crtc->dev;
4105 struct drm_i915_private *dev_priv = dev->dev_private;
4106 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4107 int pipe = intel_crtc->pipe;
f0f59a00 4108 u32 temp;
2c07245f 4109
ab9412ba 4110 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 4111
1fbc0d78
DV
4112 if (IS_IVYBRIDGE(dev))
4113 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4114
cd986abb
DV
4115 /* Write the TU size bits before fdi link training, so that error
4116 * detection works. */
4117 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4118 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4119
3860b2ec
VS
4120 /*
4121 * Sometimes spurious CPU pipe underruns happen during FDI
4122 * training, at least with VGA+HDMI cloning. Suppress them.
4123 */
4124 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4125
c98e9dcf 4126 /* For PCH output, training FDI link */
674cf967 4127 dev_priv->display.fdi_link_train(crtc);
2c07245f 4128
3ad8a208
DV
4129 /* We need to program the right clock selection before writing the pixel
4130 * mutliplier into the DPLL. */
303b81e0 4131 if (HAS_PCH_CPT(dev)) {
ee7b9f93 4132 u32 sel;
4b645f14 4133
c98e9dcf 4134 temp = I915_READ(PCH_DPLL_SEL);
11887397
DV
4135 temp |= TRANS_DPLL_ENABLE(pipe);
4136 sel = TRANS_DPLLB_SEL(pipe);
8106ddbd
ACO
4137 if (intel_crtc->config->shared_dpll ==
4138 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
ee7b9f93
JB
4139 temp |= sel;
4140 else
4141 temp &= ~sel;
c98e9dcf 4142 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 4143 }
5eddb70b 4144
3ad8a208
DV
4145 /* XXX: pch pll's can be enabled any time before we enable the PCH
4146 * transcoder, and we actually should do this to not upset any PCH
4147 * transcoder that already use the clock when we share it.
4148 *
4149 * Note that enable_shared_dpll tries to do the right thing, but
4150 * get_shared_dpll unconditionally resets the pll - we need that to have
4151 * the right LVDS enable sequence. */
85b3894f 4152 intel_enable_shared_dpll(intel_crtc);
3ad8a208 4153
d9b6cb56
JB
4154 /* set transcoder timing, panel must allow it */
4155 assert_panel_unlocked(dev_priv, pipe);
275f01b2 4156 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
8db9d77b 4157
303b81e0 4158 intel_fdi_normal_train(crtc);
5e84e1a4 4159
3860b2ec
VS
4160 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4161
c98e9dcf 4162 /* For PCH DP, enable TRANS_DP_CTL */
6e3c9717 4163 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
9c4edaee
VS
4164 const struct drm_display_mode *adjusted_mode =
4165 &intel_crtc->config->base.adjusted_mode;
dfd07d72 4166 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
f0f59a00 4167 i915_reg_t reg = TRANS_DP_CTL(pipe);
5eddb70b
CW
4168 temp = I915_READ(reg);
4169 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
4170 TRANS_DP_SYNC_MASK |
4171 TRANS_DP_BPC_MASK);
e3ef4479 4172 temp |= TRANS_DP_OUTPUT_ENABLE;
9325c9f0 4173 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf 4174
9c4edaee 4175 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 4176 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
9c4edaee 4177 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 4178 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf
JB
4179
4180 switch (intel_trans_dp_port_sel(crtc)) {
c48b5305 4181 case PORT_B:
5eddb70b 4182 temp |= TRANS_DP_PORT_SEL_B;
c98e9dcf 4183 break;
c48b5305 4184 case PORT_C:
5eddb70b 4185 temp |= TRANS_DP_PORT_SEL_C;
c98e9dcf 4186 break;
c48b5305 4187 case PORT_D:
5eddb70b 4188 temp |= TRANS_DP_PORT_SEL_D;
c98e9dcf
JB
4189 break;
4190 default:
e95d41e1 4191 BUG();
32f9d658 4192 }
2c07245f 4193
5eddb70b 4194 I915_WRITE(reg, temp);
6be4a607 4195 }
b52eb4dc 4196
b8a4f404 4197 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
4198}
4199
1507e5bd
PZ
4200static void lpt_pch_enable(struct drm_crtc *crtc)
4201{
4202 struct drm_device *dev = crtc->dev;
4203 struct drm_i915_private *dev_priv = dev->dev_private;
4204 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 4205 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
1507e5bd 4206
ab9412ba 4207 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
1507e5bd 4208
8c52b5e8 4209 lpt_program_iclkip(crtc);
1507e5bd 4210
0540e488 4211 /* Set transcoder timing. */
275f01b2 4212 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
1507e5bd 4213
937bb610 4214 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
4215}
4216
a1520318 4217static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57
JB
4218{
4219 struct drm_i915_private *dev_priv = dev->dev_private;
f0f59a00 4220 i915_reg_t dslreg = PIPEDSL(pipe);
d4270e57
JB
4221 u32 temp;
4222
4223 temp = I915_READ(dslreg);
4224 udelay(500);
4225 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 4226 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 4227 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
4228 }
4229}
4230
86adf9d7
ML
4231static int
4232skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4233 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4234 int src_w, int src_h, int dst_w, int dst_h)
a1b2278e 4235{
86adf9d7
ML
4236 struct intel_crtc_scaler_state *scaler_state =
4237 &crtc_state->scaler_state;
4238 struct intel_crtc *intel_crtc =
4239 to_intel_crtc(crtc_state->base.crtc);
a1b2278e 4240 int need_scaling;
6156a456
CK
4241
4242 need_scaling = intel_rotation_90_or_270(rotation) ?
4243 (src_h != dst_w || src_w != dst_h):
4244 (src_w != dst_w || src_h != dst_h);
a1b2278e
CK
4245
4246 /*
4247 * if plane is being disabled or scaler is no more required or force detach
4248 * - free scaler binded to this plane/crtc
4249 * - in order to do this, update crtc->scaler_usage
4250 *
4251 * Here scaler state in crtc_state is set free so that
4252 * scaler can be assigned to other user. Actual register
4253 * update to free the scaler is done in plane/panel-fit programming.
4254 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4255 */
86adf9d7 4256 if (force_detach || !need_scaling) {
a1b2278e 4257 if (*scaler_id >= 0) {
86adf9d7 4258 scaler_state->scaler_users &= ~(1 << scaler_user);
a1b2278e
CK
4259 scaler_state->scalers[*scaler_id].in_use = 0;
4260
86adf9d7
ML
4261 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4262 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4263 intel_crtc->pipe, scaler_user, *scaler_id,
a1b2278e
CK
4264 scaler_state->scaler_users);
4265 *scaler_id = -1;
4266 }
4267 return 0;
4268 }
4269
4270 /* range checks */
4271 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4272 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4273
4274 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4275 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
86adf9d7 4276 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
a1b2278e 4277 "size is out of scaler range\n",
86adf9d7 4278 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
a1b2278e
CK
4279 return -EINVAL;
4280 }
4281
86adf9d7
ML
4282 /* mark this plane as a scaler user in crtc_state */
4283 scaler_state->scaler_users |= (1 << scaler_user);
4284 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4285 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4286 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4287 scaler_state->scaler_users);
4288
4289 return 0;
4290}
4291
4292/**
4293 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4294 *
4295 * @state: crtc's scaler state
86adf9d7
ML
4296 *
4297 * Return
4298 * 0 - scaler_usage updated successfully
4299 * error - requested scaling cannot be supported or other error condition
4300 */
e435d6e5 4301int skl_update_scaler_crtc(struct intel_crtc_state *state)
86adf9d7
ML
4302{
4303 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
7c5f93b0 4304 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
86adf9d7
ML
4305
4306 DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
4307 intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
4308
e435d6e5 4309 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
fa5a7970 4310 &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
86adf9d7 4311 state->pipe_src_w, state->pipe_src_h,
aad941d5 4312 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
86adf9d7
ML
4313}
4314
4315/**
4316 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4317 *
4318 * @state: crtc's scaler state
86adf9d7
ML
4319 * @plane_state: atomic plane state to update
4320 *
4321 * Return
4322 * 0 - scaler_usage updated successfully
4323 * error - requested scaling cannot be supported or other error condition
4324 */
da20eabd
ML
4325static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4326 struct intel_plane_state *plane_state)
86adf9d7
ML
4327{
4328
4329 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
da20eabd
ML
4330 struct intel_plane *intel_plane =
4331 to_intel_plane(plane_state->base.plane);
86adf9d7
ML
4332 struct drm_framebuffer *fb = plane_state->base.fb;
4333 int ret;
4334
4335 bool force_detach = !fb || !plane_state->visible;
4336
4337 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
4338 intel_plane->base.base.id, intel_crtc->pipe,
4339 drm_plane_index(&intel_plane->base));
4340
4341 ret = skl_update_scaler(crtc_state, force_detach,
4342 drm_plane_index(&intel_plane->base),
4343 &plane_state->scaler_id,
4344 plane_state->base.rotation,
4345 drm_rect_width(&plane_state->src) >> 16,
4346 drm_rect_height(&plane_state->src) >> 16,
4347 drm_rect_width(&plane_state->dst),
4348 drm_rect_height(&plane_state->dst));
4349
4350 if (ret || plane_state->scaler_id < 0)
4351 return ret;
4352
a1b2278e 4353 /* check colorkey */
818ed961 4354 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
86adf9d7 4355 DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
818ed961 4356 intel_plane->base.base.id);
a1b2278e
CK
4357 return -EINVAL;
4358 }
4359
4360 /* Check src format */
86adf9d7
ML
4361 switch (fb->pixel_format) {
4362 case DRM_FORMAT_RGB565:
4363 case DRM_FORMAT_XBGR8888:
4364 case DRM_FORMAT_XRGB8888:
4365 case DRM_FORMAT_ABGR8888:
4366 case DRM_FORMAT_ARGB8888:
4367 case DRM_FORMAT_XRGB2101010:
4368 case DRM_FORMAT_XBGR2101010:
4369 case DRM_FORMAT_YUYV:
4370 case DRM_FORMAT_YVYU:
4371 case DRM_FORMAT_UYVY:
4372 case DRM_FORMAT_VYUY:
4373 break;
4374 default:
4375 DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
4376 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4377 return -EINVAL;
a1b2278e
CK
4378 }
4379
a1b2278e
CK
4380 return 0;
4381}
4382
e435d6e5
ML
4383static void skylake_scaler_disable(struct intel_crtc *crtc)
4384{
4385 int i;
4386
4387 for (i = 0; i < crtc->num_scalers; i++)
4388 skl_detach_scaler(crtc, i);
4389}
4390
4391static void skylake_pfit_enable(struct intel_crtc *crtc)
bd2e244f
JB
4392{
4393 struct drm_device *dev = crtc->base.dev;
4394 struct drm_i915_private *dev_priv = dev->dev_private;
4395 int pipe = crtc->pipe;
a1b2278e
CK
4396 struct intel_crtc_scaler_state *scaler_state =
4397 &crtc->config->scaler_state;
4398
4399 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4400
6e3c9717 4401 if (crtc->config->pch_pfit.enabled) {
a1b2278e
CK
4402 int id;
4403
4404 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4405 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4406 return;
4407 }
4408
4409 id = scaler_state->scaler_id;
4410 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4411 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4412 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4413 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4414
4415 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
bd2e244f
JB
4416 }
4417}
4418
b074cec8
JB
4419static void ironlake_pfit_enable(struct intel_crtc *crtc)
4420{
4421 struct drm_device *dev = crtc->base.dev;
4422 struct drm_i915_private *dev_priv = dev->dev_private;
4423 int pipe = crtc->pipe;
4424
6e3c9717 4425 if (crtc->config->pch_pfit.enabled) {
b074cec8
JB
4426 /* Force use of hard-coded filter coefficients
4427 * as some pre-programmed values are broken,
4428 * e.g. x201.
4429 */
4430 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4431 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4432 PF_PIPE_SEL_IVB(pipe));
4433 else
4434 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
6e3c9717
ACO
4435 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4436 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
d4270e57
JB
4437 }
4438}
4439
20bc8673 4440void hsw_enable_ips(struct intel_crtc *crtc)
d77e4531 4441{
cea165c3
VS
4442 struct drm_device *dev = crtc->base.dev;
4443 struct drm_i915_private *dev_priv = dev->dev_private;
d77e4531 4444
6e3c9717 4445 if (!crtc->config->ips_enabled)
d77e4531
PZ
4446 return;
4447
cea165c3
VS
4448 /* We can only enable IPS after we enable a plane and wait for a vblank */
4449 intel_wait_for_vblank(dev, crtc->pipe);
4450
d77e4531 4451 assert_plane_enabled(dev_priv, crtc->plane);
cea165c3 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, 0xc0000000));
4455 mutex_unlock(&dev_priv->rps.hw_lock);
4456 /* Quoting Art Runyan: "its not safe to expect any particular
4457 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
4458 * mailbox." Moreover, the mailbox may return a bogus state,
4459 * so we need to just enable it and continue on.
2a114cc1
BW
4460 */
4461 } else {
4462 I915_WRITE(IPS_CTL, IPS_ENABLE);
4463 /* The bit only becomes 1 in the next vblank, so this wait here
4464 * is essentially intel_wait_for_vblank. If we don't have this
4465 * and don't wait for vblanks until the end of crtc_enable, then
4466 * the HW state readout code will complain that the expected
4467 * IPS_CTL value is not the one we read. */
4468 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4469 DRM_ERROR("Timed out waiting for IPS enable\n");
4470 }
d77e4531
PZ
4471}
4472
20bc8673 4473void hsw_disable_ips(struct intel_crtc *crtc)
d77e4531
PZ
4474{
4475 struct drm_device *dev = crtc->base.dev;
4476 struct drm_i915_private *dev_priv = dev->dev_private;
4477
6e3c9717 4478 if (!crtc->config->ips_enabled)
d77e4531
PZ
4479 return;
4480
4481 assert_plane_enabled(dev_priv, crtc->plane);
23d0b130 4482 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4483 mutex_lock(&dev_priv->rps.hw_lock);
4484 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4485 mutex_unlock(&dev_priv->rps.hw_lock);
23d0b130
BW
4486 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4487 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4488 DRM_ERROR("Timed out waiting for IPS disable\n");
e59150dc 4489 } else {
2a114cc1 4490 I915_WRITE(IPS_CTL, 0);
e59150dc
JB
4491 POSTING_READ(IPS_CTL);
4492 }
d77e4531
PZ
4493
4494 /* We need to wait for a vblank before we can disable the plane. */
4495 intel_wait_for_vblank(dev, crtc->pipe);
4496}
4497
7cac945f 4498static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
d3eedb1a 4499{
7cac945f 4500 if (intel_crtc->overlay) {
d3eedb1a
VS
4501 struct drm_device *dev = intel_crtc->base.dev;
4502 struct drm_i915_private *dev_priv = dev->dev_private;
4503
4504 mutex_lock(&dev->struct_mutex);
4505 dev_priv->mm.interruptible = false;
4506 (void) intel_overlay_switch_off(intel_crtc->overlay);
4507 dev_priv->mm.interruptible = true;
4508 mutex_unlock(&dev->struct_mutex);
4509 }
4510
4511 /* Let userspace switch the overlay on again. In most cases userspace
4512 * has to recompute where to put it anyway.
4513 */
4514}
4515
87d4300a
ML
4516/**
4517 * intel_post_enable_primary - Perform operations after enabling primary plane
4518 * @crtc: the CRTC whose primary plane was just enabled
4519 *
4520 * Performs potentially sleeping operations that must be done after the primary
4521 * plane is enabled, such as updating FBC and IPS. Note that this may be
4522 * called due to an explicit primary plane update, or due to an implicit
4523 * re-enable that is caused when a sprite plane is updated to no longer
4524 * completely hide the primary plane.
4525 */
4526static void
4527intel_post_enable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4528{
4529 struct drm_device *dev = crtc->dev;
87d4300a 4530 struct drm_i915_private *dev_priv = dev->dev_private;
a5c4d7bc
VS
4531 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4532 int pipe = intel_crtc->pipe;
a5c4d7bc 4533
87d4300a
ML
4534 /*
4535 * FIXME IPS should be fine as long as one plane is
4536 * enabled, but in practice it seems to have problems
4537 * when going from primary only to sprite only and vice
4538 * versa.
4539 */
a5c4d7bc
VS
4540 hsw_enable_ips(intel_crtc);
4541
f99d7069 4542 /*
87d4300a
ML
4543 * Gen2 reports pipe underruns whenever all planes are disabled.
4544 * So don't enable underrun reporting before at least some planes
4545 * are enabled.
4546 * FIXME: Need to fix the logic to work when we turn off all planes
4547 * but leave the pipe running.
f99d7069 4548 */
87d4300a
ML
4549 if (IS_GEN2(dev))
4550 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4551
aca7b684
VS
4552 /* Underruns don't always raise interrupts, so check manually. */
4553 intel_check_cpu_fifo_underruns(dev_priv);
4554 intel_check_pch_fifo_underruns(dev_priv);
a5c4d7bc
VS
4555}
4556
2622a081 4557/* FIXME move all this to pre_plane_update() with proper state tracking */
87d4300a
ML
4558static void
4559intel_pre_disable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4560{
4561 struct drm_device *dev = crtc->dev;
4562 struct drm_i915_private *dev_priv = dev->dev_private;
4563 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4564 int pipe = intel_crtc->pipe;
a5c4d7bc 4565
87d4300a
ML
4566 /*
4567 * Gen2 reports pipe underruns whenever all planes are disabled.
4568 * So diasble underrun reporting before all the planes get disabled.
4569 * FIXME: Need to fix the logic to work when we turn off all planes
4570 * but leave the pipe running.
4571 */
4572 if (IS_GEN2(dev))
4573 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
a5c4d7bc 4574
2622a081
VS
4575 /*
4576 * FIXME IPS should be fine as long as one plane is
4577 * enabled, but in practice it seems to have problems
4578 * when going from primary only to sprite only and vice
4579 * versa.
4580 */
4581 hsw_disable_ips(intel_crtc);
4582}
4583
4584/* FIXME get rid of this and use pre_plane_update */
4585static void
4586intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
4587{
4588 struct drm_device *dev = crtc->dev;
4589 struct drm_i915_private *dev_priv = dev->dev_private;
4590 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4591 int pipe = intel_crtc->pipe;
4592
4593 intel_pre_disable_primary(crtc);
4594
87d4300a
ML
4595 /*
4596 * Vblank time updates from the shadow to live plane control register
4597 * are blocked if the memory self-refresh mode is active at that
4598 * moment. So to make sure the plane gets truly disabled, disable
4599 * first the self-refresh mode. The self-refresh enable bit in turn
4600 * will be checked/applied by the HW only at the next frame start
4601 * event which is after the vblank start event, so we need to have a
4602 * wait-for-vblank between disabling the plane and the pipe.
4603 */
262cd2e1 4604 if (HAS_GMCH_DISPLAY(dev)) {
87d4300a 4605 intel_set_memory_cxsr(dev_priv, false);
262cd2e1
VS
4606 dev_priv->wm.vlv.cxsr = false;
4607 intel_wait_for_vblank(dev, pipe);
4608 }
87d4300a
ML
4609}
4610
cd202f69 4611static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
ac21b225 4612{
cd202f69
ML
4613 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
4614 struct drm_atomic_state *old_state = old_crtc_state->base.state;
92826fcd
ML
4615 struct intel_crtc_state *pipe_config =
4616 to_intel_crtc_state(crtc->base.state);
ac21b225 4617 struct drm_device *dev = crtc->base.dev;
cd202f69
ML
4618 struct drm_plane *primary = crtc->base.primary;
4619 struct drm_plane_state *old_pri_state =
4620 drm_atomic_get_existing_plane_state(old_state, primary);
ac21b225 4621
cd202f69 4622 intel_frontbuffer_flip(dev, pipe_config->fb_bits);
ac21b225 4623
ab1d3a0e 4624 crtc->wm.cxsr_allowed = true;
852eb00d 4625
caed361d 4626 if (pipe_config->update_wm_post && pipe_config->base.active)
f015c551
VS
4627 intel_update_watermarks(&crtc->base);
4628
cd202f69
ML
4629 if (old_pri_state) {
4630 struct intel_plane_state *primary_state =
4631 to_intel_plane_state(primary->state);
4632 struct intel_plane_state *old_primary_state =
4633 to_intel_plane_state(old_pri_state);
4634
31ae71fc
ML
4635 intel_fbc_post_update(crtc);
4636
cd202f69
ML
4637 if (primary_state->visible &&
4638 (needs_modeset(&pipe_config->base) ||
4639 !old_primary_state->visible))
4640 intel_post_enable_primary(&crtc->base);
4641 }
ac21b225
ML
4642}
4643
5c74cd73 4644static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
ac21b225 4645{
5c74cd73 4646 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
ac21b225 4647 struct drm_device *dev = crtc->base.dev;
eddfcbcd 4648 struct drm_i915_private *dev_priv = dev->dev_private;
ab1d3a0e
ML
4649 struct intel_crtc_state *pipe_config =
4650 to_intel_crtc_state(crtc->base.state);
5c74cd73
ML
4651 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4652 struct drm_plane *primary = crtc->base.primary;
4653 struct drm_plane_state *old_pri_state =
4654 drm_atomic_get_existing_plane_state(old_state, primary);
4655 bool modeset = needs_modeset(&pipe_config->base);
ac21b225 4656
5c74cd73
ML
4657 if (old_pri_state) {
4658 struct intel_plane_state *primary_state =
4659 to_intel_plane_state(primary->state);
4660 struct intel_plane_state *old_primary_state =
4661 to_intel_plane_state(old_pri_state);
4662
31ae71fc
ML
4663 intel_fbc_pre_update(crtc);
4664
5c74cd73
ML
4665 if (old_primary_state->visible &&
4666 (modeset || !primary_state->visible))
4667 intel_pre_disable_primary(&crtc->base);
4668 }
852eb00d 4669
ab1d3a0e 4670 if (pipe_config->disable_cxsr) {
852eb00d 4671 crtc->wm.cxsr_allowed = false;
2dfd178d 4672
2622a081
VS
4673 /*
4674 * Vblank time updates from the shadow to live plane control register
4675 * are blocked if the memory self-refresh mode is active at that
4676 * moment. So to make sure the plane gets truly disabled, disable
4677 * first the self-refresh mode. The self-refresh enable bit in turn
4678 * will be checked/applied by the HW only at the next frame start
4679 * event which is after the vblank start event, so we need to have a
4680 * wait-for-vblank between disabling the plane and the pipe.
4681 */
4682 if (old_crtc_state->base.active) {
2dfd178d 4683 intel_set_memory_cxsr(dev_priv, false);
2622a081
VS
4684 dev_priv->wm.vlv.cxsr = false;
4685 intel_wait_for_vblank(dev, crtc->pipe);
4686 }
852eb00d 4687 }
92826fcd 4688
ed4a6a7c
MR
4689 /*
4690 * IVB workaround: must disable low power watermarks for at least
4691 * one frame before enabling scaling. LP watermarks can be re-enabled
4692 * when scaling is disabled.
4693 *
4694 * WaCxSRDisabledForSpriteScaling:ivb
4695 */
4696 if (pipe_config->disable_lp_wm) {
4697 ilk_disable_lp_wm(dev);
4698 intel_wait_for_vblank(dev, crtc->pipe);
4699 }
4700
4701 /*
4702 * If we're doing a modeset, we're done. No need to do any pre-vblank
4703 * watermark programming here.
4704 */
4705 if (needs_modeset(&pipe_config->base))
4706 return;
4707
4708 /*
4709 * For platforms that support atomic watermarks, program the
4710 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
4711 * will be the intermediate values that are safe for both pre- and
4712 * post- vblank; when vblank happens, the 'active' values will be set
4713 * to the final 'target' values and we'll do this again to get the
4714 * optimal watermarks. For gen9+ platforms, the values we program here
4715 * will be the final target values which will get automatically latched
4716 * at vblank time; no further programming will be necessary.
4717 *
4718 * If a platform hasn't been transitioned to atomic watermarks yet,
4719 * we'll continue to update watermarks the old way, if flags tell
4720 * us to.
4721 */
4722 if (dev_priv->display.initial_watermarks != NULL)
4723 dev_priv->display.initial_watermarks(pipe_config);
caed361d 4724 else if (pipe_config->update_wm_pre)
92826fcd 4725 intel_update_watermarks(&crtc->base);
ac21b225
ML
4726}
4727
d032ffa0 4728static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
87d4300a
ML
4729{
4730 struct drm_device *dev = crtc->dev;
4731 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
d032ffa0 4732 struct drm_plane *p;
87d4300a
ML
4733 int pipe = intel_crtc->pipe;
4734
7cac945f 4735 intel_crtc_dpms_overlay_disable(intel_crtc);
27321ae8 4736
d032ffa0
ML
4737 drm_for_each_plane_mask(p, dev, plane_mask)
4738 to_intel_plane(p)->disable_plane(p, crtc);
f98551ae 4739
f99d7069
DV
4740 /*
4741 * FIXME: Once we grow proper nuclear flip support out of this we need
4742 * to compute the mask of flip planes precisely. For the time being
4743 * consider this a flip to a NULL plane.
4744 */
4745 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
4746}
4747
f67a559d
JB
4748static void ironlake_crtc_enable(struct drm_crtc *crtc)
4749{
4750 struct drm_device *dev = crtc->dev;
4751 struct drm_i915_private *dev_priv = dev->dev_private;
4752 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4753 struct intel_encoder *encoder;
f67a559d 4754 int pipe = intel_crtc->pipe;
f67a559d 4755
53d9f4e9 4756 if (WARN_ON(intel_crtc->active))
f67a559d
JB
4757 return;
4758
81b088ca
VS
4759 if (intel_crtc->config->has_pch_encoder)
4760 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4761
6e3c9717 4762 if (intel_crtc->config->has_pch_encoder)
b14b1055
DV
4763 intel_prepare_shared_dpll(intel_crtc);
4764
6e3c9717 4765 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 4766 intel_dp_set_m_n(intel_crtc, M1_N1);
29407aab
DV
4767
4768 intel_set_pipe_timings(intel_crtc);
bc58be60 4769 intel_set_pipe_src_size(intel_crtc);
29407aab 4770
6e3c9717 4771 if (intel_crtc->config->has_pch_encoder) {
29407aab 4772 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4773 &intel_crtc->config->fdi_m_n, NULL);
29407aab
DV
4774 }
4775
4776 ironlake_set_pipeconf(crtc);
4777
f67a559d 4778 intel_crtc->active = true;
8664281b 4779
a72e4c9f 4780 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
8664281b 4781
f6736a1a 4782 for_each_encoder_on_crtc(dev, crtc, encoder)
952735ee
DV
4783 if (encoder->pre_enable)
4784 encoder->pre_enable(encoder);
f67a559d 4785
6e3c9717 4786 if (intel_crtc->config->has_pch_encoder) {
fff367c7
DV
4787 /* Note: FDI PLL enabling _must_ be done before we enable the
4788 * cpu pipes, hence this is separate from all the other fdi/pch
4789 * enabling. */
88cefb6c 4790 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
DV
4791 } else {
4792 assert_fdi_tx_disabled(dev_priv, pipe);
4793 assert_fdi_rx_disabled(dev_priv, pipe);
4794 }
f67a559d 4795
b074cec8 4796 ironlake_pfit_enable(intel_crtc);
f67a559d 4797
9c54c0dd
JB
4798 /*
4799 * On ILK+ LUT must be loaded before the pipe is running but with
4800 * clocks enabled
4801 */
8563b1e8 4802 intel_color_load_luts(crtc);
9c54c0dd 4803
1d5bf5d9
ID
4804 if (dev_priv->display.initial_watermarks != NULL)
4805 dev_priv->display.initial_watermarks(intel_crtc->config);
e1fdc473 4806 intel_enable_pipe(intel_crtc);
f67a559d 4807
6e3c9717 4808 if (intel_crtc->config->has_pch_encoder)
f67a559d 4809 ironlake_pch_enable(crtc);
c98e9dcf 4810
f9b61ff6
DV
4811 assert_vblank_disabled(crtc);
4812 drm_crtc_vblank_on(crtc);
4813
fa5c73b1
DV
4814 for_each_encoder_on_crtc(dev, crtc, encoder)
4815 encoder->enable(encoder);
61b77ddd
DV
4816
4817 if (HAS_PCH_CPT(dev))
a1520318 4818 cpt_verify_modeset(dev, intel_crtc->pipe);
37ca8d4c
VS
4819
4820 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
4821 if (intel_crtc->config->has_pch_encoder)
4822 intel_wait_for_vblank(dev, pipe);
4823 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607
JB
4824}
4825
42db64ef
PZ
4826/* IPS only exists on ULT machines and is tied to pipe A. */
4827static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4828{
f5adf94e 4829 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
42db64ef
PZ
4830}
4831
4f771f10
PZ
4832static void haswell_crtc_enable(struct drm_crtc *crtc)
4833{
4834 struct drm_device *dev = crtc->dev;
4835 struct drm_i915_private *dev_priv = dev->dev_private;
4836 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4837 struct intel_encoder *encoder;
99d736a2 4838 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
4d1de975 4839 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
99d736a2
ML
4840 struct intel_crtc_state *pipe_config =
4841 to_intel_crtc_state(crtc->state);
4f771f10 4842
53d9f4e9 4843 if (WARN_ON(intel_crtc->active))
4f771f10
PZ
4844 return;
4845
81b088ca
VS
4846 if (intel_crtc->config->has_pch_encoder)
4847 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4848 false);
4849
8106ddbd 4850 if (intel_crtc->config->shared_dpll)
df8ad70c
DV
4851 intel_enable_shared_dpll(intel_crtc);
4852
6e3c9717 4853 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 4854 intel_dp_set_m_n(intel_crtc, M1_N1);
229fca97 4855
4d1de975
JN
4856 if (!intel_crtc->config->has_dsi_encoder)
4857 intel_set_pipe_timings(intel_crtc);
4858
bc58be60 4859 intel_set_pipe_src_size(intel_crtc);
229fca97 4860
4d1de975
JN
4861 if (cpu_transcoder != TRANSCODER_EDP &&
4862 !transcoder_is_dsi(cpu_transcoder)) {
4863 I915_WRITE(PIPE_MULT(cpu_transcoder),
6e3c9717 4864 intel_crtc->config->pixel_multiplier - 1);
ebb69c95
CT
4865 }
4866
6e3c9717 4867 if (intel_crtc->config->has_pch_encoder) {
229fca97 4868 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4869 &intel_crtc->config->fdi_m_n, NULL);
229fca97
DV
4870 }
4871
4d1de975
JN
4872 if (!intel_crtc->config->has_dsi_encoder)
4873 haswell_set_pipeconf(crtc);
4874
391bf048 4875 haswell_set_pipemisc(crtc);
229fca97 4876
8563b1e8 4877 intel_color_set_csc(crtc);
229fca97 4878
4f771f10 4879 intel_crtc->active = true;
8664281b 4880
6b698516
DV
4881 if (intel_crtc->config->has_pch_encoder)
4882 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4883 else
4884 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4885
7d4aefd0 4886 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10
PZ
4887 if (encoder->pre_enable)
4888 encoder->pre_enable(encoder);
7d4aefd0 4889 }
4f771f10 4890
d2d65408 4891 if (intel_crtc->config->has_pch_encoder)
4fe9467d 4892 dev_priv->display.fdi_link_train(crtc);
4fe9467d 4893
a65347ba 4894 if (!intel_crtc->config->has_dsi_encoder)
7d4aefd0 4895 intel_ddi_enable_pipe_clock(intel_crtc);
4f771f10 4896
1c132b44 4897 if (INTEL_INFO(dev)->gen >= 9)
e435d6e5 4898 skylake_pfit_enable(intel_crtc);
ff6d9f55 4899 else
1c132b44 4900 ironlake_pfit_enable(intel_crtc);
4f771f10
PZ
4901
4902 /*
4903 * On ILK+ LUT must be loaded before the pipe is running but with
4904 * clocks enabled
4905 */
8563b1e8 4906 intel_color_load_luts(crtc);
4f771f10 4907
1f544388 4908 intel_ddi_set_pipe_settings(crtc);
a65347ba 4909 if (!intel_crtc->config->has_dsi_encoder)
7d4aefd0 4910 intel_ddi_enable_transcoder_func(crtc);
4f771f10 4911
1d5bf5d9
ID
4912 if (dev_priv->display.initial_watermarks != NULL)
4913 dev_priv->display.initial_watermarks(pipe_config);
4914 else
4915 intel_update_watermarks(crtc);
4d1de975
JN
4916
4917 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
4918 if (!intel_crtc->config->has_dsi_encoder)
4919 intel_enable_pipe(intel_crtc);
42db64ef 4920
6e3c9717 4921 if (intel_crtc->config->has_pch_encoder)
1507e5bd 4922 lpt_pch_enable(crtc);
4f771f10 4923
a65347ba 4924 if (intel_crtc->config->dp_encoder_is_mst)
0e32b39c
DA
4925 intel_ddi_set_vc_payload_alloc(crtc, true);
4926
f9b61ff6
DV
4927 assert_vblank_disabled(crtc);
4928 drm_crtc_vblank_on(crtc);
4929
8807e55b 4930 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10 4931 encoder->enable(encoder);
8807e55b
JN
4932 intel_opregion_notify_encoder(encoder, true);
4933 }
4f771f10 4934
6b698516
DV
4935 if (intel_crtc->config->has_pch_encoder) {
4936 intel_wait_for_vblank(dev, pipe);
4937 intel_wait_for_vblank(dev, pipe);
4938 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
d2d65408
VS
4939 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4940 true);
6b698516 4941 }
d2d65408 4942
e4916946
PZ
4943 /* If we change the relative order between pipe/planes enabling, we need
4944 * to change the workaround. */
99d736a2
ML
4945 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
4946 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
4947 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4948 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4949 }
4f771f10
PZ
4950}
4951
bfd16b2a 4952static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
3f8dce3a
DV
4953{
4954 struct drm_device *dev = crtc->base.dev;
4955 struct drm_i915_private *dev_priv = dev->dev_private;
4956 int pipe = crtc->pipe;
4957
4958 /* To avoid upsetting the power well on haswell only disable the pfit if
4959 * it's in use. The hw state code will make sure we get this right. */
bfd16b2a 4960 if (force || crtc->config->pch_pfit.enabled) {
3f8dce3a
DV
4961 I915_WRITE(PF_CTL(pipe), 0);
4962 I915_WRITE(PF_WIN_POS(pipe), 0);
4963 I915_WRITE(PF_WIN_SZ(pipe), 0);
4964 }
4965}
4966
6be4a607
JB
4967static void ironlake_crtc_disable(struct drm_crtc *crtc)
4968{
4969 struct drm_device *dev = crtc->dev;
4970 struct drm_i915_private *dev_priv = dev->dev_private;
4971 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4972 struct intel_encoder *encoder;
6be4a607 4973 int pipe = intel_crtc->pipe;
b52eb4dc 4974
37ca8d4c
VS
4975 if (intel_crtc->config->has_pch_encoder)
4976 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4977
ea9d758d
DV
4978 for_each_encoder_on_crtc(dev, crtc, encoder)
4979 encoder->disable(encoder);
4980
f9b61ff6
DV
4981 drm_crtc_vblank_off(crtc);
4982 assert_vblank_disabled(crtc);
4983
3860b2ec
VS
4984 /*
4985 * Sometimes spurious CPU pipe underruns happen when the
4986 * pipe is already disabled, but FDI RX/TX is still enabled.
4987 * Happens at least with VGA+HDMI cloning. Suppress them.
4988 */
4989 if (intel_crtc->config->has_pch_encoder)
4990 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4991
575f7ab7 4992 intel_disable_pipe(intel_crtc);
32f9d658 4993
bfd16b2a 4994 ironlake_pfit_disable(intel_crtc, false);
2c07245f 4995
3860b2ec 4996 if (intel_crtc->config->has_pch_encoder) {
5a74f70a 4997 ironlake_fdi_disable(crtc);
3860b2ec
VS
4998 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4999 }
5a74f70a 5000
bf49ec8c
DV
5001 for_each_encoder_on_crtc(dev, crtc, encoder)
5002 if (encoder->post_disable)
5003 encoder->post_disable(encoder);
2c07245f 5004
6e3c9717 5005 if (intel_crtc->config->has_pch_encoder) {
d925c59a 5006 ironlake_disable_pch_transcoder(dev_priv, pipe);
6be4a607 5007
d925c59a 5008 if (HAS_PCH_CPT(dev)) {
f0f59a00
VS
5009 i915_reg_t reg;
5010 u32 temp;
5011
d925c59a
DV
5012 /* disable TRANS_DP_CTL */
5013 reg = TRANS_DP_CTL(pipe);
5014 temp = I915_READ(reg);
5015 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5016 TRANS_DP_PORT_SEL_MASK);
5017 temp |= TRANS_DP_PORT_SEL_NONE;
5018 I915_WRITE(reg, temp);
5019
5020 /* disable DPLL_SEL */
5021 temp = I915_READ(PCH_DPLL_SEL);
11887397 5022 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 5023 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 5024 }
e3421a18 5025
d925c59a
DV
5026 ironlake_fdi_pll_disable(intel_crtc);
5027 }
81b088ca
VS
5028
5029 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607 5030}
1b3c7a47 5031
4f771f10 5032static void haswell_crtc_disable(struct drm_crtc *crtc)
ee7b9f93 5033{
4f771f10
PZ
5034 struct drm_device *dev = crtc->dev;
5035 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93 5036 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4f771f10 5037 struct intel_encoder *encoder;
6e3c9717 5038 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee7b9f93 5039
d2d65408
VS
5040 if (intel_crtc->config->has_pch_encoder)
5041 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5042 false);
5043
8807e55b
JN
5044 for_each_encoder_on_crtc(dev, crtc, encoder) {
5045 intel_opregion_notify_encoder(encoder, false);
4f771f10 5046 encoder->disable(encoder);
8807e55b 5047 }
4f771f10 5048
f9b61ff6
DV
5049 drm_crtc_vblank_off(crtc);
5050 assert_vblank_disabled(crtc);
5051
4d1de975
JN
5052 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
5053 if (!intel_crtc->config->has_dsi_encoder)
5054 intel_disable_pipe(intel_crtc);
4f771f10 5055
6e3c9717 5056 if (intel_crtc->config->dp_encoder_is_mst)
a4bf214f
VS
5057 intel_ddi_set_vc_payload_alloc(crtc, false);
5058
a65347ba 5059 if (!intel_crtc->config->has_dsi_encoder)
7d4aefd0 5060 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4f771f10 5061
1c132b44 5062 if (INTEL_INFO(dev)->gen >= 9)
e435d6e5 5063 skylake_scaler_disable(intel_crtc);
ff6d9f55 5064 else
bfd16b2a 5065 ironlake_pfit_disable(intel_crtc, false);
4f771f10 5066
a65347ba 5067 if (!intel_crtc->config->has_dsi_encoder)
7d4aefd0 5068 intel_ddi_disable_pipe_clock(intel_crtc);
4f771f10 5069
97b040aa
ID
5070 for_each_encoder_on_crtc(dev, crtc, encoder)
5071 if (encoder->post_disable)
5072 encoder->post_disable(encoder);
81b088ca 5073
92966a37
VS
5074 if (intel_crtc->config->has_pch_encoder) {
5075 lpt_disable_pch_transcoder(dev_priv);
503a74e9 5076 lpt_disable_iclkip(dev_priv);
92966a37
VS
5077 intel_ddi_fdi_disable(crtc);
5078
81b088ca
VS
5079 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5080 true);
92966a37 5081 }
4f771f10
PZ
5082}
5083
2dd24552
JB
5084static void i9xx_pfit_enable(struct intel_crtc *crtc)
5085{
5086 struct drm_device *dev = crtc->base.dev;
5087 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 5088 struct intel_crtc_state *pipe_config = crtc->config;
2dd24552 5089
681a8504 5090 if (!pipe_config->gmch_pfit.control)
2dd24552
JB
5091 return;
5092
2dd24552 5093 /*
c0b03411
DV
5094 * The panel fitter should only be adjusted whilst the pipe is disabled,
5095 * according to register description and PRM.
2dd24552 5096 */
c0b03411
DV
5097 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5098 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 5099
b074cec8
JB
5100 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5101 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
DV
5102
5103 /* Border color in case we don't scale up to the full screen. Black by
5104 * default, change to something else for debugging. */
5105 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
5106}
5107
d05410f9
DA
5108static enum intel_display_power_domain port_to_power_domain(enum port port)
5109{
5110 switch (port) {
5111 case PORT_A:
6331a704 5112 return POWER_DOMAIN_PORT_DDI_A_LANES;
d05410f9 5113 case PORT_B:
6331a704 5114 return POWER_DOMAIN_PORT_DDI_B_LANES;
d05410f9 5115 case PORT_C:
6331a704 5116 return POWER_DOMAIN_PORT_DDI_C_LANES;
d05410f9 5117 case PORT_D:
6331a704 5118 return POWER_DOMAIN_PORT_DDI_D_LANES;
d8e19f99 5119 case PORT_E:
6331a704 5120 return POWER_DOMAIN_PORT_DDI_E_LANES;
d05410f9 5121 default:
b9fec167 5122 MISSING_CASE(port);
d05410f9
DA
5123 return POWER_DOMAIN_PORT_OTHER;
5124 }
5125}
5126
25f78f58
VS
5127static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5128{
5129 switch (port) {
5130 case PORT_A:
5131 return POWER_DOMAIN_AUX_A;
5132 case PORT_B:
5133 return POWER_DOMAIN_AUX_B;
5134 case PORT_C:
5135 return POWER_DOMAIN_AUX_C;
5136 case PORT_D:
5137 return POWER_DOMAIN_AUX_D;
5138 case PORT_E:
5139 /* FIXME: Check VBT for actual wiring of PORT E */
5140 return POWER_DOMAIN_AUX_D;
5141 default:
b9fec167 5142 MISSING_CASE(port);
25f78f58
VS
5143 return POWER_DOMAIN_AUX_A;
5144 }
5145}
5146
319be8ae
ID
5147enum intel_display_power_domain
5148intel_display_port_power_domain(struct intel_encoder *intel_encoder)
5149{
5150 struct drm_device *dev = intel_encoder->base.dev;
5151 struct intel_digital_port *intel_dig_port;
5152
5153 switch (intel_encoder->type) {
5154 case INTEL_OUTPUT_UNKNOWN:
5155 /* Only DDI platforms should ever use this output type */
5156 WARN_ON_ONCE(!HAS_DDI(dev));
5157 case INTEL_OUTPUT_DISPLAYPORT:
5158 case INTEL_OUTPUT_HDMI:
5159 case INTEL_OUTPUT_EDP:
5160 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
d05410f9 5161 return port_to_power_domain(intel_dig_port->port);
0e32b39c
DA
5162 case INTEL_OUTPUT_DP_MST:
5163 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5164 return port_to_power_domain(intel_dig_port->port);
319be8ae
ID
5165 case INTEL_OUTPUT_ANALOG:
5166 return POWER_DOMAIN_PORT_CRT;
5167 case INTEL_OUTPUT_DSI:
5168 return POWER_DOMAIN_PORT_DSI;
5169 default:
5170 return POWER_DOMAIN_PORT_OTHER;
5171 }
5172}
5173
25f78f58
VS
5174enum intel_display_power_domain
5175intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5176{
5177 struct drm_device *dev = intel_encoder->base.dev;
5178 struct intel_digital_port *intel_dig_port;
5179
5180 switch (intel_encoder->type) {
5181 case INTEL_OUTPUT_UNKNOWN:
651174a4
ID
5182 case INTEL_OUTPUT_HDMI:
5183 /*
5184 * Only DDI platforms should ever use these output types.
5185 * We can get here after the HDMI detect code has already set
5186 * the type of the shared encoder. Since we can't be sure
5187 * what's the status of the given connectors, play safe and
5188 * run the DP detection too.
5189 */
25f78f58
VS
5190 WARN_ON_ONCE(!HAS_DDI(dev));
5191 case INTEL_OUTPUT_DISPLAYPORT:
5192 case INTEL_OUTPUT_EDP:
5193 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5194 return port_to_aux_power_domain(intel_dig_port->port);
5195 case INTEL_OUTPUT_DP_MST:
5196 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5197 return port_to_aux_power_domain(intel_dig_port->port);
5198 default:
b9fec167 5199 MISSING_CASE(intel_encoder->type);
25f78f58
VS
5200 return POWER_DOMAIN_AUX_A;
5201 }
5202}
5203
74bff5f9
ML
5204static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
5205 struct intel_crtc_state *crtc_state)
77d22dca 5206{
319be8ae 5207 struct drm_device *dev = crtc->dev;
74bff5f9 5208 struct drm_encoder *encoder;
319be8ae
ID
5209 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5210 enum pipe pipe = intel_crtc->pipe;
77d22dca 5211 unsigned long mask;
74bff5f9 5212 enum transcoder transcoder = crtc_state->cpu_transcoder;
77d22dca 5213
74bff5f9 5214 if (!crtc_state->base.active)
292b990e
ML
5215 return 0;
5216
77d22dca
ID
5217 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5218 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
74bff5f9
ML
5219 if (crtc_state->pch_pfit.enabled ||
5220 crtc_state->pch_pfit.force_thru)
77d22dca
ID
5221 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5222
74bff5f9
ML
5223 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5224 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5225
319be8ae 5226 mask |= BIT(intel_display_port_power_domain(intel_encoder));
74bff5f9 5227 }
319be8ae 5228
15e7ec29
ML
5229 if (crtc_state->shared_dpll)
5230 mask |= BIT(POWER_DOMAIN_PLLS);
5231
77d22dca
ID
5232 return mask;
5233}
5234
74bff5f9
ML
5235static unsigned long
5236modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5237 struct intel_crtc_state *crtc_state)
77d22dca 5238{
292b990e
ML
5239 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5240 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5241 enum intel_display_power_domain domain;
5242 unsigned long domains, new_domains, old_domains;
77d22dca 5243
292b990e 5244 old_domains = intel_crtc->enabled_power_domains;
74bff5f9
ML
5245 intel_crtc->enabled_power_domains = new_domains =
5246 get_crtc_power_domains(crtc, crtc_state);
77d22dca 5247
292b990e
ML
5248 domains = new_domains & ~old_domains;
5249
5250 for_each_power_domain(domain, domains)
5251 intel_display_power_get(dev_priv, domain);
5252
5253 return old_domains & ~new_domains;
5254}
5255
5256static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5257 unsigned long domains)
5258{
5259 enum intel_display_power_domain domain;
5260
5261 for_each_power_domain(domain, domains)
5262 intel_display_power_put(dev_priv, domain);
5263}
77d22dca 5264
adafdc6f
MK
5265static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5266{
5267 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5268
5269 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5270 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5271 return max_cdclk_freq;
5272 else if (IS_CHERRYVIEW(dev_priv))
5273 return max_cdclk_freq*95/100;
5274 else if (INTEL_INFO(dev_priv)->gen < 4)
5275 return 2*max_cdclk_freq*90/100;
5276 else
5277 return max_cdclk_freq*90/100;
5278}
5279
560a7ae4
DL
5280static void intel_update_max_cdclk(struct drm_device *dev)
5281{
5282 struct drm_i915_private *dev_priv = dev->dev_private;
5283
ef11bdb3 5284 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
560a7ae4
DL
5285 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
5286
5287 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
5288 dev_priv->max_cdclk_freq = 675000;
5289 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
5290 dev_priv->max_cdclk_freq = 540000;
5291 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
5292 dev_priv->max_cdclk_freq = 450000;
5293 else
5294 dev_priv->max_cdclk_freq = 337500;
5295 } else if (IS_BROADWELL(dev)) {
5296 /*
5297 * FIXME with extra cooling we can allow
5298 * 540 MHz for ULX and 675 Mhz for ULT.
5299 * How can we know if extra cooling is
5300 * available? PCI ID, VTB, something else?
5301 */
5302 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5303 dev_priv->max_cdclk_freq = 450000;
5304 else if (IS_BDW_ULX(dev))
5305 dev_priv->max_cdclk_freq = 450000;
5306 else if (IS_BDW_ULT(dev))
5307 dev_priv->max_cdclk_freq = 540000;
5308 else
5309 dev_priv->max_cdclk_freq = 675000;
0904deaf
MK
5310 } else if (IS_CHERRYVIEW(dev)) {
5311 dev_priv->max_cdclk_freq = 320000;
560a7ae4
DL
5312 } else if (IS_VALLEYVIEW(dev)) {
5313 dev_priv->max_cdclk_freq = 400000;
5314 } else {
5315 /* otherwise assume cdclk is fixed */
5316 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5317 }
5318
adafdc6f
MK
5319 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5320
560a7ae4
DL
5321 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5322 dev_priv->max_cdclk_freq);
adafdc6f
MK
5323
5324 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5325 dev_priv->max_dotclk_freq);
560a7ae4
DL
5326}
5327
5328static void intel_update_cdclk(struct drm_device *dev)
5329{
5330 struct drm_i915_private *dev_priv = dev->dev_private;
5331
5332 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
5333 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5334 dev_priv->cdclk_freq);
5335
5336 /*
5337 * Program the gmbus_freq based on the cdclk frequency.
5338 * BSpec erroneously claims we should aim for 4MHz, but
5339 * in fact 1MHz is the correct frequency.
5340 */
666a4537 5341 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
560a7ae4
DL
5342 /*
5343 * Program the gmbus_freq based on the cdclk frequency.
5344 * BSpec erroneously claims we should aim for 4MHz, but
5345 * in fact 1MHz is the correct frequency.
5346 */
5347 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
5348 }
5349
5350 if (dev_priv->max_cdclk_freq == 0)
5351 intel_update_max_cdclk(dev);
5352}
5353
70d0c574 5354static void broxton_set_cdclk(struct drm_device *dev, int frequency)
f8437dd1
VK
5355{
5356 struct drm_i915_private *dev_priv = dev->dev_private;
5357 uint32_t divider;
5358 uint32_t ratio;
5359 uint32_t current_freq;
5360 int ret;
5361
5362 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5363 switch (frequency) {
5364 case 144000:
5365 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5366 ratio = BXT_DE_PLL_RATIO(60);
5367 break;
5368 case 288000:
5369 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5370 ratio = BXT_DE_PLL_RATIO(60);
5371 break;
5372 case 384000:
5373 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5374 ratio = BXT_DE_PLL_RATIO(60);
5375 break;
5376 case 576000:
5377 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5378 ratio = BXT_DE_PLL_RATIO(60);
5379 break;
5380 case 624000:
5381 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5382 ratio = BXT_DE_PLL_RATIO(65);
5383 break;
5384 case 19200:
5385 /*
5386 * Bypass frequency with DE PLL disabled. Init ratio, divider
5387 * to suppress GCC warning.
5388 */
5389 ratio = 0;
5390 divider = 0;
5391 break;
5392 default:
5393 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5394
5395 return;
5396 }
5397
5398 mutex_lock(&dev_priv->rps.hw_lock);
5399 /* Inform power controller of upcoming frequency change */
5400 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5401 0x80000000);
5402 mutex_unlock(&dev_priv->rps.hw_lock);
5403
5404 if (ret) {
5405 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5406 ret, frequency);
5407 return;
5408 }
5409
5410 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5411 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5412 current_freq = current_freq * 500 + 1000;
5413
5414 /*
5415 * DE PLL has to be disabled when
5416 * - setting to 19.2MHz (bypass, PLL isn't used)
5417 * - before setting to 624MHz (PLL needs toggling)
5418 * - before setting to any frequency from 624MHz (PLL needs toggling)
5419 */
5420 if (frequency == 19200 || frequency == 624000 ||
5421 current_freq == 624000) {
5422 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5423 /* Timeout 200us */
5424 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5425 1))
5426 DRM_ERROR("timout waiting for DE PLL unlock\n");
5427 }
5428
5429 if (frequency != 19200) {
5430 uint32_t val;
5431
5432 val = I915_READ(BXT_DE_PLL_CTL);
5433 val &= ~BXT_DE_PLL_RATIO_MASK;
5434 val |= ratio;
5435 I915_WRITE(BXT_DE_PLL_CTL, val);
5436
5437 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5438 /* Timeout 200us */
5439 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5440 DRM_ERROR("timeout waiting for DE PLL lock\n");
5441
5442 val = I915_READ(CDCLK_CTL);
5443 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5444 val |= divider;
5445 /*
5446 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5447 * enable otherwise.
5448 */
5449 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5450 if (frequency >= 500000)
5451 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5452
5453 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5454 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5455 val |= (frequency - 1000) / 500;
5456 I915_WRITE(CDCLK_CTL, val);
5457 }
5458
5459 mutex_lock(&dev_priv->rps.hw_lock);
5460 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5461 DIV_ROUND_UP(frequency, 25000));
5462 mutex_unlock(&dev_priv->rps.hw_lock);
5463
5464 if (ret) {
5465 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5466 ret, frequency);
5467 return;
5468 }
5469
a47871bd 5470 intel_update_cdclk(dev);
f8437dd1
VK
5471}
5472
5473void broxton_init_cdclk(struct drm_device *dev)
5474{
5475 struct drm_i915_private *dev_priv = dev->dev_private;
5476 uint32_t val;
5477
5478 /*
5479 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5480 * or else the reset will hang because there is no PCH to respond.
5481 * Move the handshake programming to initialization sequence.
5482 * Previously was left up to BIOS.
5483 */
5484 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5485 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5486 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5487
5488 /* Enable PG1 for cdclk */
5489 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5490
5491 /* check if cd clock is enabled */
5492 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5493 DRM_DEBUG_KMS("Display already initialized\n");
5494 return;
5495 }
5496
5497 /*
5498 * FIXME:
5499 * - The initial CDCLK needs to be read from VBT.
5500 * Need to make this change after VBT has changes for BXT.
5501 * - check if setting the max (or any) cdclk freq is really necessary
5502 * here, it belongs to modeset time
5503 */
5504 broxton_set_cdclk(dev, 624000);
5505
5506 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
22e02c0b
VS
5507 POSTING_READ(DBUF_CTL);
5508
f8437dd1
VK
5509 udelay(10);
5510
5511 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5512 DRM_ERROR("DBuf power enable timeout!\n");
5513}
5514
5515void broxton_uninit_cdclk(struct drm_device *dev)
5516{
5517 struct drm_i915_private *dev_priv = dev->dev_private;
5518
5519 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
22e02c0b
VS
5520 POSTING_READ(DBUF_CTL);
5521
f8437dd1
VK
5522 udelay(10);
5523
5524 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5525 DRM_ERROR("DBuf power disable timeout!\n");
5526
5527 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5528 broxton_set_cdclk(dev, 19200);
5529
5530 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5531}
5532
5d96d8af
DL
5533static const struct skl_cdclk_entry {
5534 unsigned int freq;
5535 unsigned int vco;
5536} skl_cdclk_frequencies[] = {
5537 { .freq = 308570, .vco = 8640 },
5538 { .freq = 337500, .vco = 8100 },
5539 { .freq = 432000, .vco = 8640 },
5540 { .freq = 450000, .vco = 8100 },
5541 { .freq = 540000, .vco = 8100 },
5542 { .freq = 617140, .vco = 8640 },
5543 { .freq = 675000, .vco = 8100 },
5544};
5545
5546static unsigned int skl_cdclk_decimal(unsigned int freq)
5547{
5548 return (freq - 1000) / 500;
5549}
5550
5551static unsigned int skl_cdclk_get_vco(unsigned int freq)
5552{
5553 unsigned int i;
5554
5555 for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
5556 const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
5557
5558 if (e->freq == freq)
5559 return e->vco;
5560 }
5561
5562 return 8100;
5563}
5564
5565static void
5566skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
5567{
5568 unsigned int min_freq;
5569 u32 val;
5570
5571 /* select the minimum CDCLK before enabling DPLL 0 */
5572 val = I915_READ(CDCLK_CTL);
5573 val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
5574 val |= CDCLK_FREQ_337_308;
5575
5576 if (required_vco == 8640)
5577 min_freq = 308570;
5578 else
5579 min_freq = 337500;
5580
5581 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
5582
5583 I915_WRITE(CDCLK_CTL, val);
5584 POSTING_READ(CDCLK_CTL);
5585
5586 /*
5587 * We always enable DPLL0 with the lowest link rate possible, but still
5588 * taking into account the VCO required to operate the eDP panel at the
5589 * desired frequency. The usual DP link rates operate with a VCO of
5590 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5591 * The modeset code is responsible for the selection of the exact link
5592 * rate later on, with the constraint of choosing a frequency that
5593 * works with required_vco.
5594 */
5595 val = I915_READ(DPLL_CTRL1);
5596
5597 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5598 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5599 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
5600 if (required_vco == 8640)
5601 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5602 SKL_DPLL0);
5603 else
5604 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5605 SKL_DPLL0);
5606
5607 I915_WRITE(DPLL_CTRL1, val);
5608 POSTING_READ(DPLL_CTRL1);
5609
5610 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5611
5612 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5613 DRM_ERROR("DPLL0 not locked\n");
5614}
5615
5616static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5617{
5618 int ret;
5619 u32 val;
5620
5621 /* inform PCU we want to change CDCLK */
5622 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5623 mutex_lock(&dev_priv->rps.hw_lock);
5624 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5625 mutex_unlock(&dev_priv->rps.hw_lock);
5626
5627 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5628}
5629
5630static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5631{
5632 unsigned int i;
5633
5634 for (i = 0; i < 15; i++) {
5635 if (skl_cdclk_pcu_ready(dev_priv))
5636 return true;
5637 udelay(10);
5638 }
5639
5640 return false;
5641}
5642
5643static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
5644{
560a7ae4 5645 struct drm_device *dev = dev_priv->dev;
5d96d8af
DL
5646 u32 freq_select, pcu_ack;
5647
5648 DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
5649
5650 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5651 DRM_ERROR("failed to inform PCU about cdclk change\n");
5652 return;
5653 }
5654
5655 /* set CDCLK_CTL */
5656 switch(freq) {
5657 case 450000:
5658 case 432000:
5659 freq_select = CDCLK_FREQ_450_432;
5660 pcu_ack = 1;
5661 break;
5662 case 540000:
5663 freq_select = CDCLK_FREQ_540;
5664 pcu_ack = 2;
5665 break;
5666 case 308570:
5667 case 337500:
5668 default:
5669 freq_select = CDCLK_FREQ_337_308;
5670 pcu_ack = 0;
5671 break;
5672 case 617140:
5673 case 675000:
5674 freq_select = CDCLK_FREQ_675_617;
5675 pcu_ack = 3;
5676 break;
5677 }
5678
5679 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
5680 POSTING_READ(CDCLK_CTL);
5681
5682 /* inform PCU of the change */
5683 mutex_lock(&dev_priv->rps.hw_lock);
5684 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5685 mutex_unlock(&dev_priv->rps.hw_lock);
560a7ae4
DL
5686
5687 intel_update_cdclk(dev);
5d96d8af
DL
5688}
5689
5690void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5691{
5692 /* disable DBUF power */
5693 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5694 POSTING_READ(DBUF_CTL);
5695
5696 udelay(10);
5697
5698 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5699 DRM_ERROR("DBuf power disable timeout\n");
5700
ab96c1ee
ID
5701 /* disable DPLL0 */
5702 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5703 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5704 DRM_ERROR("Couldn't disable DPLL0\n");
5d96d8af
DL
5705}
5706
5707void skl_init_cdclk(struct drm_i915_private *dev_priv)
5708{
5d96d8af
DL
5709 unsigned int required_vco;
5710
39d9b85a
GW
5711 /* DPLL0 not enabled (happens on early BIOS versions) */
5712 if (!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE)) {
5713 /* enable DPLL0 */
5714 required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
5715 skl_dpll0_enable(dev_priv, required_vco);
5d96d8af
DL
5716 }
5717
5d96d8af
DL
5718 /* set CDCLK to the frequency the BIOS chose */
5719 skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
5720
5721 /* enable DBUF power */
5722 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5723 POSTING_READ(DBUF_CTL);
5724
5725 udelay(10);
5726
5727 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5728 DRM_ERROR("DBuf power enable timeout\n");
5729}
5730
c73666f3
SK
5731int skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
5732{
5733 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
5734 uint32_t cdctl = I915_READ(CDCLK_CTL);
5735 int freq = dev_priv->skl_boot_cdclk;
5736
f1b391a5
SK
5737 /*
5738 * check if the pre-os intialized the display
5739 * There is SWF18 scratchpad register defined which is set by the
5740 * pre-os which can be used by the OS drivers to check the status
5741 */
5742 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
5743 goto sanitize;
5744
c73666f3
SK
5745 /* Is PLL enabled and locked ? */
5746 if (!((lcpll1 & LCPLL_PLL_ENABLE) && (lcpll1 & LCPLL_PLL_LOCK)))
5747 goto sanitize;
5748
5749 /* DPLL okay; verify the cdclock
5750 *
5751 * Noticed in some instances that the freq selection is correct but
5752 * decimal part is programmed wrong from BIOS where pre-os does not
5753 * enable display. Verify the same as well.
5754 */
5755 if (cdctl == ((cdctl & CDCLK_FREQ_SEL_MASK) | skl_cdclk_decimal(freq)))
5756 /* All well; nothing to sanitize */
5757 return false;
5758sanitize:
5759 /*
5760 * As of now initialize with max cdclk till
5761 * we get dynamic cdclk support
5762 * */
5763 dev_priv->skl_boot_cdclk = dev_priv->max_cdclk_freq;
5764 skl_init_cdclk(dev_priv);
5765
5766 /* we did have to sanitize */
5767 return true;
5768}
5769
30a970c6
JB
5770/* Adjust CDclk dividers to allow high res or save power if possible */
5771static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5772{
5773 struct drm_i915_private *dev_priv = dev->dev_private;
5774 u32 val, cmd;
5775
164dfd28
VK
5776 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5777 != dev_priv->cdclk_freq);
d60c4473 5778
dfcab17e 5779 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
30a970c6 5780 cmd = 2;
dfcab17e 5781 else if (cdclk == 266667)
30a970c6
JB
5782 cmd = 1;
5783 else
5784 cmd = 0;
5785
5786 mutex_lock(&dev_priv->rps.hw_lock);
5787 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5788 val &= ~DSPFREQGUAR_MASK;
5789 val |= (cmd << DSPFREQGUAR_SHIFT);
5790 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5791 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5792 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5793 50)) {
5794 DRM_ERROR("timed out waiting for CDclk change\n");
5795 }
5796 mutex_unlock(&dev_priv->rps.hw_lock);
5797
54433e91
VS
5798 mutex_lock(&dev_priv->sb_lock);
5799
dfcab17e 5800 if (cdclk == 400000) {
6bcda4f0 5801 u32 divider;
30a970c6 5802
6bcda4f0 5803 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
30a970c6 5804
30a970c6
JB
5805 /* adjust cdclk divider */
5806 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
87d5d259 5807 val &= ~CCK_FREQUENCY_VALUES;
30a970c6
JB
5808 val |= divider;
5809 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
a877e801
VS
5810
5811 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
87d5d259 5812 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
a877e801
VS
5813 50))
5814 DRM_ERROR("timed out waiting for CDclk change\n");
30a970c6
JB
5815 }
5816
30a970c6
JB
5817 /* adjust self-refresh exit latency value */
5818 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5819 val &= ~0x7f;
5820
5821 /*
5822 * For high bandwidth configs, we set a higher latency in the bunit
5823 * so that the core display fetch happens in time to avoid underruns.
5824 */
dfcab17e 5825 if (cdclk == 400000)
30a970c6
JB
5826 val |= 4500 / 250; /* 4.5 usec */
5827 else
5828 val |= 3000 / 250; /* 3.0 usec */
5829 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
54433e91 5830
a580516d 5831 mutex_unlock(&dev_priv->sb_lock);
30a970c6 5832
b6283055 5833 intel_update_cdclk(dev);
30a970c6
JB
5834}
5835
383c5a6a
VS
5836static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5837{
5838 struct drm_i915_private *dev_priv = dev->dev_private;
5839 u32 val, cmd;
5840
164dfd28
VK
5841 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5842 != dev_priv->cdclk_freq);
383c5a6a
VS
5843
5844 switch (cdclk) {
383c5a6a
VS
5845 case 333333:
5846 case 320000:
383c5a6a 5847 case 266667:
383c5a6a 5848 case 200000:
383c5a6a
VS
5849 break;
5850 default:
5f77eeb0 5851 MISSING_CASE(cdclk);
383c5a6a
VS
5852 return;
5853 }
5854
9d0d3fda
VS
5855 /*
5856 * Specs are full of misinformation, but testing on actual
5857 * hardware has shown that we just need to write the desired
5858 * CCK divider into the Punit register.
5859 */
5860 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5861
383c5a6a
VS
5862 mutex_lock(&dev_priv->rps.hw_lock);
5863 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5864 val &= ~DSPFREQGUAR_MASK_CHV;
5865 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5866 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5867 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5868 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5869 50)) {
5870 DRM_ERROR("timed out waiting for CDclk change\n");
5871 }
5872 mutex_unlock(&dev_priv->rps.hw_lock);
5873
b6283055 5874 intel_update_cdclk(dev);
383c5a6a
VS
5875}
5876
30a970c6
JB
5877static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5878 int max_pixclk)
5879{
6bcda4f0 5880 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
6cca3195 5881 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
29dc7ef3 5882
30a970c6
JB
5883 /*
5884 * Really only a few cases to deal with, as only 4 CDclks are supported:
5885 * 200MHz
5886 * 267MHz
29dc7ef3 5887 * 320/333MHz (depends on HPLL freq)
6cca3195
VS
5888 * 400MHz (VLV only)
5889 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5890 * of the lower bin and adjust if needed.
e37c67a1
VS
5891 *
5892 * We seem to get an unstable or solid color picture at 200MHz.
5893 * Not sure what's wrong. For now use 200MHz only when all pipes
5894 * are off.
30a970c6 5895 */
6cca3195
VS
5896 if (!IS_CHERRYVIEW(dev_priv) &&
5897 max_pixclk > freq_320*limit/100)
dfcab17e 5898 return 400000;
6cca3195 5899 else if (max_pixclk > 266667*limit/100)
29dc7ef3 5900 return freq_320;
e37c67a1 5901 else if (max_pixclk > 0)
dfcab17e 5902 return 266667;
e37c67a1
VS
5903 else
5904 return 200000;
30a970c6
JB
5905}
5906
f8437dd1
VK
5907static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5908 int max_pixclk)
5909{
5910 /*
5911 * FIXME:
5912 * - remove the guardband, it's not needed on BXT
5913 * - set 19.2MHz bypass frequency if there are no active pipes
5914 */
5915 if (max_pixclk > 576000*9/10)
5916 return 624000;
5917 else if (max_pixclk > 384000*9/10)
5918 return 576000;
5919 else if (max_pixclk > 288000*9/10)
5920 return 384000;
5921 else if (max_pixclk > 144000*9/10)
5922 return 288000;
5923 else
5924 return 144000;
5925}
5926
e8788cbc 5927/* Compute the max pixel clock for new configuration. */
a821fc46
ACO
5928static int intel_mode_max_pixclk(struct drm_device *dev,
5929 struct drm_atomic_state *state)
30a970c6 5930{
565602d7
ML
5931 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
5932 struct drm_i915_private *dev_priv = dev->dev_private;
5933 struct drm_crtc *crtc;
5934 struct drm_crtc_state *crtc_state;
5935 unsigned max_pixclk = 0, i;
5936 enum pipe pipe;
30a970c6 5937
565602d7
ML
5938 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
5939 sizeof(intel_state->min_pixclk));
304603f4 5940
565602d7
ML
5941 for_each_crtc_in_state(state, crtc, crtc_state, i) {
5942 int pixclk = 0;
5943
5944 if (crtc_state->enable)
5945 pixclk = crtc_state->adjusted_mode.crtc_clock;
304603f4 5946
565602d7 5947 intel_state->min_pixclk[i] = pixclk;
30a970c6
JB
5948 }
5949
565602d7
ML
5950 for_each_pipe(dev_priv, pipe)
5951 max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
5952
30a970c6
JB
5953 return max_pixclk;
5954}
5955
27c329ed 5956static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
30a970c6 5957{
27c329ed
ML
5958 struct drm_device *dev = state->dev;
5959 struct drm_i915_private *dev_priv = dev->dev_private;
5960 int max_pixclk = intel_mode_max_pixclk(dev, state);
1a617b77
ML
5961 struct intel_atomic_state *intel_state =
5962 to_intel_atomic_state(state);
30a970c6 5963
304603f4
ACO
5964 if (max_pixclk < 0)
5965 return max_pixclk;
30a970c6 5966
1a617b77 5967 intel_state->cdclk = intel_state->dev_cdclk =
27c329ed 5968 valleyview_calc_cdclk(dev_priv, max_pixclk);
0a9ab303 5969
1a617b77
ML
5970 if (!intel_state->active_crtcs)
5971 intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
5972
27c329ed
ML
5973 return 0;
5974}
304603f4 5975
27c329ed
ML
5976static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
5977{
5978 struct drm_device *dev = state->dev;
5979 struct drm_i915_private *dev_priv = dev->dev_private;
5980 int max_pixclk = intel_mode_max_pixclk(dev, state);
1a617b77
ML
5981 struct intel_atomic_state *intel_state =
5982 to_intel_atomic_state(state);
85a96e7a 5983
27c329ed
ML
5984 if (max_pixclk < 0)
5985 return max_pixclk;
85a96e7a 5986
1a617b77 5987 intel_state->cdclk = intel_state->dev_cdclk =
27c329ed 5988 broxton_calc_cdclk(dev_priv, max_pixclk);
85a96e7a 5989
1a617b77
ML
5990 if (!intel_state->active_crtcs)
5991 intel_state->dev_cdclk = broxton_calc_cdclk(dev_priv, 0);
5992
27c329ed 5993 return 0;
30a970c6
JB
5994}
5995
1e69cd74
VS
5996static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5997{
5998 unsigned int credits, default_credits;
5999
6000 if (IS_CHERRYVIEW(dev_priv))
6001 default_credits = PFI_CREDIT(12);
6002 else
6003 default_credits = PFI_CREDIT(8);
6004
bfa7df01 6005 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
1e69cd74
VS
6006 /* CHV suggested value is 31 or 63 */
6007 if (IS_CHERRYVIEW(dev_priv))
fcc0008f 6008 credits = PFI_CREDIT_63;
1e69cd74
VS
6009 else
6010 credits = PFI_CREDIT(15);
6011 } else {
6012 credits = default_credits;
6013 }
6014
6015 /*
6016 * WA - write default credits before re-programming
6017 * FIXME: should we also set the resend bit here?
6018 */
6019 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6020 default_credits);
6021
6022 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6023 credits | PFI_CREDIT_RESEND);
6024
6025 /*
6026 * FIXME is this guaranteed to clear
6027 * immediately or should we poll for it?
6028 */
6029 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6030}
6031
27c329ed 6032static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
30a970c6 6033{
a821fc46 6034 struct drm_device *dev = old_state->dev;
30a970c6 6035 struct drm_i915_private *dev_priv = dev->dev_private;
1a617b77
ML
6036 struct intel_atomic_state *old_intel_state =
6037 to_intel_atomic_state(old_state);
6038 unsigned req_cdclk = old_intel_state->dev_cdclk;
30a970c6 6039
27c329ed
ML
6040 /*
6041 * FIXME: We can end up here with all power domains off, yet
6042 * with a CDCLK frequency other than the minimum. To account
6043 * for this take the PIPE-A power domain, which covers the HW
6044 * blocks needed for the following programming. This can be
6045 * removed once it's guaranteed that we get here either with
6046 * the minimum CDCLK set, or the required power domains
6047 * enabled.
6048 */
6049 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
738c05c0 6050
27c329ed
ML
6051 if (IS_CHERRYVIEW(dev))
6052 cherryview_set_cdclk(dev, req_cdclk);
6053 else
6054 valleyview_set_cdclk(dev, req_cdclk);
738c05c0 6055
27c329ed 6056 vlv_program_pfi_credits(dev_priv);
1e69cd74 6057
27c329ed 6058 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
30a970c6
JB
6059}
6060
89b667f8
JB
6061static void valleyview_crtc_enable(struct drm_crtc *crtc)
6062{
6063 struct drm_device *dev = crtc->dev;
a72e4c9f 6064 struct drm_i915_private *dev_priv = to_i915(dev);
89b667f8
JB
6065 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6066 struct intel_encoder *encoder;
6067 int pipe = intel_crtc->pipe;
89b667f8 6068
53d9f4e9 6069 if (WARN_ON(intel_crtc->active))
89b667f8
JB
6070 return;
6071
6e3c9717 6072 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 6073 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
6074
6075 intel_set_pipe_timings(intel_crtc);
bc58be60 6076 intel_set_pipe_src_size(intel_crtc);
5b18e57c 6077
c14b0485
VS
6078 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6079 struct drm_i915_private *dev_priv = dev->dev_private;
6080
6081 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6082 I915_WRITE(CHV_CANVAS(pipe), 0);
6083 }
6084
5b18e57c
DV
6085 i9xx_set_pipeconf(intel_crtc);
6086
89b667f8 6087 intel_crtc->active = true;
89b667f8 6088
a72e4c9f 6089 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6090
89b667f8
JB
6091 for_each_encoder_on_crtc(dev, crtc, encoder)
6092 if (encoder->pre_pll_enable)
6093 encoder->pre_pll_enable(encoder);
6094
a65347ba 6095 if (!intel_crtc->config->has_dsi_encoder) {
c0b4c660
VS
6096 if (IS_CHERRYVIEW(dev)) {
6097 chv_prepare_pll(intel_crtc, intel_crtc->config);
6e3c9717 6098 chv_enable_pll(intel_crtc, intel_crtc->config);
c0b4c660
VS
6099 } else {
6100 vlv_prepare_pll(intel_crtc, intel_crtc->config);
6e3c9717 6101 vlv_enable_pll(intel_crtc, intel_crtc->config);
c0b4c660 6102 }
9d556c99 6103 }
89b667f8
JB
6104
6105 for_each_encoder_on_crtc(dev, crtc, encoder)
6106 if (encoder->pre_enable)
6107 encoder->pre_enable(encoder);
6108
2dd24552
JB
6109 i9xx_pfit_enable(intel_crtc);
6110
8563b1e8 6111 intel_color_load_luts(crtc);
63cbb074 6112
caed361d 6113 intel_update_watermarks(crtc);
e1fdc473 6114 intel_enable_pipe(intel_crtc);
be6a6f8e 6115
4b3a9526
VS
6116 assert_vblank_disabled(crtc);
6117 drm_crtc_vblank_on(crtc);
6118
f9b61ff6
DV
6119 for_each_encoder_on_crtc(dev, crtc, encoder)
6120 encoder->enable(encoder);
89b667f8
JB
6121}
6122
f13c2ef3
DV
6123static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6124{
6125 struct drm_device *dev = crtc->base.dev;
6126 struct drm_i915_private *dev_priv = dev->dev_private;
6127
6e3c9717
ACO
6128 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6129 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
f13c2ef3
DV
6130}
6131
0b8765c6 6132static void i9xx_crtc_enable(struct drm_crtc *crtc)
79e53945
JB
6133{
6134 struct drm_device *dev = crtc->dev;
a72e4c9f 6135 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 6136 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 6137 struct intel_encoder *encoder;
79e53945 6138 int pipe = intel_crtc->pipe;
79e53945 6139
53d9f4e9 6140 if (WARN_ON(intel_crtc->active))
f7abfe8b
CW
6141 return;
6142
f13c2ef3
DV
6143 i9xx_set_pll_dividers(intel_crtc);
6144
6e3c9717 6145 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 6146 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
6147
6148 intel_set_pipe_timings(intel_crtc);
bc58be60 6149 intel_set_pipe_src_size(intel_crtc);
5b18e57c 6150
5b18e57c
DV
6151 i9xx_set_pipeconf(intel_crtc);
6152
f7abfe8b 6153 intel_crtc->active = true;
6b383a7f 6154
4a3436e8 6155 if (!IS_GEN2(dev))
a72e4c9f 6156 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6157
9d6d9f19
MK
6158 for_each_encoder_on_crtc(dev, crtc, encoder)
6159 if (encoder->pre_enable)
6160 encoder->pre_enable(encoder);
6161
f6736a1a
DV
6162 i9xx_enable_pll(intel_crtc);
6163
2dd24552
JB
6164 i9xx_pfit_enable(intel_crtc);
6165
8563b1e8 6166 intel_color_load_luts(crtc);
63cbb074 6167
f37fcc2a 6168 intel_update_watermarks(crtc);
e1fdc473 6169 intel_enable_pipe(intel_crtc);
be6a6f8e 6170
4b3a9526
VS
6171 assert_vblank_disabled(crtc);
6172 drm_crtc_vblank_on(crtc);
6173
f9b61ff6
DV
6174 for_each_encoder_on_crtc(dev, crtc, encoder)
6175 encoder->enable(encoder);
0b8765c6 6176}
79e53945 6177
87476d63
DV
6178static void i9xx_pfit_disable(struct intel_crtc *crtc)
6179{
6180 struct drm_device *dev = crtc->base.dev;
6181 struct drm_i915_private *dev_priv = dev->dev_private;
87476d63 6182
6e3c9717 6183 if (!crtc->config->gmch_pfit.control)
328d8e82 6184 return;
87476d63 6185
328d8e82 6186 assert_pipe_disabled(dev_priv, crtc->pipe);
87476d63 6187
328d8e82
DV
6188 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6189 I915_READ(PFIT_CONTROL));
6190 I915_WRITE(PFIT_CONTROL, 0);
87476d63
DV
6191}
6192
0b8765c6
JB
6193static void i9xx_crtc_disable(struct drm_crtc *crtc)
6194{
6195 struct drm_device *dev = crtc->dev;
6196 struct drm_i915_private *dev_priv = dev->dev_private;
6197 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 6198 struct intel_encoder *encoder;
0b8765c6 6199 int pipe = intel_crtc->pipe;
ef9c3aee 6200
6304cd91
VS
6201 /*
6202 * On gen2 planes are double buffered but the pipe isn't, so we must
6203 * wait for planes to fully turn off before disabling the pipe.
6204 */
90e83e53
ACO
6205 if (IS_GEN2(dev))
6206 intel_wait_for_vblank(dev, pipe);
6304cd91 6207
4b3a9526
VS
6208 for_each_encoder_on_crtc(dev, crtc, encoder)
6209 encoder->disable(encoder);
6210
f9b61ff6
DV
6211 drm_crtc_vblank_off(crtc);
6212 assert_vblank_disabled(crtc);
6213
575f7ab7 6214 intel_disable_pipe(intel_crtc);
24a1f16d 6215
87476d63 6216 i9xx_pfit_disable(intel_crtc);
24a1f16d 6217
89b667f8
JB
6218 for_each_encoder_on_crtc(dev, crtc, encoder)
6219 if (encoder->post_disable)
6220 encoder->post_disable(encoder);
6221
a65347ba 6222 if (!intel_crtc->config->has_dsi_encoder) {
076ed3b2
CML
6223 if (IS_CHERRYVIEW(dev))
6224 chv_disable_pll(dev_priv, pipe);
6225 else if (IS_VALLEYVIEW(dev))
6226 vlv_disable_pll(dev_priv, pipe);
6227 else
1c4e0274 6228 i9xx_disable_pll(intel_crtc);
076ed3b2 6229 }
0b8765c6 6230
d6db995f
VS
6231 for_each_encoder_on_crtc(dev, crtc, encoder)
6232 if (encoder->post_pll_disable)
6233 encoder->post_pll_disable(encoder);
6234
4a3436e8 6235 if (!IS_GEN2(dev))
a72e4c9f 6236 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
0b8765c6
JB
6237}
6238
b17d48e2
ML
6239static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
6240{
842e0307 6241 struct intel_encoder *encoder;
b17d48e2
ML
6242 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6243 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6244 enum intel_display_power_domain domain;
6245 unsigned long domains;
6246
6247 if (!intel_crtc->active)
6248 return;
6249
a539205a 6250 if (to_intel_plane_state(crtc->primary->state)->visible) {
fc32b1fd
ML
6251 WARN_ON(intel_crtc->unpin_work);
6252
2622a081 6253 intel_pre_disable_primary_noatomic(crtc);
54a41961
ML
6254
6255 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
6256 to_intel_plane_state(crtc->primary->state)->visible = false;
a539205a
ML
6257 }
6258
b17d48e2 6259 dev_priv->display.crtc_disable(crtc);
842e0307
ML
6260
6261 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was enabled, now disabled\n",
6262 crtc->base.id);
6263
6264 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6265 crtc->state->active = false;
37d9078b 6266 intel_crtc->active = false;
842e0307
ML
6267 crtc->enabled = false;
6268 crtc->state->connector_mask = 0;
6269 crtc->state->encoder_mask = 0;
6270
6271 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6272 encoder->base.crtc = NULL;
6273
58f9c0bc 6274 intel_fbc_disable(intel_crtc);
37d9078b 6275 intel_update_watermarks(crtc);
1f7457b1 6276 intel_disable_shared_dpll(intel_crtc);
b17d48e2
ML
6277
6278 domains = intel_crtc->enabled_power_domains;
6279 for_each_power_domain(domain, domains)
6280 intel_display_power_put(dev_priv, domain);
6281 intel_crtc->enabled_power_domains = 0;
565602d7
ML
6282
6283 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
6284 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
b17d48e2
ML
6285}
6286
6b72d486
ML
6287/*
6288 * turn all crtc's off, but do not adjust state
6289 * This has to be paired with a call to intel_modeset_setup_hw_state.
6290 */
70e0bd74 6291int intel_display_suspend(struct drm_device *dev)
ee7b9f93 6292{
e2c8b870 6293 struct drm_i915_private *dev_priv = to_i915(dev);
70e0bd74 6294 struct drm_atomic_state *state;
e2c8b870 6295 int ret;
70e0bd74 6296
e2c8b870
ML
6297 state = drm_atomic_helper_suspend(dev);
6298 ret = PTR_ERR_OR_ZERO(state);
70e0bd74
ML
6299 if (ret)
6300 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
e2c8b870
ML
6301 else
6302 dev_priv->modeset_restore_state = state;
70e0bd74 6303 return ret;
ee7b9f93
JB
6304}
6305
ea5b213a 6306void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 6307{
4ef69c7a 6308 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 6309
ea5b213a
CW
6310 drm_encoder_cleanup(encoder);
6311 kfree(intel_encoder);
7e7d76c3
JB
6312}
6313
0a91ca29
DV
6314/* Cross check the actual hw state with our own modeset state tracking (and it's
6315 * internal consistency). */
b980514c 6316static void intel_connector_check_state(struct intel_connector *connector)
79e53945 6317{
35dd3c64
ML
6318 struct drm_crtc *crtc = connector->base.state->crtc;
6319
6320 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6321 connector->base.base.id,
6322 connector->base.name);
6323
0a91ca29 6324 if (connector->get_hw_state(connector)) {
e85376cb 6325 struct intel_encoder *encoder = connector->encoder;
35dd3c64 6326 struct drm_connector_state *conn_state = connector->base.state;
0a91ca29 6327
35dd3c64
ML
6328 I915_STATE_WARN(!crtc,
6329 "connector enabled without attached crtc\n");
0a91ca29 6330
35dd3c64
ML
6331 if (!crtc)
6332 return;
6333
6334 I915_STATE_WARN(!crtc->state->active,
6335 "connector is active, but attached crtc isn't\n");
6336
e85376cb 6337 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
35dd3c64
ML
6338 return;
6339
e85376cb 6340 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
35dd3c64
ML
6341 "atomic encoder doesn't match attached encoder\n");
6342
e85376cb 6343 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
35dd3c64
ML
6344 "attached encoder crtc differs from connector crtc\n");
6345 } else {
4d688a2a
ML
6346 I915_STATE_WARN(crtc && crtc->state->active,
6347 "attached crtc is active, but connector isn't\n");
35dd3c64
ML
6348 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
6349 "best encoder set without crtc!\n");
0a91ca29 6350 }
79e53945
JB
6351}
6352
08d9bc92
ACO
6353int intel_connector_init(struct intel_connector *connector)
6354{
5350a031 6355 drm_atomic_helper_connector_reset(&connector->base);
08d9bc92 6356
5350a031 6357 if (!connector->base.state)
08d9bc92
ACO
6358 return -ENOMEM;
6359
08d9bc92
ACO
6360 return 0;
6361}
6362
6363struct intel_connector *intel_connector_alloc(void)
6364{
6365 struct intel_connector *connector;
6366
6367 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6368 if (!connector)
6369 return NULL;
6370
6371 if (intel_connector_init(connector) < 0) {
6372 kfree(connector);
6373 return NULL;
6374 }
6375
6376 return connector;
6377}
6378
f0947c37
DV
6379/* Simple connector->get_hw_state implementation for encoders that support only
6380 * one connector and no cloning and hence the encoder state determines the state
6381 * of the connector. */
6382bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 6383{
24929352 6384 enum pipe pipe = 0;
f0947c37 6385 struct intel_encoder *encoder = connector->encoder;
ea5b213a 6386
f0947c37 6387 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
6388}
6389
6d293983 6390static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
d272ddfa 6391{
6d293983
ACO
6392 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6393 return crtc_state->fdi_lanes;
d272ddfa
VS
6394
6395 return 0;
6396}
6397
6d293983 6398static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5cec258b 6399 struct intel_crtc_state *pipe_config)
1857e1da 6400{
6d293983
ACO
6401 struct drm_atomic_state *state = pipe_config->base.state;
6402 struct intel_crtc *other_crtc;
6403 struct intel_crtc_state *other_crtc_state;
6404
1857e1da
DV
6405 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6406 pipe_name(pipe), pipe_config->fdi_lanes);
6407 if (pipe_config->fdi_lanes > 4) {
6408 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6409 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6410 return -EINVAL;
1857e1da
DV
6411 }
6412
bafb6553 6413 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
1857e1da
DV
6414 if (pipe_config->fdi_lanes > 2) {
6415 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6416 pipe_config->fdi_lanes);
6d293983 6417 return -EINVAL;
1857e1da 6418 } else {
6d293983 6419 return 0;
1857e1da
DV
6420 }
6421 }
6422
6423 if (INTEL_INFO(dev)->num_pipes == 2)
6d293983 6424 return 0;
1857e1da
DV
6425
6426 /* Ivybridge 3 pipe is really complicated */
6427 switch (pipe) {
6428 case PIPE_A:
6d293983 6429 return 0;
1857e1da 6430 case PIPE_B:
6d293983
ACO
6431 if (pipe_config->fdi_lanes <= 2)
6432 return 0;
6433
6434 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6435 other_crtc_state =
6436 intel_atomic_get_crtc_state(state, other_crtc);
6437 if (IS_ERR(other_crtc_state))
6438 return PTR_ERR(other_crtc_state);
6439
6440 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
1857e1da
DV
6441 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6442 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6443 return -EINVAL;
1857e1da 6444 }
6d293983 6445 return 0;
1857e1da 6446 case PIPE_C:
251cc67c
VS
6447 if (pipe_config->fdi_lanes > 2) {
6448 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6449 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6450 return -EINVAL;
251cc67c 6451 }
6d293983
ACO
6452
6453 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6454 other_crtc_state =
6455 intel_atomic_get_crtc_state(state, other_crtc);
6456 if (IS_ERR(other_crtc_state))
6457 return PTR_ERR(other_crtc_state);
6458
6459 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
1857e1da 6460 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
6d293983 6461 return -EINVAL;
1857e1da 6462 }
6d293983 6463 return 0;
1857e1da
DV
6464 default:
6465 BUG();
6466 }
6467}
6468
e29c22c0
DV
6469#define RETRY 1
6470static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5cec258b 6471 struct intel_crtc_state *pipe_config)
877d48d5 6472{
1857e1da 6473 struct drm_device *dev = intel_crtc->base.dev;
7c5f93b0 6474 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6d293983
ACO
6475 int lane, link_bw, fdi_dotclock, ret;
6476 bool needs_recompute = false;
877d48d5 6477
e29c22c0 6478retry:
877d48d5
DV
6479 /* FDI is a binary signal running at ~2.7GHz, encoding
6480 * each output octet as 10 bits. The actual frequency
6481 * is stored as a divider into a 100MHz clock, and the
6482 * mode pixel clock is stored in units of 1KHz.
6483 * Hence the bw of each lane in terms of the mode signal
6484 * is:
6485 */
21a727b3 6486 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
877d48d5 6487
241bfc38 6488 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 6489
2bd89a07 6490 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
DV
6491 pipe_config->pipe_bpp);
6492
6493 pipe_config->fdi_lanes = lane;
6494
2bd89a07 6495 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
877d48d5 6496 link_bw, &pipe_config->fdi_m_n);
1857e1da 6497
e3b247da 6498 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
6d293983 6499 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
e29c22c0
DV
6500 pipe_config->pipe_bpp -= 2*3;
6501 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6502 pipe_config->pipe_bpp);
6503 needs_recompute = true;
6504 pipe_config->bw_constrained = true;
6505
6506 goto retry;
6507 }
6508
6509 if (needs_recompute)
6510 return RETRY;
6511
6d293983 6512 return ret;
877d48d5
DV
6513}
6514
8cfb3407
VS
6515static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6516 struct intel_crtc_state *pipe_config)
6517{
6518 if (pipe_config->pipe_bpp > 24)
6519 return false;
6520
6521 /* HSW can handle pixel rate up to cdclk? */
6522 if (IS_HASWELL(dev_priv->dev))
6523 return true;
6524
6525 /*
b432e5cf
VS
6526 * We compare against max which means we must take
6527 * the increased cdclk requirement into account when
6528 * calculating the new cdclk.
6529 *
6530 * Should measure whether using a lower cdclk w/o IPS
8cfb3407
VS
6531 */
6532 return ilk_pipe_pixel_rate(pipe_config) <=
6533 dev_priv->max_cdclk_freq * 95 / 100;
6534}
6535
42db64ef 6536static void hsw_compute_ips_config(struct intel_crtc *crtc,
5cec258b 6537 struct intel_crtc_state *pipe_config)
42db64ef 6538{
8cfb3407
VS
6539 struct drm_device *dev = crtc->base.dev;
6540 struct drm_i915_private *dev_priv = dev->dev_private;
6541
d330a953 6542 pipe_config->ips_enabled = i915.enable_ips &&
8cfb3407
VS
6543 hsw_crtc_supports_ips(crtc) &&
6544 pipe_config_supports_ips(dev_priv, pipe_config);
42db64ef
PZ
6545}
6546
39acb4aa
VS
6547static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
6548{
6549 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6550
6551 /* GDG double wide on either pipe, otherwise pipe A only */
6552 return INTEL_INFO(dev_priv)->gen < 4 &&
6553 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
6554}
6555
a43f6e0f 6556static int intel_crtc_compute_config(struct intel_crtc *crtc,
5cec258b 6557 struct intel_crtc_state *pipe_config)
79e53945 6558{
a43f6e0f 6559 struct drm_device *dev = crtc->base.dev;
8bd31e67 6560 struct drm_i915_private *dev_priv = dev->dev_private;
7c5f93b0 6561 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
89749350 6562
ad3a4479 6563 /* FIXME should check pixel clock limits on all platforms */
cf532bb2 6564 if (INTEL_INFO(dev)->gen < 4) {
39acb4aa 6565 int clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
cf532bb2
VS
6566
6567 /*
39acb4aa 6568 * Enable double wide mode when the dot clock
cf532bb2 6569 * is > 90% of the (display) core speed.
cf532bb2 6570 */
39acb4aa
VS
6571 if (intel_crtc_supports_double_wide(crtc) &&
6572 adjusted_mode->crtc_clock > clock_limit) {
ad3a4479 6573 clock_limit *= 2;
cf532bb2 6574 pipe_config->double_wide = true;
ad3a4479
VS
6575 }
6576
39acb4aa
VS
6577 if (adjusted_mode->crtc_clock > clock_limit) {
6578 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6579 adjusted_mode->crtc_clock, clock_limit,
6580 yesno(pipe_config->double_wide));
e29c22c0 6581 return -EINVAL;
39acb4aa 6582 }
2c07245f 6583 }
89749350 6584
1d1d0e27
VS
6585 /*
6586 * Pipe horizontal size must be even in:
6587 * - DVO ganged mode
6588 * - LVDS dual channel mode
6589 * - Double wide pipe
6590 */
a93e255f 6591 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
1d1d0e27
VS
6592 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6593 pipe_config->pipe_src_w &= ~1;
6594
8693a824
DL
6595 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6596 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
44f46b42
CW
6597 */
6598 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
aad941d5 6599 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
e29c22c0 6600 return -EINVAL;
44f46b42 6601
f5adf94e 6602 if (HAS_IPS(dev))
a43f6e0f
DV
6603 hsw_compute_ips_config(crtc, pipe_config);
6604
877d48d5 6605 if (pipe_config->has_pch_encoder)
a43f6e0f 6606 return ironlake_fdi_compute_config(crtc, pipe_config);
877d48d5 6607
cf5a15be 6608 return 0;
79e53945
JB
6609}
6610
1652d19e
VS
6611static int skylake_get_display_clock_speed(struct drm_device *dev)
6612{
6613 struct drm_i915_private *dev_priv = to_i915(dev);
6614 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6615 uint32_t cdctl = I915_READ(CDCLK_CTL);
6616 uint32_t linkrate;
6617
414355a7 6618 if (!(lcpll1 & LCPLL_PLL_ENABLE))
1652d19e 6619 return 24000; /* 24MHz is the cd freq with NSSC ref */
1652d19e
VS
6620
6621 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6622 return 540000;
6623
6624 linkrate = (I915_READ(DPLL_CTRL1) &
71cd8423 6625 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
1652d19e 6626
71cd8423
DL
6627 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6628 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
1652d19e
VS
6629 /* vco 8640 */
6630 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6631 case CDCLK_FREQ_450_432:
6632 return 432000;
6633 case CDCLK_FREQ_337_308:
6634 return 308570;
6635 case CDCLK_FREQ_675_617:
6636 return 617140;
6637 default:
6638 WARN(1, "Unknown cd freq selection\n");
6639 }
6640 } else {
6641 /* vco 8100 */
6642 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6643 case CDCLK_FREQ_450_432:
6644 return 450000;
6645 case CDCLK_FREQ_337_308:
6646 return 337500;
6647 case CDCLK_FREQ_675_617:
6648 return 675000;
6649 default:
6650 WARN(1, "Unknown cd freq selection\n");
6651 }
6652 }
6653
6654 /* error case, do as if DPLL0 isn't enabled */
6655 return 24000;
6656}
6657
acd3f3d3
BP
6658static int broxton_get_display_clock_speed(struct drm_device *dev)
6659{
6660 struct drm_i915_private *dev_priv = to_i915(dev);
6661 uint32_t cdctl = I915_READ(CDCLK_CTL);
6662 uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
6663 uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
6664 int cdclk;
6665
6666 if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
6667 return 19200;
6668
6669 cdclk = 19200 * pll_ratio / 2;
6670
6671 switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
6672 case BXT_CDCLK_CD2X_DIV_SEL_1:
6673 return cdclk; /* 576MHz or 624MHz */
6674 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
6675 return cdclk * 2 / 3; /* 384MHz */
6676 case BXT_CDCLK_CD2X_DIV_SEL_2:
6677 return cdclk / 2; /* 288MHz */
6678 case BXT_CDCLK_CD2X_DIV_SEL_4:
6679 return cdclk / 4; /* 144MHz */
6680 }
6681
6682 /* error case, do as if DE PLL isn't enabled */
6683 return 19200;
6684}
6685
1652d19e
VS
6686static int broadwell_get_display_clock_speed(struct drm_device *dev)
6687{
6688 struct drm_i915_private *dev_priv = dev->dev_private;
6689 uint32_t lcpll = I915_READ(LCPLL_CTL);
6690 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6691
6692 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6693 return 800000;
6694 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6695 return 450000;
6696 else if (freq == LCPLL_CLK_FREQ_450)
6697 return 450000;
6698 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6699 return 540000;
6700 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6701 return 337500;
6702 else
6703 return 675000;
6704}
6705
6706static int haswell_get_display_clock_speed(struct drm_device *dev)
6707{
6708 struct drm_i915_private *dev_priv = dev->dev_private;
6709 uint32_t lcpll = I915_READ(LCPLL_CTL);
6710 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6711
6712 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6713 return 800000;
6714 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6715 return 450000;
6716 else if (freq == LCPLL_CLK_FREQ_450)
6717 return 450000;
6718 else if (IS_HSW_ULT(dev))
6719 return 337500;
6720 else
6721 return 540000;
79e53945
JB
6722}
6723
25eb05fc
JB
6724static int valleyview_get_display_clock_speed(struct drm_device *dev)
6725{
bfa7df01
VS
6726 return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
6727 CCK_DISPLAY_CLOCK_CONTROL);
25eb05fc
JB
6728}
6729
b37a6434
VS
6730static int ilk_get_display_clock_speed(struct drm_device *dev)
6731{
6732 return 450000;
6733}
6734
e70236a8
JB
6735static int i945_get_display_clock_speed(struct drm_device *dev)
6736{
6737 return 400000;
6738}
79e53945 6739
e70236a8 6740static int i915_get_display_clock_speed(struct drm_device *dev)
79e53945 6741{
e907f170 6742 return 333333;
e70236a8 6743}
79e53945 6744
e70236a8
JB
6745static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6746{
6747 return 200000;
6748}
79e53945 6749
257a7ffc
DV
6750static int pnv_get_display_clock_speed(struct drm_device *dev)
6751{
6752 u16 gcfgc = 0;
6753
6754 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6755
6756 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6757 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
e907f170 6758 return 266667;
257a7ffc 6759 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
e907f170 6760 return 333333;
257a7ffc 6761 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
e907f170 6762 return 444444;
257a7ffc
DV
6763 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6764 return 200000;
6765 default:
6766 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6767 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
e907f170 6768 return 133333;
257a7ffc 6769 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
e907f170 6770 return 166667;
257a7ffc
DV
6771 }
6772}
6773
e70236a8
JB
6774static int i915gm_get_display_clock_speed(struct drm_device *dev)
6775{
6776 u16 gcfgc = 0;
79e53945 6777
e70236a8
JB
6778 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6779
6780 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
e907f170 6781 return 133333;
e70236a8
JB
6782 else {
6783 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6784 case GC_DISPLAY_CLOCK_333_MHZ:
e907f170 6785 return 333333;
e70236a8
JB
6786 default:
6787 case GC_DISPLAY_CLOCK_190_200_MHZ:
6788 return 190000;
79e53945 6789 }
e70236a8
JB
6790 }
6791}
6792
6793static int i865_get_display_clock_speed(struct drm_device *dev)
6794{
e907f170 6795 return 266667;
e70236a8
JB
6796}
6797
1b1d2716 6798static int i85x_get_display_clock_speed(struct drm_device *dev)
e70236a8
JB
6799{
6800 u16 hpllcc = 0;
1b1d2716 6801
65cd2b3f
VS
6802 /*
6803 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6804 * encoding is different :(
6805 * FIXME is this the right way to detect 852GM/852GMV?
6806 */
6807 if (dev->pdev->revision == 0x1)
6808 return 133333;
6809
1b1d2716
VS
6810 pci_bus_read_config_word(dev->pdev->bus,
6811 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6812
e70236a8
JB
6813 /* Assume that the hardware is in the high speed state. This
6814 * should be the default.
6815 */
6816 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6817 case GC_CLOCK_133_200:
1b1d2716 6818 case GC_CLOCK_133_200_2:
e70236a8
JB
6819 case GC_CLOCK_100_200:
6820 return 200000;
6821 case GC_CLOCK_166_250:
6822 return 250000;
6823 case GC_CLOCK_100_133:
e907f170 6824 return 133333;
1b1d2716
VS
6825 case GC_CLOCK_133_266:
6826 case GC_CLOCK_133_266_2:
6827 case GC_CLOCK_166_266:
6828 return 266667;
e70236a8 6829 }
79e53945 6830
e70236a8
JB
6831 /* Shouldn't happen */
6832 return 0;
6833}
79e53945 6834
e70236a8
JB
6835static int i830_get_display_clock_speed(struct drm_device *dev)
6836{
e907f170 6837 return 133333;
79e53945
JB
6838}
6839
34edce2f
VS
6840static unsigned int intel_hpll_vco(struct drm_device *dev)
6841{
6842 struct drm_i915_private *dev_priv = dev->dev_private;
6843 static const unsigned int blb_vco[8] = {
6844 [0] = 3200000,
6845 [1] = 4000000,
6846 [2] = 5333333,
6847 [3] = 4800000,
6848 [4] = 6400000,
6849 };
6850 static const unsigned int pnv_vco[8] = {
6851 [0] = 3200000,
6852 [1] = 4000000,
6853 [2] = 5333333,
6854 [3] = 4800000,
6855 [4] = 2666667,
6856 };
6857 static const unsigned int cl_vco[8] = {
6858 [0] = 3200000,
6859 [1] = 4000000,
6860 [2] = 5333333,
6861 [3] = 6400000,
6862 [4] = 3333333,
6863 [5] = 3566667,
6864 [6] = 4266667,
6865 };
6866 static const unsigned int elk_vco[8] = {
6867 [0] = 3200000,
6868 [1] = 4000000,
6869 [2] = 5333333,
6870 [3] = 4800000,
6871 };
6872 static const unsigned int ctg_vco[8] = {
6873 [0] = 3200000,
6874 [1] = 4000000,
6875 [2] = 5333333,
6876 [3] = 6400000,
6877 [4] = 2666667,
6878 [5] = 4266667,
6879 };
6880 const unsigned int *vco_table;
6881 unsigned int vco;
6882 uint8_t tmp = 0;
6883
6884 /* FIXME other chipsets? */
6885 if (IS_GM45(dev))
6886 vco_table = ctg_vco;
6887 else if (IS_G4X(dev))
6888 vco_table = elk_vco;
6889 else if (IS_CRESTLINE(dev))
6890 vco_table = cl_vco;
6891 else if (IS_PINEVIEW(dev))
6892 vco_table = pnv_vco;
6893 else if (IS_G33(dev))
6894 vco_table = blb_vco;
6895 else
6896 return 0;
6897
6898 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6899
6900 vco = vco_table[tmp & 0x7];
6901 if (vco == 0)
6902 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6903 else
6904 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6905
6906 return vco;
6907}
6908
6909static int gm45_get_display_clock_speed(struct drm_device *dev)
6910{
6911 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6912 uint16_t tmp = 0;
6913
6914 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6915
6916 cdclk_sel = (tmp >> 12) & 0x1;
6917
6918 switch (vco) {
6919 case 2666667:
6920 case 4000000:
6921 case 5333333:
6922 return cdclk_sel ? 333333 : 222222;
6923 case 3200000:
6924 return cdclk_sel ? 320000 : 228571;
6925 default:
6926 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
6927 return 222222;
6928 }
6929}
6930
6931static int i965gm_get_display_clock_speed(struct drm_device *dev)
6932{
6933 static const uint8_t div_3200[] = { 16, 10, 8 };
6934 static const uint8_t div_4000[] = { 20, 12, 10 };
6935 static const uint8_t div_5333[] = { 24, 16, 14 };
6936 const uint8_t *div_table;
6937 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6938 uint16_t tmp = 0;
6939
6940 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6941
6942 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
6943
6944 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6945 goto fail;
6946
6947 switch (vco) {
6948 case 3200000:
6949 div_table = div_3200;
6950 break;
6951 case 4000000:
6952 div_table = div_4000;
6953 break;
6954 case 5333333:
6955 div_table = div_5333;
6956 break;
6957 default:
6958 goto fail;
6959 }
6960
6961 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6962
caf4e252 6963fail:
34edce2f
VS
6964 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
6965 return 200000;
6966}
6967
6968static int g33_get_display_clock_speed(struct drm_device *dev)
6969{
6970 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
6971 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
6972 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
6973 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
6974 const uint8_t *div_table;
6975 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6976 uint16_t tmp = 0;
6977
6978 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6979
6980 cdclk_sel = (tmp >> 4) & 0x7;
6981
6982 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6983 goto fail;
6984
6985 switch (vco) {
6986 case 3200000:
6987 div_table = div_3200;
6988 break;
6989 case 4000000:
6990 div_table = div_4000;
6991 break;
6992 case 4800000:
6993 div_table = div_4800;
6994 break;
6995 case 5333333:
6996 div_table = div_5333;
6997 break;
6998 default:
6999 goto fail;
7000 }
7001
7002 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7003
caf4e252 7004fail:
34edce2f
VS
7005 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7006 return 190476;
7007}
7008
2c07245f 7009static void
a65851af 7010intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 7011{
a65851af
VS
7012 while (*num > DATA_LINK_M_N_MASK ||
7013 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
7014 *num >>= 1;
7015 *den >>= 1;
7016 }
7017}
7018
a65851af
VS
7019static void compute_m_n(unsigned int m, unsigned int n,
7020 uint32_t *ret_m, uint32_t *ret_n)
7021{
7022 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7023 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7024 intel_reduce_m_n_ratio(ret_m, ret_n);
7025}
7026
e69d0bc1
DV
7027void
7028intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7029 int pixel_clock, int link_clock,
7030 struct intel_link_m_n *m_n)
2c07245f 7031{
e69d0bc1 7032 m_n->tu = 64;
a65851af
VS
7033
7034 compute_m_n(bits_per_pixel * pixel_clock,
7035 link_clock * nlanes * 8,
7036 &m_n->gmch_m, &m_n->gmch_n);
7037
7038 compute_m_n(pixel_clock, link_clock,
7039 &m_n->link_m, &m_n->link_n);
2c07245f
ZW
7040}
7041
a7615030
CW
7042static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7043{
d330a953
JN
7044 if (i915.panel_use_ssc >= 0)
7045 return i915.panel_use_ssc != 0;
41aa3448 7046 return dev_priv->vbt.lvds_use_ssc
435793df 7047 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
7048}
7049
ceb41007 7050static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state)
c65d77d8 7051{
a93e255f 7052 struct drm_device *dev = crtc_state->base.crtc->dev;
c65d77d8
JB
7053 struct drm_i915_private *dev_priv = dev->dev_private;
7054 int refclk;
7055
a93e255f
ACO
7056 WARN_ON(!crtc_state->base.state);
7057
65b3d6a9 7058 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 7059 intel_panel_use_ssc(dev_priv)) {
e91e941b
VS
7060 refclk = dev_priv->vbt.lvds_ssc_freq;
7061 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
c65d77d8
JB
7062 } else if (!IS_GEN2(dev)) {
7063 refclk = 96000;
7064 } else {
7065 refclk = 48000;
7066 }
7067
7068 return refclk;
7069}
7070
7429e9d4 7071static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 7072{
7df00d7a 7073 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 7074}
f47709a9 7075
7429e9d4
DV
7076static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7077{
7078 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
7079}
7080
f47709a9 7081static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
190f68c5 7082 struct intel_crtc_state *crtc_state,
a7516a05
JB
7083 intel_clock_t *reduced_clock)
7084{
f47709a9 7085 struct drm_device *dev = crtc->base.dev;
a7516a05
JB
7086 u32 fp, fp2 = 0;
7087
7088 if (IS_PINEVIEW(dev)) {
190f68c5 7089 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7090 if (reduced_clock)
7429e9d4 7091 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 7092 } else {
190f68c5 7093 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7094 if (reduced_clock)
7429e9d4 7095 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
7096 }
7097
190f68c5 7098 crtc_state->dpll_hw_state.fp0 = fp;
a7516a05 7099
f47709a9 7100 crtc->lowfreq_avail = false;
a93e255f 7101 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ab585dea 7102 reduced_clock) {
190f68c5 7103 crtc_state->dpll_hw_state.fp1 = fp2;
f47709a9 7104 crtc->lowfreq_avail = true;
a7516a05 7105 } else {
190f68c5 7106 crtc_state->dpll_hw_state.fp1 = fp;
a7516a05
JB
7107 }
7108}
7109
5e69f97f
CML
7110static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7111 pipe)
89b667f8
JB
7112{
7113 u32 reg_val;
7114
7115 /*
7116 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7117 * and set it to a reasonable value instead.
7118 */
ab3c759a 7119 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
7120 reg_val &= 0xffffff00;
7121 reg_val |= 0x00000030;
ab3c759a 7122 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7123
ab3c759a 7124 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7125 reg_val &= 0x8cffffff;
7126 reg_val = 0x8c000000;
ab3c759a 7127 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 7128
ab3c759a 7129 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 7130 reg_val &= 0xffffff00;
ab3c759a 7131 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7132
ab3c759a 7133 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7134 reg_val &= 0x00ffffff;
7135 reg_val |= 0xb0000000;
ab3c759a 7136 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
7137}
7138
b551842d
DV
7139static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7140 struct intel_link_m_n *m_n)
7141{
7142 struct drm_device *dev = crtc->base.dev;
7143 struct drm_i915_private *dev_priv = dev->dev_private;
7144 int pipe = crtc->pipe;
7145
e3b95f1e
DV
7146 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7147 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7148 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7149 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
7150}
7151
7152static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
7153 struct intel_link_m_n *m_n,
7154 struct intel_link_m_n *m2_n2)
b551842d
DV
7155{
7156 struct drm_device *dev = crtc->base.dev;
7157 struct drm_i915_private *dev_priv = dev->dev_private;
7158 int pipe = crtc->pipe;
6e3c9717 7159 enum transcoder transcoder = crtc->config->cpu_transcoder;
b551842d
DV
7160
7161 if (INTEL_INFO(dev)->gen >= 5) {
7162 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7163 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7164 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7165 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
f769cd24
VK
7166 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7167 * for gen < 8) and if DRRS is supported (to make sure the
7168 * registers are not unnecessarily accessed).
7169 */
44395bfe 7170 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
6e3c9717 7171 crtc->config->has_drrs) {
f769cd24
VK
7172 I915_WRITE(PIPE_DATA_M2(transcoder),
7173 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7174 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7175 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7176 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7177 }
b551842d 7178 } else {
e3b95f1e
DV
7179 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7180 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7181 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7182 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
7183 }
7184}
7185
fe3cd48d 7186void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
03afc4a2 7187{
fe3cd48d
R
7188 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7189
7190 if (m_n == M1_N1) {
7191 dp_m_n = &crtc->config->dp_m_n;
7192 dp_m2_n2 = &crtc->config->dp_m2_n2;
7193 } else if (m_n == M2_N2) {
7194
7195 /*
7196 * M2_N2 registers are not supported. Hence m2_n2 divider value
7197 * needs to be programmed into M1_N1.
7198 */
7199 dp_m_n = &crtc->config->dp_m2_n2;
7200 } else {
7201 DRM_ERROR("Unsupported divider value\n");
7202 return;
7203 }
7204
6e3c9717
ACO
7205 if (crtc->config->has_pch_encoder)
7206 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
03afc4a2 7207 else
fe3cd48d 7208 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
03afc4a2
DV
7209}
7210
251ac862
DV
7211static void vlv_compute_dpll(struct intel_crtc *crtc,
7212 struct intel_crtc_state *pipe_config)
bdd4b6a6
DV
7213{
7214 u32 dpll, dpll_md;
7215
7216 /*
7217 * Enable DPIO clock input. We should never disable the reference
7218 * clock for pipe B, since VGA hotplug / manual detection depends
7219 * on it.
7220 */
60bfe44f
VS
7221 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REF_CLK_ENABLE_VLV |
7222 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_REF_CLK_VLV;
bdd4b6a6
DV
7223 /* We should never disable this, set it here for state tracking */
7224 if (crtc->pipe == PIPE_B)
7225 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7226 dpll |= DPLL_VCO_ENABLE;
d288f65f 7227 pipe_config->dpll_hw_state.dpll = dpll;
bdd4b6a6 7228
d288f65f 7229 dpll_md = (pipe_config->pixel_multiplier - 1)
bdd4b6a6 7230 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
d288f65f 7231 pipe_config->dpll_hw_state.dpll_md = dpll_md;
bdd4b6a6
DV
7232}
7233
d288f65f 7234static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7235 const struct intel_crtc_state *pipe_config)
a0c4da24 7236{
f47709a9 7237 struct drm_device *dev = crtc->base.dev;
a0c4da24 7238 struct drm_i915_private *dev_priv = dev->dev_private;
f47709a9 7239 int pipe = crtc->pipe;
bdd4b6a6 7240 u32 mdiv;
a0c4da24 7241 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 7242 u32 coreclk, reg_val;
a0c4da24 7243
a580516d 7244 mutex_lock(&dev_priv->sb_lock);
09153000 7245
d288f65f
VS
7246 bestn = pipe_config->dpll.n;
7247 bestm1 = pipe_config->dpll.m1;
7248 bestm2 = pipe_config->dpll.m2;
7249 bestp1 = pipe_config->dpll.p1;
7250 bestp2 = pipe_config->dpll.p2;
a0c4da24 7251
89b667f8
JB
7252 /* See eDP HDMI DPIO driver vbios notes doc */
7253
7254 /* PLL B needs special handling */
bdd4b6a6 7255 if (pipe == PIPE_B)
5e69f97f 7256 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
7257
7258 /* Set up Tx target for periodic Rcomp update */
ab3c759a 7259 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
7260
7261 /* Disable target IRef on PLL */
ab3c759a 7262 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 7263 reg_val &= 0x00ffffff;
ab3c759a 7264 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
7265
7266 /* Disable fast lock */
ab3c759a 7267 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
7268
7269 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
7270 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7271 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7272 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 7273 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
7274
7275 /*
7276 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7277 * but we don't support that).
7278 * Note: don't use the DAC post divider as it seems unstable.
7279 */
7280 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 7281 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7282
a0c4da24 7283 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 7284 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7285
89b667f8 7286 /* Set HBR and RBR LPF coefficients */
d288f65f 7287 if (pipe_config->port_clock == 162000 ||
409ee761
ACO
7288 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7289 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
ab3c759a 7290 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 7291 0x009f0003);
89b667f8 7292 else
ab3c759a 7293 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
7294 0x00d0000f);
7295
681a8504 7296 if (pipe_config->has_dp_encoder) {
89b667f8 7297 /* Use SSC source */
bdd4b6a6 7298 if (pipe == PIPE_A)
ab3c759a 7299 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7300 0x0df40000);
7301 else
ab3c759a 7302 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7303 0x0df70000);
7304 } else { /* HDMI or VGA */
7305 /* Use bend source */
bdd4b6a6 7306 if (pipe == PIPE_A)
ab3c759a 7307 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7308 0x0df70000);
7309 else
ab3c759a 7310 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7311 0x0df40000);
7312 }
a0c4da24 7313
ab3c759a 7314 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8 7315 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
409ee761
ACO
7316 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7317 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
89b667f8 7318 coreclk |= 0x01000000;
ab3c759a 7319 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 7320
ab3c759a 7321 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
a580516d 7322 mutex_unlock(&dev_priv->sb_lock);
a0c4da24
JB
7323}
7324
251ac862
DV
7325static void chv_compute_dpll(struct intel_crtc *crtc,
7326 struct intel_crtc_state *pipe_config)
1ae0d137 7327{
60bfe44f
VS
7328 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
7329 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
1ae0d137
VS
7330 DPLL_VCO_ENABLE;
7331 if (crtc->pipe != PIPE_A)
d288f65f 7332 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
1ae0d137 7333
d288f65f
VS
7334 pipe_config->dpll_hw_state.dpll_md =
7335 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
1ae0d137
VS
7336}
7337
d288f65f 7338static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7339 const struct intel_crtc_state *pipe_config)
9d556c99
CML
7340{
7341 struct drm_device *dev = crtc->base.dev;
7342 struct drm_i915_private *dev_priv = dev->dev_private;
7343 int pipe = crtc->pipe;
f0f59a00 7344 i915_reg_t dpll_reg = DPLL(crtc->pipe);
9d556c99 7345 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9cbe40c1 7346 u32 loopfilter, tribuf_calcntr;
9d556c99 7347 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
a945ce7e 7348 u32 dpio_val;
9cbe40c1 7349 int vco;
9d556c99 7350
d288f65f
VS
7351 bestn = pipe_config->dpll.n;
7352 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7353 bestm1 = pipe_config->dpll.m1;
7354 bestm2 = pipe_config->dpll.m2 >> 22;
7355 bestp1 = pipe_config->dpll.p1;
7356 bestp2 = pipe_config->dpll.p2;
9cbe40c1 7357 vco = pipe_config->dpll.vco;
a945ce7e 7358 dpio_val = 0;
9cbe40c1 7359 loopfilter = 0;
9d556c99
CML
7360
7361 /*
7362 * Enable Refclk and SSC
7363 */
a11b0703 7364 I915_WRITE(dpll_reg,
d288f65f 7365 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
a11b0703 7366
a580516d 7367 mutex_lock(&dev_priv->sb_lock);
9d556c99 7368
9d556c99
CML
7369 /* p1 and p2 divider */
7370 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7371 5 << DPIO_CHV_S1_DIV_SHIFT |
7372 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7373 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7374 1 << DPIO_CHV_K_DIV_SHIFT);
7375
7376 /* Feedback post-divider - m2 */
7377 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7378
7379 /* Feedback refclk divider - n and m1 */
7380 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7381 DPIO_CHV_M1_DIV_BY_2 |
7382 1 << DPIO_CHV_N_DIV_SHIFT);
7383
7384 /* M2 fraction division */
25a25dfc 7385 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
9d556c99
CML
7386
7387 /* M2 fraction division enable */
a945ce7e
VP
7388 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7389 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7390 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7391 if (bestm2_frac)
7392 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7393 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
9d556c99 7394
de3a0fde
VP
7395 /* Program digital lock detect threshold */
7396 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7397 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7398 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7399 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7400 if (!bestm2_frac)
7401 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7402 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7403
9d556c99 7404 /* Loop filter */
9cbe40c1
VP
7405 if (vco == 5400000) {
7406 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7407 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7408 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7409 tribuf_calcntr = 0x9;
7410 } else if (vco <= 6200000) {
7411 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7412 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7413 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7414 tribuf_calcntr = 0x9;
7415 } else if (vco <= 6480000) {
7416 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7417 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7418 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7419 tribuf_calcntr = 0x8;
7420 } else {
7421 /* Not supported. Apply the same limits as in the max case */
7422 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7423 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7424 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7425 tribuf_calcntr = 0;
7426 }
9d556c99
CML
7427 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7428
968040b2 7429 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
9cbe40c1
VP
7430 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7431 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7432 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7433
9d556c99
CML
7434 /* AFC Recal */
7435 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7436 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7437 DPIO_AFC_RECAL);
7438
a580516d 7439 mutex_unlock(&dev_priv->sb_lock);
9d556c99
CML
7440}
7441
d288f65f
VS
7442/**
7443 * vlv_force_pll_on - forcibly enable just the PLL
7444 * @dev_priv: i915 private structure
7445 * @pipe: pipe PLL to enable
7446 * @dpll: PLL configuration
7447 *
7448 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7449 * in cases where we need the PLL enabled even when @pipe is not going to
7450 * be enabled.
7451 */
3f36b937
TU
7452int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7453 const struct dpll *dpll)
d288f65f
VS
7454{
7455 struct intel_crtc *crtc =
7456 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
3f36b937
TU
7457 struct intel_crtc_state *pipe_config;
7458
7459 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7460 if (!pipe_config)
7461 return -ENOMEM;
7462
7463 pipe_config->base.crtc = &crtc->base;
7464 pipe_config->pixel_multiplier = 1;
7465 pipe_config->dpll = *dpll;
d288f65f
VS
7466
7467 if (IS_CHERRYVIEW(dev)) {
3f36b937
TU
7468 chv_compute_dpll(crtc, pipe_config);
7469 chv_prepare_pll(crtc, pipe_config);
7470 chv_enable_pll(crtc, pipe_config);
d288f65f 7471 } else {
3f36b937
TU
7472 vlv_compute_dpll(crtc, pipe_config);
7473 vlv_prepare_pll(crtc, pipe_config);
7474 vlv_enable_pll(crtc, pipe_config);
d288f65f 7475 }
3f36b937
TU
7476
7477 kfree(pipe_config);
7478
7479 return 0;
d288f65f
VS
7480}
7481
7482/**
7483 * vlv_force_pll_off - forcibly disable just the PLL
7484 * @dev_priv: i915 private structure
7485 * @pipe: pipe PLL to disable
7486 *
7487 * Disable the PLL for @pipe. To be used in cases where we need
7488 * the PLL enabled even when @pipe is not going to be enabled.
7489 */
7490void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7491{
7492 if (IS_CHERRYVIEW(dev))
7493 chv_disable_pll(to_i915(dev), pipe);
7494 else
7495 vlv_disable_pll(to_i915(dev), pipe);
7496}
7497
251ac862
DV
7498static void i9xx_compute_dpll(struct intel_crtc *crtc,
7499 struct intel_crtc_state *crtc_state,
ceb41007 7500 intel_clock_t *reduced_clock)
eb1cbe48 7501{
f47709a9 7502 struct drm_device *dev = crtc->base.dev;
eb1cbe48 7503 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48
DV
7504 u32 dpll;
7505 bool is_sdvo;
190f68c5 7506 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7507
190f68c5 7508 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7509
a93e255f
ACO
7510 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7511 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
eb1cbe48
DV
7512
7513 dpll = DPLL_VGA_MODE_DIS;
7514
a93e255f 7515 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
7516 dpll |= DPLLB_MODE_LVDS;
7517 else
7518 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 7519
ef1b460d 7520 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
190f68c5 7521 dpll |= (crtc_state->pixel_multiplier - 1)
198a037f 7522 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 7523 }
198a037f
DV
7524
7525 if (is_sdvo)
4a33e48d 7526 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 7527
190f68c5 7528 if (crtc_state->has_dp_encoder)
4a33e48d 7529 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
7530
7531 /* compute bitmask from p1 value */
7532 if (IS_PINEVIEW(dev))
7533 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7534 else {
7535 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7536 if (IS_G4X(dev) && reduced_clock)
7537 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7538 }
7539 switch (clock->p2) {
7540 case 5:
7541 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7542 break;
7543 case 7:
7544 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7545 break;
7546 case 10:
7547 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7548 break;
7549 case 14:
7550 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7551 break;
7552 }
7553 if (INTEL_INFO(dev)->gen >= 4)
7554 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7555
190f68c5 7556 if (crtc_state->sdvo_tv_clock)
eb1cbe48 7557 dpll |= PLL_REF_INPUT_TVCLKINBC;
a93e255f 7558 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 7559 intel_panel_use_ssc(dev_priv))
eb1cbe48
DV
7560 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7561 else
7562 dpll |= PLL_REF_INPUT_DREFCLK;
7563
7564 dpll |= DPLL_VCO_ENABLE;
190f68c5 7565 crtc_state->dpll_hw_state.dpll = dpll;
8bcc2795 7566
eb1cbe48 7567 if (INTEL_INFO(dev)->gen >= 4) {
190f68c5 7568 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
ef1b460d 7569 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
190f68c5 7570 crtc_state->dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
DV
7571 }
7572}
7573
251ac862
DV
7574static void i8xx_compute_dpll(struct intel_crtc *crtc,
7575 struct intel_crtc_state *crtc_state,
ceb41007 7576 intel_clock_t *reduced_clock)
eb1cbe48 7577{
f47709a9 7578 struct drm_device *dev = crtc->base.dev;
eb1cbe48 7579 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48 7580 u32 dpll;
190f68c5 7581 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7582
190f68c5 7583 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7584
eb1cbe48
DV
7585 dpll = DPLL_VGA_MODE_DIS;
7586
a93e255f 7587 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
7588 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7589 } else {
7590 if (clock->p1 == 2)
7591 dpll |= PLL_P1_DIVIDE_BY_TWO;
7592 else
7593 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7594 if (clock->p2 == 4)
7595 dpll |= PLL_P2_DIVIDE_BY_4;
7596 }
7597
a93e255f 7598 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
4a33e48d
DV
7599 dpll |= DPLL_DVO_2X_MODE;
7600
a93e255f 7601 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 7602 intel_panel_use_ssc(dev_priv))
eb1cbe48
DV
7603 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7604 else
7605 dpll |= PLL_REF_INPUT_DREFCLK;
7606
7607 dpll |= DPLL_VCO_ENABLE;
190f68c5 7608 crtc_state->dpll_hw_state.dpll = dpll;
eb1cbe48
DV
7609}
7610
8a654f3b 7611static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c
PZ
7612{
7613 struct drm_device *dev = intel_crtc->base.dev;
7614 struct drm_i915_private *dev_priv = dev->dev_private;
7615 enum pipe pipe = intel_crtc->pipe;
6e3c9717 7616 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
7c5f93b0 7617 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
1caea6e9
VS
7618 uint32_t crtc_vtotal, crtc_vblank_end;
7619 int vsyncshift = 0;
4d8a62ea
DV
7620
7621 /* We need to be careful not to changed the adjusted mode, for otherwise
7622 * the hw state checker will get angry at the mismatch. */
7623 crtc_vtotal = adjusted_mode->crtc_vtotal;
7624 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 7625
609aeaca 7626 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 7627 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
7628 crtc_vtotal -= 1;
7629 crtc_vblank_end -= 1;
609aeaca 7630
409ee761 7631 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
609aeaca
VS
7632 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7633 else
7634 vsyncshift = adjusted_mode->crtc_hsync_start -
7635 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
7636 if (vsyncshift < 0)
7637 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
7638 }
7639
7640 if (INTEL_INFO(dev)->gen > 3)
fe2b8f9d 7641 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 7642
fe2b8f9d 7643 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
7644 (adjusted_mode->crtc_hdisplay - 1) |
7645 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 7646 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
7647 (adjusted_mode->crtc_hblank_start - 1) |
7648 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 7649 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
7650 (adjusted_mode->crtc_hsync_start - 1) |
7651 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7652
fe2b8f9d 7653 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 7654 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 7655 ((crtc_vtotal - 1) << 16));
fe2b8f9d 7656 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 7657 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 7658 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 7659 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
7660 (adjusted_mode->crtc_vsync_start - 1) |
7661 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7662
b5e508d4
PZ
7663 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7664 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7665 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7666 * bits. */
7667 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7668 (pipe == PIPE_B || pipe == PIPE_C))
7669 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7670
bc58be60
JN
7671}
7672
7673static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
7674{
7675 struct drm_device *dev = intel_crtc->base.dev;
7676 struct drm_i915_private *dev_priv = dev->dev_private;
7677 enum pipe pipe = intel_crtc->pipe;
7678
b0e77b9c
PZ
7679 /* pipesrc controls the size that is scaled from, which should
7680 * always be the user's requested size.
7681 */
7682 I915_WRITE(PIPESRC(pipe),
6e3c9717
ACO
7683 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7684 (intel_crtc->config->pipe_src_h - 1));
b0e77b9c
PZ
7685}
7686
1bd1bd80 7687static void intel_get_pipe_timings(struct intel_crtc *crtc,
5cec258b 7688 struct intel_crtc_state *pipe_config)
1bd1bd80
DV
7689{
7690 struct drm_device *dev = crtc->base.dev;
7691 struct drm_i915_private *dev_priv = dev->dev_private;
7692 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7693 uint32_t tmp;
7694
7695 tmp = I915_READ(HTOTAL(cpu_transcoder));
2d112de7
ACO
7696 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7697 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7698 tmp = I915_READ(HBLANK(cpu_transcoder));
2d112de7
ACO
7699 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7700 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7701 tmp = I915_READ(HSYNC(cpu_transcoder));
2d112de7
ACO
7702 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7703 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7704
7705 tmp = I915_READ(VTOTAL(cpu_transcoder));
2d112de7
ACO
7706 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7707 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7708 tmp = I915_READ(VBLANK(cpu_transcoder));
2d112de7
ACO
7709 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7710 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7711 tmp = I915_READ(VSYNC(cpu_transcoder));
2d112de7
ACO
7712 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7713 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7714
7715 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
2d112de7
ACO
7716 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7717 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7718 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
1bd1bd80 7719 }
bc58be60
JN
7720}
7721
7722static void intel_get_pipe_src_size(struct intel_crtc *crtc,
7723 struct intel_crtc_state *pipe_config)
7724{
7725 struct drm_device *dev = crtc->base.dev;
7726 struct drm_i915_private *dev_priv = dev->dev_private;
7727 u32 tmp;
1bd1bd80
DV
7728
7729 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
7730 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7731 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7732
2d112de7
ACO
7733 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7734 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
DV
7735}
7736
f6a83288 7737void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5cec258b 7738 struct intel_crtc_state *pipe_config)
babea61d 7739{
2d112de7
ACO
7740 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7741 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7742 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7743 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
babea61d 7744
2d112de7
ACO
7745 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7746 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7747 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7748 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
babea61d 7749
2d112de7 7750 mode->flags = pipe_config->base.adjusted_mode.flags;
cd13f5ab 7751 mode->type = DRM_MODE_TYPE_DRIVER;
babea61d 7752
2d112de7
ACO
7753 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7754 mode->flags |= pipe_config->base.adjusted_mode.flags;
cd13f5ab
ML
7755
7756 mode->hsync = drm_mode_hsync(mode);
7757 mode->vrefresh = drm_mode_vrefresh(mode);
7758 drm_mode_set_name(mode);
babea61d
JB
7759}
7760
84b046f3
DV
7761static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7762{
7763 struct drm_device *dev = intel_crtc->base.dev;
7764 struct drm_i915_private *dev_priv = dev->dev_private;
7765 uint32_t pipeconf;
7766
9f11a9e4 7767 pipeconf = 0;
84b046f3 7768
b6b5d049
VS
7769 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7770 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7771 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
67c72a12 7772
6e3c9717 7773 if (intel_crtc->config->double_wide)
cf532bb2 7774 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 7775
ff9ce46e 7776 /* only g4x and later have fancy bpc/dither controls */
666a4537 7777 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
ff9ce46e 7778 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6e3c9717 7779 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
ff9ce46e 7780 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 7781 PIPECONF_DITHER_TYPE_SP;
84b046f3 7782
6e3c9717 7783 switch (intel_crtc->config->pipe_bpp) {
ff9ce46e
DV
7784 case 18:
7785 pipeconf |= PIPECONF_6BPC;
7786 break;
7787 case 24:
7788 pipeconf |= PIPECONF_8BPC;
7789 break;
7790 case 30:
7791 pipeconf |= PIPECONF_10BPC;
7792 break;
7793 default:
7794 /* Case prevented by intel_choose_pipe_bpp_dither. */
7795 BUG();
84b046f3
DV
7796 }
7797 }
7798
7799 if (HAS_PIPE_CXSR(dev)) {
7800 if (intel_crtc->lowfreq_avail) {
7801 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7802 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7803 } else {
7804 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
84b046f3
DV
7805 }
7806 }
7807
6e3c9717 7808 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
efc2cfff 7809 if (INTEL_INFO(dev)->gen < 4 ||
409ee761 7810 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
efc2cfff
VS
7811 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7812 else
7813 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7814 } else
84b046f3
DV
7815 pipeconf |= PIPECONF_PROGRESSIVE;
7816
666a4537
WB
7817 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
7818 intel_crtc->config->limited_color_range)
9f11a9e4 7819 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 7820
84b046f3
DV
7821 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7822 POSTING_READ(PIPECONF(intel_crtc->pipe));
7823}
7824
190f68c5
ACO
7825static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7826 struct intel_crtc_state *crtc_state)
79e53945 7827{
c7653199 7828 struct drm_device *dev = crtc->base.dev;
79e53945 7829 struct drm_i915_private *dev_priv = dev->dev_private;
ceb41007 7830 int refclk;
c329a4ec 7831 bool ok;
d4906093 7832 const intel_limit_t *limit;
79e53945 7833
dd3cd74a
ACO
7834 memset(&crtc_state->dpll_hw_state, 0,
7835 sizeof(crtc_state->dpll_hw_state));
7836
a65347ba
JN
7837 if (crtc_state->has_dsi_encoder)
7838 return 0;
43565a06 7839
190f68c5 7840 if (!crtc_state->clock_set) {
ceb41007 7841 refclk = i9xx_get_refclk(crtc_state);
79e53945 7842
e9fd1c02
JN
7843 /*
7844 * Returns a set of divisors for the desired target clock with
7845 * the given refclk, or FALSE. The returned values represent
7846 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7847 * 2) / p1 / p2.
7848 */
a93e255f
ACO
7849 limit = intel_limit(crtc_state, refclk);
7850 ok = dev_priv->display.find_dpll(limit, crtc_state,
190f68c5 7851 crtc_state->port_clock,
364ee29d
ACO
7852 refclk, NULL,
7853 &crtc_state->dpll);
f2335330 7854 if (!ok) {
e9fd1c02
JN
7855 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7856 return -EINVAL;
7857 }
f47709a9 7858 }
7026d4ac 7859
e9fd1c02 7860 if (IS_GEN2(dev)) {
ceb41007 7861 i8xx_compute_dpll(crtc, crtc_state, NULL);
e9fd1c02 7862 } else {
ceb41007 7863 i9xx_compute_dpll(crtc, crtc_state, NULL);
e9fd1c02 7864 }
79e53945 7865
c8f7a0db 7866 return 0;
f564048e
EA
7867}
7868
65b3d6a9
ACO
7869static int chv_crtc_compute_clock(struct intel_crtc *crtc,
7870 struct intel_crtc_state *crtc_state)
7871{
7872 int refclk = 100000;
7873 const intel_limit_t *limit = &intel_limits_chv;
7874
7875 memset(&crtc_state->dpll_hw_state, 0,
7876 sizeof(crtc_state->dpll_hw_state));
7877
7878 if (crtc_state->has_dsi_encoder)
7879 return 0;
7880
7881 if (!crtc_state->clock_set &&
7882 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7883 refclk, NULL, &crtc_state->dpll)) {
7884 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7885 return -EINVAL;
7886 }
7887
7888 chv_compute_dpll(crtc, crtc_state);
7889
7890 return 0;
7891}
7892
7893static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
7894 struct intel_crtc_state *crtc_state)
7895{
7896 int refclk = 100000;
7897 const intel_limit_t *limit = &intel_limits_vlv;
7898
7899 memset(&crtc_state->dpll_hw_state, 0,
7900 sizeof(crtc_state->dpll_hw_state));
7901
7902 if (crtc_state->has_dsi_encoder)
7903 return 0;
7904
7905 if (!crtc_state->clock_set &&
7906 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7907 refclk, NULL, &crtc_state->dpll)) {
7908 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7909 return -EINVAL;
7910 }
7911
7912 vlv_compute_dpll(crtc, crtc_state);
7913
7914 return 0;
7915}
7916
2fa2fe9a 7917static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5cec258b 7918 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
7919{
7920 struct drm_device *dev = crtc->base.dev;
7921 struct drm_i915_private *dev_priv = dev->dev_private;
7922 uint32_t tmp;
7923
dc9e7dec
VS
7924 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7925 return;
7926
2fa2fe9a 7927 tmp = I915_READ(PFIT_CONTROL);
06922821
DV
7928 if (!(tmp & PFIT_ENABLE))
7929 return;
2fa2fe9a 7930
06922821 7931 /* Check whether the pfit is attached to our pipe. */
2fa2fe9a
DV
7932 if (INTEL_INFO(dev)->gen < 4) {
7933 if (crtc->pipe != PIPE_B)
7934 return;
2fa2fe9a
DV
7935 } else {
7936 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7937 return;
7938 }
7939
06922821 7940 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a
DV
7941 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7942 if (INTEL_INFO(dev)->gen < 5)
7943 pipe_config->gmch_pfit.lvds_border_bits =
7944 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7945}
7946
acbec814 7947static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 7948 struct intel_crtc_state *pipe_config)
acbec814
JB
7949{
7950 struct drm_device *dev = crtc->base.dev;
7951 struct drm_i915_private *dev_priv = dev->dev_private;
7952 int pipe = pipe_config->cpu_transcoder;
7953 intel_clock_t clock;
7954 u32 mdiv;
662c6ecb 7955 int refclk = 100000;
acbec814 7956
f573de5a
SK
7957 /* In case of MIPI DPLL will not even be used */
7958 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
7959 return;
7960
a580516d 7961 mutex_lock(&dev_priv->sb_lock);
ab3c759a 7962 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
a580516d 7963 mutex_unlock(&dev_priv->sb_lock);
acbec814
JB
7964
7965 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7966 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7967 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7968 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7969 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7970
dccbea3b 7971 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
acbec814
JB
7972}
7973
5724dbd1
DL
7974static void
7975i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7976 struct intel_initial_plane_config *plane_config)
1ad292b5
JB
7977{
7978 struct drm_device *dev = crtc->base.dev;
7979 struct drm_i915_private *dev_priv = dev->dev_private;
7980 u32 val, base, offset;
7981 int pipe = crtc->pipe, plane = crtc->plane;
7982 int fourcc, pixel_format;
6761dd31 7983 unsigned int aligned_height;
b113d5ee 7984 struct drm_framebuffer *fb;
1b842c89 7985 struct intel_framebuffer *intel_fb;
1ad292b5 7986
42a7b088
DL
7987 val = I915_READ(DSPCNTR(plane));
7988 if (!(val & DISPLAY_PLANE_ENABLE))
7989 return;
7990
d9806c9f 7991 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 7992 if (!intel_fb) {
1ad292b5
JB
7993 DRM_DEBUG_KMS("failed to alloc fb\n");
7994 return;
7995 }
7996
1b842c89
DL
7997 fb = &intel_fb->base;
7998
18c5247e
DV
7999 if (INTEL_INFO(dev)->gen >= 4) {
8000 if (val & DISPPLANE_TILED) {
49af449b 8001 plane_config->tiling = I915_TILING_X;
18c5247e
DV
8002 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8003 }
8004 }
1ad292b5
JB
8005
8006 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 8007 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
8008 fb->pixel_format = fourcc;
8009 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
1ad292b5
JB
8010
8011 if (INTEL_INFO(dev)->gen >= 4) {
49af449b 8012 if (plane_config->tiling)
1ad292b5
JB
8013 offset = I915_READ(DSPTILEOFF(plane));
8014 else
8015 offset = I915_READ(DSPLINOFF(plane));
8016 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8017 } else {
8018 base = I915_READ(DSPADDR(plane));
8019 }
8020 plane_config->base = base;
8021
8022 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
8023 fb->width = ((val >> 16) & 0xfff) + 1;
8024 fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
8025
8026 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 8027 fb->pitches[0] = val & 0xffffffc0;
1ad292b5 8028
b113d5ee 8029 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
8030 fb->pixel_format,
8031 fb->modifier[0]);
1ad292b5 8032
f37b5c2b 8033 plane_config->size = fb->pitches[0] * aligned_height;
1ad292b5 8034
2844a921
DL
8035 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8036 pipe_name(pipe), plane, fb->width, fb->height,
8037 fb->bits_per_pixel, base, fb->pitches[0],
8038 plane_config->size);
1ad292b5 8039
2d14030b 8040 plane_config->fb = intel_fb;
1ad292b5
JB
8041}
8042
70b23a98 8043static void chv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 8044 struct intel_crtc_state *pipe_config)
70b23a98
VS
8045{
8046 struct drm_device *dev = crtc->base.dev;
8047 struct drm_i915_private *dev_priv = dev->dev_private;
8048 int pipe = pipe_config->cpu_transcoder;
8049 enum dpio_channel port = vlv_pipe_to_channel(pipe);
8050 intel_clock_t clock;
0d7b6b11 8051 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
70b23a98
VS
8052 int refclk = 100000;
8053
a580516d 8054 mutex_lock(&dev_priv->sb_lock);
70b23a98
VS
8055 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8056 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8057 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8058 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
0d7b6b11 8059 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
a580516d 8060 mutex_unlock(&dev_priv->sb_lock);
70b23a98
VS
8061
8062 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
0d7b6b11
ID
8063 clock.m2 = (pll_dw0 & 0xff) << 22;
8064 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8065 clock.m2 |= pll_dw2 & 0x3fffff;
70b23a98
VS
8066 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8067 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8068 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8069
dccbea3b 8070 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
70b23a98
VS
8071}
8072
0e8ffe1b 8073static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5cec258b 8074 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
8075{
8076 struct drm_device *dev = crtc->base.dev;
8077 struct drm_i915_private *dev_priv = dev->dev_private;
1729050e 8078 enum intel_display_power_domain power_domain;
0e8ffe1b 8079 uint32_t tmp;
1729050e 8080 bool ret;
0e8ffe1b 8081
1729050e
ID
8082 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8083 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0
ID
8084 return false;
8085
e143a21c 8086 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 8087 pipe_config->shared_dpll = NULL;
eccb140b 8088
1729050e
ID
8089 ret = false;
8090
0e8ffe1b
DV
8091 tmp = I915_READ(PIPECONF(crtc->pipe));
8092 if (!(tmp & PIPECONF_ENABLE))
1729050e 8093 goto out;
0e8ffe1b 8094
666a4537 8095 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
42571aef
VS
8096 switch (tmp & PIPECONF_BPC_MASK) {
8097 case PIPECONF_6BPC:
8098 pipe_config->pipe_bpp = 18;
8099 break;
8100 case PIPECONF_8BPC:
8101 pipe_config->pipe_bpp = 24;
8102 break;
8103 case PIPECONF_10BPC:
8104 pipe_config->pipe_bpp = 30;
8105 break;
8106 default:
8107 break;
8108 }
8109 }
8110
666a4537
WB
8111 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
8112 (tmp & PIPECONF_COLOR_RANGE_SELECT))
b5a9fa09
DV
8113 pipe_config->limited_color_range = true;
8114
282740f7
VS
8115 if (INTEL_INFO(dev)->gen < 4)
8116 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8117
1bd1bd80 8118 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 8119 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 8120
2fa2fe9a
DV
8121 i9xx_get_pfit_config(crtc, pipe_config);
8122
6c49f241
DV
8123 if (INTEL_INFO(dev)->gen >= 4) {
8124 tmp = I915_READ(DPLL_MD(crtc->pipe));
8125 pipe_config->pixel_multiplier =
8126 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8127 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 8128 pipe_config->dpll_hw_state.dpll_md = tmp;
6c49f241
DV
8129 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8130 tmp = I915_READ(DPLL(crtc->pipe));
8131 pipe_config->pixel_multiplier =
8132 ((tmp & SDVO_MULTIPLIER_MASK)
8133 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8134 } else {
8135 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8136 * port and will be fixed up in the encoder->get_config
8137 * function. */
8138 pipe_config->pixel_multiplier = 1;
8139 }
8bcc2795 8140 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
666a4537 8141 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
1c4e0274
VS
8142 /*
8143 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8144 * on 830. Filter it out here so that we don't
8145 * report errors due to that.
8146 */
8147 if (IS_I830(dev))
8148 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8149
8bcc2795
DV
8150 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8151 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
8152 } else {
8153 /* Mask out read-only status bits. */
8154 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8155 DPLL_PORTC_READY_MASK |
8156 DPLL_PORTB_READY_MASK);
8bcc2795 8157 }
6c49f241 8158
70b23a98
VS
8159 if (IS_CHERRYVIEW(dev))
8160 chv_crtc_clock_get(crtc, pipe_config);
8161 else if (IS_VALLEYVIEW(dev))
acbec814
JB
8162 vlv_crtc_clock_get(crtc, pipe_config);
8163 else
8164 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 8165
0f64614d
VS
8166 /*
8167 * Normally the dotclock is filled in by the encoder .get_config()
8168 * but in case the pipe is enabled w/o any ports we need a sane
8169 * default.
8170 */
8171 pipe_config->base.adjusted_mode.crtc_clock =
8172 pipe_config->port_clock / pipe_config->pixel_multiplier;
8173
1729050e
ID
8174 ret = true;
8175
8176out:
8177 intel_display_power_put(dev_priv, power_domain);
8178
8179 return ret;
0e8ffe1b
DV
8180}
8181
dde86e2d 8182static void ironlake_init_pch_refclk(struct drm_device *dev)
13d83a67
JB
8183{
8184 struct drm_i915_private *dev_priv = dev->dev_private;
13d83a67 8185 struct intel_encoder *encoder;
74cfd7ac 8186 u32 val, final;
13d83a67 8187 bool has_lvds = false;
199e5d79 8188 bool has_cpu_edp = false;
199e5d79 8189 bool has_panel = false;
99eb6a01
KP
8190 bool has_ck505 = false;
8191 bool can_ssc = false;
13d83a67
JB
8192
8193 /* We need to take the global config into account */
b2784e15 8194 for_each_intel_encoder(dev, encoder) {
199e5d79
KP
8195 switch (encoder->type) {
8196 case INTEL_OUTPUT_LVDS:
8197 has_panel = true;
8198 has_lvds = true;
8199 break;
8200 case INTEL_OUTPUT_EDP:
8201 has_panel = true;
2de6905f 8202 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
8203 has_cpu_edp = true;
8204 break;
6847d71b
PZ
8205 default:
8206 break;
13d83a67
JB
8207 }
8208 }
8209
99eb6a01 8210 if (HAS_PCH_IBX(dev)) {
41aa3448 8211 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
8212 can_ssc = has_ck505;
8213 } else {
8214 has_ck505 = false;
8215 can_ssc = true;
8216 }
8217
2de6905f
ID
8218 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8219 has_panel, has_lvds, has_ck505);
13d83a67
JB
8220
8221 /* Ironlake: try to setup display ref clock before DPLL
8222 * enabling. This is only under driver's control after
8223 * PCH B stepping, previous chipset stepping should be
8224 * ignoring this setting.
8225 */
74cfd7ac
CW
8226 val = I915_READ(PCH_DREF_CONTROL);
8227
8228 /* As we must carefully and slowly disable/enable each source in turn,
8229 * compute the final state we want first and check if we need to
8230 * make any changes at all.
8231 */
8232 final = val;
8233 final &= ~DREF_NONSPREAD_SOURCE_MASK;
8234 if (has_ck505)
8235 final |= DREF_NONSPREAD_CK505_ENABLE;
8236 else
8237 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8238
8239 final &= ~DREF_SSC_SOURCE_MASK;
8240 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8241 final &= ~DREF_SSC1_ENABLE;
8242
8243 if (has_panel) {
8244 final |= DREF_SSC_SOURCE_ENABLE;
8245
8246 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8247 final |= DREF_SSC1_ENABLE;
8248
8249 if (has_cpu_edp) {
8250 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8251 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8252 else
8253 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8254 } else
8255 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8256 } else {
8257 final |= DREF_SSC_SOURCE_DISABLE;
8258 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8259 }
8260
8261 if (final == val)
8262 return;
8263
13d83a67 8264 /* Always enable nonspread source */
74cfd7ac 8265 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 8266
99eb6a01 8267 if (has_ck505)
74cfd7ac 8268 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 8269 else
74cfd7ac 8270 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 8271
199e5d79 8272 if (has_panel) {
74cfd7ac
CW
8273 val &= ~DREF_SSC_SOURCE_MASK;
8274 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 8275
199e5d79 8276 /* SSC must be turned on before enabling the CPU output */
99eb6a01 8277 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 8278 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 8279 val |= DREF_SSC1_ENABLE;
e77166b5 8280 } else
74cfd7ac 8281 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
8282
8283 /* Get SSC going before enabling the outputs */
74cfd7ac 8284 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8285 POSTING_READ(PCH_DREF_CONTROL);
8286 udelay(200);
8287
74cfd7ac 8288 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
8289
8290 /* Enable CPU source on CPU attached eDP */
199e5d79 8291 if (has_cpu_edp) {
99eb6a01 8292 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 8293 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 8294 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 8295 } else
74cfd7ac 8296 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 8297 } else
74cfd7ac 8298 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 8299
74cfd7ac 8300 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8301 POSTING_READ(PCH_DREF_CONTROL);
8302 udelay(200);
8303 } else {
8304 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8305
74cfd7ac 8306 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
8307
8308 /* Turn off CPU output */
74cfd7ac 8309 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 8310
74cfd7ac 8311 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8312 POSTING_READ(PCH_DREF_CONTROL);
8313 udelay(200);
8314
8315 /* Turn off the SSC source */
74cfd7ac
CW
8316 val &= ~DREF_SSC_SOURCE_MASK;
8317 val |= DREF_SSC_SOURCE_DISABLE;
199e5d79
KP
8318
8319 /* Turn off SSC1 */
74cfd7ac 8320 val &= ~DREF_SSC1_ENABLE;
199e5d79 8321
74cfd7ac 8322 I915_WRITE(PCH_DREF_CONTROL, val);
13d83a67
JB
8323 POSTING_READ(PCH_DREF_CONTROL);
8324 udelay(200);
8325 }
74cfd7ac
CW
8326
8327 BUG_ON(val != final);
13d83a67
JB
8328}
8329
f31f2d55 8330static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 8331{
f31f2d55 8332 uint32_t tmp;
dde86e2d 8333
0ff066a9
PZ
8334 tmp = I915_READ(SOUTH_CHICKEN2);
8335 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8336 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 8337
0ff066a9
PZ
8338 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8339 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8340 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 8341
0ff066a9
PZ
8342 tmp = I915_READ(SOUTH_CHICKEN2);
8343 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8344 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 8345
0ff066a9
PZ
8346 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8347 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8348 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
8349}
8350
8351/* WaMPhyProgramming:hsw */
8352static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8353{
8354 uint32_t tmp;
dde86e2d
PZ
8355
8356 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8357 tmp &= ~(0xFF << 24);
8358 tmp |= (0x12 << 24);
8359 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8360
dde86e2d
PZ
8361 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8362 tmp |= (1 << 11);
8363 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8364
8365 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8366 tmp |= (1 << 11);
8367 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8368
dde86e2d
PZ
8369 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8370 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8371 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8372
8373 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8374 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8375 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8376
0ff066a9
PZ
8377 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8378 tmp &= ~(7 << 13);
8379 tmp |= (5 << 13);
8380 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 8381
0ff066a9
PZ
8382 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8383 tmp &= ~(7 << 13);
8384 tmp |= (5 << 13);
8385 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
8386
8387 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8388 tmp &= ~0xFF;
8389 tmp |= 0x1C;
8390 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8391
8392 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8393 tmp &= ~0xFF;
8394 tmp |= 0x1C;
8395 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8396
8397 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8398 tmp &= ~(0xFF << 16);
8399 tmp |= (0x1C << 16);
8400 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8401
8402 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8403 tmp &= ~(0xFF << 16);
8404 tmp |= (0x1C << 16);
8405 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8406
0ff066a9
PZ
8407 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8408 tmp |= (1 << 27);
8409 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 8410
0ff066a9
PZ
8411 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8412 tmp |= (1 << 27);
8413 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 8414
0ff066a9
PZ
8415 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8416 tmp &= ~(0xF << 28);
8417 tmp |= (4 << 28);
8418 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 8419
0ff066a9
PZ
8420 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8421 tmp &= ~(0xF << 28);
8422 tmp |= (4 << 28);
8423 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
8424}
8425
2fa86a1f
PZ
8426/* Implements 3 different sequences from BSpec chapter "Display iCLK
8427 * Programming" based on the parameters passed:
8428 * - Sequence to enable CLKOUT_DP
8429 * - Sequence to enable CLKOUT_DP without spread
8430 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8431 */
8432static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8433 bool with_fdi)
f31f2d55
PZ
8434{
8435 struct drm_i915_private *dev_priv = dev->dev_private;
2fa86a1f
PZ
8436 uint32_t reg, tmp;
8437
8438 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8439 with_spread = true;
c2699524 8440 if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
2fa86a1f 8441 with_fdi = false;
f31f2d55 8442
a580516d 8443 mutex_lock(&dev_priv->sb_lock);
f31f2d55
PZ
8444
8445 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8446 tmp &= ~SBI_SSCCTL_DISABLE;
8447 tmp |= SBI_SSCCTL_PATHALT;
8448 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8449
8450 udelay(24);
8451
2fa86a1f
PZ
8452 if (with_spread) {
8453 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8454 tmp &= ~SBI_SSCCTL_PATHALT;
8455 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 8456
2fa86a1f
PZ
8457 if (with_fdi) {
8458 lpt_reset_fdi_mphy(dev_priv);
8459 lpt_program_fdi_mphy(dev_priv);
8460 }
8461 }
dde86e2d 8462
c2699524 8463 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
2fa86a1f
PZ
8464 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8465 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8466 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246 8467
a580516d 8468 mutex_unlock(&dev_priv->sb_lock);
dde86e2d
PZ
8469}
8470
47701c3b
PZ
8471/* Sequence to disable CLKOUT_DP */
8472static void lpt_disable_clkout_dp(struct drm_device *dev)
8473{
8474 struct drm_i915_private *dev_priv = dev->dev_private;
8475 uint32_t reg, tmp;
8476
a580516d 8477 mutex_lock(&dev_priv->sb_lock);
47701c3b 8478
c2699524 8479 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
47701c3b
PZ
8480 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8481 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8482 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8483
8484 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8485 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8486 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8487 tmp |= SBI_SSCCTL_PATHALT;
8488 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8489 udelay(32);
8490 }
8491 tmp |= SBI_SSCCTL_DISABLE;
8492 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8493 }
8494
a580516d 8495 mutex_unlock(&dev_priv->sb_lock);
47701c3b
PZ
8496}
8497
f7be2c21
VS
8498#define BEND_IDX(steps) ((50 + (steps)) / 5)
8499
8500static const uint16_t sscdivintphase[] = {
8501 [BEND_IDX( 50)] = 0x3B23,
8502 [BEND_IDX( 45)] = 0x3B23,
8503 [BEND_IDX( 40)] = 0x3C23,
8504 [BEND_IDX( 35)] = 0x3C23,
8505 [BEND_IDX( 30)] = 0x3D23,
8506 [BEND_IDX( 25)] = 0x3D23,
8507 [BEND_IDX( 20)] = 0x3E23,
8508 [BEND_IDX( 15)] = 0x3E23,
8509 [BEND_IDX( 10)] = 0x3F23,
8510 [BEND_IDX( 5)] = 0x3F23,
8511 [BEND_IDX( 0)] = 0x0025,
8512 [BEND_IDX( -5)] = 0x0025,
8513 [BEND_IDX(-10)] = 0x0125,
8514 [BEND_IDX(-15)] = 0x0125,
8515 [BEND_IDX(-20)] = 0x0225,
8516 [BEND_IDX(-25)] = 0x0225,
8517 [BEND_IDX(-30)] = 0x0325,
8518 [BEND_IDX(-35)] = 0x0325,
8519 [BEND_IDX(-40)] = 0x0425,
8520 [BEND_IDX(-45)] = 0x0425,
8521 [BEND_IDX(-50)] = 0x0525,
8522};
8523
8524/*
8525 * Bend CLKOUT_DP
8526 * steps -50 to 50 inclusive, in steps of 5
8527 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
8528 * change in clock period = -(steps / 10) * 5.787 ps
8529 */
8530static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
8531{
8532 uint32_t tmp;
8533 int idx = BEND_IDX(steps);
8534
8535 if (WARN_ON(steps % 5 != 0))
8536 return;
8537
8538 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
8539 return;
8540
8541 mutex_lock(&dev_priv->sb_lock);
8542
8543 if (steps % 10 != 0)
8544 tmp = 0xAAAAAAAB;
8545 else
8546 tmp = 0x00000000;
8547 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
8548
8549 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
8550 tmp &= 0xffff0000;
8551 tmp |= sscdivintphase[idx];
8552 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
8553
8554 mutex_unlock(&dev_priv->sb_lock);
8555}
8556
8557#undef BEND_IDX
8558
bf8fa3d3
PZ
8559static void lpt_init_pch_refclk(struct drm_device *dev)
8560{
bf8fa3d3
PZ
8561 struct intel_encoder *encoder;
8562 bool has_vga = false;
8563
b2784e15 8564 for_each_intel_encoder(dev, encoder) {
bf8fa3d3
PZ
8565 switch (encoder->type) {
8566 case INTEL_OUTPUT_ANALOG:
8567 has_vga = true;
8568 break;
6847d71b
PZ
8569 default:
8570 break;
bf8fa3d3
PZ
8571 }
8572 }
8573
f7be2c21
VS
8574 if (has_vga) {
8575 lpt_bend_clkout_dp(to_i915(dev), 0);
47701c3b 8576 lpt_enable_clkout_dp(dev, true, true);
f7be2c21 8577 } else {
47701c3b 8578 lpt_disable_clkout_dp(dev);
f7be2c21 8579 }
bf8fa3d3
PZ
8580}
8581
dde86e2d
PZ
8582/*
8583 * Initialize reference clocks when the driver loads
8584 */
8585void intel_init_pch_refclk(struct drm_device *dev)
8586{
8587 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8588 ironlake_init_pch_refclk(dev);
8589 else if (HAS_PCH_LPT(dev))
8590 lpt_init_pch_refclk(dev);
8591}
8592
6ff93609 8593static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 8594{
c8203565 8595 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
79e53945
JB
8596 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8597 int pipe = intel_crtc->pipe;
c8203565
PZ
8598 uint32_t val;
8599
78114071 8600 val = 0;
c8203565 8601
6e3c9717 8602 switch (intel_crtc->config->pipe_bpp) {
c8203565 8603 case 18:
dfd07d72 8604 val |= PIPECONF_6BPC;
c8203565
PZ
8605 break;
8606 case 24:
dfd07d72 8607 val |= PIPECONF_8BPC;
c8203565
PZ
8608 break;
8609 case 30:
dfd07d72 8610 val |= PIPECONF_10BPC;
c8203565
PZ
8611 break;
8612 case 36:
dfd07d72 8613 val |= PIPECONF_12BPC;
c8203565
PZ
8614 break;
8615 default:
cc769b62
PZ
8616 /* Case prevented by intel_choose_pipe_bpp_dither. */
8617 BUG();
c8203565
PZ
8618 }
8619
6e3c9717 8620 if (intel_crtc->config->dither)
c8203565
PZ
8621 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8622
6e3c9717 8623 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
8624 val |= PIPECONF_INTERLACED_ILK;
8625 else
8626 val |= PIPECONF_PROGRESSIVE;
8627
6e3c9717 8628 if (intel_crtc->config->limited_color_range)
3685a8f3 8629 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 8630
c8203565
PZ
8631 I915_WRITE(PIPECONF(pipe), val);
8632 POSTING_READ(PIPECONF(pipe));
8633}
8634
6ff93609 8635static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 8636{
391bf048 8637 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
ee2b0b38 8638 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 8639 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
391bf048 8640 u32 val = 0;
ee2b0b38 8641
391bf048 8642 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
ee2b0b38
PZ
8643 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8644
6e3c9717 8645 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
8646 val |= PIPECONF_INTERLACED_ILK;
8647 else
8648 val |= PIPECONF_PROGRESSIVE;
8649
702e7a56
PZ
8650 I915_WRITE(PIPECONF(cpu_transcoder), val);
8651 POSTING_READ(PIPECONF(cpu_transcoder));
391bf048
JN
8652}
8653
391bf048
JN
8654static void haswell_set_pipemisc(struct drm_crtc *crtc)
8655{
8656 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8657 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756f85cf 8658
391bf048
JN
8659 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
8660 u32 val = 0;
756f85cf 8661
6e3c9717 8662 switch (intel_crtc->config->pipe_bpp) {
756f85cf
PZ
8663 case 18:
8664 val |= PIPEMISC_DITHER_6_BPC;
8665 break;
8666 case 24:
8667 val |= PIPEMISC_DITHER_8_BPC;
8668 break;
8669 case 30:
8670 val |= PIPEMISC_DITHER_10_BPC;
8671 break;
8672 case 36:
8673 val |= PIPEMISC_DITHER_12_BPC;
8674 break;
8675 default:
8676 /* Case prevented by pipe_config_set_bpp. */
8677 BUG();
8678 }
8679
6e3c9717 8680 if (intel_crtc->config->dither)
756f85cf
PZ
8681 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8682
391bf048 8683 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
756f85cf 8684 }
ee2b0b38
PZ
8685}
8686
d4b1931c
PZ
8687int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8688{
8689 /*
8690 * Account for spread spectrum to avoid
8691 * oversubscribing the link. Max center spread
8692 * is 2.5%; use 5% for safety's sake.
8693 */
8694 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 8695 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
8696}
8697
7429e9d4 8698static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 8699{
7429e9d4 8700 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
8701}
8702
b75ca6f6
ACO
8703static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
8704 struct intel_crtc_state *crtc_state,
8705 intel_clock_t *reduced_clock)
79e53945 8706{
de13a2e3 8707 struct drm_crtc *crtc = &intel_crtc->base;
79e53945
JB
8708 struct drm_device *dev = crtc->dev;
8709 struct drm_i915_private *dev_priv = dev->dev_private;
55bb9992 8710 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 8711 struct drm_connector *connector;
55bb9992
ACO
8712 struct drm_connector_state *connector_state;
8713 struct intel_encoder *encoder;
b75ca6f6 8714 u32 dpll, fp, fp2;
ceb41007 8715 int factor, i;
09ede541 8716 bool is_lvds = false, is_sdvo = false;
79e53945 8717
da3ced29 8718 for_each_connector_in_state(state, connector, connector_state, i) {
55bb9992
ACO
8719 if (connector_state->crtc != crtc_state->base.crtc)
8720 continue;
8721
8722 encoder = to_intel_encoder(connector_state->best_encoder);
8723
8724 switch (encoder->type) {
79e53945
JB
8725 case INTEL_OUTPUT_LVDS:
8726 is_lvds = true;
8727 break;
8728 case INTEL_OUTPUT_SDVO:
7d57382e 8729 case INTEL_OUTPUT_HDMI:
79e53945 8730 is_sdvo = true;
79e53945 8731 break;
6847d71b
PZ
8732 default:
8733 break;
79e53945
JB
8734 }
8735 }
79e53945 8736
c1858123 8737 /* Enable autotuning of the PLL clock (if permissible) */
8febb297
EA
8738 factor = 21;
8739 if (is_lvds) {
8740 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 8741 dev_priv->vbt.lvds_ssc_freq == 100000) ||
f0b44056 8742 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
8febb297 8743 factor = 25;
190f68c5 8744 } else if (crtc_state->sdvo_tv_clock)
8febb297 8745 factor = 20;
c1858123 8746
b75ca6f6
ACO
8747 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
8748
190f68c5 8749 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
b75ca6f6
ACO
8750 fp |= FP_CB_TUNE;
8751
8752 if (reduced_clock) {
8753 fp2 = i9xx_dpll_compute_fp(reduced_clock);
2c07245f 8754
b75ca6f6
ACO
8755 if (reduced_clock->m < factor * reduced_clock->n)
8756 fp2 |= FP_CB_TUNE;
8757 } else {
8758 fp2 = fp;
8759 }
9a7c7890 8760
5eddb70b 8761 dpll = 0;
2c07245f 8762
a07d6787
EA
8763 if (is_lvds)
8764 dpll |= DPLLB_MODE_LVDS;
8765 else
8766 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 8767
190f68c5 8768 dpll |= (crtc_state->pixel_multiplier - 1)
ef1b460d 8769 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f
DV
8770
8771 if (is_sdvo)
4a33e48d 8772 dpll |= DPLL_SDVO_HIGH_SPEED;
190f68c5 8773 if (crtc_state->has_dp_encoder)
4a33e48d 8774 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 8775
a07d6787 8776 /* compute bitmask from p1 value */
190f68c5 8777 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 8778 /* also FPA1 */
190f68c5 8779 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 8780
190f68c5 8781 switch (crtc_state->dpll.p2) {
a07d6787
EA
8782 case 5:
8783 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8784 break;
8785 case 7:
8786 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8787 break;
8788 case 10:
8789 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8790 break;
8791 case 14:
8792 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8793 break;
79e53945
JB
8794 }
8795
ceb41007 8796 if (is_lvds && intel_panel_use_ssc(dev_priv))
43565a06 8797 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
8798 else
8799 dpll |= PLL_REF_INPUT_DREFCLK;
8800
b75ca6f6
ACO
8801 dpll |= DPLL_VCO_ENABLE;
8802
8803 crtc_state->dpll_hw_state.dpll = dpll;
8804 crtc_state->dpll_hw_state.fp0 = fp;
8805 crtc_state->dpll_hw_state.fp1 = fp2;
de13a2e3
PZ
8806}
8807
190f68c5
ACO
8808static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8809 struct intel_crtc_state *crtc_state)
de13a2e3 8810{
997c030c
ACO
8811 struct drm_device *dev = crtc->base.dev;
8812 struct drm_i915_private *dev_priv = dev->dev_private;
364ee29d 8813 intel_clock_t reduced_clock;
7ed9f894 8814 bool has_reduced_clock = false;
e2b78267 8815 struct intel_shared_dpll *pll;
997c030c
ACO
8816 const intel_limit_t *limit;
8817 int refclk = 120000;
de13a2e3 8818
dd3cd74a
ACO
8819 memset(&crtc_state->dpll_hw_state, 0,
8820 sizeof(crtc_state->dpll_hw_state));
8821
ded220e2
ACO
8822 crtc->lowfreq_avail = false;
8823
8824 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
8825 if (!crtc_state->has_pch_encoder)
8826 return 0;
79e53945 8827
997c030c
ACO
8828 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8829 if (intel_panel_use_ssc(dev_priv)) {
8830 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
8831 dev_priv->vbt.lvds_ssc_freq);
8832 refclk = dev_priv->vbt.lvds_ssc_freq;
8833 }
8834
8835 if (intel_is_dual_link_lvds(dev)) {
8836 if (refclk == 100000)
8837 limit = &intel_limits_ironlake_dual_lvds_100m;
8838 else
8839 limit = &intel_limits_ironlake_dual_lvds;
8840 } else {
8841 if (refclk == 100000)
8842 limit = &intel_limits_ironlake_single_lvds_100m;
8843 else
8844 limit = &intel_limits_ironlake_single_lvds;
8845 }
8846 } else {
8847 limit = &intel_limits_ironlake_dac;
8848 }
8849
364ee29d 8850 if (!crtc_state->clock_set &&
997c030c
ACO
8851 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8852 refclk, NULL, &crtc_state->dpll)) {
364ee29d
ACO
8853 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8854 return -EINVAL;
f47709a9 8855 }
79e53945 8856
b75ca6f6
ACO
8857 ironlake_compute_dpll(crtc, crtc_state,
8858 has_reduced_clock ? &reduced_clock : NULL);
66e985c0 8859
ded220e2
ACO
8860 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
8861 if (pll == NULL) {
8862 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
8863 pipe_name(crtc->pipe));
8864 return -EINVAL;
3fb37703 8865 }
79e53945 8866
ded220e2
ACO
8867 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8868 has_reduced_clock)
c7653199 8869 crtc->lowfreq_avail = true;
e2b78267 8870
c8f7a0db 8871 return 0;
79e53945
JB
8872}
8873
eb14cb74
VS
8874static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8875 struct intel_link_m_n *m_n)
8876{
8877 struct drm_device *dev = crtc->base.dev;
8878 struct drm_i915_private *dev_priv = dev->dev_private;
8879 enum pipe pipe = crtc->pipe;
8880
8881 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8882 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8883 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8884 & ~TU_SIZE_MASK;
8885 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8886 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8887 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8888}
8889
8890static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8891 enum transcoder transcoder,
b95af8be
VK
8892 struct intel_link_m_n *m_n,
8893 struct intel_link_m_n *m2_n2)
72419203
DV
8894{
8895 struct drm_device *dev = crtc->base.dev;
8896 struct drm_i915_private *dev_priv = dev->dev_private;
eb14cb74 8897 enum pipe pipe = crtc->pipe;
72419203 8898
eb14cb74
VS
8899 if (INTEL_INFO(dev)->gen >= 5) {
8900 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8901 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8902 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8903 & ~TU_SIZE_MASK;
8904 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8905 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8906 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
b95af8be
VK
8907 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8908 * gen < 8) and if DRRS is supported (to make sure the
8909 * registers are not unnecessarily read).
8910 */
8911 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
6e3c9717 8912 crtc->config->has_drrs) {
b95af8be
VK
8913 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8914 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8915 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8916 & ~TU_SIZE_MASK;
8917 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8918 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8919 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8920 }
eb14cb74
VS
8921 } else {
8922 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8923 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8924 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8925 & ~TU_SIZE_MASK;
8926 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8927 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8928 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8929 }
8930}
8931
8932void intel_dp_get_m_n(struct intel_crtc *crtc,
5cec258b 8933 struct intel_crtc_state *pipe_config)
eb14cb74 8934{
681a8504 8935 if (pipe_config->has_pch_encoder)
eb14cb74
VS
8936 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8937 else
8938 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be
VK
8939 &pipe_config->dp_m_n,
8940 &pipe_config->dp_m2_n2);
eb14cb74 8941}
72419203 8942
eb14cb74 8943static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5cec258b 8944 struct intel_crtc_state *pipe_config)
eb14cb74
VS
8945{
8946 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be 8947 &pipe_config->fdi_m_n, NULL);
72419203
DV
8948}
8949
bd2e244f 8950static void skylake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 8951 struct intel_crtc_state *pipe_config)
bd2e244f
JB
8952{
8953 struct drm_device *dev = crtc->base.dev;
8954 struct drm_i915_private *dev_priv = dev->dev_private;
a1b2278e
CK
8955 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
8956 uint32_t ps_ctrl = 0;
8957 int id = -1;
8958 int i;
bd2e244f 8959
a1b2278e
CK
8960 /* find scaler attached to this pipe */
8961 for (i = 0; i < crtc->num_scalers; i++) {
8962 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
8963 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
8964 id = i;
8965 pipe_config->pch_pfit.enabled = true;
8966 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
8967 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
8968 break;
8969 }
8970 }
bd2e244f 8971
a1b2278e
CK
8972 scaler_state->scaler_id = id;
8973 if (id >= 0) {
8974 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
8975 } else {
8976 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
bd2e244f
JB
8977 }
8978}
8979
5724dbd1
DL
8980static void
8981skylake_get_initial_plane_config(struct intel_crtc *crtc,
8982 struct intel_initial_plane_config *plane_config)
bc8d7dff
DL
8983{
8984 struct drm_device *dev = crtc->base.dev;
8985 struct drm_i915_private *dev_priv = dev->dev_private;
40f46283 8986 u32 val, base, offset, stride_mult, tiling;
bc8d7dff
DL
8987 int pipe = crtc->pipe;
8988 int fourcc, pixel_format;
6761dd31 8989 unsigned int aligned_height;
bc8d7dff 8990 struct drm_framebuffer *fb;
1b842c89 8991 struct intel_framebuffer *intel_fb;
bc8d7dff 8992
d9806c9f 8993 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 8994 if (!intel_fb) {
bc8d7dff
DL
8995 DRM_DEBUG_KMS("failed to alloc fb\n");
8996 return;
8997 }
8998
1b842c89
DL
8999 fb = &intel_fb->base;
9000
bc8d7dff 9001 val = I915_READ(PLANE_CTL(pipe, 0));
42a7b088
DL
9002 if (!(val & PLANE_CTL_ENABLE))
9003 goto error;
9004
bc8d7dff
DL
9005 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9006 fourcc = skl_format_to_fourcc(pixel_format,
9007 val & PLANE_CTL_ORDER_RGBX,
9008 val & PLANE_CTL_ALPHA_MASK);
9009 fb->pixel_format = fourcc;
9010 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9011
40f46283
DL
9012 tiling = val & PLANE_CTL_TILED_MASK;
9013 switch (tiling) {
9014 case PLANE_CTL_TILED_LINEAR:
9015 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9016 break;
9017 case PLANE_CTL_TILED_X:
9018 plane_config->tiling = I915_TILING_X;
9019 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9020 break;
9021 case PLANE_CTL_TILED_Y:
9022 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9023 break;
9024 case PLANE_CTL_TILED_YF:
9025 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9026 break;
9027 default:
9028 MISSING_CASE(tiling);
9029 goto error;
9030 }
9031
bc8d7dff
DL
9032 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9033 plane_config->base = base;
9034
9035 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9036
9037 val = I915_READ(PLANE_SIZE(pipe, 0));
9038 fb->height = ((val >> 16) & 0xfff) + 1;
9039 fb->width = ((val >> 0) & 0x1fff) + 1;
9040
9041 val = I915_READ(PLANE_STRIDE(pipe, 0));
7b49f948 9042 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
40f46283 9043 fb->pixel_format);
bc8d7dff
DL
9044 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9045
9046 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
9047 fb->pixel_format,
9048 fb->modifier[0]);
bc8d7dff 9049
f37b5c2b 9050 plane_config->size = fb->pitches[0] * aligned_height;
bc8d7dff
DL
9051
9052 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9053 pipe_name(pipe), fb->width, fb->height,
9054 fb->bits_per_pixel, base, fb->pitches[0],
9055 plane_config->size);
9056
2d14030b 9057 plane_config->fb = intel_fb;
bc8d7dff
DL
9058 return;
9059
9060error:
9061 kfree(fb);
9062}
9063
2fa2fe9a 9064static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9065 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
9066{
9067 struct drm_device *dev = crtc->base.dev;
9068 struct drm_i915_private *dev_priv = dev->dev_private;
9069 uint32_t tmp;
9070
9071 tmp = I915_READ(PF_CTL(crtc->pipe));
9072
9073 if (tmp & PF_ENABLE) {
fd4daa9c 9074 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
DV
9075 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9076 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
DV
9077
9078 /* We currently do not free assignements of panel fitters on
9079 * ivb/hsw (since we don't use the higher upscaling modes which
9080 * differentiates them) so just WARN about this case for now. */
9081 if (IS_GEN7(dev)) {
9082 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9083 PF_PIPE_SEL_IVB(crtc->pipe));
9084 }
2fa2fe9a 9085 }
79e53945
JB
9086}
9087
5724dbd1
DL
9088static void
9089ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9090 struct intel_initial_plane_config *plane_config)
4c6baa59
JB
9091{
9092 struct drm_device *dev = crtc->base.dev;
9093 struct drm_i915_private *dev_priv = dev->dev_private;
9094 u32 val, base, offset;
aeee5a49 9095 int pipe = crtc->pipe;
4c6baa59 9096 int fourcc, pixel_format;
6761dd31 9097 unsigned int aligned_height;
b113d5ee 9098 struct drm_framebuffer *fb;
1b842c89 9099 struct intel_framebuffer *intel_fb;
4c6baa59 9100
42a7b088
DL
9101 val = I915_READ(DSPCNTR(pipe));
9102 if (!(val & DISPLAY_PLANE_ENABLE))
9103 return;
9104
d9806c9f 9105 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9106 if (!intel_fb) {
4c6baa59
JB
9107 DRM_DEBUG_KMS("failed to alloc fb\n");
9108 return;
9109 }
9110
1b842c89
DL
9111 fb = &intel_fb->base;
9112
18c5247e
DV
9113 if (INTEL_INFO(dev)->gen >= 4) {
9114 if (val & DISPPLANE_TILED) {
49af449b 9115 plane_config->tiling = I915_TILING_X;
18c5247e
DV
9116 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9117 }
9118 }
4c6baa59
JB
9119
9120 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 9121 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
9122 fb->pixel_format = fourcc;
9123 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
4c6baa59 9124
aeee5a49 9125 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
4c6baa59 9126 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
aeee5a49 9127 offset = I915_READ(DSPOFFSET(pipe));
4c6baa59 9128 } else {
49af449b 9129 if (plane_config->tiling)
aeee5a49 9130 offset = I915_READ(DSPTILEOFF(pipe));
4c6baa59 9131 else
aeee5a49 9132 offset = I915_READ(DSPLINOFF(pipe));
4c6baa59
JB
9133 }
9134 plane_config->base = base;
9135
9136 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
9137 fb->width = ((val >> 16) & 0xfff) + 1;
9138 fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
9139
9140 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 9141 fb->pitches[0] = val & 0xffffffc0;
4c6baa59 9142
b113d5ee 9143 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
9144 fb->pixel_format,
9145 fb->modifier[0]);
4c6baa59 9146
f37b5c2b 9147 plane_config->size = fb->pitches[0] * aligned_height;
4c6baa59 9148
2844a921
DL
9149 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9150 pipe_name(pipe), fb->width, fb->height,
9151 fb->bits_per_pixel, base, fb->pitches[0],
9152 plane_config->size);
b113d5ee 9153
2d14030b 9154 plane_config->fb = intel_fb;
4c6baa59
JB
9155}
9156
0e8ffe1b 9157static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9158 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
9159{
9160 struct drm_device *dev = crtc->base.dev;
9161 struct drm_i915_private *dev_priv = dev->dev_private;
1729050e 9162 enum intel_display_power_domain power_domain;
0e8ffe1b 9163 uint32_t tmp;
1729050e 9164 bool ret;
0e8ffe1b 9165
1729050e
ID
9166 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9167 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
930e8c9e
PZ
9168 return false;
9169
e143a21c 9170 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 9171 pipe_config->shared_dpll = NULL;
eccb140b 9172
1729050e 9173 ret = false;
0e8ffe1b
DV
9174 tmp = I915_READ(PIPECONF(crtc->pipe));
9175 if (!(tmp & PIPECONF_ENABLE))
1729050e 9176 goto out;
0e8ffe1b 9177
42571aef
VS
9178 switch (tmp & PIPECONF_BPC_MASK) {
9179 case PIPECONF_6BPC:
9180 pipe_config->pipe_bpp = 18;
9181 break;
9182 case PIPECONF_8BPC:
9183 pipe_config->pipe_bpp = 24;
9184 break;
9185 case PIPECONF_10BPC:
9186 pipe_config->pipe_bpp = 30;
9187 break;
9188 case PIPECONF_12BPC:
9189 pipe_config->pipe_bpp = 36;
9190 break;
9191 default:
9192 break;
9193 }
9194
b5a9fa09
DV
9195 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9196 pipe_config->limited_color_range = true;
9197
ab9412ba 9198 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0 9199 struct intel_shared_dpll *pll;
8106ddbd 9200 enum intel_dpll_id pll_id;
66e985c0 9201
88adfff1
DV
9202 pipe_config->has_pch_encoder = true;
9203
627eb5a3
DV
9204 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9205 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9206 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
9207
9208 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 9209
c0d43d62 9210 if (HAS_PCH_IBX(dev_priv->dev)) {
8106ddbd 9211 pll_id = (enum intel_dpll_id) crtc->pipe;
c0d43d62
DV
9212 } else {
9213 tmp = I915_READ(PCH_DPLL_SEL);
9214 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
8106ddbd 9215 pll_id = DPLL_ID_PCH_PLL_B;
c0d43d62 9216 else
8106ddbd 9217 pll_id= DPLL_ID_PCH_PLL_A;
c0d43d62 9218 }
66e985c0 9219
8106ddbd
ACO
9220 pipe_config->shared_dpll =
9221 intel_get_shared_dpll_by_id(dev_priv, pll_id);
9222 pll = pipe_config->shared_dpll;
66e985c0 9223
2edd6443
ACO
9224 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9225 &pipe_config->dpll_hw_state));
c93f54cf
DV
9226
9227 tmp = pipe_config->dpll_hw_state.dpll;
9228 pipe_config->pixel_multiplier =
9229 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9230 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
9231
9232 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
DV
9233 } else {
9234 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
9235 }
9236
1bd1bd80 9237 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 9238 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 9239
2fa2fe9a
DV
9240 ironlake_get_pfit_config(crtc, pipe_config);
9241
1729050e
ID
9242 ret = true;
9243
9244out:
9245 intel_display_power_put(dev_priv, power_domain);
9246
9247 return ret;
0e8ffe1b
DV
9248}
9249
be256dc7
PZ
9250static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9251{
9252 struct drm_device *dev = dev_priv->dev;
be256dc7 9253 struct intel_crtc *crtc;
be256dc7 9254
d3fcc808 9255 for_each_intel_crtc(dev, crtc)
e2c719b7 9256 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
9257 pipe_name(crtc->pipe));
9258
e2c719b7
RC
9259 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9260 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
01403de3
VS
9261 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9262 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
e2c719b7
RC
9263 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9264 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
be256dc7 9265 "CPU PWM1 enabled\n");
c5107b87 9266 if (IS_HASWELL(dev))
e2c719b7 9267 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
c5107b87 9268 "CPU PWM2 enabled\n");
e2c719b7 9269 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
be256dc7 9270 "PCH PWM1 enabled\n");
e2c719b7 9271 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
be256dc7 9272 "Utility pin enabled\n");
e2c719b7 9273 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
be256dc7 9274
9926ada1
PZ
9275 /*
9276 * In theory we can still leave IRQs enabled, as long as only the HPD
9277 * interrupts remain enabled. We used to check for that, but since it's
9278 * gen-specific and since we only disable LCPLL after we fully disable
9279 * the interrupts, the check below should be enough.
9280 */
e2c719b7 9281 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
be256dc7
PZ
9282}
9283
9ccd5aeb
PZ
9284static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9285{
9286 struct drm_device *dev = dev_priv->dev;
9287
9288 if (IS_HASWELL(dev))
9289 return I915_READ(D_COMP_HSW);
9290 else
9291 return I915_READ(D_COMP_BDW);
9292}
9293
3c4c9b81
PZ
9294static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9295{
9296 struct drm_device *dev = dev_priv->dev;
9297
9298 if (IS_HASWELL(dev)) {
9299 mutex_lock(&dev_priv->rps.hw_lock);
9300 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9301 val))
f475dadf 9302 DRM_ERROR("Failed to write to D_COMP\n");
3c4c9b81
PZ
9303 mutex_unlock(&dev_priv->rps.hw_lock);
9304 } else {
9ccd5aeb
PZ
9305 I915_WRITE(D_COMP_BDW, val);
9306 POSTING_READ(D_COMP_BDW);
3c4c9b81 9307 }
be256dc7
PZ
9308}
9309
9310/*
9311 * This function implements pieces of two sequences from BSpec:
9312 * - Sequence for display software to disable LCPLL
9313 * - Sequence for display software to allow package C8+
9314 * The steps implemented here are just the steps that actually touch the LCPLL
9315 * register. Callers should take care of disabling all the display engine
9316 * functions, doing the mode unset, fixing interrupts, etc.
9317 */
6ff58d53
PZ
9318static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9319 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
9320{
9321 uint32_t val;
9322
9323 assert_can_disable_lcpll(dev_priv);
9324
9325 val = I915_READ(LCPLL_CTL);
9326
9327 if (switch_to_fclk) {
9328 val |= LCPLL_CD_SOURCE_FCLK;
9329 I915_WRITE(LCPLL_CTL, val);
9330
9331 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9332 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9333 DRM_ERROR("Switching to FCLK failed\n");
9334
9335 val = I915_READ(LCPLL_CTL);
9336 }
9337
9338 val |= LCPLL_PLL_DISABLE;
9339 I915_WRITE(LCPLL_CTL, val);
9340 POSTING_READ(LCPLL_CTL);
9341
9342 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9343 DRM_ERROR("LCPLL still locked\n");
9344
9ccd5aeb 9345 val = hsw_read_dcomp(dev_priv);
be256dc7 9346 val |= D_COMP_COMP_DISABLE;
3c4c9b81 9347 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9348 ndelay(100);
9349
9ccd5aeb
PZ
9350 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9351 1))
be256dc7
PZ
9352 DRM_ERROR("D_COMP RCOMP still in progress\n");
9353
9354 if (allow_power_down) {
9355 val = I915_READ(LCPLL_CTL);
9356 val |= LCPLL_POWER_DOWN_ALLOW;
9357 I915_WRITE(LCPLL_CTL, val);
9358 POSTING_READ(LCPLL_CTL);
9359 }
9360}
9361
9362/*
9363 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9364 * source.
9365 */
6ff58d53 9366static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
9367{
9368 uint32_t val;
9369
9370 val = I915_READ(LCPLL_CTL);
9371
9372 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9373 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9374 return;
9375
a8a8bd54
PZ
9376 /*
9377 * Make sure we're not on PC8 state before disabling PC8, otherwise
9378 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
a8a8bd54 9379 */
59bad947 9380 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
215733fa 9381
be256dc7
PZ
9382 if (val & LCPLL_POWER_DOWN_ALLOW) {
9383 val &= ~LCPLL_POWER_DOWN_ALLOW;
9384 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 9385 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
9386 }
9387
9ccd5aeb 9388 val = hsw_read_dcomp(dev_priv);
be256dc7
PZ
9389 val |= D_COMP_COMP_FORCE;
9390 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 9391 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9392
9393 val = I915_READ(LCPLL_CTL);
9394 val &= ~LCPLL_PLL_DISABLE;
9395 I915_WRITE(LCPLL_CTL, val);
9396
9397 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9398 DRM_ERROR("LCPLL not locked yet\n");
9399
9400 if (val & LCPLL_CD_SOURCE_FCLK) {
9401 val = I915_READ(LCPLL_CTL);
9402 val &= ~LCPLL_CD_SOURCE_FCLK;
9403 I915_WRITE(LCPLL_CTL, val);
9404
9405 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9406 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9407 DRM_ERROR("Switching back to LCPLL failed\n");
9408 }
215733fa 9409
59bad947 9410 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
b6283055 9411 intel_update_cdclk(dev_priv->dev);
be256dc7
PZ
9412}
9413
765dab67
PZ
9414/*
9415 * Package states C8 and deeper are really deep PC states that can only be
9416 * reached when all the devices on the system allow it, so even if the graphics
9417 * device allows PC8+, it doesn't mean the system will actually get to these
9418 * states. Our driver only allows PC8+ when going into runtime PM.
9419 *
9420 * The requirements for PC8+ are that all the outputs are disabled, the power
9421 * well is disabled and most interrupts are disabled, and these are also
9422 * requirements for runtime PM. When these conditions are met, we manually do
9423 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9424 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9425 * hang the machine.
9426 *
9427 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9428 * the state of some registers, so when we come back from PC8+ we need to
9429 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9430 * need to take care of the registers kept by RC6. Notice that this happens even
9431 * if we don't put the device in PCI D3 state (which is what currently happens
9432 * because of the runtime PM support).
9433 *
9434 * For more, read "Display Sequences for Package C8" on the hardware
9435 * documentation.
9436 */
a14cb6fc 9437void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 9438{
c67a470b
PZ
9439 struct drm_device *dev = dev_priv->dev;
9440 uint32_t val;
9441
c67a470b
PZ
9442 DRM_DEBUG_KMS("Enabling package C8+\n");
9443
c2699524 9444 if (HAS_PCH_LPT_LP(dev)) {
c67a470b
PZ
9445 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9446 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9447 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9448 }
9449
9450 lpt_disable_clkout_dp(dev);
c67a470b
PZ
9451 hsw_disable_lcpll(dev_priv, true, true);
9452}
9453
a14cb6fc 9454void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b
PZ
9455{
9456 struct drm_device *dev = dev_priv->dev;
9457 uint32_t val;
9458
c67a470b
PZ
9459 DRM_DEBUG_KMS("Disabling package C8+\n");
9460
9461 hsw_restore_lcpll(dev_priv);
c67a470b
PZ
9462 lpt_init_pch_refclk(dev);
9463
c2699524 9464 if (HAS_PCH_LPT_LP(dev)) {
c67a470b
PZ
9465 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9466 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9467 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9468 }
c67a470b
PZ
9469}
9470
27c329ed 9471static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
f8437dd1 9472{
a821fc46 9473 struct drm_device *dev = old_state->dev;
1a617b77
ML
9474 struct intel_atomic_state *old_intel_state =
9475 to_intel_atomic_state(old_state);
9476 unsigned int req_cdclk = old_intel_state->dev_cdclk;
f8437dd1 9477
27c329ed 9478 broxton_set_cdclk(dev, req_cdclk);
f8437dd1
VK
9479}
9480
b432e5cf 9481/* compute the max rate for new configuration */
27c329ed 9482static int ilk_max_pixel_rate(struct drm_atomic_state *state)
b432e5cf 9483{
565602d7
ML
9484 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
9485 struct drm_i915_private *dev_priv = state->dev->dev_private;
9486 struct drm_crtc *crtc;
9487 struct drm_crtc_state *cstate;
27c329ed 9488 struct intel_crtc_state *crtc_state;
565602d7
ML
9489 unsigned max_pixel_rate = 0, i;
9490 enum pipe pipe;
b432e5cf 9491
565602d7
ML
9492 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
9493 sizeof(intel_state->min_pixclk));
27c329ed 9494
565602d7
ML
9495 for_each_crtc_in_state(state, crtc, cstate, i) {
9496 int pixel_rate;
27c329ed 9497
565602d7
ML
9498 crtc_state = to_intel_crtc_state(cstate);
9499 if (!crtc_state->base.enable) {
9500 intel_state->min_pixclk[i] = 0;
b432e5cf 9501 continue;
565602d7 9502 }
b432e5cf 9503
27c329ed 9504 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
b432e5cf
VS
9505
9506 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
565602d7 9507 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
b432e5cf
VS
9508 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9509
565602d7 9510 intel_state->min_pixclk[i] = pixel_rate;
b432e5cf
VS
9511 }
9512
565602d7
ML
9513 for_each_pipe(dev_priv, pipe)
9514 max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
9515
b432e5cf
VS
9516 return max_pixel_rate;
9517}
9518
9519static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9520{
9521 struct drm_i915_private *dev_priv = dev->dev_private;
9522 uint32_t val, data;
9523 int ret;
9524
9525 if (WARN((I915_READ(LCPLL_CTL) &
9526 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9527 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9528 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9529 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9530 "trying to change cdclk frequency with cdclk not enabled\n"))
9531 return;
9532
9533 mutex_lock(&dev_priv->rps.hw_lock);
9534 ret = sandybridge_pcode_write(dev_priv,
9535 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9536 mutex_unlock(&dev_priv->rps.hw_lock);
9537 if (ret) {
9538 DRM_ERROR("failed to inform pcode about cdclk change\n");
9539 return;
9540 }
9541
9542 val = I915_READ(LCPLL_CTL);
9543 val |= LCPLL_CD_SOURCE_FCLK;
9544 I915_WRITE(LCPLL_CTL, val);
9545
5ba00178
TU
9546 if (wait_for_us(I915_READ(LCPLL_CTL) &
9547 LCPLL_CD_SOURCE_FCLK_DONE, 1))
b432e5cf
VS
9548 DRM_ERROR("Switching to FCLK failed\n");
9549
9550 val = I915_READ(LCPLL_CTL);
9551 val &= ~LCPLL_CLK_FREQ_MASK;
9552
9553 switch (cdclk) {
9554 case 450000:
9555 val |= LCPLL_CLK_FREQ_450;
9556 data = 0;
9557 break;
9558 case 540000:
9559 val |= LCPLL_CLK_FREQ_54O_BDW;
9560 data = 1;
9561 break;
9562 case 337500:
9563 val |= LCPLL_CLK_FREQ_337_5_BDW;
9564 data = 2;
9565 break;
9566 case 675000:
9567 val |= LCPLL_CLK_FREQ_675_BDW;
9568 data = 3;
9569 break;
9570 default:
9571 WARN(1, "invalid cdclk frequency\n");
9572 return;
9573 }
9574
9575 I915_WRITE(LCPLL_CTL, val);
9576
9577 val = I915_READ(LCPLL_CTL);
9578 val &= ~LCPLL_CD_SOURCE_FCLK;
9579 I915_WRITE(LCPLL_CTL, val);
9580
5ba00178
TU
9581 if (wait_for_us((I915_READ(LCPLL_CTL) &
9582 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
b432e5cf
VS
9583 DRM_ERROR("Switching back to LCPLL failed\n");
9584
9585 mutex_lock(&dev_priv->rps.hw_lock);
9586 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9587 mutex_unlock(&dev_priv->rps.hw_lock);
9588
9589 intel_update_cdclk(dev);
9590
9591 WARN(cdclk != dev_priv->cdclk_freq,
9592 "cdclk requested %d kHz but got %d kHz\n",
9593 cdclk, dev_priv->cdclk_freq);
9594}
9595
27c329ed 9596static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
b432e5cf 9597{
27c329ed 9598 struct drm_i915_private *dev_priv = to_i915(state->dev);
1a617b77 9599 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
27c329ed 9600 int max_pixclk = ilk_max_pixel_rate(state);
b432e5cf
VS
9601 int cdclk;
9602
9603 /*
9604 * FIXME should also account for plane ratio
9605 * once 64bpp pixel formats are supported.
9606 */
27c329ed 9607 if (max_pixclk > 540000)
b432e5cf 9608 cdclk = 675000;
27c329ed 9609 else if (max_pixclk > 450000)
b432e5cf 9610 cdclk = 540000;
27c329ed 9611 else if (max_pixclk > 337500)
b432e5cf
VS
9612 cdclk = 450000;
9613 else
9614 cdclk = 337500;
9615
b432e5cf 9616 if (cdclk > dev_priv->max_cdclk_freq) {
63ba534e
ML
9617 DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9618 cdclk, dev_priv->max_cdclk_freq);
9619 return -EINVAL;
b432e5cf
VS
9620 }
9621
1a617b77
ML
9622 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9623 if (!intel_state->active_crtcs)
9624 intel_state->dev_cdclk = 337500;
b432e5cf
VS
9625
9626 return 0;
9627}
9628
27c329ed 9629static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
b432e5cf 9630{
27c329ed 9631 struct drm_device *dev = old_state->dev;
1a617b77
ML
9632 struct intel_atomic_state *old_intel_state =
9633 to_intel_atomic_state(old_state);
9634 unsigned req_cdclk = old_intel_state->dev_cdclk;
b432e5cf 9635
27c329ed 9636 broadwell_set_cdclk(dev, req_cdclk);
b432e5cf
VS
9637}
9638
190f68c5
ACO
9639static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9640 struct intel_crtc_state *crtc_state)
09b4ddf9 9641{
af3997b5
MK
9642 struct intel_encoder *intel_encoder =
9643 intel_ddi_get_crtc_new_encoder(crtc_state);
9644
9645 if (intel_encoder->type != INTEL_OUTPUT_DSI) {
9646 if (!intel_ddi_pll_select(crtc, crtc_state))
9647 return -EINVAL;
9648 }
716c2e55 9649
c7653199 9650 crtc->lowfreq_avail = false;
644cef34 9651
c8f7a0db 9652 return 0;
79e53945
JB
9653}
9654
3760b59c
S
9655static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9656 enum port port,
9657 struct intel_crtc_state *pipe_config)
9658{
8106ddbd
ACO
9659 enum intel_dpll_id id;
9660
3760b59c
S
9661 switch (port) {
9662 case PORT_A:
9663 pipe_config->ddi_pll_sel = SKL_DPLL0;
08250c4b 9664 id = DPLL_ID_SKL_DPLL0;
3760b59c
S
9665 break;
9666 case PORT_B:
9667 pipe_config->ddi_pll_sel = SKL_DPLL1;
08250c4b 9668 id = DPLL_ID_SKL_DPLL1;
3760b59c
S
9669 break;
9670 case PORT_C:
9671 pipe_config->ddi_pll_sel = SKL_DPLL2;
08250c4b 9672 id = DPLL_ID_SKL_DPLL2;
3760b59c
S
9673 break;
9674 default:
9675 DRM_ERROR("Incorrect port type\n");
8106ddbd 9676 return;
3760b59c 9677 }
8106ddbd
ACO
9678
9679 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
3760b59c
S
9680}
9681
96b7dfb7
S
9682static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9683 enum port port,
5cec258b 9684 struct intel_crtc_state *pipe_config)
96b7dfb7 9685{
8106ddbd 9686 enum intel_dpll_id id;
a3c988ea 9687 u32 temp;
96b7dfb7
S
9688
9689 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9690 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9691
9692 switch (pipe_config->ddi_pll_sel) {
3148ade7 9693 case SKL_DPLL0:
a3c988ea
ACO
9694 id = DPLL_ID_SKL_DPLL0;
9695 break;
96b7dfb7 9696 case SKL_DPLL1:
8106ddbd 9697 id = DPLL_ID_SKL_DPLL1;
96b7dfb7
S
9698 break;
9699 case SKL_DPLL2:
8106ddbd 9700 id = DPLL_ID_SKL_DPLL2;
96b7dfb7
S
9701 break;
9702 case SKL_DPLL3:
8106ddbd 9703 id = DPLL_ID_SKL_DPLL3;
96b7dfb7 9704 break;
8106ddbd
ACO
9705 default:
9706 MISSING_CASE(pipe_config->ddi_pll_sel);
9707 return;
96b7dfb7 9708 }
8106ddbd
ACO
9709
9710 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
96b7dfb7
S
9711}
9712
7d2c8175
DL
9713static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9714 enum port port,
5cec258b 9715 struct intel_crtc_state *pipe_config)
7d2c8175 9716{
8106ddbd
ACO
9717 enum intel_dpll_id id;
9718
7d2c8175
DL
9719 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9720
9721 switch (pipe_config->ddi_pll_sel) {
9722 case PORT_CLK_SEL_WRPLL1:
8106ddbd 9723 id = DPLL_ID_WRPLL1;
7d2c8175
DL
9724 break;
9725 case PORT_CLK_SEL_WRPLL2:
8106ddbd 9726 id = DPLL_ID_WRPLL2;
7d2c8175 9727 break;
00490c22 9728 case PORT_CLK_SEL_SPLL:
8106ddbd 9729 id = DPLL_ID_SPLL;
79bd23da 9730 break;
9d16da65
ACO
9731 case PORT_CLK_SEL_LCPLL_810:
9732 id = DPLL_ID_LCPLL_810;
9733 break;
9734 case PORT_CLK_SEL_LCPLL_1350:
9735 id = DPLL_ID_LCPLL_1350;
9736 break;
9737 case PORT_CLK_SEL_LCPLL_2700:
9738 id = DPLL_ID_LCPLL_2700;
9739 break;
8106ddbd
ACO
9740 default:
9741 MISSING_CASE(pipe_config->ddi_pll_sel);
9742 /* fall through */
9743 case PORT_CLK_SEL_NONE:
8106ddbd 9744 return;
7d2c8175 9745 }
8106ddbd
ACO
9746
9747 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
7d2c8175
DL
9748}
9749
cf30429e
JN
9750static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
9751 struct intel_crtc_state *pipe_config,
9752 unsigned long *power_domain_mask)
9753{
9754 struct drm_device *dev = crtc->base.dev;
9755 struct drm_i915_private *dev_priv = dev->dev_private;
9756 enum intel_display_power_domain power_domain;
9757 u32 tmp;
9758
9759 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
9760
9761 /*
9762 * XXX: Do intel_display_power_get_if_enabled before reading this (for
9763 * consistency and less surprising code; it's in always on power).
9764 */
9765 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9766 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9767 enum pipe trans_edp_pipe;
9768 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9769 default:
9770 WARN(1, "unknown pipe linked to edp transcoder\n");
9771 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9772 case TRANS_DDI_EDP_INPUT_A_ON:
9773 trans_edp_pipe = PIPE_A;
9774 break;
9775 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9776 trans_edp_pipe = PIPE_B;
9777 break;
9778 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9779 trans_edp_pipe = PIPE_C;
9780 break;
9781 }
9782
9783 if (trans_edp_pipe == crtc->pipe)
9784 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9785 }
9786
9787 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
9788 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9789 return false;
9790 *power_domain_mask |= BIT(power_domain);
9791
9792 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
9793
9794 return tmp & PIPECONF_ENABLE;
9795}
9796
4d1de975
JN
9797static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
9798 struct intel_crtc_state *pipe_config,
9799 unsigned long *power_domain_mask)
9800{
9801 struct drm_device *dev = crtc->base.dev;
9802 struct drm_i915_private *dev_priv = dev->dev_private;
9803 enum intel_display_power_domain power_domain;
9804 enum port port;
9805 enum transcoder cpu_transcoder;
9806 u32 tmp;
9807
9808 pipe_config->has_dsi_encoder = false;
9809
9810 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
9811 if (port == PORT_A)
9812 cpu_transcoder = TRANSCODER_DSI_A;
9813 else
9814 cpu_transcoder = TRANSCODER_DSI_C;
9815
9816 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
9817 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9818 continue;
9819 *power_domain_mask |= BIT(power_domain);
9820
9821 /* XXX: this works for video mode only */
9822 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
9823 if (!(tmp & DPI_ENABLE))
9824 continue;
9825
9826 tmp = I915_READ(MIPI_CTRL(port));
9827 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
9828 continue;
9829
9830 pipe_config->cpu_transcoder = cpu_transcoder;
9831 pipe_config->has_dsi_encoder = true;
9832 break;
9833 }
9834
9835 return pipe_config->has_dsi_encoder;
9836}
9837
26804afd 9838static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
5cec258b 9839 struct intel_crtc_state *pipe_config)
26804afd
DV
9840{
9841 struct drm_device *dev = crtc->base.dev;
9842 struct drm_i915_private *dev_priv = dev->dev_private;
d452c5b6 9843 struct intel_shared_dpll *pll;
26804afd
DV
9844 enum port port;
9845 uint32_t tmp;
9846
9847 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9848
9849 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9850
ef11bdb3 9851 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
96b7dfb7 9852 skylake_get_ddi_pll(dev_priv, port, pipe_config);
3760b59c
S
9853 else if (IS_BROXTON(dev))
9854 bxt_get_ddi_pll(dev_priv, port, pipe_config);
96b7dfb7
S
9855 else
9856 haswell_get_ddi_pll(dev_priv, port, pipe_config);
9cd86933 9857
8106ddbd
ACO
9858 pll = pipe_config->shared_dpll;
9859 if (pll) {
2edd6443
ACO
9860 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9861 &pipe_config->dpll_hw_state));
d452c5b6
DV
9862 }
9863
26804afd
DV
9864 /*
9865 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9866 * DDI E. So just check whether this pipe is wired to DDI E and whether
9867 * the PCH transcoder is on.
9868 */
ca370455
DL
9869 if (INTEL_INFO(dev)->gen < 9 &&
9870 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
26804afd
DV
9871 pipe_config->has_pch_encoder = true;
9872
9873 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9874 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9875 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9876
9877 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9878 }
9879}
9880
0e8ffe1b 9881static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9882 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
9883{
9884 struct drm_device *dev = crtc->base.dev;
9885 struct drm_i915_private *dev_priv = dev->dev_private;
1729050e
ID
9886 enum intel_display_power_domain power_domain;
9887 unsigned long power_domain_mask;
cf30429e 9888 bool active;
0e8ffe1b 9889
1729050e
ID
9890 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9891 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0 9892 return false;
1729050e
ID
9893 power_domain_mask = BIT(power_domain);
9894
8106ddbd 9895 pipe_config->shared_dpll = NULL;
c0d43d62 9896
cf30429e 9897 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
eccb140b 9898
4d1de975
JN
9899 if (IS_BROXTON(dev_priv)) {
9900 bxt_get_dsi_transcoder_state(crtc, pipe_config,
9901 &power_domain_mask);
9902 WARN_ON(active && pipe_config->has_dsi_encoder);
9903 if (pipe_config->has_dsi_encoder)
9904 active = true;
9905 }
9906
cf30429e 9907 if (!active)
1729050e 9908 goto out;
0e8ffe1b 9909
4d1de975
JN
9910 if (!pipe_config->has_dsi_encoder) {
9911 haswell_get_ddi_port_state(crtc, pipe_config);
9912 intel_get_pipe_timings(crtc, pipe_config);
9913 }
627eb5a3 9914
bc58be60 9915 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 9916
05dc698c
LL
9917 pipe_config->gamma_mode =
9918 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
9919
a1b2278e
CK
9920 if (INTEL_INFO(dev)->gen >= 9) {
9921 skl_init_scalers(dev, crtc, pipe_config);
9922 }
9923
af99ceda
CK
9924 if (INTEL_INFO(dev)->gen >= 9) {
9925 pipe_config->scaler_state.scaler_id = -1;
9926 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9927 }
9928
1729050e
ID
9929 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
9930 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
9931 power_domain_mask |= BIT(power_domain);
1c132b44 9932 if (INTEL_INFO(dev)->gen >= 9)
bd2e244f 9933 skylake_get_pfit_config(crtc, pipe_config);
ff6d9f55 9934 else
1c132b44 9935 ironlake_get_pfit_config(crtc, pipe_config);
bd2e244f 9936 }
88adfff1 9937
e59150dc
JB
9938 if (IS_HASWELL(dev))
9939 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9940 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 9941
4d1de975
JN
9942 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
9943 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
ebb69c95
CT
9944 pipe_config->pixel_multiplier =
9945 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9946 } else {
9947 pipe_config->pixel_multiplier = 1;
9948 }
6c49f241 9949
1729050e
ID
9950out:
9951 for_each_power_domain(power_domain, power_domain_mask)
9952 intel_display_power_put(dev_priv, power_domain);
9953
cf30429e 9954 return active;
0e8ffe1b
DV
9955}
9956
55a08b3f
ML
9957static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
9958 const struct intel_plane_state *plane_state)
560b85bb
CW
9959{
9960 struct drm_device *dev = crtc->dev;
9961 struct drm_i915_private *dev_priv = dev->dev_private;
9962 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
dc41c154 9963 uint32_t cntl = 0, size = 0;
560b85bb 9964
55a08b3f
ML
9965 if (plane_state && plane_state->visible) {
9966 unsigned int width = plane_state->base.crtc_w;
9967 unsigned int height = plane_state->base.crtc_h;
dc41c154
VS
9968 unsigned int stride = roundup_pow_of_two(width) * 4;
9969
9970 switch (stride) {
9971 default:
9972 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
9973 width, stride);
9974 stride = 256;
9975 /* fallthrough */
9976 case 256:
9977 case 512:
9978 case 1024:
9979 case 2048:
9980 break;
4b0e333e
CW
9981 }
9982
dc41c154
VS
9983 cntl |= CURSOR_ENABLE |
9984 CURSOR_GAMMA_ENABLE |
9985 CURSOR_FORMAT_ARGB |
9986 CURSOR_STRIDE(stride);
9987
9988 size = (height << 12) | width;
4b0e333e 9989 }
560b85bb 9990
dc41c154
VS
9991 if (intel_crtc->cursor_cntl != 0 &&
9992 (intel_crtc->cursor_base != base ||
9993 intel_crtc->cursor_size != size ||
9994 intel_crtc->cursor_cntl != cntl)) {
9995 /* On these chipsets we can only modify the base/size/stride
9996 * whilst the cursor is disabled.
9997 */
0b87c24e
VS
9998 I915_WRITE(CURCNTR(PIPE_A), 0);
9999 POSTING_READ(CURCNTR(PIPE_A));
dc41c154 10000 intel_crtc->cursor_cntl = 0;
4b0e333e 10001 }
560b85bb 10002
99d1f387 10003 if (intel_crtc->cursor_base != base) {
0b87c24e 10004 I915_WRITE(CURBASE(PIPE_A), base);
99d1f387
VS
10005 intel_crtc->cursor_base = base;
10006 }
4726e0b0 10007
dc41c154
VS
10008 if (intel_crtc->cursor_size != size) {
10009 I915_WRITE(CURSIZE, size);
10010 intel_crtc->cursor_size = size;
4b0e333e 10011 }
560b85bb 10012
4b0e333e 10013 if (intel_crtc->cursor_cntl != cntl) {
0b87c24e
VS
10014 I915_WRITE(CURCNTR(PIPE_A), cntl);
10015 POSTING_READ(CURCNTR(PIPE_A));
4b0e333e 10016 intel_crtc->cursor_cntl = cntl;
560b85bb 10017 }
560b85bb
CW
10018}
10019
55a08b3f
ML
10020static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
10021 const struct intel_plane_state *plane_state)
65a21cd6
JB
10022{
10023 struct drm_device *dev = crtc->dev;
10024 struct drm_i915_private *dev_priv = dev->dev_private;
10025 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10026 int pipe = intel_crtc->pipe;
663f3122 10027 uint32_t cntl = 0;
4b0e333e 10028
55a08b3f 10029 if (plane_state && plane_state->visible) {
4b0e333e 10030 cntl = MCURSOR_GAMMA_ENABLE;
55a08b3f 10031 switch (plane_state->base.crtc_w) {
4726e0b0
SK
10032 case 64:
10033 cntl |= CURSOR_MODE_64_ARGB_AX;
10034 break;
10035 case 128:
10036 cntl |= CURSOR_MODE_128_ARGB_AX;
10037 break;
10038 case 256:
10039 cntl |= CURSOR_MODE_256_ARGB_AX;
10040 break;
10041 default:
55a08b3f 10042 MISSING_CASE(plane_state->base.crtc_w);
4726e0b0 10043 return;
65a21cd6 10044 }
4b0e333e 10045 cntl |= pipe << 28; /* Connect to correct pipe */
47bf17a7 10046
fc6f93bc 10047 if (HAS_DDI(dev))
47bf17a7 10048 cntl |= CURSOR_PIPE_CSC_ENABLE;
65a21cd6 10049
55a08b3f
ML
10050 if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
10051 cntl |= CURSOR_ROTATE_180;
10052 }
4398ad45 10053
4b0e333e
CW
10054 if (intel_crtc->cursor_cntl != cntl) {
10055 I915_WRITE(CURCNTR(pipe), cntl);
10056 POSTING_READ(CURCNTR(pipe));
10057 intel_crtc->cursor_cntl = cntl;
65a21cd6 10058 }
4b0e333e 10059
65a21cd6 10060 /* and commit changes on next vblank */
5efb3e28
VS
10061 I915_WRITE(CURBASE(pipe), base);
10062 POSTING_READ(CURBASE(pipe));
99d1f387
VS
10063
10064 intel_crtc->cursor_base = base;
65a21cd6
JB
10065}
10066
cda4b7d3 10067/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6b383a7f 10068static void intel_crtc_update_cursor(struct drm_crtc *crtc,
55a08b3f 10069 const struct intel_plane_state *plane_state)
cda4b7d3
CW
10070{
10071 struct drm_device *dev = crtc->dev;
10072 struct drm_i915_private *dev_priv = dev->dev_private;
10073 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10074 int pipe = intel_crtc->pipe;
55a08b3f
ML
10075 u32 base = intel_crtc->cursor_addr;
10076 u32 pos = 0;
cda4b7d3 10077
55a08b3f
ML
10078 if (plane_state) {
10079 int x = plane_state->base.crtc_x;
10080 int y = plane_state->base.crtc_y;
cda4b7d3 10081
55a08b3f
ML
10082 if (x < 0) {
10083 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10084 x = -x;
10085 }
10086 pos |= x << CURSOR_X_SHIFT;
cda4b7d3 10087
55a08b3f
ML
10088 if (y < 0) {
10089 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10090 y = -y;
10091 }
10092 pos |= y << CURSOR_Y_SHIFT;
10093
10094 /* ILK+ do this automagically */
10095 if (HAS_GMCH_DISPLAY(dev) &&
10096 plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
10097 base += (plane_state->base.crtc_h *
10098 plane_state->base.crtc_w - 1) * 4;
10099 }
cda4b7d3 10100 }
cda4b7d3 10101
5efb3e28
VS
10102 I915_WRITE(CURPOS(pipe), pos);
10103
8ac54669 10104 if (IS_845G(dev) || IS_I865G(dev))
55a08b3f 10105 i845_update_cursor(crtc, base, plane_state);
5efb3e28 10106 else
55a08b3f 10107 i9xx_update_cursor(crtc, base, plane_state);
cda4b7d3
CW
10108}
10109
dc41c154
VS
10110static bool cursor_size_ok(struct drm_device *dev,
10111 uint32_t width, uint32_t height)
10112{
10113 if (width == 0 || height == 0)
10114 return false;
10115
10116 /*
10117 * 845g/865g are special in that they are only limited by
10118 * the width of their cursors, the height is arbitrary up to
10119 * the precision of the register. Everything else requires
10120 * square cursors, limited to a few power-of-two sizes.
10121 */
10122 if (IS_845G(dev) || IS_I865G(dev)) {
10123 if ((width & 63) != 0)
10124 return false;
10125
10126 if (width > (IS_845G(dev) ? 64 : 512))
10127 return false;
10128
10129 if (height > 1023)
10130 return false;
10131 } else {
10132 switch (width | height) {
10133 case 256:
10134 case 128:
10135 if (IS_GEN2(dev))
10136 return false;
10137 case 64:
10138 break;
10139 default:
10140 return false;
10141 }
10142 }
10143
10144 return true;
10145}
10146
79e53945
JB
10147/* VESA 640x480x72Hz mode to set on the pipe */
10148static struct drm_display_mode load_detect_mode = {
10149 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10150 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10151};
10152
a8bb6818
DV
10153struct drm_framebuffer *
10154__intel_framebuffer_create(struct drm_device *dev,
10155 struct drm_mode_fb_cmd2 *mode_cmd,
10156 struct drm_i915_gem_object *obj)
d2dff872
CW
10157{
10158 struct intel_framebuffer *intel_fb;
10159 int ret;
10160
10161 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
dcb1394e 10162 if (!intel_fb)
d2dff872 10163 return ERR_PTR(-ENOMEM);
d2dff872
CW
10164
10165 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
dd4916c5
DV
10166 if (ret)
10167 goto err;
d2dff872
CW
10168
10169 return &intel_fb->base;
dcb1394e 10170
dd4916c5 10171err:
dd4916c5 10172 kfree(intel_fb);
dd4916c5 10173 return ERR_PTR(ret);
d2dff872
CW
10174}
10175
b5ea642a 10176static struct drm_framebuffer *
a8bb6818
DV
10177intel_framebuffer_create(struct drm_device *dev,
10178 struct drm_mode_fb_cmd2 *mode_cmd,
10179 struct drm_i915_gem_object *obj)
10180{
10181 struct drm_framebuffer *fb;
10182 int ret;
10183
10184 ret = i915_mutex_lock_interruptible(dev);
10185 if (ret)
10186 return ERR_PTR(ret);
10187 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10188 mutex_unlock(&dev->struct_mutex);
10189
10190 return fb;
10191}
10192
d2dff872
CW
10193static u32
10194intel_framebuffer_pitch_for_width(int width, int bpp)
10195{
10196 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10197 return ALIGN(pitch, 64);
10198}
10199
10200static u32
10201intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10202{
10203 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
1267a26b 10204 return PAGE_ALIGN(pitch * mode->vdisplay);
d2dff872
CW
10205}
10206
10207static struct drm_framebuffer *
10208intel_framebuffer_create_for_mode(struct drm_device *dev,
10209 struct drm_display_mode *mode,
10210 int depth, int bpp)
10211{
dcb1394e 10212 struct drm_framebuffer *fb;
d2dff872 10213 struct drm_i915_gem_object *obj;
0fed39bd 10214 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872
CW
10215
10216 obj = i915_gem_alloc_object(dev,
10217 intel_framebuffer_size_for_mode(mode, bpp));
10218 if (obj == NULL)
10219 return ERR_PTR(-ENOMEM);
10220
10221 mode_cmd.width = mode->hdisplay;
10222 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
10223 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10224 bpp);
5ca0c34a 10225 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872 10226
dcb1394e
LW
10227 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
10228 if (IS_ERR(fb))
10229 drm_gem_object_unreference_unlocked(&obj->base);
10230
10231 return fb;
d2dff872
CW
10232}
10233
10234static struct drm_framebuffer *
10235mode_fits_in_fbdev(struct drm_device *dev,
10236 struct drm_display_mode *mode)
10237{
0695726e 10238#ifdef CONFIG_DRM_FBDEV_EMULATION
d2dff872
CW
10239 struct drm_i915_private *dev_priv = dev->dev_private;
10240 struct drm_i915_gem_object *obj;
10241 struct drm_framebuffer *fb;
10242
4c0e5528 10243 if (!dev_priv->fbdev)
d2dff872
CW
10244 return NULL;
10245
4c0e5528 10246 if (!dev_priv->fbdev->fb)
d2dff872
CW
10247 return NULL;
10248
4c0e5528
DV
10249 obj = dev_priv->fbdev->fb->obj;
10250 BUG_ON(!obj);
10251
8bcd4553 10252 fb = &dev_priv->fbdev->fb->base;
01f2c773
VS
10253 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10254 fb->bits_per_pixel))
d2dff872
CW
10255 return NULL;
10256
01f2c773 10257 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
10258 return NULL;
10259
edde3617 10260 drm_framebuffer_reference(fb);
d2dff872 10261 return fb;
4520f53a
DV
10262#else
10263 return NULL;
10264#endif
d2dff872
CW
10265}
10266
d3a40d1b
ACO
10267static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10268 struct drm_crtc *crtc,
10269 struct drm_display_mode *mode,
10270 struct drm_framebuffer *fb,
10271 int x, int y)
10272{
10273 struct drm_plane_state *plane_state;
10274 int hdisplay, vdisplay;
10275 int ret;
10276
10277 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10278 if (IS_ERR(plane_state))
10279 return PTR_ERR(plane_state);
10280
10281 if (mode)
10282 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10283 else
10284 hdisplay = vdisplay = 0;
10285
10286 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10287 if (ret)
10288 return ret;
10289 drm_atomic_set_fb_for_plane(plane_state, fb);
10290 plane_state->crtc_x = 0;
10291 plane_state->crtc_y = 0;
10292 plane_state->crtc_w = hdisplay;
10293 plane_state->crtc_h = vdisplay;
10294 plane_state->src_x = x << 16;
10295 plane_state->src_y = y << 16;
10296 plane_state->src_w = hdisplay << 16;
10297 plane_state->src_h = vdisplay << 16;
10298
10299 return 0;
10300}
10301
d2434ab7 10302bool intel_get_load_detect_pipe(struct drm_connector *connector,
7173188d 10303 struct drm_display_mode *mode,
51fd371b
RC
10304 struct intel_load_detect_pipe *old,
10305 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
10306{
10307 struct intel_crtc *intel_crtc;
d2434ab7
DV
10308 struct intel_encoder *intel_encoder =
10309 intel_attached_encoder(connector);
79e53945 10310 struct drm_crtc *possible_crtc;
4ef69c7a 10311 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
10312 struct drm_crtc *crtc = NULL;
10313 struct drm_device *dev = encoder->dev;
94352cf9 10314 struct drm_framebuffer *fb;
51fd371b 10315 struct drm_mode_config *config = &dev->mode_config;
edde3617 10316 struct drm_atomic_state *state = NULL, *restore_state = NULL;
944b0c76 10317 struct drm_connector_state *connector_state;
4be07317 10318 struct intel_crtc_state *crtc_state;
51fd371b 10319 int ret, i = -1;
79e53945 10320
d2dff872 10321 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 10322 connector->base.id, connector->name,
8e329a03 10323 encoder->base.id, encoder->name);
d2dff872 10324
edde3617
ML
10325 old->restore_state = NULL;
10326
51fd371b
RC
10327retry:
10328 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10329 if (ret)
ad3c558f 10330 goto fail;
6e9f798d 10331
79e53945
JB
10332 /*
10333 * Algorithm gets a little messy:
7a5e4805 10334 *
79e53945
JB
10335 * - if the connector already has an assigned crtc, use it (but make
10336 * sure it's on first)
7a5e4805 10337 *
79e53945
JB
10338 * - try to find the first unused crtc that can drive this connector,
10339 * and use that if we find one
79e53945
JB
10340 */
10341
10342 /* See if we already have a CRTC for this connector */
edde3617
ML
10343 if (connector->state->crtc) {
10344 crtc = connector->state->crtc;
8261b191 10345
51fd371b 10346 ret = drm_modeset_lock(&crtc->mutex, ctx);
4d02e2de 10347 if (ret)
ad3c558f 10348 goto fail;
8261b191
CW
10349
10350 /* Make sure the crtc and connector are running */
edde3617 10351 goto found;
79e53945
JB
10352 }
10353
10354 /* Find an unused one (if possible) */
70e1e0ec 10355 for_each_crtc(dev, possible_crtc) {
79e53945
JB
10356 i++;
10357 if (!(encoder->possible_crtcs & (1 << i)))
10358 continue;
edde3617
ML
10359
10360 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
10361 if (ret)
10362 goto fail;
10363
10364 if (possible_crtc->state->enable) {
10365 drm_modeset_unlock(&possible_crtc->mutex);
a459249c 10366 continue;
edde3617 10367 }
a459249c
VS
10368
10369 crtc = possible_crtc;
10370 break;
79e53945
JB
10371 }
10372
10373 /*
10374 * If we didn't find an unused CRTC, don't use any.
10375 */
10376 if (!crtc) {
7173188d 10377 DRM_DEBUG_KMS("no pipe available for load-detect\n");
ad3c558f 10378 goto fail;
79e53945
JB
10379 }
10380
edde3617
ML
10381found:
10382 intel_crtc = to_intel_crtc(crtc);
10383
4d02e2de
DV
10384 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10385 if (ret)
ad3c558f 10386 goto fail;
79e53945 10387
83a57153 10388 state = drm_atomic_state_alloc(dev);
edde3617
ML
10389 restore_state = drm_atomic_state_alloc(dev);
10390 if (!state || !restore_state) {
10391 ret = -ENOMEM;
10392 goto fail;
10393 }
83a57153
ACO
10394
10395 state->acquire_ctx = ctx;
edde3617 10396 restore_state->acquire_ctx = ctx;
83a57153 10397
944b0c76
ACO
10398 connector_state = drm_atomic_get_connector_state(state, connector);
10399 if (IS_ERR(connector_state)) {
10400 ret = PTR_ERR(connector_state);
10401 goto fail;
10402 }
10403
edde3617
ML
10404 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
10405 if (ret)
10406 goto fail;
944b0c76 10407
4be07317
ACO
10408 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10409 if (IS_ERR(crtc_state)) {
10410 ret = PTR_ERR(crtc_state);
10411 goto fail;
10412 }
10413
49d6fa21 10414 crtc_state->base.active = crtc_state->base.enable = true;
4be07317 10415
6492711d
CW
10416 if (!mode)
10417 mode = &load_detect_mode;
79e53945 10418
d2dff872
CW
10419 /* We need a framebuffer large enough to accommodate all accesses
10420 * that the plane may generate whilst we perform load detection.
10421 * We can not rely on the fbcon either being present (we get called
10422 * during its initialisation to detect all boot displays, or it may
10423 * not even exist) or that it is large enough to satisfy the
10424 * requested mode.
10425 */
94352cf9
DV
10426 fb = mode_fits_in_fbdev(dev, mode);
10427 if (fb == NULL) {
d2dff872 10428 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9 10429 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
d2dff872
CW
10430 } else
10431 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 10432 if (IS_ERR(fb)) {
d2dff872 10433 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
412b61d8 10434 goto fail;
79e53945 10435 }
79e53945 10436
d3a40d1b
ACO
10437 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10438 if (ret)
10439 goto fail;
10440
edde3617
ML
10441 drm_framebuffer_unreference(fb);
10442
10443 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
10444 if (ret)
10445 goto fail;
10446
10447 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
10448 if (!ret)
10449 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
10450 if (!ret)
10451 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
10452 if (ret) {
10453 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
10454 goto fail;
10455 }
8c7b5ccb 10456
3ba86073
ML
10457 ret = drm_atomic_commit(state);
10458 if (ret) {
6492711d 10459 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
412b61d8 10460 goto fail;
79e53945 10461 }
edde3617
ML
10462
10463 old->restore_state = restore_state;
7173188d 10464
79e53945 10465 /* let the connector get through one full cycle before testing */
9d0498a2 10466 intel_wait_for_vblank(dev, intel_crtc->pipe);
7173188d 10467 return true;
412b61d8 10468
ad3c558f 10469fail:
e5d958ef 10470 drm_atomic_state_free(state);
edde3617
ML
10471 drm_atomic_state_free(restore_state);
10472 restore_state = state = NULL;
83a57153 10473
51fd371b
RC
10474 if (ret == -EDEADLK) {
10475 drm_modeset_backoff(ctx);
10476 goto retry;
10477 }
10478
412b61d8 10479 return false;
79e53945
JB
10480}
10481
d2434ab7 10482void intel_release_load_detect_pipe(struct drm_connector *connector,
49172fee
ACO
10483 struct intel_load_detect_pipe *old,
10484 struct drm_modeset_acquire_ctx *ctx)
79e53945 10485{
d2434ab7
DV
10486 struct intel_encoder *intel_encoder =
10487 intel_attached_encoder(connector);
4ef69c7a 10488 struct drm_encoder *encoder = &intel_encoder->base;
edde3617 10489 struct drm_atomic_state *state = old->restore_state;
d3a40d1b 10490 int ret;
79e53945 10491
d2dff872 10492 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 10493 connector->base.id, connector->name,
8e329a03 10494 encoder->base.id, encoder->name);
d2dff872 10495
edde3617 10496 if (!state)
0622a53c 10497 return;
79e53945 10498
edde3617
ML
10499 ret = drm_atomic_commit(state);
10500 if (ret) {
10501 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
10502 drm_atomic_state_free(state);
10503 }
79e53945
JB
10504}
10505
da4a1efa 10506static int i9xx_pll_refclk(struct drm_device *dev,
5cec258b 10507 const struct intel_crtc_state *pipe_config)
da4a1efa
VS
10508{
10509 struct drm_i915_private *dev_priv = dev->dev_private;
10510 u32 dpll = pipe_config->dpll_hw_state.dpll;
10511
10512 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 10513 return dev_priv->vbt.lvds_ssc_freq;
da4a1efa
VS
10514 else if (HAS_PCH_SPLIT(dev))
10515 return 120000;
10516 else if (!IS_GEN2(dev))
10517 return 96000;
10518 else
10519 return 48000;
10520}
10521
79e53945 10522/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc 10523static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 10524 struct intel_crtc_state *pipe_config)
79e53945 10525{
f1f644dc 10526 struct drm_device *dev = crtc->base.dev;
79e53945 10527 struct drm_i915_private *dev_priv = dev->dev_private;
f1f644dc 10528 int pipe = pipe_config->cpu_transcoder;
293623f7 10529 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945
JB
10530 u32 fp;
10531 intel_clock_t clock;
dccbea3b 10532 int port_clock;
da4a1efa 10533 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
10534
10535 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 10536 fp = pipe_config->dpll_hw_state.fp0;
79e53945 10537 else
293623f7 10538 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
10539
10540 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
f2b115e6
AJ
10541 if (IS_PINEVIEW(dev)) {
10542 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10543 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
10544 } else {
10545 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10546 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10547 }
10548
a6c45cf0 10549 if (!IS_GEN2(dev)) {
f2b115e6
AJ
10550 if (IS_PINEVIEW(dev))
10551 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10552 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
10553 else
10554 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
10555 DPLL_FPA01_P1_POST_DIV_SHIFT);
10556
10557 switch (dpll & DPLL_MODE_MASK) {
10558 case DPLLB_MODE_DAC_SERIAL:
10559 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10560 5 : 10;
10561 break;
10562 case DPLLB_MODE_LVDS:
10563 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10564 7 : 14;
10565 break;
10566 default:
28c97730 10567 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 10568 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 10569 return;
79e53945
JB
10570 }
10571
ac58c3f0 10572 if (IS_PINEVIEW(dev))
dccbea3b 10573 port_clock = pnv_calc_dpll_params(refclk, &clock);
ac58c3f0 10574 else
dccbea3b 10575 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945 10576 } else {
0fb58223 10577 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
b1c560d1 10578 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
10579
10580 if (is_lvds) {
10581 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10582 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
10583
10584 if (lvds & LVDS_CLKB_POWER_UP)
10585 clock.p2 = 7;
10586 else
10587 clock.p2 = 14;
79e53945
JB
10588 } else {
10589 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10590 clock.p1 = 2;
10591 else {
10592 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10593 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10594 }
10595 if (dpll & PLL_P2_DIVIDE_BY_4)
10596 clock.p2 = 4;
10597 else
10598 clock.p2 = 2;
79e53945 10599 }
da4a1efa 10600
dccbea3b 10601 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945
JB
10602 }
10603
18442d08
VS
10604 /*
10605 * This value includes pixel_multiplier. We will use
241bfc38 10606 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
10607 * encoder's get_config() function.
10608 */
dccbea3b 10609 pipe_config->port_clock = port_clock;
f1f644dc
JB
10610}
10611
6878da05
VS
10612int intel_dotclock_calculate(int link_freq,
10613 const struct intel_link_m_n *m_n)
f1f644dc 10614{
f1f644dc
JB
10615 /*
10616 * The calculation for the data clock is:
1041a02f 10617 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 10618 * But we want to avoid losing precison if possible, so:
1041a02f 10619 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
10620 *
10621 * and the link clock is simpler:
1041a02f 10622 * link_clock = (m * link_clock) / n
f1f644dc
JB
10623 */
10624
6878da05
VS
10625 if (!m_n->link_n)
10626 return 0;
f1f644dc 10627
6878da05
VS
10628 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10629}
f1f644dc 10630
18442d08 10631static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 10632 struct intel_crtc_state *pipe_config)
6878da05 10633{
e3b247da 10634 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
79e53945 10635
18442d08
VS
10636 /* read out port_clock from the DPLL */
10637 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 10638
f1f644dc 10639 /*
e3b247da
VS
10640 * In case there is an active pipe without active ports,
10641 * we may need some idea for the dotclock anyway.
10642 * Calculate one based on the FDI configuration.
79e53945 10643 */
2d112de7 10644 pipe_config->base.adjusted_mode.crtc_clock =
21a727b3 10645 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
18442d08 10646 &pipe_config->fdi_m_n);
79e53945
JB
10647}
10648
10649/** Returns the currently programmed mode of the given pipe. */
10650struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10651 struct drm_crtc *crtc)
10652{
548f245b 10653 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 10654 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 10655 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
79e53945 10656 struct drm_display_mode *mode;
3f36b937 10657 struct intel_crtc_state *pipe_config;
fe2b8f9d
PZ
10658 int htot = I915_READ(HTOTAL(cpu_transcoder));
10659 int hsync = I915_READ(HSYNC(cpu_transcoder));
10660 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10661 int vsync = I915_READ(VSYNC(cpu_transcoder));
293623f7 10662 enum pipe pipe = intel_crtc->pipe;
79e53945
JB
10663
10664 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10665 if (!mode)
10666 return NULL;
10667
3f36b937
TU
10668 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10669 if (!pipe_config) {
10670 kfree(mode);
10671 return NULL;
10672 }
10673
f1f644dc
JB
10674 /*
10675 * Construct a pipe_config sufficient for getting the clock info
10676 * back out of crtc_clock_get.
10677 *
10678 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10679 * to use a real value here instead.
10680 */
3f36b937
TU
10681 pipe_config->cpu_transcoder = (enum transcoder) pipe;
10682 pipe_config->pixel_multiplier = 1;
10683 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10684 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10685 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
10686 i9xx_crtc_clock_get(intel_crtc, pipe_config);
10687
10688 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
79e53945
JB
10689 mode->hdisplay = (htot & 0xffff) + 1;
10690 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10691 mode->hsync_start = (hsync & 0xffff) + 1;
10692 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10693 mode->vdisplay = (vtot & 0xffff) + 1;
10694 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10695 mode->vsync_start = (vsync & 0xffff) + 1;
10696 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10697
10698 drm_mode_set_name(mode);
79e53945 10699
3f36b937
TU
10700 kfree(pipe_config);
10701
79e53945
JB
10702 return mode;
10703}
10704
f047e395
CW
10705void intel_mark_busy(struct drm_device *dev)
10706{
c67a470b
PZ
10707 struct drm_i915_private *dev_priv = dev->dev_private;
10708
f62a0076
CW
10709 if (dev_priv->mm.busy)
10710 return;
10711
43694d69 10712 intel_runtime_pm_get(dev_priv);
c67a470b 10713 i915_update_gfx_val(dev_priv);
43cf3bf0
CW
10714 if (INTEL_INFO(dev)->gen >= 6)
10715 gen6_rps_busy(dev_priv);
f62a0076 10716 dev_priv->mm.busy = true;
f047e395
CW
10717}
10718
10719void intel_mark_idle(struct drm_device *dev)
652c393a 10720{
c67a470b 10721 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 10722
f62a0076
CW
10723 if (!dev_priv->mm.busy)
10724 return;
10725
10726 dev_priv->mm.busy = false;
10727
3d13ef2e 10728 if (INTEL_INFO(dev)->gen >= 6)
b29c19b6 10729 gen6_rps_idle(dev->dev_private);
bb4cdd53 10730
43694d69 10731 intel_runtime_pm_put(dev_priv);
652c393a
JB
10732}
10733
79e53945
JB
10734static void intel_crtc_destroy(struct drm_crtc *crtc)
10735{
10736 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
67e77c5a
DV
10737 struct drm_device *dev = crtc->dev;
10738 struct intel_unpin_work *work;
67e77c5a 10739
5e2d7afc 10740 spin_lock_irq(&dev->event_lock);
67e77c5a
DV
10741 work = intel_crtc->unpin_work;
10742 intel_crtc->unpin_work = NULL;
5e2d7afc 10743 spin_unlock_irq(&dev->event_lock);
67e77c5a
DV
10744
10745 if (work) {
10746 cancel_work_sync(&work->work);
10747 kfree(work);
10748 }
79e53945
JB
10749
10750 drm_crtc_cleanup(crtc);
67e77c5a 10751
79e53945
JB
10752 kfree(intel_crtc);
10753}
10754
6b95a207
KH
10755static void intel_unpin_work_fn(struct work_struct *__work)
10756{
10757 struct intel_unpin_work *work =
10758 container_of(__work, struct intel_unpin_work, work);
a9ff8714
VS
10759 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10760 struct drm_device *dev = crtc->base.dev;
10761 struct drm_plane *primary = crtc->base.primary;
6b95a207 10762
b4a98e57 10763 mutex_lock(&dev->struct_mutex);
3465c580 10764 intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
05394f39 10765 drm_gem_object_unreference(&work->pending_flip_obj->base);
d9e86c0e 10766
f06cc1b9 10767 if (work->flip_queued_req)
146d84f0 10768 i915_gem_request_assign(&work->flip_queued_req, NULL);
b4a98e57
CW
10769 mutex_unlock(&dev->struct_mutex);
10770
a9ff8714 10771 intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
1eb52238 10772 intel_fbc_post_update(crtc);
89ed88ba 10773 drm_framebuffer_unreference(work->old_fb);
f99d7069 10774
a9ff8714
VS
10775 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
10776 atomic_dec(&crtc->unpin_work_count);
b4a98e57 10777
6b95a207
KH
10778 kfree(work);
10779}
10780
1afe3e9d 10781static void do_intel_finish_page_flip(struct drm_device *dev,
49b14a5c 10782 struct drm_crtc *crtc)
6b95a207 10783{
6b95a207
KH
10784 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10785 struct intel_unpin_work *work;
6b95a207
KH
10786 unsigned long flags;
10787
10788 /* Ignore early vblank irqs */
10789 if (intel_crtc == NULL)
10790 return;
10791
f326038a
DV
10792 /*
10793 * This is called both by irq handlers and the reset code (to complete
10794 * lost pageflips) so needs the full irqsave spinlocks.
10795 */
6b95a207
KH
10796 spin_lock_irqsave(&dev->event_lock, flags);
10797 work = intel_crtc->unpin_work;
e7d841ca
CW
10798
10799 /* Ensure we don't miss a work->pending update ... */
10800 smp_rmb();
10801
10802 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
6b95a207
KH
10803 spin_unlock_irqrestore(&dev->event_lock, flags);
10804 return;
10805 }
10806
d6bbafa1 10807 page_flip_completed(intel_crtc);
0af7e4df 10808
6b95a207 10809 spin_unlock_irqrestore(&dev->event_lock, flags);
6b95a207
KH
10810}
10811
1afe3e9d
JB
10812void intel_finish_page_flip(struct drm_device *dev, int pipe)
10813{
fbee40df 10814 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
10815 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10816
49b14a5c 10817 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
10818}
10819
10820void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10821{
fbee40df 10822 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
10823 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10824
49b14a5c 10825 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
10826}
10827
75f7f3ec
VS
10828/* Is 'a' after or equal to 'b'? */
10829static bool g4x_flip_count_after_eq(u32 a, u32 b)
10830{
10831 return !((a - b) & 0x80000000);
10832}
10833
10834static bool page_flip_finished(struct intel_crtc *crtc)
10835{
10836 struct drm_device *dev = crtc->base.dev;
10837 struct drm_i915_private *dev_priv = dev->dev_private;
10838
bdfa7542
VS
10839 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10840 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10841 return true;
10842
75f7f3ec
VS
10843 /*
10844 * The relevant registers doen't exist on pre-ctg.
10845 * As the flip done interrupt doesn't trigger for mmio
10846 * flips on gmch platforms, a flip count check isn't
10847 * really needed there. But since ctg has the registers,
10848 * include it in the check anyway.
10849 */
10850 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10851 return true;
10852
e8861675
ML
10853 /*
10854 * BDW signals flip done immediately if the plane
10855 * is disabled, even if the plane enable is already
10856 * armed to occur at the next vblank :(
10857 */
10858
75f7f3ec
VS
10859 /*
10860 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10861 * used the same base address. In that case the mmio flip might
10862 * have completed, but the CS hasn't even executed the flip yet.
10863 *
10864 * A flip count check isn't enough as the CS might have updated
10865 * the base address just after start of vblank, but before we
10866 * managed to process the interrupt. This means we'd complete the
10867 * CS flip too soon.
10868 *
10869 * Combining both checks should get us a good enough result. It may
10870 * still happen that the CS flip has been executed, but has not
10871 * yet actually completed. But in case the base address is the same
10872 * anyway, we don't really care.
10873 */
10874 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10875 crtc->unpin_work->gtt_offset &&
fd8f507c 10876 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
75f7f3ec
VS
10877 crtc->unpin_work->flip_count);
10878}
10879
6b95a207
KH
10880void intel_prepare_page_flip(struct drm_device *dev, int plane)
10881{
fbee40df 10882 struct drm_i915_private *dev_priv = dev->dev_private;
6b95a207
KH
10883 struct intel_crtc *intel_crtc =
10884 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10885 unsigned long flags;
10886
f326038a
DV
10887
10888 /*
10889 * This is called both by irq handlers and the reset code (to complete
10890 * lost pageflips) so needs the full irqsave spinlocks.
10891 *
10892 * NB: An MMIO update of the plane base pointer will also
e7d841ca
CW
10893 * generate a page-flip completion irq, i.e. every modeset
10894 * is also accompanied by a spurious intel_prepare_page_flip().
10895 */
6b95a207 10896 spin_lock_irqsave(&dev->event_lock, flags);
75f7f3ec 10897 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
e7d841ca 10898 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
6b95a207
KH
10899 spin_unlock_irqrestore(&dev->event_lock, flags);
10900}
10901
6042639c 10902static inline void intel_mark_page_flip_active(struct intel_unpin_work *work)
e7d841ca
CW
10903{
10904 /* Ensure that the work item is consistent when activating it ... */
10905 smp_wmb();
6042639c 10906 atomic_set(&work->pending, INTEL_FLIP_PENDING);
e7d841ca
CW
10907 /* and that it is marked active as soon as the irq could fire. */
10908 smp_wmb();
10909}
10910
8c9f3aaf
JB
10911static int intel_gen2_queue_flip(struct drm_device *dev,
10912 struct drm_crtc *crtc,
10913 struct drm_framebuffer *fb,
ed8d1975 10914 struct drm_i915_gem_object *obj,
6258fbe2 10915 struct drm_i915_gem_request *req,
ed8d1975 10916 uint32_t flags)
8c9f3aaf 10917{
4a570db5 10918 struct intel_engine_cs *engine = req->engine;
8c9f3aaf 10919 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
10920 u32 flip_mask;
10921 int ret;
10922
5fb9de1a 10923 ret = intel_ring_begin(req, 6);
8c9f3aaf 10924 if (ret)
4fa62c89 10925 return ret;
8c9f3aaf
JB
10926
10927 /* Can't queue multiple flips, so wait for the previous
10928 * one to finish before executing the next.
10929 */
10930 if (intel_crtc->plane)
10931 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10932 else
10933 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
e2f80391
TU
10934 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
10935 intel_ring_emit(engine, MI_NOOP);
10936 intel_ring_emit(engine, MI_DISPLAY_FLIP |
6d90c952 10937 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
e2f80391
TU
10938 intel_ring_emit(engine, fb->pitches[0]);
10939 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
10940 intel_ring_emit(engine, 0); /* aux display base address, unused */
e7d841ca 10941
6042639c 10942 intel_mark_page_flip_active(intel_crtc->unpin_work);
83d4092b 10943 return 0;
8c9f3aaf
JB
10944}
10945
10946static int intel_gen3_queue_flip(struct drm_device *dev,
10947 struct drm_crtc *crtc,
10948 struct drm_framebuffer *fb,
ed8d1975 10949 struct drm_i915_gem_object *obj,
6258fbe2 10950 struct drm_i915_gem_request *req,
ed8d1975 10951 uint32_t flags)
8c9f3aaf 10952{
4a570db5 10953 struct intel_engine_cs *engine = req->engine;
8c9f3aaf 10954 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
10955 u32 flip_mask;
10956 int ret;
10957
5fb9de1a 10958 ret = intel_ring_begin(req, 6);
8c9f3aaf 10959 if (ret)
4fa62c89 10960 return ret;
8c9f3aaf
JB
10961
10962 if (intel_crtc->plane)
10963 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10964 else
10965 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
e2f80391
TU
10966 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
10967 intel_ring_emit(engine, MI_NOOP);
10968 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 |
6d90c952 10969 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
e2f80391
TU
10970 intel_ring_emit(engine, fb->pitches[0]);
10971 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
10972 intel_ring_emit(engine, MI_NOOP);
6d90c952 10973
6042639c 10974 intel_mark_page_flip_active(intel_crtc->unpin_work);
83d4092b 10975 return 0;
8c9f3aaf
JB
10976}
10977
10978static int intel_gen4_queue_flip(struct drm_device *dev,
10979 struct drm_crtc *crtc,
10980 struct drm_framebuffer *fb,
ed8d1975 10981 struct drm_i915_gem_object *obj,
6258fbe2 10982 struct drm_i915_gem_request *req,
ed8d1975 10983 uint32_t flags)
8c9f3aaf 10984{
4a570db5 10985 struct intel_engine_cs *engine = req->engine;
8c9f3aaf
JB
10986 struct drm_i915_private *dev_priv = dev->dev_private;
10987 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10988 uint32_t pf, pipesrc;
10989 int ret;
10990
5fb9de1a 10991 ret = intel_ring_begin(req, 4);
8c9f3aaf 10992 if (ret)
4fa62c89 10993 return ret;
8c9f3aaf
JB
10994
10995 /* i965+ uses the linear or tiled offsets from the
10996 * Display Registers (which do not change across a page-flip)
10997 * so we need only reprogram the base address.
10998 */
e2f80391 10999 intel_ring_emit(engine, MI_DISPLAY_FLIP |
6d90c952 11000 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
e2f80391
TU
11001 intel_ring_emit(engine, fb->pitches[0]);
11002 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset |
c2c75131 11003 obj->tiling_mode);
8c9f3aaf
JB
11004
11005 /* XXX Enabling the panel-fitter across page-flip is so far
11006 * untested on non-native modes, so ignore it for now.
11007 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11008 */
11009 pf = 0;
11010 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
e2f80391 11011 intel_ring_emit(engine, pf | pipesrc);
e7d841ca 11012
6042639c 11013 intel_mark_page_flip_active(intel_crtc->unpin_work);
83d4092b 11014 return 0;
8c9f3aaf
JB
11015}
11016
11017static int intel_gen6_queue_flip(struct drm_device *dev,
11018 struct drm_crtc *crtc,
11019 struct drm_framebuffer *fb,
ed8d1975 11020 struct drm_i915_gem_object *obj,
6258fbe2 11021 struct drm_i915_gem_request *req,
ed8d1975 11022 uint32_t flags)
8c9f3aaf 11023{
4a570db5 11024 struct intel_engine_cs *engine = req->engine;
8c9f3aaf
JB
11025 struct drm_i915_private *dev_priv = dev->dev_private;
11026 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11027 uint32_t pf, pipesrc;
11028 int ret;
11029
5fb9de1a 11030 ret = intel_ring_begin(req, 4);
8c9f3aaf 11031 if (ret)
4fa62c89 11032 return ret;
8c9f3aaf 11033
e2f80391 11034 intel_ring_emit(engine, MI_DISPLAY_FLIP |
6d90c952 11035 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
e2f80391
TU
11036 intel_ring_emit(engine, fb->pitches[0] | obj->tiling_mode);
11037 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
8c9f3aaf 11038
dc257cf1
DV
11039 /* Contrary to the suggestions in the documentation,
11040 * "Enable Panel Fitter" does not seem to be required when page
11041 * flipping with a non-native mode, and worse causes a normal
11042 * modeset to fail.
11043 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11044 */
11045 pf = 0;
8c9f3aaf 11046 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
e2f80391 11047 intel_ring_emit(engine, pf | pipesrc);
e7d841ca 11048
6042639c 11049 intel_mark_page_flip_active(intel_crtc->unpin_work);
83d4092b 11050 return 0;
8c9f3aaf
JB
11051}
11052
7c9017e5
JB
11053static int intel_gen7_queue_flip(struct drm_device *dev,
11054 struct drm_crtc *crtc,
11055 struct drm_framebuffer *fb,
ed8d1975 11056 struct drm_i915_gem_object *obj,
6258fbe2 11057 struct drm_i915_gem_request *req,
ed8d1975 11058 uint32_t flags)
7c9017e5 11059{
4a570db5 11060 struct intel_engine_cs *engine = req->engine;
7c9017e5 11061 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cb05d8de 11062 uint32_t plane_bit = 0;
ffe74d75
CW
11063 int len, ret;
11064
eba905b2 11065 switch (intel_crtc->plane) {
cb05d8de
DV
11066 case PLANE_A:
11067 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11068 break;
11069 case PLANE_B:
11070 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11071 break;
11072 case PLANE_C:
11073 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11074 break;
11075 default:
11076 WARN_ONCE(1, "unknown plane in flip command\n");
4fa62c89 11077 return -ENODEV;
cb05d8de
DV
11078 }
11079
ffe74d75 11080 len = 4;
e2f80391 11081 if (engine->id == RCS) {
ffe74d75 11082 len += 6;
f476828a
DL
11083 /*
11084 * On Gen 8, SRM is now taking an extra dword to accommodate
11085 * 48bits addresses, and we need a NOOP for the batch size to
11086 * stay even.
11087 */
11088 if (IS_GEN8(dev))
11089 len += 2;
11090 }
ffe74d75 11091
f66fab8e
VS
11092 /*
11093 * BSpec MI_DISPLAY_FLIP for IVB:
11094 * "The full packet must be contained within the same cache line."
11095 *
11096 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11097 * cacheline, if we ever start emitting more commands before
11098 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11099 * then do the cacheline alignment, and finally emit the
11100 * MI_DISPLAY_FLIP.
11101 */
bba09b12 11102 ret = intel_ring_cacheline_align(req);
f66fab8e 11103 if (ret)
4fa62c89 11104 return ret;
f66fab8e 11105
5fb9de1a 11106 ret = intel_ring_begin(req, len);
7c9017e5 11107 if (ret)
4fa62c89 11108 return ret;
7c9017e5 11109
ffe74d75
CW
11110 /* Unmask the flip-done completion message. Note that the bspec says that
11111 * we should do this for both the BCS and RCS, and that we must not unmask
11112 * more than one flip event at any time (or ensure that one flip message
11113 * can be sent by waiting for flip-done prior to queueing new flips).
11114 * Experimentation says that BCS works despite DERRMR masking all
11115 * flip-done completion events and that unmasking all planes at once
11116 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11117 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11118 */
e2f80391
TU
11119 if (engine->id == RCS) {
11120 intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(1));
11121 intel_ring_emit_reg(engine, DERRMR);
11122 intel_ring_emit(engine, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11123 DERRMR_PIPEB_PRI_FLIP_DONE |
11124 DERRMR_PIPEC_PRI_FLIP_DONE));
f476828a 11125 if (IS_GEN8(dev))
e2f80391 11126 intel_ring_emit(engine, MI_STORE_REGISTER_MEM_GEN8 |
f476828a
DL
11127 MI_SRM_LRM_GLOBAL_GTT);
11128 else
e2f80391 11129 intel_ring_emit(engine, MI_STORE_REGISTER_MEM |
f476828a 11130 MI_SRM_LRM_GLOBAL_GTT);
e2f80391
TU
11131 intel_ring_emit_reg(engine, DERRMR);
11132 intel_ring_emit(engine, engine->scratch.gtt_offset + 256);
f476828a 11133 if (IS_GEN8(dev)) {
e2f80391
TU
11134 intel_ring_emit(engine, 0);
11135 intel_ring_emit(engine, MI_NOOP);
f476828a 11136 }
ffe74d75
CW
11137 }
11138
e2f80391
TU
11139 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 | plane_bit);
11140 intel_ring_emit(engine, (fb->pitches[0] | obj->tiling_mode));
11141 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
11142 intel_ring_emit(engine, (MI_NOOP));
e7d841ca 11143
6042639c 11144 intel_mark_page_flip_active(intel_crtc->unpin_work);
83d4092b 11145 return 0;
7c9017e5
JB
11146}
11147
0bc40be8 11148static bool use_mmio_flip(struct intel_engine_cs *engine,
84c33a64
SG
11149 struct drm_i915_gem_object *obj)
11150{
11151 /*
11152 * This is not being used for older platforms, because
11153 * non-availability of flip done interrupt forces us to use
11154 * CS flips. Older platforms derive flip done using some clever
11155 * tricks involving the flip_pending status bits and vblank irqs.
11156 * So using MMIO flips there would disrupt this mechanism.
11157 */
11158
0bc40be8 11159 if (engine == NULL)
8e09bf83
CW
11160 return true;
11161
0bc40be8 11162 if (INTEL_INFO(engine->dev)->gen < 5)
84c33a64
SG
11163 return false;
11164
11165 if (i915.use_mmio_flip < 0)
11166 return false;
11167 else if (i915.use_mmio_flip > 0)
11168 return true;
14bf993e
OM
11169 else if (i915.enable_execlists)
11170 return true;
fd8e058a
AG
11171 else if (obj->base.dma_buf &&
11172 !reservation_object_test_signaled_rcu(obj->base.dma_buf->resv,
11173 false))
11174 return true;
84c33a64 11175 else
666796da 11176 return engine != i915_gem_request_get_engine(obj->last_write_req);
84c33a64
SG
11177}
11178
6042639c 11179static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
86efe24a 11180 unsigned int rotation,
6042639c 11181 struct intel_unpin_work *work)
ff944564
DL
11182{
11183 struct drm_device *dev = intel_crtc->base.dev;
11184 struct drm_i915_private *dev_priv = dev->dev_private;
11185 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
ff944564 11186 const enum pipe pipe = intel_crtc->pipe;
86efe24a 11187 u32 ctl, stride, tile_height;
ff944564
DL
11188
11189 ctl = I915_READ(PLANE_CTL(pipe, 0));
11190 ctl &= ~PLANE_CTL_TILED_MASK;
2ebef630
TU
11191 switch (fb->modifier[0]) {
11192 case DRM_FORMAT_MOD_NONE:
11193 break;
11194 case I915_FORMAT_MOD_X_TILED:
ff944564 11195 ctl |= PLANE_CTL_TILED_X;
2ebef630
TU
11196 break;
11197 case I915_FORMAT_MOD_Y_TILED:
11198 ctl |= PLANE_CTL_TILED_Y;
11199 break;
11200 case I915_FORMAT_MOD_Yf_TILED:
11201 ctl |= PLANE_CTL_TILED_YF;
11202 break;
11203 default:
11204 MISSING_CASE(fb->modifier[0]);
11205 }
ff944564
DL
11206
11207 /*
11208 * The stride is either expressed as a multiple of 64 bytes chunks for
11209 * linear buffers or in number of tiles for tiled buffers.
11210 */
86efe24a
TU
11211 if (intel_rotation_90_or_270(rotation)) {
11212 /* stride = Surface height in tiles */
832be82f 11213 tile_height = intel_tile_height(dev_priv, fb->modifier[0], 0);
86efe24a
TU
11214 stride = DIV_ROUND_UP(fb->height, tile_height);
11215 } else {
11216 stride = fb->pitches[0] /
7b49f948
VS
11217 intel_fb_stride_alignment(dev_priv, fb->modifier[0],
11218 fb->pixel_format);
86efe24a 11219 }
ff944564
DL
11220
11221 /*
11222 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11223 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11224 */
11225 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11226 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11227
6042639c 11228 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
ff944564
DL
11229 POSTING_READ(PLANE_SURF(pipe, 0));
11230}
11231
6042639c
CW
11232static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
11233 struct intel_unpin_work *work)
84c33a64
SG
11234{
11235 struct drm_device *dev = intel_crtc->base.dev;
11236 struct drm_i915_private *dev_priv = dev->dev_private;
11237 struct intel_framebuffer *intel_fb =
11238 to_intel_framebuffer(intel_crtc->base.primary->fb);
11239 struct drm_i915_gem_object *obj = intel_fb->obj;
f0f59a00 11240 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
84c33a64 11241 u32 dspcntr;
84c33a64 11242
84c33a64
SG
11243 dspcntr = I915_READ(reg);
11244
c5d97472
DL
11245 if (obj->tiling_mode != I915_TILING_NONE)
11246 dspcntr |= DISPPLANE_TILED;
11247 else
11248 dspcntr &= ~DISPPLANE_TILED;
11249
84c33a64
SG
11250 I915_WRITE(reg, dspcntr);
11251
6042639c 11252 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
84c33a64 11253 POSTING_READ(DSPSURF(intel_crtc->plane));
ff944564
DL
11254}
11255
11256/*
11257 * XXX: This is the temporary way to update the plane registers until we get
11258 * around to using the usual plane update functions for MMIO flips
11259 */
6042639c 11260static void intel_do_mmio_flip(struct intel_mmio_flip *mmio_flip)
ff944564 11261{
6042639c
CW
11262 struct intel_crtc *crtc = mmio_flip->crtc;
11263 struct intel_unpin_work *work;
11264
11265 spin_lock_irq(&crtc->base.dev->event_lock);
11266 work = crtc->unpin_work;
11267 spin_unlock_irq(&crtc->base.dev->event_lock);
11268 if (work == NULL)
11269 return;
ff944564 11270
6042639c 11271 intel_mark_page_flip_active(work);
ff944564 11272
6042639c 11273 intel_pipe_update_start(crtc);
ff944564 11274
6042639c 11275 if (INTEL_INFO(mmio_flip->i915)->gen >= 9)
86efe24a 11276 skl_do_mmio_flip(crtc, mmio_flip->rotation, work);
ff944564
DL
11277 else
11278 /* use_mmio_flip() retricts MMIO flips to ilk+ */
6042639c 11279 ilk_do_mmio_flip(crtc, work);
ff944564 11280
6042639c 11281 intel_pipe_update_end(crtc);
84c33a64
SG
11282}
11283
9362c7c5 11284static void intel_mmio_flip_work_func(struct work_struct *work)
84c33a64 11285{
b2cfe0ab
CW
11286 struct intel_mmio_flip *mmio_flip =
11287 container_of(work, struct intel_mmio_flip, work);
fd8e058a
AG
11288 struct intel_framebuffer *intel_fb =
11289 to_intel_framebuffer(mmio_flip->crtc->base.primary->fb);
11290 struct drm_i915_gem_object *obj = intel_fb->obj;
84c33a64 11291
6042639c 11292 if (mmio_flip->req) {
eed29a5b 11293 WARN_ON(__i915_wait_request(mmio_flip->req,
b2cfe0ab 11294 mmio_flip->crtc->reset_counter,
bcafc4e3
CW
11295 false, NULL,
11296 &mmio_flip->i915->rps.mmioflips));
6042639c
CW
11297 i915_gem_request_unreference__unlocked(mmio_flip->req);
11298 }
84c33a64 11299
fd8e058a
AG
11300 /* For framebuffer backed by dmabuf, wait for fence */
11301 if (obj->base.dma_buf)
11302 WARN_ON(reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
11303 false, false,
11304 MAX_SCHEDULE_TIMEOUT) < 0);
11305
6042639c 11306 intel_do_mmio_flip(mmio_flip);
b2cfe0ab 11307 kfree(mmio_flip);
84c33a64
SG
11308}
11309
11310static int intel_queue_mmio_flip(struct drm_device *dev,
11311 struct drm_crtc *crtc,
86efe24a 11312 struct drm_i915_gem_object *obj)
84c33a64 11313{
b2cfe0ab
CW
11314 struct intel_mmio_flip *mmio_flip;
11315
11316 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
11317 if (mmio_flip == NULL)
11318 return -ENOMEM;
84c33a64 11319
bcafc4e3 11320 mmio_flip->i915 = to_i915(dev);
eed29a5b 11321 mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
b2cfe0ab 11322 mmio_flip->crtc = to_intel_crtc(crtc);
86efe24a 11323 mmio_flip->rotation = crtc->primary->state->rotation;
536f5b5e 11324
b2cfe0ab
CW
11325 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
11326 schedule_work(&mmio_flip->work);
84c33a64 11327
84c33a64
SG
11328 return 0;
11329}
11330
8c9f3aaf
JB
11331static int intel_default_queue_flip(struct drm_device *dev,
11332 struct drm_crtc *crtc,
11333 struct drm_framebuffer *fb,
ed8d1975 11334 struct drm_i915_gem_object *obj,
6258fbe2 11335 struct drm_i915_gem_request *req,
ed8d1975 11336 uint32_t flags)
8c9f3aaf
JB
11337{
11338 return -ENODEV;
11339}
11340
d6bbafa1
CW
11341static bool __intel_pageflip_stall_check(struct drm_device *dev,
11342 struct drm_crtc *crtc)
11343{
11344 struct drm_i915_private *dev_priv = dev->dev_private;
11345 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11346 struct intel_unpin_work *work = intel_crtc->unpin_work;
11347 u32 addr;
11348
11349 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
11350 return true;
11351
908565c2
CW
11352 if (atomic_read(&work->pending) < INTEL_FLIP_PENDING)
11353 return false;
11354
d6bbafa1
CW
11355 if (!work->enable_stall_check)
11356 return false;
11357
11358 if (work->flip_ready_vblank == 0) {
3a8a946e
DV
11359 if (work->flip_queued_req &&
11360 !i915_gem_request_completed(work->flip_queued_req, true))
d6bbafa1
CW
11361 return false;
11362
1e3feefd 11363 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
d6bbafa1
CW
11364 }
11365
1e3feefd 11366 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
d6bbafa1
CW
11367 return false;
11368
11369 /* Potential stall - if we see that the flip has happened,
11370 * assume a missed interrupt. */
11371 if (INTEL_INFO(dev)->gen >= 4)
11372 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11373 else
11374 addr = I915_READ(DSPADDR(intel_crtc->plane));
11375
11376 /* There is a potential issue here with a false positive after a flip
11377 * to the same address. We could address this by checking for a
11378 * non-incrementing frame counter.
11379 */
11380 return addr == work->gtt_offset;
11381}
11382
11383void intel_check_page_flip(struct drm_device *dev, int pipe)
11384{
11385 struct drm_i915_private *dev_priv = dev->dev_private;
11386 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11387 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6ad790c0 11388 struct intel_unpin_work *work;
f326038a 11389
6c51d46f 11390 WARN_ON(!in_interrupt());
d6bbafa1
CW
11391
11392 if (crtc == NULL)
11393 return;
11394
f326038a 11395 spin_lock(&dev->event_lock);
6ad790c0
CW
11396 work = intel_crtc->unpin_work;
11397 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
d6bbafa1 11398 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
6ad790c0 11399 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
d6bbafa1 11400 page_flip_completed(intel_crtc);
6ad790c0 11401 work = NULL;
d6bbafa1 11402 }
6ad790c0
CW
11403 if (work != NULL &&
11404 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
11405 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
f326038a 11406 spin_unlock(&dev->event_lock);
d6bbafa1
CW
11407}
11408
6b95a207
KH
11409static int intel_crtc_page_flip(struct drm_crtc *crtc,
11410 struct drm_framebuffer *fb,
ed8d1975
KP
11411 struct drm_pending_vblank_event *event,
11412 uint32_t page_flip_flags)
6b95a207
KH
11413{
11414 struct drm_device *dev = crtc->dev;
11415 struct drm_i915_private *dev_priv = dev->dev_private;
f4510a27 11416 struct drm_framebuffer *old_fb = crtc->primary->fb;
2ff8fde1 11417 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
6b95a207 11418 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
455a6808 11419 struct drm_plane *primary = crtc->primary;
a071fa00 11420 enum pipe pipe = intel_crtc->pipe;
6b95a207 11421 struct intel_unpin_work *work;
e2f80391 11422 struct intel_engine_cs *engine;
cf5d8a46 11423 bool mmio_flip;
91af127f 11424 struct drm_i915_gem_request *request = NULL;
52e68630 11425 int ret;
6b95a207 11426
2ff8fde1
MR
11427 /*
11428 * drm_mode_page_flip_ioctl() should already catch this, but double
11429 * check to be safe. In the future we may enable pageflipping from
11430 * a disabled primary plane.
11431 */
11432 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11433 return -EBUSY;
11434
e6a595d2 11435 /* Can't change pixel format via MI display flips. */
f4510a27 11436 if (fb->pixel_format != crtc->primary->fb->pixel_format)
e6a595d2
VS
11437 return -EINVAL;
11438
11439 /*
11440 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11441 * Note that pitch changes could also affect these register.
11442 */
11443 if (INTEL_INFO(dev)->gen > 3 &&
f4510a27
MR
11444 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11445 fb->pitches[0] != crtc->primary->fb->pitches[0]))
e6a595d2
VS
11446 return -EINVAL;
11447
f900db47
CW
11448 if (i915_terminally_wedged(&dev_priv->gpu_error))
11449 goto out_hang;
11450
b14c5679 11451 work = kzalloc(sizeof(*work), GFP_KERNEL);
6b95a207
KH
11452 if (work == NULL)
11453 return -ENOMEM;
11454
6b95a207 11455 work->event = event;
b4a98e57 11456 work->crtc = crtc;
ab8d6675 11457 work->old_fb = old_fb;
6b95a207
KH
11458 INIT_WORK(&work->work, intel_unpin_work_fn);
11459
87b6b101 11460 ret = drm_crtc_vblank_get(crtc);
7317c75e
JB
11461 if (ret)
11462 goto free_work;
11463
6b95a207 11464 /* We borrow the event spin lock for protecting unpin_work */
5e2d7afc 11465 spin_lock_irq(&dev->event_lock);
6b95a207 11466 if (intel_crtc->unpin_work) {
d6bbafa1
CW
11467 /* Before declaring the flip queue wedged, check if
11468 * the hardware completed the operation behind our backs.
11469 */
11470 if (__intel_pageflip_stall_check(dev, crtc)) {
11471 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11472 page_flip_completed(intel_crtc);
11473 } else {
11474 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
5e2d7afc 11475 spin_unlock_irq(&dev->event_lock);
468f0b44 11476
d6bbafa1
CW
11477 drm_crtc_vblank_put(crtc);
11478 kfree(work);
11479 return -EBUSY;
11480 }
6b95a207
KH
11481 }
11482 intel_crtc->unpin_work = work;
5e2d7afc 11483 spin_unlock_irq(&dev->event_lock);
6b95a207 11484
b4a98e57
CW
11485 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11486 flush_workqueue(dev_priv->wq);
11487
75dfca80 11488 /* Reference the objects for the scheduled work. */
ab8d6675 11489 drm_framebuffer_reference(work->old_fb);
05394f39 11490 drm_gem_object_reference(&obj->base);
6b95a207 11491
f4510a27 11492 crtc->primary->fb = fb;
afd65eb4 11493 update_state_fb(crtc->primary);
e8216e50 11494 intel_fbc_pre_update(intel_crtc);
1ed1f968 11495
e1f99ce6 11496 work->pending_flip_obj = obj;
e1f99ce6 11497
89ed88ba
CW
11498 ret = i915_mutex_lock_interruptible(dev);
11499 if (ret)
11500 goto cleanup;
11501
b4a98e57 11502 atomic_inc(&intel_crtc->unpin_work_count);
10d83730 11503 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
e1f99ce6 11504
75f7f3ec 11505 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
fd8f507c 11506 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
75f7f3ec 11507
666a4537 11508 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
4a570db5 11509 engine = &dev_priv->engine[BCS];
ab8d6675 11510 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
8e09bf83 11511 /* vlv: DISPLAY_FLIP fails to change tiling */
e2f80391 11512 engine = NULL;
48bf5b2d 11513 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
4a570db5 11514 engine = &dev_priv->engine[BCS];
4fa62c89 11515 } else if (INTEL_INFO(dev)->gen >= 7) {
666796da 11516 engine = i915_gem_request_get_engine(obj->last_write_req);
e2f80391 11517 if (engine == NULL || engine->id != RCS)
4a570db5 11518 engine = &dev_priv->engine[BCS];
4fa62c89 11519 } else {
4a570db5 11520 engine = &dev_priv->engine[RCS];
4fa62c89
VS
11521 }
11522
e2f80391 11523 mmio_flip = use_mmio_flip(engine, obj);
cf5d8a46
CW
11524
11525 /* When using CS flips, we want to emit semaphores between rings.
11526 * However, when using mmio flips we will create a task to do the
11527 * synchronisation, so all we want here is to pin the framebuffer
11528 * into the display plane and skip any waits.
11529 */
7580d774 11530 if (!mmio_flip) {
e2f80391 11531 ret = i915_gem_object_sync(obj, engine, &request);
7580d774
ML
11532 if (ret)
11533 goto cleanup_pending;
11534 }
11535
3465c580 11536 ret = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
8c9f3aaf
JB
11537 if (ret)
11538 goto cleanup_pending;
6b95a207 11539
dedf278c
TU
11540 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
11541 obj, 0);
11542 work->gtt_offset += intel_crtc->dspaddr_offset;
4fa62c89 11543
cf5d8a46 11544 if (mmio_flip) {
86efe24a 11545 ret = intel_queue_mmio_flip(dev, crtc, obj);
d6bbafa1
CW
11546 if (ret)
11547 goto cleanup_unpin;
11548
f06cc1b9
JH
11549 i915_gem_request_assign(&work->flip_queued_req,
11550 obj->last_write_req);
d6bbafa1 11551 } else {
6258fbe2 11552 if (!request) {
e2f80391 11553 request = i915_gem_request_alloc(engine, NULL);
26827088
DG
11554 if (IS_ERR(request)) {
11555 ret = PTR_ERR(request);
6258fbe2 11556 goto cleanup_unpin;
26827088 11557 }
6258fbe2
JH
11558 }
11559
11560 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
d6bbafa1
CW
11561 page_flip_flags);
11562 if (ret)
11563 goto cleanup_unpin;
11564
6258fbe2 11565 i915_gem_request_assign(&work->flip_queued_req, request);
d6bbafa1
CW
11566 }
11567
91af127f 11568 if (request)
75289874 11569 i915_add_request_no_flush(request);
91af127f 11570
1e3feefd 11571 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
d6bbafa1 11572 work->enable_stall_check = true;
4fa62c89 11573
ab8d6675 11574 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
a9ff8714 11575 to_intel_plane(primary)->frontbuffer_bit);
c80ac854 11576 mutex_unlock(&dev->struct_mutex);
a071fa00 11577
a9ff8714
VS
11578 intel_frontbuffer_flip_prepare(dev,
11579 to_intel_plane(primary)->frontbuffer_bit);
6b95a207 11580
e5510fac
JB
11581 trace_i915_flip_request(intel_crtc->plane, obj);
11582
6b95a207 11583 return 0;
96b099fd 11584
4fa62c89 11585cleanup_unpin:
3465c580 11586 intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
8c9f3aaf 11587cleanup_pending:
0aa498d5 11588 if (!IS_ERR_OR_NULL(request))
91af127f 11589 i915_gem_request_cancel(request);
b4a98e57 11590 atomic_dec(&intel_crtc->unpin_work_count);
89ed88ba
CW
11591 mutex_unlock(&dev->struct_mutex);
11592cleanup:
f4510a27 11593 crtc->primary->fb = old_fb;
afd65eb4 11594 update_state_fb(crtc->primary);
89ed88ba
CW
11595
11596 drm_gem_object_unreference_unlocked(&obj->base);
ab8d6675 11597 drm_framebuffer_unreference(work->old_fb);
96b099fd 11598
5e2d7afc 11599 spin_lock_irq(&dev->event_lock);
96b099fd 11600 intel_crtc->unpin_work = NULL;
5e2d7afc 11601 spin_unlock_irq(&dev->event_lock);
96b099fd 11602
87b6b101 11603 drm_crtc_vblank_put(crtc);
7317c75e 11604free_work:
96b099fd
CW
11605 kfree(work);
11606
f900db47 11607 if (ret == -EIO) {
02e0efb5
ML
11608 struct drm_atomic_state *state;
11609 struct drm_plane_state *plane_state;
11610
f900db47 11611out_hang:
02e0efb5
ML
11612 state = drm_atomic_state_alloc(dev);
11613 if (!state)
11614 return -ENOMEM;
11615 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11616
11617retry:
11618 plane_state = drm_atomic_get_plane_state(state, primary);
11619 ret = PTR_ERR_OR_ZERO(plane_state);
11620 if (!ret) {
11621 drm_atomic_set_fb_for_plane(plane_state, fb);
11622
11623 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11624 if (!ret)
11625 ret = drm_atomic_commit(state);
11626 }
11627
11628 if (ret == -EDEADLK) {
11629 drm_modeset_backoff(state->acquire_ctx);
11630 drm_atomic_state_clear(state);
11631 goto retry;
11632 }
11633
11634 if (ret)
11635 drm_atomic_state_free(state);
11636
f0d3dad3 11637 if (ret == 0 && event) {
5e2d7afc 11638 spin_lock_irq(&dev->event_lock);
a071fa00 11639 drm_send_vblank_event(dev, pipe, event);
5e2d7afc 11640 spin_unlock_irq(&dev->event_lock);
f0d3dad3 11641 }
f900db47 11642 }
96b099fd 11643 return ret;
6b95a207
KH
11644}
11645
da20eabd
ML
11646
11647/**
11648 * intel_wm_need_update - Check whether watermarks need updating
11649 * @plane: drm plane
11650 * @state: new plane state
11651 *
11652 * Check current plane state versus the new one to determine whether
11653 * watermarks need to be recalculated.
11654 *
11655 * Returns true or false.
11656 */
11657static bool intel_wm_need_update(struct drm_plane *plane,
11658 struct drm_plane_state *state)
11659{
d21fbe87
MR
11660 struct intel_plane_state *new = to_intel_plane_state(state);
11661 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
11662
11663 /* Update watermarks on tiling or size changes. */
92826fcd
ML
11664 if (new->visible != cur->visible)
11665 return true;
11666
11667 if (!cur->base.fb || !new->base.fb)
11668 return false;
11669
11670 if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
11671 cur->base.rotation != new->base.rotation ||
d21fbe87
MR
11672 drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
11673 drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
11674 drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
11675 drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
2791a16c 11676 return true;
7809e5ae 11677
2791a16c 11678 return false;
7809e5ae
MR
11679}
11680
d21fbe87
MR
11681static bool needs_scaling(struct intel_plane_state *state)
11682{
11683 int src_w = drm_rect_width(&state->src) >> 16;
11684 int src_h = drm_rect_height(&state->src) >> 16;
11685 int dst_w = drm_rect_width(&state->dst);
11686 int dst_h = drm_rect_height(&state->dst);
11687
11688 return (src_w != dst_w || src_h != dst_h);
11689}
11690
da20eabd
ML
11691int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11692 struct drm_plane_state *plane_state)
11693{
ab1d3a0e 11694 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
da20eabd
ML
11695 struct drm_crtc *crtc = crtc_state->crtc;
11696 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11697 struct drm_plane *plane = plane_state->plane;
11698 struct drm_device *dev = crtc->dev;
ed4a6a7c 11699 struct drm_i915_private *dev_priv = to_i915(dev);
da20eabd
ML
11700 struct intel_plane_state *old_plane_state =
11701 to_intel_plane_state(plane->state);
11702 int idx = intel_crtc->base.base.id, ret;
da20eabd
ML
11703 bool mode_changed = needs_modeset(crtc_state);
11704 bool was_crtc_enabled = crtc->state->active;
11705 bool is_crtc_enabled = crtc_state->active;
da20eabd
ML
11706 bool turn_off, turn_on, visible, was_visible;
11707 struct drm_framebuffer *fb = plane_state->fb;
11708
11709 if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
11710 plane->type != DRM_PLANE_TYPE_CURSOR) {
11711 ret = skl_update_scaler_plane(
11712 to_intel_crtc_state(crtc_state),
11713 to_intel_plane_state(plane_state));
11714 if (ret)
11715 return ret;
11716 }
11717
da20eabd
ML
11718 was_visible = old_plane_state->visible;
11719 visible = to_intel_plane_state(plane_state)->visible;
11720
11721 if (!was_crtc_enabled && WARN_ON(was_visible))
11722 was_visible = false;
11723
35c08f43
ML
11724 /*
11725 * Visibility is calculated as if the crtc was on, but
11726 * after scaler setup everything depends on it being off
11727 * when the crtc isn't active.
11728 */
11729 if (!is_crtc_enabled)
11730 to_intel_plane_state(plane_state)->visible = visible = false;
da20eabd
ML
11731
11732 if (!was_visible && !visible)
11733 return 0;
11734
e8861675
ML
11735 if (fb != old_plane_state->base.fb)
11736 pipe_config->fb_changed = true;
11737
da20eabd
ML
11738 turn_off = was_visible && (!visible || mode_changed);
11739 turn_on = visible && (!was_visible || mode_changed);
11740
11741 DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
11742 plane->base.id, fb ? fb->base.id : -1);
11743
11744 DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
11745 plane->base.id, was_visible, visible,
11746 turn_off, turn_on, mode_changed);
11747
caed361d
VS
11748 if (turn_on) {
11749 pipe_config->update_wm_pre = true;
11750
11751 /* must disable cxsr around plane enable/disable */
11752 if (plane->type != DRM_PLANE_TYPE_CURSOR)
11753 pipe_config->disable_cxsr = true;
11754 } else if (turn_off) {
11755 pipe_config->update_wm_post = true;
92826fcd 11756
852eb00d 11757 /* must disable cxsr around plane enable/disable */
e8861675 11758 if (plane->type != DRM_PLANE_TYPE_CURSOR)
ab1d3a0e 11759 pipe_config->disable_cxsr = true;
852eb00d 11760 } else if (intel_wm_need_update(plane, plane_state)) {
caed361d
VS
11761 /* FIXME bollocks */
11762 pipe_config->update_wm_pre = true;
11763 pipe_config->update_wm_post = true;
852eb00d 11764 }
da20eabd 11765
ed4a6a7c 11766 /* Pre-gen9 platforms need two-step watermark updates */
caed361d
VS
11767 if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
11768 INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
ed4a6a7c
MR
11769 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
11770
8be6ca85 11771 if (visible || was_visible)
cd202f69 11772 pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
a9ff8714 11773
31ae71fc
ML
11774 /*
11775 * WaCxSRDisabledForSpriteScaling:ivb
11776 *
11777 * cstate->update_wm was already set above, so this flag will
11778 * take effect when we commit and program watermarks.
11779 */
11780 if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev) &&
11781 needs_scaling(to_intel_plane_state(plane_state)) &&
11782 !needs_scaling(old_plane_state))
11783 pipe_config->disable_lp_wm = true;
d21fbe87 11784
da20eabd
ML
11785 return 0;
11786}
11787
6d3a1ce7
ML
11788static bool encoders_cloneable(const struct intel_encoder *a,
11789 const struct intel_encoder *b)
11790{
11791 /* masks could be asymmetric, so check both ways */
11792 return a == b || (a->cloneable & (1 << b->type) &&
11793 b->cloneable & (1 << a->type));
11794}
11795
11796static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11797 struct intel_crtc *crtc,
11798 struct intel_encoder *encoder)
11799{
11800 struct intel_encoder *source_encoder;
11801 struct drm_connector *connector;
11802 struct drm_connector_state *connector_state;
11803 int i;
11804
11805 for_each_connector_in_state(state, connector, connector_state, i) {
11806 if (connector_state->crtc != &crtc->base)
11807 continue;
11808
11809 source_encoder =
11810 to_intel_encoder(connector_state->best_encoder);
11811 if (!encoders_cloneable(encoder, source_encoder))
11812 return false;
11813 }
11814
11815 return true;
11816}
11817
11818static bool check_encoder_cloning(struct drm_atomic_state *state,
11819 struct intel_crtc *crtc)
11820{
11821 struct intel_encoder *encoder;
11822 struct drm_connector *connector;
11823 struct drm_connector_state *connector_state;
11824 int i;
11825
11826 for_each_connector_in_state(state, connector, connector_state, i) {
11827 if (connector_state->crtc != &crtc->base)
11828 continue;
11829
11830 encoder = to_intel_encoder(connector_state->best_encoder);
11831 if (!check_single_encoder_cloning(state, crtc, encoder))
11832 return false;
11833 }
11834
11835 return true;
11836}
11837
11838static int intel_crtc_atomic_check(struct drm_crtc *crtc,
11839 struct drm_crtc_state *crtc_state)
11840{
cf5a15be 11841 struct drm_device *dev = crtc->dev;
ad421372 11842 struct drm_i915_private *dev_priv = dev->dev_private;
6d3a1ce7 11843 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cf5a15be
ML
11844 struct intel_crtc_state *pipe_config =
11845 to_intel_crtc_state(crtc_state);
6d3a1ce7 11846 struct drm_atomic_state *state = crtc_state->state;
4d20cd86 11847 int ret;
6d3a1ce7
ML
11848 bool mode_changed = needs_modeset(crtc_state);
11849
11850 if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
11851 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11852 return -EINVAL;
11853 }
11854
852eb00d 11855 if (mode_changed && !crtc_state->active)
caed361d 11856 pipe_config->update_wm_post = true;
eddfcbcd 11857
ad421372
ML
11858 if (mode_changed && crtc_state->enable &&
11859 dev_priv->display.crtc_compute_clock &&
8106ddbd 11860 !WARN_ON(pipe_config->shared_dpll)) {
ad421372
ML
11861 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
11862 pipe_config);
11863 if (ret)
11864 return ret;
11865 }
11866
82cf435b
LL
11867 if (crtc_state->color_mgmt_changed) {
11868 ret = intel_color_check(crtc, crtc_state);
11869 if (ret)
11870 return ret;
11871 }
11872
e435d6e5 11873 ret = 0;
86c8bbbe 11874 if (dev_priv->display.compute_pipe_wm) {
e3bddded 11875 ret = dev_priv->display.compute_pipe_wm(pipe_config);
ed4a6a7c
MR
11876 if (ret) {
11877 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
11878 return ret;
11879 }
11880 }
11881
11882 if (dev_priv->display.compute_intermediate_wm &&
11883 !to_intel_atomic_state(state)->skip_intermediate_wm) {
11884 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
11885 return 0;
11886
11887 /*
11888 * Calculate 'intermediate' watermarks that satisfy both the
11889 * old state and the new state. We can program these
11890 * immediately.
11891 */
11892 ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
11893 intel_crtc,
11894 pipe_config);
11895 if (ret) {
11896 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
86c8bbbe 11897 return ret;
ed4a6a7c 11898 }
86c8bbbe
MR
11899 }
11900
e435d6e5
ML
11901 if (INTEL_INFO(dev)->gen >= 9) {
11902 if (mode_changed)
11903 ret = skl_update_scaler_crtc(pipe_config);
11904
11905 if (!ret)
11906 ret = intel_atomic_setup_scalers(dev, intel_crtc,
11907 pipe_config);
11908 }
11909
11910 return ret;
6d3a1ce7
ML
11911}
11912
65b38e0d 11913static const struct drm_crtc_helper_funcs intel_helper_funcs = {
f6e5b160 11914 .mode_set_base_atomic = intel_pipe_set_base_atomic,
ea2c67bb
MR
11915 .atomic_begin = intel_begin_crtc_commit,
11916 .atomic_flush = intel_finish_crtc_commit,
6d3a1ce7 11917 .atomic_check = intel_crtc_atomic_check,
f6e5b160
CW
11918};
11919
d29b2f9d
ACO
11920static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11921{
11922 struct intel_connector *connector;
11923
11924 for_each_intel_connector(dev, connector) {
11925 if (connector->base.encoder) {
11926 connector->base.state->best_encoder =
11927 connector->base.encoder;
11928 connector->base.state->crtc =
11929 connector->base.encoder->crtc;
11930 } else {
11931 connector->base.state->best_encoder = NULL;
11932 connector->base.state->crtc = NULL;
11933 }
11934 }
11935}
11936
050f7aeb 11937static void
eba905b2 11938connected_sink_compute_bpp(struct intel_connector *connector,
5cec258b 11939 struct intel_crtc_state *pipe_config)
050f7aeb
DV
11940{
11941 int bpp = pipe_config->pipe_bpp;
11942
11943 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
11944 connector->base.base.id,
c23cc417 11945 connector->base.name);
050f7aeb
DV
11946
11947 /* Don't use an invalid EDID bpc value */
11948 if (connector->base.display_info.bpc &&
11949 connector->base.display_info.bpc * 3 < bpp) {
11950 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
11951 bpp, connector->base.display_info.bpc*3);
11952 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
11953 }
11954
013dd9e0
JN
11955 /* Clamp bpp to default limit on screens without EDID 1.4 */
11956 if (connector->base.display_info.bpc == 0) {
11957 int type = connector->base.connector_type;
11958 int clamp_bpp = 24;
11959
11960 /* Fall back to 18 bpp when DP sink capability is unknown. */
11961 if (type == DRM_MODE_CONNECTOR_DisplayPort ||
11962 type == DRM_MODE_CONNECTOR_eDP)
11963 clamp_bpp = 18;
11964
11965 if (bpp > clamp_bpp) {
11966 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of %d\n",
11967 bpp, clamp_bpp);
11968 pipe_config->pipe_bpp = clamp_bpp;
11969 }
050f7aeb
DV
11970 }
11971}
11972
4e53c2e0 11973static int
050f7aeb 11974compute_baseline_pipe_bpp(struct intel_crtc *crtc,
5cec258b 11975 struct intel_crtc_state *pipe_config)
4e53c2e0 11976{
050f7aeb 11977 struct drm_device *dev = crtc->base.dev;
1486017f 11978 struct drm_atomic_state *state;
da3ced29
ACO
11979 struct drm_connector *connector;
11980 struct drm_connector_state *connector_state;
1486017f 11981 int bpp, i;
4e53c2e0 11982
666a4537 11983 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
4e53c2e0 11984 bpp = 10*3;
d328c9d7
DV
11985 else if (INTEL_INFO(dev)->gen >= 5)
11986 bpp = 12*3;
11987 else
11988 bpp = 8*3;
11989
4e53c2e0 11990
4e53c2e0
DV
11991 pipe_config->pipe_bpp = bpp;
11992
1486017f
ACO
11993 state = pipe_config->base.state;
11994
4e53c2e0 11995 /* Clamp display bpp to EDID value */
da3ced29
ACO
11996 for_each_connector_in_state(state, connector, connector_state, i) {
11997 if (connector_state->crtc != &crtc->base)
4e53c2e0
DV
11998 continue;
11999
da3ced29
ACO
12000 connected_sink_compute_bpp(to_intel_connector(connector),
12001 pipe_config);
4e53c2e0
DV
12002 }
12003
12004 return bpp;
12005}
12006
644db711
DV
12007static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
12008{
12009 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
12010 "type: 0x%x flags: 0x%x\n",
1342830c 12011 mode->crtc_clock,
644db711
DV
12012 mode->crtc_hdisplay, mode->crtc_hsync_start,
12013 mode->crtc_hsync_end, mode->crtc_htotal,
12014 mode->crtc_vdisplay, mode->crtc_vsync_start,
12015 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
12016}
12017
c0b03411 12018static void intel_dump_pipe_config(struct intel_crtc *crtc,
5cec258b 12019 struct intel_crtc_state *pipe_config,
c0b03411
DV
12020 const char *context)
12021{
6a60cd87
CK
12022 struct drm_device *dev = crtc->base.dev;
12023 struct drm_plane *plane;
12024 struct intel_plane *intel_plane;
12025 struct intel_plane_state *state;
12026 struct drm_framebuffer *fb;
12027
12028 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
12029 context, pipe_config, pipe_name(crtc->pipe));
c0b03411 12030
da205630 12031 DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
c0b03411
DV
12032 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
12033 pipe_config->pipe_bpp, pipe_config->dither);
12034 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12035 pipe_config->has_pch_encoder,
12036 pipe_config->fdi_lanes,
12037 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
12038 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
12039 pipe_config->fdi_m_n.tu);
90a6b7b0 12040 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
eb14cb74 12041 pipe_config->has_dp_encoder,
90a6b7b0 12042 pipe_config->lane_count,
eb14cb74
VS
12043 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
12044 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
12045 pipe_config->dp_m_n.tu);
b95af8be 12046
90a6b7b0 12047 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
b95af8be 12048 pipe_config->has_dp_encoder,
90a6b7b0 12049 pipe_config->lane_count,
b95af8be
VK
12050 pipe_config->dp_m2_n2.gmch_m,
12051 pipe_config->dp_m2_n2.gmch_n,
12052 pipe_config->dp_m2_n2.link_m,
12053 pipe_config->dp_m2_n2.link_n,
12054 pipe_config->dp_m2_n2.tu);
12055
55072d19
DV
12056 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12057 pipe_config->has_audio,
12058 pipe_config->has_infoframe);
12059
c0b03411 12060 DRM_DEBUG_KMS("requested mode:\n");
2d112de7 12061 drm_mode_debug_printmodeline(&pipe_config->base.mode);
c0b03411 12062 DRM_DEBUG_KMS("adjusted mode:\n");
2d112de7
ACO
12063 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12064 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
d71b8d4a 12065 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
37327abd
VS
12066 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12067 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
0ec463d3
TU
12068 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12069 crtc->num_scalers,
12070 pipe_config->scaler_state.scaler_users,
12071 pipe_config->scaler_state.scaler_id);
c0b03411
DV
12072 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12073 pipe_config->gmch_pfit.control,
12074 pipe_config->gmch_pfit.pgm_ratios,
12075 pipe_config->gmch_pfit.lvds_border_bits);
fd4daa9c 12076 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
c0b03411 12077 pipe_config->pch_pfit.pos,
fd4daa9c
CW
12078 pipe_config->pch_pfit.size,
12079 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
42db64ef 12080 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
cf532bb2 12081 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
6a60cd87 12082
415ff0f6 12083 if (IS_BROXTON(dev)) {
05712c15 12084 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
415ff0f6 12085 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
c8453338 12086 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
415ff0f6
TU
12087 pipe_config->ddi_pll_sel,
12088 pipe_config->dpll_hw_state.ebb0,
05712c15 12089 pipe_config->dpll_hw_state.ebb4,
415ff0f6
TU
12090 pipe_config->dpll_hw_state.pll0,
12091 pipe_config->dpll_hw_state.pll1,
12092 pipe_config->dpll_hw_state.pll2,
12093 pipe_config->dpll_hw_state.pll3,
12094 pipe_config->dpll_hw_state.pll6,
12095 pipe_config->dpll_hw_state.pll8,
05712c15 12096 pipe_config->dpll_hw_state.pll9,
c8453338 12097 pipe_config->dpll_hw_state.pll10,
415ff0f6 12098 pipe_config->dpll_hw_state.pcsdw12);
ef11bdb3 12099 } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
415ff0f6
TU
12100 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12101 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12102 pipe_config->ddi_pll_sel,
12103 pipe_config->dpll_hw_state.ctrl1,
12104 pipe_config->dpll_hw_state.cfgcr1,
12105 pipe_config->dpll_hw_state.cfgcr2);
12106 } else if (HAS_DDI(dev)) {
1260f07e 12107 DRM_DEBUG_KMS("ddi_pll_sel: 0x%x; dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
415ff0f6 12108 pipe_config->ddi_pll_sel,
00490c22
ML
12109 pipe_config->dpll_hw_state.wrpll,
12110 pipe_config->dpll_hw_state.spll);
415ff0f6
TU
12111 } else {
12112 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12113 "fp0: 0x%x, fp1: 0x%x\n",
12114 pipe_config->dpll_hw_state.dpll,
12115 pipe_config->dpll_hw_state.dpll_md,
12116 pipe_config->dpll_hw_state.fp0,
12117 pipe_config->dpll_hw_state.fp1);
12118 }
12119
6a60cd87
CK
12120 DRM_DEBUG_KMS("planes on this crtc\n");
12121 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12122 intel_plane = to_intel_plane(plane);
12123 if (intel_plane->pipe != crtc->pipe)
12124 continue;
12125
12126 state = to_intel_plane_state(plane->state);
12127 fb = state->base.fb;
12128 if (!fb) {
12129 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
12130 "disabled, scaler_id = %d\n",
12131 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12132 plane->base.id, intel_plane->pipe,
12133 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
12134 drm_plane_index(plane), state->scaler_id);
12135 continue;
12136 }
12137
12138 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
12139 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12140 plane->base.id, intel_plane->pipe,
12141 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
12142 drm_plane_index(plane));
12143 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
12144 fb->base.id, fb->width, fb->height, fb->pixel_format);
12145 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
12146 state->scaler_id,
12147 state->src.x1 >> 16, state->src.y1 >> 16,
12148 drm_rect_width(&state->src) >> 16,
12149 drm_rect_height(&state->src) >> 16,
12150 state->dst.x1, state->dst.y1,
12151 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
12152 }
c0b03411
DV
12153}
12154
5448a00d 12155static bool check_digital_port_conflicts(struct drm_atomic_state *state)
00f0b378 12156{
5448a00d 12157 struct drm_device *dev = state->dev;
da3ced29 12158 struct drm_connector *connector;
00f0b378
VS
12159 unsigned int used_ports = 0;
12160
12161 /*
12162 * Walk the connector list instead of the encoder
12163 * list to detect the problem on ddi platforms
12164 * where there's just one encoder per digital port.
12165 */
0bff4858
VS
12166 drm_for_each_connector(connector, dev) {
12167 struct drm_connector_state *connector_state;
12168 struct intel_encoder *encoder;
12169
12170 connector_state = drm_atomic_get_existing_connector_state(state, connector);
12171 if (!connector_state)
12172 connector_state = connector->state;
12173
5448a00d 12174 if (!connector_state->best_encoder)
00f0b378
VS
12175 continue;
12176
5448a00d
ACO
12177 encoder = to_intel_encoder(connector_state->best_encoder);
12178
12179 WARN_ON(!connector_state->crtc);
00f0b378
VS
12180
12181 switch (encoder->type) {
12182 unsigned int port_mask;
12183 case INTEL_OUTPUT_UNKNOWN:
12184 if (WARN_ON(!HAS_DDI(dev)))
12185 break;
12186 case INTEL_OUTPUT_DISPLAYPORT:
12187 case INTEL_OUTPUT_HDMI:
12188 case INTEL_OUTPUT_EDP:
12189 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12190
12191 /* the same port mustn't appear more than once */
12192 if (used_ports & port_mask)
12193 return false;
12194
12195 used_ports |= port_mask;
12196 default:
12197 break;
12198 }
12199 }
12200
12201 return true;
12202}
12203
83a57153
ACO
12204static void
12205clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12206{
12207 struct drm_crtc_state tmp_state;
663a3640 12208 struct intel_crtc_scaler_state scaler_state;
4978cc93 12209 struct intel_dpll_hw_state dpll_hw_state;
8106ddbd 12210 struct intel_shared_dpll *shared_dpll;
8504c74c 12211 uint32_t ddi_pll_sel;
c4e2d043 12212 bool force_thru;
83a57153 12213
7546a384
ACO
12214 /* FIXME: before the switch to atomic started, a new pipe_config was
12215 * kzalloc'd. Code that depends on any field being zero should be
12216 * fixed, so that the crtc_state can be safely duplicated. For now,
12217 * only fields that are know to not cause problems are preserved. */
12218
83a57153 12219 tmp_state = crtc_state->base;
663a3640 12220 scaler_state = crtc_state->scaler_state;
4978cc93
ACO
12221 shared_dpll = crtc_state->shared_dpll;
12222 dpll_hw_state = crtc_state->dpll_hw_state;
8504c74c 12223 ddi_pll_sel = crtc_state->ddi_pll_sel;
c4e2d043 12224 force_thru = crtc_state->pch_pfit.force_thru;
4978cc93 12225
83a57153 12226 memset(crtc_state, 0, sizeof *crtc_state);
4978cc93 12227
83a57153 12228 crtc_state->base = tmp_state;
663a3640 12229 crtc_state->scaler_state = scaler_state;
4978cc93
ACO
12230 crtc_state->shared_dpll = shared_dpll;
12231 crtc_state->dpll_hw_state = dpll_hw_state;
8504c74c 12232 crtc_state->ddi_pll_sel = ddi_pll_sel;
c4e2d043 12233 crtc_state->pch_pfit.force_thru = force_thru;
83a57153
ACO
12234}
12235
548ee15b 12236static int
b8cecdf5 12237intel_modeset_pipe_config(struct drm_crtc *crtc,
b359283a 12238 struct intel_crtc_state *pipe_config)
ee7b9f93 12239{
b359283a 12240 struct drm_atomic_state *state = pipe_config->base.state;
7758a113 12241 struct intel_encoder *encoder;
da3ced29 12242 struct drm_connector *connector;
0b901879 12243 struct drm_connector_state *connector_state;
d328c9d7 12244 int base_bpp, ret = -EINVAL;
0b901879 12245 int i;
e29c22c0 12246 bool retry = true;
ee7b9f93 12247
83a57153 12248 clear_intel_crtc_state(pipe_config);
7758a113 12249
e143a21c
DV
12250 pipe_config->cpu_transcoder =
12251 (enum transcoder) to_intel_crtc(crtc)->pipe;
b8cecdf5 12252
2960bc9c
ID
12253 /*
12254 * Sanitize sync polarity flags based on requested ones. If neither
12255 * positive or negative polarity is requested, treat this as meaning
12256 * negative polarity.
12257 */
2d112de7 12258 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 12259 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
2d112de7 12260 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
2960bc9c 12261
2d112de7 12262 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 12263 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
2d112de7 12264 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
2960bc9c 12265
d328c9d7
DV
12266 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12267 pipe_config);
12268 if (base_bpp < 0)
4e53c2e0
DV
12269 goto fail;
12270
e41a56be
VS
12271 /*
12272 * Determine the real pipe dimensions. Note that stereo modes can
12273 * increase the actual pipe size due to the frame doubling and
12274 * insertion of additional space for blanks between the frame. This
12275 * is stored in the crtc timings. We use the requested mode to do this
12276 * computation to clearly distinguish it from the adjusted mode, which
12277 * can be changed by the connectors in the below retry loop.
12278 */
2d112de7 12279 drm_crtc_get_hv_timing(&pipe_config->base.mode,
ecb7e16b
GP
12280 &pipe_config->pipe_src_w,
12281 &pipe_config->pipe_src_h);
e41a56be 12282
e29c22c0 12283encoder_retry:
ef1b460d 12284 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 12285 pipe_config->port_clock = 0;
ef1b460d 12286 pipe_config->pixel_multiplier = 1;
ff9a6750 12287
135c81b8 12288 /* Fill in default crtc timings, allow encoders to overwrite them. */
2d112de7
ACO
12289 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12290 CRTC_STEREO_DOUBLE);
135c81b8 12291
7758a113
DV
12292 /* Pass our mode to the connectors and the CRTC to give them a chance to
12293 * adjust it according to limitations or connector properties, and also
12294 * a chance to reject the mode entirely.
47f1c6c9 12295 */
da3ced29 12296 for_each_connector_in_state(state, connector, connector_state, i) {
0b901879 12297 if (connector_state->crtc != crtc)
7758a113 12298 continue;
7ae89233 12299
0b901879
ACO
12300 encoder = to_intel_encoder(connector_state->best_encoder);
12301
efea6e8e
DV
12302 if (!(encoder->compute_config(encoder, pipe_config))) {
12303 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
DV
12304 goto fail;
12305 }
ee7b9f93 12306 }
47f1c6c9 12307
ff9a6750
DV
12308 /* Set default port clock if not overwritten by the encoder. Needs to be
12309 * done afterwards in case the encoder adjusts the mode. */
12310 if (!pipe_config->port_clock)
2d112de7 12311 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
241bfc38 12312 * pipe_config->pixel_multiplier;
ff9a6750 12313
a43f6e0f 12314 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 12315 if (ret < 0) {
7758a113
DV
12316 DRM_DEBUG_KMS("CRTC fixup failed\n");
12317 goto fail;
ee7b9f93 12318 }
e29c22c0
DV
12319
12320 if (ret == RETRY) {
12321 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12322 ret = -EINVAL;
12323 goto fail;
12324 }
12325
12326 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12327 retry = false;
12328 goto encoder_retry;
12329 }
12330
e8fa4270
DV
12331 /* Dithering seems to not pass-through bits correctly when it should, so
12332 * only enable it on 6bpc panels. */
12333 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
62f0ace5 12334 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
d328c9d7 12335 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
4e53c2e0 12336
7758a113 12337fail:
548ee15b 12338 return ret;
ee7b9f93 12339}
47f1c6c9 12340
ea9d758d 12341static void
4740b0f2 12342intel_modeset_update_crtc_state(struct drm_atomic_state *state)
ea9d758d 12343{
0a9ab303
ACO
12344 struct drm_crtc *crtc;
12345 struct drm_crtc_state *crtc_state;
8a75d157 12346 int i;
ea9d758d 12347
7668851f 12348 /* Double check state. */
8a75d157 12349 for_each_crtc_in_state(state, crtc, crtc_state, i) {
3cb480bc 12350 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
fc467a22
ML
12351
12352 /* Update hwmode for vblank functions */
12353 if (crtc->state->active)
12354 crtc->hwmode = crtc->state->adjusted_mode;
12355 else
12356 crtc->hwmode.crtc_clock = 0;
61067a5e
ML
12357
12358 /*
12359 * Update legacy state to satisfy fbc code. This can
12360 * be removed when fbc uses the atomic state.
12361 */
12362 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
12363 struct drm_plane_state *plane_state = crtc->primary->state;
12364
12365 crtc->primary->fb = plane_state->fb;
12366 crtc->x = plane_state->src_x >> 16;
12367 crtc->y = plane_state->src_y >> 16;
12368 }
ea9d758d 12369 }
ea9d758d
DV
12370}
12371
3bd26263 12372static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 12373{
3bd26263 12374 int diff;
f1f644dc
JB
12375
12376 if (clock1 == clock2)
12377 return true;
12378
12379 if (!clock1 || !clock2)
12380 return false;
12381
12382 diff = abs(clock1 - clock2);
12383
12384 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12385 return true;
12386
12387 return false;
12388}
12389
25c5b266
DV
12390#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12391 list_for_each_entry((intel_crtc), \
12392 &(dev)->mode_config.crtc_list, \
12393 base.head) \
95150bdf 12394 for_each_if (mask & (1 <<(intel_crtc)->pipe))
25c5b266 12395
cfb23ed6
ML
12396static bool
12397intel_compare_m_n(unsigned int m, unsigned int n,
12398 unsigned int m2, unsigned int n2,
12399 bool exact)
12400{
12401 if (m == m2 && n == n2)
12402 return true;
12403
12404 if (exact || !m || !n || !m2 || !n2)
12405 return false;
12406
12407 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
12408
31d10b57
ML
12409 if (n > n2) {
12410 while (n > n2) {
cfb23ed6
ML
12411 m2 <<= 1;
12412 n2 <<= 1;
12413 }
31d10b57
ML
12414 } else if (n < n2) {
12415 while (n < n2) {
cfb23ed6
ML
12416 m <<= 1;
12417 n <<= 1;
12418 }
12419 }
12420
31d10b57
ML
12421 if (n != n2)
12422 return false;
12423
12424 return intel_fuzzy_clock_check(m, m2);
cfb23ed6
ML
12425}
12426
12427static bool
12428intel_compare_link_m_n(const struct intel_link_m_n *m_n,
12429 struct intel_link_m_n *m2_n2,
12430 bool adjust)
12431{
12432 if (m_n->tu == m2_n2->tu &&
12433 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
12434 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
12435 intel_compare_m_n(m_n->link_m, m_n->link_n,
12436 m2_n2->link_m, m2_n2->link_n, !adjust)) {
12437 if (adjust)
12438 *m2_n2 = *m_n;
12439
12440 return true;
12441 }
12442
12443 return false;
12444}
12445
0e8ffe1b 12446static bool
2fa2fe9a 12447intel_pipe_config_compare(struct drm_device *dev,
5cec258b 12448 struct intel_crtc_state *current_config,
cfb23ed6
ML
12449 struct intel_crtc_state *pipe_config,
12450 bool adjust)
0e8ffe1b 12451{
cfb23ed6
ML
12452 bool ret = true;
12453
12454#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
12455 do { \
12456 if (!adjust) \
12457 DRM_ERROR(fmt, ##__VA_ARGS__); \
12458 else \
12459 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
12460 } while (0)
12461
66e985c0
DV
12462#define PIPE_CONF_CHECK_X(name) \
12463 if (current_config->name != pipe_config->name) { \
cfb23ed6 12464 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
66e985c0
DV
12465 "(expected 0x%08x, found 0x%08x)\n", \
12466 current_config->name, \
12467 pipe_config->name); \
cfb23ed6 12468 ret = false; \
66e985c0
DV
12469 }
12470
08a24034
DV
12471#define PIPE_CONF_CHECK_I(name) \
12472 if (current_config->name != pipe_config->name) { \
cfb23ed6 12473 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
08a24034
DV
12474 "(expected %i, found %i)\n", \
12475 current_config->name, \
12476 pipe_config->name); \
cfb23ed6
ML
12477 ret = false; \
12478 }
12479
8106ddbd
ACO
12480#define PIPE_CONF_CHECK_P(name) \
12481 if (current_config->name != pipe_config->name) { \
12482 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12483 "(expected %p, found %p)\n", \
12484 current_config->name, \
12485 pipe_config->name); \
12486 ret = false; \
12487 }
12488
cfb23ed6
ML
12489#define PIPE_CONF_CHECK_M_N(name) \
12490 if (!intel_compare_link_m_n(&current_config->name, \
12491 &pipe_config->name,\
12492 adjust)) { \
12493 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12494 "(expected tu %i gmch %i/%i link %i/%i, " \
12495 "found tu %i, gmch %i/%i link %i/%i)\n", \
12496 current_config->name.tu, \
12497 current_config->name.gmch_m, \
12498 current_config->name.gmch_n, \
12499 current_config->name.link_m, \
12500 current_config->name.link_n, \
12501 pipe_config->name.tu, \
12502 pipe_config->name.gmch_m, \
12503 pipe_config->name.gmch_n, \
12504 pipe_config->name.link_m, \
12505 pipe_config->name.link_n); \
12506 ret = false; \
12507 }
12508
12509#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12510 if (!intel_compare_link_m_n(&current_config->name, \
12511 &pipe_config->name, adjust) && \
12512 !intel_compare_link_m_n(&current_config->alt_name, \
12513 &pipe_config->name, adjust)) { \
12514 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12515 "(expected tu %i gmch %i/%i link %i/%i, " \
12516 "or tu %i gmch %i/%i link %i/%i, " \
12517 "found tu %i, gmch %i/%i link %i/%i)\n", \
12518 current_config->name.tu, \
12519 current_config->name.gmch_m, \
12520 current_config->name.gmch_n, \
12521 current_config->name.link_m, \
12522 current_config->name.link_n, \
12523 current_config->alt_name.tu, \
12524 current_config->alt_name.gmch_m, \
12525 current_config->alt_name.gmch_n, \
12526 current_config->alt_name.link_m, \
12527 current_config->alt_name.link_n, \
12528 pipe_config->name.tu, \
12529 pipe_config->name.gmch_m, \
12530 pipe_config->name.gmch_n, \
12531 pipe_config->name.link_m, \
12532 pipe_config->name.link_n); \
12533 ret = false; \
88adfff1
DV
12534 }
12535
b95af8be
VK
12536/* This is required for BDW+ where there is only one set of registers for
12537 * switching between high and low RR.
12538 * This macro can be used whenever a comparison has to be made between one
12539 * hw state and multiple sw state variables.
12540 */
12541#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
12542 if ((current_config->name != pipe_config->name) && \
12543 (current_config->alt_name != pipe_config->name)) { \
cfb23ed6 12544 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
b95af8be
VK
12545 "(expected %i or %i, found %i)\n", \
12546 current_config->name, \
12547 current_config->alt_name, \
12548 pipe_config->name); \
cfb23ed6 12549 ret = false; \
b95af8be
VK
12550 }
12551
1bd1bd80
DV
12552#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12553 if ((current_config->name ^ pipe_config->name) & (mask)) { \
cfb23ed6 12554 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
1bd1bd80
DV
12555 "(expected %i, found %i)\n", \
12556 current_config->name & (mask), \
12557 pipe_config->name & (mask)); \
cfb23ed6 12558 ret = false; \
1bd1bd80
DV
12559 }
12560
5e550656
VS
12561#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12562 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
cfb23ed6 12563 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
5e550656
VS
12564 "(expected %i, found %i)\n", \
12565 current_config->name, \
12566 pipe_config->name); \
cfb23ed6 12567 ret = false; \
5e550656
VS
12568 }
12569
bb760063
DV
12570#define PIPE_CONF_QUIRK(quirk) \
12571 ((current_config->quirks | pipe_config->quirks) & (quirk))
12572
eccb140b
DV
12573 PIPE_CONF_CHECK_I(cpu_transcoder);
12574
08a24034
DV
12575 PIPE_CONF_CHECK_I(has_pch_encoder);
12576 PIPE_CONF_CHECK_I(fdi_lanes);
cfb23ed6 12577 PIPE_CONF_CHECK_M_N(fdi_m_n);
08a24034 12578
eb14cb74 12579 PIPE_CONF_CHECK_I(has_dp_encoder);
90a6b7b0 12580 PIPE_CONF_CHECK_I(lane_count);
b95af8be
VK
12581
12582 if (INTEL_INFO(dev)->gen < 8) {
cfb23ed6
ML
12583 PIPE_CONF_CHECK_M_N(dp_m_n);
12584
cfb23ed6
ML
12585 if (current_config->has_drrs)
12586 PIPE_CONF_CHECK_M_N(dp_m2_n2);
12587 } else
12588 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
eb14cb74 12589
a65347ba
JN
12590 PIPE_CONF_CHECK_I(has_dsi_encoder);
12591
2d112de7
ACO
12592 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12593 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12594 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12595 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12596 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12597 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
1bd1bd80 12598
2d112de7
ACO
12599 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12600 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12601 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12602 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12603 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12604 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
1bd1bd80 12605
c93f54cf 12606 PIPE_CONF_CHECK_I(pixel_multiplier);
6897b4b5 12607 PIPE_CONF_CHECK_I(has_hdmi_sink);
b5a9fa09 12608 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
666a4537 12609 IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
b5a9fa09 12610 PIPE_CONF_CHECK_I(limited_color_range);
e43823ec 12611 PIPE_CONF_CHECK_I(has_infoframe);
6c49f241 12612
9ed109a7
DV
12613 PIPE_CONF_CHECK_I(has_audio);
12614
2d112de7 12615 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
1bd1bd80
DV
12616 DRM_MODE_FLAG_INTERLACE);
12617
bb760063 12618 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
2d112de7 12619 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12620 DRM_MODE_FLAG_PHSYNC);
2d112de7 12621 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12622 DRM_MODE_FLAG_NHSYNC);
2d112de7 12623 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12624 DRM_MODE_FLAG_PVSYNC);
2d112de7 12625 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063
DV
12626 DRM_MODE_FLAG_NVSYNC);
12627 }
045ac3b5 12628
333b8ca8 12629 PIPE_CONF_CHECK_X(gmch_pfit.control);
e2ff2d4a
DV
12630 /* pfit ratios are autocomputed by the hw on gen4+ */
12631 if (INTEL_INFO(dev)->gen < 4)
12632 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
333b8ca8 12633 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
9953599b 12634
bfd16b2a
ML
12635 if (!adjust) {
12636 PIPE_CONF_CHECK_I(pipe_src_w);
12637 PIPE_CONF_CHECK_I(pipe_src_h);
12638
12639 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12640 if (current_config->pch_pfit.enabled) {
12641 PIPE_CONF_CHECK_X(pch_pfit.pos);
12642 PIPE_CONF_CHECK_X(pch_pfit.size);
12643 }
2fa2fe9a 12644
7aefe2b5
ML
12645 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12646 }
a1b2278e 12647
e59150dc
JB
12648 /* BDW+ don't expose a synchronous way to read the state */
12649 if (IS_HASWELL(dev))
12650 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 12651
282740f7
VS
12652 PIPE_CONF_CHECK_I(double_wide);
12653
26804afd
DV
12654 PIPE_CONF_CHECK_X(ddi_pll_sel);
12655
8106ddbd 12656 PIPE_CONF_CHECK_P(shared_dpll);
66e985c0 12657 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 12658 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
DV
12659 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12660 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
d452c5b6 12661 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
00490c22 12662 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
3f4cd19f
DL
12663 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12664 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12665 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
c0d43d62 12666
42571aef
VS
12667 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12668 PIPE_CONF_CHECK_I(pipe_bpp);
12669
2d112de7 12670 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
a9a7e98a 12671 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 12672
66e985c0 12673#undef PIPE_CONF_CHECK_X
08a24034 12674#undef PIPE_CONF_CHECK_I
8106ddbd 12675#undef PIPE_CONF_CHECK_P
b95af8be 12676#undef PIPE_CONF_CHECK_I_ALT
1bd1bd80 12677#undef PIPE_CONF_CHECK_FLAGS
5e550656 12678#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 12679#undef PIPE_CONF_QUIRK
cfb23ed6 12680#undef INTEL_ERR_OR_DBG_KMS
88adfff1 12681
cfb23ed6 12682 return ret;
0e8ffe1b
DV
12683}
12684
e3b247da
VS
12685static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
12686 const struct intel_crtc_state *pipe_config)
12687{
12688 if (pipe_config->has_pch_encoder) {
21a727b3 12689 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
e3b247da
VS
12690 &pipe_config->fdi_m_n);
12691 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
12692
12693 /*
12694 * FDI already provided one idea for the dotclock.
12695 * Yell if the encoder disagrees.
12696 */
12697 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
12698 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
12699 fdi_dotclock, dotclock);
12700 }
12701}
12702
08db6652
DL
12703static void check_wm_state(struct drm_device *dev)
12704{
12705 struct drm_i915_private *dev_priv = dev->dev_private;
12706 struct skl_ddb_allocation hw_ddb, *sw_ddb;
12707 struct intel_crtc *intel_crtc;
12708 int plane;
12709
12710 if (INTEL_INFO(dev)->gen < 9)
12711 return;
12712
12713 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12714 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12715
12716 for_each_intel_crtc(dev, intel_crtc) {
12717 struct skl_ddb_entry *hw_entry, *sw_entry;
12718 const enum pipe pipe = intel_crtc->pipe;
12719
12720 if (!intel_crtc->active)
12721 continue;
12722
12723 /* planes */
dd740780 12724 for_each_plane(dev_priv, pipe, plane) {
08db6652
DL
12725 hw_entry = &hw_ddb.plane[pipe][plane];
12726 sw_entry = &sw_ddb->plane[pipe][plane];
12727
12728 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12729 continue;
12730
12731 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12732 "(expected (%u,%u), found (%u,%u))\n",
12733 pipe_name(pipe), plane + 1,
12734 sw_entry->start, sw_entry->end,
12735 hw_entry->start, hw_entry->end);
12736 }
12737
12738 /* cursor */
4969d33e
MR
12739 hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
12740 sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
08db6652
DL
12741
12742 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12743 continue;
12744
12745 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12746 "(expected (%u,%u), found (%u,%u))\n",
12747 pipe_name(pipe),
12748 sw_entry->start, sw_entry->end,
12749 hw_entry->start, hw_entry->end);
12750 }
12751}
12752
91d1b4bd 12753static void
35dd3c64
ML
12754check_connector_state(struct drm_device *dev,
12755 struct drm_atomic_state *old_state)
8af6cf88 12756{
35dd3c64
ML
12757 struct drm_connector_state *old_conn_state;
12758 struct drm_connector *connector;
12759 int i;
8af6cf88 12760
35dd3c64
ML
12761 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
12762 struct drm_encoder *encoder = connector->encoder;
12763 struct drm_connector_state *state = connector->state;
ad3c558f 12764
8af6cf88
DV
12765 /* This also checks the encoder/connector hw state with the
12766 * ->get_hw_state callbacks. */
35dd3c64 12767 intel_connector_check_state(to_intel_connector(connector));
8af6cf88 12768
ad3c558f 12769 I915_STATE_WARN(state->best_encoder != encoder,
35dd3c64 12770 "connector's atomic encoder doesn't match legacy encoder\n");
8af6cf88 12771 }
91d1b4bd
DV
12772}
12773
12774static void
12775check_encoder_state(struct drm_device *dev)
12776{
12777 struct intel_encoder *encoder;
12778 struct intel_connector *connector;
8af6cf88 12779
b2784e15 12780 for_each_intel_encoder(dev, encoder) {
8af6cf88 12781 bool enabled = false;
4d20cd86 12782 enum pipe pipe;
8af6cf88
DV
12783
12784 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12785 encoder->base.base.id,
8e329a03 12786 encoder->base.name);
8af6cf88 12787
3a3371ff 12788 for_each_intel_connector(dev, connector) {
4d20cd86 12789 if (connector->base.state->best_encoder != &encoder->base)
8af6cf88
DV
12790 continue;
12791 enabled = true;
ad3c558f
ML
12792
12793 I915_STATE_WARN(connector->base.state->crtc !=
12794 encoder->base.crtc,
12795 "connector's crtc doesn't match encoder crtc\n");
8af6cf88 12796 }
0e32b39c 12797
e2c719b7 12798 I915_STATE_WARN(!!encoder->base.crtc != enabled,
8af6cf88
DV
12799 "encoder's enabled state mismatch "
12800 "(expected %i, found %i)\n",
12801 !!encoder->base.crtc, enabled);
7c60d198
ML
12802
12803 if (!encoder->base.crtc) {
4d20cd86 12804 bool active;
7c60d198 12805
4d20cd86
ML
12806 active = encoder->get_hw_state(encoder, &pipe);
12807 I915_STATE_WARN(active,
12808 "encoder detached but still enabled on pipe %c.\n",
12809 pipe_name(pipe));
7c60d198 12810 }
8af6cf88 12811 }
91d1b4bd
DV
12812}
12813
12814static void
4d20cd86 12815check_crtc_state(struct drm_device *dev, struct drm_atomic_state *old_state)
91d1b4bd 12816{
fbee40df 12817 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd 12818 struct intel_encoder *encoder;
4d20cd86
ML
12819 struct drm_crtc_state *old_crtc_state;
12820 struct drm_crtc *crtc;
12821 int i;
8af6cf88 12822
4d20cd86
ML
12823 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
12824 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12825 struct intel_crtc_state *pipe_config, *sw_config;
7b89b8de 12826 bool active;
8af6cf88 12827
bfd16b2a
ML
12828 if (!needs_modeset(crtc->state) &&
12829 !to_intel_crtc_state(crtc->state)->update_pipe)
4d20cd86 12830 continue;
045ac3b5 12831
4d20cd86
ML
12832 __drm_atomic_helper_crtc_destroy_state(crtc, old_crtc_state);
12833 pipe_config = to_intel_crtc_state(old_crtc_state);
12834 memset(pipe_config, 0, sizeof(*pipe_config));
12835 pipe_config->base.crtc = crtc;
12836 pipe_config->base.state = old_state;
8af6cf88 12837
4d20cd86
ML
12838 DRM_DEBUG_KMS("[CRTC:%d]\n",
12839 crtc->base.id);
8af6cf88 12840
4d20cd86
ML
12841 active = dev_priv->display.get_pipe_config(intel_crtc,
12842 pipe_config);
d62cf62a 12843
b6b5d049 12844 /* hw state is inconsistent with the pipe quirk */
4d20cd86
ML
12845 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12846 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
12847 active = crtc->state->active;
6c49f241 12848
4d20cd86 12849 I915_STATE_WARN(crtc->state->active != active,
0e8ffe1b 12850 "crtc active state doesn't match with hw state "
4d20cd86 12851 "(expected %i, found %i)\n", crtc->state->active, active);
0e8ffe1b 12852
4d20cd86 12853 I915_STATE_WARN(intel_crtc->active != crtc->state->active,
53d9f4e9 12854 "transitional active state does not match atomic hw state "
4d20cd86
ML
12855 "(expected %i, found %i)\n", crtc->state->active, intel_crtc->active);
12856
12857 for_each_encoder_on_crtc(dev, crtc, encoder) {
12858 enum pipe pipe;
12859
12860 active = encoder->get_hw_state(encoder, &pipe);
12861 I915_STATE_WARN(active != crtc->state->active,
12862 "[ENCODER:%i] active %i with crtc active %i\n",
12863 encoder->base.base.id, active, crtc->state->active);
12864
12865 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
12866 "Encoder connected to wrong pipe %c\n",
12867 pipe_name(pipe));
12868
12869 if (active)
12870 encoder->get_config(encoder, pipe_config);
12871 }
53d9f4e9 12872
4d20cd86 12873 if (!crtc->state->active)
cfb23ed6
ML
12874 continue;
12875
e3b247da
VS
12876 intel_pipe_config_sanity_check(dev_priv, pipe_config);
12877
4d20cd86
ML
12878 sw_config = to_intel_crtc_state(crtc->state);
12879 if (!intel_pipe_config_compare(dev, sw_config,
12880 pipe_config, false)) {
e2c719b7 12881 I915_STATE_WARN(1, "pipe state doesn't match!\n");
4d20cd86 12882 intel_dump_pipe_config(intel_crtc, pipe_config,
c0b03411 12883 "[hw state]");
4d20cd86 12884 intel_dump_pipe_config(intel_crtc, sw_config,
c0b03411
DV
12885 "[sw state]");
12886 }
8af6cf88
DV
12887 }
12888}
12889
91d1b4bd
DV
12890static void
12891check_shared_dpll_state(struct drm_device *dev)
12892{
fbee40df 12893 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd
DV
12894 struct intel_crtc *crtc;
12895 struct intel_dpll_hw_state dpll_hw_state;
12896 int i;
5358901f
DV
12897
12898 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
8106ddbd
ACO
12899 struct intel_shared_dpll *pll =
12900 intel_get_shared_dpll_by_id(dev_priv, i);
2dd66ebd 12901 unsigned enabled_crtcs = 0, active_crtcs = 0;
5358901f
DV
12902 bool active;
12903
12904 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12905
12906 DRM_DEBUG_KMS("%s\n", pll->name);
12907
2edd6443 12908 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
5358901f 12909
2dd66ebd
ML
12910 I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
12911 "more active pll users than references: %x vs %x\n",
12912 pll->active_mask, pll->config.crtc_mask);
9d16da65
ACO
12913
12914 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
2dd66ebd
ML
12915 I915_STATE_WARN(!pll->on && pll->active_mask,
12916 "pll in active use but not on in sw tracking\n");
12917 I915_STATE_WARN(pll->on && !pll->active_mask,
12918 "pll is on but not used by any active crtc\n");
9d16da65
ACO
12919 I915_STATE_WARN(pll->on != active,
12920 "pll on state mismatch (expected %i, found %i)\n",
12921 pll->on, active);
12922 }
5358901f 12923
d3fcc808 12924 for_each_intel_crtc(dev, crtc) {
8106ddbd 12925 if (crtc->base.state->enable && crtc->config->shared_dpll == pll)
2dd66ebd
ML
12926 enabled_crtcs |= 1 << drm_crtc_index(&crtc->base);
12927 if (crtc->base.state->active && crtc->config->shared_dpll == pll)
12928 active_crtcs |= 1 << drm_crtc_index(&crtc->base);
5358901f 12929 }
2dd66ebd
ML
12930
12931 I915_STATE_WARN(pll->active_mask != active_crtcs,
12932 "pll active crtcs mismatch (expected %x, found %x)\n",
12933 pll->active_mask, active_crtcs);
12934 I915_STATE_WARN(pll->config.crtc_mask != enabled_crtcs,
12935 "pll enabled crtcs mismatch (expected %x, found %x)\n",
12936 pll->config.crtc_mask, enabled_crtcs);
66e985c0 12937
e2c719b7 12938 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
66e985c0
DV
12939 sizeof(dpll_hw_state)),
12940 "pll hw state mismatch\n");
5358901f 12941 }
8af6cf88
DV
12942}
12943
ee165b1a
ML
12944static void
12945intel_modeset_check_state(struct drm_device *dev,
12946 struct drm_atomic_state *old_state)
91d1b4bd 12947{
08db6652 12948 check_wm_state(dev);
35dd3c64 12949 check_connector_state(dev, old_state);
91d1b4bd 12950 check_encoder_state(dev);
4d20cd86 12951 check_crtc_state(dev, old_state);
91d1b4bd
DV
12952 check_shared_dpll_state(dev);
12953}
12954
80715b2f
VS
12955static void update_scanline_offset(struct intel_crtc *crtc)
12956{
12957 struct drm_device *dev = crtc->base.dev;
12958
12959 /*
12960 * The scanline counter increments at the leading edge of hsync.
12961 *
12962 * On most platforms it starts counting from vtotal-1 on the
12963 * first active line. That means the scanline counter value is
12964 * always one less than what we would expect. Ie. just after
12965 * start of vblank, which also occurs at start of hsync (on the
12966 * last active line), the scanline counter will read vblank_start-1.
12967 *
12968 * On gen2 the scanline counter starts counting from 1 instead
12969 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12970 * to keep the value positive), instead of adding one.
12971 *
12972 * On HSW+ the behaviour of the scanline counter depends on the output
12973 * type. For DP ports it behaves like most other platforms, but on HDMI
12974 * there's an extra 1 line difference. So we need to add two instead of
12975 * one to the value.
12976 */
12977 if (IS_GEN2(dev)) {
124abe07 12978 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
80715b2f
VS
12979 int vtotal;
12980
124abe07
VS
12981 vtotal = adjusted_mode->crtc_vtotal;
12982 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
80715b2f
VS
12983 vtotal /= 2;
12984
12985 crtc->scanline_offset = vtotal - 1;
12986 } else if (HAS_DDI(dev) &&
409ee761 12987 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
80715b2f
VS
12988 crtc->scanline_offset = 2;
12989 } else
12990 crtc->scanline_offset = 1;
12991}
12992
ad421372 12993static void intel_modeset_clear_plls(struct drm_atomic_state *state)
ed6739ef 12994{
225da59b 12995 struct drm_device *dev = state->dev;
ed6739ef 12996 struct drm_i915_private *dev_priv = to_i915(dev);
ad421372 12997 struct intel_shared_dpll_config *shared_dpll = NULL;
0a9ab303
ACO
12998 struct drm_crtc *crtc;
12999 struct drm_crtc_state *crtc_state;
0a9ab303 13000 int i;
ed6739ef
ACO
13001
13002 if (!dev_priv->display.crtc_compute_clock)
ad421372 13003 return;
ed6739ef 13004
0a9ab303 13005 for_each_crtc_in_state(state, crtc, crtc_state, i) {
fb1a38a9 13006 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8106ddbd
ACO
13007 struct intel_shared_dpll *old_dpll =
13008 to_intel_crtc_state(crtc->state)->shared_dpll;
0a9ab303 13009
fb1a38a9 13010 if (!needs_modeset(crtc_state))
225da59b
ACO
13011 continue;
13012
8106ddbd 13013 to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
fb1a38a9 13014
8106ddbd 13015 if (!old_dpll)
fb1a38a9 13016 continue;
0a9ab303 13017
ad421372
ML
13018 if (!shared_dpll)
13019 shared_dpll = intel_atomic_get_shared_dpll_state(state);
ed6739ef 13020
8106ddbd 13021 intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
ad421372 13022 }
ed6739ef
ACO
13023}
13024
99d736a2
ML
13025/*
13026 * This implements the workaround described in the "notes" section of the mode
13027 * set sequence documentation. When going from no pipes or single pipe to
13028 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13029 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13030 */
13031static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13032{
13033 struct drm_crtc_state *crtc_state;
13034 struct intel_crtc *intel_crtc;
13035 struct drm_crtc *crtc;
13036 struct intel_crtc_state *first_crtc_state = NULL;
13037 struct intel_crtc_state *other_crtc_state = NULL;
13038 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13039 int i;
13040
13041 /* look at all crtc's that are going to be enabled in during modeset */
13042 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13043 intel_crtc = to_intel_crtc(crtc);
13044
13045 if (!crtc_state->active || !needs_modeset(crtc_state))
13046 continue;
13047
13048 if (first_crtc_state) {
13049 other_crtc_state = to_intel_crtc_state(crtc_state);
13050 break;
13051 } else {
13052 first_crtc_state = to_intel_crtc_state(crtc_state);
13053 first_pipe = intel_crtc->pipe;
13054 }
13055 }
13056
13057 /* No workaround needed? */
13058 if (!first_crtc_state)
13059 return 0;
13060
13061 /* w/a possibly needed, check how many crtc's are already enabled. */
13062 for_each_intel_crtc(state->dev, intel_crtc) {
13063 struct intel_crtc_state *pipe_config;
13064
13065 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13066 if (IS_ERR(pipe_config))
13067 return PTR_ERR(pipe_config);
13068
13069 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13070
13071 if (!pipe_config->base.active ||
13072 needs_modeset(&pipe_config->base))
13073 continue;
13074
13075 /* 2 or more enabled crtcs means no need for w/a */
13076 if (enabled_pipe != INVALID_PIPE)
13077 return 0;
13078
13079 enabled_pipe = intel_crtc->pipe;
13080 }
13081
13082 if (enabled_pipe != INVALID_PIPE)
13083 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13084 else if (other_crtc_state)
13085 other_crtc_state->hsw_workaround_pipe = first_pipe;
13086
13087 return 0;
13088}
13089
27c329ed
ML
13090static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13091{
13092 struct drm_crtc *crtc;
13093 struct drm_crtc_state *crtc_state;
13094 int ret = 0;
13095
13096 /* add all active pipes to the state */
13097 for_each_crtc(state->dev, crtc) {
13098 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13099 if (IS_ERR(crtc_state))
13100 return PTR_ERR(crtc_state);
13101
13102 if (!crtc_state->active || needs_modeset(crtc_state))
13103 continue;
13104
13105 crtc_state->mode_changed = true;
13106
13107 ret = drm_atomic_add_affected_connectors(state, crtc);
13108 if (ret)
13109 break;
13110
13111 ret = drm_atomic_add_affected_planes(state, crtc);
13112 if (ret)
13113 break;
13114 }
13115
13116 return ret;
13117}
13118
c347a676 13119static int intel_modeset_checks(struct drm_atomic_state *state)
054518dd 13120{
565602d7
ML
13121 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13122 struct drm_i915_private *dev_priv = state->dev->dev_private;
13123 struct drm_crtc *crtc;
13124 struct drm_crtc_state *crtc_state;
13125 int ret = 0, i;
054518dd 13126
b359283a
ML
13127 if (!check_digital_port_conflicts(state)) {
13128 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13129 return -EINVAL;
13130 }
13131
565602d7
ML
13132 intel_state->modeset = true;
13133 intel_state->active_crtcs = dev_priv->active_crtcs;
13134
13135 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13136 if (crtc_state->active)
13137 intel_state->active_crtcs |= 1 << i;
13138 else
13139 intel_state->active_crtcs &= ~(1 << i);
13140 }
13141
054518dd
ACO
13142 /*
13143 * See if the config requires any additional preparation, e.g.
13144 * to adjust global state with pipes off. We need to do this
13145 * here so we can get the modeset_pipe updated config for the new
13146 * mode set on this crtc. For other crtcs we need to use the
13147 * adjusted_mode bits in the crtc directly.
13148 */
27c329ed 13149 if (dev_priv->display.modeset_calc_cdclk) {
27c329ed
ML
13150 ret = dev_priv->display.modeset_calc_cdclk(state);
13151
1a617b77 13152 if (!ret && intel_state->dev_cdclk != dev_priv->cdclk_freq)
27c329ed
ML
13153 ret = intel_modeset_all_pipes(state);
13154
13155 if (ret < 0)
054518dd 13156 return ret;
e8788cbc
ML
13157
13158 DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
13159 intel_state->cdclk, intel_state->dev_cdclk);
27c329ed 13160 } else
1a617b77 13161 to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
054518dd 13162
ad421372 13163 intel_modeset_clear_plls(state);
054518dd 13164
565602d7 13165 if (IS_HASWELL(dev_priv))
ad421372 13166 return haswell_mode_set_planes_workaround(state);
99d736a2 13167
ad421372 13168 return 0;
c347a676
ACO
13169}
13170
aa363136
MR
13171/*
13172 * Handle calculation of various watermark data at the end of the atomic check
13173 * phase. The code here should be run after the per-crtc and per-plane 'check'
13174 * handlers to ensure that all derived state has been updated.
13175 */
13176static void calc_watermark_data(struct drm_atomic_state *state)
13177{
13178 struct drm_device *dev = state->dev;
13179 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13180 struct drm_crtc *crtc;
13181 struct drm_crtc_state *cstate;
13182 struct drm_plane *plane;
13183 struct drm_plane_state *pstate;
13184
13185 /*
13186 * Calculate watermark configuration details now that derived
13187 * plane/crtc state is all properly updated.
13188 */
13189 drm_for_each_crtc(crtc, dev) {
13190 cstate = drm_atomic_get_existing_crtc_state(state, crtc) ?:
13191 crtc->state;
13192
13193 if (cstate->active)
13194 intel_state->wm_config.num_pipes_active++;
13195 }
13196 drm_for_each_legacy_plane(plane, dev) {
13197 pstate = drm_atomic_get_existing_plane_state(state, plane) ?:
13198 plane->state;
13199
13200 if (!to_intel_plane_state(pstate)->visible)
13201 continue;
13202
13203 intel_state->wm_config.sprites_enabled = true;
13204 if (pstate->crtc_w != pstate->src_w >> 16 ||
13205 pstate->crtc_h != pstate->src_h >> 16)
13206 intel_state->wm_config.sprites_scaled = true;
13207 }
13208}
13209
74c090b1
ML
13210/**
13211 * intel_atomic_check - validate state object
13212 * @dev: drm device
13213 * @state: state to validate
13214 */
13215static int intel_atomic_check(struct drm_device *dev,
13216 struct drm_atomic_state *state)
c347a676 13217{
dd8b3bdb 13218 struct drm_i915_private *dev_priv = to_i915(dev);
aa363136 13219 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
c347a676
ACO
13220 struct drm_crtc *crtc;
13221 struct drm_crtc_state *crtc_state;
13222 int ret, i;
61333b60 13223 bool any_ms = false;
c347a676 13224
74c090b1 13225 ret = drm_atomic_helper_check_modeset(dev, state);
054518dd
ACO
13226 if (ret)
13227 return ret;
13228
c347a676 13229 for_each_crtc_in_state(state, crtc, crtc_state, i) {
cfb23ed6
ML
13230 struct intel_crtc_state *pipe_config =
13231 to_intel_crtc_state(crtc_state);
1ed51de9
DV
13232
13233 /* Catch I915_MODE_FLAG_INHERITED */
13234 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
13235 crtc_state->mode_changed = true;
cfb23ed6 13236
61333b60
ML
13237 if (!crtc_state->enable) {
13238 if (needs_modeset(crtc_state))
13239 any_ms = true;
c347a676 13240 continue;
61333b60 13241 }
c347a676 13242
26495481 13243 if (!needs_modeset(crtc_state))
cfb23ed6
ML
13244 continue;
13245
26495481
DV
13246 /* FIXME: For only active_changed we shouldn't need to do any
13247 * state recomputation at all. */
13248
1ed51de9
DV
13249 ret = drm_atomic_add_affected_connectors(state, crtc);
13250 if (ret)
13251 return ret;
b359283a 13252
cfb23ed6 13253 ret = intel_modeset_pipe_config(crtc, pipe_config);
c347a676
ACO
13254 if (ret)
13255 return ret;
13256
73831236 13257 if (i915.fastboot &&
dd8b3bdb 13258 intel_pipe_config_compare(dev,
cfb23ed6 13259 to_intel_crtc_state(crtc->state),
1ed51de9 13260 pipe_config, true)) {
26495481 13261 crtc_state->mode_changed = false;
bfd16b2a 13262 to_intel_crtc_state(crtc_state)->update_pipe = true;
26495481
DV
13263 }
13264
13265 if (needs_modeset(crtc_state)) {
13266 any_ms = true;
cfb23ed6
ML
13267
13268 ret = drm_atomic_add_affected_planes(state, crtc);
13269 if (ret)
13270 return ret;
13271 }
61333b60 13272
26495481
DV
13273 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
13274 needs_modeset(crtc_state) ?
13275 "[modeset]" : "[fastset]");
c347a676
ACO
13276 }
13277
61333b60
ML
13278 if (any_ms) {
13279 ret = intel_modeset_checks(state);
13280
13281 if (ret)
13282 return ret;
27c329ed 13283 } else
dd8b3bdb 13284 intel_state->cdclk = dev_priv->cdclk_freq;
76305b1a 13285
dd8b3bdb 13286 ret = drm_atomic_helper_check_planes(dev, state);
aa363136
MR
13287 if (ret)
13288 return ret;
13289
f51be2e0 13290 intel_fbc_choose_crtc(dev_priv, state);
aa363136
MR
13291 calc_watermark_data(state);
13292
13293 return 0;
054518dd
ACO
13294}
13295
5008e874
ML
13296static int intel_atomic_prepare_commit(struct drm_device *dev,
13297 struct drm_atomic_state *state,
13298 bool async)
13299{
7580d774
ML
13300 struct drm_i915_private *dev_priv = dev->dev_private;
13301 struct drm_plane_state *plane_state;
5008e874 13302 struct drm_crtc_state *crtc_state;
7580d774 13303 struct drm_plane *plane;
5008e874
ML
13304 struct drm_crtc *crtc;
13305 int i, ret;
13306
13307 if (async) {
13308 DRM_DEBUG_KMS("i915 does not yet support async commit\n");
13309 return -EINVAL;
13310 }
13311
13312 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13313 ret = intel_crtc_wait_for_pending_flips(crtc);
13314 if (ret)
13315 return ret;
7580d774
ML
13316
13317 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
13318 flush_workqueue(dev_priv->wq);
5008e874
ML
13319 }
13320
f935675f
ML
13321 ret = mutex_lock_interruptible(&dev->struct_mutex);
13322 if (ret)
13323 return ret;
13324
5008e874 13325 ret = drm_atomic_helper_prepare_planes(dev, state);
7580d774
ML
13326 if (!ret && !async && !i915_reset_in_progress(&dev_priv->gpu_error)) {
13327 u32 reset_counter;
13328
13329 reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
13330 mutex_unlock(&dev->struct_mutex);
13331
13332 for_each_plane_in_state(state, plane, plane_state, i) {
13333 struct intel_plane_state *intel_plane_state =
13334 to_intel_plane_state(plane_state);
13335
13336 if (!intel_plane_state->wait_req)
13337 continue;
13338
13339 ret = __i915_wait_request(intel_plane_state->wait_req,
13340 reset_counter, true,
13341 NULL, NULL);
13342
13343 /* Swallow -EIO errors to allow updates during hw lockup. */
13344 if (ret == -EIO)
13345 ret = 0;
13346
13347 if (ret)
13348 break;
13349 }
13350
13351 if (!ret)
13352 return 0;
13353
13354 mutex_lock(&dev->struct_mutex);
13355 drm_atomic_helper_cleanup_planes(dev, state);
13356 }
5008e874 13357
f935675f 13358 mutex_unlock(&dev->struct_mutex);
5008e874
ML
13359 return ret;
13360}
13361
e8861675
ML
13362static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
13363 struct drm_i915_private *dev_priv,
13364 unsigned crtc_mask)
13365{
13366 unsigned last_vblank_count[I915_MAX_PIPES];
13367 enum pipe pipe;
13368 int ret;
13369
13370 if (!crtc_mask)
13371 return;
13372
13373 for_each_pipe(dev_priv, pipe) {
13374 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13375
13376 if (!((1 << pipe) & crtc_mask))
13377 continue;
13378
13379 ret = drm_crtc_vblank_get(crtc);
13380 if (WARN_ON(ret != 0)) {
13381 crtc_mask &= ~(1 << pipe);
13382 continue;
13383 }
13384
13385 last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
13386 }
13387
13388 for_each_pipe(dev_priv, pipe) {
13389 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13390 long lret;
13391
13392 if (!((1 << pipe) & crtc_mask))
13393 continue;
13394
13395 lret = wait_event_timeout(dev->vblank[pipe].queue,
13396 last_vblank_count[pipe] !=
13397 drm_crtc_vblank_count(crtc),
13398 msecs_to_jiffies(50));
13399
13400 WARN_ON(!lret);
13401
13402 drm_crtc_vblank_put(crtc);
13403 }
13404}
13405
13406static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
13407{
13408 /* fb updated, need to unpin old fb */
13409 if (crtc_state->fb_changed)
13410 return true;
13411
13412 /* wm changes, need vblank before final wm's */
caed361d 13413 if (crtc_state->update_wm_post)
e8861675
ML
13414 return true;
13415
13416 /*
13417 * cxsr is re-enabled after vblank.
caed361d 13418 * This is already handled by crtc_state->update_wm_post,
e8861675
ML
13419 * but added for clarity.
13420 */
13421 if (crtc_state->disable_cxsr)
13422 return true;
13423
13424 return false;
13425}
13426
74c090b1
ML
13427/**
13428 * intel_atomic_commit - commit validated state object
13429 * @dev: DRM device
13430 * @state: the top-level driver state object
13431 * @async: asynchronous commit
13432 *
13433 * This function commits a top-level state object that has been validated
13434 * with drm_atomic_helper_check().
13435 *
13436 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
13437 * we can only handle plane-related operations and do not yet support
13438 * asynchronous commit.
13439 *
13440 * RETURNS
13441 * Zero for success or -errno.
13442 */
13443static int intel_atomic_commit(struct drm_device *dev,
13444 struct drm_atomic_state *state,
13445 bool async)
a6778b3c 13446{
565602d7 13447 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fbee40df 13448 struct drm_i915_private *dev_priv = dev->dev_private;
29ceb0e6 13449 struct drm_crtc_state *old_crtc_state;
7580d774 13450 struct drm_crtc *crtc;
ed4a6a7c 13451 struct intel_crtc_state *intel_cstate;
565602d7
ML
13452 int ret = 0, i;
13453 bool hw_check = intel_state->modeset;
33c8df89 13454 unsigned long put_domains[I915_MAX_PIPES] = {};
e8861675 13455 unsigned crtc_vblank_mask = 0;
a6778b3c 13456
5008e874 13457 ret = intel_atomic_prepare_commit(dev, state, async);
7580d774
ML
13458 if (ret) {
13459 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
d4afb8cc 13460 return ret;
7580d774 13461 }
d4afb8cc 13462
1c5e19f8 13463 drm_atomic_helper_swap_state(dev, state);
a1475e77
ML
13464 dev_priv->wm.config = intel_state->wm_config;
13465 intel_shared_dpll_commit(state);
1c5e19f8 13466
565602d7
ML
13467 if (intel_state->modeset) {
13468 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
13469 sizeof(intel_state->min_pixclk));
13470 dev_priv->active_crtcs = intel_state->active_crtcs;
1a617b77 13471 dev_priv->atomic_cdclk_freq = intel_state->cdclk;
33c8df89
ML
13472
13473 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
565602d7
ML
13474 }
13475
29ceb0e6 13476 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
a539205a
ML
13477 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13478
33c8df89
ML
13479 if (needs_modeset(crtc->state) ||
13480 to_intel_crtc_state(crtc->state)->update_pipe) {
13481 hw_check = true;
13482
13483 put_domains[to_intel_crtc(crtc)->pipe] =
13484 modeset_get_crtc_power_domains(crtc,
13485 to_intel_crtc_state(crtc->state));
13486 }
13487
61333b60
ML
13488 if (!needs_modeset(crtc->state))
13489 continue;
13490
29ceb0e6 13491 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
460da916 13492
29ceb0e6
VS
13493 if (old_crtc_state->active) {
13494 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
a539205a 13495 dev_priv->display.crtc_disable(crtc);
eddfcbcd 13496 intel_crtc->active = false;
58f9c0bc 13497 intel_fbc_disable(intel_crtc);
eddfcbcd 13498 intel_disable_shared_dpll(intel_crtc);
9bbc8258
VS
13499
13500 /*
13501 * Underruns don't always raise
13502 * interrupts, so check manually.
13503 */
13504 intel_check_cpu_fifo_underruns(dev_priv);
13505 intel_check_pch_fifo_underruns(dev_priv);
b9001114
ML
13506
13507 if (!crtc->state->active)
13508 intel_update_watermarks(crtc);
a539205a 13509 }
b8cecdf5 13510 }
7758a113 13511
ea9d758d
DV
13512 /* Only after disabling all output pipelines that will be changed can we
13513 * update the the output configuration. */
4740b0f2 13514 intel_modeset_update_crtc_state(state);
f6e5b160 13515
565602d7 13516 if (intel_state->modeset) {
4740b0f2 13517 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
33c8df89
ML
13518
13519 if (dev_priv->display.modeset_commit_cdclk &&
13520 intel_state->dev_cdclk != dev_priv->cdclk_freq)
13521 dev_priv->display.modeset_commit_cdclk(state);
4740b0f2 13522 }
47fab737 13523
a6778b3c 13524 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
29ceb0e6 13525 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
f6ac4b2a
ML
13526 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13527 bool modeset = needs_modeset(crtc->state);
e8861675
ML
13528 struct intel_crtc_state *pipe_config =
13529 to_intel_crtc_state(crtc->state);
13530 bool update_pipe = !modeset && pipe_config->update_pipe;
9f836f90 13531
f6ac4b2a 13532 if (modeset && crtc->state->active) {
a539205a
ML
13533 update_scanline_offset(to_intel_crtc(crtc));
13534 dev_priv->display.crtc_enable(crtc);
13535 }
80715b2f 13536
82cf435b
LL
13537 if (!modeset &&
13538 crtc->state->active &&
13539 crtc->state->color_mgmt_changed) {
13540 /*
13541 * Only update color management when not doing
13542 * a modeset as this will be done by
13543 * crtc_enable already.
13544 */
13545 intel_color_set_csc(crtc);
13546 intel_color_load_luts(crtc);
13547 }
13548
f6ac4b2a 13549 if (!modeset)
29ceb0e6 13550 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
f6ac4b2a 13551
31ae71fc
ML
13552 if (crtc->state->active &&
13553 drm_atomic_get_existing_plane_state(state, crtc->primary))
49227c4a
PZ
13554 intel_fbc_enable(intel_crtc);
13555
6173ee28
ML
13556 if (crtc->state->active &&
13557 (crtc->state->planes_changed || update_pipe))
29ceb0e6 13558 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
bfd16b2a 13559
e8861675
ML
13560 if (pipe_config->base.active && needs_vblank_wait(pipe_config))
13561 crtc_vblank_mask |= 1 << i;
80715b2f 13562 }
a6778b3c 13563
a6778b3c 13564 /* FIXME: add subpixel order */
83a57153 13565
e8861675
ML
13566 if (!state->legacy_cursor_update)
13567 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
f935675f 13568
ed4a6a7c
MR
13569 /*
13570 * Now that the vblank has passed, we can go ahead and program the
13571 * optimal watermarks on platforms that need two-step watermark
13572 * programming.
13573 *
13574 * TODO: Move this (and other cleanup) to an async worker eventually.
13575 */
29ceb0e6 13576 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
ed4a6a7c
MR
13577 intel_cstate = to_intel_crtc_state(crtc->state);
13578
13579 if (dev_priv->display.optimize_watermarks)
13580 dev_priv->display.optimize_watermarks(intel_cstate);
13581 }
13582
177246a8
MR
13583 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
13584 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
13585
13586 if (put_domains[i])
13587 modeset_put_power_domains(dev_priv, put_domains[i]);
13588 }
13589
13590 if (intel_state->modeset)
13591 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
13592
f935675f 13593 mutex_lock(&dev->struct_mutex);
d4afb8cc 13594 drm_atomic_helper_cleanup_planes(dev, state);
f935675f 13595 mutex_unlock(&dev->struct_mutex);
2bfb4627 13596
565602d7 13597 if (hw_check)
ee165b1a
ML
13598 intel_modeset_check_state(dev, state);
13599
13600 drm_atomic_state_free(state);
f30da187 13601
75714940
MK
13602 /* As one of the primary mmio accessors, KMS has a high likelihood
13603 * of triggering bugs in unclaimed access. After we finish
13604 * modesetting, see if an error has been flagged, and if so
13605 * enable debugging for the next modeset - and hope we catch
13606 * the culprit.
13607 *
13608 * XXX note that we assume display power is on at this point.
13609 * This might hold true now but we need to add pm helper to check
13610 * unclaimed only when the hardware is on, as atomic commits
13611 * can happen also when the device is completely off.
13612 */
13613 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
13614
74c090b1 13615 return 0;
7f27126e
JB
13616}
13617
c0c36b94
CW
13618void intel_crtc_restore_mode(struct drm_crtc *crtc)
13619{
83a57153
ACO
13620 struct drm_device *dev = crtc->dev;
13621 struct drm_atomic_state *state;
e694eb02 13622 struct drm_crtc_state *crtc_state;
2bfb4627 13623 int ret;
83a57153
ACO
13624
13625 state = drm_atomic_state_alloc(dev);
13626 if (!state) {
e694eb02 13627 DRM_DEBUG_KMS("[CRTC:%d] crtc restore failed, out of memory",
83a57153
ACO
13628 crtc->base.id);
13629 return;
13630 }
13631
e694eb02 13632 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
83a57153 13633
e694eb02
ML
13634retry:
13635 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13636 ret = PTR_ERR_OR_ZERO(crtc_state);
13637 if (!ret) {
13638 if (!crtc_state->active)
13639 goto out;
83a57153 13640
e694eb02 13641 crtc_state->mode_changed = true;
74c090b1 13642 ret = drm_atomic_commit(state);
83a57153
ACO
13643 }
13644
e694eb02
ML
13645 if (ret == -EDEADLK) {
13646 drm_atomic_state_clear(state);
13647 drm_modeset_backoff(state->acquire_ctx);
13648 goto retry;
4ed9fb37 13649 }
4be07317 13650
2bfb4627 13651 if (ret)
e694eb02 13652out:
2bfb4627 13653 drm_atomic_state_free(state);
c0c36b94
CW
13654}
13655
25c5b266
DV
13656#undef for_each_intel_crtc_masked
13657
f6e5b160 13658static const struct drm_crtc_funcs intel_crtc_funcs = {
82cf435b 13659 .gamma_set = drm_atomic_helper_legacy_gamma_set,
74c090b1 13660 .set_config = drm_atomic_helper_set_config,
82cf435b 13661 .set_property = drm_atomic_helper_crtc_set_property,
f6e5b160
CW
13662 .destroy = intel_crtc_destroy,
13663 .page_flip = intel_crtc_page_flip,
1356837e
MR
13664 .atomic_duplicate_state = intel_crtc_duplicate_state,
13665 .atomic_destroy_state = intel_crtc_destroy_state,
f6e5b160
CW
13666};
13667
6beb8c23
MR
13668/**
13669 * intel_prepare_plane_fb - Prepare fb for usage on plane
13670 * @plane: drm plane to prepare for
13671 * @fb: framebuffer to prepare for presentation
13672 *
13673 * Prepares a framebuffer for usage on a display plane. Generally this
13674 * involves pinning the underlying object and updating the frontbuffer tracking
13675 * bits. Some older platforms need special physical address handling for
13676 * cursor planes.
13677 *
f935675f
ML
13678 * Must be called with struct_mutex held.
13679 *
6beb8c23
MR
13680 * Returns 0 on success, negative error code on failure.
13681 */
13682int
13683intel_prepare_plane_fb(struct drm_plane *plane,
d136dfee 13684 const struct drm_plane_state *new_state)
465c120c
MR
13685{
13686 struct drm_device *dev = plane->dev;
844f9111 13687 struct drm_framebuffer *fb = new_state->fb;
6beb8c23 13688 struct intel_plane *intel_plane = to_intel_plane(plane);
6beb8c23 13689 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
1ee49399 13690 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
6beb8c23 13691 int ret = 0;
465c120c 13692
1ee49399 13693 if (!obj && !old_obj)
465c120c
MR
13694 return 0;
13695
5008e874
ML
13696 if (old_obj) {
13697 struct drm_crtc_state *crtc_state =
13698 drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
13699
13700 /* Big Hammer, we also need to ensure that any pending
13701 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
13702 * current scanout is retired before unpinning the old
13703 * framebuffer. Note that we rely on userspace rendering
13704 * into the buffer attached to the pipe they are waiting
13705 * on. If not, userspace generates a GPU hang with IPEHR
13706 * point to the MI_WAIT_FOR_EVENT.
13707 *
13708 * This should only fail upon a hung GPU, in which case we
13709 * can safely continue.
13710 */
13711 if (needs_modeset(crtc_state))
13712 ret = i915_gem_object_wait_rendering(old_obj, true);
13713
13714 /* Swallow -EIO errors to allow updates during hw lockup. */
13715 if (ret && ret != -EIO)
f935675f 13716 return ret;
5008e874
ML
13717 }
13718
3c28ff22
AG
13719 /* For framebuffer backed by dmabuf, wait for fence */
13720 if (obj && obj->base.dma_buf) {
bcf8be27
ML
13721 long lret;
13722
13723 lret = reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
13724 false, true,
13725 MAX_SCHEDULE_TIMEOUT);
13726 if (lret == -ERESTARTSYS)
13727 return lret;
3c28ff22 13728
bcf8be27 13729 WARN(lret < 0, "waiting returns %li\n", lret);
3c28ff22
AG
13730 }
13731
1ee49399
ML
13732 if (!obj) {
13733 ret = 0;
13734 } else if (plane->type == DRM_PLANE_TYPE_CURSOR &&
6beb8c23
MR
13735 INTEL_INFO(dev)->cursor_needs_physical) {
13736 int align = IS_I830(dev) ? 16 * 1024 : 256;
13737 ret = i915_gem_object_attach_phys(obj, align);
13738 if (ret)
13739 DRM_DEBUG_KMS("failed to attach phys object\n");
13740 } else {
3465c580 13741 ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
6beb8c23 13742 }
465c120c 13743
7580d774
ML
13744 if (ret == 0) {
13745 if (obj) {
13746 struct intel_plane_state *plane_state =
13747 to_intel_plane_state(new_state);
13748
13749 i915_gem_request_assign(&plane_state->wait_req,
13750 obj->last_write_req);
13751 }
13752
a9ff8714 13753 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
7580d774 13754 }
fdd508a6 13755
6beb8c23
MR
13756 return ret;
13757}
13758
38f3ce3a
MR
13759/**
13760 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13761 * @plane: drm plane to clean up for
13762 * @fb: old framebuffer that was on plane
13763 *
13764 * Cleans up a framebuffer that has just been removed from a plane.
f935675f
ML
13765 *
13766 * Must be called with struct_mutex held.
38f3ce3a
MR
13767 */
13768void
13769intel_cleanup_plane_fb(struct drm_plane *plane,
d136dfee 13770 const struct drm_plane_state *old_state)
38f3ce3a
MR
13771{
13772 struct drm_device *dev = plane->dev;
1ee49399 13773 struct intel_plane *intel_plane = to_intel_plane(plane);
7580d774 13774 struct intel_plane_state *old_intel_state;
1ee49399
ML
13775 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
13776 struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
38f3ce3a 13777
7580d774
ML
13778 old_intel_state = to_intel_plane_state(old_state);
13779
1ee49399 13780 if (!obj && !old_obj)
38f3ce3a
MR
13781 return;
13782
1ee49399
ML
13783 if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
13784 !INTEL_INFO(dev)->cursor_needs_physical))
3465c580 13785 intel_unpin_fb_obj(old_state->fb, old_state->rotation);
1ee49399
ML
13786
13787 /* prepare_fb aborted? */
13788 if ((old_obj && (old_obj->frontbuffer_bits & intel_plane->frontbuffer_bit)) ||
13789 (obj && !(obj->frontbuffer_bits & intel_plane->frontbuffer_bit)))
13790 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
7580d774
ML
13791
13792 i915_gem_request_assign(&old_intel_state->wait_req, NULL);
465c120c
MR
13793}
13794
6156a456
CK
13795int
13796skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13797{
13798 int max_scale;
13799 struct drm_device *dev;
13800 struct drm_i915_private *dev_priv;
13801 int crtc_clock, cdclk;
13802
bf8a0af0 13803 if (!intel_crtc || !crtc_state->base.enable)
6156a456
CK
13804 return DRM_PLANE_HELPER_NO_SCALING;
13805
13806 dev = intel_crtc->base.dev;
13807 dev_priv = dev->dev_private;
13808 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
27c329ed 13809 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
6156a456 13810
54bf1ce6 13811 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
6156a456
CK
13812 return DRM_PLANE_HELPER_NO_SCALING;
13813
13814 /*
13815 * skl max scale is lower of:
13816 * close to 3 but not 3, -1 is for that purpose
13817 * or
13818 * cdclk/crtc_clock
13819 */
13820 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13821
13822 return max_scale;
13823}
13824
465c120c 13825static int
3c692a41 13826intel_check_primary_plane(struct drm_plane *plane,
061e4b8d 13827 struct intel_crtc_state *crtc_state,
3c692a41
GP
13828 struct intel_plane_state *state)
13829{
2b875c22
MR
13830 struct drm_crtc *crtc = state->base.crtc;
13831 struct drm_framebuffer *fb = state->base.fb;
6156a456 13832 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
061e4b8d
ML
13833 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13834 bool can_position = false;
465c120c 13835
693bdc28
VS
13836 if (INTEL_INFO(plane->dev)->gen >= 9) {
13837 /* use scaler when colorkey is not required */
13838 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
13839 min_scale = 1;
13840 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
13841 }
d8106366 13842 can_position = true;
6156a456 13843 }
d8106366 13844
061e4b8d
ML
13845 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13846 &state->dst, &state->clip,
da20eabd
ML
13847 min_scale, max_scale,
13848 can_position, true,
13849 &state->visible);
14af293f
GP
13850}
13851
613d2b27
ML
13852static void intel_begin_crtc_commit(struct drm_crtc *crtc,
13853 struct drm_crtc_state *old_crtc_state)
3c692a41 13854{
32b7eeec 13855 struct drm_device *dev = crtc->dev;
3c692a41 13856 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
bfd16b2a
ML
13857 struct intel_crtc_state *old_intel_state =
13858 to_intel_crtc_state(old_crtc_state);
13859 bool modeset = needs_modeset(crtc->state);
3c692a41 13860
c34c9ee4 13861 /* Perform vblank evasion around commit operation */
62852622 13862 intel_pipe_update_start(intel_crtc);
0583236e 13863
bfd16b2a
ML
13864 if (modeset)
13865 return;
13866
13867 if (to_intel_crtc_state(crtc->state)->update_pipe)
13868 intel_update_pipe_config(intel_crtc, old_intel_state);
13869 else if (INTEL_INFO(dev)->gen >= 9)
0583236e 13870 skl_detach_scalers(intel_crtc);
32b7eeec
MR
13871}
13872
613d2b27
ML
13873static void intel_finish_crtc_commit(struct drm_crtc *crtc,
13874 struct drm_crtc_state *old_crtc_state)
32b7eeec 13875{
32b7eeec 13876 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
32b7eeec 13877
62852622 13878 intel_pipe_update_end(intel_crtc);
3c692a41
GP
13879}
13880
cf4c7c12 13881/**
4a3b8769
MR
13882 * intel_plane_destroy - destroy a plane
13883 * @plane: plane to destroy
cf4c7c12 13884 *
4a3b8769
MR
13885 * Common destruction function for all types of planes (primary, cursor,
13886 * sprite).
cf4c7c12 13887 */
4a3b8769 13888void intel_plane_destroy(struct drm_plane *plane)
465c120c
MR
13889{
13890 struct intel_plane *intel_plane = to_intel_plane(plane);
13891 drm_plane_cleanup(plane);
13892 kfree(intel_plane);
13893}
13894
65a3fea0 13895const struct drm_plane_funcs intel_plane_funcs = {
70a101f8
MR
13896 .update_plane = drm_atomic_helper_update_plane,
13897 .disable_plane = drm_atomic_helper_disable_plane,
3d7d6510 13898 .destroy = intel_plane_destroy,
c196e1d6 13899 .set_property = drm_atomic_helper_plane_set_property,
a98b3431
MR
13900 .atomic_get_property = intel_plane_atomic_get_property,
13901 .atomic_set_property = intel_plane_atomic_set_property,
ea2c67bb
MR
13902 .atomic_duplicate_state = intel_plane_duplicate_state,
13903 .atomic_destroy_state = intel_plane_destroy_state,
13904
465c120c
MR
13905};
13906
13907static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13908 int pipe)
13909{
13910 struct intel_plane *primary;
8e7d688b 13911 struct intel_plane_state *state;
465c120c 13912 const uint32_t *intel_primary_formats;
45e3743a 13913 unsigned int num_formats;
465c120c
MR
13914
13915 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13916 if (primary == NULL)
13917 return NULL;
13918
8e7d688b
MR
13919 state = intel_create_plane_state(&primary->base);
13920 if (!state) {
ea2c67bb
MR
13921 kfree(primary);
13922 return NULL;
13923 }
8e7d688b 13924 primary->base.state = &state->base;
ea2c67bb 13925
465c120c
MR
13926 primary->can_scale = false;
13927 primary->max_downscale = 1;
6156a456
CK
13928 if (INTEL_INFO(dev)->gen >= 9) {
13929 primary->can_scale = true;
af99ceda 13930 state->scaler_id = -1;
6156a456 13931 }
465c120c
MR
13932 primary->pipe = pipe;
13933 primary->plane = pipe;
a9ff8714 13934 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
c59cb179 13935 primary->check_plane = intel_check_primary_plane;
465c120c
MR
13936 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13937 primary->plane = !pipe;
13938
6c0fd451
DL
13939 if (INTEL_INFO(dev)->gen >= 9) {
13940 intel_primary_formats = skl_primary_formats;
13941 num_formats = ARRAY_SIZE(skl_primary_formats);
a8d201af
ML
13942
13943 primary->update_plane = skylake_update_primary_plane;
13944 primary->disable_plane = skylake_disable_primary_plane;
13945 } else if (HAS_PCH_SPLIT(dev)) {
13946 intel_primary_formats = i965_primary_formats;
13947 num_formats = ARRAY_SIZE(i965_primary_formats);
13948
13949 primary->update_plane = ironlake_update_primary_plane;
13950 primary->disable_plane = i9xx_disable_primary_plane;
6c0fd451 13951 } else if (INTEL_INFO(dev)->gen >= 4) {
568db4f2
DL
13952 intel_primary_formats = i965_primary_formats;
13953 num_formats = ARRAY_SIZE(i965_primary_formats);
a8d201af
ML
13954
13955 primary->update_plane = i9xx_update_primary_plane;
13956 primary->disable_plane = i9xx_disable_primary_plane;
6c0fd451
DL
13957 } else {
13958 intel_primary_formats = i8xx_primary_formats;
13959 num_formats = ARRAY_SIZE(i8xx_primary_formats);
a8d201af
ML
13960
13961 primary->update_plane = i9xx_update_primary_plane;
13962 primary->disable_plane = i9xx_disable_primary_plane;
465c120c
MR
13963 }
13964
13965 drm_universal_plane_init(dev, &primary->base, 0,
65a3fea0 13966 &intel_plane_funcs,
465c120c 13967 intel_primary_formats, num_formats,
b0b3b795 13968 DRM_PLANE_TYPE_PRIMARY, NULL);
48404c1e 13969
3b7a5119
SJ
13970 if (INTEL_INFO(dev)->gen >= 4)
13971 intel_create_rotation_property(dev, primary);
48404c1e 13972
ea2c67bb
MR
13973 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13974
465c120c
MR
13975 return &primary->base;
13976}
13977
3b7a5119
SJ
13978void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
13979{
13980 if (!dev->mode_config.rotation_property) {
13981 unsigned long flags = BIT(DRM_ROTATE_0) |
13982 BIT(DRM_ROTATE_180);
13983
13984 if (INTEL_INFO(dev)->gen >= 9)
13985 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
13986
13987 dev->mode_config.rotation_property =
13988 drm_mode_create_rotation_property(dev, flags);
13989 }
13990 if (dev->mode_config.rotation_property)
13991 drm_object_attach_property(&plane->base.base,
13992 dev->mode_config.rotation_property,
13993 plane->base.state->rotation);
13994}
13995
3d7d6510 13996static int
852e787c 13997intel_check_cursor_plane(struct drm_plane *plane,
061e4b8d 13998 struct intel_crtc_state *crtc_state,
852e787c 13999 struct intel_plane_state *state)
3d7d6510 14000{
061e4b8d 14001 struct drm_crtc *crtc = crtc_state->base.crtc;
2b875c22 14002 struct drm_framebuffer *fb = state->base.fb;
757f9a3e 14003 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
b29ec92c 14004 enum pipe pipe = to_intel_plane(plane)->pipe;
757f9a3e
GP
14005 unsigned stride;
14006 int ret;
3d7d6510 14007
061e4b8d
ML
14008 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14009 &state->dst, &state->clip,
3d7d6510
MR
14010 DRM_PLANE_HELPER_NO_SCALING,
14011 DRM_PLANE_HELPER_NO_SCALING,
852e787c 14012 true, true, &state->visible);
757f9a3e
GP
14013 if (ret)
14014 return ret;
14015
757f9a3e
GP
14016 /* if we want to turn off the cursor ignore width and height */
14017 if (!obj)
da20eabd 14018 return 0;
757f9a3e 14019
757f9a3e 14020 /* Check for which cursor types we support */
061e4b8d 14021 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
ea2c67bb
MR
14022 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
14023 state->base.crtc_w, state->base.crtc_h);
757f9a3e
GP
14024 return -EINVAL;
14025 }
14026
ea2c67bb
MR
14027 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
14028 if (obj->base.size < stride * state->base.crtc_h) {
757f9a3e
GP
14029 DRM_DEBUG_KMS("buffer is too small\n");
14030 return -ENOMEM;
14031 }
14032
3a656b54 14033 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
757f9a3e 14034 DRM_DEBUG_KMS("cursor cannot be tiled\n");
da20eabd 14035 return -EINVAL;
32b7eeec
MR
14036 }
14037
b29ec92c
VS
14038 /*
14039 * There's something wrong with the cursor on CHV pipe C.
14040 * If it straddles the left edge of the screen then
14041 * moving it away from the edge or disabling it often
14042 * results in a pipe underrun, and often that can lead to
14043 * dead pipe (constant underrun reported, and it scans
14044 * out just a solid color). To recover from that, the
14045 * display power well must be turned off and on again.
14046 * Refuse the put the cursor into that compromised position.
14047 */
14048 if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
14049 state->visible && state->base.crtc_x < 0) {
14050 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
14051 return -EINVAL;
14052 }
14053
da20eabd 14054 return 0;
852e787c 14055}
3d7d6510 14056
a8ad0d8e
ML
14057static void
14058intel_disable_cursor_plane(struct drm_plane *plane,
7fabf5ef 14059 struct drm_crtc *crtc)
a8ad0d8e 14060{
f2858021
ML
14061 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14062
14063 intel_crtc->cursor_addr = 0;
55a08b3f 14064 intel_crtc_update_cursor(crtc, NULL);
a8ad0d8e
ML
14065}
14066
f4a2cf29 14067static void
55a08b3f
ML
14068intel_update_cursor_plane(struct drm_plane *plane,
14069 const struct intel_crtc_state *crtc_state,
14070 const struct intel_plane_state *state)
852e787c 14071{
55a08b3f
ML
14072 struct drm_crtc *crtc = crtc_state->base.crtc;
14073 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ea2c67bb 14074 struct drm_device *dev = plane->dev;
2b875c22 14075 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
a912f12f 14076 uint32_t addr;
852e787c 14077
f4a2cf29 14078 if (!obj)
a912f12f 14079 addr = 0;
f4a2cf29 14080 else if (!INTEL_INFO(dev)->cursor_needs_physical)
a912f12f 14081 addr = i915_gem_obj_ggtt_offset(obj);
f4a2cf29 14082 else
a912f12f 14083 addr = obj->phys_handle->busaddr;
852e787c 14084
a912f12f 14085 intel_crtc->cursor_addr = addr;
55a08b3f 14086 intel_crtc_update_cursor(crtc, state);
852e787c
GP
14087}
14088
3d7d6510
MR
14089static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14090 int pipe)
14091{
14092 struct intel_plane *cursor;
8e7d688b 14093 struct intel_plane_state *state;
3d7d6510
MR
14094
14095 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
14096 if (cursor == NULL)
14097 return NULL;
14098
8e7d688b
MR
14099 state = intel_create_plane_state(&cursor->base);
14100 if (!state) {
ea2c67bb
MR
14101 kfree(cursor);
14102 return NULL;
14103 }
8e7d688b 14104 cursor->base.state = &state->base;
ea2c67bb 14105
3d7d6510
MR
14106 cursor->can_scale = false;
14107 cursor->max_downscale = 1;
14108 cursor->pipe = pipe;
14109 cursor->plane = pipe;
a9ff8714 14110 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
c59cb179 14111 cursor->check_plane = intel_check_cursor_plane;
55a08b3f 14112 cursor->update_plane = intel_update_cursor_plane;
a8ad0d8e 14113 cursor->disable_plane = intel_disable_cursor_plane;
3d7d6510
MR
14114
14115 drm_universal_plane_init(dev, &cursor->base, 0,
65a3fea0 14116 &intel_plane_funcs,
3d7d6510
MR
14117 intel_cursor_formats,
14118 ARRAY_SIZE(intel_cursor_formats),
b0b3b795 14119 DRM_PLANE_TYPE_CURSOR, NULL);
4398ad45
VS
14120
14121 if (INTEL_INFO(dev)->gen >= 4) {
14122 if (!dev->mode_config.rotation_property)
14123 dev->mode_config.rotation_property =
14124 drm_mode_create_rotation_property(dev,
14125 BIT(DRM_ROTATE_0) |
14126 BIT(DRM_ROTATE_180));
14127 if (dev->mode_config.rotation_property)
14128 drm_object_attach_property(&cursor->base.base,
14129 dev->mode_config.rotation_property,
8e7d688b 14130 state->base.rotation);
4398ad45
VS
14131 }
14132
af99ceda
CK
14133 if (INTEL_INFO(dev)->gen >=9)
14134 state->scaler_id = -1;
14135
ea2c67bb
MR
14136 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14137
3d7d6510
MR
14138 return &cursor->base;
14139}
14140
549e2bfb
CK
14141static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14142 struct intel_crtc_state *crtc_state)
14143{
14144 int i;
14145 struct intel_scaler *intel_scaler;
14146 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14147
14148 for (i = 0; i < intel_crtc->num_scalers; i++) {
14149 intel_scaler = &scaler_state->scalers[i];
14150 intel_scaler->in_use = 0;
549e2bfb
CK
14151 intel_scaler->mode = PS_SCALER_MODE_DYN;
14152 }
14153
14154 scaler_state->scaler_id = -1;
14155}
14156
b358d0a6 14157static void intel_crtc_init(struct drm_device *dev, int pipe)
79e53945 14158{
fbee40df 14159 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 14160 struct intel_crtc *intel_crtc;
f5de6e07 14161 struct intel_crtc_state *crtc_state = NULL;
3d7d6510
MR
14162 struct drm_plane *primary = NULL;
14163 struct drm_plane *cursor = NULL;
8563b1e8 14164 int ret;
79e53945 14165
955382f3 14166 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
79e53945
JB
14167 if (intel_crtc == NULL)
14168 return;
14169
f5de6e07
ACO
14170 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14171 if (!crtc_state)
14172 goto fail;
550acefd
ACO
14173 intel_crtc->config = crtc_state;
14174 intel_crtc->base.state = &crtc_state->base;
07878248 14175 crtc_state->base.crtc = &intel_crtc->base;
f5de6e07 14176
549e2bfb
CK
14177 /* initialize shared scalers */
14178 if (INTEL_INFO(dev)->gen >= 9) {
14179 if (pipe == PIPE_C)
14180 intel_crtc->num_scalers = 1;
14181 else
14182 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14183
14184 skl_init_scalers(dev, intel_crtc, crtc_state);
14185 }
14186
465c120c 14187 primary = intel_primary_plane_create(dev, pipe);
3d7d6510
MR
14188 if (!primary)
14189 goto fail;
14190
14191 cursor = intel_cursor_plane_create(dev, pipe);
14192 if (!cursor)
14193 goto fail;
14194
465c120c 14195 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
f9882876 14196 cursor, &intel_crtc_funcs, NULL);
3d7d6510
MR
14197 if (ret)
14198 goto fail;
79e53945 14199
1f1c2e24
VS
14200 /*
14201 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
8c0f92e1 14202 * is hooked to pipe B. Hence we want plane A feeding pipe B.
1f1c2e24 14203 */
80824003
JB
14204 intel_crtc->pipe = pipe;
14205 intel_crtc->plane = pipe;
3a77c4c4 14206 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
28c97730 14207 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
e2e767ab 14208 intel_crtc->plane = !pipe;
80824003
JB
14209 }
14210
4b0e333e
CW
14211 intel_crtc->cursor_base = ~0;
14212 intel_crtc->cursor_cntl = ~0;
dc41c154 14213 intel_crtc->cursor_size = ~0;
8d7849db 14214
852eb00d
VS
14215 intel_crtc->wm.cxsr_allowed = true;
14216
22fd0fab
JB
14217 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14218 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14219 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14220 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14221
79e53945 14222 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
87b6b101 14223
8563b1e8
LL
14224 intel_color_init(&intel_crtc->base);
14225
87b6b101 14226 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
3d7d6510
MR
14227 return;
14228
14229fail:
14230 if (primary)
14231 drm_plane_cleanup(primary);
14232 if (cursor)
14233 drm_plane_cleanup(cursor);
f5de6e07 14234 kfree(crtc_state);
3d7d6510 14235 kfree(intel_crtc);
79e53945
JB
14236}
14237
752aa88a
JB
14238enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14239{
14240 struct drm_encoder *encoder = connector->base.encoder;
6e9f798d 14241 struct drm_device *dev = connector->base.dev;
752aa88a 14242
51fd371b 14243 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
752aa88a 14244
d3babd3f 14245 if (!encoder || WARN_ON(!encoder->crtc))
752aa88a
JB
14246 return INVALID_PIPE;
14247
14248 return to_intel_crtc(encoder->crtc)->pipe;
14249}
14250
08d7b3d1 14251int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 14252 struct drm_file *file)
08d7b3d1 14253{
08d7b3d1 14254 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7707e653 14255 struct drm_crtc *drmmode_crtc;
c05422d5 14256 struct intel_crtc *crtc;
08d7b3d1 14257
7707e653 14258 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
08d7b3d1 14259
7707e653 14260 if (!drmmode_crtc) {
08d7b3d1 14261 DRM_ERROR("no such CRTC id\n");
3f2c2057 14262 return -ENOENT;
08d7b3d1
CW
14263 }
14264
7707e653 14265 crtc = to_intel_crtc(drmmode_crtc);
c05422d5 14266 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 14267
c05422d5 14268 return 0;
08d7b3d1
CW
14269}
14270
66a9278e 14271static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 14272{
66a9278e
DV
14273 struct drm_device *dev = encoder->base.dev;
14274 struct intel_encoder *source_encoder;
79e53945 14275 int index_mask = 0;
79e53945
JB
14276 int entry = 0;
14277
b2784e15 14278 for_each_intel_encoder(dev, source_encoder) {
bc079e8b 14279 if (encoders_cloneable(encoder, source_encoder))
66a9278e
DV
14280 index_mask |= (1 << entry);
14281
79e53945
JB
14282 entry++;
14283 }
4ef69c7a 14284
79e53945
JB
14285 return index_mask;
14286}
14287
4d302442
CW
14288static bool has_edp_a(struct drm_device *dev)
14289{
14290 struct drm_i915_private *dev_priv = dev->dev_private;
14291
14292 if (!IS_MOBILE(dev))
14293 return false;
14294
14295 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14296 return false;
14297
e3589908 14298 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
14299 return false;
14300
14301 return true;
14302}
14303
84b4e042
JB
14304static bool intel_crt_present(struct drm_device *dev)
14305{
14306 struct drm_i915_private *dev_priv = dev->dev_private;
14307
884497ed
DL
14308 if (INTEL_INFO(dev)->gen >= 9)
14309 return false;
14310
cf404ce4 14311 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
84b4e042
JB
14312 return false;
14313
14314 if (IS_CHERRYVIEW(dev))
14315 return false;
14316
65e472e4
VS
14317 if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
14318 return false;
14319
70ac54d0
VS
14320 /* DDI E can't be used if DDI A requires 4 lanes */
14321 if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
14322 return false;
14323
e4abb733 14324 if (!dev_priv->vbt.int_crt_support)
84b4e042
JB
14325 return false;
14326
14327 return true;
14328}
14329
79e53945
JB
14330static void intel_setup_outputs(struct drm_device *dev)
14331{
725e30ad 14332 struct drm_i915_private *dev_priv = dev->dev_private;
4ef69c7a 14333 struct intel_encoder *encoder;
cb0953d7 14334 bool dpd_is_edp = false;
79e53945 14335
c9093354 14336 intel_lvds_init(dev);
79e53945 14337
84b4e042 14338 if (intel_crt_present(dev))
79935fca 14339 intel_crt_init(dev);
cb0953d7 14340
c776eb2e
VK
14341 if (IS_BROXTON(dev)) {
14342 /*
14343 * FIXME: Broxton doesn't support port detection via the
14344 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14345 * detect the ports.
14346 */
14347 intel_ddi_init(dev, PORT_A);
14348 intel_ddi_init(dev, PORT_B);
14349 intel_ddi_init(dev, PORT_C);
c6c794a2
SS
14350
14351 intel_dsi_init(dev);
c776eb2e 14352 } else if (HAS_DDI(dev)) {
0e72a5b5
ED
14353 int found;
14354
de31facd
JB
14355 /*
14356 * Haswell uses DDI functions to detect digital outputs.
14357 * On SKL pre-D0 the strap isn't connected, so we assume
14358 * it's there.
14359 */
77179400 14360 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
de31facd 14361 /* WaIgnoreDDIAStrap: skl */
ef11bdb3 14362 if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
0e72a5b5
ED
14363 intel_ddi_init(dev, PORT_A);
14364
14365 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14366 * register */
14367 found = I915_READ(SFUSE_STRAP);
14368
14369 if (found & SFUSE_STRAP_DDIB_DETECTED)
14370 intel_ddi_init(dev, PORT_B);
14371 if (found & SFUSE_STRAP_DDIC_DETECTED)
14372 intel_ddi_init(dev, PORT_C);
14373 if (found & SFUSE_STRAP_DDID_DETECTED)
14374 intel_ddi_init(dev, PORT_D);
2800e4c2
RV
14375 /*
14376 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14377 */
ef11bdb3 14378 if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
2800e4c2
RV
14379 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14380 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14381 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
14382 intel_ddi_init(dev, PORT_E);
14383
0e72a5b5 14384 } else if (HAS_PCH_SPLIT(dev)) {
cb0953d7 14385 int found;
5d8a7752 14386 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
270b3042
DV
14387
14388 if (has_edp_a(dev))
14389 intel_dp_init(dev, DP_A, PORT_A);
cb0953d7 14390
dc0fa718 14391 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 14392 /* PCH SDVOB multiplex with HDMIB */
2a5c0832 14393 found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
30ad48b7 14394 if (!found)
e2debe91 14395 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
5eb08b69 14396 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
ab9d7c30 14397 intel_dp_init(dev, PCH_DP_B, PORT_B);
30ad48b7
ZW
14398 }
14399
dc0fa718 14400 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
e2debe91 14401 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
30ad48b7 14402
dc0fa718 14403 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
e2debe91 14404 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
30ad48b7 14405
5eb08b69 14406 if (I915_READ(PCH_DP_C) & DP_DETECTED)
ab9d7c30 14407 intel_dp_init(dev, PCH_DP_C, PORT_C);
5eb08b69 14408
270b3042 14409 if (I915_READ(PCH_DP_D) & DP_DETECTED)
ab9d7c30 14410 intel_dp_init(dev, PCH_DP_D, PORT_D);
666a4537 14411 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
e17ac6db
VS
14412 /*
14413 * The DP_DETECTED bit is the latched state of the DDC
14414 * SDA pin at boot. However since eDP doesn't require DDC
14415 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14416 * eDP ports may have been muxed to an alternate function.
14417 * Thus we can't rely on the DP_DETECTED bit alone to detect
14418 * eDP ports. Consult the VBT as well as DP_DETECTED to
14419 * detect eDP ports.
14420 */
e66eb81d 14421 if (I915_READ(VLV_HDMIB) & SDVO_DETECTED &&
d2182a66 14422 !intel_dp_is_edp(dev, PORT_B))
e66eb81d
VS
14423 intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
14424 if (I915_READ(VLV_DP_B) & DP_DETECTED ||
e17ac6db 14425 intel_dp_is_edp(dev, PORT_B))
e66eb81d 14426 intel_dp_init(dev, VLV_DP_B, PORT_B);
585a94b8 14427
e66eb81d 14428 if (I915_READ(VLV_HDMIC) & SDVO_DETECTED &&
d2182a66 14429 !intel_dp_is_edp(dev, PORT_C))
e66eb81d
VS
14430 intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
14431 if (I915_READ(VLV_DP_C) & DP_DETECTED ||
e17ac6db 14432 intel_dp_is_edp(dev, PORT_C))
e66eb81d 14433 intel_dp_init(dev, VLV_DP_C, PORT_C);
19c03924 14434
9418c1f1 14435 if (IS_CHERRYVIEW(dev)) {
e17ac6db 14436 /* eDP not supported on port D, so don't check VBT */
e66eb81d
VS
14437 if (I915_READ(CHV_HDMID) & SDVO_DETECTED)
14438 intel_hdmi_init(dev, CHV_HDMID, PORT_D);
14439 if (I915_READ(CHV_DP_D) & DP_DETECTED)
14440 intel_dp_init(dev, CHV_DP_D, PORT_D);
9418c1f1
VS
14441 }
14442
3cfca973 14443 intel_dsi_init(dev);
09da55dc 14444 } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
27185ae1 14445 bool found = false;
7d57382e 14446
e2debe91 14447 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14448 DRM_DEBUG_KMS("probing SDVOB\n");
2a5c0832 14449 found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
3fec3d2f 14450 if (!found && IS_G4X(dev)) {
b01f2c3a 14451 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
e2debe91 14452 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
b01f2c3a 14453 }
27185ae1 14454
3fec3d2f 14455 if (!found && IS_G4X(dev))
ab9d7c30 14456 intel_dp_init(dev, DP_B, PORT_B);
725e30ad 14457 }
13520b05
KH
14458
14459 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 14460
e2debe91 14461 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14462 DRM_DEBUG_KMS("probing SDVOC\n");
2a5c0832 14463 found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
b01f2c3a 14464 }
27185ae1 14465
e2debe91 14466 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 14467
3fec3d2f 14468 if (IS_G4X(dev)) {
b01f2c3a 14469 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
e2debe91 14470 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
b01f2c3a 14471 }
3fec3d2f 14472 if (IS_G4X(dev))
ab9d7c30 14473 intel_dp_init(dev, DP_C, PORT_C);
725e30ad 14474 }
27185ae1 14475
3fec3d2f 14476 if (IS_G4X(dev) &&
e7281eab 14477 (I915_READ(DP_D) & DP_DETECTED))
ab9d7c30 14478 intel_dp_init(dev, DP_D, PORT_D);
bad720ff 14479 } else if (IS_GEN2(dev))
79e53945
JB
14480 intel_dvo_init(dev);
14481
103a196f 14482 if (SUPPORTS_TV(dev))
79e53945
JB
14483 intel_tv_init(dev);
14484
0bc12bcb 14485 intel_psr_init(dev);
7c8f8a70 14486
b2784e15 14487 for_each_intel_encoder(dev, encoder) {
4ef69c7a
CW
14488 encoder->base.possible_crtcs = encoder->crtc_mask;
14489 encoder->base.possible_clones =
66a9278e 14490 intel_encoder_clones(encoder);
79e53945 14491 }
47356eb6 14492
dde86e2d 14493 intel_init_pch_refclk(dev);
270b3042
DV
14494
14495 drm_helper_move_panel_connectors_to_head(dev);
79e53945
JB
14496}
14497
14498static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14499{
60a5ca01 14500 struct drm_device *dev = fb->dev;
79e53945 14501 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 14502
ef2d633e 14503 drm_framebuffer_cleanup(fb);
60a5ca01 14504 mutex_lock(&dev->struct_mutex);
ef2d633e 14505 WARN_ON(!intel_fb->obj->framebuffer_references--);
60a5ca01
VS
14506 drm_gem_object_unreference(&intel_fb->obj->base);
14507 mutex_unlock(&dev->struct_mutex);
79e53945
JB
14508 kfree(intel_fb);
14509}
14510
14511static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 14512 struct drm_file *file,
79e53945
JB
14513 unsigned int *handle)
14514{
14515 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 14516 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 14517
cc917ab4
CW
14518 if (obj->userptr.mm) {
14519 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14520 return -EINVAL;
14521 }
14522
05394f39 14523 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
14524}
14525
86c98588
RV
14526static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14527 struct drm_file *file,
14528 unsigned flags, unsigned color,
14529 struct drm_clip_rect *clips,
14530 unsigned num_clips)
14531{
14532 struct drm_device *dev = fb->dev;
14533 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14534 struct drm_i915_gem_object *obj = intel_fb->obj;
14535
14536 mutex_lock(&dev->struct_mutex);
74b4ea1e 14537 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
86c98588
RV
14538 mutex_unlock(&dev->struct_mutex);
14539
14540 return 0;
14541}
14542
79e53945
JB
14543static const struct drm_framebuffer_funcs intel_fb_funcs = {
14544 .destroy = intel_user_framebuffer_destroy,
14545 .create_handle = intel_user_framebuffer_create_handle,
86c98588 14546 .dirty = intel_user_framebuffer_dirty,
79e53945
JB
14547};
14548
b321803d
DL
14549static
14550u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14551 uint32_t pixel_format)
14552{
14553 u32 gen = INTEL_INFO(dev)->gen;
14554
14555 if (gen >= 9) {
ac484963
VS
14556 int cpp = drm_format_plane_cpp(pixel_format, 0);
14557
b321803d
DL
14558 /* "The stride in bytes must not exceed the of the size of 8K
14559 * pixels and 32K bytes."
14560 */
ac484963 14561 return min(8192 * cpp, 32768);
666a4537 14562 } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
b321803d
DL
14563 return 32*1024;
14564 } else if (gen >= 4) {
14565 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14566 return 16*1024;
14567 else
14568 return 32*1024;
14569 } else if (gen >= 3) {
14570 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14571 return 8*1024;
14572 else
14573 return 16*1024;
14574 } else {
14575 /* XXX DSPC is limited to 4k tiled */
14576 return 8*1024;
14577 }
14578}
14579
b5ea642a
DV
14580static int intel_framebuffer_init(struct drm_device *dev,
14581 struct intel_framebuffer *intel_fb,
14582 struct drm_mode_fb_cmd2 *mode_cmd,
14583 struct drm_i915_gem_object *obj)
79e53945 14584{
7b49f948 14585 struct drm_i915_private *dev_priv = to_i915(dev);
6761dd31 14586 unsigned int aligned_height;
79e53945 14587 int ret;
b321803d 14588 u32 pitch_limit, stride_alignment;
79e53945 14589
dd4916c5
DV
14590 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14591
2a80eada
DV
14592 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14593 /* Enforce that fb modifier and tiling mode match, but only for
14594 * X-tiled. This is needed for FBC. */
14595 if (!!(obj->tiling_mode == I915_TILING_X) !=
14596 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14597 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14598 return -EINVAL;
14599 }
14600 } else {
14601 if (obj->tiling_mode == I915_TILING_X)
14602 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14603 else if (obj->tiling_mode == I915_TILING_Y) {
14604 DRM_DEBUG("No Y tiling for legacy addfb\n");
14605 return -EINVAL;
14606 }
14607 }
14608
9a8f0a12
TU
14609 /* Passed in modifier sanity checking. */
14610 switch (mode_cmd->modifier[0]) {
14611 case I915_FORMAT_MOD_Y_TILED:
14612 case I915_FORMAT_MOD_Yf_TILED:
14613 if (INTEL_INFO(dev)->gen < 9) {
14614 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14615 mode_cmd->modifier[0]);
14616 return -EINVAL;
14617 }
14618 case DRM_FORMAT_MOD_NONE:
14619 case I915_FORMAT_MOD_X_TILED:
14620 break;
14621 default:
c0f40428
JB
14622 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14623 mode_cmd->modifier[0]);
57cd6508 14624 return -EINVAL;
c16ed4be 14625 }
57cd6508 14626
7b49f948
VS
14627 stride_alignment = intel_fb_stride_alignment(dev_priv,
14628 mode_cmd->modifier[0],
b321803d
DL
14629 mode_cmd->pixel_format);
14630 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14631 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14632 mode_cmd->pitches[0], stride_alignment);
57cd6508 14633 return -EINVAL;
c16ed4be 14634 }
57cd6508 14635
b321803d
DL
14636 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14637 mode_cmd->pixel_format);
a35cdaa0 14638 if (mode_cmd->pitches[0] > pitch_limit) {
b321803d
DL
14639 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14640 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
2a80eada 14641 "tiled" : "linear",
a35cdaa0 14642 mode_cmd->pitches[0], pitch_limit);
5d7bd705 14643 return -EINVAL;
c16ed4be 14644 }
5d7bd705 14645
2a80eada 14646 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
c16ed4be
CW
14647 mode_cmd->pitches[0] != obj->stride) {
14648 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14649 mode_cmd->pitches[0], obj->stride);
5d7bd705 14650 return -EINVAL;
c16ed4be 14651 }
5d7bd705 14652
57779d06 14653 /* Reject formats not supported by any plane early. */
308e5bcb 14654 switch (mode_cmd->pixel_format) {
57779d06 14655 case DRM_FORMAT_C8:
04b3924d
VS
14656 case DRM_FORMAT_RGB565:
14657 case DRM_FORMAT_XRGB8888:
14658 case DRM_FORMAT_ARGB8888:
57779d06
VS
14659 break;
14660 case DRM_FORMAT_XRGB1555:
c16ed4be 14661 if (INTEL_INFO(dev)->gen > 3) {
4ee62c76
VS
14662 DRM_DEBUG("unsupported pixel format: %s\n",
14663 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14664 return -EINVAL;
c16ed4be 14665 }
57779d06 14666 break;
57779d06 14667 case DRM_FORMAT_ABGR8888:
666a4537
WB
14668 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
14669 INTEL_INFO(dev)->gen < 9) {
6c0fd451
DL
14670 DRM_DEBUG("unsupported pixel format: %s\n",
14671 drm_get_format_name(mode_cmd->pixel_format));
14672 return -EINVAL;
14673 }
14674 break;
14675 case DRM_FORMAT_XBGR8888:
04b3924d 14676 case DRM_FORMAT_XRGB2101010:
57779d06 14677 case DRM_FORMAT_XBGR2101010:
c16ed4be 14678 if (INTEL_INFO(dev)->gen < 4) {
4ee62c76
VS
14679 DRM_DEBUG("unsupported pixel format: %s\n",
14680 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14681 return -EINVAL;
c16ed4be 14682 }
b5626747 14683 break;
7531208b 14684 case DRM_FORMAT_ABGR2101010:
666a4537 14685 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
7531208b
DL
14686 DRM_DEBUG("unsupported pixel format: %s\n",
14687 drm_get_format_name(mode_cmd->pixel_format));
14688 return -EINVAL;
14689 }
14690 break;
04b3924d
VS
14691 case DRM_FORMAT_YUYV:
14692 case DRM_FORMAT_UYVY:
14693 case DRM_FORMAT_YVYU:
14694 case DRM_FORMAT_VYUY:
c16ed4be 14695 if (INTEL_INFO(dev)->gen < 5) {
4ee62c76
VS
14696 DRM_DEBUG("unsupported pixel format: %s\n",
14697 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14698 return -EINVAL;
c16ed4be 14699 }
57cd6508
CW
14700 break;
14701 default:
4ee62c76
VS
14702 DRM_DEBUG("unsupported pixel format: %s\n",
14703 drm_get_format_name(mode_cmd->pixel_format));
57cd6508
CW
14704 return -EINVAL;
14705 }
14706
90f9a336
VS
14707 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14708 if (mode_cmd->offsets[0] != 0)
14709 return -EINVAL;
14710
ec2c981e 14711 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
091df6cb
DV
14712 mode_cmd->pixel_format,
14713 mode_cmd->modifier[0]);
53155c0a
DV
14714 /* FIXME drm helper for size checks (especially planar formats)? */
14715 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14716 return -EINVAL;
14717
c7d73f6a
DV
14718 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14719 intel_fb->obj = obj;
14720
2d7a215f
VS
14721 intel_fill_fb_info(dev_priv, &intel_fb->base);
14722
79e53945
JB
14723 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14724 if (ret) {
14725 DRM_ERROR("framebuffer init failed %d\n", ret);
14726 return ret;
14727 }
14728
0b05e1e0
VS
14729 intel_fb->obj->framebuffer_references++;
14730
79e53945
JB
14731 return 0;
14732}
14733
79e53945
JB
14734static struct drm_framebuffer *
14735intel_user_framebuffer_create(struct drm_device *dev,
14736 struct drm_file *filp,
1eb83451 14737 const struct drm_mode_fb_cmd2 *user_mode_cmd)
79e53945 14738{
dcb1394e 14739 struct drm_framebuffer *fb;
05394f39 14740 struct drm_i915_gem_object *obj;
76dc3769 14741 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
79e53945 14742
308e5bcb 14743 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
76dc3769 14744 mode_cmd.handles[0]));
c8725226 14745 if (&obj->base == NULL)
cce13ff7 14746 return ERR_PTR(-ENOENT);
79e53945 14747
92907cbb 14748 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
dcb1394e
LW
14749 if (IS_ERR(fb))
14750 drm_gem_object_unreference_unlocked(&obj->base);
14751
14752 return fb;
79e53945
JB
14753}
14754
0695726e 14755#ifndef CONFIG_DRM_FBDEV_EMULATION
0632fef6 14756static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
4520f53a
DV
14757{
14758}
14759#endif
14760
79e53945 14761static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 14762 .fb_create = intel_user_framebuffer_create,
0632fef6 14763 .output_poll_changed = intel_fbdev_output_poll_changed,
5ee67f1c
MR
14764 .atomic_check = intel_atomic_check,
14765 .atomic_commit = intel_atomic_commit,
de419ab6
ML
14766 .atomic_state_alloc = intel_atomic_state_alloc,
14767 .atomic_state_clear = intel_atomic_state_clear,
79e53945
JB
14768};
14769
88212941
ID
14770/**
14771 * intel_init_display_hooks - initialize the display modesetting hooks
14772 * @dev_priv: device private
14773 */
14774void intel_init_display_hooks(struct drm_i915_private *dev_priv)
e70236a8 14775{
88212941 14776 if (HAS_PCH_SPLIT(dev_priv) || IS_G4X(dev_priv))
ee9300bb 14777 dev_priv->display.find_dpll = g4x_find_best_dpll;
88212941 14778 else if (IS_PINEVIEW(dev_priv))
ee9300bb
DV
14779 dev_priv->display.find_dpll = pnv_find_best_dpll;
14780 else
14781 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14782
88212941 14783 if (INTEL_INFO(dev_priv)->gen >= 9) {
bc8d7dff 14784 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14785 dev_priv->display.get_initial_plane_config =
14786 skylake_get_initial_plane_config;
bc8d7dff
DL
14787 dev_priv->display.crtc_compute_clock =
14788 haswell_crtc_compute_clock;
14789 dev_priv->display.crtc_enable = haswell_crtc_enable;
14790 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 14791 } else if (HAS_DDI(dev_priv)) {
0e8ffe1b 14792 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14793 dev_priv->display.get_initial_plane_config =
14794 ironlake_get_initial_plane_config;
797d0259
ACO
14795 dev_priv->display.crtc_compute_clock =
14796 haswell_crtc_compute_clock;
4f771f10
PZ
14797 dev_priv->display.crtc_enable = haswell_crtc_enable;
14798 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 14799 } else if (HAS_PCH_SPLIT(dev_priv)) {
0e8ffe1b 14800 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
5724dbd1
DL
14801 dev_priv->display.get_initial_plane_config =
14802 ironlake_get_initial_plane_config;
3fb37703
ACO
14803 dev_priv->display.crtc_compute_clock =
14804 ironlake_crtc_compute_clock;
76e5a89c
DV
14805 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14806 dev_priv->display.crtc_disable = ironlake_crtc_disable;
65b3d6a9 14807 } else if (IS_CHERRYVIEW(dev_priv)) {
89b667f8 14808 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14809 dev_priv->display.get_initial_plane_config =
14810 i9xx_get_initial_plane_config;
65b3d6a9
ACO
14811 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
14812 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14813 dev_priv->display.crtc_disable = i9xx_crtc_disable;
14814 } else if (IS_VALLEYVIEW(dev_priv)) {
14815 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14816 dev_priv->display.get_initial_plane_config =
14817 i9xx_get_initial_plane_config;
14818 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
89b667f8
JB
14819 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14820 dev_priv->display.crtc_disable = i9xx_crtc_disable;
f564048e 14821 } else {
0e8ffe1b 14822 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14823 dev_priv->display.get_initial_plane_config =
14824 i9xx_get_initial_plane_config;
d6dfee7a 14825 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
76e5a89c
DV
14826 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14827 dev_priv->display.crtc_disable = i9xx_crtc_disable;
f564048e 14828 }
e70236a8 14829
e70236a8 14830 /* Returns the core display clock speed */
88212941 14831 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
1652d19e
VS
14832 dev_priv->display.get_display_clock_speed =
14833 skylake_get_display_clock_speed;
88212941 14834 else if (IS_BROXTON(dev_priv))
acd3f3d3
BP
14835 dev_priv->display.get_display_clock_speed =
14836 broxton_get_display_clock_speed;
88212941 14837 else if (IS_BROADWELL(dev_priv))
1652d19e
VS
14838 dev_priv->display.get_display_clock_speed =
14839 broadwell_get_display_clock_speed;
88212941 14840 else if (IS_HASWELL(dev_priv))
1652d19e
VS
14841 dev_priv->display.get_display_clock_speed =
14842 haswell_get_display_clock_speed;
88212941 14843 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
25eb05fc
JB
14844 dev_priv->display.get_display_clock_speed =
14845 valleyview_get_display_clock_speed;
88212941 14846 else if (IS_GEN5(dev_priv))
b37a6434
VS
14847 dev_priv->display.get_display_clock_speed =
14848 ilk_get_display_clock_speed;
88212941
ID
14849 else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
14850 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
e70236a8
JB
14851 dev_priv->display.get_display_clock_speed =
14852 i945_get_display_clock_speed;
88212941 14853 else if (IS_GM45(dev_priv))
34edce2f
VS
14854 dev_priv->display.get_display_clock_speed =
14855 gm45_get_display_clock_speed;
88212941 14856 else if (IS_CRESTLINE(dev_priv))
34edce2f
VS
14857 dev_priv->display.get_display_clock_speed =
14858 i965gm_get_display_clock_speed;
88212941 14859 else if (IS_PINEVIEW(dev_priv))
34edce2f
VS
14860 dev_priv->display.get_display_clock_speed =
14861 pnv_get_display_clock_speed;
88212941 14862 else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
34edce2f
VS
14863 dev_priv->display.get_display_clock_speed =
14864 g33_get_display_clock_speed;
88212941 14865 else if (IS_I915G(dev_priv))
e70236a8
JB
14866 dev_priv->display.get_display_clock_speed =
14867 i915_get_display_clock_speed;
88212941 14868 else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
e70236a8
JB
14869 dev_priv->display.get_display_clock_speed =
14870 i9xx_misc_get_display_clock_speed;
88212941 14871 else if (IS_I915GM(dev_priv))
e70236a8
JB
14872 dev_priv->display.get_display_clock_speed =
14873 i915gm_get_display_clock_speed;
88212941 14874 else if (IS_I865G(dev_priv))
e70236a8
JB
14875 dev_priv->display.get_display_clock_speed =
14876 i865_get_display_clock_speed;
88212941 14877 else if (IS_I85X(dev_priv))
e70236a8 14878 dev_priv->display.get_display_clock_speed =
1b1d2716 14879 i85x_get_display_clock_speed;
623e01e5 14880 else { /* 830 */
88212941 14881 WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
e70236a8
JB
14882 dev_priv->display.get_display_clock_speed =
14883 i830_get_display_clock_speed;
623e01e5 14884 }
e70236a8 14885
88212941 14886 if (IS_GEN5(dev_priv)) {
3bb11b53 14887 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
88212941 14888 } else if (IS_GEN6(dev_priv)) {
3bb11b53 14889 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
88212941 14890 } else if (IS_IVYBRIDGE(dev_priv)) {
3bb11b53
SJ
14891 /* FIXME: detect B0+ stepping and use auto training */
14892 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
88212941 14893 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
3bb11b53 14894 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
88212941 14895 if (IS_BROADWELL(dev_priv)) {
27c329ed
ML
14896 dev_priv->display.modeset_commit_cdclk =
14897 broadwell_modeset_commit_cdclk;
14898 dev_priv->display.modeset_calc_cdclk =
14899 broadwell_modeset_calc_cdclk;
14900 }
88212941 14901 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
27c329ed
ML
14902 dev_priv->display.modeset_commit_cdclk =
14903 valleyview_modeset_commit_cdclk;
14904 dev_priv->display.modeset_calc_cdclk =
14905 valleyview_modeset_calc_cdclk;
88212941 14906 } else if (IS_BROXTON(dev_priv)) {
27c329ed
ML
14907 dev_priv->display.modeset_commit_cdclk =
14908 broxton_modeset_commit_cdclk;
14909 dev_priv->display.modeset_calc_cdclk =
14910 broxton_modeset_calc_cdclk;
e70236a8 14911 }
8c9f3aaf 14912
88212941 14913 switch (INTEL_INFO(dev_priv)->gen) {
8c9f3aaf
JB
14914 case 2:
14915 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14916 break;
14917
14918 case 3:
14919 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14920 break;
14921
14922 case 4:
14923 case 5:
14924 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14925 break;
14926
14927 case 6:
14928 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14929 break;
7c9017e5 14930 case 7:
4e0bbc31 14931 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
7c9017e5
JB
14932 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14933 break;
830c81db 14934 case 9:
ba343e02
TU
14935 /* Drop through - unsupported since execlist only. */
14936 default:
14937 /* Default just returns -ENODEV to indicate unsupported */
14938 dev_priv->display.queue_flip = intel_default_queue_flip;
8c9f3aaf 14939 }
e70236a8
JB
14940}
14941
b690e96c
JB
14942/*
14943 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14944 * resume, or other times. This quirk makes sure that's the case for
14945 * affected systems.
14946 */
0206e353 14947static void quirk_pipea_force(struct drm_device *dev)
b690e96c
JB
14948{
14949 struct drm_i915_private *dev_priv = dev->dev_private;
14950
14951 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
bc0daf48 14952 DRM_INFO("applying pipe a force quirk\n");
b690e96c
JB
14953}
14954
b6b5d049
VS
14955static void quirk_pipeb_force(struct drm_device *dev)
14956{
14957 struct drm_i915_private *dev_priv = dev->dev_private;
14958
14959 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14960 DRM_INFO("applying pipe b force quirk\n");
14961}
14962
435793df
KP
14963/*
14964 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14965 */
14966static void quirk_ssc_force_disable(struct drm_device *dev)
14967{
14968 struct drm_i915_private *dev_priv = dev->dev_private;
14969 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 14970 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
14971}
14972
4dca20ef 14973/*
5a15ab5b
CE
14974 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14975 * brightness value
4dca20ef
CE
14976 */
14977static void quirk_invert_brightness(struct drm_device *dev)
14978{
14979 struct drm_i915_private *dev_priv = dev->dev_private;
14980 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 14981 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
14982}
14983
9c72cc6f
SD
14984/* Some VBT's incorrectly indicate no backlight is present */
14985static void quirk_backlight_present(struct drm_device *dev)
14986{
14987 struct drm_i915_private *dev_priv = dev->dev_private;
14988 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14989 DRM_INFO("applying backlight present quirk\n");
14990}
14991
b690e96c
JB
14992struct intel_quirk {
14993 int device;
14994 int subsystem_vendor;
14995 int subsystem_device;
14996 void (*hook)(struct drm_device *dev);
14997};
14998
5f85f176
EE
14999/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
15000struct intel_dmi_quirk {
15001 void (*hook)(struct drm_device *dev);
15002 const struct dmi_system_id (*dmi_id_list)[];
15003};
15004
15005static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
15006{
15007 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
15008 return 1;
15009}
15010
15011static const struct intel_dmi_quirk intel_dmi_quirks[] = {
15012 {
15013 .dmi_id_list = &(const struct dmi_system_id[]) {
15014 {
15015 .callback = intel_dmi_reverse_brightness,
15016 .ident = "NCR Corporation",
15017 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
15018 DMI_MATCH(DMI_PRODUCT_NAME, ""),
15019 },
15020 },
15021 { } /* terminating entry */
15022 },
15023 .hook = quirk_invert_brightness,
15024 },
15025};
15026
c43b5634 15027static struct intel_quirk intel_quirks[] = {
b690e96c
JB
15028 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
15029 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
15030
b690e96c
JB
15031 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
15032 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
15033
5f080c0f
VS
15034 /* 830 needs to leave pipe A & dpll A up */
15035 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
15036
b6b5d049
VS
15037 /* 830 needs to leave pipe B & dpll B up */
15038 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
15039
435793df
KP
15040 /* Lenovo U160 cannot use SSC on LVDS */
15041 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
15042
15043 /* Sony Vaio Y cannot use SSC on LVDS */
15044 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 15045
be505f64
AH
15046 /* Acer Aspire 5734Z must invert backlight brightness */
15047 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
15048
15049 /* Acer/eMachines G725 */
15050 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
15051
15052 /* Acer/eMachines e725 */
15053 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
15054
15055 /* Acer/Packard Bell NCL20 */
15056 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
15057
15058 /* Acer Aspire 4736Z */
15059 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
15060
15061 /* Acer Aspire 5336 */
15062 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
2e93a1aa
SD
15063
15064 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
15065 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
d4967d8c 15066
dfb3d47b
SD
15067 /* Acer C720 Chromebook (Core i3 4005U) */
15068 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
15069
b2a9601c 15070 /* Apple Macbook 2,1 (Core 2 T7400) */
15071 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
15072
1b9448b0
JN
15073 /* Apple Macbook 4,1 */
15074 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
15075
d4967d8c
SD
15076 /* Toshiba CB35 Chromebook (Celeron 2955U) */
15077 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
724cb06f
SD
15078
15079 /* HP Chromebook 14 (Celeron 2955U) */
15080 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
cf6f0af9
JN
15081
15082 /* Dell Chromebook 11 */
15083 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
9be64eee
JN
15084
15085 /* Dell Chromebook 11 (2015 version) */
15086 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
b690e96c
JB
15087};
15088
15089static void intel_init_quirks(struct drm_device *dev)
15090{
15091 struct pci_dev *d = dev->pdev;
15092 int i;
15093
15094 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
15095 struct intel_quirk *q = &intel_quirks[i];
15096
15097 if (d->device == q->device &&
15098 (d->subsystem_vendor == q->subsystem_vendor ||
15099 q->subsystem_vendor == PCI_ANY_ID) &&
15100 (d->subsystem_device == q->subsystem_device ||
15101 q->subsystem_device == PCI_ANY_ID))
15102 q->hook(dev);
15103 }
5f85f176
EE
15104 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
15105 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
15106 intel_dmi_quirks[i].hook(dev);
15107 }
b690e96c
JB
15108}
15109
9cce37f4
JB
15110/* Disable the VGA plane that we never use */
15111static void i915_disable_vga(struct drm_device *dev)
15112{
15113 struct drm_i915_private *dev_priv = dev->dev_private;
15114 u8 sr1;
f0f59a00 15115 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
9cce37f4 15116
2b37c616 15117 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
9cce37f4 15118 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 15119 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
15120 sr1 = inb(VGA_SR_DATA);
15121 outb(sr1 | 1<<5, VGA_SR_DATA);
15122 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
15123 udelay(300);
15124
01f5a626 15125 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9cce37f4
JB
15126 POSTING_READ(vga_reg);
15127}
15128
f817586c
DV
15129void intel_modeset_init_hw(struct drm_device *dev)
15130{
1a617b77
ML
15131 struct drm_i915_private *dev_priv = dev->dev_private;
15132
b6283055 15133 intel_update_cdclk(dev);
1a617b77
ML
15134
15135 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
15136
f817586c 15137 intel_init_clock_gating(dev);
8090c6b9 15138 intel_enable_gt_powersave(dev);
f817586c
DV
15139}
15140
d93c0372
MR
15141/*
15142 * Calculate what we think the watermarks should be for the state we've read
15143 * out of the hardware and then immediately program those watermarks so that
15144 * we ensure the hardware settings match our internal state.
15145 *
15146 * We can calculate what we think WM's should be by creating a duplicate of the
15147 * current state (which was constructed during hardware readout) and running it
15148 * through the atomic check code to calculate new watermark values in the
15149 * state object.
15150 */
15151static void sanitize_watermarks(struct drm_device *dev)
15152{
15153 struct drm_i915_private *dev_priv = to_i915(dev);
15154 struct drm_atomic_state *state;
15155 struct drm_crtc *crtc;
15156 struct drm_crtc_state *cstate;
15157 struct drm_modeset_acquire_ctx ctx;
15158 int ret;
15159 int i;
15160
15161 /* Only supported on platforms that use atomic watermark design */
ed4a6a7c 15162 if (!dev_priv->display.optimize_watermarks)
d93c0372
MR
15163 return;
15164
15165 /*
15166 * We need to hold connection_mutex before calling duplicate_state so
15167 * that the connector loop is protected.
15168 */
15169 drm_modeset_acquire_init(&ctx, 0);
15170retry:
0cd1262d 15171 ret = drm_modeset_lock_all_ctx(dev, &ctx);
d93c0372
MR
15172 if (ret == -EDEADLK) {
15173 drm_modeset_backoff(&ctx);
15174 goto retry;
15175 } else if (WARN_ON(ret)) {
0cd1262d 15176 goto fail;
d93c0372
MR
15177 }
15178
15179 state = drm_atomic_helper_duplicate_state(dev, &ctx);
15180 if (WARN_ON(IS_ERR(state)))
0cd1262d 15181 goto fail;
d93c0372 15182
ed4a6a7c
MR
15183 /*
15184 * Hardware readout is the only time we don't want to calculate
15185 * intermediate watermarks (since we don't trust the current
15186 * watermarks).
15187 */
15188 to_intel_atomic_state(state)->skip_intermediate_wm = true;
15189
d93c0372
MR
15190 ret = intel_atomic_check(dev, state);
15191 if (ret) {
15192 /*
15193 * If we fail here, it means that the hardware appears to be
15194 * programmed in a way that shouldn't be possible, given our
15195 * understanding of watermark requirements. This might mean a
15196 * mistake in the hardware readout code or a mistake in the
15197 * watermark calculations for a given platform. Raise a WARN
15198 * so that this is noticeable.
15199 *
15200 * If this actually happens, we'll have to just leave the
15201 * BIOS-programmed watermarks untouched and hope for the best.
15202 */
15203 WARN(true, "Could not determine valid watermarks for inherited state\n");
0cd1262d 15204 goto fail;
d93c0372
MR
15205 }
15206
15207 /* Write calculated watermark values back */
15208 to_i915(dev)->wm.config = to_intel_atomic_state(state)->wm_config;
15209 for_each_crtc_in_state(state, crtc, cstate, i) {
15210 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
15211
ed4a6a7c
MR
15212 cs->wm.need_postvbl_update = true;
15213 dev_priv->display.optimize_watermarks(cs);
d93c0372
MR
15214 }
15215
15216 drm_atomic_state_free(state);
0cd1262d 15217fail:
d93c0372
MR
15218 drm_modeset_drop_locks(&ctx);
15219 drm_modeset_acquire_fini(&ctx);
15220}
15221
79e53945
JB
15222void intel_modeset_init(struct drm_device *dev)
15223{
652c393a 15224 struct drm_i915_private *dev_priv = dev->dev_private;
1fe47785 15225 int sprite, ret;
8cc87b75 15226 enum pipe pipe;
46f297fb 15227 struct intel_crtc *crtc;
79e53945
JB
15228
15229 drm_mode_config_init(dev);
15230
15231 dev->mode_config.min_width = 0;
15232 dev->mode_config.min_height = 0;
15233
019d96cb
DA
15234 dev->mode_config.preferred_depth = 24;
15235 dev->mode_config.prefer_shadow = 1;
15236
25bab385
TU
15237 dev->mode_config.allow_fb_modifiers = true;
15238
e6ecefaa 15239 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 15240
b690e96c
JB
15241 intel_init_quirks(dev);
15242
1fa61106
ED
15243 intel_init_pm(dev);
15244
e3c74757
BW
15245 if (INTEL_INFO(dev)->num_pipes == 0)
15246 return;
15247
69f92f67
LW
15248 /*
15249 * There may be no VBT; and if the BIOS enabled SSC we can
15250 * just keep using it to avoid unnecessary flicker. Whereas if the
15251 * BIOS isn't using it, don't assume it will work even if the VBT
15252 * indicates as much.
15253 */
15254 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
15255 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15256 DREF_SSC1_ENABLE);
15257
15258 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
15259 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
15260 bios_lvds_use_ssc ? "en" : "dis",
15261 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
15262 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
15263 }
15264 }
15265
a6c45cf0
CW
15266 if (IS_GEN2(dev)) {
15267 dev->mode_config.max_width = 2048;
15268 dev->mode_config.max_height = 2048;
15269 } else if (IS_GEN3(dev)) {
5e4d6fa7
KP
15270 dev->mode_config.max_width = 4096;
15271 dev->mode_config.max_height = 4096;
79e53945 15272 } else {
a6c45cf0
CW
15273 dev->mode_config.max_width = 8192;
15274 dev->mode_config.max_height = 8192;
79e53945 15275 }
068be561 15276
dc41c154
VS
15277 if (IS_845G(dev) || IS_I865G(dev)) {
15278 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15279 dev->mode_config.cursor_height = 1023;
15280 } else if (IS_GEN2(dev)) {
068be561
DL
15281 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15282 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15283 } else {
15284 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15285 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15286 }
15287
62106b4f 15288 dev->mode_config.fb_base = dev_priv->ggtt.mappable_base;
79e53945 15289
28c97730 15290 DRM_DEBUG_KMS("%d display pipe%s available.\n",
7eb552ae
BW
15291 INTEL_INFO(dev)->num_pipes,
15292 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
79e53945 15293
055e393f 15294 for_each_pipe(dev_priv, pipe) {
8cc87b75 15295 intel_crtc_init(dev, pipe);
3bdcfc0c 15296 for_each_sprite(dev_priv, pipe, sprite) {
1fe47785 15297 ret = intel_plane_init(dev, pipe, sprite);
7f1f3851 15298 if (ret)
06da8da2 15299 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
1fe47785 15300 pipe_name(pipe), sprite_name(pipe, sprite), ret);
7f1f3851 15301 }
79e53945
JB
15302 }
15303
bfa7df01 15304 intel_update_czclk(dev_priv);
e7dc33f3 15305 intel_update_rawclk(dev_priv);
bfa7df01
VS
15306 intel_update_cdclk(dev);
15307
e72f9fbf 15308 intel_shared_dpll_init(dev);
ee7b9f93 15309
9cce37f4
JB
15310 /* Just disable it once at startup */
15311 i915_disable_vga(dev);
79e53945 15312 intel_setup_outputs(dev);
11be49eb 15313
6e9f798d 15314 drm_modeset_lock_all(dev);
043e9bda 15315 intel_modeset_setup_hw_state(dev);
6e9f798d 15316 drm_modeset_unlock_all(dev);
46f297fb 15317
d3fcc808 15318 for_each_intel_crtc(dev, crtc) {
eeebeac5
ML
15319 struct intel_initial_plane_config plane_config = {};
15320
46f297fb
JB
15321 if (!crtc->active)
15322 continue;
15323
46f297fb 15324 /*
46f297fb
JB
15325 * Note that reserving the BIOS fb up front prevents us
15326 * from stuffing other stolen allocations like the ring
15327 * on top. This prevents some ugliness at boot time, and
15328 * can even allow for smooth boot transitions if the BIOS
15329 * fb is large enough for the active pipe configuration.
15330 */
eeebeac5
ML
15331 dev_priv->display.get_initial_plane_config(crtc,
15332 &plane_config);
15333
15334 /*
15335 * If the fb is shared between multiple heads, we'll
15336 * just get the first one.
15337 */
15338 intel_find_initial_plane_obj(crtc, &plane_config);
46f297fb 15339 }
d93c0372
MR
15340
15341 /*
15342 * Make sure hardware watermarks really match the state we read out.
15343 * Note that we need to do this after reconstructing the BIOS fb's
15344 * since the watermark calculation done here will use pstate->fb.
15345 */
15346 sanitize_watermarks(dev);
2c7111db
CW
15347}
15348
7fad798e
DV
15349static void intel_enable_pipe_a(struct drm_device *dev)
15350{
15351 struct intel_connector *connector;
15352 struct drm_connector *crt = NULL;
15353 struct intel_load_detect_pipe load_detect_temp;
208bf9fd 15354 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
7fad798e
DV
15355
15356 /* We can't just switch on the pipe A, we need to set things up with a
15357 * proper mode and output configuration. As a gross hack, enable pipe A
15358 * by enabling the load detect pipe once. */
3a3371ff 15359 for_each_intel_connector(dev, connector) {
7fad798e
DV
15360 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15361 crt = &connector->base;
15362 break;
15363 }
15364 }
15365
15366 if (!crt)
15367 return;
15368
208bf9fd 15369 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
49172fee 15370 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
7fad798e
DV
15371}
15372
fa555837
DV
15373static bool
15374intel_check_plane_mapping(struct intel_crtc *crtc)
15375{
7eb552ae
BW
15376 struct drm_device *dev = crtc->base.dev;
15377 struct drm_i915_private *dev_priv = dev->dev_private;
649636ef 15378 u32 val;
fa555837 15379
7eb552ae 15380 if (INTEL_INFO(dev)->num_pipes == 1)
fa555837
DV
15381 return true;
15382
649636ef 15383 val = I915_READ(DSPCNTR(!crtc->plane));
fa555837
DV
15384
15385 if ((val & DISPLAY_PLANE_ENABLE) &&
15386 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15387 return false;
15388
15389 return true;
15390}
15391
02e93c35
VS
15392static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15393{
15394 struct drm_device *dev = crtc->base.dev;
15395 struct intel_encoder *encoder;
15396
15397 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15398 return true;
15399
15400 return false;
15401}
15402
dd756198
VS
15403static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
15404{
15405 struct drm_device *dev = encoder->base.dev;
15406 struct intel_connector *connector;
15407
15408 for_each_connector_on_encoder(dev, &encoder->base, connector)
15409 return true;
15410
15411 return false;
15412}
15413
24929352
DV
15414static void intel_sanitize_crtc(struct intel_crtc *crtc)
15415{
15416 struct drm_device *dev = crtc->base.dev;
15417 struct drm_i915_private *dev_priv = dev->dev_private;
4d1de975 15418 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
24929352 15419
24929352 15420 /* Clear any frame start delays used for debugging left by the BIOS */
4d1de975
JN
15421 if (!transcoder_is_dsi(cpu_transcoder)) {
15422 i915_reg_t reg = PIPECONF(cpu_transcoder);
15423
15424 I915_WRITE(reg,
15425 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15426 }
24929352 15427
d3eaf884 15428 /* restore vblank interrupts to correct state */
9625604c 15429 drm_crtc_vblank_reset(&crtc->base);
d297e103 15430 if (crtc->active) {
f9cd7b88
VS
15431 struct intel_plane *plane;
15432
9625604c 15433 drm_crtc_vblank_on(&crtc->base);
f9cd7b88
VS
15434
15435 /* Disable everything but the primary plane */
15436 for_each_intel_plane_on_crtc(dev, crtc, plane) {
15437 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
15438 continue;
15439
15440 plane->disable_plane(&plane->base, &crtc->base);
15441 }
9625604c 15442 }
d3eaf884 15443
24929352 15444 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
DV
15445 * disable the crtc (and hence change the state) if it is wrong. Note
15446 * that gen4+ has a fixed plane -> pipe mapping. */
15447 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
24929352
DV
15448 bool plane;
15449
24929352
DV
15450 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
15451 crtc->base.base.id);
15452
15453 /* Pipe has the wrong plane attached and the plane is active.
15454 * Temporarily change the plane mapping and disable everything
15455 * ... */
15456 plane = crtc->plane;
b70709a6 15457 to_intel_plane_state(crtc->base.primary->state)->visible = true;
24929352 15458 crtc->plane = !plane;
b17d48e2 15459 intel_crtc_disable_noatomic(&crtc->base);
24929352 15460 crtc->plane = plane;
24929352 15461 }
24929352 15462
7fad798e
DV
15463 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15464 crtc->pipe == PIPE_A && !crtc->active) {
15465 /* BIOS forgot to enable pipe A, this mostly happens after
15466 * resume. Force-enable the pipe to fix this, the update_dpms
15467 * call below we restore the pipe to the right state, but leave
15468 * the required bits on. */
15469 intel_enable_pipe_a(dev);
15470 }
15471
24929352
DV
15472 /* Adjust the state of the output pipe according to whether we
15473 * have active connectors/encoders. */
842e0307 15474 if (crtc->active && !intel_crtc_has_encoders(crtc))
b17d48e2 15475 intel_crtc_disable_noatomic(&crtc->base);
24929352 15476
a3ed6aad 15477 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
4cc31489
DV
15478 /*
15479 * We start out with underrun reporting disabled to avoid races.
15480 * For correct bookkeeping mark this on active crtcs.
15481 *
c5ab3bc0
DV
15482 * Also on gmch platforms we dont have any hardware bits to
15483 * disable the underrun reporting. Which means we need to start
15484 * out with underrun reporting disabled also on inactive pipes,
15485 * since otherwise we'll complain about the garbage we read when
15486 * e.g. coming up after runtime pm.
15487 *
4cc31489
DV
15488 * No protection against concurrent access is required - at
15489 * worst a fifo underrun happens which also sets this to false.
15490 */
15491 crtc->cpu_fifo_underrun_disabled = true;
15492 crtc->pch_fifo_underrun_disabled = true;
15493 }
24929352
DV
15494}
15495
15496static void intel_sanitize_encoder(struct intel_encoder *encoder)
15497{
15498 struct intel_connector *connector;
15499 struct drm_device *dev = encoder->base.dev;
15500
15501 /* We need to check both for a crtc link (meaning that the
15502 * encoder is active and trying to read from a pipe) and the
15503 * pipe itself being active. */
15504 bool has_active_crtc = encoder->base.crtc &&
15505 to_intel_crtc(encoder->base.crtc)->active;
15506
dd756198 15507 if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
24929352
DV
15508 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15509 encoder->base.base.id,
8e329a03 15510 encoder->base.name);
24929352
DV
15511
15512 /* Connector is active, but has no active pipe. This is
15513 * fallout from our resume register restoring. Disable
15514 * the encoder manually again. */
15515 if (encoder->base.crtc) {
15516 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15517 encoder->base.base.id,
8e329a03 15518 encoder->base.name);
24929352 15519 encoder->disable(encoder);
a62d1497
VS
15520 if (encoder->post_disable)
15521 encoder->post_disable(encoder);
24929352 15522 }
7f1950fb 15523 encoder->base.crtc = NULL;
24929352
DV
15524
15525 /* Inconsistent output/port/pipe state happens presumably due to
15526 * a bug in one of the get_hw_state functions. Or someplace else
15527 * in our code, like the register restore mess on resume. Clamp
15528 * things to off as a safer default. */
3a3371ff 15529 for_each_intel_connector(dev, connector) {
24929352
DV
15530 if (connector->encoder != encoder)
15531 continue;
7f1950fb
EE
15532 connector->base.dpms = DRM_MODE_DPMS_OFF;
15533 connector->base.encoder = NULL;
24929352
DV
15534 }
15535 }
15536 /* Enabled encoders without active connectors will be fixed in
15537 * the crtc fixup. */
15538}
15539
04098753 15540void i915_redisable_vga_power_on(struct drm_device *dev)
0fde901f
KM
15541{
15542 struct drm_i915_private *dev_priv = dev->dev_private;
f0f59a00 15543 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
0fde901f 15544
04098753
ID
15545 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15546 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15547 i915_disable_vga(dev);
15548 }
15549}
15550
15551void i915_redisable_vga(struct drm_device *dev)
15552{
15553 struct drm_i915_private *dev_priv = dev->dev_private;
15554
8dc8a27c
PZ
15555 /* This function can be called both from intel_modeset_setup_hw_state or
15556 * at a very early point in our resume sequence, where the power well
15557 * structures are not yet restored. Since this function is at a very
15558 * paranoid "someone might have enabled VGA while we were not looking"
15559 * level, just check if the power well is enabled instead of trying to
15560 * follow the "don't touch the power well if we don't need it" policy
15561 * the rest of the driver uses. */
6392f847 15562 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
15563 return;
15564
04098753 15565 i915_redisable_vga_power_on(dev);
6392f847
ID
15566
15567 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
0fde901f
KM
15568}
15569
f9cd7b88 15570static bool primary_get_hw_state(struct intel_plane *plane)
98ec7739 15571{
f9cd7b88 15572 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
98ec7739 15573
f9cd7b88 15574 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
d032ffa0
ML
15575}
15576
f9cd7b88
VS
15577/* FIXME read out full plane state for all planes */
15578static void readout_plane_state(struct intel_crtc *crtc)
d032ffa0 15579{
b26d3ea3 15580 struct drm_plane *primary = crtc->base.primary;
f9cd7b88 15581 struct intel_plane_state *plane_state =
b26d3ea3 15582 to_intel_plane_state(primary->state);
d032ffa0 15583
19b8d387 15584 plane_state->visible = crtc->active &&
b26d3ea3
ML
15585 primary_get_hw_state(to_intel_plane(primary));
15586
15587 if (plane_state->visible)
15588 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
98ec7739
VS
15589}
15590
30e984df 15591static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352
DV
15592{
15593 struct drm_i915_private *dev_priv = dev->dev_private;
15594 enum pipe pipe;
24929352
DV
15595 struct intel_crtc *crtc;
15596 struct intel_encoder *encoder;
15597 struct intel_connector *connector;
5358901f 15598 int i;
24929352 15599
565602d7
ML
15600 dev_priv->active_crtcs = 0;
15601
d3fcc808 15602 for_each_intel_crtc(dev, crtc) {
565602d7
ML
15603 struct intel_crtc_state *crtc_state = crtc->config;
15604 int pixclk = 0;
3b117c8f 15605
565602d7
ML
15606 __drm_atomic_helper_crtc_destroy_state(&crtc->base, &crtc_state->base);
15607 memset(crtc_state, 0, sizeof(*crtc_state));
15608 crtc_state->base.crtc = &crtc->base;
24929352 15609
565602d7
ML
15610 crtc_state->base.active = crtc_state->base.enable =
15611 dev_priv->display.get_pipe_config(crtc, crtc_state);
15612
15613 crtc->base.enabled = crtc_state->base.enable;
15614 crtc->active = crtc_state->base.active;
15615
15616 if (crtc_state->base.active) {
15617 dev_priv->active_crtcs |= 1 << crtc->pipe;
15618
15619 if (IS_BROADWELL(dev_priv)) {
15620 pixclk = ilk_pipe_pixel_rate(crtc_state);
15621
15622 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
15623 if (crtc_state->ips_enabled)
15624 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
15625 } else if (IS_VALLEYVIEW(dev_priv) ||
15626 IS_CHERRYVIEW(dev_priv) ||
15627 IS_BROXTON(dev_priv))
15628 pixclk = crtc_state->base.adjusted_mode.crtc_clock;
15629 else
15630 WARN_ON(dev_priv->display.modeset_calc_cdclk);
15631 }
15632
15633 dev_priv->min_pixclk[crtc->pipe] = pixclk;
b70709a6 15634
f9cd7b88 15635 readout_plane_state(crtc);
24929352
DV
15636
15637 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15638 crtc->base.base.id,
15639 crtc->active ? "enabled" : "disabled");
15640 }
15641
5358901f
DV
15642 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15643 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15644
2edd6443
ACO
15645 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
15646 &pll->config.hw_state);
3e369b76 15647 pll->config.crtc_mask = 0;
d3fcc808 15648 for_each_intel_crtc(dev, crtc) {
2dd66ebd 15649 if (crtc->active && crtc->config->shared_dpll == pll)
3e369b76 15650 pll->config.crtc_mask |= 1 << crtc->pipe;
5358901f 15651 }
2dd66ebd 15652 pll->active_mask = pll->config.crtc_mask;
5358901f 15653
1e6f2ddc 15654 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
3e369b76 15655 pll->name, pll->config.crtc_mask, pll->on);
5358901f
DV
15656 }
15657
b2784e15 15658 for_each_intel_encoder(dev, encoder) {
24929352
DV
15659 pipe = 0;
15660
15661 if (encoder->get_hw_state(encoder, &pipe)) {
045ac3b5
JB
15662 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15663 encoder->base.crtc = &crtc->base;
6e3c9717 15664 encoder->get_config(encoder, crtc->config);
24929352
DV
15665 } else {
15666 encoder->base.crtc = NULL;
15667 }
15668
6f2bcceb 15669 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
24929352 15670 encoder->base.base.id,
8e329a03 15671 encoder->base.name,
24929352 15672 encoder->base.crtc ? "enabled" : "disabled",
6f2bcceb 15673 pipe_name(pipe));
24929352
DV
15674 }
15675
3a3371ff 15676 for_each_intel_connector(dev, connector) {
24929352
DV
15677 if (connector->get_hw_state(connector)) {
15678 connector->base.dpms = DRM_MODE_DPMS_ON;
2aa974c9
ML
15679
15680 encoder = connector->encoder;
15681 connector->base.encoder = &encoder->base;
15682
15683 if (encoder->base.crtc &&
15684 encoder->base.crtc->state->active) {
15685 /*
15686 * This has to be done during hardware readout
15687 * because anything calling .crtc_disable may
15688 * rely on the connector_mask being accurate.
15689 */
15690 encoder->base.crtc->state->connector_mask |=
15691 1 << drm_connector_index(&connector->base);
e87a52b3
ML
15692 encoder->base.crtc->state->encoder_mask |=
15693 1 << drm_encoder_index(&encoder->base);
2aa974c9
ML
15694 }
15695
24929352
DV
15696 } else {
15697 connector->base.dpms = DRM_MODE_DPMS_OFF;
15698 connector->base.encoder = NULL;
15699 }
15700 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15701 connector->base.base.id,
c23cc417 15702 connector->base.name,
24929352
DV
15703 connector->base.encoder ? "enabled" : "disabled");
15704 }
7f4c6284
VS
15705
15706 for_each_intel_crtc(dev, crtc) {
15707 crtc->base.hwmode = crtc->config->base.adjusted_mode;
15708
15709 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
15710 if (crtc->base.state->active) {
15711 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
15712 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
15713 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
15714
15715 /*
15716 * The initial mode needs to be set in order to keep
15717 * the atomic core happy. It wants a valid mode if the
15718 * crtc's enabled, so we do the above call.
15719 *
15720 * At this point some state updated by the connectors
15721 * in their ->detect() callback has not run yet, so
15722 * no recalculation can be done yet.
15723 *
15724 * Even if we could do a recalculation and modeset
15725 * right now it would cause a double modeset if
15726 * fbdev or userspace chooses a different initial mode.
15727 *
15728 * If that happens, someone indicated they wanted a
15729 * mode change, which means it's safe to do a full
15730 * recalculation.
15731 */
15732 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
9eca6832
VS
15733
15734 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
15735 update_scanline_offset(crtc);
7f4c6284 15736 }
e3b247da
VS
15737
15738 intel_pipe_config_sanity_check(dev_priv, crtc->config);
7f4c6284 15739 }
30e984df
DV
15740}
15741
043e9bda
ML
15742/* Scan out the current hw modeset state,
15743 * and sanitizes it to the current state
15744 */
15745static void
15746intel_modeset_setup_hw_state(struct drm_device *dev)
30e984df
DV
15747{
15748 struct drm_i915_private *dev_priv = dev->dev_private;
15749 enum pipe pipe;
30e984df
DV
15750 struct intel_crtc *crtc;
15751 struct intel_encoder *encoder;
35c95375 15752 int i;
30e984df
DV
15753
15754 intel_modeset_readout_hw_state(dev);
24929352
DV
15755
15756 /* HW state is read out, now we need to sanitize this mess. */
b2784e15 15757 for_each_intel_encoder(dev, encoder) {
24929352
DV
15758 intel_sanitize_encoder(encoder);
15759 }
15760
055e393f 15761 for_each_pipe(dev_priv, pipe) {
24929352
DV
15762 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15763 intel_sanitize_crtc(crtc);
6e3c9717
ACO
15764 intel_dump_pipe_config(crtc, crtc->config,
15765 "[setup_hw_state]");
24929352 15766 }
9a935856 15767
d29b2f9d
ACO
15768 intel_modeset_update_connector_atomic_state(dev);
15769
35c95375
DV
15770 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15771 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15772
2dd66ebd 15773 if (!pll->on || pll->active_mask)
35c95375
DV
15774 continue;
15775
15776 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15777
2edd6443 15778 pll->funcs.disable(dev_priv, pll);
35c95375
DV
15779 pll->on = false;
15780 }
15781
666a4537 15782 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
6eb1a681
VS
15783 vlv_wm_get_hw_state(dev);
15784 else if (IS_GEN9(dev))
3078999f
PB
15785 skl_wm_get_hw_state(dev);
15786 else if (HAS_PCH_SPLIT(dev))
243e6a44 15787 ilk_wm_get_hw_state(dev);
292b990e
ML
15788
15789 for_each_intel_crtc(dev, crtc) {
15790 unsigned long put_domains;
15791
74bff5f9 15792 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
292b990e
ML
15793 if (WARN_ON(put_domains))
15794 modeset_put_power_domains(dev_priv, put_domains);
15795 }
15796 intel_display_set_init_power(dev_priv, false);
010cf73d
PZ
15797
15798 intel_fbc_init_pipe_state(dev_priv);
043e9bda 15799}
7d0bc1ea 15800
043e9bda
ML
15801void intel_display_resume(struct drm_device *dev)
15802{
e2c8b870
ML
15803 struct drm_i915_private *dev_priv = to_i915(dev);
15804 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
15805 struct drm_modeset_acquire_ctx ctx;
043e9bda 15806 int ret;
e2c8b870 15807 bool setup = false;
f30da187 15808
e2c8b870 15809 dev_priv->modeset_restore_state = NULL;
043e9bda 15810
ea49c9ac
ML
15811 /*
15812 * This is a cludge because with real atomic modeset mode_config.mutex
15813 * won't be taken. Unfortunately some probed state like
15814 * audio_codec_enable is still protected by mode_config.mutex, so lock
15815 * it here for now.
15816 */
15817 mutex_lock(&dev->mode_config.mutex);
e2c8b870 15818 drm_modeset_acquire_init(&ctx, 0);
043e9bda 15819
e2c8b870
ML
15820retry:
15821 ret = drm_modeset_lock_all_ctx(dev, &ctx);
043e9bda 15822
e2c8b870
ML
15823 if (ret == 0 && !setup) {
15824 setup = true;
043e9bda 15825
e2c8b870
ML
15826 intel_modeset_setup_hw_state(dev);
15827 i915_redisable_vga(dev);
45e2b5f6 15828 }
8af6cf88 15829
e2c8b870
ML
15830 if (ret == 0 && state) {
15831 struct drm_crtc_state *crtc_state;
15832 struct drm_crtc *crtc;
15833 int i;
043e9bda 15834
e2c8b870
ML
15835 state->acquire_ctx = &ctx;
15836
15837 for_each_crtc_in_state(state, crtc, crtc_state, i) {
15838 /*
15839 * Force recalculation even if we restore
15840 * current state. With fast modeset this may not result
15841 * in a modeset when the state is compatible.
15842 */
15843 crtc_state->mode_changed = true;
15844 }
15845
15846 ret = drm_atomic_commit(state);
043e9bda
ML
15847 }
15848
e2c8b870
ML
15849 if (ret == -EDEADLK) {
15850 drm_modeset_backoff(&ctx);
15851 goto retry;
15852 }
043e9bda 15853
e2c8b870
ML
15854 drm_modeset_drop_locks(&ctx);
15855 drm_modeset_acquire_fini(&ctx);
ea49c9ac 15856 mutex_unlock(&dev->mode_config.mutex);
043e9bda 15857
e2c8b870
ML
15858 if (ret) {
15859 DRM_ERROR("Restoring old state failed with %i\n", ret);
15860 drm_atomic_state_free(state);
15861 }
2c7111db
CW
15862}
15863
15864void intel_modeset_gem_init(struct drm_device *dev)
15865{
484b41dd 15866 struct drm_crtc *c;
2ff8fde1 15867 struct drm_i915_gem_object *obj;
e0d6149b 15868 int ret;
484b41dd 15869
ae48434c 15870 intel_init_gt_powersave(dev);
ae48434c 15871
1833b134 15872 intel_modeset_init_hw(dev);
02e792fb
DV
15873
15874 intel_setup_overlay(dev);
484b41dd
JB
15875
15876 /*
15877 * Make sure any fbs we allocated at startup are properly
15878 * pinned & fenced. When we do the allocation it's too early
15879 * for this.
15880 */
70e1e0ec 15881 for_each_crtc(dev, c) {
2ff8fde1
MR
15882 obj = intel_fb_obj(c->primary->fb);
15883 if (obj == NULL)
484b41dd
JB
15884 continue;
15885
e0d6149b 15886 mutex_lock(&dev->struct_mutex);
3465c580
VS
15887 ret = intel_pin_and_fence_fb_obj(c->primary->fb,
15888 c->primary->state->rotation);
e0d6149b
TU
15889 mutex_unlock(&dev->struct_mutex);
15890 if (ret) {
484b41dd
JB
15891 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15892 to_intel_crtc(c)->pipe);
66e514c1
DA
15893 drm_framebuffer_unreference(c->primary->fb);
15894 c->primary->fb = NULL;
36750f28 15895 c->primary->crtc = c->primary->state->crtc = NULL;
afd65eb4 15896 update_state_fb(c->primary);
36750f28 15897 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
484b41dd
JB
15898 }
15899 }
0962c3c9
VS
15900
15901 intel_backlight_register(dev);
79e53945
JB
15902}
15903
4932e2c3
ID
15904void intel_connector_unregister(struct intel_connector *intel_connector)
15905{
15906 struct drm_connector *connector = &intel_connector->base;
15907
15908 intel_panel_destroy_backlight(connector);
34ea3d38 15909 drm_connector_unregister(connector);
4932e2c3
ID
15910}
15911
79e53945
JB
15912void intel_modeset_cleanup(struct drm_device *dev)
15913{
652c393a 15914 struct drm_i915_private *dev_priv = dev->dev_private;
19c8054c 15915 struct intel_connector *connector;
652c393a 15916
2eb5252e
ID
15917 intel_disable_gt_powersave(dev);
15918
0962c3c9
VS
15919 intel_backlight_unregister(dev);
15920
fd0c0642
DV
15921 /*
15922 * Interrupts and polling as the first thing to avoid creating havoc.
2eb5252e 15923 * Too much stuff here (turning of connectors, ...) would
fd0c0642
DV
15924 * experience fancy races otherwise.
15925 */
2aeb7d3a 15926 intel_irq_uninstall(dev_priv);
eb21b92b 15927
fd0c0642
DV
15928 /*
15929 * Due to the hpd irq storm handling the hotplug work can re-arm the
15930 * poll handlers. Hence disable polling after hpd handling is shut down.
15931 */
f87ea761 15932 drm_kms_helper_poll_fini(dev);
fd0c0642 15933
723bfd70
JB
15934 intel_unregister_dsm_handler();
15935
c937ab3e 15936 intel_fbc_global_disable(dev_priv);
69341a5e 15937
1630fe75
CW
15938 /* flush any delayed tasks or pending work */
15939 flush_scheduled_work();
15940
db31af1d 15941 /* destroy the backlight and sysfs files before encoders/connectors */
19c8054c
JN
15942 for_each_intel_connector(dev, connector)
15943 connector->unregister(connector);
d9255d57 15944
79e53945 15945 drm_mode_config_cleanup(dev);
4d7bb011
DV
15946
15947 intel_cleanup_overlay(dev);
ae48434c 15948
ae48434c 15949 intel_cleanup_gt_powersave(dev);
f5949141
DV
15950
15951 intel_teardown_gmbus(dev);
79e53945
JB
15952}
15953
f1c79df3
ZW
15954/*
15955 * Return which encoder is currently attached for connector.
15956 */
df0e9248 15957struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
79e53945 15958{
df0e9248
CW
15959 return &intel_attached_encoder(connector)->base;
15960}
f1c79df3 15961
df0e9248
CW
15962void intel_connector_attach_encoder(struct intel_connector *connector,
15963 struct intel_encoder *encoder)
15964{
15965 connector->encoder = encoder;
15966 drm_mode_connector_attach_encoder(&connector->base,
15967 &encoder->base);
79e53945 15968}
28d52043
DA
15969
15970/*
15971 * set vga decode state - true == enable VGA decode
15972 */
15973int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
15974{
15975 struct drm_i915_private *dev_priv = dev->dev_private;
a885b3cc 15976 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
15977 u16 gmch_ctrl;
15978
75fa041d
CW
15979 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15980 DRM_ERROR("failed to read control word\n");
15981 return -EIO;
15982 }
15983
c0cc8a55
CW
15984 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15985 return 0;
15986
28d52043
DA
15987 if (state)
15988 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15989 else
15990 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
15991
15992 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15993 DRM_ERROR("failed to write control word\n");
15994 return -EIO;
15995 }
15996
28d52043
DA
15997 return 0;
15998}
c4a1d9e4 15999
c4a1d9e4 16000struct intel_display_error_state {
ff57f1b0
PZ
16001
16002 u32 power_well_driver;
16003
63b66e5b
CW
16004 int num_transcoders;
16005
c4a1d9e4
CW
16006 struct intel_cursor_error_state {
16007 u32 control;
16008 u32 position;
16009 u32 base;
16010 u32 size;
52331309 16011 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
16012
16013 struct intel_pipe_error_state {
ddf9c536 16014 bool power_domain_on;
c4a1d9e4 16015 u32 source;
f301b1e1 16016 u32 stat;
52331309 16017 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
16018
16019 struct intel_plane_error_state {
16020 u32 control;
16021 u32 stride;
16022 u32 size;
16023 u32 pos;
16024 u32 addr;
16025 u32 surface;
16026 u32 tile_offset;
52331309 16027 } plane[I915_MAX_PIPES];
63b66e5b
CW
16028
16029 struct intel_transcoder_error_state {
ddf9c536 16030 bool power_domain_on;
63b66e5b
CW
16031 enum transcoder cpu_transcoder;
16032
16033 u32 conf;
16034
16035 u32 htotal;
16036 u32 hblank;
16037 u32 hsync;
16038 u32 vtotal;
16039 u32 vblank;
16040 u32 vsync;
16041 } transcoder[4];
c4a1d9e4
CW
16042};
16043
16044struct intel_display_error_state *
16045intel_display_capture_error_state(struct drm_device *dev)
16046{
fbee40df 16047 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4 16048 struct intel_display_error_state *error;
63b66e5b
CW
16049 int transcoders[] = {
16050 TRANSCODER_A,
16051 TRANSCODER_B,
16052 TRANSCODER_C,
16053 TRANSCODER_EDP,
16054 };
c4a1d9e4
CW
16055 int i;
16056
63b66e5b
CW
16057 if (INTEL_INFO(dev)->num_pipes == 0)
16058 return NULL;
16059
9d1cb914 16060 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
16061 if (error == NULL)
16062 return NULL;
16063
190be112 16064 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
ff57f1b0
PZ
16065 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
16066
055e393f 16067 for_each_pipe(dev_priv, i) {
ddf9c536 16068 error->pipe[i].power_domain_on =
f458ebbc
DV
16069 __intel_display_power_is_enabled(dev_priv,
16070 POWER_DOMAIN_PIPE(i));
ddf9c536 16071 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
16072 continue;
16073
5efb3e28
VS
16074 error->cursor[i].control = I915_READ(CURCNTR(i));
16075 error->cursor[i].position = I915_READ(CURPOS(i));
16076 error->cursor[i].base = I915_READ(CURBASE(i));
c4a1d9e4
CW
16077
16078 error->plane[i].control = I915_READ(DSPCNTR(i));
16079 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
80ca378b 16080 if (INTEL_INFO(dev)->gen <= 3) {
51889b35 16081 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
16082 error->plane[i].pos = I915_READ(DSPPOS(i));
16083 }
ca291363
PZ
16084 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
16085 error->plane[i].addr = I915_READ(DSPADDR(i));
c4a1d9e4
CW
16086 if (INTEL_INFO(dev)->gen >= 4) {
16087 error->plane[i].surface = I915_READ(DSPSURF(i));
16088 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
16089 }
16090
c4a1d9e4 16091 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1 16092
3abfce77 16093 if (HAS_GMCH_DISPLAY(dev))
f301b1e1 16094 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
16095 }
16096
4d1de975 16097 /* Note: this does not include DSI transcoders. */
63b66e5b
CW
16098 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
16099 if (HAS_DDI(dev_priv->dev))
16100 error->num_transcoders++; /* Account for eDP. */
16101
16102 for (i = 0; i < error->num_transcoders; i++) {
16103 enum transcoder cpu_transcoder = transcoders[i];
16104
ddf9c536 16105 error->transcoder[i].power_domain_on =
f458ebbc 16106 __intel_display_power_is_enabled(dev_priv,
38cc1daf 16107 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 16108 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
16109 continue;
16110
63b66e5b
CW
16111 error->transcoder[i].cpu_transcoder = cpu_transcoder;
16112
16113 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
16114 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
16115 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
16116 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
16117 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
16118 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
16119 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
16120 }
16121
16122 return error;
16123}
16124
edc3d884
MK
16125#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
16126
c4a1d9e4 16127void
edc3d884 16128intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
16129 struct drm_device *dev,
16130 struct intel_display_error_state *error)
16131{
055e393f 16132 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4
CW
16133 int i;
16134
63b66e5b
CW
16135 if (!error)
16136 return;
16137
edc3d884 16138 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
190be112 16139 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
edc3d884 16140 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 16141 error->power_well_driver);
055e393f 16142 for_each_pipe(dev_priv, i) {
edc3d884 16143 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536 16144 err_printf(m, " Power: %s\n",
87ad3212 16145 onoff(error->pipe[i].power_domain_on));
edc3d884 16146 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 16147 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
16148
16149 err_printf(m, "Plane [%d]:\n", i);
16150 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
16151 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
80ca378b 16152 if (INTEL_INFO(dev)->gen <= 3) {
edc3d884
MK
16153 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
16154 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 16155 }
4b71a570 16156 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
edc3d884 16157 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
c4a1d9e4 16158 if (INTEL_INFO(dev)->gen >= 4) {
edc3d884
MK
16159 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
16160 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
16161 }
16162
edc3d884
MK
16163 err_printf(m, "Cursor [%d]:\n", i);
16164 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
16165 err_printf(m, " POS: %08x\n", error->cursor[i].position);
16166 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 16167 }
63b66e5b
CW
16168
16169 for (i = 0; i < error->num_transcoders; i++) {
da205630 16170 err_printf(m, "CPU transcoder: %s\n",
63b66e5b 16171 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536 16172 err_printf(m, " Power: %s\n",
87ad3212 16173 onoff(error->transcoder[i].power_domain_on));
63b66e5b
CW
16174 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
16175 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
16176 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
16177 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
16178 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
16179 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
16180 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
16181 }
c4a1d9e4 16182}