]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpu/drm/i915/intel_display.c
drm/i915: Perform object clflushing asynchronously
[mirror_ubuntu-bionic-kernel.git] / drivers / gpu / drm / i915 / intel_display.c
CommitLineData
79e53945
JB
1/*
2 * Copyright © 2006-2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 */
26
618563e3 27#include <linux/dmi.h>
c1c7af60
JB
28#include <linux/module.h>
29#include <linux/input.h>
79e53945 30#include <linux/i2c.h>
7662c8bd 31#include <linux/kernel.h>
5a0e3ad6 32#include <linux/slab.h>
9cce37f4 33#include <linux/vgaarb.h>
e0dac65e 34#include <drm/drm_edid.h>
760285e7 35#include <drm/drmP.h>
79e53945 36#include "intel_drv.h"
5d723d7a 37#include "intel_frontbuffer.h"
760285e7 38#include <drm/i915_drm.h>
79e53945 39#include "i915_drv.h"
57822dc6 40#include "i915_gem_clflush.h"
db18b6a6 41#include "intel_dsi.h"
e5510fac 42#include "i915_trace.h"
319c1d42 43#include <drm/drm_atomic.h>
c196e1d6 44#include <drm/drm_atomic_helper.h>
760285e7
DH
45#include <drm/drm_dp_helper.h>
46#include <drm/drm_crtc_helper.h>
465c120c
MR
47#include <drm/drm_plane_helper.h>
48#include <drm/drm_rect.h>
c0f372b3 49#include <linux/dma_remapping.h>
fd8e058a 50#include <linux/reservation.h>
79e53945 51
5a21b665
DV
52static bool is_mmio_work(struct intel_flip_work *work)
53{
54 return work->mmio_work.func;
55}
56
465c120c 57/* Primary plane formats for gen <= 3 */
568db4f2 58static const uint32_t i8xx_primary_formats[] = {
67fe7dc5
DL
59 DRM_FORMAT_C8,
60 DRM_FORMAT_RGB565,
465c120c 61 DRM_FORMAT_XRGB1555,
67fe7dc5 62 DRM_FORMAT_XRGB8888,
465c120c
MR
63};
64
65/* Primary plane formats for gen >= 4 */
568db4f2 66static const uint32_t i965_primary_formats[] = {
6c0fd451
DL
67 DRM_FORMAT_C8,
68 DRM_FORMAT_RGB565,
69 DRM_FORMAT_XRGB8888,
70 DRM_FORMAT_XBGR8888,
71 DRM_FORMAT_XRGB2101010,
72 DRM_FORMAT_XBGR2101010,
73};
74
75static const uint32_t skl_primary_formats[] = {
67fe7dc5
DL
76 DRM_FORMAT_C8,
77 DRM_FORMAT_RGB565,
78 DRM_FORMAT_XRGB8888,
465c120c 79 DRM_FORMAT_XBGR8888,
67fe7dc5 80 DRM_FORMAT_ARGB8888,
465c120c
MR
81 DRM_FORMAT_ABGR8888,
82 DRM_FORMAT_XRGB2101010,
465c120c 83 DRM_FORMAT_XBGR2101010,
ea916ea0
KM
84 DRM_FORMAT_YUYV,
85 DRM_FORMAT_YVYU,
86 DRM_FORMAT_UYVY,
87 DRM_FORMAT_VYUY,
465c120c
MR
88};
89
3d7d6510
MR
90/* Cursor formats */
91static const uint32_t intel_cursor_formats[] = {
92 DRM_FORMAT_ARGB8888,
93};
94
f1f644dc 95static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 96 struct intel_crtc_state *pipe_config);
18442d08 97static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 98 struct intel_crtc_state *pipe_config);
f1f644dc 99
24dbf51a
CW
100static int intel_framebuffer_init(struct intel_framebuffer *ifb,
101 struct drm_i915_gem_object *obj,
102 struct drm_mode_fb_cmd2 *mode_cmd);
5b18e57c
DV
103static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
104static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
bc58be60 105static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
29407aab 106static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
107 struct intel_link_m_n *m_n,
108 struct intel_link_m_n *m2_n2);
29407aab 109static void ironlake_set_pipeconf(struct drm_crtc *crtc);
229fca97 110static void haswell_set_pipeconf(struct drm_crtc *crtc);
391bf048 111static void haswell_set_pipemisc(struct drm_crtc *crtc);
d288f65f 112static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 113 const struct intel_crtc_state *pipe_config);
d288f65f 114static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 115 const struct intel_crtc_state *pipe_config);
5a21b665
DV
116static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
117static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
1c74eeaf
NM
118static void intel_crtc_init_scalers(struct intel_crtc *crtc,
119 struct intel_crtc_state *crtc_state);
bfd16b2a
ML
120static void skylake_pfit_enable(struct intel_crtc *crtc);
121static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
122static void ironlake_pfit_enable(struct intel_crtc *crtc);
043e9bda 123static void intel_modeset_setup_hw_state(struct drm_device *dev);
2622a081 124static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
e7457a9a 125
d4906093 126struct intel_limit {
4c5def93
ACO
127 struct {
128 int min, max;
129 } dot, vco, n, m, m1, m2, p, p1;
130
131 struct {
132 int dot_limit;
133 int p2_slow, p2_fast;
134 } p2;
d4906093 135};
79e53945 136
bfa7df01 137/* returns HPLL frequency in kHz */
49cd97a3 138int vlv_get_hpll_vco(struct drm_i915_private *dev_priv)
bfa7df01
VS
139{
140 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
141
142 /* Obtain SKU information */
143 mutex_lock(&dev_priv->sb_lock);
144 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
145 CCK_FUSE_HPLL_FREQ_MASK;
146 mutex_unlock(&dev_priv->sb_lock);
147
148 return vco_freq[hpll_freq] * 1000;
149}
150
c30fec65
VS
151int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
152 const char *name, u32 reg, int ref_freq)
bfa7df01
VS
153{
154 u32 val;
155 int divider;
156
bfa7df01
VS
157 mutex_lock(&dev_priv->sb_lock);
158 val = vlv_cck_read(dev_priv, reg);
159 mutex_unlock(&dev_priv->sb_lock);
160
161 divider = val & CCK_FREQUENCY_VALUES;
162
163 WARN((val & CCK_FREQUENCY_STATUS) !=
164 (divider << CCK_FREQUENCY_STATUS_SHIFT),
165 "%s change in progress\n", name);
166
c30fec65
VS
167 return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
168}
169
7ff89ca2
VS
170int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
171 const char *name, u32 reg)
c30fec65
VS
172{
173 if (dev_priv->hpll_freq == 0)
49cd97a3 174 dev_priv->hpll_freq = vlv_get_hpll_vco(dev_priv);
c30fec65
VS
175
176 return vlv_get_cck_clock(dev_priv, name, reg,
177 dev_priv->hpll_freq);
bfa7df01
VS
178}
179
bfa7df01
VS
180static void intel_update_czclk(struct drm_i915_private *dev_priv)
181{
666a4537 182 if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
bfa7df01
VS
183 return;
184
185 dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
186 CCK_CZ_CLOCK_CONTROL);
187
188 DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
189}
190
021357ac 191static inline u32 /* units of 100MHz */
21a727b3
VS
192intel_fdi_link_freq(struct drm_i915_private *dev_priv,
193 const struct intel_crtc_state *pipe_config)
021357ac 194{
21a727b3
VS
195 if (HAS_DDI(dev_priv))
196 return pipe_config->port_clock; /* SPLL */
197 else if (IS_GEN5(dev_priv))
198 return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
e3b247da 199 else
21a727b3 200 return 270000;
021357ac
CW
201}
202
1b6f4958 203static const struct intel_limit intel_limits_i8xx_dac = {
0206e353 204 .dot = { .min = 25000, .max = 350000 },
9c333719 205 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 206 .n = { .min = 2, .max = 16 },
0206e353
AJ
207 .m = { .min = 96, .max = 140 },
208 .m1 = { .min = 18, .max = 26 },
209 .m2 = { .min = 6, .max = 16 },
210 .p = { .min = 4, .max = 128 },
211 .p1 = { .min = 2, .max = 33 },
273e27ca
EA
212 .p2 = { .dot_limit = 165000,
213 .p2_slow = 4, .p2_fast = 2 },
e4b36699
KP
214};
215
1b6f4958 216static const struct intel_limit intel_limits_i8xx_dvo = {
5d536e28 217 .dot = { .min = 25000, .max = 350000 },
9c333719 218 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 219 .n = { .min = 2, .max = 16 },
5d536e28
DV
220 .m = { .min = 96, .max = 140 },
221 .m1 = { .min = 18, .max = 26 },
222 .m2 = { .min = 6, .max = 16 },
223 .p = { .min = 4, .max = 128 },
224 .p1 = { .min = 2, .max = 33 },
225 .p2 = { .dot_limit = 165000,
226 .p2_slow = 4, .p2_fast = 4 },
227};
228
1b6f4958 229static const struct intel_limit intel_limits_i8xx_lvds = {
0206e353 230 .dot = { .min = 25000, .max = 350000 },
9c333719 231 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 232 .n = { .min = 2, .max = 16 },
0206e353
AJ
233 .m = { .min = 96, .max = 140 },
234 .m1 = { .min = 18, .max = 26 },
235 .m2 = { .min = 6, .max = 16 },
236 .p = { .min = 4, .max = 128 },
237 .p1 = { .min = 1, .max = 6 },
273e27ca
EA
238 .p2 = { .dot_limit = 165000,
239 .p2_slow = 14, .p2_fast = 7 },
e4b36699 240};
273e27ca 241
1b6f4958 242static const struct intel_limit intel_limits_i9xx_sdvo = {
0206e353
AJ
243 .dot = { .min = 20000, .max = 400000 },
244 .vco = { .min = 1400000, .max = 2800000 },
245 .n = { .min = 1, .max = 6 },
246 .m = { .min = 70, .max = 120 },
4f7dfb67
PJ
247 .m1 = { .min = 8, .max = 18 },
248 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
249 .p = { .min = 5, .max = 80 },
250 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
251 .p2 = { .dot_limit = 200000,
252 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
253};
254
1b6f4958 255static const struct intel_limit intel_limits_i9xx_lvds = {
0206e353
AJ
256 .dot = { .min = 20000, .max = 400000 },
257 .vco = { .min = 1400000, .max = 2800000 },
258 .n = { .min = 1, .max = 6 },
259 .m = { .min = 70, .max = 120 },
53a7d2d1
PJ
260 .m1 = { .min = 8, .max = 18 },
261 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
262 .p = { .min = 7, .max = 98 },
263 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
264 .p2 = { .dot_limit = 112000,
265 .p2_slow = 14, .p2_fast = 7 },
e4b36699
KP
266};
267
273e27ca 268
1b6f4958 269static const struct intel_limit intel_limits_g4x_sdvo = {
273e27ca
EA
270 .dot = { .min = 25000, .max = 270000 },
271 .vco = { .min = 1750000, .max = 3500000},
272 .n = { .min = 1, .max = 4 },
273 .m = { .min = 104, .max = 138 },
274 .m1 = { .min = 17, .max = 23 },
275 .m2 = { .min = 5, .max = 11 },
276 .p = { .min = 10, .max = 30 },
277 .p1 = { .min = 1, .max = 3},
278 .p2 = { .dot_limit = 270000,
279 .p2_slow = 10,
280 .p2_fast = 10
044c7c41 281 },
e4b36699
KP
282};
283
1b6f4958 284static const struct intel_limit intel_limits_g4x_hdmi = {
273e27ca
EA
285 .dot = { .min = 22000, .max = 400000 },
286 .vco = { .min = 1750000, .max = 3500000},
287 .n = { .min = 1, .max = 4 },
288 .m = { .min = 104, .max = 138 },
289 .m1 = { .min = 16, .max = 23 },
290 .m2 = { .min = 5, .max = 11 },
291 .p = { .min = 5, .max = 80 },
292 .p1 = { .min = 1, .max = 8},
293 .p2 = { .dot_limit = 165000,
294 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
295};
296
1b6f4958 297static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
273e27ca
EA
298 .dot = { .min = 20000, .max = 115000 },
299 .vco = { .min = 1750000, .max = 3500000 },
300 .n = { .min = 1, .max = 3 },
301 .m = { .min = 104, .max = 138 },
302 .m1 = { .min = 17, .max = 23 },
303 .m2 = { .min = 5, .max = 11 },
304 .p = { .min = 28, .max = 112 },
305 .p1 = { .min = 2, .max = 8 },
306 .p2 = { .dot_limit = 0,
307 .p2_slow = 14, .p2_fast = 14
044c7c41 308 },
e4b36699
KP
309};
310
1b6f4958 311static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
273e27ca
EA
312 .dot = { .min = 80000, .max = 224000 },
313 .vco = { .min = 1750000, .max = 3500000 },
314 .n = { .min = 1, .max = 3 },
315 .m = { .min = 104, .max = 138 },
316 .m1 = { .min = 17, .max = 23 },
317 .m2 = { .min = 5, .max = 11 },
318 .p = { .min = 14, .max = 42 },
319 .p1 = { .min = 2, .max = 6 },
320 .p2 = { .dot_limit = 0,
321 .p2_slow = 7, .p2_fast = 7
044c7c41 322 },
e4b36699
KP
323};
324
1b6f4958 325static const struct intel_limit intel_limits_pineview_sdvo = {
0206e353
AJ
326 .dot = { .min = 20000, .max = 400000},
327 .vco = { .min = 1700000, .max = 3500000 },
273e27ca 328 /* Pineview's Ncounter is a ring counter */
0206e353
AJ
329 .n = { .min = 3, .max = 6 },
330 .m = { .min = 2, .max = 256 },
273e27ca 331 /* Pineview only has one combined m divider, which we treat as m2. */
0206e353
AJ
332 .m1 = { .min = 0, .max = 0 },
333 .m2 = { .min = 0, .max = 254 },
334 .p = { .min = 5, .max = 80 },
335 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
336 .p2 = { .dot_limit = 200000,
337 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
338};
339
1b6f4958 340static const struct intel_limit intel_limits_pineview_lvds = {
0206e353
AJ
341 .dot = { .min = 20000, .max = 400000 },
342 .vco = { .min = 1700000, .max = 3500000 },
343 .n = { .min = 3, .max = 6 },
344 .m = { .min = 2, .max = 256 },
345 .m1 = { .min = 0, .max = 0 },
346 .m2 = { .min = 0, .max = 254 },
347 .p = { .min = 7, .max = 112 },
348 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
349 .p2 = { .dot_limit = 112000,
350 .p2_slow = 14, .p2_fast = 14 },
e4b36699
KP
351};
352
273e27ca
EA
353/* Ironlake / Sandybridge
354 *
355 * We calculate clock using (register_value + 2) for N/M1/M2, so here
356 * the range value for them is (actual_value - 2).
357 */
1b6f4958 358static const struct intel_limit intel_limits_ironlake_dac = {
273e27ca
EA
359 .dot = { .min = 25000, .max = 350000 },
360 .vco = { .min = 1760000, .max = 3510000 },
361 .n = { .min = 1, .max = 5 },
362 .m = { .min = 79, .max = 127 },
363 .m1 = { .min = 12, .max = 22 },
364 .m2 = { .min = 5, .max = 9 },
365 .p = { .min = 5, .max = 80 },
366 .p1 = { .min = 1, .max = 8 },
367 .p2 = { .dot_limit = 225000,
368 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
369};
370
1b6f4958 371static const struct intel_limit intel_limits_ironlake_single_lvds = {
273e27ca
EA
372 .dot = { .min = 25000, .max = 350000 },
373 .vco = { .min = 1760000, .max = 3510000 },
374 .n = { .min = 1, .max = 3 },
375 .m = { .min = 79, .max = 118 },
376 .m1 = { .min = 12, .max = 22 },
377 .m2 = { .min = 5, .max = 9 },
378 .p = { .min = 28, .max = 112 },
379 .p1 = { .min = 2, .max = 8 },
380 .p2 = { .dot_limit = 225000,
381 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
382};
383
1b6f4958 384static const struct intel_limit intel_limits_ironlake_dual_lvds = {
273e27ca
EA
385 .dot = { .min = 25000, .max = 350000 },
386 .vco = { .min = 1760000, .max = 3510000 },
387 .n = { .min = 1, .max = 3 },
388 .m = { .min = 79, .max = 127 },
389 .m1 = { .min = 12, .max = 22 },
390 .m2 = { .min = 5, .max = 9 },
391 .p = { .min = 14, .max = 56 },
392 .p1 = { .min = 2, .max = 8 },
393 .p2 = { .dot_limit = 225000,
394 .p2_slow = 7, .p2_fast = 7 },
b91ad0ec
ZW
395};
396
273e27ca 397/* LVDS 100mhz refclk limits. */
1b6f4958 398static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
273e27ca
EA
399 .dot = { .min = 25000, .max = 350000 },
400 .vco = { .min = 1760000, .max = 3510000 },
401 .n = { .min = 1, .max = 2 },
402 .m = { .min = 79, .max = 126 },
403 .m1 = { .min = 12, .max = 22 },
404 .m2 = { .min = 5, .max = 9 },
405 .p = { .min = 28, .max = 112 },
0206e353 406 .p1 = { .min = 2, .max = 8 },
273e27ca
EA
407 .p2 = { .dot_limit = 225000,
408 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
409};
410
1b6f4958 411static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
273e27ca
EA
412 .dot = { .min = 25000, .max = 350000 },
413 .vco = { .min = 1760000, .max = 3510000 },
414 .n = { .min = 1, .max = 3 },
415 .m = { .min = 79, .max = 126 },
416 .m1 = { .min = 12, .max = 22 },
417 .m2 = { .min = 5, .max = 9 },
418 .p = { .min = 14, .max = 42 },
0206e353 419 .p1 = { .min = 2, .max = 6 },
273e27ca
EA
420 .p2 = { .dot_limit = 225000,
421 .p2_slow = 7, .p2_fast = 7 },
4547668a
ZY
422};
423
1b6f4958 424static const struct intel_limit intel_limits_vlv = {
f01b7962
VS
425 /*
426 * These are the data rate limits (measured in fast clocks)
427 * since those are the strictest limits we have. The fast
428 * clock and actual rate limits are more relaxed, so checking
429 * them would make no difference.
430 */
431 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
75e53986 432 .vco = { .min = 4000000, .max = 6000000 },
a0c4da24 433 .n = { .min = 1, .max = 7 },
a0c4da24
JB
434 .m1 = { .min = 2, .max = 3 },
435 .m2 = { .min = 11, .max = 156 },
b99ab663 436 .p1 = { .min = 2, .max = 3 },
5fdc9c49 437 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
a0c4da24
JB
438};
439
1b6f4958 440static const struct intel_limit intel_limits_chv = {
ef9348c8
CML
441 /*
442 * These are the data rate limits (measured in fast clocks)
443 * since those are the strictest limits we have. The fast
444 * clock and actual rate limits are more relaxed, so checking
445 * them would make no difference.
446 */
447 .dot = { .min = 25000 * 5, .max = 540000 * 5},
17fe1021 448 .vco = { .min = 4800000, .max = 6480000 },
ef9348c8
CML
449 .n = { .min = 1, .max = 1 },
450 .m1 = { .min = 2, .max = 2 },
451 .m2 = { .min = 24 << 22, .max = 175 << 22 },
452 .p1 = { .min = 2, .max = 4 },
453 .p2 = { .p2_slow = 1, .p2_fast = 14 },
454};
455
1b6f4958 456static const struct intel_limit intel_limits_bxt = {
5ab7b0b7
ID
457 /* FIXME: find real dot limits */
458 .dot = { .min = 0, .max = INT_MAX },
e6292556 459 .vco = { .min = 4800000, .max = 6700000 },
5ab7b0b7
ID
460 .n = { .min = 1, .max = 1 },
461 .m1 = { .min = 2, .max = 2 },
462 /* FIXME: find real m2 limits */
463 .m2 = { .min = 2 << 22, .max = 255 << 22 },
464 .p1 = { .min = 2, .max = 4 },
465 .p2 = { .p2_slow = 1, .p2_fast = 20 },
466};
467
cdba954e
ACO
468static bool
469needs_modeset(struct drm_crtc_state *state)
470{
fc596660 471 return drm_atomic_crtc_needs_modeset(state);
cdba954e
ACO
472}
473
dccbea3b
ID
474/*
475 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
476 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
477 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
478 * The helpers' return value is the rate of the clock that is fed to the
479 * display engine's pipe which can be the above fast dot clock rate or a
480 * divided-down version of it.
481 */
f2b115e6 482/* m1 is reserved as 0 in Pineview, n is a ring counter */
9e2c8475 483static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
79e53945 484{
2177832f
SL
485 clock->m = clock->m2 + 2;
486 clock->p = clock->p1 * clock->p2;
ed5ca77e 487 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 488 return 0;
fb03ac01
VS
489 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
490 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
491
492 return clock->dot;
2177832f
SL
493}
494
7429e9d4
DV
495static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
496{
497 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
498}
499
9e2c8475 500static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
2177832f 501{
7429e9d4 502 clock->m = i9xx_dpll_compute_m(clock);
79e53945 503 clock->p = clock->p1 * clock->p2;
ed5ca77e 504 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
dccbea3b 505 return 0;
fb03ac01
VS
506 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
507 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
508
509 return clock->dot;
79e53945
JB
510}
511
9e2c8475 512static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
589eca67
ID
513{
514 clock->m = clock->m1 * clock->m2;
515 clock->p = clock->p1 * clock->p2;
516 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 517 return 0;
589eca67
ID
518 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
519 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
520
521 return clock->dot / 5;
589eca67
ID
522}
523
9e2c8475 524int chv_calc_dpll_params(int refclk, struct dpll *clock)
ef9348c8
CML
525{
526 clock->m = clock->m1 * clock->m2;
527 clock->p = clock->p1 * clock->p2;
528 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 529 return 0;
ef9348c8
CML
530 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
531 clock->n << 22);
532 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
533
534 return clock->dot / 5;
ef9348c8
CML
535}
536
7c04d1d9 537#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
79e53945
JB
538/**
539 * Returns whether the given set of divisors are valid for a given refclk with
540 * the given connectors.
541 */
542
e2d214ae 543static bool intel_PLL_is_valid(struct drm_i915_private *dev_priv,
1b6f4958 544 const struct intel_limit *limit,
9e2c8475 545 const struct dpll *clock)
79e53945 546{
f01b7962
VS
547 if (clock->n < limit->n.min || limit->n.max < clock->n)
548 INTELPllInvalid("n out of range\n");
79e53945 549 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
0206e353 550 INTELPllInvalid("p1 out of range\n");
79e53945 551 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
0206e353 552 INTELPllInvalid("m2 out of range\n");
79e53945 553 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
0206e353 554 INTELPllInvalid("m1 out of range\n");
f01b7962 555
e2d214ae 556 if (!IS_PINEVIEW(dev_priv) && !IS_VALLEYVIEW(dev_priv) &&
cc3f90f0 557 !IS_CHERRYVIEW(dev_priv) && !IS_GEN9_LP(dev_priv))
f01b7962
VS
558 if (clock->m1 <= clock->m2)
559 INTELPllInvalid("m1 <= m2\n");
560
e2d214ae 561 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
cc3f90f0 562 !IS_GEN9_LP(dev_priv)) {
f01b7962
VS
563 if (clock->p < limit->p.min || limit->p.max < clock->p)
564 INTELPllInvalid("p out of range\n");
565 if (clock->m < limit->m.min || limit->m.max < clock->m)
566 INTELPllInvalid("m out of range\n");
567 }
568
79e53945 569 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
0206e353 570 INTELPllInvalid("vco out of range\n");
79e53945
JB
571 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
572 * connector, etc., rather than just a single range.
573 */
574 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
0206e353 575 INTELPllInvalid("dot out of range\n");
79e53945
JB
576
577 return true;
578}
579
3b1429d9 580static int
1b6f4958 581i9xx_select_p2_div(const struct intel_limit *limit,
3b1429d9
VS
582 const struct intel_crtc_state *crtc_state,
583 int target)
79e53945 584{
3b1429d9 585 struct drm_device *dev = crtc_state->base.crtc->dev;
79e53945 586
2d84d2b3 587 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
79e53945 588 /*
a210b028
DV
589 * For LVDS just rely on its current settings for dual-channel.
590 * We haven't figured out how to reliably set up different
591 * single/dual channel state, if we even can.
79e53945 592 */
1974cad0 593 if (intel_is_dual_link_lvds(dev))
3b1429d9 594 return limit->p2.p2_fast;
79e53945 595 else
3b1429d9 596 return limit->p2.p2_slow;
79e53945
JB
597 } else {
598 if (target < limit->p2.dot_limit)
3b1429d9 599 return limit->p2.p2_slow;
79e53945 600 else
3b1429d9 601 return limit->p2.p2_fast;
79e53945 602 }
3b1429d9
VS
603}
604
70e8aa21
ACO
605/*
606 * Returns a set of divisors for the desired target clock with the given
607 * refclk, or FALSE. The returned values represent the clock equation:
608 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
609 *
610 * Target and reference clocks are specified in kHz.
611 *
612 * If match_clock is provided, then best_clock P divider must match the P
613 * divider from @match_clock used for LVDS downclocking.
614 */
3b1429d9 615static bool
1b6f4958 616i9xx_find_best_dpll(const struct intel_limit *limit,
3b1429d9 617 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
618 int target, int refclk, struct dpll *match_clock,
619 struct dpll *best_clock)
3b1429d9
VS
620{
621 struct drm_device *dev = crtc_state->base.crtc->dev;
9e2c8475 622 struct dpll clock;
3b1429d9 623 int err = target;
79e53945 624
0206e353 625 memset(best_clock, 0, sizeof(*best_clock));
79e53945 626
3b1429d9
VS
627 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
628
42158660
ZY
629 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
630 clock.m1++) {
631 for (clock.m2 = limit->m2.min;
632 clock.m2 <= limit->m2.max; clock.m2++) {
c0efc387 633 if (clock.m2 >= clock.m1)
42158660
ZY
634 break;
635 for (clock.n = limit->n.min;
636 clock.n <= limit->n.max; clock.n++) {
637 for (clock.p1 = limit->p1.min;
638 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
639 int this_err;
640
dccbea3b 641 i9xx_calc_dpll_params(refclk, &clock);
e2d214ae
TU
642 if (!intel_PLL_is_valid(to_i915(dev),
643 limit,
ac58c3f0
DV
644 &clock))
645 continue;
646 if (match_clock &&
647 clock.p != match_clock->p)
648 continue;
649
650 this_err = abs(clock.dot - target);
651 if (this_err < err) {
652 *best_clock = clock;
653 err = this_err;
654 }
655 }
656 }
657 }
658 }
659
660 return (err != target);
661}
662
70e8aa21
ACO
663/*
664 * Returns a set of divisors for the desired target clock with the given
665 * refclk, or FALSE. The returned values represent the clock equation:
666 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
667 *
668 * Target and reference clocks are specified in kHz.
669 *
670 * If match_clock is provided, then best_clock P divider must match the P
671 * divider from @match_clock used for LVDS downclocking.
672 */
ac58c3f0 673static bool
1b6f4958 674pnv_find_best_dpll(const struct intel_limit *limit,
a93e255f 675 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
676 int target, int refclk, struct dpll *match_clock,
677 struct dpll *best_clock)
79e53945 678{
3b1429d9 679 struct drm_device *dev = crtc_state->base.crtc->dev;
9e2c8475 680 struct dpll clock;
79e53945
JB
681 int err = target;
682
0206e353 683 memset(best_clock, 0, sizeof(*best_clock));
79e53945 684
3b1429d9
VS
685 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
686
42158660
ZY
687 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
688 clock.m1++) {
689 for (clock.m2 = limit->m2.min;
690 clock.m2 <= limit->m2.max; clock.m2++) {
42158660
ZY
691 for (clock.n = limit->n.min;
692 clock.n <= limit->n.max; clock.n++) {
693 for (clock.p1 = limit->p1.min;
694 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
695 int this_err;
696
dccbea3b 697 pnv_calc_dpll_params(refclk, &clock);
e2d214ae
TU
698 if (!intel_PLL_is_valid(to_i915(dev),
699 limit,
1b894b59 700 &clock))
79e53945 701 continue;
cec2f356
SP
702 if (match_clock &&
703 clock.p != match_clock->p)
704 continue;
79e53945
JB
705
706 this_err = abs(clock.dot - target);
707 if (this_err < err) {
708 *best_clock = clock;
709 err = this_err;
710 }
711 }
712 }
713 }
714 }
715
716 return (err != target);
717}
718
997c030c
ACO
719/*
720 * Returns a set of divisors for the desired target clock with the given
721 * refclk, or FALSE. The returned values represent the clock equation:
722 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
70e8aa21
ACO
723 *
724 * Target and reference clocks are specified in kHz.
725 *
726 * If match_clock is provided, then best_clock P divider must match the P
727 * divider from @match_clock used for LVDS downclocking.
997c030c 728 */
d4906093 729static bool
1b6f4958 730g4x_find_best_dpll(const struct intel_limit *limit,
a93e255f 731 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
732 int target, int refclk, struct dpll *match_clock,
733 struct dpll *best_clock)
d4906093 734{
3b1429d9 735 struct drm_device *dev = crtc_state->base.crtc->dev;
9e2c8475 736 struct dpll clock;
d4906093 737 int max_n;
3b1429d9 738 bool found = false;
6ba770dc
AJ
739 /* approximately equals target * 0.00585 */
740 int err_most = (target >> 8) + (target >> 9);
d4906093
ML
741
742 memset(best_clock, 0, sizeof(*best_clock));
3b1429d9
VS
743
744 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
745
d4906093 746 max_n = limit->n.max;
f77f13e2 747 /* based on hardware requirement, prefer smaller n to precision */
d4906093 748 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
f77f13e2 749 /* based on hardware requirement, prefere larger m1,m2 */
d4906093
ML
750 for (clock.m1 = limit->m1.max;
751 clock.m1 >= limit->m1.min; clock.m1--) {
752 for (clock.m2 = limit->m2.max;
753 clock.m2 >= limit->m2.min; clock.m2--) {
754 for (clock.p1 = limit->p1.max;
755 clock.p1 >= limit->p1.min; clock.p1--) {
756 int this_err;
757
dccbea3b 758 i9xx_calc_dpll_params(refclk, &clock);
e2d214ae
TU
759 if (!intel_PLL_is_valid(to_i915(dev),
760 limit,
1b894b59 761 &clock))
d4906093 762 continue;
1b894b59
CW
763
764 this_err = abs(clock.dot - target);
d4906093
ML
765 if (this_err < err_most) {
766 *best_clock = clock;
767 err_most = this_err;
768 max_n = clock.n;
769 found = true;
770 }
771 }
772 }
773 }
774 }
2c07245f
ZW
775 return found;
776}
777
d5dd62bd
ID
778/*
779 * Check if the calculated PLL configuration is more optimal compared to the
780 * best configuration and error found so far. Return the calculated error.
781 */
782static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
9e2c8475
ACO
783 const struct dpll *calculated_clock,
784 const struct dpll *best_clock,
d5dd62bd
ID
785 unsigned int best_error_ppm,
786 unsigned int *error_ppm)
787{
9ca3ba01
ID
788 /*
789 * For CHV ignore the error and consider only the P value.
790 * Prefer a bigger P value based on HW requirements.
791 */
920a14b2 792 if (IS_CHERRYVIEW(to_i915(dev))) {
9ca3ba01
ID
793 *error_ppm = 0;
794
795 return calculated_clock->p > best_clock->p;
796 }
797
24be4e46
ID
798 if (WARN_ON_ONCE(!target_freq))
799 return false;
800
d5dd62bd
ID
801 *error_ppm = div_u64(1000000ULL *
802 abs(target_freq - calculated_clock->dot),
803 target_freq);
804 /*
805 * Prefer a better P value over a better (smaller) error if the error
806 * is small. Ensure this preference for future configurations too by
807 * setting the error to 0.
808 */
809 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
810 *error_ppm = 0;
811
812 return true;
813 }
814
815 return *error_ppm + 10 < best_error_ppm;
816}
817
65b3d6a9
ACO
818/*
819 * Returns a set of divisors for the desired target clock with the given
820 * refclk, or FALSE. The returned values represent the clock equation:
821 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
822 */
a0c4da24 823static bool
1b6f4958 824vlv_find_best_dpll(const struct intel_limit *limit,
a93e255f 825 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
826 int target, int refclk, struct dpll *match_clock,
827 struct dpll *best_clock)
a0c4da24 828{
a93e255f 829 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 830 struct drm_device *dev = crtc->base.dev;
9e2c8475 831 struct dpll clock;
69e4f900 832 unsigned int bestppm = 1000000;
27e639bf
VS
833 /* min update 19.2 MHz */
834 int max_n = min(limit->n.max, refclk / 19200);
49e497ef 835 bool found = false;
a0c4da24 836
6b4bf1c4
VS
837 target *= 5; /* fast clock */
838
839 memset(best_clock, 0, sizeof(*best_clock));
a0c4da24
JB
840
841 /* based on hardware requirement, prefer smaller n to precision */
27e639bf 842 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
811bbf05 843 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
889059d8 844 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
c1a9ae43 845 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
6b4bf1c4 846 clock.p = clock.p1 * clock.p2;
a0c4da24 847 /* based on hardware requirement, prefer bigger m1,m2 values */
6b4bf1c4 848 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
d5dd62bd 849 unsigned int ppm;
69e4f900 850
6b4bf1c4
VS
851 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
852 refclk * clock.m1);
853
dccbea3b 854 vlv_calc_dpll_params(refclk, &clock);
43b0ac53 855
e2d214ae
TU
856 if (!intel_PLL_is_valid(to_i915(dev),
857 limit,
f01b7962 858 &clock))
43b0ac53
VS
859 continue;
860
d5dd62bd
ID
861 if (!vlv_PLL_is_optimal(dev, target,
862 &clock,
863 best_clock,
864 bestppm, &ppm))
865 continue;
6b4bf1c4 866
d5dd62bd
ID
867 *best_clock = clock;
868 bestppm = ppm;
869 found = true;
a0c4da24
JB
870 }
871 }
872 }
873 }
a0c4da24 874
49e497ef 875 return found;
a0c4da24 876}
a4fc5ed6 877
65b3d6a9
ACO
878/*
879 * Returns a set of divisors for the desired target clock with the given
880 * refclk, or FALSE. The returned values represent the clock equation:
881 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
882 */
ef9348c8 883static bool
1b6f4958 884chv_find_best_dpll(const struct intel_limit *limit,
a93e255f 885 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
886 int target, int refclk, struct dpll *match_clock,
887 struct dpll *best_clock)
ef9348c8 888{
a93e255f 889 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 890 struct drm_device *dev = crtc->base.dev;
9ca3ba01 891 unsigned int best_error_ppm;
9e2c8475 892 struct dpll clock;
ef9348c8
CML
893 uint64_t m2;
894 int found = false;
895
896 memset(best_clock, 0, sizeof(*best_clock));
9ca3ba01 897 best_error_ppm = 1000000;
ef9348c8
CML
898
899 /*
900 * Based on hardware doc, the n always set to 1, and m1 always
901 * set to 2. If requires to support 200Mhz refclk, we need to
902 * revisit this because n may not 1 anymore.
903 */
904 clock.n = 1, clock.m1 = 2;
905 target *= 5; /* fast clock */
906
907 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
908 for (clock.p2 = limit->p2.p2_fast;
909 clock.p2 >= limit->p2.p2_slow;
910 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
9ca3ba01 911 unsigned int error_ppm;
ef9348c8
CML
912
913 clock.p = clock.p1 * clock.p2;
914
915 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
916 clock.n) << 22, refclk * clock.m1);
917
918 if (m2 > INT_MAX/clock.m1)
919 continue;
920
921 clock.m2 = m2;
922
dccbea3b 923 chv_calc_dpll_params(refclk, &clock);
ef9348c8 924
e2d214ae 925 if (!intel_PLL_is_valid(to_i915(dev), limit, &clock))
ef9348c8
CML
926 continue;
927
9ca3ba01
ID
928 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
929 best_error_ppm, &error_ppm))
930 continue;
931
932 *best_clock = clock;
933 best_error_ppm = error_ppm;
934 found = true;
ef9348c8
CML
935 }
936 }
937
938 return found;
939}
940
5ab7b0b7 941bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
9e2c8475 942 struct dpll *best_clock)
5ab7b0b7 943{
65b3d6a9 944 int refclk = 100000;
1b6f4958 945 const struct intel_limit *limit = &intel_limits_bxt;
5ab7b0b7 946
65b3d6a9 947 return chv_find_best_dpll(limit, crtc_state,
5ab7b0b7
ID
948 target_clock, refclk, NULL, best_clock);
949}
950
525b9311 951bool intel_crtc_active(struct intel_crtc *crtc)
20ddf665 952{
20ddf665
VS
953 /* Be paranoid as we can arrive here with only partial
954 * state retrieved from the hardware during setup.
955 *
241bfc38 956 * We can ditch the adjusted_mode.crtc_clock check as soon
20ddf665
VS
957 * as Haswell has gained clock readout/fastboot support.
958 *
66e514c1 959 * We can ditch the crtc->primary->fb check as soon as we can
20ddf665 960 * properly reconstruct framebuffers.
c3d1f436
MR
961 *
962 * FIXME: The intel_crtc->active here should be switched to
963 * crtc->state->active once we have proper CRTC states wired up
964 * for atomic.
20ddf665 965 */
525b9311
VS
966 return crtc->active && crtc->base.primary->state->fb &&
967 crtc->config->base.adjusted_mode.crtc_clock;
20ddf665
VS
968}
969
a5c961d1
PZ
970enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
971 enum pipe pipe)
972{
98187836 973 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
a5c961d1 974
e2af48c6 975 return crtc->config->cpu_transcoder;
a5c961d1
PZ
976}
977
6315b5d3 978static bool pipe_dsl_stopped(struct drm_i915_private *dev_priv, enum pipe pipe)
fbf49ea2 979{
f0f59a00 980 i915_reg_t reg = PIPEDSL(pipe);
fbf49ea2
VS
981 u32 line1, line2;
982 u32 line_mask;
983
5db94019 984 if (IS_GEN2(dev_priv))
fbf49ea2
VS
985 line_mask = DSL_LINEMASK_GEN2;
986 else
987 line_mask = DSL_LINEMASK_GEN3;
988
989 line1 = I915_READ(reg) & line_mask;
6adfb1ef 990 msleep(5);
fbf49ea2
VS
991 line2 = I915_READ(reg) & line_mask;
992
993 return line1 == line2;
994}
995
ab7ad7f6
KP
996/*
997 * intel_wait_for_pipe_off - wait for pipe to turn off
575f7ab7 998 * @crtc: crtc whose pipe to wait for
9d0498a2
JB
999 *
1000 * After disabling a pipe, we can't wait for vblank in the usual way,
1001 * spinning on the vblank interrupt status bit, since we won't actually
1002 * see an interrupt when the pipe is disabled.
1003 *
ab7ad7f6
KP
1004 * On Gen4 and above:
1005 * wait for the pipe register state bit to turn off
1006 *
1007 * Otherwise:
1008 * wait for the display line value to settle (it usually
1009 * ends up stopping at the start of the next frame).
58e10eb9 1010 *
9d0498a2 1011 */
575f7ab7 1012static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
9d0498a2 1013{
6315b5d3 1014 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6e3c9717 1015 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 1016 enum pipe pipe = crtc->pipe;
ab7ad7f6 1017
6315b5d3 1018 if (INTEL_GEN(dev_priv) >= 4) {
f0f59a00 1019 i915_reg_t reg = PIPECONF(cpu_transcoder);
ab7ad7f6
KP
1020
1021 /* Wait for the Pipe State to go off */
b8511f53
CW
1022 if (intel_wait_for_register(dev_priv,
1023 reg, I965_PIPECONF_ACTIVE, 0,
1024 100))
284637d9 1025 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1026 } else {
ab7ad7f6 1027 /* Wait for the display line to settle */
6315b5d3 1028 if (wait_for(pipe_dsl_stopped(dev_priv, pipe), 100))
284637d9 1029 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1030 }
79e53945
JB
1031}
1032
b24e7179 1033/* Only for pre-ILK configs */
55607e8a
DV
1034void assert_pll(struct drm_i915_private *dev_priv,
1035 enum pipe pipe, bool state)
b24e7179 1036{
b24e7179
JB
1037 u32 val;
1038 bool cur_state;
1039
649636ef 1040 val = I915_READ(DPLL(pipe));
b24e7179 1041 cur_state = !!(val & DPLL_VCO_ENABLE);
e2c719b7 1042 I915_STATE_WARN(cur_state != state,
b24e7179 1043 "PLL state assertion failure (expected %s, current %s)\n",
87ad3212 1044 onoff(state), onoff(cur_state));
b24e7179 1045}
b24e7179 1046
23538ef1 1047/* XXX: the dsi pll is shared between MIPI DSI ports */
8563b1e8 1048void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
23538ef1
JN
1049{
1050 u32 val;
1051 bool cur_state;
1052
a580516d 1053 mutex_lock(&dev_priv->sb_lock);
23538ef1 1054 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
a580516d 1055 mutex_unlock(&dev_priv->sb_lock);
23538ef1
JN
1056
1057 cur_state = val & DSI_PLL_VCO_EN;
e2c719b7 1058 I915_STATE_WARN(cur_state != state,
23538ef1 1059 "DSI PLL state assertion failure (expected %s, current %s)\n",
87ad3212 1060 onoff(state), onoff(cur_state));
23538ef1 1061}
23538ef1 1062
040484af
JB
1063static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1064 enum pipe pipe, bool state)
1065{
040484af 1066 bool cur_state;
ad80a810
PZ
1067 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1068 pipe);
040484af 1069
2d1fe073 1070 if (HAS_DDI(dev_priv)) {
affa9354 1071 /* DDI does not have a specific FDI_TX register */
649636ef 1072 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
ad80a810 1073 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
bf507ef7 1074 } else {
649636ef 1075 u32 val = I915_READ(FDI_TX_CTL(pipe));
bf507ef7
ED
1076 cur_state = !!(val & FDI_TX_ENABLE);
1077 }
e2c719b7 1078 I915_STATE_WARN(cur_state != state,
040484af 1079 "FDI TX state assertion failure (expected %s, current %s)\n",
87ad3212 1080 onoff(state), onoff(cur_state));
040484af
JB
1081}
1082#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1083#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1084
1085static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1086 enum pipe pipe, bool state)
1087{
040484af
JB
1088 u32 val;
1089 bool cur_state;
1090
649636ef 1091 val = I915_READ(FDI_RX_CTL(pipe));
d63fa0dc 1092 cur_state = !!(val & FDI_RX_ENABLE);
e2c719b7 1093 I915_STATE_WARN(cur_state != state,
040484af 1094 "FDI RX state assertion failure (expected %s, current %s)\n",
87ad3212 1095 onoff(state), onoff(cur_state));
040484af
JB
1096}
1097#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1098#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1099
1100static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1101 enum pipe pipe)
1102{
040484af
JB
1103 u32 val;
1104
1105 /* ILK FDI PLL is always enabled */
7e22dbbb 1106 if (IS_GEN5(dev_priv))
040484af
JB
1107 return;
1108
bf507ef7 1109 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
2d1fe073 1110 if (HAS_DDI(dev_priv))
bf507ef7
ED
1111 return;
1112
649636ef 1113 val = I915_READ(FDI_TX_CTL(pipe));
e2c719b7 1114 I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
040484af
JB
1115}
1116
55607e8a
DV
1117void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1118 enum pipe pipe, bool state)
040484af 1119{
040484af 1120 u32 val;
55607e8a 1121 bool cur_state;
040484af 1122
649636ef 1123 val = I915_READ(FDI_RX_CTL(pipe));
55607e8a 1124 cur_state = !!(val & FDI_RX_PLL_ENABLE);
e2c719b7 1125 I915_STATE_WARN(cur_state != state,
55607e8a 1126 "FDI RX PLL assertion failure (expected %s, current %s)\n",
87ad3212 1127 onoff(state), onoff(cur_state));
040484af
JB
1128}
1129
4f8036a2 1130void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
ea0760cf 1131{
f0f59a00 1132 i915_reg_t pp_reg;
ea0760cf
JB
1133 u32 val;
1134 enum pipe panel_pipe = PIPE_A;
0de3b485 1135 bool locked = true;
ea0760cf 1136
4f8036a2 1137 if (WARN_ON(HAS_DDI(dev_priv)))
bedd4dba
JN
1138 return;
1139
4f8036a2 1140 if (HAS_PCH_SPLIT(dev_priv)) {
bedd4dba
JN
1141 u32 port_sel;
1142
44cb734c
ID
1143 pp_reg = PP_CONTROL(0);
1144 port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
bedd4dba
JN
1145
1146 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1147 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1148 panel_pipe = PIPE_B;
1149 /* XXX: else fix for eDP */
4f8036a2 1150 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
bedd4dba 1151 /* presumably write lock depends on pipe, not port select */
44cb734c 1152 pp_reg = PP_CONTROL(pipe);
bedd4dba 1153 panel_pipe = pipe;
ea0760cf 1154 } else {
44cb734c 1155 pp_reg = PP_CONTROL(0);
bedd4dba
JN
1156 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1157 panel_pipe = PIPE_B;
ea0760cf
JB
1158 }
1159
1160 val = I915_READ(pp_reg);
1161 if (!(val & PANEL_POWER_ON) ||
ec49ba2d 1162 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
ea0760cf
JB
1163 locked = false;
1164
e2c719b7 1165 I915_STATE_WARN(panel_pipe == pipe && locked,
ea0760cf 1166 "panel assertion failure, pipe %c regs locked\n",
9db4a9c7 1167 pipe_name(pipe));
ea0760cf
JB
1168}
1169
93ce0ba6
JN
1170static void assert_cursor(struct drm_i915_private *dev_priv,
1171 enum pipe pipe, bool state)
1172{
93ce0ba6
JN
1173 bool cur_state;
1174
2a307c2e 1175 if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
0b87c24e 1176 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
d9d82081 1177 else
5efb3e28 1178 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
93ce0ba6 1179
e2c719b7 1180 I915_STATE_WARN(cur_state != state,
93ce0ba6 1181 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
87ad3212 1182 pipe_name(pipe), onoff(state), onoff(cur_state));
93ce0ba6
JN
1183}
1184#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1185#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1186
b840d907
JB
1187void assert_pipe(struct drm_i915_private *dev_priv,
1188 enum pipe pipe, bool state)
b24e7179 1189{
63d7bbe9 1190 bool cur_state;
702e7a56
PZ
1191 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1192 pipe);
4feed0eb 1193 enum intel_display_power_domain power_domain;
b24e7179 1194
b6b5d049
VS
1195 /* if we need the pipe quirk it must be always on */
1196 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1197 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
8e636784
DV
1198 state = true;
1199
4feed0eb
ID
1200 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1201 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
649636ef 1202 u32 val = I915_READ(PIPECONF(cpu_transcoder));
69310161 1203 cur_state = !!(val & PIPECONF_ENABLE);
4feed0eb
ID
1204
1205 intel_display_power_put(dev_priv, power_domain);
1206 } else {
1207 cur_state = false;
69310161
PZ
1208 }
1209
e2c719b7 1210 I915_STATE_WARN(cur_state != state,
63d7bbe9 1211 "pipe %c assertion failure (expected %s, current %s)\n",
87ad3212 1212 pipe_name(pipe), onoff(state), onoff(cur_state));
b24e7179
JB
1213}
1214
931872fc
CW
1215static void assert_plane(struct drm_i915_private *dev_priv,
1216 enum plane plane, bool state)
b24e7179 1217{
b24e7179 1218 u32 val;
931872fc 1219 bool cur_state;
b24e7179 1220
649636ef 1221 val = I915_READ(DSPCNTR(plane));
931872fc 1222 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
e2c719b7 1223 I915_STATE_WARN(cur_state != state,
931872fc 1224 "plane %c assertion failure (expected %s, current %s)\n",
87ad3212 1225 plane_name(plane), onoff(state), onoff(cur_state));
b24e7179
JB
1226}
1227
931872fc
CW
1228#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1229#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1230
b24e7179
JB
1231static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1232 enum pipe pipe)
1233{
649636ef 1234 int i;
b24e7179 1235
653e1026 1236 /* Primary planes are fixed to pipes on gen4+ */
6315b5d3 1237 if (INTEL_GEN(dev_priv) >= 4) {
649636ef 1238 u32 val = I915_READ(DSPCNTR(pipe));
e2c719b7 1239 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
28c05794
AJ
1240 "plane %c assertion failure, should be disabled but not\n",
1241 plane_name(pipe));
19ec1358 1242 return;
28c05794 1243 }
19ec1358 1244
b24e7179 1245 /* Need to check both planes against the pipe */
055e393f 1246 for_each_pipe(dev_priv, i) {
649636ef
VS
1247 u32 val = I915_READ(DSPCNTR(i));
1248 enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
b24e7179 1249 DISPPLANE_SEL_PIPE_SHIFT;
e2c719b7 1250 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
9db4a9c7
JB
1251 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1252 plane_name(i), pipe_name(pipe));
b24e7179
JB
1253 }
1254}
1255
19332d7a
JB
1256static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1257 enum pipe pipe)
1258{
649636ef 1259 int sprite;
19332d7a 1260
6315b5d3 1261 if (INTEL_GEN(dev_priv) >= 9) {
3bdcfc0c 1262 for_each_sprite(dev_priv, pipe, sprite) {
649636ef 1263 u32 val = I915_READ(PLANE_CTL(pipe, sprite));
e2c719b7 1264 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
7feb8b88
DL
1265 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1266 sprite, pipe_name(pipe));
1267 }
920a14b2 1268 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
3bdcfc0c 1269 for_each_sprite(dev_priv, pipe, sprite) {
83c04a62 1270 u32 val = I915_READ(SPCNTR(pipe, PLANE_SPRITE0 + sprite));
e2c719b7 1271 I915_STATE_WARN(val & SP_ENABLE,
20674eef 1272 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1fe47785 1273 sprite_name(pipe, sprite), pipe_name(pipe));
20674eef 1274 }
6315b5d3 1275 } else if (INTEL_GEN(dev_priv) >= 7) {
649636ef 1276 u32 val = I915_READ(SPRCTL(pipe));
e2c719b7 1277 I915_STATE_WARN(val & SPRITE_ENABLE,
06da8da2 1278 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef 1279 plane_name(pipe), pipe_name(pipe));
6315b5d3 1280 } else if (INTEL_GEN(dev_priv) >= 5) {
649636ef 1281 u32 val = I915_READ(DVSCNTR(pipe));
e2c719b7 1282 I915_STATE_WARN(val & DVS_ENABLE,
06da8da2 1283 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef 1284 plane_name(pipe), pipe_name(pipe));
19332d7a
JB
1285 }
1286}
1287
08c71e5e
VS
1288static void assert_vblank_disabled(struct drm_crtc *crtc)
1289{
e2c719b7 1290 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
08c71e5e
VS
1291 drm_crtc_vblank_put(crtc);
1292}
1293
7abd4b35
ACO
1294void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1295 enum pipe pipe)
92f2584a 1296{
92f2584a
JB
1297 u32 val;
1298 bool enabled;
1299
649636ef 1300 val = I915_READ(PCH_TRANSCONF(pipe));
92f2584a 1301 enabled = !!(val & TRANS_ENABLE);
e2c719b7 1302 I915_STATE_WARN(enabled,
9db4a9c7
JB
1303 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1304 pipe_name(pipe));
92f2584a
JB
1305}
1306
4e634389
KP
1307static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1308 enum pipe pipe, u32 port_sel, u32 val)
f0575e92
KP
1309{
1310 if ((val & DP_PORT_EN) == 0)
1311 return false;
1312
2d1fe073 1313 if (HAS_PCH_CPT(dev_priv)) {
f0f59a00 1314 u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
f0575e92
KP
1315 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1316 return false;
2d1fe073 1317 } else if (IS_CHERRYVIEW(dev_priv)) {
44f37d1f
CML
1318 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1319 return false;
f0575e92
KP
1320 } else {
1321 if ((val & DP_PIPE_MASK) != (pipe << 30))
1322 return false;
1323 }
1324 return true;
1325}
1326
1519b995
KP
1327static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1328 enum pipe pipe, u32 val)
1329{
dc0fa718 1330 if ((val & SDVO_ENABLE) == 0)
1519b995
KP
1331 return false;
1332
2d1fe073 1333 if (HAS_PCH_CPT(dev_priv)) {
dc0fa718 1334 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1519b995 1335 return false;
2d1fe073 1336 } else if (IS_CHERRYVIEW(dev_priv)) {
44f37d1f
CML
1337 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1338 return false;
1519b995 1339 } else {
dc0fa718 1340 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1519b995
KP
1341 return false;
1342 }
1343 return true;
1344}
1345
1346static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1347 enum pipe pipe, u32 val)
1348{
1349 if ((val & LVDS_PORT_EN) == 0)
1350 return false;
1351
2d1fe073 1352 if (HAS_PCH_CPT(dev_priv)) {
1519b995
KP
1353 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1354 return false;
1355 } else {
1356 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1357 return false;
1358 }
1359 return true;
1360}
1361
1362static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1363 enum pipe pipe, u32 val)
1364{
1365 if ((val & ADPA_DAC_ENABLE) == 0)
1366 return false;
2d1fe073 1367 if (HAS_PCH_CPT(dev_priv)) {
1519b995
KP
1368 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1369 return false;
1370 } else {
1371 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1372 return false;
1373 }
1374 return true;
1375}
1376
291906f1 1377static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
f0f59a00
VS
1378 enum pipe pipe, i915_reg_t reg,
1379 u32 port_sel)
291906f1 1380{
47a05eca 1381 u32 val = I915_READ(reg);
e2c719b7 1382 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
291906f1 1383 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
f0f59a00 1384 i915_mmio_reg_offset(reg), pipe_name(pipe));
de9a35ab 1385
2d1fe073 1386 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
75c5da27 1387 && (val & DP_PIPEB_SELECT),
de9a35ab 1388 "IBX PCH dp port still using transcoder B\n");
291906f1
JB
1389}
1390
1391static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
f0f59a00 1392 enum pipe pipe, i915_reg_t reg)
291906f1 1393{
47a05eca 1394 u32 val = I915_READ(reg);
e2c719b7 1395 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
23c99e77 1396 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
f0f59a00 1397 i915_mmio_reg_offset(reg), pipe_name(pipe));
de9a35ab 1398
2d1fe073 1399 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
75c5da27 1400 && (val & SDVO_PIPE_B_SELECT),
de9a35ab 1401 "IBX PCH hdmi port still using transcoder B\n");
291906f1
JB
1402}
1403
1404static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1405 enum pipe pipe)
1406{
291906f1 1407 u32 val;
291906f1 1408
f0575e92
KP
1409 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1410 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1411 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
291906f1 1412
649636ef 1413 val = I915_READ(PCH_ADPA);
e2c719b7 1414 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
291906f1 1415 "PCH VGA enabled on transcoder %c, should be disabled\n",
9db4a9c7 1416 pipe_name(pipe));
291906f1 1417
649636ef 1418 val = I915_READ(PCH_LVDS);
e2c719b7 1419 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
291906f1 1420 "PCH LVDS enabled on transcoder %c, should be disabled\n",
9db4a9c7 1421 pipe_name(pipe));
291906f1 1422
e2debe91
PZ
1423 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1424 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1425 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
291906f1
JB
1426}
1427
cd2d34d9
VS
1428static void _vlv_enable_pll(struct intel_crtc *crtc,
1429 const struct intel_crtc_state *pipe_config)
1430{
1431 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1432 enum pipe pipe = crtc->pipe;
1433
1434 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1435 POSTING_READ(DPLL(pipe));
1436 udelay(150);
1437
2c30b43b
CW
1438 if (intel_wait_for_register(dev_priv,
1439 DPLL(pipe),
1440 DPLL_LOCK_VLV,
1441 DPLL_LOCK_VLV,
1442 1))
cd2d34d9
VS
1443 DRM_ERROR("DPLL %d failed to lock\n", pipe);
1444}
1445
d288f65f 1446static void vlv_enable_pll(struct intel_crtc *crtc,
5cec258b 1447 const struct intel_crtc_state *pipe_config)
87442f73 1448{
cd2d34d9 1449 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8bd3f301 1450 enum pipe pipe = crtc->pipe;
87442f73 1451
8bd3f301 1452 assert_pipe_disabled(dev_priv, pipe);
87442f73 1453
87442f73 1454 /* PLL is protected by panel, make sure we can write it */
7d1a83cb 1455 assert_panel_unlocked(dev_priv, pipe);
87442f73 1456
cd2d34d9
VS
1457 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1458 _vlv_enable_pll(crtc, pipe_config);
426115cf 1459
8bd3f301
VS
1460 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1461 POSTING_READ(DPLL_MD(pipe));
87442f73
DV
1462}
1463
cd2d34d9
VS
1464
1465static void _chv_enable_pll(struct intel_crtc *crtc,
1466 const struct intel_crtc_state *pipe_config)
9d556c99 1467{
cd2d34d9 1468 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8bd3f301 1469 enum pipe pipe = crtc->pipe;
9d556c99 1470 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9d556c99
CML
1471 u32 tmp;
1472
a580516d 1473 mutex_lock(&dev_priv->sb_lock);
9d556c99
CML
1474
1475 /* Enable back the 10bit clock to display controller */
1476 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1477 tmp |= DPIO_DCLKP_EN;
1478 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1479
54433e91
VS
1480 mutex_unlock(&dev_priv->sb_lock);
1481
9d556c99
CML
1482 /*
1483 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1484 */
1485 udelay(1);
1486
1487 /* Enable PLL */
d288f65f 1488 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
9d556c99
CML
1489
1490 /* Check PLL is locked */
6b18826a
CW
1491 if (intel_wait_for_register(dev_priv,
1492 DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
1493 1))
9d556c99 1494 DRM_ERROR("PLL %d failed to lock\n", pipe);
cd2d34d9
VS
1495}
1496
1497static void chv_enable_pll(struct intel_crtc *crtc,
1498 const struct intel_crtc_state *pipe_config)
1499{
1500 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1501 enum pipe pipe = crtc->pipe;
1502
1503 assert_pipe_disabled(dev_priv, pipe);
1504
1505 /* PLL is protected by panel, make sure we can write it */
1506 assert_panel_unlocked(dev_priv, pipe);
1507
1508 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1509 _chv_enable_pll(crtc, pipe_config);
9d556c99 1510
c231775c
VS
1511 if (pipe != PIPE_A) {
1512 /*
1513 * WaPixelRepeatModeFixForC0:chv
1514 *
1515 * DPLLCMD is AWOL. Use chicken bits to propagate
1516 * the value from DPLLBMD to either pipe B or C.
1517 */
1518 I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
1519 I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
1520 I915_WRITE(CBR4_VLV, 0);
1521 dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
1522
1523 /*
1524 * DPLLB VGA mode also seems to cause problems.
1525 * We should always have it disabled.
1526 */
1527 WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
1528 } else {
1529 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1530 POSTING_READ(DPLL_MD(pipe));
1531 }
9d556c99
CML
1532}
1533
6315b5d3 1534static int intel_num_dvo_pipes(struct drm_i915_private *dev_priv)
1c4e0274
VS
1535{
1536 struct intel_crtc *crtc;
1537 int count = 0;
1538
6315b5d3 1539 for_each_intel_crtc(&dev_priv->drm, crtc) {
3538b9df 1540 count += crtc->base.state->active &&
2d84d2b3
VS
1541 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
1542 }
1c4e0274
VS
1543
1544 return count;
1545}
1546
66e3d5c0 1547static void i9xx_enable_pll(struct intel_crtc *crtc)
63d7bbe9 1548{
6315b5d3 1549 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
f0f59a00 1550 i915_reg_t reg = DPLL(crtc->pipe);
6e3c9717 1551 u32 dpll = crtc->config->dpll_hw_state.dpll;
63d7bbe9 1552
66e3d5c0 1553 assert_pipe_disabled(dev_priv, crtc->pipe);
58c6eaa2 1554
63d7bbe9 1555 /* PLL is protected by panel, make sure we can write it */
50a0bc90 1556 if (IS_MOBILE(dev_priv) && !IS_I830(dev_priv))
66e3d5c0 1557 assert_panel_unlocked(dev_priv, crtc->pipe);
63d7bbe9 1558
1c4e0274 1559 /* Enable DVO 2x clock on both PLLs if necessary */
6315b5d3 1560 if (IS_I830(dev_priv) && intel_num_dvo_pipes(dev_priv) > 0) {
1c4e0274
VS
1561 /*
1562 * It appears to be important that we don't enable this
1563 * for the current pipe before otherwise configuring the
1564 * PLL. No idea how this should be handled if multiple
1565 * DVO outputs are enabled simultaneosly.
1566 */
1567 dpll |= DPLL_DVO_2X_MODE;
1568 I915_WRITE(DPLL(!crtc->pipe),
1569 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1570 }
66e3d5c0 1571
c2b63374
VS
1572 /*
1573 * Apparently we need to have VGA mode enabled prior to changing
1574 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1575 * dividers, even though the register value does change.
1576 */
1577 I915_WRITE(reg, 0);
1578
8e7a65aa
VS
1579 I915_WRITE(reg, dpll);
1580
66e3d5c0
DV
1581 /* Wait for the clocks to stabilize. */
1582 POSTING_READ(reg);
1583 udelay(150);
1584
6315b5d3 1585 if (INTEL_GEN(dev_priv) >= 4) {
66e3d5c0 1586 I915_WRITE(DPLL_MD(crtc->pipe),
6e3c9717 1587 crtc->config->dpll_hw_state.dpll_md);
66e3d5c0
DV
1588 } else {
1589 /* The pixel multiplier can only be updated once the
1590 * DPLL is enabled and the clocks are stable.
1591 *
1592 * So write it again.
1593 */
1594 I915_WRITE(reg, dpll);
1595 }
63d7bbe9
JB
1596
1597 /* We do this three times for luck */
66e3d5c0 1598 I915_WRITE(reg, dpll);
63d7bbe9
JB
1599 POSTING_READ(reg);
1600 udelay(150); /* wait for warmup */
66e3d5c0 1601 I915_WRITE(reg, dpll);
63d7bbe9
JB
1602 POSTING_READ(reg);
1603 udelay(150); /* wait for warmup */
66e3d5c0 1604 I915_WRITE(reg, dpll);
63d7bbe9
JB
1605 POSTING_READ(reg);
1606 udelay(150); /* wait for warmup */
1607}
1608
1609/**
50b44a44 1610 * i9xx_disable_pll - disable a PLL
63d7bbe9
JB
1611 * @dev_priv: i915 private structure
1612 * @pipe: pipe PLL to disable
1613 *
1614 * Disable the PLL for @pipe, making sure the pipe is off first.
1615 *
1616 * Note! This is for pre-ILK only.
1617 */
1c4e0274 1618static void i9xx_disable_pll(struct intel_crtc *crtc)
63d7bbe9 1619{
6315b5d3 1620 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1c4e0274
VS
1621 enum pipe pipe = crtc->pipe;
1622
1623 /* Disable DVO 2x clock on both PLLs if necessary */
50a0bc90 1624 if (IS_I830(dev_priv) &&
2d84d2b3 1625 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
6315b5d3 1626 !intel_num_dvo_pipes(dev_priv)) {
1c4e0274
VS
1627 I915_WRITE(DPLL(PIPE_B),
1628 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1629 I915_WRITE(DPLL(PIPE_A),
1630 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1631 }
1632
b6b5d049
VS
1633 /* Don't disable pipe or pipe PLLs if needed */
1634 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1635 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
63d7bbe9
JB
1636 return;
1637
1638 /* Make sure the pipe isn't still relying on us */
1639 assert_pipe_disabled(dev_priv, pipe);
1640
b8afb911 1641 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
50b44a44 1642 POSTING_READ(DPLL(pipe));
63d7bbe9
JB
1643}
1644
f6071166
JB
1645static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1646{
b8afb911 1647 u32 val;
f6071166
JB
1648
1649 /* Make sure the pipe isn't still relying on us */
1650 assert_pipe_disabled(dev_priv, pipe);
1651
03ed5cbf
VS
1652 val = DPLL_INTEGRATED_REF_CLK_VLV |
1653 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1654 if (pipe != PIPE_A)
1655 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1656
f6071166
JB
1657 I915_WRITE(DPLL(pipe), val);
1658 POSTING_READ(DPLL(pipe));
076ed3b2
CML
1659}
1660
1661static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1662{
d752048d 1663 enum dpio_channel port = vlv_pipe_to_channel(pipe);
076ed3b2
CML
1664 u32 val;
1665
a11b0703
VS
1666 /* Make sure the pipe isn't still relying on us */
1667 assert_pipe_disabled(dev_priv, pipe);
076ed3b2 1668
60bfe44f
VS
1669 val = DPLL_SSC_REF_CLK_CHV |
1670 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
a11b0703
VS
1671 if (pipe != PIPE_A)
1672 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
03ed5cbf 1673
a11b0703
VS
1674 I915_WRITE(DPLL(pipe), val);
1675 POSTING_READ(DPLL(pipe));
d752048d 1676
a580516d 1677 mutex_lock(&dev_priv->sb_lock);
d752048d
VS
1678
1679 /* Disable 10bit clock to display controller */
1680 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1681 val &= ~DPIO_DCLKP_EN;
1682 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1683
a580516d 1684 mutex_unlock(&dev_priv->sb_lock);
f6071166
JB
1685}
1686
e4607fcf 1687void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
9b6de0a1
VS
1688 struct intel_digital_port *dport,
1689 unsigned int expected_mask)
89b667f8
JB
1690{
1691 u32 port_mask;
f0f59a00 1692 i915_reg_t dpll_reg;
89b667f8 1693
e4607fcf
CML
1694 switch (dport->port) {
1695 case PORT_B:
89b667f8 1696 port_mask = DPLL_PORTB_READY_MASK;
00fc31b7 1697 dpll_reg = DPLL(0);
e4607fcf
CML
1698 break;
1699 case PORT_C:
89b667f8 1700 port_mask = DPLL_PORTC_READY_MASK;
00fc31b7 1701 dpll_reg = DPLL(0);
9b6de0a1 1702 expected_mask <<= 4;
00fc31b7
CML
1703 break;
1704 case PORT_D:
1705 port_mask = DPLL_PORTD_READY_MASK;
1706 dpll_reg = DPIO_PHY_STATUS;
e4607fcf
CML
1707 break;
1708 default:
1709 BUG();
1710 }
89b667f8 1711
370004d3
CW
1712 if (intel_wait_for_register(dev_priv,
1713 dpll_reg, port_mask, expected_mask,
1714 1000))
9b6de0a1
VS
1715 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1716 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
89b667f8
JB
1717}
1718
b8a4f404
PZ
1719static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1720 enum pipe pipe)
040484af 1721{
98187836
VS
1722 struct intel_crtc *intel_crtc = intel_get_crtc_for_pipe(dev_priv,
1723 pipe);
f0f59a00
VS
1724 i915_reg_t reg;
1725 uint32_t val, pipeconf_val;
040484af 1726
040484af 1727 /* Make sure PCH DPLL is enabled */
8106ddbd 1728 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
040484af
JB
1729
1730 /* FDI must be feeding us bits for PCH ports */
1731 assert_fdi_tx_enabled(dev_priv, pipe);
1732 assert_fdi_rx_enabled(dev_priv, pipe);
1733
6e266956 1734 if (HAS_PCH_CPT(dev_priv)) {
23670b32
DV
1735 /* Workaround: Set the timing override bit before enabling the
1736 * pch transcoder. */
1737 reg = TRANS_CHICKEN2(pipe);
1738 val = I915_READ(reg);
1739 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1740 I915_WRITE(reg, val);
59c859d6 1741 }
23670b32 1742
ab9412ba 1743 reg = PCH_TRANSCONF(pipe);
040484af 1744 val = I915_READ(reg);
5f7f726d 1745 pipeconf_val = I915_READ(PIPECONF(pipe));
e9bcff5c 1746
2d1fe073 1747 if (HAS_PCH_IBX(dev_priv)) {
e9bcff5c 1748 /*
c5de7c6f
VS
1749 * Make the BPC in transcoder be consistent with
1750 * that in pipeconf reg. For HDMI we must use 8bpc
1751 * here for both 8bpc and 12bpc.
e9bcff5c 1752 */
dfd07d72 1753 val &= ~PIPECONF_BPC_MASK;
2d84d2b3 1754 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
c5de7c6f
VS
1755 val |= PIPECONF_8BPC;
1756 else
1757 val |= pipeconf_val & PIPECONF_BPC_MASK;
e9bcff5c 1758 }
5f7f726d
PZ
1759
1760 val &= ~TRANS_INTERLACE_MASK;
1761 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
2d1fe073 1762 if (HAS_PCH_IBX(dev_priv) &&
2d84d2b3 1763 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
7c26e5c6
PZ
1764 val |= TRANS_LEGACY_INTERLACED_ILK;
1765 else
1766 val |= TRANS_INTERLACED;
5f7f726d
PZ
1767 else
1768 val |= TRANS_PROGRESSIVE;
1769
040484af 1770 I915_WRITE(reg, val | TRANS_ENABLE);
650fbd84
CW
1771 if (intel_wait_for_register(dev_priv,
1772 reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
1773 100))
4bb6f1f3 1774 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
040484af
JB
1775}
1776
8fb033d7 1777static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
937bb610 1778 enum transcoder cpu_transcoder)
040484af 1779{
8fb033d7 1780 u32 val, pipeconf_val;
8fb033d7 1781
8fb033d7 1782 /* FDI must be feeding us bits for PCH ports */
1a240d4d 1783 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
937bb610 1784 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
8fb033d7 1785
223a6fdf 1786 /* Workaround: set timing override bit. */
36c0d0cf 1787 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
23670b32 1788 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
36c0d0cf 1789 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
223a6fdf 1790
25f3ef11 1791 val = TRANS_ENABLE;
937bb610 1792 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
8fb033d7 1793
9a76b1c6
PZ
1794 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1795 PIPECONF_INTERLACED_ILK)
a35f2679 1796 val |= TRANS_INTERLACED;
8fb033d7
PZ
1797 else
1798 val |= TRANS_PROGRESSIVE;
1799
ab9412ba 1800 I915_WRITE(LPT_TRANSCONF, val);
d9f96244
CW
1801 if (intel_wait_for_register(dev_priv,
1802 LPT_TRANSCONF,
1803 TRANS_STATE_ENABLE,
1804 TRANS_STATE_ENABLE,
1805 100))
937bb610 1806 DRM_ERROR("Failed to enable PCH transcoder\n");
8fb033d7
PZ
1807}
1808
b8a4f404
PZ
1809static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1810 enum pipe pipe)
040484af 1811{
f0f59a00
VS
1812 i915_reg_t reg;
1813 uint32_t val;
040484af
JB
1814
1815 /* FDI relies on the transcoder */
1816 assert_fdi_tx_disabled(dev_priv, pipe);
1817 assert_fdi_rx_disabled(dev_priv, pipe);
1818
291906f1
JB
1819 /* Ports must be off as well */
1820 assert_pch_ports_disabled(dev_priv, pipe);
1821
ab9412ba 1822 reg = PCH_TRANSCONF(pipe);
040484af
JB
1823 val = I915_READ(reg);
1824 val &= ~TRANS_ENABLE;
1825 I915_WRITE(reg, val);
1826 /* wait for PCH transcoder off, transcoder state */
a7d04662
CW
1827 if (intel_wait_for_register(dev_priv,
1828 reg, TRANS_STATE_ENABLE, 0,
1829 50))
4bb6f1f3 1830 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
23670b32 1831
6e266956 1832 if (HAS_PCH_CPT(dev_priv)) {
23670b32
DV
1833 /* Workaround: Clear the timing override chicken bit again. */
1834 reg = TRANS_CHICKEN2(pipe);
1835 val = I915_READ(reg);
1836 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1837 I915_WRITE(reg, val);
1838 }
040484af
JB
1839}
1840
b7076546 1841void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
8fb033d7 1842{
8fb033d7
PZ
1843 u32 val;
1844
ab9412ba 1845 val = I915_READ(LPT_TRANSCONF);
8fb033d7 1846 val &= ~TRANS_ENABLE;
ab9412ba 1847 I915_WRITE(LPT_TRANSCONF, val);
8fb033d7 1848 /* wait for PCH transcoder off, transcoder state */
dfdb4749
CW
1849 if (intel_wait_for_register(dev_priv,
1850 LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
1851 50))
8a52fd9f 1852 DRM_ERROR("Failed to disable PCH transcoder\n");
223a6fdf
PZ
1853
1854 /* Workaround: clear timing override bit. */
36c0d0cf 1855 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
23670b32 1856 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
36c0d0cf 1857 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
040484af
JB
1858}
1859
65f2130c
VS
1860enum transcoder intel_crtc_pch_transcoder(struct intel_crtc *crtc)
1861{
1862 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1863
1864 WARN_ON(!crtc->config->has_pch_encoder);
1865
1866 if (HAS_PCH_LPT(dev_priv))
1867 return TRANSCODER_A;
1868 else
1869 return (enum transcoder) crtc->pipe;
1870}
1871
b24e7179 1872/**
309cfea8 1873 * intel_enable_pipe - enable a pipe, asserting requirements
0372264a 1874 * @crtc: crtc responsible for the pipe
b24e7179 1875 *
0372264a 1876 * Enable @crtc's pipe, making sure that various hardware specific requirements
b24e7179 1877 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
b24e7179 1878 */
e1fdc473 1879static void intel_enable_pipe(struct intel_crtc *crtc)
b24e7179 1880{
0372264a 1881 struct drm_device *dev = crtc->base.dev;
fac5e23e 1882 struct drm_i915_private *dev_priv = to_i915(dev);
0372264a 1883 enum pipe pipe = crtc->pipe;
1a70a728 1884 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
f0f59a00 1885 i915_reg_t reg;
b24e7179
JB
1886 u32 val;
1887
9e2ee2dd
VS
1888 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1889
58c6eaa2 1890 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 1891 assert_cursor_disabled(dev_priv, pipe);
58c6eaa2
DV
1892 assert_sprites_disabled(dev_priv, pipe);
1893
b24e7179
JB
1894 /*
1895 * A pipe without a PLL won't actually be able to drive bits from
1896 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1897 * need the check.
1898 */
09fa8bb9 1899 if (HAS_GMCH_DISPLAY(dev_priv)) {
d7edc4e5 1900 if (intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DSI))
23538ef1
JN
1901 assert_dsi_pll_enabled(dev_priv);
1902 else
1903 assert_pll_enabled(dev_priv, pipe);
09fa8bb9 1904 } else {
6e3c9717 1905 if (crtc->config->has_pch_encoder) {
040484af 1906 /* if driving the PCH, we need FDI enabled */
65f2130c
VS
1907 assert_fdi_rx_pll_enabled(dev_priv,
1908 (enum pipe) intel_crtc_pch_transcoder(crtc));
1a240d4d
DV
1909 assert_fdi_tx_pll_enabled(dev_priv,
1910 (enum pipe) cpu_transcoder);
040484af
JB
1911 }
1912 /* FIXME: assert CPU port conditions for SNB+ */
1913 }
b24e7179 1914
702e7a56 1915 reg = PIPECONF(cpu_transcoder);
b24e7179 1916 val = I915_READ(reg);
7ad25d48 1917 if (val & PIPECONF_ENABLE) {
b6b5d049
VS
1918 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1919 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
00d70b15 1920 return;
7ad25d48 1921 }
00d70b15
CW
1922
1923 I915_WRITE(reg, val | PIPECONF_ENABLE);
851855d8 1924 POSTING_READ(reg);
b7792d8b
VS
1925
1926 /*
1927 * Until the pipe starts DSL will read as 0, which would cause
1928 * an apparent vblank timestamp jump, which messes up also the
1929 * frame count when it's derived from the timestamps. So let's
1930 * wait for the pipe to start properly before we call
1931 * drm_crtc_vblank_on()
1932 */
1933 if (dev->max_vblank_count == 0 &&
1934 wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
1935 DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
b24e7179
JB
1936}
1937
1938/**
309cfea8 1939 * intel_disable_pipe - disable a pipe, asserting requirements
575f7ab7 1940 * @crtc: crtc whose pipes is to be disabled
b24e7179 1941 *
575f7ab7
VS
1942 * Disable the pipe of @crtc, making sure that various hardware
1943 * specific requirements are met, if applicable, e.g. plane
1944 * disabled, panel fitter off, etc.
b24e7179
JB
1945 *
1946 * Will wait until the pipe has shut down before returning.
1947 */
575f7ab7 1948static void intel_disable_pipe(struct intel_crtc *crtc)
b24e7179 1949{
fac5e23e 1950 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6e3c9717 1951 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 1952 enum pipe pipe = crtc->pipe;
f0f59a00 1953 i915_reg_t reg;
b24e7179
JB
1954 u32 val;
1955
9e2ee2dd
VS
1956 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
1957
b24e7179
JB
1958 /*
1959 * Make sure planes won't keep trying to pump pixels to us,
1960 * or we might hang the display.
1961 */
1962 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 1963 assert_cursor_disabled(dev_priv, pipe);
19332d7a 1964 assert_sprites_disabled(dev_priv, pipe);
b24e7179 1965
702e7a56 1966 reg = PIPECONF(cpu_transcoder);
b24e7179 1967 val = I915_READ(reg);
00d70b15
CW
1968 if ((val & PIPECONF_ENABLE) == 0)
1969 return;
1970
67adc644
VS
1971 /*
1972 * Double wide has implications for planes
1973 * so best keep it disabled when not needed.
1974 */
6e3c9717 1975 if (crtc->config->double_wide)
67adc644
VS
1976 val &= ~PIPECONF_DOUBLE_WIDE;
1977
1978 /* Don't disable pipe or pipe PLLs if needed */
b6b5d049
VS
1979 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
1980 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
67adc644
VS
1981 val &= ~PIPECONF_ENABLE;
1982
1983 I915_WRITE(reg, val);
1984 if ((val & PIPECONF_ENABLE) == 0)
1985 intel_wait_for_pipe_off(crtc);
b24e7179
JB
1986}
1987
832be82f
VS
1988static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
1989{
1990 return IS_GEN2(dev_priv) ? 2048 : 4096;
1991}
1992
27ba3910
VS
1993static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
1994 uint64_t fb_modifier, unsigned int cpp)
7b49f948
VS
1995{
1996 switch (fb_modifier) {
1997 case DRM_FORMAT_MOD_NONE:
1998 return cpp;
1999 case I915_FORMAT_MOD_X_TILED:
2000 if (IS_GEN2(dev_priv))
2001 return 128;
2002 else
2003 return 512;
2004 case I915_FORMAT_MOD_Y_TILED:
2005 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2006 return 128;
2007 else
2008 return 512;
2009 case I915_FORMAT_MOD_Yf_TILED:
2010 switch (cpp) {
2011 case 1:
2012 return 64;
2013 case 2:
2014 case 4:
2015 return 128;
2016 case 8:
2017 case 16:
2018 return 256;
2019 default:
2020 MISSING_CASE(cpp);
2021 return cpp;
2022 }
2023 break;
2024 default:
2025 MISSING_CASE(fb_modifier);
2026 return cpp;
2027 }
2028}
2029
832be82f
VS
2030unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
2031 uint64_t fb_modifier, unsigned int cpp)
a57ce0b2 2032{
832be82f
VS
2033 if (fb_modifier == DRM_FORMAT_MOD_NONE)
2034 return 1;
2035 else
2036 return intel_tile_size(dev_priv) /
27ba3910 2037 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
6761dd31
TU
2038}
2039
8d0deca8
VS
2040/* Return the tile dimensions in pixel units */
2041static void intel_tile_dims(const struct drm_i915_private *dev_priv,
2042 unsigned int *tile_width,
2043 unsigned int *tile_height,
2044 uint64_t fb_modifier,
2045 unsigned int cpp)
2046{
2047 unsigned int tile_width_bytes =
2048 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
2049
2050 *tile_width = tile_width_bytes / cpp;
2051 *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
2052}
2053
6761dd31 2054unsigned int
24dbf51a
CW
2055intel_fb_align_height(struct drm_i915_private *dev_priv,
2056 unsigned int height,
2057 uint32_t pixel_format,
2058 uint64_t fb_modifier)
6761dd31 2059{
832be82f 2060 unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
24dbf51a 2061 unsigned int tile_height = intel_tile_height(dev_priv, fb_modifier, cpp);
832be82f
VS
2062
2063 return ALIGN(height, tile_height);
a57ce0b2
JB
2064}
2065
1663b9d6
VS
2066unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2067{
2068 unsigned int size = 0;
2069 int i;
2070
2071 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2072 size += rot_info->plane[i].width * rot_info->plane[i].height;
2073
2074 return size;
2075}
2076
75c82a53 2077static void
3465c580
VS
2078intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2079 const struct drm_framebuffer *fb,
2080 unsigned int rotation)
f64b98cd 2081{
7b92c047 2082 view->type = I915_GGTT_VIEW_NORMAL;
bd2ef25d 2083 if (drm_rotation_90_or_270(rotation)) {
7b92c047 2084 view->type = I915_GGTT_VIEW_ROTATED;
8bab1193 2085 view->rotated = to_intel_framebuffer(fb)->rot_info;
2d7a215f
VS
2086 }
2087}
50470bb0 2088
603525d7 2089static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
4e9a86b6
VS
2090{
2091 if (INTEL_INFO(dev_priv)->gen >= 9)
2092 return 256 * 1024;
c0f86832 2093 else if (IS_I965G(dev_priv) || IS_I965GM(dev_priv) ||
666a4537 2094 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
4e9a86b6
VS
2095 return 128 * 1024;
2096 else if (INTEL_INFO(dev_priv)->gen >= 4)
2097 return 4 * 1024;
2098 else
44c5905e 2099 return 0;
4e9a86b6
VS
2100}
2101
603525d7
VS
2102static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
2103 uint64_t fb_modifier)
2104{
2105 switch (fb_modifier) {
2106 case DRM_FORMAT_MOD_NONE:
2107 return intel_linear_alignment(dev_priv);
2108 case I915_FORMAT_MOD_X_TILED:
2109 if (INTEL_INFO(dev_priv)->gen >= 9)
2110 return 256 * 1024;
2111 return 0;
2112 case I915_FORMAT_MOD_Y_TILED:
2113 case I915_FORMAT_MOD_Yf_TILED:
2114 return 1 * 1024 * 1024;
2115 default:
2116 MISSING_CASE(fb_modifier);
2117 return 0;
2118 }
2119}
2120
058d88c4
CW
2121struct i915_vma *
2122intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
6b95a207 2123{
850c4cdc 2124 struct drm_device *dev = fb->dev;
fac5e23e 2125 struct drm_i915_private *dev_priv = to_i915(dev);
850c4cdc 2126 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2127 struct i915_ggtt_view view;
058d88c4 2128 struct i915_vma *vma;
6b95a207 2129 u32 alignment;
6b95a207 2130
ebcdd39e
MR
2131 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2132
bae781b2 2133 alignment = intel_surf_alignment(dev_priv, fb->modifier);
6b95a207 2134
3465c580 2135 intel_fill_fb_ggtt_view(&view, fb, rotation);
f64b98cd 2136
693db184
CW
2137 /* Note that the w/a also requires 64 PTE of padding following the
2138 * bo. We currently fill all unused PTE with the shadow page and so
2139 * we should always have valid PTE following the scanout preventing
2140 * the VT-d warning.
2141 */
48f112fe 2142 if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
693db184
CW
2143 alignment = 256 * 1024;
2144
d6dd6843
PZ
2145 /*
2146 * Global gtt pte registers are special registers which actually forward
2147 * writes to a chunk of system memory. Which means that there is no risk
2148 * that the register values disappear as soon as we call
2149 * intel_runtime_pm_put(), so it is correct to wrap only the
2150 * pin/unpin/fence and not more.
2151 */
2152 intel_runtime_pm_get(dev_priv);
2153
058d88c4 2154 vma = i915_gem_object_pin_to_display_plane(obj, alignment, &view);
49ef5294
CW
2155 if (IS_ERR(vma))
2156 goto err;
6b95a207 2157
05a20d09 2158 if (i915_vma_is_map_and_fenceable(vma)) {
49ef5294
CW
2159 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2160 * fence, whereas 965+ only requires a fence if using
2161 * framebuffer compression. For simplicity, we always, when
2162 * possible, install a fence as the cost is not that onerous.
2163 *
2164 * If we fail to fence the tiled scanout, then either the
2165 * modeset will reject the change (which is highly unlikely as
2166 * the affected systems, all but one, do not have unmappable
2167 * space) or we will not be able to enable full powersaving
2168 * techniques (also likely not to apply due to various limits
2169 * FBC and the like impose on the size of the buffer, which
2170 * presumably we violated anyway with this unmappable buffer).
2171 * Anyway, it is presumably better to stumble onwards with
2172 * something and try to run the system in a "less than optimal"
2173 * mode that matches the user configuration.
2174 */
2175 if (i915_vma_get_fence(vma) == 0)
2176 i915_vma_pin_fence(vma);
9807216f 2177 }
6b95a207 2178
be1e3415 2179 i915_vma_get(vma);
49ef5294 2180err:
d6dd6843 2181 intel_runtime_pm_put(dev_priv);
058d88c4 2182 return vma;
6b95a207
KH
2183}
2184
be1e3415 2185void intel_unpin_fb_vma(struct i915_vma *vma)
1690e1eb 2186{
be1e3415 2187 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
f64b98cd 2188
49ef5294 2189 i915_vma_unpin_fence(vma);
058d88c4 2190 i915_gem_object_unpin_from_display_plane(vma);
be1e3415 2191 i915_vma_put(vma);
1690e1eb
CW
2192}
2193
ef78ec94
VS
2194static int intel_fb_pitch(const struct drm_framebuffer *fb, int plane,
2195 unsigned int rotation)
2196{
bd2ef25d 2197 if (drm_rotation_90_or_270(rotation))
ef78ec94
VS
2198 return to_intel_framebuffer(fb)->rotated[plane].pitch;
2199 else
2200 return fb->pitches[plane];
2201}
2202
6687c906
VS
2203/*
2204 * Convert the x/y offsets into a linear offset.
2205 * Only valid with 0/180 degree rotation, which is fine since linear
2206 * offset is only used with linear buffers on pre-hsw and tiled buffers
2207 * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
2208 */
2209u32 intel_fb_xy_to_linear(int x, int y,
2949056c
VS
2210 const struct intel_plane_state *state,
2211 int plane)
6687c906 2212{
2949056c 2213 const struct drm_framebuffer *fb = state->base.fb;
353c8598 2214 unsigned int cpp = fb->format->cpp[plane];
6687c906
VS
2215 unsigned int pitch = fb->pitches[plane];
2216
2217 return y * pitch + x * cpp;
2218}
2219
2220/*
2221 * Add the x/y offsets derived from fb->offsets[] to the user
2222 * specified plane src x/y offsets. The resulting x/y offsets
2223 * specify the start of scanout from the beginning of the gtt mapping.
2224 */
2225void intel_add_fb_offsets(int *x, int *y,
2949056c
VS
2226 const struct intel_plane_state *state,
2227 int plane)
6687c906
VS
2228
2229{
2949056c
VS
2230 const struct intel_framebuffer *intel_fb = to_intel_framebuffer(state->base.fb);
2231 unsigned int rotation = state->base.rotation;
6687c906 2232
bd2ef25d 2233 if (drm_rotation_90_or_270(rotation)) {
6687c906
VS
2234 *x += intel_fb->rotated[plane].x;
2235 *y += intel_fb->rotated[plane].y;
2236 } else {
2237 *x += intel_fb->normal[plane].x;
2238 *y += intel_fb->normal[plane].y;
2239 }
2240}
2241
29cf9491 2242/*
29cf9491
VS
2243 * Input tile dimensions and pitch must already be
2244 * rotated to match x and y, and in pixel units.
2245 */
66a2d927
VS
2246static u32 _intel_adjust_tile_offset(int *x, int *y,
2247 unsigned int tile_width,
2248 unsigned int tile_height,
2249 unsigned int tile_size,
2250 unsigned int pitch_tiles,
2251 u32 old_offset,
2252 u32 new_offset)
29cf9491 2253{
b9b24038 2254 unsigned int pitch_pixels = pitch_tiles * tile_width;
29cf9491
VS
2255 unsigned int tiles;
2256
2257 WARN_ON(old_offset & (tile_size - 1));
2258 WARN_ON(new_offset & (tile_size - 1));
2259 WARN_ON(new_offset > old_offset);
2260
2261 tiles = (old_offset - new_offset) / tile_size;
2262
2263 *y += tiles / pitch_tiles * tile_height;
2264 *x += tiles % pitch_tiles * tile_width;
2265
b9b24038
VS
2266 /* minimize x in case it got needlessly big */
2267 *y += *x / pitch_pixels * tile_height;
2268 *x %= pitch_pixels;
2269
29cf9491
VS
2270 return new_offset;
2271}
2272
66a2d927
VS
2273/*
2274 * Adjust the tile offset by moving the difference into
2275 * the x/y offsets.
2276 */
2277static u32 intel_adjust_tile_offset(int *x, int *y,
2278 const struct intel_plane_state *state, int plane,
2279 u32 old_offset, u32 new_offset)
2280{
2281 const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
2282 const struct drm_framebuffer *fb = state->base.fb;
353c8598 2283 unsigned int cpp = fb->format->cpp[plane];
66a2d927
VS
2284 unsigned int rotation = state->base.rotation;
2285 unsigned int pitch = intel_fb_pitch(fb, plane, rotation);
2286
2287 WARN_ON(new_offset > old_offset);
2288
bae781b2 2289 if (fb->modifier != DRM_FORMAT_MOD_NONE) {
66a2d927
VS
2290 unsigned int tile_size, tile_width, tile_height;
2291 unsigned int pitch_tiles;
2292
2293 tile_size = intel_tile_size(dev_priv);
2294 intel_tile_dims(dev_priv, &tile_width, &tile_height,
bae781b2 2295 fb->modifier, cpp);
66a2d927 2296
bd2ef25d 2297 if (drm_rotation_90_or_270(rotation)) {
66a2d927
VS
2298 pitch_tiles = pitch / tile_height;
2299 swap(tile_width, tile_height);
2300 } else {
2301 pitch_tiles = pitch / (tile_width * cpp);
2302 }
2303
2304 _intel_adjust_tile_offset(x, y, tile_width, tile_height,
2305 tile_size, pitch_tiles,
2306 old_offset, new_offset);
2307 } else {
2308 old_offset += *y * pitch + *x * cpp;
2309
2310 *y = (old_offset - new_offset) / pitch;
2311 *x = ((old_offset - new_offset) - *y * pitch) / cpp;
2312 }
2313
2314 return new_offset;
2315}
2316
8d0deca8
VS
2317/*
2318 * Computes the linear offset to the base tile and adjusts
2319 * x, y. bytes per pixel is assumed to be a power-of-two.
2320 *
2321 * In the 90/270 rotated case, x and y are assumed
2322 * to be already rotated to match the rotated GTT view, and
2323 * pitch is the tile_height aligned framebuffer height.
6687c906
VS
2324 *
2325 * This function is used when computing the derived information
2326 * under intel_framebuffer, so using any of that information
2327 * here is not allowed. Anything under drm_framebuffer can be
2328 * used. This is why the user has to pass in the pitch since it
2329 * is specified in the rotated orientation.
8d0deca8 2330 */
6687c906
VS
2331static u32 _intel_compute_tile_offset(const struct drm_i915_private *dev_priv,
2332 int *x, int *y,
2333 const struct drm_framebuffer *fb, int plane,
2334 unsigned int pitch,
2335 unsigned int rotation,
2336 u32 alignment)
c2c75131 2337{
bae781b2 2338 uint64_t fb_modifier = fb->modifier;
353c8598 2339 unsigned int cpp = fb->format->cpp[plane];
6687c906 2340 u32 offset, offset_aligned;
29cf9491 2341
29cf9491
VS
2342 if (alignment)
2343 alignment--;
2344
b5c65338 2345 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
8d0deca8
VS
2346 unsigned int tile_size, tile_width, tile_height;
2347 unsigned int tile_rows, tiles, pitch_tiles;
c2c75131 2348
d843310d 2349 tile_size = intel_tile_size(dev_priv);
8d0deca8
VS
2350 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2351 fb_modifier, cpp);
2352
bd2ef25d 2353 if (drm_rotation_90_or_270(rotation)) {
8d0deca8
VS
2354 pitch_tiles = pitch / tile_height;
2355 swap(tile_width, tile_height);
2356 } else {
2357 pitch_tiles = pitch / (tile_width * cpp);
2358 }
d843310d
VS
2359
2360 tile_rows = *y / tile_height;
2361 *y %= tile_height;
c2c75131 2362
8d0deca8
VS
2363 tiles = *x / tile_width;
2364 *x %= tile_width;
bc752862 2365
29cf9491
VS
2366 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2367 offset_aligned = offset & ~alignment;
bc752862 2368
66a2d927
VS
2369 _intel_adjust_tile_offset(x, y, tile_width, tile_height,
2370 tile_size, pitch_tiles,
2371 offset, offset_aligned);
29cf9491 2372 } else {
bc752862 2373 offset = *y * pitch + *x * cpp;
29cf9491
VS
2374 offset_aligned = offset & ~alignment;
2375
4e9a86b6
VS
2376 *y = (offset & alignment) / pitch;
2377 *x = ((offset & alignment) - *y * pitch) / cpp;
bc752862 2378 }
29cf9491
VS
2379
2380 return offset_aligned;
c2c75131
DV
2381}
2382
6687c906 2383u32 intel_compute_tile_offset(int *x, int *y,
2949056c
VS
2384 const struct intel_plane_state *state,
2385 int plane)
6687c906 2386{
2949056c
VS
2387 const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
2388 const struct drm_framebuffer *fb = state->base.fb;
2389 unsigned int rotation = state->base.rotation;
ef78ec94 2390 int pitch = intel_fb_pitch(fb, plane, rotation);
8d970654
VS
2391 u32 alignment;
2392
2393 /* AUX_DIST needs only 4K alignment */
438b74a5 2394 if (fb->format->format == DRM_FORMAT_NV12 && plane == 1)
8d970654
VS
2395 alignment = 4096;
2396 else
bae781b2 2397 alignment = intel_surf_alignment(dev_priv, fb->modifier);
6687c906
VS
2398
2399 return _intel_compute_tile_offset(dev_priv, x, y, fb, plane, pitch,
2400 rotation, alignment);
2401}
2402
2403/* Convert the fb->offset[] linear offset into x/y offsets */
2404static void intel_fb_offset_to_xy(int *x, int *y,
2405 const struct drm_framebuffer *fb, int plane)
2406{
353c8598 2407 unsigned int cpp = fb->format->cpp[plane];
6687c906
VS
2408 unsigned int pitch = fb->pitches[plane];
2409 u32 linear_offset = fb->offsets[plane];
2410
2411 *y = linear_offset / pitch;
2412 *x = linear_offset % pitch / cpp;
2413}
2414
72618ebf
VS
2415static unsigned int intel_fb_modifier_to_tiling(uint64_t fb_modifier)
2416{
2417 switch (fb_modifier) {
2418 case I915_FORMAT_MOD_X_TILED:
2419 return I915_TILING_X;
2420 case I915_FORMAT_MOD_Y_TILED:
2421 return I915_TILING_Y;
2422 default:
2423 return I915_TILING_NONE;
2424 }
2425}
2426
6687c906
VS
2427static int
2428intel_fill_fb_info(struct drm_i915_private *dev_priv,
2429 struct drm_framebuffer *fb)
2430{
2431 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
2432 struct intel_rotation_info *rot_info = &intel_fb->rot_info;
2433 u32 gtt_offset_rotated = 0;
2434 unsigned int max_size = 0;
bcb0b461 2435 int i, num_planes = fb->format->num_planes;
6687c906
VS
2436 unsigned int tile_size = intel_tile_size(dev_priv);
2437
2438 for (i = 0; i < num_planes; i++) {
2439 unsigned int width, height;
2440 unsigned int cpp, size;
2441 u32 offset;
2442 int x, y;
2443
353c8598 2444 cpp = fb->format->cpp[i];
145fcb11
VS
2445 width = drm_framebuffer_plane_width(fb->width, fb, i);
2446 height = drm_framebuffer_plane_height(fb->height, fb, i);
6687c906
VS
2447
2448 intel_fb_offset_to_xy(&x, &y, fb, i);
2449
60d5f2a4
VS
2450 /*
2451 * The fence (if used) is aligned to the start of the object
2452 * so having the framebuffer wrap around across the edge of the
2453 * fenced region doesn't really work. We have no API to configure
2454 * the fence start offset within the object (nor could we probably
2455 * on gen2/3). So it's just easier if we just require that the
2456 * fb layout agrees with the fence layout. We already check that the
2457 * fb stride matches the fence stride elsewhere.
2458 */
2459 if (i915_gem_object_is_tiled(intel_fb->obj) &&
2460 (x + width) * cpp > fb->pitches[i]) {
2461 DRM_DEBUG("bad fb plane %d offset: 0x%x\n",
2462 i, fb->offsets[i]);
2463 return -EINVAL;
2464 }
2465
6687c906
VS
2466 /*
2467 * First pixel of the framebuffer from
2468 * the start of the normal gtt mapping.
2469 */
2470 intel_fb->normal[i].x = x;
2471 intel_fb->normal[i].y = y;
2472
2473 offset = _intel_compute_tile_offset(dev_priv, &x, &y,
2474 fb, 0, fb->pitches[i],
cc926387 2475 DRM_ROTATE_0, tile_size);
6687c906
VS
2476 offset /= tile_size;
2477
bae781b2 2478 if (fb->modifier != DRM_FORMAT_MOD_NONE) {
6687c906
VS
2479 unsigned int tile_width, tile_height;
2480 unsigned int pitch_tiles;
2481 struct drm_rect r;
2482
2483 intel_tile_dims(dev_priv, &tile_width, &tile_height,
bae781b2 2484 fb->modifier, cpp);
6687c906
VS
2485
2486 rot_info->plane[i].offset = offset;
2487 rot_info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i], tile_width * cpp);
2488 rot_info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
2489 rot_info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
2490
2491 intel_fb->rotated[i].pitch =
2492 rot_info->plane[i].height * tile_height;
2493
2494 /* how many tiles does this plane need */
2495 size = rot_info->plane[i].stride * rot_info->plane[i].height;
2496 /*
2497 * If the plane isn't horizontally tile aligned,
2498 * we need one more tile.
2499 */
2500 if (x != 0)
2501 size++;
2502
2503 /* rotate the x/y offsets to match the GTT view */
2504 r.x1 = x;
2505 r.y1 = y;
2506 r.x2 = x + width;
2507 r.y2 = y + height;
2508 drm_rect_rotate(&r,
2509 rot_info->plane[i].width * tile_width,
2510 rot_info->plane[i].height * tile_height,
cc926387 2511 DRM_ROTATE_270);
6687c906
VS
2512 x = r.x1;
2513 y = r.y1;
2514
2515 /* rotate the tile dimensions to match the GTT view */
2516 pitch_tiles = intel_fb->rotated[i].pitch / tile_height;
2517 swap(tile_width, tile_height);
2518
2519 /*
2520 * We only keep the x/y offsets, so push all of the
2521 * gtt offset into the x/y offsets.
2522 */
46a1bd28
ACO
2523 _intel_adjust_tile_offset(&x, &y,
2524 tile_width, tile_height,
2525 tile_size, pitch_tiles,
66a2d927 2526 gtt_offset_rotated * tile_size, 0);
6687c906
VS
2527
2528 gtt_offset_rotated += rot_info->plane[i].width * rot_info->plane[i].height;
2529
2530 /*
2531 * First pixel of the framebuffer from
2532 * the start of the rotated gtt mapping.
2533 */
2534 intel_fb->rotated[i].x = x;
2535 intel_fb->rotated[i].y = y;
2536 } else {
2537 size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
2538 x * cpp, tile_size);
2539 }
2540
2541 /* how many tiles in total needed in the bo */
2542 max_size = max(max_size, offset + size);
2543 }
2544
2545 if (max_size * tile_size > to_intel_framebuffer(fb)->obj->base.size) {
2546 DRM_DEBUG("fb too big for bo (need %u bytes, have %zu bytes)\n",
2547 max_size * tile_size, to_intel_framebuffer(fb)->obj->base.size);
2548 return -EINVAL;
2549 }
2550
2551 return 0;
2552}
2553
b35d63fa 2554static int i9xx_format_to_fourcc(int format)
46f297fb
JB
2555{
2556 switch (format) {
2557 case DISPPLANE_8BPP:
2558 return DRM_FORMAT_C8;
2559 case DISPPLANE_BGRX555:
2560 return DRM_FORMAT_XRGB1555;
2561 case DISPPLANE_BGRX565:
2562 return DRM_FORMAT_RGB565;
2563 default:
2564 case DISPPLANE_BGRX888:
2565 return DRM_FORMAT_XRGB8888;
2566 case DISPPLANE_RGBX888:
2567 return DRM_FORMAT_XBGR8888;
2568 case DISPPLANE_BGRX101010:
2569 return DRM_FORMAT_XRGB2101010;
2570 case DISPPLANE_RGBX101010:
2571 return DRM_FORMAT_XBGR2101010;
2572 }
2573}
2574
bc8d7dff
DL
2575static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2576{
2577 switch (format) {
2578 case PLANE_CTL_FORMAT_RGB_565:
2579 return DRM_FORMAT_RGB565;
2580 default:
2581 case PLANE_CTL_FORMAT_XRGB_8888:
2582 if (rgb_order) {
2583 if (alpha)
2584 return DRM_FORMAT_ABGR8888;
2585 else
2586 return DRM_FORMAT_XBGR8888;
2587 } else {
2588 if (alpha)
2589 return DRM_FORMAT_ARGB8888;
2590 else
2591 return DRM_FORMAT_XRGB8888;
2592 }
2593 case PLANE_CTL_FORMAT_XRGB_2101010:
2594 if (rgb_order)
2595 return DRM_FORMAT_XBGR2101010;
2596 else
2597 return DRM_FORMAT_XRGB2101010;
2598 }
2599}
2600
5724dbd1 2601static bool
f6936e29
DV
2602intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2603 struct intel_initial_plane_config *plane_config)
46f297fb
JB
2604{
2605 struct drm_device *dev = crtc->base.dev;
3badb49f 2606 struct drm_i915_private *dev_priv = to_i915(dev);
72e96d64 2607 struct i915_ggtt *ggtt = &dev_priv->ggtt;
46f297fb
JB
2608 struct drm_i915_gem_object *obj = NULL;
2609 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2d14030b 2610 struct drm_framebuffer *fb = &plane_config->fb->base;
f37b5c2b
DV
2611 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2612 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2613 PAGE_SIZE);
2614
2615 size_aligned -= base_aligned;
46f297fb 2616
ff2652ea
CW
2617 if (plane_config->size == 0)
2618 return false;
2619
3badb49f
PZ
2620 /* If the FB is too big, just don't use it since fbdev is not very
2621 * important and we should probably use that space with FBC or other
2622 * features. */
72e96d64 2623 if (size_aligned * 2 > ggtt->stolen_usable_size)
3badb49f
PZ
2624 return false;
2625
12c83d99 2626 mutex_lock(&dev->struct_mutex);
187685cb 2627 obj = i915_gem_object_create_stolen_for_preallocated(dev_priv,
f37b5c2b
DV
2628 base_aligned,
2629 base_aligned,
2630 size_aligned);
24dbf51a
CW
2631 mutex_unlock(&dev->struct_mutex);
2632 if (!obj)
484b41dd 2633 return false;
46f297fb 2634
3e510a8e
CW
2635 if (plane_config->tiling == I915_TILING_X)
2636 obj->tiling_and_stride = fb->pitches[0] | I915_TILING_X;
46f297fb 2637
438b74a5 2638 mode_cmd.pixel_format = fb->format->format;
6bf129df
DL
2639 mode_cmd.width = fb->width;
2640 mode_cmd.height = fb->height;
2641 mode_cmd.pitches[0] = fb->pitches[0];
bae781b2 2642 mode_cmd.modifier[0] = fb->modifier;
18c5247e 2643 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
46f297fb 2644
24dbf51a 2645 if (intel_framebuffer_init(to_intel_framebuffer(fb), obj, &mode_cmd)) {
46f297fb
JB
2646 DRM_DEBUG_KMS("intel fb init failed\n");
2647 goto out_unref_obj;
2648 }
12c83d99 2649
484b41dd 2650
f6936e29 2651 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
484b41dd 2652 return true;
46f297fb
JB
2653
2654out_unref_obj:
f8c417cd 2655 i915_gem_object_put(obj);
484b41dd
JB
2656 return false;
2657}
2658
5a21b665
DV
2659/* Update plane->state->fb to match plane->fb after driver-internal updates */
2660static void
2661update_state_fb(struct drm_plane *plane)
2662{
2663 if (plane->fb == plane->state->fb)
2664 return;
2665
2666 if (plane->state->fb)
2667 drm_framebuffer_unreference(plane->state->fb);
2668 plane->state->fb = plane->fb;
2669 if (plane->state->fb)
2670 drm_framebuffer_reference(plane->state->fb);
2671}
2672
5724dbd1 2673static void
f6936e29
DV
2674intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2675 struct intel_initial_plane_config *plane_config)
484b41dd
JB
2676{
2677 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 2678 struct drm_i915_private *dev_priv = to_i915(dev);
484b41dd 2679 struct drm_crtc *c;
2ff8fde1 2680 struct drm_i915_gem_object *obj;
88595ac9 2681 struct drm_plane *primary = intel_crtc->base.primary;
be5651f2 2682 struct drm_plane_state *plane_state = primary->state;
200757f5
MR
2683 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2684 struct intel_plane *intel_plane = to_intel_plane(primary);
0a8d8a86
MR
2685 struct intel_plane_state *intel_state =
2686 to_intel_plane_state(plane_state);
88595ac9 2687 struct drm_framebuffer *fb;
484b41dd 2688
2d14030b 2689 if (!plane_config->fb)
484b41dd
JB
2690 return;
2691
f6936e29 2692 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
88595ac9
DV
2693 fb = &plane_config->fb->base;
2694 goto valid_fb;
f55548b5 2695 }
484b41dd 2696
2d14030b 2697 kfree(plane_config->fb);
484b41dd
JB
2698
2699 /*
2700 * Failed to alloc the obj, check to see if we should share
2701 * an fb with another CRTC instead
2702 */
70e1e0ec 2703 for_each_crtc(dev, c) {
be1e3415 2704 struct intel_plane_state *state;
484b41dd
JB
2705
2706 if (c == &intel_crtc->base)
2707 continue;
2708
be1e3415 2709 if (!to_intel_crtc(c)->active)
2ff8fde1
MR
2710 continue;
2711
be1e3415
CW
2712 state = to_intel_plane_state(c->primary->state);
2713 if (!state->vma)
484b41dd
JB
2714 continue;
2715
be1e3415
CW
2716 if (intel_plane_ggtt_offset(state) == plane_config->base) {
2717 fb = c->primary->fb;
88595ac9
DV
2718 drm_framebuffer_reference(fb);
2719 goto valid_fb;
484b41dd
JB
2720 }
2721 }
88595ac9 2722
200757f5
MR
2723 /*
2724 * We've failed to reconstruct the BIOS FB. Current display state
2725 * indicates that the primary plane is visible, but has a NULL FB,
2726 * which will lead to problems later if we don't fix it up. The
2727 * simplest solution is to just disable the primary plane now and
2728 * pretend the BIOS never had it enabled.
2729 */
1d4258db 2730 plane_state->visible = false;
200757f5 2731 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
2622a081 2732 intel_pre_disable_primary_noatomic(&intel_crtc->base);
200757f5
MR
2733 intel_plane->disable_plane(primary, &intel_crtc->base);
2734
88595ac9
DV
2735 return;
2736
2737valid_fb:
be1e3415
CW
2738 mutex_lock(&dev->struct_mutex);
2739 intel_state->vma =
2740 intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
2741 mutex_unlock(&dev->struct_mutex);
2742 if (IS_ERR(intel_state->vma)) {
2743 DRM_ERROR("failed to pin boot fb on pipe %d: %li\n",
2744 intel_crtc->pipe, PTR_ERR(intel_state->vma));
2745
2746 intel_state->vma = NULL;
2747 drm_framebuffer_unreference(fb);
2748 return;
2749 }
2750
f44e2659
VS
2751 plane_state->src_x = 0;
2752 plane_state->src_y = 0;
be5651f2
ML
2753 plane_state->src_w = fb->width << 16;
2754 plane_state->src_h = fb->height << 16;
2755
f44e2659
VS
2756 plane_state->crtc_x = 0;
2757 plane_state->crtc_y = 0;
be5651f2
ML
2758 plane_state->crtc_w = fb->width;
2759 plane_state->crtc_h = fb->height;
2760
1638d30c
RC
2761 intel_state->base.src = drm_plane_state_src(plane_state);
2762 intel_state->base.dst = drm_plane_state_dest(plane_state);
0a8d8a86 2763
88595ac9 2764 obj = intel_fb_obj(fb);
3e510a8e 2765 if (i915_gem_object_is_tiled(obj))
88595ac9
DV
2766 dev_priv->preserve_bios_swizzle = true;
2767
be5651f2
ML
2768 drm_framebuffer_reference(fb);
2769 primary->fb = primary->state->fb = fb;
36750f28 2770 primary->crtc = primary->state->crtc = &intel_crtc->base;
36750f28 2771 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
faf5bf0a
CW
2772 atomic_or(to_intel_plane(primary)->frontbuffer_bit,
2773 &obj->frontbuffer_bits);
46f297fb
JB
2774}
2775
b63a16f6
VS
2776static int skl_max_plane_width(const struct drm_framebuffer *fb, int plane,
2777 unsigned int rotation)
2778{
353c8598 2779 int cpp = fb->format->cpp[plane];
b63a16f6 2780
bae781b2 2781 switch (fb->modifier) {
b63a16f6
VS
2782 case DRM_FORMAT_MOD_NONE:
2783 case I915_FORMAT_MOD_X_TILED:
2784 switch (cpp) {
2785 case 8:
2786 return 4096;
2787 case 4:
2788 case 2:
2789 case 1:
2790 return 8192;
2791 default:
2792 MISSING_CASE(cpp);
2793 break;
2794 }
2795 break;
2796 case I915_FORMAT_MOD_Y_TILED:
2797 case I915_FORMAT_MOD_Yf_TILED:
2798 switch (cpp) {
2799 case 8:
2800 return 2048;
2801 case 4:
2802 return 4096;
2803 case 2:
2804 case 1:
2805 return 8192;
2806 default:
2807 MISSING_CASE(cpp);
2808 break;
2809 }
2810 break;
2811 default:
bae781b2 2812 MISSING_CASE(fb->modifier);
b63a16f6
VS
2813 }
2814
2815 return 2048;
2816}
2817
2818static int skl_check_main_surface(struct intel_plane_state *plane_state)
2819{
2820 const struct drm_i915_private *dev_priv = to_i915(plane_state->base.plane->dev);
2821 const struct drm_framebuffer *fb = plane_state->base.fb;
2822 unsigned int rotation = plane_state->base.rotation;
cc926387
DV
2823 int x = plane_state->base.src.x1 >> 16;
2824 int y = plane_state->base.src.y1 >> 16;
2825 int w = drm_rect_width(&plane_state->base.src) >> 16;
2826 int h = drm_rect_height(&plane_state->base.src) >> 16;
b63a16f6
VS
2827 int max_width = skl_max_plane_width(fb, 0, rotation);
2828 int max_height = 4096;
8d970654 2829 u32 alignment, offset, aux_offset = plane_state->aux.offset;
b63a16f6
VS
2830
2831 if (w > max_width || h > max_height) {
2832 DRM_DEBUG_KMS("requested Y/RGB source size %dx%d too big (limit %dx%d)\n",
2833 w, h, max_width, max_height);
2834 return -EINVAL;
2835 }
2836
2837 intel_add_fb_offsets(&x, &y, plane_state, 0);
2838 offset = intel_compute_tile_offset(&x, &y, plane_state, 0);
2839
bae781b2 2840 alignment = intel_surf_alignment(dev_priv, fb->modifier);
b63a16f6 2841
8d970654
VS
2842 /*
2843 * AUX surface offset is specified as the distance from the
2844 * main surface offset, and it must be non-negative. Make
2845 * sure that is what we will get.
2846 */
2847 if (offset > aux_offset)
2848 offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
2849 offset, aux_offset & ~(alignment - 1));
2850
b63a16f6
VS
2851 /*
2852 * When using an X-tiled surface, the plane blows up
2853 * if the x offset + width exceed the stride.
2854 *
2855 * TODO: linear and Y-tiled seem fine, Yf untested,
2856 */
bae781b2 2857 if (fb->modifier == I915_FORMAT_MOD_X_TILED) {
353c8598 2858 int cpp = fb->format->cpp[0];
b63a16f6
VS
2859
2860 while ((x + w) * cpp > fb->pitches[0]) {
2861 if (offset == 0) {
2862 DRM_DEBUG_KMS("Unable to find suitable display surface offset\n");
2863 return -EINVAL;
2864 }
2865
2866 offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
2867 offset, offset - alignment);
2868 }
2869 }
2870
2871 plane_state->main.offset = offset;
2872 plane_state->main.x = x;
2873 plane_state->main.y = y;
2874
2875 return 0;
2876}
2877
8d970654
VS
2878static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
2879{
2880 const struct drm_framebuffer *fb = plane_state->base.fb;
2881 unsigned int rotation = plane_state->base.rotation;
2882 int max_width = skl_max_plane_width(fb, 1, rotation);
2883 int max_height = 4096;
cc926387
DV
2884 int x = plane_state->base.src.x1 >> 17;
2885 int y = plane_state->base.src.y1 >> 17;
2886 int w = drm_rect_width(&plane_state->base.src) >> 17;
2887 int h = drm_rect_height(&plane_state->base.src) >> 17;
8d970654
VS
2888 u32 offset;
2889
2890 intel_add_fb_offsets(&x, &y, plane_state, 1);
2891 offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
2892
2893 /* FIXME not quite sure how/if these apply to the chroma plane */
2894 if (w > max_width || h > max_height) {
2895 DRM_DEBUG_KMS("CbCr source size %dx%d too big (limit %dx%d)\n",
2896 w, h, max_width, max_height);
2897 return -EINVAL;
2898 }
2899
2900 plane_state->aux.offset = offset;
2901 plane_state->aux.x = x;
2902 plane_state->aux.y = y;
2903
2904 return 0;
2905}
2906
b63a16f6
VS
2907int skl_check_plane_surface(struct intel_plane_state *plane_state)
2908{
2909 const struct drm_framebuffer *fb = plane_state->base.fb;
2910 unsigned int rotation = plane_state->base.rotation;
2911 int ret;
2912
a5e4c7d0
VS
2913 if (!plane_state->base.visible)
2914 return 0;
2915
b63a16f6 2916 /* Rotate src coordinates to match rotated GTT view */
bd2ef25d 2917 if (drm_rotation_90_or_270(rotation))
cc926387 2918 drm_rect_rotate(&plane_state->base.src,
da064b47
VS
2919 fb->width << 16, fb->height << 16,
2920 DRM_ROTATE_270);
b63a16f6 2921
8d970654
VS
2922 /*
2923 * Handle the AUX surface first since
2924 * the main surface setup depends on it.
2925 */
438b74a5 2926 if (fb->format->format == DRM_FORMAT_NV12) {
8d970654
VS
2927 ret = skl_check_nv12_aux_surface(plane_state);
2928 if (ret)
2929 return ret;
2930 } else {
2931 plane_state->aux.offset = ~0xfff;
2932 plane_state->aux.x = 0;
2933 plane_state->aux.y = 0;
2934 }
2935
b63a16f6
VS
2936 ret = skl_check_main_surface(plane_state);
2937 if (ret)
2938 return ret;
2939
2940 return 0;
2941}
2942
a8d201af
ML
2943static void i9xx_update_primary_plane(struct drm_plane *primary,
2944 const struct intel_crtc_state *crtc_state,
2945 const struct intel_plane_state *plane_state)
81255565 2946{
6315b5d3 2947 struct drm_i915_private *dev_priv = to_i915(primary->dev);
a8d201af
ML
2948 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2949 struct drm_framebuffer *fb = plane_state->base.fb;
81255565 2950 int plane = intel_crtc->plane;
54ea9da8 2951 u32 linear_offset;
81255565 2952 u32 dspcntr;
f0f59a00 2953 i915_reg_t reg = DSPCNTR(plane);
8d0deca8 2954 unsigned int rotation = plane_state->base.rotation;
936e71e3
VS
2955 int x = plane_state->base.src.x1 >> 16;
2956 int y = plane_state->base.src.y1 >> 16;
c9ba6fad 2957
f45651ba
VS
2958 dspcntr = DISPPLANE_GAMMA_ENABLE;
2959
fdd508a6 2960 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba 2961
6315b5d3 2962 if (INTEL_GEN(dev_priv) < 4) {
f45651ba
VS
2963 if (intel_crtc->pipe == PIPE_B)
2964 dspcntr |= DISPPLANE_SEL_PIPE_B;
2965
2966 /* pipesrc and dspsize control the size that is scaled from,
2967 * which should always be the user's requested size.
2968 */
2969 I915_WRITE(DSPSIZE(plane),
a8d201af
ML
2970 ((crtc_state->pipe_src_h - 1) << 16) |
2971 (crtc_state->pipe_src_w - 1));
f45651ba 2972 I915_WRITE(DSPPOS(plane), 0);
920a14b2 2973 } else if (IS_CHERRYVIEW(dev_priv) && plane == PLANE_B) {
c14b0485 2974 I915_WRITE(PRIMSIZE(plane),
a8d201af
ML
2975 ((crtc_state->pipe_src_h - 1) << 16) |
2976 (crtc_state->pipe_src_w - 1));
c14b0485
VS
2977 I915_WRITE(PRIMPOS(plane), 0);
2978 I915_WRITE(PRIMCNSTALPHA(plane), 0);
f45651ba 2979 }
81255565 2980
438b74a5 2981 switch (fb->format->format) {
57779d06 2982 case DRM_FORMAT_C8:
81255565
JB
2983 dspcntr |= DISPPLANE_8BPP;
2984 break;
57779d06 2985 case DRM_FORMAT_XRGB1555:
57779d06 2986 dspcntr |= DISPPLANE_BGRX555;
81255565 2987 break;
57779d06
VS
2988 case DRM_FORMAT_RGB565:
2989 dspcntr |= DISPPLANE_BGRX565;
2990 break;
2991 case DRM_FORMAT_XRGB8888:
57779d06
VS
2992 dspcntr |= DISPPLANE_BGRX888;
2993 break;
2994 case DRM_FORMAT_XBGR8888:
57779d06
VS
2995 dspcntr |= DISPPLANE_RGBX888;
2996 break;
2997 case DRM_FORMAT_XRGB2101010:
57779d06
VS
2998 dspcntr |= DISPPLANE_BGRX101010;
2999 break;
3000 case DRM_FORMAT_XBGR2101010:
57779d06 3001 dspcntr |= DISPPLANE_RGBX101010;
81255565
JB
3002 break;
3003 default:
baba133a 3004 BUG();
81255565 3005 }
57779d06 3006
72618ebf 3007 if (INTEL_GEN(dev_priv) >= 4 &&
bae781b2 3008 fb->modifier == I915_FORMAT_MOD_X_TILED)
f45651ba 3009 dspcntr |= DISPPLANE_TILED;
81255565 3010
df0cd455
VS
3011 if (rotation & DRM_ROTATE_180)
3012 dspcntr |= DISPPLANE_ROTATE_180;
3013
4ea7be2b
VS
3014 if (rotation & DRM_REFLECT_X)
3015 dspcntr |= DISPPLANE_MIRROR;
3016
9beb5fea 3017 if (IS_G4X(dev_priv))
de1aa629
VS
3018 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
3019
2949056c 3020 intel_add_fb_offsets(&x, &y, plane_state, 0);
81255565 3021
6315b5d3 3022 if (INTEL_GEN(dev_priv) >= 4)
c2c75131 3023 intel_crtc->dspaddr_offset =
2949056c 3024 intel_compute_tile_offset(&x, &y, plane_state, 0);
e506a0c6 3025
f22aa143 3026 if (rotation & DRM_ROTATE_180) {
df0cd455
VS
3027 x += crtc_state->pipe_src_w - 1;
3028 y += crtc_state->pipe_src_h - 1;
4ea7be2b
VS
3029 } else if (rotation & DRM_REFLECT_X) {
3030 x += crtc_state->pipe_src_w - 1;
48404c1e
SJ
3031 }
3032
2949056c 3033 linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
6687c906 3034
6315b5d3 3035 if (INTEL_GEN(dev_priv) < 4)
6687c906
VS
3036 intel_crtc->dspaddr_offset = linear_offset;
3037
2db3366b
PZ
3038 intel_crtc->adjusted_x = x;
3039 intel_crtc->adjusted_y = y;
3040
48404c1e
SJ
3041 I915_WRITE(reg, dspcntr);
3042
01f2c773 3043 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
6315b5d3 3044 if (INTEL_GEN(dev_priv) >= 4) {
85ba7b7d 3045 I915_WRITE(DSPSURF(plane),
be1e3415 3046 intel_plane_ggtt_offset(plane_state) +
6687c906 3047 intel_crtc->dspaddr_offset);
5eddb70b 3048 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
e506a0c6 3049 I915_WRITE(DSPLINOFF(plane), linear_offset);
bfb81049
VS
3050 } else {
3051 I915_WRITE(DSPADDR(plane),
be1e3415 3052 intel_plane_ggtt_offset(plane_state) +
bfb81049
VS
3053 intel_crtc->dspaddr_offset);
3054 }
5eddb70b 3055 POSTING_READ(reg);
17638cd6
JB
3056}
3057
a8d201af
ML
3058static void i9xx_disable_primary_plane(struct drm_plane *primary,
3059 struct drm_crtc *crtc)
17638cd6
JB
3060{
3061 struct drm_device *dev = crtc->dev;
fac5e23e 3062 struct drm_i915_private *dev_priv = to_i915(dev);
17638cd6 3063 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
17638cd6 3064 int plane = intel_crtc->plane;
f45651ba 3065
a8d201af
ML
3066 I915_WRITE(DSPCNTR(plane), 0);
3067 if (INTEL_INFO(dev_priv)->gen >= 4)
fdd508a6 3068 I915_WRITE(DSPSURF(plane), 0);
a8d201af
ML
3069 else
3070 I915_WRITE(DSPADDR(plane), 0);
3071 POSTING_READ(DSPCNTR(plane));
3072}
c9ba6fad 3073
a8d201af
ML
3074static void ironlake_update_primary_plane(struct drm_plane *primary,
3075 const struct intel_crtc_state *crtc_state,
3076 const struct intel_plane_state *plane_state)
3077{
3078 struct drm_device *dev = primary->dev;
fac5e23e 3079 struct drm_i915_private *dev_priv = to_i915(dev);
a8d201af
ML
3080 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3081 struct drm_framebuffer *fb = plane_state->base.fb;
a8d201af 3082 int plane = intel_crtc->plane;
54ea9da8 3083 u32 linear_offset;
a8d201af
ML
3084 u32 dspcntr;
3085 i915_reg_t reg = DSPCNTR(plane);
8d0deca8 3086 unsigned int rotation = plane_state->base.rotation;
936e71e3
VS
3087 int x = plane_state->base.src.x1 >> 16;
3088 int y = plane_state->base.src.y1 >> 16;
c9ba6fad 3089
f45651ba 3090 dspcntr = DISPPLANE_GAMMA_ENABLE;
fdd508a6 3091 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba 3092
8652744b 3093 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
f45651ba 3094 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
17638cd6 3095
438b74a5 3096 switch (fb->format->format) {
57779d06 3097 case DRM_FORMAT_C8:
17638cd6
JB
3098 dspcntr |= DISPPLANE_8BPP;
3099 break;
57779d06
VS
3100 case DRM_FORMAT_RGB565:
3101 dspcntr |= DISPPLANE_BGRX565;
17638cd6 3102 break;
57779d06 3103 case DRM_FORMAT_XRGB8888:
57779d06
VS
3104 dspcntr |= DISPPLANE_BGRX888;
3105 break;
3106 case DRM_FORMAT_XBGR8888:
57779d06
VS
3107 dspcntr |= DISPPLANE_RGBX888;
3108 break;
3109 case DRM_FORMAT_XRGB2101010:
57779d06
VS
3110 dspcntr |= DISPPLANE_BGRX101010;
3111 break;
3112 case DRM_FORMAT_XBGR2101010:
57779d06 3113 dspcntr |= DISPPLANE_RGBX101010;
17638cd6
JB
3114 break;
3115 default:
baba133a 3116 BUG();
17638cd6
JB
3117 }
3118
bae781b2 3119 if (fb->modifier == I915_FORMAT_MOD_X_TILED)
17638cd6 3120 dspcntr |= DISPPLANE_TILED;
17638cd6 3121
df0cd455
VS
3122 if (rotation & DRM_ROTATE_180)
3123 dspcntr |= DISPPLANE_ROTATE_180;
3124
8652744b 3125 if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv))
1f5d76db 3126 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
17638cd6 3127
2949056c 3128 intel_add_fb_offsets(&x, &y, plane_state, 0);
6687c906 3129
c2c75131 3130 intel_crtc->dspaddr_offset =
2949056c 3131 intel_compute_tile_offset(&x, &y, plane_state, 0);
6687c906 3132
df0cd455
VS
3133 /* HSW+ does this automagically in hardware */
3134 if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv) &&
3135 rotation & DRM_ROTATE_180) {
3136 x += crtc_state->pipe_src_w - 1;
3137 y += crtc_state->pipe_src_h - 1;
48404c1e
SJ
3138 }
3139
2949056c 3140 linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
6687c906 3141
2db3366b
PZ
3142 intel_crtc->adjusted_x = x;
3143 intel_crtc->adjusted_y = y;
3144
48404c1e 3145 I915_WRITE(reg, dspcntr);
17638cd6 3146
01f2c773 3147 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
85ba7b7d 3148 I915_WRITE(DSPSURF(plane),
be1e3415 3149 intel_plane_ggtt_offset(plane_state) +
6687c906 3150 intel_crtc->dspaddr_offset);
8652744b 3151 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
bc1c91eb
DL
3152 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
3153 } else {
3154 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
3155 I915_WRITE(DSPLINOFF(plane), linear_offset);
3156 }
17638cd6 3157 POSTING_READ(reg);
17638cd6
JB
3158}
3159
7b49f948
VS
3160u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
3161 uint64_t fb_modifier, uint32_t pixel_format)
b321803d 3162{
7b49f948 3163 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
b321803d 3164 return 64;
7b49f948
VS
3165 } else {
3166 int cpp = drm_format_plane_cpp(pixel_format, 0);
3167
27ba3910 3168 return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
b321803d
DL
3169 }
3170}
3171
e435d6e5
ML
3172static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
3173{
3174 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 3175 struct drm_i915_private *dev_priv = to_i915(dev);
e435d6e5
ML
3176
3177 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
3178 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
3179 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
e435d6e5
ML
3180}
3181
a1b2278e
CK
3182/*
3183 * This function detaches (aka. unbinds) unused scalers in hardware
3184 */
0583236e 3185static void skl_detach_scalers(struct intel_crtc *intel_crtc)
a1b2278e 3186{
a1b2278e
CK
3187 struct intel_crtc_scaler_state *scaler_state;
3188 int i;
3189
a1b2278e
CK
3190 scaler_state = &intel_crtc->config->scaler_state;
3191
3192 /* loop through and disable scalers that aren't in use */
3193 for (i = 0; i < intel_crtc->num_scalers; i++) {
e435d6e5
ML
3194 if (!scaler_state->scalers[i].in_use)
3195 skl_detach_scaler(intel_crtc, i);
a1b2278e
CK
3196 }
3197}
3198
d2196774
VS
3199u32 skl_plane_stride(const struct drm_framebuffer *fb, int plane,
3200 unsigned int rotation)
3201{
3202 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
3203 u32 stride = intel_fb_pitch(fb, plane, rotation);
3204
3205 /*
3206 * The stride is either expressed as a multiple of 64 bytes chunks for
3207 * linear buffers or in number of tiles for tiled buffers.
3208 */
bd2ef25d 3209 if (drm_rotation_90_or_270(rotation)) {
353c8598 3210 int cpp = fb->format->cpp[plane];
d2196774 3211
bae781b2 3212 stride /= intel_tile_height(dev_priv, fb->modifier, cpp);
d2196774 3213 } else {
bae781b2 3214 stride /= intel_fb_stride_alignment(dev_priv, fb->modifier,
438b74a5 3215 fb->format->format);
d2196774
VS
3216 }
3217
3218 return stride;
3219}
3220
6156a456 3221u32 skl_plane_ctl_format(uint32_t pixel_format)
70d21f0e 3222{
6156a456 3223 switch (pixel_format) {
d161cf7a 3224 case DRM_FORMAT_C8:
c34ce3d1 3225 return PLANE_CTL_FORMAT_INDEXED;
70d21f0e 3226 case DRM_FORMAT_RGB565:
c34ce3d1 3227 return PLANE_CTL_FORMAT_RGB_565;
70d21f0e 3228 case DRM_FORMAT_XBGR8888:
c34ce3d1 3229 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
6156a456 3230 case DRM_FORMAT_XRGB8888:
c34ce3d1 3231 return PLANE_CTL_FORMAT_XRGB_8888;
6156a456
CK
3232 /*
3233 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
3234 * to be already pre-multiplied. We need to add a knob (or a different
3235 * DRM_FORMAT) for user-space to configure that.
3236 */
f75fb42a 3237 case DRM_FORMAT_ABGR8888:
c34ce3d1 3238 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
6156a456 3239 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
6156a456 3240 case DRM_FORMAT_ARGB8888:
c34ce3d1 3241 return PLANE_CTL_FORMAT_XRGB_8888 |
6156a456 3242 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
70d21f0e 3243 case DRM_FORMAT_XRGB2101010:
c34ce3d1 3244 return PLANE_CTL_FORMAT_XRGB_2101010;
70d21f0e 3245 case DRM_FORMAT_XBGR2101010:
c34ce3d1 3246 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
6156a456 3247 case DRM_FORMAT_YUYV:
c34ce3d1 3248 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
6156a456 3249 case DRM_FORMAT_YVYU:
c34ce3d1 3250 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
6156a456 3251 case DRM_FORMAT_UYVY:
c34ce3d1 3252 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
6156a456 3253 case DRM_FORMAT_VYUY:
c34ce3d1 3254 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
70d21f0e 3255 default:
4249eeef 3256 MISSING_CASE(pixel_format);
70d21f0e 3257 }
8cfcba41 3258
c34ce3d1 3259 return 0;
6156a456 3260}
70d21f0e 3261
6156a456
CK
3262u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
3263{
6156a456 3264 switch (fb_modifier) {
30af77c4 3265 case DRM_FORMAT_MOD_NONE:
70d21f0e 3266 break;
30af77c4 3267 case I915_FORMAT_MOD_X_TILED:
c34ce3d1 3268 return PLANE_CTL_TILED_X;
b321803d 3269 case I915_FORMAT_MOD_Y_TILED:
c34ce3d1 3270 return PLANE_CTL_TILED_Y;
b321803d 3271 case I915_FORMAT_MOD_Yf_TILED:
c34ce3d1 3272 return PLANE_CTL_TILED_YF;
70d21f0e 3273 default:
6156a456 3274 MISSING_CASE(fb_modifier);
70d21f0e 3275 }
8cfcba41 3276
c34ce3d1 3277 return 0;
6156a456 3278}
70d21f0e 3279
6156a456
CK
3280u32 skl_plane_ctl_rotation(unsigned int rotation)
3281{
3b7a5119 3282 switch (rotation) {
31ad61e4 3283 case DRM_ROTATE_0:
6156a456 3284 break;
1e8df167
SJ
3285 /*
3286 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
3287 * while i915 HW rotation is clockwise, thats why this swapping.
3288 */
31ad61e4 3289 case DRM_ROTATE_90:
1e8df167 3290 return PLANE_CTL_ROTATE_270;
31ad61e4 3291 case DRM_ROTATE_180:
c34ce3d1 3292 return PLANE_CTL_ROTATE_180;
31ad61e4 3293 case DRM_ROTATE_270:
1e8df167 3294 return PLANE_CTL_ROTATE_90;
6156a456
CK
3295 default:
3296 MISSING_CASE(rotation);
3297 }
3298
c34ce3d1 3299 return 0;
6156a456
CK
3300}
3301
a8d201af
ML
3302static void skylake_update_primary_plane(struct drm_plane *plane,
3303 const struct intel_crtc_state *crtc_state,
3304 const struct intel_plane_state *plane_state)
6156a456 3305{
a8d201af 3306 struct drm_device *dev = plane->dev;
fac5e23e 3307 struct drm_i915_private *dev_priv = to_i915(dev);
a8d201af
ML
3308 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3309 struct drm_framebuffer *fb = plane_state->base.fb;
8e816bb4
VS
3310 enum plane_id plane_id = to_intel_plane(plane)->id;
3311 enum pipe pipe = to_intel_plane(plane)->pipe;
d2196774 3312 u32 plane_ctl;
a8d201af 3313 unsigned int rotation = plane_state->base.rotation;
d2196774 3314 u32 stride = skl_plane_stride(fb, 0, rotation);
b63a16f6 3315 u32 surf_addr = plane_state->main.offset;
a8d201af 3316 int scaler_id = plane_state->scaler_id;
b63a16f6
VS
3317 int src_x = plane_state->main.x;
3318 int src_y = plane_state->main.y;
936e71e3
VS
3319 int src_w = drm_rect_width(&plane_state->base.src) >> 16;
3320 int src_h = drm_rect_height(&plane_state->base.src) >> 16;
3321 int dst_x = plane_state->base.dst.x1;
3322 int dst_y = plane_state->base.dst.y1;
3323 int dst_w = drm_rect_width(&plane_state->base.dst);
3324 int dst_h = drm_rect_height(&plane_state->base.dst);
70d21f0e 3325
47f9ea8b
ACO
3326 plane_ctl = PLANE_CTL_ENABLE;
3327
3328 if (IS_GEMINILAKE(dev_priv)) {
3329 I915_WRITE(PLANE_COLOR_CTL(pipe, plane_id),
3330 PLANE_COLOR_PIPE_GAMMA_ENABLE |
3bb56da7 3331 PLANE_COLOR_PIPE_CSC_ENABLE |
47f9ea8b
ACO
3332 PLANE_COLOR_PLANE_GAMMA_DISABLE);
3333 } else {
3334 plane_ctl |=
3335 PLANE_CTL_PIPE_GAMMA_ENABLE |
3336 PLANE_CTL_PIPE_CSC_ENABLE |
3337 PLANE_CTL_PLANE_GAMMA_DISABLE;
3338 }
6156a456 3339
438b74a5 3340 plane_ctl |= skl_plane_ctl_format(fb->format->format);
bae781b2 3341 plane_ctl |= skl_plane_ctl_tiling(fb->modifier);
6156a456
CK
3342 plane_ctl |= skl_plane_ctl_rotation(rotation);
3343
6687c906
VS
3344 /* Sizes are 0 based */
3345 src_w--;
3346 src_h--;
3347 dst_w--;
3348 dst_h--;
3349
4c0b8a8b
PZ
3350 intel_crtc->dspaddr_offset = surf_addr;
3351
6687c906
VS
3352 intel_crtc->adjusted_x = src_x;
3353 intel_crtc->adjusted_y = src_y;
2db3366b 3354
8e816bb4
VS
3355 I915_WRITE(PLANE_CTL(pipe, plane_id), plane_ctl);
3356 I915_WRITE(PLANE_OFFSET(pipe, plane_id), (src_y << 16) | src_x);
3357 I915_WRITE(PLANE_STRIDE(pipe, plane_id), stride);
3358 I915_WRITE(PLANE_SIZE(pipe, plane_id), (src_h << 16) | src_w);
6156a456
CK
3359
3360 if (scaler_id >= 0) {
3361 uint32_t ps_ctrl = 0;
3362
3363 WARN_ON(!dst_w || !dst_h);
8e816bb4 3364 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(plane_id) |
6156a456
CK
3365 crtc_state->scaler_state.scalers[scaler_id].mode;
3366 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3367 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3368 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3369 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
8e816bb4 3370 I915_WRITE(PLANE_POS(pipe, plane_id), 0);
6156a456 3371 } else {
8e816bb4 3372 I915_WRITE(PLANE_POS(pipe, plane_id), (dst_y << 16) | dst_x);
6156a456
CK
3373 }
3374
8e816bb4 3375 I915_WRITE(PLANE_SURF(pipe, plane_id),
be1e3415 3376 intel_plane_ggtt_offset(plane_state) + surf_addr);
70d21f0e 3377
8e816bb4 3378 POSTING_READ(PLANE_SURF(pipe, plane_id));
70d21f0e
DL
3379}
3380
a8d201af
ML
3381static void skylake_disable_primary_plane(struct drm_plane *primary,
3382 struct drm_crtc *crtc)
17638cd6
JB
3383{
3384 struct drm_device *dev = crtc->dev;
fac5e23e 3385 struct drm_i915_private *dev_priv = to_i915(dev);
8e816bb4
VS
3386 enum plane_id plane_id = to_intel_plane(primary)->id;
3387 enum pipe pipe = to_intel_plane(primary)->pipe;
62e0fb88 3388
8e816bb4
VS
3389 I915_WRITE(PLANE_CTL(pipe, plane_id), 0);
3390 I915_WRITE(PLANE_SURF(pipe, plane_id), 0);
3391 POSTING_READ(PLANE_SURF(pipe, plane_id));
a8d201af 3392}
29b9bde6 3393
a8d201af
ML
3394/* Assume fb object is pinned & idle & fenced and just update base pointers */
3395static int
3396intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3397 int x, int y, enum mode_set_atomic state)
3398{
3399 /* Support for kgdboc is disabled, this needs a major rework. */
3400 DRM_ERROR("legacy panic handler not supported any more.\n");
3401
3402 return -ENODEV;
81255565
JB
3403}
3404
5a21b665
DV
3405static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
3406{
3407 struct intel_crtc *crtc;
3408
91c8a326 3409 for_each_intel_crtc(&dev_priv->drm, crtc)
5a21b665
DV
3410 intel_finish_page_flip_cs(dev_priv, crtc->pipe);
3411}
3412
7514747d
VS
3413static void intel_update_primary_planes(struct drm_device *dev)
3414{
7514747d 3415 struct drm_crtc *crtc;
96a02917 3416
70e1e0ec 3417 for_each_crtc(dev, crtc) {
11c22da6 3418 struct intel_plane *plane = to_intel_plane(crtc->primary);
73974893
ML
3419 struct intel_plane_state *plane_state =
3420 to_intel_plane_state(plane->base.state);
11c22da6 3421
936e71e3 3422 if (plane_state->base.visible)
a8d201af
ML
3423 plane->update_plane(&plane->base,
3424 to_intel_crtc_state(crtc->state),
3425 plane_state);
73974893
ML
3426 }
3427}
3428
3429static int
3430__intel_display_resume(struct drm_device *dev,
3431 struct drm_atomic_state *state)
3432{
3433 struct drm_crtc_state *crtc_state;
3434 struct drm_crtc *crtc;
3435 int i, ret;
11c22da6 3436
73974893 3437 intel_modeset_setup_hw_state(dev);
29b74b7f 3438 i915_redisable_vga(to_i915(dev));
73974893
ML
3439
3440 if (!state)
3441 return 0;
3442
3443 for_each_crtc_in_state(state, crtc, crtc_state, i) {
3444 /*
3445 * Force recalculation even if we restore
3446 * current state. With fast modeset this may not result
3447 * in a modeset when the state is compatible.
3448 */
3449 crtc_state->mode_changed = true;
96a02917 3450 }
73974893
ML
3451
3452 /* ignore any reset values/BIOS leftovers in the WM registers */
3453 to_intel_atomic_state(state)->skip_intermediate_wm = true;
3454
3455 ret = drm_atomic_commit(state);
3456
3457 WARN_ON(ret == -EDEADLK);
3458 return ret;
96a02917
VS
3459}
3460
4ac2ba2f
VS
3461static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
3462{
ae98104b
VS
3463 return intel_has_gpu_reset(dev_priv) &&
3464 INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
4ac2ba2f
VS
3465}
3466
c033666a 3467void intel_prepare_reset(struct drm_i915_private *dev_priv)
7514747d 3468{
73974893
ML
3469 struct drm_device *dev = &dev_priv->drm;
3470 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
3471 struct drm_atomic_state *state;
3472 int ret;
3473
73974893
ML
3474 /*
3475 * Need mode_config.mutex so that we don't
3476 * trample ongoing ->detect() and whatnot.
3477 */
3478 mutex_lock(&dev->mode_config.mutex);
3479 drm_modeset_acquire_init(ctx, 0);
3480 while (1) {
3481 ret = drm_modeset_lock_all_ctx(dev, ctx);
3482 if (ret != -EDEADLK)
3483 break;
3484
3485 drm_modeset_backoff(ctx);
3486 }
3487
3488 /* reset doesn't touch the display, but flips might get nuked anyway, */
522a63de 3489 if (!i915.force_reset_modeset_test &&
4ac2ba2f 3490 !gpu_reset_clobbers_display(dev_priv))
7514747d
VS
3491 return;
3492
f98ce92f
VS
3493 /*
3494 * Disabling the crtcs gracefully seems nicer. Also the
3495 * g33 docs say we should at least disable all the planes.
3496 */
73974893
ML
3497 state = drm_atomic_helper_duplicate_state(dev, ctx);
3498 if (IS_ERR(state)) {
3499 ret = PTR_ERR(state);
73974893 3500 DRM_ERROR("Duplicating state failed with %i\n", ret);
1e5a15d6 3501 return;
73974893
ML
3502 }
3503
3504 ret = drm_atomic_helper_disable_all(dev, ctx);
3505 if (ret) {
3506 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
1e5a15d6
ACO
3507 drm_atomic_state_put(state);
3508 return;
73974893
ML
3509 }
3510
3511 dev_priv->modeset_restore_state = state;
3512 state->acquire_ctx = ctx;
7514747d
VS
3513}
3514
c033666a 3515void intel_finish_reset(struct drm_i915_private *dev_priv)
7514747d 3516{
73974893
ML
3517 struct drm_device *dev = &dev_priv->drm;
3518 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
3519 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
3520 int ret;
3521
5a21b665
DV
3522 /*
3523 * Flips in the rings will be nuked by the reset,
3524 * so complete all pending flips so that user space
3525 * will get its events and not get stuck.
3526 */
3527 intel_complete_page_flips(dev_priv);
3528
73974893
ML
3529 dev_priv->modeset_restore_state = NULL;
3530
7514747d 3531 /* reset doesn't touch the display */
4ac2ba2f 3532 if (!gpu_reset_clobbers_display(dev_priv)) {
522a63de
ML
3533 if (!state) {
3534 /*
3535 * Flips in the rings have been nuked by the reset,
3536 * so update the base address of all primary
3537 * planes to the the last fb to make sure we're
3538 * showing the correct fb after a reset.
3539 *
3540 * FIXME: Atomic will make this obsolete since we won't schedule
3541 * CS-based flips (which might get lost in gpu resets) any more.
3542 */
3543 intel_update_primary_planes(dev);
3544 } else {
3545 ret = __intel_display_resume(dev, state);
3546 if (ret)
3547 DRM_ERROR("Restoring old state failed with %i\n", ret);
3548 }
73974893
ML
3549 } else {
3550 /*
3551 * The display has been reset as well,
3552 * so need a full re-initialization.
3553 */
3554 intel_runtime_pm_disable_interrupts(dev_priv);
3555 intel_runtime_pm_enable_interrupts(dev_priv);
7514747d 3556
51f59205 3557 intel_pps_unlock_regs_wa(dev_priv);
73974893 3558 intel_modeset_init_hw(dev);
7514747d 3559
73974893
ML
3560 spin_lock_irq(&dev_priv->irq_lock);
3561 if (dev_priv->display.hpd_irq_setup)
3562 dev_priv->display.hpd_irq_setup(dev_priv);
3563 spin_unlock_irq(&dev_priv->irq_lock);
7514747d 3564
73974893
ML
3565 ret = __intel_display_resume(dev, state);
3566 if (ret)
3567 DRM_ERROR("Restoring old state failed with %i\n", ret);
7514747d 3568
73974893
ML
3569 intel_hpd_init(dev_priv);
3570 }
7514747d 3571
0853695c
CW
3572 if (state)
3573 drm_atomic_state_put(state);
73974893
ML
3574 drm_modeset_drop_locks(ctx);
3575 drm_modeset_acquire_fini(ctx);
3576 mutex_unlock(&dev->mode_config.mutex);
7514747d
VS
3577}
3578
8af29b0c
CW
3579static bool abort_flip_on_reset(struct intel_crtc *crtc)
3580{
3581 struct i915_gpu_error *error = &to_i915(crtc->base.dev)->gpu_error;
3582
3583 if (i915_reset_in_progress(error))
3584 return true;
3585
3586 if (crtc->reset_count != i915_reset_count(error))
3587 return true;
3588
3589 return false;
3590}
3591
7d5e3799
CW
3592static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3593{
5a21b665
DV
3594 struct drm_device *dev = crtc->dev;
3595 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5a21b665
DV
3596 bool pending;
3597
8af29b0c 3598 if (abort_flip_on_reset(intel_crtc))
5a21b665
DV
3599 return false;
3600
3601 spin_lock_irq(&dev->event_lock);
3602 pending = to_intel_crtc(crtc)->flip_work != NULL;
3603 spin_unlock_irq(&dev->event_lock);
3604
3605 return pending;
7d5e3799
CW
3606}
3607
bfd16b2a
ML
3608static void intel_update_pipe_config(struct intel_crtc *crtc,
3609 struct intel_crtc_state *old_crtc_state)
e30e8f75 3610{
6315b5d3 3611 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
bfd16b2a
ML
3612 struct intel_crtc_state *pipe_config =
3613 to_intel_crtc_state(crtc->base.state);
e30e8f75 3614
bfd16b2a
ML
3615 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3616 crtc->base.mode = crtc->base.state->mode;
3617
3618 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3619 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3620 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
e30e8f75
GP
3621
3622 /*
3623 * Update pipe size and adjust fitter if needed: the reason for this is
3624 * that in compute_mode_changes we check the native mode (not the pfit
3625 * mode) to see if we can flip rather than do a full mode set. In the
3626 * fastboot case, we'll flip, but if we don't update the pipesrc and
3627 * pfit state, we'll end up with a big fb scanned out into the wrong
3628 * sized surface.
e30e8f75
GP
3629 */
3630
e30e8f75 3631 I915_WRITE(PIPESRC(crtc->pipe),
bfd16b2a
ML
3632 ((pipe_config->pipe_src_w - 1) << 16) |
3633 (pipe_config->pipe_src_h - 1));
3634
3635 /* on skylake this is done by detaching scalers */
6315b5d3 3636 if (INTEL_GEN(dev_priv) >= 9) {
bfd16b2a
ML
3637 skl_detach_scalers(crtc);
3638
3639 if (pipe_config->pch_pfit.enabled)
3640 skylake_pfit_enable(crtc);
6e266956 3641 } else if (HAS_PCH_SPLIT(dev_priv)) {
bfd16b2a
ML
3642 if (pipe_config->pch_pfit.enabled)
3643 ironlake_pfit_enable(crtc);
3644 else if (old_crtc_state->pch_pfit.enabled)
3645 ironlake_pfit_disable(crtc, true);
e30e8f75 3646 }
e30e8f75
GP
3647}
3648
5e84e1a4
ZW
3649static void intel_fdi_normal_train(struct drm_crtc *crtc)
3650{
3651 struct drm_device *dev = crtc->dev;
fac5e23e 3652 struct drm_i915_private *dev_priv = to_i915(dev);
5e84e1a4
ZW
3653 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3654 int pipe = intel_crtc->pipe;
f0f59a00
VS
3655 i915_reg_t reg;
3656 u32 temp;
5e84e1a4
ZW
3657
3658 /* enable normal train */
3659 reg = FDI_TX_CTL(pipe);
3660 temp = I915_READ(reg);
fd6b8f43 3661 if (IS_IVYBRIDGE(dev_priv)) {
357555c0
JB
3662 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3663 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
3664 } else {
3665 temp &= ~FDI_LINK_TRAIN_NONE;
3666 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 3667 }
5e84e1a4
ZW
3668 I915_WRITE(reg, temp);
3669
3670 reg = FDI_RX_CTL(pipe);
3671 temp = I915_READ(reg);
6e266956 3672 if (HAS_PCH_CPT(dev_priv)) {
5e84e1a4
ZW
3673 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3674 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3675 } else {
3676 temp &= ~FDI_LINK_TRAIN_NONE;
3677 temp |= FDI_LINK_TRAIN_NONE;
3678 }
3679 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3680
3681 /* wait one idle pattern time */
3682 POSTING_READ(reg);
3683 udelay(1000);
357555c0
JB
3684
3685 /* IVB wants error correction enabled */
fd6b8f43 3686 if (IS_IVYBRIDGE(dev_priv))
357555c0
JB
3687 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3688 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
3689}
3690
8db9d77b
ZW
3691/* The FDI link training functions for ILK/Ibexpeak. */
3692static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3693{
3694 struct drm_device *dev = crtc->dev;
fac5e23e 3695 struct drm_i915_private *dev_priv = to_i915(dev);
8db9d77b
ZW
3696 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3697 int pipe = intel_crtc->pipe;
f0f59a00
VS
3698 i915_reg_t reg;
3699 u32 temp, tries;
8db9d77b 3700
1c8562f6 3701 /* FDI needs bits from pipe first */
0fc932b8 3702 assert_pipe_enabled(dev_priv, pipe);
0fc932b8 3703
e1a44743
AJ
3704 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3705 for train result */
5eddb70b
CW
3706 reg = FDI_RX_IMR(pipe);
3707 temp = I915_READ(reg);
e1a44743
AJ
3708 temp &= ~FDI_RX_SYMBOL_LOCK;
3709 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3710 I915_WRITE(reg, temp);
3711 I915_READ(reg);
e1a44743
AJ
3712 udelay(150);
3713
8db9d77b 3714 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3715 reg = FDI_TX_CTL(pipe);
3716 temp = I915_READ(reg);
627eb5a3 3717 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3718 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3719 temp &= ~FDI_LINK_TRAIN_NONE;
3720 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 3721 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3722
5eddb70b
CW
3723 reg = FDI_RX_CTL(pipe);
3724 temp = I915_READ(reg);
8db9d77b
ZW
3725 temp &= ~FDI_LINK_TRAIN_NONE;
3726 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b
CW
3727 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3728
3729 POSTING_READ(reg);
8db9d77b
ZW
3730 udelay(150);
3731
5b2adf89 3732 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
DV
3733 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3734 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3735 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 3736
5eddb70b 3737 reg = FDI_RX_IIR(pipe);
e1a44743 3738 for (tries = 0; tries < 5; tries++) {
5eddb70b 3739 temp = I915_READ(reg);
8db9d77b
ZW
3740 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3741
3742 if ((temp & FDI_RX_BIT_LOCK)) {
3743 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 3744 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
3745 break;
3746 }
8db9d77b 3747 }
e1a44743 3748 if (tries == 5)
5eddb70b 3749 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3750
3751 /* Train 2 */
5eddb70b
CW
3752 reg = FDI_TX_CTL(pipe);
3753 temp = I915_READ(reg);
8db9d77b
ZW
3754 temp &= ~FDI_LINK_TRAIN_NONE;
3755 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3756 I915_WRITE(reg, temp);
8db9d77b 3757
5eddb70b
CW
3758 reg = FDI_RX_CTL(pipe);
3759 temp = I915_READ(reg);
8db9d77b
ZW
3760 temp &= ~FDI_LINK_TRAIN_NONE;
3761 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3762 I915_WRITE(reg, temp);
8db9d77b 3763
5eddb70b
CW
3764 POSTING_READ(reg);
3765 udelay(150);
8db9d77b 3766
5eddb70b 3767 reg = FDI_RX_IIR(pipe);
e1a44743 3768 for (tries = 0; tries < 5; tries++) {
5eddb70b 3769 temp = I915_READ(reg);
8db9d77b
ZW
3770 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3771
3772 if (temp & FDI_RX_SYMBOL_LOCK) {
5eddb70b 3773 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
3774 DRM_DEBUG_KMS("FDI train 2 done.\n");
3775 break;
3776 }
8db9d77b 3777 }
e1a44743 3778 if (tries == 5)
5eddb70b 3779 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3780
3781 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 3782
8db9d77b
ZW
3783}
3784
0206e353 3785static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
3786 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3787 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3788 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3789 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3790};
3791
3792/* The FDI link training functions for SNB/Cougarpoint. */
3793static void gen6_fdi_link_train(struct drm_crtc *crtc)
3794{
3795 struct drm_device *dev = crtc->dev;
fac5e23e 3796 struct drm_i915_private *dev_priv = to_i915(dev);
8db9d77b
ZW
3797 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3798 int pipe = intel_crtc->pipe;
f0f59a00
VS
3799 i915_reg_t reg;
3800 u32 temp, i, retry;
8db9d77b 3801
e1a44743
AJ
3802 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3803 for train result */
5eddb70b
CW
3804 reg = FDI_RX_IMR(pipe);
3805 temp = I915_READ(reg);
e1a44743
AJ
3806 temp &= ~FDI_RX_SYMBOL_LOCK;
3807 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3808 I915_WRITE(reg, temp);
3809
3810 POSTING_READ(reg);
e1a44743
AJ
3811 udelay(150);
3812
8db9d77b 3813 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3814 reg = FDI_TX_CTL(pipe);
3815 temp = I915_READ(reg);
627eb5a3 3816 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3817 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3818 temp &= ~FDI_LINK_TRAIN_NONE;
3819 temp |= FDI_LINK_TRAIN_PATTERN_1;
3820 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3821 /* SNB-B */
3822 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 3823 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3824
d74cf324
DV
3825 I915_WRITE(FDI_RX_MISC(pipe),
3826 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3827
5eddb70b
CW
3828 reg = FDI_RX_CTL(pipe);
3829 temp = I915_READ(reg);
6e266956 3830 if (HAS_PCH_CPT(dev_priv)) {
8db9d77b
ZW
3831 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3832 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3833 } else {
3834 temp &= ~FDI_LINK_TRAIN_NONE;
3835 temp |= FDI_LINK_TRAIN_PATTERN_1;
3836 }
5eddb70b
CW
3837 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3838
3839 POSTING_READ(reg);
8db9d77b
ZW
3840 udelay(150);
3841
0206e353 3842 for (i = 0; i < 4; i++) {
5eddb70b
CW
3843 reg = FDI_TX_CTL(pipe);
3844 temp = I915_READ(reg);
8db9d77b
ZW
3845 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3846 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3847 I915_WRITE(reg, temp);
3848
3849 POSTING_READ(reg);
8db9d77b
ZW
3850 udelay(500);
3851
fa37d39e
SP
3852 for (retry = 0; retry < 5; retry++) {
3853 reg = FDI_RX_IIR(pipe);
3854 temp = I915_READ(reg);
3855 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3856 if (temp & FDI_RX_BIT_LOCK) {
3857 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3858 DRM_DEBUG_KMS("FDI train 1 done.\n");
3859 break;
3860 }
3861 udelay(50);
8db9d77b 3862 }
fa37d39e
SP
3863 if (retry < 5)
3864 break;
8db9d77b
ZW
3865 }
3866 if (i == 4)
5eddb70b 3867 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3868
3869 /* Train 2 */
5eddb70b
CW
3870 reg = FDI_TX_CTL(pipe);
3871 temp = I915_READ(reg);
8db9d77b
ZW
3872 temp &= ~FDI_LINK_TRAIN_NONE;
3873 temp |= FDI_LINK_TRAIN_PATTERN_2;
5db94019 3874 if (IS_GEN6(dev_priv)) {
8db9d77b
ZW
3875 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3876 /* SNB-B */
3877 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3878 }
5eddb70b 3879 I915_WRITE(reg, temp);
8db9d77b 3880
5eddb70b
CW
3881 reg = FDI_RX_CTL(pipe);
3882 temp = I915_READ(reg);
6e266956 3883 if (HAS_PCH_CPT(dev_priv)) {
8db9d77b
ZW
3884 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3885 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3886 } else {
3887 temp &= ~FDI_LINK_TRAIN_NONE;
3888 temp |= FDI_LINK_TRAIN_PATTERN_2;
3889 }
5eddb70b
CW
3890 I915_WRITE(reg, temp);
3891
3892 POSTING_READ(reg);
8db9d77b
ZW
3893 udelay(150);
3894
0206e353 3895 for (i = 0; i < 4; i++) {
5eddb70b
CW
3896 reg = FDI_TX_CTL(pipe);
3897 temp = I915_READ(reg);
8db9d77b
ZW
3898 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3899 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3900 I915_WRITE(reg, temp);
3901
3902 POSTING_READ(reg);
8db9d77b
ZW
3903 udelay(500);
3904
fa37d39e
SP
3905 for (retry = 0; retry < 5; retry++) {
3906 reg = FDI_RX_IIR(pipe);
3907 temp = I915_READ(reg);
3908 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3909 if (temp & FDI_RX_SYMBOL_LOCK) {
3910 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3911 DRM_DEBUG_KMS("FDI train 2 done.\n");
3912 break;
3913 }
3914 udelay(50);
8db9d77b 3915 }
fa37d39e
SP
3916 if (retry < 5)
3917 break;
8db9d77b
ZW
3918 }
3919 if (i == 4)
5eddb70b 3920 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3921
3922 DRM_DEBUG_KMS("FDI train done.\n");
3923}
3924
357555c0
JB
3925/* Manual link training for Ivy Bridge A0 parts */
3926static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3927{
3928 struct drm_device *dev = crtc->dev;
fac5e23e 3929 struct drm_i915_private *dev_priv = to_i915(dev);
357555c0
JB
3930 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3931 int pipe = intel_crtc->pipe;
f0f59a00
VS
3932 i915_reg_t reg;
3933 u32 temp, i, j;
357555c0
JB
3934
3935 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3936 for train result */
3937 reg = FDI_RX_IMR(pipe);
3938 temp = I915_READ(reg);
3939 temp &= ~FDI_RX_SYMBOL_LOCK;
3940 temp &= ~FDI_RX_BIT_LOCK;
3941 I915_WRITE(reg, temp);
3942
3943 POSTING_READ(reg);
3944 udelay(150);
3945
01a415fd
DV
3946 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3947 I915_READ(FDI_RX_IIR(pipe)));
3948
139ccd3f
JB
3949 /* Try each vswing and preemphasis setting twice before moving on */
3950 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3951 /* disable first in case we need to retry */
3952 reg = FDI_TX_CTL(pipe);
3953 temp = I915_READ(reg);
3954 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3955 temp &= ~FDI_TX_ENABLE;
3956 I915_WRITE(reg, temp);
357555c0 3957
139ccd3f
JB
3958 reg = FDI_RX_CTL(pipe);
3959 temp = I915_READ(reg);
3960 temp &= ~FDI_LINK_TRAIN_AUTO;
3961 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3962 temp &= ~FDI_RX_ENABLE;
3963 I915_WRITE(reg, temp);
357555c0 3964
139ccd3f 3965 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
3966 reg = FDI_TX_CTL(pipe);
3967 temp = I915_READ(reg);
139ccd3f 3968 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3969 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
139ccd3f 3970 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 3971 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
3972 temp |= snb_b_fdi_train_param[j/2];
3973 temp |= FDI_COMPOSITE_SYNC;
3974 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 3975
139ccd3f
JB
3976 I915_WRITE(FDI_RX_MISC(pipe),
3977 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 3978
139ccd3f 3979 reg = FDI_RX_CTL(pipe);
357555c0 3980 temp = I915_READ(reg);
139ccd3f
JB
3981 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3982 temp |= FDI_COMPOSITE_SYNC;
3983 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 3984
139ccd3f
JB
3985 POSTING_READ(reg);
3986 udelay(1); /* should be 0.5us */
357555c0 3987
139ccd3f
JB
3988 for (i = 0; i < 4; i++) {
3989 reg = FDI_RX_IIR(pipe);
3990 temp = I915_READ(reg);
3991 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3992
139ccd3f
JB
3993 if (temp & FDI_RX_BIT_LOCK ||
3994 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3995 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3996 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3997 i);
3998 break;
3999 }
4000 udelay(1); /* should be 0.5us */
4001 }
4002 if (i == 4) {
4003 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
4004 continue;
4005 }
357555c0 4006
139ccd3f 4007 /* Train 2 */
357555c0
JB
4008 reg = FDI_TX_CTL(pipe);
4009 temp = I915_READ(reg);
139ccd3f
JB
4010 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
4011 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
4012 I915_WRITE(reg, temp);
4013
4014 reg = FDI_RX_CTL(pipe);
4015 temp = I915_READ(reg);
4016 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4017 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
4018 I915_WRITE(reg, temp);
4019
4020 POSTING_READ(reg);
139ccd3f 4021 udelay(2); /* should be 1.5us */
357555c0 4022
139ccd3f
JB
4023 for (i = 0; i < 4; i++) {
4024 reg = FDI_RX_IIR(pipe);
4025 temp = I915_READ(reg);
4026 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 4027
139ccd3f
JB
4028 if (temp & FDI_RX_SYMBOL_LOCK ||
4029 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
4030 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
4031 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
4032 i);
4033 goto train_done;
4034 }
4035 udelay(2); /* should be 1.5us */
357555c0 4036 }
139ccd3f
JB
4037 if (i == 4)
4038 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 4039 }
357555c0 4040
139ccd3f 4041train_done:
357555c0
JB
4042 DRM_DEBUG_KMS("FDI train done.\n");
4043}
4044
88cefb6c 4045static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 4046{
88cefb6c 4047 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 4048 struct drm_i915_private *dev_priv = to_i915(dev);
2c07245f 4049 int pipe = intel_crtc->pipe;
f0f59a00
VS
4050 i915_reg_t reg;
4051 u32 temp;
c64e311e 4052
c98e9dcf 4053 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
4054 reg = FDI_RX_CTL(pipe);
4055 temp = I915_READ(reg);
627eb5a3 4056 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
6e3c9717 4057 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
dfd07d72 4058 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
4059 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
4060
4061 POSTING_READ(reg);
c98e9dcf
JB
4062 udelay(200);
4063
4064 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
4065 temp = I915_READ(reg);
4066 I915_WRITE(reg, temp | FDI_PCDCLK);
4067
4068 POSTING_READ(reg);
c98e9dcf
JB
4069 udelay(200);
4070
20749730
PZ
4071 /* Enable CPU FDI TX PLL, always on for Ironlake */
4072 reg = FDI_TX_CTL(pipe);
4073 temp = I915_READ(reg);
4074 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
4075 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 4076
20749730
PZ
4077 POSTING_READ(reg);
4078 udelay(100);
6be4a607 4079 }
0e23b99d
JB
4080}
4081
88cefb6c
DV
4082static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
4083{
4084 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 4085 struct drm_i915_private *dev_priv = to_i915(dev);
88cefb6c 4086 int pipe = intel_crtc->pipe;
f0f59a00
VS
4087 i915_reg_t reg;
4088 u32 temp;
88cefb6c
DV
4089
4090 /* Switch from PCDclk to Rawclk */
4091 reg = FDI_RX_CTL(pipe);
4092 temp = I915_READ(reg);
4093 I915_WRITE(reg, temp & ~FDI_PCDCLK);
4094
4095 /* Disable CPU FDI TX PLL */
4096 reg = FDI_TX_CTL(pipe);
4097 temp = I915_READ(reg);
4098 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
4099
4100 POSTING_READ(reg);
4101 udelay(100);
4102
4103 reg = FDI_RX_CTL(pipe);
4104 temp = I915_READ(reg);
4105 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
4106
4107 /* Wait for the clocks to turn off. */
4108 POSTING_READ(reg);
4109 udelay(100);
4110}
4111
0fc932b8
JB
4112static void ironlake_fdi_disable(struct drm_crtc *crtc)
4113{
4114 struct drm_device *dev = crtc->dev;
fac5e23e 4115 struct drm_i915_private *dev_priv = to_i915(dev);
0fc932b8
JB
4116 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4117 int pipe = intel_crtc->pipe;
f0f59a00
VS
4118 i915_reg_t reg;
4119 u32 temp;
0fc932b8
JB
4120
4121 /* disable CPU FDI tx and PCH FDI rx */
4122 reg = FDI_TX_CTL(pipe);
4123 temp = I915_READ(reg);
4124 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
4125 POSTING_READ(reg);
4126
4127 reg = FDI_RX_CTL(pipe);
4128 temp = I915_READ(reg);
4129 temp &= ~(0x7 << 16);
dfd07d72 4130 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
4131 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
4132
4133 POSTING_READ(reg);
4134 udelay(100);
4135
4136 /* Ironlake workaround, disable clock pointer after downing FDI */
6e266956 4137 if (HAS_PCH_IBX(dev_priv))
6f06ce18 4138 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
0fc932b8
JB
4139
4140 /* still set train pattern 1 */
4141 reg = FDI_TX_CTL(pipe);
4142 temp = I915_READ(reg);
4143 temp &= ~FDI_LINK_TRAIN_NONE;
4144 temp |= FDI_LINK_TRAIN_PATTERN_1;
4145 I915_WRITE(reg, temp);
4146
4147 reg = FDI_RX_CTL(pipe);
4148 temp = I915_READ(reg);
6e266956 4149 if (HAS_PCH_CPT(dev_priv)) {
0fc932b8
JB
4150 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4151 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
4152 } else {
4153 temp &= ~FDI_LINK_TRAIN_NONE;
4154 temp |= FDI_LINK_TRAIN_PATTERN_1;
4155 }
4156 /* BPC in FDI rx is consistent with that in PIPECONF */
4157 temp &= ~(0x07 << 16);
dfd07d72 4158 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
4159 I915_WRITE(reg, temp);
4160
4161 POSTING_READ(reg);
4162 udelay(100);
4163}
4164
49d73912 4165bool intel_has_pending_fb_unpin(struct drm_i915_private *dev_priv)
5dce5b93
CW
4166{
4167 struct intel_crtc *crtc;
4168
4169 /* Note that we don't need to be called with mode_config.lock here
4170 * as our list of CRTC objects is static for the lifetime of the
4171 * device and so cannot disappear as we iterate. Similarly, we can
4172 * happily treat the predicates as racy, atomic checks as userspace
4173 * cannot claim and pin a new fb without at least acquring the
4174 * struct_mutex and so serialising with us.
4175 */
49d73912 4176 for_each_intel_crtc(&dev_priv->drm, crtc) {
5dce5b93
CW
4177 if (atomic_read(&crtc->unpin_work_count) == 0)
4178 continue;
4179
5a21b665 4180 if (crtc->flip_work)
0f0f74bc 4181 intel_wait_for_vblank(dev_priv, crtc->pipe);
5dce5b93
CW
4182
4183 return true;
4184 }
4185
4186 return false;
4187}
4188
5a21b665 4189static void page_flip_completed(struct intel_crtc *intel_crtc)
d6bbafa1
CW
4190{
4191 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
5a21b665
DV
4192 struct intel_flip_work *work = intel_crtc->flip_work;
4193
4194 intel_crtc->flip_work = NULL;
d6bbafa1
CW
4195
4196 if (work->event)
560ce1dc 4197 drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
d6bbafa1
CW
4198
4199 drm_crtc_vblank_put(&intel_crtc->base);
4200
5a21b665 4201 wake_up_all(&dev_priv->pending_flip_queue);
5a21b665
DV
4202 trace_i915_flip_complete(intel_crtc->plane,
4203 work->pending_flip_obj);
05c41f92
AR
4204
4205 queue_work(dev_priv->wq, &work->unpin_work);
d6bbafa1
CW
4206}
4207
5008e874 4208static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
e6c3a2a6 4209{
0f91128d 4210 struct drm_device *dev = crtc->dev;
fac5e23e 4211 struct drm_i915_private *dev_priv = to_i915(dev);
5008e874 4212 long ret;
e6c3a2a6 4213
2c10d571 4214 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
5008e874
ML
4215
4216 ret = wait_event_interruptible_timeout(
4217 dev_priv->pending_flip_queue,
4218 !intel_crtc_has_pending_flip(crtc),
4219 60*HZ);
4220
4221 if (ret < 0)
4222 return ret;
4223
5a21b665
DV
4224 if (ret == 0) {
4225 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4226 struct intel_flip_work *work;
4227
4228 spin_lock_irq(&dev->event_lock);
4229 work = intel_crtc->flip_work;
4230 if (work && !is_mmio_work(work)) {
4231 WARN_ONCE(1, "Removing stuck page flip\n");
4232 page_flip_completed(intel_crtc);
4233 }
4234 spin_unlock_irq(&dev->event_lock);
4235 }
5bb61643 4236
5008e874 4237 return 0;
e6c3a2a6
CW
4238}
4239
b7076546 4240void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
060f02d8
VS
4241{
4242 u32 temp;
4243
4244 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
4245
4246 mutex_lock(&dev_priv->sb_lock);
4247
4248 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4249 temp |= SBI_SSCCTL_DISABLE;
4250 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
4251
4252 mutex_unlock(&dev_priv->sb_lock);
4253}
4254
e615efe4
ED
4255/* Program iCLKIP clock to the desired frequency */
4256static void lpt_program_iclkip(struct drm_crtc *crtc)
4257{
64b46a06 4258 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6e3c9717 4259 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
e615efe4
ED
4260 u32 divsel, phaseinc, auxdiv, phasedir = 0;
4261 u32 temp;
4262
060f02d8 4263 lpt_disable_iclkip(dev_priv);
e615efe4 4264
64b46a06
VS
4265 /* The iCLK virtual clock root frequency is in MHz,
4266 * but the adjusted_mode->crtc_clock in in KHz. To get the
4267 * divisors, it is necessary to divide one by another, so we
4268 * convert the virtual clock precision to KHz here for higher
4269 * precision.
4270 */
4271 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
e615efe4
ED
4272 u32 iclk_virtual_root_freq = 172800 * 1000;
4273 u32 iclk_pi_range = 64;
64b46a06 4274 u32 desired_divisor;
e615efe4 4275
64b46a06
VS
4276 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4277 clock << auxdiv);
4278 divsel = (desired_divisor / iclk_pi_range) - 2;
4279 phaseinc = desired_divisor % iclk_pi_range;
e615efe4 4280
64b46a06
VS
4281 /*
4282 * Near 20MHz is a corner case which is
4283 * out of range for the 7-bit divisor
4284 */
4285 if (divsel <= 0x7f)
4286 break;
e615efe4
ED
4287 }
4288
4289 /* This should not happen with any sane values */
4290 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
4291 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
4292 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
4293 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
4294
4295 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 4296 clock,
e615efe4
ED
4297 auxdiv,
4298 divsel,
4299 phasedir,
4300 phaseinc);
4301
060f02d8
VS
4302 mutex_lock(&dev_priv->sb_lock);
4303
e615efe4 4304 /* Program SSCDIVINTPHASE6 */
988d6ee8 4305 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
4306 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
4307 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
4308 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
4309 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
4310 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
4311 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 4312 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
4313
4314 /* Program SSCAUXDIV */
988d6ee8 4315 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
4316 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
4317 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 4318 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
4319
4320 /* Enable modulator and associated divider */
988d6ee8 4321 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 4322 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 4323 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4 4324
060f02d8
VS
4325 mutex_unlock(&dev_priv->sb_lock);
4326
e615efe4
ED
4327 /* Wait for initialization time */
4328 udelay(24);
4329
4330 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
4331}
4332
8802e5b6
VS
4333int lpt_get_iclkip(struct drm_i915_private *dev_priv)
4334{
4335 u32 divsel, phaseinc, auxdiv;
4336 u32 iclk_virtual_root_freq = 172800 * 1000;
4337 u32 iclk_pi_range = 64;
4338 u32 desired_divisor;
4339 u32 temp;
4340
4341 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
4342 return 0;
4343
4344 mutex_lock(&dev_priv->sb_lock);
4345
4346 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4347 if (temp & SBI_SSCCTL_DISABLE) {
4348 mutex_unlock(&dev_priv->sb_lock);
4349 return 0;
4350 }
4351
4352 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
4353 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
4354 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
4355 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
4356 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
4357
4358 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
4359 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
4360 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
4361
4362 mutex_unlock(&dev_priv->sb_lock);
4363
4364 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
4365
4366 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4367 desired_divisor << auxdiv);
4368}
4369
275f01b2
DV
4370static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4371 enum pipe pch_transcoder)
4372{
4373 struct drm_device *dev = crtc->base.dev;
fac5e23e 4374 struct drm_i915_private *dev_priv = to_i915(dev);
6e3c9717 4375 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
275f01b2
DV
4376
4377 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4378 I915_READ(HTOTAL(cpu_transcoder)));
4379 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4380 I915_READ(HBLANK(cpu_transcoder)));
4381 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4382 I915_READ(HSYNC(cpu_transcoder)));
4383
4384 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4385 I915_READ(VTOTAL(cpu_transcoder)));
4386 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4387 I915_READ(VBLANK(cpu_transcoder)));
4388 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4389 I915_READ(VSYNC(cpu_transcoder)));
4390 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4391 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4392}
4393
003632d9 4394static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
1fbc0d78 4395{
fac5e23e 4396 struct drm_i915_private *dev_priv = to_i915(dev);
1fbc0d78
DV
4397 uint32_t temp;
4398
4399 temp = I915_READ(SOUTH_CHICKEN1);
003632d9 4400 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
1fbc0d78
DV
4401 return;
4402
4403 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4404 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4405
003632d9
ACO
4406 temp &= ~FDI_BC_BIFURCATION_SELECT;
4407 if (enable)
4408 temp |= FDI_BC_BIFURCATION_SELECT;
4409
4410 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
1fbc0d78
DV
4411 I915_WRITE(SOUTH_CHICKEN1, temp);
4412 POSTING_READ(SOUTH_CHICKEN1);
4413}
4414
4415static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4416{
4417 struct drm_device *dev = intel_crtc->base.dev;
1fbc0d78
DV
4418
4419 switch (intel_crtc->pipe) {
4420 case PIPE_A:
4421 break;
4422 case PIPE_B:
6e3c9717 4423 if (intel_crtc->config->fdi_lanes > 2)
003632d9 4424 cpt_set_fdi_bc_bifurcation(dev, false);
1fbc0d78 4425 else
003632d9 4426 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4427
4428 break;
4429 case PIPE_C:
003632d9 4430 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4431
4432 break;
4433 default:
4434 BUG();
4435 }
4436}
4437
c48b5305
VS
4438/* Return which DP Port should be selected for Transcoder DP control */
4439static enum port
4440intel_trans_dp_port_sel(struct drm_crtc *crtc)
4441{
4442 struct drm_device *dev = crtc->dev;
4443 struct intel_encoder *encoder;
4444
4445 for_each_encoder_on_crtc(dev, crtc, encoder) {
cca0502b 4446 if (encoder->type == INTEL_OUTPUT_DP ||
c48b5305
VS
4447 encoder->type == INTEL_OUTPUT_EDP)
4448 return enc_to_dig_port(&encoder->base)->port;
4449 }
4450
4451 return -1;
4452}
4453
f67a559d
JB
4454/*
4455 * Enable PCH resources required for PCH ports:
4456 * - PCH PLLs
4457 * - FDI training & RX/TX
4458 * - update transcoder timings
4459 * - DP transcoding bits
4460 * - transcoder
4461 */
4462static void ironlake_pch_enable(struct drm_crtc *crtc)
0e23b99d
JB
4463{
4464 struct drm_device *dev = crtc->dev;
fac5e23e 4465 struct drm_i915_private *dev_priv = to_i915(dev);
0e23b99d
JB
4466 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4467 int pipe = intel_crtc->pipe;
f0f59a00 4468 u32 temp;
2c07245f 4469
ab9412ba 4470 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 4471
fd6b8f43 4472 if (IS_IVYBRIDGE(dev_priv))
1fbc0d78
DV
4473 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4474
cd986abb
DV
4475 /* Write the TU size bits before fdi link training, so that error
4476 * detection works. */
4477 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4478 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4479
c98e9dcf 4480 /* For PCH output, training FDI link */
674cf967 4481 dev_priv->display.fdi_link_train(crtc);
2c07245f 4482
3ad8a208
DV
4483 /* We need to program the right clock selection before writing the pixel
4484 * mutliplier into the DPLL. */
6e266956 4485 if (HAS_PCH_CPT(dev_priv)) {
ee7b9f93 4486 u32 sel;
4b645f14 4487
c98e9dcf 4488 temp = I915_READ(PCH_DPLL_SEL);
11887397
DV
4489 temp |= TRANS_DPLL_ENABLE(pipe);
4490 sel = TRANS_DPLLB_SEL(pipe);
8106ddbd
ACO
4491 if (intel_crtc->config->shared_dpll ==
4492 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
ee7b9f93
JB
4493 temp |= sel;
4494 else
4495 temp &= ~sel;
c98e9dcf 4496 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 4497 }
5eddb70b 4498
3ad8a208
DV
4499 /* XXX: pch pll's can be enabled any time before we enable the PCH
4500 * transcoder, and we actually should do this to not upset any PCH
4501 * transcoder that already use the clock when we share it.
4502 *
4503 * Note that enable_shared_dpll tries to do the right thing, but
4504 * get_shared_dpll unconditionally resets the pll - we need that to have
4505 * the right LVDS enable sequence. */
85b3894f 4506 intel_enable_shared_dpll(intel_crtc);
3ad8a208 4507
d9b6cb56
JB
4508 /* set transcoder timing, panel must allow it */
4509 assert_panel_unlocked(dev_priv, pipe);
275f01b2 4510 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
8db9d77b 4511
303b81e0 4512 intel_fdi_normal_train(crtc);
5e84e1a4 4513
c98e9dcf 4514 /* For PCH DP, enable TRANS_DP_CTL */
6e266956
TU
4515 if (HAS_PCH_CPT(dev_priv) &&
4516 intel_crtc_has_dp_encoder(intel_crtc->config)) {
9c4edaee
VS
4517 const struct drm_display_mode *adjusted_mode =
4518 &intel_crtc->config->base.adjusted_mode;
dfd07d72 4519 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
f0f59a00 4520 i915_reg_t reg = TRANS_DP_CTL(pipe);
5eddb70b
CW
4521 temp = I915_READ(reg);
4522 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
4523 TRANS_DP_SYNC_MASK |
4524 TRANS_DP_BPC_MASK);
e3ef4479 4525 temp |= TRANS_DP_OUTPUT_ENABLE;
9325c9f0 4526 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf 4527
9c4edaee 4528 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 4529 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
9c4edaee 4530 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 4531 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf
JB
4532
4533 switch (intel_trans_dp_port_sel(crtc)) {
c48b5305 4534 case PORT_B:
5eddb70b 4535 temp |= TRANS_DP_PORT_SEL_B;
c98e9dcf 4536 break;
c48b5305 4537 case PORT_C:
5eddb70b 4538 temp |= TRANS_DP_PORT_SEL_C;
c98e9dcf 4539 break;
c48b5305 4540 case PORT_D:
5eddb70b 4541 temp |= TRANS_DP_PORT_SEL_D;
c98e9dcf
JB
4542 break;
4543 default:
e95d41e1 4544 BUG();
32f9d658 4545 }
2c07245f 4546
5eddb70b 4547 I915_WRITE(reg, temp);
6be4a607 4548 }
b52eb4dc 4549
b8a4f404 4550 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
4551}
4552
1507e5bd
PZ
4553static void lpt_pch_enable(struct drm_crtc *crtc)
4554{
4555 struct drm_device *dev = crtc->dev;
fac5e23e 4556 struct drm_i915_private *dev_priv = to_i915(dev);
1507e5bd 4557 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 4558 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
1507e5bd 4559
ab9412ba 4560 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
1507e5bd 4561
8c52b5e8 4562 lpt_program_iclkip(crtc);
1507e5bd 4563
0540e488 4564 /* Set transcoder timing. */
275f01b2 4565 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
1507e5bd 4566
937bb610 4567 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
4568}
4569
a1520318 4570static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57 4571{
fac5e23e 4572 struct drm_i915_private *dev_priv = to_i915(dev);
f0f59a00 4573 i915_reg_t dslreg = PIPEDSL(pipe);
d4270e57
JB
4574 u32 temp;
4575
4576 temp = I915_READ(dslreg);
4577 udelay(500);
4578 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 4579 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 4580 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
4581 }
4582}
4583
86adf9d7
ML
4584static int
4585skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4586 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4587 int src_w, int src_h, int dst_w, int dst_h)
a1b2278e 4588{
86adf9d7
ML
4589 struct intel_crtc_scaler_state *scaler_state =
4590 &crtc_state->scaler_state;
4591 struct intel_crtc *intel_crtc =
4592 to_intel_crtc(crtc_state->base.crtc);
a1b2278e 4593 int need_scaling;
6156a456 4594
bd2ef25d 4595 need_scaling = drm_rotation_90_or_270(rotation) ?
6156a456
CK
4596 (src_h != dst_w || src_w != dst_h):
4597 (src_w != dst_w || src_h != dst_h);
a1b2278e
CK
4598
4599 /*
4600 * if plane is being disabled or scaler is no more required or force detach
4601 * - free scaler binded to this plane/crtc
4602 * - in order to do this, update crtc->scaler_usage
4603 *
4604 * Here scaler state in crtc_state is set free so that
4605 * scaler can be assigned to other user. Actual register
4606 * update to free the scaler is done in plane/panel-fit programming.
4607 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4608 */
86adf9d7 4609 if (force_detach || !need_scaling) {
a1b2278e 4610 if (*scaler_id >= 0) {
86adf9d7 4611 scaler_state->scaler_users &= ~(1 << scaler_user);
a1b2278e
CK
4612 scaler_state->scalers[*scaler_id].in_use = 0;
4613
86adf9d7
ML
4614 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4615 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4616 intel_crtc->pipe, scaler_user, *scaler_id,
a1b2278e
CK
4617 scaler_state->scaler_users);
4618 *scaler_id = -1;
4619 }
4620 return 0;
4621 }
4622
4623 /* range checks */
4624 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4625 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4626
4627 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4628 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
86adf9d7 4629 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
a1b2278e 4630 "size is out of scaler range\n",
86adf9d7 4631 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
a1b2278e
CK
4632 return -EINVAL;
4633 }
4634
86adf9d7
ML
4635 /* mark this plane as a scaler user in crtc_state */
4636 scaler_state->scaler_users |= (1 << scaler_user);
4637 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4638 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4639 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4640 scaler_state->scaler_users);
4641
4642 return 0;
4643}
4644
4645/**
4646 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4647 *
4648 * @state: crtc's scaler state
86adf9d7
ML
4649 *
4650 * Return
4651 * 0 - scaler_usage updated successfully
4652 * error - requested scaling cannot be supported or other error condition
4653 */
e435d6e5 4654int skl_update_scaler_crtc(struct intel_crtc_state *state)
86adf9d7 4655{
7c5f93b0 4656 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
86adf9d7 4657
e435d6e5 4658 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
31ad61e4 4659 &state->scaler_state.scaler_id, DRM_ROTATE_0,
86adf9d7 4660 state->pipe_src_w, state->pipe_src_h,
aad941d5 4661 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
86adf9d7
ML
4662}
4663
4664/**
4665 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4666 *
4667 * @state: crtc's scaler state
86adf9d7
ML
4668 * @plane_state: atomic plane state to update
4669 *
4670 * Return
4671 * 0 - scaler_usage updated successfully
4672 * error - requested scaling cannot be supported or other error condition
4673 */
da20eabd
ML
4674static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4675 struct intel_plane_state *plane_state)
86adf9d7
ML
4676{
4677
da20eabd
ML
4678 struct intel_plane *intel_plane =
4679 to_intel_plane(plane_state->base.plane);
86adf9d7
ML
4680 struct drm_framebuffer *fb = plane_state->base.fb;
4681 int ret;
4682
936e71e3 4683 bool force_detach = !fb || !plane_state->base.visible;
86adf9d7 4684
86adf9d7
ML
4685 ret = skl_update_scaler(crtc_state, force_detach,
4686 drm_plane_index(&intel_plane->base),
4687 &plane_state->scaler_id,
4688 plane_state->base.rotation,
936e71e3
VS
4689 drm_rect_width(&plane_state->base.src) >> 16,
4690 drm_rect_height(&plane_state->base.src) >> 16,
4691 drm_rect_width(&plane_state->base.dst),
4692 drm_rect_height(&plane_state->base.dst));
86adf9d7
ML
4693
4694 if (ret || plane_state->scaler_id < 0)
4695 return ret;
4696
a1b2278e 4697 /* check colorkey */
818ed961 4698 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
72660ce0
VS
4699 DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
4700 intel_plane->base.base.id,
4701 intel_plane->base.name);
a1b2278e
CK
4702 return -EINVAL;
4703 }
4704
4705 /* Check src format */
438b74a5 4706 switch (fb->format->format) {
86adf9d7
ML
4707 case DRM_FORMAT_RGB565:
4708 case DRM_FORMAT_XBGR8888:
4709 case DRM_FORMAT_XRGB8888:
4710 case DRM_FORMAT_ABGR8888:
4711 case DRM_FORMAT_ARGB8888:
4712 case DRM_FORMAT_XRGB2101010:
4713 case DRM_FORMAT_XBGR2101010:
4714 case DRM_FORMAT_YUYV:
4715 case DRM_FORMAT_YVYU:
4716 case DRM_FORMAT_UYVY:
4717 case DRM_FORMAT_VYUY:
4718 break;
4719 default:
72660ce0
VS
4720 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
4721 intel_plane->base.base.id, intel_plane->base.name,
438b74a5 4722 fb->base.id, fb->format->format);
86adf9d7 4723 return -EINVAL;
a1b2278e
CK
4724 }
4725
a1b2278e
CK
4726 return 0;
4727}
4728
e435d6e5
ML
4729static void skylake_scaler_disable(struct intel_crtc *crtc)
4730{
4731 int i;
4732
4733 for (i = 0; i < crtc->num_scalers; i++)
4734 skl_detach_scaler(crtc, i);
4735}
4736
4737static void skylake_pfit_enable(struct intel_crtc *crtc)
bd2e244f
JB
4738{
4739 struct drm_device *dev = crtc->base.dev;
fac5e23e 4740 struct drm_i915_private *dev_priv = to_i915(dev);
bd2e244f 4741 int pipe = crtc->pipe;
a1b2278e
CK
4742 struct intel_crtc_scaler_state *scaler_state =
4743 &crtc->config->scaler_state;
4744
4745 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4746
6e3c9717 4747 if (crtc->config->pch_pfit.enabled) {
a1b2278e
CK
4748 int id;
4749
4750 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4751 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4752 return;
4753 }
4754
4755 id = scaler_state->scaler_id;
4756 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4757 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4758 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4759 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4760
4761 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
bd2e244f
JB
4762 }
4763}
4764
b074cec8
JB
4765static void ironlake_pfit_enable(struct intel_crtc *crtc)
4766{
4767 struct drm_device *dev = crtc->base.dev;
fac5e23e 4768 struct drm_i915_private *dev_priv = to_i915(dev);
b074cec8
JB
4769 int pipe = crtc->pipe;
4770
6e3c9717 4771 if (crtc->config->pch_pfit.enabled) {
b074cec8
JB
4772 /* Force use of hard-coded filter coefficients
4773 * as some pre-programmed values are broken,
4774 * e.g. x201.
4775 */
fd6b8f43 4776 if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
b074cec8
JB
4777 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4778 PF_PIPE_SEL_IVB(pipe));
4779 else
4780 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
6e3c9717
ACO
4781 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4782 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
d4270e57
JB
4783 }
4784}
4785
20bc8673 4786void hsw_enable_ips(struct intel_crtc *crtc)
d77e4531 4787{
cea165c3 4788 struct drm_device *dev = crtc->base.dev;
fac5e23e 4789 struct drm_i915_private *dev_priv = to_i915(dev);
d77e4531 4790
6e3c9717 4791 if (!crtc->config->ips_enabled)
d77e4531
PZ
4792 return;
4793
307e4498
ML
4794 /*
4795 * We can only enable IPS after we enable a plane and wait for a vblank
4796 * This function is called from post_plane_update, which is run after
4797 * a vblank wait.
4798 */
cea165c3 4799
d77e4531 4800 assert_plane_enabled(dev_priv, crtc->plane);
8652744b 4801 if (IS_BROADWELL(dev_priv)) {
2a114cc1
BW
4802 mutex_lock(&dev_priv->rps.hw_lock);
4803 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4804 mutex_unlock(&dev_priv->rps.hw_lock);
4805 /* Quoting Art Runyan: "its not safe to expect any particular
4806 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
4807 * mailbox." Moreover, the mailbox may return a bogus state,
4808 * so we need to just enable it and continue on.
2a114cc1
BW
4809 */
4810 } else {
4811 I915_WRITE(IPS_CTL, IPS_ENABLE);
4812 /* The bit only becomes 1 in the next vblank, so this wait here
4813 * is essentially intel_wait_for_vblank. If we don't have this
4814 * and don't wait for vblanks until the end of crtc_enable, then
4815 * the HW state readout code will complain that the expected
4816 * IPS_CTL value is not the one we read. */
2ec9ba3c
CW
4817 if (intel_wait_for_register(dev_priv,
4818 IPS_CTL, IPS_ENABLE, IPS_ENABLE,
4819 50))
2a114cc1
BW
4820 DRM_ERROR("Timed out waiting for IPS enable\n");
4821 }
d77e4531
PZ
4822}
4823
20bc8673 4824void hsw_disable_ips(struct intel_crtc *crtc)
d77e4531
PZ
4825{
4826 struct drm_device *dev = crtc->base.dev;
fac5e23e 4827 struct drm_i915_private *dev_priv = to_i915(dev);
d77e4531 4828
6e3c9717 4829 if (!crtc->config->ips_enabled)
d77e4531
PZ
4830 return;
4831
4832 assert_plane_enabled(dev_priv, crtc->plane);
8652744b 4833 if (IS_BROADWELL(dev_priv)) {
2a114cc1
BW
4834 mutex_lock(&dev_priv->rps.hw_lock);
4835 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4836 mutex_unlock(&dev_priv->rps.hw_lock);
23d0b130 4837 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
b85c1ecf
CW
4838 if (intel_wait_for_register(dev_priv,
4839 IPS_CTL, IPS_ENABLE, 0,
4840 42))
23d0b130 4841 DRM_ERROR("Timed out waiting for IPS disable\n");
e59150dc 4842 } else {
2a114cc1 4843 I915_WRITE(IPS_CTL, 0);
e59150dc
JB
4844 POSTING_READ(IPS_CTL);
4845 }
d77e4531
PZ
4846
4847 /* We need to wait for a vblank before we can disable the plane. */
0f0f74bc 4848 intel_wait_for_vblank(dev_priv, crtc->pipe);
d77e4531
PZ
4849}
4850
7cac945f 4851static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
d3eedb1a 4852{
7cac945f 4853 if (intel_crtc->overlay) {
d3eedb1a 4854 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 4855 struct drm_i915_private *dev_priv = to_i915(dev);
d3eedb1a
VS
4856
4857 mutex_lock(&dev->struct_mutex);
4858 dev_priv->mm.interruptible = false;
4859 (void) intel_overlay_switch_off(intel_crtc->overlay);
4860 dev_priv->mm.interruptible = true;
4861 mutex_unlock(&dev->struct_mutex);
4862 }
4863
4864 /* Let userspace switch the overlay on again. In most cases userspace
4865 * has to recompute where to put it anyway.
4866 */
4867}
4868
87d4300a
ML
4869/**
4870 * intel_post_enable_primary - Perform operations after enabling primary plane
4871 * @crtc: the CRTC whose primary plane was just enabled
4872 *
4873 * Performs potentially sleeping operations that must be done after the primary
4874 * plane is enabled, such as updating FBC and IPS. Note that this may be
4875 * called due to an explicit primary plane update, or due to an implicit
4876 * re-enable that is caused when a sprite plane is updated to no longer
4877 * completely hide the primary plane.
4878 */
4879static void
4880intel_post_enable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4881{
4882 struct drm_device *dev = crtc->dev;
fac5e23e 4883 struct drm_i915_private *dev_priv = to_i915(dev);
a5c4d7bc
VS
4884 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4885 int pipe = intel_crtc->pipe;
a5c4d7bc 4886
87d4300a
ML
4887 /*
4888 * FIXME IPS should be fine as long as one plane is
4889 * enabled, but in practice it seems to have problems
4890 * when going from primary only to sprite only and vice
4891 * versa.
4892 */
a5c4d7bc
VS
4893 hsw_enable_ips(intel_crtc);
4894
f99d7069 4895 /*
87d4300a
ML
4896 * Gen2 reports pipe underruns whenever all planes are disabled.
4897 * So don't enable underrun reporting before at least some planes
4898 * are enabled.
4899 * FIXME: Need to fix the logic to work when we turn off all planes
4900 * but leave the pipe running.
f99d7069 4901 */
5db94019 4902 if (IS_GEN2(dev_priv))
87d4300a
ML
4903 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4904
aca7b684
VS
4905 /* Underruns don't always raise interrupts, so check manually. */
4906 intel_check_cpu_fifo_underruns(dev_priv);
4907 intel_check_pch_fifo_underruns(dev_priv);
a5c4d7bc
VS
4908}
4909
2622a081 4910/* FIXME move all this to pre_plane_update() with proper state tracking */
87d4300a
ML
4911static void
4912intel_pre_disable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4913{
4914 struct drm_device *dev = crtc->dev;
fac5e23e 4915 struct drm_i915_private *dev_priv = to_i915(dev);
a5c4d7bc
VS
4916 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4917 int pipe = intel_crtc->pipe;
a5c4d7bc 4918
87d4300a
ML
4919 /*
4920 * Gen2 reports pipe underruns whenever all planes are disabled.
4921 * So diasble underrun reporting before all the planes get disabled.
4922 * FIXME: Need to fix the logic to work when we turn off all planes
4923 * but leave the pipe running.
4924 */
5db94019 4925 if (IS_GEN2(dev_priv))
87d4300a 4926 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
a5c4d7bc 4927
2622a081
VS
4928 /*
4929 * FIXME IPS should be fine as long as one plane is
4930 * enabled, but in practice it seems to have problems
4931 * when going from primary only to sprite only and vice
4932 * versa.
4933 */
4934 hsw_disable_ips(intel_crtc);
4935}
4936
4937/* FIXME get rid of this and use pre_plane_update */
4938static void
4939intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
4940{
4941 struct drm_device *dev = crtc->dev;
fac5e23e 4942 struct drm_i915_private *dev_priv = to_i915(dev);
2622a081
VS
4943 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4944 int pipe = intel_crtc->pipe;
4945
4946 intel_pre_disable_primary(crtc);
4947
87d4300a
ML
4948 /*
4949 * Vblank time updates from the shadow to live plane control register
4950 * are blocked if the memory self-refresh mode is active at that
4951 * moment. So to make sure the plane gets truly disabled, disable
4952 * first the self-refresh mode. The self-refresh enable bit in turn
4953 * will be checked/applied by the HW only at the next frame start
4954 * event which is after the vblank start event, so we need to have a
4955 * wait-for-vblank between disabling the plane and the pipe.
4956 */
11a85d6a
VS
4957 if (HAS_GMCH_DISPLAY(dev_priv) &&
4958 intel_set_memory_cxsr(dev_priv, false))
0f0f74bc 4959 intel_wait_for_vblank(dev_priv, pipe);
87d4300a
ML
4960}
4961
5a21b665
DV
4962static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
4963{
4964 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
4965 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4966 struct intel_crtc_state *pipe_config =
4967 to_intel_crtc_state(crtc->base.state);
5a21b665
DV
4968 struct drm_plane *primary = crtc->base.primary;
4969 struct drm_plane_state *old_pri_state =
4970 drm_atomic_get_existing_plane_state(old_state, primary);
4971
5748b6a1 4972 intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
5a21b665
DV
4973
4974 crtc->wm.cxsr_allowed = true;
4975
4976 if (pipe_config->update_wm_post && pipe_config->base.active)
432081bc 4977 intel_update_watermarks(crtc);
5a21b665
DV
4978
4979 if (old_pri_state) {
4980 struct intel_plane_state *primary_state =
4981 to_intel_plane_state(primary->state);
4982 struct intel_plane_state *old_primary_state =
4983 to_intel_plane_state(old_pri_state);
4984
4985 intel_fbc_post_update(crtc);
4986
936e71e3 4987 if (primary_state->base.visible &&
5a21b665 4988 (needs_modeset(&pipe_config->base) ||
936e71e3 4989 !old_primary_state->base.visible))
5a21b665
DV
4990 intel_post_enable_primary(&crtc->base);
4991 }
4992}
4993
5c74cd73 4994static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
ac21b225 4995{
5c74cd73 4996 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
ac21b225 4997 struct drm_device *dev = crtc->base.dev;
fac5e23e 4998 struct drm_i915_private *dev_priv = to_i915(dev);
ab1d3a0e
ML
4999 struct intel_crtc_state *pipe_config =
5000 to_intel_crtc_state(crtc->base.state);
5c74cd73
ML
5001 struct drm_atomic_state *old_state = old_crtc_state->base.state;
5002 struct drm_plane *primary = crtc->base.primary;
5003 struct drm_plane_state *old_pri_state =
5004 drm_atomic_get_existing_plane_state(old_state, primary);
5005 bool modeset = needs_modeset(&pipe_config->base);
ccf010fb
ML
5006 struct intel_atomic_state *old_intel_state =
5007 to_intel_atomic_state(old_state);
ac21b225 5008
5c74cd73
ML
5009 if (old_pri_state) {
5010 struct intel_plane_state *primary_state =
5011 to_intel_plane_state(primary->state);
5012 struct intel_plane_state *old_primary_state =
5013 to_intel_plane_state(old_pri_state);
5014
faf68d92 5015 intel_fbc_pre_update(crtc, pipe_config, primary_state);
31ae71fc 5016
936e71e3
VS
5017 if (old_primary_state->base.visible &&
5018 (modeset || !primary_state->base.visible))
5c74cd73
ML
5019 intel_pre_disable_primary(&crtc->base);
5020 }
852eb00d 5021
49cff963 5022 if (pipe_config->disable_cxsr && HAS_GMCH_DISPLAY(dev_priv)) {
852eb00d 5023 crtc->wm.cxsr_allowed = false;
2dfd178d 5024
2622a081
VS
5025 /*
5026 * Vblank time updates from the shadow to live plane control register
5027 * are blocked if the memory self-refresh mode is active at that
5028 * moment. So to make sure the plane gets truly disabled, disable
5029 * first the self-refresh mode. The self-refresh enable bit in turn
5030 * will be checked/applied by the HW only at the next frame start
5031 * event which is after the vblank start event, so we need to have a
5032 * wait-for-vblank between disabling the plane and the pipe.
5033 */
11a85d6a
VS
5034 if (old_crtc_state->base.active &&
5035 intel_set_memory_cxsr(dev_priv, false))
0f0f74bc 5036 intel_wait_for_vblank(dev_priv, crtc->pipe);
852eb00d 5037 }
92826fcd 5038
ed4a6a7c
MR
5039 /*
5040 * IVB workaround: must disable low power watermarks for at least
5041 * one frame before enabling scaling. LP watermarks can be re-enabled
5042 * when scaling is disabled.
5043 *
5044 * WaCxSRDisabledForSpriteScaling:ivb
5045 */
ddd2b792 5046 if (pipe_config->disable_lp_wm && ilk_disable_lp_wm(dev))
0f0f74bc 5047 intel_wait_for_vblank(dev_priv, crtc->pipe);
ed4a6a7c
MR
5048
5049 /*
5050 * If we're doing a modeset, we're done. No need to do any pre-vblank
5051 * watermark programming here.
5052 */
5053 if (needs_modeset(&pipe_config->base))
5054 return;
5055
5056 /*
5057 * For platforms that support atomic watermarks, program the
5058 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
5059 * will be the intermediate values that are safe for both pre- and
5060 * post- vblank; when vblank happens, the 'active' values will be set
5061 * to the final 'target' values and we'll do this again to get the
5062 * optimal watermarks. For gen9+ platforms, the values we program here
5063 * will be the final target values which will get automatically latched
5064 * at vblank time; no further programming will be necessary.
5065 *
5066 * If a platform hasn't been transitioned to atomic watermarks yet,
5067 * we'll continue to update watermarks the old way, if flags tell
5068 * us to.
5069 */
5070 if (dev_priv->display.initial_watermarks != NULL)
ccf010fb
ML
5071 dev_priv->display.initial_watermarks(old_intel_state,
5072 pipe_config);
caed361d 5073 else if (pipe_config->update_wm_pre)
432081bc 5074 intel_update_watermarks(crtc);
ac21b225
ML
5075}
5076
d032ffa0 5077static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
87d4300a
ML
5078{
5079 struct drm_device *dev = crtc->dev;
5080 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
d032ffa0 5081 struct drm_plane *p;
87d4300a
ML
5082 int pipe = intel_crtc->pipe;
5083
7cac945f 5084 intel_crtc_dpms_overlay_disable(intel_crtc);
27321ae8 5085
d032ffa0
ML
5086 drm_for_each_plane_mask(p, dev, plane_mask)
5087 to_intel_plane(p)->disable_plane(p, crtc);
f98551ae 5088
f99d7069
DV
5089 /*
5090 * FIXME: Once we grow proper nuclear flip support out of this we need
5091 * to compute the mask of flip planes precisely. For the time being
5092 * consider this a flip to a NULL plane.
5093 */
5748b6a1 5094 intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
5095}
5096
fb1c98b1 5097static void intel_encoders_pre_pll_enable(struct drm_crtc *crtc,
fd6bbda9 5098 struct intel_crtc_state *crtc_state,
fb1c98b1
ML
5099 struct drm_atomic_state *old_state)
5100{
5101 struct drm_connector_state *old_conn_state;
5102 struct drm_connector *conn;
5103 int i;
5104
5105 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5106 struct drm_connector_state *conn_state = conn->state;
5107 struct intel_encoder *encoder =
5108 to_intel_encoder(conn_state->best_encoder);
5109
5110 if (conn_state->crtc != crtc)
5111 continue;
5112
5113 if (encoder->pre_pll_enable)
fd6bbda9 5114 encoder->pre_pll_enable(encoder, crtc_state, conn_state);
fb1c98b1
ML
5115 }
5116}
5117
5118static void intel_encoders_pre_enable(struct drm_crtc *crtc,
fd6bbda9 5119 struct intel_crtc_state *crtc_state,
fb1c98b1
ML
5120 struct drm_atomic_state *old_state)
5121{
5122 struct drm_connector_state *old_conn_state;
5123 struct drm_connector *conn;
5124 int i;
5125
5126 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5127 struct drm_connector_state *conn_state = conn->state;
5128 struct intel_encoder *encoder =
5129 to_intel_encoder(conn_state->best_encoder);
5130
5131 if (conn_state->crtc != crtc)
5132 continue;
5133
5134 if (encoder->pre_enable)
fd6bbda9 5135 encoder->pre_enable(encoder, crtc_state, conn_state);
fb1c98b1
ML
5136 }
5137}
5138
5139static void intel_encoders_enable(struct drm_crtc *crtc,
fd6bbda9 5140 struct intel_crtc_state *crtc_state,
fb1c98b1
ML
5141 struct drm_atomic_state *old_state)
5142{
5143 struct drm_connector_state *old_conn_state;
5144 struct drm_connector *conn;
5145 int i;
5146
5147 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5148 struct drm_connector_state *conn_state = conn->state;
5149 struct intel_encoder *encoder =
5150 to_intel_encoder(conn_state->best_encoder);
5151
5152 if (conn_state->crtc != crtc)
5153 continue;
5154
fd6bbda9 5155 encoder->enable(encoder, crtc_state, conn_state);
fb1c98b1
ML
5156 intel_opregion_notify_encoder(encoder, true);
5157 }
5158}
5159
5160static void intel_encoders_disable(struct drm_crtc *crtc,
fd6bbda9 5161 struct intel_crtc_state *old_crtc_state,
fb1c98b1
ML
5162 struct drm_atomic_state *old_state)
5163{
5164 struct drm_connector_state *old_conn_state;
5165 struct drm_connector *conn;
5166 int i;
5167
5168 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5169 struct intel_encoder *encoder =
5170 to_intel_encoder(old_conn_state->best_encoder);
5171
5172 if (old_conn_state->crtc != crtc)
5173 continue;
5174
5175 intel_opregion_notify_encoder(encoder, false);
fd6bbda9 5176 encoder->disable(encoder, old_crtc_state, old_conn_state);
fb1c98b1
ML
5177 }
5178}
5179
5180static void intel_encoders_post_disable(struct drm_crtc *crtc,
fd6bbda9 5181 struct intel_crtc_state *old_crtc_state,
fb1c98b1
ML
5182 struct drm_atomic_state *old_state)
5183{
5184 struct drm_connector_state *old_conn_state;
5185 struct drm_connector *conn;
5186 int i;
5187
5188 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5189 struct intel_encoder *encoder =
5190 to_intel_encoder(old_conn_state->best_encoder);
5191
5192 if (old_conn_state->crtc != crtc)
5193 continue;
5194
5195 if (encoder->post_disable)
fd6bbda9 5196 encoder->post_disable(encoder, old_crtc_state, old_conn_state);
fb1c98b1
ML
5197 }
5198}
5199
5200static void intel_encoders_post_pll_disable(struct drm_crtc *crtc,
fd6bbda9 5201 struct intel_crtc_state *old_crtc_state,
fb1c98b1
ML
5202 struct drm_atomic_state *old_state)
5203{
5204 struct drm_connector_state *old_conn_state;
5205 struct drm_connector *conn;
5206 int i;
5207
5208 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5209 struct intel_encoder *encoder =
5210 to_intel_encoder(old_conn_state->best_encoder);
5211
5212 if (old_conn_state->crtc != crtc)
5213 continue;
5214
5215 if (encoder->post_pll_disable)
fd6bbda9 5216 encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
fb1c98b1
ML
5217 }
5218}
5219
4a806558
ML
5220static void ironlake_crtc_enable(struct intel_crtc_state *pipe_config,
5221 struct drm_atomic_state *old_state)
f67a559d 5222{
4a806558 5223 struct drm_crtc *crtc = pipe_config->base.crtc;
f67a559d 5224 struct drm_device *dev = crtc->dev;
fac5e23e 5225 struct drm_i915_private *dev_priv = to_i915(dev);
f67a559d
JB
5226 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5227 int pipe = intel_crtc->pipe;
ccf010fb
ML
5228 struct intel_atomic_state *old_intel_state =
5229 to_intel_atomic_state(old_state);
f67a559d 5230
53d9f4e9 5231 if (WARN_ON(intel_crtc->active))
f67a559d
JB
5232 return;
5233
b2c0593a
VS
5234 /*
5235 * Sometimes spurious CPU pipe underruns happen during FDI
5236 * training, at least with VGA+HDMI cloning. Suppress them.
5237 *
5238 * On ILK we get an occasional spurious CPU pipe underruns
5239 * between eDP port A enable and vdd enable. Also PCH port
5240 * enable seems to result in the occasional CPU pipe underrun.
5241 *
5242 * Spurious PCH underruns also occur during PCH enabling.
5243 */
5244 if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
5245 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
81b088ca
VS
5246 if (intel_crtc->config->has_pch_encoder)
5247 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
5248
6e3c9717 5249 if (intel_crtc->config->has_pch_encoder)
b14b1055
DV
5250 intel_prepare_shared_dpll(intel_crtc);
5251
37a5650b 5252 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 5253 intel_dp_set_m_n(intel_crtc, M1_N1);
29407aab
DV
5254
5255 intel_set_pipe_timings(intel_crtc);
bc58be60 5256 intel_set_pipe_src_size(intel_crtc);
29407aab 5257
6e3c9717 5258 if (intel_crtc->config->has_pch_encoder) {
29407aab 5259 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 5260 &intel_crtc->config->fdi_m_n, NULL);
29407aab
DV
5261 }
5262
5263 ironlake_set_pipeconf(crtc);
5264
f67a559d 5265 intel_crtc->active = true;
8664281b 5266
fd6bbda9 5267 intel_encoders_pre_enable(crtc, pipe_config, old_state);
f67a559d 5268
6e3c9717 5269 if (intel_crtc->config->has_pch_encoder) {
fff367c7
DV
5270 /* Note: FDI PLL enabling _must_ be done before we enable the
5271 * cpu pipes, hence this is separate from all the other fdi/pch
5272 * enabling. */
88cefb6c 5273 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
DV
5274 } else {
5275 assert_fdi_tx_disabled(dev_priv, pipe);
5276 assert_fdi_rx_disabled(dev_priv, pipe);
5277 }
f67a559d 5278
b074cec8 5279 ironlake_pfit_enable(intel_crtc);
f67a559d 5280
9c54c0dd
JB
5281 /*
5282 * On ILK+ LUT must be loaded before the pipe is running but with
5283 * clocks enabled
5284 */
b95c5321 5285 intel_color_load_luts(&pipe_config->base);
9c54c0dd 5286
1d5bf5d9 5287 if (dev_priv->display.initial_watermarks != NULL)
ccf010fb 5288 dev_priv->display.initial_watermarks(old_intel_state, intel_crtc->config);
e1fdc473 5289 intel_enable_pipe(intel_crtc);
f67a559d 5290
6e3c9717 5291 if (intel_crtc->config->has_pch_encoder)
f67a559d 5292 ironlake_pch_enable(crtc);
c98e9dcf 5293
f9b61ff6
DV
5294 assert_vblank_disabled(crtc);
5295 drm_crtc_vblank_on(crtc);
5296
fd6bbda9 5297 intel_encoders_enable(crtc, pipe_config, old_state);
61b77ddd 5298
6e266956 5299 if (HAS_PCH_CPT(dev_priv))
a1520318 5300 cpt_verify_modeset(dev, intel_crtc->pipe);
37ca8d4c
VS
5301
5302 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
5303 if (intel_crtc->config->has_pch_encoder)
0f0f74bc 5304 intel_wait_for_vblank(dev_priv, pipe);
b2c0593a 5305 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
37ca8d4c 5306 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607
JB
5307}
5308
42db64ef
PZ
5309/* IPS only exists on ULT machines and is tied to pipe A. */
5310static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
5311{
50a0bc90 5312 return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
42db64ef
PZ
5313}
5314
4a806558
ML
5315static void haswell_crtc_enable(struct intel_crtc_state *pipe_config,
5316 struct drm_atomic_state *old_state)
4f771f10 5317{
4a806558 5318 struct drm_crtc *crtc = pipe_config->base.crtc;
6315b5d3 5319 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
4f771f10 5320 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
99d736a2 5321 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
4d1de975 5322 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ccf010fb
ML
5323 struct intel_atomic_state *old_intel_state =
5324 to_intel_atomic_state(old_state);
4f771f10 5325
53d9f4e9 5326 if (WARN_ON(intel_crtc->active))
4f771f10
PZ
5327 return;
5328
81b088ca
VS
5329 if (intel_crtc->config->has_pch_encoder)
5330 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5331 false);
5332
fd6bbda9 5333 intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
95a7a2ae 5334
8106ddbd 5335 if (intel_crtc->config->shared_dpll)
df8ad70c
DV
5336 intel_enable_shared_dpll(intel_crtc);
5337
37a5650b 5338 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 5339 intel_dp_set_m_n(intel_crtc, M1_N1);
229fca97 5340
d7edc4e5 5341 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975
JN
5342 intel_set_pipe_timings(intel_crtc);
5343
bc58be60 5344 intel_set_pipe_src_size(intel_crtc);
229fca97 5345
4d1de975
JN
5346 if (cpu_transcoder != TRANSCODER_EDP &&
5347 !transcoder_is_dsi(cpu_transcoder)) {
5348 I915_WRITE(PIPE_MULT(cpu_transcoder),
6e3c9717 5349 intel_crtc->config->pixel_multiplier - 1);
ebb69c95
CT
5350 }
5351
6e3c9717 5352 if (intel_crtc->config->has_pch_encoder) {
229fca97 5353 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 5354 &intel_crtc->config->fdi_m_n, NULL);
229fca97
DV
5355 }
5356
d7edc4e5 5357 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975
JN
5358 haswell_set_pipeconf(crtc);
5359
391bf048 5360 haswell_set_pipemisc(crtc);
229fca97 5361
b95c5321 5362 intel_color_set_csc(&pipe_config->base);
229fca97 5363
4f771f10 5364 intel_crtc->active = true;
8664281b 5365
6b698516
DV
5366 if (intel_crtc->config->has_pch_encoder)
5367 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
5368 else
5369 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5370
fd6bbda9 5371 intel_encoders_pre_enable(crtc, pipe_config, old_state);
4f771f10 5372
d2d65408 5373 if (intel_crtc->config->has_pch_encoder)
4fe9467d 5374 dev_priv->display.fdi_link_train(crtc);
4fe9467d 5375
d7edc4e5 5376 if (!transcoder_is_dsi(cpu_transcoder))
7d4aefd0 5377 intel_ddi_enable_pipe_clock(intel_crtc);
4f771f10 5378
6315b5d3 5379 if (INTEL_GEN(dev_priv) >= 9)
e435d6e5 5380 skylake_pfit_enable(intel_crtc);
ff6d9f55 5381 else
1c132b44 5382 ironlake_pfit_enable(intel_crtc);
4f771f10
PZ
5383
5384 /*
5385 * On ILK+ LUT must be loaded before the pipe is running but with
5386 * clocks enabled
5387 */
b95c5321 5388 intel_color_load_luts(&pipe_config->base);
4f771f10 5389
1f544388 5390 intel_ddi_set_pipe_settings(crtc);
d7edc4e5 5391 if (!transcoder_is_dsi(cpu_transcoder))
7d4aefd0 5392 intel_ddi_enable_transcoder_func(crtc);
4f771f10 5393
1d5bf5d9 5394 if (dev_priv->display.initial_watermarks != NULL)
3125d39f 5395 dev_priv->display.initial_watermarks(old_intel_state, pipe_config);
4d1de975
JN
5396
5397 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
d7edc4e5 5398 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975 5399 intel_enable_pipe(intel_crtc);
42db64ef 5400
6e3c9717 5401 if (intel_crtc->config->has_pch_encoder)
1507e5bd 5402 lpt_pch_enable(crtc);
4f771f10 5403
0037071d 5404 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
0e32b39c
DA
5405 intel_ddi_set_vc_payload_alloc(crtc, true);
5406
f9b61ff6
DV
5407 assert_vblank_disabled(crtc);
5408 drm_crtc_vblank_on(crtc);
5409
fd6bbda9 5410 intel_encoders_enable(crtc, pipe_config, old_state);
4f771f10 5411
6b698516 5412 if (intel_crtc->config->has_pch_encoder) {
0f0f74bc
VS
5413 intel_wait_for_vblank(dev_priv, pipe);
5414 intel_wait_for_vblank(dev_priv, pipe);
6b698516 5415 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
d2d65408
VS
5416 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5417 true);
6b698516 5418 }
d2d65408 5419
e4916946
PZ
5420 /* If we change the relative order between pipe/planes enabling, we need
5421 * to change the workaround. */
99d736a2 5422 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
772c2a51 5423 if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
0f0f74bc
VS
5424 intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
5425 intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
99d736a2 5426 }
4f771f10
PZ
5427}
5428
bfd16b2a 5429static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
3f8dce3a
DV
5430{
5431 struct drm_device *dev = crtc->base.dev;
fac5e23e 5432 struct drm_i915_private *dev_priv = to_i915(dev);
3f8dce3a
DV
5433 int pipe = crtc->pipe;
5434
5435 /* To avoid upsetting the power well on haswell only disable the pfit if
5436 * it's in use. The hw state code will make sure we get this right. */
bfd16b2a 5437 if (force || crtc->config->pch_pfit.enabled) {
3f8dce3a
DV
5438 I915_WRITE(PF_CTL(pipe), 0);
5439 I915_WRITE(PF_WIN_POS(pipe), 0);
5440 I915_WRITE(PF_WIN_SZ(pipe), 0);
5441 }
5442}
5443
4a806558
ML
5444static void ironlake_crtc_disable(struct intel_crtc_state *old_crtc_state,
5445 struct drm_atomic_state *old_state)
6be4a607 5446{
4a806558 5447 struct drm_crtc *crtc = old_crtc_state->base.crtc;
6be4a607 5448 struct drm_device *dev = crtc->dev;
fac5e23e 5449 struct drm_i915_private *dev_priv = to_i915(dev);
6be4a607
JB
5450 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5451 int pipe = intel_crtc->pipe;
b52eb4dc 5452
b2c0593a
VS
5453 /*
5454 * Sometimes spurious CPU pipe underruns happen when the
5455 * pipe is already disabled, but FDI RX/TX is still enabled.
5456 * Happens at least with VGA+HDMI cloning. Suppress them.
5457 */
5458 if (intel_crtc->config->has_pch_encoder) {
5459 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
37ca8d4c 5460 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
b2c0593a 5461 }
37ca8d4c 5462
fd6bbda9 5463 intel_encoders_disable(crtc, old_crtc_state, old_state);
ea9d758d 5464
f9b61ff6
DV
5465 drm_crtc_vblank_off(crtc);
5466 assert_vblank_disabled(crtc);
5467
575f7ab7 5468 intel_disable_pipe(intel_crtc);
32f9d658 5469
bfd16b2a 5470 ironlake_pfit_disable(intel_crtc, false);
2c07245f 5471
b2c0593a 5472 if (intel_crtc->config->has_pch_encoder)
5a74f70a
VS
5473 ironlake_fdi_disable(crtc);
5474
fd6bbda9 5475 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
2c07245f 5476
6e3c9717 5477 if (intel_crtc->config->has_pch_encoder) {
d925c59a 5478 ironlake_disable_pch_transcoder(dev_priv, pipe);
6be4a607 5479
6e266956 5480 if (HAS_PCH_CPT(dev_priv)) {
f0f59a00
VS
5481 i915_reg_t reg;
5482 u32 temp;
5483
d925c59a
DV
5484 /* disable TRANS_DP_CTL */
5485 reg = TRANS_DP_CTL(pipe);
5486 temp = I915_READ(reg);
5487 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5488 TRANS_DP_PORT_SEL_MASK);
5489 temp |= TRANS_DP_PORT_SEL_NONE;
5490 I915_WRITE(reg, temp);
5491
5492 /* disable DPLL_SEL */
5493 temp = I915_READ(PCH_DPLL_SEL);
11887397 5494 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 5495 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 5496 }
e3421a18 5497
d925c59a
DV
5498 ironlake_fdi_pll_disable(intel_crtc);
5499 }
81b088ca 5500
b2c0593a 5501 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
81b088ca 5502 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607 5503}
1b3c7a47 5504
4a806558
ML
5505static void haswell_crtc_disable(struct intel_crtc_state *old_crtc_state,
5506 struct drm_atomic_state *old_state)
ee7b9f93 5507{
4a806558 5508 struct drm_crtc *crtc = old_crtc_state->base.crtc;
6315b5d3 5509 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
ee7b9f93 5510 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 5511 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee7b9f93 5512
d2d65408
VS
5513 if (intel_crtc->config->has_pch_encoder)
5514 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5515 false);
5516
fd6bbda9 5517 intel_encoders_disable(crtc, old_crtc_state, old_state);
4f771f10 5518
f9b61ff6
DV
5519 drm_crtc_vblank_off(crtc);
5520 assert_vblank_disabled(crtc);
5521
4d1de975 5522 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
d7edc4e5 5523 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975 5524 intel_disable_pipe(intel_crtc);
4f771f10 5525
0037071d 5526 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
a4bf214f
VS
5527 intel_ddi_set_vc_payload_alloc(crtc, false);
5528
d7edc4e5 5529 if (!transcoder_is_dsi(cpu_transcoder))
7d4aefd0 5530 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4f771f10 5531
6315b5d3 5532 if (INTEL_GEN(dev_priv) >= 9)
e435d6e5 5533 skylake_scaler_disable(intel_crtc);
ff6d9f55 5534 else
bfd16b2a 5535 ironlake_pfit_disable(intel_crtc, false);
4f771f10 5536
d7edc4e5 5537 if (!transcoder_is_dsi(cpu_transcoder))
7d4aefd0 5538 intel_ddi_disable_pipe_clock(intel_crtc);
4f771f10 5539
fd6bbda9 5540 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
81b088ca 5541
b7076546 5542 if (old_crtc_state->has_pch_encoder)
81b088ca
VS
5543 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5544 true);
4f771f10
PZ
5545}
5546
2dd24552
JB
5547static void i9xx_pfit_enable(struct intel_crtc *crtc)
5548{
5549 struct drm_device *dev = crtc->base.dev;
fac5e23e 5550 struct drm_i915_private *dev_priv = to_i915(dev);
6e3c9717 5551 struct intel_crtc_state *pipe_config = crtc->config;
2dd24552 5552
681a8504 5553 if (!pipe_config->gmch_pfit.control)
2dd24552
JB
5554 return;
5555
2dd24552 5556 /*
c0b03411
DV
5557 * The panel fitter should only be adjusted whilst the pipe is disabled,
5558 * according to register description and PRM.
2dd24552 5559 */
c0b03411
DV
5560 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5561 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 5562
b074cec8
JB
5563 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5564 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
DV
5565
5566 /* Border color in case we don't scale up to the full screen. Black by
5567 * default, change to something else for debugging. */
5568 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
5569}
5570
d05410f9
DA
5571static enum intel_display_power_domain port_to_power_domain(enum port port)
5572{
5573 switch (port) {
5574 case PORT_A:
6331a704 5575 return POWER_DOMAIN_PORT_DDI_A_LANES;
d05410f9 5576 case PORT_B:
6331a704 5577 return POWER_DOMAIN_PORT_DDI_B_LANES;
d05410f9 5578 case PORT_C:
6331a704 5579 return POWER_DOMAIN_PORT_DDI_C_LANES;
d05410f9 5580 case PORT_D:
6331a704 5581 return POWER_DOMAIN_PORT_DDI_D_LANES;
d8e19f99 5582 case PORT_E:
6331a704 5583 return POWER_DOMAIN_PORT_DDI_E_LANES;
d05410f9 5584 default:
b9fec167 5585 MISSING_CASE(port);
d05410f9
DA
5586 return POWER_DOMAIN_PORT_OTHER;
5587 }
5588}
5589
25f78f58
VS
5590static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5591{
5592 switch (port) {
5593 case PORT_A:
5594 return POWER_DOMAIN_AUX_A;
5595 case PORT_B:
5596 return POWER_DOMAIN_AUX_B;
5597 case PORT_C:
5598 return POWER_DOMAIN_AUX_C;
5599 case PORT_D:
5600 return POWER_DOMAIN_AUX_D;
5601 case PORT_E:
5602 /* FIXME: Check VBT for actual wiring of PORT E */
5603 return POWER_DOMAIN_AUX_D;
5604 default:
b9fec167 5605 MISSING_CASE(port);
25f78f58
VS
5606 return POWER_DOMAIN_AUX_A;
5607 }
5608}
5609
319be8ae
ID
5610enum intel_display_power_domain
5611intel_display_port_power_domain(struct intel_encoder *intel_encoder)
5612{
4f8036a2 5613 struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
319be8ae
ID
5614 struct intel_digital_port *intel_dig_port;
5615
5616 switch (intel_encoder->type) {
5617 case INTEL_OUTPUT_UNKNOWN:
5618 /* Only DDI platforms should ever use this output type */
4f8036a2 5619 WARN_ON_ONCE(!HAS_DDI(dev_priv));
cca0502b 5620 case INTEL_OUTPUT_DP:
319be8ae
ID
5621 case INTEL_OUTPUT_HDMI:
5622 case INTEL_OUTPUT_EDP:
5623 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
d05410f9 5624 return port_to_power_domain(intel_dig_port->port);
0e32b39c
DA
5625 case INTEL_OUTPUT_DP_MST:
5626 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5627 return port_to_power_domain(intel_dig_port->port);
319be8ae
ID
5628 case INTEL_OUTPUT_ANALOG:
5629 return POWER_DOMAIN_PORT_CRT;
5630 case INTEL_OUTPUT_DSI:
5631 return POWER_DOMAIN_PORT_DSI;
5632 default:
5633 return POWER_DOMAIN_PORT_OTHER;
5634 }
5635}
5636
25f78f58
VS
5637enum intel_display_power_domain
5638intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5639{
4f8036a2 5640 struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
25f78f58
VS
5641 struct intel_digital_port *intel_dig_port;
5642
5643 switch (intel_encoder->type) {
5644 case INTEL_OUTPUT_UNKNOWN:
651174a4
ID
5645 case INTEL_OUTPUT_HDMI:
5646 /*
5647 * Only DDI platforms should ever use these output types.
5648 * We can get here after the HDMI detect code has already set
5649 * the type of the shared encoder. Since we can't be sure
5650 * what's the status of the given connectors, play safe and
5651 * run the DP detection too.
5652 */
4f8036a2 5653 WARN_ON_ONCE(!HAS_DDI(dev_priv));
cca0502b 5654 case INTEL_OUTPUT_DP:
25f78f58
VS
5655 case INTEL_OUTPUT_EDP:
5656 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5657 return port_to_aux_power_domain(intel_dig_port->port);
5658 case INTEL_OUTPUT_DP_MST:
5659 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5660 return port_to_aux_power_domain(intel_dig_port->port);
5661 default:
b9fec167 5662 MISSING_CASE(intel_encoder->type);
25f78f58
VS
5663 return POWER_DOMAIN_AUX_A;
5664 }
5665}
5666
d8fc70b7
ACO
5667static u64 get_crtc_power_domains(struct drm_crtc *crtc,
5668 struct intel_crtc_state *crtc_state)
77d22dca 5669{
319be8ae 5670 struct drm_device *dev = crtc->dev;
37255d8d 5671 struct drm_i915_private *dev_priv = to_i915(dev);
74bff5f9 5672 struct drm_encoder *encoder;
319be8ae
ID
5673 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5674 enum pipe pipe = intel_crtc->pipe;
d8fc70b7 5675 u64 mask;
74bff5f9 5676 enum transcoder transcoder = crtc_state->cpu_transcoder;
77d22dca 5677
74bff5f9 5678 if (!crtc_state->base.active)
292b990e
ML
5679 return 0;
5680
77d22dca
ID
5681 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5682 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
74bff5f9
ML
5683 if (crtc_state->pch_pfit.enabled ||
5684 crtc_state->pch_pfit.force_thru)
d8fc70b7 5685 mask |= BIT_ULL(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
77d22dca 5686
74bff5f9
ML
5687 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5688 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5689
d8fc70b7 5690 mask |= BIT_ULL(intel_display_port_power_domain(intel_encoder));
74bff5f9 5691 }
319be8ae 5692
37255d8d
ML
5693 if (HAS_DDI(dev_priv) && crtc_state->has_audio)
5694 mask |= BIT(POWER_DOMAIN_AUDIO);
5695
15e7ec29 5696 if (crtc_state->shared_dpll)
d8fc70b7 5697 mask |= BIT_ULL(POWER_DOMAIN_PLLS);
15e7ec29 5698
77d22dca
ID
5699 return mask;
5700}
5701
d2d15016 5702static u64
74bff5f9
ML
5703modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5704 struct intel_crtc_state *crtc_state)
77d22dca 5705{
fac5e23e 5706 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
292b990e
ML
5707 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5708 enum intel_display_power_domain domain;
d8fc70b7 5709 u64 domains, new_domains, old_domains;
77d22dca 5710
292b990e 5711 old_domains = intel_crtc->enabled_power_domains;
74bff5f9
ML
5712 intel_crtc->enabled_power_domains = new_domains =
5713 get_crtc_power_domains(crtc, crtc_state);
77d22dca 5714
5a21b665 5715 domains = new_domains & ~old_domains;
292b990e
ML
5716
5717 for_each_power_domain(domain, domains)
5718 intel_display_power_get(dev_priv, domain);
5719
5a21b665 5720 return old_domains & ~new_domains;
292b990e
ML
5721}
5722
5723static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
d8fc70b7 5724 u64 domains)
292b990e
ML
5725{
5726 enum intel_display_power_domain domain;
5727
5728 for_each_power_domain(domain, domains)
5729 intel_display_power_put(dev_priv, domain);
5730}
77d22dca 5731
7ff89ca2
VS
5732static void valleyview_crtc_enable(struct intel_crtc_state *pipe_config,
5733 struct drm_atomic_state *old_state)
adafdc6f 5734{
7ff89ca2
VS
5735 struct drm_crtc *crtc = pipe_config->base.crtc;
5736 struct drm_device *dev = crtc->dev;
5737 struct drm_i915_private *dev_priv = to_i915(dev);
5738 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5739 int pipe = intel_crtc->pipe;
adafdc6f 5740
7ff89ca2
VS
5741 if (WARN_ON(intel_crtc->active))
5742 return;
adafdc6f 5743
7ff89ca2
VS
5744 if (intel_crtc_has_dp_encoder(intel_crtc->config))
5745 intel_dp_set_m_n(intel_crtc, M1_N1);
b2045352 5746
7ff89ca2
VS
5747 intel_set_pipe_timings(intel_crtc);
5748 intel_set_pipe_src_size(intel_crtc);
b2045352 5749
7ff89ca2
VS
5750 if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
5751 struct drm_i915_private *dev_priv = to_i915(dev);
560a7ae4 5752
7ff89ca2
VS
5753 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
5754 I915_WRITE(CHV_CANVAS(pipe), 0);
560a7ae4
DL
5755 }
5756
7ff89ca2 5757 i9xx_set_pipeconf(intel_crtc);
560a7ae4 5758
7ff89ca2 5759 intel_crtc->active = true;
92891e45 5760
7ff89ca2 5761 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5f199dfa 5762
7ff89ca2 5763 intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
5f199dfa 5764
7ff89ca2
VS
5765 if (IS_CHERRYVIEW(dev_priv)) {
5766 chv_prepare_pll(intel_crtc, intel_crtc->config);
5767 chv_enable_pll(intel_crtc, intel_crtc->config);
5768 } else {
5769 vlv_prepare_pll(intel_crtc, intel_crtc->config);
5770 vlv_enable_pll(intel_crtc, intel_crtc->config);
5f199dfa
VS
5771 }
5772
7ff89ca2 5773 intel_encoders_pre_enable(crtc, pipe_config, old_state);
5f199dfa 5774
7ff89ca2 5775 i9xx_pfit_enable(intel_crtc);
89b3c3c7 5776
7ff89ca2 5777 intel_color_load_luts(&pipe_config->base);
89b3c3c7 5778
7ff89ca2
VS
5779 intel_update_watermarks(intel_crtc);
5780 intel_enable_pipe(intel_crtc);
5781
5782 assert_vblank_disabled(crtc);
5783 drm_crtc_vblank_on(crtc);
89b3c3c7 5784
7ff89ca2 5785 intel_encoders_enable(crtc, pipe_config, old_state);
89b3c3c7
ACO
5786}
5787
7ff89ca2 5788static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
2b73001e 5789{
7ff89ca2
VS
5790 struct drm_device *dev = crtc->base.dev;
5791 struct drm_i915_private *dev_priv = to_i915(dev);
83d7c81f 5792
7ff89ca2
VS
5793 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
5794 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
2b73001e
VS
5795}
5796
7ff89ca2
VS
5797static void i9xx_crtc_enable(struct intel_crtc_state *pipe_config,
5798 struct drm_atomic_state *old_state)
2b73001e 5799{
7ff89ca2
VS
5800 struct drm_crtc *crtc = pipe_config->base.crtc;
5801 struct drm_device *dev = crtc->dev;
5802 struct drm_i915_private *dev_priv = to_i915(dev);
5803 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5804 enum pipe pipe = intel_crtc->pipe;
2b73001e 5805
7ff89ca2
VS
5806 if (WARN_ON(intel_crtc->active))
5807 return;
2b73001e 5808
7ff89ca2 5809 i9xx_set_pll_dividers(intel_crtc);
2b73001e 5810
7ff89ca2
VS
5811 if (intel_crtc_has_dp_encoder(intel_crtc->config))
5812 intel_dp_set_m_n(intel_crtc, M1_N1);
83d7c81f 5813
7ff89ca2
VS
5814 intel_set_pipe_timings(intel_crtc);
5815 intel_set_pipe_src_size(intel_crtc);
2b73001e 5816
7ff89ca2 5817 i9xx_set_pipeconf(intel_crtc);
f8437dd1 5818
7ff89ca2 5819 intel_crtc->active = true;
5f199dfa 5820
7ff89ca2
VS
5821 if (!IS_GEN2(dev_priv))
5822 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5f199dfa 5823
7ff89ca2 5824 intel_encoders_pre_enable(crtc, pipe_config, old_state);
f8437dd1 5825
7ff89ca2 5826 i9xx_enable_pll(intel_crtc);
f8437dd1 5827
7ff89ca2 5828 i9xx_pfit_enable(intel_crtc);
f8437dd1 5829
7ff89ca2 5830 intel_color_load_luts(&pipe_config->base);
f8437dd1 5831
7ff89ca2
VS
5832 intel_update_watermarks(intel_crtc);
5833 intel_enable_pipe(intel_crtc);
f8437dd1 5834
7ff89ca2
VS
5835 assert_vblank_disabled(crtc);
5836 drm_crtc_vblank_on(crtc);
f8437dd1 5837
7ff89ca2
VS
5838 intel_encoders_enable(crtc, pipe_config, old_state);
5839}
f8437dd1 5840
7ff89ca2
VS
5841static void i9xx_pfit_disable(struct intel_crtc *crtc)
5842{
5843 struct drm_device *dev = crtc->base.dev;
5844 struct drm_i915_private *dev_priv = to_i915(dev);
f8437dd1 5845
7ff89ca2 5846 if (!crtc->config->gmch_pfit.control)
f8437dd1 5847 return;
f8437dd1 5848
7ff89ca2
VS
5849 assert_pipe_disabled(dev_priv, crtc->pipe);
5850
5851 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
5852 I915_READ(PFIT_CONTROL));
5853 I915_WRITE(PFIT_CONTROL, 0);
f8437dd1
VK
5854}
5855
7ff89ca2
VS
5856static void i9xx_crtc_disable(struct intel_crtc_state *old_crtc_state,
5857 struct drm_atomic_state *old_state)
f8437dd1 5858{
7ff89ca2
VS
5859 struct drm_crtc *crtc = old_crtc_state->base.crtc;
5860 struct drm_device *dev = crtc->dev;
5861 struct drm_i915_private *dev_priv = to_i915(dev);
5862 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5863 int pipe = intel_crtc->pipe;
d66a2194 5864
d66a2194 5865 /*
7ff89ca2
VS
5866 * On gen2 planes are double buffered but the pipe isn't, so we must
5867 * wait for planes to fully turn off before disabling the pipe.
d66a2194 5868 */
7ff89ca2
VS
5869 if (IS_GEN2(dev_priv))
5870 intel_wait_for_vblank(dev_priv, pipe);
d66a2194 5871
7ff89ca2 5872 intel_encoders_disable(crtc, old_crtc_state, old_state);
d66a2194 5873
7ff89ca2
VS
5874 drm_crtc_vblank_off(crtc);
5875 assert_vblank_disabled(crtc);
d66a2194 5876
7ff89ca2 5877 intel_disable_pipe(intel_crtc);
d66a2194 5878
7ff89ca2 5879 i9xx_pfit_disable(intel_crtc);
89b3c3c7 5880
7ff89ca2 5881 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
d66a2194 5882
7ff89ca2
VS
5883 if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
5884 if (IS_CHERRYVIEW(dev_priv))
5885 chv_disable_pll(dev_priv, pipe);
5886 else if (IS_VALLEYVIEW(dev_priv))
5887 vlv_disable_pll(dev_priv, pipe);
5888 else
5889 i9xx_disable_pll(intel_crtc);
5890 }
c2e001ef 5891
7ff89ca2 5892 intel_encoders_post_pll_disable(crtc, old_crtc_state, old_state);
89b3c3c7 5893
7ff89ca2
VS
5894 if (!IS_GEN2(dev_priv))
5895 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
f8437dd1
VK
5896}
5897
7ff89ca2 5898static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
f8437dd1 5899{
7ff89ca2
VS
5900 struct intel_encoder *encoder;
5901 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5902 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
5903 enum intel_display_power_domain domain;
d2d15016 5904 u64 domains;
7ff89ca2
VS
5905 struct drm_atomic_state *state;
5906 struct intel_crtc_state *crtc_state;
5907 int ret;
f8437dd1 5908
7ff89ca2
VS
5909 if (!intel_crtc->active)
5910 return;
a8ca4934 5911
7ff89ca2
VS
5912 if (crtc->primary->state->visible) {
5913 WARN_ON(intel_crtc->flip_work);
5d96d8af 5914
7ff89ca2 5915 intel_pre_disable_primary_noatomic(crtc);
709e05c3 5916
7ff89ca2
VS
5917 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
5918 crtc->primary->state->visible = false;
5919 }
5d96d8af 5920
7ff89ca2
VS
5921 state = drm_atomic_state_alloc(crtc->dev);
5922 if (!state) {
5923 DRM_DEBUG_KMS("failed to disable [CRTC:%d:%s], out of memory",
5924 crtc->base.id, crtc->name);
1c3f7700 5925 return;
7ff89ca2 5926 }
9f7eb31a 5927
7ff89ca2 5928 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
ea61791e 5929
7ff89ca2
VS
5930 /* Everything's already locked, -EDEADLK can't happen. */
5931 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
5932 ret = drm_atomic_add_affected_connectors(state, crtc);
9f7eb31a 5933
7ff89ca2 5934 WARN_ON(IS_ERR(crtc_state) || ret);
5d96d8af 5935
7ff89ca2 5936 dev_priv->display.crtc_disable(crtc_state, state);
4a806558 5937
0853695c 5938 drm_atomic_state_put(state);
842e0307 5939
78108b7c
VS
5940 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
5941 crtc->base.id, crtc->name);
842e0307
ML
5942
5943 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
5944 crtc->state->active = false;
37d9078b 5945 intel_crtc->active = false;
842e0307
ML
5946 crtc->enabled = false;
5947 crtc->state->connector_mask = 0;
5948 crtc->state->encoder_mask = 0;
5949
5950 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
5951 encoder->base.crtc = NULL;
5952
58f9c0bc 5953 intel_fbc_disable(intel_crtc);
432081bc 5954 intel_update_watermarks(intel_crtc);
1f7457b1 5955 intel_disable_shared_dpll(intel_crtc);
b17d48e2
ML
5956
5957 domains = intel_crtc->enabled_power_domains;
5958 for_each_power_domain(domain, domains)
5959 intel_display_power_put(dev_priv, domain);
5960 intel_crtc->enabled_power_domains = 0;
565602d7
ML
5961
5962 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
5963 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
b17d48e2
ML
5964}
5965
6b72d486
ML
5966/*
5967 * turn all crtc's off, but do not adjust state
5968 * This has to be paired with a call to intel_modeset_setup_hw_state.
5969 */
70e0bd74 5970int intel_display_suspend(struct drm_device *dev)
ee7b9f93 5971{
e2c8b870 5972 struct drm_i915_private *dev_priv = to_i915(dev);
70e0bd74 5973 struct drm_atomic_state *state;
e2c8b870 5974 int ret;
70e0bd74 5975
e2c8b870
ML
5976 state = drm_atomic_helper_suspend(dev);
5977 ret = PTR_ERR_OR_ZERO(state);
70e0bd74
ML
5978 if (ret)
5979 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
e2c8b870
ML
5980 else
5981 dev_priv->modeset_restore_state = state;
70e0bd74 5982 return ret;
ee7b9f93
JB
5983}
5984
ea5b213a 5985void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 5986{
4ef69c7a 5987 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 5988
ea5b213a
CW
5989 drm_encoder_cleanup(encoder);
5990 kfree(intel_encoder);
7e7d76c3
JB
5991}
5992
0a91ca29
DV
5993/* Cross check the actual hw state with our own modeset state tracking (and it's
5994 * internal consistency). */
5a21b665 5995static void intel_connector_verify_state(struct intel_connector *connector)
79e53945 5996{
5a21b665 5997 struct drm_crtc *crtc = connector->base.state->crtc;
35dd3c64
ML
5998
5999 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6000 connector->base.base.id,
6001 connector->base.name);
6002
0a91ca29 6003 if (connector->get_hw_state(connector)) {
e85376cb 6004 struct intel_encoder *encoder = connector->encoder;
5a21b665 6005 struct drm_connector_state *conn_state = connector->base.state;
0a91ca29 6006
35dd3c64
ML
6007 I915_STATE_WARN(!crtc,
6008 "connector enabled without attached crtc\n");
0a91ca29 6009
35dd3c64
ML
6010 if (!crtc)
6011 return;
6012
6013 I915_STATE_WARN(!crtc->state->active,
6014 "connector is active, but attached crtc isn't\n");
6015
e85376cb 6016 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
35dd3c64
ML
6017 return;
6018
e85376cb 6019 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
35dd3c64
ML
6020 "atomic encoder doesn't match attached encoder\n");
6021
e85376cb 6022 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
35dd3c64
ML
6023 "attached encoder crtc differs from connector crtc\n");
6024 } else {
4d688a2a
ML
6025 I915_STATE_WARN(crtc && crtc->state->active,
6026 "attached crtc is active, but connector isn't\n");
5a21b665 6027 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
35dd3c64 6028 "best encoder set without crtc!\n");
0a91ca29 6029 }
79e53945
JB
6030}
6031
08d9bc92
ACO
6032int intel_connector_init(struct intel_connector *connector)
6033{
5350a031 6034 drm_atomic_helper_connector_reset(&connector->base);
08d9bc92 6035
5350a031 6036 if (!connector->base.state)
08d9bc92
ACO
6037 return -ENOMEM;
6038
08d9bc92
ACO
6039 return 0;
6040}
6041
6042struct intel_connector *intel_connector_alloc(void)
6043{
6044 struct intel_connector *connector;
6045
6046 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6047 if (!connector)
6048 return NULL;
6049
6050 if (intel_connector_init(connector) < 0) {
6051 kfree(connector);
6052 return NULL;
6053 }
6054
6055 return connector;
6056}
6057
f0947c37
DV
6058/* Simple connector->get_hw_state implementation for encoders that support only
6059 * one connector and no cloning and hence the encoder state determines the state
6060 * of the connector. */
6061bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 6062{
24929352 6063 enum pipe pipe = 0;
f0947c37 6064 struct intel_encoder *encoder = connector->encoder;
ea5b213a 6065
f0947c37 6066 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
6067}
6068
6d293983 6069static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
d272ddfa 6070{
6d293983
ACO
6071 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6072 return crtc_state->fdi_lanes;
d272ddfa
VS
6073
6074 return 0;
6075}
6076
6d293983 6077static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5cec258b 6078 struct intel_crtc_state *pipe_config)
1857e1da 6079{
8652744b 6080 struct drm_i915_private *dev_priv = to_i915(dev);
6d293983
ACO
6081 struct drm_atomic_state *state = pipe_config->base.state;
6082 struct intel_crtc *other_crtc;
6083 struct intel_crtc_state *other_crtc_state;
6084
1857e1da
DV
6085 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6086 pipe_name(pipe), pipe_config->fdi_lanes);
6087 if (pipe_config->fdi_lanes > 4) {
6088 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6089 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6090 return -EINVAL;
1857e1da
DV
6091 }
6092
8652744b 6093 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
1857e1da
DV
6094 if (pipe_config->fdi_lanes > 2) {
6095 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6096 pipe_config->fdi_lanes);
6d293983 6097 return -EINVAL;
1857e1da 6098 } else {
6d293983 6099 return 0;
1857e1da
DV
6100 }
6101 }
6102
b7f05d4a 6103 if (INTEL_INFO(dev_priv)->num_pipes == 2)
6d293983 6104 return 0;
1857e1da
DV
6105
6106 /* Ivybridge 3 pipe is really complicated */
6107 switch (pipe) {
6108 case PIPE_A:
6d293983 6109 return 0;
1857e1da 6110 case PIPE_B:
6d293983
ACO
6111 if (pipe_config->fdi_lanes <= 2)
6112 return 0;
6113
b91eb5cc 6114 other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
6d293983
ACO
6115 other_crtc_state =
6116 intel_atomic_get_crtc_state(state, other_crtc);
6117 if (IS_ERR(other_crtc_state))
6118 return PTR_ERR(other_crtc_state);
6119
6120 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
1857e1da
DV
6121 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6122 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6123 return -EINVAL;
1857e1da 6124 }
6d293983 6125 return 0;
1857e1da 6126 case PIPE_C:
251cc67c
VS
6127 if (pipe_config->fdi_lanes > 2) {
6128 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6129 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6130 return -EINVAL;
251cc67c 6131 }
6d293983 6132
b91eb5cc 6133 other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
6d293983
ACO
6134 other_crtc_state =
6135 intel_atomic_get_crtc_state(state, other_crtc);
6136 if (IS_ERR(other_crtc_state))
6137 return PTR_ERR(other_crtc_state);
6138
6139 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
1857e1da 6140 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
6d293983 6141 return -EINVAL;
1857e1da 6142 }
6d293983 6143 return 0;
1857e1da
DV
6144 default:
6145 BUG();
6146 }
6147}
6148
e29c22c0
DV
6149#define RETRY 1
6150static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5cec258b 6151 struct intel_crtc_state *pipe_config)
877d48d5 6152{
1857e1da 6153 struct drm_device *dev = intel_crtc->base.dev;
7c5f93b0 6154 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6d293983
ACO
6155 int lane, link_bw, fdi_dotclock, ret;
6156 bool needs_recompute = false;
877d48d5 6157
e29c22c0 6158retry:
877d48d5
DV
6159 /* FDI is a binary signal running at ~2.7GHz, encoding
6160 * each output octet as 10 bits. The actual frequency
6161 * is stored as a divider into a 100MHz clock, and the
6162 * mode pixel clock is stored in units of 1KHz.
6163 * Hence the bw of each lane in terms of the mode signal
6164 * is:
6165 */
21a727b3 6166 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
877d48d5 6167
241bfc38 6168 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 6169
2bd89a07 6170 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
DV
6171 pipe_config->pipe_bpp);
6172
6173 pipe_config->fdi_lanes = lane;
6174
2bd89a07 6175 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
877d48d5 6176 link_bw, &pipe_config->fdi_m_n);
1857e1da 6177
e3b247da 6178 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
6d293983 6179 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
e29c22c0 6180 pipe_config->pipe_bpp -= 2*3;
7ff89ca2
VS
6181 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6182 pipe_config->pipe_bpp);
6183 needs_recompute = true;
6184 pipe_config->bw_constrained = true;
257a7ffc 6185
7ff89ca2 6186 goto retry;
257a7ffc 6187 }
79e53945 6188
7ff89ca2
VS
6189 if (needs_recompute)
6190 return RETRY;
e70236a8 6191
7ff89ca2 6192 return ret;
e70236a8
JB
6193}
6194
7ff89ca2
VS
6195static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6196 struct intel_crtc_state *pipe_config)
e70236a8 6197{
7ff89ca2
VS
6198 if (pipe_config->pipe_bpp > 24)
6199 return false;
e70236a8 6200
7ff89ca2
VS
6201 /* HSW can handle pixel rate up to cdclk? */
6202 if (IS_HASWELL(dev_priv))
6203 return true;
1b1d2716 6204
65cd2b3f 6205 /*
7ff89ca2
VS
6206 * We compare against max which means we must take
6207 * the increased cdclk requirement into account when
6208 * calculating the new cdclk.
6209 *
6210 * Should measure whether using a lower cdclk w/o IPS
e70236a8 6211 */
7ff89ca2
VS
6212 return pipe_config->pixel_rate <=
6213 dev_priv->max_cdclk_freq * 95 / 100;
e70236a8 6214}
79e53945 6215
7ff89ca2
VS
6216static void hsw_compute_ips_config(struct intel_crtc *crtc,
6217 struct intel_crtc_state *pipe_config)
6218{
6219 struct drm_device *dev = crtc->base.dev;
6220 struct drm_i915_private *dev_priv = to_i915(dev);
34edce2f 6221
7ff89ca2
VS
6222 pipe_config->ips_enabled = i915.enable_ips &&
6223 hsw_crtc_supports_ips(crtc) &&
6224 pipe_config_supports_ips(dev_priv, pipe_config);
34edce2f
VS
6225}
6226
7ff89ca2 6227static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
34edce2f 6228{
7ff89ca2 6229 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
34edce2f 6230
7ff89ca2
VS
6231 /* GDG double wide on either pipe, otherwise pipe A only */
6232 return INTEL_INFO(dev_priv)->gen < 4 &&
6233 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
34edce2f
VS
6234}
6235
ceb99320
VS
6236static uint32_t ilk_pipe_pixel_rate(const struct intel_crtc_state *pipe_config)
6237{
6238 uint32_t pixel_rate;
6239
6240 pixel_rate = pipe_config->base.adjusted_mode.crtc_clock;
6241
6242 /*
6243 * We only use IF-ID interlacing. If we ever use
6244 * PF-ID we'll need to adjust the pixel_rate here.
6245 */
6246
6247 if (pipe_config->pch_pfit.enabled) {
6248 uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
6249 uint32_t pfit_size = pipe_config->pch_pfit.size;
6250
6251 pipe_w = pipe_config->pipe_src_w;
6252 pipe_h = pipe_config->pipe_src_h;
6253
6254 pfit_w = (pfit_size >> 16) & 0xFFFF;
6255 pfit_h = pfit_size & 0xFFFF;
6256 if (pipe_w < pfit_w)
6257 pipe_w = pfit_w;
6258 if (pipe_h < pfit_h)
6259 pipe_h = pfit_h;
6260
6261 if (WARN_ON(!pfit_w || !pfit_h))
6262 return pixel_rate;
6263
6264 pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
6265 pfit_w * pfit_h);
6266 }
6267
6268 return pixel_rate;
6269}
6270
7ff89ca2 6271static void intel_crtc_compute_pixel_rate(struct intel_crtc_state *crtc_state)
34edce2f 6272{
7ff89ca2 6273 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
34edce2f 6274
7ff89ca2
VS
6275 if (HAS_GMCH_DISPLAY(dev_priv))
6276 /* FIXME calculate proper pipe pixel rate for GMCH pfit */
6277 crtc_state->pixel_rate =
6278 crtc_state->base.adjusted_mode.crtc_clock;
6279 else
6280 crtc_state->pixel_rate =
6281 ilk_pipe_pixel_rate(crtc_state);
6282}
34edce2f 6283
7ff89ca2
VS
6284static int intel_crtc_compute_config(struct intel_crtc *crtc,
6285 struct intel_crtc_state *pipe_config)
6286{
6287 struct drm_device *dev = crtc->base.dev;
6288 struct drm_i915_private *dev_priv = to_i915(dev);
6289 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6290 int clock_limit = dev_priv->max_dotclk_freq;
34edce2f 6291
7ff89ca2
VS
6292 if (INTEL_GEN(dev_priv) < 4) {
6293 clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
34edce2f 6294
7ff89ca2
VS
6295 /*
6296 * Enable double wide mode when the dot clock
6297 * is > 90% of the (display) core speed.
6298 */
6299 if (intel_crtc_supports_double_wide(crtc) &&
6300 adjusted_mode->crtc_clock > clock_limit) {
6301 clock_limit = dev_priv->max_dotclk_freq;
6302 pipe_config->double_wide = true;
6303 }
34edce2f
VS
6304 }
6305
7ff89ca2
VS
6306 if (adjusted_mode->crtc_clock > clock_limit) {
6307 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6308 adjusted_mode->crtc_clock, clock_limit,
6309 yesno(pipe_config->double_wide));
6310 return -EINVAL;
6311 }
34edce2f 6312
7ff89ca2
VS
6313 /*
6314 * Pipe horizontal size must be even in:
6315 * - DVO ganged mode
6316 * - LVDS dual channel mode
6317 * - Double wide pipe
6318 */
6319 if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
6320 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6321 pipe_config->pipe_src_w &= ~1;
34edce2f 6322
7ff89ca2
VS
6323 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6324 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
6325 */
6326 if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
6327 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
6328 return -EINVAL;
34edce2f 6329
7ff89ca2 6330 intel_crtc_compute_pixel_rate(pipe_config);
34edce2f 6331
7ff89ca2
VS
6332 if (HAS_IPS(dev_priv))
6333 hsw_compute_ips_config(crtc, pipe_config);
34edce2f 6334
7ff89ca2
VS
6335 if (pipe_config->has_pch_encoder)
6336 return ironlake_fdi_compute_config(crtc, pipe_config);
34edce2f 6337
7ff89ca2 6338 return 0;
34edce2f
VS
6339}
6340
2c07245f 6341static void
a65851af 6342intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 6343{
a65851af
VS
6344 while (*num > DATA_LINK_M_N_MASK ||
6345 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
6346 *num >>= 1;
6347 *den >>= 1;
6348 }
6349}
6350
a65851af
VS
6351static void compute_m_n(unsigned int m, unsigned int n,
6352 uint32_t *ret_m, uint32_t *ret_n)
6353{
6354 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
6355 *ret_m = div_u64((uint64_t) m * *ret_n, n);
6356 intel_reduce_m_n_ratio(ret_m, ret_n);
6357}
6358
e69d0bc1
DV
6359void
6360intel_link_compute_m_n(int bits_per_pixel, int nlanes,
6361 int pixel_clock, int link_clock,
6362 struct intel_link_m_n *m_n)
2c07245f 6363{
e69d0bc1 6364 m_n->tu = 64;
a65851af
VS
6365
6366 compute_m_n(bits_per_pixel * pixel_clock,
6367 link_clock * nlanes * 8,
6368 &m_n->gmch_m, &m_n->gmch_n);
6369
6370 compute_m_n(pixel_clock, link_clock,
6371 &m_n->link_m, &m_n->link_n);
2c07245f
ZW
6372}
6373
a7615030
CW
6374static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
6375{
d330a953
JN
6376 if (i915.panel_use_ssc >= 0)
6377 return i915.panel_use_ssc != 0;
41aa3448 6378 return dev_priv->vbt.lvds_use_ssc
435793df 6379 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
6380}
6381
7429e9d4 6382static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 6383{
7df00d7a 6384 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 6385}
f47709a9 6386
7429e9d4
DV
6387static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
6388{
6389 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
6390}
6391
f47709a9 6392static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
190f68c5 6393 struct intel_crtc_state *crtc_state,
9e2c8475 6394 struct dpll *reduced_clock)
a7516a05 6395{
9b1e14f4 6396 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
a7516a05
JB
6397 u32 fp, fp2 = 0;
6398
9b1e14f4 6399 if (IS_PINEVIEW(dev_priv)) {
190f68c5 6400 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
a7516a05 6401 if (reduced_clock)
7429e9d4 6402 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 6403 } else {
190f68c5 6404 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
a7516a05 6405 if (reduced_clock)
7429e9d4 6406 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
6407 }
6408
190f68c5 6409 crtc_state->dpll_hw_state.fp0 = fp;
a7516a05 6410
f47709a9 6411 crtc->lowfreq_avail = false;
2d84d2b3 6412 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ab585dea 6413 reduced_clock) {
190f68c5 6414 crtc_state->dpll_hw_state.fp1 = fp2;
f47709a9 6415 crtc->lowfreq_avail = true;
a7516a05 6416 } else {
190f68c5 6417 crtc_state->dpll_hw_state.fp1 = fp;
a7516a05
JB
6418 }
6419}
6420
5e69f97f
CML
6421static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
6422 pipe)
89b667f8
JB
6423{
6424 u32 reg_val;
6425
6426 /*
6427 * PLLB opamp always calibrates to max value of 0x3f, force enable it
6428 * and set it to a reasonable value instead.
6429 */
ab3c759a 6430 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
6431 reg_val &= 0xffffff00;
6432 reg_val |= 0x00000030;
ab3c759a 6433 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 6434
ab3c759a 6435 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
6436 reg_val &= 0x8cffffff;
6437 reg_val = 0x8c000000;
ab3c759a 6438 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 6439
ab3c759a 6440 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 6441 reg_val &= 0xffffff00;
ab3c759a 6442 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 6443
ab3c759a 6444 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
6445 reg_val &= 0x00ffffff;
6446 reg_val |= 0xb0000000;
ab3c759a 6447 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
6448}
6449
b551842d
DV
6450static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
6451 struct intel_link_m_n *m_n)
6452{
6453 struct drm_device *dev = crtc->base.dev;
fac5e23e 6454 struct drm_i915_private *dev_priv = to_i915(dev);
b551842d
DV
6455 int pipe = crtc->pipe;
6456
e3b95f1e
DV
6457 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6458 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
6459 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
6460 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
6461}
6462
6463static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
6464 struct intel_link_m_n *m_n,
6465 struct intel_link_m_n *m2_n2)
b551842d 6466{
6315b5d3 6467 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
b551842d 6468 int pipe = crtc->pipe;
6e3c9717 6469 enum transcoder transcoder = crtc->config->cpu_transcoder;
b551842d 6470
6315b5d3 6471 if (INTEL_GEN(dev_priv) >= 5) {
b551842d
DV
6472 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
6473 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
6474 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
6475 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
f769cd24
VK
6476 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
6477 * for gen < 8) and if DRRS is supported (to make sure the
6478 * registers are not unnecessarily accessed).
6479 */
920a14b2
TU
6480 if (m2_n2 && (IS_CHERRYVIEW(dev_priv) ||
6481 INTEL_GEN(dev_priv) < 8) && crtc->config->has_drrs) {
f769cd24
VK
6482 I915_WRITE(PIPE_DATA_M2(transcoder),
6483 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
6484 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
6485 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
6486 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
6487 }
b551842d 6488 } else {
e3b95f1e
DV
6489 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6490 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
6491 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
6492 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
6493 }
6494}
6495
fe3cd48d 6496void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
03afc4a2 6497{
fe3cd48d
R
6498 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
6499
6500 if (m_n == M1_N1) {
6501 dp_m_n = &crtc->config->dp_m_n;
6502 dp_m2_n2 = &crtc->config->dp_m2_n2;
6503 } else if (m_n == M2_N2) {
6504
6505 /*
6506 * M2_N2 registers are not supported. Hence m2_n2 divider value
6507 * needs to be programmed into M1_N1.
6508 */
6509 dp_m_n = &crtc->config->dp_m2_n2;
6510 } else {
6511 DRM_ERROR("Unsupported divider value\n");
6512 return;
6513 }
6514
6e3c9717
ACO
6515 if (crtc->config->has_pch_encoder)
6516 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
03afc4a2 6517 else
fe3cd48d 6518 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
03afc4a2
DV
6519}
6520
251ac862
DV
6521static void vlv_compute_dpll(struct intel_crtc *crtc,
6522 struct intel_crtc_state *pipe_config)
bdd4b6a6 6523{
03ed5cbf 6524 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
cd2d34d9 6525 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
03ed5cbf
VS
6526 if (crtc->pipe != PIPE_A)
6527 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
bdd4b6a6 6528
cd2d34d9 6529 /* DPLL not used with DSI, but still need the rest set up */
d7edc4e5 6530 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
cd2d34d9
VS
6531 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
6532 DPLL_EXT_BUFFER_ENABLE_VLV;
6533
03ed5cbf
VS
6534 pipe_config->dpll_hw_state.dpll_md =
6535 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
6536}
bdd4b6a6 6537
03ed5cbf
VS
6538static void chv_compute_dpll(struct intel_crtc *crtc,
6539 struct intel_crtc_state *pipe_config)
6540{
6541 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
cd2d34d9 6542 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
03ed5cbf
VS
6543 if (crtc->pipe != PIPE_A)
6544 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
6545
cd2d34d9 6546 /* DPLL not used with DSI, but still need the rest set up */
d7edc4e5 6547 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
cd2d34d9
VS
6548 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
6549
03ed5cbf
VS
6550 pipe_config->dpll_hw_state.dpll_md =
6551 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
bdd4b6a6
DV
6552}
6553
d288f65f 6554static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 6555 const struct intel_crtc_state *pipe_config)
a0c4da24 6556{
f47709a9 6557 struct drm_device *dev = crtc->base.dev;
fac5e23e 6558 struct drm_i915_private *dev_priv = to_i915(dev);
cd2d34d9 6559 enum pipe pipe = crtc->pipe;
bdd4b6a6 6560 u32 mdiv;
a0c4da24 6561 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 6562 u32 coreclk, reg_val;
a0c4da24 6563
cd2d34d9
VS
6564 /* Enable Refclk */
6565 I915_WRITE(DPLL(pipe),
6566 pipe_config->dpll_hw_state.dpll &
6567 ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
6568
6569 /* No need to actually set up the DPLL with DSI */
6570 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
6571 return;
6572
a580516d 6573 mutex_lock(&dev_priv->sb_lock);
09153000 6574
d288f65f
VS
6575 bestn = pipe_config->dpll.n;
6576 bestm1 = pipe_config->dpll.m1;
6577 bestm2 = pipe_config->dpll.m2;
6578 bestp1 = pipe_config->dpll.p1;
6579 bestp2 = pipe_config->dpll.p2;
a0c4da24 6580
89b667f8
JB
6581 /* See eDP HDMI DPIO driver vbios notes doc */
6582
6583 /* PLL B needs special handling */
bdd4b6a6 6584 if (pipe == PIPE_B)
5e69f97f 6585 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
6586
6587 /* Set up Tx target for periodic Rcomp update */
ab3c759a 6588 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
6589
6590 /* Disable target IRef on PLL */
ab3c759a 6591 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 6592 reg_val &= 0x00ffffff;
ab3c759a 6593 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
6594
6595 /* Disable fast lock */
ab3c759a 6596 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
6597
6598 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
6599 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
6600 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
6601 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 6602 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
6603
6604 /*
6605 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
6606 * but we don't support that).
6607 * Note: don't use the DAC post divider as it seems unstable.
6608 */
6609 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 6610 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 6611
a0c4da24 6612 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 6613 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 6614
89b667f8 6615 /* Set HBR and RBR LPF coefficients */
d288f65f 6616 if (pipe_config->port_clock == 162000 ||
2d84d2b3
VS
6617 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
6618 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
ab3c759a 6619 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 6620 0x009f0003);
89b667f8 6621 else
ab3c759a 6622 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
6623 0x00d0000f);
6624
37a5650b 6625 if (intel_crtc_has_dp_encoder(pipe_config)) {
89b667f8 6626 /* Use SSC source */
bdd4b6a6 6627 if (pipe == PIPE_A)
ab3c759a 6628 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6629 0x0df40000);
6630 else
ab3c759a 6631 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6632 0x0df70000);
6633 } else { /* HDMI or VGA */
6634 /* Use bend source */
bdd4b6a6 6635 if (pipe == PIPE_A)
ab3c759a 6636 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6637 0x0df70000);
6638 else
ab3c759a 6639 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6640 0x0df40000);
6641 }
a0c4da24 6642
ab3c759a 6643 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8 6644 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
2210ce7f 6645 if (intel_crtc_has_dp_encoder(crtc->config))
89b667f8 6646 coreclk |= 0x01000000;
ab3c759a 6647 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 6648
ab3c759a 6649 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
a580516d 6650 mutex_unlock(&dev_priv->sb_lock);
a0c4da24
JB
6651}
6652
d288f65f 6653static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 6654 const struct intel_crtc_state *pipe_config)
9d556c99
CML
6655{
6656 struct drm_device *dev = crtc->base.dev;
fac5e23e 6657 struct drm_i915_private *dev_priv = to_i915(dev);
cd2d34d9 6658 enum pipe pipe = crtc->pipe;
9d556c99 6659 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9cbe40c1 6660 u32 loopfilter, tribuf_calcntr;
9d556c99 6661 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
a945ce7e 6662 u32 dpio_val;
9cbe40c1 6663 int vco;
9d556c99 6664
cd2d34d9
VS
6665 /* Enable Refclk and SSC */
6666 I915_WRITE(DPLL(pipe),
6667 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
6668
6669 /* No need to actually set up the DPLL with DSI */
6670 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
6671 return;
6672
d288f65f
VS
6673 bestn = pipe_config->dpll.n;
6674 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
6675 bestm1 = pipe_config->dpll.m1;
6676 bestm2 = pipe_config->dpll.m2 >> 22;
6677 bestp1 = pipe_config->dpll.p1;
6678 bestp2 = pipe_config->dpll.p2;
9cbe40c1 6679 vco = pipe_config->dpll.vco;
a945ce7e 6680 dpio_val = 0;
9cbe40c1 6681 loopfilter = 0;
9d556c99 6682
a580516d 6683 mutex_lock(&dev_priv->sb_lock);
9d556c99 6684
9d556c99
CML
6685 /* p1 and p2 divider */
6686 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
6687 5 << DPIO_CHV_S1_DIV_SHIFT |
6688 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
6689 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
6690 1 << DPIO_CHV_K_DIV_SHIFT);
6691
6692 /* Feedback post-divider - m2 */
6693 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
6694
6695 /* Feedback refclk divider - n and m1 */
6696 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
6697 DPIO_CHV_M1_DIV_BY_2 |
6698 1 << DPIO_CHV_N_DIV_SHIFT);
6699
6700 /* M2 fraction division */
25a25dfc 6701 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
9d556c99
CML
6702
6703 /* M2 fraction division enable */
a945ce7e
VP
6704 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
6705 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
6706 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
6707 if (bestm2_frac)
6708 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
6709 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
9d556c99 6710
de3a0fde
VP
6711 /* Program digital lock detect threshold */
6712 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
6713 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
6714 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
6715 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
6716 if (!bestm2_frac)
6717 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
6718 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
6719
9d556c99 6720 /* Loop filter */
9cbe40c1
VP
6721 if (vco == 5400000) {
6722 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
6723 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
6724 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
6725 tribuf_calcntr = 0x9;
6726 } else if (vco <= 6200000) {
6727 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
6728 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
6729 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6730 tribuf_calcntr = 0x9;
6731 } else if (vco <= 6480000) {
6732 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
6733 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
6734 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6735 tribuf_calcntr = 0x8;
6736 } else {
6737 /* Not supported. Apply the same limits as in the max case */
6738 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
6739 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
6740 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6741 tribuf_calcntr = 0;
6742 }
9d556c99
CML
6743 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
6744
968040b2 6745 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
9cbe40c1
VP
6746 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
6747 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
6748 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
6749
9d556c99
CML
6750 /* AFC Recal */
6751 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
6752 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
6753 DPIO_AFC_RECAL);
6754
a580516d 6755 mutex_unlock(&dev_priv->sb_lock);
9d556c99
CML
6756}
6757
d288f65f
VS
6758/**
6759 * vlv_force_pll_on - forcibly enable just the PLL
6760 * @dev_priv: i915 private structure
6761 * @pipe: pipe PLL to enable
6762 * @dpll: PLL configuration
6763 *
6764 * Enable the PLL for @pipe using the supplied @dpll config. To be used
6765 * in cases where we need the PLL enabled even when @pipe is not going to
6766 * be enabled.
6767 */
30ad9814 6768int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
3f36b937 6769 const struct dpll *dpll)
d288f65f 6770{
b91eb5cc 6771 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
3f36b937
TU
6772 struct intel_crtc_state *pipe_config;
6773
6774 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
6775 if (!pipe_config)
6776 return -ENOMEM;
6777
6778 pipe_config->base.crtc = &crtc->base;
6779 pipe_config->pixel_multiplier = 1;
6780 pipe_config->dpll = *dpll;
d288f65f 6781
30ad9814 6782 if (IS_CHERRYVIEW(dev_priv)) {
3f36b937
TU
6783 chv_compute_dpll(crtc, pipe_config);
6784 chv_prepare_pll(crtc, pipe_config);
6785 chv_enable_pll(crtc, pipe_config);
d288f65f 6786 } else {
3f36b937
TU
6787 vlv_compute_dpll(crtc, pipe_config);
6788 vlv_prepare_pll(crtc, pipe_config);
6789 vlv_enable_pll(crtc, pipe_config);
d288f65f 6790 }
3f36b937
TU
6791
6792 kfree(pipe_config);
6793
6794 return 0;
d288f65f
VS
6795}
6796
6797/**
6798 * vlv_force_pll_off - forcibly disable just the PLL
6799 * @dev_priv: i915 private structure
6800 * @pipe: pipe PLL to disable
6801 *
6802 * Disable the PLL for @pipe. To be used in cases where we need
6803 * the PLL enabled even when @pipe is not going to be enabled.
6804 */
30ad9814 6805void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
d288f65f 6806{
30ad9814
VS
6807 if (IS_CHERRYVIEW(dev_priv))
6808 chv_disable_pll(dev_priv, pipe);
d288f65f 6809 else
30ad9814 6810 vlv_disable_pll(dev_priv, pipe);
d288f65f
VS
6811}
6812
251ac862
DV
6813static void i9xx_compute_dpll(struct intel_crtc *crtc,
6814 struct intel_crtc_state *crtc_state,
9e2c8475 6815 struct dpll *reduced_clock)
eb1cbe48 6816{
9b1e14f4 6817 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
eb1cbe48 6818 u32 dpll;
190f68c5 6819 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 6820
190f68c5 6821 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 6822
eb1cbe48
DV
6823 dpll = DPLL_VGA_MODE_DIS;
6824
2d84d2b3 6825 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
6826 dpll |= DPLLB_MODE_LVDS;
6827 else
6828 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 6829
73f67aa8
JN
6830 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
6831 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
190f68c5 6832 dpll |= (crtc_state->pixel_multiplier - 1)
198a037f 6833 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 6834 }
198a037f 6835
3d6e9ee0
VS
6836 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
6837 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
4a33e48d 6838 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 6839
37a5650b 6840 if (intel_crtc_has_dp_encoder(crtc_state))
4a33e48d 6841 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
6842
6843 /* compute bitmask from p1 value */
9b1e14f4 6844 if (IS_PINEVIEW(dev_priv))
eb1cbe48
DV
6845 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
6846 else {
6847 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
9beb5fea 6848 if (IS_G4X(dev_priv) && reduced_clock)
eb1cbe48
DV
6849 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
6850 }
6851 switch (clock->p2) {
6852 case 5:
6853 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
6854 break;
6855 case 7:
6856 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
6857 break;
6858 case 10:
6859 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
6860 break;
6861 case 14:
6862 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
6863 break;
6864 }
9b1e14f4 6865 if (INTEL_GEN(dev_priv) >= 4)
eb1cbe48
DV
6866 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
6867
190f68c5 6868 if (crtc_state->sdvo_tv_clock)
eb1cbe48 6869 dpll |= PLL_REF_INPUT_TVCLKINBC;
2d84d2b3 6870 else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 6871 intel_panel_use_ssc(dev_priv))
eb1cbe48
DV
6872 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
6873 else
6874 dpll |= PLL_REF_INPUT_DREFCLK;
6875
6876 dpll |= DPLL_VCO_ENABLE;
190f68c5 6877 crtc_state->dpll_hw_state.dpll = dpll;
8bcc2795 6878
9b1e14f4 6879 if (INTEL_GEN(dev_priv) >= 4) {
190f68c5 6880 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
ef1b460d 6881 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
190f68c5 6882 crtc_state->dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
DV
6883 }
6884}
6885
251ac862
DV
6886static void i8xx_compute_dpll(struct intel_crtc *crtc,
6887 struct intel_crtc_state *crtc_state,
9e2c8475 6888 struct dpll *reduced_clock)
eb1cbe48 6889{
f47709a9 6890 struct drm_device *dev = crtc->base.dev;
fac5e23e 6891 struct drm_i915_private *dev_priv = to_i915(dev);
eb1cbe48 6892 u32 dpll;
190f68c5 6893 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 6894
190f68c5 6895 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 6896
eb1cbe48
DV
6897 dpll = DPLL_VGA_MODE_DIS;
6898
2d84d2b3 6899 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
6900 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6901 } else {
6902 if (clock->p1 == 2)
6903 dpll |= PLL_P1_DIVIDE_BY_TWO;
6904 else
6905 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6906 if (clock->p2 == 4)
6907 dpll |= PLL_P2_DIVIDE_BY_4;
6908 }
6909
50a0bc90
TU
6910 if (!IS_I830(dev_priv) &&
6911 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
4a33e48d
DV
6912 dpll |= DPLL_DVO_2X_MODE;
6913
2d84d2b3 6914 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 6915 intel_panel_use_ssc(dev_priv))
eb1cbe48
DV
6916 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
6917 else
6918 dpll |= PLL_REF_INPUT_DREFCLK;
6919
6920 dpll |= DPLL_VCO_ENABLE;
190f68c5 6921 crtc_state->dpll_hw_state.dpll = dpll;
eb1cbe48
DV
6922}
6923
8a654f3b 6924static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c 6925{
6315b5d3 6926 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
b0e77b9c 6927 enum pipe pipe = intel_crtc->pipe;
6e3c9717 6928 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
7c5f93b0 6929 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
1caea6e9
VS
6930 uint32_t crtc_vtotal, crtc_vblank_end;
6931 int vsyncshift = 0;
4d8a62ea
DV
6932
6933 /* We need to be careful not to changed the adjusted mode, for otherwise
6934 * the hw state checker will get angry at the mismatch. */
6935 crtc_vtotal = adjusted_mode->crtc_vtotal;
6936 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 6937
609aeaca 6938 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 6939 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
6940 crtc_vtotal -= 1;
6941 crtc_vblank_end -= 1;
609aeaca 6942
2d84d2b3 6943 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
609aeaca
VS
6944 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
6945 else
6946 vsyncshift = adjusted_mode->crtc_hsync_start -
6947 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
6948 if (vsyncshift < 0)
6949 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
6950 }
6951
6315b5d3 6952 if (INTEL_GEN(dev_priv) > 3)
fe2b8f9d 6953 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 6954
fe2b8f9d 6955 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
6956 (adjusted_mode->crtc_hdisplay - 1) |
6957 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 6958 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
6959 (adjusted_mode->crtc_hblank_start - 1) |
6960 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 6961 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
6962 (adjusted_mode->crtc_hsync_start - 1) |
6963 ((adjusted_mode->crtc_hsync_end - 1) << 16));
6964
fe2b8f9d 6965 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 6966 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 6967 ((crtc_vtotal - 1) << 16));
fe2b8f9d 6968 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 6969 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 6970 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 6971 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
6972 (adjusted_mode->crtc_vsync_start - 1) |
6973 ((adjusted_mode->crtc_vsync_end - 1) << 16));
6974
b5e508d4
PZ
6975 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
6976 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
6977 * documented on the DDI_FUNC_CTL register description, EDP Input Select
6978 * bits. */
772c2a51 6979 if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
b5e508d4
PZ
6980 (pipe == PIPE_B || pipe == PIPE_C))
6981 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
6982
bc58be60
JN
6983}
6984
6985static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
6986{
6987 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 6988 struct drm_i915_private *dev_priv = to_i915(dev);
bc58be60
JN
6989 enum pipe pipe = intel_crtc->pipe;
6990
b0e77b9c
PZ
6991 /* pipesrc controls the size that is scaled from, which should
6992 * always be the user's requested size.
6993 */
6994 I915_WRITE(PIPESRC(pipe),
6e3c9717
ACO
6995 ((intel_crtc->config->pipe_src_w - 1) << 16) |
6996 (intel_crtc->config->pipe_src_h - 1));
b0e77b9c
PZ
6997}
6998
1bd1bd80 6999static void intel_get_pipe_timings(struct intel_crtc *crtc,
5cec258b 7000 struct intel_crtc_state *pipe_config)
1bd1bd80
DV
7001{
7002 struct drm_device *dev = crtc->base.dev;
fac5e23e 7003 struct drm_i915_private *dev_priv = to_i915(dev);
1bd1bd80
DV
7004 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7005 uint32_t tmp;
7006
7007 tmp = I915_READ(HTOTAL(cpu_transcoder));
2d112de7
ACO
7008 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7009 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7010 tmp = I915_READ(HBLANK(cpu_transcoder));
2d112de7
ACO
7011 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7012 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7013 tmp = I915_READ(HSYNC(cpu_transcoder));
2d112de7
ACO
7014 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7015 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7016
7017 tmp = I915_READ(VTOTAL(cpu_transcoder));
2d112de7
ACO
7018 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7019 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7020 tmp = I915_READ(VBLANK(cpu_transcoder));
2d112de7
ACO
7021 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7022 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7023 tmp = I915_READ(VSYNC(cpu_transcoder));
2d112de7
ACO
7024 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7025 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7026
7027 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
2d112de7
ACO
7028 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7029 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7030 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
1bd1bd80 7031 }
bc58be60
JN
7032}
7033
7034static void intel_get_pipe_src_size(struct intel_crtc *crtc,
7035 struct intel_crtc_state *pipe_config)
7036{
7037 struct drm_device *dev = crtc->base.dev;
fac5e23e 7038 struct drm_i915_private *dev_priv = to_i915(dev);
bc58be60 7039 u32 tmp;
1bd1bd80
DV
7040
7041 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
7042 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7043 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7044
2d112de7
ACO
7045 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7046 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
DV
7047}
7048
f6a83288 7049void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5cec258b 7050 struct intel_crtc_state *pipe_config)
babea61d 7051{
2d112de7
ACO
7052 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7053 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7054 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7055 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
babea61d 7056
2d112de7
ACO
7057 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7058 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7059 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7060 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
babea61d 7061
2d112de7 7062 mode->flags = pipe_config->base.adjusted_mode.flags;
cd13f5ab 7063 mode->type = DRM_MODE_TYPE_DRIVER;
babea61d 7064
2d112de7 7065 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
cd13f5ab
ML
7066
7067 mode->hsync = drm_mode_hsync(mode);
7068 mode->vrefresh = drm_mode_vrefresh(mode);
7069 drm_mode_set_name(mode);
babea61d
JB
7070}
7071
84b046f3
DV
7072static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7073{
6315b5d3 7074 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
84b046f3
DV
7075 uint32_t pipeconf;
7076
9f11a9e4 7077 pipeconf = 0;
84b046f3 7078
b6b5d049
VS
7079 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7080 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7081 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
67c72a12 7082
6e3c9717 7083 if (intel_crtc->config->double_wide)
cf532bb2 7084 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 7085
ff9ce46e 7086 /* only g4x and later have fancy bpc/dither controls */
9beb5fea
TU
7087 if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
7088 IS_CHERRYVIEW(dev_priv)) {
ff9ce46e 7089 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6e3c9717 7090 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
ff9ce46e 7091 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 7092 PIPECONF_DITHER_TYPE_SP;
84b046f3 7093
6e3c9717 7094 switch (intel_crtc->config->pipe_bpp) {
ff9ce46e
DV
7095 case 18:
7096 pipeconf |= PIPECONF_6BPC;
7097 break;
7098 case 24:
7099 pipeconf |= PIPECONF_8BPC;
7100 break;
7101 case 30:
7102 pipeconf |= PIPECONF_10BPC;
7103 break;
7104 default:
7105 /* Case prevented by intel_choose_pipe_bpp_dither. */
7106 BUG();
84b046f3
DV
7107 }
7108 }
7109
56b857a5 7110 if (HAS_PIPE_CXSR(dev_priv)) {
84b046f3
DV
7111 if (intel_crtc->lowfreq_avail) {
7112 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7113 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7114 } else {
7115 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
84b046f3
DV
7116 }
7117 }
7118
6e3c9717 7119 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
6315b5d3 7120 if (INTEL_GEN(dev_priv) < 4 ||
2d84d2b3 7121 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
efc2cfff
VS
7122 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7123 else
7124 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7125 } else
84b046f3
DV
7126 pipeconf |= PIPECONF_PROGRESSIVE;
7127
920a14b2 7128 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
666a4537 7129 intel_crtc->config->limited_color_range)
9f11a9e4 7130 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 7131
84b046f3
DV
7132 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7133 POSTING_READ(PIPECONF(intel_crtc->pipe));
7134}
7135
81c97f52
ACO
7136static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
7137 struct intel_crtc_state *crtc_state)
7138{
7139 struct drm_device *dev = crtc->base.dev;
fac5e23e 7140 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 7141 const struct intel_limit *limit;
81c97f52
ACO
7142 int refclk = 48000;
7143
7144 memset(&crtc_state->dpll_hw_state, 0,
7145 sizeof(crtc_state->dpll_hw_state));
7146
2d84d2b3 7147 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
81c97f52
ACO
7148 if (intel_panel_use_ssc(dev_priv)) {
7149 refclk = dev_priv->vbt.lvds_ssc_freq;
7150 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7151 }
7152
7153 limit = &intel_limits_i8xx_lvds;
2d84d2b3 7154 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
81c97f52
ACO
7155 limit = &intel_limits_i8xx_dvo;
7156 } else {
7157 limit = &intel_limits_i8xx_dac;
7158 }
7159
7160 if (!crtc_state->clock_set &&
7161 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7162 refclk, NULL, &crtc_state->dpll)) {
7163 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7164 return -EINVAL;
7165 }
7166
7167 i8xx_compute_dpll(crtc, crtc_state, NULL);
7168
7169 return 0;
7170}
7171
19ec6693
ACO
7172static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
7173 struct intel_crtc_state *crtc_state)
7174{
7175 struct drm_device *dev = crtc->base.dev;
fac5e23e 7176 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 7177 const struct intel_limit *limit;
19ec6693
ACO
7178 int refclk = 96000;
7179
7180 memset(&crtc_state->dpll_hw_state, 0,
7181 sizeof(crtc_state->dpll_hw_state));
7182
2d84d2b3 7183 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
19ec6693
ACO
7184 if (intel_panel_use_ssc(dev_priv)) {
7185 refclk = dev_priv->vbt.lvds_ssc_freq;
7186 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7187 }
7188
7189 if (intel_is_dual_link_lvds(dev))
7190 limit = &intel_limits_g4x_dual_channel_lvds;
7191 else
7192 limit = &intel_limits_g4x_single_channel_lvds;
2d84d2b3
VS
7193 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
7194 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
19ec6693 7195 limit = &intel_limits_g4x_hdmi;
2d84d2b3 7196 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
19ec6693
ACO
7197 limit = &intel_limits_g4x_sdvo;
7198 } else {
7199 /* The option is for other outputs */
7200 limit = &intel_limits_i9xx_sdvo;
7201 }
7202
7203 if (!crtc_state->clock_set &&
7204 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7205 refclk, NULL, &crtc_state->dpll)) {
7206 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7207 return -EINVAL;
7208 }
7209
7210 i9xx_compute_dpll(crtc, crtc_state, NULL);
7211
7212 return 0;
7213}
7214
70e8aa21
ACO
7215static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
7216 struct intel_crtc_state *crtc_state)
7217{
7218 struct drm_device *dev = crtc->base.dev;
fac5e23e 7219 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 7220 const struct intel_limit *limit;
70e8aa21
ACO
7221 int refclk = 96000;
7222
7223 memset(&crtc_state->dpll_hw_state, 0,
7224 sizeof(crtc_state->dpll_hw_state));
7225
2d84d2b3 7226 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
70e8aa21
ACO
7227 if (intel_panel_use_ssc(dev_priv)) {
7228 refclk = dev_priv->vbt.lvds_ssc_freq;
7229 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7230 }
7231
7232 limit = &intel_limits_pineview_lvds;
7233 } else {
7234 limit = &intel_limits_pineview_sdvo;
7235 }
7236
7237 if (!crtc_state->clock_set &&
7238 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7239 refclk, NULL, &crtc_state->dpll)) {
7240 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7241 return -EINVAL;
7242 }
7243
7244 i9xx_compute_dpll(crtc, crtc_state, NULL);
7245
7246 return 0;
7247}
7248
190f68c5
ACO
7249static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7250 struct intel_crtc_state *crtc_state)
79e53945 7251{
c7653199 7252 struct drm_device *dev = crtc->base.dev;
fac5e23e 7253 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 7254 const struct intel_limit *limit;
81c97f52 7255 int refclk = 96000;
79e53945 7256
dd3cd74a
ACO
7257 memset(&crtc_state->dpll_hw_state, 0,
7258 sizeof(crtc_state->dpll_hw_state));
7259
2d84d2b3 7260 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
70e8aa21
ACO
7261 if (intel_panel_use_ssc(dev_priv)) {
7262 refclk = dev_priv->vbt.lvds_ssc_freq;
7263 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7264 }
43565a06 7265
70e8aa21
ACO
7266 limit = &intel_limits_i9xx_lvds;
7267 } else {
7268 limit = &intel_limits_i9xx_sdvo;
81c97f52 7269 }
79e53945 7270
70e8aa21
ACO
7271 if (!crtc_state->clock_set &&
7272 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7273 refclk, NULL, &crtc_state->dpll)) {
7274 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7275 return -EINVAL;
f47709a9 7276 }
7026d4ac 7277
81c97f52 7278 i9xx_compute_dpll(crtc, crtc_state, NULL);
79e53945 7279
c8f7a0db 7280 return 0;
f564048e
EA
7281}
7282
65b3d6a9
ACO
7283static int chv_crtc_compute_clock(struct intel_crtc *crtc,
7284 struct intel_crtc_state *crtc_state)
7285{
7286 int refclk = 100000;
1b6f4958 7287 const struct intel_limit *limit = &intel_limits_chv;
65b3d6a9
ACO
7288
7289 memset(&crtc_state->dpll_hw_state, 0,
7290 sizeof(crtc_state->dpll_hw_state));
7291
65b3d6a9
ACO
7292 if (!crtc_state->clock_set &&
7293 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7294 refclk, NULL, &crtc_state->dpll)) {
7295 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7296 return -EINVAL;
7297 }
7298
7299 chv_compute_dpll(crtc, crtc_state);
7300
7301 return 0;
7302}
7303
7304static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
7305 struct intel_crtc_state *crtc_state)
7306{
7307 int refclk = 100000;
1b6f4958 7308 const struct intel_limit *limit = &intel_limits_vlv;
65b3d6a9
ACO
7309
7310 memset(&crtc_state->dpll_hw_state, 0,
7311 sizeof(crtc_state->dpll_hw_state));
7312
65b3d6a9
ACO
7313 if (!crtc_state->clock_set &&
7314 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7315 refclk, NULL, &crtc_state->dpll)) {
7316 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7317 return -EINVAL;
7318 }
7319
7320 vlv_compute_dpll(crtc, crtc_state);
7321
7322 return 0;
7323}
7324
2fa2fe9a 7325static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5cec258b 7326 struct intel_crtc_state *pipe_config)
2fa2fe9a 7327{
6315b5d3 7328 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2fa2fe9a
DV
7329 uint32_t tmp;
7330
50a0bc90
TU
7331 if (INTEL_GEN(dev_priv) <= 3 &&
7332 (IS_I830(dev_priv) || !IS_MOBILE(dev_priv)))
dc9e7dec
VS
7333 return;
7334
2fa2fe9a 7335 tmp = I915_READ(PFIT_CONTROL);
06922821
DV
7336 if (!(tmp & PFIT_ENABLE))
7337 return;
2fa2fe9a 7338
06922821 7339 /* Check whether the pfit is attached to our pipe. */
6315b5d3 7340 if (INTEL_GEN(dev_priv) < 4) {
2fa2fe9a
DV
7341 if (crtc->pipe != PIPE_B)
7342 return;
2fa2fe9a
DV
7343 } else {
7344 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7345 return;
7346 }
7347
06922821 7348 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a 7349 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
2fa2fe9a
DV
7350}
7351
acbec814 7352static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 7353 struct intel_crtc_state *pipe_config)
acbec814
JB
7354{
7355 struct drm_device *dev = crtc->base.dev;
fac5e23e 7356 struct drm_i915_private *dev_priv = to_i915(dev);
acbec814 7357 int pipe = pipe_config->cpu_transcoder;
9e2c8475 7358 struct dpll clock;
acbec814 7359 u32 mdiv;
662c6ecb 7360 int refclk = 100000;
acbec814 7361
b521973b
VS
7362 /* In case of DSI, DPLL will not be used */
7363 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
f573de5a
SK
7364 return;
7365
a580516d 7366 mutex_lock(&dev_priv->sb_lock);
ab3c759a 7367 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
a580516d 7368 mutex_unlock(&dev_priv->sb_lock);
acbec814
JB
7369
7370 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7371 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7372 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7373 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7374 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7375
dccbea3b 7376 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
acbec814
JB
7377}
7378
5724dbd1
DL
7379static void
7380i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7381 struct intel_initial_plane_config *plane_config)
1ad292b5
JB
7382{
7383 struct drm_device *dev = crtc->base.dev;
fac5e23e 7384 struct drm_i915_private *dev_priv = to_i915(dev);
1ad292b5
JB
7385 u32 val, base, offset;
7386 int pipe = crtc->pipe, plane = crtc->plane;
7387 int fourcc, pixel_format;
6761dd31 7388 unsigned int aligned_height;
b113d5ee 7389 struct drm_framebuffer *fb;
1b842c89 7390 struct intel_framebuffer *intel_fb;
1ad292b5 7391
42a7b088
DL
7392 val = I915_READ(DSPCNTR(plane));
7393 if (!(val & DISPLAY_PLANE_ENABLE))
7394 return;
7395
d9806c9f 7396 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 7397 if (!intel_fb) {
1ad292b5
JB
7398 DRM_DEBUG_KMS("failed to alloc fb\n");
7399 return;
7400 }
7401
1b842c89
DL
7402 fb = &intel_fb->base;
7403
d2e9f5fc
VS
7404 fb->dev = dev;
7405
6315b5d3 7406 if (INTEL_GEN(dev_priv) >= 4) {
18c5247e 7407 if (val & DISPPLANE_TILED) {
49af449b 7408 plane_config->tiling = I915_TILING_X;
bae781b2 7409 fb->modifier = I915_FORMAT_MOD_X_TILED;
18c5247e
DV
7410 }
7411 }
1ad292b5
JB
7412
7413 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 7414 fourcc = i9xx_format_to_fourcc(pixel_format);
2f3f4763 7415 fb->format = drm_format_info(fourcc);
1ad292b5 7416
6315b5d3 7417 if (INTEL_GEN(dev_priv) >= 4) {
49af449b 7418 if (plane_config->tiling)
1ad292b5
JB
7419 offset = I915_READ(DSPTILEOFF(plane));
7420 else
7421 offset = I915_READ(DSPLINOFF(plane));
7422 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7423 } else {
7424 base = I915_READ(DSPADDR(plane));
7425 }
7426 plane_config->base = base;
7427
7428 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
7429 fb->width = ((val >> 16) & 0xfff) + 1;
7430 fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
7431
7432 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 7433 fb->pitches[0] = val & 0xffffffc0;
1ad292b5 7434
24dbf51a
CW
7435 aligned_height = intel_fb_align_height(dev_priv,
7436 fb->height,
438b74a5 7437 fb->format->format,
bae781b2 7438 fb->modifier);
1ad292b5 7439
f37b5c2b 7440 plane_config->size = fb->pitches[0] * aligned_height;
1ad292b5 7441
2844a921
DL
7442 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7443 pipe_name(pipe), plane, fb->width, fb->height,
272725c7 7444 fb->format->cpp[0] * 8, base, fb->pitches[0],
2844a921 7445 plane_config->size);
1ad292b5 7446
2d14030b 7447 plane_config->fb = intel_fb;
1ad292b5
JB
7448}
7449
70b23a98 7450static void chv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 7451 struct intel_crtc_state *pipe_config)
70b23a98
VS
7452{
7453 struct drm_device *dev = crtc->base.dev;
fac5e23e 7454 struct drm_i915_private *dev_priv = to_i915(dev);
70b23a98
VS
7455 int pipe = pipe_config->cpu_transcoder;
7456 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9e2c8475 7457 struct dpll clock;
0d7b6b11 7458 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
70b23a98
VS
7459 int refclk = 100000;
7460
b521973b
VS
7461 /* In case of DSI, DPLL will not be used */
7462 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7463 return;
7464
a580516d 7465 mutex_lock(&dev_priv->sb_lock);
70b23a98
VS
7466 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
7467 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
7468 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
7469 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
0d7b6b11 7470 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
a580516d 7471 mutex_unlock(&dev_priv->sb_lock);
70b23a98
VS
7472
7473 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
0d7b6b11
ID
7474 clock.m2 = (pll_dw0 & 0xff) << 22;
7475 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
7476 clock.m2 |= pll_dw2 & 0x3fffff;
70b23a98
VS
7477 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
7478 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
7479 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
7480
dccbea3b 7481 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
70b23a98
VS
7482}
7483
0e8ffe1b 7484static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5cec258b 7485 struct intel_crtc_state *pipe_config)
0e8ffe1b 7486{
6315b5d3 7487 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1729050e 7488 enum intel_display_power_domain power_domain;
0e8ffe1b 7489 uint32_t tmp;
1729050e 7490 bool ret;
0e8ffe1b 7491
1729050e
ID
7492 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
7493 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0
ID
7494 return false;
7495
e143a21c 7496 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 7497 pipe_config->shared_dpll = NULL;
eccb140b 7498
1729050e
ID
7499 ret = false;
7500
0e8ffe1b
DV
7501 tmp = I915_READ(PIPECONF(crtc->pipe));
7502 if (!(tmp & PIPECONF_ENABLE))
1729050e 7503 goto out;
0e8ffe1b 7504
9beb5fea
TU
7505 if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
7506 IS_CHERRYVIEW(dev_priv)) {
42571aef
VS
7507 switch (tmp & PIPECONF_BPC_MASK) {
7508 case PIPECONF_6BPC:
7509 pipe_config->pipe_bpp = 18;
7510 break;
7511 case PIPECONF_8BPC:
7512 pipe_config->pipe_bpp = 24;
7513 break;
7514 case PIPECONF_10BPC:
7515 pipe_config->pipe_bpp = 30;
7516 break;
7517 default:
7518 break;
7519 }
7520 }
7521
920a14b2 7522 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
666a4537 7523 (tmp & PIPECONF_COLOR_RANGE_SELECT))
b5a9fa09
DV
7524 pipe_config->limited_color_range = true;
7525
6315b5d3 7526 if (INTEL_GEN(dev_priv) < 4)
282740f7
VS
7527 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
7528
1bd1bd80 7529 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 7530 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 7531
2fa2fe9a
DV
7532 i9xx_get_pfit_config(crtc, pipe_config);
7533
6315b5d3 7534 if (INTEL_GEN(dev_priv) >= 4) {
c231775c 7535 /* No way to read it out on pipes B and C */
920a14b2 7536 if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
c231775c
VS
7537 tmp = dev_priv->chv_dpll_md[crtc->pipe];
7538 else
7539 tmp = I915_READ(DPLL_MD(crtc->pipe));
6c49f241
DV
7540 pipe_config->pixel_multiplier =
7541 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
7542 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 7543 pipe_config->dpll_hw_state.dpll_md = tmp;
50a0bc90 7544 } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
73f67aa8 7545 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
6c49f241
DV
7546 tmp = I915_READ(DPLL(crtc->pipe));
7547 pipe_config->pixel_multiplier =
7548 ((tmp & SDVO_MULTIPLIER_MASK)
7549 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
7550 } else {
7551 /* Note that on i915G/GM the pixel multiplier is in the sdvo
7552 * port and will be fixed up in the encoder->get_config
7553 * function. */
7554 pipe_config->pixel_multiplier = 1;
7555 }
8bcc2795 7556 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
920a14b2 7557 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
1c4e0274
VS
7558 /*
7559 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
7560 * on 830. Filter it out here so that we don't
7561 * report errors due to that.
7562 */
50a0bc90 7563 if (IS_I830(dev_priv))
1c4e0274
VS
7564 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
7565
8bcc2795
DV
7566 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
7567 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
7568 } else {
7569 /* Mask out read-only status bits. */
7570 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
7571 DPLL_PORTC_READY_MASK |
7572 DPLL_PORTB_READY_MASK);
8bcc2795 7573 }
6c49f241 7574
920a14b2 7575 if (IS_CHERRYVIEW(dev_priv))
70b23a98 7576 chv_crtc_clock_get(crtc, pipe_config);
11a914c2 7577 else if (IS_VALLEYVIEW(dev_priv))
acbec814
JB
7578 vlv_crtc_clock_get(crtc, pipe_config);
7579 else
7580 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 7581
0f64614d
VS
7582 /*
7583 * Normally the dotclock is filled in by the encoder .get_config()
7584 * but in case the pipe is enabled w/o any ports we need a sane
7585 * default.
7586 */
7587 pipe_config->base.adjusted_mode.crtc_clock =
7588 pipe_config->port_clock / pipe_config->pixel_multiplier;
7589
1729050e
ID
7590 ret = true;
7591
7592out:
7593 intel_display_power_put(dev_priv, power_domain);
7594
7595 return ret;
0e8ffe1b
DV
7596}
7597
c39055b0 7598static void ironlake_init_pch_refclk(struct drm_i915_private *dev_priv)
13d83a67 7599{
13d83a67 7600 struct intel_encoder *encoder;
1c1a24d2 7601 int i;
74cfd7ac 7602 u32 val, final;
13d83a67 7603 bool has_lvds = false;
199e5d79 7604 bool has_cpu_edp = false;
199e5d79 7605 bool has_panel = false;
99eb6a01
KP
7606 bool has_ck505 = false;
7607 bool can_ssc = false;
1c1a24d2 7608 bool using_ssc_source = false;
13d83a67
JB
7609
7610 /* We need to take the global config into account */
c39055b0 7611 for_each_intel_encoder(&dev_priv->drm, encoder) {
199e5d79
KP
7612 switch (encoder->type) {
7613 case INTEL_OUTPUT_LVDS:
7614 has_panel = true;
7615 has_lvds = true;
7616 break;
7617 case INTEL_OUTPUT_EDP:
7618 has_panel = true;
2de6905f 7619 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
7620 has_cpu_edp = true;
7621 break;
6847d71b
PZ
7622 default:
7623 break;
13d83a67
JB
7624 }
7625 }
7626
6e266956 7627 if (HAS_PCH_IBX(dev_priv)) {
41aa3448 7628 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
7629 can_ssc = has_ck505;
7630 } else {
7631 has_ck505 = false;
7632 can_ssc = true;
7633 }
7634
1c1a24d2
L
7635 /* Check if any DPLLs are using the SSC source */
7636 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
7637 u32 temp = I915_READ(PCH_DPLL(i));
7638
7639 if (!(temp & DPLL_VCO_ENABLE))
7640 continue;
7641
7642 if ((temp & PLL_REF_INPUT_MASK) ==
7643 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
7644 using_ssc_source = true;
7645 break;
7646 }
7647 }
7648
7649 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
7650 has_panel, has_lvds, has_ck505, using_ssc_source);
13d83a67
JB
7651
7652 /* Ironlake: try to setup display ref clock before DPLL
7653 * enabling. This is only under driver's control after
7654 * PCH B stepping, previous chipset stepping should be
7655 * ignoring this setting.
7656 */
74cfd7ac
CW
7657 val = I915_READ(PCH_DREF_CONTROL);
7658
7659 /* As we must carefully and slowly disable/enable each source in turn,
7660 * compute the final state we want first and check if we need to
7661 * make any changes at all.
7662 */
7663 final = val;
7664 final &= ~DREF_NONSPREAD_SOURCE_MASK;
7665 if (has_ck505)
7666 final |= DREF_NONSPREAD_CK505_ENABLE;
7667 else
7668 final |= DREF_NONSPREAD_SOURCE_ENABLE;
7669
8c07eb68 7670 final &= ~DREF_SSC_SOURCE_MASK;
74cfd7ac 7671 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8c07eb68 7672 final &= ~DREF_SSC1_ENABLE;
74cfd7ac
CW
7673
7674 if (has_panel) {
7675 final |= DREF_SSC_SOURCE_ENABLE;
7676
7677 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7678 final |= DREF_SSC1_ENABLE;
7679
7680 if (has_cpu_edp) {
7681 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7682 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
7683 else
7684 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
7685 } else
7686 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
1c1a24d2
L
7687 } else if (using_ssc_source) {
7688 final |= DREF_SSC_SOURCE_ENABLE;
7689 final |= DREF_SSC1_ENABLE;
74cfd7ac
CW
7690 }
7691
7692 if (final == val)
7693 return;
7694
13d83a67 7695 /* Always enable nonspread source */
74cfd7ac 7696 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 7697
99eb6a01 7698 if (has_ck505)
74cfd7ac 7699 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 7700 else
74cfd7ac 7701 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 7702
199e5d79 7703 if (has_panel) {
74cfd7ac
CW
7704 val &= ~DREF_SSC_SOURCE_MASK;
7705 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 7706
199e5d79 7707 /* SSC must be turned on before enabling the CPU output */
99eb6a01 7708 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 7709 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 7710 val |= DREF_SSC1_ENABLE;
e77166b5 7711 } else
74cfd7ac 7712 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
7713
7714 /* Get SSC going before enabling the outputs */
74cfd7ac 7715 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
7716 POSTING_READ(PCH_DREF_CONTROL);
7717 udelay(200);
7718
74cfd7ac 7719 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
7720
7721 /* Enable CPU source on CPU attached eDP */
199e5d79 7722 if (has_cpu_edp) {
99eb6a01 7723 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 7724 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 7725 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 7726 } else
74cfd7ac 7727 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 7728 } else
74cfd7ac 7729 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 7730
74cfd7ac 7731 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
7732 POSTING_READ(PCH_DREF_CONTROL);
7733 udelay(200);
7734 } else {
1c1a24d2 7735 DRM_DEBUG_KMS("Disabling CPU source output\n");
199e5d79 7736
74cfd7ac 7737 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
7738
7739 /* Turn off CPU output */
74cfd7ac 7740 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 7741
74cfd7ac 7742 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
7743 POSTING_READ(PCH_DREF_CONTROL);
7744 udelay(200);
7745
1c1a24d2
L
7746 if (!using_ssc_source) {
7747 DRM_DEBUG_KMS("Disabling SSC source\n");
199e5d79 7748
1c1a24d2
L
7749 /* Turn off the SSC source */
7750 val &= ~DREF_SSC_SOURCE_MASK;
7751 val |= DREF_SSC_SOURCE_DISABLE;
f165d283 7752
1c1a24d2
L
7753 /* Turn off SSC1 */
7754 val &= ~DREF_SSC1_ENABLE;
7755
7756 I915_WRITE(PCH_DREF_CONTROL, val);
7757 POSTING_READ(PCH_DREF_CONTROL);
7758 udelay(200);
7759 }
13d83a67 7760 }
74cfd7ac
CW
7761
7762 BUG_ON(val != final);
13d83a67
JB
7763}
7764
f31f2d55 7765static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 7766{
f31f2d55 7767 uint32_t tmp;
dde86e2d 7768
0ff066a9
PZ
7769 tmp = I915_READ(SOUTH_CHICKEN2);
7770 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
7771 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 7772
cf3598c2
ID
7773 if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
7774 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
0ff066a9 7775 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 7776
0ff066a9
PZ
7777 tmp = I915_READ(SOUTH_CHICKEN2);
7778 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
7779 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 7780
cf3598c2
ID
7781 if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
7782 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
0ff066a9 7783 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
7784}
7785
7786/* WaMPhyProgramming:hsw */
7787static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
7788{
7789 uint32_t tmp;
dde86e2d
PZ
7790
7791 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
7792 tmp &= ~(0xFF << 24);
7793 tmp |= (0x12 << 24);
7794 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
7795
dde86e2d
PZ
7796 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
7797 tmp |= (1 << 11);
7798 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
7799
7800 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
7801 tmp |= (1 << 11);
7802 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
7803
dde86e2d
PZ
7804 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
7805 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7806 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
7807
7808 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
7809 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7810 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
7811
0ff066a9
PZ
7812 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
7813 tmp &= ~(7 << 13);
7814 tmp |= (5 << 13);
7815 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 7816
0ff066a9
PZ
7817 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
7818 tmp &= ~(7 << 13);
7819 tmp |= (5 << 13);
7820 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
7821
7822 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
7823 tmp &= ~0xFF;
7824 tmp |= 0x1C;
7825 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
7826
7827 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
7828 tmp &= ~0xFF;
7829 tmp |= 0x1C;
7830 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
7831
7832 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
7833 tmp &= ~(0xFF << 16);
7834 tmp |= (0x1C << 16);
7835 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
7836
7837 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
7838 tmp &= ~(0xFF << 16);
7839 tmp |= (0x1C << 16);
7840 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
7841
0ff066a9
PZ
7842 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
7843 tmp |= (1 << 27);
7844 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 7845
0ff066a9
PZ
7846 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
7847 tmp |= (1 << 27);
7848 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 7849
0ff066a9
PZ
7850 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
7851 tmp &= ~(0xF << 28);
7852 tmp |= (4 << 28);
7853 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 7854
0ff066a9
PZ
7855 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
7856 tmp &= ~(0xF << 28);
7857 tmp |= (4 << 28);
7858 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
7859}
7860
2fa86a1f
PZ
7861/* Implements 3 different sequences from BSpec chapter "Display iCLK
7862 * Programming" based on the parameters passed:
7863 * - Sequence to enable CLKOUT_DP
7864 * - Sequence to enable CLKOUT_DP without spread
7865 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
7866 */
c39055b0
ACO
7867static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
7868 bool with_spread, bool with_fdi)
f31f2d55 7869{
2fa86a1f
PZ
7870 uint32_t reg, tmp;
7871
7872 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
7873 with_spread = true;
4f8036a2
TU
7874 if (WARN(HAS_PCH_LPT_LP(dev_priv) &&
7875 with_fdi, "LP PCH doesn't have FDI\n"))
2fa86a1f 7876 with_fdi = false;
f31f2d55 7877
a580516d 7878 mutex_lock(&dev_priv->sb_lock);
f31f2d55
PZ
7879
7880 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7881 tmp &= ~SBI_SSCCTL_DISABLE;
7882 tmp |= SBI_SSCCTL_PATHALT;
7883 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7884
7885 udelay(24);
7886
2fa86a1f
PZ
7887 if (with_spread) {
7888 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7889 tmp &= ~SBI_SSCCTL_PATHALT;
7890 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 7891
2fa86a1f
PZ
7892 if (with_fdi) {
7893 lpt_reset_fdi_mphy(dev_priv);
7894 lpt_program_fdi_mphy(dev_priv);
7895 }
7896 }
dde86e2d 7897
4f8036a2 7898 reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
2fa86a1f
PZ
7899 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
7900 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
7901 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246 7902
a580516d 7903 mutex_unlock(&dev_priv->sb_lock);
dde86e2d
PZ
7904}
7905
47701c3b 7906/* Sequence to disable CLKOUT_DP */
c39055b0 7907static void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
47701c3b 7908{
47701c3b
PZ
7909 uint32_t reg, tmp;
7910
a580516d 7911 mutex_lock(&dev_priv->sb_lock);
47701c3b 7912
4f8036a2 7913 reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
47701c3b
PZ
7914 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
7915 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
7916 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
7917
7918 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7919 if (!(tmp & SBI_SSCCTL_DISABLE)) {
7920 if (!(tmp & SBI_SSCCTL_PATHALT)) {
7921 tmp |= SBI_SSCCTL_PATHALT;
7922 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7923 udelay(32);
7924 }
7925 tmp |= SBI_SSCCTL_DISABLE;
7926 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7927 }
7928
a580516d 7929 mutex_unlock(&dev_priv->sb_lock);
47701c3b
PZ
7930}
7931
f7be2c21
VS
7932#define BEND_IDX(steps) ((50 + (steps)) / 5)
7933
7934static const uint16_t sscdivintphase[] = {
7935 [BEND_IDX( 50)] = 0x3B23,
7936 [BEND_IDX( 45)] = 0x3B23,
7937 [BEND_IDX( 40)] = 0x3C23,
7938 [BEND_IDX( 35)] = 0x3C23,
7939 [BEND_IDX( 30)] = 0x3D23,
7940 [BEND_IDX( 25)] = 0x3D23,
7941 [BEND_IDX( 20)] = 0x3E23,
7942 [BEND_IDX( 15)] = 0x3E23,
7943 [BEND_IDX( 10)] = 0x3F23,
7944 [BEND_IDX( 5)] = 0x3F23,
7945 [BEND_IDX( 0)] = 0x0025,
7946 [BEND_IDX( -5)] = 0x0025,
7947 [BEND_IDX(-10)] = 0x0125,
7948 [BEND_IDX(-15)] = 0x0125,
7949 [BEND_IDX(-20)] = 0x0225,
7950 [BEND_IDX(-25)] = 0x0225,
7951 [BEND_IDX(-30)] = 0x0325,
7952 [BEND_IDX(-35)] = 0x0325,
7953 [BEND_IDX(-40)] = 0x0425,
7954 [BEND_IDX(-45)] = 0x0425,
7955 [BEND_IDX(-50)] = 0x0525,
7956};
7957
7958/*
7959 * Bend CLKOUT_DP
7960 * steps -50 to 50 inclusive, in steps of 5
7961 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
7962 * change in clock period = -(steps / 10) * 5.787 ps
7963 */
7964static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
7965{
7966 uint32_t tmp;
7967 int idx = BEND_IDX(steps);
7968
7969 if (WARN_ON(steps % 5 != 0))
7970 return;
7971
7972 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
7973 return;
7974
7975 mutex_lock(&dev_priv->sb_lock);
7976
7977 if (steps % 10 != 0)
7978 tmp = 0xAAAAAAAB;
7979 else
7980 tmp = 0x00000000;
7981 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
7982
7983 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
7984 tmp &= 0xffff0000;
7985 tmp |= sscdivintphase[idx];
7986 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
7987
7988 mutex_unlock(&dev_priv->sb_lock);
7989}
7990
7991#undef BEND_IDX
7992
c39055b0 7993static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
bf8fa3d3 7994{
bf8fa3d3
PZ
7995 struct intel_encoder *encoder;
7996 bool has_vga = false;
7997
c39055b0 7998 for_each_intel_encoder(&dev_priv->drm, encoder) {
bf8fa3d3
PZ
7999 switch (encoder->type) {
8000 case INTEL_OUTPUT_ANALOG:
8001 has_vga = true;
8002 break;
6847d71b
PZ
8003 default:
8004 break;
bf8fa3d3
PZ
8005 }
8006 }
8007
f7be2c21 8008 if (has_vga) {
c39055b0
ACO
8009 lpt_bend_clkout_dp(dev_priv, 0);
8010 lpt_enable_clkout_dp(dev_priv, true, true);
f7be2c21 8011 } else {
c39055b0 8012 lpt_disable_clkout_dp(dev_priv);
f7be2c21 8013 }
bf8fa3d3
PZ
8014}
8015
dde86e2d
PZ
8016/*
8017 * Initialize reference clocks when the driver loads
8018 */
c39055b0 8019void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
dde86e2d 8020{
6e266956 8021 if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
c39055b0 8022 ironlake_init_pch_refclk(dev_priv);
6e266956 8023 else if (HAS_PCH_LPT(dev_priv))
c39055b0 8024 lpt_init_pch_refclk(dev_priv);
dde86e2d
PZ
8025}
8026
6ff93609 8027static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 8028{
fac5e23e 8029 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
79e53945
JB
8030 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8031 int pipe = intel_crtc->pipe;
c8203565
PZ
8032 uint32_t val;
8033
78114071 8034 val = 0;
c8203565 8035
6e3c9717 8036 switch (intel_crtc->config->pipe_bpp) {
c8203565 8037 case 18:
dfd07d72 8038 val |= PIPECONF_6BPC;
c8203565
PZ
8039 break;
8040 case 24:
dfd07d72 8041 val |= PIPECONF_8BPC;
c8203565
PZ
8042 break;
8043 case 30:
dfd07d72 8044 val |= PIPECONF_10BPC;
c8203565
PZ
8045 break;
8046 case 36:
dfd07d72 8047 val |= PIPECONF_12BPC;
c8203565
PZ
8048 break;
8049 default:
cc769b62
PZ
8050 /* Case prevented by intel_choose_pipe_bpp_dither. */
8051 BUG();
c8203565
PZ
8052 }
8053
6e3c9717 8054 if (intel_crtc->config->dither)
c8203565
PZ
8055 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8056
6e3c9717 8057 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
8058 val |= PIPECONF_INTERLACED_ILK;
8059 else
8060 val |= PIPECONF_PROGRESSIVE;
8061
6e3c9717 8062 if (intel_crtc->config->limited_color_range)
3685a8f3 8063 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 8064
c8203565
PZ
8065 I915_WRITE(PIPECONF(pipe), val);
8066 POSTING_READ(PIPECONF(pipe));
8067}
8068
6ff93609 8069static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 8070{
fac5e23e 8071 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
ee2b0b38 8072 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 8073 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
391bf048 8074 u32 val = 0;
ee2b0b38 8075
391bf048 8076 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
ee2b0b38
PZ
8077 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8078
6e3c9717 8079 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
8080 val |= PIPECONF_INTERLACED_ILK;
8081 else
8082 val |= PIPECONF_PROGRESSIVE;
8083
702e7a56
PZ
8084 I915_WRITE(PIPECONF(cpu_transcoder), val);
8085 POSTING_READ(PIPECONF(cpu_transcoder));
391bf048
JN
8086}
8087
391bf048
JN
8088static void haswell_set_pipemisc(struct drm_crtc *crtc)
8089{
fac5e23e 8090 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
391bf048 8091 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756f85cf 8092
391bf048
JN
8093 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
8094 u32 val = 0;
756f85cf 8095
6e3c9717 8096 switch (intel_crtc->config->pipe_bpp) {
756f85cf
PZ
8097 case 18:
8098 val |= PIPEMISC_DITHER_6_BPC;
8099 break;
8100 case 24:
8101 val |= PIPEMISC_DITHER_8_BPC;
8102 break;
8103 case 30:
8104 val |= PIPEMISC_DITHER_10_BPC;
8105 break;
8106 case 36:
8107 val |= PIPEMISC_DITHER_12_BPC;
8108 break;
8109 default:
8110 /* Case prevented by pipe_config_set_bpp. */
8111 BUG();
8112 }
8113
6e3c9717 8114 if (intel_crtc->config->dither)
756f85cf
PZ
8115 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8116
391bf048 8117 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
756f85cf 8118 }
ee2b0b38
PZ
8119}
8120
d4b1931c
PZ
8121int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8122{
8123 /*
8124 * Account for spread spectrum to avoid
8125 * oversubscribing the link. Max center spread
8126 * is 2.5%; use 5% for safety's sake.
8127 */
8128 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 8129 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
8130}
8131
7429e9d4 8132static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 8133{
7429e9d4 8134 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
8135}
8136
b75ca6f6
ACO
8137static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
8138 struct intel_crtc_state *crtc_state,
9e2c8475 8139 struct dpll *reduced_clock)
79e53945 8140{
de13a2e3 8141 struct drm_crtc *crtc = &intel_crtc->base;
79e53945 8142 struct drm_device *dev = crtc->dev;
fac5e23e 8143 struct drm_i915_private *dev_priv = to_i915(dev);
b75ca6f6 8144 u32 dpll, fp, fp2;
3d6e9ee0 8145 int factor;
79e53945 8146
c1858123 8147 /* Enable autotuning of the PLL clock (if permissible) */
8febb297 8148 factor = 21;
3d6e9ee0 8149 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8febb297 8150 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 8151 dev_priv->vbt.lvds_ssc_freq == 100000) ||
6e266956 8152 (HAS_PCH_IBX(dev_priv) && intel_is_dual_link_lvds(dev)))
8febb297 8153 factor = 25;
190f68c5 8154 } else if (crtc_state->sdvo_tv_clock)
8febb297 8155 factor = 20;
c1858123 8156
b75ca6f6
ACO
8157 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
8158
190f68c5 8159 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
b75ca6f6
ACO
8160 fp |= FP_CB_TUNE;
8161
8162 if (reduced_clock) {
8163 fp2 = i9xx_dpll_compute_fp(reduced_clock);
2c07245f 8164
b75ca6f6
ACO
8165 if (reduced_clock->m < factor * reduced_clock->n)
8166 fp2 |= FP_CB_TUNE;
8167 } else {
8168 fp2 = fp;
8169 }
9a7c7890 8170
5eddb70b 8171 dpll = 0;
2c07245f 8172
3d6e9ee0 8173 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
a07d6787
EA
8174 dpll |= DPLLB_MODE_LVDS;
8175 else
8176 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 8177
190f68c5 8178 dpll |= (crtc_state->pixel_multiplier - 1)
ef1b460d 8179 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f 8180
3d6e9ee0
VS
8181 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
8182 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
4a33e48d 8183 dpll |= DPLL_SDVO_HIGH_SPEED;
3d6e9ee0 8184
37a5650b 8185 if (intel_crtc_has_dp_encoder(crtc_state))
4a33e48d 8186 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 8187
7d7f8633
VS
8188 /*
8189 * The high speed IO clock is only really required for
8190 * SDVO/HDMI/DP, but we also enable it for CRT to make it
8191 * possible to share the DPLL between CRT and HDMI. Enabling
8192 * the clock needlessly does no real harm, except use up a
8193 * bit of power potentially.
8194 *
8195 * We'll limit this to IVB with 3 pipes, since it has only two
8196 * DPLLs and so DPLL sharing is the only way to get three pipes
8197 * driving PCH ports at the same time. On SNB we could do this,
8198 * and potentially avoid enabling the second DPLL, but it's not
8199 * clear if it''s a win or loss power wise. No point in doing
8200 * this on ILK at all since it has a fixed DPLL<->pipe mapping.
8201 */
8202 if (INTEL_INFO(dev_priv)->num_pipes == 3 &&
8203 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
8204 dpll |= DPLL_SDVO_HIGH_SPEED;
8205
a07d6787 8206 /* compute bitmask from p1 value */
190f68c5 8207 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 8208 /* also FPA1 */
190f68c5 8209 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 8210
190f68c5 8211 switch (crtc_state->dpll.p2) {
a07d6787
EA
8212 case 5:
8213 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8214 break;
8215 case 7:
8216 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8217 break;
8218 case 10:
8219 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8220 break;
8221 case 14:
8222 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8223 break;
79e53945
JB
8224 }
8225
3d6e9ee0
VS
8226 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8227 intel_panel_use_ssc(dev_priv))
43565a06 8228 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
8229 else
8230 dpll |= PLL_REF_INPUT_DREFCLK;
8231
b75ca6f6
ACO
8232 dpll |= DPLL_VCO_ENABLE;
8233
8234 crtc_state->dpll_hw_state.dpll = dpll;
8235 crtc_state->dpll_hw_state.fp0 = fp;
8236 crtc_state->dpll_hw_state.fp1 = fp2;
de13a2e3
PZ
8237}
8238
190f68c5
ACO
8239static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8240 struct intel_crtc_state *crtc_state)
de13a2e3 8241{
997c030c 8242 struct drm_device *dev = crtc->base.dev;
fac5e23e 8243 struct drm_i915_private *dev_priv = to_i915(dev);
9e2c8475 8244 struct dpll reduced_clock;
7ed9f894 8245 bool has_reduced_clock = false;
e2b78267 8246 struct intel_shared_dpll *pll;
1b6f4958 8247 const struct intel_limit *limit;
997c030c 8248 int refclk = 120000;
de13a2e3 8249
dd3cd74a
ACO
8250 memset(&crtc_state->dpll_hw_state, 0,
8251 sizeof(crtc_state->dpll_hw_state));
8252
ded220e2
ACO
8253 crtc->lowfreq_avail = false;
8254
8255 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
8256 if (!crtc_state->has_pch_encoder)
8257 return 0;
79e53945 8258
2d84d2b3 8259 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
997c030c
ACO
8260 if (intel_panel_use_ssc(dev_priv)) {
8261 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
8262 dev_priv->vbt.lvds_ssc_freq);
8263 refclk = dev_priv->vbt.lvds_ssc_freq;
8264 }
8265
8266 if (intel_is_dual_link_lvds(dev)) {
8267 if (refclk == 100000)
8268 limit = &intel_limits_ironlake_dual_lvds_100m;
8269 else
8270 limit = &intel_limits_ironlake_dual_lvds;
8271 } else {
8272 if (refclk == 100000)
8273 limit = &intel_limits_ironlake_single_lvds_100m;
8274 else
8275 limit = &intel_limits_ironlake_single_lvds;
8276 }
8277 } else {
8278 limit = &intel_limits_ironlake_dac;
8279 }
8280
364ee29d 8281 if (!crtc_state->clock_set &&
997c030c
ACO
8282 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8283 refclk, NULL, &crtc_state->dpll)) {
364ee29d
ACO
8284 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8285 return -EINVAL;
f47709a9 8286 }
79e53945 8287
b75ca6f6
ACO
8288 ironlake_compute_dpll(crtc, crtc_state,
8289 has_reduced_clock ? &reduced_clock : NULL);
66e985c0 8290
ded220e2
ACO
8291 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
8292 if (pll == NULL) {
8293 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
8294 pipe_name(crtc->pipe));
8295 return -EINVAL;
3fb37703 8296 }
79e53945 8297
2d84d2b3 8298 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ded220e2 8299 has_reduced_clock)
c7653199 8300 crtc->lowfreq_avail = true;
e2b78267 8301
c8f7a0db 8302 return 0;
79e53945
JB
8303}
8304
eb14cb74
VS
8305static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8306 struct intel_link_m_n *m_n)
8307{
8308 struct drm_device *dev = crtc->base.dev;
fac5e23e 8309 struct drm_i915_private *dev_priv = to_i915(dev);
eb14cb74
VS
8310 enum pipe pipe = crtc->pipe;
8311
8312 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8313 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8314 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8315 & ~TU_SIZE_MASK;
8316 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8317 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8318 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8319}
8320
8321static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8322 enum transcoder transcoder,
b95af8be
VK
8323 struct intel_link_m_n *m_n,
8324 struct intel_link_m_n *m2_n2)
72419203 8325{
6315b5d3 8326 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
eb14cb74 8327 enum pipe pipe = crtc->pipe;
72419203 8328
6315b5d3 8329 if (INTEL_GEN(dev_priv) >= 5) {
eb14cb74
VS
8330 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8331 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8332 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8333 & ~TU_SIZE_MASK;
8334 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8335 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8336 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
b95af8be
VK
8337 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8338 * gen < 8) and if DRRS is supported (to make sure the
8339 * registers are not unnecessarily read).
8340 */
6315b5d3 8341 if (m2_n2 && INTEL_GEN(dev_priv) < 8 &&
6e3c9717 8342 crtc->config->has_drrs) {
b95af8be
VK
8343 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8344 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8345 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8346 & ~TU_SIZE_MASK;
8347 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8348 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8349 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8350 }
eb14cb74
VS
8351 } else {
8352 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8353 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8354 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8355 & ~TU_SIZE_MASK;
8356 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8357 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8358 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8359 }
8360}
8361
8362void intel_dp_get_m_n(struct intel_crtc *crtc,
5cec258b 8363 struct intel_crtc_state *pipe_config)
eb14cb74 8364{
681a8504 8365 if (pipe_config->has_pch_encoder)
eb14cb74
VS
8366 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8367 else
8368 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be
VK
8369 &pipe_config->dp_m_n,
8370 &pipe_config->dp_m2_n2);
eb14cb74 8371}
72419203 8372
eb14cb74 8373static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5cec258b 8374 struct intel_crtc_state *pipe_config)
eb14cb74
VS
8375{
8376 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be 8377 &pipe_config->fdi_m_n, NULL);
72419203
DV
8378}
8379
bd2e244f 8380static void skylake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 8381 struct intel_crtc_state *pipe_config)
bd2e244f
JB
8382{
8383 struct drm_device *dev = crtc->base.dev;
fac5e23e 8384 struct drm_i915_private *dev_priv = to_i915(dev);
a1b2278e
CK
8385 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
8386 uint32_t ps_ctrl = 0;
8387 int id = -1;
8388 int i;
bd2e244f 8389
a1b2278e
CK
8390 /* find scaler attached to this pipe */
8391 for (i = 0; i < crtc->num_scalers; i++) {
8392 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
8393 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
8394 id = i;
8395 pipe_config->pch_pfit.enabled = true;
8396 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
8397 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
8398 break;
8399 }
8400 }
bd2e244f 8401
a1b2278e
CK
8402 scaler_state->scaler_id = id;
8403 if (id >= 0) {
8404 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
8405 } else {
8406 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
bd2e244f
JB
8407 }
8408}
8409
5724dbd1
DL
8410static void
8411skylake_get_initial_plane_config(struct intel_crtc *crtc,
8412 struct intel_initial_plane_config *plane_config)
bc8d7dff
DL
8413{
8414 struct drm_device *dev = crtc->base.dev;
fac5e23e 8415 struct drm_i915_private *dev_priv = to_i915(dev);
40f46283 8416 u32 val, base, offset, stride_mult, tiling;
bc8d7dff
DL
8417 int pipe = crtc->pipe;
8418 int fourcc, pixel_format;
6761dd31 8419 unsigned int aligned_height;
bc8d7dff 8420 struct drm_framebuffer *fb;
1b842c89 8421 struct intel_framebuffer *intel_fb;
bc8d7dff 8422
d9806c9f 8423 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 8424 if (!intel_fb) {
bc8d7dff
DL
8425 DRM_DEBUG_KMS("failed to alloc fb\n");
8426 return;
8427 }
8428
1b842c89
DL
8429 fb = &intel_fb->base;
8430
d2e9f5fc
VS
8431 fb->dev = dev;
8432
bc8d7dff 8433 val = I915_READ(PLANE_CTL(pipe, 0));
42a7b088
DL
8434 if (!(val & PLANE_CTL_ENABLE))
8435 goto error;
8436
bc8d7dff
DL
8437 pixel_format = val & PLANE_CTL_FORMAT_MASK;
8438 fourcc = skl_format_to_fourcc(pixel_format,
8439 val & PLANE_CTL_ORDER_RGBX,
8440 val & PLANE_CTL_ALPHA_MASK);
2f3f4763 8441 fb->format = drm_format_info(fourcc);
bc8d7dff 8442
40f46283
DL
8443 tiling = val & PLANE_CTL_TILED_MASK;
8444 switch (tiling) {
8445 case PLANE_CTL_TILED_LINEAR:
bae781b2 8446 fb->modifier = DRM_FORMAT_MOD_NONE;
40f46283
DL
8447 break;
8448 case PLANE_CTL_TILED_X:
8449 plane_config->tiling = I915_TILING_X;
bae781b2 8450 fb->modifier = I915_FORMAT_MOD_X_TILED;
40f46283
DL
8451 break;
8452 case PLANE_CTL_TILED_Y:
bae781b2 8453 fb->modifier = I915_FORMAT_MOD_Y_TILED;
40f46283
DL
8454 break;
8455 case PLANE_CTL_TILED_YF:
bae781b2 8456 fb->modifier = I915_FORMAT_MOD_Yf_TILED;
40f46283
DL
8457 break;
8458 default:
8459 MISSING_CASE(tiling);
8460 goto error;
8461 }
8462
bc8d7dff
DL
8463 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
8464 plane_config->base = base;
8465
8466 offset = I915_READ(PLANE_OFFSET(pipe, 0));
8467
8468 val = I915_READ(PLANE_SIZE(pipe, 0));
8469 fb->height = ((val >> 16) & 0xfff) + 1;
8470 fb->width = ((val >> 0) & 0x1fff) + 1;
8471
8472 val = I915_READ(PLANE_STRIDE(pipe, 0));
bae781b2 8473 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier,
438b74a5 8474 fb->format->format);
bc8d7dff
DL
8475 fb->pitches[0] = (val & 0x3ff) * stride_mult;
8476
24dbf51a
CW
8477 aligned_height = intel_fb_align_height(dev_priv,
8478 fb->height,
438b74a5 8479 fb->format->format,
bae781b2 8480 fb->modifier);
bc8d7dff 8481
f37b5c2b 8482 plane_config->size = fb->pitches[0] * aligned_height;
bc8d7dff
DL
8483
8484 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8485 pipe_name(pipe), fb->width, fb->height,
272725c7 8486 fb->format->cpp[0] * 8, base, fb->pitches[0],
bc8d7dff
DL
8487 plane_config->size);
8488
2d14030b 8489 plane_config->fb = intel_fb;
bc8d7dff
DL
8490 return;
8491
8492error:
d1a3a036 8493 kfree(intel_fb);
bc8d7dff
DL
8494}
8495
2fa2fe9a 8496static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 8497 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
8498{
8499 struct drm_device *dev = crtc->base.dev;
fac5e23e 8500 struct drm_i915_private *dev_priv = to_i915(dev);
2fa2fe9a
DV
8501 uint32_t tmp;
8502
8503 tmp = I915_READ(PF_CTL(crtc->pipe));
8504
8505 if (tmp & PF_ENABLE) {
fd4daa9c 8506 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
DV
8507 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
8508 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
DV
8509
8510 /* We currently do not free assignements of panel fitters on
8511 * ivb/hsw (since we don't use the higher upscaling modes which
8512 * differentiates them) so just WARN about this case for now. */
5db94019 8513 if (IS_GEN7(dev_priv)) {
cb8b2a30
DV
8514 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
8515 PF_PIPE_SEL_IVB(crtc->pipe));
8516 }
2fa2fe9a 8517 }
79e53945
JB
8518}
8519
5724dbd1
DL
8520static void
8521ironlake_get_initial_plane_config(struct intel_crtc *crtc,
8522 struct intel_initial_plane_config *plane_config)
4c6baa59
JB
8523{
8524 struct drm_device *dev = crtc->base.dev;
fac5e23e 8525 struct drm_i915_private *dev_priv = to_i915(dev);
4c6baa59 8526 u32 val, base, offset;
aeee5a49 8527 int pipe = crtc->pipe;
4c6baa59 8528 int fourcc, pixel_format;
6761dd31 8529 unsigned int aligned_height;
b113d5ee 8530 struct drm_framebuffer *fb;
1b842c89 8531 struct intel_framebuffer *intel_fb;
4c6baa59 8532
42a7b088
DL
8533 val = I915_READ(DSPCNTR(pipe));
8534 if (!(val & DISPLAY_PLANE_ENABLE))
8535 return;
8536
d9806c9f 8537 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 8538 if (!intel_fb) {
4c6baa59
JB
8539 DRM_DEBUG_KMS("failed to alloc fb\n");
8540 return;
8541 }
8542
1b842c89
DL
8543 fb = &intel_fb->base;
8544
d2e9f5fc
VS
8545 fb->dev = dev;
8546
6315b5d3 8547 if (INTEL_GEN(dev_priv) >= 4) {
18c5247e 8548 if (val & DISPPLANE_TILED) {
49af449b 8549 plane_config->tiling = I915_TILING_X;
bae781b2 8550 fb->modifier = I915_FORMAT_MOD_X_TILED;
18c5247e
DV
8551 }
8552 }
4c6baa59
JB
8553
8554 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 8555 fourcc = i9xx_format_to_fourcc(pixel_format);
2f3f4763 8556 fb->format = drm_format_info(fourcc);
4c6baa59 8557
aeee5a49 8558 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
8652744b 8559 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
aeee5a49 8560 offset = I915_READ(DSPOFFSET(pipe));
4c6baa59 8561 } else {
49af449b 8562 if (plane_config->tiling)
aeee5a49 8563 offset = I915_READ(DSPTILEOFF(pipe));
4c6baa59 8564 else
aeee5a49 8565 offset = I915_READ(DSPLINOFF(pipe));
4c6baa59
JB
8566 }
8567 plane_config->base = base;
8568
8569 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
8570 fb->width = ((val >> 16) & 0xfff) + 1;
8571 fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
8572
8573 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 8574 fb->pitches[0] = val & 0xffffffc0;
4c6baa59 8575
24dbf51a
CW
8576 aligned_height = intel_fb_align_height(dev_priv,
8577 fb->height,
438b74a5 8578 fb->format->format,
bae781b2 8579 fb->modifier);
4c6baa59 8580
f37b5c2b 8581 plane_config->size = fb->pitches[0] * aligned_height;
4c6baa59 8582
2844a921
DL
8583 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8584 pipe_name(pipe), fb->width, fb->height,
272725c7 8585 fb->format->cpp[0] * 8, base, fb->pitches[0],
2844a921 8586 plane_config->size);
b113d5ee 8587
2d14030b 8588 plane_config->fb = intel_fb;
4c6baa59
JB
8589}
8590
0e8ffe1b 8591static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5cec258b 8592 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
8593{
8594 struct drm_device *dev = crtc->base.dev;
fac5e23e 8595 struct drm_i915_private *dev_priv = to_i915(dev);
1729050e 8596 enum intel_display_power_domain power_domain;
0e8ffe1b 8597 uint32_t tmp;
1729050e 8598 bool ret;
0e8ffe1b 8599
1729050e
ID
8600 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8601 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
930e8c9e
PZ
8602 return false;
8603
e143a21c 8604 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 8605 pipe_config->shared_dpll = NULL;
eccb140b 8606
1729050e 8607 ret = false;
0e8ffe1b
DV
8608 tmp = I915_READ(PIPECONF(crtc->pipe));
8609 if (!(tmp & PIPECONF_ENABLE))
1729050e 8610 goto out;
0e8ffe1b 8611
42571aef
VS
8612 switch (tmp & PIPECONF_BPC_MASK) {
8613 case PIPECONF_6BPC:
8614 pipe_config->pipe_bpp = 18;
8615 break;
8616 case PIPECONF_8BPC:
8617 pipe_config->pipe_bpp = 24;
8618 break;
8619 case PIPECONF_10BPC:
8620 pipe_config->pipe_bpp = 30;
8621 break;
8622 case PIPECONF_12BPC:
8623 pipe_config->pipe_bpp = 36;
8624 break;
8625 default:
8626 break;
8627 }
8628
b5a9fa09
DV
8629 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
8630 pipe_config->limited_color_range = true;
8631
ab9412ba 8632 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0 8633 struct intel_shared_dpll *pll;
8106ddbd 8634 enum intel_dpll_id pll_id;
66e985c0 8635
88adfff1
DV
8636 pipe_config->has_pch_encoder = true;
8637
627eb5a3
DV
8638 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
8639 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
8640 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
8641
8642 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 8643
2d1fe073 8644 if (HAS_PCH_IBX(dev_priv)) {
d9a7bc67
ID
8645 /*
8646 * The pipe->pch transcoder and pch transcoder->pll
8647 * mapping is fixed.
8648 */
8106ddbd 8649 pll_id = (enum intel_dpll_id) crtc->pipe;
c0d43d62
DV
8650 } else {
8651 tmp = I915_READ(PCH_DPLL_SEL);
8652 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
8106ddbd 8653 pll_id = DPLL_ID_PCH_PLL_B;
c0d43d62 8654 else
8106ddbd 8655 pll_id= DPLL_ID_PCH_PLL_A;
c0d43d62 8656 }
66e985c0 8657
8106ddbd
ACO
8658 pipe_config->shared_dpll =
8659 intel_get_shared_dpll_by_id(dev_priv, pll_id);
8660 pll = pipe_config->shared_dpll;
66e985c0 8661
2edd6443
ACO
8662 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
8663 &pipe_config->dpll_hw_state));
c93f54cf
DV
8664
8665 tmp = pipe_config->dpll_hw_state.dpll;
8666 pipe_config->pixel_multiplier =
8667 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
8668 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
8669
8670 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
DV
8671 } else {
8672 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
8673 }
8674
1bd1bd80 8675 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 8676 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 8677
2fa2fe9a
DV
8678 ironlake_get_pfit_config(crtc, pipe_config);
8679
1729050e
ID
8680 ret = true;
8681
8682out:
8683 intel_display_power_put(dev_priv, power_domain);
8684
8685 return ret;
0e8ffe1b
DV
8686}
8687
be256dc7
PZ
8688static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
8689{
91c8a326 8690 struct drm_device *dev = &dev_priv->drm;
be256dc7 8691 struct intel_crtc *crtc;
be256dc7 8692
d3fcc808 8693 for_each_intel_crtc(dev, crtc)
e2c719b7 8694 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
8695 pipe_name(crtc->pipe));
8696
e2c719b7
RC
8697 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
8698 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
01403de3
VS
8699 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
8700 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
44cb734c 8701 I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
e2c719b7 8702 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
be256dc7 8703 "CPU PWM1 enabled\n");
772c2a51 8704 if (IS_HASWELL(dev_priv))
e2c719b7 8705 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
c5107b87 8706 "CPU PWM2 enabled\n");
e2c719b7 8707 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
be256dc7 8708 "PCH PWM1 enabled\n");
e2c719b7 8709 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
be256dc7 8710 "Utility pin enabled\n");
e2c719b7 8711 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
be256dc7 8712
9926ada1
PZ
8713 /*
8714 * In theory we can still leave IRQs enabled, as long as only the HPD
8715 * interrupts remain enabled. We used to check for that, but since it's
8716 * gen-specific and since we only disable LCPLL after we fully disable
8717 * the interrupts, the check below should be enough.
8718 */
e2c719b7 8719 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
be256dc7
PZ
8720}
8721
9ccd5aeb
PZ
8722static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
8723{
772c2a51 8724 if (IS_HASWELL(dev_priv))
9ccd5aeb
PZ
8725 return I915_READ(D_COMP_HSW);
8726 else
8727 return I915_READ(D_COMP_BDW);
8728}
8729
3c4c9b81
PZ
8730static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
8731{
772c2a51 8732 if (IS_HASWELL(dev_priv)) {
3c4c9b81
PZ
8733 mutex_lock(&dev_priv->rps.hw_lock);
8734 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
8735 val))
79cf219a 8736 DRM_DEBUG_KMS("Failed to write to D_COMP\n");
3c4c9b81
PZ
8737 mutex_unlock(&dev_priv->rps.hw_lock);
8738 } else {
9ccd5aeb
PZ
8739 I915_WRITE(D_COMP_BDW, val);
8740 POSTING_READ(D_COMP_BDW);
3c4c9b81 8741 }
be256dc7
PZ
8742}
8743
8744/*
8745 * This function implements pieces of two sequences from BSpec:
8746 * - Sequence for display software to disable LCPLL
8747 * - Sequence for display software to allow package C8+
8748 * The steps implemented here are just the steps that actually touch the LCPLL
8749 * register. Callers should take care of disabling all the display engine
8750 * functions, doing the mode unset, fixing interrupts, etc.
8751 */
6ff58d53
PZ
8752static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
8753 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
8754{
8755 uint32_t val;
8756
8757 assert_can_disable_lcpll(dev_priv);
8758
8759 val = I915_READ(LCPLL_CTL);
8760
8761 if (switch_to_fclk) {
8762 val |= LCPLL_CD_SOURCE_FCLK;
8763 I915_WRITE(LCPLL_CTL, val);
8764
f53dd63f
ID
8765 if (wait_for_us(I915_READ(LCPLL_CTL) &
8766 LCPLL_CD_SOURCE_FCLK_DONE, 1))
be256dc7
PZ
8767 DRM_ERROR("Switching to FCLK failed\n");
8768
8769 val = I915_READ(LCPLL_CTL);
8770 }
8771
8772 val |= LCPLL_PLL_DISABLE;
8773 I915_WRITE(LCPLL_CTL, val);
8774 POSTING_READ(LCPLL_CTL);
8775
24d8441d 8776 if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
be256dc7
PZ
8777 DRM_ERROR("LCPLL still locked\n");
8778
9ccd5aeb 8779 val = hsw_read_dcomp(dev_priv);
be256dc7 8780 val |= D_COMP_COMP_DISABLE;
3c4c9b81 8781 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
8782 ndelay(100);
8783
9ccd5aeb
PZ
8784 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
8785 1))
be256dc7
PZ
8786 DRM_ERROR("D_COMP RCOMP still in progress\n");
8787
8788 if (allow_power_down) {
8789 val = I915_READ(LCPLL_CTL);
8790 val |= LCPLL_POWER_DOWN_ALLOW;
8791 I915_WRITE(LCPLL_CTL, val);
8792 POSTING_READ(LCPLL_CTL);
8793 }
8794}
8795
8796/*
8797 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
8798 * source.
8799 */
6ff58d53 8800static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
8801{
8802 uint32_t val;
8803
8804 val = I915_READ(LCPLL_CTL);
8805
8806 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
8807 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
8808 return;
8809
a8a8bd54
PZ
8810 /*
8811 * Make sure we're not on PC8 state before disabling PC8, otherwise
8812 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
a8a8bd54 8813 */
59bad947 8814 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
215733fa 8815
be256dc7
PZ
8816 if (val & LCPLL_POWER_DOWN_ALLOW) {
8817 val &= ~LCPLL_POWER_DOWN_ALLOW;
8818 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 8819 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
8820 }
8821
9ccd5aeb 8822 val = hsw_read_dcomp(dev_priv);
be256dc7
PZ
8823 val |= D_COMP_COMP_FORCE;
8824 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 8825 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
8826
8827 val = I915_READ(LCPLL_CTL);
8828 val &= ~LCPLL_PLL_DISABLE;
8829 I915_WRITE(LCPLL_CTL, val);
8830
93220c08
CW
8831 if (intel_wait_for_register(dev_priv,
8832 LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
8833 5))
be256dc7
PZ
8834 DRM_ERROR("LCPLL not locked yet\n");
8835
8836 if (val & LCPLL_CD_SOURCE_FCLK) {
8837 val = I915_READ(LCPLL_CTL);
8838 val &= ~LCPLL_CD_SOURCE_FCLK;
8839 I915_WRITE(LCPLL_CTL, val);
8840
f53dd63f
ID
8841 if (wait_for_us((I915_READ(LCPLL_CTL) &
8842 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
be256dc7
PZ
8843 DRM_ERROR("Switching back to LCPLL failed\n");
8844 }
215733fa 8845
59bad947 8846 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4c75b940 8847 intel_update_cdclk(dev_priv);
be256dc7
PZ
8848}
8849
765dab67
PZ
8850/*
8851 * Package states C8 and deeper are really deep PC states that can only be
8852 * reached when all the devices on the system allow it, so even if the graphics
8853 * device allows PC8+, it doesn't mean the system will actually get to these
8854 * states. Our driver only allows PC8+ when going into runtime PM.
8855 *
8856 * The requirements for PC8+ are that all the outputs are disabled, the power
8857 * well is disabled and most interrupts are disabled, and these are also
8858 * requirements for runtime PM. When these conditions are met, we manually do
8859 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
8860 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
8861 * hang the machine.
8862 *
8863 * When we really reach PC8 or deeper states (not just when we allow it) we lose
8864 * the state of some registers, so when we come back from PC8+ we need to
8865 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
8866 * need to take care of the registers kept by RC6. Notice that this happens even
8867 * if we don't put the device in PCI D3 state (which is what currently happens
8868 * because of the runtime PM support).
8869 *
8870 * For more, read "Display Sequences for Package C8" on the hardware
8871 * documentation.
8872 */
a14cb6fc 8873void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 8874{
c67a470b
PZ
8875 uint32_t val;
8876
c67a470b
PZ
8877 DRM_DEBUG_KMS("Enabling package C8+\n");
8878
4f8036a2 8879 if (HAS_PCH_LPT_LP(dev_priv)) {
c67a470b
PZ
8880 val = I915_READ(SOUTH_DSPCLK_GATE_D);
8881 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
8882 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
8883 }
8884
c39055b0 8885 lpt_disable_clkout_dp(dev_priv);
c67a470b
PZ
8886 hsw_disable_lcpll(dev_priv, true, true);
8887}
8888
a14cb6fc 8889void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b 8890{
c67a470b
PZ
8891 uint32_t val;
8892
c67a470b
PZ
8893 DRM_DEBUG_KMS("Disabling package C8+\n");
8894
8895 hsw_restore_lcpll(dev_priv);
c39055b0 8896 lpt_init_pch_refclk(dev_priv);
c67a470b 8897
4f8036a2 8898 if (HAS_PCH_LPT_LP(dev_priv)) {
c67a470b
PZ
8899 val = I915_READ(SOUTH_DSPCLK_GATE_D);
8900 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
8901 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
8902 }
c67a470b
PZ
8903}
8904
190f68c5
ACO
8905static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
8906 struct intel_crtc_state *crtc_state)
09b4ddf9 8907{
d7edc4e5 8908 if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
af3997b5
MK
8909 if (!intel_ddi_pll_select(crtc, crtc_state))
8910 return -EINVAL;
8911 }
716c2e55 8912
c7653199 8913 crtc->lowfreq_avail = false;
644cef34 8914
c8f7a0db 8915 return 0;
79e53945
JB
8916}
8917
3760b59c
S
8918static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
8919 enum port port,
8920 struct intel_crtc_state *pipe_config)
8921{
8106ddbd
ACO
8922 enum intel_dpll_id id;
8923
3760b59c
S
8924 switch (port) {
8925 case PORT_A:
08250c4b 8926 id = DPLL_ID_SKL_DPLL0;
3760b59c
S
8927 break;
8928 case PORT_B:
08250c4b 8929 id = DPLL_ID_SKL_DPLL1;
3760b59c
S
8930 break;
8931 case PORT_C:
08250c4b 8932 id = DPLL_ID_SKL_DPLL2;
3760b59c
S
8933 break;
8934 default:
8935 DRM_ERROR("Incorrect port type\n");
8106ddbd 8936 return;
3760b59c 8937 }
8106ddbd
ACO
8938
8939 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
3760b59c
S
8940}
8941
96b7dfb7
S
8942static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
8943 enum port port,
5cec258b 8944 struct intel_crtc_state *pipe_config)
96b7dfb7 8945{
8106ddbd 8946 enum intel_dpll_id id;
a3c988ea 8947 u32 temp;
96b7dfb7
S
8948
8949 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
c856052a 8950 id = temp >> (port * 3 + 1);
96b7dfb7 8951
c856052a 8952 if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL3))
8106ddbd 8953 return;
8106ddbd
ACO
8954
8955 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
96b7dfb7
S
8956}
8957
7d2c8175
DL
8958static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
8959 enum port port,
5cec258b 8960 struct intel_crtc_state *pipe_config)
7d2c8175 8961{
8106ddbd 8962 enum intel_dpll_id id;
c856052a 8963 uint32_t ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
8106ddbd 8964
c856052a 8965 switch (ddi_pll_sel) {
7d2c8175 8966 case PORT_CLK_SEL_WRPLL1:
8106ddbd 8967 id = DPLL_ID_WRPLL1;
7d2c8175
DL
8968 break;
8969 case PORT_CLK_SEL_WRPLL2:
8106ddbd 8970 id = DPLL_ID_WRPLL2;
7d2c8175 8971 break;
00490c22 8972 case PORT_CLK_SEL_SPLL:
8106ddbd 8973 id = DPLL_ID_SPLL;
79bd23da 8974 break;
9d16da65
ACO
8975 case PORT_CLK_SEL_LCPLL_810:
8976 id = DPLL_ID_LCPLL_810;
8977 break;
8978 case PORT_CLK_SEL_LCPLL_1350:
8979 id = DPLL_ID_LCPLL_1350;
8980 break;
8981 case PORT_CLK_SEL_LCPLL_2700:
8982 id = DPLL_ID_LCPLL_2700;
8983 break;
8106ddbd 8984 default:
c856052a 8985 MISSING_CASE(ddi_pll_sel);
8106ddbd
ACO
8986 /* fall through */
8987 case PORT_CLK_SEL_NONE:
8106ddbd 8988 return;
7d2c8175 8989 }
8106ddbd
ACO
8990
8991 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
7d2c8175
DL
8992}
8993
cf30429e
JN
8994static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
8995 struct intel_crtc_state *pipe_config,
d8fc70b7 8996 u64 *power_domain_mask)
cf30429e
JN
8997{
8998 struct drm_device *dev = crtc->base.dev;
fac5e23e 8999 struct drm_i915_private *dev_priv = to_i915(dev);
cf30429e
JN
9000 enum intel_display_power_domain power_domain;
9001 u32 tmp;
9002
d9a7bc67
ID
9003 /*
9004 * The pipe->transcoder mapping is fixed with the exception of the eDP
9005 * transcoder handled below.
9006 */
cf30429e
JN
9007 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
9008
9009 /*
9010 * XXX: Do intel_display_power_get_if_enabled before reading this (for
9011 * consistency and less surprising code; it's in always on power).
9012 */
9013 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9014 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9015 enum pipe trans_edp_pipe;
9016 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9017 default:
9018 WARN(1, "unknown pipe linked to edp transcoder\n");
9019 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9020 case TRANS_DDI_EDP_INPUT_A_ON:
9021 trans_edp_pipe = PIPE_A;
9022 break;
9023 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9024 trans_edp_pipe = PIPE_B;
9025 break;
9026 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9027 trans_edp_pipe = PIPE_C;
9028 break;
9029 }
9030
9031 if (trans_edp_pipe == crtc->pipe)
9032 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9033 }
9034
9035 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
9036 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9037 return false;
d8fc70b7 9038 *power_domain_mask |= BIT_ULL(power_domain);
cf30429e
JN
9039
9040 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
9041
9042 return tmp & PIPECONF_ENABLE;
9043}
9044
4d1de975
JN
9045static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
9046 struct intel_crtc_state *pipe_config,
d8fc70b7 9047 u64 *power_domain_mask)
4d1de975
JN
9048{
9049 struct drm_device *dev = crtc->base.dev;
fac5e23e 9050 struct drm_i915_private *dev_priv = to_i915(dev);
4d1de975
JN
9051 enum intel_display_power_domain power_domain;
9052 enum port port;
9053 enum transcoder cpu_transcoder;
9054 u32 tmp;
9055
4d1de975
JN
9056 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
9057 if (port == PORT_A)
9058 cpu_transcoder = TRANSCODER_DSI_A;
9059 else
9060 cpu_transcoder = TRANSCODER_DSI_C;
9061
9062 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
9063 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9064 continue;
d8fc70b7 9065 *power_domain_mask |= BIT_ULL(power_domain);
4d1de975 9066
db18b6a6
ID
9067 /*
9068 * The PLL needs to be enabled with a valid divider
9069 * configuration, otherwise accessing DSI registers will hang
9070 * the machine. See BSpec North Display Engine
9071 * registers/MIPI[BXT]. We can break out here early, since we
9072 * need the same DSI PLL to be enabled for both DSI ports.
9073 */
9074 if (!intel_dsi_pll_is_enabled(dev_priv))
9075 break;
9076
4d1de975
JN
9077 /* XXX: this works for video mode only */
9078 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
9079 if (!(tmp & DPI_ENABLE))
9080 continue;
9081
9082 tmp = I915_READ(MIPI_CTRL(port));
9083 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
9084 continue;
9085
9086 pipe_config->cpu_transcoder = cpu_transcoder;
4d1de975
JN
9087 break;
9088 }
9089
d7edc4e5 9090 return transcoder_is_dsi(pipe_config->cpu_transcoder);
4d1de975
JN
9091}
9092
26804afd 9093static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
5cec258b 9094 struct intel_crtc_state *pipe_config)
26804afd 9095{
6315b5d3 9096 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
d452c5b6 9097 struct intel_shared_dpll *pll;
26804afd
DV
9098 enum port port;
9099 uint32_t tmp;
9100
9101 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9102
9103 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9104
b976dc53 9105 if (IS_GEN9_BC(dev_priv))
96b7dfb7 9106 skylake_get_ddi_pll(dev_priv, port, pipe_config);
cc3f90f0 9107 else if (IS_GEN9_LP(dev_priv))
3760b59c 9108 bxt_get_ddi_pll(dev_priv, port, pipe_config);
96b7dfb7
S
9109 else
9110 haswell_get_ddi_pll(dev_priv, port, pipe_config);
9cd86933 9111
8106ddbd
ACO
9112 pll = pipe_config->shared_dpll;
9113 if (pll) {
2edd6443
ACO
9114 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9115 &pipe_config->dpll_hw_state));
d452c5b6
DV
9116 }
9117
26804afd
DV
9118 /*
9119 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9120 * DDI E. So just check whether this pipe is wired to DDI E and whether
9121 * the PCH transcoder is on.
9122 */
6315b5d3 9123 if (INTEL_GEN(dev_priv) < 9 &&
ca370455 9124 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
26804afd
DV
9125 pipe_config->has_pch_encoder = true;
9126
9127 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9128 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9129 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9130
9131 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9132 }
9133}
9134
0e8ffe1b 9135static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9136 struct intel_crtc_state *pipe_config)
0e8ffe1b 9137{
6315b5d3 9138 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1729050e 9139 enum intel_display_power_domain power_domain;
d8fc70b7 9140 u64 power_domain_mask;
cf30429e 9141 bool active;
0e8ffe1b 9142
1729050e
ID
9143 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9144 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0 9145 return false;
d8fc70b7 9146 power_domain_mask = BIT_ULL(power_domain);
1729050e 9147
8106ddbd 9148 pipe_config->shared_dpll = NULL;
c0d43d62 9149
cf30429e 9150 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
eccb140b 9151
cc3f90f0 9152 if (IS_GEN9_LP(dev_priv) &&
d7edc4e5
VS
9153 bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
9154 WARN_ON(active);
9155 active = true;
4d1de975
JN
9156 }
9157
cf30429e 9158 if (!active)
1729050e 9159 goto out;
0e8ffe1b 9160
d7edc4e5 9161 if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
4d1de975
JN
9162 haswell_get_ddi_port_state(crtc, pipe_config);
9163 intel_get_pipe_timings(crtc, pipe_config);
9164 }
627eb5a3 9165
bc58be60 9166 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 9167
05dc698c
LL
9168 pipe_config->gamma_mode =
9169 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
9170
6315b5d3 9171 if (INTEL_GEN(dev_priv) >= 9) {
1c74eeaf 9172 intel_crtc_init_scalers(crtc, pipe_config);
a1b2278e 9173
af99ceda
CK
9174 pipe_config->scaler_state.scaler_id = -1;
9175 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9176 }
9177
1729050e
ID
9178 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
9179 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
d8fc70b7 9180 power_domain_mask |= BIT_ULL(power_domain);
6315b5d3 9181 if (INTEL_GEN(dev_priv) >= 9)
bd2e244f 9182 skylake_get_pfit_config(crtc, pipe_config);
ff6d9f55 9183 else
1c132b44 9184 ironlake_get_pfit_config(crtc, pipe_config);
bd2e244f 9185 }
88adfff1 9186
772c2a51 9187 if (IS_HASWELL(dev_priv))
e59150dc
JB
9188 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9189 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 9190
4d1de975
JN
9191 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
9192 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
ebb69c95
CT
9193 pipe_config->pixel_multiplier =
9194 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9195 } else {
9196 pipe_config->pixel_multiplier = 1;
9197 }
6c49f241 9198
1729050e
ID
9199out:
9200 for_each_power_domain(power_domain, power_domain_mask)
9201 intel_display_power_put(dev_priv, power_domain);
9202
cf30429e 9203 return active;
0e8ffe1b
DV
9204}
9205
55a08b3f
ML
9206static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
9207 const struct intel_plane_state *plane_state)
560b85bb
CW
9208{
9209 struct drm_device *dev = crtc->dev;
fac5e23e 9210 struct drm_i915_private *dev_priv = to_i915(dev);
560b85bb 9211 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
dc41c154 9212 uint32_t cntl = 0, size = 0;
560b85bb 9213
936e71e3 9214 if (plane_state && plane_state->base.visible) {
55a08b3f
ML
9215 unsigned int width = plane_state->base.crtc_w;
9216 unsigned int height = plane_state->base.crtc_h;
dc41c154
VS
9217 unsigned int stride = roundup_pow_of_two(width) * 4;
9218
9219 switch (stride) {
9220 default:
9221 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
9222 width, stride);
9223 stride = 256;
9224 /* fallthrough */
9225 case 256:
9226 case 512:
9227 case 1024:
9228 case 2048:
9229 break;
4b0e333e
CW
9230 }
9231
dc41c154
VS
9232 cntl |= CURSOR_ENABLE |
9233 CURSOR_GAMMA_ENABLE |
9234 CURSOR_FORMAT_ARGB |
9235 CURSOR_STRIDE(stride);
9236
9237 size = (height << 12) | width;
4b0e333e 9238 }
560b85bb 9239
dc41c154
VS
9240 if (intel_crtc->cursor_cntl != 0 &&
9241 (intel_crtc->cursor_base != base ||
9242 intel_crtc->cursor_size != size ||
9243 intel_crtc->cursor_cntl != cntl)) {
9244 /* On these chipsets we can only modify the base/size/stride
9245 * whilst the cursor is disabled.
9246 */
0b87c24e
VS
9247 I915_WRITE(CURCNTR(PIPE_A), 0);
9248 POSTING_READ(CURCNTR(PIPE_A));
dc41c154 9249 intel_crtc->cursor_cntl = 0;
4b0e333e 9250 }
560b85bb 9251
99d1f387 9252 if (intel_crtc->cursor_base != base) {
0b87c24e 9253 I915_WRITE(CURBASE(PIPE_A), base);
99d1f387
VS
9254 intel_crtc->cursor_base = base;
9255 }
4726e0b0 9256
dc41c154
VS
9257 if (intel_crtc->cursor_size != size) {
9258 I915_WRITE(CURSIZE, size);
9259 intel_crtc->cursor_size = size;
4b0e333e 9260 }
560b85bb 9261
4b0e333e 9262 if (intel_crtc->cursor_cntl != cntl) {
0b87c24e
VS
9263 I915_WRITE(CURCNTR(PIPE_A), cntl);
9264 POSTING_READ(CURCNTR(PIPE_A));
4b0e333e 9265 intel_crtc->cursor_cntl = cntl;
560b85bb 9266 }
560b85bb
CW
9267}
9268
55a08b3f
ML
9269static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
9270 const struct intel_plane_state *plane_state)
65a21cd6
JB
9271{
9272 struct drm_device *dev = crtc->dev;
fac5e23e 9273 struct drm_i915_private *dev_priv = to_i915(dev);
65a21cd6
JB
9274 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9275 int pipe = intel_crtc->pipe;
663f3122 9276 uint32_t cntl = 0;
4b0e333e 9277
936e71e3 9278 if (plane_state && plane_state->base.visible) {
4b0e333e 9279 cntl = MCURSOR_GAMMA_ENABLE;
55a08b3f 9280 switch (plane_state->base.crtc_w) {
4726e0b0
SK
9281 case 64:
9282 cntl |= CURSOR_MODE_64_ARGB_AX;
9283 break;
9284 case 128:
9285 cntl |= CURSOR_MODE_128_ARGB_AX;
9286 break;
9287 case 256:
9288 cntl |= CURSOR_MODE_256_ARGB_AX;
9289 break;
9290 default:
55a08b3f 9291 MISSING_CASE(plane_state->base.crtc_w);
4726e0b0 9292 return;
65a21cd6 9293 }
4b0e333e 9294 cntl |= pipe << 28; /* Connect to correct pipe */
47bf17a7 9295
4f8036a2 9296 if (HAS_DDI(dev_priv))
47bf17a7 9297 cntl |= CURSOR_PIPE_CSC_ENABLE;
65a21cd6 9298
f22aa143 9299 if (plane_state->base.rotation & DRM_ROTATE_180)
55a08b3f
ML
9300 cntl |= CURSOR_ROTATE_180;
9301 }
4398ad45 9302
4b0e333e
CW
9303 if (intel_crtc->cursor_cntl != cntl) {
9304 I915_WRITE(CURCNTR(pipe), cntl);
9305 POSTING_READ(CURCNTR(pipe));
9306 intel_crtc->cursor_cntl = cntl;
65a21cd6 9307 }
4b0e333e 9308
65a21cd6 9309 /* and commit changes on next vblank */
5efb3e28
VS
9310 I915_WRITE(CURBASE(pipe), base);
9311 POSTING_READ(CURBASE(pipe));
99d1f387
VS
9312
9313 intel_crtc->cursor_base = base;
65a21cd6
JB
9314}
9315
cda4b7d3 9316/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6b383a7f 9317static void intel_crtc_update_cursor(struct drm_crtc *crtc,
55a08b3f 9318 const struct intel_plane_state *plane_state)
cda4b7d3
CW
9319{
9320 struct drm_device *dev = crtc->dev;
fac5e23e 9321 struct drm_i915_private *dev_priv = to_i915(dev);
cda4b7d3
CW
9322 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9323 int pipe = intel_crtc->pipe;
55a08b3f
ML
9324 u32 base = intel_crtc->cursor_addr;
9325 u32 pos = 0;
cda4b7d3 9326
55a08b3f
ML
9327 if (plane_state) {
9328 int x = plane_state->base.crtc_x;
9329 int y = plane_state->base.crtc_y;
cda4b7d3 9330
55a08b3f
ML
9331 if (x < 0) {
9332 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
9333 x = -x;
9334 }
9335 pos |= x << CURSOR_X_SHIFT;
cda4b7d3 9336
55a08b3f
ML
9337 if (y < 0) {
9338 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
9339 y = -y;
9340 }
9341 pos |= y << CURSOR_Y_SHIFT;
9342
9343 /* ILK+ do this automagically */
49cff963 9344 if (HAS_GMCH_DISPLAY(dev_priv) &&
f22aa143 9345 plane_state->base.rotation & DRM_ROTATE_180) {
55a08b3f
ML
9346 base += (plane_state->base.crtc_h *
9347 plane_state->base.crtc_w - 1) * 4;
9348 }
cda4b7d3 9349 }
cda4b7d3 9350
5efb3e28
VS
9351 I915_WRITE(CURPOS(pipe), pos);
9352
2a307c2e 9353 if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
55a08b3f 9354 i845_update_cursor(crtc, base, plane_state);
5efb3e28 9355 else
55a08b3f 9356 i9xx_update_cursor(crtc, base, plane_state);
cda4b7d3
CW
9357}
9358
50a0bc90 9359static bool cursor_size_ok(struct drm_i915_private *dev_priv,
dc41c154
VS
9360 uint32_t width, uint32_t height)
9361{
9362 if (width == 0 || height == 0)
9363 return false;
9364
9365 /*
9366 * 845g/865g are special in that they are only limited by
9367 * the width of their cursors, the height is arbitrary up to
9368 * the precision of the register. Everything else requires
9369 * square cursors, limited to a few power-of-two sizes.
9370 */
2a307c2e 9371 if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
dc41c154
VS
9372 if ((width & 63) != 0)
9373 return false;
9374
2a307c2e 9375 if (width > (IS_I845G(dev_priv) ? 64 : 512))
dc41c154
VS
9376 return false;
9377
9378 if (height > 1023)
9379 return false;
9380 } else {
9381 switch (width | height) {
9382 case 256:
9383 case 128:
50a0bc90 9384 if (IS_GEN2(dev_priv))
dc41c154
VS
9385 return false;
9386 case 64:
9387 break;
9388 default:
9389 return false;
9390 }
9391 }
9392
9393 return true;
9394}
9395
79e53945
JB
9396/* VESA 640x480x72Hz mode to set on the pipe */
9397static struct drm_display_mode load_detect_mode = {
9398 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
9399 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
9400};
9401
a8bb6818 9402struct drm_framebuffer *
24dbf51a
CW
9403intel_framebuffer_create(struct drm_i915_gem_object *obj,
9404 struct drm_mode_fb_cmd2 *mode_cmd)
d2dff872
CW
9405{
9406 struct intel_framebuffer *intel_fb;
9407 int ret;
9408
9409 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
dcb1394e 9410 if (!intel_fb)
d2dff872 9411 return ERR_PTR(-ENOMEM);
d2dff872 9412
24dbf51a 9413 ret = intel_framebuffer_init(intel_fb, obj, mode_cmd);
dd4916c5
DV
9414 if (ret)
9415 goto err;
d2dff872
CW
9416
9417 return &intel_fb->base;
dcb1394e 9418
dd4916c5 9419err:
dd4916c5 9420 kfree(intel_fb);
dd4916c5 9421 return ERR_PTR(ret);
d2dff872
CW
9422}
9423
9424static u32
9425intel_framebuffer_pitch_for_width(int width, int bpp)
9426{
9427 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
9428 return ALIGN(pitch, 64);
9429}
9430
9431static u32
9432intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
9433{
9434 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
1267a26b 9435 return PAGE_ALIGN(pitch * mode->vdisplay);
d2dff872
CW
9436}
9437
9438static struct drm_framebuffer *
9439intel_framebuffer_create_for_mode(struct drm_device *dev,
9440 struct drm_display_mode *mode,
9441 int depth, int bpp)
9442{
dcb1394e 9443 struct drm_framebuffer *fb;
d2dff872 9444 struct drm_i915_gem_object *obj;
0fed39bd 9445 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872 9446
12d79d78 9447 obj = i915_gem_object_create(to_i915(dev),
d2dff872 9448 intel_framebuffer_size_for_mode(mode, bpp));
fe3db79b
CW
9449 if (IS_ERR(obj))
9450 return ERR_CAST(obj);
d2dff872
CW
9451
9452 mode_cmd.width = mode->hdisplay;
9453 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
9454 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
9455 bpp);
5ca0c34a 9456 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872 9457
24dbf51a 9458 fb = intel_framebuffer_create(obj, &mode_cmd);
dcb1394e 9459 if (IS_ERR(fb))
f0cd5182 9460 i915_gem_object_put(obj);
dcb1394e
LW
9461
9462 return fb;
d2dff872
CW
9463}
9464
9465static struct drm_framebuffer *
9466mode_fits_in_fbdev(struct drm_device *dev,
9467 struct drm_display_mode *mode)
9468{
0695726e 9469#ifdef CONFIG_DRM_FBDEV_EMULATION
fac5e23e 9470 struct drm_i915_private *dev_priv = to_i915(dev);
d2dff872
CW
9471 struct drm_i915_gem_object *obj;
9472 struct drm_framebuffer *fb;
9473
4c0e5528 9474 if (!dev_priv->fbdev)
d2dff872
CW
9475 return NULL;
9476
4c0e5528 9477 if (!dev_priv->fbdev->fb)
d2dff872
CW
9478 return NULL;
9479
4c0e5528
DV
9480 obj = dev_priv->fbdev->fb->obj;
9481 BUG_ON(!obj);
9482
8bcd4553 9483 fb = &dev_priv->fbdev->fb->base;
01f2c773 9484 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
272725c7 9485 fb->format->cpp[0] * 8))
d2dff872
CW
9486 return NULL;
9487
01f2c773 9488 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
9489 return NULL;
9490
edde3617 9491 drm_framebuffer_reference(fb);
d2dff872 9492 return fb;
4520f53a
DV
9493#else
9494 return NULL;
9495#endif
d2dff872
CW
9496}
9497
d3a40d1b
ACO
9498static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
9499 struct drm_crtc *crtc,
9500 struct drm_display_mode *mode,
9501 struct drm_framebuffer *fb,
9502 int x, int y)
9503{
9504 struct drm_plane_state *plane_state;
9505 int hdisplay, vdisplay;
9506 int ret;
9507
9508 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
9509 if (IS_ERR(plane_state))
9510 return PTR_ERR(plane_state);
9511
9512 if (mode)
196cd5d3 9513 drm_mode_get_hv_timing(mode, &hdisplay, &vdisplay);
d3a40d1b
ACO
9514 else
9515 hdisplay = vdisplay = 0;
9516
9517 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
9518 if (ret)
9519 return ret;
9520 drm_atomic_set_fb_for_plane(plane_state, fb);
9521 plane_state->crtc_x = 0;
9522 plane_state->crtc_y = 0;
9523 plane_state->crtc_w = hdisplay;
9524 plane_state->crtc_h = vdisplay;
9525 plane_state->src_x = x << 16;
9526 plane_state->src_y = y << 16;
9527 plane_state->src_w = hdisplay << 16;
9528 plane_state->src_h = vdisplay << 16;
9529
9530 return 0;
9531}
9532
d2434ab7 9533bool intel_get_load_detect_pipe(struct drm_connector *connector,
7173188d 9534 struct drm_display_mode *mode,
51fd371b
RC
9535 struct intel_load_detect_pipe *old,
9536 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
9537{
9538 struct intel_crtc *intel_crtc;
d2434ab7
DV
9539 struct intel_encoder *intel_encoder =
9540 intel_attached_encoder(connector);
79e53945 9541 struct drm_crtc *possible_crtc;
4ef69c7a 9542 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
9543 struct drm_crtc *crtc = NULL;
9544 struct drm_device *dev = encoder->dev;
0f0f74bc 9545 struct drm_i915_private *dev_priv = to_i915(dev);
94352cf9 9546 struct drm_framebuffer *fb;
51fd371b 9547 struct drm_mode_config *config = &dev->mode_config;
edde3617 9548 struct drm_atomic_state *state = NULL, *restore_state = NULL;
944b0c76 9549 struct drm_connector_state *connector_state;
4be07317 9550 struct intel_crtc_state *crtc_state;
51fd371b 9551 int ret, i = -1;
79e53945 9552
d2dff872 9553 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 9554 connector->base.id, connector->name,
8e329a03 9555 encoder->base.id, encoder->name);
d2dff872 9556
edde3617
ML
9557 old->restore_state = NULL;
9558
51fd371b
RC
9559retry:
9560 ret = drm_modeset_lock(&config->connection_mutex, ctx);
9561 if (ret)
ad3c558f 9562 goto fail;
6e9f798d 9563
79e53945
JB
9564 /*
9565 * Algorithm gets a little messy:
7a5e4805 9566 *
79e53945
JB
9567 * - if the connector already has an assigned crtc, use it (but make
9568 * sure it's on first)
7a5e4805 9569 *
79e53945
JB
9570 * - try to find the first unused crtc that can drive this connector,
9571 * and use that if we find one
79e53945
JB
9572 */
9573
9574 /* See if we already have a CRTC for this connector */
edde3617
ML
9575 if (connector->state->crtc) {
9576 crtc = connector->state->crtc;
8261b191 9577
51fd371b 9578 ret = drm_modeset_lock(&crtc->mutex, ctx);
4d02e2de 9579 if (ret)
ad3c558f 9580 goto fail;
8261b191
CW
9581
9582 /* Make sure the crtc and connector are running */
edde3617 9583 goto found;
79e53945
JB
9584 }
9585
9586 /* Find an unused one (if possible) */
70e1e0ec 9587 for_each_crtc(dev, possible_crtc) {
79e53945
JB
9588 i++;
9589 if (!(encoder->possible_crtcs & (1 << i)))
9590 continue;
edde3617
ML
9591
9592 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
9593 if (ret)
9594 goto fail;
9595
9596 if (possible_crtc->state->enable) {
9597 drm_modeset_unlock(&possible_crtc->mutex);
a459249c 9598 continue;
edde3617 9599 }
a459249c
VS
9600
9601 crtc = possible_crtc;
9602 break;
79e53945
JB
9603 }
9604
9605 /*
9606 * If we didn't find an unused CRTC, don't use any.
9607 */
9608 if (!crtc) {
7173188d 9609 DRM_DEBUG_KMS("no pipe available for load-detect\n");
ad3c558f 9610 goto fail;
79e53945
JB
9611 }
9612
edde3617
ML
9613found:
9614 intel_crtc = to_intel_crtc(crtc);
9615
4d02e2de
DV
9616 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
9617 if (ret)
ad3c558f 9618 goto fail;
79e53945 9619
83a57153 9620 state = drm_atomic_state_alloc(dev);
edde3617
ML
9621 restore_state = drm_atomic_state_alloc(dev);
9622 if (!state || !restore_state) {
9623 ret = -ENOMEM;
9624 goto fail;
9625 }
83a57153
ACO
9626
9627 state->acquire_ctx = ctx;
edde3617 9628 restore_state->acquire_ctx = ctx;
83a57153 9629
944b0c76
ACO
9630 connector_state = drm_atomic_get_connector_state(state, connector);
9631 if (IS_ERR(connector_state)) {
9632 ret = PTR_ERR(connector_state);
9633 goto fail;
9634 }
9635
edde3617
ML
9636 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
9637 if (ret)
9638 goto fail;
944b0c76 9639
4be07317
ACO
9640 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
9641 if (IS_ERR(crtc_state)) {
9642 ret = PTR_ERR(crtc_state);
9643 goto fail;
9644 }
9645
49d6fa21 9646 crtc_state->base.active = crtc_state->base.enable = true;
4be07317 9647
6492711d
CW
9648 if (!mode)
9649 mode = &load_detect_mode;
79e53945 9650
d2dff872
CW
9651 /* We need a framebuffer large enough to accommodate all accesses
9652 * that the plane may generate whilst we perform load detection.
9653 * We can not rely on the fbcon either being present (we get called
9654 * during its initialisation to detect all boot displays, or it may
9655 * not even exist) or that it is large enough to satisfy the
9656 * requested mode.
9657 */
94352cf9
DV
9658 fb = mode_fits_in_fbdev(dev, mode);
9659 if (fb == NULL) {
d2dff872 9660 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9 9661 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
d2dff872
CW
9662 } else
9663 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 9664 if (IS_ERR(fb)) {
d2dff872 9665 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
412b61d8 9666 goto fail;
79e53945 9667 }
79e53945 9668
d3a40d1b
ACO
9669 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
9670 if (ret)
9671 goto fail;
9672
edde3617
ML
9673 drm_framebuffer_unreference(fb);
9674
9675 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
9676 if (ret)
9677 goto fail;
9678
9679 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
9680 if (!ret)
9681 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
9682 if (!ret)
9683 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
9684 if (ret) {
9685 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
9686 goto fail;
9687 }
8c7b5ccb 9688
3ba86073
ML
9689 ret = drm_atomic_commit(state);
9690 if (ret) {
6492711d 9691 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
412b61d8 9692 goto fail;
79e53945 9693 }
edde3617
ML
9694
9695 old->restore_state = restore_state;
7abbd11f 9696 drm_atomic_state_put(state);
7173188d 9697
79e53945 9698 /* let the connector get through one full cycle before testing */
0f0f74bc 9699 intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
7173188d 9700 return true;
412b61d8 9701
ad3c558f 9702fail:
7fb71c8f
CW
9703 if (state) {
9704 drm_atomic_state_put(state);
9705 state = NULL;
9706 }
9707 if (restore_state) {
9708 drm_atomic_state_put(restore_state);
9709 restore_state = NULL;
9710 }
83a57153 9711
51fd371b
RC
9712 if (ret == -EDEADLK) {
9713 drm_modeset_backoff(ctx);
9714 goto retry;
9715 }
9716
412b61d8 9717 return false;
79e53945
JB
9718}
9719
d2434ab7 9720void intel_release_load_detect_pipe(struct drm_connector *connector,
49172fee
ACO
9721 struct intel_load_detect_pipe *old,
9722 struct drm_modeset_acquire_ctx *ctx)
79e53945 9723{
d2434ab7
DV
9724 struct intel_encoder *intel_encoder =
9725 intel_attached_encoder(connector);
4ef69c7a 9726 struct drm_encoder *encoder = &intel_encoder->base;
edde3617 9727 struct drm_atomic_state *state = old->restore_state;
d3a40d1b 9728 int ret;
79e53945 9729
d2dff872 9730 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 9731 connector->base.id, connector->name,
8e329a03 9732 encoder->base.id, encoder->name);
d2dff872 9733
edde3617 9734 if (!state)
0622a53c 9735 return;
79e53945 9736
edde3617 9737 ret = drm_atomic_commit(state);
0853695c 9738 if (ret)
edde3617 9739 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
0853695c 9740 drm_atomic_state_put(state);
79e53945
JB
9741}
9742
da4a1efa 9743static int i9xx_pll_refclk(struct drm_device *dev,
5cec258b 9744 const struct intel_crtc_state *pipe_config)
da4a1efa 9745{
fac5e23e 9746 struct drm_i915_private *dev_priv = to_i915(dev);
da4a1efa
VS
9747 u32 dpll = pipe_config->dpll_hw_state.dpll;
9748
9749 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 9750 return dev_priv->vbt.lvds_ssc_freq;
6e266956 9751 else if (HAS_PCH_SPLIT(dev_priv))
da4a1efa 9752 return 120000;
5db94019 9753 else if (!IS_GEN2(dev_priv))
da4a1efa
VS
9754 return 96000;
9755 else
9756 return 48000;
9757}
9758
79e53945 9759/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc 9760static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 9761 struct intel_crtc_state *pipe_config)
79e53945 9762{
f1f644dc 9763 struct drm_device *dev = crtc->base.dev;
fac5e23e 9764 struct drm_i915_private *dev_priv = to_i915(dev);
f1f644dc 9765 int pipe = pipe_config->cpu_transcoder;
293623f7 9766 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945 9767 u32 fp;
9e2c8475 9768 struct dpll clock;
dccbea3b 9769 int port_clock;
da4a1efa 9770 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
9771
9772 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 9773 fp = pipe_config->dpll_hw_state.fp0;
79e53945 9774 else
293623f7 9775 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
9776
9777 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
9b1e14f4 9778 if (IS_PINEVIEW(dev_priv)) {
f2b115e6
AJ
9779 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
9780 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
9781 } else {
9782 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
9783 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
9784 }
9785
5db94019 9786 if (!IS_GEN2(dev_priv)) {
9b1e14f4 9787 if (IS_PINEVIEW(dev_priv))
f2b115e6
AJ
9788 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
9789 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
9790 else
9791 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
9792 DPLL_FPA01_P1_POST_DIV_SHIFT);
9793
9794 switch (dpll & DPLL_MODE_MASK) {
9795 case DPLLB_MODE_DAC_SERIAL:
9796 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
9797 5 : 10;
9798 break;
9799 case DPLLB_MODE_LVDS:
9800 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
9801 7 : 14;
9802 break;
9803 default:
28c97730 9804 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 9805 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 9806 return;
79e53945
JB
9807 }
9808
9b1e14f4 9809 if (IS_PINEVIEW(dev_priv))
dccbea3b 9810 port_clock = pnv_calc_dpll_params(refclk, &clock);
ac58c3f0 9811 else
dccbea3b 9812 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945 9813 } else {
50a0bc90 9814 u32 lvds = IS_I830(dev_priv) ? 0 : I915_READ(LVDS);
b1c560d1 9815 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
9816
9817 if (is_lvds) {
9818 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
9819 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
9820
9821 if (lvds & LVDS_CLKB_POWER_UP)
9822 clock.p2 = 7;
9823 else
9824 clock.p2 = 14;
79e53945
JB
9825 } else {
9826 if (dpll & PLL_P1_DIVIDE_BY_TWO)
9827 clock.p1 = 2;
9828 else {
9829 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
9830 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
9831 }
9832 if (dpll & PLL_P2_DIVIDE_BY_4)
9833 clock.p2 = 4;
9834 else
9835 clock.p2 = 2;
79e53945 9836 }
da4a1efa 9837
dccbea3b 9838 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945
JB
9839 }
9840
18442d08
VS
9841 /*
9842 * This value includes pixel_multiplier. We will use
241bfc38 9843 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
9844 * encoder's get_config() function.
9845 */
dccbea3b 9846 pipe_config->port_clock = port_clock;
f1f644dc
JB
9847}
9848
6878da05
VS
9849int intel_dotclock_calculate(int link_freq,
9850 const struct intel_link_m_n *m_n)
f1f644dc 9851{
f1f644dc
JB
9852 /*
9853 * The calculation for the data clock is:
1041a02f 9854 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 9855 * But we want to avoid losing precison if possible, so:
1041a02f 9856 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
9857 *
9858 * and the link clock is simpler:
1041a02f 9859 * link_clock = (m * link_clock) / n
f1f644dc
JB
9860 */
9861
6878da05
VS
9862 if (!m_n->link_n)
9863 return 0;
f1f644dc 9864
6878da05
VS
9865 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
9866}
f1f644dc 9867
18442d08 9868static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 9869 struct intel_crtc_state *pipe_config)
6878da05 9870{
e3b247da 9871 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
79e53945 9872
18442d08
VS
9873 /* read out port_clock from the DPLL */
9874 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 9875
f1f644dc 9876 /*
e3b247da
VS
9877 * In case there is an active pipe without active ports,
9878 * we may need some idea for the dotclock anyway.
9879 * Calculate one based on the FDI configuration.
79e53945 9880 */
2d112de7 9881 pipe_config->base.adjusted_mode.crtc_clock =
21a727b3 9882 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
18442d08 9883 &pipe_config->fdi_m_n);
79e53945
JB
9884}
9885
9886/** Returns the currently programmed mode of the given pipe. */
9887struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
9888 struct drm_crtc *crtc)
9889{
fac5e23e 9890 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 9891 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 9892 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
79e53945 9893 struct drm_display_mode *mode;
3f36b937 9894 struct intel_crtc_state *pipe_config;
fe2b8f9d
PZ
9895 int htot = I915_READ(HTOTAL(cpu_transcoder));
9896 int hsync = I915_READ(HSYNC(cpu_transcoder));
9897 int vtot = I915_READ(VTOTAL(cpu_transcoder));
9898 int vsync = I915_READ(VSYNC(cpu_transcoder));
293623f7 9899 enum pipe pipe = intel_crtc->pipe;
79e53945
JB
9900
9901 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
9902 if (!mode)
9903 return NULL;
9904
3f36b937
TU
9905 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
9906 if (!pipe_config) {
9907 kfree(mode);
9908 return NULL;
9909 }
9910
f1f644dc
JB
9911 /*
9912 * Construct a pipe_config sufficient for getting the clock info
9913 * back out of crtc_clock_get.
9914 *
9915 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
9916 * to use a real value here instead.
9917 */
3f36b937
TU
9918 pipe_config->cpu_transcoder = (enum transcoder) pipe;
9919 pipe_config->pixel_multiplier = 1;
9920 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
9921 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
9922 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
9923 i9xx_crtc_clock_get(intel_crtc, pipe_config);
9924
9925 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
79e53945
JB
9926 mode->hdisplay = (htot & 0xffff) + 1;
9927 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
9928 mode->hsync_start = (hsync & 0xffff) + 1;
9929 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
9930 mode->vdisplay = (vtot & 0xffff) + 1;
9931 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
9932 mode->vsync_start = (vsync & 0xffff) + 1;
9933 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
9934
9935 drm_mode_set_name(mode);
79e53945 9936
3f36b937
TU
9937 kfree(pipe_config);
9938
79e53945
JB
9939 return mode;
9940}
9941
9942static void intel_crtc_destroy(struct drm_crtc *crtc)
9943{
9944 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
67e77c5a 9945 struct drm_device *dev = crtc->dev;
51cbaf01 9946 struct intel_flip_work *work;
67e77c5a 9947
5e2d7afc 9948 spin_lock_irq(&dev->event_lock);
5a21b665
DV
9949 work = intel_crtc->flip_work;
9950 intel_crtc->flip_work = NULL;
9951 spin_unlock_irq(&dev->event_lock);
67e77c5a 9952
5a21b665 9953 if (work) {
51cbaf01
ML
9954 cancel_work_sync(&work->mmio_work);
9955 cancel_work_sync(&work->unpin_work);
5a21b665 9956 kfree(work);
67e77c5a 9957 }
79e53945
JB
9958
9959 drm_crtc_cleanup(crtc);
67e77c5a 9960
79e53945
JB
9961 kfree(intel_crtc);
9962}
9963
6b95a207
KH
9964static void intel_unpin_work_fn(struct work_struct *__work)
9965{
51cbaf01
ML
9966 struct intel_flip_work *work =
9967 container_of(__work, struct intel_flip_work, unpin_work);
5a21b665
DV
9968 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
9969 struct drm_device *dev = crtc->base.dev;
9970 struct drm_plane *primary = crtc->base.primary;
03f476e1 9971
5a21b665
DV
9972 if (is_mmio_work(work))
9973 flush_work(&work->mmio_work);
03f476e1 9974
5a21b665 9975 mutex_lock(&dev->struct_mutex);
be1e3415 9976 intel_unpin_fb_vma(work->old_vma);
f8c417cd 9977 i915_gem_object_put(work->pending_flip_obj);
5a21b665 9978 mutex_unlock(&dev->struct_mutex);
143f73b3 9979
e8a261ea
CW
9980 i915_gem_request_put(work->flip_queued_req);
9981
5748b6a1
CW
9982 intel_frontbuffer_flip_complete(to_i915(dev),
9983 to_intel_plane(primary)->frontbuffer_bit);
5a21b665
DV
9984 intel_fbc_post_update(crtc);
9985 drm_framebuffer_unreference(work->old_fb);
143f73b3 9986
5a21b665
DV
9987 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
9988 atomic_dec(&crtc->unpin_work_count);
a6747b73 9989
5a21b665
DV
9990 kfree(work);
9991}
d9e86c0e 9992
5a21b665
DV
9993/* Is 'a' after or equal to 'b'? */
9994static bool g4x_flip_count_after_eq(u32 a, u32 b)
9995{
9996 return !((a - b) & 0x80000000);
9997}
143f73b3 9998
5a21b665
DV
9999static bool __pageflip_finished_cs(struct intel_crtc *crtc,
10000 struct intel_flip_work *work)
10001{
10002 struct drm_device *dev = crtc->base.dev;
fac5e23e 10003 struct drm_i915_private *dev_priv = to_i915(dev);
143f73b3 10004
8af29b0c 10005 if (abort_flip_on_reset(crtc))
5a21b665 10006 return true;
143f73b3 10007
5a21b665
DV
10008 /*
10009 * The relevant registers doen't exist on pre-ctg.
10010 * As the flip done interrupt doesn't trigger for mmio
10011 * flips on gmch platforms, a flip count check isn't
10012 * really needed there. But since ctg has the registers,
10013 * include it in the check anyway.
10014 */
9beb5fea 10015 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
5a21b665 10016 return true;
b4a98e57 10017
5a21b665
DV
10018 /*
10019 * BDW signals flip done immediately if the plane
10020 * is disabled, even if the plane enable is already
10021 * armed to occur at the next vblank :(
10022 */
f99d7069 10023
5a21b665
DV
10024 /*
10025 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10026 * used the same base address. In that case the mmio flip might
10027 * have completed, but the CS hasn't even executed the flip yet.
10028 *
10029 * A flip count check isn't enough as the CS might have updated
10030 * the base address just after start of vblank, but before we
10031 * managed to process the interrupt. This means we'd complete the
10032 * CS flip too soon.
10033 *
10034 * Combining both checks should get us a good enough result. It may
10035 * still happen that the CS flip has been executed, but has not
10036 * yet actually completed. But in case the base address is the same
10037 * anyway, we don't really care.
10038 */
10039 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10040 crtc->flip_work->gtt_offset &&
10041 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
10042 crtc->flip_work->flip_count);
10043}
b4a98e57 10044
5a21b665
DV
10045static bool
10046__pageflip_finished_mmio(struct intel_crtc *crtc,
10047 struct intel_flip_work *work)
10048{
10049 /*
10050 * MMIO work completes when vblank is different from
10051 * flip_queued_vblank.
10052 *
10053 * Reset counter value doesn't matter, this is handled by
10054 * i915_wait_request finishing early, so no need to handle
10055 * reset here.
10056 */
10057 return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
6b95a207
KH
10058}
10059
51cbaf01
ML
10060
10061static bool pageflip_finished(struct intel_crtc *crtc,
10062 struct intel_flip_work *work)
10063{
10064 if (!atomic_read(&work->pending))
10065 return false;
10066
10067 smp_rmb();
10068
5a21b665
DV
10069 if (is_mmio_work(work))
10070 return __pageflip_finished_mmio(crtc, work);
10071 else
10072 return __pageflip_finished_cs(crtc, work);
10073}
10074
10075void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
10076{
91c8a326 10077 struct drm_device *dev = &dev_priv->drm;
98187836 10078 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
5a21b665
DV
10079 struct intel_flip_work *work;
10080 unsigned long flags;
10081
10082 /* Ignore early vblank irqs */
10083 if (!crtc)
10084 return;
10085
51cbaf01 10086 /*
5a21b665
DV
10087 * This is called both by irq handlers and the reset code (to complete
10088 * lost pageflips) so needs the full irqsave spinlocks.
51cbaf01 10089 */
5a21b665 10090 spin_lock_irqsave(&dev->event_lock, flags);
e2af48c6 10091 work = crtc->flip_work;
5a21b665
DV
10092
10093 if (work != NULL &&
10094 !is_mmio_work(work) &&
e2af48c6
VS
10095 pageflip_finished(crtc, work))
10096 page_flip_completed(crtc);
5a21b665
DV
10097
10098 spin_unlock_irqrestore(&dev->event_lock, flags);
75f7f3ec
VS
10099}
10100
51cbaf01 10101void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
6b95a207 10102{
91c8a326 10103 struct drm_device *dev = &dev_priv->drm;
98187836 10104 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
51cbaf01 10105 struct intel_flip_work *work;
6b95a207
KH
10106 unsigned long flags;
10107
5251f04e
ML
10108 /* Ignore early vblank irqs */
10109 if (!crtc)
10110 return;
f326038a
DV
10111
10112 /*
10113 * This is called both by irq handlers and the reset code (to complete
10114 * lost pageflips) so needs the full irqsave spinlocks.
e7d841ca 10115 */
6b95a207 10116 spin_lock_irqsave(&dev->event_lock, flags);
e2af48c6 10117 work = crtc->flip_work;
5251f04e 10118
5a21b665
DV
10119 if (work != NULL &&
10120 is_mmio_work(work) &&
e2af48c6
VS
10121 pageflip_finished(crtc, work))
10122 page_flip_completed(crtc);
5251f04e 10123
6b95a207
KH
10124 spin_unlock_irqrestore(&dev->event_lock, flags);
10125}
10126
5a21b665
DV
10127static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
10128 struct intel_flip_work *work)
84c33a64 10129{
5a21b665 10130 work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
84c33a64 10131
5a21b665
DV
10132 /* Ensure that the work item is consistent when activating it ... */
10133 smp_mb__before_atomic();
10134 atomic_set(&work->pending, 1);
10135}
a6747b73 10136
5a21b665
DV
10137static int intel_gen2_queue_flip(struct drm_device *dev,
10138 struct drm_crtc *crtc,
10139 struct drm_framebuffer *fb,
10140 struct drm_i915_gem_object *obj,
10141 struct drm_i915_gem_request *req,
10142 uint32_t flags)
10143{
5a21b665 10144 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
73dec95e 10145 u32 flip_mask, *cs;
143f73b3 10146
73dec95e
TU
10147 cs = intel_ring_begin(req, 6);
10148 if (IS_ERR(cs))
10149 return PTR_ERR(cs);
143f73b3 10150
5a21b665
DV
10151 /* Can't queue multiple flips, so wait for the previous
10152 * one to finish before executing the next.
10153 */
10154 if (intel_crtc->plane)
10155 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10156 else
10157 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
73dec95e
TU
10158 *cs++ = MI_WAIT_FOR_EVENT | flip_mask;
10159 *cs++ = MI_NOOP;
10160 *cs++ = MI_DISPLAY_FLIP | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
10161 *cs++ = fb->pitches[0];
10162 *cs++ = intel_crtc->flip_work->gtt_offset;
10163 *cs++ = 0; /* aux display base address, unused */
143f73b3 10164
5a21b665
DV
10165 return 0;
10166}
84c33a64 10167
5a21b665
DV
10168static int intel_gen3_queue_flip(struct drm_device *dev,
10169 struct drm_crtc *crtc,
10170 struct drm_framebuffer *fb,
10171 struct drm_i915_gem_object *obj,
10172 struct drm_i915_gem_request *req,
10173 uint32_t flags)
10174{
5a21b665 10175 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
73dec95e 10176 u32 flip_mask, *cs;
d55dbd06 10177
73dec95e
TU
10178 cs = intel_ring_begin(req, 6);
10179 if (IS_ERR(cs))
10180 return PTR_ERR(cs);
d55dbd06 10181
5a21b665
DV
10182 if (intel_crtc->plane)
10183 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10184 else
10185 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
73dec95e
TU
10186 *cs++ = MI_WAIT_FOR_EVENT | flip_mask;
10187 *cs++ = MI_NOOP;
10188 *cs++ = MI_DISPLAY_FLIP_I915 | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
10189 *cs++ = fb->pitches[0];
10190 *cs++ = intel_crtc->flip_work->gtt_offset;
10191 *cs++ = MI_NOOP;
fd8e058a 10192
5a21b665
DV
10193 return 0;
10194}
84c33a64 10195
5a21b665
DV
10196static int intel_gen4_queue_flip(struct drm_device *dev,
10197 struct drm_crtc *crtc,
10198 struct drm_framebuffer *fb,
10199 struct drm_i915_gem_object *obj,
10200 struct drm_i915_gem_request *req,
10201 uint32_t flags)
10202{
fac5e23e 10203 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665 10204 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
73dec95e 10205 u32 pf, pipesrc, *cs;
143f73b3 10206
73dec95e
TU
10207 cs = intel_ring_begin(req, 4);
10208 if (IS_ERR(cs))
10209 return PTR_ERR(cs);
143f73b3 10210
5a21b665
DV
10211 /* i965+ uses the linear or tiled offsets from the
10212 * Display Registers (which do not change across a page-flip)
10213 * so we need only reprogram the base address.
10214 */
73dec95e
TU
10215 *cs++ = MI_DISPLAY_FLIP | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
10216 *cs++ = fb->pitches[0];
10217 *cs++ = intel_crtc->flip_work->gtt_offset |
10218 intel_fb_modifier_to_tiling(fb->modifier);
5a21b665
DV
10219
10220 /* XXX Enabling the panel-fitter across page-flip is so far
10221 * untested on non-native modes, so ignore it for now.
10222 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
10223 */
10224 pf = 0;
10225 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
73dec95e 10226 *cs++ = pf | pipesrc;
143f73b3 10227
5a21b665 10228 return 0;
8c9f3aaf
JB
10229}
10230
5a21b665
DV
10231static int intel_gen6_queue_flip(struct drm_device *dev,
10232 struct drm_crtc *crtc,
10233 struct drm_framebuffer *fb,
10234 struct drm_i915_gem_object *obj,
10235 struct drm_i915_gem_request *req,
10236 uint32_t flags)
da20eabd 10237{
fac5e23e 10238 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665 10239 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
73dec95e 10240 u32 pf, pipesrc, *cs;
d21fbe87 10241
73dec95e
TU
10242 cs = intel_ring_begin(req, 4);
10243 if (IS_ERR(cs))
10244 return PTR_ERR(cs);
92826fcd 10245
73dec95e
TU
10246 *cs++ = MI_DISPLAY_FLIP | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
10247 *cs++ = fb->pitches[0] | intel_fb_modifier_to_tiling(fb->modifier);
10248 *cs++ = intel_crtc->flip_work->gtt_offset;
92826fcd 10249
5a21b665
DV
10250 /* Contrary to the suggestions in the documentation,
10251 * "Enable Panel Fitter" does not seem to be required when page
10252 * flipping with a non-native mode, and worse causes a normal
10253 * modeset to fail.
10254 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
10255 */
10256 pf = 0;
10257 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
73dec95e 10258 *cs++ = pf | pipesrc;
7809e5ae 10259
5a21b665 10260 return 0;
7809e5ae
MR
10261}
10262
5a21b665
DV
10263static int intel_gen7_queue_flip(struct drm_device *dev,
10264 struct drm_crtc *crtc,
10265 struct drm_framebuffer *fb,
10266 struct drm_i915_gem_object *obj,
10267 struct drm_i915_gem_request *req,
10268 uint32_t flags)
d21fbe87 10269{
5db94019 10270 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665 10271 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
73dec95e 10272 u32 *cs, plane_bit = 0;
5a21b665 10273 int len, ret;
d21fbe87 10274
5a21b665
DV
10275 switch (intel_crtc->plane) {
10276 case PLANE_A:
10277 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
10278 break;
10279 case PLANE_B:
10280 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
10281 break;
10282 case PLANE_C:
10283 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
10284 break;
10285 default:
10286 WARN_ONCE(1, "unknown plane in flip command\n");
10287 return -ENODEV;
10288 }
10289
10290 len = 4;
b5321f30 10291 if (req->engine->id == RCS) {
5a21b665
DV
10292 len += 6;
10293 /*
10294 * On Gen 8, SRM is now taking an extra dword to accommodate
10295 * 48bits addresses, and we need a NOOP for the batch size to
10296 * stay even.
10297 */
5db94019 10298 if (IS_GEN8(dev_priv))
5a21b665
DV
10299 len += 2;
10300 }
10301
10302 /*
10303 * BSpec MI_DISPLAY_FLIP for IVB:
10304 * "The full packet must be contained within the same cache line."
10305 *
10306 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
10307 * cacheline, if we ever start emitting more commands before
10308 * the MI_DISPLAY_FLIP we may need to first emit everything else,
10309 * then do the cacheline alignment, and finally emit the
10310 * MI_DISPLAY_FLIP.
10311 */
10312 ret = intel_ring_cacheline_align(req);
10313 if (ret)
10314 return ret;
10315
73dec95e
TU
10316 cs = intel_ring_begin(req, len);
10317 if (IS_ERR(cs))
10318 return PTR_ERR(cs);
5a21b665
DV
10319
10320 /* Unmask the flip-done completion message. Note that the bspec says that
10321 * we should do this for both the BCS and RCS, and that we must not unmask
10322 * more than one flip event at any time (or ensure that one flip message
10323 * can be sent by waiting for flip-done prior to queueing new flips).
10324 * Experimentation says that BCS works despite DERRMR masking all
10325 * flip-done completion events and that unmasking all planes at once
10326 * for the RCS also doesn't appear to drop events. Setting the DERRMR
10327 * to zero does lead to lockups within MI_DISPLAY_FLIP.
10328 */
b5321f30 10329 if (req->engine->id == RCS) {
73dec95e
TU
10330 *cs++ = MI_LOAD_REGISTER_IMM(1);
10331 *cs++ = i915_mmio_reg_offset(DERRMR);
10332 *cs++ = ~(DERRMR_PIPEA_PRI_FLIP_DONE |
10333 DERRMR_PIPEB_PRI_FLIP_DONE |
10334 DERRMR_PIPEC_PRI_FLIP_DONE);
5db94019 10335 if (IS_GEN8(dev_priv))
73dec95e
TU
10336 *cs++ = MI_STORE_REGISTER_MEM_GEN8 |
10337 MI_SRM_LRM_GLOBAL_GTT;
5a21b665 10338 else
73dec95e
TU
10339 *cs++ = MI_STORE_REGISTER_MEM | MI_SRM_LRM_GLOBAL_GTT;
10340 *cs++ = i915_mmio_reg_offset(DERRMR);
10341 *cs++ = i915_ggtt_offset(req->engine->scratch) + 256;
5db94019 10342 if (IS_GEN8(dev_priv)) {
73dec95e
TU
10343 *cs++ = 0;
10344 *cs++ = MI_NOOP;
5a21b665
DV
10345 }
10346 }
10347
73dec95e
TU
10348 *cs++ = MI_DISPLAY_FLIP_I915 | plane_bit;
10349 *cs++ = fb->pitches[0] | intel_fb_modifier_to_tiling(fb->modifier);
10350 *cs++ = intel_crtc->flip_work->gtt_offset;
10351 *cs++ = MI_NOOP;
5a21b665
DV
10352
10353 return 0;
10354}
10355
10356static bool use_mmio_flip(struct intel_engine_cs *engine,
10357 struct drm_i915_gem_object *obj)
10358{
10359 /*
10360 * This is not being used for older platforms, because
10361 * non-availability of flip done interrupt forces us to use
10362 * CS flips. Older platforms derive flip done using some clever
10363 * tricks involving the flip_pending status bits and vblank irqs.
10364 * So using MMIO flips there would disrupt this mechanism.
10365 */
10366
10367 if (engine == NULL)
10368 return true;
10369
10370 if (INTEL_GEN(engine->i915) < 5)
10371 return false;
10372
10373 if (i915.use_mmio_flip < 0)
10374 return false;
10375 else if (i915.use_mmio_flip > 0)
10376 return true;
10377 else if (i915.enable_execlists)
10378 return true;
c37efb99 10379
d07f0e59 10380 return engine != i915_gem_object_last_write_engine(obj);
5a21b665
DV
10381}
10382
10383static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
10384 unsigned int rotation,
10385 struct intel_flip_work *work)
10386{
10387 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 10388 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
DV
10389 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
10390 const enum pipe pipe = intel_crtc->pipe;
d2196774 10391 u32 ctl, stride = skl_plane_stride(fb, 0, rotation);
5a21b665
DV
10392
10393 ctl = I915_READ(PLANE_CTL(pipe, 0));
10394 ctl &= ~PLANE_CTL_TILED_MASK;
bae781b2 10395 switch (fb->modifier) {
5a21b665
DV
10396 case DRM_FORMAT_MOD_NONE:
10397 break;
10398 case I915_FORMAT_MOD_X_TILED:
10399 ctl |= PLANE_CTL_TILED_X;
10400 break;
10401 case I915_FORMAT_MOD_Y_TILED:
10402 ctl |= PLANE_CTL_TILED_Y;
10403 break;
10404 case I915_FORMAT_MOD_Yf_TILED:
10405 ctl |= PLANE_CTL_TILED_YF;
10406 break;
10407 default:
bae781b2 10408 MISSING_CASE(fb->modifier);
5a21b665
DV
10409 }
10410
5a21b665
DV
10411 /*
10412 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
10413 * PLANE_SURF updates, the update is then guaranteed to be atomic.
10414 */
10415 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
10416 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
10417
10418 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
10419 POSTING_READ(PLANE_SURF(pipe, 0));
10420}
10421
10422static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
10423 struct intel_flip_work *work)
10424{
10425 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 10426 struct drm_i915_private *dev_priv = to_i915(dev);
72618ebf 10427 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
5a21b665
DV
10428 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
10429 u32 dspcntr;
10430
10431 dspcntr = I915_READ(reg);
10432
bae781b2 10433 if (fb->modifier == I915_FORMAT_MOD_X_TILED)
5a21b665
DV
10434 dspcntr |= DISPPLANE_TILED;
10435 else
10436 dspcntr &= ~DISPPLANE_TILED;
10437
10438 I915_WRITE(reg, dspcntr);
10439
10440 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
10441 POSTING_READ(DSPSURF(intel_crtc->plane));
10442}
10443
10444static void intel_mmio_flip_work_func(struct work_struct *w)
10445{
10446 struct intel_flip_work *work =
10447 container_of(w, struct intel_flip_work, mmio_work);
10448 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10449 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10450 struct intel_framebuffer *intel_fb =
10451 to_intel_framebuffer(crtc->base.primary->fb);
10452 struct drm_i915_gem_object *obj = intel_fb->obj;
10453
d07f0e59 10454 WARN_ON(i915_gem_object_wait(obj, 0, MAX_SCHEDULE_TIMEOUT, NULL) < 0);
5a21b665
DV
10455
10456 intel_pipe_update_start(crtc);
10457
10458 if (INTEL_GEN(dev_priv) >= 9)
10459 skl_do_mmio_flip(crtc, work->rotation, work);
10460 else
10461 /* use_mmio_flip() retricts MMIO flips to ilk+ */
10462 ilk_do_mmio_flip(crtc, work);
10463
10464 intel_pipe_update_end(crtc, work);
10465}
10466
10467static int intel_default_queue_flip(struct drm_device *dev,
10468 struct drm_crtc *crtc,
10469 struct drm_framebuffer *fb,
10470 struct drm_i915_gem_object *obj,
10471 struct drm_i915_gem_request *req,
10472 uint32_t flags)
10473{
10474 return -ENODEV;
10475}
10476
10477static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
10478 struct intel_crtc *intel_crtc,
10479 struct intel_flip_work *work)
10480{
10481 u32 addr, vblank;
10482
10483 if (!atomic_read(&work->pending))
10484 return false;
10485
10486 smp_rmb();
10487
10488 vblank = intel_crtc_get_vblank_counter(intel_crtc);
10489 if (work->flip_ready_vblank == 0) {
10490 if (work->flip_queued_req &&
f69a02c9 10491 !i915_gem_request_completed(work->flip_queued_req))
5a21b665
DV
10492 return false;
10493
10494 work->flip_ready_vblank = vblank;
10495 }
10496
10497 if (vblank - work->flip_ready_vblank < 3)
10498 return false;
10499
10500 /* Potential stall - if we see that the flip has happened,
10501 * assume a missed interrupt. */
10502 if (INTEL_GEN(dev_priv) >= 4)
10503 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
10504 else
10505 addr = I915_READ(DSPADDR(intel_crtc->plane));
10506
10507 /* There is a potential issue here with a false positive after a flip
10508 * to the same address. We could address this by checking for a
10509 * non-incrementing frame counter.
10510 */
10511 return addr == work->gtt_offset;
10512}
10513
10514void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
10515{
91c8a326 10516 struct drm_device *dev = &dev_priv->drm;
98187836 10517 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
5a21b665
DV
10518 struct intel_flip_work *work;
10519
10520 WARN_ON(!in_interrupt());
10521
10522 if (crtc == NULL)
10523 return;
10524
10525 spin_lock(&dev->event_lock);
e2af48c6 10526 work = crtc->flip_work;
5a21b665
DV
10527
10528 if (work != NULL && !is_mmio_work(work) &&
e2af48c6 10529 __pageflip_stall_check_cs(dev_priv, crtc, work)) {
5a21b665
DV
10530 WARN_ONCE(1,
10531 "Kicking stuck page flip: queued at %d, now %d\n",
e2af48c6
VS
10532 work->flip_queued_vblank, intel_crtc_get_vblank_counter(crtc));
10533 page_flip_completed(crtc);
5a21b665
DV
10534 work = NULL;
10535 }
10536
10537 if (work != NULL && !is_mmio_work(work) &&
e2af48c6 10538 intel_crtc_get_vblank_counter(crtc) - work->flip_queued_vblank > 1)
5a21b665
DV
10539 intel_queue_rps_boost_for_request(work->flip_queued_req);
10540 spin_unlock(&dev->event_lock);
10541}
10542
4c01ded5 10543__maybe_unused
5a21b665
DV
10544static int intel_crtc_page_flip(struct drm_crtc *crtc,
10545 struct drm_framebuffer *fb,
10546 struct drm_pending_vblank_event *event,
10547 uint32_t page_flip_flags)
10548{
10549 struct drm_device *dev = crtc->dev;
fac5e23e 10550 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
DV
10551 struct drm_framebuffer *old_fb = crtc->primary->fb;
10552 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
10553 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10554 struct drm_plane *primary = crtc->primary;
10555 enum pipe pipe = intel_crtc->pipe;
10556 struct intel_flip_work *work;
10557 struct intel_engine_cs *engine;
10558 bool mmio_flip;
8e637178 10559 struct drm_i915_gem_request *request;
058d88c4 10560 struct i915_vma *vma;
5a21b665
DV
10561 int ret;
10562
10563 /*
10564 * drm_mode_page_flip_ioctl() should already catch this, but double
10565 * check to be safe. In the future we may enable pageflipping from
10566 * a disabled primary plane.
10567 */
10568 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
10569 return -EBUSY;
10570
10571 /* Can't change pixel format via MI display flips. */
dbd4d576 10572 if (fb->format != crtc->primary->fb->format)
5a21b665
DV
10573 return -EINVAL;
10574
10575 /*
10576 * TILEOFF/LINOFF registers can't be changed via MI display flips.
10577 * Note that pitch changes could also affect these register.
10578 */
6315b5d3 10579 if (INTEL_GEN(dev_priv) > 3 &&
5a21b665
DV
10580 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
10581 fb->pitches[0] != crtc->primary->fb->pitches[0]))
10582 return -EINVAL;
10583
10584 if (i915_terminally_wedged(&dev_priv->gpu_error))
10585 goto out_hang;
10586
10587 work = kzalloc(sizeof(*work), GFP_KERNEL);
10588 if (work == NULL)
10589 return -ENOMEM;
10590
10591 work->event = event;
10592 work->crtc = crtc;
10593 work->old_fb = old_fb;
10594 INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
10595
10596 ret = drm_crtc_vblank_get(crtc);
10597 if (ret)
10598 goto free_work;
10599
10600 /* We borrow the event spin lock for protecting flip_work */
10601 spin_lock_irq(&dev->event_lock);
10602 if (intel_crtc->flip_work) {
10603 /* Before declaring the flip queue wedged, check if
10604 * the hardware completed the operation behind our backs.
10605 */
10606 if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
10607 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
10608 page_flip_completed(intel_crtc);
10609 } else {
10610 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
10611 spin_unlock_irq(&dev->event_lock);
10612
10613 drm_crtc_vblank_put(crtc);
10614 kfree(work);
10615 return -EBUSY;
10616 }
10617 }
10618 intel_crtc->flip_work = work;
10619 spin_unlock_irq(&dev->event_lock);
10620
10621 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
10622 flush_workqueue(dev_priv->wq);
10623
10624 /* Reference the objects for the scheduled work. */
10625 drm_framebuffer_reference(work->old_fb);
5a21b665
DV
10626
10627 crtc->primary->fb = fb;
10628 update_state_fb(crtc->primary);
faf68d92 10629
25dc556a 10630 work->pending_flip_obj = i915_gem_object_get(obj);
5a21b665
DV
10631
10632 ret = i915_mutex_lock_interruptible(dev);
10633 if (ret)
10634 goto cleanup;
10635
8af29b0c
CW
10636 intel_crtc->reset_count = i915_reset_count(&dev_priv->gpu_error);
10637 if (i915_reset_in_progress_or_wedged(&dev_priv->gpu_error)) {
5a21b665 10638 ret = -EIO;
ddbb271a 10639 goto unlock;
5a21b665
DV
10640 }
10641
10642 atomic_inc(&intel_crtc->unpin_work_count);
10643
9beb5fea 10644 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
5a21b665
DV
10645 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
10646
920a14b2 10647 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
3b3f1650 10648 engine = dev_priv->engine[BCS];
bae781b2 10649 if (fb->modifier != old_fb->modifier)
5a21b665
DV
10650 /* vlv: DISPLAY_FLIP fails to change tiling */
10651 engine = NULL;
fd6b8f43 10652 } else if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
3b3f1650 10653 engine = dev_priv->engine[BCS];
6315b5d3 10654 } else if (INTEL_GEN(dev_priv) >= 7) {
d07f0e59 10655 engine = i915_gem_object_last_write_engine(obj);
5a21b665 10656 if (engine == NULL || engine->id != RCS)
3b3f1650 10657 engine = dev_priv->engine[BCS];
5a21b665 10658 } else {
3b3f1650 10659 engine = dev_priv->engine[RCS];
5a21b665
DV
10660 }
10661
10662 mmio_flip = use_mmio_flip(engine, obj);
10663
058d88c4
CW
10664 vma = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
10665 if (IS_ERR(vma)) {
10666 ret = PTR_ERR(vma);
5a21b665 10667 goto cleanup_pending;
058d88c4 10668 }
5a21b665 10669
be1e3415
CW
10670 work->old_vma = to_intel_plane_state(primary->state)->vma;
10671 to_intel_plane_state(primary->state)->vma = vma;
10672
10673 work->gtt_offset = i915_ggtt_offset(vma) + intel_crtc->dspaddr_offset;
5a21b665
DV
10674 work->rotation = crtc->primary->state->rotation;
10675
1f061316
PZ
10676 /*
10677 * There's the potential that the next frame will not be compatible with
10678 * FBC, so we want to call pre_update() before the actual page flip.
10679 * The problem is that pre_update() caches some information about the fb
10680 * object, so we want to do this only after the object is pinned. Let's
10681 * be on the safe side and do this immediately before scheduling the
10682 * flip.
10683 */
10684 intel_fbc_pre_update(intel_crtc, intel_crtc->config,
10685 to_intel_plane_state(primary->state));
10686
5a21b665
DV
10687 if (mmio_flip) {
10688 INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
6277c8d0 10689 queue_work(system_unbound_wq, &work->mmio_work);
5a21b665 10690 } else {
e8a9c58f
CW
10691 request = i915_gem_request_alloc(engine,
10692 dev_priv->kernel_context);
8e637178
CW
10693 if (IS_ERR(request)) {
10694 ret = PTR_ERR(request);
10695 goto cleanup_unpin;
10696 }
10697
a2bc4695 10698 ret = i915_gem_request_await_object(request, obj, false);
8e637178
CW
10699 if (ret)
10700 goto cleanup_request;
10701
5a21b665
DV
10702 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
10703 page_flip_flags);
10704 if (ret)
8e637178 10705 goto cleanup_request;
5a21b665
DV
10706
10707 intel_mark_page_flip_active(intel_crtc, work);
10708
8e637178 10709 work->flip_queued_req = i915_gem_request_get(request);
5a21b665
DV
10710 i915_add_request_no_flush(request);
10711 }
10712
92117f0b 10713 i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
5a21b665
DV
10714 i915_gem_track_fb(intel_fb_obj(old_fb), obj,
10715 to_intel_plane(primary)->frontbuffer_bit);
10716 mutex_unlock(&dev->struct_mutex);
10717
5748b6a1 10718 intel_frontbuffer_flip_prepare(to_i915(dev),
5a21b665
DV
10719 to_intel_plane(primary)->frontbuffer_bit);
10720
10721 trace_i915_flip_request(intel_crtc->plane, obj);
10722
10723 return 0;
10724
8e637178
CW
10725cleanup_request:
10726 i915_add_request_no_flush(request);
5a21b665 10727cleanup_unpin:
be1e3415
CW
10728 to_intel_plane_state(primary->state)->vma = work->old_vma;
10729 intel_unpin_fb_vma(vma);
5a21b665 10730cleanup_pending:
5a21b665 10731 atomic_dec(&intel_crtc->unpin_work_count);
ddbb271a 10732unlock:
5a21b665
DV
10733 mutex_unlock(&dev->struct_mutex);
10734cleanup:
10735 crtc->primary->fb = old_fb;
10736 update_state_fb(crtc->primary);
10737
f0cd5182 10738 i915_gem_object_put(obj);
5a21b665
DV
10739 drm_framebuffer_unreference(work->old_fb);
10740
10741 spin_lock_irq(&dev->event_lock);
10742 intel_crtc->flip_work = NULL;
10743 spin_unlock_irq(&dev->event_lock);
10744
10745 drm_crtc_vblank_put(crtc);
10746free_work:
10747 kfree(work);
10748
10749 if (ret == -EIO) {
10750 struct drm_atomic_state *state;
10751 struct drm_plane_state *plane_state;
10752
10753out_hang:
10754 state = drm_atomic_state_alloc(dev);
10755 if (!state)
10756 return -ENOMEM;
10757 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
10758
10759retry:
10760 plane_state = drm_atomic_get_plane_state(state, primary);
10761 ret = PTR_ERR_OR_ZERO(plane_state);
10762 if (!ret) {
10763 drm_atomic_set_fb_for_plane(plane_state, fb);
10764
10765 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
10766 if (!ret)
10767 ret = drm_atomic_commit(state);
10768 }
10769
10770 if (ret == -EDEADLK) {
10771 drm_modeset_backoff(state->acquire_ctx);
10772 drm_atomic_state_clear(state);
10773 goto retry;
10774 }
10775
0853695c 10776 drm_atomic_state_put(state);
5a21b665
DV
10777
10778 if (ret == 0 && event) {
10779 spin_lock_irq(&dev->event_lock);
10780 drm_crtc_send_vblank_event(crtc, event);
10781 spin_unlock_irq(&dev->event_lock);
10782 }
10783 }
10784 return ret;
10785}
10786
10787
10788/**
10789 * intel_wm_need_update - Check whether watermarks need updating
10790 * @plane: drm plane
10791 * @state: new plane state
10792 *
10793 * Check current plane state versus the new one to determine whether
10794 * watermarks need to be recalculated.
10795 *
10796 * Returns true or false.
10797 */
10798static bool intel_wm_need_update(struct drm_plane *plane,
10799 struct drm_plane_state *state)
10800{
10801 struct intel_plane_state *new = to_intel_plane_state(state);
10802 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
10803
10804 /* Update watermarks on tiling or size changes. */
936e71e3 10805 if (new->base.visible != cur->base.visible)
5a21b665
DV
10806 return true;
10807
10808 if (!cur->base.fb || !new->base.fb)
10809 return false;
10810
bae781b2 10811 if (cur->base.fb->modifier != new->base.fb->modifier ||
5a21b665 10812 cur->base.rotation != new->base.rotation ||
936e71e3
VS
10813 drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
10814 drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
10815 drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
10816 drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
5a21b665
DV
10817 return true;
10818
10819 return false;
10820}
10821
10822static bool needs_scaling(struct intel_plane_state *state)
10823{
936e71e3
VS
10824 int src_w = drm_rect_width(&state->base.src) >> 16;
10825 int src_h = drm_rect_height(&state->base.src) >> 16;
10826 int dst_w = drm_rect_width(&state->base.dst);
10827 int dst_h = drm_rect_height(&state->base.dst);
5a21b665
DV
10828
10829 return (src_w != dst_w || src_h != dst_h);
10830}
d21fbe87 10831
da20eabd
ML
10832int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
10833 struct drm_plane_state *plane_state)
10834{
ab1d3a0e 10835 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
da20eabd
ML
10836 struct drm_crtc *crtc = crtc_state->crtc;
10837 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10838 struct drm_plane *plane = plane_state->plane;
10839 struct drm_device *dev = crtc->dev;
ed4a6a7c 10840 struct drm_i915_private *dev_priv = to_i915(dev);
da20eabd
ML
10841 struct intel_plane_state *old_plane_state =
10842 to_intel_plane_state(plane->state);
da20eabd
ML
10843 bool mode_changed = needs_modeset(crtc_state);
10844 bool was_crtc_enabled = crtc->state->active;
10845 bool is_crtc_enabled = crtc_state->active;
da20eabd
ML
10846 bool turn_off, turn_on, visible, was_visible;
10847 struct drm_framebuffer *fb = plane_state->fb;
78108b7c 10848 int ret;
da20eabd 10849
55b8f2a7 10850 if (INTEL_GEN(dev_priv) >= 9 && plane->type != DRM_PLANE_TYPE_CURSOR) {
da20eabd
ML
10851 ret = skl_update_scaler_plane(
10852 to_intel_crtc_state(crtc_state),
10853 to_intel_plane_state(plane_state));
10854 if (ret)
10855 return ret;
10856 }
10857
936e71e3 10858 was_visible = old_plane_state->base.visible;
1d4258db 10859 visible = plane_state->visible;
da20eabd
ML
10860
10861 if (!was_crtc_enabled && WARN_ON(was_visible))
10862 was_visible = false;
10863
35c08f43
ML
10864 /*
10865 * Visibility is calculated as if the crtc was on, but
10866 * after scaler setup everything depends on it being off
10867 * when the crtc isn't active.
f818ffea
VS
10868 *
10869 * FIXME this is wrong for watermarks. Watermarks should also
10870 * be computed as if the pipe would be active. Perhaps move
10871 * per-plane wm computation to the .check_plane() hook, and
10872 * only combine the results from all planes in the current place?
35c08f43
ML
10873 */
10874 if (!is_crtc_enabled)
1d4258db 10875 plane_state->visible = visible = false;
da20eabd
ML
10876
10877 if (!was_visible && !visible)
10878 return 0;
10879
e8861675
ML
10880 if (fb != old_plane_state->base.fb)
10881 pipe_config->fb_changed = true;
10882
da20eabd
ML
10883 turn_off = was_visible && (!visible || mode_changed);
10884 turn_on = visible && (!was_visible || mode_changed);
10885
72660ce0 10886 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
78108b7c
VS
10887 intel_crtc->base.base.id,
10888 intel_crtc->base.name,
72660ce0
VS
10889 plane->base.id, plane->name,
10890 fb ? fb->base.id : -1);
da20eabd 10891
72660ce0
VS
10892 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
10893 plane->base.id, plane->name,
10894 was_visible, visible,
da20eabd
ML
10895 turn_off, turn_on, mode_changed);
10896
caed361d
VS
10897 if (turn_on) {
10898 pipe_config->update_wm_pre = true;
10899
10900 /* must disable cxsr around plane enable/disable */
10901 if (plane->type != DRM_PLANE_TYPE_CURSOR)
10902 pipe_config->disable_cxsr = true;
10903 } else if (turn_off) {
10904 pipe_config->update_wm_post = true;
92826fcd 10905
852eb00d 10906 /* must disable cxsr around plane enable/disable */
e8861675 10907 if (plane->type != DRM_PLANE_TYPE_CURSOR)
ab1d3a0e 10908 pipe_config->disable_cxsr = true;
852eb00d 10909 } else if (intel_wm_need_update(plane, plane_state)) {
caed361d
VS
10910 /* FIXME bollocks */
10911 pipe_config->update_wm_pre = true;
10912 pipe_config->update_wm_post = true;
852eb00d 10913 }
da20eabd 10914
ed4a6a7c 10915 /* Pre-gen9 platforms need two-step watermark updates */
caed361d 10916 if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
6315b5d3 10917 INTEL_GEN(dev_priv) < 9 && dev_priv->display.optimize_watermarks)
ed4a6a7c
MR
10918 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
10919
8be6ca85 10920 if (visible || was_visible)
cd202f69 10921 pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
a9ff8714 10922
31ae71fc
ML
10923 /*
10924 * WaCxSRDisabledForSpriteScaling:ivb
10925 *
10926 * cstate->update_wm was already set above, so this flag will
10927 * take effect when we commit and program watermarks.
10928 */
fd6b8f43 10929 if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev_priv) &&
31ae71fc
ML
10930 needs_scaling(to_intel_plane_state(plane_state)) &&
10931 !needs_scaling(old_plane_state))
10932 pipe_config->disable_lp_wm = true;
d21fbe87 10933
da20eabd
ML
10934 return 0;
10935}
10936
6d3a1ce7
ML
10937static bool encoders_cloneable(const struct intel_encoder *a,
10938 const struct intel_encoder *b)
10939{
10940 /* masks could be asymmetric, so check both ways */
10941 return a == b || (a->cloneable & (1 << b->type) &&
10942 b->cloneable & (1 << a->type));
10943}
10944
10945static bool check_single_encoder_cloning(struct drm_atomic_state *state,
10946 struct intel_crtc *crtc,
10947 struct intel_encoder *encoder)
10948{
10949 struct intel_encoder *source_encoder;
10950 struct drm_connector *connector;
10951 struct drm_connector_state *connector_state;
10952 int i;
10953
10954 for_each_connector_in_state(state, connector, connector_state, i) {
10955 if (connector_state->crtc != &crtc->base)
10956 continue;
10957
10958 source_encoder =
10959 to_intel_encoder(connector_state->best_encoder);
10960 if (!encoders_cloneable(encoder, source_encoder))
10961 return false;
10962 }
10963
10964 return true;
10965}
10966
6d3a1ce7
ML
10967static int intel_crtc_atomic_check(struct drm_crtc *crtc,
10968 struct drm_crtc_state *crtc_state)
10969{
cf5a15be 10970 struct drm_device *dev = crtc->dev;
fac5e23e 10971 struct drm_i915_private *dev_priv = to_i915(dev);
6d3a1ce7 10972 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cf5a15be
ML
10973 struct intel_crtc_state *pipe_config =
10974 to_intel_crtc_state(crtc_state);
6d3a1ce7 10975 struct drm_atomic_state *state = crtc_state->state;
4d20cd86 10976 int ret;
6d3a1ce7
ML
10977 bool mode_changed = needs_modeset(crtc_state);
10978
852eb00d 10979 if (mode_changed && !crtc_state->active)
caed361d 10980 pipe_config->update_wm_post = true;
eddfcbcd 10981
ad421372
ML
10982 if (mode_changed && crtc_state->enable &&
10983 dev_priv->display.crtc_compute_clock &&
8106ddbd 10984 !WARN_ON(pipe_config->shared_dpll)) {
ad421372
ML
10985 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
10986 pipe_config);
10987 if (ret)
10988 return ret;
10989 }
10990
82cf435b
LL
10991 if (crtc_state->color_mgmt_changed) {
10992 ret = intel_color_check(crtc, crtc_state);
10993 if (ret)
10994 return ret;
e7852a4b
LL
10995
10996 /*
10997 * Changing color management on Intel hardware is
10998 * handled as part of planes update.
10999 */
11000 crtc_state->planes_changed = true;
82cf435b
LL
11001 }
11002
e435d6e5 11003 ret = 0;
86c8bbbe 11004 if (dev_priv->display.compute_pipe_wm) {
e3bddded 11005 ret = dev_priv->display.compute_pipe_wm(pipe_config);
ed4a6a7c
MR
11006 if (ret) {
11007 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
11008 return ret;
11009 }
11010 }
11011
11012 if (dev_priv->display.compute_intermediate_wm &&
11013 !to_intel_atomic_state(state)->skip_intermediate_wm) {
11014 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
11015 return 0;
11016
11017 /*
11018 * Calculate 'intermediate' watermarks that satisfy both the
11019 * old state and the new state. We can program these
11020 * immediately.
11021 */
6315b5d3 11022 ret = dev_priv->display.compute_intermediate_wm(dev,
ed4a6a7c
MR
11023 intel_crtc,
11024 pipe_config);
11025 if (ret) {
11026 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
86c8bbbe 11027 return ret;
ed4a6a7c 11028 }
e3d5457c
VS
11029 } else if (dev_priv->display.compute_intermediate_wm) {
11030 if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
11031 pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
86c8bbbe
MR
11032 }
11033
6315b5d3 11034 if (INTEL_GEN(dev_priv) >= 9) {
e435d6e5
ML
11035 if (mode_changed)
11036 ret = skl_update_scaler_crtc(pipe_config);
11037
11038 if (!ret)
11039 ret = intel_atomic_setup_scalers(dev, intel_crtc,
11040 pipe_config);
11041 }
11042
11043 return ret;
6d3a1ce7
ML
11044}
11045
65b38e0d 11046static const struct drm_crtc_helper_funcs intel_helper_funcs = {
f6e5b160 11047 .mode_set_base_atomic = intel_pipe_set_base_atomic,
5a21b665
DV
11048 .atomic_begin = intel_begin_crtc_commit,
11049 .atomic_flush = intel_finish_crtc_commit,
6d3a1ce7 11050 .atomic_check = intel_crtc_atomic_check,
f6e5b160
CW
11051};
11052
d29b2f9d
ACO
11053static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11054{
11055 struct intel_connector *connector;
11056
11057 for_each_intel_connector(dev, connector) {
8863dc7f
DV
11058 if (connector->base.state->crtc)
11059 drm_connector_unreference(&connector->base);
11060
d29b2f9d
ACO
11061 if (connector->base.encoder) {
11062 connector->base.state->best_encoder =
11063 connector->base.encoder;
11064 connector->base.state->crtc =
11065 connector->base.encoder->crtc;
8863dc7f
DV
11066
11067 drm_connector_reference(&connector->base);
d29b2f9d
ACO
11068 } else {
11069 connector->base.state->best_encoder = NULL;
11070 connector->base.state->crtc = NULL;
11071 }
11072 }
11073}
11074
050f7aeb 11075static void
eba905b2 11076connected_sink_compute_bpp(struct intel_connector *connector,
5cec258b 11077 struct intel_crtc_state *pipe_config)
050f7aeb 11078{
6a2a5c5d 11079 const struct drm_display_info *info = &connector->base.display_info;
050f7aeb
DV
11080 int bpp = pipe_config->pipe_bpp;
11081
11082 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
6a2a5c5d
VS
11083 connector->base.base.id,
11084 connector->base.name);
050f7aeb
DV
11085
11086 /* Don't use an invalid EDID bpc value */
6a2a5c5d 11087 if (info->bpc != 0 && info->bpc * 3 < bpp) {
050f7aeb 11088 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
6a2a5c5d
VS
11089 bpp, info->bpc * 3);
11090 pipe_config->pipe_bpp = info->bpc * 3;
050f7aeb
DV
11091 }
11092
196f954e 11093 /* Clamp bpp to 8 on screens without EDID 1.4 */
6a2a5c5d 11094 if (info->bpc == 0 && bpp > 24) {
196f954e
MK
11095 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
11096 bpp);
11097 pipe_config->pipe_bpp = 24;
050f7aeb
DV
11098 }
11099}
11100
4e53c2e0 11101static int
050f7aeb 11102compute_baseline_pipe_bpp(struct intel_crtc *crtc,
5cec258b 11103 struct intel_crtc_state *pipe_config)
4e53c2e0 11104{
9beb5fea 11105 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1486017f 11106 struct drm_atomic_state *state;
da3ced29
ACO
11107 struct drm_connector *connector;
11108 struct drm_connector_state *connector_state;
1486017f 11109 int bpp, i;
4e53c2e0 11110
9beb5fea
TU
11111 if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
11112 IS_CHERRYVIEW(dev_priv)))
4e53c2e0 11113 bpp = 10*3;
9beb5fea 11114 else if (INTEL_GEN(dev_priv) >= 5)
d328c9d7
DV
11115 bpp = 12*3;
11116 else
11117 bpp = 8*3;
11118
4e53c2e0 11119
4e53c2e0
DV
11120 pipe_config->pipe_bpp = bpp;
11121
1486017f
ACO
11122 state = pipe_config->base.state;
11123
4e53c2e0 11124 /* Clamp display bpp to EDID value */
da3ced29
ACO
11125 for_each_connector_in_state(state, connector, connector_state, i) {
11126 if (connector_state->crtc != &crtc->base)
4e53c2e0
DV
11127 continue;
11128
da3ced29
ACO
11129 connected_sink_compute_bpp(to_intel_connector(connector),
11130 pipe_config);
4e53c2e0
DV
11131 }
11132
11133 return bpp;
11134}
11135
644db711
DV
11136static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11137{
11138 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11139 "type: 0x%x flags: 0x%x\n",
1342830c 11140 mode->crtc_clock,
644db711
DV
11141 mode->crtc_hdisplay, mode->crtc_hsync_start,
11142 mode->crtc_hsync_end, mode->crtc_htotal,
11143 mode->crtc_vdisplay, mode->crtc_vsync_start,
11144 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
11145}
11146
f6982332
TU
11147static inline void
11148intel_dump_m_n_config(struct intel_crtc_state *pipe_config, char *id,
a4309657 11149 unsigned int lane_count, struct intel_link_m_n *m_n)
f6982332 11150{
a4309657
TU
11151 DRM_DEBUG_KMS("%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11152 id, lane_count,
f6982332
TU
11153 m_n->gmch_m, m_n->gmch_n,
11154 m_n->link_m, m_n->link_n, m_n->tu);
11155}
11156
c0b03411 11157static void intel_dump_pipe_config(struct intel_crtc *crtc,
5cec258b 11158 struct intel_crtc_state *pipe_config,
c0b03411
DV
11159 const char *context)
11160{
6a60cd87 11161 struct drm_device *dev = crtc->base.dev;
4f8036a2 11162 struct drm_i915_private *dev_priv = to_i915(dev);
6a60cd87
CK
11163 struct drm_plane *plane;
11164 struct intel_plane *intel_plane;
11165 struct intel_plane_state *state;
11166 struct drm_framebuffer *fb;
11167
66766e4f
TU
11168 DRM_DEBUG_KMS("[CRTC:%d:%s]%s\n",
11169 crtc->base.base.id, crtc->base.name, context);
c0b03411 11170
2c89429e
TU
11171 DRM_DEBUG_KMS("cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
11172 transcoder_name(pipe_config->cpu_transcoder),
c0b03411 11173 pipe_config->pipe_bpp, pipe_config->dither);
a4309657
TU
11174
11175 if (pipe_config->has_pch_encoder)
11176 intel_dump_m_n_config(pipe_config, "fdi",
11177 pipe_config->fdi_lanes,
11178 &pipe_config->fdi_m_n);
f6982332
TU
11179
11180 if (intel_crtc_has_dp_encoder(pipe_config)) {
a4309657
TU
11181 intel_dump_m_n_config(pipe_config, "dp m_n",
11182 pipe_config->lane_count, &pipe_config->dp_m_n);
d806e682
TU
11183 if (pipe_config->has_drrs)
11184 intel_dump_m_n_config(pipe_config, "dp m2_n2",
11185 pipe_config->lane_count,
11186 &pipe_config->dp_m2_n2);
f6982332 11187 }
b95af8be 11188
55072d19 11189 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
2c89429e 11190 pipe_config->has_audio, pipe_config->has_infoframe);
55072d19 11191
c0b03411 11192 DRM_DEBUG_KMS("requested mode:\n");
2d112de7 11193 drm_mode_debug_printmodeline(&pipe_config->base.mode);
c0b03411 11194 DRM_DEBUG_KMS("adjusted mode:\n");
2d112de7
ACO
11195 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
11196 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
a7d1b3f4 11197 DRM_DEBUG_KMS("port clock: %d, pipe src size: %dx%d, pixel rate %d\n",
2c89429e 11198 pipe_config->port_clock,
a7d1b3f4
VS
11199 pipe_config->pipe_src_w, pipe_config->pipe_src_h,
11200 pipe_config->pixel_rate);
dd2f616d
TU
11201
11202 if (INTEL_GEN(dev_priv) >= 9)
11203 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
11204 crtc->num_scalers,
11205 pipe_config->scaler_state.scaler_users,
11206 pipe_config->scaler_state.scaler_id);
a74f8375
TU
11207
11208 if (HAS_GMCH_DISPLAY(dev_priv))
11209 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
11210 pipe_config->gmch_pfit.control,
11211 pipe_config->gmch_pfit.pgm_ratios,
11212 pipe_config->gmch_pfit.lvds_border_bits);
11213 else
11214 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
11215 pipe_config->pch_pfit.pos,
11216 pipe_config->pch_pfit.size,
08c4d7fc 11217 enableddisabled(pipe_config->pch_pfit.enabled));
a74f8375 11218
2c89429e
TU
11219 DRM_DEBUG_KMS("ips: %i, double wide: %i\n",
11220 pipe_config->ips_enabled, pipe_config->double_wide);
6a60cd87 11221
f50b79f0 11222 intel_dpll_dump_hw_state(dev_priv, &pipe_config->dpll_hw_state);
415ff0f6 11223
6a60cd87
CK
11224 DRM_DEBUG_KMS("planes on this crtc\n");
11225 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
b3c11ac2 11226 struct drm_format_name_buf format_name;
6a60cd87
CK
11227 intel_plane = to_intel_plane(plane);
11228 if (intel_plane->pipe != crtc->pipe)
11229 continue;
11230
11231 state = to_intel_plane_state(plane->state);
11232 fb = state->base.fb;
11233 if (!fb) {
1d577e02
VS
11234 DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
11235 plane->base.id, plane->name, state->scaler_id);
6a60cd87
CK
11236 continue;
11237 }
11238
dd2f616d
TU
11239 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d, fb = %ux%u format = %s\n",
11240 plane->base.id, plane->name,
b3c11ac2 11241 fb->base.id, fb->width, fb->height,
438b74a5 11242 drm_get_format_name(fb->format->format, &format_name));
dd2f616d
TU
11243 if (INTEL_GEN(dev_priv) >= 9)
11244 DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
11245 state->scaler_id,
11246 state->base.src.x1 >> 16,
11247 state->base.src.y1 >> 16,
11248 drm_rect_width(&state->base.src) >> 16,
11249 drm_rect_height(&state->base.src) >> 16,
11250 state->base.dst.x1, state->base.dst.y1,
11251 drm_rect_width(&state->base.dst),
11252 drm_rect_height(&state->base.dst));
6a60cd87 11253 }
c0b03411
DV
11254}
11255
5448a00d 11256static bool check_digital_port_conflicts(struct drm_atomic_state *state)
00f0b378 11257{
5448a00d 11258 struct drm_device *dev = state->dev;
da3ced29 11259 struct drm_connector *connector;
00f0b378 11260 unsigned int used_ports = 0;
477321e0 11261 unsigned int used_mst_ports = 0;
00f0b378
VS
11262
11263 /*
11264 * Walk the connector list instead of the encoder
11265 * list to detect the problem on ddi platforms
11266 * where there's just one encoder per digital port.
11267 */
0bff4858
VS
11268 drm_for_each_connector(connector, dev) {
11269 struct drm_connector_state *connector_state;
11270 struct intel_encoder *encoder;
11271
11272 connector_state = drm_atomic_get_existing_connector_state(state, connector);
11273 if (!connector_state)
11274 connector_state = connector->state;
11275
5448a00d 11276 if (!connector_state->best_encoder)
00f0b378
VS
11277 continue;
11278
5448a00d
ACO
11279 encoder = to_intel_encoder(connector_state->best_encoder);
11280
11281 WARN_ON(!connector_state->crtc);
00f0b378
VS
11282
11283 switch (encoder->type) {
11284 unsigned int port_mask;
11285 case INTEL_OUTPUT_UNKNOWN:
4f8036a2 11286 if (WARN_ON(!HAS_DDI(to_i915(dev))))
00f0b378 11287 break;
cca0502b 11288 case INTEL_OUTPUT_DP:
00f0b378
VS
11289 case INTEL_OUTPUT_HDMI:
11290 case INTEL_OUTPUT_EDP:
11291 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
11292
11293 /* the same port mustn't appear more than once */
11294 if (used_ports & port_mask)
11295 return false;
11296
11297 used_ports |= port_mask;
477321e0
VS
11298 break;
11299 case INTEL_OUTPUT_DP_MST:
11300 used_mst_ports |=
11301 1 << enc_to_mst(&encoder->base)->primary->port;
11302 break;
00f0b378
VS
11303 default:
11304 break;
11305 }
11306 }
11307
477321e0
VS
11308 /* can't mix MST and SST/HDMI on the same port */
11309 if (used_ports & used_mst_ports)
11310 return false;
11311
00f0b378
VS
11312 return true;
11313}
11314
83a57153
ACO
11315static void
11316clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
11317{
11318 struct drm_crtc_state tmp_state;
663a3640 11319 struct intel_crtc_scaler_state scaler_state;
4978cc93 11320 struct intel_dpll_hw_state dpll_hw_state;
8106ddbd 11321 struct intel_shared_dpll *shared_dpll;
c4e2d043 11322 bool force_thru;
83a57153 11323
7546a384
ACO
11324 /* FIXME: before the switch to atomic started, a new pipe_config was
11325 * kzalloc'd. Code that depends on any field being zero should be
11326 * fixed, so that the crtc_state can be safely duplicated. For now,
11327 * only fields that are know to not cause problems are preserved. */
11328
83a57153 11329 tmp_state = crtc_state->base;
663a3640 11330 scaler_state = crtc_state->scaler_state;
4978cc93
ACO
11331 shared_dpll = crtc_state->shared_dpll;
11332 dpll_hw_state = crtc_state->dpll_hw_state;
c4e2d043 11333 force_thru = crtc_state->pch_pfit.force_thru;
4978cc93 11334
83a57153 11335 memset(crtc_state, 0, sizeof *crtc_state);
4978cc93 11336
83a57153 11337 crtc_state->base = tmp_state;
663a3640 11338 crtc_state->scaler_state = scaler_state;
4978cc93
ACO
11339 crtc_state->shared_dpll = shared_dpll;
11340 crtc_state->dpll_hw_state = dpll_hw_state;
c4e2d043 11341 crtc_state->pch_pfit.force_thru = force_thru;
83a57153
ACO
11342}
11343
548ee15b 11344static int
b8cecdf5 11345intel_modeset_pipe_config(struct drm_crtc *crtc,
b359283a 11346 struct intel_crtc_state *pipe_config)
ee7b9f93 11347{
b359283a 11348 struct drm_atomic_state *state = pipe_config->base.state;
7758a113 11349 struct intel_encoder *encoder;
da3ced29 11350 struct drm_connector *connector;
0b901879 11351 struct drm_connector_state *connector_state;
d328c9d7 11352 int base_bpp, ret = -EINVAL;
0b901879 11353 int i;
e29c22c0 11354 bool retry = true;
ee7b9f93 11355
83a57153 11356 clear_intel_crtc_state(pipe_config);
7758a113 11357
e143a21c
DV
11358 pipe_config->cpu_transcoder =
11359 (enum transcoder) to_intel_crtc(crtc)->pipe;
b8cecdf5 11360
2960bc9c
ID
11361 /*
11362 * Sanitize sync polarity flags based on requested ones. If neither
11363 * positive or negative polarity is requested, treat this as meaning
11364 * negative polarity.
11365 */
2d112de7 11366 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 11367 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
2d112de7 11368 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
2960bc9c 11369
2d112de7 11370 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 11371 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
2d112de7 11372 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
2960bc9c 11373
d328c9d7
DV
11374 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
11375 pipe_config);
11376 if (base_bpp < 0)
4e53c2e0
DV
11377 goto fail;
11378
e41a56be
VS
11379 /*
11380 * Determine the real pipe dimensions. Note that stereo modes can
11381 * increase the actual pipe size due to the frame doubling and
11382 * insertion of additional space for blanks between the frame. This
11383 * is stored in the crtc timings. We use the requested mode to do this
11384 * computation to clearly distinguish it from the adjusted mode, which
11385 * can be changed by the connectors in the below retry loop.
11386 */
196cd5d3 11387 drm_mode_get_hv_timing(&pipe_config->base.mode,
ecb7e16b
GP
11388 &pipe_config->pipe_src_w,
11389 &pipe_config->pipe_src_h);
e41a56be 11390
253c84c8
VS
11391 for_each_connector_in_state(state, connector, connector_state, i) {
11392 if (connector_state->crtc != crtc)
11393 continue;
11394
11395 encoder = to_intel_encoder(connector_state->best_encoder);
11396
e25148d0
VS
11397 if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
11398 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11399 goto fail;
11400 }
11401
253c84c8
VS
11402 /*
11403 * Determine output_types before calling the .compute_config()
11404 * hooks so that the hooks can use this information safely.
11405 */
11406 pipe_config->output_types |= 1 << encoder->type;
11407 }
11408
e29c22c0 11409encoder_retry:
ef1b460d 11410 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 11411 pipe_config->port_clock = 0;
ef1b460d 11412 pipe_config->pixel_multiplier = 1;
ff9a6750 11413
135c81b8 11414 /* Fill in default crtc timings, allow encoders to overwrite them. */
2d112de7
ACO
11415 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
11416 CRTC_STEREO_DOUBLE);
135c81b8 11417
7758a113
DV
11418 /* Pass our mode to the connectors and the CRTC to give them a chance to
11419 * adjust it according to limitations or connector properties, and also
11420 * a chance to reject the mode entirely.
47f1c6c9 11421 */
da3ced29 11422 for_each_connector_in_state(state, connector, connector_state, i) {
0b901879 11423 if (connector_state->crtc != crtc)
7758a113 11424 continue;
7ae89233 11425
0b901879
ACO
11426 encoder = to_intel_encoder(connector_state->best_encoder);
11427
0a478c27 11428 if (!(encoder->compute_config(encoder, pipe_config, connector_state))) {
efea6e8e 11429 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
DV
11430 goto fail;
11431 }
ee7b9f93 11432 }
47f1c6c9 11433
ff9a6750
DV
11434 /* Set default port clock if not overwritten by the encoder. Needs to be
11435 * done afterwards in case the encoder adjusts the mode. */
11436 if (!pipe_config->port_clock)
2d112de7 11437 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
241bfc38 11438 * pipe_config->pixel_multiplier;
ff9a6750 11439
a43f6e0f 11440 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 11441 if (ret < 0) {
7758a113
DV
11442 DRM_DEBUG_KMS("CRTC fixup failed\n");
11443 goto fail;
ee7b9f93 11444 }
e29c22c0
DV
11445
11446 if (ret == RETRY) {
11447 if (WARN(!retry, "loop in pipe configuration computation\n")) {
11448 ret = -EINVAL;
11449 goto fail;
11450 }
11451
11452 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
11453 retry = false;
11454 goto encoder_retry;
11455 }
11456
e8fa4270 11457 /* Dithering seems to not pass-through bits correctly when it should, so
611032bf
MN
11458 * only enable it on 6bpc panels and when its not a compliance
11459 * test requesting 6bpc video pattern.
11460 */
11461 pipe_config->dither = (pipe_config->pipe_bpp == 6*3) &&
11462 !pipe_config->dither_force_disable;
62f0ace5 11463 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
d328c9d7 11464 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
4e53c2e0 11465
7758a113 11466fail:
548ee15b 11467 return ret;
ee7b9f93 11468}
47f1c6c9 11469
ea9d758d 11470static void
4740b0f2 11471intel_modeset_update_crtc_state(struct drm_atomic_state *state)
ea9d758d 11472{
0a9ab303
ACO
11473 struct drm_crtc *crtc;
11474 struct drm_crtc_state *crtc_state;
8a75d157 11475 int i;
ea9d758d 11476
7668851f 11477 /* Double check state. */
8a75d157 11478 for_each_crtc_in_state(state, crtc, crtc_state, i) {
3cb480bc 11479 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
fc467a22
ML
11480
11481 /* Update hwmode for vblank functions */
11482 if (crtc->state->active)
11483 crtc->hwmode = crtc->state->adjusted_mode;
11484 else
11485 crtc->hwmode.crtc_clock = 0;
61067a5e
ML
11486
11487 /*
11488 * Update legacy state to satisfy fbc code. This can
11489 * be removed when fbc uses the atomic state.
11490 */
11491 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
11492 struct drm_plane_state *plane_state = crtc->primary->state;
11493
11494 crtc->primary->fb = plane_state->fb;
11495 crtc->x = plane_state->src_x >> 16;
11496 crtc->y = plane_state->src_y >> 16;
11497 }
ea9d758d 11498 }
ea9d758d
DV
11499}
11500
3bd26263 11501static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 11502{
3bd26263 11503 int diff;
f1f644dc
JB
11504
11505 if (clock1 == clock2)
11506 return true;
11507
11508 if (!clock1 || !clock2)
11509 return false;
11510
11511 diff = abs(clock1 - clock2);
11512
11513 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
11514 return true;
11515
11516 return false;
11517}
11518
cfb23ed6
ML
11519static bool
11520intel_compare_m_n(unsigned int m, unsigned int n,
11521 unsigned int m2, unsigned int n2,
11522 bool exact)
11523{
11524 if (m == m2 && n == n2)
11525 return true;
11526
11527 if (exact || !m || !n || !m2 || !n2)
11528 return false;
11529
11530 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
11531
31d10b57
ML
11532 if (n > n2) {
11533 while (n > n2) {
cfb23ed6
ML
11534 m2 <<= 1;
11535 n2 <<= 1;
11536 }
31d10b57
ML
11537 } else if (n < n2) {
11538 while (n < n2) {
cfb23ed6
ML
11539 m <<= 1;
11540 n <<= 1;
11541 }
11542 }
11543
31d10b57
ML
11544 if (n != n2)
11545 return false;
11546
11547 return intel_fuzzy_clock_check(m, m2);
cfb23ed6
ML
11548}
11549
11550static bool
11551intel_compare_link_m_n(const struct intel_link_m_n *m_n,
11552 struct intel_link_m_n *m2_n2,
11553 bool adjust)
11554{
11555 if (m_n->tu == m2_n2->tu &&
11556 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
11557 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
11558 intel_compare_m_n(m_n->link_m, m_n->link_n,
11559 m2_n2->link_m, m2_n2->link_n, !adjust)) {
11560 if (adjust)
11561 *m2_n2 = *m_n;
11562
11563 return true;
11564 }
11565
11566 return false;
11567}
11568
4e8048f8
TU
11569static void __printf(3, 4)
11570pipe_config_err(bool adjust, const char *name, const char *format, ...)
11571{
11572 char *level;
11573 unsigned int category;
11574 struct va_format vaf;
11575 va_list args;
11576
11577 if (adjust) {
11578 level = KERN_DEBUG;
11579 category = DRM_UT_KMS;
11580 } else {
11581 level = KERN_ERR;
11582 category = DRM_UT_NONE;
11583 }
11584
11585 va_start(args, format);
11586 vaf.fmt = format;
11587 vaf.va = &args;
11588
11589 drm_printk(level, category, "mismatch in %s %pV", name, &vaf);
11590
11591 va_end(args);
11592}
11593
0e8ffe1b 11594static bool
6315b5d3 11595intel_pipe_config_compare(struct drm_i915_private *dev_priv,
5cec258b 11596 struct intel_crtc_state *current_config,
cfb23ed6
ML
11597 struct intel_crtc_state *pipe_config,
11598 bool adjust)
0e8ffe1b 11599{
cfb23ed6
ML
11600 bool ret = true;
11601
66e985c0
DV
11602#define PIPE_CONF_CHECK_X(name) \
11603 if (current_config->name != pipe_config->name) { \
4e8048f8 11604 pipe_config_err(adjust, __stringify(name), \
66e985c0
DV
11605 "(expected 0x%08x, found 0x%08x)\n", \
11606 current_config->name, \
11607 pipe_config->name); \
cfb23ed6 11608 ret = false; \
66e985c0
DV
11609 }
11610
08a24034
DV
11611#define PIPE_CONF_CHECK_I(name) \
11612 if (current_config->name != pipe_config->name) { \
4e8048f8 11613 pipe_config_err(adjust, __stringify(name), \
08a24034
DV
11614 "(expected %i, found %i)\n", \
11615 current_config->name, \
11616 pipe_config->name); \
cfb23ed6
ML
11617 ret = false; \
11618 }
11619
8106ddbd
ACO
11620#define PIPE_CONF_CHECK_P(name) \
11621 if (current_config->name != pipe_config->name) { \
4e8048f8 11622 pipe_config_err(adjust, __stringify(name), \
8106ddbd
ACO
11623 "(expected %p, found %p)\n", \
11624 current_config->name, \
11625 pipe_config->name); \
11626 ret = false; \
11627 }
11628
cfb23ed6
ML
11629#define PIPE_CONF_CHECK_M_N(name) \
11630 if (!intel_compare_link_m_n(&current_config->name, \
11631 &pipe_config->name,\
11632 adjust)) { \
4e8048f8 11633 pipe_config_err(adjust, __stringify(name), \
cfb23ed6
ML
11634 "(expected tu %i gmch %i/%i link %i/%i, " \
11635 "found tu %i, gmch %i/%i link %i/%i)\n", \
11636 current_config->name.tu, \
11637 current_config->name.gmch_m, \
11638 current_config->name.gmch_n, \
11639 current_config->name.link_m, \
11640 current_config->name.link_n, \
11641 pipe_config->name.tu, \
11642 pipe_config->name.gmch_m, \
11643 pipe_config->name.gmch_n, \
11644 pipe_config->name.link_m, \
11645 pipe_config->name.link_n); \
11646 ret = false; \
11647 }
11648
55c561a7
DV
11649/* This is required for BDW+ where there is only one set of registers for
11650 * switching between high and low RR.
11651 * This macro can be used whenever a comparison has to be made between one
11652 * hw state and multiple sw state variables.
11653 */
cfb23ed6
ML
11654#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
11655 if (!intel_compare_link_m_n(&current_config->name, \
11656 &pipe_config->name, adjust) && \
11657 !intel_compare_link_m_n(&current_config->alt_name, \
11658 &pipe_config->name, adjust)) { \
4e8048f8 11659 pipe_config_err(adjust, __stringify(name), \
cfb23ed6
ML
11660 "(expected tu %i gmch %i/%i link %i/%i, " \
11661 "or tu %i gmch %i/%i link %i/%i, " \
11662 "found tu %i, gmch %i/%i link %i/%i)\n", \
11663 current_config->name.tu, \
11664 current_config->name.gmch_m, \
11665 current_config->name.gmch_n, \
11666 current_config->name.link_m, \
11667 current_config->name.link_n, \
11668 current_config->alt_name.tu, \
11669 current_config->alt_name.gmch_m, \
11670 current_config->alt_name.gmch_n, \
11671 current_config->alt_name.link_m, \
11672 current_config->alt_name.link_n, \
11673 pipe_config->name.tu, \
11674 pipe_config->name.gmch_m, \
11675 pipe_config->name.gmch_n, \
11676 pipe_config->name.link_m, \
11677 pipe_config->name.link_n); \
11678 ret = false; \
88adfff1
DV
11679 }
11680
1bd1bd80
DV
11681#define PIPE_CONF_CHECK_FLAGS(name, mask) \
11682 if ((current_config->name ^ pipe_config->name) & (mask)) { \
4e8048f8
TU
11683 pipe_config_err(adjust, __stringify(name), \
11684 "(%x) (expected %i, found %i)\n", \
11685 (mask), \
1bd1bd80
DV
11686 current_config->name & (mask), \
11687 pipe_config->name & (mask)); \
cfb23ed6 11688 ret = false; \
1bd1bd80
DV
11689 }
11690
5e550656
VS
11691#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
11692 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
4e8048f8 11693 pipe_config_err(adjust, __stringify(name), \
5e550656
VS
11694 "(expected %i, found %i)\n", \
11695 current_config->name, \
11696 pipe_config->name); \
cfb23ed6 11697 ret = false; \
5e550656
VS
11698 }
11699
bb760063
DV
11700#define PIPE_CONF_QUIRK(quirk) \
11701 ((current_config->quirks | pipe_config->quirks) & (quirk))
11702
eccb140b
DV
11703 PIPE_CONF_CHECK_I(cpu_transcoder);
11704
08a24034
DV
11705 PIPE_CONF_CHECK_I(has_pch_encoder);
11706 PIPE_CONF_CHECK_I(fdi_lanes);
cfb23ed6 11707 PIPE_CONF_CHECK_M_N(fdi_m_n);
08a24034 11708
90a6b7b0 11709 PIPE_CONF_CHECK_I(lane_count);
95a7a2ae 11710 PIPE_CONF_CHECK_X(lane_lat_optim_mask);
b95af8be 11711
6315b5d3 11712 if (INTEL_GEN(dev_priv) < 8) {
cfb23ed6
ML
11713 PIPE_CONF_CHECK_M_N(dp_m_n);
11714
cfb23ed6
ML
11715 if (current_config->has_drrs)
11716 PIPE_CONF_CHECK_M_N(dp_m2_n2);
11717 } else
11718 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
eb14cb74 11719
253c84c8 11720 PIPE_CONF_CHECK_X(output_types);
a65347ba 11721
2d112de7
ACO
11722 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
11723 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
11724 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
11725 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
11726 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
11727 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
1bd1bd80 11728
2d112de7
ACO
11729 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
11730 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
11731 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
11732 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
11733 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
11734 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
1bd1bd80 11735
c93f54cf 11736 PIPE_CONF_CHECK_I(pixel_multiplier);
6897b4b5 11737 PIPE_CONF_CHECK_I(has_hdmi_sink);
772c2a51 11738 if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
920a14b2 11739 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
b5a9fa09 11740 PIPE_CONF_CHECK_I(limited_color_range);
e43823ec 11741 PIPE_CONF_CHECK_I(has_infoframe);
6c49f241 11742
9ed109a7
DV
11743 PIPE_CONF_CHECK_I(has_audio);
11744
2d112de7 11745 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
1bd1bd80
DV
11746 DRM_MODE_FLAG_INTERLACE);
11747
bb760063 11748 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
2d112de7 11749 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 11750 DRM_MODE_FLAG_PHSYNC);
2d112de7 11751 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 11752 DRM_MODE_FLAG_NHSYNC);
2d112de7 11753 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 11754 DRM_MODE_FLAG_PVSYNC);
2d112de7 11755 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063
DV
11756 DRM_MODE_FLAG_NVSYNC);
11757 }
045ac3b5 11758
333b8ca8 11759 PIPE_CONF_CHECK_X(gmch_pfit.control);
e2ff2d4a 11760 /* pfit ratios are autocomputed by the hw on gen4+ */
6315b5d3 11761 if (INTEL_GEN(dev_priv) < 4)
7f7d8dd6 11762 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
333b8ca8 11763 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
9953599b 11764
bfd16b2a
ML
11765 if (!adjust) {
11766 PIPE_CONF_CHECK_I(pipe_src_w);
11767 PIPE_CONF_CHECK_I(pipe_src_h);
11768
11769 PIPE_CONF_CHECK_I(pch_pfit.enabled);
11770 if (current_config->pch_pfit.enabled) {
11771 PIPE_CONF_CHECK_X(pch_pfit.pos);
11772 PIPE_CONF_CHECK_X(pch_pfit.size);
11773 }
2fa2fe9a 11774
7aefe2b5 11775 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
a7d1b3f4 11776 PIPE_CONF_CHECK_CLOCK_FUZZY(pixel_rate);
7aefe2b5 11777 }
a1b2278e 11778
e59150dc 11779 /* BDW+ don't expose a synchronous way to read the state */
772c2a51 11780 if (IS_HASWELL(dev_priv))
e59150dc 11781 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 11782
282740f7
VS
11783 PIPE_CONF_CHECK_I(double_wide);
11784
8106ddbd 11785 PIPE_CONF_CHECK_P(shared_dpll);
66e985c0 11786 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 11787 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
DV
11788 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
11789 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
d452c5b6 11790 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
00490c22 11791 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
3f4cd19f
DL
11792 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
11793 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
11794 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
c0d43d62 11795
47eacbab
VS
11796 PIPE_CONF_CHECK_X(dsi_pll.ctrl);
11797 PIPE_CONF_CHECK_X(dsi_pll.div);
11798
9beb5fea 11799 if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
42571aef
VS
11800 PIPE_CONF_CHECK_I(pipe_bpp);
11801
2d112de7 11802 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
a9a7e98a 11803 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 11804
66e985c0 11805#undef PIPE_CONF_CHECK_X
08a24034 11806#undef PIPE_CONF_CHECK_I
8106ddbd 11807#undef PIPE_CONF_CHECK_P
1bd1bd80 11808#undef PIPE_CONF_CHECK_FLAGS
5e550656 11809#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 11810#undef PIPE_CONF_QUIRK
88adfff1 11811
cfb23ed6 11812 return ret;
0e8ffe1b
DV
11813}
11814
e3b247da
VS
11815static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
11816 const struct intel_crtc_state *pipe_config)
11817{
11818 if (pipe_config->has_pch_encoder) {
21a727b3 11819 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
e3b247da
VS
11820 &pipe_config->fdi_m_n);
11821 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
11822
11823 /*
11824 * FDI already provided one idea for the dotclock.
11825 * Yell if the encoder disagrees.
11826 */
11827 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
11828 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
11829 fdi_dotclock, dotclock);
11830 }
11831}
11832
c0ead703
ML
11833static void verify_wm_state(struct drm_crtc *crtc,
11834 struct drm_crtc_state *new_state)
08db6652 11835{
6315b5d3 11836 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
08db6652 11837 struct skl_ddb_allocation hw_ddb, *sw_ddb;
3de8a14c 11838 struct skl_pipe_wm hw_wm, *sw_wm;
11839 struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
11840 struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
e7c84544
ML
11841 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11842 const enum pipe pipe = intel_crtc->pipe;
3de8a14c 11843 int plane, level, max_level = ilk_wm_max_level(dev_priv);
08db6652 11844
6315b5d3 11845 if (INTEL_GEN(dev_priv) < 9 || !new_state->active)
08db6652
DL
11846 return;
11847
3de8a14c 11848 skl_pipe_wm_get_hw_state(crtc, &hw_wm);
03af79e0 11849 sw_wm = &to_intel_crtc_state(new_state)->wm.skl.optimal;
3de8a14c 11850
08db6652
DL
11851 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
11852 sw_ddb = &dev_priv->wm.skl_hw.ddb;
11853
e7c84544 11854 /* planes */
8b364b41 11855 for_each_universal_plane(dev_priv, pipe, plane) {
3de8a14c 11856 hw_plane_wm = &hw_wm.planes[plane];
11857 sw_plane_wm = &sw_wm->planes[plane];
08db6652 11858
3de8a14c 11859 /* Watermarks */
11860 for (level = 0; level <= max_level; level++) {
11861 if (skl_wm_level_equals(&hw_plane_wm->wm[level],
11862 &sw_plane_wm->wm[level]))
11863 continue;
11864
11865 DRM_ERROR("mismatch in WM pipe %c plane %d level %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
11866 pipe_name(pipe), plane + 1, level,
11867 sw_plane_wm->wm[level].plane_en,
11868 sw_plane_wm->wm[level].plane_res_b,
11869 sw_plane_wm->wm[level].plane_res_l,
11870 hw_plane_wm->wm[level].plane_en,
11871 hw_plane_wm->wm[level].plane_res_b,
11872 hw_plane_wm->wm[level].plane_res_l);
11873 }
08db6652 11874
3de8a14c 11875 if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
11876 &sw_plane_wm->trans_wm)) {
11877 DRM_ERROR("mismatch in trans WM pipe %c plane %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
11878 pipe_name(pipe), plane + 1,
11879 sw_plane_wm->trans_wm.plane_en,
11880 sw_plane_wm->trans_wm.plane_res_b,
11881 sw_plane_wm->trans_wm.plane_res_l,
11882 hw_plane_wm->trans_wm.plane_en,
11883 hw_plane_wm->trans_wm.plane_res_b,
11884 hw_plane_wm->trans_wm.plane_res_l);
11885 }
11886
11887 /* DDB */
11888 hw_ddb_entry = &hw_ddb.plane[pipe][plane];
11889 sw_ddb_entry = &sw_ddb->plane[pipe][plane];
11890
11891 if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
faccd994 11892 DRM_ERROR("mismatch in DDB state pipe %c plane %d (expected (%u,%u), found (%u,%u))\n",
3de8a14c 11893 pipe_name(pipe), plane + 1,
11894 sw_ddb_entry->start, sw_ddb_entry->end,
11895 hw_ddb_entry->start, hw_ddb_entry->end);
11896 }
e7c84544 11897 }
08db6652 11898
27082493
L
11899 /*
11900 * cursor
11901 * If the cursor plane isn't active, we may not have updated it's ddb
11902 * allocation. In that case since the ddb allocation will be updated
11903 * once the plane becomes visible, we can skip this check
11904 */
11905 if (intel_crtc->cursor_addr) {
3de8a14c 11906 hw_plane_wm = &hw_wm.planes[PLANE_CURSOR];
11907 sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
11908
11909 /* Watermarks */
11910 for (level = 0; level <= max_level; level++) {
11911 if (skl_wm_level_equals(&hw_plane_wm->wm[level],
11912 &sw_plane_wm->wm[level]))
11913 continue;
11914
11915 DRM_ERROR("mismatch in WM pipe %c cursor level %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
11916 pipe_name(pipe), level,
11917 sw_plane_wm->wm[level].plane_en,
11918 sw_plane_wm->wm[level].plane_res_b,
11919 sw_plane_wm->wm[level].plane_res_l,
11920 hw_plane_wm->wm[level].plane_en,
11921 hw_plane_wm->wm[level].plane_res_b,
11922 hw_plane_wm->wm[level].plane_res_l);
11923 }
11924
11925 if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
11926 &sw_plane_wm->trans_wm)) {
11927 DRM_ERROR("mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
11928 pipe_name(pipe),
11929 sw_plane_wm->trans_wm.plane_en,
11930 sw_plane_wm->trans_wm.plane_res_b,
11931 sw_plane_wm->trans_wm.plane_res_l,
11932 hw_plane_wm->trans_wm.plane_en,
11933 hw_plane_wm->trans_wm.plane_res_b,
11934 hw_plane_wm->trans_wm.plane_res_l);
11935 }
11936
11937 /* DDB */
11938 hw_ddb_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
11939 sw_ddb_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
27082493 11940
3de8a14c 11941 if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
faccd994 11942 DRM_ERROR("mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
27082493 11943 pipe_name(pipe),
3de8a14c 11944 sw_ddb_entry->start, sw_ddb_entry->end,
11945 hw_ddb_entry->start, hw_ddb_entry->end);
27082493 11946 }
08db6652
DL
11947 }
11948}
11949
91d1b4bd 11950static void
677100ce
ML
11951verify_connector_state(struct drm_device *dev,
11952 struct drm_atomic_state *state,
11953 struct drm_crtc *crtc)
8af6cf88 11954{
35dd3c64 11955 struct drm_connector *connector;
677100ce
ML
11956 struct drm_connector_state *old_conn_state;
11957 int i;
8af6cf88 11958
677100ce 11959 for_each_connector_in_state(state, connector, old_conn_state, i) {
35dd3c64
ML
11960 struct drm_encoder *encoder = connector->encoder;
11961 struct drm_connector_state *state = connector->state;
ad3c558f 11962
e7c84544
ML
11963 if (state->crtc != crtc)
11964 continue;
11965
5a21b665 11966 intel_connector_verify_state(to_intel_connector(connector));
8af6cf88 11967
ad3c558f 11968 I915_STATE_WARN(state->best_encoder != encoder,
35dd3c64 11969 "connector's atomic encoder doesn't match legacy encoder\n");
8af6cf88 11970 }
91d1b4bd
DV
11971}
11972
11973static void
c0ead703 11974verify_encoder_state(struct drm_device *dev)
91d1b4bd
DV
11975{
11976 struct intel_encoder *encoder;
11977 struct intel_connector *connector;
8af6cf88 11978
b2784e15 11979 for_each_intel_encoder(dev, encoder) {
8af6cf88 11980 bool enabled = false;
4d20cd86 11981 enum pipe pipe;
8af6cf88
DV
11982
11983 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
11984 encoder->base.base.id,
8e329a03 11985 encoder->base.name);
8af6cf88 11986
3a3371ff 11987 for_each_intel_connector(dev, connector) {
4d20cd86 11988 if (connector->base.state->best_encoder != &encoder->base)
8af6cf88
DV
11989 continue;
11990 enabled = true;
ad3c558f
ML
11991
11992 I915_STATE_WARN(connector->base.state->crtc !=
11993 encoder->base.crtc,
11994 "connector's crtc doesn't match encoder crtc\n");
8af6cf88 11995 }
0e32b39c 11996
e2c719b7 11997 I915_STATE_WARN(!!encoder->base.crtc != enabled,
8af6cf88
DV
11998 "encoder's enabled state mismatch "
11999 "(expected %i, found %i)\n",
12000 !!encoder->base.crtc, enabled);
7c60d198
ML
12001
12002 if (!encoder->base.crtc) {
4d20cd86 12003 bool active;
7c60d198 12004
4d20cd86
ML
12005 active = encoder->get_hw_state(encoder, &pipe);
12006 I915_STATE_WARN(active,
12007 "encoder detached but still enabled on pipe %c.\n",
12008 pipe_name(pipe));
7c60d198 12009 }
8af6cf88 12010 }
91d1b4bd
DV
12011}
12012
12013static void
c0ead703
ML
12014verify_crtc_state(struct drm_crtc *crtc,
12015 struct drm_crtc_state *old_crtc_state,
12016 struct drm_crtc_state *new_crtc_state)
91d1b4bd 12017{
e7c84544 12018 struct drm_device *dev = crtc->dev;
fac5e23e 12019 struct drm_i915_private *dev_priv = to_i915(dev);
91d1b4bd 12020 struct intel_encoder *encoder;
e7c84544
ML
12021 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12022 struct intel_crtc_state *pipe_config, *sw_config;
12023 struct drm_atomic_state *old_state;
12024 bool active;
045ac3b5 12025
e7c84544 12026 old_state = old_crtc_state->state;
ec2dc6a0 12027 __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
e7c84544
ML
12028 pipe_config = to_intel_crtc_state(old_crtc_state);
12029 memset(pipe_config, 0, sizeof(*pipe_config));
12030 pipe_config->base.crtc = crtc;
12031 pipe_config->base.state = old_state;
8af6cf88 12032
78108b7c 12033 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
8af6cf88 12034
e7c84544 12035 active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
d62cf62a 12036
e7c84544
ML
12037 /* hw state is inconsistent with the pipe quirk */
12038 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12039 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
12040 active = new_crtc_state->active;
6c49f241 12041
e7c84544
ML
12042 I915_STATE_WARN(new_crtc_state->active != active,
12043 "crtc active state doesn't match with hw state "
12044 "(expected %i, found %i)\n", new_crtc_state->active, active);
0e8ffe1b 12045
e7c84544
ML
12046 I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
12047 "transitional active state does not match atomic hw state "
12048 "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
4d20cd86 12049
e7c84544
ML
12050 for_each_encoder_on_crtc(dev, crtc, encoder) {
12051 enum pipe pipe;
4d20cd86 12052
e7c84544
ML
12053 active = encoder->get_hw_state(encoder, &pipe);
12054 I915_STATE_WARN(active != new_crtc_state->active,
12055 "[ENCODER:%i] active %i with crtc active %i\n",
12056 encoder->base.base.id, active, new_crtc_state->active);
4d20cd86 12057
e7c84544
ML
12058 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
12059 "Encoder connected to wrong pipe %c\n",
12060 pipe_name(pipe));
4d20cd86 12061
253c84c8
VS
12062 if (active) {
12063 pipe_config->output_types |= 1 << encoder->type;
e7c84544 12064 encoder->get_config(encoder, pipe_config);
253c84c8 12065 }
e7c84544 12066 }
53d9f4e9 12067
a7d1b3f4
VS
12068 intel_crtc_compute_pixel_rate(pipe_config);
12069
e7c84544
ML
12070 if (!new_crtc_state->active)
12071 return;
cfb23ed6 12072
e7c84544 12073 intel_pipe_config_sanity_check(dev_priv, pipe_config);
e3b247da 12074
e7c84544 12075 sw_config = to_intel_crtc_state(crtc->state);
6315b5d3 12076 if (!intel_pipe_config_compare(dev_priv, sw_config,
e7c84544
ML
12077 pipe_config, false)) {
12078 I915_STATE_WARN(1, "pipe state doesn't match!\n");
12079 intel_dump_pipe_config(intel_crtc, pipe_config,
12080 "[hw state]");
12081 intel_dump_pipe_config(intel_crtc, sw_config,
12082 "[sw state]");
8af6cf88
DV
12083 }
12084}
12085
91d1b4bd 12086static void
c0ead703
ML
12087verify_single_dpll_state(struct drm_i915_private *dev_priv,
12088 struct intel_shared_dpll *pll,
12089 struct drm_crtc *crtc,
12090 struct drm_crtc_state *new_state)
91d1b4bd 12091{
91d1b4bd 12092 struct intel_dpll_hw_state dpll_hw_state;
e7c84544
ML
12093 unsigned crtc_mask;
12094 bool active;
5358901f 12095
e7c84544 12096 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
5358901f 12097
e7c84544 12098 DRM_DEBUG_KMS("%s\n", pll->name);
5358901f 12099
e7c84544 12100 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
5358901f 12101
e7c84544
ML
12102 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
12103 I915_STATE_WARN(!pll->on && pll->active_mask,
12104 "pll in active use but not on in sw tracking\n");
12105 I915_STATE_WARN(pll->on && !pll->active_mask,
12106 "pll is on but not used by any active crtc\n");
12107 I915_STATE_WARN(pll->on != active,
12108 "pll on state mismatch (expected %i, found %i)\n",
12109 pll->on, active);
12110 }
5358901f 12111
e7c84544 12112 if (!crtc) {
2c42e535 12113 I915_STATE_WARN(pll->active_mask & ~pll->state.crtc_mask,
e7c84544 12114 "more active pll users than references: %x vs %x\n",
2c42e535 12115 pll->active_mask, pll->state.crtc_mask);
5358901f 12116
e7c84544
ML
12117 return;
12118 }
12119
12120 crtc_mask = 1 << drm_crtc_index(crtc);
12121
12122 if (new_state->active)
12123 I915_STATE_WARN(!(pll->active_mask & crtc_mask),
12124 "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
12125 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
12126 else
12127 I915_STATE_WARN(pll->active_mask & crtc_mask,
12128 "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
12129 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
2dd66ebd 12130
2c42e535 12131 I915_STATE_WARN(!(pll->state.crtc_mask & crtc_mask),
e7c84544 12132 "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
2c42e535 12133 crtc_mask, pll->state.crtc_mask);
66e985c0 12134
2c42e535 12135 I915_STATE_WARN(pll->on && memcmp(&pll->state.hw_state,
e7c84544
ML
12136 &dpll_hw_state,
12137 sizeof(dpll_hw_state)),
12138 "pll hw state mismatch\n");
12139}
12140
12141static void
c0ead703
ML
12142verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
12143 struct drm_crtc_state *old_crtc_state,
12144 struct drm_crtc_state *new_crtc_state)
e7c84544 12145{
fac5e23e 12146 struct drm_i915_private *dev_priv = to_i915(dev);
e7c84544
ML
12147 struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
12148 struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
12149
12150 if (new_state->shared_dpll)
c0ead703 12151 verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
e7c84544
ML
12152
12153 if (old_state->shared_dpll &&
12154 old_state->shared_dpll != new_state->shared_dpll) {
12155 unsigned crtc_mask = 1 << drm_crtc_index(crtc);
12156 struct intel_shared_dpll *pll = old_state->shared_dpll;
12157
12158 I915_STATE_WARN(pll->active_mask & crtc_mask,
12159 "pll active mismatch (didn't expect pipe %c in active mask)\n",
12160 pipe_name(drm_crtc_index(crtc)));
2c42e535 12161 I915_STATE_WARN(pll->state.crtc_mask & crtc_mask,
e7c84544
ML
12162 "pll enabled crtcs mismatch (found %x in enabled mask)\n",
12163 pipe_name(drm_crtc_index(crtc)));
5358901f 12164 }
8af6cf88
DV
12165}
12166
e7c84544 12167static void
c0ead703 12168intel_modeset_verify_crtc(struct drm_crtc *crtc,
677100ce
ML
12169 struct drm_atomic_state *state,
12170 struct drm_crtc_state *old_state,
12171 struct drm_crtc_state *new_state)
e7c84544 12172{
5a21b665
DV
12173 if (!needs_modeset(new_state) &&
12174 !to_intel_crtc_state(new_state)->update_pipe)
12175 return;
12176
c0ead703 12177 verify_wm_state(crtc, new_state);
677100ce 12178 verify_connector_state(crtc->dev, state, crtc);
c0ead703
ML
12179 verify_crtc_state(crtc, old_state, new_state);
12180 verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
e7c84544
ML
12181}
12182
12183static void
c0ead703 12184verify_disabled_dpll_state(struct drm_device *dev)
e7c84544 12185{
fac5e23e 12186 struct drm_i915_private *dev_priv = to_i915(dev);
e7c84544
ML
12187 int i;
12188
12189 for (i = 0; i < dev_priv->num_shared_dpll; i++)
c0ead703 12190 verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
e7c84544
ML
12191}
12192
12193static void
677100ce
ML
12194intel_modeset_verify_disabled(struct drm_device *dev,
12195 struct drm_atomic_state *state)
e7c84544 12196{
c0ead703 12197 verify_encoder_state(dev);
677100ce 12198 verify_connector_state(dev, state, NULL);
c0ead703 12199 verify_disabled_dpll_state(dev);
e7c84544
ML
12200}
12201
80715b2f
VS
12202static void update_scanline_offset(struct intel_crtc *crtc)
12203{
4f8036a2 12204 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
80715b2f
VS
12205
12206 /*
12207 * The scanline counter increments at the leading edge of hsync.
12208 *
12209 * On most platforms it starts counting from vtotal-1 on the
12210 * first active line. That means the scanline counter value is
12211 * always one less than what we would expect. Ie. just after
12212 * start of vblank, which also occurs at start of hsync (on the
12213 * last active line), the scanline counter will read vblank_start-1.
12214 *
12215 * On gen2 the scanline counter starts counting from 1 instead
12216 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12217 * to keep the value positive), instead of adding one.
12218 *
12219 * On HSW+ the behaviour of the scanline counter depends on the output
12220 * type. For DP ports it behaves like most other platforms, but on HDMI
12221 * there's an extra 1 line difference. So we need to add two instead of
12222 * one to the value.
12223 */
4f8036a2 12224 if (IS_GEN2(dev_priv)) {
124abe07 12225 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
80715b2f
VS
12226 int vtotal;
12227
124abe07
VS
12228 vtotal = adjusted_mode->crtc_vtotal;
12229 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
80715b2f
VS
12230 vtotal /= 2;
12231
12232 crtc->scanline_offset = vtotal - 1;
4f8036a2 12233 } else if (HAS_DDI(dev_priv) &&
2d84d2b3 12234 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
80715b2f
VS
12235 crtc->scanline_offset = 2;
12236 } else
12237 crtc->scanline_offset = 1;
12238}
12239
ad421372 12240static void intel_modeset_clear_plls(struct drm_atomic_state *state)
ed6739ef 12241{
225da59b 12242 struct drm_device *dev = state->dev;
ed6739ef 12243 struct drm_i915_private *dev_priv = to_i915(dev);
0a9ab303
ACO
12244 struct drm_crtc *crtc;
12245 struct drm_crtc_state *crtc_state;
0a9ab303 12246 int i;
ed6739ef
ACO
12247
12248 if (!dev_priv->display.crtc_compute_clock)
ad421372 12249 return;
ed6739ef 12250
0a9ab303 12251 for_each_crtc_in_state(state, crtc, crtc_state, i) {
fb1a38a9 12252 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8106ddbd
ACO
12253 struct intel_shared_dpll *old_dpll =
12254 to_intel_crtc_state(crtc->state)->shared_dpll;
0a9ab303 12255
fb1a38a9 12256 if (!needs_modeset(crtc_state))
225da59b
ACO
12257 continue;
12258
8106ddbd 12259 to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
fb1a38a9 12260
8106ddbd 12261 if (!old_dpll)
fb1a38a9 12262 continue;
0a9ab303 12263
a1c414ee 12264 intel_release_shared_dpll(old_dpll, intel_crtc, state);
ad421372 12265 }
ed6739ef
ACO
12266}
12267
99d736a2
ML
12268/*
12269 * This implements the workaround described in the "notes" section of the mode
12270 * set sequence documentation. When going from no pipes or single pipe to
12271 * multiple pipes, and planes are enabled after the pipe, we need to wait at
12272 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
12273 */
12274static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
12275{
12276 struct drm_crtc_state *crtc_state;
12277 struct intel_crtc *intel_crtc;
12278 struct drm_crtc *crtc;
12279 struct intel_crtc_state *first_crtc_state = NULL;
12280 struct intel_crtc_state *other_crtc_state = NULL;
12281 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
12282 int i;
12283
12284 /* look at all crtc's that are going to be enabled in during modeset */
12285 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12286 intel_crtc = to_intel_crtc(crtc);
12287
12288 if (!crtc_state->active || !needs_modeset(crtc_state))
12289 continue;
12290
12291 if (first_crtc_state) {
12292 other_crtc_state = to_intel_crtc_state(crtc_state);
12293 break;
12294 } else {
12295 first_crtc_state = to_intel_crtc_state(crtc_state);
12296 first_pipe = intel_crtc->pipe;
12297 }
12298 }
12299
12300 /* No workaround needed? */
12301 if (!first_crtc_state)
12302 return 0;
12303
12304 /* w/a possibly needed, check how many crtc's are already enabled. */
12305 for_each_intel_crtc(state->dev, intel_crtc) {
12306 struct intel_crtc_state *pipe_config;
12307
12308 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
12309 if (IS_ERR(pipe_config))
12310 return PTR_ERR(pipe_config);
12311
12312 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
12313
12314 if (!pipe_config->base.active ||
12315 needs_modeset(&pipe_config->base))
12316 continue;
12317
12318 /* 2 or more enabled crtcs means no need for w/a */
12319 if (enabled_pipe != INVALID_PIPE)
12320 return 0;
12321
12322 enabled_pipe = intel_crtc->pipe;
12323 }
12324
12325 if (enabled_pipe != INVALID_PIPE)
12326 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
12327 else if (other_crtc_state)
12328 other_crtc_state->hsw_workaround_pipe = first_pipe;
12329
12330 return 0;
12331}
12332
8d96561a
VS
12333static int intel_lock_all_pipes(struct drm_atomic_state *state)
12334{
12335 struct drm_crtc *crtc;
12336
12337 /* Add all pipes to the state */
12338 for_each_crtc(state->dev, crtc) {
12339 struct drm_crtc_state *crtc_state;
12340
12341 crtc_state = drm_atomic_get_crtc_state(state, crtc);
12342 if (IS_ERR(crtc_state))
12343 return PTR_ERR(crtc_state);
12344 }
12345
12346 return 0;
12347}
12348
27c329ed
ML
12349static int intel_modeset_all_pipes(struct drm_atomic_state *state)
12350{
12351 struct drm_crtc *crtc;
27c329ed 12352
8d96561a
VS
12353 /*
12354 * Add all pipes to the state, and force
12355 * a modeset on all the active ones.
12356 */
27c329ed 12357 for_each_crtc(state->dev, crtc) {
9780aad5
VS
12358 struct drm_crtc_state *crtc_state;
12359 int ret;
12360
27c329ed
ML
12361 crtc_state = drm_atomic_get_crtc_state(state, crtc);
12362 if (IS_ERR(crtc_state))
12363 return PTR_ERR(crtc_state);
12364
12365 if (!crtc_state->active || needs_modeset(crtc_state))
12366 continue;
12367
12368 crtc_state->mode_changed = true;
12369
12370 ret = drm_atomic_add_affected_connectors(state, crtc);
12371 if (ret)
9780aad5 12372 return ret;
27c329ed
ML
12373
12374 ret = drm_atomic_add_affected_planes(state, crtc);
12375 if (ret)
9780aad5 12376 return ret;
27c329ed
ML
12377 }
12378
9780aad5 12379 return 0;
27c329ed
ML
12380}
12381
c347a676 12382static int intel_modeset_checks(struct drm_atomic_state *state)
054518dd 12383{
565602d7 12384 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 12385 struct drm_i915_private *dev_priv = to_i915(state->dev);
565602d7
ML
12386 struct drm_crtc *crtc;
12387 struct drm_crtc_state *crtc_state;
12388 int ret = 0, i;
054518dd 12389
b359283a
ML
12390 if (!check_digital_port_conflicts(state)) {
12391 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
12392 return -EINVAL;
12393 }
12394
565602d7
ML
12395 intel_state->modeset = true;
12396 intel_state->active_crtcs = dev_priv->active_crtcs;
bb0f4aab
VS
12397 intel_state->cdclk.logical = dev_priv->cdclk.logical;
12398 intel_state->cdclk.actual = dev_priv->cdclk.actual;
565602d7
ML
12399
12400 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12401 if (crtc_state->active)
12402 intel_state->active_crtcs |= 1 << i;
12403 else
12404 intel_state->active_crtcs &= ~(1 << i);
8b4a7d05
MR
12405
12406 if (crtc_state->active != crtc->state->active)
12407 intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
565602d7
ML
12408 }
12409
054518dd
ACO
12410 /*
12411 * See if the config requires any additional preparation, e.g.
12412 * to adjust global state with pipes off. We need to do this
12413 * here so we can get the modeset_pipe updated config for the new
12414 * mode set on this crtc. For other crtcs we need to use the
12415 * adjusted_mode bits in the crtc directly.
12416 */
27c329ed 12417 if (dev_priv->display.modeset_calc_cdclk) {
27c329ed 12418 ret = dev_priv->display.modeset_calc_cdclk(state);
c89e39f3
CT
12419 if (ret < 0)
12420 return ret;
27c329ed 12421
8d96561a 12422 /*
bb0f4aab 12423 * Writes to dev_priv->cdclk.logical must protected by
8d96561a
VS
12424 * holding all the crtc locks, even if we don't end up
12425 * touching the hardware
12426 */
bb0f4aab
VS
12427 if (!intel_cdclk_state_compare(&dev_priv->cdclk.logical,
12428 &intel_state->cdclk.logical)) {
8d96561a
VS
12429 ret = intel_lock_all_pipes(state);
12430 if (ret < 0)
12431 return ret;
12432 }
12433
12434 /* All pipes must be switched off while we change the cdclk. */
bb0f4aab
VS
12435 if (!intel_cdclk_state_compare(&dev_priv->cdclk.actual,
12436 &intel_state->cdclk.actual)) {
27c329ed 12437 ret = intel_modeset_all_pipes(state);
8d96561a
VS
12438 if (ret < 0)
12439 return ret;
12440 }
e8788cbc 12441
bb0f4aab
VS
12442 DRM_DEBUG_KMS("New cdclk calculated to be logical %u kHz, actual %u kHz\n",
12443 intel_state->cdclk.logical.cdclk,
12444 intel_state->cdclk.actual.cdclk);
e0ca7a6b 12445 } else {
bb0f4aab 12446 to_intel_atomic_state(state)->cdclk.logical = dev_priv->cdclk.logical;
e0ca7a6b 12447 }
054518dd 12448
ad421372 12449 intel_modeset_clear_plls(state);
054518dd 12450
565602d7 12451 if (IS_HASWELL(dev_priv))
ad421372 12452 return haswell_mode_set_planes_workaround(state);
99d736a2 12453
ad421372 12454 return 0;
c347a676
ACO
12455}
12456
aa363136
MR
12457/*
12458 * Handle calculation of various watermark data at the end of the atomic check
12459 * phase. The code here should be run after the per-crtc and per-plane 'check'
12460 * handlers to ensure that all derived state has been updated.
12461 */
55994c2c 12462static int calc_watermark_data(struct drm_atomic_state *state)
aa363136
MR
12463{
12464 struct drm_device *dev = state->dev;
98d39494 12465 struct drm_i915_private *dev_priv = to_i915(dev);
98d39494
MR
12466
12467 /* Is there platform-specific watermark information to calculate? */
12468 if (dev_priv->display.compute_global_watermarks)
55994c2c
MR
12469 return dev_priv->display.compute_global_watermarks(state);
12470
12471 return 0;
aa363136
MR
12472}
12473
74c090b1
ML
12474/**
12475 * intel_atomic_check - validate state object
12476 * @dev: drm device
12477 * @state: state to validate
12478 */
12479static int intel_atomic_check(struct drm_device *dev,
12480 struct drm_atomic_state *state)
c347a676 12481{
dd8b3bdb 12482 struct drm_i915_private *dev_priv = to_i915(dev);
aa363136 12483 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
c347a676
ACO
12484 struct drm_crtc *crtc;
12485 struct drm_crtc_state *crtc_state;
12486 int ret, i;
61333b60 12487 bool any_ms = false;
c347a676 12488
74c090b1 12489 ret = drm_atomic_helper_check_modeset(dev, state);
054518dd
ACO
12490 if (ret)
12491 return ret;
12492
c347a676 12493 for_each_crtc_in_state(state, crtc, crtc_state, i) {
cfb23ed6
ML
12494 struct intel_crtc_state *pipe_config =
12495 to_intel_crtc_state(crtc_state);
1ed51de9
DV
12496
12497 /* Catch I915_MODE_FLAG_INHERITED */
12498 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
12499 crtc_state->mode_changed = true;
cfb23ed6 12500
af4a879e 12501 if (!needs_modeset(crtc_state))
c347a676
ACO
12502 continue;
12503
af4a879e
DV
12504 if (!crtc_state->enable) {
12505 any_ms = true;
cfb23ed6 12506 continue;
af4a879e 12507 }
cfb23ed6 12508
26495481
DV
12509 /* FIXME: For only active_changed we shouldn't need to do any
12510 * state recomputation at all. */
12511
1ed51de9
DV
12512 ret = drm_atomic_add_affected_connectors(state, crtc);
12513 if (ret)
12514 return ret;
b359283a 12515
cfb23ed6 12516 ret = intel_modeset_pipe_config(crtc, pipe_config);
25aa1c39
ML
12517 if (ret) {
12518 intel_dump_pipe_config(to_intel_crtc(crtc),
12519 pipe_config, "[failed]");
c347a676 12520 return ret;
25aa1c39 12521 }
c347a676 12522
73831236 12523 if (i915.fastboot &&
6315b5d3 12524 intel_pipe_config_compare(dev_priv,
cfb23ed6 12525 to_intel_crtc_state(crtc->state),
1ed51de9 12526 pipe_config, true)) {
26495481 12527 crtc_state->mode_changed = false;
bfd16b2a 12528 to_intel_crtc_state(crtc_state)->update_pipe = true;
26495481
DV
12529 }
12530
af4a879e 12531 if (needs_modeset(crtc_state))
26495481 12532 any_ms = true;
cfb23ed6 12533
af4a879e
DV
12534 ret = drm_atomic_add_affected_planes(state, crtc);
12535 if (ret)
12536 return ret;
61333b60 12537
26495481
DV
12538 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
12539 needs_modeset(crtc_state) ?
12540 "[modeset]" : "[fastset]");
c347a676
ACO
12541 }
12542
61333b60
ML
12543 if (any_ms) {
12544 ret = intel_modeset_checks(state);
12545
12546 if (ret)
12547 return ret;
e0ca7a6b 12548 } else {
bb0f4aab 12549 intel_state->cdclk.logical = dev_priv->cdclk.logical;
e0ca7a6b 12550 }
76305b1a 12551
dd8b3bdb 12552 ret = drm_atomic_helper_check_planes(dev, state);
aa363136
MR
12553 if (ret)
12554 return ret;
12555
f51be2e0 12556 intel_fbc_choose_crtc(dev_priv, state);
55994c2c 12557 return calc_watermark_data(state);
054518dd
ACO
12558}
12559
5008e874 12560static int intel_atomic_prepare_commit(struct drm_device *dev,
d07f0e59 12561 struct drm_atomic_state *state)
5008e874 12562{
fac5e23e 12563 struct drm_i915_private *dev_priv = to_i915(dev);
5008e874
ML
12564 struct drm_crtc_state *crtc_state;
12565 struct drm_crtc *crtc;
12566 int i, ret;
12567
5a21b665
DV
12568 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12569 if (state->legacy_cursor_update)
a6747b73
ML
12570 continue;
12571
5a21b665
DV
12572 ret = intel_crtc_wait_for_pending_flips(crtc);
12573 if (ret)
12574 return ret;
5008e874 12575
5a21b665
DV
12576 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
12577 flush_workqueue(dev_priv->wq);
d55dbd06
ML
12578 }
12579
f935675f
ML
12580 ret = mutex_lock_interruptible(&dev->struct_mutex);
12581 if (ret)
12582 return ret;
12583
5008e874 12584 ret = drm_atomic_helper_prepare_planes(dev, state);
f7e5838b 12585 mutex_unlock(&dev->struct_mutex);
7580d774 12586
5008e874
ML
12587 return ret;
12588}
12589
a2991414
ML
12590u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
12591{
12592 struct drm_device *dev = crtc->base.dev;
12593
12594 if (!dev->max_vblank_count)
12595 return drm_accurate_vblank_count(&crtc->base);
12596
12597 return dev->driver->get_vblank_counter(dev, crtc->pipe);
12598}
12599
5a21b665
DV
12600static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
12601 struct drm_i915_private *dev_priv,
12602 unsigned crtc_mask)
e8861675 12603{
5a21b665
DV
12604 unsigned last_vblank_count[I915_MAX_PIPES];
12605 enum pipe pipe;
12606 int ret;
e8861675 12607
5a21b665
DV
12608 if (!crtc_mask)
12609 return;
e8861675 12610
5a21b665 12611 for_each_pipe(dev_priv, pipe) {
98187836
VS
12612 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
12613 pipe);
e8861675 12614
5a21b665 12615 if (!((1 << pipe) & crtc_mask))
e8861675
ML
12616 continue;
12617
e2af48c6 12618 ret = drm_crtc_vblank_get(&crtc->base);
5a21b665
DV
12619 if (WARN_ON(ret != 0)) {
12620 crtc_mask &= ~(1 << pipe);
12621 continue;
e8861675
ML
12622 }
12623
e2af48c6 12624 last_vblank_count[pipe] = drm_crtc_vblank_count(&crtc->base);
e8861675
ML
12625 }
12626
5a21b665 12627 for_each_pipe(dev_priv, pipe) {
98187836
VS
12628 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
12629 pipe);
5a21b665 12630 long lret;
e8861675 12631
5a21b665
DV
12632 if (!((1 << pipe) & crtc_mask))
12633 continue;
d55dbd06 12634
5a21b665
DV
12635 lret = wait_event_timeout(dev->vblank[pipe].queue,
12636 last_vblank_count[pipe] !=
e2af48c6 12637 drm_crtc_vblank_count(&crtc->base),
5a21b665 12638 msecs_to_jiffies(50));
d55dbd06 12639
5a21b665 12640 WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
d55dbd06 12641
e2af48c6 12642 drm_crtc_vblank_put(&crtc->base);
d55dbd06
ML
12643 }
12644}
12645
5a21b665 12646static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
a6747b73 12647{
5a21b665
DV
12648 /* fb updated, need to unpin old fb */
12649 if (crtc_state->fb_changed)
12650 return true;
a6747b73 12651
5a21b665
DV
12652 /* wm changes, need vblank before final wm's */
12653 if (crtc_state->update_wm_post)
12654 return true;
a6747b73 12655
5a21b665
DV
12656 /*
12657 * cxsr is re-enabled after vblank.
12658 * This is already handled by crtc_state->update_wm_post,
12659 * but added for clarity.
12660 */
12661 if (crtc_state->disable_cxsr)
12662 return true;
a6747b73 12663
5a21b665 12664 return false;
e8861675
ML
12665}
12666
896e5bb0
L
12667static void intel_update_crtc(struct drm_crtc *crtc,
12668 struct drm_atomic_state *state,
12669 struct drm_crtc_state *old_crtc_state,
12670 unsigned int *crtc_vblank_mask)
12671{
12672 struct drm_device *dev = crtc->dev;
12673 struct drm_i915_private *dev_priv = to_i915(dev);
12674 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12675 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc->state);
12676 bool modeset = needs_modeset(crtc->state);
12677
12678 if (modeset) {
12679 update_scanline_offset(intel_crtc);
12680 dev_priv->display.crtc_enable(pipe_config, state);
12681 } else {
12682 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
12683 }
12684
12685 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
12686 intel_fbc_enable(
12687 intel_crtc, pipe_config,
12688 to_intel_plane_state(crtc->primary->state));
12689 }
12690
12691 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
12692
12693 if (needs_vblank_wait(pipe_config))
12694 *crtc_vblank_mask |= drm_crtc_mask(crtc);
12695}
12696
12697static void intel_update_crtcs(struct drm_atomic_state *state,
12698 unsigned int *crtc_vblank_mask)
12699{
12700 struct drm_crtc *crtc;
12701 struct drm_crtc_state *old_crtc_state;
12702 int i;
12703
12704 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
12705 if (!crtc->state->active)
12706 continue;
12707
12708 intel_update_crtc(crtc, state, old_crtc_state,
12709 crtc_vblank_mask);
12710 }
12711}
12712
27082493
L
12713static void skl_update_crtcs(struct drm_atomic_state *state,
12714 unsigned int *crtc_vblank_mask)
12715{
0f0f74bc 12716 struct drm_i915_private *dev_priv = to_i915(state->dev);
27082493
L
12717 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
12718 struct drm_crtc *crtc;
ce0ba283 12719 struct intel_crtc *intel_crtc;
27082493 12720 struct drm_crtc_state *old_crtc_state;
ce0ba283 12721 struct intel_crtc_state *cstate;
27082493
L
12722 unsigned int updated = 0;
12723 bool progress;
12724 enum pipe pipe;
5eff503b
ML
12725 int i;
12726
12727 const struct skl_ddb_entry *entries[I915_MAX_PIPES] = {};
12728
12729 for_each_crtc_in_state(state, crtc, old_crtc_state, i)
12730 /* ignore allocations for crtc's that have been turned off. */
12731 if (crtc->state->active)
12732 entries[i] = &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb;
27082493
L
12733
12734 /*
12735 * Whenever the number of active pipes changes, we need to make sure we
12736 * update the pipes in the right order so that their ddb allocations
12737 * never overlap with eachother inbetween CRTC updates. Otherwise we'll
12738 * cause pipe underruns and other bad stuff.
12739 */
12740 do {
27082493
L
12741 progress = false;
12742
12743 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
12744 bool vbl_wait = false;
12745 unsigned int cmask = drm_crtc_mask(crtc);
ce0ba283
L
12746
12747 intel_crtc = to_intel_crtc(crtc);
12748 cstate = to_intel_crtc_state(crtc->state);
12749 pipe = intel_crtc->pipe;
27082493 12750
5eff503b 12751 if (updated & cmask || !cstate->base.active)
27082493 12752 continue;
5eff503b
ML
12753
12754 if (skl_ddb_allocation_overlaps(entries, &cstate->wm.skl.ddb, i))
27082493
L
12755 continue;
12756
12757 updated |= cmask;
5eff503b 12758 entries[i] = &cstate->wm.skl.ddb;
27082493
L
12759
12760 /*
12761 * If this is an already active pipe, it's DDB changed,
12762 * and this isn't the last pipe that needs updating
12763 * then we need to wait for a vblank to pass for the
12764 * new ddb allocation to take effect.
12765 */
ce0ba283 12766 if (!skl_ddb_entry_equal(&cstate->wm.skl.ddb,
512b5527 12767 &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb) &&
27082493
L
12768 !crtc->state->active_changed &&
12769 intel_state->wm_results.dirty_pipes != updated)
12770 vbl_wait = true;
12771
12772 intel_update_crtc(crtc, state, old_crtc_state,
12773 crtc_vblank_mask);
12774
12775 if (vbl_wait)
0f0f74bc 12776 intel_wait_for_vblank(dev_priv, pipe);
27082493
L
12777
12778 progress = true;
12779 }
12780 } while (progress);
12781}
12782
ba318c61
CW
12783static void intel_atomic_helper_free_state(struct drm_i915_private *dev_priv)
12784{
12785 struct intel_atomic_state *state, *next;
12786 struct llist_node *freed;
12787
12788 freed = llist_del_all(&dev_priv->atomic_helper.free_list);
12789 llist_for_each_entry_safe(state, next, freed, freed)
12790 drm_atomic_state_put(&state->base);
12791}
12792
12793static void intel_atomic_helper_free_state_worker(struct work_struct *work)
12794{
12795 struct drm_i915_private *dev_priv =
12796 container_of(work, typeof(*dev_priv), atomic_helper.free_work);
12797
12798 intel_atomic_helper_free_state(dev_priv);
12799}
12800
94f05024 12801static void intel_atomic_commit_tail(struct drm_atomic_state *state)
a6778b3c 12802{
94f05024 12803 struct drm_device *dev = state->dev;
565602d7 12804 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 12805 struct drm_i915_private *dev_priv = to_i915(dev);
29ceb0e6 12806 struct drm_crtc_state *old_crtc_state;
7580d774 12807 struct drm_crtc *crtc;
5a21b665 12808 struct intel_crtc_state *intel_cstate;
5a21b665 12809 bool hw_check = intel_state->modeset;
d8fc70b7 12810 u64 put_domains[I915_MAX_PIPES] = {};
5a21b665 12811 unsigned crtc_vblank_mask = 0;
e95433c7 12812 int i;
a6778b3c 12813
ea0000f0
DV
12814 drm_atomic_helper_wait_for_dependencies(state);
12815
c3b32658 12816 if (intel_state->modeset)
5a21b665 12817 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
565602d7 12818
29ceb0e6 12819 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
a539205a
ML
12820 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12821
5a21b665
DV
12822 if (needs_modeset(crtc->state) ||
12823 to_intel_crtc_state(crtc->state)->update_pipe) {
12824 hw_check = true;
12825
12826 put_domains[to_intel_crtc(crtc)->pipe] =
12827 modeset_get_crtc_power_domains(crtc,
12828 to_intel_crtc_state(crtc->state));
12829 }
12830
61333b60
ML
12831 if (!needs_modeset(crtc->state))
12832 continue;
12833
29ceb0e6 12834 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
460da916 12835
29ceb0e6
VS
12836 if (old_crtc_state->active) {
12837 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
4a806558 12838 dev_priv->display.crtc_disable(to_intel_crtc_state(old_crtc_state), state);
eddfcbcd 12839 intel_crtc->active = false;
58f9c0bc 12840 intel_fbc_disable(intel_crtc);
eddfcbcd 12841 intel_disable_shared_dpll(intel_crtc);
9bbc8258
VS
12842
12843 /*
12844 * Underruns don't always raise
12845 * interrupts, so check manually.
12846 */
12847 intel_check_cpu_fifo_underruns(dev_priv);
12848 intel_check_pch_fifo_underruns(dev_priv);
b9001114 12849
e62929b3
ML
12850 if (!crtc->state->active) {
12851 /*
12852 * Make sure we don't call initial_watermarks
12853 * for ILK-style watermark updates.
12854 */
12855 if (dev_priv->display.atomic_update_watermarks)
12856 dev_priv->display.initial_watermarks(intel_state,
12857 to_intel_crtc_state(crtc->state));
12858 else
12859 intel_update_watermarks(intel_crtc);
12860 }
a539205a 12861 }
b8cecdf5 12862 }
7758a113 12863
ea9d758d
DV
12864 /* Only after disabling all output pipelines that will be changed can we
12865 * update the the output configuration. */
4740b0f2 12866 intel_modeset_update_crtc_state(state);
f6e5b160 12867
565602d7 12868 if (intel_state->modeset) {
4740b0f2 12869 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
33c8df89 12870
b0587e4d 12871 intel_set_cdclk(dev_priv, &dev_priv->cdclk.actual);
f6d1973d 12872
656d1b89
L
12873 /*
12874 * SKL workaround: bspec recommends we disable the SAGV when we
12875 * have more then one pipe enabled
12876 */
56feca91 12877 if (!intel_can_enable_sagv(state))
16dcdc4e 12878 intel_disable_sagv(dev_priv);
656d1b89 12879
677100ce 12880 intel_modeset_verify_disabled(dev, state);
4740b0f2 12881 }
47fab737 12882
896e5bb0 12883 /* Complete the events for pipes that have now been disabled */
29ceb0e6 12884 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
f6ac4b2a 12885 bool modeset = needs_modeset(crtc->state);
80715b2f 12886
1f7528c4
DV
12887 /* Complete events for now disable pipes here. */
12888 if (modeset && !crtc->state->active && crtc->state->event) {
12889 spin_lock_irq(&dev->event_lock);
12890 drm_crtc_send_vblank_event(crtc, crtc->state->event);
12891 spin_unlock_irq(&dev->event_lock);
12892
12893 crtc->state->event = NULL;
12894 }
177246a8
MR
12895 }
12896
896e5bb0
L
12897 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
12898 dev_priv->display.update_crtcs(state, &crtc_vblank_mask);
12899
94f05024
DV
12900 /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
12901 * already, but still need the state for the delayed optimization. To
12902 * fix this:
12903 * - wrap the optimization/post_plane_update stuff into a per-crtc work.
12904 * - schedule that vblank worker _before_ calling hw_done
12905 * - at the start of commit_tail, cancel it _synchrously
12906 * - switch over to the vblank wait helper in the core after that since
12907 * we don't need out special handling any more.
12908 */
5a21b665
DV
12909 if (!state->legacy_cursor_update)
12910 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
12911
12912 /*
12913 * Now that the vblank has passed, we can go ahead and program the
12914 * optimal watermarks on platforms that need two-step watermark
12915 * programming.
12916 *
12917 * TODO: Move this (and other cleanup) to an async worker eventually.
12918 */
12919 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
12920 intel_cstate = to_intel_crtc_state(crtc->state);
12921
12922 if (dev_priv->display.optimize_watermarks)
ccf010fb
ML
12923 dev_priv->display.optimize_watermarks(intel_state,
12924 intel_cstate);
5a21b665
DV
12925 }
12926
12927 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
12928 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
12929
12930 if (put_domains[i])
12931 modeset_put_power_domains(dev_priv, put_domains[i]);
12932
677100ce 12933 intel_modeset_verify_crtc(crtc, state, old_crtc_state, crtc->state);
5a21b665
DV
12934 }
12935
56feca91 12936 if (intel_state->modeset && intel_can_enable_sagv(state))
16dcdc4e 12937 intel_enable_sagv(dev_priv);
656d1b89 12938
94f05024
DV
12939 drm_atomic_helper_commit_hw_done(state);
12940
5a21b665
DV
12941 if (intel_state->modeset)
12942 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
12943
12944 mutex_lock(&dev->struct_mutex);
12945 drm_atomic_helper_cleanup_planes(dev, state);
12946 mutex_unlock(&dev->struct_mutex);
12947
ea0000f0
DV
12948 drm_atomic_helper_commit_cleanup_done(state);
12949
0853695c 12950 drm_atomic_state_put(state);
f30da187 12951
75714940
MK
12952 /* As one of the primary mmio accessors, KMS has a high likelihood
12953 * of triggering bugs in unclaimed access. After we finish
12954 * modesetting, see if an error has been flagged, and if so
12955 * enable debugging for the next modeset - and hope we catch
12956 * the culprit.
12957 *
12958 * XXX note that we assume display power is on at this point.
12959 * This might hold true now but we need to add pm helper to check
12960 * unclaimed only when the hardware is on, as atomic commits
12961 * can happen also when the device is completely off.
12962 */
12963 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
ba318c61
CW
12964
12965 intel_atomic_helper_free_state(dev_priv);
94f05024
DV
12966}
12967
12968static void intel_atomic_commit_work(struct work_struct *work)
12969{
c004a90b
CW
12970 struct drm_atomic_state *state =
12971 container_of(work, struct drm_atomic_state, commit_work);
12972
94f05024
DV
12973 intel_atomic_commit_tail(state);
12974}
12975
c004a90b
CW
12976static int __i915_sw_fence_call
12977intel_atomic_commit_ready(struct i915_sw_fence *fence,
12978 enum i915_sw_fence_notify notify)
12979{
12980 struct intel_atomic_state *state =
12981 container_of(fence, struct intel_atomic_state, commit_ready);
12982
12983 switch (notify) {
12984 case FENCE_COMPLETE:
12985 if (state->base.commit_work.func)
12986 queue_work(system_unbound_wq, &state->base.commit_work);
12987 break;
12988
12989 case FENCE_FREE:
eb955eee
CW
12990 {
12991 struct intel_atomic_helper *helper =
12992 &to_i915(state->base.dev)->atomic_helper;
12993
12994 if (llist_add(&state->freed, &helper->free_list))
12995 schedule_work(&helper->free_work);
12996 break;
12997 }
c004a90b
CW
12998 }
12999
13000 return NOTIFY_DONE;
13001}
13002
6c9c1b38
DV
13003static void intel_atomic_track_fbs(struct drm_atomic_state *state)
13004{
13005 struct drm_plane_state *old_plane_state;
13006 struct drm_plane *plane;
6c9c1b38
DV
13007 int i;
13008
faf5bf0a
CW
13009 for_each_plane_in_state(state, plane, old_plane_state, i)
13010 i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
13011 intel_fb_obj(plane->state->fb),
13012 to_intel_plane(plane)->frontbuffer_bit);
6c9c1b38
DV
13013}
13014
94f05024
DV
13015/**
13016 * intel_atomic_commit - commit validated state object
13017 * @dev: DRM device
13018 * @state: the top-level driver state object
13019 * @nonblock: nonblocking commit
13020 *
13021 * This function commits a top-level state object that has been validated
13022 * with drm_atomic_helper_check().
13023 *
94f05024
DV
13024 * RETURNS
13025 * Zero for success or -errno.
13026 */
13027static int intel_atomic_commit(struct drm_device *dev,
13028 struct drm_atomic_state *state,
13029 bool nonblock)
13030{
13031 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 13032 struct drm_i915_private *dev_priv = to_i915(dev);
94f05024
DV
13033 int ret = 0;
13034
94f05024
DV
13035 ret = drm_atomic_helper_setup_commit(state, nonblock);
13036 if (ret)
13037 return ret;
13038
c004a90b
CW
13039 drm_atomic_state_get(state);
13040 i915_sw_fence_init(&intel_state->commit_ready,
13041 intel_atomic_commit_ready);
94f05024 13042
d07f0e59 13043 ret = intel_atomic_prepare_commit(dev, state);
94f05024
DV
13044 if (ret) {
13045 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
c004a90b 13046 i915_sw_fence_commit(&intel_state->commit_ready);
94f05024
DV
13047 return ret;
13048 }
13049
13050 drm_atomic_helper_swap_state(state, true);
13051 dev_priv->wm.distrust_bios_wm = false;
3c0fb588 13052 intel_shared_dpll_swap_state(state);
6c9c1b38 13053 intel_atomic_track_fbs(state);
94f05024 13054
c3b32658
ML
13055 if (intel_state->modeset) {
13056 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
13057 sizeof(intel_state->min_pixclk));
13058 dev_priv->active_crtcs = intel_state->active_crtcs;
bb0f4aab
VS
13059 dev_priv->cdclk.logical = intel_state->cdclk.logical;
13060 dev_priv->cdclk.actual = intel_state->cdclk.actual;
c3b32658
ML
13061 }
13062
0853695c 13063 drm_atomic_state_get(state);
c004a90b
CW
13064 INIT_WORK(&state->commit_work,
13065 nonblock ? intel_atomic_commit_work : NULL);
13066
13067 i915_sw_fence_commit(&intel_state->commit_ready);
13068 if (!nonblock) {
13069 i915_sw_fence_wait(&intel_state->commit_ready);
94f05024 13070 intel_atomic_commit_tail(state);
c004a90b 13071 }
75714940 13072
74c090b1 13073 return 0;
7f27126e
JB
13074}
13075
c0c36b94
CW
13076void intel_crtc_restore_mode(struct drm_crtc *crtc)
13077{
83a57153
ACO
13078 struct drm_device *dev = crtc->dev;
13079 struct drm_atomic_state *state;
e694eb02 13080 struct drm_crtc_state *crtc_state;
2bfb4627 13081 int ret;
83a57153
ACO
13082
13083 state = drm_atomic_state_alloc(dev);
13084 if (!state) {
78108b7c
VS
13085 DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
13086 crtc->base.id, crtc->name);
83a57153
ACO
13087 return;
13088 }
13089
e694eb02 13090 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
83a57153 13091
e694eb02
ML
13092retry:
13093 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13094 ret = PTR_ERR_OR_ZERO(crtc_state);
13095 if (!ret) {
13096 if (!crtc_state->active)
13097 goto out;
83a57153 13098
e694eb02 13099 crtc_state->mode_changed = true;
74c090b1 13100 ret = drm_atomic_commit(state);
83a57153
ACO
13101 }
13102
e694eb02
ML
13103 if (ret == -EDEADLK) {
13104 drm_atomic_state_clear(state);
13105 drm_modeset_backoff(state->acquire_ctx);
13106 goto retry;
4ed9fb37 13107 }
4be07317 13108
e694eb02 13109out:
0853695c 13110 drm_atomic_state_put(state);
c0c36b94
CW
13111}
13112
a8784875
BP
13113/*
13114 * FIXME: Remove this once i915 is fully DRIVER_ATOMIC by calling
13115 * drm_atomic_helper_legacy_gamma_set() directly.
13116 */
13117static int intel_atomic_legacy_gamma_set(struct drm_crtc *crtc,
13118 u16 *red, u16 *green, u16 *blue,
13119 uint32_t size)
13120{
13121 struct drm_device *dev = crtc->dev;
13122 struct drm_mode_config *config = &dev->mode_config;
13123 struct drm_crtc_state *state;
13124 int ret;
13125
13126 ret = drm_atomic_helper_legacy_gamma_set(crtc, red, green, blue, size);
13127 if (ret)
13128 return ret;
13129
13130 /*
13131 * Make sure we update the legacy properties so this works when
13132 * atomic is not enabled.
13133 */
13134
13135 state = crtc->state;
13136
13137 drm_object_property_set_value(&crtc->base,
13138 config->degamma_lut_property,
13139 (state->degamma_lut) ?
13140 state->degamma_lut->base.id : 0);
13141
13142 drm_object_property_set_value(&crtc->base,
13143 config->ctm_property,
13144 (state->ctm) ?
13145 state->ctm->base.id : 0);
13146
13147 drm_object_property_set_value(&crtc->base,
13148 config->gamma_lut_property,
13149 (state->gamma_lut) ?
13150 state->gamma_lut->base.id : 0);
13151
13152 return 0;
13153}
13154
f6e5b160 13155static const struct drm_crtc_funcs intel_crtc_funcs = {
a8784875 13156 .gamma_set = intel_atomic_legacy_gamma_set,
74c090b1 13157 .set_config = drm_atomic_helper_set_config,
82cf435b 13158 .set_property = drm_atomic_helper_crtc_set_property,
f6e5b160 13159 .destroy = intel_crtc_destroy,
4c01ded5 13160 .page_flip = drm_atomic_helper_page_flip,
1356837e
MR
13161 .atomic_duplicate_state = intel_crtc_duplicate_state,
13162 .atomic_destroy_state = intel_crtc_destroy_state,
8c6b709d 13163 .set_crc_source = intel_crtc_set_crc_source,
f6e5b160
CW
13164};
13165
6beb8c23
MR
13166/**
13167 * intel_prepare_plane_fb - Prepare fb for usage on plane
13168 * @plane: drm plane to prepare for
13169 * @fb: framebuffer to prepare for presentation
13170 *
13171 * Prepares a framebuffer for usage on a display plane. Generally this
13172 * involves pinning the underlying object and updating the frontbuffer tracking
13173 * bits. Some older platforms need special physical address handling for
13174 * cursor planes.
13175 *
f935675f
ML
13176 * Must be called with struct_mutex held.
13177 *
6beb8c23
MR
13178 * Returns 0 on success, negative error code on failure.
13179 */
13180int
13181intel_prepare_plane_fb(struct drm_plane *plane,
1832040d 13182 struct drm_plane_state *new_state)
465c120c 13183{
c004a90b
CW
13184 struct intel_atomic_state *intel_state =
13185 to_intel_atomic_state(new_state->state);
b7f05d4a 13186 struct drm_i915_private *dev_priv = to_i915(plane->dev);
844f9111 13187 struct drm_framebuffer *fb = new_state->fb;
6beb8c23 13188 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
1ee49399 13189 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
c004a90b 13190 int ret;
465c120c 13191
57822dc6
CW
13192 if (obj) {
13193 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
13194 INTEL_INFO(dev_priv)->cursor_needs_physical) {
13195 const int align = IS_I830(dev_priv) ? 16 * 1024 : 256;
13196
13197 ret = i915_gem_object_attach_phys(obj, align);
13198 if (ret) {
13199 DRM_DEBUG_KMS("failed to attach phys object\n");
13200 return ret;
13201 }
13202 } else {
13203 struct i915_vma *vma;
13204
13205 vma = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
13206 if (IS_ERR(vma)) {
13207 DRM_DEBUG_KMS("failed to pin object\n");
13208 return PTR_ERR(vma);
13209 }
13210
13211 to_intel_plane_state(new_state)->vma = vma;
13212 }
13213 }
13214
1ee49399 13215 if (!obj && !old_obj)
465c120c
MR
13216 return 0;
13217
5008e874
ML
13218 if (old_obj) {
13219 struct drm_crtc_state *crtc_state =
c004a90b
CW
13220 drm_atomic_get_existing_crtc_state(new_state->state,
13221 plane->state->crtc);
5008e874
ML
13222
13223 /* Big Hammer, we also need to ensure that any pending
13224 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
13225 * current scanout is retired before unpinning the old
13226 * framebuffer. Note that we rely on userspace rendering
13227 * into the buffer attached to the pipe they are waiting
13228 * on. If not, userspace generates a GPU hang with IPEHR
13229 * point to the MI_WAIT_FOR_EVENT.
13230 *
13231 * This should only fail upon a hung GPU, in which case we
13232 * can safely continue.
13233 */
c004a90b
CW
13234 if (needs_modeset(crtc_state)) {
13235 ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
13236 old_obj->resv, NULL,
13237 false, 0,
13238 GFP_KERNEL);
13239 if (ret < 0)
13240 return ret;
f4457ae7 13241 }
5008e874
ML
13242 }
13243
c004a90b
CW
13244 if (new_state->fence) { /* explicit fencing */
13245 ret = i915_sw_fence_await_dma_fence(&intel_state->commit_ready,
13246 new_state->fence,
13247 I915_FENCE_TIMEOUT,
13248 GFP_KERNEL);
13249 if (ret < 0)
13250 return ret;
13251 }
13252
c37efb99
CW
13253 if (!obj)
13254 return 0;
13255
c004a90b
CW
13256 if (!new_state->fence) { /* implicit fencing */
13257 ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
13258 obj->resv, NULL,
13259 false, I915_FENCE_TIMEOUT,
13260 GFP_KERNEL);
13261 if (ret < 0)
13262 return ret;
6b5e90f5
CW
13263
13264 i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
c004a90b 13265 }
5a21b665 13266
d07f0e59 13267 return 0;
6beb8c23
MR
13268}
13269
38f3ce3a
MR
13270/**
13271 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13272 * @plane: drm plane to clean up for
13273 * @fb: old framebuffer that was on plane
13274 *
13275 * Cleans up a framebuffer that has just been removed from a plane.
f935675f
ML
13276 *
13277 * Must be called with struct_mutex held.
38f3ce3a
MR
13278 */
13279void
13280intel_cleanup_plane_fb(struct drm_plane *plane,
1832040d 13281 struct drm_plane_state *old_state)
38f3ce3a 13282{
be1e3415 13283 struct i915_vma *vma;
38f3ce3a 13284
be1e3415
CW
13285 /* Should only be called after a successful intel_prepare_plane_fb()! */
13286 vma = fetch_and_zero(&to_intel_plane_state(old_state)->vma);
13287 if (vma)
13288 intel_unpin_fb_vma(vma);
465c120c
MR
13289}
13290
6156a456
CK
13291int
13292skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13293{
13294 int max_scale;
6156a456
CK
13295 int crtc_clock, cdclk;
13296
bf8a0af0 13297 if (!intel_crtc || !crtc_state->base.enable)
6156a456
CK
13298 return DRM_PLANE_HELPER_NO_SCALING;
13299
6156a456 13300 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
bb0f4aab 13301 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk.logical.cdclk;
6156a456 13302
54bf1ce6 13303 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
6156a456
CK
13304 return DRM_PLANE_HELPER_NO_SCALING;
13305
13306 /*
13307 * skl max scale is lower of:
13308 * close to 3 but not 3, -1 is for that purpose
13309 * or
13310 * cdclk/crtc_clock
13311 */
13312 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13313
13314 return max_scale;
13315}
13316
465c120c 13317static int
3c692a41 13318intel_check_primary_plane(struct drm_plane *plane,
061e4b8d 13319 struct intel_crtc_state *crtc_state,
3c692a41
GP
13320 struct intel_plane_state *state)
13321{
b63a16f6 13322 struct drm_i915_private *dev_priv = to_i915(plane->dev);
2b875c22 13323 struct drm_crtc *crtc = state->base.crtc;
6156a456 13324 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
061e4b8d
ML
13325 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13326 bool can_position = false;
b63a16f6 13327 int ret;
465c120c 13328
b63a16f6 13329 if (INTEL_GEN(dev_priv) >= 9) {
693bdc28
VS
13330 /* use scaler when colorkey is not required */
13331 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
13332 min_scale = 1;
13333 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
13334 }
d8106366 13335 can_position = true;
6156a456 13336 }
d8106366 13337
cc926387
DV
13338 ret = drm_plane_helper_check_state(&state->base,
13339 &state->clip,
13340 min_scale, max_scale,
13341 can_position, true);
b63a16f6
VS
13342 if (ret)
13343 return ret;
13344
cc926387 13345 if (!state->base.fb)
b63a16f6
VS
13346 return 0;
13347
13348 if (INTEL_GEN(dev_priv) >= 9) {
13349 ret = skl_check_plane_surface(state);
13350 if (ret)
13351 return ret;
13352 }
13353
13354 return 0;
14af293f
GP
13355}
13356
5a21b665
DV
13357static void intel_begin_crtc_commit(struct drm_crtc *crtc,
13358 struct drm_crtc_state *old_crtc_state)
13359{
13360 struct drm_device *dev = crtc->dev;
62e0fb88 13361 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665 13362 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b707aa50
L
13363 struct intel_crtc_state *intel_cstate =
13364 to_intel_crtc_state(crtc->state);
ccf010fb 13365 struct intel_crtc_state *old_intel_cstate =
5a21b665 13366 to_intel_crtc_state(old_crtc_state);
ccf010fb
ML
13367 struct intel_atomic_state *old_intel_state =
13368 to_intel_atomic_state(old_crtc_state->state);
5a21b665
DV
13369 bool modeset = needs_modeset(crtc->state);
13370
13371 /* Perform vblank evasion around commit operation */
13372 intel_pipe_update_start(intel_crtc);
13373
13374 if (modeset)
e62929b3 13375 goto out;
5a21b665
DV
13376
13377 if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
13378 intel_color_set_csc(crtc->state);
13379 intel_color_load_luts(crtc->state);
13380 }
13381
ccf010fb
ML
13382 if (intel_cstate->update_pipe)
13383 intel_update_pipe_config(intel_crtc, old_intel_cstate);
13384 else if (INTEL_GEN(dev_priv) >= 9)
5a21b665 13385 skl_detach_scalers(intel_crtc);
62e0fb88 13386
e62929b3 13387out:
ccf010fb
ML
13388 if (dev_priv->display.atomic_update_watermarks)
13389 dev_priv->display.atomic_update_watermarks(old_intel_state,
13390 intel_cstate);
5a21b665
DV
13391}
13392
13393static void intel_finish_crtc_commit(struct drm_crtc *crtc,
13394 struct drm_crtc_state *old_crtc_state)
13395{
13396 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13397
13398 intel_pipe_update_end(intel_crtc, NULL);
13399}
13400
cf4c7c12 13401/**
4a3b8769
MR
13402 * intel_plane_destroy - destroy a plane
13403 * @plane: plane to destroy
cf4c7c12 13404 *
4a3b8769
MR
13405 * Common destruction function for all types of planes (primary, cursor,
13406 * sprite).
cf4c7c12 13407 */
4a3b8769 13408void intel_plane_destroy(struct drm_plane *plane)
465c120c 13409{
465c120c 13410 drm_plane_cleanup(plane);
69ae561f 13411 kfree(to_intel_plane(plane));
465c120c
MR
13412}
13413
65a3fea0 13414const struct drm_plane_funcs intel_plane_funcs = {
70a101f8
MR
13415 .update_plane = drm_atomic_helper_update_plane,
13416 .disable_plane = drm_atomic_helper_disable_plane,
3d7d6510 13417 .destroy = intel_plane_destroy,
c196e1d6 13418 .set_property = drm_atomic_helper_plane_set_property,
a98b3431
MR
13419 .atomic_get_property = intel_plane_atomic_get_property,
13420 .atomic_set_property = intel_plane_atomic_set_property,
ea2c67bb
MR
13421 .atomic_duplicate_state = intel_plane_duplicate_state,
13422 .atomic_destroy_state = intel_plane_destroy_state,
465c120c
MR
13423};
13424
f79f2692
ML
13425static int
13426intel_legacy_cursor_update(struct drm_plane *plane,
13427 struct drm_crtc *crtc,
13428 struct drm_framebuffer *fb,
13429 int crtc_x, int crtc_y,
13430 unsigned int crtc_w, unsigned int crtc_h,
13431 uint32_t src_x, uint32_t src_y,
13432 uint32_t src_w, uint32_t src_h)
13433{
13434 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
13435 int ret;
13436 struct drm_plane_state *old_plane_state, *new_plane_state;
13437 struct intel_plane *intel_plane = to_intel_plane(plane);
13438 struct drm_framebuffer *old_fb;
13439 struct drm_crtc_state *crtc_state = crtc->state;
be1e3415 13440 struct i915_vma *old_vma;
f79f2692
ML
13441
13442 /*
13443 * When crtc is inactive or there is a modeset pending,
13444 * wait for it to complete in the slowpath
13445 */
13446 if (!crtc_state->active || needs_modeset(crtc_state) ||
13447 to_intel_crtc_state(crtc_state)->update_pipe)
13448 goto slow;
13449
13450 old_plane_state = plane->state;
13451
13452 /*
13453 * If any parameters change that may affect watermarks,
13454 * take the slowpath. Only changing fb or position should be
13455 * in the fastpath.
13456 */
13457 if (old_plane_state->crtc != crtc ||
13458 old_plane_state->src_w != src_w ||
13459 old_plane_state->src_h != src_h ||
13460 old_plane_state->crtc_w != crtc_w ||
13461 old_plane_state->crtc_h != crtc_h ||
13462 !old_plane_state->visible ||
13463 old_plane_state->fb->modifier != fb->modifier)
13464 goto slow;
13465
13466 new_plane_state = intel_plane_duplicate_state(plane);
13467 if (!new_plane_state)
13468 return -ENOMEM;
13469
13470 drm_atomic_set_fb_for_plane(new_plane_state, fb);
13471
13472 new_plane_state->src_x = src_x;
13473 new_plane_state->src_y = src_y;
13474 new_plane_state->src_w = src_w;
13475 new_plane_state->src_h = src_h;
13476 new_plane_state->crtc_x = crtc_x;
13477 new_plane_state->crtc_y = crtc_y;
13478 new_plane_state->crtc_w = crtc_w;
13479 new_plane_state->crtc_h = crtc_h;
13480
13481 ret = intel_plane_atomic_check_with_state(to_intel_crtc_state(crtc->state),
13482 to_intel_plane_state(new_plane_state));
13483 if (ret)
13484 goto out_free;
13485
13486 /* Visibility changed, must take slowpath. */
13487 if (!new_plane_state->visible)
13488 goto slow_free;
13489
13490 ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
13491 if (ret)
13492 goto out_free;
13493
13494 if (INTEL_INFO(dev_priv)->cursor_needs_physical) {
13495 int align = IS_I830(dev_priv) ? 16 * 1024 : 256;
13496
13497 ret = i915_gem_object_attach_phys(intel_fb_obj(fb), align);
13498 if (ret) {
13499 DRM_DEBUG_KMS("failed to attach phys object\n");
13500 goto out_unlock;
13501 }
13502 } else {
13503 struct i915_vma *vma;
13504
13505 vma = intel_pin_and_fence_fb_obj(fb, new_plane_state->rotation);
13506 if (IS_ERR(vma)) {
13507 DRM_DEBUG_KMS("failed to pin object\n");
13508
13509 ret = PTR_ERR(vma);
13510 goto out_unlock;
13511 }
be1e3415
CW
13512
13513 to_intel_plane_state(new_plane_state)->vma = vma;
f79f2692
ML
13514 }
13515
13516 old_fb = old_plane_state->fb;
be1e3415 13517 old_vma = to_intel_plane_state(old_plane_state)->vma;
f79f2692
ML
13518
13519 i915_gem_track_fb(intel_fb_obj(old_fb), intel_fb_obj(fb),
13520 intel_plane->frontbuffer_bit);
13521
13522 /* Swap plane state */
13523 new_plane_state->fence = old_plane_state->fence;
13524 *to_intel_plane_state(old_plane_state) = *to_intel_plane_state(new_plane_state);
13525 new_plane_state->fence = NULL;
13526 new_plane_state->fb = old_fb;
be1e3415 13527 to_intel_plane_state(new_plane_state)->vma = old_vma;
f79f2692
ML
13528
13529 intel_plane->update_plane(plane,
13530 to_intel_crtc_state(crtc->state),
13531 to_intel_plane_state(plane->state));
13532
13533 intel_cleanup_plane_fb(plane, new_plane_state);
13534
13535out_unlock:
13536 mutex_unlock(&dev_priv->drm.struct_mutex);
13537out_free:
13538 intel_plane_destroy_state(plane, new_plane_state);
13539 return ret;
13540
13541slow_free:
13542 intel_plane_destroy_state(plane, new_plane_state);
13543slow:
13544 return drm_atomic_helper_update_plane(plane, crtc, fb,
13545 crtc_x, crtc_y, crtc_w, crtc_h,
13546 src_x, src_y, src_w, src_h);
13547}
13548
13549static const struct drm_plane_funcs intel_cursor_plane_funcs = {
13550 .update_plane = intel_legacy_cursor_update,
13551 .disable_plane = drm_atomic_helper_disable_plane,
13552 .destroy = intel_plane_destroy,
13553 .set_property = drm_atomic_helper_plane_set_property,
13554 .atomic_get_property = intel_plane_atomic_get_property,
13555 .atomic_set_property = intel_plane_atomic_set_property,
13556 .atomic_duplicate_state = intel_plane_duplicate_state,
13557 .atomic_destroy_state = intel_plane_destroy_state,
13558};
13559
b079bd17 13560static struct intel_plane *
580503c7 13561intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
465c120c 13562{
fca0ce2a
VS
13563 struct intel_plane *primary = NULL;
13564 struct intel_plane_state *state = NULL;
465c120c 13565 const uint32_t *intel_primary_formats;
93ca7e00 13566 unsigned int supported_rotations;
45e3743a 13567 unsigned int num_formats;
fca0ce2a 13568 int ret;
465c120c
MR
13569
13570 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
b079bd17
VS
13571 if (!primary) {
13572 ret = -ENOMEM;
fca0ce2a 13573 goto fail;
b079bd17 13574 }
465c120c 13575
8e7d688b 13576 state = intel_create_plane_state(&primary->base);
b079bd17
VS
13577 if (!state) {
13578 ret = -ENOMEM;
fca0ce2a 13579 goto fail;
b079bd17
VS
13580 }
13581
8e7d688b 13582 primary->base.state = &state->base;
ea2c67bb 13583
465c120c
MR
13584 primary->can_scale = false;
13585 primary->max_downscale = 1;
580503c7 13586 if (INTEL_GEN(dev_priv) >= 9) {
6156a456 13587 primary->can_scale = true;
af99ceda 13588 state->scaler_id = -1;
6156a456 13589 }
465c120c 13590 primary->pipe = pipe;
e3c566df
VS
13591 /*
13592 * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
13593 * port is hooked to pipe B. Hence we want plane A feeding pipe B.
13594 */
13595 if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4)
13596 primary->plane = (enum plane) !pipe;
13597 else
13598 primary->plane = (enum plane) pipe;
b14e5848 13599 primary->id = PLANE_PRIMARY;
a9ff8714 13600 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
c59cb179 13601 primary->check_plane = intel_check_primary_plane;
465c120c 13602
580503c7 13603 if (INTEL_GEN(dev_priv) >= 9) {
6c0fd451
DL
13604 intel_primary_formats = skl_primary_formats;
13605 num_formats = ARRAY_SIZE(skl_primary_formats);
a8d201af
ML
13606
13607 primary->update_plane = skylake_update_primary_plane;
13608 primary->disable_plane = skylake_disable_primary_plane;
6e266956 13609 } else if (HAS_PCH_SPLIT(dev_priv)) {
a8d201af
ML
13610 intel_primary_formats = i965_primary_formats;
13611 num_formats = ARRAY_SIZE(i965_primary_formats);
13612
13613 primary->update_plane = ironlake_update_primary_plane;
13614 primary->disable_plane = i9xx_disable_primary_plane;
580503c7 13615 } else if (INTEL_GEN(dev_priv) >= 4) {
568db4f2
DL
13616 intel_primary_formats = i965_primary_formats;
13617 num_formats = ARRAY_SIZE(i965_primary_formats);
a8d201af
ML
13618
13619 primary->update_plane = i9xx_update_primary_plane;
13620 primary->disable_plane = i9xx_disable_primary_plane;
6c0fd451
DL
13621 } else {
13622 intel_primary_formats = i8xx_primary_formats;
13623 num_formats = ARRAY_SIZE(i8xx_primary_formats);
a8d201af
ML
13624
13625 primary->update_plane = i9xx_update_primary_plane;
13626 primary->disable_plane = i9xx_disable_primary_plane;
465c120c
MR
13627 }
13628
580503c7
VS
13629 if (INTEL_GEN(dev_priv) >= 9)
13630 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
13631 0, &intel_plane_funcs,
38573dc1
VS
13632 intel_primary_formats, num_formats,
13633 DRM_PLANE_TYPE_PRIMARY,
13634 "plane 1%c", pipe_name(pipe));
9beb5fea 13635 else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
580503c7
VS
13636 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
13637 0, &intel_plane_funcs,
38573dc1
VS
13638 intel_primary_formats, num_formats,
13639 DRM_PLANE_TYPE_PRIMARY,
13640 "primary %c", pipe_name(pipe));
13641 else
580503c7
VS
13642 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
13643 0, &intel_plane_funcs,
38573dc1
VS
13644 intel_primary_formats, num_formats,
13645 DRM_PLANE_TYPE_PRIMARY,
13646 "plane %c", plane_name(primary->plane));
fca0ce2a
VS
13647 if (ret)
13648 goto fail;
48404c1e 13649
5481e27f 13650 if (INTEL_GEN(dev_priv) >= 9) {
93ca7e00
VS
13651 supported_rotations =
13652 DRM_ROTATE_0 | DRM_ROTATE_90 |
13653 DRM_ROTATE_180 | DRM_ROTATE_270;
4ea7be2b
VS
13654 } else if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
13655 supported_rotations =
13656 DRM_ROTATE_0 | DRM_ROTATE_180 |
13657 DRM_REFLECT_X;
5481e27f 13658 } else if (INTEL_GEN(dev_priv) >= 4) {
93ca7e00
VS
13659 supported_rotations =
13660 DRM_ROTATE_0 | DRM_ROTATE_180;
13661 } else {
13662 supported_rotations = DRM_ROTATE_0;
13663 }
13664
5481e27f 13665 if (INTEL_GEN(dev_priv) >= 4)
93ca7e00
VS
13666 drm_plane_create_rotation_property(&primary->base,
13667 DRM_ROTATE_0,
13668 supported_rotations);
48404c1e 13669
ea2c67bb
MR
13670 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13671
b079bd17 13672 return primary;
fca0ce2a
VS
13673
13674fail:
13675 kfree(state);
13676 kfree(primary);
13677
b079bd17 13678 return ERR_PTR(ret);
465c120c
MR
13679}
13680
3d7d6510 13681static int
852e787c 13682intel_check_cursor_plane(struct drm_plane *plane,
061e4b8d 13683 struct intel_crtc_state *crtc_state,
852e787c 13684 struct intel_plane_state *state)
3d7d6510 13685{
2b875c22 13686 struct drm_framebuffer *fb = state->base.fb;
757f9a3e 13687 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
b29ec92c 13688 enum pipe pipe = to_intel_plane(plane)->pipe;
757f9a3e
GP
13689 unsigned stride;
13690 int ret;
3d7d6510 13691
f8856a44
VS
13692 ret = drm_plane_helper_check_state(&state->base,
13693 &state->clip,
13694 DRM_PLANE_HELPER_NO_SCALING,
13695 DRM_PLANE_HELPER_NO_SCALING,
13696 true, true);
757f9a3e
GP
13697 if (ret)
13698 return ret;
13699
757f9a3e
GP
13700 /* if we want to turn off the cursor ignore width and height */
13701 if (!obj)
da20eabd 13702 return 0;
757f9a3e 13703
757f9a3e 13704 /* Check for which cursor types we support */
50a0bc90
TU
13705 if (!cursor_size_ok(to_i915(plane->dev), state->base.crtc_w,
13706 state->base.crtc_h)) {
ea2c67bb
MR
13707 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
13708 state->base.crtc_w, state->base.crtc_h);
757f9a3e
GP
13709 return -EINVAL;
13710 }
13711
ea2c67bb
MR
13712 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
13713 if (obj->base.size < stride * state->base.crtc_h) {
757f9a3e
GP
13714 DRM_DEBUG_KMS("buffer is too small\n");
13715 return -ENOMEM;
13716 }
13717
bae781b2 13718 if (fb->modifier != DRM_FORMAT_MOD_NONE) {
757f9a3e 13719 DRM_DEBUG_KMS("cursor cannot be tiled\n");
da20eabd 13720 return -EINVAL;
32b7eeec
MR
13721 }
13722
b29ec92c
VS
13723 /*
13724 * There's something wrong with the cursor on CHV pipe C.
13725 * If it straddles the left edge of the screen then
13726 * moving it away from the edge or disabling it often
13727 * results in a pipe underrun, and often that can lead to
13728 * dead pipe (constant underrun reported, and it scans
13729 * out just a solid color). To recover from that, the
13730 * display power well must be turned off and on again.
13731 * Refuse the put the cursor into that compromised position.
13732 */
920a14b2 13733 if (IS_CHERRYVIEW(to_i915(plane->dev)) && pipe == PIPE_C &&
936e71e3 13734 state->base.visible && state->base.crtc_x < 0) {
b29ec92c
VS
13735 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
13736 return -EINVAL;
13737 }
13738
da20eabd 13739 return 0;
852e787c 13740}
3d7d6510 13741
a8ad0d8e
ML
13742static void
13743intel_disable_cursor_plane(struct drm_plane *plane,
7fabf5ef 13744 struct drm_crtc *crtc)
a8ad0d8e 13745{
f2858021
ML
13746 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13747
13748 intel_crtc->cursor_addr = 0;
55a08b3f 13749 intel_crtc_update_cursor(crtc, NULL);
a8ad0d8e
ML
13750}
13751
f4a2cf29 13752static void
55a08b3f
ML
13753intel_update_cursor_plane(struct drm_plane *plane,
13754 const struct intel_crtc_state *crtc_state,
13755 const struct intel_plane_state *state)
852e787c 13756{
55a08b3f
ML
13757 struct drm_crtc *crtc = crtc_state->base.crtc;
13758 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b7f05d4a 13759 struct drm_i915_private *dev_priv = to_i915(plane->dev);
2b875c22 13760 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
a912f12f 13761 uint32_t addr;
852e787c 13762
f4a2cf29 13763 if (!obj)
a912f12f 13764 addr = 0;
b7f05d4a 13765 else if (!INTEL_INFO(dev_priv)->cursor_needs_physical)
be1e3415 13766 addr = intel_plane_ggtt_offset(state);
f4a2cf29 13767 else
a912f12f 13768 addr = obj->phys_handle->busaddr;
852e787c 13769
a912f12f 13770 intel_crtc->cursor_addr = addr;
55a08b3f 13771 intel_crtc_update_cursor(crtc, state);
852e787c
GP
13772}
13773
b079bd17 13774static struct intel_plane *
580503c7 13775intel_cursor_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
3d7d6510 13776{
fca0ce2a
VS
13777 struct intel_plane *cursor = NULL;
13778 struct intel_plane_state *state = NULL;
13779 int ret;
3d7d6510
MR
13780
13781 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
b079bd17
VS
13782 if (!cursor) {
13783 ret = -ENOMEM;
fca0ce2a 13784 goto fail;
b079bd17 13785 }
3d7d6510 13786
8e7d688b 13787 state = intel_create_plane_state(&cursor->base);
b079bd17
VS
13788 if (!state) {
13789 ret = -ENOMEM;
fca0ce2a 13790 goto fail;
b079bd17
VS
13791 }
13792
8e7d688b 13793 cursor->base.state = &state->base;
ea2c67bb 13794
3d7d6510
MR
13795 cursor->can_scale = false;
13796 cursor->max_downscale = 1;
13797 cursor->pipe = pipe;
13798 cursor->plane = pipe;
b14e5848 13799 cursor->id = PLANE_CURSOR;
a9ff8714 13800 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
c59cb179 13801 cursor->check_plane = intel_check_cursor_plane;
55a08b3f 13802 cursor->update_plane = intel_update_cursor_plane;
a8ad0d8e 13803 cursor->disable_plane = intel_disable_cursor_plane;
3d7d6510 13804
580503c7 13805 ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
f79f2692 13806 0, &intel_cursor_plane_funcs,
fca0ce2a
VS
13807 intel_cursor_formats,
13808 ARRAY_SIZE(intel_cursor_formats),
38573dc1
VS
13809 DRM_PLANE_TYPE_CURSOR,
13810 "cursor %c", pipe_name(pipe));
fca0ce2a
VS
13811 if (ret)
13812 goto fail;
4398ad45 13813
5481e27f 13814 if (INTEL_GEN(dev_priv) >= 4)
93ca7e00
VS
13815 drm_plane_create_rotation_property(&cursor->base,
13816 DRM_ROTATE_0,
13817 DRM_ROTATE_0 |
13818 DRM_ROTATE_180);
4398ad45 13819
580503c7 13820 if (INTEL_GEN(dev_priv) >= 9)
af99ceda
CK
13821 state->scaler_id = -1;
13822
ea2c67bb
MR
13823 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
13824
b079bd17 13825 return cursor;
fca0ce2a
VS
13826
13827fail:
13828 kfree(state);
13829 kfree(cursor);
13830
b079bd17 13831 return ERR_PTR(ret);
3d7d6510
MR
13832}
13833
1c74eeaf
NM
13834static void intel_crtc_init_scalers(struct intel_crtc *crtc,
13835 struct intel_crtc_state *crtc_state)
549e2bfb 13836{
65edccce
VS
13837 struct intel_crtc_scaler_state *scaler_state =
13838 &crtc_state->scaler_state;
1c74eeaf 13839 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
549e2bfb 13840 int i;
549e2bfb 13841
1c74eeaf
NM
13842 crtc->num_scalers = dev_priv->info.num_scalers[crtc->pipe];
13843 if (!crtc->num_scalers)
13844 return;
13845
65edccce
VS
13846 for (i = 0; i < crtc->num_scalers; i++) {
13847 struct intel_scaler *scaler = &scaler_state->scalers[i];
13848
13849 scaler->in_use = 0;
13850 scaler->mode = PS_SCALER_MODE_DYN;
549e2bfb
CK
13851 }
13852
13853 scaler_state->scaler_id = -1;
13854}
13855
5ab0d85b 13856static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
79e53945
JB
13857{
13858 struct intel_crtc *intel_crtc;
f5de6e07 13859 struct intel_crtc_state *crtc_state = NULL;
b079bd17
VS
13860 struct intel_plane *primary = NULL;
13861 struct intel_plane *cursor = NULL;
a81d6fa0 13862 int sprite, ret;
79e53945 13863
955382f3 13864 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
b079bd17
VS
13865 if (!intel_crtc)
13866 return -ENOMEM;
79e53945 13867
f5de6e07 13868 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
b079bd17
VS
13869 if (!crtc_state) {
13870 ret = -ENOMEM;
f5de6e07 13871 goto fail;
b079bd17 13872 }
550acefd
ACO
13873 intel_crtc->config = crtc_state;
13874 intel_crtc->base.state = &crtc_state->base;
07878248 13875 crtc_state->base.crtc = &intel_crtc->base;
f5de6e07 13876
580503c7 13877 primary = intel_primary_plane_create(dev_priv, pipe);
b079bd17
VS
13878 if (IS_ERR(primary)) {
13879 ret = PTR_ERR(primary);
3d7d6510 13880 goto fail;
b079bd17 13881 }
d97d7b48 13882 intel_crtc->plane_ids_mask |= BIT(primary->id);
3d7d6510 13883
a81d6fa0 13884 for_each_sprite(dev_priv, pipe, sprite) {
b079bd17
VS
13885 struct intel_plane *plane;
13886
580503c7 13887 plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
d2b2cbce 13888 if (IS_ERR(plane)) {
b079bd17
VS
13889 ret = PTR_ERR(plane);
13890 goto fail;
13891 }
d97d7b48 13892 intel_crtc->plane_ids_mask |= BIT(plane->id);
a81d6fa0
VS
13893 }
13894
580503c7 13895 cursor = intel_cursor_plane_create(dev_priv, pipe);
d2b2cbce 13896 if (IS_ERR(cursor)) {
b079bd17 13897 ret = PTR_ERR(cursor);
3d7d6510 13898 goto fail;
b079bd17 13899 }
d97d7b48 13900 intel_crtc->plane_ids_mask |= BIT(cursor->id);
3d7d6510 13901
5ab0d85b 13902 ret = drm_crtc_init_with_planes(&dev_priv->drm, &intel_crtc->base,
b079bd17
VS
13903 &primary->base, &cursor->base,
13904 &intel_crtc_funcs,
4d5d72b7 13905 "pipe %c", pipe_name(pipe));
3d7d6510
MR
13906 if (ret)
13907 goto fail;
79e53945 13908
80824003 13909 intel_crtc->pipe = pipe;
e3c566df 13910 intel_crtc->plane = primary->plane;
80824003 13911
4b0e333e
CW
13912 intel_crtc->cursor_base = ~0;
13913 intel_crtc->cursor_cntl = ~0;
dc41c154 13914 intel_crtc->cursor_size = ~0;
8d7849db 13915
852eb00d
VS
13916 intel_crtc->wm.cxsr_allowed = true;
13917
1c74eeaf
NM
13918 /* initialize shared scalers */
13919 intel_crtc_init_scalers(intel_crtc, crtc_state);
13920
22fd0fab
JB
13921 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
13922 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
e2af48c6
VS
13923 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = intel_crtc;
13924 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = intel_crtc;
22fd0fab 13925
79e53945 13926 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
87b6b101 13927
8563b1e8
LL
13928 intel_color_init(&intel_crtc->base);
13929
87b6b101 13930 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
b079bd17
VS
13931
13932 return 0;
3d7d6510
MR
13933
13934fail:
b079bd17
VS
13935 /*
13936 * drm_mode_config_cleanup() will free up any
13937 * crtcs/planes already initialized.
13938 */
f5de6e07 13939 kfree(crtc_state);
3d7d6510 13940 kfree(intel_crtc);
b079bd17
VS
13941
13942 return ret;
79e53945
JB
13943}
13944
752aa88a
JB
13945enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
13946{
13947 struct drm_encoder *encoder = connector->base.encoder;
6e9f798d 13948 struct drm_device *dev = connector->base.dev;
752aa88a 13949
51fd371b 13950 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
752aa88a 13951
d3babd3f 13952 if (!encoder || WARN_ON(!encoder->crtc))
752aa88a
JB
13953 return INVALID_PIPE;
13954
13955 return to_intel_crtc(encoder->crtc)->pipe;
13956}
13957
08d7b3d1 13958int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 13959 struct drm_file *file)
08d7b3d1 13960{
08d7b3d1 13961 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7707e653 13962 struct drm_crtc *drmmode_crtc;
c05422d5 13963 struct intel_crtc *crtc;
08d7b3d1 13964
7707e653 13965 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
71240ed2 13966 if (!drmmode_crtc)
3f2c2057 13967 return -ENOENT;
08d7b3d1 13968
7707e653 13969 crtc = to_intel_crtc(drmmode_crtc);
c05422d5 13970 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 13971
c05422d5 13972 return 0;
08d7b3d1
CW
13973}
13974
66a9278e 13975static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 13976{
66a9278e
DV
13977 struct drm_device *dev = encoder->base.dev;
13978 struct intel_encoder *source_encoder;
79e53945 13979 int index_mask = 0;
79e53945
JB
13980 int entry = 0;
13981
b2784e15 13982 for_each_intel_encoder(dev, source_encoder) {
bc079e8b 13983 if (encoders_cloneable(encoder, source_encoder))
66a9278e
DV
13984 index_mask |= (1 << entry);
13985
79e53945
JB
13986 entry++;
13987 }
4ef69c7a 13988
79e53945
JB
13989 return index_mask;
13990}
13991
646d5772 13992static bool has_edp_a(struct drm_i915_private *dev_priv)
4d302442 13993{
646d5772 13994 if (!IS_MOBILE(dev_priv))
4d302442
CW
13995 return false;
13996
13997 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
13998 return false;
13999
5db94019 14000 if (IS_GEN5(dev_priv) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
14001 return false;
14002
14003 return true;
14004}
14005
6315b5d3 14006static bool intel_crt_present(struct drm_i915_private *dev_priv)
84b4e042 14007{
6315b5d3 14008 if (INTEL_GEN(dev_priv) >= 9)
884497ed
DL
14009 return false;
14010
50a0bc90 14011 if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
84b4e042
JB
14012 return false;
14013
920a14b2 14014 if (IS_CHERRYVIEW(dev_priv))
84b4e042
JB
14015 return false;
14016
4f8036a2
TU
14017 if (HAS_PCH_LPT_H(dev_priv) &&
14018 I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
65e472e4
VS
14019 return false;
14020
70ac54d0 14021 /* DDI E can't be used if DDI A requires 4 lanes */
4f8036a2 14022 if (HAS_DDI(dev_priv) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
70ac54d0
VS
14023 return false;
14024
e4abb733 14025 if (!dev_priv->vbt.int_crt_support)
84b4e042
JB
14026 return false;
14027
14028 return true;
14029}
14030
8090ba8c
ID
14031void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
14032{
14033 int pps_num;
14034 int pps_idx;
14035
14036 if (HAS_DDI(dev_priv))
14037 return;
14038 /*
14039 * This w/a is needed at least on CPT/PPT, but to be sure apply it
14040 * everywhere where registers can be write protected.
14041 */
14042 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
14043 pps_num = 2;
14044 else
14045 pps_num = 1;
14046
14047 for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
14048 u32 val = I915_READ(PP_CONTROL(pps_idx));
14049
14050 val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
14051 I915_WRITE(PP_CONTROL(pps_idx), val);
14052 }
14053}
14054
44cb734c
ID
14055static void intel_pps_init(struct drm_i915_private *dev_priv)
14056{
cc3f90f0 14057 if (HAS_PCH_SPLIT(dev_priv) || IS_GEN9_LP(dev_priv))
44cb734c
ID
14058 dev_priv->pps_mmio_base = PCH_PPS_BASE;
14059 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
14060 dev_priv->pps_mmio_base = VLV_PPS_BASE;
14061 else
14062 dev_priv->pps_mmio_base = PPS_BASE;
8090ba8c
ID
14063
14064 intel_pps_unlock_regs_wa(dev_priv);
44cb734c
ID
14065}
14066
c39055b0 14067static void intel_setup_outputs(struct drm_i915_private *dev_priv)
79e53945 14068{
4ef69c7a 14069 struct intel_encoder *encoder;
cb0953d7 14070 bool dpd_is_edp = false;
79e53945 14071
44cb734c
ID
14072 intel_pps_init(dev_priv);
14073
97a824e1
ID
14074 /*
14075 * intel_edp_init_connector() depends on this completing first, to
14076 * prevent the registeration of both eDP and LVDS and the incorrect
14077 * sharing of the PPS.
14078 */
c39055b0 14079 intel_lvds_init(dev_priv);
79e53945 14080
6315b5d3 14081 if (intel_crt_present(dev_priv))
c39055b0 14082 intel_crt_init(dev_priv);
cb0953d7 14083
cc3f90f0 14084 if (IS_GEN9_LP(dev_priv)) {
c776eb2e
VK
14085 /*
14086 * FIXME: Broxton doesn't support port detection via the
14087 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14088 * detect the ports.
14089 */
c39055b0
ACO
14090 intel_ddi_init(dev_priv, PORT_A);
14091 intel_ddi_init(dev_priv, PORT_B);
14092 intel_ddi_init(dev_priv, PORT_C);
c6c794a2 14093
c39055b0 14094 intel_dsi_init(dev_priv);
4f8036a2 14095 } else if (HAS_DDI(dev_priv)) {
0e72a5b5
ED
14096 int found;
14097
de31facd
JB
14098 /*
14099 * Haswell uses DDI functions to detect digital outputs.
14100 * On SKL pre-D0 the strap isn't connected, so we assume
14101 * it's there.
14102 */
77179400 14103 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
de31facd 14104 /* WaIgnoreDDIAStrap: skl */
b976dc53 14105 if (found || IS_GEN9_BC(dev_priv))
c39055b0 14106 intel_ddi_init(dev_priv, PORT_A);
0e72a5b5
ED
14107
14108 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14109 * register */
14110 found = I915_READ(SFUSE_STRAP);
14111
14112 if (found & SFUSE_STRAP_DDIB_DETECTED)
c39055b0 14113 intel_ddi_init(dev_priv, PORT_B);
0e72a5b5 14114 if (found & SFUSE_STRAP_DDIC_DETECTED)
c39055b0 14115 intel_ddi_init(dev_priv, PORT_C);
0e72a5b5 14116 if (found & SFUSE_STRAP_DDID_DETECTED)
c39055b0 14117 intel_ddi_init(dev_priv, PORT_D);
2800e4c2
RV
14118 /*
14119 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14120 */
b976dc53 14121 if (IS_GEN9_BC(dev_priv) &&
2800e4c2
RV
14122 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14123 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14124 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
c39055b0 14125 intel_ddi_init(dev_priv, PORT_E);
2800e4c2 14126
6e266956 14127 } else if (HAS_PCH_SPLIT(dev_priv)) {
cb0953d7 14128 int found;
dd11bc10 14129 dpd_is_edp = intel_dp_is_edp(dev_priv, PORT_D);
270b3042 14130
646d5772 14131 if (has_edp_a(dev_priv))
c39055b0 14132 intel_dp_init(dev_priv, DP_A, PORT_A);
cb0953d7 14133
dc0fa718 14134 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 14135 /* PCH SDVOB multiplex with HDMIB */
c39055b0 14136 found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
30ad48b7 14137 if (!found)
c39055b0 14138 intel_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
5eb08b69 14139 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
c39055b0 14140 intel_dp_init(dev_priv, PCH_DP_B, PORT_B);
30ad48b7
ZW
14141 }
14142
dc0fa718 14143 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
c39055b0 14144 intel_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
30ad48b7 14145
dc0fa718 14146 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
c39055b0 14147 intel_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
30ad48b7 14148
5eb08b69 14149 if (I915_READ(PCH_DP_C) & DP_DETECTED)
c39055b0 14150 intel_dp_init(dev_priv, PCH_DP_C, PORT_C);
5eb08b69 14151
270b3042 14152 if (I915_READ(PCH_DP_D) & DP_DETECTED)
c39055b0 14153 intel_dp_init(dev_priv, PCH_DP_D, PORT_D);
920a14b2 14154 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
22f35042 14155 bool has_edp, has_port;
457c52d8 14156
e17ac6db
VS
14157 /*
14158 * The DP_DETECTED bit is the latched state of the DDC
14159 * SDA pin at boot. However since eDP doesn't require DDC
14160 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14161 * eDP ports may have been muxed to an alternate function.
14162 * Thus we can't rely on the DP_DETECTED bit alone to detect
14163 * eDP ports. Consult the VBT as well as DP_DETECTED to
14164 * detect eDP ports.
22f35042
VS
14165 *
14166 * Sadly the straps seem to be missing sometimes even for HDMI
14167 * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
14168 * and VBT for the presence of the port. Additionally we can't
14169 * trust the port type the VBT declares as we've seen at least
14170 * HDMI ports that the VBT claim are DP or eDP.
e17ac6db 14171 */
dd11bc10 14172 has_edp = intel_dp_is_edp(dev_priv, PORT_B);
22f35042
VS
14173 has_port = intel_bios_is_port_present(dev_priv, PORT_B);
14174 if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
c39055b0 14175 has_edp &= intel_dp_init(dev_priv, VLV_DP_B, PORT_B);
22f35042 14176 if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
c39055b0 14177 intel_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
585a94b8 14178
dd11bc10 14179 has_edp = intel_dp_is_edp(dev_priv, PORT_C);
22f35042
VS
14180 has_port = intel_bios_is_port_present(dev_priv, PORT_C);
14181 if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
c39055b0 14182 has_edp &= intel_dp_init(dev_priv, VLV_DP_C, PORT_C);
22f35042 14183 if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
c39055b0 14184 intel_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
19c03924 14185
920a14b2 14186 if (IS_CHERRYVIEW(dev_priv)) {
22f35042
VS
14187 /*
14188 * eDP not supported on port D,
14189 * so no need to worry about it
14190 */
14191 has_port = intel_bios_is_port_present(dev_priv, PORT_D);
14192 if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
c39055b0 14193 intel_dp_init(dev_priv, CHV_DP_D, PORT_D);
22f35042 14194 if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
c39055b0 14195 intel_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
9418c1f1
VS
14196 }
14197
c39055b0 14198 intel_dsi_init(dev_priv);
5db94019 14199 } else if (!IS_GEN2(dev_priv) && !IS_PINEVIEW(dev_priv)) {
27185ae1 14200 bool found = false;
7d57382e 14201
e2debe91 14202 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14203 DRM_DEBUG_KMS("probing SDVOB\n");
c39055b0 14204 found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
9beb5fea 14205 if (!found && IS_G4X(dev_priv)) {
b01f2c3a 14206 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
c39055b0 14207 intel_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
b01f2c3a 14208 }
27185ae1 14209
9beb5fea 14210 if (!found && IS_G4X(dev_priv))
c39055b0 14211 intel_dp_init(dev_priv, DP_B, PORT_B);
725e30ad 14212 }
13520b05
KH
14213
14214 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 14215
e2debe91 14216 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14217 DRM_DEBUG_KMS("probing SDVOC\n");
c39055b0 14218 found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
b01f2c3a 14219 }
27185ae1 14220
e2debe91 14221 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 14222
9beb5fea 14223 if (IS_G4X(dev_priv)) {
b01f2c3a 14224 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
c39055b0 14225 intel_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
b01f2c3a 14226 }
9beb5fea 14227 if (IS_G4X(dev_priv))
c39055b0 14228 intel_dp_init(dev_priv, DP_C, PORT_C);
725e30ad 14229 }
27185ae1 14230
9beb5fea 14231 if (IS_G4X(dev_priv) && (I915_READ(DP_D) & DP_DETECTED))
c39055b0 14232 intel_dp_init(dev_priv, DP_D, PORT_D);
5db94019 14233 } else if (IS_GEN2(dev_priv))
c39055b0 14234 intel_dvo_init(dev_priv);
79e53945 14235
56b857a5 14236 if (SUPPORTS_TV(dev_priv))
c39055b0 14237 intel_tv_init(dev_priv);
79e53945 14238
c39055b0 14239 intel_psr_init(dev_priv);
7c8f8a70 14240
c39055b0 14241 for_each_intel_encoder(&dev_priv->drm, encoder) {
4ef69c7a
CW
14242 encoder->base.possible_crtcs = encoder->crtc_mask;
14243 encoder->base.possible_clones =
66a9278e 14244 intel_encoder_clones(encoder);
79e53945 14245 }
47356eb6 14246
c39055b0 14247 intel_init_pch_refclk(dev_priv);
270b3042 14248
c39055b0 14249 drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
79e53945
JB
14250}
14251
14252static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14253{
14254 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 14255
ef2d633e 14256 drm_framebuffer_cleanup(fb);
70001cd2
CW
14257
14258 WARN_ON(atomic_dec_return(&intel_fb->obj->framebuffer_references) < 0);
f8c417cd 14259 i915_gem_object_put(intel_fb->obj);
70001cd2 14260
79e53945
JB
14261 kfree(intel_fb);
14262}
14263
14264static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 14265 struct drm_file *file,
79e53945
JB
14266 unsigned int *handle)
14267{
14268 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 14269 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 14270
cc917ab4
CW
14271 if (obj->userptr.mm) {
14272 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14273 return -EINVAL;
14274 }
14275
05394f39 14276 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
14277}
14278
86c98588
RV
14279static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14280 struct drm_file *file,
14281 unsigned flags, unsigned color,
14282 struct drm_clip_rect *clips,
14283 unsigned num_clips)
14284{
5a97bcc6 14285 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
86c98588 14286
5a97bcc6 14287 i915_gem_object_flush_if_display(obj);
74b4ea1e 14288 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
86c98588
RV
14289
14290 return 0;
14291}
14292
79e53945
JB
14293static const struct drm_framebuffer_funcs intel_fb_funcs = {
14294 .destroy = intel_user_framebuffer_destroy,
14295 .create_handle = intel_user_framebuffer_create_handle,
86c98588 14296 .dirty = intel_user_framebuffer_dirty,
79e53945
JB
14297};
14298
b321803d 14299static
920a14b2
TU
14300u32 intel_fb_pitch_limit(struct drm_i915_private *dev_priv,
14301 uint64_t fb_modifier, uint32_t pixel_format)
b321803d 14302{
24dbf51a 14303 u32 gen = INTEL_GEN(dev_priv);
b321803d
DL
14304
14305 if (gen >= 9) {
ac484963
VS
14306 int cpp = drm_format_plane_cpp(pixel_format, 0);
14307
b321803d
DL
14308 /* "The stride in bytes must not exceed the of the size of 8K
14309 * pixels and 32K bytes."
14310 */
ac484963 14311 return min(8192 * cpp, 32768);
6401c37d 14312 } else if (gen >= 5 && !HAS_GMCH_DISPLAY(dev_priv)) {
b321803d
DL
14313 return 32*1024;
14314 } else if (gen >= 4) {
14315 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14316 return 16*1024;
14317 else
14318 return 32*1024;
14319 } else if (gen >= 3) {
14320 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14321 return 8*1024;
14322 else
14323 return 16*1024;
14324 } else {
14325 /* XXX DSPC is limited to 4k tiled */
14326 return 8*1024;
14327 }
14328}
14329
24dbf51a
CW
14330static int intel_framebuffer_init(struct intel_framebuffer *intel_fb,
14331 struct drm_i915_gem_object *obj,
14332 struct drm_mode_fb_cmd2 *mode_cmd)
79e53945 14333{
24dbf51a 14334 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
c2ff7370 14335 unsigned int tiling = i915_gem_object_get_tiling(obj);
b321803d 14336 u32 pitch_limit, stride_alignment;
b3c11ac2 14337 struct drm_format_name_buf format_name;
24dbf51a 14338 int ret = -EINVAL;
79e53945 14339
24dbf51a 14340 atomic_inc(&obj->framebuffer_references);
dd4916c5 14341
2a80eada 14342 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
c2ff7370
VS
14343 /*
14344 * If there's a fence, enforce that
14345 * the fb modifier and tiling mode match.
14346 */
14347 if (tiling != I915_TILING_NONE &&
14348 tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
2a80eada 14349 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
24dbf51a 14350 goto err;
2a80eada
DV
14351 }
14352 } else {
c2ff7370 14353 if (tiling == I915_TILING_X) {
2a80eada 14354 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
c2ff7370 14355 } else if (tiling == I915_TILING_Y) {
2a80eada 14356 DRM_DEBUG("No Y tiling for legacy addfb\n");
24dbf51a 14357 goto err;
2a80eada
DV
14358 }
14359 }
14360
9a8f0a12
TU
14361 /* Passed in modifier sanity checking. */
14362 switch (mode_cmd->modifier[0]) {
14363 case I915_FORMAT_MOD_Y_TILED:
14364 case I915_FORMAT_MOD_Yf_TILED:
6315b5d3 14365 if (INTEL_GEN(dev_priv) < 9) {
9a8f0a12
TU
14366 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14367 mode_cmd->modifier[0]);
24dbf51a 14368 goto err;
9a8f0a12
TU
14369 }
14370 case DRM_FORMAT_MOD_NONE:
14371 case I915_FORMAT_MOD_X_TILED:
14372 break;
14373 default:
c0f40428
JB
14374 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14375 mode_cmd->modifier[0]);
24dbf51a 14376 goto err;
c16ed4be 14377 }
57cd6508 14378
c2ff7370
VS
14379 /*
14380 * gen2/3 display engine uses the fence if present,
14381 * so the tiling mode must match the fb modifier exactly.
14382 */
14383 if (INTEL_INFO(dev_priv)->gen < 4 &&
14384 tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
14385 DRM_DEBUG("tiling_mode must match fb modifier exactly on gen2/3\n");
14386 return -EINVAL;
14387 }
14388
7b49f948
VS
14389 stride_alignment = intel_fb_stride_alignment(dev_priv,
14390 mode_cmd->modifier[0],
b321803d
DL
14391 mode_cmd->pixel_format);
14392 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14393 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14394 mode_cmd->pitches[0], stride_alignment);
24dbf51a 14395 goto err;
c16ed4be 14396 }
57cd6508 14397
920a14b2 14398 pitch_limit = intel_fb_pitch_limit(dev_priv, mode_cmd->modifier[0],
b321803d 14399 mode_cmd->pixel_format);
a35cdaa0 14400 if (mode_cmd->pitches[0] > pitch_limit) {
b321803d
DL
14401 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14402 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
2a80eada 14403 "tiled" : "linear",
a35cdaa0 14404 mode_cmd->pitches[0], pitch_limit);
24dbf51a 14405 goto err;
c16ed4be 14406 }
5d7bd705 14407
c2ff7370
VS
14408 /*
14409 * If there's a fence, enforce that
14410 * the fb pitch and fence stride match.
14411 */
14412 if (tiling != I915_TILING_NONE &&
3e510a8e 14413 mode_cmd->pitches[0] != i915_gem_object_get_stride(obj)) {
c16ed4be 14414 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
3e510a8e
CW
14415 mode_cmd->pitches[0],
14416 i915_gem_object_get_stride(obj));
24dbf51a 14417 goto err;
c16ed4be 14418 }
5d7bd705 14419
57779d06 14420 /* Reject formats not supported by any plane early. */
308e5bcb 14421 switch (mode_cmd->pixel_format) {
57779d06 14422 case DRM_FORMAT_C8:
04b3924d
VS
14423 case DRM_FORMAT_RGB565:
14424 case DRM_FORMAT_XRGB8888:
14425 case DRM_FORMAT_ARGB8888:
57779d06
VS
14426 break;
14427 case DRM_FORMAT_XRGB1555:
6315b5d3 14428 if (INTEL_GEN(dev_priv) > 3) {
b3c11ac2
EE
14429 DRM_DEBUG("unsupported pixel format: %s\n",
14430 drm_get_format_name(mode_cmd->pixel_format, &format_name));
57779d06 14431 return -EINVAL;
c16ed4be 14432 }
57779d06 14433 break;
57779d06 14434 case DRM_FORMAT_ABGR8888:
920a14b2 14435 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
6315b5d3 14436 INTEL_GEN(dev_priv) < 9) {
b3c11ac2
EE
14437 DRM_DEBUG("unsupported pixel format: %s\n",
14438 drm_get_format_name(mode_cmd->pixel_format, &format_name));
6c0fd451
DL
14439 return -EINVAL;
14440 }
14441 break;
14442 case DRM_FORMAT_XBGR8888:
04b3924d 14443 case DRM_FORMAT_XRGB2101010:
57779d06 14444 case DRM_FORMAT_XBGR2101010:
6315b5d3 14445 if (INTEL_GEN(dev_priv) < 4) {
b3c11ac2
EE
14446 DRM_DEBUG("unsupported pixel format: %s\n",
14447 drm_get_format_name(mode_cmd->pixel_format, &format_name));
57779d06 14448 return -EINVAL;
c16ed4be 14449 }
b5626747 14450 break;
7531208b 14451 case DRM_FORMAT_ABGR2101010:
920a14b2 14452 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
b3c11ac2
EE
14453 DRM_DEBUG("unsupported pixel format: %s\n",
14454 drm_get_format_name(mode_cmd->pixel_format, &format_name));
7531208b
DL
14455 return -EINVAL;
14456 }
14457 break;
04b3924d
VS
14458 case DRM_FORMAT_YUYV:
14459 case DRM_FORMAT_UYVY:
14460 case DRM_FORMAT_YVYU:
14461 case DRM_FORMAT_VYUY:
6315b5d3 14462 if (INTEL_GEN(dev_priv) < 5) {
b3c11ac2
EE
14463 DRM_DEBUG("unsupported pixel format: %s\n",
14464 drm_get_format_name(mode_cmd->pixel_format, &format_name));
57779d06 14465 return -EINVAL;
c16ed4be 14466 }
57cd6508
CW
14467 break;
14468 default:
b3c11ac2
EE
14469 DRM_DEBUG("unsupported pixel format: %s\n",
14470 drm_get_format_name(mode_cmd->pixel_format, &format_name));
57cd6508
CW
14471 return -EINVAL;
14472 }
14473
90f9a336
VS
14474 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14475 if (mode_cmd->offsets[0] != 0)
24dbf51a 14476 goto err;
90f9a336 14477
24dbf51a
CW
14478 drm_helper_mode_fill_fb_struct(&dev_priv->drm,
14479 &intel_fb->base, mode_cmd);
c7d73f6a
DV
14480 intel_fb->obj = obj;
14481
6687c906
VS
14482 ret = intel_fill_fb_info(dev_priv, &intel_fb->base);
14483 if (ret)
14484 return ret;
2d7a215f 14485
24dbf51a
CW
14486 ret = drm_framebuffer_init(obj->base.dev,
14487 &intel_fb->base,
14488 &intel_fb_funcs);
79e53945
JB
14489 if (ret) {
14490 DRM_ERROR("framebuffer init failed %d\n", ret);
24dbf51a 14491 goto err;
79e53945
JB
14492 }
14493
79e53945 14494 return 0;
24dbf51a
CW
14495
14496err:
14497 atomic_dec(&obj->framebuffer_references);
14498 return ret;
79e53945
JB
14499}
14500
79e53945
JB
14501static struct drm_framebuffer *
14502intel_user_framebuffer_create(struct drm_device *dev,
14503 struct drm_file *filp,
1eb83451 14504 const struct drm_mode_fb_cmd2 *user_mode_cmd)
79e53945 14505{
dcb1394e 14506 struct drm_framebuffer *fb;
05394f39 14507 struct drm_i915_gem_object *obj;
76dc3769 14508 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
79e53945 14509
03ac0642
CW
14510 obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
14511 if (!obj)
cce13ff7 14512 return ERR_PTR(-ENOENT);
79e53945 14513
24dbf51a 14514 fb = intel_framebuffer_create(obj, &mode_cmd);
dcb1394e 14515 if (IS_ERR(fb))
f0cd5182 14516 i915_gem_object_put(obj);
dcb1394e
LW
14517
14518 return fb;
79e53945
JB
14519}
14520
778e23a9
CW
14521static void intel_atomic_state_free(struct drm_atomic_state *state)
14522{
14523 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
14524
14525 drm_atomic_state_default_release(state);
14526
14527 i915_sw_fence_fini(&intel_state->commit_ready);
14528
14529 kfree(state);
14530}
14531
79e53945 14532static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 14533 .fb_create = intel_user_framebuffer_create,
0632fef6 14534 .output_poll_changed = intel_fbdev_output_poll_changed,
5ee67f1c
MR
14535 .atomic_check = intel_atomic_check,
14536 .atomic_commit = intel_atomic_commit,
de419ab6
ML
14537 .atomic_state_alloc = intel_atomic_state_alloc,
14538 .atomic_state_clear = intel_atomic_state_clear,
778e23a9 14539 .atomic_state_free = intel_atomic_state_free,
79e53945
JB
14540};
14541
88212941
ID
14542/**
14543 * intel_init_display_hooks - initialize the display modesetting hooks
14544 * @dev_priv: device private
14545 */
14546void intel_init_display_hooks(struct drm_i915_private *dev_priv)
e70236a8 14547{
7ff89ca2
VS
14548 intel_init_cdclk_hooks(dev_priv);
14549
88212941 14550 if (INTEL_INFO(dev_priv)->gen >= 9) {
bc8d7dff 14551 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14552 dev_priv->display.get_initial_plane_config =
14553 skylake_get_initial_plane_config;
bc8d7dff
DL
14554 dev_priv->display.crtc_compute_clock =
14555 haswell_crtc_compute_clock;
14556 dev_priv->display.crtc_enable = haswell_crtc_enable;
14557 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 14558 } else if (HAS_DDI(dev_priv)) {
0e8ffe1b 14559 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14560 dev_priv->display.get_initial_plane_config =
14561 ironlake_get_initial_plane_config;
797d0259
ACO
14562 dev_priv->display.crtc_compute_clock =
14563 haswell_crtc_compute_clock;
4f771f10
PZ
14564 dev_priv->display.crtc_enable = haswell_crtc_enable;
14565 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 14566 } else if (HAS_PCH_SPLIT(dev_priv)) {
0e8ffe1b 14567 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
5724dbd1
DL
14568 dev_priv->display.get_initial_plane_config =
14569 ironlake_get_initial_plane_config;
3fb37703
ACO
14570 dev_priv->display.crtc_compute_clock =
14571 ironlake_crtc_compute_clock;
76e5a89c
DV
14572 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14573 dev_priv->display.crtc_disable = ironlake_crtc_disable;
65b3d6a9 14574 } else if (IS_CHERRYVIEW(dev_priv)) {
89b667f8 14575 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14576 dev_priv->display.get_initial_plane_config =
14577 i9xx_get_initial_plane_config;
65b3d6a9
ACO
14578 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
14579 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14580 dev_priv->display.crtc_disable = i9xx_crtc_disable;
14581 } else if (IS_VALLEYVIEW(dev_priv)) {
14582 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14583 dev_priv->display.get_initial_plane_config =
14584 i9xx_get_initial_plane_config;
14585 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
89b667f8
JB
14586 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14587 dev_priv->display.crtc_disable = i9xx_crtc_disable;
19ec6693
ACO
14588 } else if (IS_G4X(dev_priv)) {
14589 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14590 dev_priv->display.get_initial_plane_config =
14591 i9xx_get_initial_plane_config;
14592 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
14593 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14594 dev_priv->display.crtc_disable = i9xx_crtc_disable;
70e8aa21
ACO
14595 } else if (IS_PINEVIEW(dev_priv)) {
14596 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14597 dev_priv->display.get_initial_plane_config =
14598 i9xx_get_initial_plane_config;
14599 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
14600 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14601 dev_priv->display.crtc_disable = i9xx_crtc_disable;
81c97f52 14602 } else if (!IS_GEN2(dev_priv)) {
0e8ffe1b 14603 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14604 dev_priv->display.get_initial_plane_config =
14605 i9xx_get_initial_plane_config;
d6dfee7a 14606 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
76e5a89c
DV
14607 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14608 dev_priv->display.crtc_disable = i9xx_crtc_disable;
81c97f52
ACO
14609 } else {
14610 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14611 dev_priv->display.get_initial_plane_config =
14612 i9xx_get_initial_plane_config;
14613 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
14614 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14615 dev_priv->display.crtc_disable = i9xx_crtc_disable;
f564048e 14616 }
e70236a8 14617
88212941 14618 if (IS_GEN5(dev_priv)) {
3bb11b53 14619 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
88212941 14620 } else if (IS_GEN6(dev_priv)) {
3bb11b53 14621 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
88212941 14622 } else if (IS_IVYBRIDGE(dev_priv)) {
3bb11b53
SJ
14623 /* FIXME: detect B0+ stepping and use auto training */
14624 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
88212941 14625 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
3bb11b53 14626 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
445e780b
VS
14627 }
14628
27082493
L
14629 if (dev_priv->info.gen >= 9)
14630 dev_priv->display.update_crtcs = skl_update_crtcs;
14631 else
14632 dev_priv->display.update_crtcs = intel_update_crtcs;
14633
5a21b665
DV
14634 switch (INTEL_INFO(dev_priv)->gen) {
14635 case 2:
14636 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14637 break;
14638
14639 case 3:
14640 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14641 break;
14642
14643 case 4:
14644 case 5:
14645 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14646 break;
14647
14648 case 6:
14649 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14650 break;
14651 case 7:
14652 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
14653 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14654 break;
14655 case 9:
14656 /* Drop through - unsupported since execlist only. */
14657 default:
14658 /* Default just returns -ENODEV to indicate unsupported */
14659 dev_priv->display.queue_flip = intel_default_queue_flip;
14660 }
e70236a8
JB
14661}
14662
b690e96c
JB
14663/*
14664 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14665 * resume, or other times. This quirk makes sure that's the case for
14666 * affected systems.
14667 */
0206e353 14668static void quirk_pipea_force(struct drm_device *dev)
b690e96c 14669{
fac5e23e 14670 struct drm_i915_private *dev_priv = to_i915(dev);
b690e96c
JB
14671
14672 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
bc0daf48 14673 DRM_INFO("applying pipe a force quirk\n");
b690e96c
JB
14674}
14675
b6b5d049
VS
14676static void quirk_pipeb_force(struct drm_device *dev)
14677{
fac5e23e 14678 struct drm_i915_private *dev_priv = to_i915(dev);
b6b5d049
VS
14679
14680 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14681 DRM_INFO("applying pipe b force quirk\n");
14682}
14683
435793df
KP
14684/*
14685 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14686 */
14687static void quirk_ssc_force_disable(struct drm_device *dev)
14688{
fac5e23e 14689 struct drm_i915_private *dev_priv = to_i915(dev);
435793df 14690 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 14691 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
14692}
14693
4dca20ef 14694/*
5a15ab5b
CE
14695 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14696 * brightness value
4dca20ef
CE
14697 */
14698static void quirk_invert_brightness(struct drm_device *dev)
14699{
fac5e23e 14700 struct drm_i915_private *dev_priv = to_i915(dev);
4dca20ef 14701 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 14702 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
14703}
14704
9c72cc6f
SD
14705/* Some VBT's incorrectly indicate no backlight is present */
14706static void quirk_backlight_present(struct drm_device *dev)
14707{
fac5e23e 14708 struct drm_i915_private *dev_priv = to_i915(dev);
9c72cc6f
SD
14709 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14710 DRM_INFO("applying backlight present quirk\n");
14711}
14712
b690e96c
JB
14713struct intel_quirk {
14714 int device;
14715 int subsystem_vendor;
14716 int subsystem_device;
14717 void (*hook)(struct drm_device *dev);
14718};
14719
5f85f176
EE
14720/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14721struct intel_dmi_quirk {
14722 void (*hook)(struct drm_device *dev);
14723 const struct dmi_system_id (*dmi_id_list)[];
14724};
14725
14726static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14727{
14728 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14729 return 1;
14730}
14731
14732static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14733 {
14734 .dmi_id_list = &(const struct dmi_system_id[]) {
14735 {
14736 .callback = intel_dmi_reverse_brightness,
14737 .ident = "NCR Corporation",
14738 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14739 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14740 },
14741 },
14742 { } /* terminating entry */
14743 },
14744 .hook = quirk_invert_brightness,
14745 },
14746};
14747
c43b5634 14748static struct intel_quirk intel_quirks[] = {
b690e96c
JB
14749 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
14750 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
14751
b690e96c
JB
14752 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
14753 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
14754
5f080c0f
VS
14755 /* 830 needs to leave pipe A & dpll A up */
14756 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
14757
b6b5d049
VS
14758 /* 830 needs to leave pipe B & dpll B up */
14759 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
14760
435793df
KP
14761 /* Lenovo U160 cannot use SSC on LVDS */
14762 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
14763
14764 /* Sony Vaio Y cannot use SSC on LVDS */
14765 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 14766
be505f64
AH
14767 /* Acer Aspire 5734Z must invert backlight brightness */
14768 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14769
14770 /* Acer/eMachines G725 */
14771 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14772
14773 /* Acer/eMachines e725 */
14774 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
14775
14776 /* Acer/Packard Bell NCL20 */
14777 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
14778
14779 /* Acer Aspire 4736Z */
14780 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
14781
14782 /* Acer Aspire 5336 */
14783 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
2e93a1aa
SD
14784
14785 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
14786 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
d4967d8c 14787
dfb3d47b
SD
14788 /* Acer C720 Chromebook (Core i3 4005U) */
14789 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
14790
b2a9601c 14791 /* Apple Macbook 2,1 (Core 2 T7400) */
14792 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
14793
1b9448b0
JN
14794 /* Apple Macbook 4,1 */
14795 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
14796
d4967d8c
SD
14797 /* Toshiba CB35 Chromebook (Celeron 2955U) */
14798 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
724cb06f
SD
14799
14800 /* HP Chromebook 14 (Celeron 2955U) */
14801 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
cf6f0af9
JN
14802
14803 /* Dell Chromebook 11 */
14804 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
9be64eee
JN
14805
14806 /* Dell Chromebook 11 (2015 version) */
14807 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
b690e96c
JB
14808};
14809
14810static void intel_init_quirks(struct drm_device *dev)
14811{
14812 struct pci_dev *d = dev->pdev;
14813 int i;
14814
14815 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
14816 struct intel_quirk *q = &intel_quirks[i];
14817
14818 if (d->device == q->device &&
14819 (d->subsystem_vendor == q->subsystem_vendor ||
14820 q->subsystem_vendor == PCI_ANY_ID) &&
14821 (d->subsystem_device == q->subsystem_device ||
14822 q->subsystem_device == PCI_ANY_ID))
14823 q->hook(dev);
14824 }
5f85f176
EE
14825 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
14826 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
14827 intel_dmi_quirks[i].hook(dev);
14828 }
b690e96c
JB
14829}
14830
9cce37f4 14831/* Disable the VGA plane that we never use */
29b74b7f 14832static void i915_disable_vga(struct drm_i915_private *dev_priv)
9cce37f4 14833{
52a05c30 14834 struct pci_dev *pdev = dev_priv->drm.pdev;
9cce37f4 14835 u8 sr1;
920a14b2 14836 i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
9cce37f4 14837
2b37c616 14838 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
52a05c30 14839 vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 14840 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
14841 sr1 = inb(VGA_SR_DATA);
14842 outb(sr1 | 1<<5, VGA_SR_DATA);
52a05c30 14843 vga_put(pdev, VGA_RSRC_LEGACY_IO);
9cce37f4
JB
14844 udelay(300);
14845
01f5a626 14846 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9cce37f4
JB
14847 POSTING_READ(vga_reg);
14848}
14849
f817586c
DV
14850void intel_modeset_init_hw(struct drm_device *dev)
14851{
fac5e23e 14852 struct drm_i915_private *dev_priv = to_i915(dev);
1a617b77 14853
4c75b940 14854 intel_update_cdclk(dev_priv);
bb0f4aab 14855 dev_priv->cdclk.logical = dev_priv->cdclk.actual = dev_priv->cdclk.hw;
1a617b77 14856
46f16e63 14857 intel_init_clock_gating(dev_priv);
f817586c
DV
14858}
14859
d93c0372
MR
14860/*
14861 * Calculate what we think the watermarks should be for the state we've read
14862 * out of the hardware and then immediately program those watermarks so that
14863 * we ensure the hardware settings match our internal state.
14864 *
14865 * We can calculate what we think WM's should be by creating a duplicate of the
14866 * current state (which was constructed during hardware readout) and running it
14867 * through the atomic check code to calculate new watermark values in the
14868 * state object.
14869 */
14870static void sanitize_watermarks(struct drm_device *dev)
14871{
14872 struct drm_i915_private *dev_priv = to_i915(dev);
14873 struct drm_atomic_state *state;
ccf010fb 14874 struct intel_atomic_state *intel_state;
d93c0372
MR
14875 struct drm_crtc *crtc;
14876 struct drm_crtc_state *cstate;
14877 struct drm_modeset_acquire_ctx ctx;
14878 int ret;
14879 int i;
14880
14881 /* Only supported on platforms that use atomic watermark design */
ed4a6a7c 14882 if (!dev_priv->display.optimize_watermarks)
d93c0372
MR
14883 return;
14884
14885 /*
14886 * We need to hold connection_mutex before calling duplicate_state so
14887 * that the connector loop is protected.
14888 */
14889 drm_modeset_acquire_init(&ctx, 0);
14890retry:
0cd1262d 14891 ret = drm_modeset_lock_all_ctx(dev, &ctx);
d93c0372
MR
14892 if (ret == -EDEADLK) {
14893 drm_modeset_backoff(&ctx);
14894 goto retry;
14895 } else if (WARN_ON(ret)) {
0cd1262d 14896 goto fail;
d93c0372
MR
14897 }
14898
14899 state = drm_atomic_helper_duplicate_state(dev, &ctx);
14900 if (WARN_ON(IS_ERR(state)))
0cd1262d 14901 goto fail;
d93c0372 14902
ccf010fb
ML
14903 intel_state = to_intel_atomic_state(state);
14904
ed4a6a7c
MR
14905 /*
14906 * Hardware readout is the only time we don't want to calculate
14907 * intermediate watermarks (since we don't trust the current
14908 * watermarks).
14909 */
ccf010fb 14910 intel_state->skip_intermediate_wm = true;
ed4a6a7c 14911
d93c0372
MR
14912 ret = intel_atomic_check(dev, state);
14913 if (ret) {
14914 /*
14915 * If we fail here, it means that the hardware appears to be
14916 * programmed in a way that shouldn't be possible, given our
14917 * understanding of watermark requirements. This might mean a
14918 * mistake in the hardware readout code or a mistake in the
14919 * watermark calculations for a given platform. Raise a WARN
14920 * so that this is noticeable.
14921 *
14922 * If this actually happens, we'll have to just leave the
14923 * BIOS-programmed watermarks untouched and hope for the best.
14924 */
14925 WARN(true, "Could not determine valid watermarks for inherited state\n");
b9a1b717 14926 goto put_state;
d93c0372
MR
14927 }
14928
14929 /* Write calculated watermark values back */
d93c0372
MR
14930 for_each_crtc_in_state(state, crtc, cstate, i) {
14931 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
14932
ed4a6a7c 14933 cs->wm.need_postvbl_update = true;
ccf010fb 14934 dev_priv->display.optimize_watermarks(intel_state, cs);
d93c0372
MR
14935 }
14936
b9a1b717 14937put_state:
0853695c 14938 drm_atomic_state_put(state);
0cd1262d 14939fail:
d93c0372
MR
14940 drm_modeset_drop_locks(&ctx);
14941 drm_modeset_acquire_fini(&ctx);
14942}
14943
b079bd17 14944int intel_modeset_init(struct drm_device *dev)
79e53945 14945{
72e96d64
JL
14946 struct drm_i915_private *dev_priv = to_i915(dev);
14947 struct i915_ggtt *ggtt = &dev_priv->ggtt;
8cc87b75 14948 enum pipe pipe;
46f297fb 14949 struct intel_crtc *crtc;
79e53945
JB
14950
14951 drm_mode_config_init(dev);
14952
14953 dev->mode_config.min_width = 0;
14954 dev->mode_config.min_height = 0;
14955
019d96cb
DA
14956 dev->mode_config.preferred_depth = 24;
14957 dev->mode_config.prefer_shadow = 1;
14958
25bab385
TU
14959 dev->mode_config.allow_fb_modifiers = true;
14960
e6ecefaa 14961 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 14962
eb955eee 14963 INIT_WORK(&dev_priv->atomic_helper.free_work,
ba318c61 14964 intel_atomic_helper_free_state_worker);
eb955eee 14965
b690e96c
JB
14966 intel_init_quirks(dev);
14967
62d75df7 14968 intel_init_pm(dev_priv);
1fa61106 14969
b7f05d4a 14970 if (INTEL_INFO(dev_priv)->num_pipes == 0)
b079bd17 14971 return 0;
e3c74757 14972
69f92f67
LW
14973 /*
14974 * There may be no VBT; and if the BIOS enabled SSC we can
14975 * just keep using it to avoid unnecessary flicker. Whereas if the
14976 * BIOS isn't using it, don't assume it will work even if the VBT
14977 * indicates as much.
14978 */
6e266956 14979 if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
69f92f67
LW
14980 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
14981 DREF_SSC1_ENABLE);
14982
14983 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
14984 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
14985 bios_lvds_use_ssc ? "en" : "dis",
14986 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
14987 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
14988 }
14989 }
14990
5db94019 14991 if (IS_GEN2(dev_priv)) {
a6c45cf0
CW
14992 dev->mode_config.max_width = 2048;
14993 dev->mode_config.max_height = 2048;
5db94019 14994 } else if (IS_GEN3(dev_priv)) {
5e4d6fa7
KP
14995 dev->mode_config.max_width = 4096;
14996 dev->mode_config.max_height = 4096;
79e53945 14997 } else {
a6c45cf0
CW
14998 dev->mode_config.max_width = 8192;
14999 dev->mode_config.max_height = 8192;
79e53945 15000 }
068be561 15001
2a307c2e
JN
15002 if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
15003 dev->mode_config.cursor_width = IS_I845G(dev_priv) ? 64 : 512;
dc41c154 15004 dev->mode_config.cursor_height = 1023;
5db94019 15005 } else if (IS_GEN2(dev_priv)) {
068be561
DL
15006 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15007 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15008 } else {
15009 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15010 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15011 }
15012
72e96d64 15013 dev->mode_config.fb_base = ggtt->mappable_base;
79e53945 15014
28c97730 15015 DRM_DEBUG_KMS("%d display pipe%s available.\n",
b7f05d4a
TU
15016 INTEL_INFO(dev_priv)->num_pipes,
15017 INTEL_INFO(dev_priv)->num_pipes > 1 ? "s" : "");
79e53945 15018
055e393f 15019 for_each_pipe(dev_priv, pipe) {
b079bd17
VS
15020 int ret;
15021
5ab0d85b 15022 ret = intel_crtc_init(dev_priv, pipe);
b079bd17
VS
15023 if (ret) {
15024 drm_mode_config_cleanup(dev);
15025 return ret;
15026 }
79e53945
JB
15027 }
15028
bfa7df01 15029 intel_update_czclk(dev_priv);
4c75b940 15030 intel_update_cdclk(dev_priv);
bb0f4aab 15031 dev_priv->cdclk.logical = dev_priv->cdclk.actual = dev_priv->cdclk.hw;
bfa7df01 15032
e72f9fbf 15033 intel_shared_dpll_init(dev);
ee7b9f93 15034
b2045352 15035 if (dev_priv->max_cdclk_freq == 0)
4c75b940 15036 intel_update_max_cdclk(dev_priv);
b2045352 15037
9cce37f4 15038 /* Just disable it once at startup */
29b74b7f 15039 i915_disable_vga(dev_priv);
c39055b0 15040 intel_setup_outputs(dev_priv);
11be49eb 15041
6e9f798d 15042 drm_modeset_lock_all(dev);
043e9bda 15043 intel_modeset_setup_hw_state(dev);
6e9f798d 15044 drm_modeset_unlock_all(dev);
46f297fb 15045
d3fcc808 15046 for_each_intel_crtc(dev, crtc) {
eeebeac5
ML
15047 struct intel_initial_plane_config plane_config = {};
15048
46f297fb
JB
15049 if (!crtc->active)
15050 continue;
15051
46f297fb 15052 /*
46f297fb
JB
15053 * Note that reserving the BIOS fb up front prevents us
15054 * from stuffing other stolen allocations like the ring
15055 * on top. This prevents some ugliness at boot time, and
15056 * can even allow for smooth boot transitions if the BIOS
15057 * fb is large enough for the active pipe configuration.
15058 */
eeebeac5
ML
15059 dev_priv->display.get_initial_plane_config(crtc,
15060 &plane_config);
15061
15062 /*
15063 * If the fb is shared between multiple heads, we'll
15064 * just get the first one.
15065 */
15066 intel_find_initial_plane_obj(crtc, &plane_config);
46f297fb 15067 }
d93c0372
MR
15068
15069 /*
15070 * Make sure hardware watermarks really match the state we read out.
15071 * Note that we need to do this after reconstructing the BIOS fb's
15072 * since the watermark calculation done here will use pstate->fb.
15073 */
15074 sanitize_watermarks(dev);
b079bd17
VS
15075
15076 return 0;
2c7111db
CW
15077}
15078
7fad798e
DV
15079static void intel_enable_pipe_a(struct drm_device *dev)
15080{
15081 struct intel_connector *connector;
15082 struct drm_connector *crt = NULL;
15083 struct intel_load_detect_pipe load_detect_temp;
208bf9fd 15084 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
7fad798e
DV
15085
15086 /* We can't just switch on the pipe A, we need to set things up with a
15087 * proper mode and output configuration. As a gross hack, enable pipe A
15088 * by enabling the load detect pipe once. */
3a3371ff 15089 for_each_intel_connector(dev, connector) {
7fad798e
DV
15090 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15091 crt = &connector->base;
15092 break;
15093 }
15094 }
15095
15096 if (!crt)
15097 return;
15098
208bf9fd 15099 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
49172fee 15100 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
7fad798e
DV
15101}
15102
fa555837
DV
15103static bool
15104intel_check_plane_mapping(struct intel_crtc *crtc)
15105{
b7f05d4a 15106 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
649636ef 15107 u32 val;
fa555837 15108
b7f05d4a 15109 if (INTEL_INFO(dev_priv)->num_pipes == 1)
fa555837
DV
15110 return true;
15111
649636ef 15112 val = I915_READ(DSPCNTR(!crtc->plane));
fa555837
DV
15113
15114 if ((val & DISPLAY_PLANE_ENABLE) &&
15115 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15116 return false;
15117
15118 return true;
15119}
15120
02e93c35
VS
15121static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15122{
15123 struct drm_device *dev = crtc->base.dev;
15124 struct intel_encoder *encoder;
15125
15126 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15127 return true;
15128
15129 return false;
15130}
15131
496b0fc3
ML
15132static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
15133{
15134 struct drm_device *dev = encoder->base.dev;
15135 struct intel_connector *connector;
15136
15137 for_each_connector_on_encoder(dev, &encoder->base, connector)
15138 return connector;
15139
15140 return NULL;
15141}
15142
a168f5b3
VS
15143static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
15144 enum transcoder pch_transcoder)
15145{
15146 return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
15147 (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == TRANSCODER_A);
15148}
15149
24929352
DV
15150static void intel_sanitize_crtc(struct intel_crtc *crtc)
15151{
15152 struct drm_device *dev = crtc->base.dev;
fac5e23e 15153 struct drm_i915_private *dev_priv = to_i915(dev);
4d1de975 15154 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
24929352 15155
24929352 15156 /* Clear any frame start delays used for debugging left by the BIOS */
4d1de975
JN
15157 if (!transcoder_is_dsi(cpu_transcoder)) {
15158 i915_reg_t reg = PIPECONF(cpu_transcoder);
15159
15160 I915_WRITE(reg,
15161 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15162 }
24929352 15163
d3eaf884 15164 /* restore vblank interrupts to correct state */
9625604c 15165 drm_crtc_vblank_reset(&crtc->base);
d297e103 15166 if (crtc->active) {
f9cd7b88
VS
15167 struct intel_plane *plane;
15168
9625604c 15169 drm_crtc_vblank_on(&crtc->base);
f9cd7b88
VS
15170
15171 /* Disable everything but the primary plane */
15172 for_each_intel_plane_on_crtc(dev, crtc, plane) {
15173 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
15174 continue;
15175
15176 plane->disable_plane(&plane->base, &crtc->base);
15177 }
9625604c 15178 }
d3eaf884 15179
24929352 15180 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
DV
15181 * disable the crtc (and hence change the state) if it is wrong. Note
15182 * that gen4+ has a fixed plane -> pipe mapping. */
6315b5d3 15183 if (INTEL_GEN(dev_priv) < 4 && !intel_check_plane_mapping(crtc)) {
24929352
DV
15184 bool plane;
15185
78108b7c
VS
15186 DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
15187 crtc->base.base.id, crtc->base.name);
24929352
DV
15188
15189 /* Pipe has the wrong plane attached and the plane is active.
15190 * Temporarily change the plane mapping and disable everything
15191 * ... */
15192 plane = crtc->plane;
1d4258db 15193 crtc->base.primary->state->visible = true;
24929352 15194 crtc->plane = !plane;
b17d48e2 15195 intel_crtc_disable_noatomic(&crtc->base);
24929352 15196 crtc->plane = plane;
24929352 15197 }
24929352 15198
7fad798e
DV
15199 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15200 crtc->pipe == PIPE_A && !crtc->active) {
15201 /* BIOS forgot to enable pipe A, this mostly happens after
15202 * resume. Force-enable the pipe to fix this, the update_dpms
15203 * call below we restore the pipe to the right state, but leave
15204 * the required bits on. */
15205 intel_enable_pipe_a(dev);
15206 }
15207
24929352
DV
15208 /* Adjust the state of the output pipe according to whether we
15209 * have active connectors/encoders. */
842e0307 15210 if (crtc->active && !intel_crtc_has_encoders(crtc))
b17d48e2 15211 intel_crtc_disable_noatomic(&crtc->base);
24929352 15212
49cff963 15213 if (crtc->active || HAS_GMCH_DISPLAY(dev_priv)) {
4cc31489
DV
15214 /*
15215 * We start out with underrun reporting disabled to avoid races.
15216 * For correct bookkeeping mark this on active crtcs.
15217 *
c5ab3bc0
DV
15218 * Also on gmch platforms we dont have any hardware bits to
15219 * disable the underrun reporting. Which means we need to start
15220 * out with underrun reporting disabled also on inactive pipes,
15221 * since otherwise we'll complain about the garbage we read when
15222 * e.g. coming up after runtime pm.
15223 *
4cc31489
DV
15224 * No protection against concurrent access is required - at
15225 * worst a fifo underrun happens which also sets this to false.
15226 */
15227 crtc->cpu_fifo_underrun_disabled = true;
a168f5b3
VS
15228 /*
15229 * We track the PCH trancoder underrun reporting state
15230 * within the crtc. With crtc for pipe A housing the underrun
15231 * reporting state for PCH transcoder A, crtc for pipe B housing
15232 * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
15233 * and marking underrun reporting as disabled for the non-existing
15234 * PCH transcoders B and C would prevent enabling the south
15235 * error interrupt (see cpt_can_enable_serr_int()).
15236 */
15237 if (has_pch_trancoder(dev_priv, (enum transcoder)crtc->pipe))
15238 crtc->pch_fifo_underrun_disabled = true;
4cc31489 15239 }
24929352
DV
15240}
15241
15242static void intel_sanitize_encoder(struct intel_encoder *encoder)
15243{
15244 struct intel_connector *connector;
24929352
DV
15245
15246 /* We need to check both for a crtc link (meaning that the
15247 * encoder is active and trying to read from a pipe) and the
15248 * pipe itself being active. */
15249 bool has_active_crtc = encoder->base.crtc &&
15250 to_intel_crtc(encoder->base.crtc)->active;
15251
496b0fc3
ML
15252 connector = intel_encoder_find_connector(encoder);
15253 if (connector && !has_active_crtc) {
24929352
DV
15254 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15255 encoder->base.base.id,
8e329a03 15256 encoder->base.name);
24929352
DV
15257
15258 /* Connector is active, but has no active pipe. This is
15259 * fallout from our resume register restoring. Disable
15260 * the encoder manually again. */
15261 if (encoder->base.crtc) {
fd6bbda9
ML
15262 struct drm_crtc_state *crtc_state = encoder->base.crtc->state;
15263
24929352
DV
15264 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15265 encoder->base.base.id,
8e329a03 15266 encoder->base.name);
fd6bbda9 15267 encoder->disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
a62d1497 15268 if (encoder->post_disable)
fd6bbda9 15269 encoder->post_disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
24929352 15270 }
7f1950fb 15271 encoder->base.crtc = NULL;
24929352
DV
15272
15273 /* Inconsistent output/port/pipe state happens presumably due to
15274 * a bug in one of the get_hw_state functions. Or someplace else
15275 * in our code, like the register restore mess on resume. Clamp
15276 * things to off as a safer default. */
fd6bbda9
ML
15277
15278 connector->base.dpms = DRM_MODE_DPMS_OFF;
15279 connector->base.encoder = NULL;
24929352
DV
15280 }
15281 /* Enabled encoders without active connectors will be fixed in
15282 * the crtc fixup. */
15283}
15284
29b74b7f 15285void i915_redisable_vga_power_on(struct drm_i915_private *dev_priv)
0fde901f 15286{
920a14b2 15287 i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
0fde901f 15288
04098753
ID
15289 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15290 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
29b74b7f 15291 i915_disable_vga(dev_priv);
04098753
ID
15292 }
15293}
15294
29b74b7f 15295void i915_redisable_vga(struct drm_i915_private *dev_priv)
04098753 15296{
8dc8a27c
PZ
15297 /* This function can be called both from intel_modeset_setup_hw_state or
15298 * at a very early point in our resume sequence, where the power well
15299 * structures are not yet restored. Since this function is at a very
15300 * paranoid "someone might have enabled VGA while we were not looking"
15301 * level, just check if the power well is enabled instead of trying to
15302 * follow the "don't touch the power well if we don't need it" policy
15303 * the rest of the driver uses. */
6392f847 15304 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
15305 return;
15306
29b74b7f 15307 i915_redisable_vga_power_on(dev_priv);
6392f847
ID
15308
15309 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
0fde901f
KM
15310}
15311
f9cd7b88 15312static bool primary_get_hw_state(struct intel_plane *plane)
98ec7739 15313{
f9cd7b88 15314 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
98ec7739 15315
f9cd7b88 15316 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
d032ffa0
ML
15317}
15318
f9cd7b88
VS
15319/* FIXME read out full plane state for all planes */
15320static void readout_plane_state(struct intel_crtc *crtc)
d032ffa0 15321{
b26d3ea3 15322 struct drm_plane *primary = crtc->base.primary;
f9cd7b88 15323 struct intel_plane_state *plane_state =
b26d3ea3 15324 to_intel_plane_state(primary->state);
d032ffa0 15325
936e71e3 15326 plane_state->base.visible = crtc->active &&
b26d3ea3
ML
15327 primary_get_hw_state(to_intel_plane(primary));
15328
936e71e3 15329 if (plane_state->base.visible)
b26d3ea3 15330 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
98ec7739
VS
15331}
15332
30e984df 15333static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352 15334{
fac5e23e 15335 struct drm_i915_private *dev_priv = to_i915(dev);
24929352 15336 enum pipe pipe;
24929352
DV
15337 struct intel_crtc *crtc;
15338 struct intel_encoder *encoder;
15339 struct intel_connector *connector;
5358901f 15340 int i;
24929352 15341
565602d7
ML
15342 dev_priv->active_crtcs = 0;
15343
d3fcc808 15344 for_each_intel_crtc(dev, crtc) {
a8cd6da0
VS
15345 struct intel_crtc_state *crtc_state =
15346 to_intel_crtc_state(crtc->base.state);
3b117c8f 15347
ec2dc6a0 15348 __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
565602d7
ML
15349 memset(crtc_state, 0, sizeof(*crtc_state));
15350 crtc_state->base.crtc = &crtc->base;
24929352 15351
565602d7
ML
15352 crtc_state->base.active = crtc_state->base.enable =
15353 dev_priv->display.get_pipe_config(crtc, crtc_state);
15354
15355 crtc->base.enabled = crtc_state->base.enable;
15356 crtc->active = crtc_state->base.active;
15357
aca1ebf4 15358 if (crtc_state->base.active)
565602d7
ML
15359 dev_priv->active_crtcs |= 1 << crtc->pipe;
15360
f9cd7b88 15361 readout_plane_state(crtc);
24929352 15362
78108b7c
VS
15363 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
15364 crtc->base.base.id, crtc->base.name,
a8cd6da0 15365 enableddisabled(crtc_state->base.active));
24929352
DV
15366 }
15367
5358901f
DV
15368 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15369 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15370
2edd6443 15371 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
2c42e535
ACO
15372 &pll->state.hw_state);
15373 pll->state.crtc_mask = 0;
d3fcc808 15374 for_each_intel_crtc(dev, crtc) {
a8cd6da0
VS
15375 struct intel_crtc_state *crtc_state =
15376 to_intel_crtc_state(crtc->base.state);
15377
15378 if (crtc_state->base.active &&
15379 crtc_state->shared_dpll == pll)
2c42e535 15380 pll->state.crtc_mask |= 1 << crtc->pipe;
5358901f 15381 }
2c42e535 15382 pll->active_mask = pll->state.crtc_mask;
5358901f 15383
1e6f2ddc 15384 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
2c42e535 15385 pll->name, pll->state.crtc_mask, pll->on);
5358901f
DV
15386 }
15387
b2784e15 15388 for_each_intel_encoder(dev, encoder) {
24929352
DV
15389 pipe = 0;
15390
15391 if (encoder->get_hw_state(encoder, &pipe)) {
a8cd6da0
VS
15392 struct intel_crtc_state *crtc_state;
15393
98187836 15394 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
a8cd6da0 15395 crtc_state = to_intel_crtc_state(crtc->base.state);
e2af48c6 15396
045ac3b5 15397 encoder->base.crtc = &crtc->base;
a8cd6da0
VS
15398 crtc_state->output_types |= 1 << encoder->type;
15399 encoder->get_config(encoder, crtc_state);
24929352
DV
15400 } else {
15401 encoder->base.crtc = NULL;
15402 }
15403
6f2bcceb 15404 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
08c4d7fc
TU
15405 encoder->base.base.id, encoder->base.name,
15406 enableddisabled(encoder->base.crtc),
6f2bcceb 15407 pipe_name(pipe));
24929352
DV
15408 }
15409
3a3371ff 15410 for_each_intel_connector(dev, connector) {
24929352
DV
15411 if (connector->get_hw_state(connector)) {
15412 connector->base.dpms = DRM_MODE_DPMS_ON;
2aa974c9
ML
15413
15414 encoder = connector->encoder;
15415 connector->base.encoder = &encoder->base;
15416
15417 if (encoder->base.crtc &&
15418 encoder->base.crtc->state->active) {
15419 /*
15420 * This has to be done during hardware readout
15421 * because anything calling .crtc_disable may
15422 * rely on the connector_mask being accurate.
15423 */
15424 encoder->base.crtc->state->connector_mask |=
15425 1 << drm_connector_index(&connector->base);
e87a52b3
ML
15426 encoder->base.crtc->state->encoder_mask |=
15427 1 << drm_encoder_index(&encoder->base);
2aa974c9
ML
15428 }
15429
24929352
DV
15430 } else {
15431 connector->base.dpms = DRM_MODE_DPMS_OFF;
15432 connector->base.encoder = NULL;
15433 }
15434 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
08c4d7fc
TU
15435 connector->base.base.id, connector->base.name,
15436 enableddisabled(connector->base.encoder));
24929352 15437 }
7f4c6284
VS
15438
15439 for_each_intel_crtc(dev, crtc) {
a8cd6da0
VS
15440 struct intel_crtc_state *crtc_state =
15441 to_intel_crtc_state(crtc->base.state);
aca1ebf4
VS
15442 int pixclk = 0;
15443
a8cd6da0 15444 crtc->base.hwmode = crtc_state->base.adjusted_mode;
7f4c6284
VS
15445
15446 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
a8cd6da0
VS
15447 if (crtc_state->base.active) {
15448 intel_mode_from_pipe_config(&crtc->base.mode, crtc_state);
15449 intel_mode_from_pipe_config(&crtc_state->base.adjusted_mode, crtc_state);
7f4c6284
VS
15450 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
15451
15452 /*
15453 * The initial mode needs to be set in order to keep
15454 * the atomic core happy. It wants a valid mode if the
15455 * crtc's enabled, so we do the above call.
15456 *
7800fb69
DV
15457 * But we don't set all the derived state fully, hence
15458 * set a flag to indicate that a full recalculation is
15459 * needed on the next commit.
7f4c6284 15460 */
a8cd6da0 15461 crtc_state->base.mode.private_flags = I915_MODE_FLAG_INHERITED;
9eca6832 15462
a7d1b3f4
VS
15463 intel_crtc_compute_pixel_rate(crtc_state);
15464
15465 if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv) ||
15466 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
15467 pixclk = crtc_state->pixel_rate;
aca1ebf4
VS
15468 else
15469 WARN_ON(dev_priv->display.modeset_calc_cdclk);
15470
15471 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
a8cd6da0 15472 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
aca1ebf4
VS
15473 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
15474
9eca6832
VS
15475 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
15476 update_scanline_offset(crtc);
7f4c6284 15477 }
e3b247da 15478
aca1ebf4
VS
15479 dev_priv->min_pixclk[crtc->pipe] = pixclk;
15480
a8cd6da0 15481 intel_pipe_config_sanity_check(dev_priv, crtc_state);
7f4c6284 15482 }
30e984df
DV
15483}
15484
043e9bda
ML
15485/* Scan out the current hw modeset state,
15486 * and sanitizes it to the current state
15487 */
15488static void
15489intel_modeset_setup_hw_state(struct drm_device *dev)
30e984df 15490{
fac5e23e 15491 struct drm_i915_private *dev_priv = to_i915(dev);
30e984df 15492 enum pipe pipe;
30e984df
DV
15493 struct intel_crtc *crtc;
15494 struct intel_encoder *encoder;
35c95375 15495 int i;
30e984df
DV
15496
15497 intel_modeset_readout_hw_state(dev);
24929352
DV
15498
15499 /* HW state is read out, now we need to sanitize this mess. */
b2784e15 15500 for_each_intel_encoder(dev, encoder) {
24929352
DV
15501 intel_sanitize_encoder(encoder);
15502 }
15503
055e393f 15504 for_each_pipe(dev_priv, pipe) {
98187836 15505 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
e2af48c6 15506
24929352 15507 intel_sanitize_crtc(crtc);
6e3c9717
ACO
15508 intel_dump_pipe_config(crtc, crtc->config,
15509 "[setup_hw_state]");
24929352 15510 }
9a935856 15511
d29b2f9d
ACO
15512 intel_modeset_update_connector_atomic_state(dev);
15513
35c95375
DV
15514 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15515 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15516
2dd66ebd 15517 if (!pll->on || pll->active_mask)
35c95375
DV
15518 continue;
15519
15520 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15521
2edd6443 15522 pll->funcs.disable(dev_priv, pll);
35c95375
DV
15523 pll->on = false;
15524 }
15525
920a14b2 15526 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
6eb1a681 15527 vlv_wm_get_hw_state(dev);
5db94019 15528 else if (IS_GEN9(dev_priv))
3078999f 15529 skl_wm_get_hw_state(dev);
6e266956 15530 else if (HAS_PCH_SPLIT(dev_priv))
243e6a44 15531 ilk_wm_get_hw_state(dev);
292b990e
ML
15532
15533 for_each_intel_crtc(dev, crtc) {
d8fc70b7 15534 u64 put_domains;
292b990e 15535
74bff5f9 15536 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
292b990e
ML
15537 if (WARN_ON(put_domains))
15538 modeset_put_power_domains(dev_priv, put_domains);
15539 }
15540 intel_display_set_init_power(dev_priv, false);
010cf73d 15541
8d8c386c
ID
15542 intel_power_domains_verify_state(dev_priv);
15543
010cf73d 15544 intel_fbc_init_pipe_state(dev_priv);
043e9bda 15545}
7d0bc1ea 15546
043e9bda
ML
15547void intel_display_resume(struct drm_device *dev)
15548{
e2c8b870
ML
15549 struct drm_i915_private *dev_priv = to_i915(dev);
15550 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
15551 struct drm_modeset_acquire_ctx ctx;
043e9bda 15552 int ret;
f30da187 15553
e2c8b870 15554 dev_priv->modeset_restore_state = NULL;
73974893
ML
15555 if (state)
15556 state->acquire_ctx = &ctx;
043e9bda 15557
ea49c9ac
ML
15558 /*
15559 * This is a cludge because with real atomic modeset mode_config.mutex
15560 * won't be taken. Unfortunately some probed state like
15561 * audio_codec_enable is still protected by mode_config.mutex, so lock
15562 * it here for now.
15563 */
15564 mutex_lock(&dev->mode_config.mutex);
e2c8b870 15565 drm_modeset_acquire_init(&ctx, 0);
043e9bda 15566
73974893
ML
15567 while (1) {
15568 ret = drm_modeset_lock_all_ctx(dev, &ctx);
15569 if (ret != -EDEADLK)
15570 break;
043e9bda 15571
e2c8b870 15572 drm_modeset_backoff(&ctx);
e2c8b870 15573 }
043e9bda 15574
73974893
ML
15575 if (!ret)
15576 ret = __intel_display_resume(dev, state);
15577
e2c8b870
ML
15578 drm_modeset_drop_locks(&ctx);
15579 drm_modeset_acquire_fini(&ctx);
ea49c9ac 15580 mutex_unlock(&dev->mode_config.mutex);
043e9bda 15581
0853695c 15582 if (ret)
e2c8b870 15583 DRM_ERROR("Restoring old state failed with %i\n", ret);
3c5e37f1
CW
15584 if (state)
15585 drm_atomic_state_put(state);
2c7111db
CW
15586}
15587
15588void intel_modeset_gem_init(struct drm_device *dev)
15589{
dc97997a 15590 struct drm_i915_private *dev_priv = to_i915(dev);
484b41dd 15591
dc97997a 15592 intel_init_gt_powersave(dev_priv);
ae48434c 15593
1833b134 15594 intel_modeset_init_hw(dev);
02e792fb 15595
1ee8da6d 15596 intel_setup_overlay(dev_priv);
1ebaa0b9
CW
15597}
15598
15599int intel_connector_register(struct drm_connector *connector)
15600{
15601 struct intel_connector *intel_connector = to_intel_connector(connector);
15602 int ret;
15603
15604 ret = intel_backlight_device_register(intel_connector);
15605 if (ret)
15606 goto err;
15607
15608 return 0;
0962c3c9 15609
1ebaa0b9
CW
15610err:
15611 return ret;
79e53945
JB
15612}
15613
c191eca1 15614void intel_connector_unregister(struct drm_connector *connector)
4932e2c3 15615{
e63d87c0 15616 struct intel_connector *intel_connector = to_intel_connector(connector);
4932e2c3 15617
e63d87c0 15618 intel_backlight_device_unregister(intel_connector);
4932e2c3 15619 intel_panel_destroy_backlight(connector);
4932e2c3
ID
15620}
15621
79e53945
JB
15622void intel_modeset_cleanup(struct drm_device *dev)
15623{
fac5e23e 15624 struct drm_i915_private *dev_priv = to_i915(dev);
652c393a 15625
eb955eee
CW
15626 flush_work(&dev_priv->atomic_helper.free_work);
15627 WARN_ON(!llist_empty(&dev_priv->atomic_helper.free_list));
15628
dc97997a 15629 intel_disable_gt_powersave(dev_priv);
2eb5252e 15630
fd0c0642
DV
15631 /*
15632 * Interrupts and polling as the first thing to avoid creating havoc.
2eb5252e 15633 * Too much stuff here (turning of connectors, ...) would
fd0c0642
DV
15634 * experience fancy races otherwise.
15635 */
2aeb7d3a 15636 intel_irq_uninstall(dev_priv);
eb21b92b 15637
fd0c0642
DV
15638 /*
15639 * Due to the hpd irq storm handling the hotplug work can re-arm the
15640 * poll handlers. Hence disable polling after hpd handling is shut down.
15641 */
f87ea761 15642 drm_kms_helper_poll_fini(dev);
fd0c0642 15643
723bfd70
JB
15644 intel_unregister_dsm_handler();
15645
c937ab3e 15646 intel_fbc_global_disable(dev_priv);
69341a5e 15647
1630fe75
CW
15648 /* flush any delayed tasks or pending work */
15649 flush_scheduled_work();
15650
79e53945 15651 drm_mode_config_cleanup(dev);
4d7bb011 15652
1ee8da6d 15653 intel_cleanup_overlay(dev_priv);
ae48434c 15654
dc97997a 15655 intel_cleanup_gt_powersave(dev_priv);
f5949141 15656
40196446 15657 intel_teardown_gmbus(dev_priv);
79e53945
JB
15658}
15659
df0e9248
CW
15660void intel_connector_attach_encoder(struct intel_connector *connector,
15661 struct intel_encoder *encoder)
15662{
15663 connector->encoder = encoder;
15664 drm_mode_connector_attach_encoder(&connector->base,
15665 &encoder->base);
79e53945 15666}
28d52043
DA
15667
15668/*
15669 * set vga decode state - true == enable VGA decode
15670 */
6315b5d3 15671int intel_modeset_vga_set_state(struct drm_i915_private *dev_priv, bool state)
28d52043 15672{
6315b5d3 15673 unsigned reg = INTEL_GEN(dev_priv) >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
15674 u16 gmch_ctrl;
15675
75fa041d
CW
15676 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15677 DRM_ERROR("failed to read control word\n");
15678 return -EIO;
15679 }
15680
c0cc8a55
CW
15681 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15682 return 0;
15683
28d52043
DA
15684 if (state)
15685 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15686 else
15687 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
15688
15689 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15690 DRM_ERROR("failed to write control word\n");
15691 return -EIO;
15692 }
15693
28d52043
DA
15694 return 0;
15695}
c4a1d9e4 15696
98a2f411
CW
15697#if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
15698
c4a1d9e4 15699struct intel_display_error_state {
ff57f1b0
PZ
15700
15701 u32 power_well_driver;
15702
63b66e5b
CW
15703 int num_transcoders;
15704
c4a1d9e4
CW
15705 struct intel_cursor_error_state {
15706 u32 control;
15707 u32 position;
15708 u32 base;
15709 u32 size;
52331309 15710 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
15711
15712 struct intel_pipe_error_state {
ddf9c536 15713 bool power_domain_on;
c4a1d9e4 15714 u32 source;
f301b1e1 15715 u32 stat;
52331309 15716 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
15717
15718 struct intel_plane_error_state {
15719 u32 control;
15720 u32 stride;
15721 u32 size;
15722 u32 pos;
15723 u32 addr;
15724 u32 surface;
15725 u32 tile_offset;
52331309 15726 } plane[I915_MAX_PIPES];
63b66e5b
CW
15727
15728 struct intel_transcoder_error_state {
ddf9c536 15729 bool power_domain_on;
63b66e5b
CW
15730 enum transcoder cpu_transcoder;
15731
15732 u32 conf;
15733
15734 u32 htotal;
15735 u32 hblank;
15736 u32 hsync;
15737 u32 vtotal;
15738 u32 vblank;
15739 u32 vsync;
15740 } transcoder[4];
c4a1d9e4
CW
15741};
15742
15743struct intel_display_error_state *
c033666a 15744intel_display_capture_error_state(struct drm_i915_private *dev_priv)
c4a1d9e4 15745{
c4a1d9e4 15746 struct intel_display_error_state *error;
63b66e5b
CW
15747 int transcoders[] = {
15748 TRANSCODER_A,
15749 TRANSCODER_B,
15750 TRANSCODER_C,
15751 TRANSCODER_EDP,
15752 };
c4a1d9e4
CW
15753 int i;
15754
c033666a 15755 if (INTEL_INFO(dev_priv)->num_pipes == 0)
63b66e5b
CW
15756 return NULL;
15757
9d1cb914 15758 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
15759 if (error == NULL)
15760 return NULL;
15761
c033666a 15762 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
ff57f1b0
PZ
15763 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
15764
055e393f 15765 for_each_pipe(dev_priv, i) {
ddf9c536 15766 error->pipe[i].power_domain_on =
f458ebbc
DV
15767 __intel_display_power_is_enabled(dev_priv,
15768 POWER_DOMAIN_PIPE(i));
ddf9c536 15769 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
15770 continue;
15771
5efb3e28
VS
15772 error->cursor[i].control = I915_READ(CURCNTR(i));
15773 error->cursor[i].position = I915_READ(CURPOS(i));
15774 error->cursor[i].base = I915_READ(CURBASE(i));
c4a1d9e4
CW
15775
15776 error->plane[i].control = I915_READ(DSPCNTR(i));
15777 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
c033666a 15778 if (INTEL_GEN(dev_priv) <= 3) {
51889b35 15779 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
15780 error->plane[i].pos = I915_READ(DSPPOS(i));
15781 }
c033666a 15782 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
ca291363 15783 error->plane[i].addr = I915_READ(DSPADDR(i));
c033666a 15784 if (INTEL_GEN(dev_priv) >= 4) {
c4a1d9e4
CW
15785 error->plane[i].surface = I915_READ(DSPSURF(i));
15786 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15787 }
15788
c4a1d9e4 15789 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1 15790
c033666a 15791 if (HAS_GMCH_DISPLAY(dev_priv))
f301b1e1 15792 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
15793 }
15794
4d1de975 15795 /* Note: this does not include DSI transcoders. */
c033666a 15796 error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
2d1fe073 15797 if (HAS_DDI(dev_priv))
63b66e5b
CW
15798 error->num_transcoders++; /* Account for eDP. */
15799
15800 for (i = 0; i < error->num_transcoders; i++) {
15801 enum transcoder cpu_transcoder = transcoders[i];
15802
ddf9c536 15803 error->transcoder[i].power_domain_on =
f458ebbc 15804 __intel_display_power_is_enabled(dev_priv,
38cc1daf 15805 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 15806 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
15807 continue;
15808
63b66e5b
CW
15809 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15810
15811 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15812 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15813 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15814 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15815 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15816 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15817 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
15818 }
15819
15820 return error;
15821}
15822
edc3d884
MK
15823#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15824
c4a1d9e4 15825void
edc3d884 15826intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
15827 struct intel_display_error_state *error)
15828{
5a4c6f1b 15829 struct drm_i915_private *dev_priv = m->i915;
c4a1d9e4
CW
15830 int i;
15831
63b66e5b
CW
15832 if (!error)
15833 return;
15834
b7f05d4a 15835 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev_priv)->num_pipes);
8652744b 15836 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
edc3d884 15837 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 15838 error->power_well_driver);
055e393f 15839 for_each_pipe(dev_priv, i) {
edc3d884 15840 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536 15841 err_printf(m, " Power: %s\n",
87ad3212 15842 onoff(error->pipe[i].power_domain_on));
edc3d884 15843 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 15844 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
15845
15846 err_printf(m, "Plane [%d]:\n", i);
15847 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15848 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
5f56d5f9 15849 if (INTEL_GEN(dev_priv) <= 3) {
edc3d884
MK
15850 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15851 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 15852 }
772c2a51 15853 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
edc3d884 15854 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
5f56d5f9 15855 if (INTEL_GEN(dev_priv) >= 4) {
edc3d884
MK
15856 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15857 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
15858 }
15859
edc3d884
MK
15860 err_printf(m, "Cursor [%d]:\n", i);
15861 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15862 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15863 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 15864 }
63b66e5b
CW
15865
15866 for (i = 0; i < error->num_transcoders; i++) {
da205630 15867 err_printf(m, "CPU transcoder: %s\n",
63b66e5b 15868 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536 15869 err_printf(m, " Power: %s\n",
87ad3212 15870 onoff(error->transcoder[i].power_domain_on));
63b66e5b
CW
15871 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15872 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15873 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15874 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15875 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15876 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
15877 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
15878 }
c4a1d9e4 15879}
98a2f411
CW
15880
15881#endif