]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpu/drm/i915/intel_display.c
drm/i915: Move fp divisor calculation into ironlake_compute_dpll()
[mirror_ubuntu-bionic-kernel.git] / drivers / gpu / drm / i915 / intel_display.c
CommitLineData
79e53945
JB
1/*
2 * Copyright © 2006-2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 */
26
618563e3 27#include <linux/dmi.h>
c1c7af60
JB
28#include <linux/module.h>
29#include <linux/input.h>
79e53945 30#include <linux/i2c.h>
7662c8bd 31#include <linux/kernel.h>
5a0e3ad6 32#include <linux/slab.h>
9cce37f4 33#include <linux/vgaarb.h>
e0dac65e 34#include <drm/drm_edid.h>
760285e7 35#include <drm/drmP.h>
79e53945 36#include "intel_drv.h"
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);
ceb41007 114static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state);
bfd16b2a
ML
115static void skylake_pfit_enable(struct intel_crtc *crtc);
116static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
117static void ironlake_pfit_enable(struct intel_crtc *crtc);
043e9bda 118static void intel_modeset_setup_hw_state(struct drm_device *dev);
2622a081 119static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
e7457a9a 120
79e53945 121typedef struct {
0206e353 122 int min, max;
79e53945
JB
123} intel_range_t;
124
125typedef struct {
0206e353
AJ
126 int dot_limit;
127 int p2_slow, p2_fast;
79e53945
JB
128} intel_p2_t;
129
d4906093
ML
130typedef struct intel_limit intel_limit_t;
131struct intel_limit {
0206e353
AJ
132 intel_range_t dot, vco, n, m, m1, m2, p, p1;
133 intel_p2_t p2;
d4906093 134};
79e53945 135
bfa7df01
VS
136/* returns HPLL frequency in kHz */
137static int valleyview_get_vco(struct drm_i915_private *dev_priv)
138{
139 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
140
141 /* Obtain SKU information */
142 mutex_lock(&dev_priv->sb_lock);
143 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
144 CCK_FUSE_HPLL_FREQ_MASK;
145 mutex_unlock(&dev_priv->sb_lock);
146
147 return vco_freq[hpll_freq] * 1000;
148}
149
150static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
151 const char *name, u32 reg)
152{
153 u32 val;
154 int divider;
155
156 if (dev_priv->hpll_freq == 0)
157 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
158
159 mutex_lock(&dev_priv->sb_lock);
160 val = vlv_cck_read(dev_priv, reg);
161 mutex_unlock(&dev_priv->sb_lock);
162
163 divider = val & CCK_FREQUENCY_VALUES;
164
165 WARN((val & CCK_FREQUENCY_STATUS) !=
166 (divider << CCK_FREQUENCY_STATUS_SHIFT),
167 "%s change in progress\n", name);
168
169 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
170}
171
e7dc33f3
VS
172static int
173intel_pch_rawclk(struct drm_i915_private *dev_priv)
d2acd215 174{
e7dc33f3
VS
175 return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
176}
d2acd215 177
e7dc33f3
VS
178static int
179intel_vlv_hrawclk(struct drm_i915_private *dev_priv)
180{
35d38d1f
VS
181 return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
182 CCK_DISPLAY_REF_CLOCK_CONTROL);
d2acd215
DV
183}
184
e7dc33f3
VS
185static int
186intel_g4x_hrawclk(struct drm_i915_private *dev_priv)
79e50a4f 187{
79e50a4f
JN
188 uint32_t clkcfg;
189
e7dc33f3 190 /* hrawclock is 1/4 the FSB frequency */
79e50a4f
JN
191 clkcfg = I915_READ(CLKCFG);
192 switch (clkcfg & CLKCFG_FSB_MASK) {
193 case CLKCFG_FSB_400:
e7dc33f3 194 return 100000;
79e50a4f 195 case CLKCFG_FSB_533:
e7dc33f3 196 return 133333;
79e50a4f 197 case CLKCFG_FSB_667:
e7dc33f3 198 return 166667;
79e50a4f 199 case CLKCFG_FSB_800:
e7dc33f3 200 return 200000;
79e50a4f 201 case CLKCFG_FSB_1067:
e7dc33f3 202 return 266667;
79e50a4f 203 case CLKCFG_FSB_1333:
e7dc33f3 204 return 333333;
79e50a4f
JN
205 /* these two are just a guess; one of them might be right */
206 case CLKCFG_FSB_1600:
207 case CLKCFG_FSB_1600_ALT:
e7dc33f3 208 return 400000;
79e50a4f 209 default:
e7dc33f3 210 return 133333;
79e50a4f
JN
211 }
212}
213
e7dc33f3
VS
214static void intel_update_rawclk(struct drm_i915_private *dev_priv)
215{
216 if (HAS_PCH_SPLIT(dev_priv))
217 dev_priv->rawclk_freq = intel_pch_rawclk(dev_priv);
218 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
219 dev_priv->rawclk_freq = intel_vlv_hrawclk(dev_priv);
220 else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
221 dev_priv->rawclk_freq = intel_g4x_hrawclk(dev_priv);
222 else
223 return; /* no rawclk on other platforms, or no need to know it */
224
225 DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
226}
227
bfa7df01
VS
228static void intel_update_czclk(struct drm_i915_private *dev_priv)
229{
666a4537 230 if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
bfa7df01
VS
231 return;
232
233 dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
234 CCK_CZ_CLOCK_CONTROL);
235
236 DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
237}
238
021357ac 239static inline u32 /* units of 100MHz */
21a727b3
VS
240intel_fdi_link_freq(struct drm_i915_private *dev_priv,
241 const struct intel_crtc_state *pipe_config)
021357ac 242{
21a727b3
VS
243 if (HAS_DDI(dev_priv))
244 return pipe_config->port_clock; /* SPLL */
245 else if (IS_GEN5(dev_priv))
246 return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
e3b247da 247 else
21a727b3 248 return 270000;
021357ac
CW
249}
250
5d536e28 251static const intel_limit_t intel_limits_i8xx_dac = {
0206e353 252 .dot = { .min = 25000, .max = 350000 },
9c333719 253 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 254 .n = { .min = 2, .max = 16 },
0206e353
AJ
255 .m = { .min = 96, .max = 140 },
256 .m1 = { .min = 18, .max = 26 },
257 .m2 = { .min = 6, .max = 16 },
258 .p = { .min = 4, .max = 128 },
259 .p1 = { .min = 2, .max = 33 },
273e27ca
EA
260 .p2 = { .dot_limit = 165000,
261 .p2_slow = 4, .p2_fast = 2 },
e4b36699
KP
262};
263
5d536e28
DV
264static const intel_limit_t intel_limits_i8xx_dvo = {
265 .dot = { .min = 25000, .max = 350000 },
9c333719 266 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 267 .n = { .min = 2, .max = 16 },
5d536e28
DV
268 .m = { .min = 96, .max = 140 },
269 .m1 = { .min = 18, .max = 26 },
270 .m2 = { .min = 6, .max = 16 },
271 .p = { .min = 4, .max = 128 },
272 .p1 = { .min = 2, .max = 33 },
273 .p2 = { .dot_limit = 165000,
274 .p2_slow = 4, .p2_fast = 4 },
275};
276
e4b36699 277static const intel_limit_t intel_limits_i8xx_lvds = {
0206e353 278 .dot = { .min = 25000, .max = 350000 },
9c333719 279 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 280 .n = { .min = 2, .max = 16 },
0206e353
AJ
281 .m = { .min = 96, .max = 140 },
282 .m1 = { .min = 18, .max = 26 },
283 .m2 = { .min = 6, .max = 16 },
284 .p = { .min = 4, .max = 128 },
285 .p1 = { .min = 1, .max = 6 },
273e27ca
EA
286 .p2 = { .dot_limit = 165000,
287 .p2_slow = 14, .p2_fast = 7 },
e4b36699 288};
273e27ca 289
e4b36699 290static const intel_limit_t intel_limits_i9xx_sdvo = {
0206e353
AJ
291 .dot = { .min = 20000, .max = 400000 },
292 .vco = { .min = 1400000, .max = 2800000 },
293 .n = { .min = 1, .max = 6 },
294 .m = { .min = 70, .max = 120 },
4f7dfb67
PJ
295 .m1 = { .min = 8, .max = 18 },
296 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
297 .p = { .min = 5, .max = 80 },
298 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
299 .p2 = { .dot_limit = 200000,
300 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
301};
302
303static const intel_limit_t intel_limits_i9xx_lvds = {
0206e353
AJ
304 .dot = { .min = 20000, .max = 400000 },
305 .vco = { .min = 1400000, .max = 2800000 },
306 .n = { .min = 1, .max = 6 },
307 .m = { .min = 70, .max = 120 },
53a7d2d1
PJ
308 .m1 = { .min = 8, .max = 18 },
309 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
310 .p = { .min = 7, .max = 98 },
311 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
312 .p2 = { .dot_limit = 112000,
313 .p2_slow = 14, .p2_fast = 7 },
e4b36699
KP
314};
315
273e27ca 316
e4b36699 317static const intel_limit_t intel_limits_g4x_sdvo = {
273e27ca
EA
318 .dot = { .min = 25000, .max = 270000 },
319 .vco = { .min = 1750000, .max = 3500000},
320 .n = { .min = 1, .max = 4 },
321 .m = { .min = 104, .max = 138 },
322 .m1 = { .min = 17, .max = 23 },
323 .m2 = { .min = 5, .max = 11 },
324 .p = { .min = 10, .max = 30 },
325 .p1 = { .min = 1, .max = 3},
326 .p2 = { .dot_limit = 270000,
327 .p2_slow = 10,
328 .p2_fast = 10
044c7c41 329 },
e4b36699
KP
330};
331
332static const intel_limit_t intel_limits_g4x_hdmi = {
273e27ca
EA
333 .dot = { .min = 22000, .max = 400000 },
334 .vco = { .min = 1750000, .max = 3500000},
335 .n = { .min = 1, .max = 4 },
336 .m = { .min = 104, .max = 138 },
337 .m1 = { .min = 16, .max = 23 },
338 .m2 = { .min = 5, .max = 11 },
339 .p = { .min = 5, .max = 80 },
340 .p1 = { .min = 1, .max = 8},
341 .p2 = { .dot_limit = 165000,
342 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
343};
344
345static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
273e27ca
EA
346 .dot = { .min = 20000, .max = 115000 },
347 .vco = { .min = 1750000, .max = 3500000 },
348 .n = { .min = 1, .max = 3 },
349 .m = { .min = 104, .max = 138 },
350 .m1 = { .min = 17, .max = 23 },
351 .m2 = { .min = 5, .max = 11 },
352 .p = { .min = 28, .max = 112 },
353 .p1 = { .min = 2, .max = 8 },
354 .p2 = { .dot_limit = 0,
355 .p2_slow = 14, .p2_fast = 14
044c7c41 356 },
e4b36699
KP
357};
358
359static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
273e27ca
EA
360 .dot = { .min = 80000, .max = 224000 },
361 .vco = { .min = 1750000, .max = 3500000 },
362 .n = { .min = 1, .max = 3 },
363 .m = { .min = 104, .max = 138 },
364 .m1 = { .min = 17, .max = 23 },
365 .m2 = { .min = 5, .max = 11 },
366 .p = { .min = 14, .max = 42 },
367 .p1 = { .min = 2, .max = 6 },
368 .p2 = { .dot_limit = 0,
369 .p2_slow = 7, .p2_fast = 7
044c7c41 370 },
e4b36699
KP
371};
372
f2b115e6 373static const intel_limit_t intel_limits_pineview_sdvo = {
0206e353
AJ
374 .dot = { .min = 20000, .max = 400000},
375 .vco = { .min = 1700000, .max = 3500000 },
273e27ca 376 /* Pineview's Ncounter is a ring counter */
0206e353
AJ
377 .n = { .min = 3, .max = 6 },
378 .m = { .min = 2, .max = 256 },
273e27ca 379 /* Pineview only has one combined m divider, which we treat as m2. */
0206e353
AJ
380 .m1 = { .min = 0, .max = 0 },
381 .m2 = { .min = 0, .max = 254 },
382 .p = { .min = 5, .max = 80 },
383 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
384 .p2 = { .dot_limit = 200000,
385 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
386};
387
f2b115e6 388static const intel_limit_t intel_limits_pineview_lvds = {
0206e353
AJ
389 .dot = { .min = 20000, .max = 400000 },
390 .vco = { .min = 1700000, .max = 3500000 },
391 .n = { .min = 3, .max = 6 },
392 .m = { .min = 2, .max = 256 },
393 .m1 = { .min = 0, .max = 0 },
394 .m2 = { .min = 0, .max = 254 },
395 .p = { .min = 7, .max = 112 },
396 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
397 .p2 = { .dot_limit = 112000,
398 .p2_slow = 14, .p2_fast = 14 },
e4b36699
KP
399};
400
273e27ca
EA
401/* Ironlake / Sandybridge
402 *
403 * We calculate clock using (register_value + 2) for N/M1/M2, so here
404 * the range value for them is (actual_value - 2).
405 */
b91ad0ec 406static const intel_limit_t intel_limits_ironlake_dac = {
273e27ca
EA
407 .dot = { .min = 25000, .max = 350000 },
408 .vco = { .min = 1760000, .max = 3510000 },
409 .n = { .min = 1, .max = 5 },
410 .m = { .min = 79, .max = 127 },
411 .m1 = { .min = 12, .max = 22 },
412 .m2 = { .min = 5, .max = 9 },
413 .p = { .min = 5, .max = 80 },
414 .p1 = { .min = 1, .max = 8 },
415 .p2 = { .dot_limit = 225000,
416 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
417};
418
b91ad0ec 419static const intel_limit_t intel_limits_ironlake_single_lvds = {
273e27ca
EA
420 .dot = { .min = 25000, .max = 350000 },
421 .vco = { .min = 1760000, .max = 3510000 },
422 .n = { .min = 1, .max = 3 },
423 .m = { .min = 79, .max = 118 },
424 .m1 = { .min = 12, .max = 22 },
425 .m2 = { .min = 5, .max = 9 },
426 .p = { .min = 28, .max = 112 },
427 .p1 = { .min = 2, .max = 8 },
428 .p2 = { .dot_limit = 225000,
429 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
430};
431
432static const intel_limit_t intel_limits_ironlake_dual_lvds = {
273e27ca
EA
433 .dot = { .min = 25000, .max = 350000 },
434 .vco = { .min = 1760000, .max = 3510000 },
435 .n = { .min = 1, .max = 3 },
436 .m = { .min = 79, .max = 127 },
437 .m1 = { .min = 12, .max = 22 },
438 .m2 = { .min = 5, .max = 9 },
439 .p = { .min = 14, .max = 56 },
440 .p1 = { .min = 2, .max = 8 },
441 .p2 = { .dot_limit = 225000,
442 .p2_slow = 7, .p2_fast = 7 },
b91ad0ec
ZW
443};
444
273e27ca 445/* LVDS 100mhz refclk limits. */
b91ad0ec 446static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
273e27ca
EA
447 .dot = { .min = 25000, .max = 350000 },
448 .vco = { .min = 1760000, .max = 3510000 },
449 .n = { .min = 1, .max = 2 },
450 .m = { .min = 79, .max = 126 },
451 .m1 = { .min = 12, .max = 22 },
452 .m2 = { .min = 5, .max = 9 },
453 .p = { .min = 28, .max = 112 },
0206e353 454 .p1 = { .min = 2, .max = 8 },
273e27ca
EA
455 .p2 = { .dot_limit = 225000,
456 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
457};
458
459static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
273e27ca
EA
460 .dot = { .min = 25000, .max = 350000 },
461 .vco = { .min = 1760000, .max = 3510000 },
462 .n = { .min = 1, .max = 3 },
463 .m = { .min = 79, .max = 126 },
464 .m1 = { .min = 12, .max = 22 },
465 .m2 = { .min = 5, .max = 9 },
466 .p = { .min = 14, .max = 42 },
0206e353 467 .p1 = { .min = 2, .max = 6 },
273e27ca
EA
468 .p2 = { .dot_limit = 225000,
469 .p2_slow = 7, .p2_fast = 7 },
4547668a
ZY
470};
471
dc730512 472static const intel_limit_t intel_limits_vlv = {
f01b7962
VS
473 /*
474 * These are the data rate limits (measured in fast clocks)
475 * since those are the strictest limits we have. The fast
476 * clock and actual rate limits are more relaxed, so checking
477 * them would make no difference.
478 */
479 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
75e53986 480 .vco = { .min = 4000000, .max = 6000000 },
a0c4da24 481 .n = { .min = 1, .max = 7 },
a0c4da24
JB
482 .m1 = { .min = 2, .max = 3 },
483 .m2 = { .min = 11, .max = 156 },
b99ab663 484 .p1 = { .min = 2, .max = 3 },
5fdc9c49 485 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
a0c4da24
JB
486};
487
ef9348c8
CML
488static const intel_limit_t intel_limits_chv = {
489 /*
490 * These are the data rate limits (measured in fast clocks)
491 * since those are the strictest limits we have. The fast
492 * clock and actual rate limits are more relaxed, so checking
493 * them would make no difference.
494 */
495 .dot = { .min = 25000 * 5, .max = 540000 * 5},
17fe1021 496 .vco = { .min = 4800000, .max = 6480000 },
ef9348c8
CML
497 .n = { .min = 1, .max = 1 },
498 .m1 = { .min = 2, .max = 2 },
499 .m2 = { .min = 24 << 22, .max = 175 << 22 },
500 .p1 = { .min = 2, .max = 4 },
501 .p2 = { .p2_slow = 1, .p2_fast = 14 },
502};
503
5ab7b0b7
ID
504static const intel_limit_t intel_limits_bxt = {
505 /* FIXME: find real dot limits */
506 .dot = { .min = 0, .max = INT_MAX },
e6292556 507 .vco = { .min = 4800000, .max = 6700000 },
5ab7b0b7
ID
508 .n = { .min = 1, .max = 1 },
509 .m1 = { .min = 2, .max = 2 },
510 /* FIXME: find real m2 limits */
511 .m2 = { .min = 2 << 22, .max = 255 << 22 },
512 .p1 = { .min = 2, .max = 4 },
513 .p2 = { .p2_slow = 1, .p2_fast = 20 },
514};
515
cdba954e
ACO
516static bool
517needs_modeset(struct drm_crtc_state *state)
518{
fc596660 519 return drm_atomic_crtc_needs_modeset(state);
cdba954e
ACO
520}
521
e0638cdf
PZ
522/**
523 * Returns whether any output on the specified pipe is of the specified type
524 */
4093561b 525bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
e0638cdf 526{
409ee761 527 struct drm_device *dev = crtc->base.dev;
e0638cdf
PZ
528 struct intel_encoder *encoder;
529
409ee761 530 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
e0638cdf
PZ
531 if (encoder->type == type)
532 return true;
533
534 return false;
535}
536
d0737e1d
ACO
537/**
538 * Returns whether any output on the specified pipe will have the specified
539 * type after a staged modeset is complete, i.e., the same as
540 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
541 * encoder->crtc.
542 */
a93e255f
ACO
543static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
544 int type)
d0737e1d 545{
a93e255f 546 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 547 struct drm_connector *connector;
a93e255f 548 struct drm_connector_state *connector_state;
d0737e1d 549 struct intel_encoder *encoder;
a93e255f
ACO
550 int i, num_connectors = 0;
551
da3ced29 552 for_each_connector_in_state(state, connector, connector_state, i) {
a93e255f
ACO
553 if (connector_state->crtc != crtc_state->base.crtc)
554 continue;
555
556 num_connectors++;
d0737e1d 557
a93e255f
ACO
558 encoder = to_intel_encoder(connector_state->best_encoder);
559 if (encoder->type == type)
d0737e1d 560 return true;
a93e255f
ACO
561 }
562
563 WARN_ON(num_connectors == 0);
d0737e1d
ACO
564
565 return false;
566}
567
a93e255f
ACO
568static const intel_limit_t *
569intel_g4x_limit(struct intel_crtc_state *crtc_state)
044c7c41 570{
a93e255f 571 struct drm_device *dev = crtc_state->base.crtc->dev;
044c7c41
ML
572 const intel_limit_t *limit;
573
a93e255f 574 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
1974cad0 575 if (intel_is_dual_link_lvds(dev))
e4b36699 576 limit = &intel_limits_g4x_dual_channel_lvds;
044c7c41 577 else
e4b36699 578 limit = &intel_limits_g4x_single_channel_lvds;
a93e255f
ACO
579 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
580 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
e4b36699 581 limit = &intel_limits_g4x_hdmi;
a93e255f 582 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
e4b36699 583 limit = &intel_limits_g4x_sdvo;
044c7c41 584 } else /* The option is for other outputs */
e4b36699 585 limit = &intel_limits_i9xx_sdvo;
044c7c41
ML
586
587 return limit;
588}
589
a93e255f
ACO
590static const intel_limit_t *
591intel_limit(struct intel_crtc_state *crtc_state, int refclk)
79e53945 592{
a93e255f 593 struct drm_device *dev = crtc_state->base.crtc->dev;
79e53945
JB
594 const intel_limit_t *limit;
595
5ab7b0b7
ID
596 if (IS_BROXTON(dev))
597 limit = &intel_limits_bxt;
8f0d5b9b
ACO
598 else if (WARN_ON(HAS_PCH_SPLIT(dev)))
599 limit = NULL;
2c07245f 600 else if (IS_G4X(dev)) {
a93e255f 601 limit = intel_g4x_limit(crtc_state);
f2b115e6 602 } else if (IS_PINEVIEW(dev)) {
a93e255f 603 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
f2b115e6 604 limit = &intel_limits_pineview_lvds;
2177832f 605 else
f2b115e6 606 limit = &intel_limits_pineview_sdvo;
ef9348c8
CML
607 } else if (IS_CHERRYVIEW(dev)) {
608 limit = &intel_limits_chv;
a0c4da24 609 } else if (IS_VALLEYVIEW(dev)) {
dc730512 610 limit = &intel_limits_vlv;
a6c45cf0 611 } else if (!IS_GEN2(dev)) {
a93e255f 612 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
a6c45cf0
CW
613 limit = &intel_limits_i9xx_lvds;
614 else
615 limit = &intel_limits_i9xx_sdvo;
79e53945 616 } else {
a93e255f 617 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
e4b36699 618 limit = &intel_limits_i8xx_lvds;
a93e255f 619 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
e4b36699 620 limit = &intel_limits_i8xx_dvo;
5d536e28
DV
621 else
622 limit = &intel_limits_i8xx_dac;
79e53945
JB
623 }
624 return limit;
625}
626
dccbea3b
ID
627/*
628 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
629 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
630 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
631 * The helpers' return value is the rate of the clock that is fed to the
632 * display engine's pipe which can be the above fast dot clock rate or a
633 * divided-down version of it.
634 */
f2b115e6 635/* m1 is reserved as 0 in Pineview, n is a ring counter */
dccbea3b 636static int pnv_calc_dpll_params(int refclk, intel_clock_t *clock)
79e53945 637{
2177832f
SL
638 clock->m = clock->m2 + 2;
639 clock->p = clock->p1 * clock->p2;
ed5ca77e 640 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 641 return 0;
fb03ac01
VS
642 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
643 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
644
645 return clock->dot;
2177832f
SL
646}
647
7429e9d4
DV
648static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
649{
650 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
651}
652
dccbea3b 653static int i9xx_calc_dpll_params(int refclk, intel_clock_t *clock)
2177832f 654{
7429e9d4 655 clock->m = i9xx_dpll_compute_m(clock);
79e53945 656 clock->p = clock->p1 * clock->p2;
ed5ca77e 657 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
dccbea3b 658 return 0;
fb03ac01
VS
659 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
660 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
661
662 return clock->dot;
79e53945
JB
663}
664
dccbea3b 665static int vlv_calc_dpll_params(int refclk, intel_clock_t *clock)
589eca67
ID
666{
667 clock->m = clock->m1 * clock->m2;
668 clock->p = clock->p1 * clock->p2;
669 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 670 return 0;
589eca67
ID
671 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
672 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
673
674 return clock->dot / 5;
589eca67
ID
675}
676
dccbea3b 677int chv_calc_dpll_params(int refclk, intel_clock_t *clock)
ef9348c8
CML
678{
679 clock->m = clock->m1 * clock->m2;
680 clock->p = clock->p1 * clock->p2;
681 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 682 return 0;
ef9348c8
CML
683 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
684 clock->n << 22);
685 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
686
687 return clock->dot / 5;
ef9348c8
CML
688}
689
7c04d1d9 690#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
79e53945
JB
691/**
692 * Returns whether the given set of divisors are valid for a given refclk with
693 * the given connectors.
694 */
695
1b894b59
CW
696static bool intel_PLL_is_valid(struct drm_device *dev,
697 const intel_limit_t *limit,
698 const intel_clock_t *clock)
79e53945 699{
f01b7962
VS
700 if (clock->n < limit->n.min || limit->n.max < clock->n)
701 INTELPllInvalid("n out of range\n");
79e53945 702 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
0206e353 703 INTELPllInvalid("p1 out of range\n");
79e53945 704 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
0206e353 705 INTELPllInvalid("m2 out of range\n");
79e53945 706 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
0206e353 707 INTELPllInvalid("m1 out of range\n");
f01b7962 708
666a4537
WB
709 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) &&
710 !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev))
f01b7962
VS
711 if (clock->m1 <= clock->m2)
712 INTELPllInvalid("m1 <= m2\n");
713
666a4537 714 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev)) {
f01b7962
VS
715 if (clock->p < limit->p.min || limit->p.max < clock->p)
716 INTELPllInvalid("p out of range\n");
717 if (clock->m < limit->m.min || limit->m.max < clock->m)
718 INTELPllInvalid("m out of range\n");
719 }
720
79e53945 721 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
0206e353 722 INTELPllInvalid("vco out of range\n");
79e53945
JB
723 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
724 * connector, etc., rather than just a single range.
725 */
726 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
0206e353 727 INTELPllInvalid("dot out of range\n");
79e53945
JB
728
729 return true;
730}
731
3b1429d9
VS
732static int
733i9xx_select_p2_div(const intel_limit_t *limit,
734 const struct intel_crtc_state *crtc_state,
735 int target)
79e53945 736{
3b1429d9 737 struct drm_device *dev = crtc_state->base.crtc->dev;
79e53945 738
a93e255f 739 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
79e53945 740 /*
a210b028
DV
741 * For LVDS just rely on its current settings for dual-channel.
742 * We haven't figured out how to reliably set up different
743 * single/dual channel state, if we even can.
79e53945 744 */
1974cad0 745 if (intel_is_dual_link_lvds(dev))
3b1429d9 746 return limit->p2.p2_fast;
79e53945 747 else
3b1429d9 748 return limit->p2.p2_slow;
79e53945
JB
749 } else {
750 if (target < limit->p2.dot_limit)
3b1429d9 751 return limit->p2.p2_slow;
79e53945 752 else
3b1429d9 753 return limit->p2.p2_fast;
79e53945 754 }
3b1429d9
VS
755}
756
757static bool
758i9xx_find_best_dpll(const intel_limit_t *limit,
759 struct intel_crtc_state *crtc_state,
760 int target, int refclk, intel_clock_t *match_clock,
761 intel_clock_t *best_clock)
762{
763 struct drm_device *dev = crtc_state->base.crtc->dev;
764 intel_clock_t clock;
765 int err = target;
79e53945 766
0206e353 767 memset(best_clock, 0, sizeof(*best_clock));
79e53945 768
3b1429d9
VS
769 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
770
42158660
ZY
771 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
772 clock.m1++) {
773 for (clock.m2 = limit->m2.min;
774 clock.m2 <= limit->m2.max; clock.m2++) {
c0efc387 775 if (clock.m2 >= clock.m1)
42158660
ZY
776 break;
777 for (clock.n = limit->n.min;
778 clock.n <= limit->n.max; clock.n++) {
779 for (clock.p1 = limit->p1.min;
780 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
781 int this_err;
782
dccbea3b 783 i9xx_calc_dpll_params(refclk, &clock);
ac58c3f0
DV
784 if (!intel_PLL_is_valid(dev, limit,
785 &clock))
786 continue;
787 if (match_clock &&
788 clock.p != match_clock->p)
789 continue;
790
791 this_err = abs(clock.dot - target);
792 if (this_err < err) {
793 *best_clock = clock;
794 err = this_err;
795 }
796 }
797 }
798 }
799 }
800
801 return (err != target);
802}
803
804static bool
a93e255f
ACO
805pnv_find_best_dpll(const intel_limit_t *limit,
806 struct intel_crtc_state *crtc_state,
ee9300bb
DV
807 int target, int refclk, intel_clock_t *match_clock,
808 intel_clock_t *best_clock)
79e53945 809{
3b1429d9 810 struct drm_device *dev = crtc_state->base.crtc->dev;
79e53945 811 intel_clock_t clock;
79e53945
JB
812 int err = target;
813
0206e353 814 memset(best_clock, 0, sizeof(*best_clock));
79e53945 815
3b1429d9
VS
816 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
817
42158660
ZY
818 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
819 clock.m1++) {
820 for (clock.m2 = limit->m2.min;
821 clock.m2 <= limit->m2.max; clock.m2++) {
42158660
ZY
822 for (clock.n = limit->n.min;
823 clock.n <= limit->n.max; clock.n++) {
824 for (clock.p1 = limit->p1.min;
825 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
826 int this_err;
827
dccbea3b 828 pnv_calc_dpll_params(refclk, &clock);
1b894b59
CW
829 if (!intel_PLL_is_valid(dev, limit,
830 &clock))
79e53945 831 continue;
cec2f356
SP
832 if (match_clock &&
833 clock.p != match_clock->p)
834 continue;
79e53945
JB
835
836 this_err = abs(clock.dot - target);
837 if (this_err < err) {
838 *best_clock = clock;
839 err = this_err;
840 }
841 }
842 }
843 }
844 }
845
846 return (err != target);
847}
848
d4906093 849static bool
a93e255f
ACO
850g4x_find_best_dpll(const intel_limit_t *limit,
851 struct intel_crtc_state *crtc_state,
ee9300bb
DV
852 int target, int refclk, intel_clock_t *match_clock,
853 intel_clock_t *best_clock)
d4906093 854{
3b1429d9 855 struct drm_device *dev = crtc_state->base.crtc->dev;
d4906093
ML
856 intel_clock_t clock;
857 int max_n;
3b1429d9 858 bool found = false;
6ba770dc
AJ
859 /* approximately equals target * 0.00585 */
860 int err_most = (target >> 8) + (target >> 9);
d4906093
ML
861
862 memset(best_clock, 0, sizeof(*best_clock));
3b1429d9
VS
863
864 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
865
d4906093 866 max_n = limit->n.max;
f77f13e2 867 /* based on hardware requirement, prefer smaller n to precision */
d4906093 868 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
f77f13e2 869 /* based on hardware requirement, prefere larger m1,m2 */
d4906093
ML
870 for (clock.m1 = limit->m1.max;
871 clock.m1 >= limit->m1.min; clock.m1--) {
872 for (clock.m2 = limit->m2.max;
873 clock.m2 >= limit->m2.min; clock.m2--) {
874 for (clock.p1 = limit->p1.max;
875 clock.p1 >= limit->p1.min; clock.p1--) {
876 int this_err;
877
dccbea3b 878 i9xx_calc_dpll_params(refclk, &clock);
1b894b59
CW
879 if (!intel_PLL_is_valid(dev, limit,
880 &clock))
d4906093 881 continue;
1b894b59
CW
882
883 this_err = abs(clock.dot - target);
d4906093
ML
884 if (this_err < err_most) {
885 *best_clock = clock;
886 err_most = this_err;
887 max_n = clock.n;
888 found = true;
889 }
890 }
891 }
892 }
893 }
2c07245f
ZW
894 return found;
895}
896
d5dd62bd
ID
897/*
898 * Check if the calculated PLL configuration is more optimal compared to the
899 * best configuration and error found so far. Return the calculated error.
900 */
901static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
902 const intel_clock_t *calculated_clock,
903 const intel_clock_t *best_clock,
904 unsigned int best_error_ppm,
905 unsigned int *error_ppm)
906{
9ca3ba01
ID
907 /*
908 * For CHV ignore the error and consider only the P value.
909 * Prefer a bigger P value based on HW requirements.
910 */
911 if (IS_CHERRYVIEW(dev)) {
912 *error_ppm = 0;
913
914 return calculated_clock->p > best_clock->p;
915 }
916
24be4e46
ID
917 if (WARN_ON_ONCE(!target_freq))
918 return false;
919
d5dd62bd
ID
920 *error_ppm = div_u64(1000000ULL *
921 abs(target_freq - calculated_clock->dot),
922 target_freq);
923 /*
924 * Prefer a better P value over a better (smaller) error if the error
925 * is small. Ensure this preference for future configurations too by
926 * setting the error to 0.
927 */
928 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
929 *error_ppm = 0;
930
931 return true;
932 }
933
934 return *error_ppm + 10 < best_error_ppm;
935}
936
a0c4da24 937static bool
a93e255f
ACO
938vlv_find_best_dpll(const intel_limit_t *limit,
939 struct intel_crtc_state *crtc_state,
ee9300bb
DV
940 int target, int refclk, intel_clock_t *match_clock,
941 intel_clock_t *best_clock)
a0c4da24 942{
a93e255f 943 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 944 struct drm_device *dev = crtc->base.dev;
6b4bf1c4 945 intel_clock_t clock;
69e4f900 946 unsigned int bestppm = 1000000;
27e639bf
VS
947 /* min update 19.2 MHz */
948 int max_n = min(limit->n.max, refclk / 19200);
49e497ef 949 bool found = false;
a0c4da24 950
6b4bf1c4
VS
951 target *= 5; /* fast clock */
952
953 memset(best_clock, 0, sizeof(*best_clock));
a0c4da24
JB
954
955 /* based on hardware requirement, prefer smaller n to precision */
27e639bf 956 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
811bbf05 957 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
889059d8 958 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
c1a9ae43 959 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
6b4bf1c4 960 clock.p = clock.p1 * clock.p2;
a0c4da24 961 /* based on hardware requirement, prefer bigger m1,m2 values */
6b4bf1c4 962 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
d5dd62bd 963 unsigned int ppm;
69e4f900 964
6b4bf1c4
VS
965 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
966 refclk * clock.m1);
967
dccbea3b 968 vlv_calc_dpll_params(refclk, &clock);
43b0ac53 969
f01b7962
VS
970 if (!intel_PLL_is_valid(dev, limit,
971 &clock))
43b0ac53
VS
972 continue;
973
d5dd62bd
ID
974 if (!vlv_PLL_is_optimal(dev, target,
975 &clock,
976 best_clock,
977 bestppm, &ppm))
978 continue;
6b4bf1c4 979
d5dd62bd
ID
980 *best_clock = clock;
981 bestppm = ppm;
982 found = true;
a0c4da24
JB
983 }
984 }
985 }
986 }
a0c4da24 987
49e497ef 988 return found;
a0c4da24 989}
a4fc5ed6 990
ef9348c8 991static bool
a93e255f
ACO
992chv_find_best_dpll(const intel_limit_t *limit,
993 struct intel_crtc_state *crtc_state,
ef9348c8
CML
994 int target, int refclk, intel_clock_t *match_clock,
995 intel_clock_t *best_clock)
996{
a93e255f 997 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 998 struct drm_device *dev = crtc->base.dev;
9ca3ba01 999 unsigned int best_error_ppm;
ef9348c8
CML
1000 intel_clock_t clock;
1001 uint64_t m2;
1002 int found = false;
1003
1004 memset(best_clock, 0, sizeof(*best_clock));
9ca3ba01 1005 best_error_ppm = 1000000;
ef9348c8
CML
1006
1007 /*
1008 * Based on hardware doc, the n always set to 1, and m1 always
1009 * set to 2. If requires to support 200Mhz refclk, we need to
1010 * revisit this because n may not 1 anymore.
1011 */
1012 clock.n = 1, clock.m1 = 2;
1013 target *= 5; /* fast clock */
1014
1015 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
1016 for (clock.p2 = limit->p2.p2_fast;
1017 clock.p2 >= limit->p2.p2_slow;
1018 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
9ca3ba01 1019 unsigned int error_ppm;
ef9348c8
CML
1020
1021 clock.p = clock.p1 * clock.p2;
1022
1023 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
1024 clock.n) << 22, refclk * clock.m1);
1025
1026 if (m2 > INT_MAX/clock.m1)
1027 continue;
1028
1029 clock.m2 = m2;
1030
dccbea3b 1031 chv_calc_dpll_params(refclk, &clock);
ef9348c8
CML
1032
1033 if (!intel_PLL_is_valid(dev, limit, &clock))
1034 continue;
1035
9ca3ba01
ID
1036 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
1037 best_error_ppm, &error_ppm))
1038 continue;
1039
1040 *best_clock = clock;
1041 best_error_ppm = error_ppm;
1042 found = true;
ef9348c8
CML
1043 }
1044 }
1045
1046 return found;
1047}
1048
5ab7b0b7
ID
1049bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
1050 intel_clock_t *best_clock)
1051{
ceb41007 1052 int refclk = i9xx_get_refclk(crtc_state);
5ab7b0b7
ID
1053
1054 return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
1055 target_clock, refclk, NULL, best_clock);
1056}
1057
20ddf665
VS
1058bool intel_crtc_active(struct drm_crtc *crtc)
1059{
1060 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1061
1062 /* Be paranoid as we can arrive here with only partial
1063 * state retrieved from the hardware during setup.
1064 *
241bfc38 1065 * We can ditch the adjusted_mode.crtc_clock check as soon
20ddf665
VS
1066 * as Haswell has gained clock readout/fastboot support.
1067 *
66e514c1 1068 * We can ditch the crtc->primary->fb check as soon as we can
20ddf665 1069 * properly reconstruct framebuffers.
c3d1f436
MR
1070 *
1071 * FIXME: The intel_crtc->active here should be switched to
1072 * crtc->state->active once we have proper CRTC states wired up
1073 * for atomic.
20ddf665 1074 */
c3d1f436 1075 return intel_crtc->active && crtc->primary->state->fb &&
6e3c9717 1076 intel_crtc->config->base.adjusted_mode.crtc_clock;
20ddf665
VS
1077}
1078
a5c961d1
PZ
1079enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1080 enum pipe pipe)
1081{
1082 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1083 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1084
6e3c9717 1085 return intel_crtc->config->cpu_transcoder;
a5c961d1
PZ
1086}
1087
fbf49ea2
VS
1088static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1089{
1090 struct drm_i915_private *dev_priv = dev->dev_private;
f0f59a00 1091 i915_reg_t reg = PIPEDSL(pipe);
fbf49ea2
VS
1092 u32 line1, line2;
1093 u32 line_mask;
1094
1095 if (IS_GEN2(dev))
1096 line_mask = DSL_LINEMASK_GEN2;
1097 else
1098 line_mask = DSL_LINEMASK_GEN3;
1099
1100 line1 = I915_READ(reg) & line_mask;
6adfb1ef 1101 msleep(5);
fbf49ea2
VS
1102 line2 = I915_READ(reg) & line_mask;
1103
1104 return line1 == line2;
1105}
1106
ab7ad7f6
KP
1107/*
1108 * intel_wait_for_pipe_off - wait for pipe to turn off
575f7ab7 1109 * @crtc: crtc whose pipe to wait for
9d0498a2
JB
1110 *
1111 * After disabling a pipe, we can't wait for vblank in the usual way,
1112 * spinning on the vblank interrupt status bit, since we won't actually
1113 * see an interrupt when the pipe is disabled.
1114 *
ab7ad7f6
KP
1115 * On Gen4 and above:
1116 * wait for the pipe register state bit to turn off
1117 *
1118 * Otherwise:
1119 * wait for the display line value to settle (it usually
1120 * ends up stopping at the start of the next frame).
58e10eb9 1121 *
9d0498a2 1122 */
575f7ab7 1123static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
9d0498a2 1124{
575f7ab7 1125 struct drm_device *dev = crtc->base.dev;
9d0498a2 1126 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 1127 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 1128 enum pipe pipe = crtc->pipe;
ab7ad7f6
KP
1129
1130 if (INTEL_INFO(dev)->gen >= 4) {
f0f59a00 1131 i915_reg_t reg = PIPECONF(cpu_transcoder);
ab7ad7f6
KP
1132
1133 /* Wait for the Pipe State to go off */
58e10eb9
CW
1134 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1135 100))
284637d9 1136 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1137 } else {
ab7ad7f6 1138 /* Wait for the display line to settle */
fbf49ea2 1139 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
284637d9 1140 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1141 }
79e53945
JB
1142}
1143
b24e7179 1144/* Only for pre-ILK configs */
55607e8a
DV
1145void assert_pll(struct drm_i915_private *dev_priv,
1146 enum pipe pipe, bool state)
b24e7179 1147{
b24e7179
JB
1148 u32 val;
1149 bool cur_state;
1150
649636ef 1151 val = I915_READ(DPLL(pipe));
b24e7179 1152 cur_state = !!(val & DPLL_VCO_ENABLE);
e2c719b7 1153 I915_STATE_WARN(cur_state != state,
b24e7179 1154 "PLL state assertion failure (expected %s, current %s)\n",
87ad3212 1155 onoff(state), onoff(cur_state));
b24e7179 1156}
b24e7179 1157
23538ef1 1158/* XXX: the dsi pll is shared between MIPI DSI ports */
8563b1e8 1159void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
23538ef1
JN
1160{
1161 u32 val;
1162 bool cur_state;
1163
a580516d 1164 mutex_lock(&dev_priv->sb_lock);
23538ef1 1165 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
a580516d 1166 mutex_unlock(&dev_priv->sb_lock);
23538ef1
JN
1167
1168 cur_state = val & DSI_PLL_VCO_EN;
e2c719b7 1169 I915_STATE_WARN(cur_state != state,
23538ef1 1170 "DSI PLL state assertion failure (expected %s, current %s)\n",
87ad3212 1171 onoff(state), onoff(cur_state));
23538ef1 1172}
23538ef1 1173
040484af
JB
1174static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1175 enum pipe pipe, bool state)
1176{
040484af 1177 bool cur_state;
ad80a810
PZ
1178 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1179 pipe);
040484af 1180
affa9354
PZ
1181 if (HAS_DDI(dev_priv->dev)) {
1182 /* DDI does not have a specific FDI_TX register */
649636ef 1183 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
ad80a810 1184 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
bf507ef7 1185 } else {
649636ef 1186 u32 val = I915_READ(FDI_TX_CTL(pipe));
bf507ef7
ED
1187 cur_state = !!(val & FDI_TX_ENABLE);
1188 }
e2c719b7 1189 I915_STATE_WARN(cur_state != state,
040484af 1190 "FDI TX state assertion failure (expected %s, current %s)\n",
87ad3212 1191 onoff(state), onoff(cur_state));
040484af
JB
1192}
1193#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1194#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1195
1196static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1197 enum pipe pipe, bool state)
1198{
040484af
JB
1199 u32 val;
1200 bool cur_state;
1201
649636ef 1202 val = I915_READ(FDI_RX_CTL(pipe));
d63fa0dc 1203 cur_state = !!(val & FDI_RX_ENABLE);
e2c719b7 1204 I915_STATE_WARN(cur_state != state,
040484af 1205 "FDI RX state assertion failure (expected %s, current %s)\n",
87ad3212 1206 onoff(state), onoff(cur_state));
040484af
JB
1207}
1208#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1209#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1210
1211static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1212 enum pipe pipe)
1213{
040484af
JB
1214 u32 val;
1215
1216 /* ILK FDI PLL is always enabled */
3d13ef2e 1217 if (INTEL_INFO(dev_priv->dev)->gen == 5)
040484af
JB
1218 return;
1219
bf507ef7 1220 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
affa9354 1221 if (HAS_DDI(dev_priv->dev))
bf507ef7
ED
1222 return;
1223
649636ef 1224 val = I915_READ(FDI_TX_CTL(pipe));
e2c719b7 1225 I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
040484af
JB
1226}
1227
55607e8a
DV
1228void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1229 enum pipe pipe, bool state)
040484af 1230{
040484af 1231 u32 val;
55607e8a 1232 bool cur_state;
040484af 1233
649636ef 1234 val = I915_READ(FDI_RX_CTL(pipe));
55607e8a 1235 cur_state = !!(val & FDI_RX_PLL_ENABLE);
e2c719b7 1236 I915_STATE_WARN(cur_state != state,
55607e8a 1237 "FDI RX PLL assertion failure (expected %s, current %s)\n",
87ad3212 1238 onoff(state), onoff(cur_state));
040484af
JB
1239}
1240
b680c37a
DV
1241void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1242 enum pipe pipe)
ea0760cf 1243{
bedd4dba 1244 struct drm_device *dev = dev_priv->dev;
f0f59a00 1245 i915_reg_t pp_reg;
ea0760cf
JB
1246 u32 val;
1247 enum pipe panel_pipe = PIPE_A;
0de3b485 1248 bool locked = true;
ea0760cf 1249
bedd4dba
JN
1250 if (WARN_ON(HAS_DDI(dev)))
1251 return;
1252
1253 if (HAS_PCH_SPLIT(dev)) {
1254 u32 port_sel;
1255
ea0760cf 1256 pp_reg = PCH_PP_CONTROL;
bedd4dba
JN
1257 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1258
1259 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1260 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1261 panel_pipe = PIPE_B;
1262 /* XXX: else fix for eDP */
666a4537 1263 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
bedd4dba
JN
1264 /* presumably write lock depends on pipe, not port select */
1265 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1266 panel_pipe = pipe;
ea0760cf
JB
1267 } else {
1268 pp_reg = PP_CONTROL;
bedd4dba
JN
1269 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1270 panel_pipe = PIPE_B;
ea0760cf
JB
1271 }
1272
1273 val = I915_READ(pp_reg);
1274 if (!(val & PANEL_POWER_ON) ||
ec49ba2d 1275 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
ea0760cf
JB
1276 locked = false;
1277
e2c719b7 1278 I915_STATE_WARN(panel_pipe == pipe && locked,
ea0760cf 1279 "panel assertion failure, pipe %c regs locked\n",
9db4a9c7 1280 pipe_name(pipe));
ea0760cf
JB
1281}
1282
93ce0ba6
JN
1283static void assert_cursor(struct drm_i915_private *dev_priv,
1284 enum pipe pipe, bool state)
1285{
1286 struct drm_device *dev = dev_priv->dev;
1287 bool cur_state;
1288
d9d82081 1289 if (IS_845G(dev) || IS_I865G(dev))
0b87c24e 1290 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
d9d82081 1291 else
5efb3e28 1292 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
93ce0ba6 1293
e2c719b7 1294 I915_STATE_WARN(cur_state != state,
93ce0ba6 1295 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
87ad3212 1296 pipe_name(pipe), onoff(state), onoff(cur_state));
93ce0ba6
JN
1297}
1298#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1299#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1300
b840d907
JB
1301void assert_pipe(struct drm_i915_private *dev_priv,
1302 enum pipe pipe, bool state)
b24e7179 1303{
63d7bbe9 1304 bool cur_state;
702e7a56
PZ
1305 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1306 pipe);
4feed0eb 1307 enum intel_display_power_domain power_domain;
b24e7179 1308
b6b5d049
VS
1309 /* if we need the pipe quirk it must be always on */
1310 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1311 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
8e636784
DV
1312 state = true;
1313
4feed0eb
ID
1314 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1315 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
649636ef 1316 u32 val = I915_READ(PIPECONF(cpu_transcoder));
69310161 1317 cur_state = !!(val & PIPECONF_ENABLE);
4feed0eb
ID
1318
1319 intel_display_power_put(dev_priv, power_domain);
1320 } else {
1321 cur_state = false;
69310161
PZ
1322 }
1323
e2c719b7 1324 I915_STATE_WARN(cur_state != state,
63d7bbe9 1325 "pipe %c assertion failure (expected %s, current %s)\n",
87ad3212 1326 pipe_name(pipe), onoff(state), onoff(cur_state));
b24e7179
JB
1327}
1328
931872fc
CW
1329static void assert_plane(struct drm_i915_private *dev_priv,
1330 enum plane plane, bool state)
b24e7179 1331{
b24e7179 1332 u32 val;
931872fc 1333 bool cur_state;
b24e7179 1334
649636ef 1335 val = I915_READ(DSPCNTR(plane));
931872fc 1336 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
e2c719b7 1337 I915_STATE_WARN(cur_state != state,
931872fc 1338 "plane %c assertion failure (expected %s, current %s)\n",
87ad3212 1339 plane_name(plane), onoff(state), onoff(cur_state));
b24e7179
JB
1340}
1341
931872fc
CW
1342#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1343#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1344
b24e7179
JB
1345static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1346 enum pipe pipe)
1347{
653e1026 1348 struct drm_device *dev = dev_priv->dev;
649636ef 1349 int i;
b24e7179 1350
653e1026
VS
1351 /* Primary planes are fixed to pipes on gen4+ */
1352 if (INTEL_INFO(dev)->gen >= 4) {
649636ef 1353 u32 val = I915_READ(DSPCNTR(pipe));
e2c719b7 1354 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
28c05794
AJ
1355 "plane %c assertion failure, should be disabled but not\n",
1356 plane_name(pipe));
19ec1358 1357 return;
28c05794 1358 }
19ec1358 1359
b24e7179 1360 /* Need to check both planes against the pipe */
055e393f 1361 for_each_pipe(dev_priv, i) {
649636ef
VS
1362 u32 val = I915_READ(DSPCNTR(i));
1363 enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
b24e7179 1364 DISPPLANE_SEL_PIPE_SHIFT;
e2c719b7 1365 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
9db4a9c7
JB
1366 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1367 plane_name(i), pipe_name(pipe));
b24e7179
JB
1368 }
1369}
1370
19332d7a
JB
1371static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1372 enum pipe pipe)
1373{
20674eef 1374 struct drm_device *dev = dev_priv->dev;
649636ef 1375 int sprite;
19332d7a 1376
7feb8b88 1377 if (INTEL_INFO(dev)->gen >= 9) {
3bdcfc0c 1378 for_each_sprite(dev_priv, pipe, sprite) {
649636ef 1379 u32 val = I915_READ(PLANE_CTL(pipe, sprite));
e2c719b7 1380 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
7feb8b88
DL
1381 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1382 sprite, pipe_name(pipe));
1383 }
666a4537 1384 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
3bdcfc0c 1385 for_each_sprite(dev_priv, pipe, sprite) {
649636ef 1386 u32 val = I915_READ(SPCNTR(pipe, sprite));
e2c719b7 1387 I915_STATE_WARN(val & SP_ENABLE,
20674eef 1388 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1fe47785 1389 sprite_name(pipe, sprite), pipe_name(pipe));
20674eef
VS
1390 }
1391 } else if (INTEL_INFO(dev)->gen >= 7) {
649636ef 1392 u32 val = I915_READ(SPRCTL(pipe));
e2c719b7 1393 I915_STATE_WARN(val & SPRITE_ENABLE,
06da8da2 1394 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef
VS
1395 plane_name(pipe), pipe_name(pipe));
1396 } else if (INTEL_INFO(dev)->gen >= 5) {
649636ef 1397 u32 val = I915_READ(DVSCNTR(pipe));
e2c719b7 1398 I915_STATE_WARN(val & DVS_ENABLE,
06da8da2 1399 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef 1400 plane_name(pipe), pipe_name(pipe));
19332d7a
JB
1401 }
1402}
1403
08c71e5e
VS
1404static void assert_vblank_disabled(struct drm_crtc *crtc)
1405{
e2c719b7 1406 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
08c71e5e
VS
1407 drm_crtc_vblank_put(crtc);
1408}
1409
7abd4b35
ACO
1410void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1411 enum pipe pipe)
92f2584a 1412{
92f2584a
JB
1413 u32 val;
1414 bool enabled;
1415
649636ef 1416 val = I915_READ(PCH_TRANSCONF(pipe));
92f2584a 1417 enabled = !!(val & TRANS_ENABLE);
e2c719b7 1418 I915_STATE_WARN(enabled,
9db4a9c7
JB
1419 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1420 pipe_name(pipe));
92f2584a
JB
1421}
1422
4e634389
KP
1423static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1424 enum pipe pipe, u32 port_sel, u32 val)
f0575e92
KP
1425{
1426 if ((val & DP_PORT_EN) == 0)
1427 return false;
1428
1429 if (HAS_PCH_CPT(dev_priv->dev)) {
f0f59a00 1430 u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
f0575e92
KP
1431 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1432 return false;
44f37d1f
CML
1433 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1434 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1435 return false;
f0575e92
KP
1436 } else {
1437 if ((val & DP_PIPE_MASK) != (pipe << 30))
1438 return false;
1439 }
1440 return true;
1441}
1442
1519b995
KP
1443static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1444 enum pipe pipe, u32 val)
1445{
dc0fa718 1446 if ((val & SDVO_ENABLE) == 0)
1519b995
KP
1447 return false;
1448
1449 if (HAS_PCH_CPT(dev_priv->dev)) {
dc0fa718 1450 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1519b995 1451 return false;
44f37d1f
CML
1452 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1453 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1454 return false;
1519b995 1455 } else {
dc0fa718 1456 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1519b995
KP
1457 return false;
1458 }
1459 return true;
1460}
1461
1462static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1463 enum pipe pipe, u32 val)
1464{
1465 if ((val & LVDS_PORT_EN) == 0)
1466 return false;
1467
1468 if (HAS_PCH_CPT(dev_priv->dev)) {
1469 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1470 return false;
1471 } else {
1472 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1473 return false;
1474 }
1475 return true;
1476}
1477
1478static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1479 enum pipe pipe, u32 val)
1480{
1481 if ((val & ADPA_DAC_ENABLE) == 0)
1482 return false;
1483 if (HAS_PCH_CPT(dev_priv->dev)) {
1484 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1485 return false;
1486 } else {
1487 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1488 return false;
1489 }
1490 return true;
1491}
1492
291906f1 1493static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
f0f59a00
VS
1494 enum pipe pipe, i915_reg_t reg,
1495 u32 port_sel)
291906f1 1496{
47a05eca 1497 u32 val = I915_READ(reg);
e2c719b7 1498 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
291906f1 1499 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
f0f59a00 1500 i915_mmio_reg_offset(reg), pipe_name(pipe));
de9a35ab 1501
e2c719b7 1502 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
75c5da27 1503 && (val & DP_PIPEB_SELECT),
de9a35ab 1504 "IBX PCH dp port still using transcoder B\n");
291906f1
JB
1505}
1506
1507static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
f0f59a00 1508 enum pipe pipe, i915_reg_t reg)
291906f1 1509{
47a05eca 1510 u32 val = I915_READ(reg);
e2c719b7 1511 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
23c99e77 1512 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
f0f59a00 1513 i915_mmio_reg_offset(reg), pipe_name(pipe));
de9a35ab 1514
e2c719b7 1515 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
75c5da27 1516 && (val & SDVO_PIPE_B_SELECT),
de9a35ab 1517 "IBX PCH hdmi port still using transcoder B\n");
291906f1
JB
1518}
1519
1520static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1521 enum pipe pipe)
1522{
291906f1 1523 u32 val;
291906f1 1524
f0575e92
KP
1525 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1526 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1527 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
291906f1 1528
649636ef 1529 val = I915_READ(PCH_ADPA);
e2c719b7 1530 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
291906f1 1531 "PCH VGA enabled on transcoder %c, should be disabled\n",
9db4a9c7 1532 pipe_name(pipe));
291906f1 1533
649636ef 1534 val = I915_READ(PCH_LVDS);
e2c719b7 1535 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
291906f1 1536 "PCH LVDS enabled on transcoder %c, should be disabled\n",
9db4a9c7 1537 pipe_name(pipe));
291906f1 1538
e2debe91
PZ
1539 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1540 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1541 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
291906f1
JB
1542}
1543
d288f65f 1544static void vlv_enable_pll(struct intel_crtc *crtc,
5cec258b 1545 const struct intel_crtc_state *pipe_config)
87442f73 1546{
426115cf
DV
1547 struct drm_device *dev = crtc->base.dev;
1548 struct drm_i915_private *dev_priv = dev->dev_private;
f0f59a00 1549 i915_reg_t reg = DPLL(crtc->pipe);
d288f65f 1550 u32 dpll = pipe_config->dpll_hw_state.dpll;
87442f73 1551
426115cf 1552 assert_pipe_disabled(dev_priv, crtc->pipe);
87442f73 1553
87442f73 1554 /* PLL is protected by panel, make sure we can write it */
6a9e7363 1555 if (IS_MOBILE(dev_priv->dev))
426115cf 1556 assert_panel_unlocked(dev_priv, crtc->pipe);
87442f73 1557
426115cf
DV
1558 I915_WRITE(reg, dpll);
1559 POSTING_READ(reg);
1560 udelay(150);
1561
1562 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1563 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1564
d288f65f 1565 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
426115cf 1566 POSTING_READ(DPLL_MD(crtc->pipe));
87442f73
DV
1567
1568 /* We do this three times for luck */
426115cf 1569 I915_WRITE(reg, dpll);
87442f73
DV
1570 POSTING_READ(reg);
1571 udelay(150); /* wait for warmup */
426115cf 1572 I915_WRITE(reg, dpll);
87442f73
DV
1573 POSTING_READ(reg);
1574 udelay(150); /* wait for warmup */
426115cf 1575 I915_WRITE(reg, dpll);
87442f73
DV
1576 POSTING_READ(reg);
1577 udelay(150); /* wait for warmup */
1578}
1579
d288f65f 1580static void chv_enable_pll(struct intel_crtc *crtc,
5cec258b 1581 const struct intel_crtc_state *pipe_config)
9d556c99
CML
1582{
1583 struct drm_device *dev = crtc->base.dev;
1584 struct drm_i915_private *dev_priv = dev->dev_private;
1585 int pipe = crtc->pipe;
1586 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9d556c99
CML
1587 u32 tmp;
1588
1589 assert_pipe_disabled(dev_priv, crtc->pipe);
1590
a580516d 1591 mutex_lock(&dev_priv->sb_lock);
9d556c99
CML
1592
1593 /* Enable back the 10bit clock to display controller */
1594 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1595 tmp |= DPIO_DCLKP_EN;
1596 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1597
54433e91
VS
1598 mutex_unlock(&dev_priv->sb_lock);
1599
9d556c99
CML
1600 /*
1601 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1602 */
1603 udelay(1);
1604
1605 /* Enable PLL */
d288f65f 1606 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
9d556c99
CML
1607
1608 /* Check PLL is locked */
a11b0703 1609 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
9d556c99
CML
1610 DRM_ERROR("PLL %d failed to lock\n", pipe);
1611
a11b0703 1612 /* not sure when this should be written */
d288f65f 1613 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
a11b0703 1614 POSTING_READ(DPLL_MD(pipe));
9d556c99
CML
1615}
1616
1c4e0274
VS
1617static int intel_num_dvo_pipes(struct drm_device *dev)
1618{
1619 struct intel_crtc *crtc;
1620 int count = 0;
1621
1622 for_each_intel_crtc(dev, crtc)
3538b9df 1623 count += crtc->base.state->active &&
409ee761 1624 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
1c4e0274
VS
1625
1626 return count;
1627}
1628
66e3d5c0 1629static void i9xx_enable_pll(struct intel_crtc *crtc)
63d7bbe9 1630{
66e3d5c0
DV
1631 struct drm_device *dev = crtc->base.dev;
1632 struct drm_i915_private *dev_priv = dev->dev_private;
f0f59a00 1633 i915_reg_t reg = DPLL(crtc->pipe);
6e3c9717 1634 u32 dpll = crtc->config->dpll_hw_state.dpll;
63d7bbe9 1635
66e3d5c0 1636 assert_pipe_disabled(dev_priv, crtc->pipe);
58c6eaa2 1637
63d7bbe9 1638 /* No really, not for ILK+ */
3d13ef2e 1639 BUG_ON(INTEL_INFO(dev)->gen >= 5);
63d7bbe9
JB
1640
1641 /* PLL is protected by panel, make sure we can write it */
66e3d5c0
DV
1642 if (IS_MOBILE(dev) && !IS_I830(dev))
1643 assert_panel_unlocked(dev_priv, crtc->pipe);
63d7bbe9 1644
1c4e0274
VS
1645 /* Enable DVO 2x clock on both PLLs if necessary */
1646 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1647 /*
1648 * It appears to be important that we don't enable this
1649 * for the current pipe before otherwise configuring the
1650 * PLL. No idea how this should be handled if multiple
1651 * DVO outputs are enabled simultaneosly.
1652 */
1653 dpll |= DPLL_DVO_2X_MODE;
1654 I915_WRITE(DPLL(!crtc->pipe),
1655 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1656 }
66e3d5c0 1657
c2b63374
VS
1658 /*
1659 * Apparently we need to have VGA mode enabled prior to changing
1660 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1661 * dividers, even though the register value does change.
1662 */
1663 I915_WRITE(reg, 0);
1664
8e7a65aa
VS
1665 I915_WRITE(reg, dpll);
1666
66e3d5c0
DV
1667 /* Wait for the clocks to stabilize. */
1668 POSTING_READ(reg);
1669 udelay(150);
1670
1671 if (INTEL_INFO(dev)->gen >= 4) {
1672 I915_WRITE(DPLL_MD(crtc->pipe),
6e3c9717 1673 crtc->config->dpll_hw_state.dpll_md);
66e3d5c0
DV
1674 } else {
1675 /* The pixel multiplier can only be updated once the
1676 * DPLL is enabled and the clocks are stable.
1677 *
1678 * So write it again.
1679 */
1680 I915_WRITE(reg, dpll);
1681 }
63d7bbe9
JB
1682
1683 /* We do this three times for luck */
66e3d5c0 1684 I915_WRITE(reg, dpll);
63d7bbe9
JB
1685 POSTING_READ(reg);
1686 udelay(150); /* wait for warmup */
66e3d5c0 1687 I915_WRITE(reg, dpll);
63d7bbe9
JB
1688 POSTING_READ(reg);
1689 udelay(150); /* wait for warmup */
66e3d5c0 1690 I915_WRITE(reg, dpll);
63d7bbe9
JB
1691 POSTING_READ(reg);
1692 udelay(150); /* wait for warmup */
1693}
1694
1695/**
50b44a44 1696 * i9xx_disable_pll - disable a PLL
63d7bbe9
JB
1697 * @dev_priv: i915 private structure
1698 * @pipe: pipe PLL to disable
1699 *
1700 * Disable the PLL for @pipe, making sure the pipe is off first.
1701 *
1702 * Note! This is for pre-ILK only.
1703 */
1c4e0274 1704static void i9xx_disable_pll(struct intel_crtc *crtc)
63d7bbe9 1705{
1c4e0274
VS
1706 struct drm_device *dev = crtc->base.dev;
1707 struct drm_i915_private *dev_priv = dev->dev_private;
1708 enum pipe pipe = crtc->pipe;
1709
1710 /* Disable DVO 2x clock on both PLLs if necessary */
1711 if (IS_I830(dev) &&
409ee761 1712 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
3538b9df 1713 !intel_num_dvo_pipes(dev)) {
1c4e0274
VS
1714 I915_WRITE(DPLL(PIPE_B),
1715 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1716 I915_WRITE(DPLL(PIPE_A),
1717 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1718 }
1719
b6b5d049
VS
1720 /* Don't disable pipe or pipe PLLs if needed */
1721 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1722 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
63d7bbe9
JB
1723 return;
1724
1725 /* Make sure the pipe isn't still relying on us */
1726 assert_pipe_disabled(dev_priv, pipe);
1727
b8afb911 1728 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
50b44a44 1729 POSTING_READ(DPLL(pipe));
63d7bbe9
JB
1730}
1731
f6071166
JB
1732static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1733{
b8afb911 1734 u32 val;
f6071166
JB
1735
1736 /* Make sure the pipe isn't still relying on us */
1737 assert_pipe_disabled(dev_priv, pipe);
1738
e5cbfbfb
ID
1739 /*
1740 * Leave integrated clock source and reference clock enabled for pipe B.
1741 * The latter is needed for VGA hotplug / manual detection.
1742 */
b8afb911 1743 val = DPLL_VGA_MODE_DIS;
f6071166 1744 if (pipe == PIPE_B)
60bfe44f 1745 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REF_CLK_ENABLE_VLV;
f6071166
JB
1746 I915_WRITE(DPLL(pipe), val);
1747 POSTING_READ(DPLL(pipe));
076ed3b2
CML
1748
1749}
1750
1751static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1752{
d752048d 1753 enum dpio_channel port = vlv_pipe_to_channel(pipe);
076ed3b2
CML
1754 u32 val;
1755
a11b0703
VS
1756 /* Make sure the pipe isn't still relying on us */
1757 assert_pipe_disabled(dev_priv, pipe);
076ed3b2 1758
a11b0703 1759 /* Set PLL en = 0 */
60bfe44f
VS
1760 val = DPLL_SSC_REF_CLK_CHV |
1761 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
a11b0703
VS
1762 if (pipe != PIPE_A)
1763 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1764 I915_WRITE(DPLL(pipe), val);
1765 POSTING_READ(DPLL(pipe));
d752048d 1766
a580516d 1767 mutex_lock(&dev_priv->sb_lock);
d752048d
VS
1768
1769 /* Disable 10bit clock to display controller */
1770 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1771 val &= ~DPIO_DCLKP_EN;
1772 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1773
a580516d 1774 mutex_unlock(&dev_priv->sb_lock);
f6071166
JB
1775}
1776
e4607fcf 1777void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
9b6de0a1
VS
1778 struct intel_digital_port *dport,
1779 unsigned int expected_mask)
89b667f8
JB
1780{
1781 u32 port_mask;
f0f59a00 1782 i915_reg_t dpll_reg;
89b667f8 1783
e4607fcf
CML
1784 switch (dport->port) {
1785 case PORT_B:
89b667f8 1786 port_mask = DPLL_PORTB_READY_MASK;
00fc31b7 1787 dpll_reg = DPLL(0);
e4607fcf
CML
1788 break;
1789 case PORT_C:
89b667f8 1790 port_mask = DPLL_PORTC_READY_MASK;
00fc31b7 1791 dpll_reg = DPLL(0);
9b6de0a1 1792 expected_mask <<= 4;
00fc31b7
CML
1793 break;
1794 case PORT_D:
1795 port_mask = DPLL_PORTD_READY_MASK;
1796 dpll_reg = DPIO_PHY_STATUS;
e4607fcf
CML
1797 break;
1798 default:
1799 BUG();
1800 }
89b667f8 1801
9b6de0a1
VS
1802 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1803 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1804 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
89b667f8
JB
1805}
1806
b8a4f404
PZ
1807static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1808 enum pipe pipe)
040484af 1809{
23670b32 1810 struct drm_device *dev = dev_priv->dev;
7c26e5c6 1811 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
e2b78267 1812 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
f0f59a00
VS
1813 i915_reg_t reg;
1814 uint32_t val, pipeconf_val;
040484af
JB
1815
1816 /* PCH only available on ILK+ */
55522f37 1817 BUG_ON(!HAS_PCH_SPLIT(dev));
040484af
JB
1818
1819 /* Make sure PCH DPLL is enabled */
8106ddbd 1820 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
040484af
JB
1821
1822 /* FDI must be feeding us bits for PCH ports */
1823 assert_fdi_tx_enabled(dev_priv, pipe);
1824 assert_fdi_rx_enabled(dev_priv, pipe);
1825
23670b32
DV
1826 if (HAS_PCH_CPT(dev)) {
1827 /* Workaround: Set the timing override bit before enabling the
1828 * pch transcoder. */
1829 reg = TRANS_CHICKEN2(pipe);
1830 val = I915_READ(reg);
1831 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1832 I915_WRITE(reg, val);
59c859d6 1833 }
23670b32 1834
ab9412ba 1835 reg = PCH_TRANSCONF(pipe);
040484af 1836 val = I915_READ(reg);
5f7f726d 1837 pipeconf_val = I915_READ(PIPECONF(pipe));
e9bcff5c
JB
1838
1839 if (HAS_PCH_IBX(dev_priv->dev)) {
1840 /*
c5de7c6f
VS
1841 * Make the BPC in transcoder be consistent with
1842 * that in pipeconf reg. For HDMI we must use 8bpc
1843 * here for both 8bpc and 12bpc.
e9bcff5c 1844 */
dfd07d72 1845 val &= ~PIPECONF_BPC_MASK;
c5de7c6f
VS
1846 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
1847 val |= PIPECONF_8BPC;
1848 else
1849 val |= pipeconf_val & PIPECONF_BPC_MASK;
e9bcff5c 1850 }
5f7f726d
PZ
1851
1852 val &= ~TRANS_INTERLACE_MASK;
1853 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
7c26e5c6 1854 if (HAS_PCH_IBX(dev_priv->dev) &&
409ee761 1855 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
7c26e5c6
PZ
1856 val |= TRANS_LEGACY_INTERLACED_ILK;
1857 else
1858 val |= TRANS_INTERLACED;
5f7f726d
PZ
1859 else
1860 val |= TRANS_PROGRESSIVE;
1861
040484af
JB
1862 I915_WRITE(reg, val | TRANS_ENABLE);
1863 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
4bb6f1f3 1864 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
040484af
JB
1865}
1866
8fb033d7 1867static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
937bb610 1868 enum transcoder cpu_transcoder)
040484af 1869{
8fb033d7 1870 u32 val, pipeconf_val;
8fb033d7
PZ
1871
1872 /* PCH only available on ILK+ */
55522f37 1873 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
8fb033d7 1874
8fb033d7 1875 /* FDI must be feeding us bits for PCH ports */
1a240d4d 1876 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
937bb610 1877 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
8fb033d7 1878
223a6fdf 1879 /* Workaround: set timing override bit. */
36c0d0cf 1880 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
23670b32 1881 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
36c0d0cf 1882 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
223a6fdf 1883
25f3ef11 1884 val = TRANS_ENABLE;
937bb610 1885 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
8fb033d7 1886
9a76b1c6
PZ
1887 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1888 PIPECONF_INTERLACED_ILK)
a35f2679 1889 val |= TRANS_INTERLACED;
8fb033d7
PZ
1890 else
1891 val |= TRANS_PROGRESSIVE;
1892
ab9412ba
DV
1893 I915_WRITE(LPT_TRANSCONF, val);
1894 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
937bb610 1895 DRM_ERROR("Failed to enable PCH transcoder\n");
8fb033d7
PZ
1896}
1897
b8a4f404
PZ
1898static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1899 enum pipe pipe)
040484af 1900{
23670b32 1901 struct drm_device *dev = dev_priv->dev;
f0f59a00
VS
1902 i915_reg_t reg;
1903 uint32_t val;
040484af
JB
1904
1905 /* FDI relies on the transcoder */
1906 assert_fdi_tx_disabled(dev_priv, pipe);
1907 assert_fdi_rx_disabled(dev_priv, pipe);
1908
291906f1
JB
1909 /* Ports must be off as well */
1910 assert_pch_ports_disabled(dev_priv, pipe);
1911
ab9412ba 1912 reg = PCH_TRANSCONF(pipe);
040484af
JB
1913 val = I915_READ(reg);
1914 val &= ~TRANS_ENABLE;
1915 I915_WRITE(reg, val);
1916 /* wait for PCH transcoder off, transcoder state */
1917 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
4bb6f1f3 1918 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
23670b32 1919
c465613b 1920 if (HAS_PCH_CPT(dev)) {
23670b32
DV
1921 /* Workaround: Clear the timing override chicken bit again. */
1922 reg = TRANS_CHICKEN2(pipe);
1923 val = I915_READ(reg);
1924 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1925 I915_WRITE(reg, val);
1926 }
040484af
JB
1927}
1928
ab4d966c 1929static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
8fb033d7 1930{
8fb033d7
PZ
1931 u32 val;
1932
ab9412ba 1933 val = I915_READ(LPT_TRANSCONF);
8fb033d7 1934 val &= ~TRANS_ENABLE;
ab9412ba 1935 I915_WRITE(LPT_TRANSCONF, val);
8fb033d7 1936 /* wait for PCH transcoder off, transcoder state */
ab9412ba 1937 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
8a52fd9f 1938 DRM_ERROR("Failed to disable PCH transcoder\n");
223a6fdf
PZ
1939
1940 /* Workaround: clear timing override bit. */
36c0d0cf 1941 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
23670b32 1942 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
36c0d0cf 1943 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
040484af
JB
1944}
1945
b24e7179 1946/**
309cfea8 1947 * intel_enable_pipe - enable a pipe, asserting requirements
0372264a 1948 * @crtc: crtc responsible for the pipe
b24e7179 1949 *
0372264a 1950 * Enable @crtc's pipe, making sure that various hardware specific requirements
b24e7179 1951 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
b24e7179 1952 */
e1fdc473 1953static void intel_enable_pipe(struct intel_crtc *crtc)
b24e7179 1954{
0372264a
PZ
1955 struct drm_device *dev = crtc->base.dev;
1956 struct drm_i915_private *dev_priv = dev->dev_private;
1957 enum pipe pipe = crtc->pipe;
1a70a728 1958 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
1a240d4d 1959 enum pipe pch_transcoder;
f0f59a00 1960 i915_reg_t reg;
b24e7179
JB
1961 u32 val;
1962
9e2ee2dd
VS
1963 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1964
58c6eaa2 1965 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 1966 assert_cursor_disabled(dev_priv, pipe);
58c6eaa2
DV
1967 assert_sprites_disabled(dev_priv, pipe);
1968
681e5811 1969 if (HAS_PCH_LPT(dev_priv->dev))
cc391bbb
PZ
1970 pch_transcoder = TRANSCODER_A;
1971 else
1972 pch_transcoder = pipe;
1973
b24e7179
JB
1974 /*
1975 * A pipe without a PLL won't actually be able to drive bits from
1976 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1977 * need the check.
1978 */
50360403 1979 if (HAS_GMCH_DISPLAY(dev_priv->dev))
a65347ba 1980 if (crtc->config->has_dsi_encoder)
23538ef1
JN
1981 assert_dsi_pll_enabled(dev_priv);
1982 else
1983 assert_pll_enabled(dev_priv, pipe);
040484af 1984 else {
6e3c9717 1985 if (crtc->config->has_pch_encoder) {
040484af 1986 /* if driving the PCH, we need FDI enabled */
cc391bbb 1987 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
1a240d4d
DV
1988 assert_fdi_tx_pll_enabled(dev_priv,
1989 (enum pipe) cpu_transcoder);
040484af
JB
1990 }
1991 /* FIXME: assert CPU port conditions for SNB+ */
1992 }
b24e7179 1993
702e7a56 1994 reg = PIPECONF(cpu_transcoder);
b24e7179 1995 val = I915_READ(reg);
7ad25d48 1996 if (val & PIPECONF_ENABLE) {
b6b5d049
VS
1997 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1998 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
00d70b15 1999 return;
7ad25d48 2000 }
00d70b15
CW
2001
2002 I915_WRITE(reg, val | PIPECONF_ENABLE);
851855d8 2003 POSTING_READ(reg);
b7792d8b
VS
2004
2005 /*
2006 * Until the pipe starts DSL will read as 0, which would cause
2007 * an apparent vblank timestamp jump, which messes up also the
2008 * frame count when it's derived from the timestamps. So let's
2009 * wait for the pipe to start properly before we call
2010 * drm_crtc_vblank_on()
2011 */
2012 if (dev->max_vblank_count == 0 &&
2013 wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
2014 DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
b24e7179
JB
2015}
2016
2017/**
309cfea8 2018 * intel_disable_pipe - disable a pipe, asserting requirements
575f7ab7 2019 * @crtc: crtc whose pipes is to be disabled
b24e7179 2020 *
575f7ab7
VS
2021 * Disable the pipe of @crtc, making sure that various hardware
2022 * specific requirements are met, if applicable, e.g. plane
2023 * disabled, panel fitter off, etc.
b24e7179
JB
2024 *
2025 * Will wait until the pipe has shut down before returning.
2026 */
575f7ab7 2027static void intel_disable_pipe(struct intel_crtc *crtc)
b24e7179 2028{
575f7ab7 2029 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
6e3c9717 2030 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 2031 enum pipe pipe = crtc->pipe;
f0f59a00 2032 i915_reg_t reg;
b24e7179
JB
2033 u32 val;
2034
9e2ee2dd
VS
2035 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2036
b24e7179
JB
2037 /*
2038 * Make sure planes won't keep trying to pump pixels to us,
2039 * or we might hang the display.
2040 */
2041 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2042 assert_cursor_disabled(dev_priv, pipe);
19332d7a 2043 assert_sprites_disabled(dev_priv, pipe);
b24e7179 2044
702e7a56 2045 reg = PIPECONF(cpu_transcoder);
b24e7179 2046 val = I915_READ(reg);
00d70b15
CW
2047 if ((val & PIPECONF_ENABLE) == 0)
2048 return;
2049
67adc644
VS
2050 /*
2051 * Double wide has implications for planes
2052 * so best keep it disabled when not needed.
2053 */
6e3c9717 2054 if (crtc->config->double_wide)
67adc644
VS
2055 val &= ~PIPECONF_DOUBLE_WIDE;
2056
2057 /* Don't disable pipe or pipe PLLs if needed */
b6b5d049
VS
2058 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2059 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
67adc644
VS
2060 val &= ~PIPECONF_ENABLE;
2061
2062 I915_WRITE(reg, val);
2063 if ((val & PIPECONF_ENABLE) == 0)
2064 intel_wait_for_pipe_off(crtc);
b24e7179
JB
2065}
2066
693db184
CW
2067static bool need_vtd_wa(struct drm_device *dev)
2068{
2069#ifdef CONFIG_INTEL_IOMMU
2070 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2071 return true;
2072#endif
2073 return false;
2074}
2075
832be82f
VS
2076static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
2077{
2078 return IS_GEN2(dev_priv) ? 2048 : 4096;
2079}
2080
27ba3910
VS
2081static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
2082 uint64_t fb_modifier, unsigned int cpp)
7b49f948
VS
2083{
2084 switch (fb_modifier) {
2085 case DRM_FORMAT_MOD_NONE:
2086 return cpp;
2087 case I915_FORMAT_MOD_X_TILED:
2088 if (IS_GEN2(dev_priv))
2089 return 128;
2090 else
2091 return 512;
2092 case I915_FORMAT_MOD_Y_TILED:
2093 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2094 return 128;
2095 else
2096 return 512;
2097 case I915_FORMAT_MOD_Yf_TILED:
2098 switch (cpp) {
2099 case 1:
2100 return 64;
2101 case 2:
2102 case 4:
2103 return 128;
2104 case 8:
2105 case 16:
2106 return 256;
2107 default:
2108 MISSING_CASE(cpp);
2109 return cpp;
2110 }
2111 break;
2112 default:
2113 MISSING_CASE(fb_modifier);
2114 return cpp;
2115 }
2116}
2117
832be82f
VS
2118unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
2119 uint64_t fb_modifier, unsigned int cpp)
a57ce0b2 2120{
832be82f
VS
2121 if (fb_modifier == DRM_FORMAT_MOD_NONE)
2122 return 1;
2123 else
2124 return intel_tile_size(dev_priv) /
27ba3910 2125 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
6761dd31
TU
2126}
2127
8d0deca8
VS
2128/* Return the tile dimensions in pixel units */
2129static void intel_tile_dims(const struct drm_i915_private *dev_priv,
2130 unsigned int *tile_width,
2131 unsigned int *tile_height,
2132 uint64_t fb_modifier,
2133 unsigned int cpp)
2134{
2135 unsigned int tile_width_bytes =
2136 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
2137
2138 *tile_width = tile_width_bytes / cpp;
2139 *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
2140}
2141
6761dd31
TU
2142unsigned int
2143intel_fb_align_height(struct drm_device *dev, unsigned int height,
832be82f 2144 uint32_t pixel_format, uint64_t fb_modifier)
6761dd31 2145{
832be82f
VS
2146 unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
2147 unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
2148
2149 return ALIGN(height, tile_height);
a57ce0b2
JB
2150}
2151
1663b9d6
VS
2152unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2153{
2154 unsigned int size = 0;
2155 int i;
2156
2157 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2158 size += rot_info->plane[i].width * rot_info->plane[i].height;
2159
2160 return size;
2161}
2162
75c82a53 2163static void
3465c580
VS
2164intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2165 const struct drm_framebuffer *fb,
2166 unsigned int rotation)
f64b98cd 2167{
2d7a215f
VS
2168 if (intel_rotation_90_or_270(rotation)) {
2169 *view = i915_ggtt_view_rotated;
2170 view->params.rotated = to_intel_framebuffer(fb)->rot_info;
2171 } else {
2172 *view = i915_ggtt_view_normal;
2173 }
2174}
50470bb0 2175
2d7a215f
VS
2176static void
2177intel_fill_fb_info(struct drm_i915_private *dev_priv,
2178 struct drm_framebuffer *fb)
2179{
2180 struct intel_rotation_info *info = &to_intel_framebuffer(fb)->rot_info;
2181 unsigned int tile_size, tile_width, tile_height, cpp;
50470bb0 2182
d9b3288e
VS
2183 tile_size = intel_tile_size(dev_priv);
2184
2185 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
8d0deca8
VS
2186 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2187 fb->modifier[0], cpp);
d9b3288e 2188
1663b9d6
VS
2189 info->plane[0].width = DIV_ROUND_UP(fb->pitches[0], tile_width * cpp);
2190 info->plane[0].height = DIV_ROUND_UP(fb->height, tile_height);
84fe03f7 2191
89e3e142 2192 if (info->pixel_format == DRM_FORMAT_NV12) {
832be82f 2193 cpp = drm_format_plane_cpp(fb->pixel_format, 1);
8d0deca8
VS
2194 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2195 fb->modifier[1], cpp);
d9b3288e 2196
2d7a215f 2197 info->uv_offset = fb->offsets[1];
1663b9d6
VS
2198 info->plane[1].width = DIV_ROUND_UP(fb->pitches[1], tile_width * cpp);
2199 info->plane[1].height = DIV_ROUND_UP(fb->height / 2, tile_height);
89e3e142 2200 }
f64b98cd
TU
2201}
2202
603525d7 2203static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
4e9a86b6
VS
2204{
2205 if (INTEL_INFO(dev_priv)->gen >= 9)
2206 return 256 * 1024;
985b8bb4 2207 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
666a4537 2208 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
4e9a86b6
VS
2209 return 128 * 1024;
2210 else if (INTEL_INFO(dev_priv)->gen >= 4)
2211 return 4 * 1024;
2212 else
44c5905e 2213 return 0;
4e9a86b6
VS
2214}
2215
603525d7
VS
2216static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
2217 uint64_t fb_modifier)
2218{
2219 switch (fb_modifier) {
2220 case DRM_FORMAT_MOD_NONE:
2221 return intel_linear_alignment(dev_priv);
2222 case I915_FORMAT_MOD_X_TILED:
2223 if (INTEL_INFO(dev_priv)->gen >= 9)
2224 return 256 * 1024;
2225 return 0;
2226 case I915_FORMAT_MOD_Y_TILED:
2227 case I915_FORMAT_MOD_Yf_TILED:
2228 return 1 * 1024 * 1024;
2229 default:
2230 MISSING_CASE(fb_modifier);
2231 return 0;
2232 }
2233}
2234
127bd2ac 2235int
3465c580
VS
2236intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
2237 unsigned int rotation)
6b95a207 2238{
850c4cdc 2239 struct drm_device *dev = fb->dev;
ce453d81 2240 struct drm_i915_private *dev_priv = dev->dev_private;
850c4cdc 2241 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2242 struct i915_ggtt_view view;
6b95a207
KH
2243 u32 alignment;
2244 int ret;
2245
ebcdd39e
MR
2246 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2247
603525d7 2248 alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
6b95a207 2249
3465c580 2250 intel_fill_fb_ggtt_view(&view, fb, rotation);
f64b98cd 2251
693db184
CW
2252 /* Note that the w/a also requires 64 PTE of padding following the
2253 * bo. We currently fill all unused PTE with the shadow page and so
2254 * we should always have valid PTE following the scanout preventing
2255 * the VT-d warning.
2256 */
2257 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2258 alignment = 256 * 1024;
2259
d6dd6843
PZ
2260 /*
2261 * Global gtt pte registers are special registers which actually forward
2262 * writes to a chunk of system memory. Which means that there is no risk
2263 * that the register values disappear as soon as we call
2264 * intel_runtime_pm_put(), so it is correct to wrap only the
2265 * pin/unpin/fence and not more.
2266 */
2267 intel_runtime_pm_get(dev_priv);
2268
7580d774
ML
2269 ret = i915_gem_object_pin_to_display_plane(obj, alignment,
2270 &view);
48b956c5 2271 if (ret)
b26a6b35 2272 goto err_pm;
6b95a207
KH
2273
2274 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2275 * fence, whereas 965+ only requires a fence if using
2276 * framebuffer compression. For simplicity, we always install
2277 * a fence as the cost is not that onerous.
2278 */
9807216f
VK
2279 if (view.type == I915_GGTT_VIEW_NORMAL) {
2280 ret = i915_gem_object_get_fence(obj);
2281 if (ret == -EDEADLK) {
2282 /*
2283 * -EDEADLK means there are no free fences
2284 * no pending flips.
2285 *
2286 * This is propagated to atomic, but it uses
2287 * -EDEADLK to force a locking recovery, so
2288 * change the returned error to -EBUSY.
2289 */
2290 ret = -EBUSY;
2291 goto err_unpin;
2292 } else if (ret)
2293 goto err_unpin;
1690e1eb 2294
9807216f
VK
2295 i915_gem_object_pin_fence(obj);
2296 }
6b95a207 2297
d6dd6843 2298 intel_runtime_pm_put(dev_priv);
6b95a207 2299 return 0;
48b956c5
CW
2300
2301err_unpin:
f64b98cd 2302 i915_gem_object_unpin_from_display_plane(obj, &view);
b26a6b35 2303err_pm:
d6dd6843 2304 intel_runtime_pm_put(dev_priv);
48b956c5 2305 return ret;
6b95a207
KH
2306}
2307
3465c580 2308static void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
1690e1eb 2309{
82bc3b2d 2310 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2311 struct i915_ggtt_view view;
82bc3b2d 2312
ebcdd39e
MR
2313 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2314
3465c580 2315 intel_fill_fb_ggtt_view(&view, fb, rotation);
f64b98cd 2316
9807216f
VK
2317 if (view.type == I915_GGTT_VIEW_NORMAL)
2318 i915_gem_object_unpin_fence(obj);
2319
f64b98cd 2320 i915_gem_object_unpin_from_display_plane(obj, &view);
1690e1eb
CW
2321}
2322
29cf9491
VS
2323/*
2324 * Adjust the tile offset by moving the difference into
2325 * the x/y offsets.
2326 *
2327 * Input tile dimensions and pitch must already be
2328 * rotated to match x and y, and in pixel units.
2329 */
2330static u32 intel_adjust_tile_offset(int *x, int *y,
2331 unsigned int tile_width,
2332 unsigned int tile_height,
2333 unsigned int tile_size,
2334 unsigned int pitch_tiles,
2335 u32 old_offset,
2336 u32 new_offset)
2337{
2338 unsigned int tiles;
2339
2340 WARN_ON(old_offset & (tile_size - 1));
2341 WARN_ON(new_offset & (tile_size - 1));
2342 WARN_ON(new_offset > old_offset);
2343
2344 tiles = (old_offset - new_offset) / tile_size;
2345
2346 *y += tiles / pitch_tiles * tile_height;
2347 *x += tiles % pitch_tiles * tile_width;
2348
2349 return new_offset;
2350}
2351
8d0deca8
VS
2352/*
2353 * Computes the linear offset to the base tile and adjusts
2354 * x, y. bytes per pixel is assumed to be a power-of-two.
2355 *
2356 * In the 90/270 rotated case, x and y are assumed
2357 * to be already rotated to match the rotated GTT view, and
2358 * pitch is the tile_height aligned framebuffer height.
2359 */
4f2d9934
VS
2360u32 intel_compute_tile_offset(int *x, int *y,
2361 const struct drm_framebuffer *fb, int plane,
8d0deca8
VS
2362 unsigned int pitch,
2363 unsigned int rotation)
c2c75131 2364{
4f2d9934
VS
2365 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
2366 uint64_t fb_modifier = fb->modifier[plane];
2367 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
29cf9491
VS
2368 u32 offset, offset_aligned, alignment;
2369
2370 alignment = intel_surf_alignment(dev_priv, fb_modifier);
2371 if (alignment)
2372 alignment--;
2373
b5c65338 2374 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
8d0deca8
VS
2375 unsigned int tile_size, tile_width, tile_height;
2376 unsigned int tile_rows, tiles, pitch_tiles;
c2c75131 2377
d843310d 2378 tile_size = intel_tile_size(dev_priv);
8d0deca8
VS
2379 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2380 fb_modifier, cpp);
2381
2382 if (intel_rotation_90_or_270(rotation)) {
2383 pitch_tiles = pitch / tile_height;
2384 swap(tile_width, tile_height);
2385 } else {
2386 pitch_tiles = pitch / (tile_width * cpp);
2387 }
d843310d
VS
2388
2389 tile_rows = *y / tile_height;
2390 *y %= tile_height;
c2c75131 2391
8d0deca8
VS
2392 tiles = *x / tile_width;
2393 *x %= tile_width;
bc752862 2394
29cf9491
VS
2395 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2396 offset_aligned = offset & ~alignment;
bc752862 2397
29cf9491
VS
2398 intel_adjust_tile_offset(x, y, tile_width, tile_height,
2399 tile_size, pitch_tiles,
2400 offset, offset_aligned);
2401 } else {
bc752862 2402 offset = *y * pitch + *x * cpp;
29cf9491
VS
2403 offset_aligned = offset & ~alignment;
2404
4e9a86b6
VS
2405 *y = (offset & alignment) / pitch;
2406 *x = ((offset & alignment) - *y * pitch) / cpp;
bc752862 2407 }
29cf9491
VS
2408
2409 return offset_aligned;
c2c75131
DV
2410}
2411
b35d63fa 2412static int i9xx_format_to_fourcc(int format)
46f297fb
JB
2413{
2414 switch (format) {
2415 case DISPPLANE_8BPP:
2416 return DRM_FORMAT_C8;
2417 case DISPPLANE_BGRX555:
2418 return DRM_FORMAT_XRGB1555;
2419 case DISPPLANE_BGRX565:
2420 return DRM_FORMAT_RGB565;
2421 default:
2422 case DISPPLANE_BGRX888:
2423 return DRM_FORMAT_XRGB8888;
2424 case DISPPLANE_RGBX888:
2425 return DRM_FORMAT_XBGR8888;
2426 case DISPPLANE_BGRX101010:
2427 return DRM_FORMAT_XRGB2101010;
2428 case DISPPLANE_RGBX101010:
2429 return DRM_FORMAT_XBGR2101010;
2430 }
2431}
2432
bc8d7dff
DL
2433static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2434{
2435 switch (format) {
2436 case PLANE_CTL_FORMAT_RGB_565:
2437 return DRM_FORMAT_RGB565;
2438 default:
2439 case PLANE_CTL_FORMAT_XRGB_8888:
2440 if (rgb_order) {
2441 if (alpha)
2442 return DRM_FORMAT_ABGR8888;
2443 else
2444 return DRM_FORMAT_XBGR8888;
2445 } else {
2446 if (alpha)
2447 return DRM_FORMAT_ARGB8888;
2448 else
2449 return DRM_FORMAT_XRGB8888;
2450 }
2451 case PLANE_CTL_FORMAT_XRGB_2101010:
2452 if (rgb_order)
2453 return DRM_FORMAT_XBGR2101010;
2454 else
2455 return DRM_FORMAT_XRGB2101010;
2456 }
2457}
2458
5724dbd1 2459static bool
f6936e29
DV
2460intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2461 struct intel_initial_plane_config *plane_config)
46f297fb
JB
2462{
2463 struct drm_device *dev = crtc->base.dev;
3badb49f 2464 struct drm_i915_private *dev_priv = to_i915(dev);
46f297fb
JB
2465 struct drm_i915_gem_object *obj = NULL;
2466 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2d14030b 2467 struct drm_framebuffer *fb = &plane_config->fb->base;
f37b5c2b
DV
2468 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2469 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2470 PAGE_SIZE);
2471
2472 size_aligned -= base_aligned;
46f297fb 2473
ff2652ea
CW
2474 if (plane_config->size == 0)
2475 return false;
2476
3badb49f
PZ
2477 /* If the FB is too big, just don't use it since fbdev is not very
2478 * important and we should probably use that space with FBC or other
2479 * features. */
62106b4f 2480 if (size_aligned * 2 > dev_priv->ggtt.stolen_usable_size)
3badb49f
PZ
2481 return false;
2482
12c83d99
TU
2483 mutex_lock(&dev->struct_mutex);
2484
f37b5c2b
DV
2485 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2486 base_aligned,
2487 base_aligned,
2488 size_aligned);
12c83d99
TU
2489 if (!obj) {
2490 mutex_unlock(&dev->struct_mutex);
484b41dd 2491 return false;
12c83d99 2492 }
46f297fb 2493
49af449b
DL
2494 obj->tiling_mode = plane_config->tiling;
2495 if (obj->tiling_mode == I915_TILING_X)
6bf129df 2496 obj->stride = fb->pitches[0];
46f297fb 2497
6bf129df
DL
2498 mode_cmd.pixel_format = fb->pixel_format;
2499 mode_cmd.width = fb->width;
2500 mode_cmd.height = fb->height;
2501 mode_cmd.pitches[0] = fb->pitches[0];
18c5247e
DV
2502 mode_cmd.modifier[0] = fb->modifier[0];
2503 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
46f297fb 2504
6bf129df 2505 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
484b41dd 2506 &mode_cmd, obj)) {
46f297fb
JB
2507 DRM_DEBUG_KMS("intel fb init failed\n");
2508 goto out_unref_obj;
2509 }
12c83d99 2510
46f297fb 2511 mutex_unlock(&dev->struct_mutex);
484b41dd 2512
f6936e29 2513 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
484b41dd 2514 return true;
46f297fb
JB
2515
2516out_unref_obj:
2517 drm_gem_object_unreference(&obj->base);
2518 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2519 return false;
2520}
2521
afd65eb4
MR
2522/* Update plane->state->fb to match plane->fb after driver-internal updates */
2523static void
2524update_state_fb(struct drm_plane *plane)
2525{
2526 if (plane->fb == plane->state->fb)
2527 return;
2528
2529 if (plane->state->fb)
2530 drm_framebuffer_unreference(plane->state->fb);
2531 plane->state->fb = plane->fb;
2532 if (plane->state->fb)
2533 drm_framebuffer_reference(plane->state->fb);
2534}
2535
5724dbd1 2536static void
f6936e29
DV
2537intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2538 struct intel_initial_plane_config *plane_config)
484b41dd
JB
2539{
2540 struct drm_device *dev = intel_crtc->base.dev;
d9ceb816 2541 struct drm_i915_private *dev_priv = dev->dev_private;
484b41dd
JB
2542 struct drm_crtc *c;
2543 struct intel_crtc *i;
2ff8fde1 2544 struct drm_i915_gem_object *obj;
88595ac9 2545 struct drm_plane *primary = intel_crtc->base.primary;
be5651f2 2546 struct drm_plane_state *plane_state = primary->state;
200757f5
MR
2547 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2548 struct intel_plane *intel_plane = to_intel_plane(primary);
0a8d8a86
MR
2549 struct intel_plane_state *intel_state =
2550 to_intel_plane_state(plane_state);
88595ac9 2551 struct drm_framebuffer *fb;
484b41dd 2552
2d14030b 2553 if (!plane_config->fb)
484b41dd
JB
2554 return;
2555
f6936e29 2556 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
88595ac9
DV
2557 fb = &plane_config->fb->base;
2558 goto valid_fb;
f55548b5 2559 }
484b41dd 2560
2d14030b 2561 kfree(plane_config->fb);
484b41dd
JB
2562
2563 /*
2564 * Failed to alloc the obj, check to see if we should share
2565 * an fb with another CRTC instead
2566 */
70e1e0ec 2567 for_each_crtc(dev, c) {
484b41dd
JB
2568 i = to_intel_crtc(c);
2569
2570 if (c == &intel_crtc->base)
2571 continue;
2572
2ff8fde1
MR
2573 if (!i->active)
2574 continue;
2575
88595ac9
DV
2576 fb = c->primary->fb;
2577 if (!fb)
484b41dd
JB
2578 continue;
2579
88595ac9 2580 obj = intel_fb_obj(fb);
2ff8fde1 2581 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
88595ac9
DV
2582 drm_framebuffer_reference(fb);
2583 goto valid_fb;
484b41dd
JB
2584 }
2585 }
88595ac9 2586
200757f5
MR
2587 /*
2588 * We've failed to reconstruct the BIOS FB. Current display state
2589 * indicates that the primary plane is visible, but has a NULL FB,
2590 * which will lead to problems later if we don't fix it up. The
2591 * simplest solution is to just disable the primary plane now and
2592 * pretend the BIOS never had it enabled.
2593 */
2594 to_intel_plane_state(plane_state)->visible = false;
2595 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
2622a081 2596 intel_pre_disable_primary_noatomic(&intel_crtc->base);
200757f5
MR
2597 intel_plane->disable_plane(primary, &intel_crtc->base);
2598
88595ac9
DV
2599 return;
2600
2601valid_fb:
f44e2659
VS
2602 plane_state->src_x = 0;
2603 plane_state->src_y = 0;
be5651f2
ML
2604 plane_state->src_w = fb->width << 16;
2605 plane_state->src_h = fb->height << 16;
2606
f44e2659
VS
2607 plane_state->crtc_x = 0;
2608 plane_state->crtc_y = 0;
be5651f2
ML
2609 plane_state->crtc_w = fb->width;
2610 plane_state->crtc_h = fb->height;
2611
0a8d8a86
MR
2612 intel_state->src.x1 = plane_state->src_x;
2613 intel_state->src.y1 = plane_state->src_y;
2614 intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
2615 intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
2616 intel_state->dst.x1 = plane_state->crtc_x;
2617 intel_state->dst.y1 = plane_state->crtc_y;
2618 intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
2619 intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
2620
88595ac9
DV
2621 obj = intel_fb_obj(fb);
2622 if (obj->tiling_mode != I915_TILING_NONE)
2623 dev_priv->preserve_bios_swizzle = true;
2624
be5651f2
ML
2625 drm_framebuffer_reference(fb);
2626 primary->fb = primary->state->fb = fb;
36750f28 2627 primary->crtc = primary->state->crtc = &intel_crtc->base;
36750f28 2628 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
a9ff8714 2629 obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
46f297fb
JB
2630}
2631
a8d201af
ML
2632static void i9xx_update_primary_plane(struct drm_plane *primary,
2633 const struct intel_crtc_state *crtc_state,
2634 const struct intel_plane_state *plane_state)
81255565 2635{
a8d201af 2636 struct drm_device *dev = primary->dev;
81255565 2637 struct drm_i915_private *dev_priv = dev->dev_private;
a8d201af
ML
2638 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2639 struct drm_framebuffer *fb = plane_state->base.fb;
2640 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
81255565 2641 int plane = intel_crtc->plane;
54ea9da8 2642 u32 linear_offset;
81255565 2643 u32 dspcntr;
f0f59a00 2644 i915_reg_t reg = DSPCNTR(plane);
8d0deca8 2645 unsigned int rotation = plane_state->base.rotation;
ac484963 2646 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
54ea9da8
VS
2647 int x = plane_state->src.x1 >> 16;
2648 int y = plane_state->src.y1 >> 16;
c9ba6fad 2649
f45651ba
VS
2650 dspcntr = DISPPLANE_GAMMA_ENABLE;
2651
fdd508a6 2652 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2653
2654 if (INTEL_INFO(dev)->gen < 4) {
2655 if (intel_crtc->pipe == PIPE_B)
2656 dspcntr |= DISPPLANE_SEL_PIPE_B;
2657
2658 /* pipesrc and dspsize control the size that is scaled from,
2659 * which should always be the user's requested size.
2660 */
2661 I915_WRITE(DSPSIZE(plane),
a8d201af
ML
2662 ((crtc_state->pipe_src_h - 1) << 16) |
2663 (crtc_state->pipe_src_w - 1));
f45651ba 2664 I915_WRITE(DSPPOS(plane), 0);
c14b0485
VS
2665 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2666 I915_WRITE(PRIMSIZE(plane),
a8d201af
ML
2667 ((crtc_state->pipe_src_h - 1) << 16) |
2668 (crtc_state->pipe_src_w - 1));
c14b0485
VS
2669 I915_WRITE(PRIMPOS(plane), 0);
2670 I915_WRITE(PRIMCNSTALPHA(plane), 0);
f45651ba 2671 }
81255565 2672
57779d06
VS
2673 switch (fb->pixel_format) {
2674 case DRM_FORMAT_C8:
81255565
JB
2675 dspcntr |= DISPPLANE_8BPP;
2676 break;
57779d06 2677 case DRM_FORMAT_XRGB1555:
57779d06 2678 dspcntr |= DISPPLANE_BGRX555;
81255565 2679 break;
57779d06
VS
2680 case DRM_FORMAT_RGB565:
2681 dspcntr |= DISPPLANE_BGRX565;
2682 break;
2683 case DRM_FORMAT_XRGB8888:
57779d06
VS
2684 dspcntr |= DISPPLANE_BGRX888;
2685 break;
2686 case DRM_FORMAT_XBGR8888:
57779d06
VS
2687 dspcntr |= DISPPLANE_RGBX888;
2688 break;
2689 case DRM_FORMAT_XRGB2101010:
57779d06
VS
2690 dspcntr |= DISPPLANE_BGRX101010;
2691 break;
2692 case DRM_FORMAT_XBGR2101010:
57779d06 2693 dspcntr |= DISPPLANE_RGBX101010;
81255565
JB
2694 break;
2695 default:
baba133a 2696 BUG();
81255565 2697 }
57779d06 2698
f45651ba
VS
2699 if (INTEL_INFO(dev)->gen >= 4 &&
2700 obj->tiling_mode != I915_TILING_NONE)
2701 dspcntr |= DISPPLANE_TILED;
81255565 2702
de1aa629
VS
2703 if (IS_G4X(dev))
2704 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2705
ac484963 2706 linear_offset = y * fb->pitches[0] + x * cpp;
81255565 2707
c2c75131
DV
2708 if (INTEL_INFO(dev)->gen >= 4) {
2709 intel_crtc->dspaddr_offset =
4f2d9934 2710 intel_compute_tile_offset(&x, &y, fb, 0,
8d0deca8 2711 fb->pitches[0], rotation);
c2c75131
DV
2712 linear_offset -= intel_crtc->dspaddr_offset;
2713 } else {
e506a0c6 2714 intel_crtc->dspaddr_offset = linear_offset;
c2c75131 2715 }
e506a0c6 2716
8d0deca8 2717 if (rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2718 dspcntr |= DISPPLANE_ROTATE_180;
2719
a8d201af
ML
2720 x += (crtc_state->pipe_src_w - 1);
2721 y += (crtc_state->pipe_src_h - 1);
48404c1e
SJ
2722
2723 /* Finding the last pixel of the last line of the display
2724 data and adding to linear_offset*/
2725 linear_offset +=
a8d201af 2726 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
ac484963 2727 (crtc_state->pipe_src_w - 1) * cpp;
48404c1e
SJ
2728 }
2729
2db3366b
PZ
2730 intel_crtc->adjusted_x = x;
2731 intel_crtc->adjusted_y = y;
2732
48404c1e
SJ
2733 I915_WRITE(reg, dspcntr);
2734
01f2c773 2735 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
a6c45cf0 2736 if (INTEL_INFO(dev)->gen >= 4) {
85ba7b7d
DV
2737 I915_WRITE(DSPSURF(plane),
2738 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
5eddb70b 2739 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
e506a0c6 2740 I915_WRITE(DSPLINOFF(plane), linear_offset);
5eddb70b 2741 } else
f343c5f6 2742 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
5eddb70b 2743 POSTING_READ(reg);
17638cd6
JB
2744}
2745
a8d201af
ML
2746static void i9xx_disable_primary_plane(struct drm_plane *primary,
2747 struct drm_crtc *crtc)
17638cd6
JB
2748{
2749 struct drm_device *dev = crtc->dev;
2750 struct drm_i915_private *dev_priv = dev->dev_private;
2751 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
17638cd6 2752 int plane = intel_crtc->plane;
f45651ba 2753
a8d201af
ML
2754 I915_WRITE(DSPCNTR(plane), 0);
2755 if (INTEL_INFO(dev_priv)->gen >= 4)
fdd508a6 2756 I915_WRITE(DSPSURF(plane), 0);
a8d201af
ML
2757 else
2758 I915_WRITE(DSPADDR(plane), 0);
2759 POSTING_READ(DSPCNTR(plane));
2760}
c9ba6fad 2761
a8d201af
ML
2762static void ironlake_update_primary_plane(struct drm_plane *primary,
2763 const struct intel_crtc_state *crtc_state,
2764 const struct intel_plane_state *plane_state)
2765{
2766 struct drm_device *dev = primary->dev;
2767 struct drm_i915_private *dev_priv = dev->dev_private;
2768 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2769 struct drm_framebuffer *fb = plane_state->base.fb;
2770 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
2771 int plane = intel_crtc->plane;
54ea9da8 2772 u32 linear_offset;
a8d201af
ML
2773 u32 dspcntr;
2774 i915_reg_t reg = DSPCNTR(plane);
8d0deca8 2775 unsigned int rotation = plane_state->base.rotation;
ac484963 2776 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
a8d201af
ML
2777 int x = plane_state->src.x1 >> 16;
2778 int y = plane_state->src.y1 >> 16;
c9ba6fad 2779
f45651ba 2780 dspcntr = DISPPLANE_GAMMA_ENABLE;
fdd508a6 2781 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2782
2783 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2784 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
17638cd6 2785
57779d06
VS
2786 switch (fb->pixel_format) {
2787 case DRM_FORMAT_C8:
17638cd6
JB
2788 dspcntr |= DISPPLANE_8BPP;
2789 break;
57779d06
VS
2790 case DRM_FORMAT_RGB565:
2791 dspcntr |= DISPPLANE_BGRX565;
17638cd6 2792 break;
57779d06 2793 case DRM_FORMAT_XRGB8888:
57779d06
VS
2794 dspcntr |= DISPPLANE_BGRX888;
2795 break;
2796 case DRM_FORMAT_XBGR8888:
57779d06
VS
2797 dspcntr |= DISPPLANE_RGBX888;
2798 break;
2799 case DRM_FORMAT_XRGB2101010:
57779d06
VS
2800 dspcntr |= DISPPLANE_BGRX101010;
2801 break;
2802 case DRM_FORMAT_XBGR2101010:
57779d06 2803 dspcntr |= DISPPLANE_RGBX101010;
17638cd6
JB
2804 break;
2805 default:
baba133a 2806 BUG();
17638cd6
JB
2807 }
2808
2809 if (obj->tiling_mode != I915_TILING_NONE)
2810 dspcntr |= DISPPLANE_TILED;
17638cd6 2811
f45651ba 2812 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
1f5d76db 2813 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
17638cd6 2814
ac484963 2815 linear_offset = y * fb->pitches[0] + x * cpp;
c2c75131 2816 intel_crtc->dspaddr_offset =
4f2d9934 2817 intel_compute_tile_offset(&x, &y, fb, 0,
8d0deca8 2818 fb->pitches[0], rotation);
c2c75131 2819 linear_offset -= intel_crtc->dspaddr_offset;
8d0deca8 2820 if (rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2821 dspcntr |= DISPPLANE_ROTATE_180;
2822
2823 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
a8d201af
ML
2824 x += (crtc_state->pipe_src_w - 1);
2825 y += (crtc_state->pipe_src_h - 1);
48404c1e
SJ
2826
2827 /* Finding the last pixel of the last line of the display
2828 data and adding to linear_offset*/
2829 linear_offset +=
a8d201af 2830 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
ac484963 2831 (crtc_state->pipe_src_w - 1) * cpp;
48404c1e
SJ
2832 }
2833 }
2834
2db3366b
PZ
2835 intel_crtc->adjusted_x = x;
2836 intel_crtc->adjusted_y = y;
2837
48404c1e 2838 I915_WRITE(reg, dspcntr);
17638cd6 2839
01f2c773 2840 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
85ba7b7d
DV
2841 I915_WRITE(DSPSURF(plane),
2842 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
b3dc685e 2843 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
bc1c91eb
DL
2844 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2845 } else {
2846 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2847 I915_WRITE(DSPLINOFF(plane), linear_offset);
2848 }
17638cd6 2849 POSTING_READ(reg);
17638cd6
JB
2850}
2851
7b49f948
VS
2852u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
2853 uint64_t fb_modifier, uint32_t pixel_format)
b321803d 2854{
7b49f948 2855 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
b321803d 2856 return 64;
7b49f948
VS
2857 } else {
2858 int cpp = drm_format_plane_cpp(pixel_format, 0);
2859
27ba3910 2860 return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
b321803d
DL
2861 }
2862}
2863
44eb0cb9
MK
2864u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
2865 struct drm_i915_gem_object *obj,
2866 unsigned int plane)
121920fa 2867{
ce7f1728 2868 struct i915_ggtt_view view;
dedf278c 2869 struct i915_vma *vma;
44eb0cb9 2870 u64 offset;
121920fa 2871
e7941294 2872 intel_fill_fb_ggtt_view(&view, intel_plane->base.state->fb,
3465c580 2873 intel_plane->base.state->rotation);
121920fa 2874
ce7f1728 2875 vma = i915_gem_obj_to_ggtt_view(obj, &view);
dedf278c 2876 if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
ce7f1728 2877 view.type))
dedf278c
TU
2878 return -1;
2879
44eb0cb9 2880 offset = vma->node.start;
dedf278c
TU
2881
2882 if (plane == 1) {
7723f47d 2883 offset += vma->ggtt_view.params.rotated.uv_start_page *
dedf278c
TU
2884 PAGE_SIZE;
2885 }
2886
44eb0cb9
MK
2887 WARN_ON(upper_32_bits(offset));
2888
2889 return lower_32_bits(offset);
121920fa
TU
2890}
2891
e435d6e5
ML
2892static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
2893{
2894 struct drm_device *dev = intel_crtc->base.dev;
2895 struct drm_i915_private *dev_priv = dev->dev_private;
2896
2897 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
2898 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
2899 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
e435d6e5
ML
2900}
2901
a1b2278e
CK
2902/*
2903 * This function detaches (aka. unbinds) unused scalers in hardware
2904 */
0583236e 2905static void skl_detach_scalers(struct intel_crtc *intel_crtc)
a1b2278e 2906{
a1b2278e
CK
2907 struct intel_crtc_scaler_state *scaler_state;
2908 int i;
2909
a1b2278e
CK
2910 scaler_state = &intel_crtc->config->scaler_state;
2911
2912 /* loop through and disable scalers that aren't in use */
2913 for (i = 0; i < intel_crtc->num_scalers; i++) {
e435d6e5
ML
2914 if (!scaler_state->scalers[i].in_use)
2915 skl_detach_scaler(intel_crtc, i);
a1b2278e
CK
2916 }
2917}
2918
6156a456 2919u32 skl_plane_ctl_format(uint32_t pixel_format)
70d21f0e 2920{
6156a456 2921 switch (pixel_format) {
d161cf7a 2922 case DRM_FORMAT_C8:
c34ce3d1 2923 return PLANE_CTL_FORMAT_INDEXED;
70d21f0e 2924 case DRM_FORMAT_RGB565:
c34ce3d1 2925 return PLANE_CTL_FORMAT_RGB_565;
70d21f0e 2926 case DRM_FORMAT_XBGR8888:
c34ce3d1 2927 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
6156a456 2928 case DRM_FORMAT_XRGB8888:
c34ce3d1 2929 return PLANE_CTL_FORMAT_XRGB_8888;
6156a456
CK
2930 /*
2931 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2932 * to be already pre-multiplied. We need to add a knob (or a different
2933 * DRM_FORMAT) for user-space to configure that.
2934 */
f75fb42a 2935 case DRM_FORMAT_ABGR8888:
c34ce3d1 2936 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
6156a456 2937 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
6156a456 2938 case DRM_FORMAT_ARGB8888:
c34ce3d1 2939 return PLANE_CTL_FORMAT_XRGB_8888 |
6156a456 2940 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
70d21f0e 2941 case DRM_FORMAT_XRGB2101010:
c34ce3d1 2942 return PLANE_CTL_FORMAT_XRGB_2101010;
70d21f0e 2943 case DRM_FORMAT_XBGR2101010:
c34ce3d1 2944 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
6156a456 2945 case DRM_FORMAT_YUYV:
c34ce3d1 2946 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
6156a456 2947 case DRM_FORMAT_YVYU:
c34ce3d1 2948 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
6156a456 2949 case DRM_FORMAT_UYVY:
c34ce3d1 2950 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
6156a456 2951 case DRM_FORMAT_VYUY:
c34ce3d1 2952 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
70d21f0e 2953 default:
4249eeef 2954 MISSING_CASE(pixel_format);
70d21f0e 2955 }
8cfcba41 2956
c34ce3d1 2957 return 0;
6156a456 2958}
70d21f0e 2959
6156a456
CK
2960u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2961{
6156a456 2962 switch (fb_modifier) {
30af77c4 2963 case DRM_FORMAT_MOD_NONE:
70d21f0e 2964 break;
30af77c4 2965 case I915_FORMAT_MOD_X_TILED:
c34ce3d1 2966 return PLANE_CTL_TILED_X;
b321803d 2967 case I915_FORMAT_MOD_Y_TILED:
c34ce3d1 2968 return PLANE_CTL_TILED_Y;
b321803d 2969 case I915_FORMAT_MOD_Yf_TILED:
c34ce3d1 2970 return PLANE_CTL_TILED_YF;
70d21f0e 2971 default:
6156a456 2972 MISSING_CASE(fb_modifier);
70d21f0e 2973 }
8cfcba41 2974
c34ce3d1 2975 return 0;
6156a456 2976}
70d21f0e 2977
6156a456
CK
2978u32 skl_plane_ctl_rotation(unsigned int rotation)
2979{
3b7a5119 2980 switch (rotation) {
6156a456
CK
2981 case BIT(DRM_ROTATE_0):
2982 break;
1e8df167
SJ
2983 /*
2984 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
2985 * while i915 HW rotation is clockwise, thats why this swapping.
2986 */
3b7a5119 2987 case BIT(DRM_ROTATE_90):
1e8df167 2988 return PLANE_CTL_ROTATE_270;
3b7a5119 2989 case BIT(DRM_ROTATE_180):
c34ce3d1 2990 return PLANE_CTL_ROTATE_180;
3b7a5119 2991 case BIT(DRM_ROTATE_270):
1e8df167 2992 return PLANE_CTL_ROTATE_90;
6156a456
CK
2993 default:
2994 MISSING_CASE(rotation);
2995 }
2996
c34ce3d1 2997 return 0;
6156a456
CK
2998}
2999
a8d201af
ML
3000static void skylake_update_primary_plane(struct drm_plane *plane,
3001 const struct intel_crtc_state *crtc_state,
3002 const struct intel_plane_state *plane_state)
6156a456 3003{
a8d201af 3004 struct drm_device *dev = plane->dev;
6156a456 3005 struct drm_i915_private *dev_priv = dev->dev_private;
a8d201af
ML
3006 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3007 struct drm_framebuffer *fb = plane_state->base.fb;
3008 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
6156a456
CK
3009 int pipe = intel_crtc->pipe;
3010 u32 plane_ctl, stride_div, stride;
3011 u32 tile_height, plane_offset, plane_size;
a8d201af 3012 unsigned int rotation = plane_state->base.rotation;
6156a456 3013 int x_offset, y_offset;
44eb0cb9 3014 u32 surf_addr;
a8d201af
ML
3015 int scaler_id = plane_state->scaler_id;
3016 int src_x = plane_state->src.x1 >> 16;
3017 int src_y = plane_state->src.y1 >> 16;
3018 int src_w = drm_rect_width(&plane_state->src) >> 16;
3019 int src_h = drm_rect_height(&plane_state->src) >> 16;
3020 int dst_x = plane_state->dst.x1;
3021 int dst_y = plane_state->dst.y1;
3022 int dst_w = drm_rect_width(&plane_state->dst);
3023 int dst_h = drm_rect_height(&plane_state->dst);
70d21f0e 3024
6156a456
CK
3025 plane_ctl = PLANE_CTL_ENABLE |
3026 PLANE_CTL_PIPE_GAMMA_ENABLE |
3027 PLANE_CTL_PIPE_CSC_ENABLE;
3028
3029 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3030 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
3031 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
6156a456
CK
3032 plane_ctl |= skl_plane_ctl_rotation(rotation);
3033
7b49f948 3034 stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
b321803d 3035 fb->pixel_format);
dedf278c 3036 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
3b7a5119 3037
a42e5a23
PZ
3038 WARN_ON(drm_rect_width(&plane_state->src) == 0);
3039
3b7a5119 3040 if (intel_rotation_90_or_270(rotation)) {
832be82f
VS
3041 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
3042
3b7a5119 3043 /* stride = Surface height in tiles */
832be82f 3044 tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
3b7a5119 3045 stride = DIV_ROUND_UP(fb->height, tile_height);
a8d201af
ML
3046 x_offset = stride * tile_height - src_y - src_h;
3047 y_offset = src_x;
6156a456 3048 plane_size = (src_w - 1) << 16 | (src_h - 1);
3b7a5119
SJ
3049 } else {
3050 stride = fb->pitches[0] / stride_div;
a8d201af
ML
3051 x_offset = src_x;
3052 y_offset = src_y;
6156a456 3053 plane_size = (src_h - 1) << 16 | (src_w - 1);
3b7a5119
SJ
3054 }
3055 plane_offset = y_offset << 16 | x_offset;
b321803d 3056
2db3366b
PZ
3057 intel_crtc->adjusted_x = x_offset;
3058 intel_crtc->adjusted_y = y_offset;
3059
70d21f0e 3060 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
3b7a5119
SJ
3061 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3062 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3063 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
6156a456
CK
3064
3065 if (scaler_id >= 0) {
3066 uint32_t ps_ctrl = 0;
3067
3068 WARN_ON(!dst_w || !dst_h);
3069 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3070 crtc_state->scaler_state.scalers[scaler_id].mode;
3071 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3072 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3073 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3074 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3075 I915_WRITE(PLANE_POS(pipe, 0), 0);
3076 } else {
3077 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3078 }
3079
121920fa 3080 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
70d21f0e
DL
3081
3082 POSTING_READ(PLANE_SURF(pipe, 0));
3083}
3084
a8d201af
ML
3085static void skylake_disable_primary_plane(struct drm_plane *primary,
3086 struct drm_crtc *crtc)
17638cd6
JB
3087{
3088 struct drm_device *dev = crtc->dev;
3089 struct drm_i915_private *dev_priv = dev->dev_private;
a8d201af 3090 int pipe = to_intel_crtc(crtc)->pipe;
17638cd6 3091
a8d201af
ML
3092 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3093 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3094 POSTING_READ(PLANE_SURF(pipe, 0));
3095}
29b9bde6 3096
a8d201af
ML
3097/* Assume fb object is pinned & idle & fenced and just update base pointers */
3098static int
3099intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3100 int x, int y, enum mode_set_atomic state)
3101{
3102 /* Support for kgdboc is disabled, this needs a major rework. */
3103 DRM_ERROR("legacy panic handler not supported any more.\n");
3104
3105 return -ENODEV;
81255565
JB
3106}
3107
7514747d 3108static void intel_complete_page_flips(struct drm_device *dev)
96a02917 3109{
96a02917
VS
3110 struct drm_crtc *crtc;
3111
70e1e0ec 3112 for_each_crtc(dev, crtc) {
96a02917
VS
3113 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3114 enum plane plane = intel_crtc->plane;
3115
3116 intel_prepare_page_flip(dev, plane);
3117 intel_finish_page_flip_plane(dev, plane);
3118 }
7514747d
VS
3119}
3120
3121static void intel_update_primary_planes(struct drm_device *dev)
3122{
7514747d 3123 struct drm_crtc *crtc;
96a02917 3124
70e1e0ec 3125 for_each_crtc(dev, crtc) {
11c22da6
ML
3126 struct intel_plane *plane = to_intel_plane(crtc->primary);
3127 struct intel_plane_state *plane_state;
96a02917 3128
11c22da6 3129 drm_modeset_lock_crtc(crtc, &plane->base);
11c22da6
ML
3130 plane_state = to_intel_plane_state(plane->base.state);
3131
a8d201af
ML
3132 if (plane_state->visible)
3133 plane->update_plane(&plane->base,
3134 to_intel_crtc_state(crtc->state),
3135 plane_state);
11c22da6
ML
3136
3137 drm_modeset_unlock_crtc(crtc);
96a02917
VS
3138 }
3139}
3140
7514747d
VS
3141void intel_prepare_reset(struct drm_device *dev)
3142{
3143 /* no reset support for gen2 */
3144 if (IS_GEN2(dev))
3145 return;
3146
3147 /* reset doesn't touch the display */
3148 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3149 return;
3150
3151 drm_modeset_lock_all(dev);
f98ce92f
VS
3152 /*
3153 * Disabling the crtcs gracefully seems nicer. Also the
3154 * g33 docs say we should at least disable all the planes.
3155 */
6b72d486 3156 intel_display_suspend(dev);
7514747d
VS
3157}
3158
3159void intel_finish_reset(struct drm_device *dev)
3160{
3161 struct drm_i915_private *dev_priv = to_i915(dev);
3162
3163 /*
3164 * Flips in the rings will be nuked by the reset,
3165 * so complete all pending flips so that user space
3166 * will get its events and not get stuck.
3167 */
3168 intel_complete_page_flips(dev);
3169
3170 /* no reset support for gen2 */
3171 if (IS_GEN2(dev))
3172 return;
3173
3174 /* reset doesn't touch the display */
3175 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3176 /*
3177 * Flips in the rings have been nuked by the reset,
3178 * so update the base address of all primary
3179 * planes to the the last fb to make sure we're
3180 * showing the correct fb after a reset.
11c22da6
ML
3181 *
3182 * FIXME: Atomic will make this obsolete since we won't schedule
3183 * CS-based flips (which might get lost in gpu resets) any more.
7514747d
VS
3184 */
3185 intel_update_primary_planes(dev);
3186 return;
3187 }
3188
3189 /*
3190 * The display has been reset as well,
3191 * so need a full re-initialization.
3192 */
3193 intel_runtime_pm_disable_interrupts(dev_priv);
3194 intel_runtime_pm_enable_interrupts(dev_priv);
3195
3196 intel_modeset_init_hw(dev);
3197
3198 spin_lock_irq(&dev_priv->irq_lock);
3199 if (dev_priv->display.hpd_irq_setup)
3200 dev_priv->display.hpd_irq_setup(dev);
3201 spin_unlock_irq(&dev_priv->irq_lock);
3202
043e9bda 3203 intel_display_resume(dev);
7514747d
VS
3204
3205 intel_hpd_init(dev_priv);
3206
3207 drm_modeset_unlock_all(dev);
3208}
3209
7d5e3799
CW
3210static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3211{
3212 struct drm_device *dev = crtc->dev;
3213 struct drm_i915_private *dev_priv = dev->dev_private;
3214 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7d5e3799
CW
3215 bool pending;
3216
3217 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3218 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3219 return false;
3220
5e2d7afc 3221 spin_lock_irq(&dev->event_lock);
7d5e3799 3222 pending = to_intel_crtc(crtc)->unpin_work != NULL;
5e2d7afc 3223 spin_unlock_irq(&dev->event_lock);
7d5e3799
CW
3224
3225 return pending;
3226}
3227
bfd16b2a
ML
3228static void intel_update_pipe_config(struct intel_crtc *crtc,
3229 struct intel_crtc_state *old_crtc_state)
e30e8f75
GP
3230{
3231 struct drm_device *dev = crtc->base.dev;
3232 struct drm_i915_private *dev_priv = dev->dev_private;
bfd16b2a
ML
3233 struct intel_crtc_state *pipe_config =
3234 to_intel_crtc_state(crtc->base.state);
e30e8f75 3235
bfd16b2a
ML
3236 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3237 crtc->base.mode = crtc->base.state->mode;
3238
3239 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3240 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3241 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
e30e8f75 3242
44522d85 3243 if (HAS_DDI(dev))
8563b1e8 3244 intel_color_set_csc(&crtc->base);
44522d85 3245
e30e8f75
GP
3246 /*
3247 * Update pipe size and adjust fitter if needed: the reason for this is
3248 * that in compute_mode_changes we check the native mode (not the pfit
3249 * mode) to see if we can flip rather than do a full mode set. In the
3250 * fastboot case, we'll flip, but if we don't update the pipesrc and
3251 * pfit state, we'll end up with a big fb scanned out into the wrong
3252 * sized surface.
e30e8f75
GP
3253 */
3254
e30e8f75 3255 I915_WRITE(PIPESRC(crtc->pipe),
bfd16b2a
ML
3256 ((pipe_config->pipe_src_w - 1) << 16) |
3257 (pipe_config->pipe_src_h - 1));
3258
3259 /* on skylake this is done by detaching scalers */
3260 if (INTEL_INFO(dev)->gen >= 9) {
3261 skl_detach_scalers(crtc);
3262
3263 if (pipe_config->pch_pfit.enabled)
3264 skylake_pfit_enable(crtc);
3265 } else if (HAS_PCH_SPLIT(dev)) {
3266 if (pipe_config->pch_pfit.enabled)
3267 ironlake_pfit_enable(crtc);
3268 else if (old_crtc_state->pch_pfit.enabled)
3269 ironlake_pfit_disable(crtc, true);
e30e8f75 3270 }
e30e8f75
GP
3271}
3272
5e84e1a4
ZW
3273static void intel_fdi_normal_train(struct drm_crtc *crtc)
3274{
3275 struct drm_device *dev = crtc->dev;
3276 struct drm_i915_private *dev_priv = dev->dev_private;
3277 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3278 int pipe = intel_crtc->pipe;
f0f59a00
VS
3279 i915_reg_t reg;
3280 u32 temp;
5e84e1a4
ZW
3281
3282 /* enable normal train */
3283 reg = FDI_TX_CTL(pipe);
3284 temp = I915_READ(reg);
61e499bf 3285 if (IS_IVYBRIDGE(dev)) {
357555c0
JB
3286 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3287 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
3288 } else {
3289 temp &= ~FDI_LINK_TRAIN_NONE;
3290 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 3291 }
5e84e1a4
ZW
3292 I915_WRITE(reg, temp);
3293
3294 reg = FDI_RX_CTL(pipe);
3295 temp = I915_READ(reg);
3296 if (HAS_PCH_CPT(dev)) {
3297 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3298 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3299 } else {
3300 temp &= ~FDI_LINK_TRAIN_NONE;
3301 temp |= FDI_LINK_TRAIN_NONE;
3302 }
3303 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3304
3305 /* wait one idle pattern time */
3306 POSTING_READ(reg);
3307 udelay(1000);
357555c0
JB
3308
3309 /* IVB wants error correction enabled */
3310 if (IS_IVYBRIDGE(dev))
3311 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3312 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
3313}
3314
8db9d77b
ZW
3315/* The FDI link training functions for ILK/Ibexpeak. */
3316static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3317{
3318 struct drm_device *dev = crtc->dev;
3319 struct drm_i915_private *dev_priv = dev->dev_private;
3320 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3321 int pipe = intel_crtc->pipe;
f0f59a00
VS
3322 i915_reg_t reg;
3323 u32 temp, tries;
8db9d77b 3324
1c8562f6 3325 /* FDI needs bits from pipe first */
0fc932b8 3326 assert_pipe_enabled(dev_priv, pipe);
0fc932b8 3327
e1a44743
AJ
3328 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3329 for train result */
5eddb70b
CW
3330 reg = FDI_RX_IMR(pipe);
3331 temp = I915_READ(reg);
e1a44743
AJ
3332 temp &= ~FDI_RX_SYMBOL_LOCK;
3333 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3334 I915_WRITE(reg, temp);
3335 I915_READ(reg);
e1a44743
AJ
3336 udelay(150);
3337
8db9d77b 3338 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3339 reg = FDI_TX_CTL(pipe);
3340 temp = I915_READ(reg);
627eb5a3 3341 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3342 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3343 temp &= ~FDI_LINK_TRAIN_NONE;
3344 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 3345 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3346
5eddb70b
CW
3347 reg = FDI_RX_CTL(pipe);
3348 temp = I915_READ(reg);
8db9d77b
ZW
3349 temp &= ~FDI_LINK_TRAIN_NONE;
3350 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b
CW
3351 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3352
3353 POSTING_READ(reg);
8db9d77b
ZW
3354 udelay(150);
3355
5b2adf89 3356 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
DV
3357 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3358 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3359 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 3360
5eddb70b 3361 reg = FDI_RX_IIR(pipe);
e1a44743 3362 for (tries = 0; tries < 5; tries++) {
5eddb70b 3363 temp = I915_READ(reg);
8db9d77b
ZW
3364 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3365
3366 if ((temp & FDI_RX_BIT_LOCK)) {
3367 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 3368 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
3369 break;
3370 }
8db9d77b 3371 }
e1a44743 3372 if (tries == 5)
5eddb70b 3373 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3374
3375 /* Train 2 */
5eddb70b
CW
3376 reg = FDI_TX_CTL(pipe);
3377 temp = I915_READ(reg);
8db9d77b
ZW
3378 temp &= ~FDI_LINK_TRAIN_NONE;
3379 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3380 I915_WRITE(reg, temp);
8db9d77b 3381
5eddb70b
CW
3382 reg = FDI_RX_CTL(pipe);
3383 temp = I915_READ(reg);
8db9d77b
ZW
3384 temp &= ~FDI_LINK_TRAIN_NONE;
3385 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3386 I915_WRITE(reg, temp);
8db9d77b 3387
5eddb70b
CW
3388 POSTING_READ(reg);
3389 udelay(150);
8db9d77b 3390
5eddb70b 3391 reg = FDI_RX_IIR(pipe);
e1a44743 3392 for (tries = 0; tries < 5; tries++) {
5eddb70b 3393 temp = I915_READ(reg);
8db9d77b
ZW
3394 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3395
3396 if (temp & FDI_RX_SYMBOL_LOCK) {
5eddb70b 3397 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
3398 DRM_DEBUG_KMS("FDI train 2 done.\n");
3399 break;
3400 }
8db9d77b 3401 }
e1a44743 3402 if (tries == 5)
5eddb70b 3403 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3404
3405 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 3406
8db9d77b
ZW
3407}
3408
0206e353 3409static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
3410 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3411 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3412 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3413 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3414};
3415
3416/* The FDI link training functions for SNB/Cougarpoint. */
3417static void gen6_fdi_link_train(struct drm_crtc *crtc)
3418{
3419 struct drm_device *dev = crtc->dev;
3420 struct drm_i915_private *dev_priv = dev->dev_private;
3421 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3422 int pipe = intel_crtc->pipe;
f0f59a00
VS
3423 i915_reg_t reg;
3424 u32 temp, i, retry;
8db9d77b 3425
e1a44743
AJ
3426 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3427 for train result */
5eddb70b
CW
3428 reg = FDI_RX_IMR(pipe);
3429 temp = I915_READ(reg);
e1a44743
AJ
3430 temp &= ~FDI_RX_SYMBOL_LOCK;
3431 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3432 I915_WRITE(reg, temp);
3433
3434 POSTING_READ(reg);
e1a44743
AJ
3435 udelay(150);
3436
8db9d77b 3437 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3438 reg = FDI_TX_CTL(pipe);
3439 temp = I915_READ(reg);
627eb5a3 3440 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3441 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3442 temp &= ~FDI_LINK_TRAIN_NONE;
3443 temp |= FDI_LINK_TRAIN_PATTERN_1;
3444 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3445 /* SNB-B */
3446 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 3447 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3448
d74cf324
DV
3449 I915_WRITE(FDI_RX_MISC(pipe),
3450 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3451
5eddb70b
CW
3452 reg = FDI_RX_CTL(pipe);
3453 temp = I915_READ(reg);
8db9d77b
ZW
3454 if (HAS_PCH_CPT(dev)) {
3455 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3456 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3457 } else {
3458 temp &= ~FDI_LINK_TRAIN_NONE;
3459 temp |= FDI_LINK_TRAIN_PATTERN_1;
3460 }
5eddb70b
CW
3461 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3462
3463 POSTING_READ(reg);
8db9d77b
ZW
3464 udelay(150);
3465
0206e353 3466 for (i = 0; i < 4; i++) {
5eddb70b
CW
3467 reg = FDI_TX_CTL(pipe);
3468 temp = I915_READ(reg);
8db9d77b
ZW
3469 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3470 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3471 I915_WRITE(reg, temp);
3472
3473 POSTING_READ(reg);
8db9d77b
ZW
3474 udelay(500);
3475
fa37d39e
SP
3476 for (retry = 0; retry < 5; retry++) {
3477 reg = FDI_RX_IIR(pipe);
3478 temp = I915_READ(reg);
3479 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3480 if (temp & FDI_RX_BIT_LOCK) {
3481 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3482 DRM_DEBUG_KMS("FDI train 1 done.\n");
3483 break;
3484 }
3485 udelay(50);
8db9d77b 3486 }
fa37d39e
SP
3487 if (retry < 5)
3488 break;
8db9d77b
ZW
3489 }
3490 if (i == 4)
5eddb70b 3491 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3492
3493 /* Train 2 */
5eddb70b
CW
3494 reg = FDI_TX_CTL(pipe);
3495 temp = I915_READ(reg);
8db9d77b
ZW
3496 temp &= ~FDI_LINK_TRAIN_NONE;
3497 temp |= FDI_LINK_TRAIN_PATTERN_2;
3498 if (IS_GEN6(dev)) {
3499 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3500 /* SNB-B */
3501 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3502 }
5eddb70b 3503 I915_WRITE(reg, temp);
8db9d77b 3504
5eddb70b
CW
3505 reg = FDI_RX_CTL(pipe);
3506 temp = I915_READ(reg);
8db9d77b
ZW
3507 if (HAS_PCH_CPT(dev)) {
3508 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3509 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3510 } else {
3511 temp &= ~FDI_LINK_TRAIN_NONE;
3512 temp |= FDI_LINK_TRAIN_PATTERN_2;
3513 }
5eddb70b
CW
3514 I915_WRITE(reg, temp);
3515
3516 POSTING_READ(reg);
8db9d77b
ZW
3517 udelay(150);
3518
0206e353 3519 for (i = 0; i < 4; i++) {
5eddb70b
CW
3520 reg = FDI_TX_CTL(pipe);
3521 temp = I915_READ(reg);
8db9d77b
ZW
3522 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3523 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3524 I915_WRITE(reg, temp);
3525
3526 POSTING_READ(reg);
8db9d77b
ZW
3527 udelay(500);
3528
fa37d39e
SP
3529 for (retry = 0; retry < 5; retry++) {
3530 reg = FDI_RX_IIR(pipe);
3531 temp = I915_READ(reg);
3532 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3533 if (temp & FDI_RX_SYMBOL_LOCK) {
3534 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3535 DRM_DEBUG_KMS("FDI train 2 done.\n");
3536 break;
3537 }
3538 udelay(50);
8db9d77b 3539 }
fa37d39e
SP
3540 if (retry < 5)
3541 break;
8db9d77b
ZW
3542 }
3543 if (i == 4)
5eddb70b 3544 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3545
3546 DRM_DEBUG_KMS("FDI train done.\n");
3547}
3548
357555c0
JB
3549/* Manual link training for Ivy Bridge A0 parts */
3550static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3551{
3552 struct drm_device *dev = crtc->dev;
3553 struct drm_i915_private *dev_priv = dev->dev_private;
3554 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3555 int pipe = intel_crtc->pipe;
f0f59a00
VS
3556 i915_reg_t reg;
3557 u32 temp, i, j;
357555c0
JB
3558
3559 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3560 for train result */
3561 reg = FDI_RX_IMR(pipe);
3562 temp = I915_READ(reg);
3563 temp &= ~FDI_RX_SYMBOL_LOCK;
3564 temp &= ~FDI_RX_BIT_LOCK;
3565 I915_WRITE(reg, temp);
3566
3567 POSTING_READ(reg);
3568 udelay(150);
3569
01a415fd
DV
3570 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3571 I915_READ(FDI_RX_IIR(pipe)));
3572
139ccd3f
JB
3573 /* Try each vswing and preemphasis setting twice before moving on */
3574 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3575 /* disable first in case we need to retry */
3576 reg = FDI_TX_CTL(pipe);
3577 temp = I915_READ(reg);
3578 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3579 temp &= ~FDI_TX_ENABLE;
3580 I915_WRITE(reg, temp);
357555c0 3581
139ccd3f
JB
3582 reg = FDI_RX_CTL(pipe);
3583 temp = I915_READ(reg);
3584 temp &= ~FDI_LINK_TRAIN_AUTO;
3585 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3586 temp &= ~FDI_RX_ENABLE;
3587 I915_WRITE(reg, temp);
357555c0 3588
139ccd3f 3589 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
3590 reg = FDI_TX_CTL(pipe);
3591 temp = I915_READ(reg);
139ccd3f 3592 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3593 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
139ccd3f 3594 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 3595 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
3596 temp |= snb_b_fdi_train_param[j/2];
3597 temp |= FDI_COMPOSITE_SYNC;
3598 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 3599
139ccd3f
JB
3600 I915_WRITE(FDI_RX_MISC(pipe),
3601 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 3602
139ccd3f 3603 reg = FDI_RX_CTL(pipe);
357555c0 3604 temp = I915_READ(reg);
139ccd3f
JB
3605 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3606 temp |= FDI_COMPOSITE_SYNC;
3607 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 3608
139ccd3f
JB
3609 POSTING_READ(reg);
3610 udelay(1); /* should be 0.5us */
357555c0 3611
139ccd3f
JB
3612 for (i = 0; i < 4; i++) {
3613 reg = FDI_RX_IIR(pipe);
3614 temp = I915_READ(reg);
3615 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3616
139ccd3f
JB
3617 if (temp & FDI_RX_BIT_LOCK ||
3618 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3619 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3620 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3621 i);
3622 break;
3623 }
3624 udelay(1); /* should be 0.5us */
3625 }
3626 if (i == 4) {
3627 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3628 continue;
3629 }
357555c0 3630
139ccd3f 3631 /* Train 2 */
357555c0
JB
3632 reg = FDI_TX_CTL(pipe);
3633 temp = I915_READ(reg);
139ccd3f
JB
3634 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3635 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3636 I915_WRITE(reg, temp);
3637
3638 reg = FDI_RX_CTL(pipe);
3639 temp = I915_READ(reg);
3640 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3641 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
3642 I915_WRITE(reg, temp);
3643
3644 POSTING_READ(reg);
139ccd3f 3645 udelay(2); /* should be 1.5us */
357555c0 3646
139ccd3f
JB
3647 for (i = 0; i < 4; i++) {
3648 reg = FDI_RX_IIR(pipe);
3649 temp = I915_READ(reg);
3650 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3651
139ccd3f
JB
3652 if (temp & FDI_RX_SYMBOL_LOCK ||
3653 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3654 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3655 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3656 i);
3657 goto train_done;
3658 }
3659 udelay(2); /* should be 1.5us */
357555c0 3660 }
139ccd3f
JB
3661 if (i == 4)
3662 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 3663 }
357555c0 3664
139ccd3f 3665train_done:
357555c0
JB
3666 DRM_DEBUG_KMS("FDI train done.\n");
3667}
3668
88cefb6c 3669static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 3670{
88cefb6c 3671 struct drm_device *dev = intel_crtc->base.dev;
2c07245f 3672 struct drm_i915_private *dev_priv = dev->dev_private;
2c07245f 3673 int pipe = intel_crtc->pipe;
f0f59a00
VS
3674 i915_reg_t reg;
3675 u32 temp;
c64e311e 3676
c98e9dcf 3677 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
3678 reg = FDI_RX_CTL(pipe);
3679 temp = I915_READ(reg);
627eb5a3 3680 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
6e3c9717 3681 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
dfd07d72 3682 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
3683 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3684
3685 POSTING_READ(reg);
c98e9dcf
JB
3686 udelay(200);
3687
3688 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
3689 temp = I915_READ(reg);
3690 I915_WRITE(reg, temp | FDI_PCDCLK);
3691
3692 POSTING_READ(reg);
c98e9dcf
JB
3693 udelay(200);
3694
20749730
PZ
3695 /* Enable CPU FDI TX PLL, always on for Ironlake */
3696 reg = FDI_TX_CTL(pipe);
3697 temp = I915_READ(reg);
3698 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3699 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 3700
20749730
PZ
3701 POSTING_READ(reg);
3702 udelay(100);
6be4a607 3703 }
0e23b99d
JB
3704}
3705
88cefb6c
DV
3706static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3707{
3708 struct drm_device *dev = intel_crtc->base.dev;
3709 struct drm_i915_private *dev_priv = dev->dev_private;
3710 int pipe = intel_crtc->pipe;
f0f59a00
VS
3711 i915_reg_t reg;
3712 u32 temp;
88cefb6c
DV
3713
3714 /* Switch from PCDclk to Rawclk */
3715 reg = FDI_RX_CTL(pipe);
3716 temp = I915_READ(reg);
3717 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3718
3719 /* Disable CPU FDI TX PLL */
3720 reg = FDI_TX_CTL(pipe);
3721 temp = I915_READ(reg);
3722 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3723
3724 POSTING_READ(reg);
3725 udelay(100);
3726
3727 reg = FDI_RX_CTL(pipe);
3728 temp = I915_READ(reg);
3729 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3730
3731 /* Wait for the clocks to turn off. */
3732 POSTING_READ(reg);
3733 udelay(100);
3734}
3735
0fc932b8
JB
3736static void ironlake_fdi_disable(struct drm_crtc *crtc)
3737{
3738 struct drm_device *dev = crtc->dev;
3739 struct drm_i915_private *dev_priv = dev->dev_private;
3740 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3741 int pipe = intel_crtc->pipe;
f0f59a00
VS
3742 i915_reg_t reg;
3743 u32 temp;
0fc932b8
JB
3744
3745 /* disable CPU FDI tx and PCH FDI rx */
3746 reg = FDI_TX_CTL(pipe);
3747 temp = I915_READ(reg);
3748 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3749 POSTING_READ(reg);
3750
3751 reg = FDI_RX_CTL(pipe);
3752 temp = I915_READ(reg);
3753 temp &= ~(0x7 << 16);
dfd07d72 3754 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3755 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3756
3757 POSTING_READ(reg);
3758 udelay(100);
3759
3760 /* Ironlake workaround, disable clock pointer after downing FDI */
eba905b2 3761 if (HAS_PCH_IBX(dev))
6f06ce18 3762 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
0fc932b8
JB
3763
3764 /* still set train pattern 1 */
3765 reg = FDI_TX_CTL(pipe);
3766 temp = I915_READ(reg);
3767 temp &= ~FDI_LINK_TRAIN_NONE;
3768 temp |= FDI_LINK_TRAIN_PATTERN_1;
3769 I915_WRITE(reg, temp);
3770
3771 reg = FDI_RX_CTL(pipe);
3772 temp = I915_READ(reg);
3773 if (HAS_PCH_CPT(dev)) {
3774 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3775 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3776 } else {
3777 temp &= ~FDI_LINK_TRAIN_NONE;
3778 temp |= FDI_LINK_TRAIN_PATTERN_1;
3779 }
3780 /* BPC in FDI rx is consistent with that in PIPECONF */
3781 temp &= ~(0x07 << 16);
dfd07d72 3782 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3783 I915_WRITE(reg, temp);
3784
3785 POSTING_READ(reg);
3786 udelay(100);
3787}
3788
5dce5b93
CW
3789bool intel_has_pending_fb_unpin(struct drm_device *dev)
3790{
3791 struct intel_crtc *crtc;
3792
3793 /* Note that we don't need to be called with mode_config.lock here
3794 * as our list of CRTC objects is static for the lifetime of the
3795 * device and so cannot disappear as we iterate. Similarly, we can
3796 * happily treat the predicates as racy, atomic checks as userspace
3797 * cannot claim and pin a new fb without at least acquring the
3798 * struct_mutex and so serialising with us.
3799 */
d3fcc808 3800 for_each_intel_crtc(dev, crtc) {
5dce5b93
CW
3801 if (atomic_read(&crtc->unpin_work_count) == 0)
3802 continue;
3803
3804 if (crtc->unpin_work)
3805 intel_wait_for_vblank(dev, crtc->pipe);
3806
3807 return true;
3808 }
3809
3810 return false;
3811}
3812
d6bbafa1
CW
3813static void page_flip_completed(struct intel_crtc *intel_crtc)
3814{
3815 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3816 struct intel_unpin_work *work = intel_crtc->unpin_work;
3817
3818 /* ensure that the unpin work is consistent wrt ->pending. */
3819 smp_rmb();
3820 intel_crtc->unpin_work = NULL;
3821
3822 if (work->event)
3823 drm_send_vblank_event(intel_crtc->base.dev,
3824 intel_crtc->pipe,
3825 work->event);
3826
3827 drm_crtc_vblank_put(&intel_crtc->base);
3828
3829 wake_up_all(&dev_priv->pending_flip_queue);
3830 queue_work(dev_priv->wq, &work->work);
3831
3832 trace_i915_flip_complete(intel_crtc->plane,
3833 work->pending_flip_obj);
3834}
3835
5008e874 3836static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
e6c3a2a6 3837{
0f91128d 3838 struct drm_device *dev = crtc->dev;
5bb61643 3839 struct drm_i915_private *dev_priv = dev->dev_private;
5008e874 3840 long ret;
e6c3a2a6 3841
2c10d571 3842 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
5008e874
ML
3843
3844 ret = wait_event_interruptible_timeout(
3845 dev_priv->pending_flip_queue,
3846 !intel_crtc_has_pending_flip(crtc),
3847 60*HZ);
3848
3849 if (ret < 0)
3850 return ret;
3851
3852 if (ret == 0) {
9c787942 3853 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2c10d571 3854
5e2d7afc 3855 spin_lock_irq(&dev->event_lock);
9c787942
CW
3856 if (intel_crtc->unpin_work) {
3857 WARN_ONCE(1, "Removing stuck page flip\n");
3858 page_flip_completed(intel_crtc);
3859 }
5e2d7afc 3860 spin_unlock_irq(&dev->event_lock);
9c787942 3861 }
5bb61643 3862
5008e874 3863 return 0;
e6c3a2a6
CW
3864}
3865
060f02d8
VS
3866static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
3867{
3868 u32 temp;
3869
3870 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3871
3872 mutex_lock(&dev_priv->sb_lock);
3873
3874 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3875 temp |= SBI_SSCCTL_DISABLE;
3876 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
3877
3878 mutex_unlock(&dev_priv->sb_lock);
3879}
3880
e615efe4
ED
3881/* Program iCLKIP clock to the desired frequency */
3882static void lpt_program_iclkip(struct drm_crtc *crtc)
3883{
64b46a06 3884 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6e3c9717 3885 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
e615efe4
ED
3886 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3887 u32 temp;
3888
060f02d8 3889 lpt_disable_iclkip(dev_priv);
e615efe4 3890
64b46a06
VS
3891 /* The iCLK virtual clock root frequency is in MHz,
3892 * but the adjusted_mode->crtc_clock in in KHz. To get the
3893 * divisors, it is necessary to divide one by another, so we
3894 * convert the virtual clock precision to KHz here for higher
3895 * precision.
3896 */
3897 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
e615efe4
ED
3898 u32 iclk_virtual_root_freq = 172800 * 1000;
3899 u32 iclk_pi_range = 64;
64b46a06 3900 u32 desired_divisor;
e615efe4 3901
64b46a06
VS
3902 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3903 clock << auxdiv);
3904 divsel = (desired_divisor / iclk_pi_range) - 2;
3905 phaseinc = desired_divisor % iclk_pi_range;
e615efe4 3906
64b46a06
VS
3907 /*
3908 * Near 20MHz is a corner case which is
3909 * out of range for the 7-bit divisor
3910 */
3911 if (divsel <= 0x7f)
3912 break;
e615efe4
ED
3913 }
3914
3915 /* This should not happen with any sane values */
3916 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3917 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3918 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3919 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3920
3921 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 3922 clock,
e615efe4
ED
3923 auxdiv,
3924 divsel,
3925 phasedir,
3926 phaseinc);
3927
060f02d8
VS
3928 mutex_lock(&dev_priv->sb_lock);
3929
e615efe4 3930 /* Program SSCDIVINTPHASE6 */
988d6ee8 3931 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
3932 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3933 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3934 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3935 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3936 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3937 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 3938 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
3939
3940 /* Program SSCAUXDIV */
988d6ee8 3941 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
3942 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3943 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 3944 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
3945
3946 /* Enable modulator and associated divider */
988d6ee8 3947 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 3948 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 3949 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4 3950
060f02d8
VS
3951 mutex_unlock(&dev_priv->sb_lock);
3952
e615efe4
ED
3953 /* Wait for initialization time */
3954 udelay(24);
3955
3956 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
3957}
3958
8802e5b6
VS
3959int lpt_get_iclkip(struct drm_i915_private *dev_priv)
3960{
3961 u32 divsel, phaseinc, auxdiv;
3962 u32 iclk_virtual_root_freq = 172800 * 1000;
3963 u32 iclk_pi_range = 64;
3964 u32 desired_divisor;
3965 u32 temp;
3966
3967 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
3968 return 0;
3969
3970 mutex_lock(&dev_priv->sb_lock);
3971
3972 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3973 if (temp & SBI_SSCCTL_DISABLE) {
3974 mutex_unlock(&dev_priv->sb_lock);
3975 return 0;
3976 }
3977
3978 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
3979 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
3980 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
3981 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
3982 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
3983
3984 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
3985 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
3986 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
3987
3988 mutex_unlock(&dev_priv->sb_lock);
3989
3990 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
3991
3992 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3993 desired_divisor << auxdiv);
3994}
3995
275f01b2
DV
3996static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3997 enum pipe pch_transcoder)
3998{
3999 struct drm_device *dev = crtc->base.dev;
4000 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 4001 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
275f01b2
DV
4002
4003 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4004 I915_READ(HTOTAL(cpu_transcoder)));
4005 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4006 I915_READ(HBLANK(cpu_transcoder)));
4007 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4008 I915_READ(HSYNC(cpu_transcoder)));
4009
4010 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4011 I915_READ(VTOTAL(cpu_transcoder)));
4012 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4013 I915_READ(VBLANK(cpu_transcoder)));
4014 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4015 I915_READ(VSYNC(cpu_transcoder)));
4016 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4017 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4018}
4019
003632d9 4020static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
1fbc0d78
DV
4021{
4022 struct drm_i915_private *dev_priv = dev->dev_private;
4023 uint32_t temp;
4024
4025 temp = I915_READ(SOUTH_CHICKEN1);
003632d9 4026 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
1fbc0d78
DV
4027 return;
4028
4029 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4030 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4031
003632d9
ACO
4032 temp &= ~FDI_BC_BIFURCATION_SELECT;
4033 if (enable)
4034 temp |= FDI_BC_BIFURCATION_SELECT;
4035
4036 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
1fbc0d78
DV
4037 I915_WRITE(SOUTH_CHICKEN1, temp);
4038 POSTING_READ(SOUTH_CHICKEN1);
4039}
4040
4041static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4042{
4043 struct drm_device *dev = intel_crtc->base.dev;
1fbc0d78
DV
4044
4045 switch (intel_crtc->pipe) {
4046 case PIPE_A:
4047 break;
4048 case PIPE_B:
6e3c9717 4049 if (intel_crtc->config->fdi_lanes > 2)
003632d9 4050 cpt_set_fdi_bc_bifurcation(dev, false);
1fbc0d78 4051 else
003632d9 4052 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4053
4054 break;
4055 case PIPE_C:
003632d9 4056 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4057
4058 break;
4059 default:
4060 BUG();
4061 }
4062}
4063
c48b5305
VS
4064/* Return which DP Port should be selected for Transcoder DP control */
4065static enum port
4066intel_trans_dp_port_sel(struct drm_crtc *crtc)
4067{
4068 struct drm_device *dev = crtc->dev;
4069 struct intel_encoder *encoder;
4070
4071 for_each_encoder_on_crtc(dev, crtc, encoder) {
4072 if (encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
4073 encoder->type == INTEL_OUTPUT_EDP)
4074 return enc_to_dig_port(&encoder->base)->port;
4075 }
4076
4077 return -1;
4078}
4079
f67a559d
JB
4080/*
4081 * Enable PCH resources required for PCH ports:
4082 * - PCH PLLs
4083 * - FDI training & RX/TX
4084 * - update transcoder timings
4085 * - DP transcoding bits
4086 * - transcoder
4087 */
4088static void ironlake_pch_enable(struct drm_crtc *crtc)
0e23b99d
JB
4089{
4090 struct drm_device *dev = crtc->dev;
4091 struct drm_i915_private *dev_priv = dev->dev_private;
4092 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4093 int pipe = intel_crtc->pipe;
f0f59a00 4094 u32 temp;
2c07245f 4095
ab9412ba 4096 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 4097
1fbc0d78
DV
4098 if (IS_IVYBRIDGE(dev))
4099 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4100
cd986abb
DV
4101 /* Write the TU size bits before fdi link training, so that error
4102 * detection works. */
4103 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4104 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4105
3860b2ec
VS
4106 /*
4107 * Sometimes spurious CPU pipe underruns happen during FDI
4108 * training, at least with VGA+HDMI cloning. Suppress them.
4109 */
4110 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4111
c98e9dcf 4112 /* For PCH output, training FDI link */
674cf967 4113 dev_priv->display.fdi_link_train(crtc);
2c07245f 4114
3ad8a208
DV
4115 /* We need to program the right clock selection before writing the pixel
4116 * mutliplier into the DPLL. */
303b81e0 4117 if (HAS_PCH_CPT(dev)) {
ee7b9f93 4118 u32 sel;
4b645f14 4119
c98e9dcf 4120 temp = I915_READ(PCH_DPLL_SEL);
11887397
DV
4121 temp |= TRANS_DPLL_ENABLE(pipe);
4122 sel = TRANS_DPLLB_SEL(pipe);
8106ddbd
ACO
4123 if (intel_crtc->config->shared_dpll ==
4124 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
ee7b9f93
JB
4125 temp |= sel;
4126 else
4127 temp &= ~sel;
c98e9dcf 4128 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 4129 }
5eddb70b 4130
3ad8a208
DV
4131 /* XXX: pch pll's can be enabled any time before we enable the PCH
4132 * transcoder, and we actually should do this to not upset any PCH
4133 * transcoder that already use the clock when we share it.
4134 *
4135 * Note that enable_shared_dpll tries to do the right thing, but
4136 * get_shared_dpll unconditionally resets the pll - we need that to have
4137 * the right LVDS enable sequence. */
85b3894f 4138 intel_enable_shared_dpll(intel_crtc);
3ad8a208 4139
d9b6cb56
JB
4140 /* set transcoder timing, panel must allow it */
4141 assert_panel_unlocked(dev_priv, pipe);
275f01b2 4142 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
8db9d77b 4143
303b81e0 4144 intel_fdi_normal_train(crtc);
5e84e1a4 4145
3860b2ec
VS
4146 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4147
c98e9dcf 4148 /* For PCH DP, enable TRANS_DP_CTL */
6e3c9717 4149 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
9c4edaee
VS
4150 const struct drm_display_mode *adjusted_mode =
4151 &intel_crtc->config->base.adjusted_mode;
dfd07d72 4152 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
f0f59a00 4153 i915_reg_t reg = TRANS_DP_CTL(pipe);
5eddb70b
CW
4154 temp = I915_READ(reg);
4155 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
4156 TRANS_DP_SYNC_MASK |
4157 TRANS_DP_BPC_MASK);
e3ef4479 4158 temp |= TRANS_DP_OUTPUT_ENABLE;
9325c9f0 4159 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf 4160
9c4edaee 4161 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 4162 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
9c4edaee 4163 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 4164 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf
JB
4165
4166 switch (intel_trans_dp_port_sel(crtc)) {
c48b5305 4167 case PORT_B:
5eddb70b 4168 temp |= TRANS_DP_PORT_SEL_B;
c98e9dcf 4169 break;
c48b5305 4170 case PORT_C:
5eddb70b 4171 temp |= TRANS_DP_PORT_SEL_C;
c98e9dcf 4172 break;
c48b5305 4173 case PORT_D:
5eddb70b 4174 temp |= TRANS_DP_PORT_SEL_D;
c98e9dcf
JB
4175 break;
4176 default:
e95d41e1 4177 BUG();
32f9d658 4178 }
2c07245f 4179
5eddb70b 4180 I915_WRITE(reg, temp);
6be4a607 4181 }
b52eb4dc 4182
b8a4f404 4183 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
4184}
4185
1507e5bd
PZ
4186static void lpt_pch_enable(struct drm_crtc *crtc)
4187{
4188 struct drm_device *dev = crtc->dev;
4189 struct drm_i915_private *dev_priv = dev->dev_private;
4190 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 4191 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
1507e5bd 4192
ab9412ba 4193 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
1507e5bd 4194
8c52b5e8 4195 lpt_program_iclkip(crtc);
1507e5bd 4196
0540e488 4197 /* Set transcoder timing. */
275f01b2 4198 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
1507e5bd 4199
937bb610 4200 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
4201}
4202
a1520318 4203static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57
JB
4204{
4205 struct drm_i915_private *dev_priv = dev->dev_private;
f0f59a00 4206 i915_reg_t dslreg = PIPEDSL(pipe);
d4270e57
JB
4207 u32 temp;
4208
4209 temp = I915_READ(dslreg);
4210 udelay(500);
4211 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 4212 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 4213 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
4214 }
4215}
4216
86adf9d7
ML
4217static int
4218skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4219 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4220 int src_w, int src_h, int dst_w, int dst_h)
a1b2278e 4221{
86adf9d7
ML
4222 struct intel_crtc_scaler_state *scaler_state =
4223 &crtc_state->scaler_state;
4224 struct intel_crtc *intel_crtc =
4225 to_intel_crtc(crtc_state->base.crtc);
a1b2278e 4226 int need_scaling;
6156a456
CK
4227
4228 need_scaling = intel_rotation_90_or_270(rotation) ?
4229 (src_h != dst_w || src_w != dst_h):
4230 (src_w != dst_w || src_h != dst_h);
a1b2278e
CK
4231
4232 /*
4233 * if plane is being disabled or scaler is no more required or force detach
4234 * - free scaler binded to this plane/crtc
4235 * - in order to do this, update crtc->scaler_usage
4236 *
4237 * Here scaler state in crtc_state is set free so that
4238 * scaler can be assigned to other user. Actual register
4239 * update to free the scaler is done in plane/panel-fit programming.
4240 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4241 */
86adf9d7 4242 if (force_detach || !need_scaling) {
a1b2278e 4243 if (*scaler_id >= 0) {
86adf9d7 4244 scaler_state->scaler_users &= ~(1 << scaler_user);
a1b2278e
CK
4245 scaler_state->scalers[*scaler_id].in_use = 0;
4246
86adf9d7
ML
4247 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4248 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4249 intel_crtc->pipe, scaler_user, *scaler_id,
a1b2278e
CK
4250 scaler_state->scaler_users);
4251 *scaler_id = -1;
4252 }
4253 return 0;
4254 }
4255
4256 /* range checks */
4257 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4258 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4259
4260 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4261 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
86adf9d7 4262 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
a1b2278e 4263 "size is out of scaler range\n",
86adf9d7 4264 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
a1b2278e
CK
4265 return -EINVAL;
4266 }
4267
86adf9d7
ML
4268 /* mark this plane as a scaler user in crtc_state */
4269 scaler_state->scaler_users |= (1 << scaler_user);
4270 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4271 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4272 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4273 scaler_state->scaler_users);
4274
4275 return 0;
4276}
4277
4278/**
4279 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4280 *
4281 * @state: crtc's scaler state
86adf9d7
ML
4282 *
4283 * Return
4284 * 0 - scaler_usage updated successfully
4285 * error - requested scaling cannot be supported or other error condition
4286 */
e435d6e5 4287int skl_update_scaler_crtc(struct intel_crtc_state *state)
86adf9d7
ML
4288{
4289 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
7c5f93b0 4290 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
86adf9d7
ML
4291
4292 DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
4293 intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
4294
e435d6e5 4295 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
fa5a7970 4296 &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
86adf9d7 4297 state->pipe_src_w, state->pipe_src_h,
aad941d5 4298 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
86adf9d7
ML
4299}
4300
4301/**
4302 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4303 *
4304 * @state: crtc's scaler state
86adf9d7
ML
4305 * @plane_state: atomic plane state to update
4306 *
4307 * Return
4308 * 0 - scaler_usage updated successfully
4309 * error - requested scaling cannot be supported or other error condition
4310 */
da20eabd
ML
4311static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4312 struct intel_plane_state *plane_state)
86adf9d7
ML
4313{
4314
4315 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
da20eabd
ML
4316 struct intel_plane *intel_plane =
4317 to_intel_plane(plane_state->base.plane);
86adf9d7
ML
4318 struct drm_framebuffer *fb = plane_state->base.fb;
4319 int ret;
4320
4321 bool force_detach = !fb || !plane_state->visible;
4322
4323 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
4324 intel_plane->base.base.id, intel_crtc->pipe,
4325 drm_plane_index(&intel_plane->base));
4326
4327 ret = skl_update_scaler(crtc_state, force_detach,
4328 drm_plane_index(&intel_plane->base),
4329 &plane_state->scaler_id,
4330 plane_state->base.rotation,
4331 drm_rect_width(&plane_state->src) >> 16,
4332 drm_rect_height(&plane_state->src) >> 16,
4333 drm_rect_width(&plane_state->dst),
4334 drm_rect_height(&plane_state->dst));
4335
4336 if (ret || plane_state->scaler_id < 0)
4337 return ret;
4338
a1b2278e 4339 /* check colorkey */
818ed961 4340 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
86adf9d7 4341 DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
818ed961 4342 intel_plane->base.base.id);
a1b2278e
CK
4343 return -EINVAL;
4344 }
4345
4346 /* Check src format */
86adf9d7
ML
4347 switch (fb->pixel_format) {
4348 case DRM_FORMAT_RGB565:
4349 case DRM_FORMAT_XBGR8888:
4350 case DRM_FORMAT_XRGB8888:
4351 case DRM_FORMAT_ABGR8888:
4352 case DRM_FORMAT_ARGB8888:
4353 case DRM_FORMAT_XRGB2101010:
4354 case DRM_FORMAT_XBGR2101010:
4355 case DRM_FORMAT_YUYV:
4356 case DRM_FORMAT_YVYU:
4357 case DRM_FORMAT_UYVY:
4358 case DRM_FORMAT_VYUY:
4359 break;
4360 default:
4361 DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
4362 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4363 return -EINVAL;
a1b2278e
CK
4364 }
4365
a1b2278e
CK
4366 return 0;
4367}
4368
e435d6e5
ML
4369static void skylake_scaler_disable(struct intel_crtc *crtc)
4370{
4371 int i;
4372
4373 for (i = 0; i < crtc->num_scalers; i++)
4374 skl_detach_scaler(crtc, i);
4375}
4376
4377static void skylake_pfit_enable(struct intel_crtc *crtc)
bd2e244f
JB
4378{
4379 struct drm_device *dev = crtc->base.dev;
4380 struct drm_i915_private *dev_priv = dev->dev_private;
4381 int pipe = crtc->pipe;
a1b2278e
CK
4382 struct intel_crtc_scaler_state *scaler_state =
4383 &crtc->config->scaler_state;
4384
4385 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4386
6e3c9717 4387 if (crtc->config->pch_pfit.enabled) {
a1b2278e
CK
4388 int id;
4389
4390 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4391 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4392 return;
4393 }
4394
4395 id = scaler_state->scaler_id;
4396 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4397 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4398 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4399 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4400
4401 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
bd2e244f
JB
4402 }
4403}
4404
b074cec8
JB
4405static void ironlake_pfit_enable(struct intel_crtc *crtc)
4406{
4407 struct drm_device *dev = crtc->base.dev;
4408 struct drm_i915_private *dev_priv = dev->dev_private;
4409 int pipe = crtc->pipe;
4410
6e3c9717 4411 if (crtc->config->pch_pfit.enabled) {
b074cec8
JB
4412 /* Force use of hard-coded filter coefficients
4413 * as some pre-programmed values are broken,
4414 * e.g. x201.
4415 */
4416 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4417 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4418 PF_PIPE_SEL_IVB(pipe));
4419 else
4420 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
6e3c9717
ACO
4421 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4422 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
d4270e57
JB
4423 }
4424}
4425
20bc8673 4426void hsw_enable_ips(struct intel_crtc *crtc)
d77e4531 4427{
cea165c3
VS
4428 struct drm_device *dev = crtc->base.dev;
4429 struct drm_i915_private *dev_priv = dev->dev_private;
d77e4531 4430
6e3c9717 4431 if (!crtc->config->ips_enabled)
d77e4531
PZ
4432 return;
4433
cea165c3
VS
4434 /* We can only enable IPS after we enable a plane and wait for a vblank */
4435 intel_wait_for_vblank(dev, crtc->pipe);
4436
d77e4531 4437 assert_plane_enabled(dev_priv, crtc->plane);
cea165c3 4438 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4439 mutex_lock(&dev_priv->rps.hw_lock);
4440 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4441 mutex_unlock(&dev_priv->rps.hw_lock);
4442 /* Quoting Art Runyan: "its not safe to expect any particular
4443 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
4444 * mailbox." Moreover, the mailbox may return a bogus state,
4445 * so we need to just enable it and continue on.
2a114cc1
BW
4446 */
4447 } else {
4448 I915_WRITE(IPS_CTL, IPS_ENABLE);
4449 /* The bit only becomes 1 in the next vblank, so this wait here
4450 * is essentially intel_wait_for_vblank. If we don't have this
4451 * and don't wait for vblanks until the end of crtc_enable, then
4452 * the HW state readout code will complain that the expected
4453 * IPS_CTL value is not the one we read. */
4454 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4455 DRM_ERROR("Timed out waiting for IPS enable\n");
4456 }
d77e4531
PZ
4457}
4458
20bc8673 4459void hsw_disable_ips(struct intel_crtc *crtc)
d77e4531
PZ
4460{
4461 struct drm_device *dev = crtc->base.dev;
4462 struct drm_i915_private *dev_priv = dev->dev_private;
4463
6e3c9717 4464 if (!crtc->config->ips_enabled)
d77e4531
PZ
4465 return;
4466
4467 assert_plane_enabled(dev_priv, crtc->plane);
23d0b130 4468 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4469 mutex_lock(&dev_priv->rps.hw_lock);
4470 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4471 mutex_unlock(&dev_priv->rps.hw_lock);
23d0b130
BW
4472 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4473 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4474 DRM_ERROR("Timed out waiting for IPS disable\n");
e59150dc 4475 } else {
2a114cc1 4476 I915_WRITE(IPS_CTL, 0);
e59150dc
JB
4477 POSTING_READ(IPS_CTL);
4478 }
d77e4531
PZ
4479
4480 /* We need to wait for a vblank before we can disable the plane. */
4481 intel_wait_for_vblank(dev, crtc->pipe);
4482}
4483
7cac945f 4484static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
d3eedb1a 4485{
7cac945f 4486 if (intel_crtc->overlay) {
d3eedb1a
VS
4487 struct drm_device *dev = intel_crtc->base.dev;
4488 struct drm_i915_private *dev_priv = dev->dev_private;
4489
4490 mutex_lock(&dev->struct_mutex);
4491 dev_priv->mm.interruptible = false;
4492 (void) intel_overlay_switch_off(intel_crtc->overlay);
4493 dev_priv->mm.interruptible = true;
4494 mutex_unlock(&dev->struct_mutex);
4495 }
4496
4497 /* Let userspace switch the overlay on again. In most cases userspace
4498 * has to recompute where to put it anyway.
4499 */
4500}
4501
87d4300a
ML
4502/**
4503 * intel_post_enable_primary - Perform operations after enabling primary plane
4504 * @crtc: the CRTC whose primary plane was just enabled
4505 *
4506 * Performs potentially sleeping operations that must be done after the primary
4507 * plane is enabled, such as updating FBC and IPS. Note that this may be
4508 * called due to an explicit primary plane update, or due to an implicit
4509 * re-enable that is caused when a sprite plane is updated to no longer
4510 * completely hide the primary plane.
4511 */
4512static void
4513intel_post_enable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4514{
4515 struct drm_device *dev = crtc->dev;
87d4300a 4516 struct drm_i915_private *dev_priv = dev->dev_private;
a5c4d7bc
VS
4517 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4518 int pipe = intel_crtc->pipe;
a5c4d7bc 4519
87d4300a
ML
4520 /*
4521 * FIXME IPS should be fine as long as one plane is
4522 * enabled, but in practice it seems to have problems
4523 * when going from primary only to sprite only and vice
4524 * versa.
4525 */
a5c4d7bc
VS
4526 hsw_enable_ips(intel_crtc);
4527
f99d7069 4528 /*
87d4300a
ML
4529 * Gen2 reports pipe underruns whenever all planes are disabled.
4530 * So don't enable underrun reporting before at least some planes
4531 * are enabled.
4532 * FIXME: Need to fix the logic to work when we turn off all planes
4533 * but leave the pipe running.
f99d7069 4534 */
87d4300a
ML
4535 if (IS_GEN2(dev))
4536 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4537
aca7b684
VS
4538 /* Underruns don't always raise interrupts, so check manually. */
4539 intel_check_cpu_fifo_underruns(dev_priv);
4540 intel_check_pch_fifo_underruns(dev_priv);
a5c4d7bc
VS
4541}
4542
2622a081 4543/* FIXME move all this to pre_plane_update() with proper state tracking */
87d4300a
ML
4544static void
4545intel_pre_disable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4546{
4547 struct drm_device *dev = crtc->dev;
4548 struct drm_i915_private *dev_priv = dev->dev_private;
4549 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4550 int pipe = intel_crtc->pipe;
a5c4d7bc 4551
87d4300a
ML
4552 /*
4553 * Gen2 reports pipe underruns whenever all planes are disabled.
4554 * So diasble underrun reporting before all the planes get disabled.
4555 * FIXME: Need to fix the logic to work when we turn off all planes
4556 * but leave the pipe running.
4557 */
4558 if (IS_GEN2(dev))
4559 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
a5c4d7bc 4560
2622a081
VS
4561 /*
4562 * FIXME IPS should be fine as long as one plane is
4563 * enabled, but in practice it seems to have problems
4564 * when going from primary only to sprite only and vice
4565 * versa.
4566 */
4567 hsw_disable_ips(intel_crtc);
4568}
4569
4570/* FIXME get rid of this and use pre_plane_update */
4571static void
4572intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
4573{
4574 struct drm_device *dev = crtc->dev;
4575 struct drm_i915_private *dev_priv = dev->dev_private;
4576 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4577 int pipe = intel_crtc->pipe;
4578
4579 intel_pre_disable_primary(crtc);
4580
87d4300a
ML
4581 /*
4582 * Vblank time updates from the shadow to live plane control register
4583 * are blocked if the memory self-refresh mode is active at that
4584 * moment. So to make sure the plane gets truly disabled, disable
4585 * first the self-refresh mode. The self-refresh enable bit in turn
4586 * will be checked/applied by the HW only at the next frame start
4587 * event which is after the vblank start event, so we need to have a
4588 * wait-for-vblank between disabling the plane and the pipe.
4589 */
262cd2e1 4590 if (HAS_GMCH_DISPLAY(dev)) {
87d4300a 4591 intel_set_memory_cxsr(dev_priv, false);
262cd2e1
VS
4592 dev_priv->wm.vlv.cxsr = false;
4593 intel_wait_for_vblank(dev, pipe);
4594 }
87d4300a
ML
4595}
4596
cd202f69 4597static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
ac21b225 4598{
cd202f69
ML
4599 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
4600 struct drm_atomic_state *old_state = old_crtc_state->base.state;
92826fcd
ML
4601 struct intel_crtc_state *pipe_config =
4602 to_intel_crtc_state(crtc->base.state);
ac21b225 4603 struct drm_device *dev = crtc->base.dev;
cd202f69
ML
4604 struct drm_plane *primary = crtc->base.primary;
4605 struct drm_plane_state *old_pri_state =
4606 drm_atomic_get_existing_plane_state(old_state, primary);
ac21b225 4607
cd202f69 4608 intel_frontbuffer_flip(dev, pipe_config->fb_bits);
ac21b225 4609
ab1d3a0e 4610 crtc->wm.cxsr_allowed = true;
852eb00d 4611
caed361d 4612 if (pipe_config->update_wm_post && pipe_config->base.active)
f015c551
VS
4613 intel_update_watermarks(&crtc->base);
4614
cd202f69
ML
4615 if (old_pri_state) {
4616 struct intel_plane_state *primary_state =
4617 to_intel_plane_state(primary->state);
4618 struct intel_plane_state *old_primary_state =
4619 to_intel_plane_state(old_pri_state);
4620
31ae71fc
ML
4621 intel_fbc_post_update(crtc);
4622
cd202f69
ML
4623 if (primary_state->visible &&
4624 (needs_modeset(&pipe_config->base) ||
4625 !old_primary_state->visible))
4626 intel_post_enable_primary(&crtc->base);
4627 }
ac21b225
ML
4628}
4629
5c74cd73 4630static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
ac21b225 4631{
5c74cd73 4632 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
ac21b225 4633 struct drm_device *dev = crtc->base.dev;
eddfcbcd 4634 struct drm_i915_private *dev_priv = dev->dev_private;
ab1d3a0e
ML
4635 struct intel_crtc_state *pipe_config =
4636 to_intel_crtc_state(crtc->base.state);
5c74cd73
ML
4637 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4638 struct drm_plane *primary = crtc->base.primary;
4639 struct drm_plane_state *old_pri_state =
4640 drm_atomic_get_existing_plane_state(old_state, primary);
4641 bool modeset = needs_modeset(&pipe_config->base);
ac21b225 4642
5c74cd73
ML
4643 if (old_pri_state) {
4644 struct intel_plane_state *primary_state =
4645 to_intel_plane_state(primary->state);
4646 struct intel_plane_state *old_primary_state =
4647 to_intel_plane_state(old_pri_state);
4648
31ae71fc
ML
4649 intel_fbc_pre_update(crtc);
4650
5c74cd73
ML
4651 if (old_primary_state->visible &&
4652 (modeset || !primary_state->visible))
4653 intel_pre_disable_primary(&crtc->base);
4654 }
852eb00d 4655
ab1d3a0e 4656 if (pipe_config->disable_cxsr) {
852eb00d 4657 crtc->wm.cxsr_allowed = false;
2dfd178d 4658
2622a081
VS
4659 /*
4660 * Vblank time updates from the shadow to live plane control register
4661 * are blocked if the memory self-refresh mode is active at that
4662 * moment. So to make sure the plane gets truly disabled, disable
4663 * first the self-refresh mode. The self-refresh enable bit in turn
4664 * will be checked/applied by the HW only at the next frame start
4665 * event which is after the vblank start event, so we need to have a
4666 * wait-for-vblank between disabling the plane and the pipe.
4667 */
4668 if (old_crtc_state->base.active) {
2dfd178d 4669 intel_set_memory_cxsr(dev_priv, false);
2622a081
VS
4670 dev_priv->wm.vlv.cxsr = false;
4671 intel_wait_for_vblank(dev, crtc->pipe);
4672 }
852eb00d 4673 }
92826fcd 4674
ed4a6a7c
MR
4675 /*
4676 * IVB workaround: must disable low power watermarks for at least
4677 * one frame before enabling scaling. LP watermarks can be re-enabled
4678 * when scaling is disabled.
4679 *
4680 * WaCxSRDisabledForSpriteScaling:ivb
4681 */
4682 if (pipe_config->disable_lp_wm) {
4683 ilk_disable_lp_wm(dev);
4684 intel_wait_for_vblank(dev, crtc->pipe);
4685 }
4686
4687 /*
4688 * If we're doing a modeset, we're done. No need to do any pre-vblank
4689 * watermark programming here.
4690 */
4691 if (needs_modeset(&pipe_config->base))
4692 return;
4693
4694 /*
4695 * For platforms that support atomic watermarks, program the
4696 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
4697 * will be the intermediate values that are safe for both pre- and
4698 * post- vblank; when vblank happens, the 'active' values will be set
4699 * to the final 'target' values and we'll do this again to get the
4700 * optimal watermarks. For gen9+ platforms, the values we program here
4701 * will be the final target values which will get automatically latched
4702 * at vblank time; no further programming will be necessary.
4703 *
4704 * If a platform hasn't been transitioned to atomic watermarks yet,
4705 * we'll continue to update watermarks the old way, if flags tell
4706 * us to.
4707 */
4708 if (dev_priv->display.initial_watermarks != NULL)
4709 dev_priv->display.initial_watermarks(pipe_config);
caed361d 4710 else if (pipe_config->update_wm_pre)
92826fcd 4711 intel_update_watermarks(&crtc->base);
ac21b225
ML
4712}
4713
d032ffa0 4714static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
87d4300a
ML
4715{
4716 struct drm_device *dev = crtc->dev;
4717 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
d032ffa0 4718 struct drm_plane *p;
87d4300a
ML
4719 int pipe = intel_crtc->pipe;
4720
7cac945f 4721 intel_crtc_dpms_overlay_disable(intel_crtc);
27321ae8 4722
d032ffa0
ML
4723 drm_for_each_plane_mask(p, dev, plane_mask)
4724 to_intel_plane(p)->disable_plane(p, crtc);
f98551ae 4725
f99d7069
DV
4726 /*
4727 * FIXME: Once we grow proper nuclear flip support out of this we need
4728 * to compute the mask of flip planes precisely. For the time being
4729 * consider this a flip to a NULL plane.
4730 */
4731 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
4732}
4733
f67a559d
JB
4734static void ironlake_crtc_enable(struct drm_crtc *crtc)
4735{
4736 struct drm_device *dev = crtc->dev;
4737 struct drm_i915_private *dev_priv = dev->dev_private;
4738 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4739 struct intel_encoder *encoder;
f67a559d 4740 int pipe = intel_crtc->pipe;
f67a559d 4741
53d9f4e9 4742 if (WARN_ON(intel_crtc->active))
f67a559d
JB
4743 return;
4744
81b088ca
VS
4745 if (intel_crtc->config->has_pch_encoder)
4746 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4747
6e3c9717 4748 if (intel_crtc->config->has_pch_encoder)
b14b1055
DV
4749 intel_prepare_shared_dpll(intel_crtc);
4750
6e3c9717 4751 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 4752 intel_dp_set_m_n(intel_crtc, M1_N1);
29407aab
DV
4753
4754 intel_set_pipe_timings(intel_crtc);
bc58be60 4755 intel_set_pipe_src_size(intel_crtc);
29407aab 4756
6e3c9717 4757 if (intel_crtc->config->has_pch_encoder) {
29407aab 4758 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4759 &intel_crtc->config->fdi_m_n, NULL);
29407aab
DV
4760 }
4761
4762 ironlake_set_pipeconf(crtc);
4763
f67a559d 4764 intel_crtc->active = true;
8664281b 4765
a72e4c9f 4766 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
8664281b 4767
f6736a1a 4768 for_each_encoder_on_crtc(dev, crtc, encoder)
952735ee
DV
4769 if (encoder->pre_enable)
4770 encoder->pre_enable(encoder);
f67a559d 4771
6e3c9717 4772 if (intel_crtc->config->has_pch_encoder) {
fff367c7
DV
4773 /* Note: FDI PLL enabling _must_ be done before we enable the
4774 * cpu pipes, hence this is separate from all the other fdi/pch
4775 * enabling. */
88cefb6c 4776 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
DV
4777 } else {
4778 assert_fdi_tx_disabled(dev_priv, pipe);
4779 assert_fdi_rx_disabled(dev_priv, pipe);
4780 }
f67a559d 4781
b074cec8 4782 ironlake_pfit_enable(intel_crtc);
f67a559d 4783
9c54c0dd
JB
4784 /*
4785 * On ILK+ LUT must be loaded before the pipe is running but with
4786 * clocks enabled
4787 */
8563b1e8 4788 intel_color_load_luts(crtc);
9c54c0dd 4789
1d5bf5d9
ID
4790 if (dev_priv->display.initial_watermarks != NULL)
4791 dev_priv->display.initial_watermarks(intel_crtc->config);
e1fdc473 4792 intel_enable_pipe(intel_crtc);
f67a559d 4793
6e3c9717 4794 if (intel_crtc->config->has_pch_encoder)
f67a559d 4795 ironlake_pch_enable(crtc);
c98e9dcf 4796
f9b61ff6
DV
4797 assert_vblank_disabled(crtc);
4798 drm_crtc_vblank_on(crtc);
4799
fa5c73b1
DV
4800 for_each_encoder_on_crtc(dev, crtc, encoder)
4801 encoder->enable(encoder);
61b77ddd
DV
4802
4803 if (HAS_PCH_CPT(dev))
a1520318 4804 cpt_verify_modeset(dev, intel_crtc->pipe);
37ca8d4c
VS
4805
4806 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
4807 if (intel_crtc->config->has_pch_encoder)
4808 intel_wait_for_vblank(dev, pipe);
4809 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607
JB
4810}
4811
42db64ef
PZ
4812/* IPS only exists on ULT machines and is tied to pipe A. */
4813static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4814{
f5adf94e 4815 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
42db64ef
PZ
4816}
4817
4f771f10
PZ
4818static void haswell_crtc_enable(struct drm_crtc *crtc)
4819{
4820 struct drm_device *dev = crtc->dev;
4821 struct drm_i915_private *dev_priv = dev->dev_private;
4822 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4823 struct intel_encoder *encoder;
99d736a2 4824 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
4d1de975 4825 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
99d736a2
ML
4826 struct intel_crtc_state *pipe_config =
4827 to_intel_crtc_state(crtc->state);
4f771f10 4828
53d9f4e9 4829 if (WARN_ON(intel_crtc->active))
4f771f10
PZ
4830 return;
4831
81b088ca
VS
4832 if (intel_crtc->config->has_pch_encoder)
4833 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4834 false);
4835
8106ddbd 4836 if (intel_crtc->config->shared_dpll)
df8ad70c
DV
4837 intel_enable_shared_dpll(intel_crtc);
4838
6e3c9717 4839 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 4840 intel_dp_set_m_n(intel_crtc, M1_N1);
229fca97 4841
4d1de975
JN
4842 if (!intel_crtc->config->has_dsi_encoder)
4843 intel_set_pipe_timings(intel_crtc);
4844
bc58be60 4845 intel_set_pipe_src_size(intel_crtc);
229fca97 4846
4d1de975
JN
4847 if (cpu_transcoder != TRANSCODER_EDP &&
4848 !transcoder_is_dsi(cpu_transcoder)) {
4849 I915_WRITE(PIPE_MULT(cpu_transcoder),
6e3c9717 4850 intel_crtc->config->pixel_multiplier - 1);
ebb69c95
CT
4851 }
4852
6e3c9717 4853 if (intel_crtc->config->has_pch_encoder) {
229fca97 4854 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4855 &intel_crtc->config->fdi_m_n, NULL);
229fca97
DV
4856 }
4857
4d1de975
JN
4858 if (!intel_crtc->config->has_dsi_encoder)
4859 haswell_set_pipeconf(crtc);
4860
391bf048 4861 haswell_set_pipemisc(crtc);
229fca97 4862
8563b1e8 4863 intel_color_set_csc(crtc);
229fca97 4864
4f771f10 4865 intel_crtc->active = true;
8664281b 4866
6b698516
DV
4867 if (intel_crtc->config->has_pch_encoder)
4868 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4869 else
4870 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4871
7d4aefd0 4872 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10
PZ
4873 if (encoder->pre_enable)
4874 encoder->pre_enable(encoder);
7d4aefd0 4875 }
4f771f10 4876
d2d65408 4877 if (intel_crtc->config->has_pch_encoder)
4fe9467d 4878 dev_priv->display.fdi_link_train(crtc);
4fe9467d 4879
a65347ba 4880 if (!intel_crtc->config->has_dsi_encoder)
7d4aefd0 4881 intel_ddi_enable_pipe_clock(intel_crtc);
4f771f10 4882
1c132b44 4883 if (INTEL_INFO(dev)->gen >= 9)
e435d6e5 4884 skylake_pfit_enable(intel_crtc);
ff6d9f55 4885 else
1c132b44 4886 ironlake_pfit_enable(intel_crtc);
4f771f10
PZ
4887
4888 /*
4889 * On ILK+ LUT must be loaded before the pipe is running but with
4890 * clocks enabled
4891 */
8563b1e8 4892 intel_color_load_luts(crtc);
4f771f10 4893
1f544388 4894 intel_ddi_set_pipe_settings(crtc);
a65347ba 4895 if (!intel_crtc->config->has_dsi_encoder)
7d4aefd0 4896 intel_ddi_enable_transcoder_func(crtc);
4f771f10 4897
1d5bf5d9
ID
4898 if (dev_priv->display.initial_watermarks != NULL)
4899 dev_priv->display.initial_watermarks(pipe_config);
4900 else
4901 intel_update_watermarks(crtc);
4d1de975
JN
4902
4903 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
4904 if (!intel_crtc->config->has_dsi_encoder)
4905 intel_enable_pipe(intel_crtc);
42db64ef 4906
6e3c9717 4907 if (intel_crtc->config->has_pch_encoder)
1507e5bd 4908 lpt_pch_enable(crtc);
4f771f10 4909
a65347ba 4910 if (intel_crtc->config->dp_encoder_is_mst)
0e32b39c
DA
4911 intel_ddi_set_vc_payload_alloc(crtc, true);
4912
f9b61ff6
DV
4913 assert_vblank_disabled(crtc);
4914 drm_crtc_vblank_on(crtc);
4915
8807e55b 4916 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10 4917 encoder->enable(encoder);
8807e55b
JN
4918 intel_opregion_notify_encoder(encoder, true);
4919 }
4f771f10 4920
6b698516
DV
4921 if (intel_crtc->config->has_pch_encoder) {
4922 intel_wait_for_vblank(dev, pipe);
4923 intel_wait_for_vblank(dev, pipe);
4924 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
d2d65408
VS
4925 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4926 true);
6b698516 4927 }
d2d65408 4928
e4916946
PZ
4929 /* If we change the relative order between pipe/planes enabling, we need
4930 * to change the workaround. */
99d736a2
ML
4931 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
4932 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
4933 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4934 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4935 }
4f771f10
PZ
4936}
4937
bfd16b2a 4938static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
3f8dce3a
DV
4939{
4940 struct drm_device *dev = crtc->base.dev;
4941 struct drm_i915_private *dev_priv = dev->dev_private;
4942 int pipe = crtc->pipe;
4943
4944 /* To avoid upsetting the power well on haswell only disable the pfit if
4945 * it's in use. The hw state code will make sure we get this right. */
bfd16b2a 4946 if (force || crtc->config->pch_pfit.enabled) {
3f8dce3a
DV
4947 I915_WRITE(PF_CTL(pipe), 0);
4948 I915_WRITE(PF_WIN_POS(pipe), 0);
4949 I915_WRITE(PF_WIN_SZ(pipe), 0);
4950 }
4951}
4952
6be4a607
JB
4953static void ironlake_crtc_disable(struct drm_crtc *crtc)
4954{
4955 struct drm_device *dev = crtc->dev;
4956 struct drm_i915_private *dev_priv = dev->dev_private;
4957 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4958 struct intel_encoder *encoder;
6be4a607 4959 int pipe = intel_crtc->pipe;
b52eb4dc 4960
37ca8d4c
VS
4961 if (intel_crtc->config->has_pch_encoder)
4962 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4963
ea9d758d
DV
4964 for_each_encoder_on_crtc(dev, crtc, encoder)
4965 encoder->disable(encoder);
4966
f9b61ff6
DV
4967 drm_crtc_vblank_off(crtc);
4968 assert_vblank_disabled(crtc);
4969
3860b2ec
VS
4970 /*
4971 * Sometimes spurious CPU pipe underruns happen when the
4972 * pipe is already disabled, but FDI RX/TX is still enabled.
4973 * Happens at least with VGA+HDMI cloning. Suppress them.
4974 */
4975 if (intel_crtc->config->has_pch_encoder)
4976 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4977
575f7ab7 4978 intel_disable_pipe(intel_crtc);
32f9d658 4979
bfd16b2a 4980 ironlake_pfit_disable(intel_crtc, false);
2c07245f 4981
3860b2ec 4982 if (intel_crtc->config->has_pch_encoder) {
5a74f70a 4983 ironlake_fdi_disable(crtc);
3860b2ec
VS
4984 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4985 }
5a74f70a 4986
bf49ec8c
DV
4987 for_each_encoder_on_crtc(dev, crtc, encoder)
4988 if (encoder->post_disable)
4989 encoder->post_disable(encoder);
2c07245f 4990
6e3c9717 4991 if (intel_crtc->config->has_pch_encoder) {
d925c59a 4992 ironlake_disable_pch_transcoder(dev_priv, pipe);
6be4a607 4993
d925c59a 4994 if (HAS_PCH_CPT(dev)) {
f0f59a00
VS
4995 i915_reg_t reg;
4996 u32 temp;
4997
d925c59a
DV
4998 /* disable TRANS_DP_CTL */
4999 reg = TRANS_DP_CTL(pipe);
5000 temp = I915_READ(reg);
5001 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5002 TRANS_DP_PORT_SEL_MASK);
5003 temp |= TRANS_DP_PORT_SEL_NONE;
5004 I915_WRITE(reg, temp);
5005
5006 /* disable DPLL_SEL */
5007 temp = I915_READ(PCH_DPLL_SEL);
11887397 5008 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 5009 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 5010 }
e3421a18 5011
d925c59a
DV
5012 ironlake_fdi_pll_disable(intel_crtc);
5013 }
81b088ca
VS
5014
5015 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607 5016}
1b3c7a47 5017
4f771f10 5018static void haswell_crtc_disable(struct drm_crtc *crtc)
ee7b9f93 5019{
4f771f10
PZ
5020 struct drm_device *dev = crtc->dev;
5021 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93 5022 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4f771f10 5023 struct intel_encoder *encoder;
6e3c9717 5024 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee7b9f93 5025
d2d65408
VS
5026 if (intel_crtc->config->has_pch_encoder)
5027 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5028 false);
5029
8807e55b
JN
5030 for_each_encoder_on_crtc(dev, crtc, encoder) {
5031 intel_opregion_notify_encoder(encoder, false);
4f771f10 5032 encoder->disable(encoder);
8807e55b 5033 }
4f771f10 5034
f9b61ff6
DV
5035 drm_crtc_vblank_off(crtc);
5036 assert_vblank_disabled(crtc);
5037
4d1de975
JN
5038 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
5039 if (!intel_crtc->config->has_dsi_encoder)
5040 intel_disable_pipe(intel_crtc);
4f771f10 5041
6e3c9717 5042 if (intel_crtc->config->dp_encoder_is_mst)
a4bf214f
VS
5043 intel_ddi_set_vc_payload_alloc(crtc, false);
5044
a65347ba 5045 if (!intel_crtc->config->has_dsi_encoder)
7d4aefd0 5046 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4f771f10 5047
1c132b44 5048 if (INTEL_INFO(dev)->gen >= 9)
e435d6e5 5049 skylake_scaler_disable(intel_crtc);
ff6d9f55 5050 else
bfd16b2a 5051 ironlake_pfit_disable(intel_crtc, false);
4f771f10 5052
a65347ba 5053 if (!intel_crtc->config->has_dsi_encoder)
7d4aefd0 5054 intel_ddi_disable_pipe_clock(intel_crtc);
4f771f10 5055
97b040aa
ID
5056 for_each_encoder_on_crtc(dev, crtc, encoder)
5057 if (encoder->post_disable)
5058 encoder->post_disable(encoder);
81b088ca 5059
92966a37
VS
5060 if (intel_crtc->config->has_pch_encoder) {
5061 lpt_disable_pch_transcoder(dev_priv);
503a74e9 5062 lpt_disable_iclkip(dev_priv);
92966a37
VS
5063 intel_ddi_fdi_disable(crtc);
5064
81b088ca
VS
5065 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5066 true);
92966a37 5067 }
4f771f10
PZ
5068}
5069
2dd24552
JB
5070static void i9xx_pfit_enable(struct intel_crtc *crtc)
5071{
5072 struct drm_device *dev = crtc->base.dev;
5073 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 5074 struct intel_crtc_state *pipe_config = crtc->config;
2dd24552 5075
681a8504 5076 if (!pipe_config->gmch_pfit.control)
2dd24552
JB
5077 return;
5078
2dd24552 5079 /*
c0b03411
DV
5080 * The panel fitter should only be adjusted whilst the pipe is disabled,
5081 * according to register description and PRM.
2dd24552 5082 */
c0b03411
DV
5083 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5084 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 5085
b074cec8
JB
5086 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5087 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
DV
5088
5089 /* Border color in case we don't scale up to the full screen. Black by
5090 * default, change to something else for debugging. */
5091 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
5092}
5093
d05410f9
DA
5094static enum intel_display_power_domain port_to_power_domain(enum port port)
5095{
5096 switch (port) {
5097 case PORT_A:
6331a704 5098 return POWER_DOMAIN_PORT_DDI_A_LANES;
d05410f9 5099 case PORT_B:
6331a704 5100 return POWER_DOMAIN_PORT_DDI_B_LANES;
d05410f9 5101 case PORT_C:
6331a704 5102 return POWER_DOMAIN_PORT_DDI_C_LANES;
d05410f9 5103 case PORT_D:
6331a704 5104 return POWER_DOMAIN_PORT_DDI_D_LANES;
d8e19f99 5105 case PORT_E:
6331a704 5106 return POWER_DOMAIN_PORT_DDI_E_LANES;
d05410f9 5107 default:
b9fec167 5108 MISSING_CASE(port);
d05410f9
DA
5109 return POWER_DOMAIN_PORT_OTHER;
5110 }
5111}
5112
25f78f58
VS
5113static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5114{
5115 switch (port) {
5116 case PORT_A:
5117 return POWER_DOMAIN_AUX_A;
5118 case PORT_B:
5119 return POWER_DOMAIN_AUX_B;
5120 case PORT_C:
5121 return POWER_DOMAIN_AUX_C;
5122 case PORT_D:
5123 return POWER_DOMAIN_AUX_D;
5124 case PORT_E:
5125 /* FIXME: Check VBT for actual wiring of PORT E */
5126 return POWER_DOMAIN_AUX_D;
5127 default:
b9fec167 5128 MISSING_CASE(port);
25f78f58
VS
5129 return POWER_DOMAIN_AUX_A;
5130 }
5131}
5132
319be8ae
ID
5133enum intel_display_power_domain
5134intel_display_port_power_domain(struct intel_encoder *intel_encoder)
5135{
5136 struct drm_device *dev = intel_encoder->base.dev;
5137 struct intel_digital_port *intel_dig_port;
5138
5139 switch (intel_encoder->type) {
5140 case INTEL_OUTPUT_UNKNOWN:
5141 /* Only DDI platforms should ever use this output type */
5142 WARN_ON_ONCE(!HAS_DDI(dev));
5143 case INTEL_OUTPUT_DISPLAYPORT:
5144 case INTEL_OUTPUT_HDMI:
5145 case INTEL_OUTPUT_EDP:
5146 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
d05410f9 5147 return port_to_power_domain(intel_dig_port->port);
0e32b39c
DA
5148 case INTEL_OUTPUT_DP_MST:
5149 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5150 return port_to_power_domain(intel_dig_port->port);
319be8ae
ID
5151 case INTEL_OUTPUT_ANALOG:
5152 return POWER_DOMAIN_PORT_CRT;
5153 case INTEL_OUTPUT_DSI:
5154 return POWER_DOMAIN_PORT_DSI;
5155 default:
5156 return POWER_DOMAIN_PORT_OTHER;
5157 }
5158}
5159
25f78f58
VS
5160enum intel_display_power_domain
5161intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5162{
5163 struct drm_device *dev = intel_encoder->base.dev;
5164 struct intel_digital_port *intel_dig_port;
5165
5166 switch (intel_encoder->type) {
5167 case INTEL_OUTPUT_UNKNOWN:
651174a4
ID
5168 case INTEL_OUTPUT_HDMI:
5169 /*
5170 * Only DDI platforms should ever use these output types.
5171 * We can get here after the HDMI detect code has already set
5172 * the type of the shared encoder. Since we can't be sure
5173 * what's the status of the given connectors, play safe and
5174 * run the DP detection too.
5175 */
25f78f58
VS
5176 WARN_ON_ONCE(!HAS_DDI(dev));
5177 case INTEL_OUTPUT_DISPLAYPORT:
5178 case INTEL_OUTPUT_EDP:
5179 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5180 return port_to_aux_power_domain(intel_dig_port->port);
5181 case INTEL_OUTPUT_DP_MST:
5182 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5183 return port_to_aux_power_domain(intel_dig_port->port);
5184 default:
b9fec167 5185 MISSING_CASE(intel_encoder->type);
25f78f58
VS
5186 return POWER_DOMAIN_AUX_A;
5187 }
5188}
5189
74bff5f9
ML
5190static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
5191 struct intel_crtc_state *crtc_state)
77d22dca 5192{
319be8ae 5193 struct drm_device *dev = crtc->dev;
74bff5f9 5194 struct drm_encoder *encoder;
319be8ae
ID
5195 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5196 enum pipe pipe = intel_crtc->pipe;
77d22dca 5197 unsigned long mask;
74bff5f9 5198 enum transcoder transcoder = crtc_state->cpu_transcoder;
77d22dca 5199
74bff5f9 5200 if (!crtc_state->base.active)
292b990e
ML
5201 return 0;
5202
77d22dca
ID
5203 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5204 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
74bff5f9
ML
5205 if (crtc_state->pch_pfit.enabled ||
5206 crtc_state->pch_pfit.force_thru)
77d22dca
ID
5207 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5208
74bff5f9
ML
5209 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5210 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5211
319be8ae 5212 mask |= BIT(intel_display_port_power_domain(intel_encoder));
74bff5f9 5213 }
319be8ae 5214
15e7ec29
ML
5215 if (crtc_state->shared_dpll)
5216 mask |= BIT(POWER_DOMAIN_PLLS);
5217
77d22dca
ID
5218 return mask;
5219}
5220
74bff5f9
ML
5221static unsigned long
5222modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5223 struct intel_crtc_state *crtc_state)
77d22dca 5224{
292b990e
ML
5225 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5226 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5227 enum intel_display_power_domain domain;
5228 unsigned long domains, new_domains, old_domains;
77d22dca 5229
292b990e 5230 old_domains = intel_crtc->enabled_power_domains;
74bff5f9
ML
5231 intel_crtc->enabled_power_domains = new_domains =
5232 get_crtc_power_domains(crtc, crtc_state);
77d22dca 5233
292b990e
ML
5234 domains = new_domains & ~old_domains;
5235
5236 for_each_power_domain(domain, domains)
5237 intel_display_power_get(dev_priv, domain);
5238
5239 return old_domains & ~new_domains;
5240}
5241
5242static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5243 unsigned long domains)
5244{
5245 enum intel_display_power_domain domain;
5246
5247 for_each_power_domain(domain, domains)
5248 intel_display_power_put(dev_priv, domain);
5249}
77d22dca 5250
adafdc6f
MK
5251static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5252{
5253 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5254
5255 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5256 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5257 return max_cdclk_freq;
5258 else if (IS_CHERRYVIEW(dev_priv))
5259 return max_cdclk_freq*95/100;
5260 else if (INTEL_INFO(dev_priv)->gen < 4)
5261 return 2*max_cdclk_freq*90/100;
5262 else
5263 return max_cdclk_freq*90/100;
5264}
5265
560a7ae4
DL
5266static void intel_update_max_cdclk(struct drm_device *dev)
5267{
5268 struct drm_i915_private *dev_priv = dev->dev_private;
5269
ef11bdb3 5270 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
560a7ae4
DL
5271 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
5272
5273 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
5274 dev_priv->max_cdclk_freq = 675000;
5275 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
5276 dev_priv->max_cdclk_freq = 540000;
5277 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
5278 dev_priv->max_cdclk_freq = 450000;
5279 else
5280 dev_priv->max_cdclk_freq = 337500;
5281 } else if (IS_BROADWELL(dev)) {
5282 /*
5283 * FIXME with extra cooling we can allow
5284 * 540 MHz for ULX and 675 Mhz for ULT.
5285 * How can we know if extra cooling is
5286 * available? PCI ID, VTB, something else?
5287 */
5288 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5289 dev_priv->max_cdclk_freq = 450000;
5290 else if (IS_BDW_ULX(dev))
5291 dev_priv->max_cdclk_freq = 450000;
5292 else if (IS_BDW_ULT(dev))
5293 dev_priv->max_cdclk_freq = 540000;
5294 else
5295 dev_priv->max_cdclk_freq = 675000;
0904deaf
MK
5296 } else if (IS_CHERRYVIEW(dev)) {
5297 dev_priv->max_cdclk_freq = 320000;
560a7ae4
DL
5298 } else if (IS_VALLEYVIEW(dev)) {
5299 dev_priv->max_cdclk_freq = 400000;
5300 } else {
5301 /* otherwise assume cdclk is fixed */
5302 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5303 }
5304
adafdc6f
MK
5305 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5306
560a7ae4
DL
5307 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5308 dev_priv->max_cdclk_freq);
adafdc6f
MK
5309
5310 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5311 dev_priv->max_dotclk_freq);
560a7ae4
DL
5312}
5313
5314static void intel_update_cdclk(struct drm_device *dev)
5315{
5316 struct drm_i915_private *dev_priv = dev->dev_private;
5317
5318 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
5319 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5320 dev_priv->cdclk_freq);
5321
5322 /*
5323 * Program the gmbus_freq based on the cdclk frequency.
5324 * BSpec erroneously claims we should aim for 4MHz, but
5325 * in fact 1MHz is the correct frequency.
5326 */
666a4537 5327 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
560a7ae4
DL
5328 /*
5329 * Program the gmbus_freq based on the cdclk frequency.
5330 * BSpec erroneously claims we should aim for 4MHz, but
5331 * in fact 1MHz is the correct frequency.
5332 */
5333 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
5334 }
5335
5336 if (dev_priv->max_cdclk_freq == 0)
5337 intel_update_max_cdclk(dev);
5338}
5339
70d0c574 5340static void broxton_set_cdclk(struct drm_device *dev, int frequency)
f8437dd1
VK
5341{
5342 struct drm_i915_private *dev_priv = dev->dev_private;
5343 uint32_t divider;
5344 uint32_t ratio;
5345 uint32_t current_freq;
5346 int ret;
5347
5348 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5349 switch (frequency) {
5350 case 144000:
5351 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5352 ratio = BXT_DE_PLL_RATIO(60);
5353 break;
5354 case 288000:
5355 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5356 ratio = BXT_DE_PLL_RATIO(60);
5357 break;
5358 case 384000:
5359 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5360 ratio = BXT_DE_PLL_RATIO(60);
5361 break;
5362 case 576000:
5363 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5364 ratio = BXT_DE_PLL_RATIO(60);
5365 break;
5366 case 624000:
5367 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5368 ratio = BXT_DE_PLL_RATIO(65);
5369 break;
5370 case 19200:
5371 /*
5372 * Bypass frequency with DE PLL disabled. Init ratio, divider
5373 * to suppress GCC warning.
5374 */
5375 ratio = 0;
5376 divider = 0;
5377 break;
5378 default:
5379 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5380
5381 return;
5382 }
5383
5384 mutex_lock(&dev_priv->rps.hw_lock);
5385 /* Inform power controller of upcoming frequency change */
5386 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5387 0x80000000);
5388 mutex_unlock(&dev_priv->rps.hw_lock);
5389
5390 if (ret) {
5391 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5392 ret, frequency);
5393 return;
5394 }
5395
5396 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5397 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5398 current_freq = current_freq * 500 + 1000;
5399
5400 /*
5401 * DE PLL has to be disabled when
5402 * - setting to 19.2MHz (bypass, PLL isn't used)
5403 * - before setting to 624MHz (PLL needs toggling)
5404 * - before setting to any frequency from 624MHz (PLL needs toggling)
5405 */
5406 if (frequency == 19200 || frequency == 624000 ||
5407 current_freq == 624000) {
5408 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5409 /* Timeout 200us */
5410 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5411 1))
5412 DRM_ERROR("timout waiting for DE PLL unlock\n");
5413 }
5414
5415 if (frequency != 19200) {
5416 uint32_t val;
5417
5418 val = I915_READ(BXT_DE_PLL_CTL);
5419 val &= ~BXT_DE_PLL_RATIO_MASK;
5420 val |= ratio;
5421 I915_WRITE(BXT_DE_PLL_CTL, val);
5422
5423 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5424 /* Timeout 200us */
5425 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5426 DRM_ERROR("timeout waiting for DE PLL lock\n");
5427
5428 val = I915_READ(CDCLK_CTL);
5429 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5430 val |= divider;
5431 /*
5432 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5433 * enable otherwise.
5434 */
5435 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5436 if (frequency >= 500000)
5437 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5438
5439 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5440 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5441 val |= (frequency - 1000) / 500;
5442 I915_WRITE(CDCLK_CTL, val);
5443 }
5444
5445 mutex_lock(&dev_priv->rps.hw_lock);
5446 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5447 DIV_ROUND_UP(frequency, 25000));
5448 mutex_unlock(&dev_priv->rps.hw_lock);
5449
5450 if (ret) {
5451 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5452 ret, frequency);
5453 return;
5454 }
5455
a47871bd 5456 intel_update_cdclk(dev);
f8437dd1
VK
5457}
5458
5459void broxton_init_cdclk(struct drm_device *dev)
5460{
5461 struct drm_i915_private *dev_priv = dev->dev_private;
5462 uint32_t val;
5463
5464 /*
5465 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5466 * or else the reset will hang because there is no PCH to respond.
5467 * Move the handshake programming to initialization sequence.
5468 * Previously was left up to BIOS.
5469 */
5470 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5471 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5472 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5473
5474 /* Enable PG1 for cdclk */
5475 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5476
5477 /* check if cd clock is enabled */
5478 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5479 DRM_DEBUG_KMS("Display already initialized\n");
5480 return;
5481 }
5482
5483 /*
5484 * FIXME:
5485 * - The initial CDCLK needs to be read from VBT.
5486 * Need to make this change after VBT has changes for BXT.
5487 * - check if setting the max (or any) cdclk freq is really necessary
5488 * here, it belongs to modeset time
5489 */
5490 broxton_set_cdclk(dev, 624000);
5491
5492 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
22e02c0b
VS
5493 POSTING_READ(DBUF_CTL);
5494
f8437dd1
VK
5495 udelay(10);
5496
5497 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5498 DRM_ERROR("DBuf power enable timeout!\n");
5499}
5500
5501void broxton_uninit_cdclk(struct drm_device *dev)
5502{
5503 struct drm_i915_private *dev_priv = dev->dev_private;
5504
5505 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
22e02c0b
VS
5506 POSTING_READ(DBUF_CTL);
5507
f8437dd1
VK
5508 udelay(10);
5509
5510 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5511 DRM_ERROR("DBuf power disable timeout!\n");
5512
5513 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5514 broxton_set_cdclk(dev, 19200);
5515
5516 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5517}
5518
5d96d8af
DL
5519static const struct skl_cdclk_entry {
5520 unsigned int freq;
5521 unsigned int vco;
5522} skl_cdclk_frequencies[] = {
5523 { .freq = 308570, .vco = 8640 },
5524 { .freq = 337500, .vco = 8100 },
5525 { .freq = 432000, .vco = 8640 },
5526 { .freq = 450000, .vco = 8100 },
5527 { .freq = 540000, .vco = 8100 },
5528 { .freq = 617140, .vco = 8640 },
5529 { .freq = 675000, .vco = 8100 },
5530};
5531
5532static unsigned int skl_cdclk_decimal(unsigned int freq)
5533{
5534 return (freq - 1000) / 500;
5535}
5536
5537static unsigned int skl_cdclk_get_vco(unsigned int freq)
5538{
5539 unsigned int i;
5540
5541 for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
5542 const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
5543
5544 if (e->freq == freq)
5545 return e->vco;
5546 }
5547
5548 return 8100;
5549}
5550
5551static void
5552skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
5553{
5554 unsigned int min_freq;
5555 u32 val;
5556
5557 /* select the minimum CDCLK before enabling DPLL 0 */
5558 val = I915_READ(CDCLK_CTL);
5559 val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
5560 val |= CDCLK_FREQ_337_308;
5561
5562 if (required_vco == 8640)
5563 min_freq = 308570;
5564 else
5565 min_freq = 337500;
5566
5567 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
5568
5569 I915_WRITE(CDCLK_CTL, val);
5570 POSTING_READ(CDCLK_CTL);
5571
5572 /*
5573 * We always enable DPLL0 with the lowest link rate possible, but still
5574 * taking into account the VCO required to operate the eDP panel at the
5575 * desired frequency. The usual DP link rates operate with a VCO of
5576 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5577 * The modeset code is responsible for the selection of the exact link
5578 * rate later on, with the constraint of choosing a frequency that
5579 * works with required_vco.
5580 */
5581 val = I915_READ(DPLL_CTRL1);
5582
5583 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5584 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5585 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
5586 if (required_vco == 8640)
5587 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5588 SKL_DPLL0);
5589 else
5590 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5591 SKL_DPLL0);
5592
5593 I915_WRITE(DPLL_CTRL1, val);
5594 POSTING_READ(DPLL_CTRL1);
5595
5596 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5597
5598 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5599 DRM_ERROR("DPLL0 not locked\n");
5600}
5601
5602static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5603{
5604 int ret;
5605 u32 val;
5606
5607 /* inform PCU we want to change CDCLK */
5608 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5609 mutex_lock(&dev_priv->rps.hw_lock);
5610 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5611 mutex_unlock(&dev_priv->rps.hw_lock);
5612
5613 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5614}
5615
5616static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5617{
5618 unsigned int i;
5619
5620 for (i = 0; i < 15; i++) {
5621 if (skl_cdclk_pcu_ready(dev_priv))
5622 return true;
5623 udelay(10);
5624 }
5625
5626 return false;
5627}
5628
5629static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
5630{
560a7ae4 5631 struct drm_device *dev = dev_priv->dev;
5d96d8af
DL
5632 u32 freq_select, pcu_ack;
5633
5634 DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
5635
5636 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5637 DRM_ERROR("failed to inform PCU about cdclk change\n");
5638 return;
5639 }
5640
5641 /* set CDCLK_CTL */
5642 switch(freq) {
5643 case 450000:
5644 case 432000:
5645 freq_select = CDCLK_FREQ_450_432;
5646 pcu_ack = 1;
5647 break;
5648 case 540000:
5649 freq_select = CDCLK_FREQ_540;
5650 pcu_ack = 2;
5651 break;
5652 case 308570:
5653 case 337500:
5654 default:
5655 freq_select = CDCLK_FREQ_337_308;
5656 pcu_ack = 0;
5657 break;
5658 case 617140:
5659 case 675000:
5660 freq_select = CDCLK_FREQ_675_617;
5661 pcu_ack = 3;
5662 break;
5663 }
5664
5665 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
5666 POSTING_READ(CDCLK_CTL);
5667
5668 /* inform PCU of the change */
5669 mutex_lock(&dev_priv->rps.hw_lock);
5670 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5671 mutex_unlock(&dev_priv->rps.hw_lock);
560a7ae4
DL
5672
5673 intel_update_cdclk(dev);
5d96d8af
DL
5674}
5675
5676void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5677{
5678 /* disable DBUF power */
5679 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5680 POSTING_READ(DBUF_CTL);
5681
5682 udelay(10);
5683
5684 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5685 DRM_ERROR("DBuf power disable timeout\n");
5686
ab96c1ee
ID
5687 /* disable DPLL0 */
5688 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5689 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5690 DRM_ERROR("Couldn't disable DPLL0\n");
5d96d8af
DL
5691}
5692
5693void skl_init_cdclk(struct drm_i915_private *dev_priv)
5694{
5d96d8af
DL
5695 unsigned int required_vco;
5696
39d9b85a
GW
5697 /* DPLL0 not enabled (happens on early BIOS versions) */
5698 if (!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE)) {
5699 /* enable DPLL0 */
5700 required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
5701 skl_dpll0_enable(dev_priv, required_vco);
5d96d8af
DL
5702 }
5703
5d96d8af
DL
5704 /* set CDCLK to the frequency the BIOS chose */
5705 skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
5706
5707 /* enable DBUF power */
5708 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5709 POSTING_READ(DBUF_CTL);
5710
5711 udelay(10);
5712
5713 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5714 DRM_ERROR("DBuf power enable timeout\n");
5715}
5716
c73666f3
SK
5717int skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
5718{
5719 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
5720 uint32_t cdctl = I915_READ(CDCLK_CTL);
5721 int freq = dev_priv->skl_boot_cdclk;
5722
f1b391a5
SK
5723 /*
5724 * check if the pre-os intialized the display
5725 * There is SWF18 scratchpad register defined which is set by the
5726 * pre-os which can be used by the OS drivers to check the status
5727 */
5728 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
5729 goto sanitize;
5730
c73666f3
SK
5731 /* Is PLL enabled and locked ? */
5732 if (!((lcpll1 & LCPLL_PLL_ENABLE) && (lcpll1 & LCPLL_PLL_LOCK)))
5733 goto sanitize;
5734
5735 /* DPLL okay; verify the cdclock
5736 *
5737 * Noticed in some instances that the freq selection is correct but
5738 * decimal part is programmed wrong from BIOS where pre-os does not
5739 * enable display. Verify the same as well.
5740 */
5741 if (cdctl == ((cdctl & CDCLK_FREQ_SEL_MASK) | skl_cdclk_decimal(freq)))
5742 /* All well; nothing to sanitize */
5743 return false;
5744sanitize:
5745 /*
5746 * As of now initialize with max cdclk till
5747 * we get dynamic cdclk support
5748 * */
5749 dev_priv->skl_boot_cdclk = dev_priv->max_cdclk_freq;
5750 skl_init_cdclk(dev_priv);
5751
5752 /* we did have to sanitize */
5753 return true;
5754}
5755
30a970c6
JB
5756/* Adjust CDclk dividers to allow high res or save power if possible */
5757static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5758{
5759 struct drm_i915_private *dev_priv = dev->dev_private;
5760 u32 val, cmd;
5761
164dfd28
VK
5762 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5763 != dev_priv->cdclk_freq);
d60c4473 5764
dfcab17e 5765 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
30a970c6 5766 cmd = 2;
dfcab17e 5767 else if (cdclk == 266667)
30a970c6
JB
5768 cmd = 1;
5769 else
5770 cmd = 0;
5771
5772 mutex_lock(&dev_priv->rps.hw_lock);
5773 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5774 val &= ~DSPFREQGUAR_MASK;
5775 val |= (cmd << DSPFREQGUAR_SHIFT);
5776 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5777 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5778 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5779 50)) {
5780 DRM_ERROR("timed out waiting for CDclk change\n");
5781 }
5782 mutex_unlock(&dev_priv->rps.hw_lock);
5783
54433e91
VS
5784 mutex_lock(&dev_priv->sb_lock);
5785
dfcab17e 5786 if (cdclk == 400000) {
6bcda4f0 5787 u32 divider;
30a970c6 5788
6bcda4f0 5789 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
30a970c6 5790
30a970c6
JB
5791 /* adjust cdclk divider */
5792 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
87d5d259 5793 val &= ~CCK_FREQUENCY_VALUES;
30a970c6
JB
5794 val |= divider;
5795 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
a877e801
VS
5796
5797 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
87d5d259 5798 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
a877e801
VS
5799 50))
5800 DRM_ERROR("timed out waiting for CDclk change\n");
30a970c6
JB
5801 }
5802
30a970c6
JB
5803 /* adjust self-refresh exit latency value */
5804 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5805 val &= ~0x7f;
5806
5807 /*
5808 * For high bandwidth configs, we set a higher latency in the bunit
5809 * so that the core display fetch happens in time to avoid underruns.
5810 */
dfcab17e 5811 if (cdclk == 400000)
30a970c6
JB
5812 val |= 4500 / 250; /* 4.5 usec */
5813 else
5814 val |= 3000 / 250; /* 3.0 usec */
5815 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
54433e91 5816
a580516d 5817 mutex_unlock(&dev_priv->sb_lock);
30a970c6 5818
b6283055 5819 intel_update_cdclk(dev);
30a970c6
JB
5820}
5821
383c5a6a
VS
5822static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5823{
5824 struct drm_i915_private *dev_priv = dev->dev_private;
5825 u32 val, cmd;
5826
164dfd28
VK
5827 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5828 != dev_priv->cdclk_freq);
383c5a6a
VS
5829
5830 switch (cdclk) {
383c5a6a
VS
5831 case 333333:
5832 case 320000:
383c5a6a 5833 case 266667:
383c5a6a 5834 case 200000:
383c5a6a
VS
5835 break;
5836 default:
5f77eeb0 5837 MISSING_CASE(cdclk);
383c5a6a
VS
5838 return;
5839 }
5840
9d0d3fda
VS
5841 /*
5842 * Specs are full of misinformation, but testing on actual
5843 * hardware has shown that we just need to write the desired
5844 * CCK divider into the Punit register.
5845 */
5846 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5847
383c5a6a
VS
5848 mutex_lock(&dev_priv->rps.hw_lock);
5849 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5850 val &= ~DSPFREQGUAR_MASK_CHV;
5851 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5852 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5853 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5854 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5855 50)) {
5856 DRM_ERROR("timed out waiting for CDclk change\n");
5857 }
5858 mutex_unlock(&dev_priv->rps.hw_lock);
5859
b6283055 5860 intel_update_cdclk(dev);
383c5a6a
VS
5861}
5862
30a970c6
JB
5863static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5864 int max_pixclk)
5865{
6bcda4f0 5866 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
6cca3195 5867 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
29dc7ef3 5868
30a970c6
JB
5869 /*
5870 * Really only a few cases to deal with, as only 4 CDclks are supported:
5871 * 200MHz
5872 * 267MHz
29dc7ef3 5873 * 320/333MHz (depends on HPLL freq)
6cca3195
VS
5874 * 400MHz (VLV only)
5875 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5876 * of the lower bin and adjust if needed.
e37c67a1
VS
5877 *
5878 * We seem to get an unstable or solid color picture at 200MHz.
5879 * Not sure what's wrong. For now use 200MHz only when all pipes
5880 * are off.
30a970c6 5881 */
6cca3195
VS
5882 if (!IS_CHERRYVIEW(dev_priv) &&
5883 max_pixclk > freq_320*limit/100)
dfcab17e 5884 return 400000;
6cca3195 5885 else if (max_pixclk > 266667*limit/100)
29dc7ef3 5886 return freq_320;
e37c67a1 5887 else if (max_pixclk > 0)
dfcab17e 5888 return 266667;
e37c67a1
VS
5889 else
5890 return 200000;
30a970c6
JB
5891}
5892
f8437dd1
VK
5893static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5894 int max_pixclk)
5895{
5896 /*
5897 * FIXME:
5898 * - remove the guardband, it's not needed on BXT
5899 * - set 19.2MHz bypass frequency if there are no active pipes
5900 */
5901 if (max_pixclk > 576000*9/10)
5902 return 624000;
5903 else if (max_pixclk > 384000*9/10)
5904 return 576000;
5905 else if (max_pixclk > 288000*9/10)
5906 return 384000;
5907 else if (max_pixclk > 144000*9/10)
5908 return 288000;
5909 else
5910 return 144000;
5911}
5912
e8788cbc 5913/* Compute the max pixel clock for new configuration. */
a821fc46
ACO
5914static int intel_mode_max_pixclk(struct drm_device *dev,
5915 struct drm_atomic_state *state)
30a970c6 5916{
565602d7
ML
5917 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
5918 struct drm_i915_private *dev_priv = dev->dev_private;
5919 struct drm_crtc *crtc;
5920 struct drm_crtc_state *crtc_state;
5921 unsigned max_pixclk = 0, i;
5922 enum pipe pipe;
30a970c6 5923
565602d7
ML
5924 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
5925 sizeof(intel_state->min_pixclk));
304603f4 5926
565602d7
ML
5927 for_each_crtc_in_state(state, crtc, crtc_state, i) {
5928 int pixclk = 0;
5929
5930 if (crtc_state->enable)
5931 pixclk = crtc_state->adjusted_mode.crtc_clock;
304603f4 5932
565602d7 5933 intel_state->min_pixclk[i] = pixclk;
30a970c6
JB
5934 }
5935
565602d7
ML
5936 for_each_pipe(dev_priv, pipe)
5937 max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
5938
30a970c6
JB
5939 return max_pixclk;
5940}
5941
27c329ed 5942static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
30a970c6 5943{
27c329ed
ML
5944 struct drm_device *dev = state->dev;
5945 struct drm_i915_private *dev_priv = dev->dev_private;
5946 int max_pixclk = intel_mode_max_pixclk(dev, state);
1a617b77
ML
5947 struct intel_atomic_state *intel_state =
5948 to_intel_atomic_state(state);
30a970c6 5949
304603f4
ACO
5950 if (max_pixclk < 0)
5951 return max_pixclk;
30a970c6 5952
1a617b77 5953 intel_state->cdclk = intel_state->dev_cdclk =
27c329ed 5954 valleyview_calc_cdclk(dev_priv, max_pixclk);
0a9ab303 5955
1a617b77
ML
5956 if (!intel_state->active_crtcs)
5957 intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
5958
27c329ed
ML
5959 return 0;
5960}
304603f4 5961
27c329ed
ML
5962static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
5963{
5964 struct drm_device *dev = state->dev;
5965 struct drm_i915_private *dev_priv = dev->dev_private;
5966 int max_pixclk = intel_mode_max_pixclk(dev, state);
1a617b77
ML
5967 struct intel_atomic_state *intel_state =
5968 to_intel_atomic_state(state);
85a96e7a 5969
27c329ed
ML
5970 if (max_pixclk < 0)
5971 return max_pixclk;
85a96e7a 5972
1a617b77 5973 intel_state->cdclk = intel_state->dev_cdclk =
27c329ed 5974 broxton_calc_cdclk(dev_priv, max_pixclk);
85a96e7a 5975
1a617b77
ML
5976 if (!intel_state->active_crtcs)
5977 intel_state->dev_cdclk = broxton_calc_cdclk(dev_priv, 0);
5978
27c329ed 5979 return 0;
30a970c6
JB
5980}
5981
1e69cd74
VS
5982static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5983{
5984 unsigned int credits, default_credits;
5985
5986 if (IS_CHERRYVIEW(dev_priv))
5987 default_credits = PFI_CREDIT(12);
5988 else
5989 default_credits = PFI_CREDIT(8);
5990
bfa7df01 5991 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
1e69cd74
VS
5992 /* CHV suggested value is 31 or 63 */
5993 if (IS_CHERRYVIEW(dev_priv))
fcc0008f 5994 credits = PFI_CREDIT_63;
1e69cd74
VS
5995 else
5996 credits = PFI_CREDIT(15);
5997 } else {
5998 credits = default_credits;
5999 }
6000
6001 /*
6002 * WA - write default credits before re-programming
6003 * FIXME: should we also set the resend bit here?
6004 */
6005 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6006 default_credits);
6007
6008 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6009 credits | PFI_CREDIT_RESEND);
6010
6011 /*
6012 * FIXME is this guaranteed to clear
6013 * immediately or should we poll for it?
6014 */
6015 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6016}
6017
27c329ed 6018static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
30a970c6 6019{
a821fc46 6020 struct drm_device *dev = old_state->dev;
30a970c6 6021 struct drm_i915_private *dev_priv = dev->dev_private;
1a617b77
ML
6022 struct intel_atomic_state *old_intel_state =
6023 to_intel_atomic_state(old_state);
6024 unsigned req_cdclk = old_intel_state->dev_cdclk;
30a970c6 6025
27c329ed
ML
6026 /*
6027 * FIXME: We can end up here with all power domains off, yet
6028 * with a CDCLK frequency other than the minimum. To account
6029 * for this take the PIPE-A power domain, which covers the HW
6030 * blocks needed for the following programming. This can be
6031 * removed once it's guaranteed that we get here either with
6032 * the minimum CDCLK set, or the required power domains
6033 * enabled.
6034 */
6035 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
738c05c0 6036
27c329ed
ML
6037 if (IS_CHERRYVIEW(dev))
6038 cherryview_set_cdclk(dev, req_cdclk);
6039 else
6040 valleyview_set_cdclk(dev, req_cdclk);
738c05c0 6041
27c329ed 6042 vlv_program_pfi_credits(dev_priv);
1e69cd74 6043
27c329ed 6044 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
30a970c6
JB
6045}
6046
89b667f8
JB
6047static void valleyview_crtc_enable(struct drm_crtc *crtc)
6048{
6049 struct drm_device *dev = crtc->dev;
a72e4c9f 6050 struct drm_i915_private *dev_priv = to_i915(dev);
89b667f8
JB
6051 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6052 struct intel_encoder *encoder;
6053 int pipe = intel_crtc->pipe;
89b667f8 6054
53d9f4e9 6055 if (WARN_ON(intel_crtc->active))
89b667f8
JB
6056 return;
6057
6e3c9717 6058 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 6059 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
6060
6061 intel_set_pipe_timings(intel_crtc);
bc58be60 6062 intel_set_pipe_src_size(intel_crtc);
5b18e57c 6063
c14b0485
VS
6064 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6065 struct drm_i915_private *dev_priv = dev->dev_private;
6066
6067 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6068 I915_WRITE(CHV_CANVAS(pipe), 0);
6069 }
6070
5b18e57c
DV
6071 i9xx_set_pipeconf(intel_crtc);
6072
89b667f8 6073 intel_crtc->active = true;
89b667f8 6074
a72e4c9f 6075 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6076
89b667f8
JB
6077 for_each_encoder_on_crtc(dev, crtc, encoder)
6078 if (encoder->pre_pll_enable)
6079 encoder->pre_pll_enable(encoder);
6080
a65347ba 6081 if (!intel_crtc->config->has_dsi_encoder) {
c0b4c660
VS
6082 if (IS_CHERRYVIEW(dev)) {
6083 chv_prepare_pll(intel_crtc, intel_crtc->config);
6e3c9717 6084 chv_enable_pll(intel_crtc, intel_crtc->config);
c0b4c660
VS
6085 } else {
6086 vlv_prepare_pll(intel_crtc, intel_crtc->config);
6e3c9717 6087 vlv_enable_pll(intel_crtc, intel_crtc->config);
c0b4c660 6088 }
9d556c99 6089 }
89b667f8
JB
6090
6091 for_each_encoder_on_crtc(dev, crtc, encoder)
6092 if (encoder->pre_enable)
6093 encoder->pre_enable(encoder);
6094
2dd24552
JB
6095 i9xx_pfit_enable(intel_crtc);
6096
8563b1e8 6097 intel_color_load_luts(crtc);
63cbb074 6098
caed361d 6099 intel_update_watermarks(crtc);
e1fdc473 6100 intel_enable_pipe(intel_crtc);
be6a6f8e 6101
4b3a9526
VS
6102 assert_vblank_disabled(crtc);
6103 drm_crtc_vblank_on(crtc);
6104
f9b61ff6
DV
6105 for_each_encoder_on_crtc(dev, crtc, encoder)
6106 encoder->enable(encoder);
89b667f8
JB
6107}
6108
f13c2ef3
DV
6109static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6110{
6111 struct drm_device *dev = crtc->base.dev;
6112 struct drm_i915_private *dev_priv = dev->dev_private;
6113
6e3c9717
ACO
6114 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6115 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
f13c2ef3
DV
6116}
6117
0b8765c6 6118static void i9xx_crtc_enable(struct drm_crtc *crtc)
79e53945
JB
6119{
6120 struct drm_device *dev = crtc->dev;
a72e4c9f 6121 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 6122 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 6123 struct intel_encoder *encoder;
79e53945 6124 int pipe = intel_crtc->pipe;
79e53945 6125
53d9f4e9 6126 if (WARN_ON(intel_crtc->active))
f7abfe8b
CW
6127 return;
6128
f13c2ef3
DV
6129 i9xx_set_pll_dividers(intel_crtc);
6130
6e3c9717 6131 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 6132 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
6133
6134 intel_set_pipe_timings(intel_crtc);
bc58be60 6135 intel_set_pipe_src_size(intel_crtc);
5b18e57c 6136
5b18e57c
DV
6137 i9xx_set_pipeconf(intel_crtc);
6138
f7abfe8b 6139 intel_crtc->active = true;
6b383a7f 6140
4a3436e8 6141 if (!IS_GEN2(dev))
a72e4c9f 6142 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6143
9d6d9f19
MK
6144 for_each_encoder_on_crtc(dev, crtc, encoder)
6145 if (encoder->pre_enable)
6146 encoder->pre_enable(encoder);
6147
f6736a1a
DV
6148 i9xx_enable_pll(intel_crtc);
6149
2dd24552
JB
6150 i9xx_pfit_enable(intel_crtc);
6151
8563b1e8 6152 intel_color_load_luts(crtc);
63cbb074 6153
f37fcc2a 6154 intel_update_watermarks(crtc);
e1fdc473 6155 intel_enable_pipe(intel_crtc);
be6a6f8e 6156
4b3a9526
VS
6157 assert_vblank_disabled(crtc);
6158 drm_crtc_vblank_on(crtc);
6159
f9b61ff6
DV
6160 for_each_encoder_on_crtc(dev, crtc, encoder)
6161 encoder->enable(encoder);
0b8765c6 6162}
79e53945 6163
87476d63
DV
6164static void i9xx_pfit_disable(struct intel_crtc *crtc)
6165{
6166 struct drm_device *dev = crtc->base.dev;
6167 struct drm_i915_private *dev_priv = dev->dev_private;
87476d63 6168
6e3c9717 6169 if (!crtc->config->gmch_pfit.control)
328d8e82 6170 return;
87476d63 6171
328d8e82 6172 assert_pipe_disabled(dev_priv, crtc->pipe);
87476d63 6173
328d8e82
DV
6174 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6175 I915_READ(PFIT_CONTROL));
6176 I915_WRITE(PFIT_CONTROL, 0);
87476d63
DV
6177}
6178
0b8765c6
JB
6179static void i9xx_crtc_disable(struct drm_crtc *crtc)
6180{
6181 struct drm_device *dev = crtc->dev;
6182 struct drm_i915_private *dev_priv = dev->dev_private;
6183 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 6184 struct intel_encoder *encoder;
0b8765c6 6185 int pipe = intel_crtc->pipe;
ef9c3aee 6186
6304cd91
VS
6187 /*
6188 * On gen2 planes are double buffered but the pipe isn't, so we must
6189 * wait for planes to fully turn off before disabling the pipe.
6190 */
90e83e53
ACO
6191 if (IS_GEN2(dev))
6192 intel_wait_for_vblank(dev, pipe);
6304cd91 6193
4b3a9526
VS
6194 for_each_encoder_on_crtc(dev, crtc, encoder)
6195 encoder->disable(encoder);
6196
f9b61ff6
DV
6197 drm_crtc_vblank_off(crtc);
6198 assert_vblank_disabled(crtc);
6199
575f7ab7 6200 intel_disable_pipe(intel_crtc);
24a1f16d 6201
87476d63 6202 i9xx_pfit_disable(intel_crtc);
24a1f16d 6203
89b667f8
JB
6204 for_each_encoder_on_crtc(dev, crtc, encoder)
6205 if (encoder->post_disable)
6206 encoder->post_disable(encoder);
6207
a65347ba 6208 if (!intel_crtc->config->has_dsi_encoder) {
076ed3b2
CML
6209 if (IS_CHERRYVIEW(dev))
6210 chv_disable_pll(dev_priv, pipe);
6211 else if (IS_VALLEYVIEW(dev))
6212 vlv_disable_pll(dev_priv, pipe);
6213 else
1c4e0274 6214 i9xx_disable_pll(intel_crtc);
076ed3b2 6215 }
0b8765c6 6216
d6db995f
VS
6217 for_each_encoder_on_crtc(dev, crtc, encoder)
6218 if (encoder->post_pll_disable)
6219 encoder->post_pll_disable(encoder);
6220
4a3436e8 6221 if (!IS_GEN2(dev))
a72e4c9f 6222 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
0b8765c6
JB
6223}
6224
b17d48e2
ML
6225static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
6226{
842e0307 6227 struct intel_encoder *encoder;
b17d48e2
ML
6228 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6229 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6230 enum intel_display_power_domain domain;
6231 unsigned long domains;
6232
6233 if (!intel_crtc->active)
6234 return;
6235
a539205a 6236 if (to_intel_plane_state(crtc->primary->state)->visible) {
fc32b1fd
ML
6237 WARN_ON(intel_crtc->unpin_work);
6238
2622a081 6239 intel_pre_disable_primary_noatomic(crtc);
54a41961
ML
6240
6241 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
6242 to_intel_plane_state(crtc->primary->state)->visible = false;
a539205a
ML
6243 }
6244
b17d48e2 6245 dev_priv->display.crtc_disable(crtc);
842e0307
ML
6246
6247 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was enabled, now disabled\n",
6248 crtc->base.id);
6249
6250 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6251 crtc->state->active = false;
37d9078b 6252 intel_crtc->active = false;
842e0307
ML
6253 crtc->enabled = false;
6254 crtc->state->connector_mask = 0;
6255 crtc->state->encoder_mask = 0;
6256
6257 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6258 encoder->base.crtc = NULL;
6259
58f9c0bc 6260 intel_fbc_disable(intel_crtc);
37d9078b 6261 intel_update_watermarks(crtc);
1f7457b1 6262 intel_disable_shared_dpll(intel_crtc);
b17d48e2
ML
6263
6264 domains = intel_crtc->enabled_power_domains;
6265 for_each_power_domain(domain, domains)
6266 intel_display_power_put(dev_priv, domain);
6267 intel_crtc->enabled_power_domains = 0;
565602d7
ML
6268
6269 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
6270 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
b17d48e2
ML
6271}
6272
6b72d486
ML
6273/*
6274 * turn all crtc's off, but do not adjust state
6275 * This has to be paired with a call to intel_modeset_setup_hw_state.
6276 */
70e0bd74 6277int intel_display_suspend(struct drm_device *dev)
ee7b9f93 6278{
e2c8b870 6279 struct drm_i915_private *dev_priv = to_i915(dev);
70e0bd74 6280 struct drm_atomic_state *state;
e2c8b870 6281 int ret;
70e0bd74 6282
e2c8b870
ML
6283 state = drm_atomic_helper_suspend(dev);
6284 ret = PTR_ERR_OR_ZERO(state);
70e0bd74
ML
6285 if (ret)
6286 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
e2c8b870
ML
6287 else
6288 dev_priv->modeset_restore_state = state;
70e0bd74 6289 return ret;
ee7b9f93
JB
6290}
6291
ea5b213a 6292void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 6293{
4ef69c7a 6294 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 6295
ea5b213a
CW
6296 drm_encoder_cleanup(encoder);
6297 kfree(intel_encoder);
7e7d76c3
JB
6298}
6299
0a91ca29
DV
6300/* Cross check the actual hw state with our own modeset state tracking (and it's
6301 * internal consistency). */
b980514c 6302static void intel_connector_check_state(struct intel_connector *connector)
79e53945 6303{
35dd3c64
ML
6304 struct drm_crtc *crtc = connector->base.state->crtc;
6305
6306 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6307 connector->base.base.id,
6308 connector->base.name);
6309
0a91ca29 6310 if (connector->get_hw_state(connector)) {
e85376cb 6311 struct intel_encoder *encoder = connector->encoder;
35dd3c64 6312 struct drm_connector_state *conn_state = connector->base.state;
0a91ca29 6313
35dd3c64
ML
6314 I915_STATE_WARN(!crtc,
6315 "connector enabled without attached crtc\n");
0a91ca29 6316
35dd3c64
ML
6317 if (!crtc)
6318 return;
6319
6320 I915_STATE_WARN(!crtc->state->active,
6321 "connector is active, but attached crtc isn't\n");
6322
e85376cb 6323 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
35dd3c64
ML
6324 return;
6325
e85376cb 6326 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
35dd3c64
ML
6327 "atomic encoder doesn't match attached encoder\n");
6328
e85376cb 6329 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
35dd3c64
ML
6330 "attached encoder crtc differs from connector crtc\n");
6331 } else {
4d688a2a
ML
6332 I915_STATE_WARN(crtc && crtc->state->active,
6333 "attached crtc is active, but connector isn't\n");
35dd3c64
ML
6334 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
6335 "best encoder set without crtc!\n");
0a91ca29 6336 }
79e53945
JB
6337}
6338
08d9bc92
ACO
6339int intel_connector_init(struct intel_connector *connector)
6340{
5350a031 6341 drm_atomic_helper_connector_reset(&connector->base);
08d9bc92 6342
5350a031 6343 if (!connector->base.state)
08d9bc92
ACO
6344 return -ENOMEM;
6345
08d9bc92
ACO
6346 return 0;
6347}
6348
6349struct intel_connector *intel_connector_alloc(void)
6350{
6351 struct intel_connector *connector;
6352
6353 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6354 if (!connector)
6355 return NULL;
6356
6357 if (intel_connector_init(connector) < 0) {
6358 kfree(connector);
6359 return NULL;
6360 }
6361
6362 return connector;
6363}
6364
f0947c37
DV
6365/* Simple connector->get_hw_state implementation for encoders that support only
6366 * one connector and no cloning and hence the encoder state determines the state
6367 * of the connector. */
6368bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 6369{
24929352 6370 enum pipe pipe = 0;
f0947c37 6371 struct intel_encoder *encoder = connector->encoder;
ea5b213a 6372
f0947c37 6373 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
6374}
6375
6d293983 6376static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
d272ddfa 6377{
6d293983
ACO
6378 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6379 return crtc_state->fdi_lanes;
d272ddfa
VS
6380
6381 return 0;
6382}
6383
6d293983 6384static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5cec258b 6385 struct intel_crtc_state *pipe_config)
1857e1da 6386{
6d293983
ACO
6387 struct drm_atomic_state *state = pipe_config->base.state;
6388 struct intel_crtc *other_crtc;
6389 struct intel_crtc_state *other_crtc_state;
6390
1857e1da
DV
6391 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6392 pipe_name(pipe), pipe_config->fdi_lanes);
6393 if (pipe_config->fdi_lanes > 4) {
6394 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6395 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6396 return -EINVAL;
1857e1da
DV
6397 }
6398
bafb6553 6399 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
1857e1da
DV
6400 if (pipe_config->fdi_lanes > 2) {
6401 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6402 pipe_config->fdi_lanes);
6d293983 6403 return -EINVAL;
1857e1da 6404 } else {
6d293983 6405 return 0;
1857e1da
DV
6406 }
6407 }
6408
6409 if (INTEL_INFO(dev)->num_pipes == 2)
6d293983 6410 return 0;
1857e1da
DV
6411
6412 /* Ivybridge 3 pipe is really complicated */
6413 switch (pipe) {
6414 case PIPE_A:
6d293983 6415 return 0;
1857e1da 6416 case PIPE_B:
6d293983
ACO
6417 if (pipe_config->fdi_lanes <= 2)
6418 return 0;
6419
6420 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6421 other_crtc_state =
6422 intel_atomic_get_crtc_state(state, other_crtc);
6423 if (IS_ERR(other_crtc_state))
6424 return PTR_ERR(other_crtc_state);
6425
6426 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
1857e1da
DV
6427 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6428 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6429 return -EINVAL;
1857e1da 6430 }
6d293983 6431 return 0;
1857e1da 6432 case PIPE_C:
251cc67c
VS
6433 if (pipe_config->fdi_lanes > 2) {
6434 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6435 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6436 return -EINVAL;
251cc67c 6437 }
6d293983
ACO
6438
6439 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6440 other_crtc_state =
6441 intel_atomic_get_crtc_state(state, other_crtc);
6442 if (IS_ERR(other_crtc_state))
6443 return PTR_ERR(other_crtc_state);
6444
6445 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
1857e1da 6446 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
6d293983 6447 return -EINVAL;
1857e1da 6448 }
6d293983 6449 return 0;
1857e1da
DV
6450 default:
6451 BUG();
6452 }
6453}
6454
e29c22c0
DV
6455#define RETRY 1
6456static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5cec258b 6457 struct intel_crtc_state *pipe_config)
877d48d5 6458{
1857e1da 6459 struct drm_device *dev = intel_crtc->base.dev;
7c5f93b0 6460 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6d293983
ACO
6461 int lane, link_bw, fdi_dotclock, ret;
6462 bool needs_recompute = false;
877d48d5 6463
e29c22c0 6464retry:
877d48d5
DV
6465 /* FDI is a binary signal running at ~2.7GHz, encoding
6466 * each output octet as 10 bits. The actual frequency
6467 * is stored as a divider into a 100MHz clock, and the
6468 * mode pixel clock is stored in units of 1KHz.
6469 * Hence the bw of each lane in terms of the mode signal
6470 * is:
6471 */
21a727b3 6472 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
877d48d5 6473
241bfc38 6474 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 6475
2bd89a07 6476 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
DV
6477 pipe_config->pipe_bpp);
6478
6479 pipe_config->fdi_lanes = lane;
6480
2bd89a07 6481 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
877d48d5 6482 link_bw, &pipe_config->fdi_m_n);
1857e1da 6483
e3b247da 6484 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
6d293983 6485 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
e29c22c0
DV
6486 pipe_config->pipe_bpp -= 2*3;
6487 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6488 pipe_config->pipe_bpp);
6489 needs_recompute = true;
6490 pipe_config->bw_constrained = true;
6491
6492 goto retry;
6493 }
6494
6495 if (needs_recompute)
6496 return RETRY;
6497
6d293983 6498 return ret;
877d48d5
DV
6499}
6500
8cfb3407
VS
6501static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6502 struct intel_crtc_state *pipe_config)
6503{
6504 if (pipe_config->pipe_bpp > 24)
6505 return false;
6506
6507 /* HSW can handle pixel rate up to cdclk? */
6508 if (IS_HASWELL(dev_priv->dev))
6509 return true;
6510
6511 /*
b432e5cf
VS
6512 * We compare against max which means we must take
6513 * the increased cdclk requirement into account when
6514 * calculating the new cdclk.
6515 *
6516 * Should measure whether using a lower cdclk w/o IPS
8cfb3407
VS
6517 */
6518 return ilk_pipe_pixel_rate(pipe_config) <=
6519 dev_priv->max_cdclk_freq * 95 / 100;
6520}
6521
42db64ef 6522static void hsw_compute_ips_config(struct intel_crtc *crtc,
5cec258b 6523 struct intel_crtc_state *pipe_config)
42db64ef 6524{
8cfb3407
VS
6525 struct drm_device *dev = crtc->base.dev;
6526 struct drm_i915_private *dev_priv = dev->dev_private;
6527
d330a953 6528 pipe_config->ips_enabled = i915.enable_ips &&
8cfb3407
VS
6529 hsw_crtc_supports_ips(crtc) &&
6530 pipe_config_supports_ips(dev_priv, pipe_config);
42db64ef
PZ
6531}
6532
39acb4aa
VS
6533static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
6534{
6535 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6536
6537 /* GDG double wide on either pipe, otherwise pipe A only */
6538 return INTEL_INFO(dev_priv)->gen < 4 &&
6539 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
6540}
6541
a43f6e0f 6542static int intel_crtc_compute_config(struct intel_crtc *crtc,
5cec258b 6543 struct intel_crtc_state *pipe_config)
79e53945 6544{
a43f6e0f 6545 struct drm_device *dev = crtc->base.dev;
8bd31e67 6546 struct drm_i915_private *dev_priv = dev->dev_private;
7c5f93b0 6547 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
89749350 6548
ad3a4479 6549 /* FIXME should check pixel clock limits on all platforms */
cf532bb2 6550 if (INTEL_INFO(dev)->gen < 4) {
39acb4aa 6551 int clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
cf532bb2
VS
6552
6553 /*
39acb4aa 6554 * Enable double wide mode when the dot clock
cf532bb2 6555 * is > 90% of the (display) core speed.
cf532bb2 6556 */
39acb4aa
VS
6557 if (intel_crtc_supports_double_wide(crtc) &&
6558 adjusted_mode->crtc_clock > clock_limit) {
ad3a4479 6559 clock_limit *= 2;
cf532bb2 6560 pipe_config->double_wide = true;
ad3a4479
VS
6561 }
6562
39acb4aa
VS
6563 if (adjusted_mode->crtc_clock > clock_limit) {
6564 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6565 adjusted_mode->crtc_clock, clock_limit,
6566 yesno(pipe_config->double_wide));
e29c22c0 6567 return -EINVAL;
39acb4aa 6568 }
2c07245f 6569 }
89749350 6570
1d1d0e27
VS
6571 /*
6572 * Pipe horizontal size must be even in:
6573 * - DVO ganged mode
6574 * - LVDS dual channel mode
6575 * - Double wide pipe
6576 */
a93e255f 6577 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
1d1d0e27
VS
6578 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6579 pipe_config->pipe_src_w &= ~1;
6580
8693a824
DL
6581 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6582 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
44f46b42
CW
6583 */
6584 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
aad941d5 6585 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
e29c22c0 6586 return -EINVAL;
44f46b42 6587
f5adf94e 6588 if (HAS_IPS(dev))
a43f6e0f
DV
6589 hsw_compute_ips_config(crtc, pipe_config);
6590
877d48d5 6591 if (pipe_config->has_pch_encoder)
a43f6e0f 6592 return ironlake_fdi_compute_config(crtc, pipe_config);
877d48d5 6593
cf5a15be 6594 return 0;
79e53945
JB
6595}
6596
1652d19e
VS
6597static int skylake_get_display_clock_speed(struct drm_device *dev)
6598{
6599 struct drm_i915_private *dev_priv = to_i915(dev);
6600 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6601 uint32_t cdctl = I915_READ(CDCLK_CTL);
6602 uint32_t linkrate;
6603
414355a7 6604 if (!(lcpll1 & LCPLL_PLL_ENABLE))
1652d19e 6605 return 24000; /* 24MHz is the cd freq with NSSC ref */
1652d19e
VS
6606
6607 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6608 return 540000;
6609
6610 linkrate = (I915_READ(DPLL_CTRL1) &
71cd8423 6611 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
1652d19e 6612
71cd8423
DL
6613 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6614 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
1652d19e
VS
6615 /* vco 8640 */
6616 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6617 case CDCLK_FREQ_450_432:
6618 return 432000;
6619 case CDCLK_FREQ_337_308:
6620 return 308570;
6621 case CDCLK_FREQ_675_617:
6622 return 617140;
6623 default:
6624 WARN(1, "Unknown cd freq selection\n");
6625 }
6626 } else {
6627 /* vco 8100 */
6628 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6629 case CDCLK_FREQ_450_432:
6630 return 450000;
6631 case CDCLK_FREQ_337_308:
6632 return 337500;
6633 case CDCLK_FREQ_675_617:
6634 return 675000;
6635 default:
6636 WARN(1, "Unknown cd freq selection\n");
6637 }
6638 }
6639
6640 /* error case, do as if DPLL0 isn't enabled */
6641 return 24000;
6642}
6643
acd3f3d3
BP
6644static int broxton_get_display_clock_speed(struct drm_device *dev)
6645{
6646 struct drm_i915_private *dev_priv = to_i915(dev);
6647 uint32_t cdctl = I915_READ(CDCLK_CTL);
6648 uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
6649 uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
6650 int cdclk;
6651
6652 if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
6653 return 19200;
6654
6655 cdclk = 19200 * pll_ratio / 2;
6656
6657 switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
6658 case BXT_CDCLK_CD2X_DIV_SEL_1:
6659 return cdclk; /* 576MHz or 624MHz */
6660 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
6661 return cdclk * 2 / 3; /* 384MHz */
6662 case BXT_CDCLK_CD2X_DIV_SEL_2:
6663 return cdclk / 2; /* 288MHz */
6664 case BXT_CDCLK_CD2X_DIV_SEL_4:
6665 return cdclk / 4; /* 144MHz */
6666 }
6667
6668 /* error case, do as if DE PLL isn't enabled */
6669 return 19200;
6670}
6671
1652d19e
VS
6672static int broadwell_get_display_clock_speed(struct drm_device *dev)
6673{
6674 struct drm_i915_private *dev_priv = dev->dev_private;
6675 uint32_t lcpll = I915_READ(LCPLL_CTL);
6676 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6677
6678 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6679 return 800000;
6680 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6681 return 450000;
6682 else if (freq == LCPLL_CLK_FREQ_450)
6683 return 450000;
6684 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6685 return 540000;
6686 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6687 return 337500;
6688 else
6689 return 675000;
6690}
6691
6692static int haswell_get_display_clock_speed(struct drm_device *dev)
6693{
6694 struct drm_i915_private *dev_priv = dev->dev_private;
6695 uint32_t lcpll = I915_READ(LCPLL_CTL);
6696 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6697
6698 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6699 return 800000;
6700 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6701 return 450000;
6702 else if (freq == LCPLL_CLK_FREQ_450)
6703 return 450000;
6704 else if (IS_HSW_ULT(dev))
6705 return 337500;
6706 else
6707 return 540000;
79e53945
JB
6708}
6709
25eb05fc
JB
6710static int valleyview_get_display_clock_speed(struct drm_device *dev)
6711{
bfa7df01
VS
6712 return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
6713 CCK_DISPLAY_CLOCK_CONTROL);
25eb05fc
JB
6714}
6715
b37a6434
VS
6716static int ilk_get_display_clock_speed(struct drm_device *dev)
6717{
6718 return 450000;
6719}
6720
e70236a8
JB
6721static int i945_get_display_clock_speed(struct drm_device *dev)
6722{
6723 return 400000;
6724}
79e53945 6725
e70236a8 6726static int i915_get_display_clock_speed(struct drm_device *dev)
79e53945 6727{
e907f170 6728 return 333333;
e70236a8 6729}
79e53945 6730
e70236a8
JB
6731static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6732{
6733 return 200000;
6734}
79e53945 6735
257a7ffc
DV
6736static int pnv_get_display_clock_speed(struct drm_device *dev)
6737{
6738 u16 gcfgc = 0;
6739
6740 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6741
6742 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6743 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
e907f170 6744 return 266667;
257a7ffc 6745 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
e907f170 6746 return 333333;
257a7ffc 6747 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
e907f170 6748 return 444444;
257a7ffc
DV
6749 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6750 return 200000;
6751 default:
6752 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6753 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
e907f170 6754 return 133333;
257a7ffc 6755 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
e907f170 6756 return 166667;
257a7ffc
DV
6757 }
6758}
6759
e70236a8
JB
6760static int i915gm_get_display_clock_speed(struct drm_device *dev)
6761{
6762 u16 gcfgc = 0;
79e53945 6763
e70236a8
JB
6764 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6765
6766 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
e907f170 6767 return 133333;
e70236a8
JB
6768 else {
6769 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6770 case GC_DISPLAY_CLOCK_333_MHZ:
e907f170 6771 return 333333;
e70236a8
JB
6772 default:
6773 case GC_DISPLAY_CLOCK_190_200_MHZ:
6774 return 190000;
79e53945 6775 }
e70236a8
JB
6776 }
6777}
6778
6779static int i865_get_display_clock_speed(struct drm_device *dev)
6780{
e907f170 6781 return 266667;
e70236a8
JB
6782}
6783
1b1d2716 6784static int i85x_get_display_clock_speed(struct drm_device *dev)
e70236a8
JB
6785{
6786 u16 hpllcc = 0;
1b1d2716 6787
65cd2b3f
VS
6788 /*
6789 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6790 * encoding is different :(
6791 * FIXME is this the right way to detect 852GM/852GMV?
6792 */
6793 if (dev->pdev->revision == 0x1)
6794 return 133333;
6795
1b1d2716
VS
6796 pci_bus_read_config_word(dev->pdev->bus,
6797 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6798
e70236a8
JB
6799 /* Assume that the hardware is in the high speed state. This
6800 * should be the default.
6801 */
6802 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6803 case GC_CLOCK_133_200:
1b1d2716 6804 case GC_CLOCK_133_200_2:
e70236a8
JB
6805 case GC_CLOCK_100_200:
6806 return 200000;
6807 case GC_CLOCK_166_250:
6808 return 250000;
6809 case GC_CLOCK_100_133:
e907f170 6810 return 133333;
1b1d2716
VS
6811 case GC_CLOCK_133_266:
6812 case GC_CLOCK_133_266_2:
6813 case GC_CLOCK_166_266:
6814 return 266667;
e70236a8 6815 }
79e53945 6816
e70236a8
JB
6817 /* Shouldn't happen */
6818 return 0;
6819}
79e53945 6820
e70236a8
JB
6821static int i830_get_display_clock_speed(struct drm_device *dev)
6822{
e907f170 6823 return 133333;
79e53945
JB
6824}
6825
34edce2f
VS
6826static unsigned int intel_hpll_vco(struct drm_device *dev)
6827{
6828 struct drm_i915_private *dev_priv = dev->dev_private;
6829 static const unsigned int blb_vco[8] = {
6830 [0] = 3200000,
6831 [1] = 4000000,
6832 [2] = 5333333,
6833 [3] = 4800000,
6834 [4] = 6400000,
6835 };
6836 static const unsigned int pnv_vco[8] = {
6837 [0] = 3200000,
6838 [1] = 4000000,
6839 [2] = 5333333,
6840 [3] = 4800000,
6841 [4] = 2666667,
6842 };
6843 static const unsigned int cl_vco[8] = {
6844 [0] = 3200000,
6845 [1] = 4000000,
6846 [2] = 5333333,
6847 [3] = 6400000,
6848 [4] = 3333333,
6849 [5] = 3566667,
6850 [6] = 4266667,
6851 };
6852 static const unsigned int elk_vco[8] = {
6853 [0] = 3200000,
6854 [1] = 4000000,
6855 [2] = 5333333,
6856 [3] = 4800000,
6857 };
6858 static const unsigned int ctg_vco[8] = {
6859 [0] = 3200000,
6860 [1] = 4000000,
6861 [2] = 5333333,
6862 [3] = 6400000,
6863 [4] = 2666667,
6864 [5] = 4266667,
6865 };
6866 const unsigned int *vco_table;
6867 unsigned int vco;
6868 uint8_t tmp = 0;
6869
6870 /* FIXME other chipsets? */
6871 if (IS_GM45(dev))
6872 vco_table = ctg_vco;
6873 else if (IS_G4X(dev))
6874 vco_table = elk_vco;
6875 else if (IS_CRESTLINE(dev))
6876 vco_table = cl_vco;
6877 else if (IS_PINEVIEW(dev))
6878 vco_table = pnv_vco;
6879 else if (IS_G33(dev))
6880 vco_table = blb_vco;
6881 else
6882 return 0;
6883
6884 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6885
6886 vco = vco_table[tmp & 0x7];
6887 if (vco == 0)
6888 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6889 else
6890 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6891
6892 return vco;
6893}
6894
6895static int gm45_get_display_clock_speed(struct drm_device *dev)
6896{
6897 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6898 uint16_t tmp = 0;
6899
6900 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6901
6902 cdclk_sel = (tmp >> 12) & 0x1;
6903
6904 switch (vco) {
6905 case 2666667:
6906 case 4000000:
6907 case 5333333:
6908 return cdclk_sel ? 333333 : 222222;
6909 case 3200000:
6910 return cdclk_sel ? 320000 : 228571;
6911 default:
6912 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
6913 return 222222;
6914 }
6915}
6916
6917static int i965gm_get_display_clock_speed(struct drm_device *dev)
6918{
6919 static const uint8_t div_3200[] = { 16, 10, 8 };
6920 static const uint8_t div_4000[] = { 20, 12, 10 };
6921 static const uint8_t div_5333[] = { 24, 16, 14 };
6922 const uint8_t *div_table;
6923 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6924 uint16_t tmp = 0;
6925
6926 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6927
6928 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
6929
6930 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6931 goto fail;
6932
6933 switch (vco) {
6934 case 3200000:
6935 div_table = div_3200;
6936 break;
6937 case 4000000:
6938 div_table = div_4000;
6939 break;
6940 case 5333333:
6941 div_table = div_5333;
6942 break;
6943 default:
6944 goto fail;
6945 }
6946
6947 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6948
caf4e252 6949fail:
34edce2f
VS
6950 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
6951 return 200000;
6952}
6953
6954static int g33_get_display_clock_speed(struct drm_device *dev)
6955{
6956 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
6957 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
6958 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
6959 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
6960 const uint8_t *div_table;
6961 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6962 uint16_t tmp = 0;
6963
6964 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6965
6966 cdclk_sel = (tmp >> 4) & 0x7;
6967
6968 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6969 goto fail;
6970
6971 switch (vco) {
6972 case 3200000:
6973 div_table = div_3200;
6974 break;
6975 case 4000000:
6976 div_table = div_4000;
6977 break;
6978 case 4800000:
6979 div_table = div_4800;
6980 break;
6981 case 5333333:
6982 div_table = div_5333;
6983 break;
6984 default:
6985 goto fail;
6986 }
6987
6988 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6989
caf4e252 6990fail:
34edce2f
VS
6991 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
6992 return 190476;
6993}
6994
2c07245f 6995static void
a65851af 6996intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 6997{
a65851af
VS
6998 while (*num > DATA_LINK_M_N_MASK ||
6999 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
7000 *num >>= 1;
7001 *den >>= 1;
7002 }
7003}
7004
a65851af
VS
7005static void compute_m_n(unsigned int m, unsigned int n,
7006 uint32_t *ret_m, uint32_t *ret_n)
7007{
7008 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7009 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7010 intel_reduce_m_n_ratio(ret_m, ret_n);
7011}
7012
e69d0bc1
DV
7013void
7014intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7015 int pixel_clock, int link_clock,
7016 struct intel_link_m_n *m_n)
2c07245f 7017{
e69d0bc1 7018 m_n->tu = 64;
a65851af
VS
7019
7020 compute_m_n(bits_per_pixel * pixel_clock,
7021 link_clock * nlanes * 8,
7022 &m_n->gmch_m, &m_n->gmch_n);
7023
7024 compute_m_n(pixel_clock, link_clock,
7025 &m_n->link_m, &m_n->link_n);
2c07245f
ZW
7026}
7027
a7615030
CW
7028static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7029{
d330a953
JN
7030 if (i915.panel_use_ssc >= 0)
7031 return i915.panel_use_ssc != 0;
41aa3448 7032 return dev_priv->vbt.lvds_use_ssc
435793df 7033 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
7034}
7035
ceb41007 7036static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state)
c65d77d8 7037{
a93e255f 7038 struct drm_device *dev = crtc_state->base.crtc->dev;
c65d77d8
JB
7039 struct drm_i915_private *dev_priv = dev->dev_private;
7040 int refclk;
7041
a93e255f
ACO
7042 WARN_ON(!crtc_state->base.state);
7043
666a4537 7044 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev) || IS_BROXTON(dev)) {
9a0ea498 7045 refclk = 100000;
a93e255f 7046 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 7047 intel_panel_use_ssc(dev_priv)) {
e91e941b
VS
7048 refclk = dev_priv->vbt.lvds_ssc_freq;
7049 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
c65d77d8
JB
7050 } else if (!IS_GEN2(dev)) {
7051 refclk = 96000;
7052 } else {
7053 refclk = 48000;
7054 }
7055
7056 return refclk;
7057}
7058
7429e9d4 7059static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 7060{
7df00d7a 7061 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 7062}
f47709a9 7063
7429e9d4
DV
7064static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7065{
7066 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
7067}
7068
f47709a9 7069static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
190f68c5 7070 struct intel_crtc_state *crtc_state,
a7516a05
JB
7071 intel_clock_t *reduced_clock)
7072{
f47709a9 7073 struct drm_device *dev = crtc->base.dev;
a7516a05
JB
7074 u32 fp, fp2 = 0;
7075
7076 if (IS_PINEVIEW(dev)) {
190f68c5 7077 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7078 if (reduced_clock)
7429e9d4 7079 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 7080 } else {
190f68c5 7081 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7082 if (reduced_clock)
7429e9d4 7083 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
7084 }
7085
190f68c5 7086 crtc_state->dpll_hw_state.fp0 = fp;
a7516a05 7087
f47709a9 7088 crtc->lowfreq_avail = false;
a93e255f 7089 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ab585dea 7090 reduced_clock) {
190f68c5 7091 crtc_state->dpll_hw_state.fp1 = fp2;
f47709a9 7092 crtc->lowfreq_avail = true;
a7516a05 7093 } else {
190f68c5 7094 crtc_state->dpll_hw_state.fp1 = fp;
a7516a05
JB
7095 }
7096}
7097
5e69f97f
CML
7098static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7099 pipe)
89b667f8
JB
7100{
7101 u32 reg_val;
7102
7103 /*
7104 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7105 * and set it to a reasonable value instead.
7106 */
ab3c759a 7107 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
7108 reg_val &= 0xffffff00;
7109 reg_val |= 0x00000030;
ab3c759a 7110 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7111
ab3c759a 7112 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7113 reg_val &= 0x8cffffff;
7114 reg_val = 0x8c000000;
ab3c759a 7115 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 7116
ab3c759a 7117 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 7118 reg_val &= 0xffffff00;
ab3c759a 7119 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7120
ab3c759a 7121 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7122 reg_val &= 0x00ffffff;
7123 reg_val |= 0xb0000000;
ab3c759a 7124 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
7125}
7126
b551842d
DV
7127static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7128 struct intel_link_m_n *m_n)
7129{
7130 struct drm_device *dev = crtc->base.dev;
7131 struct drm_i915_private *dev_priv = dev->dev_private;
7132 int pipe = crtc->pipe;
7133
e3b95f1e
DV
7134 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7135 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7136 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7137 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
7138}
7139
7140static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
7141 struct intel_link_m_n *m_n,
7142 struct intel_link_m_n *m2_n2)
b551842d
DV
7143{
7144 struct drm_device *dev = crtc->base.dev;
7145 struct drm_i915_private *dev_priv = dev->dev_private;
7146 int pipe = crtc->pipe;
6e3c9717 7147 enum transcoder transcoder = crtc->config->cpu_transcoder;
b551842d
DV
7148
7149 if (INTEL_INFO(dev)->gen >= 5) {
7150 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7151 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7152 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7153 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
f769cd24
VK
7154 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7155 * for gen < 8) and if DRRS is supported (to make sure the
7156 * registers are not unnecessarily accessed).
7157 */
44395bfe 7158 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
6e3c9717 7159 crtc->config->has_drrs) {
f769cd24
VK
7160 I915_WRITE(PIPE_DATA_M2(transcoder),
7161 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7162 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7163 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7164 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7165 }
b551842d 7166 } else {
e3b95f1e
DV
7167 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7168 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7169 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7170 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
7171 }
7172}
7173
fe3cd48d 7174void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
03afc4a2 7175{
fe3cd48d
R
7176 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7177
7178 if (m_n == M1_N1) {
7179 dp_m_n = &crtc->config->dp_m_n;
7180 dp_m2_n2 = &crtc->config->dp_m2_n2;
7181 } else if (m_n == M2_N2) {
7182
7183 /*
7184 * M2_N2 registers are not supported. Hence m2_n2 divider value
7185 * needs to be programmed into M1_N1.
7186 */
7187 dp_m_n = &crtc->config->dp_m2_n2;
7188 } else {
7189 DRM_ERROR("Unsupported divider value\n");
7190 return;
7191 }
7192
6e3c9717
ACO
7193 if (crtc->config->has_pch_encoder)
7194 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
03afc4a2 7195 else
fe3cd48d 7196 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
03afc4a2
DV
7197}
7198
251ac862
DV
7199static void vlv_compute_dpll(struct intel_crtc *crtc,
7200 struct intel_crtc_state *pipe_config)
bdd4b6a6
DV
7201{
7202 u32 dpll, dpll_md;
7203
7204 /*
7205 * Enable DPIO clock input. We should never disable the reference
7206 * clock for pipe B, since VGA hotplug / manual detection depends
7207 * on it.
7208 */
60bfe44f
VS
7209 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REF_CLK_ENABLE_VLV |
7210 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_REF_CLK_VLV;
bdd4b6a6
DV
7211 /* We should never disable this, set it here for state tracking */
7212 if (crtc->pipe == PIPE_B)
7213 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7214 dpll |= DPLL_VCO_ENABLE;
d288f65f 7215 pipe_config->dpll_hw_state.dpll = dpll;
bdd4b6a6 7216
d288f65f 7217 dpll_md = (pipe_config->pixel_multiplier - 1)
bdd4b6a6 7218 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
d288f65f 7219 pipe_config->dpll_hw_state.dpll_md = dpll_md;
bdd4b6a6
DV
7220}
7221
d288f65f 7222static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7223 const struct intel_crtc_state *pipe_config)
a0c4da24 7224{
f47709a9 7225 struct drm_device *dev = crtc->base.dev;
a0c4da24 7226 struct drm_i915_private *dev_priv = dev->dev_private;
f47709a9 7227 int pipe = crtc->pipe;
bdd4b6a6 7228 u32 mdiv;
a0c4da24 7229 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 7230 u32 coreclk, reg_val;
a0c4da24 7231
a580516d 7232 mutex_lock(&dev_priv->sb_lock);
09153000 7233
d288f65f
VS
7234 bestn = pipe_config->dpll.n;
7235 bestm1 = pipe_config->dpll.m1;
7236 bestm2 = pipe_config->dpll.m2;
7237 bestp1 = pipe_config->dpll.p1;
7238 bestp2 = pipe_config->dpll.p2;
a0c4da24 7239
89b667f8
JB
7240 /* See eDP HDMI DPIO driver vbios notes doc */
7241
7242 /* PLL B needs special handling */
bdd4b6a6 7243 if (pipe == PIPE_B)
5e69f97f 7244 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
7245
7246 /* Set up Tx target for periodic Rcomp update */
ab3c759a 7247 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
7248
7249 /* Disable target IRef on PLL */
ab3c759a 7250 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 7251 reg_val &= 0x00ffffff;
ab3c759a 7252 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
7253
7254 /* Disable fast lock */
ab3c759a 7255 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
7256
7257 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
7258 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7259 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7260 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 7261 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
7262
7263 /*
7264 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7265 * but we don't support that).
7266 * Note: don't use the DAC post divider as it seems unstable.
7267 */
7268 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 7269 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7270
a0c4da24 7271 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 7272 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7273
89b667f8 7274 /* Set HBR and RBR LPF coefficients */
d288f65f 7275 if (pipe_config->port_clock == 162000 ||
409ee761
ACO
7276 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7277 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
ab3c759a 7278 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 7279 0x009f0003);
89b667f8 7280 else
ab3c759a 7281 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
7282 0x00d0000f);
7283
681a8504 7284 if (pipe_config->has_dp_encoder) {
89b667f8 7285 /* Use SSC source */
bdd4b6a6 7286 if (pipe == PIPE_A)
ab3c759a 7287 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7288 0x0df40000);
7289 else
ab3c759a 7290 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7291 0x0df70000);
7292 } else { /* HDMI or VGA */
7293 /* Use bend source */
bdd4b6a6 7294 if (pipe == PIPE_A)
ab3c759a 7295 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7296 0x0df70000);
7297 else
ab3c759a 7298 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7299 0x0df40000);
7300 }
a0c4da24 7301
ab3c759a 7302 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8 7303 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
409ee761
ACO
7304 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7305 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
89b667f8 7306 coreclk |= 0x01000000;
ab3c759a 7307 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 7308
ab3c759a 7309 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
a580516d 7310 mutex_unlock(&dev_priv->sb_lock);
a0c4da24
JB
7311}
7312
251ac862
DV
7313static void chv_compute_dpll(struct intel_crtc *crtc,
7314 struct intel_crtc_state *pipe_config)
1ae0d137 7315{
60bfe44f
VS
7316 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
7317 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
1ae0d137
VS
7318 DPLL_VCO_ENABLE;
7319 if (crtc->pipe != PIPE_A)
d288f65f 7320 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
1ae0d137 7321
d288f65f
VS
7322 pipe_config->dpll_hw_state.dpll_md =
7323 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
1ae0d137
VS
7324}
7325
d288f65f 7326static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7327 const struct intel_crtc_state *pipe_config)
9d556c99
CML
7328{
7329 struct drm_device *dev = crtc->base.dev;
7330 struct drm_i915_private *dev_priv = dev->dev_private;
7331 int pipe = crtc->pipe;
f0f59a00 7332 i915_reg_t dpll_reg = DPLL(crtc->pipe);
9d556c99 7333 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9cbe40c1 7334 u32 loopfilter, tribuf_calcntr;
9d556c99 7335 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
a945ce7e 7336 u32 dpio_val;
9cbe40c1 7337 int vco;
9d556c99 7338
d288f65f
VS
7339 bestn = pipe_config->dpll.n;
7340 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7341 bestm1 = pipe_config->dpll.m1;
7342 bestm2 = pipe_config->dpll.m2 >> 22;
7343 bestp1 = pipe_config->dpll.p1;
7344 bestp2 = pipe_config->dpll.p2;
9cbe40c1 7345 vco = pipe_config->dpll.vco;
a945ce7e 7346 dpio_val = 0;
9cbe40c1 7347 loopfilter = 0;
9d556c99
CML
7348
7349 /*
7350 * Enable Refclk and SSC
7351 */
a11b0703 7352 I915_WRITE(dpll_reg,
d288f65f 7353 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
a11b0703 7354
a580516d 7355 mutex_lock(&dev_priv->sb_lock);
9d556c99 7356
9d556c99
CML
7357 /* p1 and p2 divider */
7358 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7359 5 << DPIO_CHV_S1_DIV_SHIFT |
7360 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7361 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7362 1 << DPIO_CHV_K_DIV_SHIFT);
7363
7364 /* Feedback post-divider - m2 */
7365 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7366
7367 /* Feedback refclk divider - n and m1 */
7368 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7369 DPIO_CHV_M1_DIV_BY_2 |
7370 1 << DPIO_CHV_N_DIV_SHIFT);
7371
7372 /* M2 fraction division */
25a25dfc 7373 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
9d556c99
CML
7374
7375 /* M2 fraction division enable */
a945ce7e
VP
7376 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7377 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7378 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7379 if (bestm2_frac)
7380 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7381 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
9d556c99 7382
de3a0fde
VP
7383 /* Program digital lock detect threshold */
7384 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7385 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7386 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7387 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7388 if (!bestm2_frac)
7389 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7390 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7391
9d556c99 7392 /* Loop filter */
9cbe40c1
VP
7393 if (vco == 5400000) {
7394 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7395 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7396 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7397 tribuf_calcntr = 0x9;
7398 } else if (vco <= 6200000) {
7399 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7400 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7401 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7402 tribuf_calcntr = 0x9;
7403 } else if (vco <= 6480000) {
7404 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7405 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7406 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7407 tribuf_calcntr = 0x8;
7408 } else {
7409 /* Not supported. Apply the same limits as in the max case */
7410 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7411 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7412 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7413 tribuf_calcntr = 0;
7414 }
9d556c99
CML
7415 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7416
968040b2 7417 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
9cbe40c1
VP
7418 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7419 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7420 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7421
9d556c99
CML
7422 /* AFC Recal */
7423 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7424 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7425 DPIO_AFC_RECAL);
7426
a580516d 7427 mutex_unlock(&dev_priv->sb_lock);
9d556c99
CML
7428}
7429
d288f65f
VS
7430/**
7431 * vlv_force_pll_on - forcibly enable just the PLL
7432 * @dev_priv: i915 private structure
7433 * @pipe: pipe PLL to enable
7434 * @dpll: PLL configuration
7435 *
7436 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7437 * in cases where we need the PLL enabled even when @pipe is not going to
7438 * be enabled.
7439 */
3f36b937
TU
7440int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7441 const struct dpll *dpll)
d288f65f
VS
7442{
7443 struct intel_crtc *crtc =
7444 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
3f36b937
TU
7445 struct intel_crtc_state *pipe_config;
7446
7447 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7448 if (!pipe_config)
7449 return -ENOMEM;
7450
7451 pipe_config->base.crtc = &crtc->base;
7452 pipe_config->pixel_multiplier = 1;
7453 pipe_config->dpll = *dpll;
d288f65f
VS
7454
7455 if (IS_CHERRYVIEW(dev)) {
3f36b937
TU
7456 chv_compute_dpll(crtc, pipe_config);
7457 chv_prepare_pll(crtc, pipe_config);
7458 chv_enable_pll(crtc, pipe_config);
d288f65f 7459 } else {
3f36b937
TU
7460 vlv_compute_dpll(crtc, pipe_config);
7461 vlv_prepare_pll(crtc, pipe_config);
7462 vlv_enable_pll(crtc, pipe_config);
d288f65f 7463 }
3f36b937
TU
7464
7465 kfree(pipe_config);
7466
7467 return 0;
d288f65f
VS
7468}
7469
7470/**
7471 * vlv_force_pll_off - forcibly disable just the PLL
7472 * @dev_priv: i915 private structure
7473 * @pipe: pipe PLL to disable
7474 *
7475 * Disable the PLL for @pipe. To be used in cases where we need
7476 * the PLL enabled even when @pipe is not going to be enabled.
7477 */
7478void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7479{
7480 if (IS_CHERRYVIEW(dev))
7481 chv_disable_pll(to_i915(dev), pipe);
7482 else
7483 vlv_disable_pll(to_i915(dev), pipe);
7484}
7485
251ac862
DV
7486static void i9xx_compute_dpll(struct intel_crtc *crtc,
7487 struct intel_crtc_state *crtc_state,
ceb41007 7488 intel_clock_t *reduced_clock)
eb1cbe48 7489{
f47709a9 7490 struct drm_device *dev = crtc->base.dev;
eb1cbe48 7491 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48
DV
7492 u32 dpll;
7493 bool is_sdvo;
190f68c5 7494 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7495
190f68c5 7496 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7497
a93e255f
ACO
7498 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7499 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
eb1cbe48
DV
7500
7501 dpll = DPLL_VGA_MODE_DIS;
7502
a93e255f 7503 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
7504 dpll |= DPLLB_MODE_LVDS;
7505 else
7506 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 7507
ef1b460d 7508 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
190f68c5 7509 dpll |= (crtc_state->pixel_multiplier - 1)
198a037f 7510 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 7511 }
198a037f
DV
7512
7513 if (is_sdvo)
4a33e48d 7514 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 7515
190f68c5 7516 if (crtc_state->has_dp_encoder)
4a33e48d 7517 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
7518
7519 /* compute bitmask from p1 value */
7520 if (IS_PINEVIEW(dev))
7521 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7522 else {
7523 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7524 if (IS_G4X(dev) && reduced_clock)
7525 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7526 }
7527 switch (clock->p2) {
7528 case 5:
7529 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7530 break;
7531 case 7:
7532 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7533 break;
7534 case 10:
7535 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7536 break;
7537 case 14:
7538 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7539 break;
7540 }
7541 if (INTEL_INFO(dev)->gen >= 4)
7542 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7543
190f68c5 7544 if (crtc_state->sdvo_tv_clock)
eb1cbe48 7545 dpll |= PLL_REF_INPUT_TVCLKINBC;
a93e255f 7546 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 7547 intel_panel_use_ssc(dev_priv))
eb1cbe48
DV
7548 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7549 else
7550 dpll |= PLL_REF_INPUT_DREFCLK;
7551
7552 dpll |= DPLL_VCO_ENABLE;
190f68c5 7553 crtc_state->dpll_hw_state.dpll = dpll;
8bcc2795 7554
eb1cbe48 7555 if (INTEL_INFO(dev)->gen >= 4) {
190f68c5 7556 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
ef1b460d 7557 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
190f68c5 7558 crtc_state->dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
DV
7559 }
7560}
7561
251ac862
DV
7562static void i8xx_compute_dpll(struct intel_crtc *crtc,
7563 struct intel_crtc_state *crtc_state,
ceb41007 7564 intel_clock_t *reduced_clock)
eb1cbe48 7565{
f47709a9 7566 struct drm_device *dev = crtc->base.dev;
eb1cbe48 7567 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48 7568 u32 dpll;
190f68c5 7569 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7570
190f68c5 7571 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7572
eb1cbe48
DV
7573 dpll = DPLL_VGA_MODE_DIS;
7574
a93e255f 7575 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
7576 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7577 } else {
7578 if (clock->p1 == 2)
7579 dpll |= PLL_P1_DIVIDE_BY_TWO;
7580 else
7581 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7582 if (clock->p2 == 4)
7583 dpll |= PLL_P2_DIVIDE_BY_4;
7584 }
7585
a93e255f 7586 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
4a33e48d
DV
7587 dpll |= DPLL_DVO_2X_MODE;
7588
a93e255f 7589 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 7590 intel_panel_use_ssc(dev_priv))
eb1cbe48
DV
7591 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7592 else
7593 dpll |= PLL_REF_INPUT_DREFCLK;
7594
7595 dpll |= DPLL_VCO_ENABLE;
190f68c5 7596 crtc_state->dpll_hw_state.dpll = dpll;
eb1cbe48
DV
7597}
7598
8a654f3b 7599static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c
PZ
7600{
7601 struct drm_device *dev = intel_crtc->base.dev;
7602 struct drm_i915_private *dev_priv = dev->dev_private;
7603 enum pipe pipe = intel_crtc->pipe;
6e3c9717 7604 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
7c5f93b0 7605 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
1caea6e9
VS
7606 uint32_t crtc_vtotal, crtc_vblank_end;
7607 int vsyncshift = 0;
4d8a62ea
DV
7608
7609 /* We need to be careful not to changed the adjusted mode, for otherwise
7610 * the hw state checker will get angry at the mismatch. */
7611 crtc_vtotal = adjusted_mode->crtc_vtotal;
7612 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 7613
609aeaca 7614 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 7615 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
7616 crtc_vtotal -= 1;
7617 crtc_vblank_end -= 1;
609aeaca 7618
409ee761 7619 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
609aeaca
VS
7620 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7621 else
7622 vsyncshift = adjusted_mode->crtc_hsync_start -
7623 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
7624 if (vsyncshift < 0)
7625 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
7626 }
7627
7628 if (INTEL_INFO(dev)->gen > 3)
fe2b8f9d 7629 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 7630
fe2b8f9d 7631 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
7632 (adjusted_mode->crtc_hdisplay - 1) |
7633 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 7634 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
7635 (adjusted_mode->crtc_hblank_start - 1) |
7636 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 7637 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
7638 (adjusted_mode->crtc_hsync_start - 1) |
7639 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7640
fe2b8f9d 7641 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 7642 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 7643 ((crtc_vtotal - 1) << 16));
fe2b8f9d 7644 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 7645 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 7646 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 7647 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
7648 (adjusted_mode->crtc_vsync_start - 1) |
7649 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7650
b5e508d4
PZ
7651 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7652 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7653 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7654 * bits. */
7655 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7656 (pipe == PIPE_B || pipe == PIPE_C))
7657 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7658
bc58be60
JN
7659}
7660
7661static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
7662{
7663 struct drm_device *dev = intel_crtc->base.dev;
7664 struct drm_i915_private *dev_priv = dev->dev_private;
7665 enum pipe pipe = intel_crtc->pipe;
7666
b0e77b9c
PZ
7667 /* pipesrc controls the size that is scaled from, which should
7668 * always be the user's requested size.
7669 */
7670 I915_WRITE(PIPESRC(pipe),
6e3c9717
ACO
7671 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7672 (intel_crtc->config->pipe_src_h - 1));
b0e77b9c
PZ
7673}
7674
1bd1bd80 7675static void intel_get_pipe_timings(struct intel_crtc *crtc,
5cec258b 7676 struct intel_crtc_state *pipe_config)
1bd1bd80
DV
7677{
7678 struct drm_device *dev = crtc->base.dev;
7679 struct drm_i915_private *dev_priv = dev->dev_private;
7680 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7681 uint32_t tmp;
7682
7683 tmp = I915_READ(HTOTAL(cpu_transcoder));
2d112de7
ACO
7684 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7685 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7686 tmp = I915_READ(HBLANK(cpu_transcoder));
2d112de7
ACO
7687 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7688 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7689 tmp = I915_READ(HSYNC(cpu_transcoder));
2d112de7
ACO
7690 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7691 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7692
7693 tmp = I915_READ(VTOTAL(cpu_transcoder));
2d112de7
ACO
7694 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7695 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7696 tmp = I915_READ(VBLANK(cpu_transcoder));
2d112de7
ACO
7697 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7698 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7699 tmp = I915_READ(VSYNC(cpu_transcoder));
2d112de7
ACO
7700 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7701 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7702
7703 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
2d112de7
ACO
7704 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7705 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7706 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
1bd1bd80 7707 }
bc58be60
JN
7708}
7709
7710static void intel_get_pipe_src_size(struct intel_crtc *crtc,
7711 struct intel_crtc_state *pipe_config)
7712{
7713 struct drm_device *dev = crtc->base.dev;
7714 struct drm_i915_private *dev_priv = dev->dev_private;
7715 u32 tmp;
1bd1bd80
DV
7716
7717 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
7718 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7719 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7720
2d112de7
ACO
7721 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7722 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
DV
7723}
7724
f6a83288 7725void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5cec258b 7726 struct intel_crtc_state *pipe_config)
babea61d 7727{
2d112de7
ACO
7728 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7729 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7730 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7731 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
babea61d 7732
2d112de7
ACO
7733 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7734 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7735 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7736 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
babea61d 7737
2d112de7 7738 mode->flags = pipe_config->base.adjusted_mode.flags;
cd13f5ab 7739 mode->type = DRM_MODE_TYPE_DRIVER;
babea61d 7740
2d112de7
ACO
7741 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7742 mode->flags |= pipe_config->base.adjusted_mode.flags;
cd13f5ab
ML
7743
7744 mode->hsync = drm_mode_hsync(mode);
7745 mode->vrefresh = drm_mode_vrefresh(mode);
7746 drm_mode_set_name(mode);
babea61d
JB
7747}
7748
84b046f3
DV
7749static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7750{
7751 struct drm_device *dev = intel_crtc->base.dev;
7752 struct drm_i915_private *dev_priv = dev->dev_private;
7753 uint32_t pipeconf;
7754
9f11a9e4 7755 pipeconf = 0;
84b046f3 7756
b6b5d049
VS
7757 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7758 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7759 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
67c72a12 7760
6e3c9717 7761 if (intel_crtc->config->double_wide)
cf532bb2 7762 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 7763
ff9ce46e 7764 /* only g4x and later have fancy bpc/dither controls */
666a4537 7765 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
ff9ce46e 7766 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6e3c9717 7767 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
ff9ce46e 7768 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 7769 PIPECONF_DITHER_TYPE_SP;
84b046f3 7770
6e3c9717 7771 switch (intel_crtc->config->pipe_bpp) {
ff9ce46e
DV
7772 case 18:
7773 pipeconf |= PIPECONF_6BPC;
7774 break;
7775 case 24:
7776 pipeconf |= PIPECONF_8BPC;
7777 break;
7778 case 30:
7779 pipeconf |= PIPECONF_10BPC;
7780 break;
7781 default:
7782 /* Case prevented by intel_choose_pipe_bpp_dither. */
7783 BUG();
84b046f3
DV
7784 }
7785 }
7786
7787 if (HAS_PIPE_CXSR(dev)) {
7788 if (intel_crtc->lowfreq_avail) {
7789 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7790 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7791 } else {
7792 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
84b046f3
DV
7793 }
7794 }
7795
6e3c9717 7796 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
efc2cfff 7797 if (INTEL_INFO(dev)->gen < 4 ||
409ee761 7798 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
efc2cfff
VS
7799 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7800 else
7801 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7802 } else
84b046f3
DV
7803 pipeconf |= PIPECONF_PROGRESSIVE;
7804
666a4537
WB
7805 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
7806 intel_crtc->config->limited_color_range)
9f11a9e4 7807 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 7808
84b046f3
DV
7809 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7810 POSTING_READ(PIPECONF(intel_crtc->pipe));
7811}
7812
190f68c5
ACO
7813static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7814 struct intel_crtc_state *crtc_state)
79e53945 7815{
c7653199 7816 struct drm_device *dev = crtc->base.dev;
79e53945 7817 struct drm_i915_private *dev_priv = dev->dev_private;
ceb41007 7818 int refclk;
c329a4ec 7819 bool ok;
d4906093 7820 const intel_limit_t *limit;
79e53945 7821
dd3cd74a
ACO
7822 memset(&crtc_state->dpll_hw_state, 0,
7823 sizeof(crtc_state->dpll_hw_state));
7824
a65347ba
JN
7825 if (crtc_state->has_dsi_encoder)
7826 return 0;
43565a06 7827
190f68c5 7828 if (!crtc_state->clock_set) {
ceb41007 7829 refclk = i9xx_get_refclk(crtc_state);
79e53945 7830
e9fd1c02
JN
7831 /*
7832 * Returns a set of divisors for the desired target clock with
7833 * the given refclk, or FALSE. The returned values represent
7834 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7835 * 2) / p1 / p2.
7836 */
a93e255f
ACO
7837 limit = intel_limit(crtc_state, refclk);
7838 ok = dev_priv->display.find_dpll(limit, crtc_state,
190f68c5 7839 crtc_state->port_clock,
364ee29d
ACO
7840 refclk, NULL,
7841 &crtc_state->dpll);
f2335330 7842 if (!ok) {
e9fd1c02
JN
7843 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7844 return -EINVAL;
7845 }
f47709a9 7846 }
7026d4ac 7847
e9fd1c02 7848 if (IS_GEN2(dev)) {
ceb41007 7849 i8xx_compute_dpll(crtc, crtc_state, NULL);
9d556c99 7850 } else if (IS_CHERRYVIEW(dev)) {
251ac862 7851 chv_compute_dpll(crtc, crtc_state);
e9fd1c02 7852 } else if (IS_VALLEYVIEW(dev)) {
251ac862 7853 vlv_compute_dpll(crtc, crtc_state);
e9fd1c02 7854 } else {
ceb41007 7855 i9xx_compute_dpll(crtc, crtc_state, NULL);
e9fd1c02 7856 }
79e53945 7857
c8f7a0db 7858 return 0;
f564048e
EA
7859}
7860
2fa2fe9a 7861static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5cec258b 7862 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
7863{
7864 struct drm_device *dev = crtc->base.dev;
7865 struct drm_i915_private *dev_priv = dev->dev_private;
7866 uint32_t tmp;
7867
dc9e7dec
VS
7868 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7869 return;
7870
2fa2fe9a 7871 tmp = I915_READ(PFIT_CONTROL);
06922821
DV
7872 if (!(tmp & PFIT_ENABLE))
7873 return;
2fa2fe9a 7874
06922821 7875 /* Check whether the pfit is attached to our pipe. */
2fa2fe9a
DV
7876 if (INTEL_INFO(dev)->gen < 4) {
7877 if (crtc->pipe != PIPE_B)
7878 return;
2fa2fe9a
DV
7879 } else {
7880 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7881 return;
7882 }
7883
06922821 7884 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a
DV
7885 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7886 if (INTEL_INFO(dev)->gen < 5)
7887 pipe_config->gmch_pfit.lvds_border_bits =
7888 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7889}
7890
acbec814 7891static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 7892 struct intel_crtc_state *pipe_config)
acbec814
JB
7893{
7894 struct drm_device *dev = crtc->base.dev;
7895 struct drm_i915_private *dev_priv = dev->dev_private;
7896 int pipe = pipe_config->cpu_transcoder;
7897 intel_clock_t clock;
7898 u32 mdiv;
662c6ecb 7899 int refclk = 100000;
acbec814 7900
f573de5a
SK
7901 /* In case of MIPI DPLL will not even be used */
7902 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
7903 return;
7904
a580516d 7905 mutex_lock(&dev_priv->sb_lock);
ab3c759a 7906 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
a580516d 7907 mutex_unlock(&dev_priv->sb_lock);
acbec814
JB
7908
7909 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7910 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7911 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7912 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7913 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7914
dccbea3b 7915 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
acbec814
JB
7916}
7917
5724dbd1
DL
7918static void
7919i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7920 struct intel_initial_plane_config *plane_config)
1ad292b5
JB
7921{
7922 struct drm_device *dev = crtc->base.dev;
7923 struct drm_i915_private *dev_priv = dev->dev_private;
7924 u32 val, base, offset;
7925 int pipe = crtc->pipe, plane = crtc->plane;
7926 int fourcc, pixel_format;
6761dd31 7927 unsigned int aligned_height;
b113d5ee 7928 struct drm_framebuffer *fb;
1b842c89 7929 struct intel_framebuffer *intel_fb;
1ad292b5 7930
42a7b088
DL
7931 val = I915_READ(DSPCNTR(plane));
7932 if (!(val & DISPLAY_PLANE_ENABLE))
7933 return;
7934
d9806c9f 7935 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 7936 if (!intel_fb) {
1ad292b5
JB
7937 DRM_DEBUG_KMS("failed to alloc fb\n");
7938 return;
7939 }
7940
1b842c89
DL
7941 fb = &intel_fb->base;
7942
18c5247e
DV
7943 if (INTEL_INFO(dev)->gen >= 4) {
7944 if (val & DISPPLANE_TILED) {
49af449b 7945 plane_config->tiling = I915_TILING_X;
18c5247e
DV
7946 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
7947 }
7948 }
1ad292b5
JB
7949
7950 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 7951 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
7952 fb->pixel_format = fourcc;
7953 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
1ad292b5
JB
7954
7955 if (INTEL_INFO(dev)->gen >= 4) {
49af449b 7956 if (plane_config->tiling)
1ad292b5
JB
7957 offset = I915_READ(DSPTILEOFF(plane));
7958 else
7959 offset = I915_READ(DSPLINOFF(plane));
7960 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7961 } else {
7962 base = I915_READ(DSPADDR(plane));
7963 }
7964 plane_config->base = base;
7965
7966 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
7967 fb->width = ((val >> 16) & 0xfff) + 1;
7968 fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
7969
7970 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 7971 fb->pitches[0] = val & 0xffffffc0;
1ad292b5 7972
b113d5ee 7973 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
7974 fb->pixel_format,
7975 fb->modifier[0]);
1ad292b5 7976
f37b5c2b 7977 plane_config->size = fb->pitches[0] * aligned_height;
1ad292b5 7978
2844a921
DL
7979 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7980 pipe_name(pipe), plane, fb->width, fb->height,
7981 fb->bits_per_pixel, base, fb->pitches[0],
7982 plane_config->size);
1ad292b5 7983
2d14030b 7984 plane_config->fb = intel_fb;
1ad292b5
JB
7985}
7986
70b23a98 7987static void chv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 7988 struct intel_crtc_state *pipe_config)
70b23a98
VS
7989{
7990 struct drm_device *dev = crtc->base.dev;
7991 struct drm_i915_private *dev_priv = dev->dev_private;
7992 int pipe = pipe_config->cpu_transcoder;
7993 enum dpio_channel port = vlv_pipe_to_channel(pipe);
7994 intel_clock_t clock;
0d7b6b11 7995 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
70b23a98
VS
7996 int refclk = 100000;
7997
a580516d 7998 mutex_lock(&dev_priv->sb_lock);
70b23a98
VS
7999 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8000 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8001 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8002 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
0d7b6b11 8003 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
a580516d 8004 mutex_unlock(&dev_priv->sb_lock);
70b23a98
VS
8005
8006 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
0d7b6b11
ID
8007 clock.m2 = (pll_dw0 & 0xff) << 22;
8008 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8009 clock.m2 |= pll_dw2 & 0x3fffff;
70b23a98
VS
8010 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8011 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8012 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8013
dccbea3b 8014 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
70b23a98
VS
8015}
8016
0e8ffe1b 8017static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5cec258b 8018 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
8019{
8020 struct drm_device *dev = crtc->base.dev;
8021 struct drm_i915_private *dev_priv = dev->dev_private;
1729050e 8022 enum intel_display_power_domain power_domain;
0e8ffe1b 8023 uint32_t tmp;
1729050e 8024 bool ret;
0e8ffe1b 8025
1729050e
ID
8026 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8027 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0
ID
8028 return false;
8029
e143a21c 8030 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 8031 pipe_config->shared_dpll = NULL;
eccb140b 8032
1729050e
ID
8033 ret = false;
8034
0e8ffe1b
DV
8035 tmp = I915_READ(PIPECONF(crtc->pipe));
8036 if (!(tmp & PIPECONF_ENABLE))
1729050e 8037 goto out;
0e8ffe1b 8038
666a4537 8039 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
42571aef
VS
8040 switch (tmp & PIPECONF_BPC_MASK) {
8041 case PIPECONF_6BPC:
8042 pipe_config->pipe_bpp = 18;
8043 break;
8044 case PIPECONF_8BPC:
8045 pipe_config->pipe_bpp = 24;
8046 break;
8047 case PIPECONF_10BPC:
8048 pipe_config->pipe_bpp = 30;
8049 break;
8050 default:
8051 break;
8052 }
8053 }
8054
666a4537
WB
8055 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
8056 (tmp & PIPECONF_COLOR_RANGE_SELECT))
b5a9fa09
DV
8057 pipe_config->limited_color_range = true;
8058
282740f7
VS
8059 if (INTEL_INFO(dev)->gen < 4)
8060 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8061
1bd1bd80 8062 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 8063 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 8064
2fa2fe9a
DV
8065 i9xx_get_pfit_config(crtc, pipe_config);
8066
6c49f241
DV
8067 if (INTEL_INFO(dev)->gen >= 4) {
8068 tmp = I915_READ(DPLL_MD(crtc->pipe));
8069 pipe_config->pixel_multiplier =
8070 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8071 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 8072 pipe_config->dpll_hw_state.dpll_md = tmp;
6c49f241
DV
8073 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8074 tmp = I915_READ(DPLL(crtc->pipe));
8075 pipe_config->pixel_multiplier =
8076 ((tmp & SDVO_MULTIPLIER_MASK)
8077 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8078 } else {
8079 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8080 * port and will be fixed up in the encoder->get_config
8081 * function. */
8082 pipe_config->pixel_multiplier = 1;
8083 }
8bcc2795 8084 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
666a4537 8085 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
1c4e0274
VS
8086 /*
8087 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8088 * on 830. Filter it out here so that we don't
8089 * report errors due to that.
8090 */
8091 if (IS_I830(dev))
8092 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8093
8bcc2795
DV
8094 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8095 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
8096 } else {
8097 /* Mask out read-only status bits. */
8098 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8099 DPLL_PORTC_READY_MASK |
8100 DPLL_PORTB_READY_MASK);
8bcc2795 8101 }
6c49f241 8102
70b23a98
VS
8103 if (IS_CHERRYVIEW(dev))
8104 chv_crtc_clock_get(crtc, pipe_config);
8105 else if (IS_VALLEYVIEW(dev))
acbec814
JB
8106 vlv_crtc_clock_get(crtc, pipe_config);
8107 else
8108 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 8109
0f64614d
VS
8110 /*
8111 * Normally the dotclock is filled in by the encoder .get_config()
8112 * but in case the pipe is enabled w/o any ports we need a sane
8113 * default.
8114 */
8115 pipe_config->base.adjusted_mode.crtc_clock =
8116 pipe_config->port_clock / pipe_config->pixel_multiplier;
8117
1729050e
ID
8118 ret = true;
8119
8120out:
8121 intel_display_power_put(dev_priv, power_domain);
8122
8123 return ret;
0e8ffe1b
DV
8124}
8125
dde86e2d 8126static void ironlake_init_pch_refclk(struct drm_device *dev)
13d83a67
JB
8127{
8128 struct drm_i915_private *dev_priv = dev->dev_private;
13d83a67 8129 struct intel_encoder *encoder;
74cfd7ac 8130 u32 val, final;
13d83a67 8131 bool has_lvds = false;
199e5d79 8132 bool has_cpu_edp = false;
199e5d79 8133 bool has_panel = false;
99eb6a01
KP
8134 bool has_ck505 = false;
8135 bool can_ssc = false;
13d83a67
JB
8136
8137 /* We need to take the global config into account */
b2784e15 8138 for_each_intel_encoder(dev, encoder) {
199e5d79
KP
8139 switch (encoder->type) {
8140 case INTEL_OUTPUT_LVDS:
8141 has_panel = true;
8142 has_lvds = true;
8143 break;
8144 case INTEL_OUTPUT_EDP:
8145 has_panel = true;
2de6905f 8146 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
8147 has_cpu_edp = true;
8148 break;
6847d71b
PZ
8149 default:
8150 break;
13d83a67
JB
8151 }
8152 }
8153
99eb6a01 8154 if (HAS_PCH_IBX(dev)) {
41aa3448 8155 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
8156 can_ssc = has_ck505;
8157 } else {
8158 has_ck505 = false;
8159 can_ssc = true;
8160 }
8161
2de6905f
ID
8162 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8163 has_panel, has_lvds, has_ck505);
13d83a67
JB
8164
8165 /* Ironlake: try to setup display ref clock before DPLL
8166 * enabling. This is only under driver's control after
8167 * PCH B stepping, previous chipset stepping should be
8168 * ignoring this setting.
8169 */
74cfd7ac
CW
8170 val = I915_READ(PCH_DREF_CONTROL);
8171
8172 /* As we must carefully and slowly disable/enable each source in turn,
8173 * compute the final state we want first and check if we need to
8174 * make any changes at all.
8175 */
8176 final = val;
8177 final &= ~DREF_NONSPREAD_SOURCE_MASK;
8178 if (has_ck505)
8179 final |= DREF_NONSPREAD_CK505_ENABLE;
8180 else
8181 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8182
8183 final &= ~DREF_SSC_SOURCE_MASK;
8184 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8185 final &= ~DREF_SSC1_ENABLE;
8186
8187 if (has_panel) {
8188 final |= DREF_SSC_SOURCE_ENABLE;
8189
8190 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8191 final |= DREF_SSC1_ENABLE;
8192
8193 if (has_cpu_edp) {
8194 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8195 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8196 else
8197 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8198 } else
8199 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8200 } else {
8201 final |= DREF_SSC_SOURCE_DISABLE;
8202 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8203 }
8204
8205 if (final == val)
8206 return;
8207
13d83a67 8208 /* Always enable nonspread source */
74cfd7ac 8209 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 8210
99eb6a01 8211 if (has_ck505)
74cfd7ac 8212 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 8213 else
74cfd7ac 8214 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 8215
199e5d79 8216 if (has_panel) {
74cfd7ac
CW
8217 val &= ~DREF_SSC_SOURCE_MASK;
8218 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 8219
199e5d79 8220 /* SSC must be turned on before enabling the CPU output */
99eb6a01 8221 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 8222 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 8223 val |= DREF_SSC1_ENABLE;
e77166b5 8224 } else
74cfd7ac 8225 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
8226
8227 /* Get SSC going before enabling the outputs */
74cfd7ac 8228 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8229 POSTING_READ(PCH_DREF_CONTROL);
8230 udelay(200);
8231
74cfd7ac 8232 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
8233
8234 /* Enable CPU source on CPU attached eDP */
199e5d79 8235 if (has_cpu_edp) {
99eb6a01 8236 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 8237 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 8238 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 8239 } else
74cfd7ac 8240 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 8241 } else
74cfd7ac 8242 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 8243
74cfd7ac 8244 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8245 POSTING_READ(PCH_DREF_CONTROL);
8246 udelay(200);
8247 } else {
8248 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8249
74cfd7ac 8250 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
8251
8252 /* Turn off CPU output */
74cfd7ac 8253 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 8254
74cfd7ac 8255 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8256 POSTING_READ(PCH_DREF_CONTROL);
8257 udelay(200);
8258
8259 /* Turn off the SSC source */
74cfd7ac
CW
8260 val &= ~DREF_SSC_SOURCE_MASK;
8261 val |= DREF_SSC_SOURCE_DISABLE;
199e5d79
KP
8262
8263 /* Turn off SSC1 */
74cfd7ac 8264 val &= ~DREF_SSC1_ENABLE;
199e5d79 8265
74cfd7ac 8266 I915_WRITE(PCH_DREF_CONTROL, val);
13d83a67
JB
8267 POSTING_READ(PCH_DREF_CONTROL);
8268 udelay(200);
8269 }
74cfd7ac
CW
8270
8271 BUG_ON(val != final);
13d83a67
JB
8272}
8273
f31f2d55 8274static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 8275{
f31f2d55 8276 uint32_t tmp;
dde86e2d 8277
0ff066a9
PZ
8278 tmp = I915_READ(SOUTH_CHICKEN2);
8279 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8280 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 8281
0ff066a9
PZ
8282 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8283 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8284 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 8285
0ff066a9
PZ
8286 tmp = I915_READ(SOUTH_CHICKEN2);
8287 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8288 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 8289
0ff066a9
PZ
8290 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8291 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8292 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
8293}
8294
8295/* WaMPhyProgramming:hsw */
8296static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8297{
8298 uint32_t tmp;
dde86e2d
PZ
8299
8300 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8301 tmp &= ~(0xFF << 24);
8302 tmp |= (0x12 << 24);
8303 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8304
dde86e2d
PZ
8305 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8306 tmp |= (1 << 11);
8307 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8308
8309 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8310 tmp |= (1 << 11);
8311 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8312
dde86e2d
PZ
8313 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8314 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8315 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8316
8317 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8318 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8319 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8320
0ff066a9
PZ
8321 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8322 tmp &= ~(7 << 13);
8323 tmp |= (5 << 13);
8324 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 8325
0ff066a9
PZ
8326 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8327 tmp &= ~(7 << 13);
8328 tmp |= (5 << 13);
8329 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
8330
8331 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8332 tmp &= ~0xFF;
8333 tmp |= 0x1C;
8334 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8335
8336 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8337 tmp &= ~0xFF;
8338 tmp |= 0x1C;
8339 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8340
8341 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8342 tmp &= ~(0xFF << 16);
8343 tmp |= (0x1C << 16);
8344 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8345
8346 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8347 tmp &= ~(0xFF << 16);
8348 tmp |= (0x1C << 16);
8349 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8350
0ff066a9
PZ
8351 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8352 tmp |= (1 << 27);
8353 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 8354
0ff066a9
PZ
8355 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8356 tmp |= (1 << 27);
8357 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 8358
0ff066a9
PZ
8359 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8360 tmp &= ~(0xF << 28);
8361 tmp |= (4 << 28);
8362 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 8363
0ff066a9
PZ
8364 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8365 tmp &= ~(0xF << 28);
8366 tmp |= (4 << 28);
8367 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
8368}
8369
2fa86a1f
PZ
8370/* Implements 3 different sequences from BSpec chapter "Display iCLK
8371 * Programming" based on the parameters passed:
8372 * - Sequence to enable CLKOUT_DP
8373 * - Sequence to enable CLKOUT_DP without spread
8374 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8375 */
8376static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8377 bool with_fdi)
f31f2d55
PZ
8378{
8379 struct drm_i915_private *dev_priv = dev->dev_private;
2fa86a1f
PZ
8380 uint32_t reg, tmp;
8381
8382 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8383 with_spread = true;
c2699524 8384 if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
2fa86a1f 8385 with_fdi = false;
f31f2d55 8386
a580516d 8387 mutex_lock(&dev_priv->sb_lock);
f31f2d55
PZ
8388
8389 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8390 tmp &= ~SBI_SSCCTL_DISABLE;
8391 tmp |= SBI_SSCCTL_PATHALT;
8392 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8393
8394 udelay(24);
8395
2fa86a1f
PZ
8396 if (with_spread) {
8397 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8398 tmp &= ~SBI_SSCCTL_PATHALT;
8399 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 8400
2fa86a1f
PZ
8401 if (with_fdi) {
8402 lpt_reset_fdi_mphy(dev_priv);
8403 lpt_program_fdi_mphy(dev_priv);
8404 }
8405 }
dde86e2d 8406
c2699524 8407 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
2fa86a1f
PZ
8408 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8409 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8410 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246 8411
a580516d 8412 mutex_unlock(&dev_priv->sb_lock);
dde86e2d
PZ
8413}
8414
47701c3b
PZ
8415/* Sequence to disable CLKOUT_DP */
8416static void lpt_disable_clkout_dp(struct drm_device *dev)
8417{
8418 struct drm_i915_private *dev_priv = dev->dev_private;
8419 uint32_t reg, tmp;
8420
a580516d 8421 mutex_lock(&dev_priv->sb_lock);
47701c3b 8422
c2699524 8423 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
47701c3b
PZ
8424 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8425 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8426 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8427
8428 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8429 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8430 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8431 tmp |= SBI_SSCCTL_PATHALT;
8432 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8433 udelay(32);
8434 }
8435 tmp |= SBI_SSCCTL_DISABLE;
8436 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8437 }
8438
a580516d 8439 mutex_unlock(&dev_priv->sb_lock);
47701c3b
PZ
8440}
8441
f7be2c21
VS
8442#define BEND_IDX(steps) ((50 + (steps)) / 5)
8443
8444static const uint16_t sscdivintphase[] = {
8445 [BEND_IDX( 50)] = 0x3B23,
8446 [BEND_IDX( 45)] = 0x3B23,
8447 [BEND_IDX( 40)] = 0x3C23,
8448 [BEND_IDX( 35)] = 0x3C23,
8449 [BEND_IDX( 30)] = 0x3D23,
8450 [BEND_IDX( 25)] = 0x3D23,
8451 [BEND_IDX( 20)] = 0x3E23,
8452 [BEND_IDX( 15)] = 0x3E23,
8453 [BEND_IDX( 10)] = 0x3F23,
8454 [BEND_IDX( 5)] = 0x3F23,
8455 [BEND_IDX( 0)] = 0x0025,
8456 [BEND_IDX( -5)] = 0x0025,
8457 [BEND_IDX(-10)] = 0x0125,
8458 [BEND_IDX(-15)] = 0x0125,
8459 [BEND_IDX(-20)] = 0x0225,
8460 [BEND_IDX(-25)] = 0x0225,
8461 [BEND_IDX(-30)] = 0x0325,
8462 [BEND_IDX(-35)] = 0x0325,
8463 [BEND_IDX(-40)] = 0x0425,
8464 [BEND_IDX(-45)] = 0x0425,
8465 [BEND_IDX(-50)] = 0x0525,
8466};
8467
8468/*
8469 * Bend CLKOUT_DP
8470 * steps -50 to 50 inclusive, in steps of 5
8471 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
8472 * change in clock period = -(steps / 10) * 5.787 ps
8473 */
8474static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
8475{
8476 uint32_t tmp;
8477 int idx = BEND_IDX(steps);
8478
8479 if (WARN_ON(steps % 5 != 0))
8480 return;
8481
8482 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
8483 return;
8484
8485 mutex_lock(&dev_priv->sb_lock);
8486
8487 if (steps % 10 != 0)
8488 tmp = 0xAAAAAAAB;
8489 else
8490 tmp = 0x00000000;
8491 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
8492
8493 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
8494 tmp &= 0xffff0000;
8495 tmp |= sscdivintphase[idx];
8496 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
8497
8498 mutex_unlock(&dev_priv->sb_lock);
8499}
8500
8501#undef BEND_IDX
8502
bf8fa3d3
PZ
8503static void lpt_init_pch_refclk(struct drm_device *dev)
8504{
bf8fa3d3
PZ
8505 struct intel_encoder *encoder;
8506 bool has_vga = false;
8507
b2784e15 8508 for_each_intel_encoder(dev, encoder) {
bf8fa3d3
PZ
8509 switch (encoder->type) {
8510 case INTEL_OUTPUT_ANALOG:
8511 has_vga = true;
8512 break;
6847d71b
PZ
8513 default:
8514 break;
bf8fa3d3
PZ
8515 }
8516 }
8517
f7be2c21
VS
8518 if (has_vga) {
8519 lpt_bend_clkout_dp(to_i915(dev), 0);
47701c3b 8520 lpt_enable_clkout_dp(dev, true, true);
f7be2c21 8521 } else {
47701c3b 8522 lpt_disable_clkout_dp(dev);
f7be2c21 8523 }
bf8fa3d3
PZ
8524}
8525
dde86e2d
PZ
8526/*
8527 * Initialize reference clocks when the driver loads
8528 */
8529void intel_init_pch_refclk(struct drm_device *dev)
8530{
8531 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8532 ironlake_init_pch_refclk(dev);
8533 else if (HAS_PCH_LPT(dev))
8534 lpt_init_pch_refclk(dev);
8535}
8536
6ff93609 8537static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 8538{
c8203565 8539 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
79e53945
JB
8540 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8541 int pipe = intel_crtc->pipe;
c8203565
PZ
8542 uint32_t val;
8543
78114071 8544 val = 0;
c8203565 8545
6e3c9717 8546 switch (intel_crtc->config->pipe_bpp) {
c8203565 8547 case 18:
dfd07d72 8548 val |= PIPECONF_6BPC;
c8203565
PZ
8549 break;
8550 case 24:
dfd07d72 8551 val |= PIPECONF_8BPC;
c8203565
PZ
8552 break;
8553 case 30:
dfd07d72 8554 val |= PIPECONF_10BPC;
c8203565
PZ
8555 break;
8556 case 36:
dfd07d72 8557 val |= PIPECONF_12BPC;
c8203565
PZ
8558 break;
8559 default:
cc769b62
PZ
8560 /* Case prevented by intel_choose_pipe_bpp_dither. */
8561 BUG();
c8203565
PZ
8562 }
8563
6e3c9717 8564 if (intel_crtc->config->dither)
c8203565
PZ
8565 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8566
6e3c9717 8567 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
8568 val |= PIPECONF_INTERLACED_ILK;
8569 else
8570 val |= PIPECONF_PROGRESSIVE;
8571
6e3c9717 8572 if (intel_crtc->config->limited_color_range)
3685a8f3 8573 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 8574
c8203565
PZ
8575 I915_WRITE(PIPECONF(pipe), val);
8576 POSTING_READ(PIPECONF(pipe));
8577}
8578
6ff93609 8579static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 8580{
391bf048 8581 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
ee2b0b38 8582 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 8583 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
391bf048 8584 u32 val = 0;
ee2b0b38 8585
391bf048 8586 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
ee2b0b38
PZ
8587 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8588
6e3c9717 8589 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
8590 val |= PIPECONF_INTERLACED_ILK;
8591 else
8592 val |= PIPECONF_PROGRESSIVE;
8593
702e7a56
PZ
8594 I915_WRITE(PIPECONF(cpu_transcoder), val);
8595 POSTING_READ(PIPECONF(cpu_transcoder));
391bf048
JN
8596}
8597
391bf048
JN
8598static void haswell_set_pipemisc(struct drm_crtc *crtc)
8599{
8600 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8601 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756f85cf 8602
391bf048
JN
8603 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
8604 u32 val = 0;
756f85cf 8605
6e3c9717 8606 switch (intel_crtc->config->pipe_bpp) {
756f85cf
PZ
8607 case 18:
8608 val |= PIPEMISC_DITHER_6_BPC;
8609 break;
8610 case 24:
8611 val |= PIPEMISC_DITHER_8_BPC;
8612 break;
8613 case 30:
8614 val |= PIPEMISC_DITHER_10_BPC;
8615 break;
8616 case 36:
8617 val |= PIPEMISC_DITHER_12_BPC;
8618 break;
8619 default:
8620 /* Case prevented by pipe_config_set_bpp. */
8621 BUG();
8622 }
8623
6e3c9717 8624 if (intel_crtc->config->dither)
756f85cf
PZ
8625 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8626
391bf048 8627 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
756f85cf 8628 }
ee2b0b38
PZ
8629}
8630
6591c6e4 8631static bool ironlake_compute_clocks(struct drm_crtc *crtc,
190f68c5 8632 struct intel_crtc_state *crtc_state,
6591c6e4
PZ
8633 intel_clock_t *clock,
8634 bool *has_reduced_clock,
8635 intel_clock_t *reduced_clock)
8636{
8637 struct drm_device *dev = crtc->dev;
8638 struct drm_i915_private *dev_priv = dev->dev_private;
6591c6e4 8639 int refclk;
d4906093 8640 const intel_limit_t *limit;
c329a4ec 8641 bool ret;
79e53945 8642
8f0d5b9b
ACO
8643 refclk = 120000;
8644
8645 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8646 if (intel_panel_use_ssc(dev_priv)) {
8647 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
8648 dev_priv->vbt.lvds_ssc_freq);
8649 refclk = dev_priv->vbt.lvds_ssc_freq;
8650 }
8651
8652 if (intel_is_dual_link_lvds(dev)) {
8653 if (refclk == 100000)
8654 limit = &intel_limits_ironlake_dual_lvds_100m;
8655 else
8656 limit = &intel_limits_ironlake_dual_lvds;
8657 } else {
8658 if (refclk == 100000)
8659 limit = &intel_limits_ironlake_single_lvds_100m;
8660 else
8661 limit = &intel_limits_ironlake_single_lvds;
8662 }
26ce6d59 8663 } else {
8f0d5b9b 8664 limit = &intel_limits_ironlake_dac;
26ce6d59 8665 }
79e53945 8666
d4906093
ML
8667 /*
8668 * Returns a set of divisors for the desired target clock with the given
8669 * refclk, or FALSE. The returned values represent the clock equation:
8670 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
8671 */
2d7feacc
ACO
8672 ret = g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8673 refclk, NULL, clock);
6591c6e4
PZ
8674 if (!ret)
8675 return false;
cda4b7d3 8676
6591c6e4
PZ
8677 return true;
8678}
8679
d4b1931c
PZ
8680int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8681{
8682 /*
8683 * Account for spread spectrum to avoid
8684 * oversubscribing the link. Max center spread
8685 * is 2.5%; use 5% for safety's sake.
8686 */
8687 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 8688 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
8689}
8690
7429e9d4 8691static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 8692{
7429e9d4 8693 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
8694}
8695
b75ca6f6
ACO
8696static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
8697 struct intel_crtc_state *crtc_state,
8698 intel_clock_t *reduced_clock)
79e53945 8699{
de13a2e3 8700 struct drm_crtc *crtc = &intel_crtc->base;
79e53945
JB
8701 struct drm_device *dev = crtc->dev;
8702 struct drm_i915_private *dev_priv = dev->dev_private;
55bb9992 8703 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 8704 struct drm_connector *connector;
55bb9992
ACO
8705 struct drm_connector_state *connector_state;
8706 struct intel_encoder *encoder;
b75ca6f6 8707 u32 dpll, fp, fp2;
ceb41007 8708 int factor, i;
09ede541 8709 bool is_lvds = false, is_sdvo = false;
79e53945 8710
da3ced29 8711 for_each_connector_in_state(state, connector, connector_state, i) {
55bb9992
ACO
8712 if (connector_state->crtc != crtc_state->base.crtc)
8713 continue;
8714
8715 encoder = to_intel_encoder(connector_state->best_encoder);
8716
8717 switch (encoder->type) {
79e53945
JB
8718 case INTEL_OUTPUT_LVDS:
8719 is_lvds = true;
8720 break;
8721 case INTEL_OUTPUT_SDVO:
7d57382e 8722 case INTEL_OUTPUT_HDMI:
79e53945 8723 is_sdvo = true;
79e53945 8724 break;
6847d71b
PZ
8725 default:
8726 break;
79e53945
JB
8727 }
8728 }
79e53945 8729
c1858123 8730 /* Enable autotuning of the PLL clock (if permissible) */
8febb297
EA
8731 factor = 21;
8732 if (is_lvds) {
8733 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 8734 dev_priv->vbt.lvds_ssc_freq == 100000) ||
f0b44056 8735 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
8febb297 8736 factor = 25;
190f68c5 8737 } else if (crtc_state->sdvo_tv_clock)
8febb297 8738 factor = 20;
c1858123 8739
b75ca6f6
ACO
8740 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
8741
190f68c5 8742 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
b75ca6f6
ACO
8743 fp |= FP_CB_TUNE;
8744
8745 if (reduced_clock) {
8746 fp2 = i9xx_dpll_compute_fp(reduced_clock);
2c07245f 8747
b75ca6f6
ACO
8748 if (reduced_clock->m < factor * reduced_clock->n)
8749 fp2 |= FP_CB_TUNE;
8750 } else {
8751 fp2 = fp;
8752 }
9a7c7890 8753
5eddb70b 8754 dpll = 0;
2c07245f 8755
a07d6787
EA
8756 if (is_lvds)
8757 dpll |= DPLLB_MODE_LVDS;
8758 else
8759 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 8760
190f68c5 8761 dpll |= (crtc_state->pixel_multiplier - 1)
ef1b460d 8762 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f
DV
8763
8764 if (is_sdvo)
4a33e48d 8765 dpll |= DPLL_SDVO_HIGH_SPEED;
190f68c5 8766 if (crtc_state->has_dp_encoder)
4a33e48d 8767 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 8768
a07d6787 8769 /* compute bitmask from p1 value */
190f68c5 8770 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 8771 /* also FPA1 */
190f68c5 8772 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 8773
190f68c5 8774 switch (crtc_state->dpll.p2) {
a07d6787
EA
8775 case 5:
8776 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8777 break;
8778 case 7:
8779 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8780 break;
8781 case 10:
8782 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8783 break;
8784 case 14:
8785 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8786 break;
79e53945
JB
8787 }
8788
ceb41007 8789 if (is_lvds && intel_panel_use_ssc(dev_priv))
43565a06 8790 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
8791 else
8792 dpll |= PLL_REF_INPUT_DREFCLK;
8793
b75ca6f6
ACO
8794 dpll |= DPLL_VCO_ENABLE;
8795
8796 crtc_state->dpll_hw_state.dpll = dpll;
8797 crtc_state->dpll_hw_state.fp0 = fp;
8798 crtc_state->dpll_hw_state.fp1 = fp2;
de13a2e3
PZ
8799}
8800
190f68c5
ACO
8801static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8802 struct intel_crtc_state *crtc_state)
de13a2e3 8803{
364ee29d 8804 intel_clock_t reduced_clock;
7ed9f894 8805 bool has_reduced_clock = false;
e2b78267 8806 struct intel_shared_dpll *pll;
de13a2e3 8807
dd3cd74a
ACO
8808 memset(&crtc_state->dpll_hw_state, 0,
8809 sizeof(crtc_state->dpll_hw_state));
8810
ded220e2
ACO
8811 crtc->lowfreq_avail = false;
8812
8813 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
8814 if (!crtc_state->has_pch_encoder)
8815 return 0;
79e53945 8816
364ee29d
ACO
8817 if (!crtc_state->clock_set &&
8818 !ironlake_compute_clocks(&crtc->base, crtc_state,
8819 &crtc_state->dpll,
8820 &has_reduced_clock,
8821 &reduced_clock)) {
8822 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8823 return -EINVAL;
f47709a9 8824 }
79e53945 8825
b75ca6f6
ACO
8826 ironlake_compute_dpll(crtc, crtc_state,
8827 has_reduced_clock ? &reduced_clock : NULL);
66e985c0 8828
ded220e2
ACO
8829 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
8830 if (pll == NULL) {
8831 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
8832 pipe_name(crtc->pipe));
8833 return -EINVAL;
3fb37703 8834 }
79e53945 8835
ded220e2
ACO
8836 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8837 has_reduced_clock)
c7653199 8838 crtc->lowfreq_avail = true;
e2b78267 8839
c8f7a0db 8840 return 0;
79e53945
JB
8841}
8842
eb14cb74
VS
8843static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8844 struct intel_link_m_n *m_n)
8845{
8846 struct drm_device *dev = crtc->base.dev;
8847 struct drm_i915_private *dev_priv = dev->dev_private;
8848 enum pipe pipe = crtc->pipe;
8849
8850 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8851 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8852 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8853 & ~TU_SIZE_MASK;
8854 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8855 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8856 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8857}
8858
8859static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8860 enum transcoder transcoder,
b95af8be
VK
8861 struct intel_link_m_n *m_n,
8862 struct intel_link_m_n *m2_n2)
72419203
DV
8863{
8864 struct drm_device *dev = crtc->base.dev;
8865 struct drm_i915_private *dev_priv = dev->dev_private;
eb14cb74 8866 enum pipe pipe = crtc->pipe;
72419203 8867
eb14cb74
VS
8868 if (INTEL_INFO(dev)->gen >= 5) {
8869 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8870 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8871 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8872 & ~TU_SIZE_MASK;
8873 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8874 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8875 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
b95af8be
VK
8876 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8877 * gen < 8) and if DRRS is supported (to make sure the
8878 * registers are not unnecessarily read).
8879 */
8880 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
6e3c9717 8881 crtc->config->has_drrs) {
b95af8be
VK
8882 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8883 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8884 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8885 & ~TU_SIZE_MASK;
8886 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8887 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8888 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8889 }
eb14cb74
VS
8890 } else {
8891 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8892 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8893 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8894 & ~TU_SIZE_MASK;
8895 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8896 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8897 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8898 }
8899}
8900
8901void intel_dp_get_m_n(struct intel_crtc *crtc,
5cec258b 8902 struct intel_crtc_state *pipe_config)
eb14cb74 8903{
681a8504 8904 if (pipe_config->has_pch_encoder)
eb14cb74
VS
8905 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8906 else
8907 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be
VK
8908 &pipe_config->dp_m_n,
8909 &pipe_config->dp_m2_n2);
eb14cb74 8910}
72419203 8911
eb14cb74 8912static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5cec258b 8913 struct intel_crtc_state *pipe_config)
eb14cb74
VS
8914{
8915 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be 8916 &pipe_config->fdi_m_n, NULL);
72419203
DV
8917}
8918
bd2e244f 8919static void skylake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 8920 struct intel_crtc_state *pipe_config)
bd2e244f
JB
8921{
8922 struct drm_device *dev = crtc->base.dev;
8923 struct drm_i915_private *dev_priv = dev->dev_private;
a1b2278e
CK
8924 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
8925 uint32_t ps_ctrl = 0;
8926 int id = -1;
8927 int i;
bd2e244f 8928
a1b2278e
CK
8929 /* find scaler attached to this pipe */
8930 for (i = 0; i < crtc->num_scalers; i++) {
8931 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
8932 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
8933 id = i;
8934 pipe_config->pch_pfit.enabled = true;
8935 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
8936 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
8937 break;
8938 }
8939 }
bd2e244f 8940
a1b2278e
CK
8941 scaler_state->scaler_id = id;
8942 if (id >= 0) {
8943 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
8944 } else {
8945 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
bd2e244f
JB
8946 }
8947}
8948
5724dbd1
DL
8949static void
8950skylake_get_initial_plane_config(struct intel_crtc *crtc,
8951 struct intel_initial_plane_config *plane_config)
bc8d7dff
DL
8952{
8953 struct drm_device *dev = crtc->base.dev;
8954 struct drm_i915_private *dev_priv = dev->dev_private;
40f46283 8955 u32 val, base, offset, stride_mult, tiling;
bc8d7dff
DL
8956 int pipe = crtc->pipe;
8957 int fourcc, pixel_format;
6761dd31 8958 unsigned int aligned_height;
bc8d7dff 8959 struct drm_framebuffer *fb;
1b842c89 8960 struct intel_framebuffer *intel_fb;
bc8d7dff 8961
d9806c9f 8962 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 8963 if (!intel_fb) {
bc8d7dff
DL
8964 DRM_DEBUG_KMS("failed to alloc fb\n");
8965 return;
8966 }
8967
1b842c89
DL
8968 fb = &intel_fb->base;
8969
bc8d7dff 8970 val = I915_READ(PLANE_CTL(pipe, 0));
42a7b088
DL
8971 if (!(val & PLANE_CTL_ENABLE))
8972 goto error;
8973
bc8d7dff
DL
8974 pixel_format = val & PLANE_CTL_FORMAT_MASK;
8975 fourcc = skl_format_to_fourcc(pixel_format,
8976 val & PLANE_CTL_ORDER_RGBX,
8977 val & PLANE_CTL_ALPHA_MASK);
8978 fb->pixel_format = fourcc;
8979 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
8980
40f46283
DL
8981 tiling = val & PLANE_CTL_TILED_MASK;
8982 switch (tiling) {
8983 case PLANE_CTL_TILED_LINEAR:
8984 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
8985 break;
8986 case PLANE_CTL_TILED_X:
8987 plane_config->tiling = I915_TILING_X;
8988 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8989 break;
8990 case PLANE_CTL_TILED_Y:
8991 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
8992 break;
8993 case PLANE_CTL_TILED_YF:
8994 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
8995 break;
8996 default:
8997 MISSING_CASE(tiling);
8998 goto error;
8999 }
9000
bc8d7dff
DL
9001 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9002 plane_config->base = base;
9003
9004 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9005
9006 val = I915_READ(PLANE_SIZE(pipe, 0));
9007 fb->height = ((val >> 16) & 0xfff) + 1;
9008 fb->width = ((val >> 0) & 0x1fff) + 1;
9009
9010 val = I915_READ(PLANE_STRIDE(pipe, 0));
7b49f948 9011 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
40f46283 9012 fb->pixel_format);
bc8d7dff
DL
9013 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9014
9015 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
9016 fb->pixel_format,
9017 fb->modifier[0]);
bc8d7dff 9018
f37b5c2b 9019 plane_config->size = fb->pitches[0] * aligned_height;
bc8d7dff
DL
9020
9021 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9022 pipe_name(pipe), fb->width, fb->height,
9023 fb->bits_per_pixel, base, fb->pitches[0],
9024 plane_config->size);
9025
2d14030b 9026 plane_config->fb = intel_fb;
bc8d7dff
DL
9027 return;
9028
9029error:
9030 kfree(fb);
9031}
9032
2fa2fe9a 9033static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9034 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
9035{
9036 struct drm_device *dev = crtc->base.dev;
9037 struct drm_i915_private *dev_priv = dev->dev_private;
9038 uint32_t tmp;
9039
9040 tmp = I915_READ(PF_CTL(crtc->pipe));
9041
9042 if (tmp & PF_ENABLE) {
fd4daa9c 9043 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
DV
9044 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9045 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
DV
9046
9047 /* We currently do not free assignements of panel fitters on
9048 * ivb/hsw (since we don't use the higher upscaling modes which
9049 * differentiates them) so just WARN about this case for now. */
9050 if (IS_GEN7(dev)) {
9051 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9052 PF_PIPE_SEL_IVB(crtc->pipe));
9053 }
2fa2fe9a 9054 }
79e53945
JB
9055}
9056
5724dbd1
DL
9057static void
9058ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9059 struct intel_initial_plane_config *plane_config)
4c6baa59
JB
9060{
9061 struct drm_device *dev = crtc->base.dev;
9062 struct drm_i915_private *dev_priv = dev->dev_private;
9063 u32 val, base, offset;
aeee5a49 9064 int pipe = crtc->pipe;
4c6baa59 9065 int fourcc, pixel_format;
6761dd31 9066 unsigned int aligned_height;
b113d5ee 9067 struct drm_framebuffer *fb;
1b842c89 9068 struct intel_framebuffer *intel_fb;
4c6baa59 9069
42a7b088
DL
9070 val = I915_READ(DSPCNTR(pipe));
9071 if (!(val & DISPLAY_PLANE_ENABLE))
9072 return;
9073
d9806c9f 9074 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9075 if (!intel_fb) {
4c6baa59
JB
9076 DRM_DEBUG_KMS("failed to alloc fb\n");
9077 return;
9078 }
9079
1b842c89
DL
9080 fb = &intel_fb->base;
9081
18c5247e
DV
9082 if (INTEL_INFO(dev)->gen >= 4) {
9083 if (val & DISPPLANE_TILED) {
49af449b 9084 plane_config->tiling = I915_TILING_X;
18c5247e
DV
9085 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9086 }
9087 }
4c6baa59
JB
9088
9089 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 9090 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
9091 fb->pixel_format = fourcc;
9092 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
4c6baa59 9093
aeee5a49 9094 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
4c6baa59 9095 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
aeee5a49 9096 offset = I915_READ(DSPOFFSET(pipe));
4c6baa59 9097 } else {
49af449b 9098 if (plane_config->tiling)
aeee5a49 9099 offset = I915_READ(DSPTILEOFF(pipe));
4c6baa59 9100 else
aeee5a49 9101 offset = I915_READ(DSPLINOFF(pipe));
4c6baa59
JB
9102 }
9103 plane_config->base = base;
9104
9105 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
9106 fb->width = ((val >> 16) & 0xfff) + 1;
9107 fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
9108
9109 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 9110 fb->pitches[0] = val & 0xffffffc0;
4c6baa59 9111
b113d5ee 9112 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
9113 fb->pixel_format,
9114 fb->modifier[0]);
4c6baa59 9115
f37b5c2b 9116 plane_config->size = fb->pitches[0] * aligned_height;
4c6baa59 9117
2844a921
DL
9118 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9119 pipe_name(pipe), fb->width, fb->height,
9120 fb->bits_per_pixel, base, fb->pitches[0],
9121 plane_config->size);
b113d5ee 9122
2d14030b 9123 plane_config->fb = intel_fb;
4c6baa59
JB
9124}
9125
0e8ffe1b 9126static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9127 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
9128{
9129 struct drm_device *dev = crtc->base.dev;
9130 struct drm_i915_private *dev_priv = dev->dev_private;
1729050e 9131 enum intel_display_power_domain power_domain;
0e8ffe1b 9132 uint32_t tmp;
1729050e 9133 bool ret;
0e8ffe1b 9134
1729050e
ID
9135 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9136 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
930e8c9e
PZ
9137 return false;
9138
e143a21c 9139 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 9140 pipe_config->shared_dpll = NULL;
eccb140b 9141
1729050e 9142 ret = false;
0e8ffe1b
DV
9143 tmp = I915_READ(PIPECONF(crtc->pipe));
9144 if (!(tmp & PIPECONF_ENABLE))
1729050e 9145 goto out;
0e8ffe1b 9146
42571aef
VS
9147 switch (tmp & PIPECONF_BPC_MASK) {
9148 case PIPECONF_6BPC:
9149 pipe_config->pipe_bpp = 18;
9150 break;
9151 case PIPECONF_8BPC:
9152 pipe_config->pipe_bpp = 24;
9153 break;
9154 case PIPECONF_10BPC:
9155 pipe_config->pipe_bpp = 30;
9156 break;
9157 case PIPECONF_12BPC:
9158 pipe_config->pipe_bpp = 36;
9159 break;
9160 default:
9161 break;
9162 }
9163
b5a9fa09
DV
9164 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9165 pipe_config->limited_color_range = true;
9166
ab9412ba 9167 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0 9168 struct intel_shared_dpll *pll;
8106ddbd 9169 enum intel_dpll_id pll_id;
66e985c0 9170
88adfff1
DV
9171 pipe_config->has_pch_encoder = true;
9172
627eb5a3
DV
9173 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9174 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9175 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
9176
9177 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 9178
c0d43d62 9179 if (HAS_PCH_IBX(dev_priv->dev)) {
8106ddbd 9180 pll_id = (enum intel_dpll_id) crtc->pipe;
c0d43d62
DV
9181 } else {
9182 tmp = I915_READ(PCH_DPLL_SEL);
9183 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
8106ddbd 9184 pll_id = DPLL_ID_PCH_PLL_B;
c0d43d62 9185 else
8106ddbd 9186 pll_id= DPLL_ID_PCH_PLL_A;
c0d43d62 9187 }
66e985c0 9188
8106ddbd
ACO
9189 pipe_config->shared_dpll =
9190 intel_get_shared_dpll_by_id(dev_priv, pll_id);
9191 pll = pipe_config->shared_dpll;
66e985c0 9192
2edd6443
ACO
9193 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9194 &pipe_config->dpll_hw_state));
c93f54cf
DV
9195
9196 tmp = pipe_config->dpll_hw_state.dpll;
9197 pipe_config->pixel_multiplier =
9198 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9199 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
9200
9201 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
DV
9202 } else {
9203 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
9204 }
9205
1bd1bd80 9206 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 9207 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 9208
2fa2fe9a
DV
9209 ironlake_get_pfit_config(crtc, pipe_config);
9210
1729050e
ID
9211 ret = true;
9212
9213out:
9214 intel_display_power_put(dev_priv, power_domain);
9215
9216 return ret;
0e8ffe1b
DV
9217}
9218
be256dc7
PZ
9219static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9220{
9221 struct drm_device *dev = dev_priv->dev;
be256dc7 9222 struct intel_crtc *crtc;
be256dc7 9223
d3fcc808 9224 for_each_intel_crtc(dev, crtc)
e2c719b7 9225 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
9226 pipe_name(crtc->pipe));
9227
e2c719b7
RC
9228 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9229 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
01403de3
VS
9230 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9231 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
e2c719b7
RC
9232 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9233 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
be256dc7 9234 "CPU PWM1 enabled\n");
c5107b87 9235 if (IS_HASWELL(dev))
e2c719b7 9236 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
c5107b87 9237 "CPU PWM2 enabled\n");
e2c719b7 9238 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
be256dc7 9239 "PCH PWM1 enabled\n");
e2c719b7 9240 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
be256dc7 9241 "Utility pin enabled\n");
e2c719b7 9242 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
be256dc7 9243
9926ada1
PZ
9244 /*
9245 * In theory we can still leave IRQs enabled, as long as only the HPD
9246 * interrupts remain enabled. We used to check for that, but since it's
9247 * gen-specific and since we only disable LCPLL after we fully disable
9248 * the interrupts, the check below should be enough.
9249 */
e2c719b7 9250 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
be256dc7
PZ
9251}
9252
9ccd5aeb
PZ
9253static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9254{
9255 struct drm_device *dev = dev_priv->dev;
9256
9257 if (IS_HASWELL(dev))
9258 return I915_READ(D_COMP_HSW);
9259 else
9260 return I915_READ(D_COMP_BDW);
9261}
9262
3c4c9b81
PZ
9263static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9264{
9265 struct drm_device *dev = dev_priv->dev;
9266
9267 if (IS_HASWELL(dev)) {
9268 mutex_lock(&dev_priv->rps.hw_lock);
9269 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9270 val))
f475dadf 9271 DRM_ERROR("Failed to write to D_COMP\n");
3c4c9b81
PZ
9272 mutex_unlock(&dev_priv->rps.hw_lock);
9273 } else {
9ccd5aeb
PZ
9274 I915_WRITE(D_COMP_BDW, val);
9275 POSTING_READ(D_COMP_BDW);
3c4c9b81 9276 }
be256dc7
PZ
9277}
9278
9279/*
9280 * This function implements pieces of two sequences from BSpec:
9281 * - Sequence for display software to disable LCPLL
9282 * - Sequence for display software to allow package C8+
9283 * The steps implemented here are just the steps that actually touch the LCPLL
9284 * register. Callers should take care of disabling all the display engine
9285 * functions, doing the mode unset, fixing interrupts, etc.
9286 */
6ff58d53
PZ
9287static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9288 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
9289{
9290 uint32_t val;
9291
9292 assert_can_disable_lcpll(dev_priv);
9293
9294 val = I915_READ(LCPLL_CTL);
9295
9296 if (switch_to_fclk) {
9297 val |= LCPLL_CD_SOURCE_FCLK;
9298 I915_WRITE(LCPLL_CTL, val);
9299
9300 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9301 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9302 DRM_ERROR("Switching to FCLK failed\n");
9303
9304 val = I915_READ(LCPLL_CTL);
9305 }
9306
9307 val |= LCPLL_PLL_DISABLE;
9308 I915_WRITE(LCPLL_CTL, val);
9309 POSTING_READ(LCPLL_CTL);
9310
9311 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9312 DRM_ERROR("LCPLL still locked\n");
9313
9ccd5aeb 9314 val = hsw_read_dcomp(dev_priv);
be256dc7 9315 val |= D_COMP_COMP_DISABLE;
3c4c9b81 9316 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9317 ndelay(100);
9318
9ccd5aeb
PZ
9319 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9320 1))
be256dc7
PZ
9321 DRM_ERROR("D_COMP RCOMP still in progress\n");
9322
9323 if (allow_power_down) {
9324 val = I915_READ(LCPLL_CTL);
9325 val |= LCPLL_POWER_DOWN_ALLOW;
9326 I915_WRITE(LCPLL_CTL, val);
9327 POSTING_READ(LCPLL_CTL);
9328 }
9329}
9330
9331/*
9332 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9333 * source.
9334 */
6ff58d53 9335static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
9336{
9337 uint32_t val;
9338
9339 val = I915_READ(LCPLL_CTL);
9340
9341 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9342 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9343 return;
9344
a8a8bd54
PZ
9345 /*
9346 * Make sure we're not on PC8 state before disabling PC8, otherwise
9347 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
a8a8bd54 9348 */
59bad947 9349 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
215733fa 9350
be256dc7
PZ
9351 if (val & LCPLL_POWER_DOWN_ALLOW) {
9352 val &= ~LCPLL_POWER_DOWN_ALLOW;
9353 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 9354 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
9355 }
9356
9ccd5aeb 9357 val = hsw_read_dcomp(dev_priv);
be256dc7
PZ
9358 val |= D_COMP_COMP_FORCE;
9359 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 9360 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9361
9362 val = I915_READ(LCPLL_CTL);
9363 val &= ~LCPLL_PLL_DISABLE;
9364 I915_WRITE(LCPLL_CTL, val);
9365
9366 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9367 DRM_ERROR("LCPLL not locked yet\n");
9368
9369 if (val & LCPLL_CD_SOURCE_FCLK) {
9370 val = I915_READ(LCPLL_CTL);
9371 val &= ~LCPLL_CD_SOURCE_FCLK;
9372 I915_WRITE(LCPLL_CTL, val);
9373
9374 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9375 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9376 DRM_ERROR("Switching back to LCPLL failed\n");
9377 }
215733fa 9378
59bad947 9379 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
b6283055 9380 intel_update_cdclk(dev_priv->dev);
be256dc7
PZ
9381}
9382
765dab67
PZ
9383/*
9384 * Package states C8 and deeper are really deep PC states that can only be
9385 * reached when all the devices on the system allow it, so even if the graphics
9386 * device allows PC8+, it doesn't mean the system will actually get to these
9387 * states. Our driver only allows PC8+ when going into runtime PM.
9388 *
9389 * The requirements for PC8+ are that all the outputs are disabled, the power
9390 * well is disabled and most interrupts are disabled, and these are also
9391 * requirements for runtime PM. When these conditions are met, we manually do
9392 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9393 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9394 * hang the machine.
9395 *
9396 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9397 * the state of some registers, so when we come back from PC8+ we need to
9398 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9399 * need to take care of the registers kept by RC6. Notice that this happens even
9400 * if we don't put the device in PCI D3 state (which is what currently happens
9401 * because of the runtime PM support).
9402 *
9403 * For more, read "Display Sequences for Package C8" on the hardware
9404 * documentation.
9405 */
a14cb6fc 9406void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 9407{
c67a470b
PZ
9408 struct drm_device *dev = dev_priv->dev;
9409 uint32_t val;
9410
c67a470b
PZ
9411 DRM_DEBUG_KMS("Enabling package C8+\n");
9412
c2699524 9413 if (HAS_PCH_LPT_LP(dev)) {
c67a470b
PZ
9414 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9415 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9416 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9417 }
9418
9419 lpt_disable_clkout_dp(dev);
c67a470b
PZ
9420 hsw_disable_lcpll(dev_priv, true, true);
9421}
9422
a14cb6fc 9423void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b
PZ
9424{
9425 struct drm_device *dev = dev_priv->dev;
9426 uint32_t val;
9427
c67a470b
PZ
9428 DRM_DEBUG_KMS("Disabling package C8+\n");
9429
9430 hsw_restore_lcpll(dev_priv);
c67a470b
PZ
9431 lpt_init_pch_refclk(dev);
9432
c2699524 9433 if (HAS_PCH_LPT_LP(dev)) {
c67a470b
PZ
9434 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9435 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9436 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9437 }
c67a470b
PZ
9438}
9439
27c329ed 9440static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
f8437dd1 9441{
a821fc46 9442 struct drm_device *dev = old_state->dev;
1a617b77
ML
9443 struct intel_atomic_state *old_intel_state =
9444 to_intel_atomic_state(old_state);
9445 unsigned int req_cdclk = old_intel_state->dev_cdclk;
f8437dd1 9446
27c329ed 9447 broxton_set_cdclk(dev, req_cdclk);
f8437dd1
VK
9448}
9449
b432e5cf 9450/* compute the max rate for new configuration */
27c329ed 9451static int ilk_max_pixel_rate(struct drm_atomic_state *state)
b432e5cf 9452{
565602d7
ML
9453 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
9454 struct drm_i915_private *dev_priv = state->dev->dev_private;
9455 struct drm_crtc *crtc;
9456 struct drm_crtc_state *cstate;
27c329ed 9457 struct intel_crtc_state *crtc_state;
565602d7
ML
9458 unsigned max_pixel_rate = 0, i;
9459 enum pipe pipe;
b432e5cf 9460
565602d7
ML
9461 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
9462 sizeof(intel_state->min_pixclk));
27c329ed 9463
565602d7
ML
9464 for_each_crtc_in_state(state, crtc, cstate, i) {
9465 int pixel_rate;
27c329ed 9466
565602d7
ML
9467 crtc_state = to_intel_crtc_state(cstate);
9468 if (!crtc_state->base.enable) {
9469 intel_state->min_pixclk[i] = 0;
b432e5cf 9470 continue;
565602d7 9471 }
b432e5cf 9472
27c329ed 9473 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
b432e5cf
VS
9474
9475 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
565602d7 9476 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
b432e5cf
VS
9477 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9478
565602d7 9479 intel_state->min_pixclk[i] = pixel_rate;
b432e5cf
VS
9480 }
9481
565602d7
ML
9482 for_each_pipe(dev_priv, pipe)
9483 max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
9484
b432e5cf
VS
9485 return max_pixel_rate;
9486}
9487
9488static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9489{
9490 struct drm_i915_private *dev_priv = dev->dev_private;
9491 uint32_t val, data;
9492 int ret;
9493
9494 if (WARN((I915_READ(LCPLL_CTL) &
9495 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9496 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9497 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9498 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9499 "trying to change cdclk frequency with cdclk not enabled\n"))
9500 return;
9501
9502 mutex_lock(&dev_priv->rps.hw_lock);
9503 ret = sandybridge_pcode_write(dev_priv,
9504 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9505 mutex_unlock(&dev_priv->rps.hw_lock);
9506 if (ret) {
9507 DRM_ERROR("failed to inform pcode about cdclk change\n");
9508 return;
9509 }
9510
9511 val = I915_READ(LCPLL_CTL);
9512 val |= LCPLL_CD_SOURCE_FCLK;
9513 I915_WRITE(LCPLL_CTL, val);
9514
5ba00178
TU
9515 if (wait_for_us(I915_READ(LCPLL_CTL) &
9516 LCPLL_CD_SOURCE_FCLK_DONE, 1))
b432e5cf
VS
9517 DRM_ERROR("Switching to FCLK failed\n");
9518
9519 val = I915_READ(LCPLL_CTL);
9520 val &= ~LCPLL_CLK_FREQ_MASK;
9521
9522 switch (cdclk) {
9523 case 450000:
9524 val |= LCPLL_CLK_FREQ_450;
9525 data = 0;
9526 break;
9527 case 540000:
9528 val |= LCPLL_CLK_FREQ_54O_BDW;
9529 data = 1;
9530 break;
9531 case 337500:
9532 val |= LCPLL_CLK_FREQ_337_5_BDW;
9533 data = 2;
9534 break;
9535 case 675000:
9536 val |= LCPLL_CLK_FREQ_675_BDW;
9537 data = 3;
9538 break;
9539 default:
9540 WARN(1, "invalid cdclk frequency\n");
9541 return;
9542 }
9543
9544 I915_WRITE(LCPLL_CTL, val);
9545
9546 val = I915_READ(LCPLL_CTL);
9547 val &= ~LCPLL_CD_SOURCE_FCLK;
9548 I915_WRITE(LCPLL_CTL, val);
9549
5ba00178
TU
9550 if (wait_for_us((I915_READ(LCPLL_CTL) &
9551 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
b432e5cf
VS
9552 DRM_ERROR("Switching back to LCPLL failed\n");
9553
9554 mutex_lock(&dev_priv->rps.hw_lock);
9555 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9556 mutex_unlock(&dev_priv->rps.hw_lock);
9557
9558 intel_update_cdclk(dev);
9559
9560 WARN(cdclk != dev_priv->cdclk_freq,
9561 "cdclk requested %d kHz but got %d kHz\n",
9562 cdclk, dev_priv->cdclk_freq);
9563}
9564
27c329ed 9565static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
b432e5cf 9566{
27c329ed 9567 struct drm_i915_private *dev_priv = to_i915(state->dev);
1a617b77 9568 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
27c329ed 9569 int max_pixclk = ilk_max_pixel_rate(state);
b432e5cf
VS
9570 int cdclk;
9571
9572 /*
9573 * FIXME should also account for plane ratio
9574 * once 64bpp pixel formats are supported.
9575 */
27c329ed 9576 if (max_pixclk > 540000)
b432e5cf 9577 cdclk = 675000;
27c329ed 9578 else if (max_pixclk > 450000)
b432e5cf 9579 cdclk = 540000;
27c329ed 9580 else if (max_pixclk > 337500)
b432e5cf
VS
9581 cdclk = 450000;
9582 else
9583 cdclk = 337500;
9584
b432e5cf 9585 if (cdclk > dev_priv->max_cdclk_freq) {
63ba534e
ML
9586 DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9587 cdclk, dev_priv->max_cdclk_freq);
9588 return -EINVAL;
b432e5cf
VS
9589 }
9590
1a617b77
ML
9591 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9592 if (!intel_state->active_crtcs)
9593 intel_state->dev_cdclk = 337500;
b432e5cf
VS
9594
9595 return 0;
9596}
9597
27c329ed 9598static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
b432e5cf 9599{
27c329ed 9600 struct drm_device *dev = old_state->dev;
1a617b77
ML
9601 struct intel_atomic_state *old_intel_state =
9602 to_intel_atomic_state(old_state);
9603 unsigned req_cdclk = old_intel_state->dev_cdclk;
b432e5cf 9604
27c329ed 9605 broadwell_set_cdclk(dev, req_cdclk);
b432e5cf
VS
9606}
9607
190f68c5
ACO
9608static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9609 struct intel_crtc_state *crtc_state)
09b4ddf9 9610{
af3997b5
MK
9611 struct intel_encoder *intel_encoder =
9612 intel_ddi_get_crtc_new_encoder(crtc_state);
9613
9614 if (intel_encoder->type != INTEL_OUTPUT_DSI) {
9615 if (!intel_ddi_pll_select(crtc, crtc_state))
9616 return -EINVAL;
9617 }
716c2e55 9618
c7653199 9619 crtc->lowfreq_avail = false;
644cef34 9620
c8f7a0db 9621 return 0;
79e53945
JB
9622}
9623
3760b59c
S
9624static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9625 enum port port,
9626 struct intel_crtc_state *pipe_config)
9627{
8106ddbd
ACO
9628 enum intel_dpll_id id;
9629
3760b59c
S
9630 switch (port) {
9631 case PORT_A:
9632 pipe_config->ddi_pll_sel = SKL_DPLL0;
08250c4b 9633 id = DPLL_ID_SKL_DPLL0;
3760b59c
S
9634 break;
9635 case PORT_B:
9636 pipe_config->ddi_pll_sel = SKL_DPLL1;
08250c4b 9637 id = DPLL_ID_SKL_DPLL1;
3760b59c
S
9638 break;
9639 case PORT_C:
9640 pipe_config->ddi_pll_sel = SKL_DPLL2;
08250c4b 9641 id = DPLL_ID_SKL_DPLL2;
3760b59c
S
9642 break;
9643 default:
9644 DRM_ERROR("Incorrect port type\n");
8106ddbd 9645 return;
3760b59c 9646 }
8106ddbd
ACO
9647
9648 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
3760b59c
S
9649}
9650
96b7dfb7
S
9651static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9652 enum port port,
5cec258b 9653 struct intel_crtc_state *pipe_config)
96b7dfb7 9654{
8106ddbd 9655 enum intel_dpll_id id;
a3c988ea 9656 u32 temp;
96b7dfb7
S
9657
9658 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9659 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9660
9661 switch (pipe_config->ddi_pll_sel) {
3148ade7 9662 case SKL_DPLL0:
a3c988ea
ACO
9663 id = DPLL_ID_SKL_DPLL0;
9664 break;
96b7dfb7 9665 case SKL_DPLL1:
8106ddbd 9666 id = DPLL_ID_SKL_DPLL1;
96b7dfb7
S
9667 break;
9668 case SKL_DPLL2:
8106ddbd 9669 id = DPLL_ID_SKL_DPLL2;
96b7dfb7
S
9670 break;
9671 case SKL_DPLL3:
8106ddbd 9672 id = DPLL_ID_SKL_DPLL3;
96b7dfb7 9673 break;
8106ddbd
ACO
9674 default:
9675 MISSING_CASE(pipe_config->ddi_pll_sel);
9676 return;
96b7dfb7 9677 }
8106ddbd
ACO
9678
9679 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
96b7dfb7
S
9680}
9681
7d2c8175
DL
9682static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9683 enum port port,
5cec258b 9684 struct intel_crtc_state *pipe_config)
7d2c8175 9685{
8106ddbd
ACO
9686 enum intel_dpll_id id;
9687
7d2c8175
DL
9688 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9689
9690 switch (pipe_config->ddi_pll_sel) {
9691 case PORT_CLK_SEL_WRPLL1:
8106ddbd 9692 id = DPLL_ID_WRPLL1;
7d2c8175
DL
9693 break;
9694 case PORT_CLK_SEL_WRPLL2:
8106ddbd 9695 id = DPLL_ID_WRPLL2;
7d2c8175 9696 break;
00490c22 9697 case PORT_CLK_SEL_SPLL:
8106ddbd 9698 id = DPLL_ID_SPLL;
79bd23da 9699 break;
9d16da65
ACO
9700 case PORT_CLK_SEL_LCPLL_810:
9701 id = DPLL_ID_LCPLL_810;
9702 break;
9703 case PORT_CLK_SEL_LCPLL_1350:
9704 id = DPLL_ID_LCPLL_1350;
9705 break;
9706 case PORT_CLK_SEL_LCPLL_2700:
9707 id = DPLL_ID_LCPLL_2700;
9708 break;
8106ddbd
ACO
9709 default:
9710 MISSING_CASE(pipe_config->ddi_pll_sel);
9711 /* fall through */
9712 case PORT_CLK_SEL_NONE:
8106ddbd 9713 return;
7d2c8175 9714 }
8106ddbd
ACO
9715
9716 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
7d2c8175
DL
9717}
9718
cf30429e
JN
9719static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
9720 struct intel_crtc_state *pipe_config,
9721 unsigned long *power_domain_mask)
9722{
9723 struct drm_device *dev = crtc->base.dev;
9724 struct drm_i915_private *dev_priv = dev->dev_private;
9725 enum intel_display_power_domain power_domain;
9726 u32 tmp;
9727
9728 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
9729
9730 /*
9731 * XXX: Do intel_display_power_get_if_enabled before reading this (for
9732 * consistency and less surprising code; it's in always on power).
9733 */
9734 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9735 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9736 enum pipe trans_edp_pipe;
9737 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9738 default:
9739 WARN(1, "unknown pipe linked to edp transcoder\n");
9740 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9741 case TRANS_DDI_EDP_INPUT_A_ON:
9742 trans_edp_pipe = PIPE_A;
9743 break;
9744 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9745 trans_edp_pipe = PIPE_B;
9746 break;
9747 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9748 trans_edp_pipe = PIPE_C;
9749 break;
9750 }
9751
9752 if (trans_edp_pipe == crtc->pipe)
9753 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9754 }
9755
9756 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
9757 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9758 return false;
9759 *power_domain_mask |= BIT(power_domain);
9760
9761 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
9762
9763 return tmp & PIPECONF_ENABLE;
9764}
9765
4d1de975
JN
9766static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
9767 struct intel_crtc_state *pipe_config,
9768 unsigned long *power_domain_mask)
9769{
9770 struct drm_device *dev = crtc->base.dev;
9771 struct drm_i915_private *dev_priv = dev->dev_private;
9772 enum intel_display_power_domain power_domain;
9773 enum port port;
9774 enum transcoder cpu_transcoder;
9775 u32 tmp;
9776
9777 pipe_config->has_dsi_encoder = false;
9778
9779 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
9780 if (port == PORT_A)
9781 cpu_transcoder = TRANSCODER_DSI_A;
9782 else
9783 cpu_transcoder = TRANSCODER_DSI_C;
9784
9785 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
9786 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9787 continue;
9788 *power_domain_mask |= BIT(power_domain);
9789
9790 /* XXX: this works for video mode only */
9791 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
9792 if (!(tmp & DPI_ENABLE))
9793 continue;
9794
9795 tmp = I915_READ(MIPI_CTRL(port));
9796 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
9797 continue;
9798
9799 pipe_config->cpu_transcoder = cpu_transcoder;
9800 pipe_config->has_dsi_encoder = true;
9801 break;
9802 }
9803
9804 return pipe_config->has_dsi_encoder;
9805}
9806
26804afd 9807static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
5cec258b 9808 struct intel_crtc_state *pipe_config)
26804afd
DV
9809{
9810 struct drm_device *dev = crtc->base.dev;
9811 struct drm_i915_private *dev_priv = dev->dev_private;
d452c5b6 9812 struct intel_shared_dpll *pll;
26804afd
DV
9813 enum port port;
9814 uint32_t tmp;
9815
9816 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9817
9818 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9819
ef11bdb3 9820 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
96b7dfb7 9821 skylake_get_ddi_pll(dev_priv, port, pipe_config);
3760b59c
S
9822 else if (IS_BROXTON(dev))
9823 bxt_get_ddi_pll(dev_priv, port, pipe_config);
96b7dfb7
S
9824 else
9825 haswell_get_ddi_pll(dev_priv, port, pipe_config);
9cd86933 9826
8106ddbd
ACO
9827 pll = pipe_config->shared_dpll;
9828 if (pll) {
2edd6443
ACO
9829 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9830 &pipe_config->dpll_hw_state));
d452c5b6
DV
9831 }
9832
26804afd
DV
9833 /*
9834 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9835 * DDI E. So just check whether this pipe is wired to DDI E and whether
9836 * the PCH transcoder is on.
9837 */
ca370455
DL
9838 if (INTEL_INFO(dev)->gen < 9 &&
9839 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
26804afd
DV
9840 pipe_config->has_pch_encoder = true;
9841
9842 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9843 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9844 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9845
9846 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9847 }
9848}
9849
0e8ffe1b 9850static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9851 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
9852{
9853 struct drm_device *dev = crtc->base.dev;
9854 struct drm_i915_private *dev_priv = dev->dev_private;
1729050e
ID
9855 enum intel_display_power_domain power_domain;
9856 unsigned long power_domain_mask;
cf30429e 9857 bool active;
0e8ffe1b 9858
1729050e
ID
9859 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9860 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0 9861 return false;
1729050e
ID
9862 power_domain_mask = BIT(power_domain);
9863
8106ddbd 9864 pipe_config->shared_dpll = NULL;
c0d43d62 9865
cf30429e 9866 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
eccb140b 9867
4d1de975
JN
9868 if (IS_BROXTON(dev_priv)) {
9869 bxt_get_dsi_transcoder_state(crtc, pipe_config,
9870 &power_domain_mask);
9871 WARN_ON(active && pipe_config->has_dsi_encoder);
9872 if (pipe_config->has_dsi_encoder)
9873 active = true;
9874 }
9875
cf30429e 9876 if (!active)
1729050e 9877 goto out;
0e8ffe1b 9878
4d1de975
JN
9879 if (!pipe_config->has_dsi_encoder) {
9880 haswell_get_ddi_port_state(crtc, pipe_config);
9881 intel_get_pipe_timings(crtc, pipe_config);
9882 }
627eb5a3 9883
bc58be60 9884 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 9885
05dc698c
LL
9886 pipe_config->gamma_mode =
9887 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
9888
a1b2278e
CK
9889 if (INTEL_INFO(dev)->gen >= 9) {
9890 skl_init_scalers(dev, crtc, pipe_config);
9891 }
9892
af99ceda
CK
9893 if (INTEL_INFO(dev)->gen >= 9) {
9894 pipe_config->scaler_state.scaler_id = -1;
9895 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9896 }
9897
1729050e
ID
9898 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
9899 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
9900 power_domain_mask |= BIT(power_domain);
1c132b44 9901 if (INTEL_INFO(dev)->gen >= 9)
bd2e244f 9902 skylake_get_pfit_config(crtc, pipe_config);
ff6d9f55 9903 else
1c132b44 9904 ironlake_get_pfit_config(crtc, pipe_config);
bd2e244f 9905 }
88adfff1 9906
e59150dc
JB
9907 if (IS_HASWELL(dev))
9908 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9909 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 9910
4d1de975
JN
9911 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
9912 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
ebb69c95
CT
9913 pipe_config->pixel_multiplier =
9914 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9915 } else {
9916 pipe_config->pixel_multiplier = 1;
9917 }
6c49f241 9918
1729050e
ID
9919out:
9920 for_each_power_domain(power_domain, power_domain_mask)
9921 intel_display_power_put(dev_priv, power_domain);
9922
cf30429e 9923 return active;
0e8ffe1b
DV
9924}
9925
55a08b3f
ML
9926static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
9927 const struct intel_plane_state *plane_state)
560b85bb
CW
9928{
9929 struct drm_device *dev = crtc->dev;
9930 struct drm_i915_private *dev_priv = dev->dev_private;
9931 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
dc41c154 9932 uint32_t cntl = 0, size = 0;
560b85bb 9933
55a08b3f
ML
9934 if (plane_state && plane_state->visible) {
9935 unsigned int width = plane_state->base.crtc_w;
9936 unsigned int height = plane_state->base.crtc_h;
dc41c154
VS
9937 unsigned int stride = roundup_pow_of_two(width) * 4;
9938
9939 switch (stride) {
9940 default:
9941 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
9942 width, stride);
9943 stride = 256;
9944 /* fallthrough */
9945 case 256:
9946 case 512:
9947 case 1024:
9948 case 2048:
9949 break;
4b0e333e
CW
9950 }
9951
dc41c154
VS
9952 cntl |= CURSOR_ENABLE |
9953 CURSOR_GAMMA_ENABLE |
9954 CURSOR_FORMAT_ARGB |
9955 CURSOR_STRIDE(stride);
9956
9957 size = (height << 12) | width;
4b0e333e 9958 }
560b85bb 9959
dc41c154
VS
9960 if (intel_crtc->cursor_cntl != 0 &&
9961 (intel_crtc->cursor_base != base ||
9962 intel_crtc->cursor_size != size ||
9963 intel_crtc->cursor_cntl != cntl)) {
9964 /* On these chipsets we can only modify the base/size/stride
9965 * whilst the cursor is disabled.
9966 */
0b87c24e
VS
9967 I915_WRITE(CURCNTR(PIPE_A), 0);
9968 POSTING_READ(CURCNTR(PIPE_A));
dc41c154 9969 intel_crtc->cursor_cntl = 0;
4b0e333e 9970 }
560b85bb 9971
99d1f387 9972 if (intel_crtc->cursor_base != base) {
0b87c24e 9973 I915_WRITE(CURBASE(PIPE_A), base);
99d1f387
VS
9974 intel_crtc->cursor_base = base;
9975 }
4726e0b0 9976
dc41c154
VS
9977 if (intel_crtc->cursor_size != size) {
9978 I915_WRITE(CURSIZE, size);
9979 intel_crtc->cursor_size = size;
4b0e333e 9980 }
560b85bb 9981
4b0e333e 9982 if (intel_crtc->cursor_cntl != cntl) {
0b87c24e
VS
9983 I915_WRITE(CURCNTR(PIPE_A), cntl);
9984 POSTING_READ(CURCNTR(PIPE_A));
4b0e333e 9985 intel_crtc->cursor_cntl = cntl;
560b85bb 9986 }
560b85bb
CW
9987}
9988
55a08b3f
ML
9989static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
9990 const struct intel_plane_state *plane_state)
65a21cd6
JB
9991{
9992 struct drm_device *dev = crtc->dev;
9993 struct drm_i915_private *dev_priv = dev->dev_private;
9994 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9995 int pipe = intel_crtc->pipe;
663f3122 9996 uint32_t cntl = 0;
4b0e333e 9997
55a08b3f 9998 if (plane_state && plane_state->visible) {
4b0e333e 9999 cntl = MCURSOR_GAMMA_ENABLE;
55a08b3f 10000 switch (plane_state->base.crtc_w) {
4726e0b0
SK
10001 case 64:
10002 cntl |= CURSOR_MODE_64_ARGB_AX;
10003 break;
10004 case 128:
10005 cntl |= CURSOR_MODE_128_ARGB_AX;
10006 break;
10007 case 256:
10008 cntl |= CURSOR_MODE_256_ARGB_AX;
10009 break;
10010 default:
55a08b3f 10011 MISSING_CASE(plane_state->base.crtc_w);
4726e0b0 10012 return;
65a21cd6 10013 }
4b0e333e 10014 cntl |= pipe << 28; /* Connect to correct pipe */
47bf17a7 10015
fc6f93bc 10016 if (HAS_DDI(dev))
47bf17a7 10017 cntl |= CURSOR_PIPE_CSC_ENABLE;
65a21cd6 10018
55a08b3f
ML
10019 if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
10020 cntl |= CURSOR_ROTATE_180;
10021 }
4398ad45 10022
4b0e333e
CW
10023 if (intel_crtc->cursor_cntl != cntl) {
10024 I915_WRITE(CURCNTR(pipe), cntl);
10025 POSTING_READ(CURCNTR(pipe));
10026 intel_crtc->cursor_cntl = cntl;
65a21cd6 10027 }
4b0e333e 10028
65a21cd6 10029 /* and commit changes on next vblank */
5efb3e28
VS
10030 I915_WRITE(CURBASE(pipe), base);
10031 POSTING_READ(CURBASE(pipe));
99d1f387
VS
10032
10033 intel_crtc->cursor_base = base;
65a21cd6
JB
10034}
10035
cda4b7d3 10036/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6b383a7f 10037static void intel_crtc_update_cursor(struct drm_crtc *crtc,
55a08b3f 10038 const struct intel_plane_state *plane_state)
cda4b7d3
CW
10039{
10040 struct drm_device *dev = crtc->dev;
10041 struct drm_i915_private *dev_priv = dev->dev_private;
10042 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10043 int pipe = intel_crtc->pipe;
55a08b3f
ML
10044 u32 base = intel_crtc->cursor_addr;
10045 u32 pos = 0;
cda4b7d3 10046
55a08b3f
ML
10047 if (plane_state) {
10048 int x = plane_state->base.crtc_x;
10049 int y = plane_state->base.crtc_y;
cda4b7d3 10050
55a08b3f
ML
10051 if (x < 0) {
10052 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10053 x = -x;
10054 }
10055 pos |= x << CURSOR_X_SHIFT;
cda4b7d3 10056
55a08b3f
ML
10057 if (y < 0) {
10058 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10059 y = -y;
10060 }
10061 pos |= y << CURSOR_Y_SHIFT;
10062
10063 /* ILK+ do this automagically */
10064 if (HAS_GMCH_DISPLAY(dev) &&
10065 plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
10066 base += (plane_state->base.crtc_h *
10067 plane_state->base.crtc_w - 1) * 4;
10068 }
cda4b7d3 10069 }
cda4b7d3 10070
5efb3e28
VS
10071 I915_WRITE(CURPOS(pipe), pos);
10072
8ac54669 10073 if (IS_845G(dev) || IS_I865G(dev))
55a08b3f 10074 i845_update_cursor(crtc, base, plane_state);
5efb3e28 10075 else
55a08b3f 10076 i9xx_update_cursor(crtc, base, plane_state);
cda4b7d3
CW
10077}
10078
dc41c154
VS
10079static bool cursor_size_ok(struct drm_device *dev,
10080 uint32_t width, uint32_t height)
10081{
10082 if (width == 0 || height == 0)
10083 return false;
10084
10085 /*
10086 * 845g/865g are special in that they are only limited by
10087 * the width of their cursors, the height is arbitrary up to
10088 * the precision of the register. Everything else requires
10089 * square cursors, limited to a few power-of-two sizes.
10090 */
10091 if (IS_845G(dev) || IS_I865G(dev)) {
10092 if ((width & 63) != 0)
10093 return false;
10094
10095 if (width > (IS_845G(dev) ? 64 : 512))
10096 return false;
10097
10098 if (height > 1023)
10099 return false;
10100 } else {
10101 switch (width | height) {
10102 case 256:
10103 case 128:
10104 if (IS_GEN2(dev))
10105 return false;
10106 case 64:
10107 break;
10108 default:
10109 return false;
10110 }
10111 }
10112
10113 return true;
10114}
10115
79e53945
JB
10116/* VESA 640x480x72Hz mode to set on the pipe */
10117static struct drm_display_mode load_detect_mode = {
10118 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10119 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10120};
10121
a8bb6818
DV
10122struct drm_framebuffer *
10123__intel_framebuffer_create(struct drm_device *dev,
10124 struct drm_mode_fb_cmd2 *mode_cmd,
10125 struct drm_i915_gem_object *obj)
d2dff872
CW
10126{
10127 struct intel_framebuffer *intel_fb;
10128 int ret;
10129
10130 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
dcb1394e 10131 if (!intel_fb)
d2dff872 10132 return ERR_PTR(-ENOMEM);
d2dff872
CW
10133
10134 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
dd4916c5
DV
10135 if (ret)
10136 goto err;
d2dff872
CW
10137
10138 return &intel_fb->base;
dcb1394e 10139
dd4916c5 10140err:
dd4916c5 10141 kfree(intel_fb);
dd4916c5 10142 return ERR_PTR(ret);
d2dff872
CW
10143}
10144
b5ea642a 10145static struct drm_framebuffer *
a8bb6818
DV
10146intel_framebuffer_create(struct drm_device *dev,
10147 struct drm_mode_fb_cmd2 *mode_cmd,
10148 struct drm_i915_gem_object *obj)
10149{
10150 struct drm_framebuffer *fb;
10151 int ret;
10152
10153 ret = i915_mutex_lock_interruptible(dev);
10154 if (ret)
10155 return ERR_PTR(ret);
10156 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10157 mutex_unlock(&dev->struct_mutex);
10158
10159 return fb;
10160}
10161
d2dff872
CW
10162static u32
10163intel_framebuffer_pitch_for_width(int width, int bpp)
10164{
10165 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10166 return ALIGN(pitch, 64);
10167}
10168
10169static u32
10170intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10171{
10172 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
1267a26b 10173 return PAGE_ALIGN(pitch * mode->vdisplay);
d2dff872
CW
10174}
10175
10176static struct drm_framebuffer *
10177intel_framebuffer_create_for_mode(struct drm_device *dev,
10178 struct drm_display_mode *mode,
10179 int depth, int bpp)
10180{
dcb1394e 10181 struct drm_framebuffer *fb;
d2dff872 10182 struct drm_i915_gem_object *obj;
0fed39bd 10183 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872
CW
10184
10185 obj = i915_gem_alloc_object(dev,
10186 intel_framebuffer_size_for_mode(mode, bpp));
10187 if (obj == NULL)
10188 return ERR_PTR(-ENOMEM);
10189
10190 mode_cmd.width = mode->hdisplay;
10191 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
10192 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10193 bpp);
5ca0c34a 10194 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872 10195
dcb1394e
LW
10196 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
10197 if (IS_ERR(fb))
10198 drm_gem_object_unreference_unlocked(&obj->base);
10199
10200 return fb;
d2dff872
CW
10201}
10202
10203static struct drm_framebuffer *
10204mode_fits_in_fbdev(struct drm_device *dev,
10205 struct drm_display_mode *mode)
10206{
0695726e 10207#ifdef CONFIG_DRM_FBDEV_EMULATION
d2dff872
CW
10208 struct drm_i915_private *dev_priv = dev->dev_private;
10209 struct drm_i915_gem_object *obj;
10210 struct drm_framebuffer *fb;
10211
4c0e5528 10212 if (!dev_priv->fbdev)
d2dff872
CW
10213 return NULL;
10214
4c0e5528 10215 if (!dev_priv->fbdev->fb)
d2dff872
CW
10216 return NULL;
10217
4c0e5528
DV
10218 obj = dev_priv->fbdev->fb->obj;
10219 BUG_ON(!obj);
10220
8bcd4553 10221 fb = &dev_priv->fbdev->fb->base;
01f2c773
VS
10222 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10223 fb->bits_per_pixel))
d2dff872
CW
10224 return NULL;
10225
01f2c773 10226 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
10227 return NULL;
10228
edde3617 10229 drm_framebuffer_reference(fb);
d2dff872 10230 return fb;
4520f53a
DV
10231#else
10232 return NULL;
10233#endif
d2dff872
CW
10234}
10235
d3a40d1b
ACO
10236static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10237 struct drm_crtc *crtc,
10238 struct drm_display_mode *mode,
10239 struct drm_framebuffer *fb,
10240 int x, int y)
10241{
10242 struct drm_plane_state *plane_state;
10243 int hdisplay, vdisplay;
10244 int ret;
10245
10246 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10247 if (IS_ERR(plane_state))
10248 return PTR_ERR(plane_state);
10249
10250 if (mode)
10251 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10252 else
10253 hdisplay = vdisplay = 0;
10254
10255 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10256 if (ret)
10257 return ret;
10258 drm_atomic_set_fb_for_plane(plane_state, fb);
10259 plane_state->crtc_x = 0;
10260 plane_state->crtc_y = 0;
10261 plane_state->crtc_w = hdisplay;
10262 plane_state->crtc_h = vdisplay;
10263 plane_state->src_x = x << 16;
10264 plane_state->src_y = y << 16;
10265 plane_state->src_w = hdisplay << 16;
10266 plane_state->src_h = vdisplay << 16;
10267
10268 return 0;
10269}
10270
d2434ab7 10271bool intel_get_load_detect_pipe(struct drm_connector *connector,
7173188d 10272 struct drm_display_mode *mode,
51fd371b
RC
10273 struct intel_load_detect_pipe *old,
10274 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
10275{
10276 struct intel_crtc *intel_crtc;
d2434ab7
DV
10277 struct intel_encoder *intel_encoder =
10278 intel_attached_encoder(connector);
79e53945 10279 struct drm_crtc *possible_crtc;
4ef69c7a 10280 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
10281 struct drm_crtc *crtc = NULL;
10282 struct drm_device *dev = encoder->dev;
94352cf9 10283 struct drm_framebuffer *fb;
51fd371b 10284 struct drm_mode_config *config = &dev->mode_config;
edde3617 10285 struct drm_atomic_state *state = NULL, *restore_state = NULL;
944b0c76 10286 struct drm_connector_state *connector_state;
4be07317 10287 struct intel_crtc_state *crtc_state;
51fd371b 10288 int ret, i = -1;
79e53945 10289
d2dff872 10290 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 10291 connector->base.id, connector->name,
8e329a03 10292 encoder->base.id, encoder->name);
d2dff872 10293
edde3617
ML
10294 old->restore_state = NULL;
10295
51fd371b
RC
10296retry:
10297 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10298 if (ret)
ad3c558f 10299 goto fail;
6e9f798d 10300
79e53945
JB
10301 /*
10302 * Algorithm gets a little messy:
7a5e4805 10303 *
79e53945
JB
10304 * - if the connector already has an assigned crtc, use it (but make
10305 * sure it's on first)
7a5e4805 10306 *
79e53945
JB
10307 * - try to find the first unused crtc that can drive this connector,
10308 * and use that if we find one
79e53945
JB
10309 */
10310
10311 /* See if we already have a CRTC for this connector */
edde3617
ML
10312 if (connector->state->crtc) {
10313 crtc = connector->state->crtc;
8261b191 10314
51fd371b 10315 ret = drm_modeset_lock(&crtc->mutex, ctx);
4d02e2de 10316 if (ret)
ad3c558f 10317 goto fail;
8261b191
CW
10318
10319 /* Make sure the crtc and connector are running */
edde3617 10320 goto found;
79e53945
JB
10321 }
10322
10323 /* Find an unused one (if possible) */
70e1e0ec 10324 for_each_crtc(dev, possible_crtc) {
79e53945
JB
10325 i++;
10326 if (!(encoder->possible_crtcs & (1 << i)))
10327 continue;
edde3617
ML
10328
10329 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
10330 if (ret)
10331 goto fail;
10332
10333 if (possible_crtc->state->enable) {
10334 drm_modeset_unlock(&possible_crtc->mutex);
a459249c 10335 continue;
edde3617 10336 }
a459249c
VS
10337
10338 crtc = possible_crtc;
10339 break;
79e53945
JB
10340 }
10341
10342 /*
10343 * If we didn't find an unused CRTC, don't use any.
10344 */
10345 if (!crtc) {
7173188d 10346 DRM_DEBUG_KMS("no pipe available for load-detect\n");
ad3c558f 10347 goto fail;
79e53945
JB
10348 }
10349
edde3617
ML
10350found:
10351 intel_crtc = to_intel_crtc(crtc);
10352
4d02e2de
DV
10353 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10354 if (ret)
ad3c558f 10355 goto fail;
79e53945 10356
83a57153 10357 state = drm_atomic_state_alloc(dev);
edde3617
ML
10358 restore_state = drm_atomic_state_alloc(dev);
10359 if (!state || !restore_state) {
10360 ret = -ENOMEM;
10361 goto fail;
10362 }
83a57153
ACO
10363
10364 state->acquire_ctx = ctx;
edde3617 10365 restore_state->acquire_ctx = ctx;
83a57153 10366
944b0c76
ACO
10367 connector_state = drm_atomic_get_connector_state(state, connector);
10368 if (IS_ERR(connector_state)) {
10369 ret = PTR_ERR(connector_state);
10370 goto fail;
10371 }
10372
edde3617
ML
10373 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
10374 if (ret)
10375 goto fail;
944b0c76 10376
4be07317
ACO
10377 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10378 if (IS_ERR(crtc_state)) {
10379 ret = PTR_ERR(crtc_state);
10380 goto fail;
10381 }
10382
49d6fa21 10383 crtc_state->base.active = crtc_state->base.enable = true;
4be07317 10384
6492711d
CW
10385 if (!mode)
10386 mode = &load_detect_mode;
79e53945 10387
d2dff872
CW
10388 /* We need a framebuffer large enough to accommodate all accesses
10389 * that the plane may generate whilst we perform load detection.
10390 * We can not rely on the fbcon either being present (we get called
10391 * during its initialisation to detect all boot displays, or it may
10392 * not even exist) or that it is large enough to satisfy the
10393 * requested mode.
10394 */
94352cf9
DV
10395 fb = mode_fits_in_fbdev(dev, mode);
10396 if (fb == NULL) {
d2dff872 10397 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9 10398 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
d2dff872
CW
10399 } else
10400 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 10401 if (IS_ERR(fb)) {
d2dff872 10402 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
412b61d8 10403 goto fail;
79e53945 10404 }
79e53945 10405
d3a40d1b
ACO
10406 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10407 if (ret)
10408 goto fail;
10409
edde3617
ML
10410 drm_framebuffer_unreference(fb);
10411
10412 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
10413 if (ret)
10414 goto fail;
10415
10416 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
10417 if (!ret)
10418 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
10419 if (!ret)
10420 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
10421 if (ret) {
10422 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
10423 goto fail;
10424 }
8c7b5ccb 10425
3ba86073
ML
10426 ret = drm_atomic_commit(state);
10427 if (ret) {
6492711d 10428 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
412b61d8 10429 goto fail;
79e53945 10430 }
edde3617
ML
10431
10432 old->restore_state = restore_state;
7173188d 10433
79e53945 10434 /* let the connector get through one full cycle before testing */
9d0498a2 10435 intel_wait_for_vblank(dev, intel_crtc->pipe);
7173188d 10436 return true;
412b61d8 10437
ad3c558f 10438fail:
e5d958ef 10439 drm_atomic_state_free(state);
edde3617
ML
10440 drm_atomic_state_free(restore_state);
10441 restore_state = state = NULL;
83a57153 10442
51fd371b
RC
10443 if (ret == -EDEADLK) {
10444 drm_modeset_backoff(ctx);
10445 goto retry;
10446 }
10447
412b61d8 10448 return false;
79e53945
JB
10449}
10450
d2434ab7 10451void intel_release_load_detect_pipe(struct drm_connector *connector,
49172fee
ACO
10452 struct intel_load_detect_pipe *old,
10453 struct drm_modeset_acquire_ctx *ctx)
79e53945 10454{
d2434ab7
DV
10455 struct intel_encoder *intel_encoder =
10456 intel_attached_encoder(connector);
4ef69c7a 10457 struct drm_encoder *encoder = &intel_encoder->base;
edde3617 10458 struct drm_atomic_state *state = old->restore_state;
d3a40d1b 10459 int ret;
79e53945 10460
d2dff872 10461 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 10462 connector->base.id, connector->name,
8e329a03 10463 encoder->base.id, encoder->name);
d2dff872 10464
edde3617 10465 if (!state)
0622a53c 10466 return;
79e53945 10467
edde3617
ML
10468 ret = drm_atomic_commit(state);
10469 if (ret) {
10470 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
10471 drm_atomic_state_free(state);
10472 }
79e53945
JB
10473}
10474
da4a1efa 10475static int i9xx_pll_refclk(struct drm_device *dev,
5cec258b 10476 const struct intel_crtc_state *pipe_config)
da4a1efa
VS
10477{
10478 struct drm_i915_private *dev_priv = dev->dev_private;
10479 u32 dpll = pipe_config->dpll_hw_state.dpll;
10480
10481 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 10482 return dev_priv->vbt.lvds_ssc_freq;
da4a1efa
VS
10483 else if (HAS_PCH_SPLIT(dev))
10484 return 120000;
10485 else if (!IS_GEN2(dev))
10486 return 96000;
10487 else
10488 return 48000;
10489}
10490
79e53945 10491/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc 10492static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 10493 struct intel_crtc_state *pipe_config)
79e53945 10494{
f1f644dc 10495 struct drm_device *dev = crtc->base.dev;
79e53945 10496 struct drm_i915_private *dev_priv = dev->dev_private;
f1f644dc 10497 int pipe = pipe_config->cpu_transcoder;
293623f7 10498 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945
JB
10499 u32 fp;
10500 intel_clock_t clock;
dccbea3b 10501 int port_clock;
da4a1efa 10502 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
10503
10504 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 10505 fp = pipe_config->dpll_hw_state.fp0;
79e53945 10506 else
293623f7 10507 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
10508
10509 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
f2b115e6
AJ
10510 if (IS_PINEVIEW(dev)) {
10511 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10512 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
10513 } else {
10514 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10515 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10516 }
10517
a6c45cf0 10518 if (!IS_GEN2(dev)) {
f2b115e6
AJ
10519 if (IS_PINEVIEW(dev))
10520 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10521 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
10522 else
10523 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
10524 DPLL_FPA01_P1_POST_DIV_SHIFT);
10525
10526 switch (dpll & DPLL_MODE_MASK) {
10527 case DPLLB_MODE_DAC_SERIAL:
10528 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10529 5 : 10;
10530 break;
10531 case DPLLB_MODE_LVDS:
10532 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10533 7 : 14;
10534 break;
10535 default:
28c97730 10536 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 10537 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 10538 return;
79e53945
JB
10539 }
10540
ac58c3f0 10541 if (IS_PINEVIEW(dev))
dccbea3b 10542 port_clock = pnv_calc_dpll_params(refclk, &clock);
ac58c3f0 10543 else
dccbea3b 10544 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945 10545 } else {
0fb58223 10546 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
b1c560d1 10547 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
10548
10549 if (is_lvds) {
10550 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10551 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
10552
10553 if (lvds & LVDS_CLKB_POWER_UP)
10554 clock.p2 = 7;
10555 else
10556 clock.p2 = 14;
79e53945
JB
10557 } else {
10558 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10559 clock.p1 = 2;
10560 else {
10561 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10562 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10563 }
10564 if (dpll & PLL_P2_DIVIDE_BY_4)
10565 clock.p2 = 4;
10566 else
10567 clock.p2 = 2;
79e53945 10568 }
da4a1efa 10569
dccbea3b 10570 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945
JB
10571 }
10572
18442d08
VS
10573 /*
10574 * This value includes pixel_multiplier. We will use
241bfc38 10575 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
10576 * encoder's get_config() function.
10577 */
dccbea3b 10578 pipe_config->port_clock = port_clock;
f1f644dc
JB
10579}
10580
6878da05
VS
10581int intel_dotclock_calculate(int link_freq,
10582 const struct intel_link_m_n *m_n)
f1f644dc 10583{
f1f644dc
JB
10584 /*
10585 * The calculation for the data clock is:
1041a02f 10586 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 10587 * But we want to avoid losing precison if possible, so:
1041a02f 10588 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
10589 *
10590 * and the link clock is simpler:
1041a02f 10591 * link_clock = (m * link_clock) / n
f1f644dc
JB
10592 */
10593
6878da05
VS
10594 if (!m_n->link_n)
10595 return 0;
f1f644dc 10596
6878da05
VS
10597 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10598}
f1f644dc 10599
18442d08 10600static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 10601 struct intel_crtc_state *pipe_config)
6878da05 10602{
e3b247da 10603 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
79e53945 10604
18442d08
VS
10605 /* read out port_clock from the DPLL */
10606 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 10607
f1f644dc 10608 /*
e3b247da
VS
10609 * In case there is an active pipe without active ports,
10610 * we may need some idea for the dotclock anyway.
10611 * Calculate one based on the FDI configuration.
79e53945 10612 */
2d112de7 10613 pipe_config->base.adjusted_mode.crtc_clock =
21a727b3 10614 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
18442d08 10615 &pipe_config->fdi_m_n);
79e53945
JB
10616}
10617
10618/** Returns the currently programmed mode of the given pipe. */
10619struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10620 struct drm_crtc *crtc)
10621{
548f245b 10622 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 10623 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 10624 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
79e53945 10625 struct drm_display_mode *mode;
3f36b937 10626 struct intel_crtc_state *pipe_config;
fe2b8f9d
PZ
10627 int htot = I915_READ(HTOTAL(cpu_transcoder));
10628 int hsync = I915_READ(HSYNC(cpu_transcoder));
10629 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10630 int vsync = I915_READ(VSYNC(cpu_transcoder));
293623f7 10631 enum pipe pipe = intel_crtc->pipe;
79e53945
JB
10632
10633 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10634 if (!mode)
10635 return NULL;
10636
3f36b937
TU
10637 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10638 if (!pipe_config) {
10639 kfree(mode);
10640 return NULL;
10641 }
10642
f1f644dc
JB
10643 /*
10644 * Construct a pipe_config sufficient for getting the clock info
10645 * back out of crtc_clock_get.
10646 *
10647 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10648 * to use a real value here instead.
10649 */
3f36b937
TU
10650 pipe_config->cpu_transcoder = (enum transcoder) pipe;
10651 pipe_config->pixel_multiplier = 1;
10652 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10653 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10654 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
10655 i9xx_crtc_clock_get(intel_crtc, pipe_config);
10656
10657 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
79e53945
JB
10658 mode->hdisplay = (htot & 0xffff) + 1;
10659 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10660 mode->hsync_start = (hsync & 0xffff) + 1;
10661 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10662 mode->vdisplay = (vtot & 0xffff) + 1;
10663 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10664 mode->vsync_start = (vsync & 0xffff) + 1;
10665 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10666
10667 drm_mode_set_name(mode);
79e53945 10668
3f36b937
TU
10669 kfree(pipe_config);
10670
79e53945
JB
10671 return mode;
10672}
10673
f047e395
CW
10674void intel_mark_busy(struct drm_device *dev)
10675{
c67a470b
PZ
10676 struct drm_i915_private *dev_priv = dev->dev_private;
10677
f62a0076
CW
10678 if (dev_priv->mm.busy)
10679 return;
10680
43694d69 10681 intel_runtime_pm_get(dev_priv);
c67a470b 10682 i915_update_gfx_val(dev_priv);
43cf3bf0
CW
10683 if (INTEL_INFO(dev)->gen >= 6)
10684 gen6_rps_busy(dev_priv);
f62a0076 10685 dev_priv->mm.busy = true;
f047e395
CW
10686}
10687
10688void intel_mark_idle(struct drm_device *dev)
652c393a 10689{
c67a470b 10690 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 10691
f62a0076
CW
10692 if (!dev_priv->mm.busy)
10693 return;
10694
10695 dev_priv->mm.busy = false;
10696
3d13ef2e 10697 if (INTEL_INFO(dev)->gen >= 6)
b29c19b6 10698 gen6_rps_idle(dev->dev_private);
bb4cdd53 10699
43694d69 10700 intel_runtime_pm_put(dev_priv);
652c393a
JB
10701}
10702
79e53945
JB
10703static void intel_crtc_destroy(struct drm_crtc *crtc)
10704{
10705 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
67e77c5a
DV
10706 struct drm_device *dev = crtc->dev;
10707 struct intel_unpin_work *work;
67e77c5a 10708
5e2d7afc 10709 spin_lock_irq(&dev->event_lock);
67e77c5a
DV
10710 work = intel_crtc->unpin_work;
10711 intel_crtc->unpin_work = NULL;
5e2d7afc 10712 spin_unlock_irq(&dev->event_lock);
67e77c5a
DV
10713
10714 if (work) {
10715 cancel_work_sync(&work->work);
10716 kfree(work);
10717 }
79e53945
JB
10718
10719 drm_crtc_cleanup(crtc);
67e77c5a 10720
79e53945
JB
10721 kfree(intel_crtc);
10722}
10723
6b95a207
KH
10724static void intel_unpin_work_fn(struct work_struct *__work)
10725{
10726 struct intel_unpin_work *work =
10727 container_of(__work, struct intel_unpin_work, work);
a9ff8714
VS
10728 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10729 struct drm_device *dev = crtc->base.dev;
10730 struct drm_plane *primary = crtc->base.primary;
6b95a207 10731
b4a98e57 10732 mutex_lock(&dev->struct_mutex);
3465c580 10733 intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
05394f39 10734 drm_gem_object_unreference(&work->pending_flip_obj->base);
d9e86c0e 10735
f06cc1b9 10736 if (work->flip_queued_req)
146d84f0 10737 i915_gem_request_assign(&work->flip_queued_req, NULL);
b4a98e57
CW
10738 mutex_unlock(&dev->struct_mutex);
10739
a9ff8714 10740 intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
1eb52238 10741 intel_fbc_post_update(crtc);
89ed88ba 10742 drm_framebuffer_unreference(work->old_fb);
f99d7069 10743
a9ff8714
VS
10744 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
10745 atomic_dec(&crtc->unpin_work_count);
b4a98e57 10746
6b95a207
KH
10747 kfree(work);
10748}
10749
1afe3e9d 10750static void do_intel_finish_page_flip(struct drm_device *dev,
49b14a5c 10751 struct drm_crtc *crtc)
6b95a207 10752{
6b95a207
KH
10753 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10754 struct intel_unpin_work *work;
6b95a207
KH
10755 unsigned long flags;
10756
10757 /* Ignore early vblank irqs */
10758 if (intel_crtc == NULL)
10759 return;
10760
f326038a
DV
10761 /*
10762 * This is called both by irq handlers and the reset code (to complete
10763 * lost pageflips) so needs the full irqsave spinlocks.
10764 */
6b95a207
KH
10765 spin_lock_irqsave(&dev->event_lock, flags);
10766 work = intel_crtc->unpin_work;
e7d841ca
CW
10767
10768 /* Ensure we don't miss a work->pending update ... */
10769 smp_rmb();
10770
10771 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
6b95a207
KH
10772 spin_unlock_irqrestore(&dev->event_lock, flags);
10773 return;
10774 }
10775
d6bbafa1 10776 page_flip_completed(intel_crtc);
0af7e4df 10777
6b95a207 10778 spin_unlock_irqrestore(&dev->event_lock, flags);
6b95a207
KH
10779}
10780
1afe3e9d
JB
10781void intel_finish_page_flip(struct drm_device *dev, int pipe)
10782{
fbee40df 10783 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
10784 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10785
49b14a5c 10786 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
10787}
10788
10789void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10790{
fbee40df 10791 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
10792 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10793
49b14a5c 10794 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
10795}
10796
75f7f3ec
VS
10797/* Is 'a' after or equal to 'b'? */
10798static bool g4x_flip_count_after_eq(u32 a, u32 b)
10799{
10800 return !((a - b) & 0x80000000);
10801}
10802
10803static bool page_flip_finished(struct intel_crtc *crtc)
10804{
10805 struct drm_device *dev = crtc->base.dev;
10806 struct drm_i915_private *dev_priv = dev->dev_private;
10807
bdfa7542
VS
10808 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10809 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10810 return true;
10811
75f7f3ec
VS
10812 /*
10813 * The relevant registers doen't exist on pre-ctg.
10814 * As the flip done interrupt doesn't trigger for mmio
10815 * flips on gmch platforms, a flip count check isn't
10816 * really needed there. But since ctg has the registers,
10817 * include it in the check anyway.
10818 */
10819 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10820 return true;
10821
e8861675
ML
10822 /*
10823 * BDW signals flip done immediately if the plane
10824 * is disabled, even if the plane enable is already
10825 * armed to occur at the next vblank :(
10826 */
10827
75f7f3ec
VS
10828 /*
10829 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10830 * used the same base address. In that case the mmio flip might
10831 * have completed, but the CS hasn't even executed the flip yet.
10832 *
10833 * A flip count check isn't enough as the CS might have updated
10834 * the base address just after start of vblank, but before we
10835 * managed to process the interrupt. This means we'd complete the
10836 * CS flip too soon.
10837 *
10838 * Combining both checks should get us a good enough result. It may
10839 * still happen that the CS flip has been executed, but has not
10840 * yet actually completed. But in case the base address is the same
10841 * anyway, we don't really care.
10842 */
10843 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10844 crtc->unpin_work->gtt_offset &&
fd8f507c 10845 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
75f7f3ec
VS
10846 crtc->unpin_work->flip_count);
10847}
10848
6b95a207
KH
10849void intel_prepare_page_flip(struct drm_device *dev, int plane)
10850{
fbee40df 10851 struct drm_i915_private *dev_priv = dev->dev_private;
6b95a207
KH
10852 struct intel_crtc *intel_crtc =
10853 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10854 unsigned long flags;
10855
f326038a
DV
10856
10857 /*
10858 * This is called both by irq handlers and the reset code (to complete
10859 * lost pageflips) so needs the full irqsave spinlocks.
10860 *
10861 * NB: An MMIO update of the plane base pointer will also
e7d841ca
CW
10862 * generate a page-flip completion irq, i.e. every modeset
10863 * is also accompanied by a spurious intel_prepare_page_flip().
10864 */
6b95a207 10865 spin_lock_irqsave(&dev->event_lock, flags);
75f7f3ec 10866 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
e7d841ca 10867 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
6b95a207
KH
10868 spin_unlock_irqrestore(&dev->event_lock, flags);
10869}
10870
6042639c 10871static inline void intel_mark_page_flip_active(struct intel_unpin_work *work)
e7d841ca
CW
10872{
10873 /* Ensure that the work item is consistent when activating it ... */
10874 smp_wmb();
6042639c 10875 atomic_set(&work->pending, INTEL_FLIP_PENDING);
e7d841ca
CW
10876 /* and that it is marked active as soon as the irq could fire. */
10877 smp_wmb();
10878}
10879
8c9f3aaf
JB
10880static int intel_gen2_queue_flip(struct drm_device *dev,
10881 struct drm_crtc *crtc,
10882 struct drm_framebuffer *fb,
ed8d1975 10883 struct drm_i915_gem_object *obj,
6258fbe2 10884 struct drm_i915_gem_request *req,
ed8d1975 10885 uint32_t flags)
8c9f3aaf 10886{
4a570db5 10887 struct intel_engine_cs *engine = req->engine;
8c9f3aaf 10888 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
10889 u32 flip_mask;
10890 int ret;
10891
5fb9de1a 10892 ret = intel_ring_begin(req, 6);
8c9f3aaf 10893 if (ret)
4fa62c89 10894 return ret;
8c9f3aaf
JB
10895
10896 /* Can't queue multiple flips, so wait for the previous
10897 * one to finish before executing the next.
10898 */
10899 if (intel_crtc->plane)
10900 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10901 else
10902 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
e2f80391
TU
10903 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
10904 intel_ring_emit(engine, MI_NOOP);
10905 intel_ring_emit(engine, MI_DISPLAY_FLIP |
6d90c952 10906 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
e2f80391
TU
10907 intel_ring_emit(engine, fb->pitches[0]);
10908 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
10909 intel_ring_emit(engine, 0); /* aux display base address, unused */
e7d841ca 10910
6042639c 10911 intel_mark_page_flip_active(intel_crtc->unpin_work);
83d4092b 10912 return 0;
8c9f3aaf
JB
10913}
10914
10915static int intel_gen3_queue_flip(struct drm_device *dev,
10916 struct drm_crtc *crtc,
10917 struct drm_framebuffer *fb,
ed8d1975 10918 struct drm_i915_gem_object *obj,
6258fbe2 10919 struct drm_i915_gem_request *req,
ed8d1975 10920 uint32_t flags)
8c9f3aaf 10921{
4a570db5 10922 struct intel_engine_cs *engine = req->engine;
8c9f3aaf 10923 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
10924 u32 flip_mask;
10925 int ret;
10926
5fb9de1a 10927 ret = intel_ring_begin(req, 6);
8c9f3aaf 10928 if (ret)
4fa62c89 10929 return ret;
8c9f3aaf
JB
10930
10931 if (intel_crtc->plane)
10932 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10933 else
10934 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
e2f80391
TU
10935 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
10936 intel_ring_emit(engine, MI_NOOP);
10937 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 |
6d90c952 10938 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
e2f80391
TU
10939 intel_ring_emit(engine, fb->pitches[0]);
10940 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
10941 intel_ring_emit(engine, MI_NOOP);
6d90c952 10942
6042639c 10943 intel_mark_page_flip_active(intel_crtc->unpin_work);
83d4092b 10944 return 0;
8c9f3aaf
JB
10945}
10946
10947static int intel_gen4_queue_flip(struct drm_device *dev,
10948 struct drm_crtc *crtc,
10949 struct drm_framebuffer *fb,
ed8d1975 10950 struct drm_i915_gem_object *obj,
6258fbe2 10951 struct drm_i915_gem_request *req,
ed8d1975 10952 uint32_t flags)
8c9f3aaf 10953{
4a570db5 10954 struct intel_engine_cs *engine = req->engine;
8c9f3aaf
JB
10955 struct drm_i915_private *dev_priv = dev->dev_private;
10956 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10957 uint32_t pf, pipesrc;
10958 int ret;
10959
5fb9de1a 10960 ret = intel_ring_begin(req, 4);
8c9f3aaf 10961 if (ret)
4fa62c89 10962 return ret;
8c9f3aaf
JB
10963
10964 /* i965+ uses the linear or tiled offsets from the
10965 * Display Registers (which do not change across a page-flip)
10966 * so we need only reprogram the base address.
10967 */
e2f80391 10968 intel_ring_emit(engine, MI_DISPLAY_FLIP |
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 |
c2c75131 10972 obj->tiling_mode);
8c9f3aaf
JB
10973
10974 /* XXX Enabling the panel-fitter across page-flip is so far
10975 * untested on non-native modes, so ignore it for now.
10976 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
10977 */
10978 pf = 0;
10979 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
e2f80391 10980 intel_ring_emit(engine, pf | pipesrc);
e7d841ca 10981
6042639c 10982 intel_mark_page_flip_active(intel_crtc->unpin_work);
83d4092b 10983 return 0;
8c9f3aaf
JB
10984}
10985
10986static int intel_gen6_queue_flip(struct drm_device *dev,
10987 struct drm_crtc *crtc,
10988 struct drm_framebuffer *fb,
ed8d1975 10989 struct drm_i915_gem_object *obj,
6258fbe2 10990 struct drm_i915_gem_request *req,
ed8d1975 10991 uint32_t flags)
8c9f3aaf 10992{
4a570db5 10993 struct intel_engine_cs *engine = req->engine;
8c9f3aaf
JB
10994 struct drm_i915_private *dev_priv = dev->dev_private;
10995 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10996 uint32_t pf, pipesrc;
10997 int ret;
10998
5fb9de1a 10999 ret = intel_ring_begin(req, 4);
8c9f3aaf 11000 if (ret)
4fa62c89 11001 return ret;
8c9f3aaf 11002
e2f80391 11003 intel_ring_emit(engine, MI_DISPLAY_FLIP |
6d90c952 11004 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
e2f80391
TU
11005 intel_ring_emit(engine, fb->pitches[0] | obj->tiling_mode);
11006 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
8c9f3aaf 11007
dc257cf1
DV
11008 /* Contrary to the suggestions in the documentation,
11009 * "Enable Panel Fitter" does not seem to be required when page
11010 * flipping with a non-native mode, and worse causes a normal
11011 * modeset to fail.
11012 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11013 */
11014 pf = 0;
8c9f3aaf 11015 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
e2f80391 11016 intel_ring_emit(engine, pf | pipesrc);
e7d841ca 11017
6042639c 11018 intel_mark_page_flip_active(intel_crtc->unpin_work);
83d4092b 11019 return 0;
8c9f3aaf
JB
11020}
11021
7c9017e5
JB
11022static int intel_gen7_queue_flip(struct drm_device *dev,
11023 struct drm_crtc *crtc,
11024 struct drm_framebuffer *fb,
ed8d1975 11025 struct drm_i915_gem_object *obj,
6258fbe2 11026 struct drm_i915_gem_request *req,
ed8d1975 11027 uint32_t flags)
7c9017e5 11028{
4a570db5 11029 struct intel_engine_cs *engine = req->engine;
7c9017e5 11030 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cb05d8de 11031 uint32_t plane_bit = 0;
ffe74d75
CW
11032 int len, ret;
11033
eba905b2 11034 switch (intel_crtc->plane) {
cb05d8de
DV
11035 case PLANE_A:
11036 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11037 break;
11038 case PLANE_B:
11039 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11040 break;
11041 case PLANE_C:
11042 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11043 break;
11044 default:
11045 WARN_ONCE(1, "unknown plane in flip command\n");
4fa62c89 11046 return -ENODEV;
cb05d8de
DV
11047 }
11048
ffe74d75 11049 len = 4;
e2f80391 11050 if (engine->id == RCS) {
ffe74d75 11051 len += 6;
f476828a
DL
11052 /*
11053 * On Gen 8, SRM is now taking an extra dword to accommodate
11054 * 48bits addresses, and we need a NOOP for the batch size to
11055 * stay even.
11056 */
11057 if (IS_GEN8(dev))
11058 len += 2;
11059 }
ffe74d75 11060
f66fab8e
VS
11061 /*
11062 * BSpec MI_DISPLAY_FLIP for IVB:
11063 * "The full packet must be contained within the same cache line."
11064 *
11065 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11066 * cacheline, if we ever start emitting more commands before
11067 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11068 * then do the cacheline alignment, and finally emit the
11069 * MI_DISPLAY_FLIP.
11070 */
bba09b12 11071 ret = intel_ring_cacheline_align(req);
f66fab8e 11072 if (ret)
4fa62c89 11073 return ret;
f66fab8e 11074
5fb9de1a 11075 ret = intel_ring_begin(req, len);
7c9017e5 11076 if (ret)
4fa62c89 11077 return ret;
7c9017e5 11078
ffe74d75
CW
11079 /* Unmask the flip-done completion message. Note that the bspec says that
11080 * we should do this for both the BCS and RCS, and that we must not unmask
11081 * more than one flip event at any time (or ensure that one flip message
11082 * can be sent by waiting for flip-done prior to queueing new flips).
11083 * Experimentation says that BCS works despite DERRMR masking all
11084 * flip-done completion events and that unmasking all planes at once
11085 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11086 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11087 */
e2f80391
TU
11088 if (engine->id == RCS) {
11089 intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(1));
11090 intel_ring_emit_reg(engine, DERRMR);
11091 intel_ring_emit(engine, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11092 DERRMR_PIPEB_PRI_FLIP_DONE |
11093 DERRMR_PIPEC_PRI_FLIP_DONE));
f476828a 11094 if (IS_GEN8(dev))
e2f80391 11095 intel_ring_emit(engine, MI_STORE_REGISTER_MEM_GEN8 |
f476828a
DL
11096 MI_SRM_LRM_GLOBAL_GTT);
11097 else
e2f80391 11098 intel_ring_emit(engine, MI_STORE_REGISTER_MEM |
f476828a 11099 MI_SRM_LRM_GLOBAL_GTT);
e2f80391
TU
11100 intel_ring_emit_reg(engine, DERRMR);
11101 intel_ring_emit(engine, engine->scratch.gtt_offset + 256);
f476828a 11102 if (IS_GEN8(dev)) {
e2f80391
TU
11103 intel_ring_emit(engine, 0);
11104 intel_ring_emit(engine, MI_NOOP);
f476828a 11105 }
ffe74d75
CW
11106 }
11107
e2f80391
TU
11108 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 | plane_bit);
11109 intel_ring_emit(engine, (fb->pitches[0] | obj->tiling_mode));
11110 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
11111 intel_ring_emit(engine, (MI_NOOP));
e7d841ca 11112
6042639c 11113 intel_mark_page_flip_active(intel_crtc->unpin_work);
83d4092b 11114 return 0;
7c9017e5
JB
11115}
11116
0bc40be8 11117static bool use_mmio_flip(struct intel_engine_cs *engine,
84c33a64
SG
11118 struct drm_i915_gem_object *obj)
11119{
11120 /*
11121 * This is not being used for older platforms, because
11122 * non-availability of flip done interrupt forces us to use
11123 * CS flips. Older platforms derive flip done using some clever
11124 * tricks involving the flip_pending status bits and vblank irqs.
11125 * So using MMIO flips there would disrupt this mechanism.
11126 */
11127
0bc40be8 11128 if (engine == NULL)
8e09bf83
CW
11129 return true;
11130
0bc40be8 11131 if (INTEL_INFO(engine->dev)->gen < 5)
84c33a64
SG
11132 return false;
11133
11134 if (i915.use_mmio_flip < 0)
11135 return false;
11136 else if (i915.use_mmio_flip > 0)
11137 return true;
14bf993e
OM
11138 else if (i915.enable_execlists)
11139 return true;
fd8e058a
AG
11140 else if (obj->base.dma_buf &&
11141 !reservation_object_test_signaled_rcu(obj->base.dma_buf->resv,
11142 false))
11143 return true;
84c33a64 11144 else
666796da 11145 return engine != i915_gem_request_get_engine(obj->last_write_req);
84c33a64
SG
11146}
11147
6042639c 11148static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
86efe24a 11149 unsigned int rotation,
6042639c 11150 struct intel_unpin_work *work)
ff944564
DL
11151{
11152 struct drm_device *dev = intel_crtc->base.dev;
11153 struct drm_i915_private *dev_priv = dev->dev_private;
11154 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
ff944564 11155 const enum pipe pipe = intel_crtc->pipe;
86efe24a 11156 u32 ctl, stride, tile_height;
ff944564
DL
11157
11158 ctl = I915_READ(PLANE_CTL(pipe, 0));
11159 ctl &= ~PLANE_CTL_TILED_MASK;
2ebef630
TU
11160 switch (fb->modifier[0]) {
11161 case DRM_FORMAT_MOD_NONE:
11162 break;
11163 case I915_FORMAT_MOD_X_TILED:
ff944564 11164 ctl |= PLANE_CTL_TILED_X;
2ebef630
TU
11165 break;
11166 case I915_FORMAT_MOD_Y_TILED:
11167 ctl |= PLANE_CTL_TILED_Y;
11168 break;
11169 case I915_FORMAT_MOD_Yf_TILED:
11170 ctl |= PLANE_CTL_TILED_YF;
11171 break;
11172 default:
11173 MISSING_CASE(fb->modifier[0]);
11174 }
ff944564
DL
11175
11176 /*
11177 * The stride is either expressed as a multiple of 64 bytes chunks for
11178 * linear buffers or in number of tiles for tiled buffers.
11179 */
86efe24a
TU
11180 if (intel_rotation_90_or_270(rotation)) {
11181 /* stride = Surface height in tiles */
832be82f 11182 tile_height = intel_tile_height(dev_priv, fb->modifier[0], 0);
86efe24a
TU
11183 stride = DIV_ROUND_UP(fb->height, tile_height);
11184 } else {
11185 stride = fb->pitches[0] /
7b49f948
VS
11186 intel_fb_stride_alignment(dev_priv, fb->modifier[0],
11187 fb->pixel_format);
86efe24a 11188 }
ff944564
DL
11189
11190 /*
11191 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11192 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11193 */
11194 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11195 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11196
6042639c 11197 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
ff944564
DL
11198 POSTING_READ(PLANE_SURF(pipe, 0));
11199}
11200
6042639c
CW
11201static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
11202 struct intel_unpin_work *work)
84c33a64
SG
11203{
11204 struct drm_device *dev = intel_crtc->base.dev;
11205 struct drm_i915_private *dev_priv = dev->dev_private;
11206 struct intel_framebuffer *intel_fb =
11207 to_intel_framebuffer(intel_crtc->base.primary->fb);
11208 struct drm_i915_gem_object *obj = intel_fb->obj;
f0f59a00 11209 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
84c33a64 11210 u32 dspcntr;
84c33a64 11211
84c33a64
SG
11212 dspcntr = I915_READ(reg);
11213
c5d97472
DL
11214 if (obj->tiling_mode != I915_TILING_NONE)
11215 dspcntr |= DISPPLANE_TILED;
11216 else
11217 dspcntr &= ~DISPPLANE_TILED;
11218
84c33a64
SG
11219 I915_WRITE(reg, dspcntr);
11220
6042639c 11221 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
84c33a64 11222 POSTING_READ(DSPSURF(intel_crtc->plane));
ff944564
DL
11223}
11224
11225/*
11226 * XXX: This is the temporary way to update the plane registers until we get
11227 * around to using the usual plane update functions for MMIO flips
11228 */
6042639c 11229static void intel_do_mmio_flip(struct intel_mmio_flip *mmio_flip)
ff944564 11230{
6042639c
CW
11231 struct intel_crtc *crtc = mmio_flip->crtc;
11232 struct intel_unpin_work *work;
11233
11234 spin_lock_irq(&crtc->base.dev->event_lock);
11235 work = crtc->unpin_work;
11236 spin_unlock_irq(&crtc->base.dev->event_lock);
11237 if (work == NULL)
11238 return;
ff944564 11239
6042639c 11240 intel_mark_page_flip_active(work);
ff944564 11241
6042639c 11242 intel_pipe_update_start(crtc);
ff944564 11243
6042639c 11244 if (INTEL_INFO(mmio_flip->i915)->gen >= 9)
86efe24a 11245 skl_do_mmio_flip(crtc, mmio_flip->rotation, work);
ff944564
DL
11246 else
11247 /* use_mmio_flip() retricts MMIO flips to ilk+ */
6042639c 11248 ilk_do_mmio_flip(crtc, work);
ff944564 11249
6042639c 11250 intel_pipe_update_end(crtc);
84c33a64
SG
11251}
11252
9362c7c5 11253static void intel_mmio_flip_work_func(struct work_struct *work)
84c33a64 11254{
b2cfe0ab
CW
11255 struct intel_mmio_flip *mmio_flip =
11256 container_of(work, struct intel_mmio_flip, work);
fd8e058a
AG
11257 struct intel_framebuffer *intel_fb =
11258 to_intel_framebuffer(mmio_flip->crtc->base.primary->fb);
11259 struct drm_i915_gem_object *obj = intel_fb->obj;
84c33a64 11260
6042639c 11261 if (mmio_flip->req) {
eed29a5b 11262 WARN_ON(__i915_wait_request(mmio_flip->req,
b2cfe0ab 11263 mmio_flip->crtc->reset_counter,
bcafc4e3
CW
11264 false, NULL,
11265 &mmio_flip->i915->rps.mmioflips));
6042639c
CW
11266 i915_gem_request_unreference__unlocked(mmio_flip->req);
11267 }
84c33a64 11268
fd8e058a
AG
11269 /* For framebuffer backed by dmabuf, wait for fence */
11270 if (obj->base.dma_buf)
11271 WARN_ON(reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
11272 false, false,
11273 MAX_SCHEDULE_TIMEOUT) < 0);
11274
6042639c 11275 intel_do_mmio_flip(mmio_flip);
b2cfe0ab 11276 kfree(mmio_flip);
84c33a64
SG
11277}
11278
11279static int intel_queue_mmio_flip(struct drm_device *dev,
11280 struct drm_crtc *crtc,
86efe24a 11281 struct drm_i915_gem_object *obj)
84c33a64 11282{
b2cfe0ab
CW
11283 struct intel_mmio_flip *mmio_flip;
11284
11285 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
11286 if (mmio_flip == NULL)
11287 return -ENOMEM;
84c33a64 11288
bcafc4e3 11289 mmio_flip->i915 = to_i915(dev);
eed29a5b 11290 mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
b2cfe0ab 11291 mmio_flip->crtc = to_intel_crtc(crtc);
86efe24a 11292 mmio_flip->rotation = crtc->primary->state->rotation;
536f5b5e 11293
b2cfe0ab
CW
11294 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
11295 schedule_work(&mmio_flip->work);
84c33a64 11296
84c33a64
SG
11297 return 0;
11298}
11299
8c9f3aaf
JB
11300static int intel_default_queue_flip(struct drm_device *dev,
11301 struct drm_crtc *crtc,
11302 struct drm_framebuffer *fb,
ed8d1975 11303 struct drm_i915_gem_object *obj,
6258fbe2 11304 struct drm_i915_gem_request *req,
ed8d1975 11305 uint32_t flags)
8c9f3aaf
JB
11306{
11307 return -ENODEV;
11308}
11309
d6bbafa1
CW
11310static bool __intel_pageflip_stall_check(struct drm_device *dev,
11311 struct drm_crtc *crtc)
11312{
11313 struct drm_i915_private *dev_priv = dev->dev_private;
11314 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11315 struct intel_unpin_work *work = intel_crtc->unpin_work;
11316 u32 addr;
11317
11318 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
11319 return true;
11320
908565c2
CW
11321 if (atomic_read(&work->pending) < INTEL_FLIP_PENDING)
11322 return false;
11323
d6bbafa1
CW
11324 if (!work->enable_stall_check)
11325 return false;
11326
11327 if (work->flip_ready_vblank == 0) {
3a8a946e
DV
11328 if (work->flip_queued_req &&
11329 !i915_gem_request_completed(work->flip_queued_req, true))
d6bbafa1
CW
11330 return false;
11331
1e3feefd 11332 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
d6bbafa1
CW
11333 }
11334
1e3feefd 11335 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
d6bbafa1
CW
11336 return false;
11337
11338 /* Potential stall - if we see that the flip has happened,
11339 * assume a missed interrupt. */
11340 if (INTEL_INFO(dev)->gen >= 4)
11341 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11342 else
11343 addr = I915_READ(DSPADDR(intel_crtc->plane));
11344
11345 /* There is a potential issue here with a false positive after a flip
11346 * to the same address. We could address this by checking for a
11347 * non-incrementing frame counter.
11348 */
11349 return addr == work->gtt_offset;
11350}
11351
11352void intel_check_page_flip(struct drm_device *dev, int pipe)
11353{
11354 struct drm_i915_private *dev_priv = dev->dev_private;
11355 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11356 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6ad790c0 11357 struct intel_unpin_work *work;
f326038a 11358
6c51d46f 11359 WARN_ON(!in_interrupt());
d6bbafa1
CW
11360
11361 if (crtc == NULL)
11362 return;
11363
f326038a 11364 spin_lock(&dev->event_lock);
6ad790c0
CW
11365 work = intel_crtc->unpin_work;
11366 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
d6bbafa1 11367 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
6ad790c0 11368 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
d6bbafa1 11369 page_flip_completed(intel_crtc);
6ad790c0 11370 work = NULL;
d6bbafa1 11371 }
6ad790c0
CW
11372 if (work != NULL &&
11373 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
11374 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
f326038a 11375 spin_unlock(&dev->event_lock);
d6bbafa1
CW
11376}
11377
6b95a207
KH
11378static int intel_crtc_page_flip(struct drm_crtc *crtc,
11379 struct drm_framebuffer *fb,
ed8d1975
KP
11380 struct drm_pending_vblank_event *event,
11381 uint32_t page_flip_flags)
6b95a207
KH
11382{
11383 struct drm_device *dev = crtc->dev;
11384 struct drm_i915_private *dev_priv = dev->dev_private;
f4510a27 11385 struct drm_framebuffer *old_fb = crtc->primary->fb;
2ff8fde1 11386 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
6b95a207 11387 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
455a6808 11388 struct drm_plane *primary = crtc->primary;
a071fa00 11389 enum pipe pipe = intel_crtc->pipe;
6b95a207 11390 struct intel_unpin_work *work;
e2f80391 11391 struct intel_engine_cs *engine;
cf5d8a46 11392 bool mmio_flip;
91af127f 11393 struct drm_i915_gem_request *request = NULL;
52e68630 11394 int ret;
6b95a207 11395
2ff8fde1
MR
11396 /*
11397 * drm_mode_page_flip_ioctl() should already catch this, but double
11398 * check to be safe. In the future we may enable pageflipping from
11399 * a disabled primary plane.
11400 */
11401 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11402 return -EBUSY;
11403
e6a595d2 11404 /* Can't change pixel format via MI display flips. */
f4510a27 11405 if (fb->pixel_format != crtc->primary->fb->pixel_format)
e6a595d2
VS
11406 return -EINVAL;
11407
11408 /*
11409 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11410 * Note that pitch changes could also affect these register.
11411 */
11412 if (INTEL_INFO(dev)->gen > 3 &&
f4510a27
MR
11413 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11414 fb->pitches[0] != crtc->primary->fb->pitches[0]))
e6a595d2
VS
11415 return -EINVAL;
11416
f900db47
CW
11417 if (i915_terminally_wedged(&dev_priv->gpu_error))
11418 goto out_hang;
11419
b14c5679 11420 work = kzalloc(sizeof(*work), GFP_KERNEL);
6b95a207
KH
11421 if (work == NULL)
11422 return -ENOMEM;
11423
6b95a207 11424 work->event = event;
b4a98e57 11425 work->crtc = crtc;
ab8d6675 11426 work->old_fb = old_fb;
6b95a207
KH
11427 INIT_WORK(&work->work, intel_unpin_work_fn);
11428
87b6b101 11429 ret = drm_crtc_vblank_get(crtc);
7317c75e
JB
11430 if (ret)
11431 goto free_work;
11432
6b95a207 11433 /* We borrow the event spin lock for protecting unpin_work */
5e2d7afc 11434 spin_lock_irq(&dev->event_lock);
6b95a207 11435 if (intel_crtc->unpin_work) {
d6bbafa1
CW
11436 /* Before declaring the flip queue wedged, check if
11437 * the hardware completed the operation behind our backs.
11438 */
11439 if (__intel_pageflip_stall_check(dev, crtc)) {
11440 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11441 page_flip_completed(intel_crtc);
11442 } else {
11443 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
5e2d7afc 11444 spin_unlock_irq(&dev->event_lock);
468f0b44 11445
d6bbafa1
CW
11446 drm_crtc_vblank_put(crtc);
11447 kfree(work);
11448 return -EBUSY;
11449 }
6b95a207
KH
11450 }
11451 intel_crtc->unpin_work = work;
5e2d7afc 11452 spin_unlock_irq(&dev->event_lock);
6b95a207 11453
b4a98e57
CW
11454 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11455 flush_workqueue(dev_priv->wq);
11456
75dfca80 11457 /* Reference the objects for the scheduled work. */
ab8d6675 11458 drm_framebuffer_reference(work->old_fb);
05394f39 11459 drm_gem_object_reference(&obj->base);
6b95a207 11460
f4510a27 11461 crtc->primary->fb = fb;
afd65eb4 11462 update_state_fb(crtc->primary);
e8216e50 11463 intel_fbc_pre_update(intel_crtc);
1ed1f968 11464
e1f99ce6 11465 work->pending_flip_obj = obj;
e1f99ce6 11466
89ed88ba
CW
11467 ret = i915_mutex_lock_interruptible(dev);
11468 if (ret)
11469 goto cleanup;
11470
b4a98e57 11471 atomic_inc(&intel_crtc->unpin_work_count);
10d83730 11472 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
e1f99ce6 11473
75f7f3ec 11474 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
fd8f507c 11475 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
75f7f3ec 11476
666a4537 11477 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
4a570db5 11478 engine = &dev_priv->engine[BCS];
ab8d6675 11479 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
8e09bf83 11480 /* vlv: DISPLAY_FLIP fails to change tiling */
e2f80391 11481 engine = NULL;
48bf5b2d 11482 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
4a570db5 11483 engine = &dev_priv->engine[BCS];
4fa62c89 11484 } else if (INTEL_INFO(dev)->gen >= 7) {
666796da 11485 engine = i915_gem_request_get_engine(obj->last_write_req);
e2f80391 11486 if (engine == NULL || engine->id != RCS)
4a570db5 11487 engine = &dev_priv->engine[BCS];
4fa62c89 11488 } else {
4a570db5 11489 engine = &dev_priv->engine[RCS];
4fa62c89
VS
11490 }
11491
e2f80391 11492 mmio_flip = use_mmio_flip(engine, obj);
cf5d8a46
CW
11493
11494 /* When using CS flips, we want to emit semaphores between rings.
11495 * However, when using mmio flips we will create a task to do the
11496 * synchronisation, so all we want here is to pin the framebuffer
11497 * into the display plane and skip any waits.
11498 */
7580d774 11499 if (!mmio_flip) {
e2f80391 11500 ret = i915_gem_object_sync(obj, engine, &request);
7580d774
ML
11501 if (ret)
11502 goto cleanup_pending;
11503 }
11504
3465c580 11505 ret = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
8c9f3aaf
JB
11506 if (ret)
11507 goto cleanup_pending;
6b95a207 11508
dedf278c
TU
11509 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
11510 obj, 0);
11511 work->gtt_offset += intel_crtc->dspaddr_offset;
4fa62c89 11512
cf5d8a46 11513 if (mmio_flip) {
86efe24a 11514 ret = intel_queue_mmio_flip(dev, crtc, obj);
d6bbafa1
CW
11515 if (ret)
11516 goto cleanup_unpin;
11517
f06cc1b9
JH
11518 i915_gem_request_assign(&work->flip_queued_req,
11519 obj->last_write_req);
d6bbafa1 11520 } else {
6258fbe2 11521 if (!request) {
e2f80391 11522 request = i915_gem_request_alloc(engine, NULL);
26827088
DG
11523 if (IS_ERR(request)) {
11524 ret = PTR_ERR(request);
6258fbe2 11525 goto cleanup_unpin;
26827088 11526 }
6258fbe2
JH
11527 }
11528
11529 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
d6bbafa1
CW
11530 page_flip_flags);
11531 if (ret)
11532 goto cleanup_unpin;
11533
6258fbe2 11534 i915_gem_request_assign(&work->flip_queued_req, request);
d6bbafa1
CW
11535 }
11536
91af127f 11537 if (request)
75289874 11538 i915_add_request_no_flush(request);
91af127f 11539
1e3feefd 11540 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
d6bbafa1 11541 work->enable_stall_check = true;
4fa62c89 11542
ab8d6675 11543 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
a9ff8714 11544 to_intel_plane(primary)->frontbuffer_bit);
c80ac854 11545 mutex_unlock(&dev->struct_mutex);
a071fa00 11546
a9ff8714
VS
11547 intel_frontbuffer_flip_prepare(dev,
11548 to_intel_plane(primary)->frontbuffer_bit);
6b95a207 11549
e5510fac
JB
11550 trace_i915_flip_request(intel_crtc->plane, obj);
11551
6b95a207 11552 return 0;
96b099fd 11553
4fa62c89 11554cleanup_unpin:
3465c580 11555 intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
8c9f3aaf 11556cleanup_pending:
0aa498d5 11557 if (!IS_ERR_OR_NULL(request))
91af127f 11558 i915_gem_request_cancel(request);
b4a98e57 11559 atomic_dec(&intel_crtc->unpin_work_count);
89ed88ba
CW
11560 mutex_unlock(&dev->struct_mutex);
11561cleanup:
f4510a27 11562 crtc->primary->fb = old_fb;
afd65eb4 11563 update_state_fb(crtc->primary);
89ed88ba
CW
11564
11565 drm_gem_object_unreference_unlocked(&obj->base);
ab8d6675 11566 drm_framebuffer_unreference(work->old_fb);
96b099fd 11567
5e2d7afc 11568 spin_lock_irq(&dev->event_lock);
96b099fd 11569 intel_crtc->unpin_work = NULL;
5e2d7afc 11570 spin_unlock_irq(&dev->event_lock);
96b099fd 11571
87b6b101 11572 drm_crtc_vblank_put(crtc);
7317c75e 11573free_work:
96b099fd
CW
11574 kfree(work);
11575
f900db47 11576 if (ret == -EIO) {
02e0efb5
ML
11577 struct drm_atomic_state *state;
11578 struct drm_plane_state *plane_state;
11579
f900db47 11580out_hang:
02e0efb5
ML
11581 state = drm_atomic_state_alloc(dev);
11582 if (!state)
11583 return -ENOMEM;
11584 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11585
11586retry:
11587 plane_state = drm_atomic_get_plane_state(state, primary);
11588 ret = PTR_ERR_OR_ZERO(plane_state);
11589 if (!ret) {
11590 drm_atomic_set_fb_for_plane(plane_state, fb);
11591
11592 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11593 if (!ret)
11594 ret = drm_atomic_commit(state);
11595 }
11596
11597 if (ret == -EDEADLK) {
11598 drm_modeset_backoff(state->acquire_ctx);
11599 drm_atomic_state_clear(state);
11600 goto retry;
11601 }
11602
11603 if (ret)
11604 drm_atomic_state_free(state);
11605
f0d3dad3 11606 if (ret == 0 && event) {
5e2d7afc 11607 spin_lock_irq(&dev->event_lock);
a071fa00 11608 drm_send_vblank_event(dev, pipe, event);
5e2d7afc 11609 spin_unlock_irq(&dev->event_lock);
f0d3dad3 11610 }
f900db47 11611 }
96b099fd 11612 return ret;
6b95a207
KH
11613}
11614
da20eabd
ML
11615
11616/**
11617 * intel_wm_need_update - Check whether watermarks need updating
11618 * @plane: drm plane
11619 * @state: new plane state
11620 *
11621 * Check current plane state versus the new one to determine whether
11622 * watermarks need to be recalculated.
11623 *
11624 * Returns true or false.
11625 */
11626static bool intel_wm_need_update(struct drm_plane *plane,
11627 struct drm_plane_state *state)
11628{
d21fbe87
MR
11629 struct intel_plane_state *new = to_intel_plane_state(state);
11630 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
11631
11632 /* Update watermarks on tiling or size changes. */
92826fcd
ML
11633 if (new->visible != cur->visible)
11634 return true;
11635
11636 if (!cur->base.fb || !new->base.fb)
11637 return false;
11638
11639 if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
11640 cur->base.rotation != new->base.rotation ||
d21fbe87
MR
11641 drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
11642 drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
11643 drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
11644 drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
2791a16c 11645 return true;
7809e5ae 11646
2791a16c 11647 return false;
7809e5ae
MR
11648}
11649
d21fbe87
MR
11650static bool needs_scaling(struct intel_plane_state *state)
11651{
11652 int src_w = drm_rect_width(&state->src) >> 16;
11653 int src_h = drm_rect_height(&state->src) >> 16;
11654 int dst_w = drm_rect_width(&state->dst);
11655 int dst_h = drm_rect_height(&state->dst);
11656
11657 return (src_w != dst_w || src_h != dst_h);
11658}
11659
da20eabd
ML
11660int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11661 struct drm_plane_state *plane_state)
11662{
ab1d3a0e 11663 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
da20eabd
ML
11664 struct drm_crtc *crtc = crtc_state->crtc;
11665 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11666 struct drm_plane *plane = plane_state->plane;
11667 struct drm_device *dev = crtc->dev;
ed4a6a7c 11668 struct drm_i915_private *dev_priv = to_i915(dev);
da20eabd
ML
11669 struct intel_plane_state *old_plane_state =
11670 to_intel_plane_state(plane->state);
11671 int idx = intel_crtc->base.base.id, ret;
da20eabd
ML
11672 bool mode_changed = needs_modeset(crtc_state);
11673 bool was_crtc_enabled = crtc->state->active;
11674 bool is_crtc_enabled = crtc_state->active;
da20eabd
ML
11675 bool turn_off, turn_on, visible, was_visible;
11676 struct drm_framebuffer *fb = plane_state->fb;
11677
11678 if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
11679 plane->type != DRM_PLANE_TYPE_CURSOR) {
11680 ret = skl_update_scaler_plane(
11681 to_intel_crtc_state(crtc_state),
11682 to_intel_plane_state(plane_state));
11683 if (ret)
11684 return ret;
11685 }
11686
da20eabd
ML
11687 was_visible = old_plane_state->visible;
11688 visible = to_intel_plane_state(plane_state)->visible;
11689
11690 if (!was_crtc_enabled && WARN_ON(was_visible))
11691 was_visible = false;
11692
35c08f43
ML
11693 /*
11694 * Visibility is calculated as if the crtc was on, but
11695 * after scaler setup everything depends on it being off
11696 * when the crtc isn't active.
11697 */
11698 if (!is_crtc_enabled)
11699 to_intel_plane_state(plane_state)->visible = visible = false;
da20eabd
ML
11700
11701 if (!was_visible && !visible)
11702 return 0;
11703
e8861675
ML
11704 if (fb != old_plane_state->base.fb)
11705 pipe_config->fb_changed = true;
11706
da20eabd
ML
11707 turn_off = was_visible && (!visible || mode_changed);
11708 turn_on = visible && (!was_visible || mode_changed);
11709
11710 DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
11711 plane->base.id, fb ? fb->base.id : -1);
11712
11713 DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
11714 plane->base.id, was_visible, visible,
11715 turn_off, turn_on, mode_changed);
11716
caed361d
VS
11717 if (turn_on) {
11718 pipe_config->update_wm_pre = true;
11719
11720 /* must disable cxsr around plane enable/disable */
11721 if (plane->type != DRM_PLANE_TYPE_CURSOR)
11722 pipe_config->disable_cxsr = true;
11723 } else if (turn_off) {
11724 pipe_config->update_wm_post = true;
92826fcd 11725
852eb00d 11726 /* must disable cxsr around plane enable/disable */
e8861675 11727 if (plane->type != DRM_PLANE_TYPE_CURSOR)
ab1d3a0e 11728 pipe_config->disable_cxsr = true;
852eb00d 11729 } else if (intel_wm_need_update(plane, plane_state)) {
caed361d
VS
11730 /* FIXME bollocks */
11731 pipe_config->update_wm_pre = true;
11732 pipe_config->update_wm_post = true;
852eb00d 11733 }
da20eabd 11734
ed4a6a7c 11735 /* Pre-gen9 platforms need two-step watermark updates */
caed361d
VS
11736 if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
11737 INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
ed4a6a7c
MR
11738 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
11739
8be6ca85 11740 if (visible || was_visible)
cd202f69 11741 pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
a9ff8714 11742
31ae71fc
ML
11743 /*
11744 * WaCxSRDisabledForSpriteScaling:ivb
11745 *
11746 * cstate->update_wm was already set above, so this flag will
11747 * take effect when we commit and program watermarks.
11748 */
11749 if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev) &&
11750 needs_scaling(to_intel_plane_state(plane_state)) &&
11751 !needs_scaling(old_plane_state))
11752 pipe_config->disable_lp_wm = true;
d21fbe87 11753
da20eabd
ML
11754 return 0;
11755}
11756
6d3a1ce7
ML
11757static bool encoders_cloneable(const struct intel_encoder *a,
11758 const struct intel_encoder *b)
11759{
11760 /* masks could be asymmetric, so check both ways */
11761 return a == b || (a->cloneable & (1 << b->type) &&
11762 b->cloneable & (1 << a->type));
11763}
11764
11765static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11766 struct intel_crtc *crtc,
11767 struct intel_encoder *encoder)
11768{
11769 struct intel_encoder *source_encoder;
11770 struct drm_connector *connector;
11771 struct drm_connector_state *connector_state;
11772 int i;
11773
11774 for_each_connector_in_state(state, connector, connector_state, i) {
11775 if (connector_state->crtc != &crtc->base)
11776 continue;
11777
11778 source_encoder =
11779 to_intel_encoder(connector_state->best_encoder);
11780 if (!encoders_cloneable(encoder, source_encoder))
11781 return false;
11782 }
11783
11784 return true;
11785}
11786
11787static bool check_encoder_cloning(struct drm_atomic_state *state,
11788 struct intel_crtc *crtc)
11789{
11790 struct intel_encoder *encoder;
11791 struct drm_connector *connector;
11792 struct drm_connector_state *connector_state;
11793 int i;
11794
11795 for_each_connector_in_state(state, connector, connector_state, i) {
11796 if (connector_state->crtc != &crtc->base)
11797 continue;
11798
11799 encoder = to_intel_encoder(connector_state->best_encoder);
11800 if (!check_single_encoder_cloning(state, crtc, encoder))
11801 return false;
11802 }
11803
11804 return true;
11805}
11806
11807static int intel_crtc_atomic_check(struct drm_crtc *crtc,
11808 struct drm_crtc_state *crtc_state)
11809{
cf5a15be 11810 struct drm_device *dev = crtc->dev;
ad421372 11811 struct drm_i915_private *dev_priv = dev->dev_private;
6d3a1ce7 11812 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cf5a15be
ML
11813 struct intel_crtc_state *pipe_config =
11814 to_intel_crtc_state(crtc_state);
6d3a1ce7 11815 struct drm_atomic_state *state = crtc_state->state;
4d20cd86 11816 int ret;
6d3a1ce7
ML
11817 bool mode_changed = needs_modeset(crtc_state);
11818
11819 if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
11820 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11821 return -EINVAL;
11822 }
11823
852eb00d 11824 if (mode_changed && !crtc_state->active)
caed361d 11825 pipe_config->update_wm_post = true;
eddfcbcd 11826
ad421372
ML
11827 if (mode_changed && crtc_state->enable &&
11828 dev_priv->display.crtc_compute_clock &&
8106ddbd 11829 !WARN_ON(pipe_config->shared_dpll)) {
ad421372
ML
11830 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
11831 pipe_config);
11832 if (ret)
11833 return ret;
11834 }
11835
82cf435b
LL
11836 if (crtc_state->color_mgmt_changed) {
11837 ret = intel_color_check(crtc, crtc_state);
11838 if (ret)
11839 return ret;
11840 }
11841
e435d6e5 11842 ret = 0;
86c8bbbe 11843 if (dev_priv->display.compute_pipe_wm) {
e3bddded 11844 ret = dev_priv->display.compute_pipe_wm(pipe_config);
ed4a6a7c
MR
11845 if (ret) {
11846 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
11847 return ret;
11848 }
11849 }
11850
11851 if (dev_priv->display.compute_intermediate_wm &&
11852 !to_intel_atomic_state(state)->skip_intermediate_wm) {
11853 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
11854 return 0;
11855
11856 /*
11857 * Calculate 'intermediate' watermarks that satisfy both the
11858 * old state and the new state. We can program these
11859 * immediately.
11860 */
11861 ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
11862 intel_crtc,
11863 pipe_config);
11864 if (ret) {
11865 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
86c8bbbe 11866 return ret;
ed4a6a7c 11867 }
86c8bbbe
MR
11868 }
11869
e435d6e5
ML
11870 if (INTEL_INFO(dev)->gen >= 9) {
11871 if (mode_changed)
11872 ret = skl_update_scaler_crtc(pipe_config);
11873
11874 if (!ret)
11875 ret = intel_atomic_setup_scalers(dev, intel_crtc,
11876 pipe_config);
11877 }
11878
11879 return ret;
6d3a1ce7
ML
11880}
11881
65b38e0d 11882static const struct drm_crtc_helper_funcs intel_helper_funcs = {
f6e5b160 11883 .mode_set_base_atomic = intel_pipe_set_base_atomic,
ea2c67bb
MR
11884 .atomic_begin = intel_begin_crtc_commit,
11885 .atomic_flush = intel_finish_crtc_commit,
6d3a1ce7 11886 .atomic_check = intel_crtc_atomic_check,
f6e5b160
CW
11887};
11888
d29b2f9d
ACO
11889static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11890{
11891 struct intel_connector *connector;
11892
11893 for_each_intel_connector(dev, connector) {
11894 if (connector->base.encoder) {
11895 connector->base.state->best_encoder =
11896 connector->base.encoder;
11897 connector->base.state->crtc =
11898 connector->base.encoder->crtc;
11899 } else {
11900 connector->base.state->best_encoder = NULL;
11901 connector->base.state->crtc = NULL;
11902 }
11903 }
11904}
11905
050f7aeb 11906static void
eba905b2 11907connected_sink_compute_bpp(struct intel_connector *connector,
5cec258b 11908 struct intel_crtc_state *pipe_config)
050f7aeb
DV
11909{
11910 int bpp = pipe_config->pipe_bpp;
11911
11912 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
11913 connector->base.base.id,
c23cc417 11914 connector->base.name);
050f7aeb
DV
11915
11916 /* Don't use an invalid EDID bpc value */
11917 if (connector->base.display_info.bpc &&
11918 connector->base.display_info.bpc * 3 < bpp) {
11919 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
11920 bpp, connector->base.display_info.bpc*3);
11921 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
11922 }
11923
013dd9e0
JN
11924 /* Clamp bpp to default limit on screens without EDID 1.4 */
11925 if (connector->base.display_info.bpc == 0) {
11926 int type = connector->base.connector_type;
11927 int clamp_bpp = 24;
11928
11929 /* Fall back to 18 bpp when DP sink capability is unknown. */
11930 if (type == DRM_MODE_CONNECTOR_DisplayPort ||
11931 type == DRM_MODE_CONNECTOR_eDP)
11932 clamp_bpp = 18;
11933
11934 if (bpp > clamp_bpp) {
11935 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of %d\n",
11936 bpp, clamp_bpp);
11937 pipe_config->pipe_bpp = clamp_bpp;
11938 }
050f7aeb
DV
11939 }
11940}
11941
4e53c2e0 11942static int
050f7aeb 11943compute_baseline_pipe_bpp(struct intel_crtc *crtc,
5cec258b 11944 struct intel_crtc_state *pipe_config)
4e53c2e0 11945{
050f7aeb 11946 struct drm_device *dev = crtc->base.dev;
1486017f 11947 struct drm_atomic_state *state;
da3ced29
ACO
11948 struct drm_connector *connector;
11949 struct drm_connector_state *connector_state;
1486017f 11950 int bpp, i;
4e53c2e0 11951
666a4537 11952 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
4e53c2e0 11953 bpp = 10*3;
d328c9d7
DV
11954 else if (INTEL_INFO(dev)->gen >= 5)
11955 bpp = 12*3;
11956 else
11957 bpp = 8*3;
11958
4e53c2e0 11959
4e53c2e0
DV
11960 pipe_config->pipe_bpp = bpp;
11961
1486017f
ACO
11962 state = pipe_config->base.state;
11963
4e53c2e0 11964 /* Clamp display bpp to EDID value */
da3ced29
ACO
11965 for_each_connector_in_state(state, connector, connector_state, i) {
11966 if (connector_state->crtc != &crtc->base)
4e53c2e0
DV
11967 continue;
11968
da3ced29
ACO
11969 connected_sink_compute_bpp(to_intel_connector(connector),
11970 pipe_config);
4e53c2e0
DV
11971 }
11972
11973 return bpp;
11974}
11975
644db711
DV
11976static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11977{
11978 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11979 "type: 0x%x flags: 0x%x\n",
1342830c 11980 mode->crtc_clock,
644db711
DV
11981 mode->crtc_hdisplay, mode->crtc_hsync_start,
11982 mode->crtc_hsync_end, mode->crtc_htotal,
11983 mode->crtc_vdisplay, mode->crtc_vsync_start,
11984 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
11985}
11986
c0b03411 11987static void intel_dump_pipe_config(struct intel_crtc *crtc,
5cec258b 11988 struct intel_crtc_state *pipe_config,
c0b03411
DV
11989 const char *context)
11990{
6a60cd87
CK
11991 struct drm_device *dev = crtc->base.dev;
11992 struct drm_plane *plane;
11993 struct intel_plane *intel_plane;
11994 struct intel_plane_state *state;
11995 struct drm_framebuffer *fb;
11996
11997 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
11998 context, pipe_config, pipe_name(crtc->pipe));
c0b03411 11999
da205630 12000 DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
c0b03411
DV
12001 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
12002 pipe_config->pipe_bpp, pipe_config->dither);
12003 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12004 pipe_config->has_pch_encoder,
12005 pipe_config->fdi_lanes,
12006 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
12007 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
12008 pipe_config->fdi_m_n.tu);
90a6b7b0 12009 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
eb14cb74 12010 pipe_config->has_dp_encoder,
90a6b7b0 12011 pipe_config->lane_count,
eb14cb74
VS
12012 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
12013 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
12014 pipe_config->dp_m_n.tu);
b95af8be 12015
90a6b7b0 12016 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
b95af8be 12017 pipe_config->has_dp_encoder,
90a6b7b0 12018 pipe_config->lane_count,
b95af8be
VK
12019 pipe_config->dp_m2_n2.gmch_m,
12020 pipe_config->dp_m2_n2.gmch_n,
12021 pipe_config->dp_m2_n2.link_m,
12022 pipe_config->dp_m2_n2.link_n,
12023 pipe_config->dp_m2_n2.tu);
12024
55072d19
DV
12025 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12026 pipe_config->has_audio,
12027 pipe_config->has_infoframe);
12028
c0b03411 12029 DRM_DEBUG_KMS("requested mode:\n");
2d112de7 12030 drm_mode_debug_printmodeline(&pipe_config->base.mode);
c0b03411 12031 DRM_DEBUG_KMS("adjusted mode:\n");
2d112de7
ACO
12032 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12033 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
d71b8d4a 12034 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
37327abd
VS
12035 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12036 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
0ec463d3
TU
12037 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12038 crtc->num_scalers,
12039 pipe_config->scaler_state.scaler_users,
12040 pipe_config->scaler_state.scaler_id);
c0b03411
DV
12041 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12042 pipe_config->gmch_pfit.control,
12043 pipe_config->gmch_pfit.pgm_ratios,
12044 pipe_config->gmch_pfit.lvds_border_bits);
fd4daa9c 12045 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
c0b03411 12046 pipe_config->pch_pfit.pos,
fd4daa9c
CW
12047 pipe_config->pch_pfit.size,
12048 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
42db64ef 12049 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
cf532bb2 12050 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
6a60cd87 12051
415ff0f6 12052 if (IS_BROXTON(dev)) {
05712c15 12053 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
415ff0f6 12054 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
c8453338 12055 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
415ff0f6
TU
12056 pipe_config->ddi_pll_sel,
12057 pipe_config->dpll_hw_state.ebb0,
05712c15 12058 pipe_config->dpll_hw_state.ebb4,
415ff0f6
TU
12059 pipe_config->dpll_hw_state.pll0,
12060 pipe_config->dpll_hw_state.pll1,
12061 pipe_config->dpll_hw_state.pll2,
12062 pipe_config->dpll_hw_state.pll3,
12063 pipe_config->dpll_hw_state.pll6,
12064 pipe_config->dpll_hw_state.pll8,
05712c15 12065 pipe_config->dpll_hw_state.pll9,
c8453338 12066 pipe_config->dpll_hw_state.pll10,
415ff0f6 12067 pipe_config->dpll_hw_state.pcsdw12);
ef11bdb3 12068 } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
415ff0f6
TU
12069 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12070 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12071 pipe_config->ddi_pll_sel,
12072 pipe_config->dpll_hw_state.ctrl1,
12073 pipe_config->dpll_hw_state.cfgcr1,
12074 pipe_config->dpll_hw_state.cfgcr2);
12075 } else if (HAS_DDI(dev)) {
1260f07e 12076 DRM_DEBUG_KMS("ddi_pll_sel: 0x%x; dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
415ff0f6 12077 pipe_config->ddi_pll_sel,
00490c22
ML
12078 pipe_config->dpll_hw_state.wrpll,
12079 pipe_config->dpll_hw_state.spll);
415ff0f6
TU
12080 } else {
12081 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12082 "fp0: 0x%x, fp1: 0x%x\n",
12083 pipe_config->dpll_hw_state.dpll,
12084 pipe_config->dpll_hw_state.dpll_md,
12085 pipe_config->dpll_hw_state.fp0,
12086 pipe_config->dpll_hw_state.fp1);
12087 }
12088
6a60cd87
CK
12089 DRM_DEBUG_KMS("planes on this crtc\n");
12090 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12091 intel_plane = to_intel_plane(plane);
12092 if (intel_plane->pipe != crtc->pipe)
12093 continue;
12094
12095 state = to_intel_plane_state(plane->state);
12096 fb = state->base.fb;
12097 if (!fb) {
12098 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
12099 "disabled, scaler_id = %d\n",
12100 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12101 plane->base.id, intel_plane->pipe,
12102 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
12103 drm_plane_index(plane), state->scaler_id);
12104 continue;
12105 }
12106
12107 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
12108 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12109 plane->base.id, intel_plane->pipe,
12110 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
12111 drm_plane_index(plane));
12112 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
12113 fb->base.id, fb->width, fb->height, fb->pixel_format);
12114 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
12115 state->scaler_id,
12116 state->src.x1 >> 16, state->src.y1 >> 16,
12117 drm_rect_width(&state->src) >> 16,
12118 drm_rect_height(&state->src) >> 16,
12119 state->dst.x1, state->dst.y1,
12120 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
12121 }
c0b03411
DV
12122}
12123
5448a00d 12124static bool check_digital_port_conflicts(struct drm_atomic_state *state)
00f0b378 12125{
5448a00d 12126 struct drm_device *dev = state->dev;
da3ced29 12127 struct drm_connector *connector;
00f0b378
VS
12128 unsigned int used_ports = 0;
12129
12130 /*
12131 * Walk the connector list instead of the encoder
12132 * list to detect the problem on ddi platforms
12133 * where there's just one encoder per digital port.
12134 */
0bff4858
VS
12135 drm_for_each_connector(connector, dev) {
12136 struct drm_connector_state *connector_state;
12137 struct intel_encoder *encoder;
12138
12139 connector_state = drm_atomic_get_existing_connector_state(state, connector);
12140 if (!connector_state)
12141 connector_state = connector->state;
12142
5448a00d 12143 if (!connector_state->best_encoder)
00f0b378
VS
12144 continue;
12145
5448a00d
ACO
12146 encoder = to_intel_encoder(connector_state->best_encoder);
12147
12148 WARN_ON(!connector_state->crtc);
00f0b378
VS
12149
12150 switch (encoder->type) {
12151 unsigned int port_mask;
12152 case INTEL_OUTPUT_UNKNOWN:
12153 if (WARN_ON(!HAS_DDI(dev)))
12154 break;
12155 case INTEL_OUTPUT_DISPLAYPORT:
12156 case INTEL_OUTPUT_HDMI:
12157 case INTEL_OUTPUT_EDP:
12158 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12159
12160 /* the same port mustn't appear more than once */
12161 if (used_ports & port_mask)
12162 return false;
12163
12164 used_ports |= port_mask;
12165 default:
12166 break;
12167 }
12168 }
12169
12170 return true;
12171}
12172
83a57153
ACO
12173static void
12174clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12175{
12176 struct drm_crtc_state tmp_state;
663a3640 12177 struct intel_crtc_scaler_state scaler_state;
4978cc93 12178 struct intel_dpll_hw_state dpll_hw_state;
8106ddbd 12179 struct intel_shared_dpll *shared_dpll;
8504c74c 12180 uint32_t ddi_pll_sel;
c4e2d043 12181 bool force_thru;
83a57153 12182
7546a384
ACO
12183 /* FIXME: before the switch to atomic started, a new pipe_config was
12184 * kzalloc'd. Code that depends on any field being zero should be
12185 * fixed, so that the crtc_state can be safely duplicated. For now,
12186 * only fields that are know to not cause problems are preserved. */
12187
83a57153 12188 tmp_state = crtc_state->base;
663a3640 12189 scaler_state = crtc_state->scaler_state;
4978cc93
ACO
12190 shared_dpll = crtc_state->shared_dpll;
12191 dpll_hw_state = crtc_state->dpll_hw_state;
8504c74c 12192 ddi_pll_sel = crtc_state->ddi_pll_sel;
c4e2d043 12193 force_thru = crtc_state->pch_pfit.force_thru;
4978cc93 12194
83a57153 12195 memset(crtc_state, 0, sizeof *crtc_state);
4978cc93 12196
83a57153 12197 crtc_state->base = tmp_state;
663a3640 12198 crtc_state->scaler_state = scaler_state;
4978cc93
ACO
12199 crtc_state->shared_dpll = shared_dpll;
12200 crtc_state->dpll_hw_state = dpll_hw_state;
8504c74c 12201 crtc_state->ddi_pll_sel = ddi_pll_sel;
c4e2d043 12202 crtc_state->pch_pfit.force_thru = force_thru;
83a57153
ACO
12203}
12204
548ee15b 12205static int
b8cecdf5 12206intel_modeset_pipe_config(struct drm_crtc *crtc,
b359283a 12207 struct intel_crtc_state *pipe_config)
ee7b9f93 12208{
b359283a 12209 struct drm_atomic_state *state = pipe_config->base.state;
7758a113 12210 struct intel_encoder *encoder;
da3ced29 12211 struct drm_connector *connector;
0b901879 12212 struct drm_connector_state *connector_state;
d328c9d7 12213 int base_bpp, ret = -EINVAL;
0b901879 12214 int i;
e29c22c0 12215 bool retry = true;
ee7b9f93 12216
83a57153 12217 clear_intel_crtc_state(pipe_config);
7758a113 12218
e143a21c
DV
12219 pipe_config->cpu_transcoder =
12220 (enum transcoder) to_intel_crtc(crtc)->pipe;
b8cecdf5 12221
2960bc9c
ID
12222 /*
12223 * Sanitize sync polarity flags based on requested ones. If neither
12224 * positive or negative polarity is requested, treat this as meaning
12225 * negative polarity.
12226 */
2d112de7 12227 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 12228 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
2d112de7 12229 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
2960bc9c 12230
2d112de7 12231 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 12232 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
2d112de7 12233 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
2960bc9c 12234
d328c9d7
DV
12235 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12236 pipe_config);
12237 if (base_bpp < 0)
4e53c2e0
DV
12238 goto fail;
12239
e41a56be
VS
12240 /*
12241 * Determine the real pipe dimensions. Note that stereo modes can
12242 * increase the actual pipe size due to the frame doubling and
12243 * insertion of additional space for blanks between the frame. This
12244 * is stored in the crtc timings. We use the requested mode to do this
12245 * computation to clearly distinguish it from the adjusted mode, which
12246 * can be changed by the connectors in the below retry loop.
12247 */
2d112de7 12248 drm_crtc_get_hv_timing(&pipe_config->base.mode,
ecb7e16b
GP
12249 &pipe_config->pipe_src_w,
12250 &pipe_config->pipe_src_h);
e41a56be 12251
e29c22c0 12252encoder_retry:
ef1b460d 12253 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 12254 pipe_config->port_clock = 0;
ef1b460d 12255 pipe_config->pixel_multiplier = 1;
ff9a6750 12256
135c81b8 12257 /* Fill in default crtc timings, allow encoders to overwrite them. */
2d112de7
ACO
12258 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12259 CRTC_STEREO_DOUBLE);
135c81b8 12260
7758a113
DV
12261 /* Pass our mode to the connectors and the CRTC to give them a chance to
12262 * adjust it according to limitations or connector properties, and also
12263 * a chance to reject the mode entirely.
47f1c6c9 12264 */
da3ced29 12265 for_each_connector_in_state(state, connector, connector_state, i) {
0b901879 12266 if (connector_state->crtc != crtc)
7758a113 12267 continue;
7ae89233 12268
0b901879
ACO
12269 encoder = to_intel_encoder(connector_state->best_encoder);
12270
efea6e8e
DV
12271 if (!(encoder->compute_config(encoder, pipe_config))) {
12272 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
DV
12273 goto fail;
12274 }
ee7b9f93 12275 }
47f1c6c9 12276
ff9a6750
DV
12277 /* Set default port clock if not overwritten by the encoder. Needs to be
12278 * done afterwards in case the encoder adjusts the mode. */
12279 if (!pipe_config->port_clock)
2d112de7 12280 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
241bfc38 12281 * pipe_config->pixel_multiplier;
ff9a6750 12282
a43f6e0f 12283 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 12284 if (ret < 0) {
7758a113
DV
12285 DRM_DEBUG_KMS("CRTC fixup failed\n");
12286 goto fail;
ee7b9f93 12287 }
e29c22c0
DV
12288
12289 if (ret == RETRY) {
12290 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12291 ret = -EINVAL;
12292 goto fail;
12293 }
12294
12295 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12296 retry = false;
12297 goto encoder_retry;
12298 }
12299
e8fa4270
DV
12300 /* Dithering seems to not pass-through bits correctly when it should, so
12301 * only enable it on 6bpc panels. */
12302 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
62f0ace5 12303 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
d328c9d7 12304 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
4e53c2e0 12305
7758a113 12306fail:
548ee15b 12307 return ret;
ee7b9f93 12308}
47f1c6c9 12309
ea9d758d 12310static void
4740b0f2 12311intel_modeset_update_crtc_state(struct drm_atomic_state *state)
ea9d758d 12312{
0a9ab303
ACO
12313 struct drm_crtc *crtc;
12314 struct drm_crtc_state *crtc_state;
8a75d157 12315 int i;
ea9d758d 12316
7668851f 12317 /* Double check state. */
8a75d157 12318 for_each_crtc_in_state(state, crtc, crtc_state, i) {
3cb480bc 12319 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
fc467a22
ML
12320
12321 /* Update hwmode for vblank functions */
12322 if (crtc->state->active)
12323 crtc->hwmode = crtc->state->adjusted_mode;
12324 else
12325 crtc->hwmode.crtc_clock = 0;
61067a5e
ML
12326
12327 /*
12328 * Update legacy state to satisfy fbc code. This can
12329 * be removed when fbc uses the atomic state.
12330 */
12331 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
12332 struct drm_plane_state *plane_state = crtc->primary->state;
12333
12334 crtc->primary->fb = plane_state->fb;
12335 crtc->x = plane_state->src_x >> 16;
12336 crtc->y = plane_state->src_y >> 16;
12337 }
ea9d758d 12338 }
ea9d758d
DV
12339}
12340
3bd26263 12341static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 12342{
3bd26263 12343 int diff;
f1f644dc
JB
12344
12345 if (clock1 == clock2)
12346 return true;
12347
12348 if (!clock1 || !clock2)
12349 return false;
12350
12351 diff = abs(clock1 - clock2);
12352
12353 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12354 return true;
12355
12356 return false;
12357}
12358
25c5b266
DV
12359#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12360 list_for_each_entry((intel_crtc), \
12361 &(dev)->mode_config.crtc_list, \
12362 base.head) \
95150bdf 12363 for_each_if (mask & (1 <<(intel_crtc)->pipe))
25c5b266 12364
cfb23ed6
ML
12365static bool
12366intel_compare_m_n(unsigned int m, unsigned int n,
12367 unsigned int m2, unsigned int n2,
12368 bool exact)
12369{
12370 if (m == m2 && n == n2)
12371 return true;
12372
12373 if (exact || !m || !n || !m2 || !n2)
12374 return false;
12375
12376 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
12377
31d10b57
ML
12378 if (n > n2) {
12379 while (n > n2) {
cfb23ed6
ML
12380 m2 <<= 1;
12381 n2 <<= 1;
12382 }
31d10b57
ML
12383 } else if (n < n2) {
12384 while (n < n2) {
cfb23ed6
ML
12385 m <<= 1;
12386 n <<= 1;
12387 }
12388 }
12389
31d10b57
ML
12390 if (n != n2)
12391 return false;
12392
12393 return intel_fuzzy_clock_check(m, m2);
cfb23ed6
ML
12394}
12395
12396static bool
12397intel_compare_link_m_n(const struct intel_link_m_n *m_n,
12398 struct intel_link_m_n *m2_n2,
12399 bool adjust)
12400{
12401 if (m_n->tu == m2_n2->tu &&
12402 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
12403 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
12404 intel_compare_m_n(m_n->link_m, m_n->link_n,
12405 m2_n2->link_m, m2_n2->link_n, !adjust)) {
12406 if (adjust)
12407 *m2_n2 = *m_n;
12408
12409 return true;
12410 }
12411
12412 return false;
12413}
12414
0e8ffe1b 12415static bool
2fa2fe9a 12416intel_pipe_config_compare(struct drm_device *dev,
5cec258b 12417 struct intel_crtc_state *current_config,
cfb23ed6
ML
12418 struct intel_crtc_state *pipe_config,
12419 bool adjust)
0e8ffe1b 12420{
cfb23ed6
ML
12421 bool ret = true;
12422
12423#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
12424 do { \
12425 if (!adjust) \
12426 DRM_ERROR(fmt, ##__VA_ARGS__); \
12427 else \
12428 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
12429 } while (0)
12430
66e985c0
DV
12431#define PIPE_CONF_CHECK_X(name) \
12432 if (current_config->name != pipe_config->name) { \
cfb23ed6 12433 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
66e985c0
DV
12434 "(expected 0x%08x, found 0x%08x)\n", \
12435 current_config->name, \
12436 pipe_config->name); \
cfb23ed6 12437 ret = false; \
66e985c0
DV
12438 }
12439
08a24034
DV
12440#define PIPE_CONF_CHECK_I(name) \
12441 if (current_config->name != pipe_config->name) { \
cfb23ed6 12442 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
08a24034
DV
12443 "(expected %i, found %i)\n", \
12444 current_config->name, \
12445 pipe_config->name); \
cfb23ed6
ML
12446 ret = false; \
12447 }
12448
8106ddbd
ACO
12449#define PIPE_CONF_CHECK_P(name) \
12450 if (current_config->name != pipe_config->name) { \
12451 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12452 "(expected %p, found %p)\n", \
12453 current_config->name, \
12454 pipe_config->name); \
12455 ret = false; \
12456 }
12457
cfb23ed6
ML
12458#define PIPE_CONF_CHECK_M_N(name) \
12459 if (!intel_compare_link_m_n(&current_config->name, \
12460 &pipe_config->name,\
12461 adjust)) { \
12462 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12463 "(expected tu %i gmch %i/%i link %i/%i, " \
12464 "found tu %i, gmch %i/%i link %i/%i)\n", \
12465 current_config->name.tu, \
12466 current_config->name.gmch_m, \
12467 current_config->name.gmch_n, \
12468 current_config->name.link_m, \
12469 current_config->name.link_n, \
12470 pipe_config->name.tu, \
12471 pipe_config->name.gmch_m, \
12472 pipe_config->name.gmch_n, \
12473 pipe_config->name.link_m, \
12474 pipe_config->name.link_n); \
12475 ret = false; \
12476 }
12477
12478#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12479 if (!intel_compare_link_m_n(&current_config->name, \
12480 &pipe_config->name, adjust) && \
12481 !intel_compare_link_m_n(&current_config->alt_name, \
12482 &pipe_config->name, adjust)) { \
12483 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12484 "(expected tu %i gmch %i/%i link %i/%i, " \
12485 "or tu %i gmch %i/%i link %i/%i, " \
12486 "found tu %i, gmch %i/%i link %i/%i)\n", \
12487 current_config->name.tu, \
12488 current_config->name.gmch_m, \
12489 current_config->name.gmch_n, \
12490 current_config->name.link_m, \
12491 current_config->name.link_n, \
12492 current_config->alt_name.tu, \
12493 current_config->alt_name.gmch_m, \
12494 current_config->alt_name.gmch_n, \
12495 current_config->alt_name.link_m, \
12496 current_config->alt_name.link_n, \
12497 pipe_config->name.tu, \
12498 pipe_config->name.gmch_m, \
12499 pipe_config->name.gmch_n, \
12500 pipe_config->name.link_m, \
12501 pipe_config->name.link_n); \
12502 ret = false; \
88adfff1
DV
12503 }
12504
b95af8be
VK
12505/* This is required for BDW+ where there is only one set of registers for
12506 * switching between high and low RR.
12507 * This macro can be used whenever a comparison has to be made between one
12508 * hw state and multiple sw state variables.
12509 */
12510#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
12511 if ((current_config->name != pipe_config->name) && \
12512 (current_config->alt_name != pipe_config->name)) { \
cfb23ed6 12513 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
b95af8be
VK
12514 "(expected %i or %i, found %i)\n", \
12515 current_config->name, \
12516 current_config->alt_name, \
12517 pipe_config->name); \
cfb23ed6 12518 ret = false; \
b95af8be
VK
12519 }
12520
1bd1bd80
DV
12521#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12522 if ((current_config->name ^ pipe_config->name) & (mask)) { \
cfb23ed6 12523 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
1bd1bd80
DV
12524 "(expected %i, found %i)\n", \
12525 current_config->name & (mask), \
12526 pipe_config->name & (mask)); \
cfb23ed6 12527 ret = false; \
1bd1bd80
DV
12528 }
12529
5e550656
VS
12530#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12531 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
cfb23ed6 12532 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
5e550656
VS
12533 "(expected %i, found %i)\n", \
12534 current_config->name, \
12535 pipe_config->name); \
cfb23ed6 12536 ret = false; \
5e550656
VS
12537 }
12538
bb760063
DV
12539#define PIPE_CONF_QUIRK(quirk) \
12540 ((current_config->quirks | pipe_config->quirks) & (quirk))
12541
eccb140b
DV
12542 PIPE_CONF_CHECK_I(cpu_transcoder);
12543
08a24034
DV
12544 PIPE_CONF_CHECK_I(has_pch_encoder);
12545 PIPE_CONF_CHECK_I(fdi_lanes);
cfb23ed6 12546 PIPE_CONF_CHECK_M_N(fdi_m_n);
08a24034 12547
eb14cb74 12548 PIPE_CONF_CHECK_I(has_dp_encoder);
90a6b7b0 12549 PIPE_CONF_CHECK_I(lane_count);
b95af8be
VK
12550
12551 if (INTEL_INFO(dev)->gen < 8) {
cfb23ed6
ML
12552 PIPE_CONF_CHECK_M_N(dp_m_n);
12553
cfb23ed6
ML
12554 if (current_config->has_drrs)
12555 PIPE_CONF_CHECK_M_N(dp_m2_n2);
12556 } else
12557 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
eb14cb74 12558
a65347ba
JN
12559 PIPE_CONF_CHECK_I(has_dsi_encoder);
12560
2d112de7
ACO
12561 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12562 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12563 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12564 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12565 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12566 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
1bd1bd80 12567
2d112de7
ACO
12568 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12569 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12570 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12571 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12572 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12573 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
1bd1bd80 12574
c93f54cf 12575 PIPE_CONF_CHECK_I(pixel_multiplier);
6897b4b5 12576 PIPE_CONF_CHECK_I(has_hdmi_sink);
b5a9fa09 12577 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
666a4537 12578 IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
b5a9fa09 12579 PIPE_CONF_CHECK_I(limited_color_range);
e43823ec 12580 PIPE_CONF_CHECK_I(has_infoframe);
6c49f241 12581
9ed109a7
DV
12582 PIPE_CONF_CHECK_I(has_audio);
12583
2d112de7 12584 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
1bd1bd80
DV
12585 DRM_MODE_FLAG_INTERLACE);
12586
bb760063 12587 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
2d112de7 12588 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12589 DRM_MODE_FLAG_PHSYNC);
2d112de7 12590 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12591 DRM_MODE_FLAG_NHSYNC);
2d112de7 12592 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12593 DRM_MODE_FLAG_PVSYNC);
2d112de7 12594 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063
DV
12595 DRM_MODE_FLAG_NVSYNC);
12596 }
045ac3b5 12597
333b8ca8 12598 PIPE_CONF_CHECK_X(gmch_pfit.control);
e2ff2d4a
DV
12599 /* pfit ratios are autocomputed by the hw on gen4+ */
12600 if (INTEL_INFO(dev)->gen < 4)
12601 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
333b8ca8 12602 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
9953599b 12603
bfd16b2a
ML
12604 if (!adjust) {
12605 PIPE_CONF_CHECK_I(pipe_src_w);
12606 PIPE_CONF_CHECK_I(pipe_src_h);
12607
12608 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12609 if (current_config->pch_pfit.enabled) {
12610 PIPE_CONF_CHECK_X(pch_pfit.pos);
12611 PIPE_CONF_CHECK_X(pch_pfit.size);
12612 }
2fa2fe9a 12613
7aefe2b5
ML
12614 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12615 }
a1b2278e 12616
e59150dc
JB
12617 /* BDW+ don't expose a synchronous way to read the state */
12618 if (IS_HASWELL(dev))
12619 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 12620
282740f7
VS
12621 PIPE_CONF_CHECK_I(double_wide);
12622
26804afd
DV
12623 PIPE_CONF_CHECK_X(ddi_pll_sel);
12624
8106ddbd 12625 PIPE_CONF_CHECK_P(shared_dpll);
66e985c0 12626 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 12627 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
DV
12628 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12629 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
d452c5b6 12630 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
00490c22 12631 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
3f4cd19f
DL
12632 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12633 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12634 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
c0d43d62 12635
42571aef
VS
12636 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12637 PIPE_CONF_CHECK_I(pipe_bpp);
12638
2d112de7 12639 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
a9a7e98a 12640 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 12641
66e985c0 12642#undef PIPE_CONF_CHECK_X
08a24034 12643#undef PIPE_CONF_CHECK_I
8106ddbd 12644#undef PIPE_CONF_CHECK_P
b95af8be 12645#undef PIPE_CONF_CHECK_I_ALT
1bd1bd80 12646#undef PIPE_CONF_CHECK_FLAGS
5e550656 12647#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 12648#undef PIPE_CONF_QUIRK
cfb23ed6 12649#undef INTEL_ERR_OR_DBG_KMS
88adfff1 12650
cfb23ed6 12651 return ret;
0e8ffe1b
DV
12652}
12653
e3b247da
VS
12654static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
12655 const struct intel_crtc_state *pipe_config)
12656{
12657 if (pipe_config->has_pch_encoder) {
21a727b3 12658 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
e3b247da
VS
12659 &pipe_config->fdi_m_n);
12660 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
12661
12662 /*
12663 * FDI already provided one idea for the dotclock.
12664 * Yell if the encoder disagrees.
12665 */
12666 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
12667 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
12668 fdi_dotclock, dotclock);
12669 }
12670}
12671
08db6652
DL
12672static void check_wm_state(struct drm_device *dev)
12673{
12674 struct drm_i915_private *dev_priv = dev->dev_private;
12675 struct skl_ddb_allocation hw_ddb, *sw_ddb;
12676 struct intel_crtc *intel_crtc;
12677 int plane;
12678
12679 if (INTEL_INFO(dev)->gen < 9)
12680 return;
12681
12682 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12683 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12684
12685 for_each_intel_crtc(dev, intel_crtc) {
12686 struct skl_ddb_entry *hw_entry, *sw_entry;
12687 const enum pipe pipe = intel_crtc->pipe;
12688
12689 if (!intel_crtc->active)
12690 continue;
12691
12692 /* planes */
dd740780 12693 for_each_plane(dev_priv, pipe, plane) {
08db6652
DL
12694 hw_entry = &hw_ddb.plane[pipe][plane];
12695 sw_entry = &sw_ddb->plane[pipe][plane];
12696
12697 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12698 continue;
12699
12700 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12701 "(expected (%u,%u), found (%u,%u))\n",
12702 pipe_name(pipe), plane + 1,
12703 sw_entry->start, sw_entry->end,
12704 hw_entry->start, hw_entry->end);
12705 }
12706
12707 /* cursor */
4969d33e
MR
12708 hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
12709 sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
08db6652
DL
12710
12711 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12712 continue;
12713
12714 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12715 "(expected (%u,%u), found (%u,%u))\n",
12716 pipe_name(pipe),
12717 sw_entry->start, sw_entry->end,
12718 hw_entry->start, hw_entry->end);
12719 }
12720}
12721
91d1b4bd 12722static void
35dd3c64
ML
12723check_connector_state(struct drm_device *dev,
12724 struct drm_atomic_state *old_state)
8af6cf88 12725{
35dd3c64
ML
12726 struct drm_connector_state *old_conn_state;
12727 struct drm_connector *connector;
12728 int i;
8af6cf88 12729
35dd3c64
ML
12730 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
12731 struct drm_encoder *encoder = connector->encoder;
12732 struct drm_connector_state *state = connector->state;
ad3c558f 12733
8af6cf88
DV
12734 /* This also checks the encoder/connector hw state with the
12735 * ->get_hw_state callbacks. */
35dd3c64 12736 intel_connector_check_state(to_intel_connector(connector));
8af6cf88 12737
ad3c558f 12738 I915_STATE_WARN(state->best_encoder != encoder,
35dd3c64 12739 "connector's atomic encoder doesn't match legacy encoder\n");
8af6cf88 12740 }
91d1b4bd
DV
12741}
12742
12743static void
12744check_encoder_state(struct drm_device *dev)
12745{
12746 struct intel_encoder *encoder;
12747 struct intel_connector *connector;
8af6cf88 12748
b2784e15 12749 for_each_intel_encoder(dev, encoder) {
8af6cf88 12750 bool enabled = false;
4d20cd86 12751 enum pipe pipe;
8af6cf88
DV
12752
12753 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12754 encoder->base.base.id,
8e329a03 12755 encoder->base.name);
8af6cf88 12756
3a3371ff 12757 for_each_intel_connector(dev, connector) {
4d20cd86 12758 if (connector->base.state->best_encoder != &encoder->base)
8af6cf88
DV
12759 continue;
12760 enabled = true;
ad3c558f
ML
12761
12762 I915_STATE_WARN(connector->base.state->crtc !=
12763 encoder->base.crtc,
12764 "connector's crtc doesn't match encoder crtc\n");
8af6cf88 12765 }
0e32b39c 12766
e2c719b7 12767 I915_STATE_WARN(!!encoder->base.crtc != enabled,
8af6cf88
DV
12768 "encoder's enabled state mismatch "
12769 "(expected %i, found %i)\n",
12770 !!encoder->base.crtc, enabled);
7c60d198
ML
12771
12772 if (!encoder->base.crtc) {
4d20cd86 12773 bool active;
7c60d198 12774
4d20cd86
ML
12775 active = encoder->get_hw_state(encoder, &pipe);
12776 I915_STATE_WARN(active,
12777 "encoder detached but still enabled on pipe %c.\n",
12778 pipe_name(pipe));
7c60d198 12779 }
8af6cf88 12780 }
91d1b4bd
DV
12781}
12782
12783static void
4d20cd86 12784check_crtc_state(struct drm_device *dev, struct drm_atomic_state *old_state)
91d1b4bd 12785{
fbee40df 12786 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd 12787 struct intel_encoder *encoder;
4d20cd86
ML
12788 struct drm_crtc_state *old_crtc_state;
12789 struct drm_crtc *crtc;
12790 int i;
8af6cf88 12791
4d20cd86
ML
12792 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
12793 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12794 struct intel_crtc_state *pipe_config, *sw_config;
7b89b8de 12795 bool active;
8af6cf88 12796
bfd16b2a
ML
12797 if (!needs_modeset(crtc->state) &&
12798 !to_intel_crtc_state(crtc->state)->update_pipe)
4d20cd86 12799 continue;
045ac3b5 12800
4d20cd86
ML
12801 __drm_atomic_helper_crtc_destroy_state(crtc, old_crtc_state);
12802 pipe_config = to_intel_crtc_state(old_crtc_state);
12803 memset(pipe_config, 0, sizeof(*pipe_config));
12804 pipe_config->base.crtc = crtc;
12805 pipe_config->base.state = old_state;
8af6cf88 12806
4d20cd86
ML
12807 DRM_DEBUG_KMS("[CRTC:%d]\n",
12808 crtc->base.id);
8af6cf88 12809
4d20cd86
ML
12810 active = dev_priv->display.get_pipe_config(intel_crtc,
12811 pipe_config);
d62cf62a 12812
b6b5d049 12813 /* hw state is inconsistent with the pipe quirk */
4d20cd86
ML
12814 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12815 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
12816 active = crtc->state->active;
6c49f241 12817
4d20cd86 12818 I915_STATE_WARN(crtc->state->active != active,
0e8ffe1b 12819 "crtc active state doesn't match with hw state "
4d20cd86 12820 "(expected %i, found %i)\n", crtc->state->active, active);
0e8ffe1b 12821
4d20cd86 12822 I915_STATE_WARN(intel_crtc->active != crtc->state->active,
53d9f4e9 12823 "transitional active state does not match atomic hw state "
4d20cd86
ML
12824 "(expected %i, found %i)\n", crtc->state->active, intel_crtc->active);
12825
12826 for_each_encoder_on_crtc(dev, crtc, encoder) {
12827 enum pipe pipe;
12828
12829 active = encoder->get_hw_state(encoder, &pipe);
12830 I915_STATE_WARN(active != crtc->state->active,
12831 "[ENCODER:%i] active %i with crtc active %i\n",
12832 encoder->base.base.id, active, crtc->state->active);
12833
12834 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
12835 "Encoder connected to wrong pipe %c\n",
12836 pipe_name(pipe));
12837
12838 if (active)
12839 encoder->get_config(encoder, pipe_config);
12840 }
53d9f4e9 12841
4d20cd86 12842 if (!crtc->state->active)
cfb23ed6
ML
12843 continue;
12844
e3b247da
VS
12845 intel_pipe_config_sanity_check(dev_priv, pipe_config);
12846
4d20cd86
ML
12847 sw_config = to_intel_crtc_state(crtc->state);
12848 if (!intel_pipe_config_compare(dev, sw_config,
12849 pipe_config, false)) {
e2c719b7 12850 I915_STATE_WARN(1, "pipe state doesn't match!\n");
4d20cd86 12851 intel_dump_pipe_config(intel_crtc, pipe_config,
c0b03411 12852 "[hw state]");
4d20cd86 12853 intel_dump_pipe_config(intel_crtc, sw_config,
c0b03411
DV
12854 "[sw state]");
12855 }
8af6cf88
DV
12856 }
12857}
12858
91d1b4bd
DV
12859static void
12860check_shared_dpll_state(struct drm_device *dev)
12861{
fbee40df 12862 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd
DV
12863 struct intel_crtc *crtc;
12864 struct intel_dpll_hw_state dpll_hw_state;
12865 int i;
5358901f
DV
12866
12867 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
8106ddbd
ACO
12868 struct intel_shared_dpll *pll =
12869 intel_get_shared_dpll_by_id(dev_priv, i);
2dd66ebd 12870 unsigned enabled_crtcs = 0, active_crtcs = 0;
5358901f
DV
12871 bool active;
12872
12873 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12874
12875 DRM_DEBUG_KMS("%s\n", pll->name);
12876
2edd6443 12877 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
5358901f 12878
2dd66ebd
ML
12879 I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
12880 "more active pll users than references: %x vs %x\n",
12881 pll->active_mask, pll->config.crtc_mask);
9d16da65
ACO
12882
12883 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
2dd66ebd
ML
12884 I915_STATE_WARN(!pll->on && pll->active_mask,
12885 "pll in active use but not on in sw tracking\n");
12886 I915_STATE_WARN(pll->on && !pll->active_mask,
12887 "pll is on but not used by any active crtc\n");
9d16da65
ACO
12888 I915_STATE_WARN(pll->on != active,
12889 "pll on state mismatch (expected %i, found %i)\n",
12890 pll->on, active);
12891 }
5358901f 12892
d3fcc808 12893 for_each_intel_crtc(dev, crtc) {
8106ddbd 12894 if (crtc->base.state->enable && crtc->config->shared_dpll == pll)
2dd66ebd
ML
12895 enabled_crtcs |= 1 << drm_crtc_index(&crtc->base);
12896 if (crtc->base.state->active && crtc->config->shared_dpll == pll)
12897 active_crtcs |= 1 << drm_crtc_index(&crtc->base);
5358901f 12898 }
2dd66ebd
ML
12899
12900 I915_STATE_WARN(pll->active_mask != active_crtcs,
12901 "pll active crtcs mismatch (expected %x, found %x)\n",
12902 pll->active_mask, active_crtcs);
12903 I915_STATE_WARN(pll->config.crtc_mask != enabled_crtcs,
12904 "pll enabled crtcs mismatch (expected %x, found %x)\n",
12905 pll->config.crtc_mask, enabled_crtcs);
66e985c0 12906
e2c719b7 12907 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
66e985c0
DV
12908 sizeof(dpll_hw_state)),
12909 "pll hw state mismatch\n");
5358901f 12910 }
8af6cf88
DV
12911}
12912
ee165b1a
ML
12913static void
12914intel_modeset_check_state(struct drm_device *dev,
12915 struct drm_atomic_state *old_state)
91d1b4bd 12916{
08db6652 12917 check_wm_state(dev);
35dd3c64 12918 check_connector_state(dev, old_state);
91d1b4bd 12919 check_encoder_state(dev);
4d20cd86 12920 check_crtc_state(dev, old_state);
91d1b4bd
DV
12921 check_shared_dpll_state(dev);
12922}
12923
80715b2f
VS
12924static void update_scanline_offset(struct intel_crtc *crtc)
12925{
12926 struct drm_device *dev = crtc->base.dev;
12927
12928 /*
12929 * The scanline counter increments at the leading edge of hsync.
12930 *
12931 * On most platforms it starts counting from vtotal-1 on the
12932 * first active line. That means the scanline counter value is
12933 * always one less than what we would expect. Ie. just after
12934 * start of vblank, which also occurs at start of hsync (on the
12935 * last active line), the scanline counter will read vblank_start-1.
12936 *
12937 * On gen2 the scanline counter starts counting from 1 instead
12938 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12939 * to keep the value positive), instead of adding one.
12940 *
12941 * On HSW+ the behaviour of the scanline counter depends on the output
12942 * type. For DP ports it behaves like most other platforms, but on HDMI
12943 * there's an extra 1 line difference. So we need to add two instead of
12944 * one to the value.
12945 */
12946 if (IS_GEN2(dev)) {
124abe07 12947 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
80715b2f
VS
12948 int vtotal;
12949
124abe07
VS
12950 vtotal = adjusted_mode->crtc_vtotal;
12951 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
80715b2f
VS
12952 vtotal /= 2;
12953
12954 crtc->scanline_offset = vtotal - 1;
12955 } else if (HAS_DDI(dev) &&
409ee761 12956 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
80715b2f
VS
12957 crtc->scanline_offset = 2;
12958 } else
12959 crtc->scanline_offset = 1;
12960}
12961
ad421372 12962static void intel_modeset_clear_plls(struct drm_atomic_state *state)
ed6739ef 12963{
225da59b 12964 struct drm_device *dev = state->dev;
ed6739ef 12965 struct drm_i915_private *dev_priv = to_i915(dev);
ad421372 12966 struct intel_shared_dpll_config *shared_dpll = NULL;
0a9ab303
ACO
12967 struct drm_crtc *crtc;
12968 struct drm_crtc_state *crtc_state;
0a9ab303 12969 int i;
ed6739ef
ACO
12970
12971 if (!dev_priv->display.crtc_compute_clock)
ad421372 12972 return;
ed6739ef 12973
0a9ab303 12974 for_each_crtc_in_state(state, crtc, crtc_state, i) {
fb1a38a9 12975 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8106ddbd
ACO
12976 struct intel_shared_dpll *old_dpll =
12977 to_intel_crtc_state(crtc->state)->shared_dpll;
0a9ab303 12978
fb1a38a9 12979 if (!needs_modeset(crtc_state))
225da59b
ACO
12980 continue;
12981
8106ddbd 12982 to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
fb1a38a9 12983
8106ddbd 12984 if (!old_dpll)
fb1a38a9 12985 continue;
0a9ab303 12986
ad421372
ML
12987 if (!shared_dpll)
12988 shared_dpll = intel_atomic_get_shared_dpll_state(state);
ed6739ef 12989
8106ddbd 12990 intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
ad421372 12991 }
ed6739ef
ACO
12992}
12993
99d736a2
ML
12994/*
12995 * This implements the workaround described in the "notes" section of the mode
12996 * set sequence documentation. When going from no pipes or single pipe to
12997 * multiple pipes, and planes are enabled after the pipe, we need to wait at
12998 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
12999 */
13000static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13001{
13002 struct drm_crtc_state *crtc_state;
13003 struct intel_crtc *intel_crtc;
13004 struct drm_crtc *crtc;
13005 struct intel_crtc_state *first_crtc_state = NULL;
13006 struct intel_crtc_state *other_crtc_state = NULL;
13007 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13008 int i;
13009
13010 /* look at all crtc's that are going to be enabled in during modeset */
13011 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13012 intel_crtc = to_intel_crtc(crtc);
13013
13014 if (!crtc_state->active || !needs_modeset(crtc_state))
13015 continue;
13016
13017 if (first_crtc_state) {
13018 other_crtc_state = to_intel_crtc_state(crtc_state);
13019 break;
13020 } else {
13021 first_crtc_state = to_intel_crtc_state(crtc_state);
13022 first_pipe = intel_crtc->pipe;
13023 }
13024 }
13025
13026 /* No workaround needed? */
13027 if (!first_crtc_state)
13028 return 0;
13029
13030 /* w/a possibly needed, check how many crtc's are already enabled. */
13031 for_each_intel_crtc(state->dev, intel_crtc) {
13032 struct intel_crtc_state *pipe_config;
13033
13034 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13035 if (IS_ERR(pipe_config))
13036 return PTR_ERR(pipe_config);
13037
13038 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13039
13040 if (!pipe_config->base.active ||
13041 needs_modeset(&pipe_config->base))
13042 continue;
13043
13044 /* 2 or more enabled crtcs means no need for w/a */
13045 if (enabled_pipe != INVALID_PIPE)
13046 return 0;
13047
13048 enabled_pipe = intel_crtc->pipe;
13049 }
13050
13051 if (enabled_pipe != INVALID_PIPE)
13052 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13053 else if (other_crtc_state)
13054 other_crtc_state->hsw_workaround_pipe = first_pipe;
13055
13056 return 0;
13057}
13058
27c329ed
ML
13059static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13060{
13061 struct drm_crtc *crtc;
13062 struct drm_crtc_state *crtc_state;
13063 int ret = 0;
13064
13065 /* add all active pipes to the state */
13066 for_each_crtc(state->dev, crtc) {
13067 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13068 if (IS_ERR(crtc_state))
13069 return PTR_ERR(crtc_state);
13070
13071 if (!crtc_state->active || needs_modeset(crtc_state))
13072 continue;
13073
13074 crtc_state->mode_changed = true;
13075
13076 ret = drm_atomic_add_affected_connectors(state, crtc);
13077 if (ret)
13078 break;
13079
13080 ret = drm_atomic_add_affected_planes(state, crtc);
13081 if (ret)
13082 break;
13083 }
13084
13085 return ret;
13086}
13087
c347a676 13088static int intel_modeset_checks(struct drm_atomic_state *state)
054518dd 13089{
565602d7
ML
13090 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13091 struct drm_i915_private *dev_priv = state->dev->dev_private;
13092 struct drm_crtc *crtc;
13093 struct drm_crtc_state *crtc_state;
13094 int ret = 0, i;
054518dd 13095
b359283a
ML
13096 if (!check_digital_port_conflicts(state)) {
13097 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13098 return -EINVAL;
13099 }
13100
565602d7
ML
13101 intel_state->modeset = true;
13102 intel_state->active_crtcs = dev_priv->active_crtcs;
13103
13104 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13105 if (crtc_state->active)
13106 intel_state->active_crtcs |= 1 << i;
13107 else
13108 intel_state->active_crtcs &= ~(1 << i);
13109 }
13110
054518dd
ACO
13111 /*
13112 * See if the config requires any additional preparation, e.g.
13113 * to adjust global state with pipes off. We need to do this
13114 * here so we can get the modeset_pipe updated config for the new
13115 * mode set on this crtc. For other crtcs we need to use the
13116 * adjusted_mode bits in the crtc directly.
13117 */
27c329ed 13118 if (dev_priv->display.modeset_calc_cdclk) {
27c329ed
ML
13119 ret = dev_priv->display.modeset_calc_cdclk(state);
13120
1a617b77 13121 if (!ret && intel_state->dev_cdclk != dev_priv->cdclk_freq)
27c329ed
ML
13122 ret = intel_modeset_all_pipes(state);
13123
13124 if (ret < 0)
054518dd 13125 return ret;
e8788cbc
ML
13126
13127 DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
13128 intel_state->cdclk, intel_state->dev_cdclk);
27c329ed 13129 } else
1a617b77 13130 to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
054518dd 13131
ad421372 13132 intel_modeset_clear_plls(state);
054518dd 13133
565602d7 13134 if (IS_HASWELL(dev_priv))
ad421372 13135 return haswell_mode_set_planes_workaround(state);
99d736a2 13136
ad421372 13137 return 0;
c347a676
ACO
13138}
13139
aa363136
MR
13140/*
13141 * Handle calculation of various watermark data at the end of the atomic check
13142 * phase. The code here should be run after the per-crtc and per-plane 'check'
13143 * handlers to ensure that all derived state has been updated.
13144 */
13145static void calc_watermark_data(struct drm_atomic_state *state)
13146{
13147 struct drm_device *dev = state->dev;
13148 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13149 struct drm_crtc *crtc;
13150 struct drm_crtc_state *cstate;
13151 struct drm_plane *plane;
13152 struct drm_plane_state *pstate;
13153
13154 /*
13155 * Calculate watermark configuration details now that derived
13156 * plane/crtc state is all properly updated.
13157 */
13158 drm_for_each_crtc(crtc, dev) {
13159 cstate = drm_atomic_get_existing_crtc_state(state, crtc) ?:
13160 crtc->state;
13161
13162 if (cstate->active)
13163 intel_state->wm_config.num_pipes_active++;
13164 }
13165 drm_for_each_legacy_plane(plane, dev) {
13166 pstate = drm_atomic_get_existing_plane_state(state, plane) ?:
13167 plane->state;
13168
13169 if (!to_intel_plane_state(pstate)->visible)
13170 continue;
13171
13172 intel_state->wm_config.sprites_enabled = true;
13173 if (pstate->crtc_w != pstate->src_w >> 16 ||
13174 pstate->crtc_h != pstate->src_h >> 16)
13175 intel_state->wm_config.sprites_scaled = true;
13176 }
13177}
13178
74c090b1
ML
13179/**
13180 * intel_atomic_check - validate state object
13181 * @dev: drm device
13182 * @state: state to validate
13183 */
13184static int intel_atomic_check(struct drm_device *dev,
13185 struct drm_atomic_state *state)
c347a676 13186{
dd8b3bdb 13187 struct drm_i915_private *dev_priv = to_i915(dev);
aa363136 13188 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
c347a676
ACO
13189 struct drm_crtc *crtc;
13190 struct drm_crtc_state *crtc_state;
13191 int ret, i;
61333b60 13192 bool any_ms = false;
c347a676 13193
74c090b1 13194 ret = drm_atomic_helper_check_modeset(dev, state);
054518dd
ACO
13195 if (ret)
13196 return ret;
13197
c347a676 13198 for_each_crtc_in_state(state, crtc, crtc_state, i) {
cfb23ed6
ML
13199 struct intel_crtc_state *pipe_config =
13200 to_intel_crtc_state(crtc_state);
1ed51de9
DV
13201
13202 /* Catch I915_MODE_FLAG_INHERITED */
13203 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
13204 crtc_state->mode_changed = true;
cfb23ed6 13205
61333b60
ML
13206 if (!crtc_state->enable) {
13207 if (needs_modeset(crtc_state))
13208 any_ms = true;
c347a676 13209 continue;
61333b60 13210 }
c347a676 13211
26495481 13212 if (!needs_modeset(crtc_state))
cfb23ed6
ML
13213 continue;
13214
26495481
DV
13215 /* FIXME: For only active_changed we shouldn't need to do any
13216 * state recomputation at all. */
13217
1ed51de9
DV
13218 ret = drm_atomic_add_affected_connectors(state, crtc);
13219 if (ret)
13220 return ret;
b359283a 13221
cfb23ed6 13222 ret = intel_modeset_pipe_config(crtc, pipe_config);
c347a676
ACO
13223 if (ret)
13224 return ret;
13225
73831236 13226 if (i915.fastboot &&
dd8b3bdb 13227 intel_pipe_config_compare(dev,
cfb23ed6 13228 to_intel_crtc_state(crtc->state),
1ed51de9 13229 pipe_config, true)) {
26495481 13230 crtc_state->mode_changed = false;
bfd16b2a 13231 to_intel_crtc_state(crtc_state)->update_pipe = true;
26495481
DV
13232 }
13233
13234 if (needs_modeset(crtc_state)) {
13235 any_ms = true;
cfb23ed6
ML
13236
13237 ret = drm_atomic_add_affected_planes(state, crtc);
13238 if (ret)
13239 return ret;
13240 }
61333b60 13241
26495481
DV
13242 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
13243 needs_modeset(crtc_state) ?
13244 "[modeset]" : "[fastset]");
c347a676
ACO
13245 }
13246
61333b60
ML
13247 if (any_ms) {
13248 ret = intel_modeset_checks(state);
13249
13250 if (ret)
13251 return ret;
27c329ed 13252 } else
dd8b3bdb 13253 intel_state->cdclk = dev_priv->cdclk_freq;
76305b1a 13254
dd8b3bdb 13255 ret = drm_atomic_helper_check_planes(dev, state);
aa363136
MR
13256 if (ret)
13257 return ret;
13258
f51be2e0 13259 intel_fbc_choose_crtc(dev_priv, state);
aa363136
MR
13260 calc_watermark_data(state);
13261
13262 return 0;
054518dd
ACO
13263}
13264
5008e874
ML
13265static int intel_atomic_prepare_commit(struct drm_device *dev,
13266 struct drm_atomic_state *state,
13267 bool async)
13268{
7580d774
ML
13269 struct drm_i915_private *dev_priv = dev->dev_private;
13270 struct drm_plane_state *plane_state;
5008e874 13271 struct drm_crtc_state *crtc_state;
7580d774 13272 struct drm_plane *plane;
5008e874
ML
13273 struct drm_crtc *crtc;
13274 int i, ret;
13275
13276 if (async) {
13277 DRM_DEBUG_KMS("i915 does not yet support async commit\n");
13278 return -EINVAL;
13279 }
13280
13281 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13282 ret = intel_crtc_wait_for_pending_flips(crtc);
13283 if (ret)
13284 return ret;
7580d774
ML
13285
13286 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
13287 flush_workqueue(dev_priv->wq);
5008e874
ML
13288 }
13289
f935675f
ML
13290 ret = mutex_lock_interruptible(&dev->struct_mutex);
13291 if (ret)
13292 return ret;
13293
5008e874 13294 ret = drm_atomic_helper_prepare_planes(dev, state);
7580d774
ML
13295 if (!ret && !async && !i915_reset_in_progress(&dev_priv->gpu_error)) {
13296 u32 reset_counter;
13297
13298 reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
13299 mutex_unlock(&dev->struct_mutex);
13300
13301 for_each_plane_in_state(state, plane, plane_state, i) {
13302 struct intel_plane_state *intel_plane_state =
13303 to_intel_plane_state(plane_state);
13304
13305 if (!intel_plane_state->wait_req)
13306 continue;
13307
13308 ret = __i915_wait_request(intel_plane_state->wait_req,
13309 reset_counter, true,
13310 NULL, NULL);
13311
13312 /* Swallow -EIO errors to allow updates during hw lockup. */
13313 if (ret == -EIO)
13314 ret = 0;
13315
13316 if (ret)
13317 break;
13318 }
13319
13320 if (!ret)
13321 return 0;
13322
13323 mutex_lock(&dev->struct_mutex);
13324 drm_atomic_helper_cleanup_planes(dev, state);
13325 }
5008e874 13326
f935675f 13327 mutex_unlock(&dev->struct_mutex);
5008e874
ML
13328 return ret;
13329}
13330
e8861675
ML
13331static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
13332 struct drm_i915_private *dev_priv,
13333 unsigned crtc_mask)
13334{
13335 unsigned last_vblank_count[I915_MAX_PIPES];
13336 enum pipe pipe;
13337 int ret;
13338
13339 if (!crtc_mask)
13340 return;
13341
13342 for_each_pipe(dev_priv, pipe) {
13343 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13344
13345 if (!((1 << pipe) & crtc_mask))
13346 continue;
13347
13348 ret = drm_crtc_vblank_get(crtc);
13349 if (WARN_ON(ret != 0)) {
13350 crtc_mask &= ~(1 << pipe);
13351 continue;
13352 }
13353
13354 last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
13355 }
13356
13357 for_each_pipe(dev_priv, pipe) {
13358 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13359 long lret;
13360
13361 if (!((1 << pipe) & crtc_mask))
13362 continue;
13363
13364 lret = wait_event_timeout(dev->vblank[pipe].queue,
13365 last_vblank_count[pipe] !=
13366 drm_crtc_vblank_count(crtc),
13367 msecs_to_jiffies(50));
13368
13369 WARN_ON(!lret);
13370
13371 drm_crtc_vblank_put(crtc);
13372 }
13373}
13374
13375static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
13376{
13377 /* fb updated, need to unpin old fb */
13378 if (crtc_state->fb_changed)
13379 return true;
13380
13381 /* wm changes, need vblank before final wm's */
caed361d 13382 if (crtc_state->update_wm_post)
e8861675
ML
13383 return true;
13384
13385 /*
13386 * cxsr is re-enabled after vblank.
caed361d 13387 * This is already handled by crtc_state->update_wm_post,
e8861675
ML
13388 * but added for clarity.
13389 */
13390 if (crtc_state->disable_cxsr)
13391 return true;
13392
13393 return false;
13394}
13395
74c090b1
ML
13396/**
13397 * intel_atomic_commit - commit validated state object
13398 * @dev: DRM device
13399 * @state: the top-level driver state object
13400 * @async: asynchronous commit
13401 *
13402 * This function commits a top-level state object that has been validated
13403 * with drm_atomic_helper_check().
13404 *
13405 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
13406 * we can only handle plane-related operations and do not yet support
13407 * asynchronous commit.
13408 *
13409 * RETURNS
13410 * Zero for success or -errno.
13411 */
13412static int intel_atomic_commit(struct drm_device *dev,
13413 struct drm_atomic_state *state,
13414 bool async)
a6778b3c 13415{
565602d7 13416 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fbee40df 13417 struct drm_i915_private *dev_priv = dev->dev_private;
29ceb0e6 13418 struct drm_crtc_state *old_crtc_state;
7580d774 13419 struct drm_crtc *crtc;
ed4a6a7c 13420 struct intel_crtc_state *intel_cstate;
565602d7
ML
13421 int ret = 0, i;
13422 bool hw_check = intel_state->modeset;
33c8df89 13423 unsigned long put_domains[I915_MAX_PIPES] = {};
e8861675 13424 unsigned crtc_vblank_mask = 0;
a6778b3c 13425
5008e874 13426 ret = intel_atomic_prepare_commit(dev, state, async);
7580d774
ML
13427 if (ret) {
13428 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
d4afb8cc 13429 return ret;
7580d774 13430 }
d4afb8cc 13431
1c5e19f8 13432 drm_atomic_helper_swap_state(dev, state);
a1475e77
ML
13433 dev_priv->wm.config = intel_state->wm_config;
13434 intel_shared_dpll_commit(state);
1c5e19f8 13435
565602d7
ML
13436 if (intel_state->modeset) {
13437 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
13438 sizeof(intel_state->min_pixclk));
13439 dev_priv->active_crtcs = intel_state->active_crtcs;
1a617b77 13440 dev_priv->atomic_cdclk_freq = intel_state->cdclk;
33c8df89
ML
13441
13442 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
565602d7
ML
13443 }
13444
29ceb0e6 13445 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
a539205a
ML
13446 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13447
33c8df89
ML
13448 if (needs_modeset(crtc->state) ||
13449 to_intel_crtc_state(crtc->state)->update_pipe) {
13450 hw_check = true;
13451
13452 put_domains[to_intel_crtc(crtc)->pipe] =
13453 modeset_get_crtc_power_domains(crtc,
13454 to_intel_crtc_state(crtc->state));
13455 }
13456
61333b60
ML
13457 if (!needs_modeset(crtc->state))
13458 continue;
13459
29ceb0e6 13460 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
460da916 13461
29ceb0e6
VS
13462 if (old_crtc_state->active) {
13463 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
a539205a 13464 dev_priv->display.crtc_disable(crtc);
eddfcbcd 13465 intel_crtc->active = false;
58f9c0bc 13466 intel_fbc_disable(intel_crtc);
eddfcbcd 13467 intel_disable_shared_dpll(intel_crtc);
9bbc8258
VS
13468
13469 /*
13470 * Underruns don't always raise
13471 * interrupts, so check manually.
13472 */
13473 intel_check_cpu_fifo_underruns(dev_priv);
13474 intel_check_pch_fifo_underruns(dev_priv);
b9001114
ML
13475
13476 if (!crtc->state->active)
13477 intel_update_watermarks(crtc);
a539205a 13478 }
b8cecdf5 13479 }
7758a113 13480
ea9d758d
DV
13481 /* Only after disabling all output pipelines that will be changed can we
13482 * update the the output configuration. */
4740b0f2 13483 intel_modeset_update_crtc_state(state);
f6e5b160 13484
565602d7 13485 if (intel_state->modeset) {
4740b0f2 13486 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
33c8df89
ML
13487
13488 if (dev_priv->display.modeset_commit_cdclk &&
13489 intel_state->dev_cdclk != dev_priv->cdclk_freq)
13490 dev_priv->display.modeset_commit_cdclk(state);
4740b0f2 13491 }
47fab737 13492
a6778b3c 13493 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
29ceb0e6 13494 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
f6ac4b2a
ML
13495 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13496 bool modeset = needs_modeset(crtc->state);
e8861675
ML
13497 struct intel_crtc_state *pipe_config =
13498 to_intel_crtc_state(crtc->state);
13499 bool update_pipe = !modeset && pipe_config->update_pipe;
9f836f90 13500
f6ac4b2a 13501 if (modeset && crtc->state->active) {
a539205a
ML
13502 update_scanline_offset(to_intel_crtc(crtc));
13503 dev_priv->display.crtc_enable(crtc);
13504 }
80715b2f 13505
82cf435b
LL
13506 if (!modeset &&
13507 crtc->state->active &&
13508 crtc->state->color_mgmt_changed) {
13509 /*
13510 * Only update color management when not doing
13511 * a modeset as this will be done by
13512 * crtc_enable already.
13513 */
13514 intel_color_set_csc(crtc);
13515 intel_color_load_luts(crtc);
13516 }
13517
f6ac4b2a 13518 if (!modeset)
29ceb0e6 13519 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
f6ac4b2a 13520
31ae71fc
ML
13521 if (crtc->state->active &&
13522 drm_atomic_get_existing_plane_state(state, crtc->primary))
49227c4a
PZ
13523 intel_fbc_enable(intel_crtc);
13524
6173ee28
ML
13525 if (crtc->state->active &&
13526 (crtc->state->planes_changed || update_pipe))
29ceb0e6 13527 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
bfd16b2a 13528
e8861675
ML
13529 if (pipe_config->base.active && needs_vblank_wait(pipe_config))
13530 crtc_vblank_mask |= 1 << i;
80715b2f 13531 }
a6778b3c 13532
a6778b3c 13533 /* FIXME: add subpixel order */
83a57153 13534
e8861675
ML
13535 if (!state->legacy_cursor_update)
13536 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
f935675f 13537
ed4a6a7c
MR
13538 /*
13539 * Now that the vblank has passed, we can go ahead and program the
13540 * optimal watermarks on platforms that need two-step watermark
13541 * programming.
13542 *
13543 * TODO: Move this (and other cleanup) to an async worker eventually.
13544 */
29ceb0e6 13545 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
ed4a6a7c
MR
13546 intel_cstate = to_intel_crtc_state(crtc->state);
13547
13548 if (dev_priv->display.optimize_watermarks)
13549 dev_priv->display.optimize_watermarks(intel_cstate);
13550 }
13551
177246a8
MR
13552 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
13553 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
13554
13555 if (put_domains[i])
13556 modeset_put_power_domains(dev_priv, put_domains[i]);
13557 }
13558
13559 if (intel_state->modeset)
13560 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
13561
f935675f 13562 mutex_lock(&dev->struct_mutex);
d4afb8cc 13563 drm_atomic_helper_cleanup_planes(dev, state);
f935675f 13564 mutex_unlock(&dev->struct_mutex);
2bfb4627 13565
565602d7 13566 if (hw_check)
ee165b1a
ML
13567 intel_modeset_check_state(dev, state);
13568
13569 drm_atomic_state_free(state);
f30da187 13570
75714940
MK
13571 /* As one of the primary mmio accessors, KMS has a high likelihood
13572 * of triggering bugs in unclaimed access. After we finish
13573 * modesetting, see if an error has been flagged, and if so
13574 * enable debugging for the next modeset - and hope we catch
13575 * the culprit.
13576 *
13577 * XXX note that we assume display power is on at this point.
13578 * This might hold true now but we need to add pm helper to check
13579 * unclaimed only when the hardware is on, as atomic commits
13580 * can happen also when the device is completely off.
13581 */
13582 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
13583
74c090b1 13584 return 0;
7f27126e
JB
13585}
13586
c0c36b94
CW
13587void intel_crtc_restore_mode(struct drm_crtc *crtc)
13588{
83a57153
ACO
13589 struct drm_device *dev = crtc->dev;
13590 struct drm_atomic_state *state;
e694eb02 13591 struct drm_crtc_state *crtc_state;
2bfb4627 13592 int ret;
83a57153
ACO
13593
13594 state = drm_atomic_state_alloc(dev);
13595 if (!state) {
e694eb02 13596 DRM_DEBUG_KMS("[CRTC:%d] crtc restore failed, out of memory",
83a57153
ACO
13597 crtc->base.id);
13598 return;
13599 }
13600
e694eb02 13601 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
83a57153 13602
e694eb02
ML
13603retry:
13604 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13605 ret = PTR_ERR_OR_ZERO(crtc_state);
13606 if (!ret) {
13607 if (!crtc_state->active)
13608 goto out;
83a57153 13609
e694eb02 13610 crtc_state->mode_changed = true;
74c090b1 13611 ret = drm_atomic_commit(state);
83a57153
ACO
13612 }
13613
e694eb02
ML
13614 if (ret == -EDEADLK) {
13615 drm_atomic_state_clear(state);
13616 drm_modeset_backoff(state->acquire_ctx);
13617 goto retry;
4ed9fb37 13618 }
4be07317 13619
2bfb4627 13620 if (ret)
e694eb02 13621out:
2bfb4627 13622 drm_atomic_state_free(state);
c0c36b94
CW
13623}
13624
25c5b266
DV
13625#undef for_each_intel_crtc_masked
13626
f6e5b160 13627static const struct drm_crtc_funcs intel_crtc_funcs = {
82cf435b 13628 .gamma_set = drm_atomic_helper_legacy_gamma_set,
74c090b1 13629 .set_config = drm_atomic_helper_set_config,
82cf435b 13630 .set_property = drm_atomic_helper_crtc_set_property,
f6e5b160
CW
13631 .destroy = intel_crtc_destroy,
13632 .page_flip = intel_crtc_page_flip,
1356837e
MR
13633 .atomic_duplicate_state = intel_crtc_duplicate_state,
13634 .atomic_destroy_state = intel_crtc_destroy_state,
f6e5b160
CW
13635};
13636
6beb8c23
MR
13637/**
13638 * intel_prepare_plane_fb - Prepare fb for usage on plane
13639 * @plane: drm plane to prepare for
13640 * @fb: framebuffer to prepare for presentation
13641 *
13642 * Prepares a framebuffer for usage on a display plane. Generally this
13643 * involves pinning the underlying object and updating the frontbuffer tracking
13644 * bits. Some older platforms need special physical address handling for
13645 * cursor planes.
13646 *
f935675f
ML
13647 * Must be called with struct_mutex held.
13648 *
6beb8c23
MR
13649 * Returns 0 on success, negative error code on failure.
13650 */
13651int
13652intel_prepare_plane_fb(struct drm_plane *plane,
d136dfee 13653 const struct drm_plane_state *new_state)
465c120c
MR
13654{
13655 struct drm_device *dev = plane->dev;
844f9111 13656 struct drm_framebuffer *fb = new_state->fb;
6beb8c23 13657 struct intel_plane *intel_plane = to_intel_plane(plane);
6beb8c23 13658 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
1ee49399 13659 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
6beb8c23 13660 int ret = 0;
465c120c 13661
1ee49399 13662 if (!obj && !old_obj)
465c120c
MR
13663 return 0;
13664
5008e874
ML
13665 if (old_obj) {
13666 struct drm_crtc_state *crtc_state =
13667 drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
13668
13669 /* Big Hammer, we also need to ensure that any pending
13670 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
13671 * current scanout is retired before unpinning the old
13672 * framebuffer. Note that we rely on userspace rendering
13673 * into the buffer attached to the pipe they are waiting
13674 * on. If not, userspace generates a GPU hang with IPEHR
13675 * point to the MI_WAIT_FOR_EVENT.
13676 *
13677 * This should only fail upon a hung GPU, in which case we
13678 * can safely continue.
13679 */
13680 if (needs_modeset(crtc_state))
13681 ret = i915_gem_object_wait_rendering(old_obj, true);
13682
13683 /* Swallow -EIO errors to allow updates during hw lockup. */
13684 if (ret && ret != -EIO)
f935675f 13685 return ret;
5008e874
ML
13686 }
13687
3c28ff22
AG
13688 /* For framebuffer backed by dmabuf, wait for fence */
13689 if (obj && obj->base.dma_buf) {
bcf8be27
ML
13690 long lret;
13691
13692 lret = reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
13693 false, true,
13694 MAX_SCHEDULE_TIMEOUT);
13695 if (lret == -ERESTARTSYS)
13696 return lret;
3c28ff22 13697
bcf8be27 13698 WARN(lret < 0, "waiting returns %li\n", lret);
3c28ff22
AG
13699 }
13700
1ee49399
ML
13701 if (!obj) {
13702 ret = 0;
13703 } else if (plane->type == DRM_PLANE_TYPE_CURSOR &&
6beb8c23
MR
13704 INTEL_INFO(dev)->cursor_needs_physical) {
13705 int align = IS_I830(dev) ? 16 * 1024 : 256;
13706 ret = i915_gem_object_attach_phys(obj, align);
13707 if (ret)
13708 DRM_DEBUG_KMS("failed to attach phys object\n");
13709 } else {
3465c580 13710 ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
6beb8c23 13711 }
465c120c 13712
7580d774
ML
13713 if (ret == 0) {
13714 if (obj) {
13715 struct intel_plane_state *plane_state =
13716 to_intel_plane_state(new_state);
13717
13718 i915_gem_request_assign(&plane_state->wait_req,
13719 obj->last_write_req);
13720 }
13721
a9ff8714 13722 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
7580d774 13723 }
fdd508a6 13724
6beb8c23
MR
13725 return ret;
13726}
13727
38f3ce3a
MR
13728/**
13729 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13730 * @plane: drm plane to clean up for
13731 * @fb: old framebuffer that was on plane
13732 *
13733 * Cleans up a framebuffer that has just been removed from a plane.
f935675f
ML
13734 *
13735 * Must be called with struct_mutex held.
38f3ce3a
MR
13736 */
13737void
13738intel_cleanup_plane_fb(struct drm_plane *plane,
d136dfee 13739 const struct drm_plane_state *old_state)
38f3ce3a
MR
13740{
13741 struct drm_device *dev = plane->dev;
1ee49399 13742 struct intel_plane *intel_plane = to_intel_plane(plane);
7580d774 13743 struct intel_plane_state *old_intel_state;
1ee49399
ML
13744 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
13745 struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
38f3ce3a 13746
7580d774
ML
13747 old_intel_state = to_intel_plane_state(old_state);
13748
1ee49399 13749 if (!obj && !old_obj)
38f3ce3a
MR
13750 return;
13751
1ee49399
ML
13752 if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
13753 !INTEL_INFO(dev)->cursor_needs_physical))
3465c580 13754 intel_unpin_fb_obj(old_state->fb, old_state->rotation);
1ee49399
ML
13755
13756 /* prepare_fb aborted? */
13757 if ((old_obj && (old_obj->frontbuffer_bits & intel_plane->frontbuffer_bit)) ||
13758 (obj && !(obj->frontbuffer_bits & intel_plane->frontbuffer_bit)))
13759 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
7580d774
ML
13760
13761 i915_gem_request_assign(&old_intel_state->wait_req, NULL);
465c120c
MR
13762}
13763
6156a456
CK
13764int
13765skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13766{
13767 int max_scale;
13768 struct drm_device *dev;
13769 struct drm_i915_private *dev_priv;
13770 int crtc_clock, cdclk;
13771
bf8a0af0 13772 if (!intel_crtc || !crtc_state->base.enable)
6156a456
CK
13773 return DRM_PLANE_HELPER_NO_SCALING;
13774
13775 dev = intel_crtc->base.dev;
13776 dev_priv = dev->dev_private;
13777 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
27c329ed 13778 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
6156a456 13779
54bf1ce6 13780 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
6156a456
CK
13781 return DRM_PLANE_HELPER_NO_SCALING;
13782
13783 /*
13784 * skl max scale is lower of:
13785 * close to 3 but not 3, -1 is for that purpose
13786 * or
13787 * cdclk/crtc_clock
13788 */
13789 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13790
13791 return max_scale;
13792}
13793
465c120c 13794static int
3c692a41 13795intel_check_primary_plane(struct drm_plane *plane,
061e4b8d 13796 struct intel_crtc_state *crtc_state,
3c692a41
GP
13797 struct intel_plane_state *state)
13798{
2b875c22
MR
13799 struct drm_crtc *crtc = state->base.crtc;
13800 struct drm_framebuffer *fb = state->base.fb;
6156a456 13801 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
061e4b8d
ML
13802 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13803 bool can_position = false;
465c120c 13804
693bdc28
VS
13805 if (INTEL_INFO(plane->dev)->gen >= 9) {
13806 /* use scaler when colorkey is not required */
13807 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
13808 min_scale = 1;
13809 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
13810 }
d8106366 13811 can_position = true;
6156a456 13812 }
d8106366 13813
061e4b8d
ML
13814 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13815 &state->dst, &state->clip,
da20eabd
ML
13816 min_scale, max_scale,
13817 can_position, true,
13818 &state->visible);
14af293f
GP
13819}
13820
613d2b27
ML
13821static void intel_begin_crtc_commit(struct drm_crtc *crtc,
13822 struct drm_crtc_state *old_crtc_state)
3c692a41 13823{
32b7eeec 13824 struct drm_device *dev = crtc->dev;
3c692a41 13825 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
bfd16b2a
ML
13826 struct intel_crtc_state *old_intel_state =
13827 to_intel_crtc_state(old_crtc_state);
13828 bool modeset = needs_modeset(crtc->state);
3c692a41 13829
c34c9ee4 13830 /* Perform vblank evasion around commit operation */
62852622 13831 intel_pipe_update_start(intel_crtc);
0583236e 13832
bfd16b2a
ML
13833 if (modeset)
13834 return;
13835
13836 if (to_intel_crtc_state(crtc->state)->update_pipe)
13837 intel_update_pipe_config(intel_crtc, old_intel_state);
13838 else if (INTEL_INFO(dev)->gen >= 9)
0583236e 13839 skl_detach_scalers(intel_crtc);
32b7eeec
MR
13840}
13841
613d2b27
ML
13842static void intel_finish_crtc_commit(struct drm_crtc *crtc,
13843 struct drm_crtc_state *old_crtc_state)
32b7eeec 13844{
32b7eeec 13845 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
32b7eeec 13846
62852622 13847 intel_pipe_update_end(intel_crtc);
3c692a41
GP
13848}
13849
cf4c7c12 13850/**
4a3b8769
MR
13851 * intel_plane_destroy - destroy a plane
13852 * @plane: plane to destroy
cf4c7c12 13853 *
4a3b8769
MR
13854 * Common destruction function for all types of planes (primary, cursor,
13855 * sprite).
cf4c7c12 13856 */
4a3b8769 13857void intel_plane_destroy(struct drm_plane *plane)
465c120c
MR
13858{
13859 struct intel_plane *intel_plane = to_intel_plane(plane);
13860 drm_plane_cleanup(plane);
13861 kfree(intel_plane);
13862}
13863
65a3fea0 13864const struct drm_plane_funcs intel_plane_funcs = {
70a101f8
MR
13865 .update_plane = drm_atomic_helper_update_plane,
13866 .disable_plane = drm_atomic_helper_disable_plane,
3d7d6510 13867 .destroy = intel_plane_destroy,
c196e1d6 13868 .set_property = drm_atomic_helper_plane_set_property,
a98b3431
MR
13869 .atomic_get_property = intel_plane_atomic_get_property,
13870 .atomic_set_property = intel_plane_atomic_set_property,
ea2c67bb
MR
13871 .atomic_duplicate_state = intel_plane_duplicate_state,
13872 .atomic_destroy_state = intel_plane_destroy_state,
13873
465c120c
MR
13874};
13875
13876static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13877 int pipe)
13878{
13879 struct intel_plane *primary;
8e7d688b 13880 struct intel_plane_state *state;
465c120c 13881 const uint32_t *intel_primary_formats;
45e3743a 13882 unsigned int num_formats;
465c120c
MR
13883
13884 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13885 if (primary == NULL)
13886 return NULL;
13887
8e7d688b
MR
13888 state = intel_create_plane_state(&primary->base);
13889 if (!state) {
ea2c67bb
MR
13890 kfree(primary);
13891 return NULL;
13892 }
8e7d688b 13893 primary->base.state = &state->base;
ea2c67bb 13894
465c120c
MR
13895 primary->can_scale = false;
13896 primary->max_downscale = 1;
6156a456
CK
13897 if (INTEL_INFO(dev)->gen >= 9) {
13898 primary->can_scale = true;
af99ceda 13899 state->scaler_id = -1;
6156a456 13900 }
465c120c
MR
13901 primary->pipe = pipe;
13902 primary->plane = pipe;
a9ff8714 13903 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
c59cb179 13904 primary->check_plane = intel_check_primary_plane;
465c120c
MR
13905 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13906 primary->plane = !pipe;
13907
6c0fd451
DL
13908 if (INTEL_INFO(dev)->gen >= 9) {
13909 intel_primary_formats = skl_primary_formats;
13910 num_formats = ARRAY_SIZE(skl_primary_formats);
a8d201af
ML
13911
13912 primary->update_plane = skylake_update_primary_plane;
13913 primary->disable_plane = skylake_disable_primary_plane;
13914 } else if (HAS_PCH_SPLIT(dev)) {
13915 intel_primary_formats = i965_primary_formats;
13916 num_formats = ARRAY_SIZE(i965_primary_formats);
13917
13918 primary->update_plane = ironlake_update_primary_plane;
13919 primary->disable_plane = i9xx_disable_primary_plane;
6c0fd451 13920 } else if (INTEL_INFO(dev)->gen >= 4) {
568db4f2
DL
13921 intel_primary_formats = i965_primary_formats;
13922 num_formats = ARRAY_SIZE(i965_primary_formats);
a8d201af
ML
13923
13924 primary->update_plane = i9xx_update_primary_plane;
13925 primary->disable_plane = i9xx_disable_primary_plane;
6c0fd451
DL
13926 } else {
13927 intel_primary_formats = i8xx_primary_formats;
13928 num_formats = ARRAY_SIZE(i8xx_primary_formats);
a8d201af
ML
13929
13930 primary->update_plane = i9xx_update_primary_plane;
13931 primary->disable_plane = i9xx_disable_primary_plane;
465c120c
MR
13932 }
13933
13934 drm_universal_plane_init(dev, &primary->base, 0,
65a3fea0 13935 &intel_plane_funcs,
465c120c 13936 intel_primary_formats, num_formats,
b0b3b795 13937 DRM_PLANE_TYPE_PRIMARY, NULL);
48404c1e 13938
3b7a5119
SJ
13939 if (INTEL_INFO(dev)->gen >= 4)
13940 intel_create_rotation_property(dev, primary);
48404c1e 13941
ea2c67bb
MR
13942 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13943
465c120c
MR
13944 return &primary->base;
13945}
13946
3b7a5119
SJ
13947void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
13948{
13949 if (!dev->mode_config.rotation_property) {
13950 unsigned long flags = BIT(DRM_ROTATE_0) |
13951 BIT(DRM_ROTATE_180);
13952
13953 if (INTEL_INFO(dev)->gen >= 9)
13954 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
13955
13956 dev->mode_config.rotation_property =
13957 drm_mode_create_rotation_property(dev, flags);
13958 }
13959 if (dev->mode_config.rotation_property)
13960 drm_object_attach_property(&plane->base.base,
13961 dev->mode_config.rotation_property,
13962 plane->base.state->rotation);
13963}
13964
3d7d6510 13965static int
852e787c 13966intel_check_cursor_plane(struct drm_plane *plane,
061e4b8d 13967 struct intel_crtc_state *crtc_state,
852e787c 13968 struct intel_plane_state *state)
3d7d6510 13969{
061e4b8d 13970 struct drm_crtc *crtc = crtc_state->base.crtc;
2b875c22 13971 struct drm_framebuffer *fb = state->base.fb;
757f9a3e 13972 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
b29ec92c 13973 enum pipe pipe = to_intel_plane(plane)->pipe;
757f9a3e
GP
13974 unsigned stride;
13975 int ret;
3d7d6510 13976
061e4b8d
ML
13977 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13978 &state->dst, &state->clip,
3d7d6510
MR
13979 DRM_PLANE_HELPER_NO_SCALING,
13980 DRM_PLANE_HELPER_NO_SCALING,
852e787c 13981 true, true, &state->visible);
757f9a3e
GP
13982 if (ret)
13983 return ret;
13984
757f9a3e
GP
13985 /* if we want to turn off the cursor ignore width and height */
13986 if (!obj)
da20eabd 13987 return 0;
757f9a3e 13988
757f9a3e 13989 /* Check for which cursor types we support */
061e4b8d 13990 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
ea2c67bb
MR
13991 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
13992 state->base.crtc_w, state->base.crtc_h);
757f9a3e
GP
13993 return -EINVAL;
13994 }
13995
ea2c67bb
MR
13996 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
13997 if (obj->base.size < stride * state->base.crtc_h) {
757f9a3e
GP
13998 DRM_DEBUG_KMS("buffer is too small\n");
13999 return -ENOMEM;
14000 }
14001
3a656b54 14002 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
757f9a3e 14003 DRM_DEBUG_KMS("cursor cannot be tiled\n");
da20eabd 14004 return -EINVAL;
32b7eeec
MR
14005 }
14006
b29ec92c
VS
14007 /*
14008 * There's something wrong with the cursor on CHV pipe C.
14009 * If it straddles the left edge of the screen then
14010 * moving it away from the edge or disabling it often
14011 * results in a pipe underrun, and often that can lead to
14012 * dead pipe (constant underrun reported, and it scans
14013 * out just a solid color). To recover from that, the
14014 * display power well must be turned off and on again.
14015 * Refuse the put the cursor into that compromised position.
14016 */
14017 if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
14018 state->visible && state->base.crtc_x < 0) {
14019 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
14020 return -EINVAL;
14021 }
14022
da20eabd 14023 return 0;
852e787c 14024}
3d7d6510 14025
a8ad0d8e
ML
14026static void
14027intel_disable_cursor_plane(struct drm_plane *plane,
7fabf5ef 14028 struct drm_crtc *crtc)
a8ad0d8e 14029{
f2858021
ML
14030 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14031
14032 intel_crtc->cursor_addr = 0;
55a08b3f 14033 intel_crtc_update_cursor(crtc, NULL);
a8ad0d8e
ML
14034}
14035
f4a2cf29 14036static void
55a08b3f
ML
14037intel_update_cursor_plane(struct drm_plane *plane,
14038 const struct intel_crtc_state *crtc_state,
14039 const struct intel_plane_state *state)
852e787c 14040{
55a08b3f
ML
14041 struct drm_crtc *crtc = crtc_state->base.crtc;
14042 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ea2c67bb 14043 struct drm_device *dev = plane->dev;
2b875c22 14044 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
a912f12f 14045 uint32_t addr;
852e787c 14046
f4a2cf29 14047 if (!obj)
a912f12f 14048 addr = 0;
f4a2cf29 14049 else if (!INTEL_INFO(dev)->cursor_needs_physical)
a912f12f 14050 addr = i915_gem_obj_ggtt_offset(obj);
f4a2cf29 14051 else
a912f12f 14052 addr = obj->phys_handle->busaddr;
852e787c 14053
a912f12f 14054 intel_crtc->cursor_addr = addr;
55a08b3f 14055 intel_crtc_update_cursor(crtc, state);
852e787c
GP
14056}
14057
3d7d6510
MR
14058static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14059 int pipe)
14060{
14061 struct intel_plane *cursor;
8e7d688b 14062 struct intel_plane_state *state;
3d7d6510
MR
14063
14064 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
14065 if (cursor == NULL)
14066 return NULL;
14067
8e7d688b
MR
14068 state = intel_create_plane_state(&cursor->base);
14069 if (!state) {
ea2c67bb
MR
14070 kfree(cursor);
14071 return NULL;
14072 }
8e7d688b 14073 cursor->base.state = &state->base;
ea2c67bb 14074
3d7d6510
MR
14075 cursor->can_scale = false;
14076 cursor->max_downscale = 1;
14077 cursor->pipe = pipe;
14078 cursor->plane = pipe;
a9ff8714 14079 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
c59cb179 14080 cursor->check_plane = intel_check_cursor_plane;
55a08b3f 14081 cursor->update_plane = intel_update_cursor_plane;
a8ad0d8e 14082 cursor->disable_plane = intel_disable_cursor_plane;
3d7d6510
MR
14083
14084 drm_universal_plane_init(dev, &cursor->base, 0,
65a3fea0 14085 &intel_plane_funcs,
3d7d6510
MR
14086 intel_cursor_formats,
14087 ARRAY_SIZE(intel_cursor_formats),
b0b3b795 14088 DRM_PLANE_TYPE_CURSOR, NULL);
4398ad45
VS
14089
14090 if (INTEL_INFO(dev)->gen >= 4) {
14091 if (!dev->mode_config.rotation_property)
14092 dev->mode_config.rotation_property =
14093 drm_mode_create_rotation_property(dev,
14094 BIT(DRM_ROTATE_0) |
14095 BIT(DRM_ROTATE_180));
14096 if (dev->mode_config.rotation_property)
14097 drm_object_attach_property(&cursor->base.base,
14098 dev->mode_config.rotation_property,
8e7d688b 14099 state->base.rotation);
4398ad45
VS
14100 }
14101
af99ceda
CK
14102 if (INTEL_INFO(dev)->gen >=9)
14103 state->scaler_id = -1;
14104
ea2c67bb
MR
14105 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14106
3d7d6510
MR
14107 return &cursor->base;
14108}
14109
549e2bfb
CK
14110static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14111 struct intel_crtc_state *crtc_state)
14112{
14113 int i;
14114 struct intel_scaler *intel_scaler;
14115 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14116
14117 for (i = 0; i < intel_crtc->num_scalers; i++) {
14118 intel_scaler = &scaler_state->scalers[i];
14119 intel_scaler->in_use = 0;
549e2bfb
CK
14120 intel_scaler->mode = PS_SCALER_MODE_DYN;
14121 }
14122
14123 scaler_state->scaler_id = -1;
14124}
14125
b358d0a6 14126static void intel_crtc_init(struct drm_device *dev, int pipe)
79e53945 14127{
fbee40df 14128 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 14129 struct intel_crtc *intel_crtc;
f5de6e07 14130 struct intel_crtc_state *crtc_state = NULL;
3d7d6510
MR
14131 struct drm_plane *primary = NULL;
14132 struct drm_plane *cursor = NULL;
8563b1e8 14133 int ret;
79e53945 14134
955382f3 14135 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
79e53945
JB
14136 if (intel_crtc == NULL)
14137 return;
14138
f5de6e07
ACO
14139 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14140 if (!crtc_state)
14141 goto fail;
550acefd
ACO
14142 intel_crtc->config = crtc_state;
14143 intel_crtc->base.state = &crtc_state->base;
07878248 14144 crtc_state->base.crtc = &intel_crtc->base;
f5de6e07 14145
549e2bfb
CK
14146 /* initialize shared scalers */
14147 if (INTEL_INFO(dev)->gen >= 9) {
14148 if (pipe == PIPE_C)
14149 intel_crtc->num_scalers = 1;
14150 else
14151 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14152
14153 skl_init_scalers(dev, intel_crtc, crtc_state);
14154 }
14155
465c120c 14156 primary = intel_primary_plane_create(dev, pipe);
3d7d6510
MR
14157 if (!primary)
14158 goto fail;
14159
14160 cursor = intel_cursor_plane_create(dev, pipe);
14161 if (!cursor)
14162 goto fail;
14163
465c120c 14164 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
f9882876 14165 cursor, &intel_crtc_funcs, NULL);
3d7d6510
MR
14166 if (ret)
14167 goto fail;
79e53945 14168
1f1c2e24
VS
14169 /*
14170 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
8c0f92e1 14171 * is hooked to pipe B. Hence we want plane A feeding pipe B.
1f1c2e24 14172 */
80824003
JB
14173 intel_crtc->pipe = pipe;
14174 intel_crtc->plane = pipe;
3a77c4c4 14175 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
28c97730 14176 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
e2e767ab 14177 intel_crtc->plane = !pipe;
80824003
JB
14178 }
14179
4b0e333e
CW
14180 intel_crtc->cursor_base = ~0;
14181 intel_crtc->cursor_cntl = ~0;
dc41c154 14182 intel_crtc->cursor_size = ~0;
8d7849db 14183
852eb00d
VS
14184 intel_crtc->wm.cxsr_allowed = true;
14185
22fd0fab
JB
14186 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14187 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14188 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14189 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14190
79e53945 14191 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
87b6b101 14192
8563b1e8
LL
14193 intel_color_init(&intel_crtc->base);
14194
87b6b101 14195 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
3d7d6510
MR
14196 return;
14197
14198fail:
14199 if (primary)
14200 drm_plane_cleanup(primary);
14201 if (cursor)
14202 drm_plane_cleanup(cursor);
f5de6e07 14203 kfree(crtc_state);
3d7d6510 14204 kfree(intel_crtc);
79e53945
JB
14205}
14206
752aa88a
JB
14207enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14208{
14209 struct drm_encoder *encoder = connector->base.encoder;
6e9f798d 14210 struct drm_device *dev = connector->base.dev;
752aa88a 14211
51fd371b 14212 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
752aa88a 14213
d3babd3f 14214 if (!encoder || WARN_ON(!encoder->crtc))
752aa88a
JB
14215 return INVALID_PIPE;
14216
14217 return to_intel_crtc(encoder->crtc)->pipe;
14218}
14219
08d7b3d1 14220int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 14221 struct drm_file *file)
08d7b3d1 14222{
08d7b3d1 14223 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7707e653 14224 struct drm_crtc *drmmode_crtc;
c05422d5 14225 struct intel_crtc *crtc;
08d7b3d1 14226
7707e653 14227 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
08d7b3d1 14228
7707e653 14229 if (!drmmode_crtc) {
08d7b3d1 14230 DRM_ERROR("no such CRTC id\n");
3f2c2057 14231 return -ENOENT;
08d7b3d1
CW
14232 }
14233
7707e653 14234 crtc = to_intel_crtc(drmmode_crtc);
c05422d5 14235 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 14236
c05422d5 14237 return 0;
08d7b3d1
CW
14238}
14239
66a9278e 14240static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 14241{
66a9278e
DV
14242 struct drm_device *dev = encoder->base.dev;
14243 struct intel_encoder *source_encoder;
79e53945 14244 int index_mask = 0;
79e53945
JB
14245 int entry = 0;
14246
b2784e15 14247 for_each_intel_encoder(dev, source_encoder) {
bc079e8b 14248 if (encoders_cloneable(encoder, source_encoder))
66a9278e
DV
14249 index_mask |= (1 << entry);
14250
79e53945
JB
14251 entry++;
14252 }
4ef69c7a 14253
79e53945
JB
14254 return index_mask;
14255}
14256
4d302442
CW
14257static bool has_edp_a(struct drm_device *dev)
14258{
14259 struct drm_i915_private *dev_priv = dev->dev_private;
14260
14261 if (!IS_MOBILE(dev))
14262 return false;
14263
14264 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14265 return false;
14266
e3589908 14267 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
14268 return false;
14269
14270 return true;
14271}
14272
84b4e042
JB
14273static bool intel_crt_present(struct drm_device *dev)
14274{
14275 struct drm_i915_private *dev_priv = dev->dev_private;
14276
884497ed
DL
14277 if (INTEL_INFO(dev)->gen >= 9)
14278 return false;
14279
cf404ce4 14280 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
84b4e042
JB
14281 return false;
14282
14283 if (IS_CHERRYVIEW(dev))
14284 return false;
14285
65e472e4
VS
14286 if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
14287 return false;
14288
70ac54d0
VS
14289 /* DDI E can't be used if DDI A requires 4 lanes */
14290 if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
14291 return false;
14292
e4abb733 14293 if (!dev_priv->vbt.int_crt_support)
84b4e042
JB
14294 return false;
14295
14296 return true;
14297}
14298
79e53945
JB
14299static void intel_setup_outputs(struct drm_device *dev)
14300{
725e30ad 14301 struct drm_i915_private *dev_priv = dev->dev_private;
4ef69c7a 14302 struct intel_encoder *encoder;
cb0953d7 14303 bool dpd_is_edp = false;
79e53945 14304
c9093354 14305 intel_lvds_init(dev);
79e53945 14306
84b4e042 14307 if (intel_crt_present(dev))
79935fca 14308 intel_crt_init(dev);
cb0953d7 14309
c776eb2e
VK
14310 if (IS_BROXTON(dev)) {
14311 /*
14312 * FIXME: Broxton doesn't support port detection via the
14313 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14314 * detect the ports.
14315 */
14316 intel_ddi_init(dev, PORT_A);
14317 intel_ddi_init(dev, PORT_B);
14318 intel_ddi_init(dev, PORT_C);
c6c794a2
SS
14319
14320 intel_dsi_init(dev);
c776eb2e 14321 } else if (HAS_DDI(dev)) {
0e72a5b5
ED
14322 int found;
14323
de31facd
JB
14324 /*
14325 * Haswell uses DDI functions to detect digital outputs.
14326 * On SKL pre-D0 the strap isn't connected, so we assume
14327 * it's there.
14328 */
77179400 14329 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
de31facd 14330 /* WaIgnoreDDIAStrap: skl */
ef11bdb3 14331 if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
0e72a5b5
ED
14332 intel_ddi_init(dev, PORT_A);
14333
14334 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14335 * register */
14336 found = I915_READ(SFUSE_STRAP);
14337
14338 if (found & SFUSE_STRAP_DDIB_DETECTED)
14339 intel_ddi_init(dev, PORT_B);
14340 if (found & SFUSE_STRAP_DDIC_DETECTED)
14341 intel_ddi_init(dev, PORT_C);
14342 if (found & SFUSE_STRAP_DDID_DETECTED)
14343 intel_ddi_init(dev, PORT_D);
2800e4c2
RV
14344 /*
14345 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14346 */
ef11bdb3 14347 if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
2800e4c2
RV
14348 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14349 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14350 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
14351 intel_ddi_init(dev, PORT_E);
14352
0e72a5b5 14353 } else if (HAS_PCH_SPLIT(dev)) {
cb0953d7 14354 int found;
5d8a7752 14355 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
270b3042
DV
14356
14357 if (has_edp_a(dev))
14358 intel_dp_init(dev, DP_A, PORT_A);
cb0953d7 14359
dc0fa718 14360 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 14361 /* PCH SDVOB multiplex with HDMIB */
2a5c0832 14362 found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
30ad48b7 14363 if (!found)
e2debe91 14364 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
5eb08b69 14365 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
ab9d7c30 14366 intel_dp_init(dev, PCH_DP_B, PORT_B);
30ad48b7
ZW
14367 }
14368
dc0fa718 14369 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
e2debe91 14370 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
30ad48b7 14371
dc0fa718 14372 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
e2debe91 14373 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
30ad48b7 14374
5eb08b69 14375 if (I915_READ(PCH_DP_C) & DP_DETECTED)
ab9d7c30 14376 intel_dp_init(dev, PCH_DP_C, PORT_C);
5eb08b69 14377
270b3042 14378 if (I915_READ(PCH_DP_D) & DP_DETECTED)
ab9d7c30 14379 intel_dp_init(dev, PCH_DP_D, PORT_D);
666a4537 14380 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
e17ac6db
VS
14381 /*
14382 * The DP_DETECTED bit is the latched state of the DDC
14383 * SDA pin at boot. However since eDP doesn't require DDC
14384 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14385 * eDP ports may have been muxed to an alternate function.
14386 * Thus we can't rely on the DP_DETECTED bit alone to detect
14387 * eDP ports. Consult the VBT as well as DP_DETECTED to
14388 * detect eDP ports.
14389 */
e66eb81d 14390 if (I915_READ(VLV_HDMIB) & SDVO_DETECTED &&
d2182a66 14391 !intel_dp_is_edp(dev, PORT_B))
e66eb81d
VS
14392 intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
14393 if (I915_READ(VLV_DP_B) & DP_DETECTED ||
e17ac6db 14394 intel_dp_is_edp(dev, PORT_B))
e66eb81d 14395 intel_dp_init(dev, VLV_DP_B, PORT_B);
585a94b8 14396
e66eb81d 14397 if (I915_READ(VLV_HDMIC) & SDVO_DETECTED &&
d2182a66 14398 !intel_dp_is_edp(dev, PORT_C))
e66eb81d
VS
14399 intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
14400 if (I915_READ(VLV_DP_C) & DP_DETECTED ||
e17ac6db 14401 intel_dp_is_edp(dev, PORT_C))
e66eb81d 14402 intel_dp_init(dev, VLV_DP_C, PORT_C);
19c03924 14403
9418c1f1 14404 if (IS_CHERRYVIEW(dev)) {
e17ac6db 14405 /* eDP not supported on port D, so don't check VBT */
e66eb81d
VS
14406 if (I915_READ(CHV_HDMID) & SDVO_DETECTED)
14407 intel_hdmi_init(dev, CHV_HDMID, PORT_D);
14408 if (I915_READ(CHV_DP_D) & DP_DETECTED)
14409 intel_dp_init(dev, CHV_DP_D, PORT_D);
9418c1f1
VS
14410 }
14411
3cfca973 14412 intel_dsi_init(dev);
09da55dc 14413 } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
27185ae1 14414 bool found = false;
7d57382e 14415
e2debe91 14416 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14417 DRM_DEBUG_KMS("probing SDVOB\n");
2a5c0832 14418 found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
3fec3d2f 14419 if (!found && IS_G4X(dev)) {
b01f2c3a 14420 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
e2debe91 14421 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
b01f2c3a 14422 }
27185ae1 14423
3fec3d2f 14424 if (!found && IS_G4X(dev))
ab9d7c30 14425 intel_dp_init(dev, DP_B, PORT_B);
725e30ad 14426 }
13520b05
KH
14427
14428 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 14429
e2debe91 14430 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14431 DRM_DEBUG_KMS("probing SDVOC\n");
2a5c0832 14432 found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
b01f2c3a 14433 }
27185ae1 14434
e2debe91 14435 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 14436
3fec3d2f 14437 if (IS_G4X(dev)) {
b01f2c3a 14438 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
e2debe91 14439 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
b01f2c3a 14440 }
3fec3d2f 14441 if (IS_G4X(dev))
ab9d7c30 14442 intel_dp_init(dev, DP_C, PORT_C);
725e30ad 14443 }
27185ae1 14444
3fec3d2f 14445 if (IS_G4X(dev) &&
e7281eab 14446 (I915_READ(DP_D) & DP_DETECTED))
ab9d7c30 14447 intel_dp_init(dev, DP_D, PORT_D);
bad720ff 14448 } else if (IS_GEN2(dev))
79e53945
JB
14449 intel_dvo_init(dev);
14450
103a196f 14451 if (SUPPORTS_TV(dev))
79e53945
JB
14452 intel_tv_init(dev);
14453
0bc12bcb 14454 intel_psr_init(dev);
7c8f8a70 14455
b2784e15 14456 for_each_intel_encoder(dev, encoder) {
4ef69c7a
CW
14457 encoder->base.possible_crtcs = encoder->crtc_mask;
14458 encoder->base.possible_clones =
66a9278e 14459 intel_encoder_clones(encoder);
79e53945 14460 }
47356eb6 14461
dde86e2d 14462 intel_init_pch_refclk(dev);
270b3042
DV
14463
14464 drm_helper_move_panel_connectors_to_head(dev);
79e53945
JB
14465}
14466
14467static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14468{
60a5ca01 14469 struct drm_device *dev = fb->dev;
79e53945 14470 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 14471
ef2d633e 14472 drm_framebuffer_cleanup(fb);
60a5ca01 14473 mutex_lock(&dev->struct_mutex);
ef2d633e 14474 WARN_ON(!intel_fb->obj->framebuffer_references--);
60a5ca01
VS
14475 drm_gem_object_unreference(&intel_fb->obj->base);
14476 mutex_unlock(&dev->struct_mutex);
79e53945
JB
14477 kfree(intel_fb);
14478}
14479
14480static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 14481 struct drm_file *file,
79e53945
JB
14482 unsigned int *handle)
14483{
14484 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 14485 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 14486
cc917ab4
CW
14487 if (obj->userptr.mm) {
14488 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14489 return -EINVAL;
14490 }
14491
05394f39 14492 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
14493}
14494
86c98588
RV
14495static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14496 struct drm_file *file,
14497 unsigned flags, unsigned color,
14498 struct drm_clip_rect *clips,
14499 unsigned num_clips)
14500{
14501 struct drm_device *dev = fb->dev;
14502 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14503 struct drm_i915_gem_object *obj = intel_fb->obj;
14504
14505 mutex_lock(&dev->struct_mutex);
74b4ea1e 14506 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
86c98588
RV
14507 mutex_unlock(&dev->struct_mutex);
14508
14509 return 0;
14510}
14511
79e53945
JB
14512static const struct drm_framebuffer_funcs intel_fb_funcs = {
14513 .destroy = intel_user_framebuffer_destroy,
14514 .create_handle = intel_user_framebuffer_create_handle,
86c98588 14515 .dirty = intel_user_framebuffer_dirty,
79e53945
JB
14516};
14517
b321803d
DL
14518static
14519u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14520 uint32_t pixel_format)
14521{
14522 u32 gen = INTEL_INFO(dev)->gen;
14523
14524 if (gen >= 9) {
ac484963
VS
14525 int cpp = drm_format_plane_cpp(pixel_format, 0);
14526
b321803d
DL
14527 /* "The stride in bytes must not exceed the of the size of 8K
14528 * pixels and 32K bytes."
14529 */
ac484963 14530 return min(8192 * cpp, 32768);
666a4537 14531 } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
b321803d
DL
14532 return 32*1024;
14533 } else if (gen >= 4) {
14534 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14535 return 16*1024;
14536 else
14537 return 32*1024;
14538 } else if (gen >= 3) {
14539 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14540 return 8*1024;
14541 else
14542 return 16*1024;
14543 } else {
14544 /* XXX DSPC is limited to 4k tiled */
14545 return 8*1024;
14546 }
14547}
14548
b5ea642a
DV
14549static int intel_framebuffer_init(struct drm_device *dev,
14550 struct intel_framebuffer *intel_fb,
14551 struct drm_mode_fb_cmd2 *mode_cmd,
14552 struct drm_i915_gem_object *obj)
79e53945 14553{
7b49f948 14554 struct drm_i915_private *dev_priv = to_i915(dev);
6761dd31 14555 unsigned int aligned_height;
79e53945 14556 int ret;
b321803d 14557 u32 pitch_limit, stride_alignment;
79e53945 14558
dd4916c5
DV
14559 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14560
2a80eada
DV
14561 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14562 /* Enforce that fb modifier and tiling mode match, but only for
14563 * X-tiled. This is needed for FBC. */
14564 if (!!(obj->tiling_mode == I915_TILING_X) !=
14565 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14566 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14567 return -EINVAL;
14568 }
14569 } else {
14570 if (obj->tiling_mode == I915_TILING_X)
14571 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14572 else if (obj->tiling_mode == I915_TILING_Y) {
14573 DRM_DEBUG("No Y tiling for legacy addfb\n");
14574 return -EINVAL;
14575 }
14576 }
14577
9a8f0a12
TU
14578 /* Passed in modifier sanity checking. */
14579 switch (mode_cmd->modifier[0]) {
14580 case I915_FORMAT_MOD_Y_TILED:
14581 case I915_FORMAT_MOD_Yf_TILED:
14582 if (INTEL_INFO(dev)->gen < 9) {
14583 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14584 mode_cmd->modifier[0]);
14585 return -EINVAL;
14586 }
14587 case DRM_FORMAT_MOD_NONE:
14588 case I915_FORMAT_MOD_X_TILED:
14589 break;
14590 default:
c0f40428
JB
14591 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14592 mode_cmd->modifier[0]);
57cd6508 14593 return -EINVAL;
c16ed4be 14594 }
57cd6508 14595
7b49f948
VS
14596 stride_alignment = intel_fb_stride_alignment(dev_priv,
14597 mode_cmd->modifier[0],
b321803d
DL
14598 mode_cmd->pixel_format);
14599 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14600 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14601 mode_cmd->pitches[0], stride_alignment);
57cd6508 14602 return -EINVAL;
c16ed4be 14603 }
57cd6508 14604
b321803d
DL
14605 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14606 mode_cmd->pixel_format);
a35cdaa0 14607 if (mode_cmd->pitches[0] > pitch_limit) {
b321803d
DL
14608 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14609 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
2a80eada 14610 "tiled" : "linear",
a35cdaa0 14611 mode_cmd->pitches[0], pitch_limit);
5d7bd705 14612 return -EINVAL;
c16ed4be 14613 }
5d7bd705 14614
2a80eada 14615 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
c16ed4be
CW
14616 mode_cmd->pitches[0] != obj->stride) {
14617 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14618 mode_cmd->pitches[0], obj->stride);
5d7bd705 14619 return -EINVAL;
c16ed4be 14620 }
5d7bd705 14621
57779d06 14622 /* Reject formats not supported by any plane early. */
308e5bcb 14623 switch (mode_cmd->pixel_format) {
57779d06 14624 case DRM_FORMAT_C8:
04b3924d
VS
14625 case DRM_FORMAT_RGB565:
14626 case DRM_FORMAT_XRGB8888:
14627 case DRM_FORMAT_ARGB8888:
57779d06
VS
14628 break;
14629 case DRM_FORMAT_XRGB1555:
c16ed4be 14630 if (INTEL_INFO(dev)->gen > 3) {
4ee62c76
VS
14631 DRM_DEBUG("unsupported pixel format: %s\n",
14632 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14633 return -EINVAL;
c16ed4be 14634 }
57779d06 14635 break;
57779d06 14636 case DRM_FORMAT_ABGR8888:
666a4537
WB
14637 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
14638 INTEL_INFO(dev)->gen < 9) {
6c0fd451
DL
14639 DRM_DEBUG("unsupported pixel format: %s\n",
14640 drm_get_format_name(mode_cmd->pixel_format));
14641 return -EINVAL;
14642 }
14643 break;
14644 case DRM_FORMAT_XBGR8888:
04b3924d 14645 case DRM_FORMAT_XRGB2101010:
57779d06 14646 case DRM_FORMAT_XBGR2101010:
c16ed4be 14647 if (INTEL_INFO(dev)->gen < 4) {
4ee62c76
VS
14648 DRM_DEBUG("unsupported pixel format: %s\n",
14649 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14650 return -EINVAL;
c16ed4be 14651 }
b5626747 14652 break;
7531208b 14653 case DRM_FORMAT_ABGR2101010:
666a4537 14654 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
7531208b
DL
14655 DRM_DEBUG("unsupported pixel format: %s\n",
14656 drm_get_format_name(mode_cmd->pixel_format));
14657 return -EINVAL;
14658 }
14659 break;
04b3924d
VS
14660 case DRM_FORMAT_YUYV:
14661 case DRM_FORMAT_UYVY:
14662 case DRM_FORMAT_YVYU:
14663 case DRM_FORMAT_VYUY:
c16ed4be 14664 if (INTEL_INFO(dev)->gen < 5) {
4ee62c76
VS
14665 DRM_DEBUG("unsupported pixel format: %s\n",
14666 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14667 return -EINVAL;
c16ed4be 14668 }
57cd6508
CW
14669 break;
14670 default:
4ee62c76
VS
14671 DRM_DEBUG("unsupported pixel format: %s\n",
14672 drm_get_format_name(mode_cmd->pixel_format));
57cd6508
CW
14673 return -EINVAL;
14674 }
14675
90f9a336
VS
14676 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14677 if (mode_cmd->offsets[0] != 0)
14678 return -EINVAL;
14679
ec2c981e 14680 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
091df6cb
DV
14681 mode_cmd->pixel_format,
14682 mode_cmd->modifier[0]);
53155c0a
DV
14683 /* FIXME drm helper for size checks (especially planar formats)? */
14684 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14685 return -EINVAL;
14686
c7d73f6a
DV
14687 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14688 intel_fb->obj = obj;
14689
2d7a215f
VS
14690 intel_fill_fb_info(dev_priv, &intel_fb->base);
14691
79e53945
JB
14692 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14693 if (ret) {
14694 DRM_ERROR("framebuffer init failed %d\n", ret);
14695 return ret;
14696 }
14697
0b05e1e0
VS
14698 intel_fb->obj->framebuffer_references++;
14699
79e53945
JB
14700 return 0;
14701}
14702
79e53945
JB
14703static struct drm_framebuffer *
14704intel_user_framebuffer_create(struct drm_device *dev,
14705 struct drm_file *filp,
1eb83451 14706 const struct drm_mode_fb_cmd2 *user_mode_cmd)
79e53945 14707{
dcb1394e 14708 struct drm_framebuffer *fb;
05394f39 14709 struct drm_i915_gem_object *obj;
76dc3769 14710 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
79e53945 14711
308e5bcb 14712 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
76dc3769 14713 mode_cmd.handles[0]));
c8725226 14714 if (&obj->base == NULL)
cce13ff7 14715 return ERR_PTR(-ENOENT);
79e53945 14716
92907cbb 14717 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
dcb1394e
LW
14718 if (IS_ERR(fb))
14719 drm_gem_object_unreference_unlocked(&obj->base);
14720
14721 return fb;
79e53945
JB
14722}
14723
0695726e 14724#ifndef CONFIG_DRM_FBDEV_EMULATION
0632fef6 14725static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
4520f53a
DV
14726{
14727}
14728#endif
14729
79e53945 14730static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 14731 .fb_create = intel_user_framebuffer_create,
0632fef6 14732 .output_poll_changed = intel_fbdev_output_poll_changed,
5ee67f1c
MR
14733 .atomic_check = intel_atomic_check,
14734 .atomic_commit = intel_atomic_commit,
de419ab6
ML
14735 .atomic_state_alloc = intel_atomic_state_alloc,
14736 .atomic_state_clear = intel_atomic_state_clear,
79e53945
JB
14737};
14738
88212941
ID
14739/**
14740 * intel_init_display_hooks - initialize the display modesetting hooks
14741 * @dev_priv: device private
14742 */
14743void intel_init_display_hooks(struct drm_i915_private *dev_priv)
e70236a8 14744{
88212941 14745 if (HAS_PCH_SPLIT(dev_priv) || IS_G4X(dev_priv))
ee9300bb 14746 dev_priv->display.find_dpll = g4x_find_best_dpll;
88212941 14747 else if (IS_CHERRYVIEW(dev_priv))
ef9348c8 14748 dev_priv->display.find_dpll = chv_find_best_dpll;
88212941 14749 else if (IS_VALLEYVIEW(dev_priv))
ee9300bb 14750 dev_priv->display.find_dpll = vlv_find_best_dpll;
88212941 14751 else if (IS_PINEVIEW(dev_priv))
ee9300bb
DV
14752 dev_priv->display.find_dpll = pnv_find_best_dpll;
14753 else
14754 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14755
88212941 14756 if (INTEL_INFO(dev_priv)->gen >= 9) {
bc8d7dff 14757 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14758 dev_priv->display.get_initial_plane_config =
14759 skylake_get_initial_plane_config;
bc8d7dff
DL
14760 dev_priv->display.crtc_compute_clock =
14761 haswell_crtc_compute_clock;
14762 dev_priv->display.crtc_enable = haswell_crtc_enable;
14763 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 14764 } else if (HAS_DDI(dev_priv)) {
0e8ffe1b 14765 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14766 dev_priv->display.get_initial_plane_config =
14767 ironlake_get_initial_plane_config;
797d0259
ACO
14768 dev_priv->display.crtc_compute_clock =
14769 haswell_crtc_compute_clock;
4f771f10
PZ
14770 dev_priv->display.crtc_enable = haswell_crtc_enable;
14771 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 14772 } else if (HAS_PCH_SPLIT(dev_priv)) {
0e8ffe1b 14773 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
5724dbd1
DL
14774 dev_priv->display.get_initial_plane_config =
14775 ironlake_get_initial_plane_config;
3fb37703
ACO
14776 dev_priv->display.crtc_compute_clock =
14777 ironlake_crtc_compute_clock;
76e5a89c
DV
14778 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14779 dev_priv->display.crtc_disable = ironlake_crtc_disable;
88212941 14780 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
89b667f8 14781 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14782 dev_priv->display.get_initial_plane_config =
14783 i9xx_get_initial_plane_config;
d6dfee7a 14784 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
89b667f8
JB
14785 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14786 dev_priv->display.crtc_disable = i9xx_crtc_disable;
f564048e 14787 } else {
0e8ffe1b 14788 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14789 dev_priv->display.get_initial_plane_config =
14790 i9xx_get_initial_plane_config;
d6dfee7a 14791 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
76e5a89c
DV
14792 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14793 dev_priv->display.crtc_disable = i9xx_crtc_disable;
f564048e 14794 }
e70236a8 14795
e70236a8 14796 /* Returns the core display clock speed */
88212941 14797 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
1652d19e
VS
14798 dev_priv->display.get_display_clock_speed =
14799 skylake_get_display_clock_speed;
88212941 14800 else if (IS_BROXTON(dev_priv))
acd3f3d3
BP
14801 dev_priv->display.get_display_clock_speed =
14802 broxton_get_display_clock_speed;
88212941 14803 else if (IS_BROADWELL(dev_priv))
1652d19e
VS
14804 dev_priv->display.get_display_clock_speed =
14805 broadwell_get_display_clock_speed;
88212941 14806 else if (IS_HASWELL(dev_priv))
1652d19e
VS
14807 dev_priv->display.get_display_clock_speed =
14808 haswell_get_display_clock_speed;
88212941 14809 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
25eb05fc
JB
14810 dev_priv->display.get_display_clock_speed =
14811 valleyview_get_display_clock_speed;
88212941 14812 else if (IS_GEN5(dev_priv))
b37a6434
VS
14813 dev_priv->display.get_display_clock_speed =
14814 ilk_get_display_clock_speed;
88212941
ID
14815 else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
14816 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
e70236a8
JB
14817 dev_priv->display.get_display_clock_speed =
14818 i945_get_display_clock_speed;
88212941 14819 else if (IS_GM45(dev_priv))
34edce2f
VS
14820 dev_priv->display.get_display_clock_speed =
14821 gm45_get_display_clock_speed;
88212941 14822 else if (IS_CRESTLINE(dev_priv))
34edce2f
VS
14823 dev_priv->display.get_display_clock_speed =
14824 i965gm_get_display_clock_speed;
88212941 14825 else if (IS_PINEVIEW(dev_priv))
34edce2f
VS
14826 dev_priv->display.get_display_clock_speed =
14827 pnv_get_display_clock_speed;
88212941 14828 else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
34edce2f
VS
14829 dev_priv->display.get_display_clock_speed =
14830 g33_get_display_clock_speed;
88212941 14831 else if (IS_I915G(dev_priv))
e70236a8
JB
14832 dev_priv->display.get_display_clock_speed =
14833 i915_get_display_clock_speed;
88212941 14834 else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
e70236a8
JB
14835 dev_priv->display.get_display_clock_speed =
14836 i9xx_misc_get_display_clock_speed;
88212941 14837 else if (IS_I915GM(dev_priv))
e70236a8
JB
14838 dev_priv->display.get_display_clock_speed =
14839 i915gm_get_display_clock_speed;
88212941 14840 else if (IS_I865G(dev_priv))
e70236a8
JB
14841 dev_priv->display.get_display_clock_speed =
14842 i865_get_display_clock_speed;
88212941 14843 else if (IS_I85X(dev_priv))
e70236a8 14844 dev_priv->display.get_display_clock_speed =
1b1d2716 14845 i85x_get_display_clock_speed;
623e01e5 14846 else { /* 830 */
88212941 14847 WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
e70236a8
JB
14848 dev_priv->display.get_display_clock_speed =
14849 i830_get_display_clock_speed;
623e01e5 14850 }
e70236a8 14851
88212941 14852 if (IS_GEN5(dev_priv)) {
3bb11b53 14853 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
88212941 14854 } else if (IS_GEN6(dev_priv)) {
3bb11b53 14855 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
88212941 14856 } else if (IS_IVYBRIDGE(dev_priv)) {
3bb11b53
SJ
14857 /* FIXME: detect B0+ stepping and use auto training */
14858 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
88212941 14859 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
3bb11b53 14860 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
88212941 14861 if (IS_BROADWELL(dev_priv)) {
27c329ed
ML
14862 dev_priv->display.modeset_commit_cdclk =
14863 broadwell_modeset_commit_cdclk;
14864 dev_priv->display.modeset_calc_cdclk =
14865 broadwell_modeset_calc_cdclk;
14866 }
88212941 14867 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
27c329ed
ML
14868 dev_priv->display.modeset_commit_cdclk =
14869 valleyview_modeset_commit_cdclk;
14870 dev_priv->display.modeset_calc_cdclk =
14871 valleyview_modeset_calc_cdclk;
88212941 14872 } else if (IS_BROXTON(dev_priv)) {
27c329ed
ML
14873 dev_priv->display.modeset_commit_cdclk =
14874 broxton_modeset_commit_cdclk;
14875 dev_priv->display.modeset_calc_cdclk =
14876 broxton_modeset_calc_cdclk;
e70236a8 14877 }
8c9f3aaf 14878
88212941 14879 switch (INTEL_INFO(dev_priv)->gen) {
8c9f3aaf
JB
14880 case 2:
14881 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14882 break;
14883
14884 case 3:
14885 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14886 break;
14887
14888 case 4:
14889 case 5:
14890 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14891 break;
14892
14893 case 6:
14894 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14895 break;
7c9017e5 14896 case 7:
4e0bbc31 14897 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
7c9017e5
JB
14898 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14899 break;
830c81db 14900 case 9:
ba343e02
TU
14901 /* Drop through - unsupported since execlist only. */
14902 default:
14903 /* Default just returns -ENODEV to indicate unsupported */
14904 dev_priv->display.queue_flip = intel_default_queue_flip;
8c9f3aaf 14905 }
e70236a8
JB
14906}
14907
b690e96c
JB
14908/*
14909 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14910 * resume, or other times. This quirk makes sure that's the case for
14911 * affected systems.
14912 */
0206e353 14913static void quirk_pipea_force(struct drm_device *dev)
b690e96c
JB
14914{
14915 struct drm_i915_private *dev_priv = dev->dev_private;
14916
14917 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
bc0daf48 14918 DRM_INFO("applying pipe a force quirk\n");
b690e96c
JB
14919}
14920
b6b5d049
VS
14921static void quirk_pipeb_force(struct drm_device *dev)
14922{
14923 struct drm_i915_private *dev_priv = dev->dev_private;
14924
14925 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14926 DRM_INFO("applying pipe b force quirk\n");
14927}
14928
435793df
KP
14929/*
14930 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14931 */
14932static void quirk_ssc_force_disable(struct drm_device *dev)
14933{
14934 struct drm_i915_private *dev_priv = dev->dev_private;
14935 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 14936 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
14937}
14938
4dca20ef 14939/*
5a15ab5b
CE
14940 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14941 * brightness value
4dca20ef
CE
14942 */
14943static void quirk_invert_brightness(struct drm_device *dev)
14944{
14945 struct drm_i915_private *dev_priv = dev->dev_private;
14946 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 14947 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
14948}
14949
9c72cc6f
SD
14950/* Some VBT's incorrectly indicate no backlight is present */
14951static void quirk_backlight_present(struct drm_device *dev)
14952{
14953 struct drm_i915_private *dev_priv = dev->dev_private;
14954 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14955 DRM_INFO("applying backlight present quirk\n");
14956}
14957
b690e96c
JB
14958struct intel_quirk {
14959 int device;
14960 int subsystem_vendor;
14961 int subsystem_device;
14962 void (*hook)(struct drm_device *dev);
14963};
14964
5f85f176
EE
14965/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14966struct intel_dmi_quirk {
14967 void (*hook)(struct drm_device *dev);
14968 const struct dmi_system_id (*dmi_id_list)[];
14969};
14970
14971static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14972{
14973 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14974 return 1;
14975}
14976
14977static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14978 {
14979 .dmi_id_list = &(const struct dmi_system_id[]) {
14980 {
14981 .callback = intel_dmi_reverse_brightness,
14982 .ident = "NCR Corporation",
14983 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14984 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14985 },
14986 },
14987 { } /* terminating entry */
14988 },
14989 .hook = quirk_invert_brightness,
14990 },
14991};
14992
c43b5634 14993static struct intel_quirk intel_quirks[] = {
b690e96c
JB
14994 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
14995 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
14996
b690e96c
JB
14997 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
14998 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
14999
5f080c0f
VS
15000 /* 830 needs to leave pipe A & dpll A up */
15001 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
15002
b6b5d049
VS
15003 /* 830 needs to leave pipe B & dpll B up */
15004 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
15005
435793df
KP
15006 /* Lenovo U160 cannot use SSC on LVDS */
15007 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
15008
15009 /* Sony Vaio Y cannot use SSC on LVDS */
15010 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 15011
be505f64
AH
15012 /* Acer Aspire 5734Z must invert backlight brightness */
15013 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
15014
15015 /* Acer/eMachines G725 */
15016 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
15017
15018 /* Acer/eMachines e725 */
15019 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
15020
15021 /* Acer/Packard Bell NCL20 */
15022 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
15023
15024 /* Acer Aspire 4736Z */
15025 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
15026
15027 /* Acer Aspire 5336 */
15028 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
2e93a1aa
SD
15029
15030 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
15031 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
d4967d8c 15032
dfb3d47b
SD
15033 /* Acer C720 Chromebook (Core i3 4005U) */
15034 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
15035
b2a9601c 15036 /* Apple Macbook 2,1 (Core 2 T7400) */
15037 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
15038
1b9448b0
JN
15039 /* Apple Macbook 4,1 */
15040 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
15041
d4967d8c
SD
15042 /* Toshiba CB35 Chromebook (Celeron 2955U) */
15043 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
724cb06f
SD
15044
15045 /* HP Chromebook 14 (Celeron 2955U) */
15046 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
cf6f0af9
JN
15047
15048 /* Dell Chromebook 11 */
15049 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
9be64eee
JN
15050
15051 /* Dell Chromebook 11 (2015 version) */
15052 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
b690e96c
JB
15053};
15054
15055static void intel_init_quirks(struct drm_device *dev)
15056{
15057 struct pci_dev *d = dev->pdev;
15058 int i;
15059
15060 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
15061 struct intel_quirk *q = &intel_quirks[i];
15062
15063 if (d->device == q->device &&
15064 (d->subsystem_vendor == q->subsystem_vendor ||
15065 q->subsystem_vendor == PCI_ANY_ID) &&
15066 (d->subsystem_device == q->subsystem_device ||
15067 q->subsystem_device == PCI_ANY_ID))
15068 q->hook(dev);
15069 }
5f85f176
EE
15070 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
15071 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
15072 intel_dmi_quirks[i].hook(dev);
15073 }
b690e96c
JB
15074}
15075
9cce37f4
JB
15076/* Disable the VGA plane that we never use */
15077static void i915_disable_vga(struct drm_device *dev)
15078{
15079 struct drm_i915_private *dev_priv = dev->dev_private;
15080 u8 sr1;
f0f59a00 15081 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
9cce37f4 15082
2b37c616 15083 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
9cce37f4 15084 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 15085 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
15086 sr1 = inb(VGA_SR_DATA);
15087 outb(sr1 | 1<<5, VGA_SR_DATA);
15088 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
15089 udelay(300);
15090
01f5a626 15091 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9cce37f4
JB
15092 POSTING_READ(vga_reg);
15093}
15094
f817586c
DV
15095void intel_modeset_init_hw(struct drm_device *dev)
15096{
1a617b77
ML
15097 struct drm_i915_private *dev_priv = dev->dev_private;
15098
b6283055 15099 intel_update_cdclk(dev);
1a617b77
ML
15100
15101 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
15102
f817586c 15103 intel_init_clock_gating(dev);
8090c6b9 15104 intel_enable_gt_powersave(dev);
f817586c
DV
15105}
15106
d93c0372
MR
15107/*
15108 * Calculate what we think the watermarks should be for the state we've read
15109 * out of the hardware and then immediately program those watermarks so that
15110 * we ensure the hardware settings match our internal state.
15111 *
15112 * We can calculate what we think WM's should be by creating a duplicate of the
15113 * current state (which was constructed during hardware readout) and running it
15114 * through the atomic check code to calculate new watermark values in the
15115 * state object.
15116 */
15117static void sanitize_watermarks(struct drm_device *dev)
15118{
15119 struct drm_i915_private *dev_priv = to_i915(dev);
15120 struct drm_atomic_state *state;
15121 struct drm_crtc *crtc;
15122 struct drm_crtc_state *cstate;
15123 struct drm_modeset_acquire_ctx ctx;
15124 int ret;
15125 int i;
15126
15127 /* Only supported on platforms that use atomic watermark design */
ed4a6a7c 15128 if (!dev_priv->display.optimize_watermarks)
d93c0372
MR
15129 return;
15130
15131 /*
15132 * We need to hold connection_mutex before calling duplicate_state so
15133 * that the connector loop is protected.
15134 */
15135 drm_modeset_acquire_init(&ctx, 0);
15136retry:
0cd1262d 15137 ret = drm_modeset_lock_all_ctx(dev, &ctx);
d93c0372
MR
15138 if (ret == -EDEADLK) {
15139 drm_modeset_backoff(&ctx);
15140 goto retry;
15141 } else if (WARN_ON(ret)) {
0cd1262d 15142 goto fail;
d93c0372
MR
15143 }
15144
15145 state = drm_atomic_helper_duplicate_state(dev, &ctx);
15146 if (WARN_ON(IS_ERR(state)))
0cd1262d 15147 goto fail;
d93c0372 15148
ed4a6a7c
MR
15149 /*
15150 * Hardware readout is the only time we don't want to calculate
15151 * intermediate watermarks (since we don't trust the current
15152 * watermarks).
15153 */
15154 to_intel_atomic_state(state)->skip_intermediate_wm = true;
15155
d93c0372
MR
15156 ret = intel_atomic_check(dev, state);
15157 if (ret) {
15158 /*
15159 * If we fail here, it means that the hardware appears to be
15160 * programmed in a way that shouldn't be possible, given our
15161 * understanding of watermark requirements. This might mean a
15162 * mistake in the hardware readout code or a mistake in the
15163 * watermark calculations for a given platform. Raise a WARN
15164 * so that this is noticeable.
15165 *
15166 * If this actually happens, we'll have to just leave the
15167 * BIOS-programmed watermarks untouched and hope for the best.
15168 */
15169 WARN(true, "Could not determine valid watermarks for inherited state\n");
0cd1262d 15170 goto fail;
d93c0372
MR
15171 }
15172
15173 /* Write calculated watermark values back */
15174 to_i915(dev)->wm.config = to_intel_atomic_state(state)->wm_config;
15175 for_each_crtc_in_state(state, crtc, cstate, i) {
15176 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
15177
ed4a6a7c
MR
15178 cs->wm.need_postvbl_update = true;
15179 dev_priv->display.optimize_watermarks(cs);
d93c0372
MR
15180 }
15181
15182 drm_atomic_state_free(state);
0cd1262d 15183fail:
d93c0372
MR
15184 drm_modeset_drop_locks(&ctx);
15185 drm_modeset_acquire_fini(&ctx);
15186}
15187
79e53945
JB
15188void intel_modeset_init(struct drm_device *dev)
15189{
652c393a 15190 struct drm_i915_private *dev_priv = dev->dev_private;
1fe47785 15191 int sprite, ret;
8cc87b75 15192 enum pipe pipe;
46f297fb 15193 struct intel_crtc *crtc;
79e53945
JB
15194
15195 drm_mode_config_init(dev);
15196
15197 dev->mode_config.min_width = 0;
15198 dev->mode_config.min_height = 0;
15199
019d96cb
DA
15200 dev->mode_config.preferred_depth = 24;
15201 dev->mode_config.prefer_shadow = 1;
15202
25bab385
TU
15203 dev->mode_config.allow_fb_modifiers = true;
15204
e6ecefaa 15205 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 15206
b690e96c
JB
15207 intel_init_quirks(dev);
15208
1fa61106
ED
15209 intel_init_pm(dev);
15210
e3c74757
BW
15211 if (INTEL_INFO(dev)->num_pipes == 0)
15212 return;
15213
69f92f67
LW
15214 /*
15215 * There may be no VBT; and if the BIOS enabled SSC we can
15216 * just keep using it to avoid unnecessary flicker. Whereas if the
15217 * BIOS isn't using it, don't assume it will work even if the VBT
15218 * indicates as much.
15219 */
15220 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
15221 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15222 DREF_SSC1_ENABLE);
15223
15224 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
15225 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
15226 bios_lvds_use_ssc ? "en" : "dis",
15227 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
15228 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
15229 }
15230 }
15231
a6c45cf0
CW
15232 if (IS_GEN2(dev)) {
15233 dev->mode_config.max_width = 2048;
15234 dev->mode_config.max_height = 2048;
15235 } else if (IS_GEN3(dev)) {
5e4d6fa7
KP
15236 dev->mode_config.max_width = 4096;
15237 dev->mode_config.max_height = 4096;
79e53945 15238 } else {
a6c45cf0
CW
15239 dev->mode_config.max_width = 8192;
15240 dev->mode_config.max_height = 8192;
79e53945 15241 }
068be561 15242
dc41c154
VS
15243 if (IS_845G(dev) || IS_I865G(dev)) {
15244 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15245 dev->mode_config.cursor_height = 1023;
15246 } else if (IS_GEN2(dev)) {
068be561
DL
15247 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15248 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15249 } else {
15250 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15251 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15252 }
15253
62106b4f 15254 dev->mode_config.fb_base = dev_priv->ggtt.mappable_base;
79e53945 15255
28c97730 15256 DRM_DEBUG_KMS("%d display pipe%s available.\n",
7eb552ae
BW
15257 INTEL_INFO(dev)->num_pipes,
15258 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
79e53945 15259
055e393f 15260 for_each_pipe(dev_priv, pipe) {
8cc87b75 15261 intel_crtc_init(dev, pipe);
3bdcfc0c 15262 for_each_sprite(dev_priv, pipe, sprite) {
1fe47785 15263 ret = intel_plane_init(dev, pipe, sprite);
7f1f3851 15264 if (ret)
06da8da2 15265 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
1fe47785 15266 pipe_name(pipe), sprite_name(pipe, sprite), ret);
7f1f3851 15267 }
79e53945
JB
15268 }
15269
bfa7df01 15270 intel_update_czclk(dev_priv);
e7dc33f3 15271 intel_update_rawclk(dev_priv);
bfa7df01
VS
15272 intel_update_cdclk(dev);
15273
e72f9fbf 15274 intel_shared_dpll_init(dev);
ee7b9f93 15275
9cce37f4
JB
15276 /* Just disable it once at startup */
15277 i915_disable_vga(dev);
79e53945 15278 intel_setup_outputs(dev);
11be49eb 15279
6e9f798d 15280 drm_modeset_lock_all(dev);
043e9bda 15281 intel_modeset_setup_hw_state(dev);
6e9f798d 15282 drm_modeset_unlock_all(dev);
46f297fb 15283
d3fcc808 15284 for_each_intel_crtc(dev, crtc) {
eeebeac5
ML
15285 struct intel_initial_plane_config plane_config = {};
15286
46f297fb
JB
15287 if (!crtc->active)
15288 continue;
15289
46f297fb 15290 /*
46f297fb
JB
15291 * Note that reserving the BIOS fb up front prevents us
15292 * from stuffing other stolen allocations like the ring
15293 * on top. This prevents some ugliness at boot time, and
15294 * can even allow for smooth boot transitions if the BIOS
15295 * fb is large enough for the active pipe configuration.
15296 */
eeebeac5
ML
15297 dev_priv->display.get_initial_plane_config(crtc,
15298 &plane_config);
15299
15300 /*
15301 * If the fb is shared between multiple heads, we'll
15302 * just get the first one.
15303 */
15304 intel_find_initial_plane_obj(crtc, &plane_config);
46f297fb 15305 }
d93c0372
MR
15306
15307 /*
15308 * Make sure hardware watermarks really match the state we read out.
15309 * Note that we need to do this after reconstructing the BIOS fb's
15310 * since the watermark calculation done here will use pstate->fb.
15311 */
15312 sanitize_watermarks(dev);
2c7111db
CW
15313}
15314
7fad798e
DV
15315static void intel_enable_pipe_a(struct drm_device *dev)
15316{
15317 struct intel_connector *connector;
15318 struct drm_connector *crt = NULL;
15319 struct intel_load_detect_pipe load_detect_temp;
208bf9fd 15320 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
7fad798e
DV
15321
15322 /* We can't just switch on the pipe A, we need to set things up with a
15323 * proper mode and output configuration. As a gross hack, enable pipe A
15324 * by enabling the load detect pipe once. */
3a3371ff 15325 for_each_intel_connector(dev, connector) {
7fad798e
DV
15326 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15327 crt = &connector->base;
15328 break;
15329 }
15330 }
15331
15332 if (!crt)
15333 return;
15334
208bf9fd 15335 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
49172fee 15336 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
7fad798e
DV
15337}
15338
fa555837
DV
15339static bool
15340intel_check_plane_mapping(struct intel_crtc *crtc)
15341{
7eb552ae
BW
15342 struct drm_device *dev = crtc->base.dev;
15343 struct drm_i915_private *dev_priv = dev->dev_private;
649636ef 15344 u32 val;
fa555837 15345
7eb552ae 15346 if (INTEL_INFO(dev)->num_pipes == 1)
fa555837
DV
15347 return true;
15348
649636ef 15349 val = I915_READ(DSPCNTR(!crtc->plane));
fa555837
DV
15350
15351 if ((val & DISPLAY_PLANE_ENABLE) &&
15352 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15353 return false;
15354
15355 return true;
15356}
15357
02e93c35
VS
15358static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15359{
15360 struct drm_device *dev = crtc->base.dev;
15361 struct intel_encoder *encoder;
15362
15363 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15364 return true;
15365
15366 return false;
15367}
15368
dd756198
VS
15369static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
15370{
15371 struct drm_device *dev = encoder->base.dev;
15372 struct intel_connector *connector;
15373
15374 for_each_connector_on_encoder(dev, &encoder->base, connector)
15375 return true;
15376
15377 return false;
15378}
15379
24929352
DV
15380static void intel_sanitize_crtc(struct intel_crtc *crtc)
15381{
15382 struct drm_device *dev = crtc->base.dev;
15383 struct drm_i915_private *dev_priv = dev->dev_private;
4d1de975 15384 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
24929352 15385
24929352 15386 /* Clear any frame start delays used for debugging left by the BIOS */
4d1de975
JN
15387 if (!transcoder_is_dsi(cpu_transcoder)) {
15388 i915_reg_t reg = PIPECONF(cpu_transcoder);
15389
15390 I915_WRITE(reg,
15391 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15392 }
24929352 15393
d3eaf884 15394 /* restore vblank interrupts to correct state */
9625604c 15395 drm_crtc_vblank_reset(&crtc->base);
d297e103 15396 if (crtc->active) {
f9cd7b88
VS
15397 struct intel_plane *plane;
15398
9625604c 15399 drm_crtc_vblank_on(&crtc->base);
f9cd7b88
VS
15400
15401 /* Disable everything but the primary plane */
15402 for_each_intel_plane_on_crtc(dev, crtc, plane) {
15403 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
15404 continue;
15405
15406 plane->disable_plane(&plane->base, &crtc->base);
15407 }
9625604c 15408 }
d3eaf884 15409
24929352 15410 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
DV
15411 * disable the crtc (and hence change the state) if it is wrong. Note
15412 * that gen4+ has a fixed plane -> pipe mapping. */
15413 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
24929352
DV
15414 bool plane;
15415
24929352
DV
15416 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
15417 crtc->base.base.id);
15418
15419 /* Pipe has the wrong plane attached and the plane is active.
15420 * Temporarily change the plane mapping and disable everything
15421 * ... */
15422 plane = crtc->plane;
b70709a6 15423 to_intel_plane_state(crtc->base.primary->state)->visible = true;
24929352 15424 crtc->plane = !plane;
b17d48e2 15425 intel_crtc_disable_noatomic(&crtc->base);
24929352 15426 crtc->plane = plane;
24929352 15427 }
24929352 15428
7fad798e
DV
15429 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15430 crtc->pipe == PIPE_A && !crtc->active) {
15431 /* BIOS forgot to enable pipe A, this mostly happens after
15432 * resume. Force-enable the pipe to fix this, the update_dpms
15433 * call below we restore the pipe to the right state, but leave
15434 * the required bits on. */
15435 intel_enable_pipe_a(dev);
15436 }
15437
24929352
DV
15438 /* Adjust the state of the output pipe according to whether we
15439 * have active connectors/encoders. */
842e0307 15440 if (crtc->active && !intel_crtc_has_encoders(crtc))
b17d48e2 15441 intel_crtc_disable_noatomic(&crtc->base);
24929352 15442
a3ed6aad 15443 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
4cc31489
DV
15444 /*
15445 * We start out with underrun reporting disabled to avoid races.
15446 * For correct bookkeeping mark this on active crtcs.
15447 *
c5ab3bc0
DV
15448 * Also on gmch platforms we dont have any hardware bits to
15449 * disable the underrun reporting. Which means we need to start
15450 * out with underrun reporting disabled also on inactive pipes,
15451 * since otherwise we'll complain about the garbage we read when
15452 * e.g. coming up after runtime pm.
15453 *
4cc31489
DV
15454 * No protection against concurrent access is required - at
15455 * worst a fifo underrun happens which also sets this to false.
15456 */
15457 crtc->cpu_fifo_underrun_disabled = true;
15458 crtc->pch_fifo_underrun_disabled = true;
15459 }
24929352
DV
15460}
15461
15462static void intel_sanitize_encoder(struct intel_encoder *encoder)
15463{
15464 struct intel_connector *connector;
15465 struct drm_device *dev = encoder->base.dev;
15466
15467 /* We need to check both for a crtc link (meaning that the
15468 * encoder is active and trying to read from a pipe) and the
15469 * pipe itself being active. */
15470 bool has_active_crtc = encoder->base.crtc &&
15471 to_intel_crtc(encoder->base.crtc)->active;
15472
dd756198 15473 if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
24929352
DV
15474 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15475 encoder->base.base.id,
8e329a03 15476 encoder->base.name);
24929352
DV
15477
15478 /* Connector is active, but has no active pipe. This is
15479 * fallout from our resume register restoring. Disable
15480 * the encoder manually again. */
15481 if (encoder->base.crtc) {
15482 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15483 encoder->base.base.id,
8e329a03 15484 encoder->base.name);
24929352 15485 encoder->disable(encoder);
a62d1497
VS
15486 if (encoder->post_disable)
15487 encoder->post_disable(encoder);
24929352 15488 }
7f1950fb 15489 encoder->base.crtc = NULL;
24929352
DV
15490
15491 /* Inconsistent output/port/pipe state happens presumably due to
15492 * a bug in one of the get_hw_state functions. Or someplace else
15493 * in our code, like the register restore mess on resume. Clamp
15494 * things to off as a safer default. */
3a3371ff 15495 for_each_intel_connector(dev, connector) {
24929352
DV
15496 if (connector->encoder != encoder)
15497 continue;
7f1950fb
EE
15498 connector->base.dpms = DRM_MODE_DPMS_OFF;
15499 connector->base.encoder = NULL;
24929352
DV
15500 }
15501 }
15502 /* Enabled encoders without active connectors will be fixed in
15503 * the crtc fixup. */
15504}
15505
04098753 15506void i915_redisable_vga_power_on(struct drm_device *dev)
0fde901f
KM
15507{
15508 struct drm_i915_private *dev_priv = dev->dev_private;
f0f59a00 15509 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
0fde901f 15510
04098753
ID
15511 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15512 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15513 i915_disable_vga(dev);
15514 }
15515}
15516
15517void i915_redisable_vga(struct drm_device *dev)
15518{
15519 struct drm_i915_private *dev_priv = dev->dev_private;
15520
8dc8a27c
PZ
15521 /* This function can be called both from intel_modeset_setup_hw_state or
15522 * at a very early point in our resume sequence, where the power well
15523 * structures are not yet restored. Since this function is at a very
15524 * paranoid "someone might have enabled VGA while we were not looking"
15525 * level, just check if the power well is enabled instead of trying to
15526 * follow the "don't touch the power well if we don't need it" policy
15527 * the rest of the driver uses. */
6392f847 15528 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
15529 return;
15530
04098753 15531 i915_redisable_vga_power_on(dev);
6392f847
ID
15532
15533 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
0fde901f
KM
15534}
15535
f9cd7b88 15536static bool primary_get_hw_state(struct intel_plane *plane)
98ec7739 15537{
f9cd7b88 15538 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
98ec7739 15539
f9cd7b88 15540 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
d032ffa0
ML
15541}
15542
f9cd7b88
VS
15543/* FIXME read out full plane state for all planes */
15544static void readout_plane_state(struct intel_crtc *crtc)
d032ffa0 15545{
b26d3ea3 15546 struct drm_plane *primary = crtc->base.primary;
f9cd7b88 15547 struct intel_plane_state *plane_state =
b26d3ea3 15548 to_intel_plane_state(primary->state);
d032ffa0 15549
19b8d387 15550 plane_state->visible = crtc->active &&
b26d3ea3
ML
15551 primary_get_hw_state(to_intel_plane(primary));
15552
15553 if (plane_state->visible)
15554 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
98ec7739
VS
15555}
15556
30e984df 15557static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352
DV
15558{
15559 struct drm_i915_private *dev_priv = dev->dev_private;
15560 enum pipe pipe;
24929352
DV
15561 struct intel_crtc *crtc;
15562 struct intel_encoder *encoder;
15563 struct intel_connector *connector;
5358901f 15564 int i;
24929352 15565
565602d7
ML
15566 dev_priv->active_crtcs = 0;
15567
d3fcc808 15568 for_each_intel_crtc(dev, crtc) {
565602d7
ML
15569 struct intel_crtc_state *crtc_state = crtc->config;
15570 int pixclk = 0;
3b117c8f 15571
565602d7
ML
15572 __drm_atomic_helper_crtc_destroy_state(&crtc->base, &crtc_state->base);
15573 memset(crtc_state, 0, sizeof(*crtc_state));
15574 crtc_state->base.crtc = &crtc->base;
24929352 15575
565602d7
ML
15576 crtc_state->base.active = crtc_state->base.enable =
15577 dev_priv->display.get_pipe_config(crtc, crtc_state);
15578
15579 crtc->base.enabled = crtc_state->base.enable;
15580 crtc->active = crtc_state->base.active;
15581
15582 if (crtc_state->base.active) {
15583 dev_priv->active_crtcs |= 1 << crtc->pipe;
15584
15585 if (IS_BROADWELL(dev_priv)) {
15586 pixclk = ilk_pipe_pixel_rate(crtc_state);
15587
15588 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
15589 if (crtc_state->ips_enabled)
15590 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
15591 } else if (IS_VALLEYVIEW(dev_priv) ||
15592 IS_CHERRYVIEW(dev_priv) ||
15593 IS_BROXTON(dev_priv))
15594 pixclk = crtc_state->base.adjusted_mode.crtc_clock;
15595 else
15596 WARN_ON(dev_priv->display.modeset_calc_cdclk);
15597 }
15598
15599 dev_priv->min_pixclk[crtc->pipe] = pixclk;
b70709a6 15600
f9cd7b88 15601 readout_plane_state(crtc);
24929352
DV
15602
15603 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15604 crtc->base.base.id,
15605 crtc->active ? "enabled" : "disabled");
15606 }
15607
5358901f
DV
15608 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15609 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15610
2edd6443
ACO
15611 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
15612 &pll->config.hw_state);
3e369b76 15613 pll->config.crtc_mask = 0;
d3fcc808 15614 for_each_intel_crtc(dev, crtc) {
2dd66ebd 15615 if (crtc->active && crtc->config->shared_dpll == pll)
3e369b76 15616 pll->config.crtc_mask |= 1 << crtc->pipe;
5358901f 15617 }
2dd66ebd 15618 pll->active_mask = pll->config.crtc_mask;
5358901f 15619
1e6f2ddc 15620 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
3e369b76 15621 pll->name, pll->config.crtc_mask, pll->on);
5358901f
DV
15622 }
15623
b2784e15 15624 for_each_intel_encoder(dev, encoder) {
24929352
DV
15625 pipe = 0;
15626
15627 if (encoder->get_hw_state(encoder, &pipe)) {
045ac3b5
JB
15628 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15629 encoder->base.crtc = &crtc->base;
6e3c9717 15630 encoder->get_config(encoder, crtc->config);
24929352
DV
15631 } else {
15632 encoder->base.crtc = NULL;
15633 }
15634
6f2bcceb 15635 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
24929352 15636 encoder->base.base.id,
8e329a03 15637 encoder->base.name,
24929352 15638 encoder->base.crtc ? "enabled" : "disabled",
6f2bcceb 15639 pipe_name(pipe));
24929352
DV
15640 }
15641
3a3371ff 15642 for_each_intel_connector(dev, connector) {
24929352
DV
15643 if (connector->get_hw_state(connector)) {
15644 connector->base.dpms = DRM_MODE_DPMS_ON;
2aa974c9
ML
15645
15646 encoder = connector->encoder;
15647 connector->base.encoder = &encoder->base;
15648
15649 if (encoder->base.crtc &&
15650 encoder->base.crtc->state->active) {
15651 /*
15652 * This has to be done during hardware readout
15653 * because anything calling .crtc_disable may
15654 * rely on the connector_mask being accurate.
15655 */
15656 encoder->base.crtc->state->connector_mask |=
15657 1 << drm_connector_index(&connector->base);
e87a52b3
ML
15658 encoder->base.crtc->state->encoder_mask |=
15659 1 << drm_encoder_index(&encoder->base);
2aa974c9
ML
15660 }
15661
24929352
DV
15662 } else {
15663 connector->base.dpms = DRM_MODE_DPMS_OFF;
15664 connector->base.encoder = NULL;
15665 }
15666 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15667 connector->base.base.id,
c23cc417 15668 connector->base.name,
24929352
DV
15669 connector->base.encoder ? "enabled" : "disabled");
15670 }
7f4c6284
VS
15671
15672 for_each_intel_crtc(dev, crtc) {
15673 crtc->base.hwmode = crtc->config->base.adjusted_mode;
15674
15675 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
15676 if (crtc->base.state->active) {
15677 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
15678 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
15679 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
15680
15681 /*
15682 * The initial mode needs to be set in order to keep
15683 * the atomic core happy. It wants a valid mode if the
15684 * crtc's enabled, so we do the above call.
15685 *
15686 * At this point some state updated by the connectors
15687 * in their ->detect() callback has not run yet, so
15688 * no recalculation can be done yet.
15689 *
15690 * Even if we could do a recalculation and modeset
15691 * right now it would cause a double modeset if
15692 * fbdev or userspace chooses a different initial mode.
15693 *
15694 * If that happens, someone indicated they wanted a
15695 * mode change, which means it's safe to do a full
15696 * recalculation.
15697 */
15698 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
9eca6832
VS
15699
15700 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
15701 update_scanline_offset(crtc);
7f4c6284 15702 }
e3b247da
VS
15703
15704 intel_pipe_config_sanity_check(dev_priv, crtc->config);
7f4c6284 15705 }
30e984df
DV
15706}
15707
043e9bda
ML
15708/* Scan out the current hw modeset state,
15709 * and sanitizes it to the current state
15710 */
15711static void
15712intel_modeset_setup_hw_state(struct drm_device *dev)
30e984df
DV
15713{
15714 struct drm_i915_private *dev_priv = dev->dev_private;
15715 enum pipe pipe;
30e984df
DV
15716 struct intel_crtc *crtc;
15717 struct intel_encoder *encoder;
35c95375 15718 int i;
30e984df
DV
15719
15720 intel_modeset_readout_hw_state(dev);
24929352
DV
15721
15722 /* HW state is read out, now we need to sanitize this mess. */
b2784e15 15723 for_each_intel_encoder(dev, encoder) {
24929352
DV
15724 intel_sanitize_encoder(encoder);
15725 }
15726
055e393f 15727 for_each_pipe(dev_priv, pipe) {
24929352
DV
15728 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15729 intel_sanitize_crtc(crtc);
6e3c9717
ACO
15730 intel_dump_pipe_config(crtc, crtc->config,
15731 "[setup_hw_state]");
24929352 15732 }
9a935856 15733
d29b2f9d
ACO
15734 intel_modeset_update_connector_atomic_state(dev);
15735
35c95375
DV
15736 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15737 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15738
2dd66ebd 15739 if (!pll->on || pll->active_mask)
35c95375
DV
15740 continue;
15741
15742 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15743
2edd6443 15744 pll->funcs.disable(dev_priv, pll);
35c95375
DV
15745 pll->on = false;
15746 }
15747
666a4537 15748 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
6eb1a681
VS
15749 vlv_wm_get_hw_state(dev);
15750 else if (IS_GEN9(dev))
3078999f
PB
15751 skl_wm_get_hw_state(dev);
15752 else if (HAS_PCH_SPLIT(dev))
243e6a44 15753 ilk_wm_get_hw_state(dev);
292b990e
ML
15754
15755 for_each_intel_crtc(dev, crtc) {
15756 unsigned long put_domains;
15757
74bff5f9 15758 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
292b990e
ML
15759 if (WARN_ON(put_domains))
15760 modeset_put_power_domains(dev_priv, put_domains);
15761 }
15762 intel_display_set_init_power(dev_priv, false);
010cf73d
PZ
15763
15764 intel_fbc_init_pipe_state(dev_priv);
043e9bda 15765}
7d0bc1ea 15766
043e9bda
ML
15767void intel_display_resume(struct drm_device *dev)
15768{
e2c8b870
ML
15769 struct drm_i915_private *dev_priv = to_i915(dev);
15770 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
15771 struct drm_modeset_acquire_ctx ctx;
043e9bda 15772 int ret;
e2c8b870 15773 bool setup = false;
f30da187 15774
e2c8b870 15775 dev_priv->modeset_restore_state = NULL;
043e9bda 15776
ea49c9ac
ML
15777 /*
15778 * This is a cludge because with real atomic modeset mode_config.mutex
15779 * won't be taken. Unfortunately some probed state like
15780 * audio_codec_enable is still protected by mode_config.mutex, so lock
15781 * it here for now.
15782 */
15783 mutex_lock(&dev->mode_config.mutex);
e2c8b870 15784 drm_modeset_acquire_init(&ctx, 0);
043e9bda 15785
e2c8b870
ML
15786retry:
15787 ret = drm_modeset_lock_all_ctx(dev, &ctx);
043e9bda 15788
e2c8b870
ML
15789 if (ret == 0 && !setup) {
15790 setup = true;
043e9bda 15791
e2c8b870
ML
15792 intel_modeset_setup_hw_state(dev);
15793 i915_redisable_vga(dev);
45e2b5f6 15794 }
8af6cf88 15795
e2c8b870
ML
15796 if (ret == 0 && state) {
15797 struct drm_crtc_state *crtc_state;
15798 struct drm_crtc *crtc;
15799 int i;
043e9bda 15800
e2c8b870
ML
15801 state->acquire_ctx = &ctx;
15802
15803 for_each_crtc_in_state(state, crtc, crtc_state, i) {
15804 /*
15805 * Force recalculation even if we restore
15806 * current state. With fast modeset this may not result
15807 * in a modeset when the state is compatible.
15808 */
15809 crtc_state->mode_changed = true;
15810 }
15811
15812 ret = drm_atomic_commit(state);
043e9bda
ML
15813 }
15814
e2c8b870
ML
15815 if (ret == -EDEADLK) {
15816 drm_modeset_backoff(&ctx);
15817 goto retry;
15818 }
043e9bda 15819
e2c8b870
ML
15820 drm_modeset_drop_locks(&ctx);
15821 drm_modeset_acquire_fini(&ctx);
ea49c9ac 15822 mutex_unlock(&dev->mode_config.mutex);
043e9bda 15823
e2c8b870
ML
15824 if (ret) {
15825 DRM_ERROR("Restoring old state failed with %i\n", ret);
15826 drm_atomic_state_free(state);
15827 }
2c7111db
CW
15828}
15829
15830void intel_modeset_gem_init(struct drm_device *dev)
15831{
484b41dd 15832 struct drm_crtc *c;
2ff8fde1 15833 struct drm_i915_gem_object *obj;
e0d6149b 15834 int ret;
484b41dd 15835
ae48434c 15836 intel_init_gt_powersave(dev);
ae48434c 15837
1833b134 15838 intel_modeset_init_hw(dev);
02e792fb
DV
15839
15840 intel_setup_overlay(dev);
484b41dd
JB
15841
15842 /*
15843 * Make sure any fbs we allocated at startup are properly
15844 * pinned & fenced. When we do the allocation it's too early
15845 * for this.
15846 */
70e1e0ec 15847 for_each_crtc(dev, c) {
2ff8fde1
MR
15848 obj = intel_fb_obj(c->primary->fb);
15849 if (obj == NULL)
484b41dd
JB
15850 continue;
15851
e0d6149b 15852 mutex_lock(&dev->struct_mutex);
3465c580
VS
15853 ret = intel_pin_and_fence_fb_obj(c->primary->fb,
15854 c->primary->state->rotation);
e0d6149b
TU
15855 mutex_unlock(&dev->struct_mutex);
15856 if (ret) {
484b41dd
JB
15857 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15858 to_intel_crtc(c)->pipe);
66e514c1
DA
15859 drm_framebuffer_unreference(c->primary->fb);
15860 c->primary->fb = NULL;
36750f28 15861 c->primary->crtc = c->primary->state->crtc = NULL;
afd65eb4 15862 update_state_fb(c->primary);
36750f28 15863 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
484b41dd
JB
15864 }
15865 }
0962c3c9
VS
15866
15867 intel_backlight_register(dev);
79e53945
JB
15868}
15869
4932e2c3
ID
15870void intel_connector_unregister(struct intel_connector *intel_connector)
15871{
15872 struct drm_connector *connector = &intel_connector->base;
15873
15874 intel_panel_destroy_backlight(connector);
34ea3d38 15875 drm_connector_unregister(connector);
4932e2c3
ID
15876}
15877
79e53945
JB
15878void intel_modeset_cleanup(struct drm_device *dev)
15879{
652c393a 15880 struct drm_i915_private *dev_priv = dev->dev_private;
19c8054c 15881 struct intel_connector *connector;
652c393a 15882
2eb5252e
ID
15883 intel_disable_gt_powersave(dev);
15884
0962c3c9
VS
15885 intel_backlight_unregister(dev);
15886
fd0c0642
DV
15887 /*
15888 * Interrupts and polling as the first thing to avoid creating havoc.
2eb5252e 15889 * Too much stuff here (turning of connectors, ...) would
fd0c0642
DV
15890 * experience fancy races otherwise.
15891 */
2aeb7d3a 15892 intel_irq_uninstall(dev_priv);
eb21b92b 15893
fd0c0642
DV
15894 /*
15895 * Due to the hpd irq storm handling the hotplug work can re-arm the
15896 * poll handlers. Hence disable polling after hpd handling is shut down.
15897 */
f87ea761 15898 drm_kms_helper_poll_fini(dev);
fd0c0642 15899
723bfd70
JB
15900 intel_unregister_dsm_handler();
15901
c937ab3e 15902 intel_fbc_global_disable(dev_priv);
69341a5e 15903
1630fe75
CW
15904 /* flush any delayed tasks or pending work */
15905 flush_scheduled_work();
15906
db31af1d 15907 /* destroy the backlight and sysfs files before encoders/connectors */
19c8054c
JN
15908 for_each_intel_connector(dev, connector)
15909 connector->unregister(connector);
d9255d57 15910
79e53945 15911 drm_mode_config_cleanup(dev);
4d7bb011
DV
15912
15913 intel_cleanup_overlay(dev);
ae48434c 15914
ae48434c 15915 intel_cleanup_gt_powersave(dev);
f5949141
DV
15916
15917 intel_teardown_gmbus(dev);
79e53945
JB
15918}
15919
f1c79df3
ZW
15920/*
15921 * Return which encoder is currently attached for connector.
15922 */
df0e9248 15923struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
79e53945 15924{
df0e9248
CW
15925 return &intel_attached_encoder(connector)->base;
15926}
f1c79df3 15927
df0e9248
CW
15928void intel_connector_attach_encoder(struct intel_connector *connector,
15929 struct intel_encoder *encoder)
15930{
15931 connector->encoder = encoder;
15932 drm_mode_connector_attach_encoder(&connector->base,
15933 &encoder->base);
79e53945 15934}
28d52043
DA
15935
15936/*
15937 * set vga decode state - true == enable VGA decode
15938 */
15939int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
15940{
15941 struct drm_i915_private *dev_priv = dev->dev_private;
a885b3cc 15942 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
15943 u16 gmch_ctrl;
15944
75fa041d
CW
15945 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15946 DRM_ERROR("failed to read control word\n");
15947 return -EIO;
15948 }
15949
c0cc8a55
CW
15950 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15951 return 0;
15952
28d52043
DA
15953 if (state)
15954 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15955 else
15956 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
15957
15958 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15959 DRM_ERROR("failed to write control word\n");
15960 return -EIO;
15961 }
15962
28d52043
DA
15963 return 0;
15964}
c4a1d9e4 15965
c4a1d9e4 15966struct intel_display_error_state {
ff57f1b0
PZ
15967
15968 u32 power_well_driver;
15969
63b66e5b
CW
15970 int num_transcoders;
15971
c4a1d9e4
CW
15972 struct intel_cursor_error_state {
15973 u32 control;
15974 u32 position;
15975 u32 base;
15976 u32 size;
52331309 15977 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
15978
15979 struct intel_pipe_error_state {
ddf9c536 15980 bool power_domain_on;
c4a1d9e4 15981 u32 source;
f301b1e1 15982 u32 stat;
52331309 15983 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
15984
15985 struct intel_plane_error_state {
15986 u32 control;
15987 u32 stride;
15988 u32 size;
15989 u32 pos;
15990 u32 addr;
15991 u32 surface;
15992 u32 tile_offset;
52331309 15993 } plane[I915_MAX_PIPES];
63b66e5b
CW
15994
15995 struct intel_transcoder_error_state {
ddf9c536 15996 bool power_domain_on;
63b66e5b
CW
15997 enum transcoder cpu_transcoder;
15998
15999 u32 conf;
16000
16001 u32 htotal;
16002 u32 hblank;
16003 u32 hsync;
16004 u32 vtotal;
16005 u32 vblank;
16006 u32 vsync;
16007 } transcoder[4];
c4a1d9e4
CW
16008};
16009
16010struct intel_display_error_state *
16011intel_display_capture_error_state(struct drm_device *dev)
16012{
fbee40df 16013 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4 16014 struct intel_display_error_state *error;
63b66e5b
CW
16015 int transcoders[] = {
16016 TRANSCODER_A,
16017 TRANSCODER_B,
16018 TRANSCODER_C,
16019 TRANSCODER_EDP,
16020 };
c4a1d9e4
CW
16021 int i;
16022
63b66e5b
CW
16023 if (INTEL_INFO(dev)->num_pipes == 0)
16024 return NULL;
16025
9d1cb914 16026 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
16027 if (error == NULL)
16028 return NULL;
16029
190be112 16030 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
ff57f1b0
PZ
16031 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
16032
055e393f 16033 for_each_pipe(dev_priv, i) {
ddf9c536 16034 error->pipe[i].power_domain_on =
f458ebbc
DV
16035 __intel_display_power_is_enabled(dev_priv,
16036 POWER_DOMAIN_PIPE(i));
ddf9c536 16037 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
16038 continue;
16039
5efb3e28
VS
16040 error->cursor[i].control = I915_READ(CURCNTR(i));
16041 error->cursor[i].position = I915_READ(CURPOS(i));
16042 error->cursor[i].base = I915_READ(CURBASE(i));
c4a1d9e4
CW
16043
16044 error->plane[i].control = I915_READ(DSPCNTR(i));
16045 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
80ca378b 16046 if (INTEL_INFO(dev)->gen <= 3) {
51889b35 16047 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
16048 error->plane[i].pos = I915_READ(DSPPOS(i));
16049 }
ca291363
PZ
16050 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
16051 error->plane[i].addr = I915_READ(DSPADDR(i));
c4a1d9e4
CW
16052 if (INTEL_INFO(dev)->gen >= 4) {
16053 error->plane[i].surface = I915_READ(DSPSURF(i));
16054 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
16055 }
16056
c4a1d9e4 16057 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1 16058
3abfce77 16059 if (HAS_GMCH_DISPLAY(dev))
f301b1e1 16060 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
16061 }
16062
4d1de975 16063 /* Note: this does not include DSI transcoders. */
63b66e5b
CW
16064 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
16065 if (HAS_DDI(dev_priv->dev))
16066 error->num_transcoders++; /* Account for eDP. */
16067
16068 for (i = 0; i < error->num_transcoders; i++) {
16069 enum transcoder cpu_transcoder = transcoders[i];
16070
ddf9c536 16071 error->transcoder[i].power_domain_on =
f458ebbc 16072 __intel_display_power_is_enabled(dev_priv,
38cc1daf 16073 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 16074 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
16075 continue;
16076
63b66e5b
CW
16077 error->transcoder[i].cpu_transcoder = cpu_transcoder;
16078
16079 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
16080 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
16081 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
16082 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
16083 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
16084 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
16085 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
16086 }
16087
16088 return error;
16089}
16090
edc3d884
MK
16091#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
16092
c4a1d9e4 16093void
edc3d884 16094intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
16095 struct drm_device *dev,
16096 struct intel_display_error_state *error)
16097{
055e393f 16098 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4
CW
16099 int i;
16100
63b66e5b
CW
16101 if (!error)
16102 return;
16103
edc3d884 16104 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
190be112 16105 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
edc3d884 16106 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 16107 error->power_well_driver);
055e393f 16108 for_each_pipe(dev_priv, i) {
edc3d884 16109 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536 16110 err_printf(m, " Power: %s\n",
87ad3212 16111 onoff(error->pipe[i].power_domain_on));
edc3d884 16112 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 16113 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
16114
16115 err_printf(m, "Plane [%d]:\n", i);
16116 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
16117 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
80ca378b 16118 if (INTEL_INFO(dev)->gen <= 3) {
edc3d884
MK
16119 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
16120 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 16121 }
4b71a570 16122 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
edc3d884 16123 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
c4a1d9e4 16124 if (INTEL_INFO(dev)->gen >= 4) {
edc3d884
MK
16125 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
16126 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
16127 }
16128
edc3d884
MK
16129 err_printf(m, "Cursor [%d]:\n", i);
16130 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
16131 err_printf(m, " POS: %08x\n", error->cursor[i].position);
16132 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 16133 }
63b66e5b
CW
16134
16135 for (i = 0; i < error->num_transcoders; i++) {
da205630 16136 err_printf(m, "CPU transcoder: %s\n",
63b66e5b 16137 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536 16138 err_printf(m, " Power: %s\n",
87ad3212 16139 onoff(error->transcoder[i].power_domain_on));
63b66e5b
CW
16140 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
16141 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
16142 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
16143 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
16144 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
16145 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
16146 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
16147 }
c4a1d9e4 16148}