]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpu/drm/i915/intel_display.c
drm/i915: Pass intel_crtc to DDI functions called from crtc en/disable
[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
4cbe4b2b 3649static void intel_fdi_normal_train(struct intel_crtc *crtc)
5e84e1a4 3650{
4cbe4b2b 3651 struct drm_device *dev = crtc->base.dev;
fac5e23e 3652 struct drm_i915_private *dev_priv = to_i915(dev);
4cbe4b2b 3653 int pipe = crtc->pipe;
f0f59a00
VS
3654 i915_reg_t reg;
3655 u32 temp;
5e84e1a4
ZW
3656
3657 /* enable normal train */
3658 reg = FDI_TX_CTL(pipe);
3659 temp = I915_READ(reg);
fd6b8f43 3660 if (IS_IVYBRIDGE(dev_priv)) {
357555c0
JB
3661 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3662 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
3663 } else {
3664 temp &= ~FDI_LINK_TRAIN_NONE;
3665 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 3666 }
5e84e1a4
ZW
3667 I915_WRITE(reg, temp);
3668
3669 reg = FDI_RX_CTL(pipe);
3670 temp = I915_READ(reg);
6e266956 3671 if (HAS_PCH_CPT(dev_priv)) {
5e84e1a4
ZW
3672 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3673 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3674 } else {
3675 temp &= ~FDI_LINK_TRAIN_NONE;
3676 temp |= FDI_LINK_TRAIN_NONE;
3677 }
3678 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3679
3680 /* wait one idle pattern time */
3681 POSTING_READ(reg);
3682 udelay(1000);
357555c0
JB
3683
3684 /* IVB wants error correction enabled */
fd6b8f43 3685 if (IS_IVYBRIDGE(dev_priv))
357555c0
JB
3686 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3687 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
3688}
3689
8db9d77b 3690/* The FDI link training functions for ILK/Ibexpeak. */
dc4a1094
ACO
3691static void ironlake_fdi_link_train(struct intel_crtc *crtc,
3692 const struct intel_crtc_state *crtc_state)
8db9d77b 3693{
4cbe4b2b 3694 struct drm_device *dev = crtc->base.dev;
fac5e23e 3695 struct drm_i915_private *dev_priv = to_i915(dev);
4cbe4b2b 3696 int pipe = crtc->pipe;
f0f59a00
VS
3697 i915_reg_t reg;
3698 u32 temp, tries;
8db9d77b 3699
1c8562f6 3700 /* FDI needs bits from pipe first */
0fc932b8 3701 assert_pipe_enabled(dev_priv, pipe);
0fc932b8 3702
e1a44743
AJ
3703 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3704 for train result */
5eddb70b
CW
3705 reg = FDI_RX_IMR(pipe);
3706 temp = I915_READ(reg);
e1a44743
AJ
3707 temp &= ~FDI_RX_SYMBOL_LOCK;
3708 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3709 I915_WRITE(reg, temp);
3710 I915_READ(reg);
e1a44743
AJ
3711 udelay(150);
3712
8db9d77b 3713 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3714 reg = FDI_TX_CTL(pipe);
3715 temp = I915_READ(reg);
627eb5a3 3716 temp &= ~FDI_DP_PORT_WIDTH_MASK;
dc4a1094 3717 temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
8db9d77b
ZW
3718 temp &= ~FDI_LINK_TRAIN_NONE;
3719 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 3720 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3721
5eddb70b
CW
3722 reg = FDI_RX_CTL(pipe);
3723 temp = I915_READ(reg);
8db9d77b
ZW
3724 temp &= ~FDI_LINK_TRAIN_NONE;
3725 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b
CW
3726 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3727
3728 POSTING_READ(reg);
8db9d77b
ZW
3729 udelay(150);
3730
5b2adf89 3731 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
DV
3732 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3733 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3734 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 3735
5eddb70b 3736 reg = FDI_RX_IIR(pipe);
e1a44743 3737 for (tries = 0; tries < 5; tries++) {
5eddb70b 3738 temp = I915_READ(reg);
8db9d77b
ZW
3739 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3740
3741 if ((temp & FDI_RX_BIT_LOCK)) {
3742 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 3743 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
3744 break;
3745 }
8db9d77b 3746 }
e1a44743 3747 if (tries == 5)
5eddb70b 3748 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3749
3750 /* Train 2 */
5eddb70b
CW
3751 reg = FDI_TX_CTL(pipe);
3752 temp = I915_READ(reg);
8db9d77b
ZW
3753 temp &= ~FDI_LINK_TRAIN_NONE;
3754 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3755 I915_WRITE(reg, temp);
8db9d77b 3756
5eddb70b
CW
3757 reg = FDI_RX_CTL(pipe);
3758 temp = I915_READ(reg);
8db9d77b
ZW
3759 temp &= ~FDI_LINK_TRAIN_NONE;
3760 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3761 I915_WRITE(reg, temp);
8db9d77b 3762
5eddb70b
CW
3763 POSTING_READ(reg);
3764 udelay(150);
8db9d77b 3765
5eddb70b 3766 reg = FDI_RX_IIR(pipe);
e1a44743 3767 for (tries = 0; tries < 5; tries++) {
5eddb70b 3768 temp = I915_READ(reg);
8db9d77b
ZW
3769 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3770
3771 if (temp & FDI_RX_SYMBOL_LOCK) {
5eddb70b 3772 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
3773 DRM_DEBUG_KMS("FDI train 2 done.\n");
3774 break;
3775 }
8db9d77b 3776 }
e1a44743 3777 if (tries == 5)
5eddb70b 3778 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3779
3780 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 3781
8db9d77b
ZW
3782}
3783
0206e353 3784static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
3785 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3786 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3787 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3788 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3789};
3790
3791/* The FDI link training functions for SNB/Cougarpoint. */
dc4a1094
ACO
3792static void gen6_fdi_link_train(struct intel_crtc *crtc,
3793 const struct intel_crtc_state *crtc_state)
8db9d77b 3794{
4cbe4b2b 3795 struct drm_device *dev = crtc->base.dev;
fac5e23e 3796 struct drm_i915_private *dev_priv = to_i915(dev);
4cbe4b2b 3797 int pipe = crtc->pipe;
f0f59a00
VS
3798 i915_reg_t reg;
3799 u32 temp, i, retry;
8db9d77b 3800
e1a44743
AJ
3801 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3802 for train result */
5eddb70b
CW
3803 reg = FDI_RX_IMR(pipe);
3804 temp = I915_READ(reg);
e1a44743
AJ
3805 temp &= ~FDI_RX_SYMBOL_LOCK;
3806 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3807 I915_WRITE(reg, temp);
3808
3809 POSTING_READ(reg);
e1a44743
AJ
3810 udelay(150);
3811
8db9d77b 3812 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3813 reg = FDI_TX_CTL(pipe);
3814 temp = I915_READ(reg);
627eb5a3 3815 temp &= ~FDI_DP_PORT_WIDTH_MASK;
dc4a1094 3816 temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
8db9d77b
ZW
3817 temp &= ~FDI_LINK_TRAIN_NONE;
3818 temp |= FDI_LINK_TRAIN_PATTERN_1;
3819 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3820 /* SNB-B */
3821 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 3822 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3823
d74cf324
DV
3824 I915_WRITE(FDI_RX_MISC(pipe),
3825 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3826
5eddb70b
CW
3827 reg = FDI_RX_CTL(pipe);
3828 temp = I915_READ(reg);
6e266956 3829 if (HAS_PCH_CPT(dev_priv)) {
8db9d77b
ZW
3830 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3831 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3832 } else {
3833 temp &= ~FDI_LINK_TRAIN_NONE;
3834 temp |= FDI_LINK_TRAIN_PATTERN_1;
3835 }
5eddb70b
CW
3836 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3837
3838 POSTING_READ(reg);
8db9d77b
ZW
3839 udelay(150);
3840
0206e353 3841 for (i = 0; i < 4; i++) {
5eddb70b
CW
3842 reg = FDI_TX_CTL(pipe);
3843 temp = I915_READ(reg);
8db9d77b
ZW
3844 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3845 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3846 I915_WRITE(reg, temp);
3847
3848 POSTING_READ(reg);
8db9d77b
ZW
3849 udelay(500);
3850
fa37d39e
SP
3851 for (retry = 0; retry < 5; retry++) {
3852 reg = FDI_RX_IIR(pipe);
3853 temp = I915_READ(reg);
3854 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3855 if (temp & FDI_RX_BIT_LOCK) {
3856 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3857 DRM_DEBUG_KMS("FDI train 1 done.\n");
3858 break;
3859 }
3860 udelay(50);
8db9d77b 3861 }
fa37d39e
SP
3862 if (retry < 5)
3863 break;
8db9d77b
ZW
3864 }
3865 if (i == 4)
5eddb70b 3866 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3867
3868 /* Train 2 */
5eddb70b
CW
3869 reg = FDI_TX_CTL(pipe);
3870 temp = I915_READ(reg);
8db9d77b
ZW
3871 temp &= ~FDI_LINK_TRAIN_NONE;
3872 temp |= FDI_LINK_TRAIN_PATTERN_2;
5db94019 3873 if (IS_GEN6(dev_priv)) {
8db9d77b
ZW
3874 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3875 /* SNB-B */
3876 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3877 }
5eddb70b 3878 I915_WRITE(reg, temp);
8db9d77b 3879
5eddb70b
CW
3880 reg = FDI_RX_CTL(pipe);
3881 temp = I915_READ(reg);
6e266956 3882 if (HAS_PCH_CPT(dev_priv)) {
8db9d77b
ZW
3883 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3884 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3885 } else {
3886 temp &= ~FDI_LINK_TRAIN_NONE;
3887 temp |= FDI_LINK_TRAIN_PATTERN_2;
3888 }
5eddb70b
CW
3889 I915_WRITE(reg, temp);
3890
3891 POSTING_READ(reg);
8db9d77b
ZW
3892 udelay(150);
3893
0206e353 3894 for (i = 0; i < 4; i++) {
5eddb70b
CW
3895 reg = FDI_TX_CTL(pipe);
3896 temp = I915_READ(reg);
8db9d77b
ZW
3897 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3898 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3899 I915_WRITE(reg, temp);
3900
3901 POSTING_READ(reg);
8db9d77b
ZW
3902 udelay(500);
3903
fa37d39e
SP
3904 for (retry = 0; retry < 5; retry++) {
3905 reg = FDI_RX_IIR(pipe);
3906 temp = I915_READ(reg);
3907 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3908 if (temp & FDI_RX_SYMBOL_LOCK) {
3909 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3910 DRM_DEBUG_KMS("FDI train 2 done.\n");
3911 break;
3912 }
3913 udelay(50);
8db9d77b 3914 }
fa37d39e
SP
3915 if (retry < 5)
3916 break;
8db9d77b
ZW
3917 }
3918 if (i == 4)
5eddb70b 3919 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3920
3921 DRM_DEBUG_KMS("FDI train done.\n");
3922}
3923
357555c0 3924/* Manual link training for Ivy Bridge A0 parts */
dc4a1094
ACO
3925static void ivb_manual_fdi_link_train(struct intel_crtc *crtc,
3926 const struct intel_crtc_state *crtc_state)
357555c0 3927{
4cbe4b2b 3928 struct drm_device *dev = crtc->base.dev;
fac5e23e 3929 struct drm_i915_private *dev_priv = to_i915(dev);
4cbe4b2b 3930 int pipe = crtc->pipe;
f0f59a00
VS
3931 i915_reg_t reg;
3932 u32 temp, i, j;
357555c0
JB
3933
3934 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3935 for train result */
3936 reg = FDI_RX_IMR(pipe);
3937 temp = I915_READ(reg);
3938 temp &= ~FDI_RX_SYMBOL_LOCK;
3939 temp &= ~FDI_RX_BIT_LOCK;
3940 I915_WRITE(reg, temp);
3941
3942 POSTING_READ(reg);
3943 udelay(150);
3944
01a415fd
DV
3945 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3946 I915_READ(FDI_RX_IIR(pipe)));
3947
139ccd3f
JB
3948 /* Try each vswing and preemphasis setting twice before moving on */
3949 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3950 /* disable first in case we need to retry */
3951 reg = FDI_TX_CTL(pipe);
3952 temp = I915_READ(reg);
3953 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3954 temp &= ~FDI_TX_ENABLE;
3955 I915_WRITE(reg, temp);
357555c0 3956
139ccd3f
JB
3957 reg = FDI_RX_CTL(pipe);
3958 temp = I915_READ(reg);
3959 temp &= ~FDI_LINK_TRAIN_AUTO;
3960 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3961 temp &= ~FDI_RX_ENABLE;
3962 I915_WRITE(reg, temp);
357555c0 3963
139ccd3f 3964 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
3965 reg = FDI_TX_CTL(pipe);
3966 temp = I915_READ(reg);
139ccd3f 3967 temp &= ~FDI_DP_PORT_WIDTH_MASK;
dc4a1094 3968 temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
139ccd3f 3969 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 3970 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
3971 temp |= snb_b_fdi_train_param[j/2];
3972 temp |= FDI_COMPOSITE_SYNC;
3973 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 3974
139ccd3f
JB
3975 I915_WRITE(FDI_RX_MISC(pipe),
3976 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 3977
139ccd3f 3978 reg = FDI_RX_CTL(pipe);
357555c0 3979 temp = I915_READ(reg);
139ccd3f
JB
3980 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3981 temp |= FDI_COMPOSITE_SYNC;
3982 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 3983
139ccd3f
JB
3984 POSTING_READ(reg);
3985 udelay(1); /* should be 0.5us */
357555c0 3986
139ccd3f
JB
3987 for (i = 0; i < 4; i++) {
3988 reg = FDI_RX_IIR(pipe);
3989 temp = I915_READ(reg);
3990 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3991
139ccd3f
JB
3992 if (temp & FDI_RX_BIT_LOCK ||
3993 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3994 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3995 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3996 i);
3997 break;
3998 }
3999 udelay(1); /* should be 0.5us */
4000 }
4001 if (i == 4) {
4002 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
4003 continue;
4004 }
357555c0 4005
139ccd3f 4006 /* Train 2 */
357555c0
JB
4007 reg = FDI_TX_CTL(pipe);
4008 temp = I915_READ(reg);
139ccd3f
JB
4009 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
4010 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
4011 I915_WRITE(reg, temp);
4012
4013 reg = FDI_RX_CTL(pipe);
4014 temp = I915_READ(reg);
4015 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4016 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
4017 I915_WRITE(reg, temp);
4018
4019 POSTING_READ(reg);
139ccd3f 4020 udelay(2); /* should be 1.5us */
357555c0 4021
139ccd3f
JB
4022 for (i = 0; i < 4; i++) {
4023 reg = FDI_RX_IIR(pipe);
4024 temp = I915_READ(reg);
4025 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 4026
139ccd3f
JB
4027 if (temp & FDI_RX_SYMBOL_LOCK ||
4028 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
4029 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
4030 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
4031 i);
4032 goto train_done;
4033 }
4034 udelay(2); /* should be 1.5us */
357555c0 4035 }
139ccd3f
JB
4036 if (i == 4)
4037 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 4038 }
357555c0 4039
139ccd3f 4040train_done:
357555c0
JB
4041 DRM_DEBUG_KMS("FDI train done.\n");
4042}
4043
88cefb6c 4044static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 4045{
88cefb6c 4046 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 4047 struct drm_i915_private *dev_priv = to_i915(dev);
2c07245f 4048 int pipe = intel_crtc->pipe;
f0f59a00
VS
4049 i915_reg_t reg;
4050 u32 temp;
c64e311e 4051
c98e9dcf 4052 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
4053 reg = FDI_RX_CTL(pipe);
4054 temp = I915_READ(reg);
627eb5a3 4055 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
6e3c9717 4056 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
dfd07d72 4057 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
4058 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
4059
4060 POSTING_READ(reg);
c98e9dcf
JB
4061 udelay(200);
4062
4063 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
4064 temp = I915_READ(reg);
4065 I915_WRITE(reg, temp | FDI_PCDCLK);
4066
4067 POSTING_READ(reg);
c98e9dcf
JB
4068 udelay(200);
4069
20749730
PZ
4070 /* Enable CPU FDI TX PLL, always on for Ironlake */
4071 reg = FDI_TX_CTL(pipe);
4072 temp = I915_READ(reg);
4073 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
4074 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 4075
20749730
PZ
4076 POSTING_READ(reg);
4077 udelay(100);
6be4a607 4078 }
0e23b99d
JB
4079}
4080
88cefb6c
DV
4081static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
4082{
4083 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 4084 struct drm_i915_private *dev_priv = to_i915(dev);
88cefb6c 4085 int pipe = intel_crtc->pipe;
f0f59a00
VS
4086 i915_reg_t reg;
4087 u32 temp;
88cefb6c
DV
4088
4089 /* Switch from PCDclk to Rawclk */
4090 reg = FDI_RX_CTL(pipe);
4091 temp = I915_READ(reg);
4092 I915_WRITE(reg, temp & ~FDI_PCDCLK);
4093
4094 /* Disable CPU FDI TX PLL */
4095 reg = FDI_TX_CTL(pipe);
4096 temp = I915_READ(reg);
4097 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
4098
4099 POSTING_READ(reg);
4100 udelay(100);
4101
4102 reg = FDI_RX_CTL(pipe);
4103 temp = I915_READ(reg);
4104 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
4105
4106 /* Wait for the clocks to turn off. */
4107 POSTING_READ(reg);
4108 udelay(100);
4109}
4110
0fc932b8
JB
4111static void ironlake_fdi_disable(struct drm_crtc *crtc)
4112{
4113 struct drm_device *dev = crtc->dev;
fac5e23e 4114 struct drm_i915_private *dev_priv = to_i915(dev);
0fc932b8
JB
4115 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4116 int pipe = intel_crtc->pipe;
f0f59a00
VS
4117 i915_reg_t reg;
4118 u32 temp;
0fc932b8
JB
4119
4120 /* disable CPU FDI tx and PCH FDI rx */
4121 reg = FDI_TX_CTL(pipe);
4122 temp = I915_READ(reg);
4123 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
4124 POSTING_READ(reg);
4125
4126 reg = FDI_RX_CTL(pipe);
4127 temp = I915_READ(reg);
4128 temp &= ~(0x7 << 16);
dfd07d72 4129 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
4130 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
4131
4132 POSTING_READ(reg);
4133 udelay(100);
4134
4135 /* Ironlake workaround, disable clock pointer after downing FDI */
6e266956 4136 if (HAS_PCH_IBX(dev_priv))
6f06ce18 4137 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
0fc932b8
JB
4138
4139 /* still set train pattern 1 */
4140 reg = FDI_TX_CTL(pipe);
4141 temp = I915_READ(reg);
4142 temp &= ~FDI_LINK_TRAIN_NONE;
4143 temp |= FDI_LINK_TRAIN_PATTERN_1;
4144 I915_WRITE(reg, temp);
4145
4146 reg = FDI_RX_CTL(pipe);
4147 temp = I915_READ(reg);
6e266956 4148 if (HAS_PCH_CPT(dev_priv)) {
0fc932b8
JB
4149 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4150 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
4151 } else {
4152 temp &= ~FDI_LINK_TRAIN_NONE;
4153 temp |= FDI_LINK_TRAIN_PATTERN_1;
4154 }
4155 /* BPC in FDI rx is consistent with that in PIPECONF */
4156 temp &= ~(0x07 << 16);
dfd07d72 4157 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
4158 I915_WRITE(reg, temp);
4159
4160 POSTING_READ(reg);
4161 udelay(100);
4162}
4163
49d73912 4164bool intel_has_pending_fb_unpin(struct drm_i915_private *dev_priv)
5dce5b93
CW
4165{
4166 struct intel_crtc *crtc;
4167
4168 /* Note that we don't need to be called with mode_config.lock here
4169 * as our list of CRTC objects is static for the lifetime of the
4170 * device and so cannot disappear as we iterate. Similarly, we can
4171 * happily treat the predicates as racy, atomic checks as userspace
4172 * cannot claim and pin a new fb without at least acquring the
4173 * struct_mutex and so serialising with us.
4174 */
49d73912 4175 for_each_intel_crtc(&dev_priv->drm, crtc) {
5dce5b93
CW
4176 if (atomic_read(&crtc->unpin_work_count) == 0)
4177 continue;
4178
5a21b665 4179 if (crtc->flip_work)
0f0f74bc 4180 intel_wait_for_vblank(dev_priv, crtc->pipe);
5dce5b93
CW
4181
4182 return true;
4183 }
4184
4185 return false;
4186}
4187
5a21b665 4188static void page_flip_completed(struct intel_crtc *intel_crtc)
d6bbafa1
CW
4189{
4190 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
5a21b665
DV
4191 struct intel_flip_work *work = intel_crtc->flip_work;
4192
4193 intel_crtc->flip_work = NULL;
d6bbafa1
CW
4194
4195 if (work->event)
560ce1dc 4196 drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
d6bbafa1
CW
4197
4198 drm_crtc_vblank_put(&intel_crtc->base);
4199
5a21b665 4200 wake_up_all(&dev_priv->pending_flip_queue);
5a21b665
DV
4201 trace_i915_flip_complete(intel_crtc->plane,
4202 work->pending_flip_obj);
05c41f92
AR
4203
4204 queue_work(dev_priv->wq, &work->unpin_work);
d6bbafa1
CW
4205}
4206
5008e874 4207static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
e6c3a2a6 4208{
0f91128d 4209 struct drm_device *dev = crtc->dev;
fac5e23e 4210 struct drm_i915_private *dev_priv = to_i915(dev);
5008e874 4211 long ret;
e6c3a2a6 4212
2c10d571 4213 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
5008e874
ML
4214
4215 ret = wait_event_interruptible_timeout(
4216 dev_priv->pending_flip_queue,
4217 !intel_crtc_has_pending_flip(crtc),
4218 60*HZ);
4219
4220 if (ret < 0)
4221 return ret;
4222
5a21b665
DV
4223 if (ret == 0) {
4224 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4225 struct intel_flip_work *work;
4226
4227 spin_lock_irq(&dev->event_lock);
4228 work = intel_crtc->flip_work;
4229 if (work && !is_mmio_work(work)) {
4230 WARN_ONCE(1, "Removing stuck page flip\n");
4231 page_flip_completed(intel_crtc);
4232 }
4233 spin_unlock_irq(&dev->event_lock);
4234 }
5bb61643 4235
5008e874 4236 return 0;
e6c3a2a6
CW
4237}
4238
b7076546 4239void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
060f02d8
VS
4240{
4241 u32 temp;
4242
4243 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
4244
4245 mutex_lock(&dev_priv->sb_lock);
4246
4247 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4248 temp |= SBI_SSCCTL_DISABLE;
4249 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
4250
4251 mutex_unlock(&dev_priv->sb_lock);
4252}
4253
e615efe4 4254/* Program iCLKIP clock to the desired frequency */
0dcdc382 4255static void lpt_program_iclkip(struct intel_crtc *crtc)
e615efe4 4256{
0dcdc382
ACO
4257 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4258 int clock = crtc->config->base.adjusted_mode.crtc_clock;
e615efe4
ED
4259 u32 divsel, phaseinc, auxdiv, phasedir = 0;
4260 u32 temp;
4261
060f02d8 4262 lpt_disable_iclkip(dev_priv);
e615efe4 4263
64b46a06
VS
4264 /* The iCLK virtual clock root frequency is in MHz,
4265 * but the adjusted_mode->crtc_clock in in KHz. To get the
4266 * divisors, it is necessary to divide one by another, so we
4267 * convert the virtual clock precision to KHz here for higher
4268 * precision.
4269 */
4270 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
e615efe4
ED
4271 u32 iclk_virtual_root_freq = 172800 * 1000;
4272 u32 iclk_pi_range = 64;
64b46a06 4273 u32 desired_divisor;
e615efe4 4274
64b46a06
VS
4275 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4276 clock << auxdiv);
4277 divsel = (desired_divisor / iclk_pi_range) - 2;
4278 phaseinc = desired_divisor % iclk_pi_range;
e615efe4 4279
64b46a06
VS
4280 /*
4281 * Near 20MHz is a corner case which is
4282 * out of range for the 7-bit divisor
4283 */
4284 if (divsel <= 0x7f)
4285 break;
e615efe4
ED
4286 }
4287
4288 /* This should not happen with any sane values */
4289 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
4290 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
4291 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
4292 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
4293
4294 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 4295 clock,
e615efe4
ED
4296 auxdiv,
4297 divsel,
4298 phasedir,
4299 phaseinc);
4300
060f02d8
VS
4301 mutex_lock(&dev_priv->sb_lock);
4302
e615efe4 4303 /* Program SSCDIVINTPHASE6 */
988d6ee8 4304 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
4305 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
4306 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
4307 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
4308 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
4309 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
4310 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 4311 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
4312
4313 /* Program SSCAUXDIV */
988d6ee8 4314 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
4315 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
4316 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 4317 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
4318
4319 /* Enable modulator and associated divider */
988d6ee8 4320 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 4321 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 4322 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4 4323
060f02d8
VS
4324 mutex_unlock(&dev_priv->sb_lock);
4325
e615efe4
ED
4326 /* Wait for initialization time */
4327 udelay(24);
4328
4329 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
4330}
4331
8802e5b6
VS
4332int lpt_get_iclkip(struct drm_i915_private *dev_priv)
4333{
4334 u32 divsel, phaseinc, auxdiv;
4335 u32 iclk_virtual_root_freq = 172800 * 1000;
4336 u32 iclk_pi_range = 64;
4337 u32 desired_divisor;
4338 u32 temp;
4339
4340 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
4341 return 0;
4342
4343 mutex_lock(&dev_priv->sb_lock);
4344
4345 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4346 if (temp & SBI_SSCCTL_DISABLE) {
4347 mutex_unlock(&dev_priv->sb_lock);
4348 return 0;
4349 }
4350
4351 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
4352 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
4353 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
4354 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
4355 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
4356
4357 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
4358 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
4359 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
4360
4361 mutex_unlock(&dev_priv->sb_lock);
4362
4363 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
4364
4365 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4366 desired_divisor << auxdiv);
4367}
4368
275f01b2
DV
4369static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4370 enum pipe pch_transcoder)
4371{
4372 struct drm_device *dev = crtc->base.dev;
fac5e23e 4373 struct drm_i915_private *dev_priv = to_i915(dev);
6e3c9717 4374 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
275f01b2
DV
4375
4376 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4377 I915_READ(HTOTAL(cpu_transcoder)));
4378 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4379 I915_READ(HBLANK(cpu_transcoder)));
4380 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4381 I915_READ(HSYNC(cpu_transcoder)));
4382
4383 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4384 I915_READ(VTOTAL(cpu_transcoder)));
4385 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4386 I915_READ(VBLANK(cpu_transcoder)));
4387 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4388 I915_READ(VSYNC(cpu_transcoder)));
4389 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4390 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4391}
4392
003632d9 4393static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
1fbc0d78 4394{
fac5e23e 4395 struct drm_i915_private *dev_priv = to_i915(dev);
1fbc0d78
DV
4396 uint32_t temp;
4397
4398 temp = I915_READ(SOUTH_CHICKEN1);
003632d9 4399 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
1fbc0d78
DV
4400 return;
4401
4402 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4403 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4404
003632d9
ACO
4405 temp &= ~FDI_BC_BIFURCATION_SELECT;
4406 if (enable)
4407 temp |= FDI_BC_BIFURCATION_SELECT;
4408
4409 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
1fbc0d78
DV
4410 I915_WRITE(SOUTH_CHICKEN1, temp);
4411 POSTING_READ(SOUTH_CHICKEN1);
4412}
4413
4414static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4415{
4416 struct drm_device *dev = intel_crtc->base.dev;
1fbc0d78
DV
4417
4418 switch (intel_crtc->pipe) {
4419 case PIPE_A:
4420 break;
4421 case PIPE_B:
6e3c9717 4422 if (intel_crtc->config->fdi_lanes > 2)
003632d9 4423 cpt_set_fdi_bc_bifurcation(dev, false);
1fbc0d78 4424 else
003632d9 4425 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4426
4427 break;
4428 case PIPE_C:
003632d9 4429 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4430
4431 break;
4432 default:
4433 BUG();
4434 }
4435}
4436
c48b5305
VS
4437/* Return which DP Port should be selected for Transcoder DP control */
4438static enum port
4cbe4b2b 4439intel_trans_dp_port_sel(struct intel_crtc *crtc)
c48b5305 4440{
4cbe4b2b 4441 struct drm_device *dev = crtc->base.dev;
c48b5305
VS
4442 struct intel_encoder *encoder;
4443
4cbe4b2b 4444 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
cca0502b 4445 if (encoder->type == INTEL_OUTPUT_DP ||
c48b5305
VS
4446 encoder->type == INTEL_OUTPUT_EDP)
4447 return enc_to_dig_port(&encoder->base)->port;
4448 }
4449
4450 return -1;
4451}
4452
f67a559d
JB
4453/*
4454 * Enable PCH resources required for PCH ports:
4455 * - PCH PLLs
4456 * - FDI training & RX/TX
4457 * - update transcoder timings
4458 * - DP transcoding bits
4459 * - transcoder
4460 */
2ce42273 4461static void ironlake_pch_enable(const struct intel_crtc_state *crtc_state)
0e23b99d 4462{
2ce42273 4463 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
4cbe4b2b 4464 struct drm_device *dev = crtc->base.dev;
fac5e23e 4465 struct drm_i915_private *dev_priv = to_i915(dev);
4cbe4b2b 4466 int pipe = crtc->pipe;
f0f59a00 4467 u32 temp;
2c07245f 4468
ab9412ba 4469 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 4470
fd6b8f43 4471 if (IS_IVYBRIDGE(dev_priv))
4cbe4b2b 4472 ivybridge_update_fdi_bc_bifurcation(crtc);
1fbc0d78 4473
cd986abb
DV
4474 /* Write the TU size bits before fdi link training, so that error
4475 * detection works. */
4476 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4477 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4478
c98e9dcf 4479 /* For PCH output, training FDI link */
dc4a1094 4480 dev_priv->display.fdi_link_train(crtc, crtc_state);
2c07245f 4481
3ad8a208
DV
4482 /* We need to program the right clock selection before writing the pixel
4483 * mutliplier into the DPLL. */
6e266956 4484 if (HAS_PCH_CPT(dev_priv)) {
ee7b9f93 4485 u32 sel;
4b645f14 4486
c98e9dcf 4487 temp = I915_READ(PCH_DPLL_SEL);
11887397
DV
4488 temp |= TRANS_DPLL_ENABLE(pipe);
4489 sel = TRANS_DPLLB_SEL(pipe);
2ce42273 4490 if (crtc_state->shared_dpll ==
8106ddbd 4491 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
ee7b9f93
JB
4492 temp |= sel;
4493 else
4494 temp &= ~sel;
c98e9dcf 4495 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 4496 }
5eddb70b 4497
3ad8a208
DV
4498 /* XXX: pch pll's can be enabled any time before we enable the PCH
4499 * transcoder, and we actually should do this to not upset any PCH
4500 * transcoder that already use the clock when we share it.
4501 *
4502 * Note that enable_shared_dpll tries to do the right thing, but
4503 * get_shared_dpll unconditionally resets the pll - we need that to have
4504 * the right LVDS enable sequence. */
4cbe4b2b 4505 intel_enable_shared_dpll(crtc);
3ad8a208 4506
d9b6cb56
JB
4507 /* set transcoder timing, panel must allow it */
4508 assert_panel_unlocked(dev_priv, pipe);
4cbe4b2b 4509 ironlake_pch_transcoder_set_timings(crtc, pipe);
8db9d77b 4510
303b81e0 4511 intel_fdi_normal_train(crtc);
5e84e1a4 4512
c98e9dcf 4513 /* For PCH DP, enable TRANS_DP_CTL */
6e266956 4514 if (HAS_PCH_CPT(dev_priv) &&
2ce42273 4515 intel_crtc_has_dp_encoder(crtc_state)) {
9c4edaee 4516 const struct drm_display_mode *adjusted_mode =
2ce42273 4517 &crtc_state->base.adjusted_mode;
dfd07d72 4518 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
f0f59a00 4519 i915_reg_t reg = TRANS_DP_CTL(pipe);
5eddb70b
CW
4520 temp = I915_READ(reg);
4521 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
4522 TRANS_DP_SYNC_MASK |
4523 TRANS_DP_BPC_MASK);
e3ef4479 4524 temp |= TRANS_DP_OUTPUT_ENABLE;
9325c9f0 4525 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf 4526
9c4edaee 4527 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 4528 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
9c4edaee 4529 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 4530 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf
JB
4531
4532 switch (intel_trans_dp_port_sel(crtc)) {
c48b5305 4533 case PORT_B:
5eddb70b 4534 temp |= TRANS_DP_PORT_SEL_B;
c98e9dcf 4535 break;
c48b5305 4536 case PORT_C:
5eddb70b 4537 temp |= TRANS_DP_PORT_SEL_C;
c98e9dcf 4538 break;
c48b5305 4539 case PORT_D:
5eddb70b 4540 temp |= TRANS_DP_PORT_SEL_D;
c98e9dcf
JB
4541 break;
4542 default:
e95d41e1 4543 BUG();
32f9d658 4544 }
2c07245f 4545
5eddb70b 4546 I915_WRITE(reg, temp);
6be4a607 4547 }
b52eb4dc 4548
b8a4f404 4549 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
4550}
4551
2ce42273 4552static void lpt_pch_enable(const struct intel_crtc_state *crtc_state)
1507e5bd 4553{
2ce42273 4554 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
0dcdc382 4555 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2ce42273 4556 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
1507e5bd 4557
ab9412ba 4558 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
1507e5bd 4559
8c52b5e8 4560 lpt_program_iclkip(crtc);
1507e5bd 4561
0540e488 4562 /* Set transcoder timing. */
0dcdc382 4563 ironlake_pch_transcoder_set_timings(crtc, PIPE_A);
1507e5bd 4564
937bb610 4565 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
4566}
4567
a1520318 4568static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57 4569{
fac5e23e 4570 struct drm_i915_private *dev_priv = to_i915(dev);
f0f59a00 4571 i915_reg_t dslreg = PIPEDSL(pipe);
d4270e57
JB
4572 u32 temp;
4573
4574 temp = I915_READ(dslreg);
4575 udelay(500);
4576 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 4577 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 4578 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
4579 }
4580}
4581
86adf9d7
ML
4582static int
4583skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4584 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4585 int src_w, int src_h, int dst_w, int dst_h)
a1b2278e 4586{
86adf9d7
ML
4587 struct intel_crtc_scaler_state *scaler_state =
4588 &crtc_state->scaler_state;
4589 struct intel_crtc *intel_crtc =
4590 to_intel_crtc(crtc_state->base.crtc);
a1b2278e 4591 int need_scaling;
6156a456 4592
bd2ef25d 4593 need_scaling = drm_rotation_90_or_270(rotation) ?
6156a456
CK
4594 (src_h != dst_w || src_w != dst_h):
4595 (src_w != dst_w || src_h != dst_h);
a1b2278e
CK
4596
4597 /*
4598 * if plane is being disabled or scaler is no more required or force detach
4599 * - free scaler binded to this plane/crtc
4600 * - in order to do this, update crtc->scaler_usage
4601 *
4602 * Here scaler state in crtc_state is set free so that
4603 * scaler can be assigned to other user. Actual register
4604 * update to free the scaler is done in plane/panel-fit programming.
4605 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4606 */
86adf9d7 4607 if (force_detach || !need_scaling) {
a1b2278e 4608 if (*scaler_id >= 0) {
86adf9d7 4609 scaler_state->scaler_users &= ~(1 << scaler_user);
a1b2278e
CK
4610 scaler_state->scalers[*scaler_id].in_use = 0;
4611
86adf9d7
ML
4612 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4613 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4614 intel_crtc->pipe, scaler_user, *scaler_id,
a1b2278e
CK
4615 scaler_state->scaler_users);
4616 *scaler_id = -1;
4617 }
4618 return 0;
4619 }
4620
4621 /* range checks */
4622 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4623 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4624
4625 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4626 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
86adf9d7 4627 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
a1b2278e 4628 "size is out of scaler range\n",
86adf9d7 4629 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
a1b2278e
CK
4630 return -EINVAL;
4631 }
4632
86adf9d7
ML
4633 /* mark this plane as a scaler user in crtc_state */
4634 scaler_state->scaler_users |= (1 << scaler_user);
4635 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4636 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4637 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4638 scaler_state->scaler_users);
4639
4640 return 0;
4641}
4642
4643/**
4644 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4645 *
4646 * @state: crtc's scaler state
86adf9d7
ML
4647 *
4648 * Return
4649 * 0 - scaler_usage updated successfully
4650 * error - requested scaling cannot be supported or other error condition
4651 */
e435d6e5 4652int skl_update_scaler_crtc(struct intel_crtc_state *state)
86adf9d7 4653{
7c5f93b0 4654 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
86adf9d7 4655
e435d6e5 4656 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
31ad61e4 4657 &state->scaler_state.scaler_id, DRM_ROTATE_0,
86adf9d7 4658 state->pipe_src_w, state->pipe_src_h,
aad941d5 4659 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
86adf9d7
ML
4660}
4661
4662/**
4663 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4664 *
4665 * @state: crtc's scaler state
86adf9d7
ML
4666 * @plane_state: atomic plane state to update
4667 *
4668 * Return
4669 * 0 - scaler_usage updated successfully
4670 * error - requested scaling cannot be supported or other error condition
4671 */
da20eabd
ML
4672static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4673 struct intel_plane_state *plane_state)
86adf9d7
ML
4674{
4675
da20eabd
ML
4676 struct intel_plane *intel_plane =
4677 to_intel_plane(plane_state->base.plane);
86adf9d7
ML
4678 struct drm_framebuffer *fb = plane_state->base.fb;
4679 int ret;
4680
936e71e3 4681 bool force_detach = !fb || !plane_state->base.visible;
86adf9d7 4682
86adf9d7
ML
4683 ret = skl_update_scaler(crtc_state, force_detach,
4684 drm_plane_index(&intel_plane->base),
4685 &plane_state->scaler_id,
4686 plane_state->base.rotation,
936e71e3
VS
4687 drm_rect_width(&plane_state->base.src) >> 16,
4688 drm_rect_height(&plane_state->base.src) >> 16,
4689 drm_rect_width(&plane_state->base.dst),
4690 drm_rect_height(&plane_state->base.dst));
86adf9d7
ML
4691
4692 if (ret || plane_state->scaler_id < 0)
4693 return ret;
4694
a1b2278e 4695 /* check colorkey */
818ed961 4696 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
72660ce0
VS
4697 DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
4698 intel_plane->base.base.id,
4699 intel_plane->base.name);
a1b2278e
CK
4700 return -EINVAL;
4701 }
4702
4703 /* Check src format */
438b74a5 4704 switch (fb->format->format) {
86adf9d7
ML
4705 case DRM_FORMAT_RGB565:
4706 case DRM_FORMAT_XBGR8888:
4707 case DRM_FORMAT_XRGB8888:
4708 case DRM_FORMAT_ABGR8888:
4709 case DRM_FORMAT_ARGB8888:
4710 case DRM_FORMAT_XRGB2101010:
4711 case DRM_FORMAT_XBGR2101010:
4712 case DRM_FORMAT_YUYV:
4713 case DRM_FORMAT_YVYU:
4714 case DRM_FORMAT_UYVY:
4715 case DRM_FORMAT_VYUY:
4716 break;
4717 default:
72660ce0
VS
4718 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
4719 intel_plane->base.base.id, intel_plane->base.name,
438b74a5 4720 fb->base.id, fb->format->format);
86adf9d7 4721 return -EINVAL;
a1b2278e
CK
4722 }
4723
a1b2278e
CK
4724 return 0;
4725}
4726
e435d6e5
ML
4727static void skylake_scaler_disable(struct intel_crtc *crtc)
4728{
4729 int i;
4730
4731 for (i = 0; i < crtc->num_scalers; i++)
4732 skl_detach_scaler(crtc, i);
4733}
4734
4735static void skylake_pfit_enable(struct intel_crtc *crtc)
bd2e244f
JB
4736{
4737 struct drm_device *dev = crtc->base.dev;
fac5e23e 4738 struct drm_i915_private *dev_priv = to_i915(dev);
bd2e244f 4739 int pipe = crtc->pipe;
a1b2278e
CK
4740 struct intel_crtc_scaler_state *scaler_state =
4741 &crtc->config->scaler_state;
4742
4743 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4744
6e3c9717 4745 if (crtc->config->pch_pfit.enabled) {
a1b2278e
CK
4746 int id;
4747
4748 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4749 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4750 return;
4751 }
4752
4753 id = scaler_state->scaler_id;
4754 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4755 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4756 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4757 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4758
4759 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
bd2e244f
JB
4760 }
4761}
4762
b074cec8
JB
4763static void ironlake_pfit_enable(struct intel_crtc *crtc)
4764{
4765 struct drm_device *dev = crtc->base.dev;
fac5e23e 4766 struct drm_i915_private *dev_priv = to_i915(dev);
b074cec8
JB
4767 int pipe = crtc->pipe;
4768
6e3c9717 4769 if (crtc->config->pch_pfit.enabled) {
b074cec8
JB
4770 /* Force use of hard-coded filter coefficients
4771 * as some pre-programmed values are broken,
4772 * e.g. x201.
4773 */
fd6b8f43 4774 if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
b074cec8
JB
4775 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4776 PF_PIPE_SEL_IVB(pipe));
4777 else
4778 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
6e3c9717
ACO
4779 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4780 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
d4270e57
JB
4781 }
4782}
4783
20bc8673 4784void hsw_enable_ips(struct intel_crtc *crtc)
d77e4531 4785{
cea165c3 4786 struct drm_device *dev = crtc->base.dev;
fac5e23e 4787 struct drm_i915_private *dev_priv = to_i915(dev);
d77e4531 4788
6e3c9717 4789 if (!crtc->config->ips_enabled)
d77e4531
PZ
4790 return;
4791
307e4498
ML
4792 /*
4793 * We can only enable IPS after we enable a plane and wait for a vblank
4794 * This function is called from post_plane_update, which is run after
4795 * a vblank wait.
4796 */
cea165c3 4797
d77e4531 4798 assert_plane_enabled(dev_priv, crtc->plane);
8652744b 4799 if (IS_BROADWELL(dev_priv)) {
2a114cc1
BW
4800 mutex_lock(&dev_priv->rps.hw_lock);
4801 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4802 mutex_unlock(&dev_priv->rps.hw_lock);
4803 /* Quoting Art Runyan: "its not safe to expect any particular
4804 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
4805 * mailbox." Moreover, the mailbox may return a bogus state,
4806 * so we need to just enable it and continue on.
2a114cc1
BW
4807 */
4808 } else {
4809 I915_WRITE(IPS_CTL, IPS_ENABLE);
4810 /* The bit only becomes 1 in the next vblank, so this wait here
4811 * is essentially intel_wait_for_vblank. If we don't have this
4812 * and don't wait for vblanks until the end of crtc_enable, then
4813 * the HW state readout code will complain that the expected
4814 * IPS_CTL value is not the one we read. */
2ec9ba3c
CW
4815 if (intel_wait_for_register(dev_priv,
4816 IPS_CTL, IPS_ENABLE, IPS_ENABLE,
4817 50))
2a114cc1
BW
4818 DRM_ERROR("Timed out waiting for IPS enable\n");
4819 }
d77e4531
PZ
4820}
4821
20bc8673 4822void hsw_disable_ips(struct intel_crtc *crtc)
d77e4531
PZ
4823{
4824 struct drm_device *dev = crtc->base.dev;
fac5e23e 4825 struct drm_i915_private *dev_priv = to_i915(dev);
d77e4531 4826
6e3c9717 4827 if (!crtc->config->ips_enabled)
d77e4531
PZ
4828 return;
4829
4830 assert_plane_enabled(dev_priv, crtc->plane);
8652744b 4831 if (IS_BROADWELL(dev_priv)) {
2a114cc1
BW
4832 mutex_lock(&dev_priv->rps.hw_lock);
4833 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4834 mutex_unlock(&dev_priv->rps.hw_lock);
23d0b130 4835 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
b85c1ecf
CW
4836 if (intel_wait_for_register(dev_priv,
4837 IPS_CTL, IPS_ENABLE, 0,
4838 42))
23d0b130 4839 DRM_ERROR("Timed out waiting for IPS disable\n");
e59150dc 4840 } else {
2a114cc1 4841 I915_WRITE(IPS_CTL, 0);
e59150dc
JB
4842 POSTING_READ(IPS_CTL);
4843 }
d77e4531
PZ
4844
4845 /* We need to wait for a vblank before we can disable the plane. */
0f0f74bc 4846 intel_wait_for_vblank(dev_priv, crtc->pipe);
d77e4531
PZ
4847}
4848
7cac945f 4849static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
d3eedb1a 4850{
7cac945f 4851 if (intel_crtc->overlay) {
d3eedb1a 4852 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 4853 struct drm_i915_private *dev_priv = to_i915(dev);
d3eedb1a
VS
4854
4855 mutex_lock(&dev->struct_mutex);
4856 dev_priv->mm.interruptible = false;
4857 (void) intel_overlay_switch_off(intel_crtc->overlay);
4858 dev_priv->mm.interruptible = true;
4859 mutex_unlock(&dev->struct_mutex);
4860 }
4861
4862 /* Let userspace switch the overlay on again. In most cases userspace
4863 * has to recompute where to put it anyway.
4864 */
4865}
4866
87d4300a
ML
4867/**
4868 * intel_post_enable_primary - Perform operations after enabling primary plane
4869 * @crtc: the CRTC whose primary plane was just enabled
4870 *
4871 * Performs potentially sleeping operations that must be done after the primary
4872 * plane is enabled, such as updating FBC and IPS. Note that this may be
4873 * called due to an explicit primary plane update, or due to an implicit
4874 * re-enable that is caused when a sprite plane is updated to no longer
4875 * completely hide the primary plane.
4876 */
4877static void
4878intel_post_enable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4879{
4880 struct drm_device *dev = crtc->dev;
fac5e23e 4881 struct drm_i915_private *dev_priv = to_i915(dev);
a5c4d7bc
VS
4882 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4883 int pipe = intel_crtc->pipe;
a5c4d7bc 4884
87d4300a
ML
4885 /*
4886 * FIXME IPS should be fine as long as one plane is
4887 * enabled, but in practice it seems to have problems
4888 * when going from primary only to sprite only and vice
4889 * versa.
4890 */
a5c4d7bc
VS
4891 hsw_enable_ips(intel_crtc);
4892
f99d7069 4893 /*
87d4300a
ML
4894 * Gen2 reports pipe underruns whenever all planes are disabled.
4895 * So don't enable underrun reporting before at least some planes
4896 * are enabled.
4897 * FIXME: Need to fix the logic to work when we turn off all planes
4898 * but leave the pipe running.
f99d7069 4899 */
5db94019 4900 if (IS_GEN2(dev_priv))
87d4300a
ML
4901 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4902
aca7b684
VS
4903 /* Underruns don't always raise interrupts, so check manually. */
4904 intel_check_cpu_fifo_underruns(dev_priv);
4905 intel_check_pch_fifo_underruns(dev_priv);
a5c4d7bc
VS
4906}
4907
2622a081 4908/* FIXME move all this to pre_plane_update() with proper state tracking */
87d4300a
ML
4909static void
4910intel_pre_disable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4911{
4912 struct drm_device *dev = crtc->dev;
fac5e23e 4913 struct drm_i915_private *dev_priv = to_i915(dev);
a5c4d7bc
VS
4914 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4915 int pipe = intel_crtc->pipe;
a5c4d7bc 4916
87d4300a
ML
4917 /*
4918 * Gen2 reports pipe underruns whenever all planes are disabled.
4919 * So diasble underrun reporting before all the planes get disabled.
4920 * FIXME: Need to fix the logic to work when we turn off all planes
4921 * but leave the pipe running.
4922 */
5db94019 4923 if (IS_GEN2(dev_priv))
87d4300a 4924 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
a5c4d7bc 4925
2622a081
VS
4926 /*
4927 * FIXME IPS should be fine as long as one plane is
4928 * enabled, but in practice it seems to have problems
4929 * when going from primary only to sprite only and vice
4930 * versa.
4931 */
4932 hsw_disable_ips(intel_crtc);
4933}
4934
4935/* FIXME get rid of this and use pre_plane_update */
4936static void
4937intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
4938{
4939 struct drm_device *dev = crtc->dev;
fac5e23e 4940 struct drm_i915_private *dev_priv = to_i915(dev);
2622a081
VS
4941 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4942 int pipe = intel_crtc->pipe;
4943
4944 intel_pre_disable_primary(crtc);
4945
87d4300a
ML
4946 /*
4947 * Vblank time updates from the shadow to live plane control register
4948 * are blocked if the memory self-refresh mode is active at that
4949 * moment. So to make sure the plane gets truly disabled, disable
4950 * first the self-refresh mode. The self-refresh enable bit in turn
4951 * will be checked/applied by the HW only at the next frame start
4952 * event which is after the vblank start event, so we need to have a
4953 * wait-for-vblank between disabling the plane and the pipe.
4954 */
11a85d6a
VS
4955 if (HAS_GMCH_DISPLAY(dev_priv) &&
4956 intel_set_memory_cxsr(dev_priv, false))
0f0f74bc 4957 intel_wait_for_vblank(dev_priv, pipe);
87d4300a
ML
4958}
4959
5a21b665
DV
4960static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
4961{
4962 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
4963 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4964 struct intel_crtc_state *pipe_config =
4965 to_intel_crtc_state(crtc->base.state);
5a21b665
DV
4966 struct drm_plane *primary = crtc->base.primary;
4967 struct drm_plane_state *old_pri_state =
4968 drm_atomic_get_existing_plane_state(old_state, primary);
4969
5748b6a1 4970 intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
5a21b665
DV
4971
4972 crtc->wm.cxsr_allowed = true;
4973
4974 if (pipe_config->update_wm_post && pipe_config->base.active)
432081bc 4975 intel_update_watermarks(crtc);
5a21b665
DV
4976
4977 if (old_pri_state) {
4978 struct intel_plane_state *primary_state =
4979 to_intel_plane_state(primary->state);
4980 struct intel_plane_state *old_primary_state =
4981 to_intel_plane_state(old_pri_state);
4982
4983 intel_fbc_post_update(crtc);
4984
936e71e3 4985 if (primary_state->base.visible &&
5a21b665 4986 (needs_modeset(&pipe_config->base) ||
936e71e3 4987 !old_primary_state->base.visible))
5a21b665
DV
4988 intel_post_enable_primary(&crtc->base);
4989 }
4990}
4991
5c74cd73 4992static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
ac21b225 4993{
5c74cd73 4994 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
ac21b225 4995 struct drm_device *dev = crtc->base.dev;
fac5e23e 4996 struct drm_i915_private *dev_priv = to_i915(dev);
ab1d3a0e
ML
4997 struct intel_crtc_state *pipe_config =
4998 to_intel_crtc_state(crtc->base.state);
5c74cd73
ML
4999 struct drm_atomic_state *old_state = old_crtc_state->base.state;
5000 struct drm_plane *primary = crtc->base.primary;
5001 struct drm_plane_state *old_pri_state =
5002 drm_atomic_get_existing_plane_state(old_state, primary);
5003 bool modeset = needs_modeset(&pipe_config->base);
ccf010fb
ML
5004 struct intel_atomic_state *old_intel_state =
5005 to_intel_atomic_state(old_state);
ac21b225 5006
5c74cd73
ML
5007 if (old_pri_state) {
5008 struct intel_plane_state *primary_state =
5009 to_intel_plane_state(primary->state);
5010 struct intel_plane_state *old_primary_state =
5011 to_intel_plane_state(old_pri_state);
5012
faf68d92 5013 intel_fbc_pre_update(crtc, pipe_config, primary_state);
31ae71fc 5014
936e71e3
VS
5015 if (old_primary_state->base.visible &&
5016 (modeset || !primary_state->base.visible))
5c74cd73
ML
5017 intel_pre_disable_primary(&crtc->base);
5018 }
852eb00d 5019
49cff963 5020 if (pipe_config->disable_cxsr && HAS_GMCH_DISPLAY(dev_priv)) {
852eb00d 5021 crtc->wm.cxsr_allowed = false;
2dfd178d 5022
2622a081
VS
5023 /*
5024 * Vblank time updates from the shadow to live plane control register
5025 * are blocked if the memory self-refresh mode is active at that
5026 * moment. So to make sure the plane gets truly disabled, disable
5027 * first the self-refresh mode. The self-refresh enable bit in turn
5028 * will be checked/applied by the HW only at the next frame start
5029 * event which is after the vblank start event, so we need to have a
5030 * wait-for-vblank between disabling the plane and the pipe.
5031 */
11a85d6a
VS
5032 if (old_crtc_state->base.active &&
5033 intel_set_memory_cxsr(dev_priv, false))
0f0f74bc 5034 intel_wait_for_vblank(dev_priv, crtc->pipe);
852eb00d 5035 }
92826fcd 5036
ed4a6a7c
MR
5037 /*
5038 * IVB workaround: must disable low power watermarks for at least
5039 * one frame before enabling scaling. LP watermarks can be re-enabled
5040 * when scaling is disabled.
5041 *
5042 * WaCxSRDisabledForSpriteScaling:ivb
5043 */
ddd2b792 5044 if (pipe_config->disable_lp_wm && ilk_disable_lp_wm(dev))
0f0f74bc 5045 intel_wait_for_vblank(dev_priv, crtc->pipe);
ed4a6a7c
MR
5046
5047 /*
5048 * If we're doing a modeset, we're done. No need to do any pre-vblank
5049 * watermark programming here.
5050 */
5051 if (needs_modeset(&pipe_config->base))
5052 return;
5053
5054 /*
5055 * For platforms that support atomic watermarks, program the
5056 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
5057 * will be the intermediate values that are safe for both pre- and
5058 * post- vblank; when vblank happens, the 'active' values will be set
5059 * to the final 'target' values and we'll do this again to get the
5060 * optimal watermarks. For gen9+ platforms, the values we program here
5061 * will be the final target values which will get automatically latched
5062 * at vblank time; no further programming will be necessary.
5063 *
5064 * If a platform hasn't been transitioned to atomic watermarks yet,
5065 * we'll continue to update watermarks the old way, if flags tell
5066 * us to.
5067 */
5068 if (dev_priv->display.initial_watermarks != NULL)
ccf010fb
ML
5069 dev_priv->display.initial_watermarks(old_intel_state,
5070 pipe_config);
caed361d 5071 else if (pipe_config->update_wm_pre)
432081bc 5072 intel_update_watermarks(crtc);
ac21b225
ML
5073}
5074
d032ffa0 5075static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
87d4300a
ML
5076{
5077 struct drm_device *dev = crtc->dev;
5078 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
d032ffa0 5079 struct drm_plane *p;
87d4300a
ML
5080 int pipe = intel_crtc->pipe;
5081
7cac945f 5082 intel_crtc_dpms_overlay_disable(intel_crtc);
27321ae8 5083
d032ffa0
ML
5084 drm_for_each_plane_mask(p, dev, plane_mask)
5085 to_intel_plane(p)->disable_plane(p, crtc);
f98551ae 5086
f99d7069
DV
5087 /*
5088 * FIXME: Once we grow proper nuclear flip support out of this we need
5089 * to compute the mask of flip planes precisely. For the time being
5090 * consider this a flip to a NULL plane.
5091 */
5748b6a1 5092 intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
5093}
5094
fb1c98b1 5095static void intel_encoders_pre_pll_enable(struct drm_crtc *crtc,
fd6bbda9 5096 struct intel_crtc_state *crtc_state,
fb1c98b1
ML
5097 struct drm_atomic_state *old_state)
5098{
5099 struct drm_connector_state *old_conn_state;
5100 struct drm_connector *conn;
5101 int i;
5102
5103 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5104 struct drm_connector_state *conn_state = conn->state;
5105 struct intel_encoder *encoder =
5106 to_intel_encoder(conn_state->best_encoder);
5107
5108 if (conn_state->crtc != crtc)
5109 continue;
5110
5111 if (encoder->pre_pll_enable)
fd6bbda9 5112 encoder->pre_pll_enable(encoder, crtc_state, conn_state);
fb1c98b1
ML
5113 }
5114}
5115
5116static void intel_encoders_pre_enable(struct drm_crtc *crtc,
fd6bbda9 5117 struct intel_crtc_state *crtc_state,
fb1c98b1
ML
5118 struct drm_atomic_state *old_state)
5119{
5120 struct drm_connector_state *old_conn_state;
5121 struct drm_connector *conn;
5122 int i;
5123
5124 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5125 struct drm_connector_state *conn_state = conn->state;
5126 struct intel_encoder *encoder =
5127 to_intel_encoder(conn_state->best_encoder);
5128
5129 if (conn_state->crtc != crtc)
5130 continue;
5131
5132 if (encoder->pre_enable)
fd6bbda9 5133 encoder->pre_enable(encoder, crtc_state, conn_state);
fb1c98b1
ML
5134 }
5135}
5136
5137static void intel_encoders_enable(struct drm_crtc *crtc,
fd6bbda9 5138 struct intel_crtc_state *crtc_state,
fb1c98b1
ML
5139 struct drm_atomic_state *old_state)
5140{
5141 struct drm_connector_state *old_conn_state;
5142 struct drm_connector *conn;
5143 int i;
5144
5145 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5146 struct drm_connector_state *conn_state = conn->state;
5147 struct intel_encoder *encoder =
5148 to_intel_encoder(conn_state->best_encoder);
5149
5150 if (conn_state->crtc != crtc)
5151 continue;
5152
fd6bbda9 5153 encoder->enable(encoder, crtc_state, conn_state);
fb1c98b1
ML
5154 intel_opregion_notify_encoder(encoder, true);
5155 }
5156}
5157
5158static void intel_encoders_disable(struct drm_crtc *crtc,
fd6bbda9 5159 struct intel_crtc_state *old_crtc_state,
fb1c98b1
ML
5160 struct drm_atomic_state *old_state)
5161{
5162 struct drm_connector_state *old_conn_state;
5163 struct drm_connector *conn;
5164 int i;
5165
5166 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5167 struct intel_encoder *encoder =
5168 to_intel_encoder(old_conn_state->best_encoder);
5169
5170 if (old_conn_state->crtc != crtc)
5171 continue;
5172
5173 intel_opregion_notify_encoder(encoder, false);
fd6bbda9 5174 encoder->disable(encoder, old_crtc_state, old_conn_state);
fb1c98b1
ML
5175 }
5176}
5177
5178static void intel_encoders_post_disable(struct drm_crtc *crtc,
fd6bbda9 5179 struct intel_crtc_state *old_crtc_state,
fb1c98b1
ML
5180 struct drm_atomic_state *old_state)
5181{
5182 struct drm_connector_state *old_conn_state;
5183 struct drm_connector *conn;
5184 int i;
5185
5186 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5187 struct intel_encoder *encoder =
5188 to_intel_encoder(old_conn_state->best_encoder);
5189
5190 if (old_conn_state->crtc != crtc)
5191 continue;
5192
5193 if (encoder->post_disable)
fd6bbda9 5194 encoder->post_disable(encoder, old_crtc_state, old_conn_state);
fb1c98b1
ML
5195 }
5196}
5197
5198static void intel_encoders_post_pll_disable(struct drm_crtc *crtc,
fd6bbda9 5199 struct intel_crtc_state *old_crtc_state,
fb1c98b1
ML
5200 struct drm_atomic_state *old_state)
5201{
5202 struct drm_connector_state *old_conn_state;
5203 struct drm_connector *conn;
5204 int i;
5205
5206 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5207 struct intel_encoder *encoder =
5208 to_intel_encoder(old_conn_state->best_encoder);
5209
5210 if (old_conn_state->crtc != crtc)
5211 continue;
5212
5213 if (encoder->post_pll_disable)
fd6bbda9 5214 encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
fb1c98b1
ML
5215 }
5216}
5217
4a806558
ML
5218static void ironlake_crtc_enable(struct intel_crtc_state *pipe_config,
5219 struct drm_atomic_state *old_state)
f67a559d 5220{
4a806558 5221 struct drm_crtc *crtc = pipe_config->base.crtc;
f67a559d 5222 struct drm_device *dev = crtc->dev;
fac5e23e 5223 struct drm_i915_private *dev_priv = to_i915(dev);
f67a559d
JB
5224 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5225 int pipe = intel_crtc->pipe;
ccf010fb
ML
5226 struct intel_atomic_state *old_intel_state =
5227 to_intel_atomic_state(old_state);
f67a559d 5228
53d9f4e9 5229 if (WARN_ON(intel_crtc->active))
f67a559d
JB
5230 return;
5231
b2c0593a
VS
5232 /*
5233 * Sometimes spurious CPU pipe underruns happen during FDI
5234 * training, at least with VGA+HDMI cloning. Suppress them.
5235 *
5236 * On ILK we get an occasional spurious CPU pipe underruns
5237 * between eDP port A enable and vdd enable. Also PCH port
5238 * enable seems to result in the occasional CPU pipe underrun.
5239 *
5240 * Spurious PCH underruns also occur during PCH enabling.
5241 */
5242 if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
5243 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
81b088ca
VS
5244 if (intel_crtc->config->has_pch_encoder)
5245 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
5246
6e3c9717 5247 if (intel_crtc->config->has_pch_encoder)
b14b1055
DV
5248 intel_prepare_shared_dpll(intel_crtc);
5249
37a5650b 5250 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 5251 intel_dp_set_m_n(intel_crtc, M1_N1);
29407aab
DV
5252
5253 intel_set_pipe_timings(intel_crtc);
bc58be60 5254 intel_set_pipe_src_size(intel_crtc);
29407aab 5255
6e3c9717 5256 if (intel_crtc->config->has_pch_encoder) {
29407aab 5257 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 5258 &intel_crtc->config->fdi_m_n, NULL);
29407aab
DV
5259 }
5260
5261 ironlake_set_pipeconf(crtc);
5262
f67a559d 5263 intel_crtc->active = true;
8664281b 5264
fd6bbda9 5265 intel_encoders_pre_enable(crtc, pipe_config, old_state);
f67a559d 5266
6e3c9717 5267 if (intel_crtc->config->has_pch_encoder) {
fff367c7
DV
5268 /* Note: FDI PLL enabling _must_ be done before we enable the
5269 * cpu pipes, hence this is separate from all the other fdi/pch
5270 * enabling. */
88cefb6c 5271 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
DV
5272 } else {
5273 assert_fdi_tx_disabled(dev_priv, pipe);
5274 assert_fdi_rx_disabled(dev_priv, pipe);
5275 }
f67a559d 5276
b074cec8 5277 ironlake_pfit_enable(intel_crtc);
f67a559d 5278
9c54c0dd
JB
5279 /*
5280 * On ILK+ LUT must be loaded before the pipe is running but with
5281 * clocks enabled
5282 */
b95c5321 5283 intel_color_load_luts(&pipe_config->base);
9c54c0dd 5284
1d5bf5d9 5285 if (dev_priv->display.initial_watermarks != NULL)
ccf010fb 5286 dev_priv->display.initial_watermarks(old_intel_state, intel_crtc->config);
e1fdc473 5287 intel_enable_pipe(intel_crtc);
f67a559d 5288
6e3c9717 5289 if (intel_crtc->config->has_pch_encoder)
2ce42273 5290 ironlake_pch_enable(pipe_config);
c98e9dcf 5291
f9b61ff6
DV
5292 assert_vblank_disabled(crtc);
5293 drm_crtc_vblank_on(crtc);
5294
fd6bbda9 5295 intel_encoders_enable(crtc, pipe_config, old_state);
61b77ddd 5296
6e266956 5297 if (HAS_PCH_CPT(dev_priv))
a1520318 5298 cpt_verify_modeset(dev, intel_crtc->pipe);
37ca8d4c
VS
5299
5300 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
5301 if (intel_crtc->config->has_pch_encoder)
0f0f74bc 5302 intel_wait_for_vblank(dev_priv, pipe);
b2c0593a 5303 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
37ca8d4c 5304 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607
JB
5305}
5306
42db64ef
PZ
5307/* IPS only exists on ULT machines and is tied to pipe A. */
5308static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
5309{
50a0bc90 5310 return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
42db64ef
PZ
5311}
5312
4a806558
ML
5313static void haswell_crtc_enable(struct intel_crtc_state *pipe_config,
5314 struct drm_atomic_state *old_state)
4f771f10 5315{
4a806558 5316 struct drm_crtc *crtc = pipe_config->base.crtc;
6315b5d3 5317 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
4f771f10 5318 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
99d736a2 5319 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
4d1de975 5320 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ccf010fb
ML
5321 struct intel_atomic_state *old_intel_state =
5322 to_intel_atomic_state(old_state);
4f771f10 5323
53d9f4e9 5324 if (WARN_ON(intel_crtc->active))
4f771f10
PZ
5325 return;
5326
81b088ca
VS
5327 if (intel_crtc->config->has_pch_encoder)
5328 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5329 false);
5330
fd6bbda9 5331 intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
95a7a2ae 5332
8106ddbd 5333 if (intel_crtc->config->shared_dpll)
df8ad70c
DV
5334 intel_enable_shared_dpll(intel_crtc);
5335
37a5650b 5336 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 5337 intel_dp_set_m_n(intel_crtc, M1_N1);
229fca97 5338
d7edc4e5 5339 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975
JN
5340 intel_set_pipe_timings(intel_crtc);
5341
bc58be60 5342 intel_set_pipe_src_size(intel_crtc);
229fca97 5343
4d1de975
JN
5344 if (cpu_transcoder != TRANSCODER_EDP &&
5345 !transcoder_is_dsi(cpu_transcoder)) {
5346 I915_WRITE(PIPE_MULT(cpu_transcoder),
6e3c9717 5347 intel_crtc->config->pixel_multiplier - 1);
ebb69c95
CT
5348 }
5349
6e3c9717 5350 if (intel_crtc->config->has_pch_encoder) {
229fca97 5351 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 5352 &intel_crtc->config->fdi_m_n, NULL);
229fca97
DV
5353 }
5354
d7edc4e5 5355 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975
JN
5356 haswell_set_pipeconf(crtc);
5357
391bf048 5358 haswell_set_pipemisc(crtc);
229fca97 5359
b95c5321 5360 intel_color_set_csc(&pipe_config->base);
229fca97 5361
4f771f10 5362 intel_crtc->active = true;
8664281b 5363
6b698516
DV
5364 if (intel_crtc->config->has_pch_encoder)
5365 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
5366 else
5367 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5368
fd6bbda9 5369 intel_encoders_pre_enable(crtc, pipe_config, old_state);
4f771f10 5370
d2d65408 5371 if (intel_crtc->config->has_pch_encoder)
dc4a1094 5372 dev_priv->display.fdi_link_train(intel_crtc, pipe_config);
4fe9467d 5373
d7edc4e5 5374 if (!transcoder_is_dsi(cpu_transcoder))
7d4aefd0 5375 intel_ddi_enable_pipe_clock(intel_crtc);
4f771f10 5376
6315b5d3 5377 if (INTEL_GEN(dev_priv) >= 9)
e435d6e5 5378 skylake_pfit_enable(intel_crtc);
ff6d9f55 5379 else
1c132b44 5380 ironlake_pfit_enable(intel_crtc);
4f771f10
PZ
5381
5382 /*
5383 * On ILK+ LUT must be loaded before the pipe is running but with
5384 * clocks enabled
5385 */
b95c5321 5386 intel_color_load_luts(&pipe_config->base);
4f771f10 5387
e9ce1a62 5388 intel_ddi_set_pipe_settings(intel_crtc);
d7edc4e5 5389 if (!transcoder_is_dsi(cpu_transcoder))
e9ce1a62 5390 intel_ddi_enable_transcoder_func(intel_crtc);
4f771f10 5391
1d5bf5d9 5392 if (dev_priv->display.initial_watermarks != NULL)
3125d39f 5393 dev_priv->display.initial_watermarks(old_intel_state, pipe_config);
4d1de975
JN
5394
5395 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
d7edc4e5 5396 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975 5397 intel_enable_pipe(intel_crtc);
42db64ef 5398
6e3c9717 5399 if (intel_crtc->config->has_pch_encoder)
2ce42273 5400 lpt_pch_enable(pipe_config);
4f771f10 5401
0037071d 5402 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
e9ce1a62 5403 intel_ddi_set_vc_payload_alloc(intel_crtc, true);
0e32b39c 5404
f9b61ff6
DV
5405 assert_vblank_disabled(crtc);
5406 drm_crtc_vblank_on(crtc);
5407
fd6bbda9 5408 intel_encoders_enable(crtc, pipe_config, old_state);
4f771f10 5409
6b698516 5410 if (intel_crtc->config->has_pch_encoder) {
0f0f74bc
VS
5411 intel_wait_for_vblank(dev_priv, pipe);
5412 intel_wait_for_vblank(dev_priv, pipe);
6b698516 5413 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
d2d65408
VS
5414 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5415 true);
6b698516 5416 }
d2d65408 5417
e4916946
PZ
5418 /* If we change the relative order between pipe/planes enabling, we need
5419 * to change the workaround. */
99d736a2 5420 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
772c2a51 5421 if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
0f0f74bc
VS
5422 intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
5423 intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
99d736a2 5424 }
4f771f10
PZ
5425}
5426
bfd16b2a 5427static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
3f8dce3a
DV
5428{
5429 struct drm_device *dev = crtc->base.dev;
fac5e23e 5430 struct drm_i915_private *dev_priv = to_i915(dev);
3f8dce3a
DV
5431 int pipe = crtc->pipe;
5432
5433 /* To avoid upsetting the power well on haswell only disable the pfit if
5434 * it's in use. The hw state code will make sure we get this right. */
bfd16b2a 5435 if (force || crtc->config->pch_pfit.enabled) {
3f8dce3a
DV
5436 I915_WRITE(PF_CTL(pipe), 0);
5437 I915_WRITE(PF_WIN_POS(pipe), 0);
5438 I915_WRITE(PF_WIN_SZ(pipe), 0);
5439 }
5440}
5441
4a806558
ML
5442static void ironlake_crtc_disable(struct intel_crtc_state *old_crtc_state,
5443 struct drm_atomic_state *old_state)
6be4a607 5444{
4a806558 5445 struct drm_crtc *crtc = old_crtc_state->base.crtc;
6be4a607 5446 struct drm_device *dev = crtc->dev;
fac5e23e 5447 struct drm_i915_private *dev_priv = to_i915(dev);
6be4a607
JB
5448 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5449 int pipe = intel_crtc->pipe;
b52eb4dc 5450
b2c0593a
VS
5451 /*
5452 * Sometimes spurious CPU pipe underruns happen when the
5453 * pipe is already disabled, but FDI RX/TX is still enabled.
5454 * Happens at least with VGA+HDMI cloning. Suppress them.
5455 */
5456 if (intel_crtc->config->has_pch_encoder) {
5457 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
37ca8d4c 5458 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
b2c0593a 5459 }
37ca8d4c 5460
fd6bbda9 5461 intel_encoders_disable(crtc, old_crtc_state, old_state);
ea9d758d 5462
f9b61ff6
DV
5463 drm_crtc_vblank_off(crtc);
5464 assert_vblank_disabled(crtc);
5465
575f7ab7 5466 intel_disable_pipe(intel_crtc);
32f9d658 5467
bfd16b2a 5468 ironlake_pfit_disable(intel_crtc, false);
2c07245f 5469
b2c0593a 5470 if (intel_crtc->config->has_pch_encoder)
5a74f70a
VS
5471 ironlake_fdi_disable(crtc);
5472
fd6bbda9 5473 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
2c07245f 5474
6e3c9717 5475 if (intel_crtc->config->has_pch_encoder) {
d925c59a 5476 ironlake_disable_pch_transcoder(dev_priv, pipe);
6be4a607 5477
6e266956 5478 if (HAS_PCH_CPT(dev_priv)) {
f0f59a00
VS
5479 i915_reg_t reg;
5480 u32 temp;
5481
d925c59a
DV
5482 /* disable TRANS_DP_CTL */
5483 reg = TRANS_DP_CTL(pipe);
5484 temp = I915_READ(reg);
5485 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5486 TRANS_DP_PORT_SEL_MASK);
5487 temp |= TRANS_DP_PORT_SEL_NONE;
5488 I915_WRITE(reg, temp);
5489
5490 /* disable DPLL_SEL */
5491 temp = I915_READ(PCH_DPLL_SEL);
11887397 5492 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 5493 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 5494 }
e3421a18 5495
d925c59a
DV
5496 ironlake_fdi_pll_disable(intel_crtc);
5497 }
81b088ca 5498
b2c0593a 5499 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
81b088ca 5500 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607 5501}
1b3c7a47 5502
4a806558
ML
5503static void haswell_crtc_disable(struct intel_crtc_state *old_crtc_state,
5504 struct drm_atomic_state *old_state)
ee7b9f93 5505{
4a806558 5506 struct drm_crtc *crtc = old_crtc_state->base.crtc;
6315b5d3 5507 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
ee7b9f93 5508 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 5509 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee7b9f93 5510
d2d65408
VS
5511 if (intel_crtc->config->has_pch_encoder)
5512 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5513 false);
5514
fd6bbda9 5515 intel_encoders_disable(crtc, old_crtc_state, old_state);
4f771f10 5516
f9b61ff6
DV
5517 drm_crtc_vblank_off(crtc);
5518 assert_vblank_disabled(crtc);
5519
4d1de975 5520 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
d7edc4e5 5521 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975 5522 intel_disable_pipe(intel_crtc);
4f771f10 5523
0037071d 5524 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
e9ce1a62 5525 intel_ddi_set_vc_payload_alloc(intel_crtc, false);
a4bf214f 5526
d7edc4e5 5527 if (!transcoder_is_dsi(cpu_transcoder))
7d4aefd0 5528 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4f771f10 5529
6315b5d3 5530 if (INTEL_GEN(dev_priv) >= 9)
e435d6e5 5531 skylake_scaler_disable(intel_crtc);
ff6d9f55 5532 else
bfd16b2a 5533 ironlake_pfit_disable(intel_crtc, false);
4f771f10 5534
d7edc4e5 5535 if (!transcoder_is_dsi(cpu_transcoder))
7d4aefd0 5536 intel_ddi_disable_pipe_clock(intel_crtc);
4f771f10 5537
fd6bbda9 5538 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
81b088ca 5539
b7076546 5540 if (old_crtc_state->has_pch_encoder)
81b088ca
VS
5541 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5542 true);
4f771f10
PZ
5543}
5544
2dd24552
JB
5545static void i9xx_pfit_enable(struct intel_crtc *crtc)
5546{
5547 struct drm_device *dev = crtc->base.dev;
fac5e23e 5548 struct drm_i915_private *dev_priv = to_i915(dev);
6e3c9717 5549 struct intel_crtc_state *pipe_config = crtc->config;
2dd24552 5550
681a8504 5551 if (!pipe_config->gmch_pfit.control)
2dd24552
JB
5552 return;
5553
2dd24552 5554 /*
c0b03411
DV
5555 * The panel fitter should only be adjusted whilst the pipe is disabled,
5556 * according to register description and PRM.
2dd24552 5557 */
c0b03411
DV
5558 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5559 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 5560
b074cec8
JB
5561 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5562 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
DV
5563
5564 /* Border color in case we don't scale up to the full screen. Black by
5565 * default, change to something else for debugging. */
5566 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
5567}
5568
79f255a0 5569enum intel_display_power_domain intel_port_to_power_domain(enum port port)
d05410f9
DA
5570{
5571 switch (port) {
5572 case PORT_A:
6331a704 5573 return POWER_DOMAIN_PORT_DDI_A_LANES;
d05410f9 5574 case PORT_B:
6331a704 5575 return POWER_DOMAIN_PORT_DDI_B_LANES;
d05410f9 5576 case PORT_C:
6331a704 5577 return POWER_DOMAIN_PORT_DDI_C_LANES;
d05410f9 5578 case PORT_D:
6331a704 5579 return POWER_DOMAIN_PORT_DDI_D_LANES;
d8e19f99 5580 case PORT_E:
6331a704 5581 return POWER_DOMAIN_PORT_DDI_E_LANES;
d05410f9 5582 default:
b9fec167 5583 MISSING_CASE(port);
d05410f9
DA
5584 return POWER_DOMAIN_PORT_OTHER;
5585 }
5586}
5587
d8fc70b7
ACO
5588static u64 get_crtc_power_domains(struct drm_crtc *crtc,
5589 struct intel_crtc_state *crtc_state)
77d22dca 5590{
319be8ae 5591 struct drm_device *dev = crtc->dev;
37255d8d 5592 struct drm_i915_private *dev_priv = to_i915(dev);
74bff5f9 5593 struct drm_encoder *encoder;
319be8ae
ID
5594 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5595 enum pipe pipe = intel_crtc->pipe;
d8fc70b7 5596 u64 mask;
74bff5f9 5597 enum transcoder transcoder = crtc_state->cpu_transcoder;
77d22dca 5598
74bff5f9 5599 if (!crtc_state->base.active)
292b990e
ML
5600 return 0;
5601
77d22dca
ID
5602 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5603 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
74bff5f9
ML
5604 if (crtc_state->pch_pfit.enabled ||
5605 crtc_state->pch_pfit.force_thru)
d8fc70b7 5606 mask |= BIT_ULL(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
77d22dca 5607
74bff5f9
ML
5608 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5609 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5610
79f255a0 5611 mask |= BIT_ULL(intel_encoder->power_domain);
74bff5f9 5612 }
319be8ae 5613
37255d8d
ML
5614 if (HAS_DDI(dev_priv) && crtc_state->has_audio)
5615 mask |= BIT(POWER_DOMAIN_AUDIO);
5616
15e7ec29 5617 if (crtc_state->shared_dpll)
d8fc70b7 5618 mask |= BIT_ULL(POWER_DOMAIN_PLLS);
15e7ec29 5619
77d22dca
ID
5620 return mask;
5621}
5622
d2d15016 5623static u64
74bff5f9
ML
5624modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5625 struct intel_crtc_state *crtc_state)
77d22dca 5626{
fac5e23e 5627 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
292b990e
ML
5628 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5629 enum intel_display_power_domain domain;
d8fc70b7 5630 u64 domains, new_domains, old_domains;
77d22dca 5631
292b990e 5632 old_domains = intel_crtc->enabled_power_domains;
74bff5f9
ML
5633 intel_crtc->enabled_power_domains = new_domains =
5634 get_crtc_power_domains(crtc, crtc_state);
77d22dca 5635
5a21b665 5636 domains = new_domains & ~old_domains;
292b990e
ML
5637
5638 for_each_power_domain(domain, domains)
5639 intel_display_power_get(dev_priv, domain);
5640
5a21b665 5641 return old_domains & ~new_domains;
292b990e
ML
5642}
5643
5644static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
d8fc70b7 5645 u64 domains)
292b990e
ML
5646{
5647 enum intel_display_power_domain domain;
5648
5649 for_each_power_domain(domain, domains)
5650 intel_display_power_put(dev_priv, domain);
5651}
77d22dca 5652
7ff89ca2
VS
5653static void valleyview_crtc_enable(struct intel_crtc_state *pipe_config,
5654 struct drm_atomic_state *old_state)
adafdc6f 5655{
7ff89ca2
VS
5656 struct drm_crtc *crtc = pipe_config->base.crtc;
5657 struct drm_device *dev = crtc->dev;
5658 struct drm_i915_private *dev_priv = to_i915(dev);
5659 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5660 int pipe = intel_crtc->pipe;
adafdc6f 5661
7ff89ca2
VS
5662 if (WARN_ON(intel_crtc->active))
5663 return;
adafdc6f 5664
7ff89ca2
VS
5665 if (intel_crtc_has_dp_encoder(intel_crtc->config))
5666 intel_dp_set_m_n(intel_crtc, M1_N1);
b2045352 5667
7ff89ca2
VS
5668 intel_set_pipe_timings(intel_crtc);
5669 intel_set_pipe_src_size(intel_crtc);
b2045352 5670
7ff89ca2
VS
5671 if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
5672 struct drm_i915_private *dev_priv = to_i915(dev);
560a7ae4 5673
7ff89ca2
VS
5674 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
5675 I915_WRITE(CHV_CANVAS(pipe), 0);
560a7ae4
DL
5676 }
5677
7ff89ca2 5678 i9xx_set_pipeconf(intel_crtc);
560a7ae4 5679
7ff89ca2 5680 intel_crtc->active = true;
92891e45 5681
7ff89ca2 5682 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5f199dfa 5683
7ff89ca2 5684 intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
5f199dfa 5685
7ff89ca2
VS
5686 if (IS_CHERRYVIEW(dev_priv)) {
5687 chv_prepare_pll(intel_crtc, intel_crtc->config);
5688 chv_enable_pll(intel_crtc, intel_crtc->config);
5689 } else {
5690 vlv_prepare_pll(intel_crtc, intel_crtc->config);
5691 vlv_enable_pll(intel_crtc, intel_crtc->config);
5f199dfa
VS
5692 }
5693
7ff89ca2 5694 intel_encoders_pre_enable(crtc, pipe_config, old_state);
5f199dfa 5695
7ff89ca2 5696 i9xx_pfit_enable(intel_crtc);
89b3c3c7 5697
7ff89ca2 5698 intel_color_load_luts(&pipe_config->base);
89b3c3c7 5699
7ff89ca2
VS
5700 intel_update_watermarks(intel_crtc);
5701 intel_enable_pipe(intel_crtc);
5702
5703 assert_vblank_disabled(crtc);
5704 drm_crtc_vblank_on(crtc);
89b3c3c7 5705
7ff89ca2 5706 intel_encoders_enable(crtc, pipe_config, old_state);
89b3c3c7
ACO
5707}
5708
7ff89ca2 5709static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
2b73001e 5710{
7ff89ca2
VS
5711 struct drm_device *dev = crtc->base.dev;
5712 struct drm_i915_private *dev_priv = to_i915(dev);
83d7c81f 5713
7ff89ca2
VS
5714 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
5715 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
2b73001e
VS
5716}
5717
7ff89ca2
VS
5718static void i9xx_crtc_enable(struct intel_crtc_state *pipe_config,
5719 struct drm_atomic_state *old_state)
2b73001e 5720{
7ff89ca2
VS
5721 struct drm_crtc *crtc = pipe_config->base.crtc;
5722 struct drm_device *dev = crtc->dev;
5723 struct drm_i915_private *dev_priv = to_i915(dev);
5724 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5725 enum pipe pipe = intel_crtc->pipe;
2b73001e 5726
7ff89ca2
VS
5727 if (WARN_ON(intel_crtc->active))
5728 return;
2b73001e 5729
7ff89ca2 5730 i9xx_set_pll_dividers(intel_crtc);
2b73001e 5731
7ff89ca2
VS
5732 if (intel_crtc_has_dp_encoder(intel_crtc->config))
5733 intel_dp_set_m_n(intel_crtc, M1_N1);
83d7c81f 5734
7ff89ca2
VS
5735 intel_set_pipe_timings(intel_crtc);
5736 intel_set_pipe_src_size(intel_crtc);
2b73001e 5737
7ff89ca2 5738 i9xx_set_pipeconf(intel_crtc);
f8437dd1 5739
7ff89ca2 5740 intel_crtc->active = true;
5f199dfa 5741
7ff89ca2
VS
5742 if (!IS_GEN2(dev_priv))
5743 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5f199dfa 5744
7ff89ca2 5745 intel_encoders_pre_enable(crtc, pipe_config, old_state);
f8437dd1 5746
7ff89ca2 5747 i9xx_enable_pll(intel_crtc);
f8437dd1 5748
7ff89ca2 5749 i9xx_pfit_enable(intel_crtc);
f8437dd1 5750
7ff89ca2 5751 intel_color_load_luts(&pipe_config->base);
f8437dd1 5752
7ff89ca2
VS
5753 intel_update_watermarks(intel_crtc);
5754 intel_enable_pipe(intel_crtc);
f8437dd1 5755
7ff89ca2
VS
5756 assert_vblank_disabled(crtc);
5757 drm_crtc_vblank_on(crtc);
f8437dd1 5758
7ff89ca2
VS
5759 intel_encoders_enable(crtc, pipe_config, old_state);
5760}
f8437dd1 5761
7ff89ca2
VS
5762static void i9xx_pfit_disable(struct intel_crtc *crtc)
5763{
5764 struct drm_device *dev = crtc->base.dev;
5765 struct drm_i915_private *dev_priv = to_i915(dev);
f8437dd1 5766
7ff89ca2 5767 if (!crtc->config->gmch_pfit.control)
f8437dd1 5768 return;
f8437dd1 5769
7ff89ca2
VS
5770 assert_pipe_disabled(dev_priv, crtc->pipe);
5771
5772 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
5773 I915_READ(PFIT_CONTROL));
5774 I915_WRITE(PFIT_CONTROL, 0);
f8437dd1
VK
5775}
5776
7ff89ca2
VS
5777static void i9xx_crtc_disable(struct intel_crtc_state *old_crtc_state,
5778 struct drm_atomic_state *old_state)
f8437dd1 5779{
7ff89ca2
VS
5780 struct drm_crtc *crtc = old_crtc_state->base.crtc;
5781 struct drm_device *dev = crtc->dev;
5782 struct drm_i915_private *dev_priv = to_i915(dev);
5783 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5784 int pipe = intel_crtc->pipe;
d66a2194 5785
d66a2194 5786 /*
7ff89ca2
VS
5787 * On gen2 planes are double buffered but the pipe isn't, so we must
5788 * wait for planes to fully turn off before disabling the pipe.
d66a2194 5789 */
7ff89ca2
VS
5790 if (IS_GEN2(dev_priv))
5791 intel_wait_for_vblank(dev_priv, pipe);
d66a2194 5792
7ff89ca2 5793 intel_encoders_disable(crtc, old_crtc_state, old_state);
d66a2194 5794
7ff89ca2
VS
5795 drm_crtc_vblank_off(crtc);
5796 assert_vblank_disabled(crtc);
d66a2194 5797
7ff89ca2 5798 intel_disable_pipe(intel_crtc);
d66a2194 5799
7ff89ca2 5800 i9xx_pfit_disable(intel_crtc);
89b3c3c7 5801
7ff89ca2 5802 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
d66a2194 5803
7ff89ca2
VS
5804 if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
5805 if (IS_CHERRYVIEW(dev_priv))
5806 chv_disable_pll(dev_priv, pipe);
5807 else if (IS_VALLEYVIEW(dev_priv))
5808 vlv_disable_pll(dev_priv, pipe);
5809 else
5810 i9xx_disable_pll(intel_crtc);
5811 }
c2e001ef 5812
7ff89ca2 5813 intel_encoders_post_pll_disable(crtc, old_crtc_state, old_state);
89b3c3c7 5814
7ff89ca2
VS
5815 if (!IS_GEN2(dev_priv))
5816 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
f8437dd1
VK
5817}
5818
7ff89ca2 5819static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
f8437dd1 5820{
7ff89ca2
VS
5821 struct intel_encoder *encoder;
5822 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5823 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
5824 enum intel_display_power_domain domain;
d2d15016 5825 u64 domains;
7ff89ca2
VS
5826 struct drm_atomic_state *state;
5827 struct intel_crtc_state *crtc_state;
5828 int ret;
f8437dd1 5829
7ff89ca2
VS
5830 if (!intel_crtc->active)
5831 return;
a8ca4934 5832
7ff89ca2
VS
5833 if (crtc->primary->state->visible) {
5834 WARN_ON(intel_crtc->flip_work);
5d96d8af 5835
7ff89ca2 5836 intel_pre_disable_primary_noatomic(crtc);
709e05c3 5837
7ff89ca2
VS
5838 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
5839 crtc->primary->state->visible = false;
5840 }
5d96d8af 5841
7ff89ca2
VS
5842 state = drm_atomic_state_alloc(crtc->dev);
5843 if (!state) {
5844 DRM_DEBUG_KMS("failed to disable [CRTC:%d:%s], out of memory",
5845 crtc->base.id, crtc->name);
1c3f7700 5846 return;
7ff89ca2 5847 }
9f7eb31a 5848
7ff89ca2 5849 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
ea61791e 5850
7ff89ca2
VS
5851 /* Everything's already locked, -EDEADLK can't happen. */
5852 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
5853 ret = drm_atomic_add_affected_connectors(state, crtc);
9f7eb31a 5854
7ff89ca2 5855 WARN_ON(IS_ERR(crtc_state) || ret);
5d96d8af 5856
7ff89ca2 5857 dev_priv->display.crtc_disable(crtc_state, state);
4a806558 5858
0853695c 5859 drm_atomic_state_put(state);
842e0307 5860
78108b7c
VS
5861 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
5862 crtc->base.id, crtc->name);
842e0307
ML
5863
5864 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
5865 crtc->state->active = false;
37d9078b 5866 intel_crtc->active = false;
842e0307
ML
5867 crtc->enabled = false;
5868 crtc->state->connector_mask = 0;
5869 crtc->state->encoder_mask = 0;
5870
5871 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
5872 encoder->base.crtc = NULL;
5873
58f9c0bc 5874 intel_fbc_disable(intel_crtc);
432081bc 5875 intel_update_watermarks(intel_crtc);
1f7457b1 5876 intel_disable_shared_dpll(intel_crtc);
b17d48e2
ML
5877
5878 domains = intel_crtc->enabled_power_domains;
5879 for_each_power_domain(domain, domains)
5880 intel_display_power_put(dev_priv, domain);
5881 intel_crtc->enabled_power_domains = 0;
565602d7
ML
5882
5883 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
5884 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
b17d48e2
ML
5885}
5886
6b72d486
ML
5887/*
5888 * turn all crtc's off, but do not adjust state
5889 * This has to be paired with a call to intel_modeset_setup_hw_state.
5890 */
70e0bd74 5891int intel_display_suspend(struct drm_device *dev)
ee7b9f93 5892{
e2c8b870 5893 struct drm_i915_private *dev_priv = to_i915(dev);
70e0bd74 5894 struct drm_atomic_state *state;
e2c8b870 5895 int ret;
70e0bd74 5896
e2c8b870
ML
5897 state = drm_atomic_helper_suspend(dev);
5898 ret = PTR_ERR_OR_ZERO(state);
70e0bd74
ML
5899 if (ret)
5900 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
e2c8b870
ML
5901 else
5902 dev_priv->modeset_restore_state = state;
70e0bd74 5903 return ret;
ee7b9f93
JB
5904}
5905
ea5b213a 5906void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 5907{
4ef69c7a 5908 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 5909
ea5b213a
CW
5910 drm_encoder_cleanup(encoder);
5911 kfree(intel_encoder);
7e7d76c3
JB
5912}
5913
0a91ca29
DV
5914/* Cross check the actual hw state with our own modeset state tracking (and it's
5915 * internal consistency). */
5a21b665 5916static void intel_connector_verify_state(struct intel_connector *connector)
79e53945 5917{
5a21b665 5918 struct drm_crtc *crtc = connector->base.state->crtc;
35dd3c64
ML
5919
5920 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
5921 connector->base.base.id,
5922 connector->base.name);
5923
0a91ca29 5924 if (connector->get_hw_state(connector)) {
e85376cb 5925 struct intel_encoder *encoder = connector->encoder;
5a21b665 5926 struct drm_connector_state *conn_state = connector->base.state;
0a91ca29 5927
35dd3c64
ML
5928 I915_STATE_WARN(!crtc,
5929 "connector enabled without attached crtc\n");
0a91ca29 5930
35dd3c64
ML
5931 if (!crtc)
5932 return;
5933
5934 I915_STATE_WARN(!crtc->state->active,
5935 "connector is active, but attached crtc isn't\n");
5936
e85376cb 5937 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
35dd3c64
ML
5938 return;
5939
e85376cb 5940 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
35dd3c64
ML
5941 "atomic encoder doesn't match attached encoder\n");
5942
e85376cb 5943 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
35dd3c64
ML
5944 "attached encoder crtc differs from connector crtc\n");
5945 } else {
4d688a2a
ML
5946 I915_STATE_WARN(crtc && crtc->state->active,
5947 "attached crtc is active, but connector isn't\n");
5a21b665 5948 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
35dd3c64 5949 "best encoder set without crtc!\n");
0a91ca29 5950 }
79e53945
JB
5951}
5952
08d9bc92
ACO
5953int intel_connector_init(struct intel_connector *connector)
5954{
5350a031 5955 drm_atomic_helper_connector_reset(&connector->base);
08d9bc92 5956
5350a031 5957 if (!connector->base.state)
08d9bc92
ACO
5958 return -ENOMEM;
5959
08d9bc92
ACO
5960 return 0;
5961}
5962
5963struct intel_connector *intel_connector_alloc(void)
5964{
5965 struct intel_connector *connector;
5966
5967 connector = kzalloc(sizeof *connector, GFP_KERNEL);
5968 if (!connector)
5969 return NULL;
5970
5971 if (intel_connector_init(connector) < 0) {
5972 kfree(connector);
5973 return NULL;
5974 }
5975
5976 return connector;
5977}
5978
f0947c37
DV
5979/* Simple connector->get_hw_state implementation for encoders that support only
5980 * one connector and no cloning and hence the encoder state determines the state
5981 * of the connector. */
5982bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 5983{
24929352 5984 enum pipe pipe = 0;
f0947c37 5985 struct intel_encoder *encoder = connector->encoder;
ea5b213a 5986
f0947c37 5987 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
5988}
5989
6d293983 5990static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
d272ddfa 5991{
6d293983
ACO
5992 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
5993 return crtc_state->fdi_lanes;
d272ddfa
VS
5994
5995 return 0;
5996}
5997
6d293983 5998static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5cec258b 5999 struct intel_crtc_state *pipe_config)
1857e1da 6000{
8652744b 6001 struct drm_i915_private *dev_priv = to_i915(dev);
6d293983
ACO
6002 struct drm_atomic_state *state = pipe_config->base.state;
6003 struct intel_crtc *other_crtc;
6004 struct intel_crtc_state *other_crtc_state;
6005
1857e1da
DV
6006 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6007 pipe_name(pipe), pipe_config->fdi_lanes);
6008 if (pipe_config->fdi_lanes > 4) {
6009 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6010 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6011 return -EINVAL;
1857e1da
DV
6012 }
6013
8652744b 6014 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
1857e1da
DV
6015 if (pipe_config->fdi_lanes > 2) {
6016 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6017 pipe_config->fdi_lanes);
6d293983 6018 return -EINVAL;
1857e1da 6019 } else {
6d293983 6020 return 0;
1857e1da
DV
6021 }
6022 }
6023
b7f05d4a 6024 if (INTEL_INFO(dev_priv)->num_pipes == 2)
6d293983 6025 return 0;
1857e1da
DV
6026
6027 /* Ivybridge 3 pipe is really complicated */
6028 switch (pipe) {
6029 case PIPE_A:
6d293983 6030 return 0;
1857e1da 6031 case PIPE_B:
6d293983
ACO
6032 if (pipe_config->fdi_lanes <= 2)
6033 return 0;
6034
b91eb5cc 6035 other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
6d293983
ACO
6036 other_crtc_state =
6037 intel_atomic_get_crtc_state(state, other_crtc);
6038 if (IS_ERR(other_crtc_state))
6039 return PTR_ERR(other_crtc_state);
6040
6041 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
1857e1da
DV
6042 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6043 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6044 return -EINVAL;
1857e1da 6045 }
6d293983 6046 return 0;
1857e1da 6047 case PIPE_C:
251cc67c
VS
6048 if (pipe_config->fdi_lanes > 2) {
6049 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6050 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6051 return -EINVAL;
251cc67c 6052 }
6d293983 6053
b91eb5cc 6054 other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
6d293983
ACO
6055 other_crtc_state =
6056 intel_atomic_get_crtc_state(state, other_crtc);
6057 if (IS_ERR(other_crtc_state))
6058 return PTR_ERR(other_crtc_state);
6059
6060 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
1857e1da 6061 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
6d293983 6062 return -EINVAL;
1857e1da 6063 }
6d293983 6064 return 0;
1857e1da
DV
6065 default:
6066 BUG();
6067 }
6068}
6069
e29c22c0
DV
6070#define RETRY 1
6071static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5cec258b 6072 struct intel_crtc_state *pipe_config)
877d48d5 6073{
1857e1da 6074 struct drm_device *dev = intel_crtc->base.dev;
7c5f93b0 6075 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6d293983
ACO
6076 int lane, link_bw, fdi_dotclock, ret;
6077 bool needs_recompute = false;
877d48d5 6078
e29c22c0 6079retry:
877d48d5
DV
6080 /* FDI is a binary signal running at ~2.7GHz, encoding
6081 * each output octet as 10 bits. The actual frequency
6082 * is stored as a divider into a 100MHz clock, and the
6083 * mode pixel clock is stored in units of 1KHz.
6084 * Hence the bw of each lane in terms of the mode signal
6085 * is:
6086 */
21a727b3 6087 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
877d48d5 6088
241bfc38 6089 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 6090
2bd89a07 6091 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
DV
6092 pipe_config->pipe_bpp);
6093
6094 pipe_config->fdi_lanes = lane;
6095
2bd89a07 6096 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
877d48d5 6097 link_bw, &pipe_config->fdi_m_n);
1857e1da 6098
e3b247da 6099 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
6d293983 6100 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
e29c22c0 6101 pipe_config->pipe_bpp -= 2*3;
7ff89ca2
VS
6102 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6103 pipe_config->pipe_bpp);
6104 needs_recompute = true;
6105 pipe_config->bw_constrained = true;
257a7ffc 6106
7ff89ca2 6107 goto retry;
257a7ffc 6108 }
79e53945 6109
7ff89ca2
VS
6110 if (needs_recompute)
6111 return RETRY;
e70236a8 6112
7ff89ca2 6113 return ret;
e70236a8
JB
6114}
6115
7ff89ca2
VS
6116static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6117 struct intel_crtc_state *pipe_config)
e70236a8 6118{
7ff89ca2
VS
6119 if (pipe_config->pipe_bpp > 24)
6120 return false;
e70236a8 6121
7ff89ca2
VS
6122 /* HSW can handle pixel rate up to cdclk? */
6123 if (IS_HASWELL(dev_priv))
6124 return true;
1b1d2716 6125
65cd2b3f 6126 /*
7ff89ca2
VS
6127 * We compare against max which means we must take
6128 * the increased cdclk requirement into account when
6129 * calculating the new cdclk.
6130 *
6131 * Should measure whether using a lower cdclk w/o IPS
e70236a8 6132 */
7ff89ca2
VS
6133 return pipe_config->pixel_rate <=
6134 dev_priv->max_cdclk_freq * 95 / 100;
e70236a8 6135}
79e53945 6136
7ff89ca2
VS
6137static void hsw_compute_ips_config(struct intel_crtc *crtc,
6138 struct intel_crtc_state *pipe_config)
6139{
6140 struct drm_device *dev = crtc->base.dev;
6141 struct drm_i915_private *dev_priv = to_i915(dev);
34edce2f 6142
7ff89ca2
VS
6143 pipe_config->ips_enabled = i915.enable_ips &&
6144 hsw_crtc_supports_ips(crtc) &&
6145 pipe_config_supports_ips(dev_priv, pipe_config);
34edce2f
VS
6146}
6147
7ff89ca2 6148static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
34edce2f 6149{
7ff89ca2 6150 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
34edce2f 6151
7ff89ca2
VS
6152 /* GDG double wide on either pipe, otherwise pipe A only */
6153 return INTEL_INFO(dev_priv)->gen < 4 &&
6154 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
34edce2f
VS
6155}
6156
ceb99320
VS
6157static uint32_t ilk_pipe_pixel_rate(const struct intel_crtc_state *pipe_config)
6158{
6159 uint32_t pixel_rate;
6160
6161 pixel_rate = pipe_config->base.adjusted_mode.crtc_clock;
6162
6163 /*
6164 * We only use IF-ID interlacing. If we ever use
6165 * PF-ID we'll need to adjust the pixel_rate here.
6166 */
6167
6168 if (pipe_config->pch_pfit.enabled) {
6169 uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
6170 uint32_t pfit_size = pipe_config->pch_pfit.size;
6171
6172 pipe_w = pipe_config->pipe_src_w;
6173 pipe_h = pipe_config->pipe_src_h;
6174
6175 pfit_w = (pfit_size >> 16) & 0xFFFF;
6176 pfit_h = pfit_size & 0xFFFF;
6177 if (pipe_w < pfit_w)
6178 pipe_w = pfit_w;
6179 if (pipe_h < pfit_h)
6180 pipe_h = pfit_h;
6181
6182 if (WARN_ON(!pfit_w || !pfit_h))
6183 return pixel_rate;
6184
6185 pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
6186 pfit_w * pfit_h);
6187 }
6188
6189 return pixel_rate;
6190}
6191
7ff89ca2 6192static void intel_crtc_compute_pixel_rate(struct intel_crtc_state *crtc_state)
34edce2f 6193{
7ff89ca2 6194 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
34edce2f 6195
7ff89ca2
VS
6196 if (HAS_GMCH_DISPLAY(dev_priv))
6197 /* FIXME calculate proper pipe pixel rate for GMCH pfit */
6198 crtc_state->pixel_rate =
6199 crtc_state->base.adjusted_mode.crtc_clock;
6200 else
6201 crtc_state->pixel_rate =
6202 ilk_pipe_pixel_rate(crtc_state);
6203}
34edce2f 6204
7ff89ca2
VS
6205static int intel_crtc_compute_config(struct intel_crtc *crtc,
6206 struct intel_crtc_state *pipe_config)
6207{
6208 struct drm_device *dev = crtc->base.dev;
6209 struct drm_i915_private *dev_priv = to_i915(dev);
6210 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6211 int clock_limit = dev_priv->max_dotclk_freq;
34edce2f 6212
7ff89ca2
VS
6213 if (INTEL_GEN(dev_priv) < 4) {
6214 clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
34edce2f 6215
7ff89ca2
VS
6216 /*
6217 * Enable double wide mode when the dot clock
6218 * is > 90% of the (display) core speed.
6219 */
6220 if (intel_crtc_supports_double_wide(crtc) &&
6221 adjusted_mode->crtc_clock > clock_limit) {
6222 clock_limit = dev_priv->max_dotclk_freq;
6223 pipe_config->double_wide = true;
6224 }
34edce2f
VS
6225 }
6226
7ff89ca2
VS
6227 if (adjusted_mode->crtc_clock > clock_limit) {
6228 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6229 adjusted_mode->crtc_clock, clock_limit,
6230 yesno(pipe_config->double_wide));
6231 return -EINVAL;
6232 }
34edce2f 6233
7ff89ca2
VS
6234 /*
6235 * Pipe horizontal size must be even in:
6236 * - DVO ganged mode
6237 * - LVDS dual channel mode
6238 * - Double wide pipe
6239 */
6240 if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
6241 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6242 pipe_config->pipe_src_w &= ~1;
34edce2f 6243
7ff89ca2
VS
6244 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6245 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
6246 */
6247 if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
6248 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
6249 return -EINVAL;
34edce2f 6250
7ff89ca2 6251 intel_crtc_compute_pixel_rate(pipe_config);
34edce2f 6252
7ff89ca2
VS
6253 if (HAS_IPS(dev_priv))
6254 hsw_compute_ips_config(crtc, pipe_config);
34edce2f 6255
7ff89ca2
VS
6256 if (pipe_config->has_pch_encoder)
6257 return ironlake_fdi_compute_config(crtc, pipe_config);
34edce2f 6258
7ff89ca2 6259 return 0;
34edce2f
VS
6260}
6261
2c07245f 6262static void
a65851af 6263intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 6264{
a65851af
VS
6265 while (*num > DATA_LINK_M_N_MASK ||
6266 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
6267 *num >>= 1;
6268 *den >>= 1;
6269 }
6270}
6271
a65851af
VS
6272static void compute_m_n(unsigned int m, unsigned int n,
6273 uint32_t *ret_m, uint32_t *ret_n)
6274{
6275 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
6276 *ret_m = div_u64((uint64_t) m * *ret_n, n);
6277 intel_reduce_m_n_ratio(ret_m, ret_n);
6278}
6279
e69d0bc1
DV
6280void
6281intel_link_compute_m_n(int bits_per_pixel, int nlanes,
6282 int pixel_clock, int link_clock,
6283 struct intel_link_m_n *m_n)
2c07245f 6284{
e69d0bc1 6285 m_n->tu = 64;
a65851af
VS
6286
6287 compute_m_n(bits_per_pixel * pixel_clock,
6288 link_clock * nlanes * 8,
6289 &m_n->gmch_m, &m_n->gmch_n);
6290
6291 compute_m_n(pixel_clock, link_clock,
6292 &m_n->link_m, &m_n->link_n);
2c07245f
ZW
6293}
6294
a7615030
CW
6295static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
6296{
d330a953
JN
6297 if (i915.panel_use_ssc >= 0)
6298 return i915.panel_use_ssc != 0;
41aa3448 6299 return dev_priv->vbt.lvds_use_ssc
435793df 6300 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
6301}
6302
7429e9d4 6303static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 6304{
7df00d7a 6305 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 6306}
f47709a9 6307
7429e9d4
DV
6308static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
6309{
6310 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
6311}
6312
f47709a9 6313static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
190f68c5 6314 struct intel_crtc_state *crtc_state,
9e2c8475 6315 struct dpll *reduced_clock)
a7516a05 6316{
9b1e14f4 6317 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
a7516a05
JB
6318 u32 fp, fp2 = 0;
6319
9b1e14f4 6320 if (IS_PINEVIEW(dev_priv)) {
190f68c5 6321 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
a7516a05 6322 if (reduced_clock)
7429e9d4 6323 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 6324 } else {
190f68c5 6325 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
a7516a05 6326 if (reduced_clock)
7429e9d4 6327 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
6328 }
6329
190f68c5 6330 crtc_state->dpll_hw_state.fp0 = fp;
a7516a05 6331
f47709a9 6332 crtc->lowfreq_avail = false;
2d84d2b3 6333 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ab585dea 6334 reduced_clock) {
190f68c5 6335 crtc_state->dpll_hw_state.fp1 = fp2;
f47709a9 6336 crtc->lowfreq_avail = true;
a7516a05 6337 } else {
190f68c5 6338 crtc_state->dpll_hw_state.fp1 = fp;
a7516a05
JB
6339 }
6340}
6341
5e69f97f
CML
6342static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
6343 pipe)
89b667f8
JB
6344{
6345 u32 reg_val;
6346
6347 /*
6348 * PLLB opamp always calibrates to max value of 0x3f, force enable it
6349 * and set it to a reasonable value instead.
6350 */
ab3c759a 6351 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
6352 reg_val &= 0xffffff00;
6353 reg_val |= 0x00000030;
ab3c759a 6354 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 6355
ab3c759a 6356 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
6357 reg_val &= 0x8cffffff;
6358 reg_val = 0x8c000000;
ab3c759a 6359 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 6360
ab3c759a 6361 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 6362 reg_val &= 0xffffff00;
ab3c759a 6363 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 6364
ab3c759a 6365 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
6366 reg_val &= 0x00ffffff;
6367 reg_val |= 0xb0000000;
ab3c759a 6368 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
6369}
6370
b551842d
DV
6371static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
6372 struct intel_link_m_n *m_n)
6373{
6374 struct drm_device *dev = crtc->base.dev;
fac5e23e 6375 struct drm_i915_private *dev_priv = to_i915(dev);
b551842d
DV
6376 int pipe = crtc->pipe;
6377
e3b95f1e
DV
6378 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6379 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
6380 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
6381 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
6382}
6383
6384static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
6385 struct intel_link_m_n *m_n,
6386 struct intel_link_m_n *m2_n2)
b551842d 6387{
6315b5d3 6388 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
b551842d 6389 int pipe = crtc->pipe;
6e3c9717 6390 enum transcoder transcoder = crtc->config->cpu_transcoder;
b551842d 6391
6315b5d3 6392 if (INTEL_GEN(dev_priv) >= 5) {
b551842d
DV
6393 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
6394 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
6395 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
6396 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
f769cd24
VK
6397 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
6398 * for gen < 8) and if DRRS is supported (to make sure the
6399 * registers are not unnecessarily accessed).
6400 */
920a14b2
TU
6401 if (m2_n2 && (IS_CHERRYVIEW(dev_priv) ||
6402 INTEL_GEN(dev_priv) < 8) && crtc->config->has_drrs) {
f769cd24
VK
6403 I915_WRITE(PIPE_DATA_M2(transcoder),
6404 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
6405 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
6406 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
6407 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
6408 }
b551842d 6409 } else {
e3b95f1e
DV
6410 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6411 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
6412 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
6413 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
6414 }
6415}
6416
fe3cd48d 6417void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
03afc4a2 6418{
fe3cd48d
R
6419 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
6420
6421 if (m_n == M1_N1) {
6422 dp_m_n = &crtc->config->dp_m_n;
6423 dp_m2_n2 = &crtc->config->dp_m2_n2;
6424 } else if (m_n == M2_N2) {
6425
6426 /*
6427 * M2_N2 registers are not supported. Hence m2_n2 divider value
6428 * needs to be programmed into M1_N1.
6429 */
6430 dp_m_n = &crtc->config->dp_m2_n2;
6431 } else {
6432 DRM_ERROR("Unsupported divider value\n");
6433 return;
6434 }
6435
6e3c9717
ACO
6436 if (crtc->config->has_pch_encoder)
6437 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
03afc4a2 6438 else
fe3cd48d 6439 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
03afc4a2
DV
6440}
6441
251ac862
DV
6442static void vlv_compute_dpll(struct intel_crtc *crtc,
6443 struct intel_crtc_state *pipe_config)
bdd4b6a6 6444{
03ed5cbf 6445 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
cd2d34d9 6446 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
03ed5cbf
VS
6447 if (crtc->pipe != PIPE_A)
6448 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
bdd4b6a6 6449
cd2d34d9 6450 /* DPLL not used with DSI, but still need the rest set up */
d7edc4e5 6451 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
cd2d34d9
VS
6452 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
6453 DPLL_EXT_BUFFER_ENABLE_VLV;
6454
03ed5cbf
VS
6455 pipe_config->dpll_hw_state.dpll_md =
6456 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
6457}
bdd4b6a6 6458
03ed5cbf
VS
6459static void chv_compute_dpll(struct intel_crtc *crtc,
6460 struct intel_crtc_state *pipe_config)
6461{
6462 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
cd2d34d9 6463 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
03ed5cbf
VS
6464 if (crtc->pipe != PIPE_A)
6465 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
6466
cd2d34d9 6467 /* DPLL not used with DSI, but still need the rest set up */
d7edc4e5 6468 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
cd2d34d9
VS
6469 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
6470
03ed5cbf
VS
6471 pipe_config->dpll_hw_state.dpll_md =
6472 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
bdd4b6a6
DV
6473}
6474
d288f65f 6475static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 6476 const struct intel_crtc_state *pipe_config)
a0c4da24 6477{
f47709a9 6478 struct drm_device *dev = crtc->base.dev;
fac5e23e 6479 struct drm_i915_private *dev_priv = to_i915(dev);
cd2d34d9 6480 enum pipe pipe = crtc->pipe;
bdd4b6a6 6481 u32 mdiv;
a0c4da24 6482 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 6483 u32 coreclk, reg_val;
a0c4da24 6484
cd2d34d9
VS
6485 /* Enable Refclk */
6486 I915_WRITE(DPLL(pipe),
6487 pipe_config->dpll_hw_state.dpll &
6488 ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
6489
6490 /* No need to actually set up the DPLL with DSI */
6491 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
6492 return;
6493
a580516d 6494 mutex_lock(&dev_priv->sb_lock);
09153000 6495
d288f65f
VS
6496 bestn = pipe_config->dpll.n;
6497 bestm1 = pipe_config->dpll.m1;
6498 bestm2 = pipe_config->dpll.m2;
6499 bestp1 = pipe_config->dpll.p1;
6500 bestp2 = pipe_config->dpll.p2;
a0c4da24 6501
89b667f8
JB
6502 /* See eDP HDMI DPIO driver vbios notes doc */
6503
6504 /* PLL B needs special handling */
bdd4b6a6 6505 if (pipe == PIPE_B)
5e69f97f 6506 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
6507
6508 /* Set up Tx target for periodic Rcomp update */
ab3c759a 6509 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
6510
6511 /* Disable target IRef on PLL */
ab3c759a 6512 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 6513 reg_val &= 0x00ffffff;
ab3c759a 6514 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
6515
6516 /* Disable fast lock */
ab3c759a 6517 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
6518
6519 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
6520 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
6521 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
6522 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 6523 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
6524
6525 /*
6526 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
6527 * but we don't support that).
6528 * Note: don't use the DAC post divider as it seems unstable.
6529 */
6530 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 6531 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 6532
a0c4da24 6533 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 6534 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 6535
89b667f8 6536 /* Set HBR and RBR LPF coefficients */
d288f65f 6537 if (pipe_config->port_clock == 162000 ||
2d84d2b3
VS
6538 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
6539 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
ab3c759a 6540 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 6541 0x009f0003);
89b667f8 6542 else
ab3c759a 6543 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
6544 0x00d0000f);
6545
37a5650b 6546 if (intel_crtc_has_dp_encoder(pipe_config)) {
89b667f8 6547 /* Use SSC source */
bdd4b6a6 6548 if (pipe == PIPE_A)
ab3c759a 6549 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6550 0x0df40000);
6551 else
ab3c759a 6552 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6553 0x0df70000);
6554 } else { /* HDMI or VGA */
6555 /* Use bend source */
bdd4b6a6 6556 if (pipe == PIPE_A)
ab3c759a 6557 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6558 0x0df70000);
6559 else
ab3c759a 6560 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6561 0x0df40000);
6562 }
a0c4da24 6563
ab3c759a 6564 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8 6565 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
2210ce7f 6566 if (intel_crtc_has_dp_encoder(crtc->config))
89b667f8 6567 coreclk |= 0x01000000;
ab3c759a 6568 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 6569
ab3c759a 6570 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
a580516d 6571 mutex_unlock(&dev_priv->sb_lock);
a0c4da24
JB
6572}
6573
d288f65f 6574static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 6575 const struct intel_crtc_state *pipe_config)
9d556c99
CML
6576{
6577 struct drm_device *dev = crtc->base.dev;
fac5e23e 6578 struct drm_i915_private *dev_priv = to_i915(dev);
cd2d34d9 6579 enum pipe pipe = crtc->pipe;
9d556c99 6580 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9cbe40c1 6581 u32 loopfilter, tribuf_calcntr;
9d556c99 6582 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
a945ce7e 6583 u32 dpio_val;
9cbe40c1 6584 int vco;
9d556c99 6585
cd2d34d9
VS
6586 /* Enable Refclk and SSC */
6587 I915_WRITE(DPLL(pipe),
6588 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
6589
6590 /* No need to actually set up the DPLL with DSI */
6591 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
6592 return;
6593
d288f65f
VS
6594 bestn = pipe_config->dpll.n;
6595 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
6596 bestm1 = pipe_config->dpll.m1;
6597 bestm2 = pipe_config->dpll.m2 >> 22;
6598 bestp1 = pipe_config->dpll.p1;
6599 bestp2 = pipe_config->dpll.p2;
9cbe40c1 6600 vco = pipe_config->dpll.vco;
a945ce7e 6601 dpio_val = 0;
9cbe40c1 6602 loopfilter = 0;
9d556c99 6603
a580516d 6604 mutex_lock(&dev_priv->sb_lock);
9d556c99 6605
9d556c99
CML
6606 /* p1 and p2 divider */
6607 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
6608 5 << DPIO_CHV_S1_DIV_SHIFT |
6609 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
6610 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
6611 1 << DPIO_CHV_K_DIV_SHIFT);
6612
6613 /* Feedback post-divider - m2 */
6614 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
6615
6616 /* Feedback refclk divider - n and m1 */
6617 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
6618 DPIO_CHV_M1_DIV_BY_2 |
6619 1 << DPIO_CHV_N_DIV_SHIFT);
6620
6621 /* M2 fraction division */
25a25dfc 6622 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
9d556c99
CML
6623
6624 /* M2 fraction division enable */
a945ce7e
VP
6625 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
6626 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
6627 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
6628 if (bestm2_frac)
6629 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
6630 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
9d556c99 6631
de3a0fde
VP
6632 /* Program digital lock detect threshold */
6633 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
6634 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
6635 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
6636 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
6637 if (!bestm2_frac)
6638 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
6639 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
6640
9d556c99 6641 /* Loop filter */
9cbe40c1
VP
6642 if (vco == 5400000) {
6643 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
6644 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
6645 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
6646 tribuf_calcntr = 0x9;
6647 } else if (vco <= 6200000) {
6648 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
6649 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
6650 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6651 tribuf_calcntr = 0x9;
6652 } else if (vco <= 6480000) {
6653 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
6654 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
6655 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6656 tribuf_calcntr = 0x8;
6657 } else {
6658 /* Not supported. Apply the same limits as in the max case */
6659 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
6660 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
6661 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6662 tribuf_calcntr = 0;
6663 }
9d556c99
CML
6664 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
6665
968040b2 6666 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
9cbe40c1
VP
6667 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
6668 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
6669 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
6670
9d556c99
CML
6671 /* AFC Recal */
6672 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
6673 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
6674 DPIO_AFC_RECAL);
6675
a580516d 6676 mutex_unlock(&dev_priv->sb_lock);
9d556c99
CML
6677}
6678
d288f65f
VS
6679/**
6680 * vlv_force_pll_on - forcibly enable just the PLL
6681 * @dev_priv: i915 private structure
6682 * @pipe: pipe PLL to enable
6683 * @dpll: PLL configuration
6684 *
6685 * Enable the PLL for @pipe using the supplied @dpll config. To be used
6686 * in cases where we need the PLL enabled even when @pipe is not going to
6687 * be enabled.
6688 */
30ad9814 6689int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
3f36b937 6690 const struct dpll *dpll)
d288f65f 6691{
b91eb5cc 6692 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
3f36b937
TU
6693 struct intel_crtc_state *pipe_config;
6694
6695 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
6696 if (!pipe_config)
6697 return -ENOMEM;
6698
6699 pipe_config->base.crtc = &crtc->base;
6700 pipe_config->pixel_multiplier = 1;
6701 pipe_config->dpll = *dpll;
d288f65f 6702
30ad9814 6703 if (IS_CHERRYVIEW(dev_priv)) {
3f36b937
TU
6704 chv_compute_dpll(crtc, pipe_config);
6705 chv_prepare_pll(crtc, pipe_config);
6706 chv_enable_pll(crtc, pipe_config);
d288f65f 6707 } else {
3f36b937
TU
6708 vlv_compute_dpll(crtc, pipe_config);
6709 vlv_prepare_pll(crtc, pipe_config);
6710 vlv_enable_pll(crtc, pipe_config);
d288f65f 6711 }
3f36b937
TU
6712
6713 kfree(pipe_config);
6714
6715 return 0;
d288f65f
VS
6716}
6717
6718/**
6719 * vlv_force_pll_off - forcibly disable just the PLL
6720 * @dev_priv: i915 private structure
6721 * @pipe: pipe PLL to disable
6722 *
6723 * Disable the PLL for @pipe. To be used in cases where we need
6724 * the PLL enabled even when @pipe is not going to be enabled.
6725 */
30ad9814 6726void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
d288f65f 6727{
30ad9814
VS
6728 if (IS_CHERRYVIEW(dev_priv))
6729 chv_disable_pll(dev_priv, pipe);
d288f65f 6730 else
30ad9814 6731 vlv_disable_pll(dev_priv, pipe);
d288f65f
VS
6732}
6733
251ac862
DV
6734static void i9xx_compute_dpll(struct intel_crtc *crtc,
6735 struct intel_crtc_state *crtc_state,
9e2c8475 6736 struct dpll *reduced_clock)
eb1cbe48 6737{
9b1e14f4 6738 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
eb1cbe48 6739 u32 dpll;
190f68c5 6740 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 6741
190f68c5 6742 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 6743
eb1cbe48
DV
6744 dpll = DPLL_VGA_MODE_DIS;
6745
2d84d2b3 6746 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
6747 dpll |= DPLLB_MODE_LVDS;
6748 else
6749 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 6750
73f67aa8
JN
6751 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
6752 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
190f68c5 6753 dpll |= (crtc_state->pixel_multiplier - 1)
198a037f 6754 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 6755 }
198a037f 6756
3d6e9ee0
VS
6757 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
6758 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
4a33e48d 6759 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 6760
37a5650b 6761 if (intel_crtc_has_dp_encoder(crtc_state))
4a33e48d 6762 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
6763
6764 /* compute bitmask from p1 value */
9b1e14f4 6765 if (IS_PINEVIEW(dev_priv))
eb1cbe48
DV
6766 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
6767 else {
6768 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
9beb5fea 6769 if (IS_G4X(dev_priv) && reduced_clock)
eb1cbe48
DV
6770 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
6771 }
6772 switch (clock->p2) {
6773 case 5:
6774 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
6775 break;
6776 case 7:
6777 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
6778 break;
6779 case 10:
6780 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
6781 break;
6782 case 14:
6783 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
6784 break;
6785 }
9b1e14f4 6786 if (INTEL_GEN(dev_priv) >= 4)
eb1cbe48
DV
6787 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
6788
190f68c5 6789 if (crtc_state->sdvo_tv_clock)
eb1cbe48 6790 dpll |= PLL_REF_INPUT_TVCLKINBC;
2d84d2b3 6791 else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 6792 intel_panel_use_ssc(dev_priv))
eb1cbe48
DV
6793 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
6794 else
6795 dpll |= PLL_REF_INPUT_DREFCLK;
6796
6797 dpll |= DPLL_VCO_ENABLE;
190f68c5 6798 crtc_state->dpll_hw_state.dpll = dpll;
8bcc2795 6799
9b1e14f4 6800 if (INTEL_GEN(dev_priv) >= 4) {
190f68c5 6801 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
ef1b460d 6802 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
190f68c5 6803 crtc_state->dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
DV
6804 }
6805}
6806
251ac862
DV
6807static void i8xx_compute_dpll(struct intel_crtc *crtc,
6808 struct intel_crtc_state *crtc_state,
9e2c8475 6809 struct dpll *reduced_clock)
eb1cbe48 6810{
f47709a9 6811 struct drm_device *dev = crtc->base.dev;
fac5e23e 6812 struct drm_i915_private *dev_priv = to_i915(dev);
eb1cbe48 6813 u32 dpll;
190f68c5 6814 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 6815
190f68c5 6816 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 6817
eb1cbe48
DV
6818 dpll = DPLL_VGA_MODE_DIS;
6819
2d84d2b3 6820 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
6821 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6822 } else {
6823 if (clock->p1 == 2)
6824 dpll |= PLL_P1_DIVIDE_BY_TWO;
6825 else
6826 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6827 if (clock->p2 == 4)
6828 dpll |= PLL_P2_DIVIDE_BY_4;
6829 }
6830
50a0bc90
TU
6831 if (!IS_I830(dev_priv) &&
6832 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
4a33e48d
DV
6833 dpll |= DPLL_DVO_2X_MODE;
6834
2d84d2b3 6835 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 6836 intel_panel_use_ssc(dev_priv))
eb1cbe48
DV
6837 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
6838 else
6839 dpll |= PLL_REF_INPUT_DREFCLK;
6840
6841 dpll |= DPLL_VCO_ENABLE;
190f68c5 6842 crtc_state->dpll_hw_state.dpll = dpll;
eb1cbe48
DV
6843}
6844
8a654f3b 6845static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c 6846{
6315b5d3 6847 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
b0e77b9c 6848 enum pipe pipe = intel_crtc->pipe;
6e3c9717 6849 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
7c5f93b0 6850 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
1caea6e9
VS
6851 uint32_t crtc_vtotal, crtc_vblank_end;
6852 int vsyncshift = 0;
4d8a62ea
DV
6853
6854 /* We need to be careful not to changed the adjusted mode, for otherwise
6855 * the hw state checker will get angry at the mismatch. */
6856 crtc_vtotal = adjusted_mode->crtc_vtotal;
6857 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 6858
609aeaca 6859 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 6860 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
6861 crtc_vtotal -= 1;
6862 crtc_vblank_end -= 1;
609aeaca 6863
2d84d2b3 6864 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
609aeaca
VS
6865 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
6866 else
6867 vsyncshift = adjusted_mode->crtc_hsync_start -
6868 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
6869 if (vsyncshift < 0)
6870 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
6871 }
6872
6315b5d3 6873 if (INTEL_GEN(dev_priv) > 3)
fe2b8f9d 6874 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 6875
fe2b8f9d 6876 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
6877 (adjusted_mode->crtc_hdisplay - 1) |
6878 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 6879 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
6880 (adjusted_mode->crtc_hblank_start - 1) |
6881 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 6882 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
6883 (adjusted_mode->crtc_hsync_start - 1) |
6884 ((adjusted_mode->crtc_hsync_end - 1) << 16));
6885
fe2b8f9d 6886 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 6887 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 6888 ((crtc_vtotal - 1) << 16));
fe2b8f9d 6889 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 6890 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 6891 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 6892 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
6893 (adjusted_mode->crtc_vsync_start - 1) |
6894 ((adjusted_mode->crtc_vsync_end - 1) << 16));
6895
b5e508d4
PZ
6896 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
6897 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
6898 * documented on the DDI_FUNC_CTL register description, EDP Input Select
6899 * bits. */
772c2a51 6900 if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
b5e508d4
PZ
6901 (pipe == PIPE_B || pipe == PIPE_C))
6902 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
6903
bc58be60
JN
6904}
6905
6906static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
6907{
6908 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 6909 struct drm_i915_private *dev_priv = to_i915(dev);
bc58be60
JN
6910 enum pipe pipe = intel_crtc->pipe;
6911
b0e77b9c
PZ
6912 /* pipesrc controls the size that is scaled from, which should
6913 * always be the user's requested size.
6914 */
6915 I915_WRITE(PIPESRC(pipe),
6e3c9717
ACO
6916 ((intel_crtc->config->pipe_src_w - 1) << 16) |
6917 (intel_crtc->config->pipe_src_h - 1));
b0e77b9c
PZ
6918}
6919
1bd1bd80 6920static void intel_get_pipe_timings(struct intel_crtc *crtc,
5cec258b 6921 struct intel_crtc_state *pipe_config)
1bd1bd80
DV
6922{
6923 struct drm_device *dev = crtc->base.dev;
fac5e23e 6924 struct drm_i915_private *dev_priv = to_i915(dev);
1bd1bd80
DV
6925 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
6926 uint32_t tmp;
6927
6928 tmp = I915_READ(HTOTAL(cpu_transcoder));
2d112de7
ACO
6929 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
6930 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 6931 tmp = I915_READ(HBLANK(cpu_transcoder));
2d112de7
ACO
6932 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
6933 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 6934 tmp = I915_READ(HSYNC(cpu_transcoder));
2d112de7
ACO
6935 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
6936 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
6937
6938 tmp = I915_READ(VTOTAL(cpu_transcoder));
2d112de7
ACO
6939 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
6940 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 6941 tmp = I915_READ(VBLANK(cpu_transcoder));
2d112de7
ACO
6942 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
6943 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 6944 tmp = I915_READ(VSYNC(cpu_transcoder));
2d112de7
ACO
6945 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
6946 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
6947
6948 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
2d112de7
ACO
6949 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
6950 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
6951 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
1bd1bd80 6952 }
bc58be60
JN
6953}
6954
6955static void intel_get_pipe_src_size(struct intel_crtc *crtc,
6956 struct intel_crtc_state *pipe_config)
6957{
6958 struct drm_device *dev = crtc->base.dev;
fac5e23e 6959 struct drm_i915_private *dev_priv = to_i915(dev);
bc58be60 6960 u32 tmp;
1bd1bd80
DV
6961
6962 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
6963 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
6964 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
6965
2d112de7
ACO
6966 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
6967 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
DV
6968}
6969
f6a83288 6970void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5cec258b 6971 struct intel_crtc_state *pipe_config)
babea61d 6972{
2d112de7
ACO
6973 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
6974 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
6975 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
6976 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
babea61d 6977
2d112de7
ACO
6978 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
6979 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
6980 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
6981 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
babea61d 6982
2d112de7 6983 mode->flags = pipe_config->base.adjusted_mode.flags;
cd13f5ab 6984 mode->type = DRM_MODE_TYPE_DRIVER;
babea61d 6985
2d112de7 6986 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
cd13f5ab
ML
6987
6988 mode->hsync = drm_mode_hsync(mode);
6989 mode->vrefresh = drm_mode_vrefresh(mode);
6990 drm_mode_set_name(mode);
babea61d
JB
6991}
6992
84b046f3
DV
6993static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
6994{
6315b5d3 6995 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
84b046f3
DV
6996 uint32_t pipeconf;
6997
9f11a9e4 6998 pipeconf = 0;
84b046f3 6999
b6b5d049
VS
7000 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7001 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7002 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
67c72a12 7003
6e3c9717 7004 if (intel_crtc->config->double_wide)
cf532bb2 7005 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 7006
ff9ce46e 7007 /* only g4x and later have fancy bpc/dither controls */
9beb5fea
TU
7008 if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
7009 IS_CHERRYVIEW(dev_priv)) {
ff9ce46e 7010 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6e3c9717 7011 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
ff9ce46e 7012 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 7013 PIPECONF_DITHER_TYPE_SP;
84b046f3 7014
6e3c9717 7015 switch (intel_crtc->config->pipe_bpp) {
ff9ce46e
DV
7016 case 18:
7017 pipeconf |= PIPECONF_6BPC;
7018 break;
7019 case 24:
7020 pipeconf |= PIPECONF_8BPC;
7021 break;
7022 case 30:
7023 pipeconf |= PIPECONF_10BPC;
7024 break;
7025 default:
7026 /* Case prevented by intel_choose_pipe_bpp_dither. */
7027 BUG();
84b046f3
DV
7028 }
7029 }
7030
56b857a5 7031 if (HAS_PIPE_CXSR(dev_priv)) {
84b046f3
DV
7032 if (intel_crtc->lowfreq_avail) {
7033 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7034 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7035 } else {
7036 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
84b046f3
DV
7037 }
7038 }
7039
6e3c9717 7040 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
6315b5d3 7041 if (INTEL_GEN(dev_priv) < 4 ||
2d84d2b3 7042 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
efc2cfff
VS
7043 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7044 else
7045 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7046 } else
84b046f3
DV
7047 pipeconf |= PIPECONF_PROGRESSIVE;
7048
920a14b2 7049 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
666a4537 7050 intel_crtc->config->limited_color_range)
9f11a9e4 7051 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 7052
84b046f3
DV
7053 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7054 POSTING_READ(PIPECONF(intel_crtc->pipe));
7055}
7056
81c97f52
ACO
7057static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
7058 struct intel_crtc_state *crtc_state)
7059{
7060 struct drm_device *dev = crtc->base.dev;
fac5e23e 7061 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 7062 const struct intel_limit *limit;
81c97f52
ACO
7063 int refclk = 48000;
7064
7065 memset(&crtc_state->dpll_hw_state, 0,
7066 sizeof(crtc_state->dpll_hw_state));
7067
2d84d2b3 7068 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
81c97f52
ACO
7069 if (intel_panel_use_ssc(dev_priv)) {
7070 refclk = dev_priv->vbt.lvds_ssc_freq;
7071 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7072 }
7073
7074 limit = &intel_limits_i8xx_lvds;
2d84d2b3 7075 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
81c97f52
ACO
7076 limit = &intel_limits_i8xx_dvo;
7077 } else {
7078 limit = &intel_limits_i8xx_dac;
7079 }
7080
7081 if (!crtc_state->clock_set &&
7082 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7083 refclk, NULL, &crtc_state->dpll)) {
7084 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7085 return -EINVAL;
7086 }
7087
7088 i8xx_compute_dpll(crtc, crtc_state, NULL);
7089
7090 return 0;
7091}
7092
19ec6693
ACO
7093static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
7094 struct intel_crtc_state *crtc_state)
7095{
7096 struct drm_device *dev = crtc->base.dev;
fac5e23e 7097 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 7098 const struct intel_limit *limit;
19ec6693
ACO
7099 int refclk = 96000;
7100
7101 memset(&crtc_state->dpll_hw_state, 0,
7102 sizeof(crtc_state->dpll_hw_state));
7103
2d84d2b3 7104 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
19ec6693
ACO
7105 if (intel_panel_use_ssc(dev_priv)) {
7106 refclk = dev_priv->vbt.lvds_ssc_freq;
7107 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7108 }
7109
7110 if (intel_is_dual_link_lvds(dev))
7111 limit = &intel_limits_g4x_dual_channel_lvds;
7112 else
7113 limit = &intel_limits_g4x_single_channel_lvds;
2d84d2b3
VS
7114 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
7115 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
19ec6693 7116 limit = &intel_limits_g4x_hdmi;
2d84d2b3 7117 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
19ec6693
ACO
7118 limit = &intel_limits_g4x_sdvo;
7119 } else {
7120 /* The option is for other outputs */
7121 limit = &intel_limits_i9xx_sdvo;
7122 }
7123
7124 if (!crtc_state->clock_set &&
7125 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7126 refclk, NULL, &crtc_state->dpll)) {
7127 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7128 return -EINVAL;
7129 }
7130
7131 i9xx_compute_dpll(crtc, crtc_state, NULL);
7132
7133 return 0;
7134}
7135
70e8aa21
ACO
7136static int pnv_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;
70e8aa21
ACO
7142 int refclk = 96000;
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)) {
70e8aa21
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_pineview_lvds;
7154 } else {
7155 limit = &intel_limits_pineview_sdvo;
7156 }
7157
7158 if (!crtc_state->clock_set &&
7159 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7160 refclk, NULL, &crtc_state->dpll)) {
7161 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7162 return -EINVAL;
7163 }
7164
7165 i9xx_compute_dpll(crtc, crtc_state, NULL);
7166
7167 return 0;
7168}
7169
190f68c5
ACO
7170static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7171 struct intel_crtc_state *crtc_state)
79e53945 7172{
c7653199 7173 struct drm_device *dev = crtc->base.dev;
fac5e23e 7174 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 7175 const struct intel_limit *limit;
81c97f52 7176 int refclk = 96000;
79e53945 7177
dd3cd74a
ACO
7178 memset(&crtc_state->dpll_hw_state, 0,
7179 sizeof(crtc_state->dpll_hw_state));
7180
2d84d2b3 7181 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
70e8aa21
ACO
7182 if (intel_panel_use_ssc(dev_priv)) {
7183 refclk = dev_priv->vbt.lvds_ssc_freq;
7184 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7185 }
43565a06 7186
70e8aa21
ACO
7187 limit = &intel_limits_i9xx_lvds;
7188 } else {
7189 limit = &intel_limits_i9xx_sdvo;
81c97f52 7190 }
79e53945 7191
70e8aa21
ACO
7192 if (!crtc_state->clock_set &&
7193 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7194 refclk, NULL, &crtc_state->dpll)) {
7195 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7196 return -EINVAL;
f47709a9 7197 }
7026d4ac 7198
81c97f52 7199 i9xx_compute_dpll(crtc, crtc_state, NULL);
79e53945 7200
c8f7a0db 7201 return 0;
f564048e
EA
7202}
7203
65b3d6a9
ACO
7204static int chv_crtc_compute_clock(struct intel_crtc *crtc,
7205 struct intel_crtc_state *crtc_state)
7206{
7207 int refclk = 100000;
1b6f4958 7208 const struct intel_limit *limit = &intel_limits_chv;
65b3d6a9
ACO
7209
7210 memset(&crtc_state->dpll_hw_state, 0,
7211 sizeof(crtc_state->dpll_hw_state));
7212
65b3d6a9
ACO
7213 if (!crtc_state->clock_set &&
7214 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7215 refclk, NULL, &crtc_state->dpll)) {
7216 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7217 return -EINVAL;
7218 }
7219
7220 chv_compute_dpll(crtc, crtc_state);
7221
7222 return 0;
7223}
7224
7225static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
7226 struct intel_crtc_state *crtc_state)
7227{
7228 int refclk = 100000;
1b6f4958 7229 const struct intel_limit *limit = &intel_limits_vlv;
65b3d6a9
ACO
7230
7231 memset(&crtc_state->dpll_hw_state, 0,
7232 sizeof(crtc_state->dpll_hw_state));
7233
65b3d6a9
ACO
7234 if (!crtc_state->clock_set &&
7235 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7236 refclk, NULL, &crtc_state->dpll)) {
7237 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7238 return -EINVAL;
7239 }
7240
7241 vlv_compute_dpll(crtc, crtc_state);
7242
7243 return 0;
7244}
7245
2fa2fe9a 7246static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5cec258b 7247 struct intel_crtc_state *pipe_config)
2fa2fe9a 7248{
6315b5d3 7249 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2fa2fe9a
DV
7250 uint32_t tmp;
7251
50a0bc90
TU
7252 if (INTEL_GEN(dev_priv) <= 3 &&
7253 (IS_I830(dev_priv) || !IS_MOBILE(dev_priv)))
dc9e7dec
VS
7254 return;
7255
2fa2fe9a 7256 tmp = I915_READ(PFIT_CONTROL);
06922821
DV
7257 if (!(tmp & PFIT_ENABLE))
7258 return;
2fa2fe9a 7259
06922821 7260 /* Check whether the pfit is attached to our pipe. */
6315b5d3 7261 if (INTEL_GEN(dev_priv) < 4) {
2fa2fe9a
DV
7262 if (crtc->pipe != PIPE_B)
7263 return;
2fa2fe9a
DV
7264 } else {
7265 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7266 return;
7267 }
7268
06922821 7269 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a 7270 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
2fa2fe9a
DV
7271}
7272
acbec814 7273static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 7274 struct intel_crtc_state *pipe_config)
acbec814
JB
7275{
7276 struct drm_device *dev = crtc->base.dev;
fac5e23e 7277 struct drm_i915_private *dev_priv = to_i915(dev);
acbec814 7278 int pipe = pipe_config->cpu_transcoder;
9e2c8475 7279 struct dpll clock;
acbec814 7280 u32 mdiv;
662c6ecb 7281 int refclk = 100000;
acbec814 7282
b521973b
VS
7283 /* In case of DSI, DPLL will not be used */
7284 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
f573de5a
SK
7285 return;
7286
a580516d 7287 mutex_lock(&dev_priv->sb_lock);
ab3c759a 7288 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
a580516d 7289 mutex_unlock(&dev_priv->sb_lock);
acbec814
JB
7290
7291 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7292 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7293 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7294 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7295 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7296
dccbea3b 7297 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
acbec814
JB
7298}
7299
5724dbd1
DL
7300static void
7301i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7302 struct intel_initial_plane_config *plane_config)
1ad292b5
JB
7303{
7304 struct drm_device *dev = crtc->base.dev;
fac5e23e 7305 struct drm_i915_private *dev_priv = to_i915(dev);
1ad292b5
JB
7306 u32 val, base, offset;
7307 int pipe = crtc->pipe, plane = crtc->plane;
7308 int fourcc, pixel_format;
6761dd31 7309 unsigned int aligned_height;
b113d5ee 7310 struct drm_framebuffer *fb;
1b842c89 7311 struct intel_framebuffer *intel_fb;
1ad292b5 7312
42a7b088
DL
7313 val = I915_READ(DSPCNTR(plane));
7314 if (!(val & DISPLAY_PLANE_ENABLE))
7315 return;
7316
d9806c9f 7317 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 7318 if (!intel_fb) {
1ad292b5
JB
7319 DRM_DEBUG_KMS("failed to alloc fb\n");
7320 return;
7321 }
7322
1b842c89
DL
7323 fb = &intel_fb->base;
7324
d2e9f5fc
VS
7325 fb->dev = dev;
7326
6315b5d3 7327 if (INTEL_GEN(dev_priv) >= 4) {
18c5247e 7328 if (val & DISPPLANE_TILED) {
49af449b 7329 plane_config->tiling = I915_TILING_X;
bae781b2 7330 fb->modifier = I915_FORMAT_MOD_X_TILED;
18c5247e
DV
7331 }
7332 }
1ad292b5
JB
7333
7334 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 7335 fourcc = i9xx_format_to_fourcc(pixel_format);
2f3f4763 7336 fb->format = drm_format_info(fourcc);
1ad292b5 7337
6315b5d3 7338 if (INTEL_GEN(dev_priv) >= 4) {
49af449b 7339 if (plane_config->tiling)
1ad292b5
JB
7340 offset = I915_READ(DSPTILEOFF(plane));
7341 else
7342 offset = I915_READ(DSPLINOFF(plane));
7343 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7344 } else {
7345 base = I915_READ(DSPADDR(plane));
7346 }
7347 plane_config->base = base;
7348
7349 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
7350 fb->width = ((val >> 16) & 0xfff) + 1;
7351 fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
7352
7353 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 7354 fb->pitches[0] = val & 0xffffffc0;
1ad292b5 7355
24dbf51a
CW
7356 aligned_height = intel_fb_align_height(dev_priv,
7357 fb->height,
438b74a5 7358 fb->format->format,
bae781b2 7359 fb->modifier);
1ad292b5 7360
f37b5c2b 7361 plane_config->size = fb->pitches[0] * aligned_height;
1ad292b5 7362
2844a921
DL
7363 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7364 pipe_name(pipe), plane, fb->width, fb->height,
272725c7 7365 fb->format->cpp[0] * 8, base, fb->pitches[0],
2844a921 7366 plane_config->size);
1ad292b5 7367
2d14030b 7368 plane_config->fb = intel_fb;
1ad292b5
JB
7369}
7370
70b23a98 7371static void chv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 7372 struct intel_crtc_state *pipe_config)
70b23a98
VS
7373{
7374 struct drm_device *dev = crtc->base.dev;
fac5e23e 7375 struct drm_i915_private *dev_priv = to_i915(dev);
70b23a98
VS
7376 int pipe = pipe_config->cpu_transcoder;
7377 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9e2c8475 7378 struct dpll clock;
0d7b6b11 7379 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
70b23a98
VS
7380 int refclk = 100000;
7381
b521973b
VS
7382 /* In case of DSI, DPLL will not be used */
7383 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7384 return;
7385
a580516d 7386 mutex_lock(&dev_priv->sb_lock);
70b23a98
VS
7387 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
7388 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
7389 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
7390 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
0d7b6b11 7391 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
a580516d 7392 mutex_unlock(&dev_priv->sb_lock);
70b23a98
VS
7393
7394 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
0d7b6b11
ID
7395 clock.m2 = (pll_dw0 & 0xff) << 22;
7396 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
7397 clock.m2 |= pll_dw2 & 0x3fffff;
70b23a98
VS
7398 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
7399 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
7400 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
7401
dccbea3b 7402 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
70b23a98
VS
7403}
7404
0e8ffe1b 7405static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5cec258b 7406 struct intel_crtc_state *pipe_config)
0e8ffe1b 7407{
6315b5d3 7408 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1729050e 7409 enum intel_display_power_domain power_domain;
0e8ffe1b 7410 uint32_t tmp;
1729050e 7411 bool ret;
0e8ffe1b 7412
1729050e
ID
7413 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
7414 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0
ID
7415 return false;
7416
e143a21c 7417 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 7418 pipe_config->shared_dpll = NULL;
eccb140b 7419
1729050e
ID
7420 ret = false;
7421
0e8ffe1b
DV
7422 tmp = I915_READ(PIPECONF(crtc->pipe));
7423 if (!(tmp & PIPECONF_ENABLE))
1729050e 7424 goto out;
0e8ffe1b 7425
9beb5fea
TU
7426 if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
7427 IS_CHERRYVIEW(dev_priv)) {
42571aef
VS
7428 switch (tmp & PIPECONF_BPC_MASK) {
7429 case PIPECONF_6BPC:
7430 pipe_config->pipe_bpp = 18;
7431 break;
7432 case PIPECONF_8BPC:
7433 pipe_config->pipe_bpp = 24;
7434 break;
7435 case PIPECONF_10BPC:
7436 pipe_config->pipe_bpp = 30;
7437 break;
7438 default:
7439 break;
7440 }
7441 }
7442
920a14b2 7443 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
666a4537 7444 (tmp & PIPECONF_COLOR_RANGE_SELECT))
b5a9fa09
DV
7445 pipe_config->limited_color_range = true;
7446
6315b5d3 7447 if (INTEL_GEN(dev_priv) < 4)
282740f7
VS
7448 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
7449
1bd1bd80 7450 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 7451 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 7452
2fa2fe9a
DV
7453 i9xx_get_pfit_config(crtc, pipe_config);
7454
6315b5d3 7455 if (INTEL_GEN(dev_priv) >= 4) {
c231775c 7456 /* No way to read it out on pipes B and C */
920a14b2 7457 if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
c231775c
VS
7458 tmp = dev_priv->chv_dpll_md[crtc->pipe];
7459 else
7460 tmp = I915_READ(DPLL_MD(crtc->pipe));
6c49f241
DV
7461 pipe_config->pixel_multiplier =
7462 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
7463 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 7464 pipe_config->dpll_hw_state.dpll_md = tmp;
50a0bc90 7465 } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
73f67aa8 7466 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
6c49f241
DV
7467 tmp = I915_READ(DPLL(crtc->pipe));
7468 pipe_config->pixel_multiplier =
7469 ((tmp & SDVO_MULTIPLIER_MASK)
7470 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
7471 } else {
7472 /* Note that on i915G/GM the pixel multiplier is in the sdvo
7473 * port and will be fixed up in the encoder->get_config
7474 * function. */
7475 pipe_config->pixel_multiplier = 1;
7476 }
8bcc2795 7477 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
920a14b2 7478 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
1c4e0274
VS
7479 /*
7480 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
7481 * on 830. Filter it out here so that we don't
7482 * report errors due to that.
7483 */
50a0bc90 7484 if (IS_I830(dev_priv))
1c4e0274
VS
7485 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
7486
8bcc2795
DV
7487 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
7488 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
7489 } else {
7490 /* Mask out read-only status bits. */
7491 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
7492 DPLL_PORTC_READY_MASK |
7493 DPLL_PORTB_READY_MASK);
8bcc2795 7494 }
6c49f241 7495
920a14b2 7496 if (IS_CHERRYVIEW(dev_priv))
70b23a98 7497 chv_crtc_clock_get(crtc, pipe_config);
11a914c2 7498 else if (IS_VALLEYVIEW(dev_priv))
acbec814
JB
7499 vlv_crtc_clock_get(crtc, pipe_config);
7500 else
7501 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 7502
0f64614d
VS
7503 /*
7504 * Normally the dotclock is filled in by the encoder .get_config()
7505 * but in case the pipe is enabled w/o any ports we need a sane
7506 * default.
7507 */
7508 pipe_config->base.adjusted_mode.crtc_clock =
7509 pipe_config->port_clock / pipe_config->pixel_multiplier;
7510
1729050e
ID
7511 ret = true;
7512
7513out:
7514 intel_display_power_put(dev_priv, power_domain);
7515
7516 return ret;
0e8ffe1b
DV
7517}
7518
c39055b0 7519static void ironlake_init_pch_refclk(struct drm_i915_private *dev_priv)
13d83a67 7520{
13d83a67 7521 struct intel_encoder *encoder;
1c1a24d2 7522 int i;
74cfd7ac 7523 u32 val, final;
13d83a67 7524 bool has_lvds = false;
199e5d79 7525 bool has_cpu_edp = false;
199e5d79 7526 bool has_panel = false;
99eb6a01
KP
7527 bool has_ck505 = false;
7528 bool can_ssc = false;
1c1a24d2 7529 bool using_ssc_source = false;
13d83a67
JB
7530
7531 /* We need to take the global config into account */
c39055b0 7532 for_each_intel_encoder(&dev_priv->drm, encoder) {
199e5d79
KP
7533 switch (encoder->type) {
7534 case INTEL_OUTPUT_LVDS:
7535 has_panel = true;
7536 has_lvds = true;
7537 break;
7538 case INTEL_OUTPUT_EDP:
7539 has_panel = true;
2de6905f 7540 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
7541 has_cpu_edp = true;
7542 break;
6847d71b
PZ
7543 default:
7544 break;
13d83a67
JB
7545 }
7546 }
7547
6e266956 7548 if (HAS_PCH_IBX(dev_priv)) {
41aa3448 7549 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
7550 can_ssc = has_ck505;
7551 } else {
7552 has_ck505 = false;
7553 can_ssc = true;
7554 }
7555
1c1a24d2
L
7556 /* Check if any DPLLs are using the SSC source */
7557 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
7558 u32 temp = I915_READ(PCH_DPLL(i));
7559
7560 if (!(temp & DPLL_VCO_ENABLE))
7561 continue;
7562
7563 if ((temp & PLL_REF_INPUT_MASK) ==
7564 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
7565 using_ssc_source = true;
7566 break;
7567 }
7568 }
7569
7570 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
7571 has_panel, has_lvds, has_ck505, using_ssc_source);
13d83a67
JB
7572
7573 /* Ironlake: try to setup display ref clock before DPLL
7574 * enabling. This is only under driver's control after
7575 * PCH B stepping, previous chipset stepping should be
7576 * ignoring this setting.
7577 */
74cfd7ac
CW
7578 val = I915_READ(PCH_DREF_CONTROL);
7579
7580 /* As we must carefully and slowly disable/enable each source in turn,
7581 * compute the final state we want first and check if we need to
7582 * make any changes at all.
7583 */
7584 final = val;
7585 final &= ~DREF_NONSPREAD_SOURCE_MASK;
7586 if (has_ck505)
7587 final |= DREF_NONSPREAD_CK505_ENABLE;
7588 else
7589 final |= DREF_NONSPREAD_SOURCE_ENABLE;
7590
8c07eb68 7591 final &= ~DREF_SSC_SOURCE_MASK;
74cfd7ac 7592 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8c07eb68 7593 final &= ~DREF_SSC1_ENABLE;
74cfd7ac
CW
7594
7595 if (has_panel) {
7596 final |= DREF_SSC_SOURCE_ENABLE;
7597
7598 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7599 final |= DREF_SSC1_ENABLE;
7600
7601 if (has_cpu_edp) {
7602 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7603 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
7604 else
7605 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
7606 } else
7607 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
1c1a24d2
L
7608 } else if (using_ssc_source) {
7609 final |= DREF_SSC_SOURCE_ENABLE;
7610 final |= DREF_SSC1_ENABLE;
74cfd7ac
CW
7611 }
7612
7613 if (final == val)
7614 return;
7615
13d83a67 7616 /* Always enable nonspread source */
74cfd7ac 7617 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 7618
99eb6a01 7619 if (has_ck505)
74cfd7ac 7620 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 7621 else
74cfd7ac 7622 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 7623
199e5d79 7624 if (has_panel) {
74cfd7ac
CW
7625 val &= ~DREF_SSC_SOURCE_MASK;
7626 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 7627
199e5d79 7628 /* SSC must be turned on before enabling the CPU output */
99eb6a01 7629 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 7630 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 7631 val |= DREF_SSC1_ENABLE;
e77166b5 7632 } else
74cfd7ac 7633 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
7634
7635 /* Get SSC going before enabling the outputs */
74cfd7ac 7636 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
7637 POSTING_READ(PCH_DREF_CONTROL);
7638 udelay(200);
7639
74cfd7ac 7640 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
7641
7642 /* Enable CPU source on CPU attached eDP */
199e5d79 7643 if (has_cpu_edp) {
99eb6a01 7644 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 7645 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 7646 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 7647 } else
74cfd7ac 7648 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 7649 } else
74cfd7ac 7650 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 7651
74cfd7ac 7652 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
7653 POSTING_READ(PCH_DREF_CONTROL);
7654 udelay(200);
7655 } else {
1c1a24d2 7656 DRM_DEBUG_KMS("Disabling CPU source output\n");
199e5d79 7657
74cfd7ac 7658 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
7659
7660 /* Turn off CPU output */
74cfd7ac 7661 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 7662
74cfd7ac 7663 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
7664 POSTING_READ(PCH_DREF_CONTROL);
7665 udelay(200);
7666
1c1a24d2
L
7667 if (!using_ssc_source) {
7668 DRM_DEBUG_KMS("Disabling SSC source\n");
199e5d79 7669
1c1a24d2
L
7670 /* Turn off the SSC source */
7671 val &= ~DREF_SSC_SOURCE_MASK;
7672 val |= DREF_SSC_SOURCE_DISABLE;
f165d283 7673
1c1a24d2
L
7674 /* Turn off SSC1 */
7675 val &= ~DREF_SSC1_ENABLE;
7676
7677 I915_WRITE(PCH_DREF_CONTROL, val);
7678 POSTING_READ(PCH_DREF_CONTROL);
7679 udelay(200);
7680 }
13d83a67 7681 }
74cfd7ac
CW
7682
7683 BUG_ON(val != final);
13d83a67
JB
7684}
7685
f31f2d55 7686static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 7687{
f31f2d55 7688 uint32_t tmp;
dde86e2d 7689
0ff066a9
PZ
7690 tmp = I915_READ(SOUTH_CHICKEN2);
7691 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
7692 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 7693
cf3598c2
ID
7694 if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
7695 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
0ff066a9 7696 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 7697
0ff066a9
PZ
7698 tmp = I915_READ(SOUTH_CHICKEN2);
7699 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
7700 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 7701
cf3598c2
ID
7702 if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
7703 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
0ff066a9 7704 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
7705}
7706
7707/* WaMPhyProgramming:hsw */
7708static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
7709{
7710 uint32_t tmp;
dde86e2d
PZ
7711
7712 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
7713 tmp &= ~(0xFF << 24);
7714 tmp |= (0x12 << 24);
7715 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
7716
dde86e2d
PZ
7717 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
7718 tmp |= (1 << 11);
7719 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
7720
7721 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
7722 tmp |= (1 << 11);
7723 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
7724
dde86e2d
PZ
7725 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
7726 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7727 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
7728
7729 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
7730 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7731 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
7732
0ff066a9
PZ
7733 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
7734 tmp &= ~(7 << 13);
7735 tmp |= (5 << 13);
7736 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 7737
0ff066a9
PZ
7738 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
7739 tmp &= ~(7 << 13);
7740 tmp |= (5 << 13);
7741 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
7742
7743 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
7744 tmp &= ~0xFF;
7745 tmp |= 0x1C;
7746 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
7747
7748 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
7749 tmp &= ~0xFF;
7750 tmp |= 0x1C;
7751 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
7752
7753 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
7754 tmp &= ~(0xFF << 16);
7755 tmp |= (0x1C << 16);
7756 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
7757
7758 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
7759 tmp &= ~(0xFF << 16);
7760 tmp |= (0x1C << 16);
7761 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
7762
0ff066a9
PZ
7763 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
7764 tmp |= (1 << 27);
7765 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 7766
0ff066a9
PZ
7767 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
7768 tmp |= (1 << 27);
7769 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 7770
0ff066a9
PZ
7771 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
7772 tmp &= ~(0xF << 28);
7773 tmp |= (4 << 28);
7774 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 7775
0ff066a9
PZ
7776 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
7777 tmp &= ~(0xF << 28);
7778 tmp |= (4 << 28);
7779 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
7780}
7781
2fa86a1f
PZ
7782/* Implements 3 different sequences from BSpec chapter "Display iCLK
7783 * Programming" based on the parameters passed:
7784 * - Sequence to enable CLKOUT_DP
7785 * - Sequence to enable CLKOUT_DP without spread
7786 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
7787 */
c39055b0
ACO
7788static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
7789 bool with_spread, bool with_fdi)
f31f2d55 7790{
2fa86a1f
PZ
7791 uint32_t reg, tmp;
7792
7793 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
7794 with_spread = true;
4f8036a2
TU
7795 if (WARN(HAS_PCH_LPT_LP(dev_priv) &&
7796 with_fdi, "LP PCH doesn't have FDI\n"))
2fa86a1f 7797 with_fdi = false;
f31f2d55 7798
a580516d 7799 mutex_lock(&dev_priv->sb_lock);
f31f2d55
PZ
7800
7801 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7802 tmp &= ~SBI_SSCCTL_DISABLE;
7803 tmp |= SBI_SSCCTL_PATHALT;
7804 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7805
7806 udelay(24);
7807
2fa86a1f
PZ
7808 if (with_spread) {
7809 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7810 tmp &= ~SBI_SSCCTL_PATHALT;
7811 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 7812
2fa86a1f
PZ
7813 if (with_fdi) {
7814 lpt_reset_fdi_mphy(dev_priv);
7815 lpt_program_fdi_mphy(dev_priv);
7816 }
7817 }
dde86e2d 7818
4f8036a2 7819 reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
2fa86a1f
PZ
7820 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
7821 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
7822 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246 7823
a580516d 7824 mutex_unlock(&dev_priv->sb_lock);
dde86e2d
PZ
7825}
7826
47701c3b 7827/* Sequence to disable CLKOUT_DP */
c39055b0 7828static void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
47701c3b 7829{
47701c3b
PZ
7830 uint32_t reg, tmp;
7831
a580516d 7832 mutex_lock(&dev_priv->sb_lock);
47701c3b 7833
4f8036a2 7834 reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
47701c3b
PZ
7835 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
7836 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
7837 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
7838
7839 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7840 if (!(tmp & SBI_SSCCTL_DISABLE)) {
7841 if (!(tmp & SBI_SSCCTL_PATHALT)) {
7842 tmp |= SBI_SSCCTL_PATHALT;
7843 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7844 udelay(32);
7845 }
7846 tmp |= SBI_SSCCTL_DISABLE;
7847 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7848 }
7849
a580516d 7850 mutex_unlock(&dev_priv->sb_lock);
47701c3b
PZ
7851}
7852
f7be2c21
VS
7853#define BEND_IDX(steps) ((50 + (steps)) / 5)
7854
7855static const uint16_t sscdivintphase[] = {
7856 [BEND_IDX( 50)] = 0x3B23,
7857 [BEND_IDX( 45)] = 0x3B23,
7858 [BEND_IDX( 40)] = 0x3C23,
7859 [BEND_IDX( 35)] = 0x3C23,
7860 [BEND_IDX( 30)] = 0x3D23,
7861 [BEND_IDX( 25)] = 0x3D23,
7862 [BEND_IDX( 20)] = 0x3E23,
7863 [BEND_IDX( 15)] = 0x3E23,
7864 [BEND_IDX( 10)] = 0x3F23,
7865 [BEND_IDX( 5)] = 0x3F23,
7866 [BEND_IDX( 0)] = 0x0025,
7867 [BEND_IDX( -5)] = 0x0025,
7868 [BEND_IDX(-10)] = 0x0125,
7869 [BEND_IDX(-15)] = 0x0125,
7870 [BEND_IDX(-20)] = 0x0225,
7871 [BEND_IDX(-25)] = 0x0225,
7872 [BEND_IDX(-30)] = 0x0325,
7873 [BEND_IDX(-35)] = 0x0325,
7874 [BEND_IDX(-40)] = 0x0425,
7875 [BEND_IDX(-45)] = 0x0425,
7876 [BEND_IDX(-50)] = 0x0525,
7877};
7878
7879/*
7880 * Bend CLKOUT_DP
7881 * steps -50 to 50 inclusive, in steps of 5
7882 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
7883 * change in clock period = -(steps / 10) * 5.787 ps
7884 */
7885static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
7886{
7887 uint32_t tmp;
7888 int idx = BEND_IDX(steps);
7889
7890 if (WARN_ON(steps % 5 != 0))
7891 return;
7892
7893 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
7894 return;
7895
7896 mutex_lock(&dev_priv->sb_lock);
7897
7898 if (steps % 10 != 0)
7899 tmp = 0xAAAAAAAB;
7900 else
7901 tmp = 0x00000000;
7902 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
7903
7904 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
7905 tmp &= 0xffff0000;
7906 tmp |= sscdivintphase[idx];
7907 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
7908
7909 mutex_unlock(&dev_priv->sb_lock);
7910}
7911
7912#undef BEND_IDX
7913
c39055b0 7914static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
bf8fa3d3 7915{
bf8fa3d3
PZ
7916 struct intel_encoder *encoder;
7917 bool has_vga = false;
7918
c39055b0 7919 for_each_intel_encoder(&dev_priv->drm, encoder) {
bf8fa3d3
PZ
7920 switch (encoder->type) {
7921 case INTEL_OUTPUT_ANALOG:
7922 has_vga = true;
7923 break;
6847d71b
PZ
7924 default:
7925 break;
bf8fa3d3
PZ
7926 }
7927 }
7928
f7be2c21 7929 if (has_vga) {
c39055b0
ACO
7930 lpt_bend_clkout_dp(dev_priv, 0);
7931 lpt_enable_clkout_dp(dev_priv, true, true);
f7be2c21 7932 } else {
c39055b0 7933 lpt_disable_clkout_dp(dev_priv);
f7be2c21 7934 }
bf8fa3d3
PZ
7935}
7936
dde86e2d
PZ
7937/*
7938 * Initialize reference clocks when the driver loads
7939 */
c39055b0 7940void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
dde86e2d 7941{
6e266956 7942 if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
c39055b0 7943 ironlake_init_pch_refclk(dev_priv);
6e266956 7944 else if (HAS_PCH_LPT(dev_priv))
c39055b0 7945 lpt_init_pch_refclk(dev_priv);
dde86e2d
PZ
7946}
7947
6ff93609 7948static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 7949{
fac5e23e 7950 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
79e53945
JB
7951 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7952 int pipe = intel_crtc->pipe;
c8203565
PZ
7953 uint32_t val;
7954
78114071 7955 val = 0;
c8203565 7956
6e3c9717 7957 switch (intel_crtc->config->pipe_bpp) {
c8203565 7958 case 18:
dfd07d72 7959 val |= PIPECONF_6BPC;
c8203565
PZ
7960 break;
7961 case 24:
dfd07d72 7962 val |= PIPECONF_8BPC;
c8203565
PZ
7963 break;
7964 case 30:
dfd07d72 7965 val |= PIPECONF_10BPC;
c8203565
PZ
7966 break;
7967 case 36:
dfd07d72 7968 val |= PIPECONF_12BPC;
c8203565
PZ
7969 break;
7970 default:
cc769b62
PZ
7971 /* Case prevented by intel_choose_pipe_bpp_dither. */
7972 BUG();
c8203565
PZ
7973 }
7974
6e3c9717 7975 if (intel_crtc->config->dither)
c8203565
PZ
7976 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
7977
6e3c9717 7978 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
7979 val |= PIPECONF_INTERLACED_ILK;
7980 else
7981 val |= PIPECONF_PROGRESSIVE;
7982
6e3c9717 7983 if (intel_crtc->config->limited_color_range)
3685a8f3 7984 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 7985
c8203565
PZ
7986 I915_WRITE(PIPECONF(pipe), val);
7987 POSTING_READ(PIPECONF(pipe));
7988}
7989
6ff93609 7990static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 7991{
fac5e23e 7992 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
ee2b0b38 7993 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 7994 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
391bf048 7995 u32 val = 0;
ee2b0b38 7996
391bf048 7997 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
ee2b0b38
PZ
7998 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
7999
6e3c9717 8000 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
8001 val |= PIPECONF_INTERLACED_ILK;
8002 else
8003 val |= PIPECONF_PROGRESSIVE;
8004
702e7a56
PZ
8005 I915_WRITE(PIPECONF(cpu_transcoder), val);
8006 POSTING_READ(PIPECONF(cpu_transcoder));
391bf048
JN
8007}
8008
391bf048
JN
8009static void haswell_set_pipemisc(struct drm_crtc *crtc)
8010{
fac5e23e 8011 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
391bf048 8012 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756f85cf 8013
391bf048
JN
8014 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
8015 u32 val = 0;
756f85cf 8016
6e3c9717 8017 switch (intel_crtc->config->pipe_bpp) {
756f85cf
PZ
8018 case 18:
8019 val |= PIPEMISC_DITHER_6_BPC;
8020 break;
8021 case 24:
8022 val |= PIPEMISC_DITHER_8_BPC;
8023 break;
8024 case 30:
8025 val |= PIPEMISC_DITHER_10_BPC;
8026 break;
8027 case 36:
8028 val |= PIPEMISC_DITHER_12_BPC;
8029 break;
8030 default:
8031 /* Case prevented by pipe_config_set_bpp. */
8032 BUG();
8033 }
8034
6e3c9717 8035 if (intel_crtc->config->dither)
756f85cf
PZ
8036 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8037
391bf048 8038 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
756f85cf 8039 }
ee2b0b38
PZ
8040}
8041
d4b1931c
PZ
8042int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8043{
8044 /*
8045 * Account for spread spectrum to avoid
8046 * oversubscribing the link. Max center spread
8047 * is 2.5%; use 5% for safety's sake.
8048 */
8049 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 8050 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
8051}
8052
7429e9d4 8053static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 8054{
7429e9d4 8055 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
8056}
8057
b75ca6f6
ACO
8058static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
8059 struct intel_crtc_state *crtc_state,
9e2c8475 8060 struct dpll *reduced_clock)
79e53945 8061{
de13a2e3 8062 struct drm_crtc *crtc = &intel_crtc->base;
79e53945 8063 struct drm_device *dev = crtc->dev;
fac5e23e 8064 struct drm_i915_private *dev_priv = to_i915(dev);
b75ca6f6 8065 u32 dpll, fp, fp2;
3d6e9ee0 8066 int factor;
79e53945 8067
c1858123 8068 /* Enable autotuning of the PLL clock (if permissible) */
8febb297 8069 factor = 21;
3d6e9ee0 8070 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8febb297 8071 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 8072 dev_priv->vbt.lvds_ssc_freq == 100000) ||
6e266956 8073 (HAS_PCH_IBX(dev_priv) && intel_is_dual_link_lvds(dev)))
8febb297 8074 factor = 25;
190f68c5 8075 } else if (crtc_state->sdvo_tv_clock)
8febb297 8076 factor = 20;
c1858123 8077
b75ca6f6
ACO
8078 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
8079
190f68c5 8080 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
b75ca6f6
ACO
8081 fp |= FP_CB_TUNE;
8082
8083 if (reduced_clock) {
8084 fp2 = i9xx_dpll_compute_fp(reduced_clock);
2c07245f 8085
b75ca6f6
ACO
8086 if (reduced_clock->m < factor * reduced_clock->n)
8087 fp2 |= FP_CB_TUNE;
8088 } else {
8089 fp2 = fp;
8090 }
9a7c7890 8091
5eddb70b 8092 dpll = 0;
2c07245f 8093
3d6e9ee0 8094 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
a07d6787
EA
8095 dpll |= DPLLB_MODE_LVDS;
8096 else
8097 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 8098
190f68c5 8099 dpll |= (crtc_state->pixel_multiplier - 1)
ef1b460d 8100 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f 8101
3d6e9ee0
VS
8102 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
8103 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
4a33e48d 8104 dpll |= DPLL_SDVO_HIGH_SPEED;
3d6e9ee0 8105
37a5650b 8106 if (intel_crtc_has_dp_encoder(crtc_state))
4a33e48d 8107 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 8108
7d7f8633
VS
8109 /*
8110 * The high speed IO clock is only really required for
8111 * SDVO/HDMI/DP, but we also enable it for CRT to make it
8112 * possible to share the DPLL between CRT and HDMI. Enabling
8113 * the clock needlessly does no real harm, except use up a
8114 * bit of power potentially.
8115 *
8116 * We'll limit this to IVB with 3 pipes, since it has only two
8117 * DPLLs and so DPLL sharing is the only way to get three pipes
8118 * driving PCH ports at the same time. On SNB we could do this,
8119 * and potentially avoid enabling the second DPLL, but it's not
8120 * clear if it''s a win or loss power wise. No point in doing
8121 * this on ILK at all since it has a fixed DPLL<->pipe mapping.
8122 */
8123 if (INTEL_INFO(dev_priv)->num_pipes == 3 &&
8124 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
8125 dpll |= DPLL_SDVO_HIGH_SPEED;
8126
a07d6787 8127 /* compute bitmask from p1 value */
190f68c5 8128 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 8129 /* also FPA1 */
190f68c5 8130 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 8131
190f68c5 8132 switch (crtc_state->dpll.p2) {
a07d6787
EA
8133 case 5:
8134 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8135 break;
8136 case 7:
8137 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8138 break;
8139 case 10:
8140 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8141 break;
8142 case 14:
8143 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8144 break;
79e53945
JB
8145 }
8146
3d6e9ee0
VS
8147 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8148 intel_panel_use_ssc(dev_priv))
43565a06 8149 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
8150 else
8151 dpll |= PLL_REF_INPUT_DREFCLK;
8152
b75ca6f6
ACO
8153 dpll |= DPLL_VCO_ENABLE;
8154
8155 crtc_state->dpll_hw_state.dpll = dpll;
8156 crtc_state->dpll_hw_state.fp0 = fp;
8157 crtc_state->dpll_hw_state.fp1 = fp2;
de13a2e3
PZ
8158}
8159
190f68c5
ACO
8160static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8161 struct intel_crtc_state *crtc_state)
de13a2e3 8162{
997c030c 8163 struct drm_device *dev = crtc->base.dev;
fac5e23e 8164 struct drm_i915_private *dev_priv = to_i915(dev);
9e2c8475 8165 struct dpll reduced_clock;
7ed9f894 8166 bool has_reduced_clock = false;
e2b78267 8167 struct intel_shared_dpll *pll;
1b6f4958 8168 const struct intel_limit *limit;
997c030c 8169 int refclk = 120000;
de13a2e3 8170
dd3cd74a
ACO
8171 memset(&crtc_state->dpll_hw_state, 0,
8172 sizeof(crtc_state->dpll_hw_state));
8173
ded220e2
ACO
8174 crtc->lowfreq_avail = false;
8175
8176 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
8177 if (!crtc_state->has_pch_encoder)
8178 return 0;
79e53945 8179
2d84d2b3 8180 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
997c030c
ACO
8181 if (intel_panel_use_ssc(dev_priv)) {
8182 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
8183 dev_priv->vbt.lvds_ssc_freq);
8184 refclk = dev_priv->vbt.lvds_ssc_freq;
8185 }
8186
8187 if (intel_is_dual_link_lvds(dev)) {
8188 if (refclk == 100000)
8189 limit = &intel_limits_ironlake_dual_lvds_100m;
8190 else
8191 limit = &intel_limits_ironlake_dual_lvds;
8192 } else {
8193 if (refclk == 100000)
8194 limit = &intel_limits_ironlake_single_lvds_100m;
8195 else
8196 limit = &intel_limits_ironlake_single_lvds;
8197 }
8198 } else {
8199 limit = &intel_limits_ironlake_dac;
8200 }
8201
364ee29d 8202 if (!crtc_state->clock_set &&
997c030c
ACO
8203 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8204 refclk, NULL, &crtc_state->dpll)) {
364ee29d
ACO
8205 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8206 return -EINVAL;
f47709a9 8207 }
79e53945 8208
b75ca6f6
ACO
8209 ironlake_compute_dpll(crtc, crtc_state,
8210 has_reduced_clock ? &reduced_clock : NULL);
66e985c0 8211
ded220e2
ACO
8212 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
8213 if (pll == NULL) {
8214 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
8215 pipe_name(crtc->pipe));
8216 return -EINVAL;
3fb37703 8217 }
79e53945 8218
2d84d2b3 8219 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ded220e2 8220 has_reduced_clock)
c7653199 8221 crtc->lowfreq_avail = true;
e2b78267 8222
c8f7a0db 8223 return 0;
79e53945
JB
8224}
8225
eb14cb74
VS
8226static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8227 struct intel_link_m_n *m_n)
8228{
8229 struct drm_device *dev = crtc->base.dev;
fac5e23e 8230 struct drm_i915_private *dev_priv = to_i915(dev);
eb14cb74
VS
8231 enum pipe pipe = crtc->pipe;
8232
8233 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8234 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8235 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8236 & ~TU_SIZE_MASK;
8237 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8238 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8239 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8240}
8241
8242static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8243 enum transcoder transcoder,
b95af8be
VK
8244 struct intel_link_m_n *m_n,
8245 struct intel_link_m_n *m2_n2)
72419203 8246{
6315b5d3 8247 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
eb14cb74 8248 enum pipe pipe = crtc->pipe;
72419203 8249
6315b5d3 8250 if (INTEL_GEN(dev_priv) >= 5) {
eb14cb74
VS
8251 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8252 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8253 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8254 & ~TU_SIZE_MASK;
8255 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8256 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8257 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
b95af8be
VK
8258 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8259 * gen < 8) and if DRRS is supported (to make sure the
8260 * registers are not unnecessarily read).
8261 */
6315b5d3 8262 if (m2_n2 && INTEL_GEN(dev_priv) < 8 &&
6e3c9717 8263 crtc->config->has_drrs) {
b95af8be
VK
8264 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8265 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8266 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8267 & ~TU_SIZE_MASK;
8268 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8269 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8270 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8271 }
eb14cb74
VS
8272 } else {
8273 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8274 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8275 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8276 & ~TU_SIZE_MASK;
8277 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8278 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8279 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8280 }
8281}
8282
8283void intel_dp_get_m_n(struct intel_crtc *crtc,
5cec258b 8284 struct intel_crtc_state *pipe_config)
eb14cb74 8285{
681a8504 8286 if (pipe_config->has_pch_encoder)
eb14cb74
VS
8287 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8288 else
8289 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be
VK
8290 &pipe_config->dp_m_n,
8291 &pipe_config->dp_m2_n2);
eb14cb74 8292}
72419203 8293
eb14cb74 8294static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5cec258b 8295 struct intel_crtc_state *pipe_config)
eb14cb74
VS
8296{
8297 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be 8298 &pipe_config->fdi_m_n, NULL);
72419203
DV
8299}
8300
bd2e244f 8301static void skylake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 8302 struct intel_crtc_state *pipe_config)
bd2e244f
JB
8303{
8304 struct drm_device *dev = crtc->base.dev;
fac5e23e 8305 struct drm_i915_private *dev_priv = to_i915(dev);
a1b2278e
CK
8306 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
8307 uint32_t ps_ctrl = 0;
8308 int id = -1;
8309 int i;
bd2e244f 8310
a1b2278e
CK
8311 /* find scaler attached to this pipe */
8312 for (i = 0; i < crtc->num_scalers; i++) {
8313 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
8314 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
8315 id = i;
8316 pipe_config->pch_pfit.enabled = true;
8317 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
8318 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
8319 break;
8320 }
8321 }
bd2e244f 8322
a1b2278e
CK
8323 scaler_state->scaler_id = id;
8324 if (id >= 0) {
8325 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
8326 } else {
8327 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
bd2e244f
JB
8328 }
8329}
8330
5724dbd1
DL
8331static void
8332skylake_get_initial_plane_config(struct intel_crtc *crtc,
8333 struct intel_initial_plane_config *plane_config)
bc8d7dff
DL
8334{
8335 struct drm_device *dev = crtc->base.dev;
fac5e23e 8336 struct drm_i915_private *dev_priv = to_i915(dev);
40f46283 8337 u32 val, base, offset, stride_mult, tiling;
bc8d7dff
DL
8338 int pipe = crtc->pipe;
8339 int fourcc, pixel_format;
6761dd31 8340 unsigned int aligned_height;
bc8d7dff 8341 struct drm_framebuffer *fb;
1b842c89 8342 struct intel_framebuffer *intel_fb;
bc8d7dff 8343
d9806c9f 8344 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 8345 if (!intel_fb) {
bc8d7dff
DL
8346 DRM_DEBUG_KMS("failed to alloc fb\n");
8347 return;
8348 }
8349
1b842c89
DL
8350 fb = &intel_fb->base;
8351
d2e9f5fc
VS
8352 fb->dev = dev;
8353
bc8d7dff 8354 val = I915_READ(PLANE_CTL(pipe, 0));
42a7b088
DL
8355 if (!(val & PLANE_CTL_ENABLE))
8356 goto error;
8357
bc8d7dff
DL
8358 pixel_format = val & PLANE_CTL_FORMAT_MASK;
8359 fourcc = skl_format_to_fourcc(pixel_format,
8360 val & PLANE_CTL_ORDER_RGBX,
8361 val & PLANE_CTL_ALPHA_MASK);
2f3f4763 8362 fb->format = drm_format_info(fourcc);
bc8d7dff 8363
40f46283
DL
8364 tiling = val & PLANE_CTL_TILED_MASK;
8365 switch (tiling) {
8366 case PLANE_CTL_TILED_LINEAR:
bae781b2 8367 fb->modifier = DRM_FORMAT_MOD_NONE;
40f46283
DL
8368 break;
8369 case PLANE_CTL_TILED_X:
8370 plane_config->tiling = I915_TILING_X;
bae781b2 8371 fb->modifier = I915_FORMAT_MOD_X_TILED;
40f46283
DL
8372 break;
8373 case PLANE_CTL_TILED_Y:
bae781b2 8374 fb->modifier = I915_FORMAT_MOD_Y_TILED;
40f46283
DL
8375 break;
8376 case PLANE_CTL_TILED_YF:
bae781b2 8377 fb->modifier = I915_FORMAT_MOD_Yf_TILED;
40f46283
DL
8378 break;
8379 default:
8380 MISSING_CASE(tiling);
8381 goto error;
8382 }
8383
bc8d7dff
DL
8384 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
8385 plane_config->base = base;
8386
8387 offset = I915_READ(PLANE_OFFSET(pipe, 0));
8388
8389 val = I915_READ(PLANE_SIZE(pipe, 0));
8390 fb->height = ((val >> 16) & 0xfff) + 1;
8391 fb->width = ((val >> 0) & 0x1fff) + 1;
8392
8393 val = I915_READ(PLANE_STRIDE(pipe, 0));
bae781b2 8394 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier,
438b74a5 8395 fb->format->format);
bc8d7dff
DL
8396 fb->pitches[0] = (val & 0x3ff) * stride_mult;
8397
24dbf51a
CW
8398 aligned_height = intel_fb_align_height(dev_priv,
8399 fb->height,
438b74a5 8400 fb->format->format,
bae781b2 8401 fb->modifier);
bc8d7dff 8402
f37b5c2b 8403 plane_config->size = fb->pitches[0] * aligned_height;
bc8d7dff
DL
8404
8405 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8406 pipe_name(pipe), fb->width, fb->height,
272725c7 8407 fb->format->cpp[0] * 8, base, fb->pitches[0],
bc8d7dff
DL
8408 plane_config->size);
8409
2d14030b 8410 plane_config->fb = intel_fb;
bc8d7dff
DL
8411 return;
8412
8413error:
d1a3a036 8414 kfree(intel_fb);
bc8d7dff
DL
8415}
8416
2fa2fe9a 8417static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 8418 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
8419{
8420 struct drm_device *dev = crtc->base.dev;
fac5e23e 8421 struct drm_i915_private *dev_priv = to_i915(dev);
2fa2fe9a
DV
8422 uint32_t tmp;
8423
8424 tmp = I915_READ(PF_CTL(crtc->pipe));
8425
8426 if (tmp & PF_ENABLE) {
fd4daa9c 8427 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
DV
8428 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
8429 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
DV
8430
8431 /* We currently do not free assignements of panel fitters on
8432 * ivb/hsw (since we don't use the higher upscaling modes which
8433 * differentiates them) so just WARN about this case for now. */
5db94019 8434 if (IS_GEN7(dev_priv)) {
cb8b2a30
DV
8435 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
8436 PF_PIPE_SEL_IVB(crtc->pipe));
8437 }
2fa2fe9a 8438 }
79e53945
JB
8439}
8440
5724dbd1
DL
8441static void
8442ironlake_get_initial_plane_config(struct intel_crtc *crtc,
8443 struct intel_initial_plane_config *plane_config)
4c6baa59
JB
8444{
8445 struct drm_device *dev = crtc->base.dev;
fac5e23e 8446 struct drm_i915_private *dev_priv = to_i915(dev);
4c6baa59 8447 u32 val, base, offset;
aeee5a49 8448 int pipe = crtc->pipe;
4c6baa59 8449 int fourcc, pixel_format;
6761dd31 8450 unsigned int aligned_height;
b113d5ee 8451 struct drm_framebuffer *fb;
1b842c89 8452 struct intel_framebuffer *intel_fb;
4c6baa59 8453
42a7b088
DL
8454 val = I915_READ(DSPCNTR(pipe));
8455 if (!(val & DISPLAY_PLANE_ENABLE))
8456 return;
8457
d9806c9f 8458 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 8459 if (!intel_fb) {
4c6baa59
JB
8460 DRM_DEBUG_KMS("failed to alloc fb\n");
8461 return;
8462 }
8463
1b842c89
DL
8464 fb = &intel_fb->base;
8465
d2e9f5fc
VS
8466 fb->dev = dev;
8467
6315b5d3 8468 if (INTEL_GEN(dev_priv) >= 4) {
18c5247e 8469 if (val & DISPPLANE_TILED) {
49af449b 8470 plane_config->tiling = I915_TILING_X;
bae781b2 8471 fb->modifier = I915_FORMAT_MOD_X_TILED;
18c5247e
DV
8472 }
8473 }
4c6baa59
JB
8474
8475 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 8476 fourcc = i9xx_format_to_fourcc(pixel_format);
2f3f4763 8477 fb->format = drm_format_info(fourcc);
4c6baa59 8478
aeee5a49 8479 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
8652744b 8480 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
aeee5a49 8481 offset = I915_READ(DSPOFFSET(pipe));
4c6baa59 8482 } else {
49af449b 8483 if (plane_config->tiling)
aeee5a49 8484 offset = I915_READ(DSPTILEOFF(pipe));
4c6baa59 8485 else
aeee5a49 8486 offset = I915_READ(DSPLINOFF(pipe));
4c6baa59
JB
8487 }
8488 plane_config->base = base;
8489
8490 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
8491 fb->width = ((val >> 16) & 0xfff) + 1;
8492 fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
8493
8494 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 8495 fb->pitches[0] = val & 0xffffffc0;
4c6baa59 8496
24dbf51a
CW
8497 aligned_height = intel_fb_align_height(dev_priv,
8498 fb->height,
438b74a5 8499 fb->format->format,
bae781b2 8500 fb->modifier);
4c6baa59 8501
f37b5c2b 8502 plane_config->size = fb->pitches[0] * aligned_height;
4c6baa59 8503
2844a921
DL
8504 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8505 pipe_name(pipe), fb->width, fb->height,
272725c7 8506 fb->format->cpp[0] * 8, base, fb->pitches[0],
2844a921 8507 plane_config->size);
b113d5ee 8508
2d14030b 8509 plane_config->fb = intel_fb;
4c6baa59
JB
8510}
8511
0e8ffe1b 8512static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5cec258b 8513 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
8514{
8515 struct drm_device *dev = crtc->base.dev;
fac5e23e 8516 struct drm_i915_private *dev_priv = to_i915(dev);
1729050e 8517 enum intel_display_power_domain power_domain;
0e8ffe1b 8518 uint32_t tmp;
1729050e 8519 bool ret;
0e8ffe1b 8520
1729050e
ID
8521 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8522 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
930e8c9e
PZ
8523 return false;
8524
e143a21c 8525 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 8526 pipe_config->shared_dpll = NULL;
eccb140b 8527
1729050e 8528 ret = false;
0e8ffe1b
DV
8529 tmp = I915_READ(PIPECONF(crtc->pipe));
8530 if (!(tmp & PIPECONF_ENABLE))
1729050e 8531 goto out;
0e8ffe1b 8532
42571aef
VS
8533 switch (tmp & PIPECONF_BPC_MASK) {
8534 case PIPECONF_6BPC:
8535 pipe_config->pipe_bpp = 18;
8536 break;
8537 case PIPECONF_8BPC:
8538 pipe_config->pipe_bpp = 24;
8539 break;
8540 case PIPECONF_10BPC:
8541 pipe_config->pipe_bpp = 30;
8542 break;
8543 case PIPECONF_12BPC:
8544 pipe_config->pipe_bpp = 36;
8545 break;
8546 default:
8547 break;
8548 }
8549
b5a9fa09
DV
8550 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
8551 pipe_config->limited_color_range = true;
8552
ab9412ba 8553 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0 8554 struct intel_shared_dpll *pll;
8106ddbd 8555 enum intel_dpll_id pll_id;
66e985c0 8556
88adfff1
DV
8557 pipe_config->has_pch_encoder = true;
8558
627eb5a3
DV
8559 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
8560 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
8561 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
8562
8563 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 8564
2d1fe073 8565 if (HAS_PCH_IBX(dev_priv)) {
d9a7bc67
ID
8566 /*
8567 * The pipe->pch transcoder and pch transcoder->pll
8568 * mapping is fixed.
8569 */
8106ddbd 8570 pll_id = (enum intel_dpll_id) crtc->pipe;
c0d43d62
DV
8571 } else {
8572 tmp = I915_READ(PCH_DPLL_SEL);
8573 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
8106ddbd 8574 pll_id = DPLL_ID_PCH_PLL_B;
c0d43d62 8575 else
8106ddbd 8576 pll_id= DPLL_ID_PCH_PLL_A;
c0d43d62 8577 }
66e985c0 8578
8106ddbd
ACO
8579 pipe_config->shared_dpll =
8580 intel_get_shared_dpll_by_id(dev_priv, pll_id);
8581 pll = pipe_config->shared_dpll;
66e985c0 8582
2edd6443
ACO
8583 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
8584 &pipe_config->dpll_hw_state));
c93f54cf
DV
8585
8586 tmp = pipe_config->dpll_hw_state.dpll;
8587 pipe_config->pixel_multiplier =
8588 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
8589 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
8590
8591 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
DV
8592 } else {
8593 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
8594 }
8595
1bd1bd80 8596 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 8597 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 8598
2fa2fe9a
DV
8599 ironlake_get_pfit_config(crtc, pipe_config);
8600
1729050e
ID
8601 ret = true;
8602
8603out:
8604 intel_display_power_put(dev_priv, power_domain);
8605
8606 return ret;
0e8ffe1b
DV
8607}
8608
be256dc7
PZ
8609static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
8610{
91c8a326 8611 struct drm_device *dev = &dev_priv->drm;
be256dc7 8612 struct intel_crtc *crtc;
be256dc7 8613
d3fcc808 8614 for_each_intel_crtc(dev, crtc)
e2c719b7 8615 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
8616 pipe_name(crtc->pipe));
8617
e2c719b7
RC
8618 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
8619 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
01403de3
VS
8620 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
8621 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
44cb734c 8622 I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
e2c719b7 8623 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
be256dc7 8624 "CPU PWM1 enabled\n");
772c2a51 8625 if (IS_HASWELL(dev_priv))
e2c719b7 8626 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
c5107b87 8627 "CPU PWM2 enabled\n");
e2c719b7 8628 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
be256dc7 8629 "PCH PWM1 enabled\n");
e2c719b7 8630 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
be256dc7 8631 "Utility pin enabled\n");
e2c719b7 8632 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
be256dc7 8633
9926ada1
PZ
8634 /*
8635 * In theory we can still leave IRQs enabled, as long as only the HPD
8636 * interrupts remain enabled. We used to check for that, but since it's
8637 * gen-specific and since we only disable LCPLL after we fully disable
8638 * the interrupts, the check below should be enough.
8639 */
e2c719b7 8640 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
be256dc7
PZ
8641}
8642
9ccd5aeb
PZ
8643static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
8644{
772c2a51 8645 if (IS_HASWELL(dev_priv))
9ccd5aeb
PZ
8646 return I915_READ(D_COMP_HSW);
8647 else
8648 return I915_READ(D_COMP_BDW);
8649}
8650
3c4c9b81
PZ
8651static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
8652{
772c2a51 8653 if (IS_HASWELL(dev_priv)) {
3c4c9b81
PZ
8654 mutex_lock(&dev_priv->rps.hw_lock);
8655 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
8656 val))
79cf219a 8657 DRM_DEBUG_KMS("Failed to write to D_COMP\n");
3c4c9b81
PZ
8658 mutex_unlock(&dev_priv->rps.hw_lock);
8659 } else {
9ccd5aeb
PZ
8660 I915_WRITE(D_COMP_BDW, val);
8661 POSTING_READ(D_COMP_BDW);
3c4c9b81 8662 }
be256dc7
PZ
8663}
8664
8665/*
8666 * This function implements pieces of two sequences from BSpec:
8667 * - Sequence for display software to disable LCPLL
8668 * - Sequence for display software to allow package C8+
8669 * The steps implemented here are just the steps that actually touch the LCPLL
8670 * register. Callers should take care of disabling all the display engine
8671 * functions, doing the mode unset, fixing interrupts, etc.
8672 */
6ff58d53
PZ
8673static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
8674 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
8675{
8676 uint32_t val;
8677
8678 assert_can_disable_lcpll(dev_priv);
8679
8680 val = I915_READ(LCPLL_CTL);
8681
8682 if (switch_to_fclk) {
8683 val |= LCPLL_CD_SOURCE_FCLK;
8684 I915_WRITE(LCPLL_CTL, val);
8685
f53dd63f
ID
8686 if (wait_for_us(I915_READ(LCPLL_CTL) &
8687 LCPLL_CD_SOURCE_FCLK_DONE, 1))
be256dc7
PZ
8688 DRM_ERROR("Switching to FCLK failed\n");
8689
8690 val = I915_READ(LCPLL_CTL);
8691 }
8692
8693 val |= LCPLL_PLL_DISABLE;
8694 I915_WRITE(LCPLL_CTL, val);
8695 POSTING_READ(LCPLL_CTL);
8696
24d8441d 8697 if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
be256dc7
PZ
8698 DRM_ERROR("LCPLL still locked\n");
8699
9ccd5aeb 8700 val = hsw_read_dcomp(dev_priv);
be256dc7 8701 val |= D_COMP_COMP_DISABLE;
3c4c9b81 8702 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
8703 ndelay(100);
8704
9ccd5aeb
PZ
8705 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
8706 1))
be256dc7
PZ
8707 DRM_ERROR("D_COMP RCOMP still in progress\n");
8708
8709 if (allow_power_down) {
8710 val = I915_READ(LCPLL_CTL);
8711 val |= LCPLL_POWER_DOWN_ALLOW;
8712 I915_WRITE(LCPLL_CTL, val);
8713 POSTING_READ(LCPLL_CTL);
8714 }
8715}
8716
8717/*
8718 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
8719 * source.
8720 */
6ff58d53 8721static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
8722{
8723 uint32_t val;
8724
8725 val = I915_READ(LCPLL_CTL);
8726
8727 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
8728 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
8729 return;
8730
a8a8bd54
PZ
8731 /*
8732 * Make sure we're not on PC8 state before disabling PC8, otherwise
8733 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
a8a8bd54 8734 */
59bad947 8735 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
215733fa 8736
be256dc7
PZ
8737 if (val & LCPLL_POWER_DOWN_ALLOW) {
8738 val &= ~LCPLL_POWER_DOWN_ALLOW;
8739 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 8740 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
8741 }
8742
9ccd5aeb 8743 val = hsw_read_dcomp(dev_priv);
be256dc7
PZ
8744 val |= D_COMP_COMP_FORCE;
8745 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 8746 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
8747
8748 val = I915_READ(LCPLL_CTL);
8749 val &= ~LCPLL_PLL_DISABLE;
8750 I915_WRITE(LCPLL_CTL, val);
8751
93220c08
CW
8752 if (intel_wait_for_register(dev_priv,
8753 LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
8754 5))
be256dc7
PZ
8755 DRM_ERROR("LCPLL not locked yet\n");
8756
8757 if (val & LCPLL_CD_SOURCE_FCLK) {
8758 val = I915_READ(LCPLL_CTL);
8759 val &= ~LCPLL_CD_SOURCE_FCLK;
8760 I915_WRITE(LCPLL_CTL, val);
8761
f53dd63f
ID
8762 if (wait_for_us((I915_READ(LCPLL_CTL) &
8763 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
be256dc7
PZ
8764 DRM_ERROR("Switching back to LCPLL failed\n");
8765 }
215733fa 8766
59bad947 8767 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4c75b940 8768 intel_update_cdclk(dev_priv);
be256dc7
PZ
8769}
8770
765dab67
PZ
8771/*
8772 * Package states C8 and deeper are really deep PC states that can only be
8773 * reached when all the devices on the system allow it, so even if the graphics
8774 * device allows PC8+, it doesn't mean the system will actually get to these
8775 * states. Our driver only allows PC8+ when going into runtime PM.
8776 *
8777 * The requirements for PC8+ are that all the outputs are disabled, the power
8778 * well is disabled and most interrupts are disabled, and these are also
8779 * requirements for runtime PM. When these conditions are met, we manually do
8780 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
8781 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
8782 * hang the machine.
8783 *
8784 * When we really reach PC8 or deeper states (not just when we allow it) we lose
8785 * the state of some registers, so when we come back from PC8+ we need to
8786 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
8787 * need to take care of the registers kept by RC6. Notice that this happens even
8788 * if we don't put the device in PCI D3 state (which is what currently happens
8789 * because of the runtime PM support).
8790 *
8791 * For more, read "Display Sequences for Package C8" on the hardware
8792 * documentation.
8793 */
a14cb6fc 8794void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 8795{
c67a470b
PZ
8796 uint32_t val;
8797
c67a470b
PZ
8798 DRM_DEBUG_KMS("Enabling package C8+\n");
8799
4f8036a2 8800 if (HAS_PCH_LPT_LP(dev_priv)) {
c67a470b
PZ
8801 val = I915_READ(SOUTH_DSPCLK_GATE_D);
8802 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
8803 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
8804 }
8805
c39055b0 8806 lpt_disable_clkout_dp(dev_priv);
c67a470b
PZ
8807 hsw_disable_lcpll(dev_priv, true, true);
8808}
8809
a14cb6fc 8810void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b 8811{
c67a470b
PZ
8812 uint32_t val;
8813
c67a470b
PZ
8814 DRM_DEBUG_KMS("Disabling package C8+\n");
8815
8816 hsw_restore_lcpll(dev_priv);
c39055b0 8817 lpt_init_pch_refclk(dev_priv);
c67a470b 8818
4f8036a2 8819 if (HAS_PCH_LPT_LP(dev_priv)) {
c67a470b
PZ
8820 val = I915_READ(SOUTH_DSPCLK_GATE_D);
8821 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
8822 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
8823 }
c67a470b
PZ
8824}
8825
190f68c5
ACO
8826static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
8827 struct intel_crtc_state *crtc_state)
09b4ddf9 8828{
d7edc4e5 8829 if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
af3997b5
MK
8830 if (!intel_ddi_pll_select(crtc, crtc_state))
8831 return -EINVAL;
8832 }
716c2e55 8833
c7653199 8834 crtc->lowfreq_avail = false;
644cef34 8835
c8f7a0db 8836 return 0;
79e53945
JB
8837}
8838
3760b59c
S
8839static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
8840 enum port port,
8841 struct intel_crtc_state *pipe_config)
8842{
8106ddbd
ACO
8843 enum intel_dpll_id id;
8844
3760b59c
S
8845 switch (port) {
8846 case PORT_A:
08250c4b 8847 id = DPLL_ID_SKL_DPLL0;
3760b59c
S
8848 break;
8849 case PORT_B:
08250c4b 8850 id = DPLL_ID_SKL_DPLL1;
3760b59c
S
8851 break;
8852 case PORT_C:
08250c4b 8853 id = DPLL_ID_SKL_DPLL2;
3760b59c
S
8854 break;
8855 default:
8856 DRM_ERROR("Incorrect port type\n");
8106ddbd 8857 return;
3760b59c 8858 }
8106ddbd
ACO
8859
8860 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
3760b59c
S
8861}
8862
96b7dfb7
S
8863static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
8864 enum port port,
5cec258b 8865 struct intel_crtc_state *pipe_config)
96b7dfb7 8866{
8106ddbd 8867 enum intel_dpll_id id;
a3c988ea 8868 u32 temp;
96b7dfb7
S
8869
8870 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
c856052a 8871 id = temp >> (port * 3 + 1);
96b7dfb7 8872
c856052a 8873 if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL3))
8106ddbd 8874 return;
8106ddbd
ACO
8875
8876 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
96b7dfb7
S
8877}
8878
7d2c8175
DL
8879static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
8880 enum port port,
5cec258b 8881 struct intel_crtc_state *pipe_config)
7d2c8175 8882{
8106ddbd 8883 enum intel_dpll_id id;
c856052a 8884 uint32_t ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
8106ddbd 8885
c856052a 8886 switch (ddi_pll_sel) {
7d2c8175 8887 case PORT_CLK_SEL_WRPLL1:
8106ddbd 8888 id = DPLL_ID_WRPLL1;
7d2c8175
DL
8889 break;
8890 case PORT_CLK_SEL_WRPLL2:
8106ddbd 8891 id = DPLL_ID_WRPLL2;
7d2c8175 8892 break;
00490c22 8893 case PORT_CLK_SEL_SPLL:
8106ddbd 8894 id = DPLL_ID_SPLL;
79bd23da 8895 break;
9d16da65
ACO
8896 case PORT_CLK_SEL_LCPLL_810:
8897 id = DPLL_ID_LCPLL_810;
8898 break;
8899 case PORT_CLK_SEL_LCPLL_1350:
8900 id = DPLL_ID_LCPLL_1350;
8901 break;
8902 case PORT_CLK_SEL_LCPLL_2700:
8903 id = DPLL_ID_LCPLL_2700;
8904 break;
8106ddbd 8905 default:
c856052a 8906 MISSING_CASE(ddi_pll_sel);
8106ddbd
ACO
8907 /* fall through */
8908 case PORT_CLK_SEL_NONE:
8106ddbd 8909 return;
7d2c8175 8910 }
8106ddbd
ACO
8911
8912 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
7d2c8175
DL
8913}
8914
cf30429e
JN
8915static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
8916 struct intel_crtc_state *pipe_config,
d8fc70b7 8917 u64 *power_domain_mask)
cf30429e
JN
8918{
8919 struct drm_device *dev = crtc->base.dev;
fac5e23e 8920 struct drm_i915_private *dev_priv = to_i915(dev);
cf30429e
JN
8921 enum intel_display_power_domain power_domain;
8922 u32 tmp;
8923
d9a7bc67
ID
8924 /*
8925 * The pipe->transcoder mapping is fixed with the exception of the eDP
8926 * transcoder handled below.
8927 */
cf30429e
JN
8928 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8929
8930 /*
8931 * XXX: Do intel_display_power_get_if_enabled before reading this (for
8932 * consistency and less surprising code; it's in always on power).
8933 */
8934 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
8935 if (tmp & TRANS_DDI_FUNC_ENABLE) {
8936 enum pipe trans_edp_pipe;
8937 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
8938 default:
8939 WARN(1, "unknown pipe linked to edp transcoder\n");
8940 case TRANS_DDI_EDP_INPUT_A_ONOFF:
8941 case TRANS_DDI_EDP_INPUT_A_ON:
8942 trans_edp_pipe = PIPE_A;
8943 break;
8944 case TRANS_DDI_EDP_INPUT_B_ONOFF:
8945 trans_edp_pipe = PIPE_B;
8946 break;
8947 case TRANS_DDI_EDP_INPUT_C_ONOFF:
8948 trans_edp_pipe = PIPE_C;
8949 break;
8950 }
8951
8952 if (trans_edp_pipe == crtc->pipe)
8953 pipe_config->cpu_transcoder = TRANSCODER_EDP;
8954 }
8955
8956 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
8957 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
8958 return false;
d8fc70b7 8959 *power_domain_mask |= BIT_ULL(power_domain);
cf30429e
JN
8960
8961 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
8962
8963 return tmp & PIPECONF_ENABLE;
8964}
8965
4d1de975
JN
8966static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
8967 struct intel_crtc_state *pipe_config,
d8fc70b7 8968 u64 *power_domain_mask)
4d1de975
JN
8969{
8970 struct drm_device *dev = crtc->base.dev;
fac5e23e 8971 struct drm_i915_private *dev_priv = to_i915(dev);
4d1de975
JN
8972 enum intel_display_power_domain power_domain;
8973 enum port port;
8974 enum transcoder cpu_transcoder;
8975 u32 tmp;
8976
4d1de975
JN
8977 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
8978 if (port == PORT_A)
8979 cpu_transcoder = TRANSCODER_DSI_A;
8980 else
8981 cpu_transcoder = TRANSCODER_DSI_C;
8982
8983 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
8984 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
8985 continue;
d8fc70b7 8986 *power_domain_mask |= BIT_ULL(power_domain);
4d1de975 8987
db18b6a6
ID
8988 /*
8989 * The PLL needs to be enabled with a valid divider
8990 * configuration, otherwise accessing DSI registers will hang
8991 * the machine. See BSpec North Display Engine
8992 * registers/MIPI[BXT]. We can break out here early, since we
8993 * need the same DSI PLL to be enabled for both DSI ports.
8994 */
8995 if (!intel_dsi_pll_is_enabled(dev_priv))
8996 break;
8997
4d1de975
JN
8998 /* XXX: this works for video mode only */
8999 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
9000 if (!(tmp & DPI_ENABLE))
9001 continue;
9002
9003 tmp = I915_READ(MIPI_CTRL(port));
9004 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
9005 continue;
9006
9007 pipe_config->cpu_transcoder = cpu_transcoder;
4d1de975
JN
9008 break;
9009 }
9010
d7edc4e5 9011 return transcoder_is_dsi(pipe_config->cpu_transcoder);
4d1de975
JN
9012}
9013
26804afd 9014static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
5cec258b 9015 struct intel_crtc_state *pipe_config)
26804afd 9016{
6315b5d3 9017 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
d452c5b6 9018 struct intel_shared_dpll *pll;
26804afd
DV
9019 enum port port;
9020 uint32_t tmp;
9021
9022 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9023
9024 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9025
b976dc53 9026 if (IS_GEN9_BC(dev_priv))
96b7dfb7 9027 skylake_get_ddi_pll(dev_priv, port, pipe_config);
cc3f90f0 9028 else if (IS_GEN9_LP(dev_priv))
3760b59c 9029 bxt_get_ddi_pll(dev_priv, port, pipe_config);
96b7dfb7
S
9030 else
9031 haswell_get_ddi_pll(dev_priv, port, pipe_config);
9cd86933 9032
8106ddbd
ACO
9033 pll = pipe_config->shared_dpll;
9034 if (pll) {
2edd6443
ACO
9035 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9036 &pipe_config->dpll_hw_state));
d452c5b6
DV
9037 }
9038
26804afd
DV
9039 /*
9040 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9041 * DDI E. So just check whether this pipe is wired to DDI E and whether
9042 * the PCH transcoder is on.
9043 */
6315b5d3 9044 if (INTEL_GEN(dev_priv) < 9 &&
ca370455 9045 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
26804afd
DV
9046 pipe_config->has_pch_encoder = true;
9047
9048 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9049 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9050 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9051
9052 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9053 }
9054}
9055
0e8ffe1b 9056static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9057 struct intel_crtc_state *pipe_config)
0e8ffe1b 9058{
6315b5d3 9059 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1729050e 9060 enum intel_display_power_domain power_domain;
d8fc70b7 9061 u64 power_domain_mask;
cf30429e 9062 bool active;
0e8ffe1b 9063
1729050e
ID
9064 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9065 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0 9066 return false;
d8fc70b7 9067 power_domain_mask = BIT_ULL(power_domain);
1729050e 9068
8106ddbd 9069 pipe_config->shared_dpll = NULL;
c0d43d62 9070
cf30429e 9071 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
eccb140b 9072
cc3f90f0 9073 if (IS_GEN9_LP(dev_priv) &&
d7edc4e5
VS
9074 bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
9075 WARN_ON(active);
9076 active = true;
4d1de975
JN
9077 }
9078
cf30429e 9079 if (!active)
1729050e 9080 goto out;
0e8ffe1b 9081
d7edc4e5 9082 if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
4d1de975
JN
9083 haswell_get_ddi_port_state(crtc, pipe_config);
9084 intel_get_pipe_timings(crtc, pipe_config);
9085 }
627eb5a3 9086
bc58be60 9087 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 9088
05dc698c
LL
9089 pipe_config->gamma_mode =
9090 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
9091
6315b5d3 9092 if (INTEL_GEN(dev_priv) >= 9) {
1c74eeaf 9093 intel_crtc_init_scalers(crtc, pipe_config);
a1b2278e 9094
af99ceda
CK
9095 pipe_config->scaler_state.scaler_id = -1;
9096 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9097 }
9098
1729050e
ID
9099 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
9100 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
d8fc70b7 9101 power_domain_mask |= BIT_ULL(power_domain);
6315b5d3 9102 if (INTEL_GEN(dev_priv) >= 9)
bd2e244f 9103 skylake_get_pfit_config(crtc, pipe_config);
ff6d9f55 9104 else
1c132b44 9105 ironlake_get_pfit_config(crtc, pipe_config);
bd2e244f 9106 }
88adfff1 9107
772c2a51 9108 if (IS_HASWELL(dev_priv))
e59150dc
JB
9109 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9110 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 9111
4d1de975
JN
9112 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
9113 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
ebb69c95
CT
9114 pipe_config->pixel_multiplier =
9115 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9116 } else {
9117 pipe_config->pixel_multiplier = 1;
9118 }
6c49f241 9119
1729050e
ID
9120out:
9121 for_each_power_domain(power_domain, power_domain_mask)
9122 intel_display_power_put(dev_priv, power_domain);
9123
cf30429e 9124 return active;
0e8ffe1b
DV
9125}
9126
55a08b3f
ML
9127static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
9128 const struct intel_plane_state *plane_state)
560b85bb
CW
9129{
9130 struct drm_device *dev = crtc->dev;
fac5e23e 9131 struct drm_i915_private *dev_priv = to_i915(dev);
560b85bb 9132 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
dc41c154 9133 uint32_t cntl = 0, size = 0;
560b85bb 9134
936e71e3 9135 if (plane_state && plane_state->base.visible) {
55a08b3f
ML
9136 unsigned int width = plane_state->base.crtc_w;
9137 unsigned int height = plane_state->base.crtc_h;
dc41c154
VS
9138 unsigned int stride = roundup_pow_of_two(width) * 4;
9139
9140 switch (stride) {
9141 default:
9142 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
9143 width, stride);
9144 stride = 256;
9145 /* fallthrough */
9146 case 256:
9147 case 512:
9148 case 1024:
9149 case 2048:
9150 break;
4b0e333e
CW
9151 }
9152
dc41c154
VS
9153 cntl |= CURSOR_ENABLE |
9154 CURSOR_GAMMA_ENABLE |
9155 CURSOR_FORMAT_ARGB |
9156 CURSOR_STRIDE(stride);
9157
9158 size = (height << 12) | width;
4b0e333e 9159 }
560b85bb 9160
dc41c154
VS
9161 if (intel_crtc->cursor_cntl != 0 &&
9162 (intel_crtc->cursor_base != base ||
9163 intel_crtc->cursor_size != size ||
9164 intel_crtc->cursor_cntl != cntl)) {
9165 /* On these chipsets we can only modify the base/size/stride
9166 * whilst the cursor is disabled.
9167 */
0b87c24e
VS
9168 I915_WRITE(CURCNTR(PIPE_A), 0);
9169 POSTING_READ(CURCNTR(PIPE_A));
dc41c154 9170 intel_crtc->cursor_cntl = 0;
4b0e333e 9171 }
560b85bb 9172
99d1f387 9173 if (intel_crtc->cursor_base != base) {
0b87c24e 9174 I915_WRITE(CURBASE(PIPE_A), base);
99d1f387
VS
9175 intel_crtc->cursor_base = base;
9176 }
4726e0b0 9177
dc41c154
VS
9178 if (intel_crtc->cursor_size != size) {
9179 I915_WRITE(CURSIZE, size);
9180 intel_crtc->cursor_size = size;
4b0e333e 9181 }
560b85bb 9182
4b0e333e 9183 if (intel_crtc->cursor_cntl != cntl) {
0b87c24e
VS
9184 I915_WRITE(CURCNTR(PIPE_A), cntl);
9185 POSTING_READ(CURCNTR(PIPE_A));
4b0e333e 9186 intel_crtc->cursor_cntl = cntl;
560b85bb 9187 }
560b85bb
CW
9188}
9189
55a08b3f
ML
9190static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
9191 const struct intel_plane_state *plane_state)
65a21cd6
JB
9192{
9193 struct drm_device *dev = crtc->dev;
fac5e23e 9194 struct drm_i915_private *dev_priv = to_i915(dev);
65a21cd6
JB
9195 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9196 int pipe = intel_crtc->pipe;
663f3122 9197 uint32_t cntl = 0;
4b0e333e 9198
936e71e3 9199 if (plane_state && plane_state->base.visible) {
4b0e333e 9200 cntl = MCURSOR_GAMMA_ENABLE;
55a08b3f 9201 switch (plane_state->base.crtc_w) {
4726e0b0
SK
9202 case 64:
9203 cntl |= CURSOR_MODE_64_ARGB_AX;
9204 break;
9205 case 128:
9206 cntl |= CURSOR_MODE_128_ARGB_AX;
9207 break;
9208 case 256:
9209 cntl |= CURSOR_MODE_256_ARGB_AX;
9210 break;
9211 default:
55a08b3f 9212 MISSING_CASE(plane_state->base.crtc_w);
4726e0b0 9213 return;
65a21cd6 9214 }
4b0e333e 9215 cntl |= pipe << 28; /* Connect to correct pipe */
47bf17a7 9216
4f8036a2 9217 if (HAS_DDI(dev_priv))
47bf17a7 9218 cntl |= CURSOR_PIPE_CSC_ENABLE;
65a21cd6 9219
f22aa143 9220 if (plane_state->base.rotation & DRM_ROTATE_180)
55a08b3f
ML
9221 cntl |= CURSOR_ROTATE_180;
9222 }
4398ad45 9223
4b0e333e
CW
9224 if (intel_crtc->cursor_cntl != cntl) {
9225 I915_WRITE(CURCNTR(pipe), cntl);
9226 POSTING_READ(CURCNTR(pipe));
9227 intel_crtc->cursor_cntl = cntl;
65a21cd6 9228 }
4b0e333e 9229
65a21cd6 9230 /* and commit changes on next vblank */
5efb3e28
VS
9231 I915_WRITE(CURBASE(pipe), base);
9232 POSTING_READ(CURBASE(pipe));
99d1f387
VS
9233
9234 intel_crtc->cursor_base = base;
65a21cd6
JB
9235}
9236
cda4b7d3 9237/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6b383a7f 9238static void intel_crtc_update_cursor(struct drm_crtc *crtc,
55a08b3f 9239 const struct intel_plane_state *plane_state)
cda4b7d3
CW
9240{
9241 struct drm_device *dev = crtc->dev;
fac5e23e 9242 struct drm_i915_private *dev_priv = to_i915(dev);
cda4b7d3
CW
9243 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9244 int pipe = intel_crtc->pipe;
55a08b3f
ML
9245 u32 base = intel_crtc->cursor_addr;
9246 u32 pos = 0;
cda4b7d3 9247
55a08b3f
ML
9248 if (plane_state) {
9249 int x = plane_state->base.crtc_x;
9250 int y = plane_state->base.crtc_y;
cda4b7d3 9251
55a08b3f
ML
9252 if (x < 0) {
9253 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
9254 x = -x;
9255 }
9256 pos |= x << CURSOR_X_SHIFT;
cda4b7d3 9257
55a08b3f
ML
9258 if (y < 0) {
9259 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
9260 y = -y;
9261 }
9262 pos |= y << CURSOR_Y_SHIFT;
9263
9264 /* ILK+ do this automagically */
49cff963 9265 if (HAS_GMCH_DISPLAY(dev_priv) &&
f22aa143 9266 plane_state->base.rotation & DRM_ROTATE_180) {
55a08b3f
ML
9267 base += (plane_state->base.crtc_h *
9268 plane_state->base.crtc_w - 1) * 4;
9269 }
cda4b7d3 9270 }
cda4b7d3 9271
5efb3e28
VS
9272 I915_WRITE(CURPOS(pipe), pos);
9273
2a307c2e 9274 if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
55a08b3f 9275 i845_update_cursor(crtc, base, plane_state);
5efb3e28 9276 else
55a08b3f 9277 i9xx_update_cursor(crtc, base, plane_state);
cda4b7d3
CW
9278}
9279
50a0bc90 9280static bool cursor_size_ok(struct drm_i915_private *dev_priv,
dc41c154
VS
9281 uint32_t width, uint32_t height)
9282{
9283 if (width == 0 || height == 0)
9284 return false;
9285
9286 /*
9287 * 845g/865g are special in that they are only limited by
9288 * the width of their cursors, the height is arbitrary up to
9289 * the precision of the register. Everything else requires
9290 * square cursors, limited to a few power-of-two sizes.
9291 */
2a307c2e 9292 if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
dc41c154
VS
9293 if ((width & 63) != 0)
9294 return false;
9295
2a307c2e 9296 if (width > (IS_I845G(dev_priv) ? 64 : 512))
dc41c154
VS
9297 return false;
9298
9299 if (height > 1023)
9300 return false;
9301 } else {
9302 switch (width | height) {
9303 case 256:
9304 case 128:
50a0bc90 9305 if (IS_GEN2(dev_priv))
dc41c154
VS
9306 return false;
9307 case 64:
9308 break;
9309 default:
9310 return false;
9311 }
9312 }
9313
9314 return true;
9315}
9316
79e53945
JB
9317/* VESA 640x480x72Hz mode to set on the pipe */
9318static struct drm_display_mode load_detect_mode = {
9319 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
9320 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
9321};
9322
a8bb6818 9323struct drm_framebuffer *
24dbf51a
CW
9324intel_framebuffer_create(struct drm_i915_gem_object *obj,
9325 struct drm_mode_fb_cmd2 *mode_cmd)
d2dff872
CW
9326{
9327 struct intel_framebuffer *intel_fb;
9328 int ret;
9329
9330 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
dcb1394e 9331 if (!intel_fb)
d2dff872 9332 return ERR_PTR(-ENOMEM);
d2dff872 9333
24dbf51a 9334 ret = intel_framebuffer_init(intel_fb, obj, mode_cmd);
dd4916c5
DV
9335 if (ret)
9336 goto err;
d2dff872
CW
9337
9338 return &intel_fb->base;
dcb1394e 9339
dd4916c5 9340err:
dd4916c5 9341 kfree(intel_fb);
dd4916c5 9342 return ERR_PTR(ret);
d2dff872
CW
9343}
9344
9345static u32
9346intel_framebuffer_pitch_for_width(int width, int bpp)
9347{
9348 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
9349 return ALIGN(pitch, 64);
9350}
9351
9352static u32
9353intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
9354{
9355 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
1267a26b 9356 return PAGE_ALIGN(pitch * mode->vdisplay);
d2dff872
CW
9357}
9358
9359static struct drm_framebuffer *
9360intel_framebuffer_create_for_mode(struct drm_device *dev,
9361 struct drm_display_mode *mode,
9362 int depth, int bpp)
9363{
dcb1394e 9364 struct drm_framebuffer *fb;
d2dff872 9365 struct drm_i915_gem_object *obj;
0fed39bd 9366 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872 9367
12d79d78 9368 obj = i915_gem_object_create(to_i915(dev),
d2dff872 9369 intel_framebuffer_size_for_mode(mode, bpp));
fe3db79b
CW
9370 if (IS_ERR(obj))
9371 return ERR_CAST(obj);
d2dff872
CW
9372
9373 mode_cmd.width = mode->hdisplay;
9374 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
9375 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
9376 bpp);
5ca0c34a 9377 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872 9378
24dbf51a 9379 fb = intel_framebuffer_create(obj, &mode_cmd);
dcb1394e 9380 if (IS_ERR(fb))
f0cd5182 9381 i915_gem_object_put(obj);
dcb1394e
LW
9382
9383 return fb;
d2dff872
CW
9384}
9385
9386static struct drm_framebuffer *
9387mode_fits_in_fbdev(struct drm_device *dev,
9388 struct drm_display_mode *mode)
9389{
0695726e 9390#ifdef CONFIG_DRM_FBDEV_EMULATION
fac5e23e 9391 struct drm_i915_private *dev_priv = to_i915(dev);
d2dff872
CW
9392 struct drm_i915_gem_object *obj;
9393 struct drm_framebuffer *fb;
9394
4c0e5528 9395 if (!dev_priv->fbdev)
d2dff872
CW
9396 return NULL;
9397
4c0e5528 9398 if (!dev_priv->fbdev->fb)
d2dff872
CW
9399 return NULL;
9400
4c0e5528
DV
9401 obj = dev_priv->fbdev->fb->obj;
9402 BUG_ON(!obj);
9403
8bcd4553 9404 fb = &dev_priv->fbdev->fb->base;
01f2c773 9405 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
272725c7 9406 fb->format->cpp[0] * 8))
d2dff872
CW
9407 return NULL;
9408
01f2c773 9409 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
9410 return NULL;
9411
edde3617 9412 drm_framebuffer_reference(fb);
d2dff872 9413 return fb;
4520f53a
DV
9414#else
9415 return NULL;
9416#endif
d2dff872
CW
9417}
9418
d3a40d1b
ACO
9419static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
9420 struct drm_crtc *crtc,
9421 struct drm_display_mode *mode,
9422 struct drm_framebuffer *fb,
9423 int x, int y)
9424{
9425 struct drm_plane_state *plane_state;
9426 int hdisplay, vdisplay;
9427 int ret;
9428
9429 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
9430 if (IS_ERR(plane_state))
9431 return PTR_ERR(plane_state);
9432
9433 if (mode)
196cd5d3 9434 drm_mode_get_hv_timing(mode, &hdisplay, &vdisplay);
d3a40d1b
ACO
9435 else
9436 hdisplay = vdisplay = 0;
9437
9438 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
9439 if (ret)
9440 return ret;
9441 drm_atomic_set_fb_for_plane(plane_state, fb);
9442 plane_state->crtc_x = 0;
9443 plane_state->crtc_y = 0;
9444 plane_state->crtc_w = hdisplay;
9445 plane_state->crtc_h = vdisplay;
9446 plane_state->src_x = x << 16;
9447 plane_state->src_y = y << 16;
9448 plane_state->src_w = hdisplay << 16;
9449 plane_state->src_h = vdisplay << 16;
9450
9451 return 0;
9452}
9453
d2434ab7 9454bool intel_get_load_detect_pipe(struct drm_connector *connector,
7173188d 9455 struct drm_display_mode *mode,
51fd371b
RC
9456 struct intel_load_detect_pipe *old,
9457 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
9458{
9459 struct intel_crtc *intel_crtc;
d2434ab7
DV
9460 struct intel_encoder *intel_encoder =
9461 intel_attached_encoder(connector);
79e53945 9462 struct drm_crtc *possible_crtc;
4ef69c7a 9463 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
9464 struct drm_crtc *crtc = NULL;
9465 struct drm_device *dev = encoder->dev;
0f0f74bc 9466 struct drm_i915_private *dev_priv = to_i915(dev);
94352cf9 9467 struct drm_framebuffer *fb;
51fd371b 9468 struct drm_mode_config *config = &dev->mode_config;
edde3617 9469 struct drm_atomic_state *state = NULL, *restore_state = NULL;
944b0c76 9470 struct drm_connector_state *connector_state;
4be07317 9471 struct intel_crtc_state *crtc_state;
51fd371b 9472 int ret, i = -1;
79e53945 9473
d2dff872 9474 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 9475 connector->base.id, connector->name,
8e329a03 9476 encoder->base.id, encoder->name);
d2dff872 9477
edde3617
ML
9478 old->restore_state = NULL;
9479
51fd371b
RC
9480retry:
9481 ret = drm_modeset_lock(&config->connection_mutex, ctx);
9482 if (ret)
ad3c558f 9483 goto fail;
6e9f798d 9484
79e53945
JB
9485 /*
9486 * Algorithm gets a little messy:
7a5e4805 9487 *
79e53945
JB
9488 * - if the connector already has an assigned crtc, use it (but make
9489 * sure it's on first)
7a5e4805 9490 *
79e53945
JB
9491 * - try to find the first unused crtc that can drive this connector,
9492 * and use that if we find one
79e53945
JB
9493 */
9494
9495 /* See if we already have a CRTC for this connector */
edde3617
ML
9496 if (connector->state->crtc) {
9497 crtc = connector->state->crtc;
8261b191 9498
51fd371b 9499 ret = drm_modeset_lock(&crtc->mutex, ctx);
4d02e2de 9500 if (ret)
ad3c558f 9501 goto fail;
8261b191
CW
9502
9503 /* Make sure the crtc and connector are running */
edde3617 9504 goto found;
79e53945
JB
9505 }
9506
9507 /* Find an unused one (if possible) */
70e1e0ec 9508 for_each_crtc(dev, possible_crtc) {
79e53945
JB
9509 i++;
9510 if (!(encoder->possible_crtcs & (1 << i)))
9511 continue;
edde3617
ML
9512
9513 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
9514 if (ret)
9515 goto fail;
9516
9517 if (possible_crtc->state->enable) {
9518 drm_modeset_unlock(&possible_crtc->mutex);
a459249c 9519 continue;
edde3617 9520 }
a459249c
VS
9521
9522 crtc = possible_crtc;
9523 break;
79e53945
JB
9524 }
9525
9526 /*
9527 * If we didn't find an unused CRTC, don't use any.
9528 */
9529 if (!crtc) {
7173188d 9530 DRM_DEBUG_KMS("no pipe available for load-detect\n");
ad3c558f 9531 goto fail;
79e53945
JB
9532 }
9533
edde3617
ML
9534found:
9535 intel_crtc = to_intel_crtc(crtc);
9536
4d02e2de
DV
9537 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
9538 if (ret)
ad3c558f 9539 goto fail;
79e53945 9540
83a57153 9541 state = drm_atomic_state_alloc(dev);
edde3617
ML
9542 restore_state = drm_atomic_state_alloc(dev);
9543 if (!state || !restore_state) {
9544 ret = -ENOMEM;
9545 goto fail;
9546 }
83a57153
ACO
9547
9548 state->acquire_ctx = ctx;
edde3617 9549 restore_state->acquire_ctx = ctx;
83a57153 9550
944b0c76
ACO
9551 connector_state = drm_atomic_get_connector_state(state, connector);
9552 if (IS_ERR(connector_state)) {
9553 ret = PTR_ERR(connector_state);
9554 goto fail;
9555 }
9556
edde3617
ML
9557 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
9558 if (ret)
9559 goto fail;
944b0c76 9560
4be07317
ACO
9561 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
9562 if (IS_ERR(crtc_state)) {
9563 ret = PTR_ERR(crtc_state);
9564 goto fail;
9565 }
9566
49d6fa21 9567 crtc_state->base.active = crtc_state->base.enable = true;
4be07317 9568
6492711d
CW
9569 if (!mode)
9570 mode = &load_detect_mode;
79e53945 9571
d2dff872
CW
9572 /* We need a framebuffer large enough to accommodate all accesses
9573 * that the plane may generate whilst we perform load detection.
9574 * We can not rely on the fbcon either being present (we get called
9575 * during its initialisation to detect all boot displays, or it may
9576 * not even exist) or that it is large enough to satisfy the
9577 * requested mode.
9578 */
94352cf9
DV
9579 fb = mode_fits_in_fbdev(dev, mode);
9580 if (fb == NULL) {
d2dff872 9581 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9 9582 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
d2dff872
CW
9583 } else
9584 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 9585 if (IS_ERR(fb)) {
d2dff872 9586 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
412b61d8 9587 goto fail;
79e53945 9588 }
79e53945 9589
d3a40d1b
ACO
9590 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
9591 if (ret)
9592 goto fail;
9593
edde3617
ML
9594 drm_framebuffer_unreference(fb);
9595
9596 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
9597 if (ret)
9598 goto fail;
9599
9600 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
9601 if (!ret)
9602 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
9603 if (!ret)
9604 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
9605 if (ret) {
9606 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
9607 goto fail;
9608 }
8c7b5ccb 9609
3ba86073
ML
9610 ret = drm_atomic_commit(state);
9611 if (ret) {
6492711d 9612 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
412b61d8 9613 goto fail;
79e53945 9614 }
edde3617
ML
9615
9616 old->restore_state = restore_state;
7abbd11f 9617 drm_atomic_state_put(state);
7173188d 9618
79e53945 9619 /* let the connector get through one full cycle before testing */
0f0f74bc 9620 intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
7173188d 9621 return true;
412b61d8 9622
ad3c558f 9623fail:
7fb71c8f
CW
9624 if (state) {
9625 drm_atomic_state_put(state);
9626 state = NULL;
9627 }
9628 if (restore_state) {
9629 drm_atomic_state_put(restore_state);
9630 restore_state = NULL;
9631 }
83a57153 9632
51fd371b
RC
9633 if (ret == -EDEADLK) {
9634 drm_modeset_backoff(ctx);
9635 goto retry;
9636 }
9637
412b61d8 9638 return false;
79e53945
JB
9639}
9640
d2434ab7 9641void intel_release_load_detect_pipe(struct drm_connector *connector,
49172fee
ACO
9642 struct intel_load_detect_pipe *old,
9643 struct drm_modeset_acquire_ctx *ctx)
79e53945 9644{
d2434ab7
DV
9645 struct intel_encoder *intel_encoder =
9646 intel_attached_encoder(connector);
4ef69c7a 9647 struct drm_encoder *encoder = &intel_encoder->base;
edde3617 9648 struct drm_atomic_state *state = old->restore_state;
d3a40d1b 9649 int ret;
79e53945 9650
d2dff872 9651 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 9652 connector->base.id, connector->name,
8e329a03 9653 encoder->base.id, encoder->name);
d2dff872 9654
edde3617 9655 if (!state)
0622a53c 9656 return;
79e53945 9657
edde3617 9658 ret = drm_atomic_commit(state);
0853695c 9659 if (ret)
edde3617 9660 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
0853695c 9661 drm_atomic_state_put(state);
79e53945
JB
9662}
9663
da4a1efa 9664static int i9xx_pll_refclk(struct drm_device *dev,
5cec258b 9665 const struct intel_crtc_state *pipe_config)
da4a1efa 9666{
fac5e23e 9667 struct drm_i915_private *dev_priv = to_i915(dev);
da4a1efa
VS
9668 u32 dpll = pipe_config->dpll_hw_state.dpll;
9669
9670 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 9671 return dev_priv->vbt.lvds_ssc_freq;
6e266956 9672 else if (HAS_PCH_SPLIT(dev_priv))
da4a1efa 9673 return 120000;
5db94019 9674 else if (!IS_GEN2(dev_priv))
da4a1efa
VS
9675 return 96000;
9676 else
9677 return 48000;
9678}
9679
79e53945 9680/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc 9681static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 9682 struct intel_crtc_state *pipe_config)
79e53945 9683{
f1f644dc 9684 struct drm_device *dev = crtc->base.dev;
fac5e23e 9685 struct drm_i915_private *dev_priv = to_i915(dev);
f1f644dc 9686 int pipe = pipe_config->cpu_transcoder;
293623f7 9687 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945 9688 u32 fp;
9e2c8475 9689 struct dpll clock;
dccbea3b 9690 int port_clock;
da4a1efa 9691 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
9692
9693 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 9694 fp = pipe_config->dpll_hw_state.fp0;
79e53945 9695 else
293623f7 9696 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
9697
9698 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
9b1e14f4 9699 if (IS_PINEVIEW(dev_priv)) {
f2b115e6
AJ
9700 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
9701 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
9702 } else {
9703 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
9704 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
9705 }
9706
5db94019 9707 if (!IS_GEN2(dev_priv)) {
9b1e14f4 9708 if (IS_PINEVIEW(dev_priv))
f2b115e6
AJ
9709 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
9710 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
9711 else
9712 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
9713 DPLL_FPA01_P1_POST_DIV_SHIFT);
9714
9715 switch (dpll & DPLL_MODE_MASK) {
9716 case DPLLB_MODE_DAC_SERIAL:
9717 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
9718 5 : 10;
9719 break;
9720 case DPLLB_MODE_LVDS:
9721 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
9722 7 : 14;
9723 break;
9724 default:
28c97730 9725 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 9726 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 9727 return;
79e53945
JB
9728 }
9729
9b1e14f4 9730 if (IS_PINEVIEW(dev_priv))
dccbea3b 9731 port_clock = pnv_calc_dpll_params(refclk, &clock);
ac58c3f0 9732 else
dccbea3b 9733 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945 9734 } else {
50a0bc90 9735 u32 lvds = IS_I830(dev_priv) ? 0 : I915_READ(LVDS);
b1c560d1 9736 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
9737
9738 if (is_lvds) {
9739 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
9740 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
9741
9742 if (lvds & LVDS_CLKB_POWER_UP)
9743 clock.p2 = 7;
9744 else
9745 clock.p2 = 14;
79e53945
JB
9746 } else {
9747 if (dpll & PLL_P1_DIVIDE_BY_TWO)
9748 clock.p1 = 2;
9749 else {
9750 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
9751 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
9752 }
9753 if (dpll & PLL_P2_DIVIDE_BY_4)
9754 clock.p2 = 4;
9755 else
9756 clock.p2 = 2;
79e53945 9757 }
da4a1efa 9758
dccbea3b 9759 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945
JB
9760 }
9761
18442d08
VS
9762 /*
9763 * This value includes pixel_multiplier. We will use
241bfc38 9764 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
9765 * encoder's get_config() function.
9766 */
dccbea3b 9767 pipe_config->port_clock = port_clock;
f1f644dc
JB
9768}
9769
6878da05
VS
9770int intel_dotclock_calculate(int link_freq,
9771 const struct intel_link_m_n *m_n)
f1f644dc 9772{
f1f644dc
JB
9773 /*
9774 * The calculation for the data clock is:
1041a02f 9775 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 9776 * But we want to avoid losing precison if possible, so:
1041a02f 9777 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
9778 *
9779 * and the link clock is simpler:
1041a02f 9780 * link_clock = (m * link_clock) / n
f1f644dc
JB
9781 */
9782
6878da05
VS
9783 if (!m_n->link_n)
9784 return 0;
f1f644dc 9785
6878da05
VS
9786 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
9787}
f1f644dc 9788
18442d08 9789static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 9790 struct intel_crtc_state *pipe_config)
6878da05 9791{
e3b247da 9792 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
79e53945 9793
18442d08
VS
9794 /* read out port_clock from the DPLL */
9795 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 9796
f1f644dc 9797 /*
e3b247da
VS
9798 * In case there is an active pipe without active ports,
9799 * we may need some idea for the dotclock anyway.
9800 * Calculate one based on the FDI configuration.
79e53945 9801 */
2d112de7 9802 pipe_config->base.adjusted_mode.crtc_clock =
21a727b3 9803 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
18442d08 9804 &pipe_config->fdi_m_n);
79e53945
JB
9805}
9806
9807/** Returns the currently programmed mode of the given pipe. */
9808struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
9809 struct drm_crtc *crtc)
9810{
fac5e23e 9811 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 9812 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 9813 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
79e53945 9814 struct drm_display_mode *mode;
3f36b937 9815 struct intel_crtc_state *pipe_config;
fe2b8f9d
PZ
9816 int htot = I915_READ(HTOTAL(cpu_transcoder));
9817 int hsync = I915_READ(HSYNC(cpu_transcoder));
9818 int vtot = I915_READ(VTOTAL(cpu_transcoder));
9819 int vsync = I915_READ(VSYNC(cpu_transcoder));
293623f7 9820 enum pipe pipe = intel_crtc->pipe;
79e53945
JB
9821
9822 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
9823 if (!mode)
9824 return NULL;
9825
3f36b937
TU
9826 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
9827 if (!pipe_config) {
9828 kfree(mode);
9829 return NULL;
9830 }
9831
f1f644dc
JB
9832 /*
9833 * Construct a pipe_config sufficient for getting the clock info
9834 * back out of crtc_clock_get.
9835 *
9836 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
9837 * to use a real value here instead.
9838 */
3f36b937
TU
9839 pipe_config->cpu_transcoder = (enum transcoder) pipe;
9840 pipe_config->pixel_multiplier = 1;
9841 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
9842 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
9843 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
9844 i9xx_crtc_clock_get(intel_crtc, pipe_config);
9845
9846 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
79e53945
JB
9847 mode->hdisplay = (htot & 0xffff) + 1;
9848 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
9849 mode->hsync_start = (hsync & 0xffff) + 1;
9850 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
9851 mode->vdisplay = (vtot & 0xffff) + 1;
9852 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
9853 mode->vsync_start = (vsync & 0xffff) + 1;
9854 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
9855
9856 drm_mode_set_name(mode);
79e53945 9857
3f36b937
TU
9858 kfree(pipe_config);
9859
79e53945
JB
9860 return mode;
9861}
9862
9863static void intel_crtc_destroy(struct drm_crtc *crtc)
9864{
9865 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
67e77c5a 9866 struct drm_device *dev = crtc->dev;
51cbaf01 9867 struct intel_flip_work *work;
67e77c5a 9868
5e2d7afc 9869 spin_lock_irq(&dev->event_lock);
5a21b665
DV
9870 work = intel_crtc->flip_work;
9871 intel_crtc->flip_work = NULL;
9872 spin_unlock_irq(&dev->event_lock);
67e77c5a 9873
5a21b665 9874 if (work) {
51cbaf01
ML
9875 cancel_work_sync(&work->mmio_work);
9876 cancel_work_sync(&work->unpin_work);
5a21b665 9877 kfree(work);
67e77c5a 9878 }
79e53945
JB
9879
9880 drm_crtc_cleanup(crtc);
67e77c5a 9881
79e53945
JB
9882 kfree(intel_crtc);
9883}
9884
6b95a207
KH
9885static void intel_unpin_work_fn(struct work_struct *__work)
9886{
51cbaf01
ML
9887 struct intel_flip_work *work =
9888 container_of(__work, struct intel_flip_work, unpin_work);
5a21b665
DV
9889 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
9890 struct drm_device *dev = crtc->base.dev;
9891 struct drm_plane *primary = crtc->base.primary;
03f476e1 9892
5a21b665
DV
9893 if (is_mmio_work(work))
9894 flush_work(&work->mmio_work);
03f476e1 9895
5a21b665 9896 mutex_lock(&dev->struct_mutex);
be1e3415 9897 intel_unpin_fb_vma(work->old_vma);
f8c417cd 9898 i915_gem_object_put(work->pending_flip_obj);
5a21b665 9899 mutex_unlock(&dev->struct_mutex);
143f73b3 9900
e8a261ea
CW
9901 i915_gem_request_put(work->flip_queued_req);
9902
5748b6a1
CW
9903 intel_frontbuffer_flip_complete(to_i915(dev),
9904 to_intel_plane(primary)->frontbuffer_bit);
5a21b665
DV
9905 intel_fbc_post_update(crtc);
9906 drm_framebuffer_unreference(work->old_fb);
143f73b3 9907
5a21b665
DV
9908 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
9909 atomic_dec(&crtc->unpin_work_count);
a6747b73 9910
5a21b665
DV
9911 kfree(work);
9912}
d9e86c0e 9913
5a21b665
DV
9914/* Is 'a' after or equal to 'b'? */
9915static bool g4x_flip_count_after_eq(u32 a, u32 b)
9916{
9917 return !((a - b) & 0x80000000);
9918}
143f73b3 9919
5a21b665
DV
9920static bool __pageflip_finished_cs(struct intel_crtc *crtc,
9921 struct intel_flip_work *work)
9922{
9923 struct drm_device *dev = crtc->base.dev;
fac5e23e 9924 struct drm_i915_private *dev_priv = to_i915(dev);
143f73b3 9925
8af29b0c 9926 if (abort_flip_on_reset(crtc))
5a21b665 9927 return true;
143f73b3 9928
5a21b665
DV
9929 /*
9930 * The relevant registers doen't exist on pre-ctg.
9931 * As the flip done interrupt doesn't trigger for mmio
9932 * flips on gmch platforms, a flip count check isn't
9933 * really needed there. But since ctg has the registers,
9934 * include it in the check anyway.
9935 */
9beb5fea 9936 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
5a21b665 9937 return true;
b4a98e57 9938
5a21b665
DV
9939 /*
9940 * BDW signals flip done immediately if the plane
9941 * is disabled, even if the plane enable is already
9942 * armed to occur at the next vblank :(
9943 */
f99d7069 9944
5a21b665
DV
9945 /*
9946 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
9947 * used the same base address. In that case the mmio flip might
9948 * have completed, but the CS hasn't even executed the flip yet.
9949 *
9950 * A flip count check isn't enough as the CS might have updated
9951 * the base address just after start of vblank, but before we
9952 * managed to process the interrupt. This means we'd complete the
9953 * CS flip too soon.
9954 *
9955 * Combining both checks should get us a good enough result. It may
9956 * still happen that the CS flip has been executed, but has not
9957 * yet actually completed. But in case the base address is the same
9958 * anyway, we don't really care.
9959 */
9960 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
9961 crtc->flip_work->gtt_offset &&
9962 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
9963 crtc->flip_work->flip_count);
9964}
b4a98e57 9965
5a21b665
DV
9966static bool
9967__pageflip_finished_mmio(struct intel_crtc *crtc,
9968 struct intel_flip_work *work)
9969{
9970 /*
9971 * MMIO work completes when vblank is different from
9972 * flip_queued_vblank.
9973 *
9974 * Reset counter value doesn't matter, this is handled by
9975 * i915_wait_request finishing early, so no need to handle
9976 * reset here.
9977 */
9978 return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
6b95a207
KH
9979}
9980
51cbaf01
ML
9981
9982static bool pageflip_finished(struct intel_crtc *crtc,
9983 struct intel_flip_work *work)
9984{
9985 if (!atomic_read(&work->pending))
9986 return false;
9987
9988 smp_rmb();
9989
5a21b665
DV
9990 if (is_mmio_work(work))
9991 return __pageflip_finished_mmio(crtc, work);
9992 else
9993 return __pageflip_finished_cs(crtc, work);
9994}
9995
9996void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
9997{
91c8a326 9998 struct drm_device *dev = &dev_priv->drm;
98187836 9999 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
5a21b665
DV
10000 struct intel_flip_work *work;
10001 unsigned long flags;
10002
10003 /* Ignore early vblank irqs */
10004 if (!crtc)
10005 return;
10006
51cbaf01 10007 /*
5a21b665
DV
10008 * This is called both by irq handlers and the reset code (to complete
10009 * lost pageflips) so needs the full irqsave spinlocks.
51cbaf01 10010 */
5a21b665 10011 spin_lock_irqsave(&dev->event_lock, flags);
e2af48c6 10012 work = crtc->flip_work;
5a21b665
DV
10013
10014 if (work != NULL &&
10015 !is_mmio_work(work) &&
e2af48c6
VS
10016 pageflip_finished(crtc, work))
10017 page_flip_completed(crtc);
5a21b665
DV
10018
10019 spin_unlock_irqrestore(&dev->event_lock, flags);
75f7f3ec
VS
10020}
10021
51cbaf01 10022void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
6b95a207 10023{
91c8a326 10024 struct drm_device *dev = &dev_priv->drm;
98187836 10025 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
51cbaf01 10026 struct intel_flip_work *work;
6b95a207
KH
10027 unsigned long flags;
10028
5251f04e
ML
10029 /* Ignore early vblank irqs */
10030 if (!crtc)
10031 return;
f326038a
DV
10032
10033 /*
10034 * This is called both by irq handlers and the reset code (to complete
10035 * lost pageflips) so needs the full irqsave spinlocks.
e7d841ca 10036 */
6b95a207 10037 spin_lock_irqsave(&dev->event_lock, flags);
e2af48c6 10038 work = crtc->flip_work;
5251f04e 10039
5a21b665
DV
10040 if (work != NULL &&
10041 is_mmio_work(work) &&
e2af48c6
VS
10042 pageflip_finished(crtc, work))
10043 page_flip_completed(crtc);
5251f04e 10044
6b95a207
KH
10045 spin_unlock_irqrestore(&dev->event_lock, flags);
10046}
10047
5a21b665
DV
10048static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
10049 struct intel_flip_work *work)
84c33a64 10050{
5a21b665 10051 work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
84c33a64 10052
5a21b665
DV
10053 /* Ensure that the work item is consistent when activating it ... */
10054 smp_mb__before_atomic();
10055 atomic_set(&work->pending, 1);
10056}
a6747b73 10057
5a21b665
DV
10058static int intel_gen2_queue_flip(struct drm_device *dev,
10059 struct drm_crtc *crtc,
10060 struct drm_framebuffer *fb,
10061 struct drm_i915_gem_object *obj,
10062 struct drm_i915_gem_request *req,
10063 uint32_t flags)
10064{
5a21b665 10065 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
73dec95e 10066 u32 flip_mask, *cs;
143f73b3 10067
73dec95e
TU
10068 cs = intel_ring_begin(req, 6);
10069 if (IS_ERR(cs))
10070 return PTR_ERR(cs);
143f73b3 10071
5a21b665
DV
10072 /* Can't queue multiple flips, so wait for the previous
10073 * one to finish before executing the next.
10074 */
10075 if (intel_crtc->plane)
10076 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10077 else
10078 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
73dec95e
TU
10079 *cs++ = MI_WAIT_FOR_EVENT | flip_mask;
10080 *cs++ = MI_NOOP;
10081 *cs++ = MI_DISPLAY_FLIP | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
10082 *cs++ = fb->pitches[0];
10083 *cs++ = intel_crtc->flip_work->gtt_offset;
10084 *cs++ = 0; /* aux display base address, unused */
143f73b3 10085
5a21b665
DV
10086 return 0;
10087}
84c33a64 10088
5a21b665
DV
10089static int intel_gen3_queue_flip(struct drm_device *dev,
10090 struct drm_crtc *crtc,
10091 struct drm_framebuffer *fb,
10092 struct drm_i915_gem_object *obj,
10093 struct drm_i915_gem_request *req,
10094 uint32_t flags)
10095{
5a21b665 10096 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
73dec95e 10097 u32 flip_mask, *cs;
d55dbd06 10098
73dec95e
TU
10099 cs = intel_ring_begin(req, 6);
10100 if (IS_ERR(cs))
10101 return PTR_ERR(cs);
d55dbd06 10102
5a21b665
DV
10103 if (intel_crtc->plane)
10104 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10105 else
10106 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
73dec95e
TU
10107 *cs++ = MI_WAIT_FOR_EVENT | flip_mask;
10108 *cs++ = MI_NOOP;
10109 *cs++ = MI_DISPLAY_FLIP_I915 | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
10110 *cs++ = fb->pitches[0];
10111 *cs++ = intel_crtc->flip_work->gtt_offset;
10112 *cs++ = MI_NOOP;
fd8e058a 10113
5a21b665
DV
10114 return 0;
10115}
84c33a64 10116
5a21b665
DV
10117static int intel_gen4_queue_flip(struct drm_device *dev,
10118 struct drm_crtc *crtc,
10119 struct drm_framebuffer *fb,
10120 struct drm_i915_gem_object *obj,
10121 struct drm_i915_gem_request *req,
10122 uint32_t flags)
10123{
fac5e23e 10124 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665 10125 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
73dec95e 10126 u32 pf, pipesrc, *cs;
143f73b3 10127
73dec95e
TU
10128 cs = intel_ring_begin(req, 4);
10129 if (IS_ERR(cs))
10130 return PTR_ERR(cs);
143f73b3 10131
5a21b665
DV
10132 /* i965+ uses the linear or tiled offsets from the
10133 * Display Registers (which do not change across a page-flip)
10134 * so we need only reprogram the base address.
10135 */
73dec95e
TU
10136 *cs++ = MI_DISPLAY_FLIP | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
10137 *cs++ = fb->pitches[0];
10138 *cs++ = intel_crtc->flip_work->gtt_offset |
10139 intel_fb_modifier_to_tiling(fb->modifier);
5a21b665
DV
10140
10141 /* XXX Enabling the panel-fitter across page-flip is so far
10142 * untested on non-native modes, so ignore it for now.
10143 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
10144 */
10145 pf = 0;
10146 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
73dec95e 10147 *cs++ = pf | pipesrc;
143f73b3 10148
5a21b665 10149 return 0;
8c9f3aaf
JB
10150}
10151
5a21b665
DV
10152static int intel_gen6_queue_flip(struct drm_device *dev,
10153 struct drm_crtc *crtc,
10154 struct drm_framebuffer *fb,
10155 struct drm_i915_gem_object *obj,
10156 struct drm_i915_gem_request *req,
10157 uint32_t flags)
da20eabd 10158{
fac5e23e 10159 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665 10160 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
73dec95e 10161 u32 pf, pipesrc, *cs;
d21fbe87 10162
73dec95e
TU
10163 cs = intel_ring_begin(req, 4);
10164 if (IS_ERR(cs))
10165 return PTR_ERR(cs);
92826fcd 10166
73dec95e
TU
10167 *cs++ = MI_DISPLAY_FLIP | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
10168 *cs++ = fb->pitches[0] | intel_fb_modifier_to_tiling(fb->modifier);
10169 *cs++ = intel_crtc->flip_work->gtt_offset;
92826fcd 10170
5a21b665
DV
10171 /* Contrary to the suggestions in the documentation,
10172 * "Enable Panel Fitter" does not seem to be required when page
10173 * flipping with a non-native mode, and worse causes a normal
10174 * modeset to fail.
10175 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
10176 */
10177 pf = 0;
10178 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
73dec95e 10179 *cs++ = pf | pipesrc;
7809e5ae 10180
5a21b665 10181 return 0;
7809e5ae
MR
10182}
10183
5a21b665
DV
10184static int intel_gen7_queue_flip(struct drm_device *dev,
10185 struct drm_crtc *crtc,
10186 struct drm_framebuffer *fb,
10187 struct drm_i915_gem_object *obj,
10188 struct drm_i915_gem_request *req,
10189 uint32_t flags)
d21fbe87 10190{
5db94019 10191 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665 10192 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
73dec95e 10193 u32 *cs, plane_bit = 0;
5a21b665 10194 int len, ret;
d21fbe87 10195
5a21b665
DV
10196 switch (intel_crtc->plane) {
10197 case PLANE_A:
10198 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
10199 break;
10200 case PLANE_B:
10201 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
10202 break;
10203 case PLANE_C:
10204 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
10205 break;
10206 default:
10207 WARN_ONCE(1, "unknown plane in flip command\n");
10208 return -ENODEV;
10209 }
10210
10211 len = 4;
b5321f30 10212 if (req->engine->id == RCS) {
5a21b665
DV
10213 len += 6;
10214 /*
10215 * On Gen 8, SRM is now taking an extra dword to accommodate
10216 * 48bits addresses, and we need a NOOP for the batch size to
10217 * stay even.
10218 */
5db94019 10219 if (IS_GEN8(dev_priv))
5a21b665
DV
10220 len += 2;
10221 }
10222
10223 /*
10224 * BSpec MI_DISPLAY_FLIP for IVB:
10225 * "The full packet must be contained within the same cache line."
10226 *
10227 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
10228 * cacheline, if we ever start emitting more commands before
10229 * the MI_DISPLAY_FLIP we may need to first emit everything else,
10230 * then do the cacheline alignment, and finally emit the
10231 * MI_DISPLAY_FLIP.
10232 */
10233 ret = intel_ring_cacheline_align(req);
10234 if (ret)
10235 return ret;
10236
73dec95e
TU
10237 cs = intel_ring_begin(req, len);
10238 if (IS_ERR(cs))
10239 return PTR_ERR(cs);
5a21b665
DV
10240
10241 /* Unmask the flip-done completion message. Note that the bspec says that
10242 * we should do this for both the BCS and RCS, and that we must not unmask
10243 * more than one flip event at any time (or ensure that one flip message
10244 * can be sent by waiting for flip-done prior to queueing new flips).
10245 * Experimentation says that BCS works despite DERRMR masking all
10246 * flip-done completion events and that unmasking all planes at once
10247 * for the RCS also doesn't appear to drop events. Setting the DERRMR
10248 * to zero does lead to lockups within MI_DISPLAY_FLIP.
10249 */
b5321f30 10250 if (req->engine->id == RCS) {
73dec95e
TU
10251 *cs++ = MI_LOAD_REGISTER_IMM(1);
10252 *cs++ = i915_mmio_reg_offset(DERRMR);
10253 *cs++ = ~(DERRMR_PIPEA_PRI_FLIP_DONE |
10254 DERRMR_PIPEB_PRI_FLIP_DONE |
10255 DERRMR_PIPEC_PRI_FLIP_DONE);
5db94019 10256 if (IS_GEN8(dev_priv))
73dec95e
TU
10257 *cs++ = MI_STORE_REGISTER_MEM_GEN8 |
10258 MI_SRM_LRM_GLOBAL_GTT;
5a21b665 10259 else
73dec95e
TU
10260 *cs++ = MI_STORE_REGISTER_MEM | MI_SRM_LRM_GLOBAL_GTT;
10261 *cs++ = i915_mmio_reg_offset(DERRMR);
10262 *cs++ = i915_ggtt_offset(req->engine->scratch) + 256;
5db94019 10263 if (IS_GEN8(dev_priv)) {
73dec95e
TU
10264 *cs++ = 0;
10265 *cs++ = MI_NOOP;
5a21b665
DV
10266 }
10267 }
10268
73dec95e
TU
10269 *cs++ = MI_DISPLAY_FLIP_I915 | plane_bit;
10270 *cs++ = fb->pitches[0] | intel_fb_modifier_to_tiling(fb->modifier);
10271 *cs++ = intel_crtc->flip_work->gtt_offset;
10272 *cs++ = MI_NOOP;
5a21b665
DV
10273
10274 return 0;
10275}
10276
10277static bool use_mmio_flip(struct intel_engine_cs *engine,
10278 struct drm_i915_gem_object *obj)
10279{
10280 /*
10281 * This is not being used for older platforms, because
10282 * non-availability of flip done interrupt forces us to use
10283 * CS flips. Older platforms derive flip done using some clever
10284 * tricks involving the flip_pending status bits and vblank irqs.
10285 * So using MMIO flips there would disrupt this mechanism.
10286 */
10287
10288 if (engine == NULL)
10289 return true;
10290
10291 if (INTEL_GEN(engine->i915) < 5)
10292 return false;
10293
10294 if (i915.use_mmio_flip < 0)
10295 return false;
10296 else if (i915.use_mmio_flip > 0)
10297 return true;
10298 else if (i915.enable_execlists)
10299 return true;
c37efb99 10300
d07f0e59 10301 return engine != i915_gem_object_last_write_engine(obj);
5a21b665
DV
10302}
10303
10304static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
10305 unsigned int rotation,
10306 struct intel_flip_work *work)
10307{
10308 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 10309 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
DV
10310 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
10311 const enum pipe pipe = intel_crtc->pipe;
d2196774 10312 u32 ctl, stride = skl_plane_stride(fb, 0, rotation);
5a21b665
DV
10313
10314 ctl = I915_READ(PLANE_CTL(pipe, 0));
10315 ctl &= ~PLANE_CTL_TILED_MASK;
bae781b2 10316 switch (fb->modifier) {
5a21b665
DV
10317 case DRM_FORMAT_MOD_NONE:
10318 break;
10319 case I915_FORMAT_MOD_X_TILED:
10320 ctl |= PLANE_CTL_TILED_X;
10321 break;
10322 case I915_FORMAT_MOD_Y_TILED:
10323 ctl |= PLANE_CTL_TILED_Y;
10324 break;
10325 case I915_FORMAT_MOD_Yf_TILED:
10326 ctl |= PLANE_CTL_TILED_YF;
10327 break;
10328 default:
bae781b2 10329 MISSING_CASE(fb->modifier);
5a21b665
DV
10330 }
10331
5a21b665
DV
10332 /*
10333 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
10334 * PLANE_SURF updates, the update is then guaranteed to be atomic.
10335 */
10336 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
10337 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
10338
10339 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
10340 POSTING_READ(PLANE_SURF(pipe, 0));
10341}
10342
10343static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
10344 struct intel_flip_work *work)
10345{
10346 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 10347 struct drm_i915_private *dev_priv = to_i915(dev);
72618ebf 10348 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
5a21b665
DV
10349 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
10350 u32 dspcntr;
10351
10352 dspcntr = I915_READ(reg);
10353
bae781b2 10354 if (fb->modifier == I915_FORMAT_MOD_X_TILED)
5a21b665
DV
10355 dspcntr |= DISPPLANE_TILED;
10356 else
10357 dspcntr &= ~DISPPLANE_TILED;
10358
10359 I915_WRITE(reg, dspcntr);
10360
10361 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
10362 POSTING_READ(DSPSURF(intel_crtc->plane));
10363}
10364
10365static void intel_mmio_flip_work_func(struct work_struct *w)
10366{
10367 struct intel_flip_work *work =
10368 container_of(w, struct intel_flip_work, mmio_work);
10369 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10370 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10371 struct intel_framebuffer *intel_fb =
10372 to_intel_framebuffer(crtc->base.primary->fb);
10373 struct drm_i915_gem_object *obj = intel_fb->obj;
10374
d07f0e59 10375 WARN_ON(i915_gem_object_wait(obj, 0, MAX_SCHEDULE_TIMEOUT, NULL) < 0);
5a21b665
DV
10376
10377 intel_pipe_update_start(crtc);
10378
10379 if (INTEL_GEN(dev_priv) >= 9)
10380 skl_do_mmio_flip(crtc, work->rotation, work);
10381 else
10382 /* use_mmio_flip() retricts MMIO flips to ilk+ */
10383 ilk_do_mmio_flip(crtc, work);
10384
10385 intel_pipe_update_end(crtc, work);
10386}
10387
10388static int intel_default_queue_flip(struct drm_device *dev,
10389 struct drm_crtc *crtc,
10390 struct drm_framebuffer *fb,
10391 struct drm_i915_gem_object *obj,
10392 struct drm_i915_gem_request *req,
10393 uint32_t flags)
10394{
10395 return -ENODEV;
10396}
10397
10398static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
10399 struct intel_crtc *intel_crtc,
10400 struct intel_flip_work *work)
10401{
10402 u32 addr, vblank;
10403
10404 if (!atomic_read(&work->pending))
10405 return false;
10406
10407 smp_rmb();
10408
10409 vblank = intel_crtc_get_vblank_counter(intel_crtc);
10410 if (work->flip_ready_vblank == 0) {
10411 if (work->flip_queued_req &&
f69a02c9 10412 !i915_gem_request_completed(work->flip_queued_req))
5a21b665
DV
10413 return false;
10414
10415 work->flip_ready_vblank = vblank;
10416 }
10417
10418 if (vblank - work->flip_ready_vblank < 3)
10419 return false;
10420
10421 /* Potential stall - if we see that the flip has happened,
10422 * assume a missed interrupt. */
10423 if (INTEL_GEN(dev_priv) >= 4)
10424 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
10425 else
10426 addr = I915_READ(DSPADDR(intel_crtc->plane));
10427
10428 /* There is a potential issue here with a false positive after a flip
10429 * to the same address. We could address this by checking for a
10430 * non-incrementing frame counter.
10431 */
10432 return addr == work->gtt_offset;
10433}
10434
10435void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
10436{
91c8a326 10437 struct drm_device *dev = &dev_priv->drm;
98187836 10438 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
5a21b665
DV
10439 struct intel_flip_work *work;
10440
10441 WARN_ON(!in_interrupt());
10442
10443 if (crtc == NULL)
10444 return;
10445
10446 spin_lock(&dev->event_lock);
e2af48c6 10447 work = crtc->flip_work;
5a21b665
DV
10448
10449 if (work != NULL && !is_mmio_work(work) &&
e2af48c6 10450 __pageflip_stall_check_cs(dev_priv, crtc, work)) {
5a21b665
DV
10451 WARN_ONCE(1,
10452 "Kicking stuck page flip: queued at %d, now %d\n",
e2af48c6
VS
10453 work->flip_queued_vblank, intel_crtc_get_vblank_counter(crtc));
10454 page_flip_completed(crtc);
5a21b665
DV
10455 work = NULL;
10456 }
10457
10458 if (work != NULL && !is_mmio_work(work) &&
e2af48c6 10459 intel_crtc_get_vblank_counter(crtc) - work->flip_queued_vblank > 1)
5a21b665
DV
10460 intel_queue_rps_boost_for_request(work->flip_queued_req);
10461 spin_unlock(&dev->event_lock);
10462}
10463
4c01ded5 10464__maybe_unused
5a21b665
DV
10465static int intel_crtc_page_flip(struct drm_crtc *crtc,
10466 struct drm_framebuffer *fb,
10467 struct drm_pending_vblank_event *event,
10468 uint32_t page_flip_flags)
10469{
10470 struct drm_device *dev = crtc->dev;
fac5e23e 10471 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
DV
10472 struct drm_framebuffer *old_fb = crtc->primary->fb;
10473 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
10474 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10475 struct drm_plane *primary = crtc->primary;
10476 enum pipe pipe = intel_crtc->pipe;
10477 struct intel_flip_work *work;
10478 struct intel_engine_cs *engine;
10479 bool mmio_flip;
8e637178 10480 struct drm_i915_gem_request *request;
058d88c4 10481 struct i915_vma *vma;
5a21b665
DV
10482 int ret;
10483
10484 /*
10485 * drm_mode_page_flip_ioctl() should already catch this, but double
10486 * check to be safe. In the future we may enable pageflipping from
10487 * a disabled primary plane.
10488 */
10489 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
10490 return -EBUSY;
10491
10492 /* Can't change pixel format via MI display flips. */
dbd4d576 10493 if (fb->format != crtc->primary->fb->format)
5a21b665
DV
10494 return -EINVAL;
10495
10496 /*
10497 * TILEOFF/LINOFF registers can't be changed via MI display flips.
10498 * Note that pitch changes could also affect these register.
10499 */
6315b5d3 10500 if (INTEL_GEN(dev_priv) > 3 &&
5a21b665
DV
10501 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
10502 fb->pitches[0] != crtc->primary->fb->pitches[0]))
10503 return -EINVAL;
10504
10505 if (i915_terminally_wedged(&dev_priv->gpu_error))
10506 goto out_hang;
10507
10508 work = kzalloc(sizeof(*work), GFP_KERNEL);
10509 if (work == NULL)
10510 return -ENOMEM;
10511
10512 work->event = event;
10513 work->crtc = crtc;
10514 work->old_fb = old_fb;
10515 INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
10516
10517 ret = drm_crtc_vblank_get(crtc);
10518 if (ret)
10519 goto free_work;
10520
10521 /* We borrow the event spin lock for protecting flip_work */
10522 spin_lock_irq(&dev->event_lock);
10523 if (intel_crtc->flip_work) {
10524 /* Before declaring the flip queue wedged, check if
10525 * the hardware completed the operation behind our backs.
10526 */
10527 if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
10528 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
10529 page_flip_completed(intel_crtc);
10530 } else {
10531 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
10532 spin_unlock_irq(&dev->event_lock);
10533
10534 drm_crtc_vblank_put(crtc);
10535 kfree(work);
10536 return -EBUSY;
10537 }
10538 }
10539 intel_crtc->flip_work = work;
10540 spin_unlock_irq(&dev->event_lock);
10541
10542 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
10543 flush_workqueue(dev_priv->wq);
10544
10545 /* Reference the objects for the scheduled work. */
10546 drm_framebuffer_reference(work->old_fb);
5a21b665
DV
10547
10548 crtc->primary->fb = fb;
10549 update_state_fb(crtc->primary);
faf68d92 10550
25dc556a 10551 work->pending_flip_obj = i915_gem_object_get(obj);
5a21b665
DV
10552
10553 ret = i915_mutex_lock_interruptible(dev);
10554 if (ret)
10555 goto cleanup;
10556
8af29b0c
CW
10557 intel_crtc->reset_count = i915_reset_count(&dev_priv->gpu_error);
10558 if (i915_reset_in_progress_or_wedged(&dev_priv->gpu_error)) {
5a21b665 10559 ret = -EIO;
ddbb271a 10560 goto unlock;
5a21b665
DV
10561 }
10562
10563 atomic_inc(&intel_crtc->unpin_work_count);
10564
9beb5fea 10565 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
5a21b665
DV
10566 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
10567
920a14b2 10568 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
3b3f1650 10569 engine = dev_priv->engine[BCS];
bae781b2 10570 if (fb->modifier != old_fb->modifier)
5a21b665
DV
10571 /* vlv: DISPLAY_FLIP fails to change tiling */
10572 engine = NULL;
fd6b8f43 10573 } else if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
3b3f1650 10574 engine = dev_priv->engine[BCS];
6315b5d3 10575 } else if (INTEL_GEN(dev_priv) >= 7) {
d07f0e59 10576 engine = i915_gem_object_last_write_engine(obj);
5a21b665 10577 if (engine == NULL || engine->id != RCS)
3b3f1650 10578 engine = dev_priv->engine[BCS];
5a21b665 10579 } else {
3b3f1650 10580 engine = dev_priv->engine[RCS];
5a21b665
DV
10581 }
10582
10583 mmio_flip = use_mmio_flip(engine, obj);
10584
058d88c4
CW
10585 vma = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
10586 if (IS_ERR(vma)) {
10587 ret = PTR_ERR(vma);
5a21b665 10588 goto cleanup_pending;
058d88c4 10589 }
5a21b665 10590
be1e3415
CW
10591 work->old_vma = to_intel_plane_state(primary->state)->vma;
10592 to_intel_plane_state(primary->state)->vma = vma;
10593
10594 work->gtt_offset = i915_ggtt_offset(vma) + intel_crtc->dspaddr_offset;
5a21b665
DV
10595 work->rotation = crtc->primary->state->rotation;
10596
1f061316
PZ
10597 /*
10598 * There's the potential that the next frame will not be compatible with
10599 * FBC, so we want to call pre_update() before the actual page flip.
10600 * The problem is that pre_update() caches some information about the fb
10601 * object, so we want to do this only after the object is pinned. Let's
10602 * be on the safe side and do this immediately before scheduling the
10603 * flip.
10604 */
10605 intel_fbc_pre_update(intel_crtc, intel_crtc->config,
10606 to_intel_plane_state(primary->state));
10607
5a21b665
DV
10608 if (mmio_flip) {
10609 INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
6277c8d0 10610 queue_work(system_unbound_wq, &work->mmio_work);
5a21b665 10611 } else {
e8a9c58f
CW
10612 request = i915_gem_request_alloc(engine,
10613 dev_priv->kernel_context);
8e637178
CW
10614 if (IS_ERR(request)) {
10615 ret = PTR_ERR(request);
10616 goto cleanup_unpin;
10617 }
10618
a2bc4695 10619 ret = i915_gem_request_await_object(request, obj, false);
8e637178
CW
10620 if (ret)
10621 goto cleanup_request;
10622
5a21b665
DV
10623 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
10624 page_flip_flags);
10625 if (ret)
8e637178 10626 goto cleanup_request;
5a21b665
DV
10627
10628 intel_mark_page_flip_active(intel_crtc, work);
10629
8e637178 10630 work->flip_queued_req = i915_gem_request_get(request);
5a21b665
DV
10631 i915_add_request_no_flush(request);
10632 }
10633
92117f0b 10634 i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
5a21b665
DV
10635 i915_gem_track_fb(intel_fb_obj(old_fb), obj,
10636 to_intel_plane(primary)->frontbuffer_bit);
10637 mutex_unlock(&dev->struct_mutex);
10638
5748b6a1 10639 intel_frontbuffer_flip_prepare(to_i915(dev),
5a21b665
DV
10640 to_intel_plane(primary)->frontbuffer_bit);
10641
10642 trace_i915_flip_request(intel_crtc->plane, obj);
10643
10644 return 0;
10645
8e637178
CW
10646cleanup_request:
10647 i915_add_request_no_flush(request);
5a21b665 10648cleanup_unpin:
be1e3415
CW
10649 to_intel_plane_state(primary->state)->vma = work->old_vma;
10650 intel_unpin_fb_vma(vma);
5a21b665 10651cleanup_pending:
5a21b665 10652 atomic_dec(&intel_crtc->unpin_work_count);
ddbb271a 10653unlock:
5a21b665
DV
10654 mutex_unlock(&dev->struct_mutex);
10655cleanup:
10656 crtc->primary->fb = old_fb;
10657 update_state_fb(crtc->primary);
10658
f0cd5182 10659 i915_gem_object_put(obj);
5a21b665
DV
10660 drm_framebuffer_unreference(work->old_fb);
10661
10662 spin_lock_irq(&dev->event_lock);
10663 intel_crtc->flip_work = NULL;
10664 spin_unlock_irq(&dev->event_lock);
10665
10666 drm_crtc_vblank_put(crtc);
10667free_work:
10668 kfree(work);
10669
10670 if (ret == -EIO) {
10671 struct drm_atomic_state *state;
10672 struct drm_plane_state *plane_state;
10673
10674out_hang:
10675 state = drm_atomic_state_alloc(dev);
10676 if (!state)
10677 return -ENOMEM;
10678 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
10679
10680retry:
10681 plane_state = drm_atomic_get_plane_state(state, primary);
10682 ret = PTR_ERR_OR_ZERO(plane_state);
10683 if (!ret) {
10684 drm_atomic_set_fb_for_plane(plane_state, fb);
10685
10686 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
10687 if (!ret)
10688 ret = drm_atomic_commit(state);
10689 }
10690
10691 if (ret == -EDEADLK) {
10692 drm_modeset_backoff(state->acquire_ctx);
10693 drm_atomic_state_clear(state);
10694 goto retry;
10695 }
10696
0853695c 10697 drm_atomic_state_put(state);
5a21b665
DV
10698
10699 if (ret == 0 && event) {
10700 spin_lock_irq(&dev->event_lock);
10701 drm_crtc_send_vblank_event(crtc, event);
10702 spin_unlock_irq(&dev->event_lock);
10703 }
10704 }
10705 return ret;
10706}
10707
10708
10709/**
10710 * intel_wm_need_update - Check whether watermarks need updating
10711 * @plane: drm plane
10712 * @state: new plane state
10713 *
10714 * Check current plane state versus the new one to determine whether
10715 * watermarks need to be recalculated.
10716 *
10717 * Returns true or false.
10718 */
10719static bool intel_wm_need_update(struct drm_plane *plane,
10720 struct drm_plane_state *state)
10721{
10722 struct intel_plane_state *new = to_intel_plane_state(state);
10723 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
10724
10725 /* Update watermarks on tiling or size changes. */
936e71e3 10726 if (new->base.visible != cur->base.visible)
5a21b665
DV
10727 return true;
10728
10729 if (!cur->base.fb || !new->base.fb)
10730 return false;
10731
bae781b2 10732 if (cur->base.fb->modifier != new->base.fb->modifier ||
5a21b665 10733 cur->base.rotation != new->base.rotation ||
936e71e3
VS
10734 drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
10735 drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
10736 drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
10737 drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
5a21b665
DV
10738 return true;
10739
10740 return false;
10741}
10742
10743static bool needs_scaling(struct intel_plane_state *state)
10744{
936e71e3
VS
10745 int src_w = drm_rect_width(&state->base.src) >> 16;
10746 int src_h = drm_rect_height(&state->base.src) >> 16;
10747 int dst_w = drm_rect_width(&state->base.dst);
10748 int dst_h = drm_rect_height(&state->base.dst);
5a21b665
DV
10749
10750 return (src_w != dst_w || src_h != dst_h);
10751}
d21fbe87 10752
da20eabd
ML
10753int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
10754 struct drm_plane_state *plane_state)
10755{
ab1d3a0e 10756 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
da20eabd
ML
10757 struct drm_crtc *crtc = crtc_state->crtc;
10758 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10759 struct drm_plane *plane = plane_state->plane;
10760 struct drm_device *dev = crtc->dev;
ed4a6a7c 10761 struct drm_i915_private *dev_priv = to_i915(dev);
da20eabd
ML
10762 struct intel_plane_state *old_plane_state =
10763 to_intel_plane_state(plane->state);
da20eabd
ML
10764 bool mode_changed = needs_modeset(crtc_state);
10765 bool was_crtc_enabled = crtc->state->active;
10766 bool is_crtc_enabled = crtc_state->active;
da20eabd
ML
10767 bool turn_off, turn_on, visible, was_visible;
10768 struct drm_framebuffer *fb = plane_state->fb;
78108b7c 10769 int ret;
da20eabd 10770
55b8f2a7 10771 if (INTEL_GEN(dev_priv) >= 9 && plane->type != DRM_PLANE_TYPE_CURSOR) {
da20eabd
ML
10772 ret = skl_update_scaler_plane(
10773 to_intel_crtc_state(crtc_state),
10774 to_intel_plane_state(plane_state));
10775 if (ret)
10776 return ret;
10777 }
10778
936e71e3 10779 was_visible = old_plane_state->base.visible;
1d4258db 10780 visible = plane_state->visible;
da20eabd
ML
10781
10782 if (!was_crtc_enabled && WARN_ON(was_visible))
10783 was_visible = false;
10784
35c08f43
ML
10785 /*
10786 * Visibility is calculated as if the crtc was on, but
10787 * after scaler setup everything depends on it being off
10788 * when the crtc isn't active.
f818ffea
VS
10789 *
10790 * FIXME this is wrong for watermarks. Watermarks should also
10791 * be computed as if the pipe would be active. Perhaps move
10792 * per-plane wm computation to the .check_plane() hook, and
10793 * only combine the results from all planes in the current place?
35c08f43
ML
10794 */
10795 if (!is_crtc_enabled)
1d4258db 10796 plane_state->visible = visible = false;
da20eabd
ML
10797
10798 if (!was_visible && !visible)
10799 return 0;
10800
e8861675
ML
10801 if (fb != old_plane_state->base.fb)
10802 pipe_config->fb_changed = true;
10803
da20eabd
ML
10804 turn_off = was_visible && (!visible || mode_changed);
10805 turn_on = visible && (!was_visible || mode_changed);
10806
72660ce0 10807 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
78108b7c
VS
10808 intel_crtc->base.base.id,
10809 intel_crtc->base.name,
72660ce0
VS
10810 plane->base.id, plane->name,
10811 fb ? fb->base.id : -1);
da20eabd 10812
72660ce0
VS
10813 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
10814 plane->base.id, plane->name,
10815 was_visible, visible,
da20eabd
ML
10816 turn_off, turn_on, mode_changed);
10817
caed361d
VS
10818 if (turn_on) {
10819 pipe_config->update_wm_pre = true;
10820
10821 /* must disable cxsr around plane enable/disable */
10822 if (plane->type != DRM_PLANE_TYPE_CURSOR)
10823 pipe_config->disable_cxsr = true;
10824 } else if (turn_off) {
10825 pipe_config->update_wm_post = true;
92826fcd 10826
852eb00d 10827 /* must disable cxsr around plane enable/disable */
e8861675 10828 if (plane->type != DRM_PLANE_TYPE_CURSOR)
ab1d3a0e 10829 pipe_config->disable_cxsr = true;
852eb00d 10830 } else if (intel_wm_need_update(plane, plane_state)) {
caed361d
VS
10831 /* FIXME bollocks */
10832 pipe_config->update_wm_pre = true;
10833 pipe_config->update_wm_post = true;
852eb00d 10834 }
da20eabd 10835
ed4a6a7c 10836 /* Pre-gen9 platforms need two-step watermark updates */
caed361d 10837 if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
6315b5d3 10838 INTEL_GEN(dev_priv) < 9 && dev_priv->display.optimize_watermarks)
ed4a6a7c
MR
10839 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
10840
8be6ca85 10841 if (visible || was_visible)
cd202f69 10842 pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
a9ff8714 10843
31ae71fc
ML
10844 /*
10845 * WaCxSRDisabledForSpriteScaling:ivb
10846 *
10847 * cstate->update_wm was already set above, so this flag will
10848 * take effect when we commit and program watermarks.
10849 */
fd6b8f43 10850 if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev_priv) &&
31ae71fc
ML
10851 needs_scaling(to_intel_plane_state(plane_state)) &&
10852 !needs_scaling(old_plane_state))
10853 pipe_config->disable_lp_wm = true;
d21fbe87 10854
da20eabd
ML
10855 return 0;
10856}
10857
6d3a1ce7
ML
10858static bool encoders_cloneable(const struct intel_encoder *a,
10859 const struct intel_encoder *b)
10860{
10861 /* masks could be asymmetric, so check both ways */
10862 return a == b || (a->cloneable & (1 << b->type) &&
10863 b->cloneable & (1 << a->type));
10864}
10865
10866static bool check_single_encoder_cloning(struct drm_atomic_state *state,
10867 struct intel_crtc *crtc,
10868 struct intel_encoder *encoder)
10869{
10870 struct intel_encoder *source_encoder;
10871 struct drm_connector *connector;
10872 struct drm_connector_state *connector_state;
10873 int i;
10874
10875 for_each_connector_in_state(state, connector, connector_state, i) {
10876 if (connector_state->crtc != &crtc->base)
10877 continue;
10878
10879 source_encoder =
10880 to_intel_encoder(connector_state->best_encoder);
10881 if (!encoders_cloneable(encoder, source_encoder))
10882 return false;
10883 }
10884
10885 return true;
10886}
10887
6d3a1ce7
ML
10888static int intel_crtc_atomic_check(struct drm_crtc *crtc,
10889 struct drm_crtc_state *crtc_state)
10890{
cf5a15be 10891 struct drm_device *dev = crtc->dev;
fac5e23e 10892 struct drm_i915_private *dev_priv = to_i915(dev);
6d3a1ce7 10893 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cf5a15be
ML
10894 struct intel_crtc_state *pipe_config =
10895 to_intel_crtc_state(crtc_state);
6d3a1ce7 10896 struct drm_atomic_state *state = crtc_state->state;
4d20cd86 10897 int ret;
6d3a1ce7
ML
10898 bool mode_changed = needs_modeset(crtc_state);
10899
852eb00d 10900 if (mode_changed && !crtc_state->active)
caed361d 10901 pipe_config->update_wm_post = true;
eddfcbcd 10902
ad421372
ML
10903 if (mode_changed && crtc_state->enable &&
10904 dev_priv->display.crtc_compute_clock &&
8106ddbd 10905 !WARN_ON(pipe_config->shared_dpll)) {
ad421372
ML
10906 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
10907 pipe_config);
10908 if (ret)
10909 return ret;
10910 }
10911
82cf435b
LL
10912 if (crtc_state->color_mgmt_changed) {
10913 ret = intel_color_check(crtc, crtc_state);
10914 if (ret)
10915 return ret;
e7852a4b
LL
10916
10917 /*
10918 * Changing color management on Intel hardware is
10919 * handled as part of planes update.
10920 */
10921 crtc_state->planes_changed = true;
82cf435b
LL
10922 }
10923
e435d6e5 10924 ret = 0;
86c8bbbe 10925 if (dev_priv->display.compute_pipe_wm) {
e3bddded 10926 ret = dev_priv->display.compute_pipe_wm(pipe_config);
ed4a6a7c
MR
10927 if (ret) {
10928 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
10929 return ret;
10930 }
10931 }
10932
10933 if (dev_priv->display.compute_intermediate_wm &&
10934 !to_intel_atomic_state(state)->skip_intermediate_wm) {
10935 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
10936 return 0;
10937
10938 /*
10939 * Calculate 'intermediate' watermarks that satisfy both the
10940 * old state and the new state. We can program these
10941 * immediately.
10942 */
6315b5d3 10943 ret = dev_priv->display.compute_intermediate_wm(dev,
ed4a6a7c
MR
10944 intel_crtc,
10945 pipe_config);
10946 if (ret) {
10947 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
86c8bbbe 10948 return ret;
ed4a6a7c 10949 }
e3d5457c
VS
10950 } else if (dev_priv->display.compute_intermediate_wm) {
10951 if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
10952 pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
86c8bbbe
MR
10953 }
10954
6315b5d3 10955 if (INTEL_GEN(dev_priv) >= 9) {
e435d6e5
ML
10956 if (mode_changed)
10957 ret = skl_update_scaler_crtc(pipe_config);
10958
10959 if (!ret)
6ebc6923 10960 ret = intel_atomic_setup_scalers(dev_priv, intel_crtc,
e435d6e5
ML
10961 pipe_config);
10962 }
10963
10964 return ret;
6d3a1ce7
ML
10965}
10966
65b38e0d 10967static const struct drm_crtc_helper_funcs intel_helper_funcs = {
f6e5b160 10968 .mode_set_base_atomic = intel_pipe_set_base_atomic,
5a21b665
DV
10969 .atomic_begin = intel_begin_crtc_commit,
10970 .atomic_flush = intel_finish_crtc_commit,
6d3a1ce7 10971 .atomic_check = intel_crtc_atomic_check,
f6e5b160
CW
10972};
10973
d29b2f9d
ACO
10974static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
10975{
10976 struct intel_connector *connector;
10977
10978 for_each_intel_connector(dev, connector) {
8863dc7f
DV
10979 if (connector->base.state->crtc)
10980 drm_connector_unreference(&connector->base);
10981
d29b2f9d
ACO
10982 if (connector->base.encoder) {
10983 connector->base.state->best_encoder =
10984 connector->base.encoder;
10985 connector->base.state->crtc =
10986 connector->base.encoder->crtc;
8863dc7f
DV
10987
10988 drm_connector_reference(&connector->base);
d29b2f9d
ACO
10989 } else {
10990 connector->base.state->best_encoder = NULL;
10991 connector->base.state->crtc = NULL;
10992 }
10993 }
10994}
10995
050f7aeb 10996static void
eba905b2 10997connected_sink_compute_bpp(struct intel_connector *connector,
5cec258b 10998 struct intel_crtc_state *pipe_config)
050f7aeb 10999{
6a2a5c5d 11000 const struct drm_display_info *info = &connector->base.display_info;
050f7aeb
DV
11001 int bpp = pipe_config->pipe_bpp;
11002
11003 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
6a2a5c5d
VS
11004 connector->base.base.id,
11005 connector->base.name);
050f7aeb
DV
11006
11007 /* Don't use an invalid EDID bpc value */
6a2a5c5d 11008 if (info->bpc != 0 && info->bpc * 3 < bpp) {
050f7aeb 11009 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
6a2a5c5d
VS
11010 bpp, info->bpc * 3);
11011 pipe_config->pipe_bpp = info->bpc * 3;
050f7aeb
DV
11012 }
11013
196f954e 11014 /* Clamp bpp to 8 on screens without EDID 1.4 */
6a2a5c5d 11015 if (info->bpc == 0 && bpp > 24) {
196f954e
MK
11016 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
11017 bpp);
11018 pipe_config->pipe_bpp = 24;
050f7aeb
DV
11019 }
11020}
11021
4e53c2e0 11022static int
050f7aeb 11023compute_baseline_pipe_bpp(struct intel_crtc *crtc,
5cec258b 11024 struct intel_crtc_state *pipe_config)
4e53c2e0 11025{
9beb5fea 11026 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1486017f 11027 struct drm_atomic_state *state;
da3ced29
ACO
11028 struct drm_connector *connector;
11029 struct drm_connector_state *connector_state;
1486017f 11030 int bpp, i;
4e53c2e0 11031
9beb5fea
TU
11032 if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
11033 IS_CHERRYVIEW(dev_priv)))
4e53c2e0 11034 bpp = 10*3;
9beb5fea 11035 else if (INTEL_GEN(dev_priv) >= 5)
d328c9d7
DV
11036 bpp = 12*3;
11037 else
11038 bpp = 8*3;
11039
4e53c2e0 11040
4e53c2e0
DV
11041 pipe_config->pipe_bpp = bpp;
11042
1486017f
ACO
11043 state = pipe_config->base.state;
11044
4e53c2e0 11045 /* Clamp display bpp to EDID value */
da3ced29
ACO
11046 for_each_connector_in_state(state, connector, connector_state, i) {
11047 if (connector_state->crtc != &crtc->base)
4e53c2e0
DV
11048 continue;
11049
da3ced29
ACO
11050 connected_sink_compute_bpp(to_intel_connector(connector),
11051 pipe_config);
4e53c2e0
DV
11052 }
11053
11054 return bpp;
11055}
11056
644db711
DV
11057static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11058{
11059 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11060 "type: 0x%x flags: 0x%x\n",
1342830c 11061 mode->crtc_clock,
644db711
DV
11062 mode->crtc_hdisplay, mode->crtc_hsync_start,
11063 mode->crtc_hsync_end, mode->crtc_htotal,
11064 mode->crtc_vdisplay, mode->crtc_vsync_start,
11065 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
11066}
11067
f6982332
TU
11068static inline void
11069intel_dump_m_n_config(struct intel_crtc_state *pipe_config, char *id,
a4309657 11070 unsigned int lane_count, struct intel_link_m_n *m_n)
f6982332 11071{
a4309657
TU
11072 DRM_DEBUG_KMS("%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11073 id, lane_count,
f6982332
TU
11074 m_n->gmch_m, m_n->gmch_n,
11075 m_n->link_m, m_n->link_n, m_n->tu);
11076}
11077
c0b03411 11078static void intel_dump_pipe_config(struct intel_crtc *crtc,
5cec258b 11079 struct intel_crtc_state *pipe_config,
c0b03411
DV
11080 const char *context)
11081{
6a60cd87 11082 struct drm_device *dev = crtc->base.dev;
4f8036a2 11083 struct drm_i915_private *dev_priv = to_i915(dev);
6a60cd87
CK
11084 struct drm_plane *plane;
11085 struct intel_plane *intel_plane;
11086 struct intel_plane_state *state;
11087 struct drm_framebuffer *fb;
11088
66766e4f
TU
11089 DRM_DEBUG_KMS("[CRTC:%d:%s]%s\n",
11090 crtc->base.base.id, crtc->base.name, context);
c0b03411 11091
2c89429e
TU
11092 DRM_DEBUG_KMS("cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
11093 transcoder_name(pipe_config->cpu_transcoder),
c0b03411 11094 pipe_config->pipe_bpp, pipe_config->dither);
a4309657
TU
11095
11096 if (pipe_config->has_pch_encoder)
11097 intel_dump_m_n_config(pipe_config, "fdi",
11098 pipe_config->fdi_lanes,
11099 &pipe_config->fdi_m_n);
f6982332
TU
11100
11101 if (intel_crtc_has_dp_encoder(pipe_config)) {
a4309657
TU
11102 intel_dump_m_n_config(pipe_config, "dp m_n",
11103 pipe_config->lane_count, &pipe_config->dp_m_n);
d806e682
TU
11104 if (pipe_config->has_drrs)
11105 intel_dump_m_n_config(pipe_config, "dp m2_n2",
11106 pipe_config->lane_count,
11107 &pipe_config->dp_m2_n2);
f6982332 11108 }
b95af8be 11109
55072d19 11110 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
2c89429e 11111 pipe_config->has_audio, pipe_config->has_infoframe);
55072d19 11112
c0b03411 11113 DRM_DEBUG_KMS("requested mode:\n");
2d112de7 11114 drm_mode_debug_printmodeline(&pipe_config->base.mode);
c0b03411 11115 DRM_DEBUG_KMS("adjusted mode:\n");
2d112de7
ACO
11116 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
11117 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
a7d1b3f4 11118 DRM_DEBUG_KMS("port clock: %d, pipe src size: %dx%d, pixel rate %d\n",
2c89429e 11119 pipe_config->port_clock,
a7d1b3f4
VS
11120 pipe_config->pipe_src_w, pipe_config->pipe_src_h,
11121 pipe_config->pixel_rate);
dd2f616d
TU
11122
11123 if (INTEL_GEN(dev_priv) >= 9)
11124 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
11125 crtc->num_scalers,
11126 pipe_config->scaler_state.scaler_users,
11127 pipe_config->scaler_state.scaler_id);
a74f8375
TU
11128
11129 if (HAS_GMCH_DISPLAY(dev_priv))
11130 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
11131 pipe_config->gmch_pfit.control,
11132 pipe_config->gmch_pfit.pgm_ratios,
11133 pipe_config->gmch_pfit.lvds_border_bits);
11134 else
11135 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
11136 pipe_config->pch_pfit.pos,
11137 pipe_config->pch_pfit.size,
08c4d7fc 11138 enableddisabled(pipe_config->pch_pfit.enabled));
a74f8375 11139
2c89429e
TU
11140 DRM_DEBUG_KMS("ips: %i, double wide: %i\n",
11141 pipe_config->ips_enabled, pipe_config->double_wide);
6a60cd87 11142
f50b79f0 11143 intel_dpll_dump_hw_state(dev_priv, &pipe_config->dpll_hw_state);
415ff0f6 11144
6a60cd87
CK
11145 DRM_DEBUG_KMS("planes on this crtc\n");
11146 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
b3c11ac2 11147 struct drm_format_name_buf format_name;
6a60cd87
CK
11148 intel_plane = to_intel_plane(plane);
11149 if (intel_plane->pipe != crtc->pipe)
11150 continue;
11151
11152 state = to_intel_plane_state(plane->state);
11153 fb = state->base.fb;
11154 if (!fb) {
1d577e02
VS
11155 DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
11156 plane->base.id, plane->name, state->scaler_id);
6a60cd87
CK
11157 continue;
11158 }
11159
dd2f616d
TU
11160 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d, fb = %ux%u format = %s\n",
11161 plane->base.id, plane->name,
b3c11ac2 11162 fb->base.id, fb->width, fb->height,
438b74a5 11163 drm_get_format_name(fb->format->format, &format_name));
dd2f616d
TU
11164 if (INTEL_GEN(dev_priv) >= 9)
11165 DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
11166 state->scaler_id,
11167 state->base.src.x1 >> 16,
11168 state->base.src.y1 >> 16,
11169 drm_rect_width(&state->base.src) >> 16,
11170 drm_rect_height(&state->base.src) >> 16,
11171 state->base.dst.x1, state->base.dst.y1,
11172 drm_rect_width(&state->base.dst),
11173 drm_rect_height(&state->base.dst));
6a60cd87 11174 }
c0b03411
DV
11175}
11176
5448a00d 11177static bool check_digital_port_conflicts(struct drm_atomic_state *state)
00f0b378 11178{
5448a00d 11179 struct drm_device *dev = state->dev;
da3ced29 11180 struct drm_connector *connector;
00f0b378 11181 unsigned int used_ports = 0;
477321e0 11182 unsigned int used_mst_ports = 0;
00f0b378
VS
11183
11184 /*
11185 * Walk the connector list instead of the encoder
11186 * list to detect the problem on ddi platforms
11187 * where there's just one encoder per digital port.
11188 */
0bff4858
VS
11189 drm_for_each_connector(connector, dev) {
11190 struct drm_connector_state *connector_state;
11191 struct intel_encoder *encoder;
11192
11193 connector_state = drm_atomic_get_existing_connector_state(state, connector);
11194 if (!connector_state)
11195 connector_state = connector->state;
11196
5448a00d 11197 if (!connector_state->best_encoder)
00f0b378
VS
11198 continue;
11199
5448a00d
ACO
11200 encoder = to_intel_encoder(connector_state->best_encoder);
11201
11202 WARN_ON(!connector_state->crtc);
00f0b378
VS
11203
11204 switch (encoder->type) {
11205 unsigned int port_mask;
11206 case INTEL_OUTPUT_UNKNOWN:
4f8036a2 11207 if (WARN_ON(!HAS_DDI(to_i915(dev))))
00f0b378 11208 break;
cca0502b 11209 case INTEL_OUTPUT_DP:
00f0b378
VS
11210 case INTEL_OUTPUT_HDMI:
11211 case INTEL_OUTPUT_EDP:
11212 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
11213
11214 /* the same port mustn't appear more than once */
11215 if (used_ports & port_mask)
11216 return false;
11217
11218 used_ports |= port_mask;
477321e0
VS
11219 break;
11220 case INTEL_OUTPUT_DP_MST:
11221 used_mst_ports |=
11222 1 << enc_to_mst(&encoder->base)->primary->port;
11223 break;
00f0b378
VS
11224 default:
11225 break;
11226 }
11227 }
11228
477321e0
VS
11229 /* can't mix MST and SST/HDMI on the same port */
11230 if (used_ports & used_mst_ports)
11231 return false;
11232
00f0b378
VS
11233 return true;
11234}
11235
83a57153
ACO
11236static void
11237clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
11238{
11239 struct drm_crtc_state tmp_state;
663a3640 11240 struct intel_crtc_scaler_state scaler_state;
4978cc93 11241 struct intel_dpll_hw_state dpll_hw_state;
8106ddbd 11242 struct intel_shared_dpll *shared_dpll;
c4e2d043 11243 bool force_thru;
83a57153 11244
7546a384
ACO
11245 /* FIXME: before the switch to atomic started, a new pipe_config was
11246 * kzalloc'd. Code that depends on any field being zero should be
11247 * fixed, so that the crtc_state can be safely duplicated. For now,
11248 * only fields that are know to not cause problems are preserved. */
11249
83a57153 11250 tmp_state = crtc_state->base;
663a3640 11251 scaler_state = crtc_state->scaler_state;
4978cc93
ACO
11252 shared_dpll = crtc_state->shared_dpll;
11253 dpll_hw_state = crtc_state->dpll_hw_state;
c4e2d043 11254 force_thru = crtc_state->pch_pfit.force_thru;
4978cc93 11255
83a57153 11256 memset(crtc_state, 0, sizeof *crtc_state);
4978cc93 11257
83a57153 11258 crtc_state->base = tmp_state;
663a3640 11259 crtc_state->scaler_state = scaler_state;
4978cc93
ACO
11260 crtc_state->shared_dpll = shared_dpll;
11261 crtc_state->dpll_hw_state = dpll_hw_state;
c4e2d043 11262 crtc_state->pch_pfit.force_thru = force_thru;
83a57153
ACO
11263}
11264
548ee15b 11265static int
b8cecdf5 11266intel_modeset_pipe_config(struct drm_crtc *crtc,
b359283a 11267 struct intel_crtc_state *pipe_config)
ee7b9f93 11268{
b359283a 11269 struct drm_atomic_state *state = pipe_config->base.state;
7758a113 11270 struct intel_encoder *encoder;
da3ced29 11271 struct drm_connector *connector;
0b901879 11272 struct drm_connector_state *connector_state;
d328c9d7 11273 int base_bpp, ret = -EINVAL;
0b901879 11274 int i;
e29c22c0 11275 bool retry = true;
ee7b9f93 11276
83a57153 11277 clear_intel_crtc_state(pipe_config);
7758a113 11278
e143a21c
DV
11279 pipe_config->cpu_transcoder =
11280 (enum transcoder) to_intel_crtc(crtc)->pipe;
b8cecdf5 11281
2960bc9c
ID
11282 /*
11283 * Sanitize sync polarity flags based on requested ones. If neither
11284 * positive or negative polarity is requested, treat this as meaning
11285 * negative polarity.
11286 */
2d112de7 11287 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 11288 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
2d112de7 11289 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
2960bc9c 11290
2d112de7 11291 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 11292 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
2d112de7 11293 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
2960bc9c 11294
d328c9d7
DV
11295 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
11296 pipe_config);
11297 if (base_bpp < 0)
4e53c2e0
DV
11298 goto fail;
11299
e41a56be
VS
11300 /*
11301 * Determine the real pipe dimensions. Note that stereo modes can
11302 * increase the actual pipe size due to the frame doubling and
11303 * insertion of additional space for blanks between the frame. This
11304 * is stored in the crtc timings. We use the requested mode to do this
11305 * computation to clearly distinguish it from the adjusted mode, which
11306 * can be changed by the connectors in the below retry loop.
11307 */
196cd5d3 11308 drm_mode_get_hv_timing(&pipe_config->base.mode,
ecb7e16b
GP
11309 &pipe_config->pipe_src_w,
11310 &pipe_config->pipe_src_h);
e41a56be 11311
253c84c8
VS
11312 for_each_connector_in_state(state, connector, connector_state, i) {
11313 if (connector_state->crtc != crtc)
11314 continue;
11315
11316 encoder = to_intel_encoder(connector_state->best_encoder);
11317
e25148d0
VS
11318 if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
11319 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11320 goto fail;
11321 }
11322
253c84c8
VS
11323 /*
11324 * Determine output_types before calling the .compute_config()
11325 * hooks so that the hooks can use this information safely.
11326 */
11327 pipe_config->output_types |= 1 << encoder->type;
11328 }
11329
e29c22c0 11330encoder_retry:
ef1b460d 11331 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 11332 pipe_config->port_clock = 0;
ef1b460d 11333 pipe_config->pixel_multiplier = 1;
ff9a6750 11334
135c81b8 11335 /* Fill in default crtc timings, allow encoders to overwrite them. */
2d112de7
ACO
11336 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
11337 CRTC_STEREO_DOUBLE);
135c81b8 11338
7758a113
DV
11339 /* Pass our mode to the connectors and the CRTC to give them a chance to
11340 * adjust it according to limitations or connector properties, and also
11341 * a chance to reject the mode entirely.
47f1c6c9 11342 */
da3ced29 11343 for_each_connector_in_state(state, connector, connector_state, i) {
0b901879 11344 if (connector_state->crtc != crtc)
7758a113 11345 continue;
7ae89233 11346
0b901879
ACO
11347 encoder = to_intel_encoder(connector_state->best_encoder);
11348
0a478c27 11349 if (!(encoder->compute_config(encoder, pipe_config, connector_state))) {
efea6e8e 11350 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
DV
11351 goto fail;
11352 }
ee7b9f93 11353 }
47f1c6c9 11354
ff9a6750
DV
11355 /* Set default port clock if not overwritten by the encoder. Needs to be
11356 * done afterwards in case the encoder adjusts the mode. */
11357 if (!pipe_config->port_clock)
2d112de7 11358 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
241bfc38 11359 * pipe_config->pixel_multiplier;
ff9a6750 11360
a43f6e0f 11361 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 11362 if (ret < 0) {
7758a113
DV
11363 DRM_DEBUG_KMS("CRTC fixup failed\n");
11364 goto fail;
ee7b9f93 11365 }
e29c22c0
DV
11366
11367 if (ret == RETRY) {
11368 if (WARN(!retry, "loop in pipe configuration computation\n")) {
11369 ret = -EINVAL;
11370 goto fail;
11371 }
11372
11373 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
11374 retry = false;
11375 goto encoder_retry;
11376 }
11377
e8fa4270 11378 /* Dithering seems to not pass-through bits correctly when it should, so
611032bf
MN
11379 * only enable it on 6bpc panels and when its not a compliance
11380 * test requesting 6bpc video pattern.
11381 */
11382 pipe_config->dither = (pipe_config->pipe_bpp == 6*3) &&
11383 !pipe_config->dither_force_disable;
62f0ace5 11384 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
d328c9d7 11385 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
4e53c2e0 11386
7758a113 11387fail:
548ee15b 11388 return ret;
ee7b9f93 11389}
47f1c6c9 11390
ea9d758d 11391static void
4740b0f2 11392intel_modeset_update_crtc_state(struct drm_atomic_state *state)
ea9d758d 11393{
0a9ab303
ACO
11394 struct drm_crtc *crtc;
11395 struct drm_crtc_state *crtc_state;
8a75d157 11396 int i;
ea9d758d 11397
7668851f 11398 /* Double check state. */
8a75d157 11399 for_each_crtc_in_state(state, crtc, crtc_state, i) {
3cb480bc 11400 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
fc467a22
ML
11401
11402 /* Update hwmode for vblank functions */
11403 if (crtc->state->active)
11404 crtc->hwmode = crtc->state->adjusted_mode;
11405 else
11406 crtc->hwmode.crtc_clock = 0;
61067a5e
ML
11407
11408 /*
11409 * Update legacy state to satisfy fbc code. This can
11410 * be removed when fbc uses the atomic state.
11411 */
11412 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
11413 struct drm_plane_state *plane_state = crtc->primary->state;
11414
11415 crtc->primary->fb = plane_state->fb;
11416 crtc->x = plane_state->src_x >> 16;
11417 crtc->y = plane_state->src_y >> 16;
11418 }
ea9d758d 11419 }
ea9d758d
DV
11420}
11421
3bd26263 11422static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 11423{
3bd26263 11424 int diff;
f1f644dc
JB
11425
11426 if (clock1 == clock2)
11427 return true;
11428
11429 if (!clock1 || !clock2)
11430 return false;
11431
11432 diff = abs(clock1 - clock2);
11433
11434 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
11435 return true;
11436
11437 return false;
11438}
11439
cfb23ed6
ML
11440static bool
11441intel_compare_m_n(unsigned int m, unsigned int n,
11442 unsigned int m2, unsigned int n2,
11443 bool exact)
11444{
11445 if (m == m2 && n == n2)
11446 return true;
11447
11448 if (exact || !m || !n || !m2 || !n2)
11449 return false;
11450
11451 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
11452
31d10b57
ML
11453 if (n > n2) {
11454 while (n > n2) {
cfb23ed6
ML
11455 m2 <<= 1;
11456 n2 <<= 1;
11457 }
31d10b57
ML
11458 } else if (n < n2) {
11459 while (n < n2) {
cfb23ed6
ML
11460 m <<= 1;
11461 n <<= 1;
11462 }
11463 }
11464
31d10b57
ML
11465 if (n != n2)
11466 return false;
11467
11468 return intel_fuzzy_clock_check(m, m2);
cfb23ed6
ML
11469}
11470
11471static bool
11472intel_compare_link_m_n(const struct intel_link_m_n *m_n,
11473 struct intel_link_m_n *m2_n2,
11474 bool adjust)
11475{
11476 if (m_n->tu == m2_n2->tu &&
11477 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
11478 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
11479 intel_compare_m_n(m_n->link_m, m_n->link_n,
11480 m2_n2->link_m, m2_n2->link_n, !adjust)) {
11481 if (adjust)
11482 *m2_n2 = *m_n;
11483
11484 return true;
11485 }
11486
11487 return false;
11488}
11489
4e8048f8
TU
11490static void __printf(3, 4)
11491pipe_config_err(bool adjust, const char *name, const char *format, ...)
11492{
11493 char *level;
11494 unsigned int category;
11495 struct va_format vaf;
11496 va_list args;
11497
11498 if (adjust) {
11499 level = KERN_DEBUG;
11500 category = DRM_UT_KMS;
11501 } else {
11502 level = KERN_ERR;
11503 category = DRM_UT_NONE;
11504 }
11505
11506 va_start(args, format);
11507 vaf.fmt = format;
11508 vaf.va = &args;
11509
11510 drm_printk(level, category, "mismatch in %s %pV", name, &vaf);
11511
11512 va_end(args);
11513}
11514
0e8ffe1b 11515static bool
6315b5d3 11516intel_pipe_config_compare(struct drm_i915_private *dev_priv,
5cec258b 11517 struct intel_crtc_state *current_config,
cfb23ed6
ML
11518 struct intel_crtc_state *pipe_config,
11519 bool adjust)
0e8ffe1b 11520{
cfb23ed6
ML
11521 bool ret = true;
11522
66e985c0
DV
11523#define PIPE_CONF_CHECK_X(name) \
11524 if (current_config->name != pipe_config->name) { \
4e8048f8 11525 pipe_config_err(adjust, __stringify(name), \
66e985c0
DV
11526 "(expected 0x%08x, found 0x%08x)\n", \
11527 current_config->name, \
11528 pipe_config->name); \
cfb23ed6 11529 ret = false; \
66e985c0
DV
11530 }
11531
08a24034
DV
11532#define PIPE_CONF_CHECK_I(name) \
11533 if (current_config->name != pipe_config->name) { \
4e8048f8 11534 pipe_config_err(adjust, __stringify(name), \
08a24034
DV
11535 "(expected %i, found %i)\n", \
11536 current_config->name, \
11537 pipe_config->name); \
cfb23ed6
ML
11538 ret = false; \
11539 }
11540
8106ddbd
ACO
11541#define PIPE_CONF_CHECK_P(name) \
11542 if (current_config->name != pipe_config->name) { \
4e8048f8 11543 pipe_config_err(adjust, __stringify(name), \
8106ddbd
ACO
11544 "(expected %p, found %p)\n", \
11545 current_config->name, \
11546 pipe_config->name); \
11547 ret = false; \
11548 }
11549
cfb23ed6
ML
11550#define PIPE_CONF_CHECK_M_N(name) \
11551 if (!intel_compare_link_m_n(&current_config->name, \
11552 &pipe_config->name,\
11553 adjust)) { \
4e8048f8 11554 pipe_config_err(adjust, __stringify(name), \
cfb23ed6
ML
11555 "(expected tu %i gmch %i/%i link %i/%i, " \
11556 "found tu %i, gmch %i/%i link %i/%i)\n", \
11557 current_config->name.tu, \
11558 current_config->name.gmch_m, \
11559 current_config->name.gmch_n, \
11560 current_config->name.link_m, \
11561 current_config->name.link_n, \
11562 pipe_config->name.tu, \
11563 pipe_config->name.gmch_m, \
11564 pipe_config->name.gmch_n, \
11565 pipe_config->name.link_m, \
11566 pipe_config->name.link_n); \
11567 ret = false; \
11568 }
11569
55c561a7
DV
11570/* This is required for BDW+ where there is only one set of registers for
11571 * switching between high and low RR.
11572 * This macro can be used whenever a comparison has to be made between one
11573 * hw state and multiple sw state variables.
11574 */
cfb23ed6
ML
11575#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
11576 if (!intel_compare_link_m_n(&current_config->name, \
11577 &pipe_config->name, adjust) && \
11578 !intel_compare_link_m_n(&current_config->alt_name, \
11579 &pipe_config->name, adjust)) { \
4e8048f8 11580 pipe_config_err(adjust, __stringify(name), \
cfb23ed6
ML
11581 "(expected tu %i gmch %i/%i link %i/%i, " \
11582 "or tu %i gmch %i/%i link %i/%i, " \
11583 "found tu %i, gmch %i/%i link %i/%i)\n", \
11584 current_config->name.tu, \
11585 current_config->name.gmch_m, \
11586 current_config->name.gmch_n, \
11587 current_config->name.link_m, \
11588 current_config->name.link_n, \
11589 current_config->alt_name.tu, \
11590 current_config->alt_name.gmch_m, \
11591 current_config->alt_name.gmch_n, \
11592 current_config->alt_name.link_m, \
11593 current_config->alt_name.link_n, \
11594 pipe_config->name.tu, \
11595 pipe_config->name.gmch_m, \
11596 pipe_config->name.gmch_n, \
11597 pipe_config->name.link_m, \
11598 pipe_config->name.link_n); \
11599 ret = false; \
88adfff1
DV
11600 }
11601
1bd1bd80
DV
11602#define PIPE_CONF_CHECK_FLAGS(name, mask) \
11603 if ((current_config->name ^ pipe_config->name) & (mask)) { \
4e8048f8
TU
11604 pipe_config_err(adjust, __stringify(name), \
11605 "(%x) (expected %i, found %i)\n", \
11606 (mask), \
1bd1bd80
DV
11607 current_config->name & (mask), \
11608 pipe_config->name & (mask)); \
cfb23ed6 11609 ret = false; \
1bd1bd80
DV
11610 }
11611
5e550656
VS
11612#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
11613 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
4e8048f8 11614 pipe_config_err(adjust, __stringify(name), \
5e550656
VS
11615 "(expected %i, found %i)\n", \
11616 current_config->name, \
11617 pipe_config->name); \
cfb23ed6 11618 ret = false; \
5e550656
VS
11619 }
11620
bb760063
DV
11621#define PIPE_CONF_QUIRK(quirk) \
11622 ((current_config->quirks | pipe_config->quirks) & (quirk))
11623
eccb140b
DV
11624 PIPE_CONF_CHECK_I(cpu_transcoder);
11625
08a24034
DV
11626 PIPE_CONF_CHECK_I(has_pch_encoder);
11627 PIPE_CONF_CHECK_I(fdi_lanes);
cfb23ed6 11628 PIPE_CONF_CHECK_M_N(fdi_m_n);
08a24034 11629
90a6b7b0 11630 PIPE_CONF_CHECK_I(lane_count);
95a7a2ae 11631 PIPE_CONF_CHECK_X(lane_lat_optim_mask);
b95af8be 11632
6315b5d3 11633 if (INTEL_GEN(dev_priv) < 8) {
cfb23ed6
ML
11634 PIPE_CONF_CHECK_M_N(dp_m_n);
11635
cfb23ed6
ML
11636 if (current_config->has_drrs)
11637 PIPE_CONF_CHECK_M_N(dp_m2_n2);
11638 } else
11639 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
eb14cb74 11640
253c84c8 11641 PIPE_CONF_CHECK_X(output_types);
a65347ba 11642
2d112de7
ACO
11643 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
11644 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
11645 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
11646 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
11647 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
11648 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
1bd1bd80 11649
2d112de7
ACO
11650 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
11651 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
11652 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
11653 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
11654 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
11655 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
1bd1bd80 11656
c93f54cf 11657 PIPE_CONF_CHECK_I(pixel_multiplier);
6897b4b5 11658 PIPE_CONF_CHECK_I(has_hdmi_sink);
772c2a51 11659 if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
920a14b2 11660 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
b5a9fa09 11661 PIPE_CONF_CHECK_I(limited_color_range);
e43823ec 11662 PIPE_CONF_CHECK_I(has_infoframe);
6c49f241 11663
9ed109a7
DV
11664 PIPE_CONF_CHECK_I(has_audio);
11665
2d112de7 11666 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
1bd1bd80
DV
11667 DRM_MODE_FLAG_INTERLACE);
11668
bb760063 11669 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
2d112de7 11670 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 11671 DRM_MODE_FLAG_PHSYNC);
2d112de7 11672 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 11673 DRM_MODE_FLAG_NHSYNC);
2d112de7 11674 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 11675 DRM_MODE_FLAG_PVSYNC);
2d112de7 11676 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063
DV
11677 DRM_MODE_FLAG_NVSYNC);
11678 }
045ac3b5 11679
333b8ca8 11680 PIPE_CONF_CHECK_X(gmch_pfit.control);
e2ff2d4a 11681 /* pfit ratios are autocomputed by the hw on gen4+ */
6315b5d3 11682 if (INTEL_GEN(dev_priv) < 4)
7f7d8dd6 11683 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
333b8ca8 11684 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
9953599b 11685
bfd16b2a
ML
11686 if (!adjust) {
11687 PIPE_CONF_CHECK_I(pipe_src_w);
11688 PIPE_CONF_CHECK_I(pipe_src_h);
11689
11690 PIPE_CONF_CHECK_I(pch_pfit.enabled);
11691 if (current_config->pch_pfit.enabled) {
11692 PIPE_CONF_CHECK_X(pch_pfit.pos);
11693 PIPE_CONF_CHECK_X(pch_pfit.size);
11694 }
2fa2fe9a 11695
7aefe2b5 11696 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
a7d1b3f4 11697 PIPE_CONF_CHECK_CLOCK_FUZZY(pixel_rate);
7aefe2b5 11698 }
a1b2278e 11699
e59150dc 11700 /* BDW+ don't expose a synchronous way to read the state */
772c2a51 11701 if (IS_HASWELL(dev_priv))
e59150dc 11702 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 11703
282740f7
VS
11704 PIPE_CONF_CHECK_I(double_wide);
11705
8106ddbd 11706 PIPE_CONF_CHECK_P(shared_dpll);
66e985c0 11707 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 11708 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
DV
11709 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
11710 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
d452c5b6 11711 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
00490c22 11712 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
3f4cd19f
DL
11713 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
11714 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
11715 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
c0d43d62 11716
47eacbab
VS
11717 PIPE_CONF_CHECK_X(dsi_pll.ctrl);
11718 PIPE_CONF_CHECK_X(dsi_pll.div);
11719
9beb5fea 11720 if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
42571aef
VS
11721 PIPE_CONF_CHECK_I(pipe_bpp);
11722
2d112de7 11723 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
a9a7e98a 11724 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 11725
66e985c0 11726#undef PIPE_CONF_CHECK_X
08a24034 11727#undef PIPE_CONF_CHECK_I
8106ddbd 11728#undef PIPE_CONF_CHECK_P
1bd1bd80 11729#undef PIPE_CONF_CHECK_FLAGS
5e550656 11730#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 11731#undef PIPE_CONF_QUIRK
88adfff1 11732
cfb23ed6 11733 return ret;
0e8ffe1b
DV
11734}
11735
e3b247da
VS
11736static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
11737 const struct intel_crtc_state *pipe_config)
11738{
11739 if (pipe_config->has_pch_encoder) {
21a727b3 11740 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
e3b247da
VS
11741 &pipe_config->fdi_m_n);
11742 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
11743
11744 /*
11745 * FDI already provided one idea for the dotclock.
11746 * Yell if the encoder disagrees.
11747 */
11748 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
11749 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
11750 fdi_dotclock, dotclock);
11751 }
11752}
11753
c0ead703
ML
11754static void verify_wm_state(struct drm_crtc *crtc,
11755 struct drm_crtc_state *new_state)
08db6652 11756{
6315b5d3 11757 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
08db6652 11758 struct skl_ddb_allocation hw_ddb, *sw_ddb;
3de8a14c 11759 struct skl_pipe_wm hw_wm, *sw_wm;
11760 struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
11761 struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
e7c84544
ML
11762 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11763 const enum pipe pipe = intel_crtc->pipe;
3de8a14c 11764 int plane, level, max_level = ilk_wm_max_level(dev_priv);
08db6652 11765
6315b5d3 11766 if (INTEL_GEN(dev_priv) < 9 || !new_state->active)
08db6652
DL
11767 return;
11768
3de8a14c 11769 skl_pipe_wm_get_hw_state(crtc, &hw_wm);
03af79e0 11770 sw_wm = &to_intel_crtc_state(new_state)->wm.skl.optimal;
3de8a14c 11771
08db6652
DL
11772 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
11773 sw_ddb = &dev_priv->wm.skl_hw.ddb;
11774
e7c84544 11775 /* planes */
8b364b41 11776 for_each_universal_plane(dev_priv, pipe, plane) {
3de8a14c 11777 hw_plane_wm = &hw_wm.planes[plane];
11778 sw_plane_wm = &sw_wm->planes[plane];
08db6652 11779
3de8a14c 11780 /* Watermarks */
11781 for (level = 0; level <= max_level; level++) {
11782 if (skl_wm_level_equals(&hw_plane_wm->wm[level],
11783 &sw_plane_wm->wm[level]))
11784 continue;
11785
11786 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",
11787 pipe_name(pipe), plane + 1, level,
11788 sw_plane_wm->wm[level].plane_en,
11789 sw_plane_wm->wm[level].plane_res_b,
11790 sw_plane_wm->wm[level].plane_res_l,
11791 hw_plane_wm->wm[level].plane_en,
11792 hw_plane_wm->wm[level].plane_res_b,
11793 hw_plane_wm->wm[level].plane_res_l);
11794 }
08db6652 11795
3de8a14c 11796 if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
11797 &sw_plane_wm->trans_wm)) {
11798 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",
11799 pipe_name(pipe), plane + 1,
11800 sw_plane_wm->trans_wm.plane_en,
11801 sw_plane_wm->trans_wm.plane_res_b,
11802 sw_plane_wm->trans_wm.plane_res_l,
11803 hw_plane_wm->trans_wm.plane_en,
11804 hw_plane_wm->trans_wm.plane_res_b,
11805 hw_plane_wm->trans_wm.plane_res_l);
11806 }
11807
11808 /* DDB */
11809 hw_ddb_entry = &hw_ddb.plane[pipe][plane];
11810 sw_ddb_entry = &sw_ddb->plane[pipe][plane];
11811
11812 if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
faccd994 11813 DRM_ERROR("mismatch in DDB state pipe %c plane %d (expected (%u,%u), found (%u,%u))\n",
3de8a14c 11814 pipe_name(pipe), plane + 1,
11815 sw_ddb_entry->start, sw_ddb_entry->end,
11816 hw_ddb_entry->start, hw_ddb_entry->end);
11817 }
e7c84544 11818 }
08db6652 11819
27082493
L
11820 /*
11821 * cursor
11822 * If the cursor plane isn't active, we may not have updated it's ddb
11823 * allocation. In that case since the ddb allocation will be updated
11824 * once the plane becomes visible, we can skip this check
11825 */
11826 if (intel_crtc->cursor_addr) {
3de8a14c 11827 hw_plane_wm = &hw_wm.planes[PLANE_CURSOR];
11828 sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
11829
11830 /* Watermarks */
11831 for (level = 0; level <= max_level; level++) {
11832 if (skl_wm_level_equals(&hw_plane_wm->wm[level],
11833 &sw_plane_wm->wm[level]))
11834 continue;
11835
11836 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",
11837 pipe_name(pipe), level,
11838 sw_plane_wm->wm[level].plane_en,
11839 sw_plane_wm->wm[level].plane_res_b,
11840 sw_plane_wm->wm[level].plane_res_l,
11841 hw_plane_wm->wm[level].plane_en,
11842 hw_plane_wm->wm[level].plane_res_b,
11843 hw_plane_wm->wm[level].plane_res_l);
11844 }
11845
11846 if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
11847 &sw_plane_wm->trans_wm)) {
11848 DRM_ERROR("mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
11849 pipe_name(pipe),
11850 sw_plane_wm->trans_wm.plane_en,
11851 sw_plane_wm->trans_wm.plane_res_b,
11852 sw_plane_wm->trans_wm.plane_res_l,
11853 hw_plane_wm->trans_wm.plane_en,
11854 hw_plane_wm->trans_wm.plane_res_b,
11855 hw_plane_wm->trans_wm.plane_res_l);
11856 }
11857
11858 /* DDB */
11859 hw_ddb_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
11860 sw_ddb_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
27082493 11861
3de8a14c 11862 if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
faccd994 11863 DRM_ERROR("mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
27082493 11864 pipe_name(pipe),
3de8a14c 11865 sw_ddb_entry->start, sw_ddb_entry->end,
11866 hw_ddb_entry->start, hw_ddb_entry->end);
27082493 11867 }
08db6652
DL
11868 }
11869}
11870
91d1b4bd 11871static void
677100ce
ML
11872verify_connector_state(struct drm_device *dev,
11873 struct drm_atomic_state *state,
11874 struct drm_crtc *crtc)
8af6cf88 11875{
35dd3c64 11876 struct drm_connector *connector;
677100ce
ML
11877 struct drm_connector_state *old_conn_state;
11878 int i;
8af6cf88 11879
677100ce 11880 for_each_connector_in_state(state, connector, old_conn_state, i) {
35dd3c64
ML
11881 struct drm_encoder *encoder = connector->encoder;
11882 struct drm_connector_state *state = connector->state;
ad3c558f 11883
e7c84544
ML
11884 if (state->crtc != crtc)
11885 continue;
11886
5a21b665 11887 intel_connector_verify_state(to_intel_connector(connector));
8af6cf88 11888
ad3c558f 11889 I915_STATE_WARN(state->best_encoder != encoder,
35dd3c64 11890 "connector's atomic encoder doesn't match legacy encoder\n");
8af6cf88 11891 }
91d1b4bd
DV
11892}
11893
11894static void
c0ead703 11895verify_encoder_state(struct drm_device *dev)
91d1b4bd
DV
11896{
11897 struct intel_encoder *encoder;
11898 struct intel_connector *connector;
8af6cf88 11899
b2784e15 11900 for_each_intel_encoder(dev, encoder) {
8af6cf88 11901 bool enabled = false;
4d20cd86 11902 enum pipe pipe;
8af6cf88
DV
11903
11904 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
11905 encoder->base.base.id,
8e329a03 11906 encoder->base.name);
8af6cf88 11907
3a3371ff 11908 for_each_intel_connector(dev, connector) {
4d20cd86 11909 if (connector->base.state->best_encoder != &encoder->base)
8af6cf88
DV
11910 continue;
11911 enabled = true;
ad3c558f
ML
11912
11913 I915_STATE_WARN(connector->base.state->crtc !=
11914 encoder->base.crtc,
11915 "connector's crtc doesn't match encoder crtc\n");
8af6cf88 11916 }
0e32b39c 11917
e2c719b7 11918 I915_STATE_WARN(!!encoder->base.crtc != enabled,
8af6cf88
DV
11919 "encoder's enabled state mismatch "
11920 "(expected %i, found %i)\n",
11921 !!encoder->base.crtc, enabled);
7c60d198
ML
11922
11923 if (!encoder->base.crtc) {
4d20cd86 11924 bool active;
7c60d198 11925
4d20cd86
ML
11926 active = encoder->get_hw_state(encoder, &pipe);
11927 I915_STATE_WARN(active,
11928 "encoder detached but still enabled on pipe %c.\n",
11929 pipe_name(pipe));
7c60d198 11930 }
8af6cf88 11931 }
91d1b4bd
DV
11932}
11933
11934static void
c0ead703
ML
11935verify_crtc_state(struct drm_crtc *crtc,
11936 struct drm_crtc_state *old_crtc_state,
11937 struct drm_crtc_state *new_crtc_state)
91d1b4bd 11938{
e7c84544 11939 struct drm_device *dev = crtc->dev;
fac5e23e 11940 struct drm_i915_private *dev_priv = to_i915(dev);
91d1b4bd 11941 struct intel_encoder *encoder;
e7c84544
ML
11942 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11943 struct intel_crtc_state *pipe_config, *sw_config;
11944 struct drm_atomic_state *old_state;
11945 bool active;
045ac3b5 11946
e7c84544 11947 old_state = old_crtc_state->state;
ec2dc6a0 11948 __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
e7c84544
ML
11949 pipe_config = to_intel_crtc_state(old_crtc_state);
11950 memset(pipe_config, 0, sizeof(*pipe_config));
11951 pipe_config->base.crtc = crtc;
11952 pipe_config->base.state = old_state;
8af6cf88 11953
78108b7c 11954 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
8af6cf88 11955
e7c84544 11956 active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
d62cf62a 11957
e7c84544
ML
11958 /* hw state is inconsistent with the pipe quirk */
11959 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
11960 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
11961 active = new_crtc_state->active;
6c49f241 11962
e7c84544
ML
11963 I915_STATE_WARN(new_crtc_state->active != active,
11964 "crtc active state doesn't match with hw state "
11965 "(expected %i, found %i)\n", new_crtc_state->active, active);
0e8ffe1b 11966
e7c84544
ML
11967 I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
11968 "transitional active state does not match atomic hw state "
11969 "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
4d20cd86 11970
e7c84544
ML
11971 for_each_encoder_on_crtc(dev, crtc, encoder) {
11972 enum pipe pipe;
4d20cd86 11973
e7c84544
ML
11974 active = encoder->get_hw_state(encoder, &pipe);
11975 I915_STATE_WARN(active != new_crtc_state->active,
11976 "[ENCODER:%i] active %i with crtc active %i\n",
11977 encoder->base.base.id, active, new_crtc_state->active);
4d20cd86 11978
e7c84544
ML
11979 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
11980 "Encoder connected to wrong pipe %c\n",
11981 pipe_name(pipe));
4d20cd86 11982
253c84c8
VS
11983 if (active) {
11984 pipe_config->output_types |= 1 << encoder->type;
e7c84544 11985 encoder->get_config(encoder, pipe_config);
253c84c8 11986 }
e7c84544 11987 }
53d9f4e9 11988
a7d1b3f4
VS
11989 intel_crtc_compute_pixel_rate(pipe_config);
11990
e7c84544
ML
11991 if (!new_crtc_state->active)
11992 return;
cfb23ed6 11993
e7c84544 11994 intel_pipe_config_sanity_check(dev_priv, pipe_config);
e3b247da 11995
e7c84544 11996 sw_config = to_intel_crtc_state(crtc->state);
6315b5d3 11997 if (!intel_pipe_config_compare(dev_priv, sw_config,
e7c84544
ML
11998 pipe_config, false)) {
11999 I915_STATE_WARN(1, "pipe state doesn't match!\n");
12000 intel_dump_pipe_config(intel_crtc, pipe_config,
12001 "[hw state]");
12002 intel_dump_pipe_config(intel_crtc, sw_config,
12003 "[sw state]");
8af6cf88
DV
12004 }
12005}
12006
91d1b4bd 12007static void
c0ead703
ML
12008verify_single_dpll_state(struct drm_i915_private *dev_priv,
12009 struct intel_shared_dpll *pll,
12010 struct drm_crtc *crtc,
12011 struct drm_crtc_state *new_state)
91d1b4bd 12012{
91d1b4bd 12013 struct intel_dpll_hw_state dpll_hw_state;
e7c84544
ML
12014 unsigned crtc_mask;
12015 bool active;
5358901f 12016
e7c84544 12017 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
5358901f 12018
e7c84544 12019 DRM_DEBUG_KMS("%s\n", pll->name);
5358901f 12020
e7c84544 12021 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
5358901f 12022
e7c84544
ML
12023 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
12024 I915_STATE_WARN(!pll->on && pll->active_mask,
12025 "pll in active use but not on in sw tracking\n");
12026 I915_STATE_WARN(pll->on && !pll->active_mask,
12027 "pll is on but not used by any active crtc\n");
12028 I915_STATE_WARN(pll->on != active,
12029 "pll on state mismatch (expected %i, found %i)\n",
12030 pll->on, active);
12031 }
5358901f 12032
e7c84544 12033 if (!crtc) {
2c42e535 12034 I915_STATE_WARN(pll->active_mask & ~pll->state.crtc_mask,
e7c84544 12035 "more active pll users than references: %x vs %x\n",
2c42e535 12036 pll->active_mask, pll->state.crtc_mask);
5358901f 12037
e7c84544
ML
12038 return;
12039 }
12040
12041 crtc_mask = 1 << drm_crtc_index(crtc);
12042
12043 if (new_state->active)
12044 I915_STATE_WARN(!(pll->active_mask & crtc_mask),
12045 "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
12046 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
12047 else
12048 I915_STATE_WARN(pll->active_mask & crtc_mask,
12049 "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
12050 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
2dd66ebd 12051
2c42e535 12052 I915_STATE_WARN(!(pll->state.crtc_mask & crtc_mask),
e7c84544 12053 "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
2c42e535 12054 crtc_mask, pll->state.crtc_mask);
66e985c0 12055
2c42e535 12056 I915_STATE_WARN(pll->on && memcmp(&pll->state.hw_state,
e7c84544
ML
12057 &dpll_hw_state,
12058 sizeof(dpll_hw_state)),
12059 "pll hw state mismatch\n");
12060}
12061
12062static void
c0ead703
ML
12063verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
12064 struct drm_crtc_state *old_crtc_state,
12065 struct drm_crtc_state *new_crtc_state)
e7c84544 12066{
fac5e23e 12067 struct drm_i915_private *dev_priv = to_i915(dev);
e7c84544
ML
12068 struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
12069 struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
12070
12071 if (new_state->shared_dpll)
c0ead703 12072 verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
e7c84544
ML
12073
12074 if (old_state->shared_dpll &&
12075 old_state->shared_dpll != new_state->shared_dpll) {
12076 unsigned crtc_mask = 1 << drm_crtc_index(crtc);
12077 struct intel_shared_dpll *pll = old_state->shared_dpll;
12078
12079 I915_STATE_WARN(pll->active_mask & crtc_mask,
12080 "pll active mismatch (didn't expect pipe %c in active mask)\n",
12081 pipe_name(drm_crtc_index(crtc)));
2c42e535 12082 I915_STATE_WARN(pll->state.crtc_mask & crtc_mask,
e7c84544
ML
12083 "pll enabled crtcs mismatch (found %x in enabled mask)\n",
12084 pipe_name(drm_crtc_index(crtc)));
5358901f 12085 }
8af6cf88
DV
12086}
12087
e7c84544 12088static void
c0ead703 12089intel_modeset_verify_crtc(struct drm_crtc *crtc,
677100ce
ML
12090 struct drm_atomic_state *state,
12091 struct drm_crtc_state *old_state,
12092 struct drm_crtc_state *new_state)
e7c84544 12093{
5a21b665
DV
12094 if (!needs_modeset(new_state) &&
12095 !to_intel_crtc_state(new_state)->update_pipe)
12096 return;
12097
c0ead703 12098 verify_wm_state(crtc, new_state);
677100ce 12099 verify_connector_state(crtc->dev, state, crtc);
c0ead703
ML
12100 verify_crtc_state(crtc, old_state, new_state);
12101 verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
e7c84544
ML
12102}
12103
12104static void
c0ead703 12105verify_disabled_dpll_state(struct drm_device *dev)
e7c84544 12106{
fac5e23e 12107 struct drm_i915_private *dev_priv = to_i915(dev);
e7c84544
ML
12108 int i;
12109
12110 for (i = 0; i < dev_priv->num_shared_dpll; i++)
c0ead703 12111 verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
e7c84544
ML
12112}
12113
12114static void
677100ce
ML
12115intel_modeset_verify_disabled(struct drm_device *dev,
12116 struct drm_atomic_state *state)
e7c84544 12117{
c0ead703 12118 verify_encoder_state(dev);
677100ce 12119 verify_connector_state(dev, state, NULL);
c0ead703 12120 verify_disabled_dpll_state(dev);
e7c84544
ML
12121}
12122
80715b2f
VS
12123static void update_scanline_offset(struct intel_crtc *crtc)
12124{
4f8036a2 12125 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
80715b2f
VS
12126
12127 /*
12128 * The scanline counter increments at the leading edge of hsync.
12129 *
12130 * On most platforms it starts counting from vtotal-1 on the
12131 * first active line. That means the scanline counter value is
12132 * always one less than what we would expect. Ie. just after
12133 * start of vblank, which also occurs at start of hsync (on the
12134 * last active line), the scanline counter will read vblank_start-1.
12135 *
12136 * On gen2 the scanline counter starts counting from 1 instead
12137 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12138 * to keep the value positive), instead of adding one.
12139 *
12140 * On HSW+ the behaviour of the scanline counter depends on the output
12141 * type. For DP ports it behaves like most other platforms, but on HDMI
12142 * there's an extra 1 line difference. So we need to add two instead of
12143 * one to the value.
12144 */
4f8036a2 12145 if (IS_GEN2(dev_priv)) {
124abe07 12146 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
80715b2f
VS
12147 int vtotal;
12148
124abe07
VS
12149 vtotal = adjusted_mode->crtc_vtotal;
12150 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
80715b2f
VS
12151 vtotal /= 2;
12152
12153 crtc->scanline_offset = vtotal - 1;
4f8036a2 12154 } else if (HAS_DDI(dev_priv) &&
2d84d2b3 12155 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
80715b2f
VS
12156 crtc->scanline_offset = 2;
12157 } else
12158 crtc->scanline_offset = 1;
12159}
12160
ad421372 12161static void intel_modeset_clear_plls(struct drm_atomic_state *state)
ed6739ef 12162{
225da59b 12163 struct drm_device *dev = state->dev;
ed6739ef 12164 struct drm_i915_private *dev_priv = to_i915(dev);
0a9ab303
ACO
12165 struct drm_crtc *crtc;
12166 struct drm_crtc_state *crtc_state;
0a9ab303 12167 int i;
ed6739ef
ACO
12168
12169 if (!dev_priv->display.crtc_compute_clock)
ad421372 12170 return;
ed6739ef 12171
0a9ab303 12172 for_each_crtc_in_state(state, crtc, crtc_state, i) {
fb1a38a9 12173 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8106ddbd
ACO
12174 struct intel_shared_dpll *old_dpll =
12175 to_intel_crtc_state(crtc->state)->shared_dpll;
0a9ab303 12176
fb1a38a9 12177 if (!needs_modeset(crtc_state))
225da59b
ACO
12178 continue;
12179
8106ddbd 12180 to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
fb1a38a9 12181
8106ddbd 12182 if (!old_dpll)
fb1a38a9 12183 continue;
0a9ab303 12184
a1c414ee 12185 intel_release_shared_dpll(old_dpll, intel_crtc, state);
ad421372 12186 }
ed6739ef
ACO
12187}
12188
99d736a2
ML
12189/*
12190 * This implements the workaround described in the "notes" section of the mode
12191 * set sequence documentation. When going from no pipes or single pipe to
12192 * multiple pipes, and planes are enabled after the pipe, we need to wait at
12193 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
12194 */
12195static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
12196{
12197 struct drm_crtc_state *crtc_state;
12198 struct intel_crtc *intel_crtc;
12199 struct drm_crtc *crtc;
12200 struct intel_crtc_state *first_crtc_state = NULL;
12201 struct intel_crtc_state *other_crtc_state = NULL;
12202 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
12203 int i;
12204
12205 /* look at all crtc's that are going to be enabled in during modeset */
12206 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12207 intel_crtc = to_intel_crtc(crtc);
12208
12209 if (!crtc_state->active || !needs_modeset(crtc_state))
12210 continue;
12211
12212 if (first_crtc_state) {
12213 other_crtc_state = to_intel_crtc_state(crtc_state);
12214 break;
12215 } else {
12216 first_crtc_state = to_intel_crtc_state(crtc_state);
12217 first_pipe = intel_crtc->pipe;
12218 }
12219 }
12220
12221 /* No workaround needed? */
12222 if (!first_crtc_state)
12223 return 0;
12224
12225 /* w/a possibly needed, check how many crtc's are already enabled. */
12226 for_each_intel_crtc(state->dev, intel_crtc) {
12227 struct intel_crtc_state *pipe_config;
12228
12229 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
12230 if (IS_ERR(pipe_config))
12231 return PTR_ERR(pipe_config);
12232
12233 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
12234
12235 if (!pipe_config->base.active ||
12236 needs_modeset(&pipe_config->base))
12237 continue;
12238
12239 /* 2 or more enabled crtcs means no need for w/a */
12240 if (enabled_pipe != INVALID_PIPE)
12241 return 0;
12242
12243 enabled_pipe = intel_crtc->pipe;
12244 }
12245
12246 if (enabled_pipe != INVALID_PIPE)
12247 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
12248 else if (other_crtc_state)
12249 other_crtc_state->hsw_workaround_pipe = first_pipe;
12250
12251 return 0;
12252}
12253
8d96561a
VS
12254static int intel_lock_all_pipes(struct drm_atomic_state *state)
12255{
12256 struct drm_crtc *crtc;
12257
12258 /* Add all pipes to the state */
12259 for_each_crtc(state->dev, crtc) {
12260 struct drm_crtc_state *crtc_state;
12261
12262 crtc_state = drm_atomic_get_crtc_state(state, crtc);
12263 if (IS_ERR(crtc_state))
12264 return PTR_ERR(crtc_state);
12265 }
12266
12267 return 0;
12268}
12269
27c329ed
ML
12270static int intel_modeset_all_pipes(struct drm_atomic_state *state)
12271{
12272 struct drm_crtc *crtc;
27c329ed 12273
8d96561a
VS
12274 /*
12275 * Add all pipes to the state, and force
12276 * a modeset on all the active ones.
12277 */
27c329ed 12278 for_each_crtc(state->dev, crtc) {
9780aad5
VS
12279 struct drm_crtc_state *crtc_state;
12280 int ret;
12281
27c329ed
ML
12282 crtc_state = drm_atomic_get_crtc_state(state, crtc);
12283 if (IS_ERR(crtc_state))
12284 return PTR_ERR(crtc_state);
12285
12286 if (!crtc_state->active || needs_modeset(crtc_state))
12287 continue;
12288
12289 crtc_state->mode_changed = true;
12290
12291 ret = drm_atomic_add_affected_connectors(state, crtc);
12292 if (ret)
9780aad5 12293 return ret;
27c329ed
ML
12294
12295 ret = drm_atomic_add_affected_planes(state, crtc);
12296 if (ret)
9780aad5 12297 return ret;
27c329ed
ML
12298 }
12299
9780aad5 12300 return 0;
27c329ed
ML
12301}
12302
c347a676 12303static int intel_modeset_checks(struct drm_atomic_state *state)
054518dd 12304{
565602d7 12305 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 12306 struct drm_i915_private *dev_priv = to_i915(state->dev);
565602d7
ML
12307 struct drm_crtc *crtc;
12308 struct drm_crtc_state *crtc_state;
12309 int ret = 0, i;
054518dd 12310
b359283a
ML
12311 if (!check_digital_port_conflicts(state)) {
12312 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
12313 return -EINVAL;
12314 }
12315
565602d7
ML
12316 intel_state->modeset = true;
12317 intel_state->active_crtcs = dev_priv->active_crtcs;
bb0f4aab
VS
12318 intel_state->cdclk.logical = dev_priv->cdclk.logical;
12319 intel_state->cdclk.actual = dev_priv->cdclk.actual;
565602d7
ML
12320
12321 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12322 if (crtc_state->active)
12323 intel_state->active_crtcs |= 1 << i;
12324 else
12325 intel_state->active_crtcs &= ~(1 << i);
8b4a7d05
MR
12326
12327 if (crtc_state->active != crtc->state->active)
12328 intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
565602d7
ML
12329 }
12330
054518dd
ACO
12331 /*
12332 * See if the config requires any additional preparation, e.g.
12333 * to adjust global state with pipes off. We need to do this
12334 * here so we can get the modeset_pipe updated config for the new
12335 * mode set on this crtc. For other crtcs we need to use the
12336 * adjusted_mode bits in the crtc directly.
12337 */
27c329ed 12338 if (dev_priv->display.modeset_calc_cdclk) {
27c329ed 12339 ret = dev_priv->display.modeset_calc_cdclk(state);
c89e39f3
CT
12340 if (ret < 0)
12341 return ret;
27c329ed 12342
8d96561a 12343 /*
bb0f4aab 12344 * Writes to dev_priv->cdclk.logical must protected by
8d96561a
VS
12345 * holding all the crtc locks, even if we don't end up
12346 * touching the hardware
12347 */
bb0f4aab
VS
12348 if (!intel_cdclk_state_compare(&dev_priv->cdclk.logical,
12349 &intel_state->cdclk.logical)) {
8d96561a
VS
12350 ret = intel_lock_all_pipes(state);
12351 if (ret < 0)
12352 return ret;
12353 }
12354
12355 /* All pipes must be switched off while we change the cdclk. */
bb0f4aab
VS
12356 if (!intel_cdclk_state_compare(&dev_priv->cdclk.actual,
12357 &intel_state->cdclk.actual)) {
27c329ed 12358 ret = intel_modeset_all_pipes(state);
8d96561a
VS
12359 if (ret < 0)
12360 return ret;
12361 }
e8788cbc 12362
bb0f4aab
VS
12363 DRM_DEBUG_KMS("New cdclk calculated to be logical %u kHz, actual %u kHz\n",
12364 intel_state->cdclk.logical.cdclk,
12365 intel_state->cdclk.actual.cdclk);
e0ca7a6b 12366 } else {
bb0f4aab 12367 to_intel_atomic_state(state)->cdclk.logical = dev_priv->cdclk.logical;
e0ca7a6b 12368 }
054518dd 12369
ad421372 12370 intel_modeset_clear_plls(state);
054518dd 12371
565602d7 12372 if (IS_HASWELL(dev_priv))
ad421372 12373 return haswell_mode_set_planes_workaround(state);
99d736a2 12374
ad421372 12375 return 0;
c347a676
ACO
12376}
12377
aa363136
MR
12378/*
12379 * Handle calculation of various watermark data at the end of the atomic check
12380 * phase. The code here should be run after the per-crtc and per-plane 'check'
12381 * handlers to ensure that all derived state has been updated.
12382 */
55994c2c 12383static int calc_watermark_data(struct drm_atomic_state *state)
aa363136
MR
12384{
12385 struct drm_device *dev = state->dev;
98d39494 12386 struct drm_i915_private *dev_priv = to_i915(dev);
98d39494
MR
12387
12388 /* Is there platform-specific watermark information to calculate? */
12389 if (dev_priv->display.compute_global_watermarks)
55994c2c
MR
12390 return dev_priv->display.compute_global_watermarks(state);
12391
12392 return 0;
aa363136
MR
12393}
12394
74c090b1
ML
12395/**
12396 * intel_atomic_check - validate state object
12397 * @dev: drm device
12398 * @state: state to validate
12399 */
12400static int intel_atomic_check(struct drm_device *dev,
12401 struct drm_atomic_state *state)
c347a676 12402{
dd8b3bdb 12403 struct drm_i915_private *dev_priv = to_i915(dev);
aa363136 12404 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
c347a676
ACO
12405 struct drm_crtc *crtc;
12406 struct drm_crtc_state *crtc_state;
12407 int ret, i;
61333b60 12408 bool any_ms = false;
c347a676 12409
74c090b1 12410 ret = drm_atomic_helper_check_modeset(dev, state);
054518dd
ACO
12411 if (ret)
12412 return ret;
12413
c347a676 12414 for_each_crtc_in_state(state, crtc, crtc_state, i) {
cfb23ed6
ML
12415 struct intel_crtc_state *pipe_config =
12416 to_intel_crtc_state(crtc_state);
1ed51de9
DV
12417
12418 /* Catch I915_MODE_FLAG_INHERITED */
12419 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
12420 crtc_state->mode_changed = true;
cfb23ed6 12421
af4a879e 12422 if (!needs_modeset(crtc_state))
c347a676
ACO
12423 continue;
12424
af4a879e
DV
12425 if (!crtc_state->enable) {
12426 any_ms = true;
cfb23ed6 12427 continue;
af4a879e 12428 }
cfb23ed6 12429
26495481
DV
12430 /* FIXME: For only active_changed we shouldn't need to do any
12431 * state recomputation at all. */
12432
1ed51de9
DV
12433 ret = drm_atomic_add_affected_connectors(state, crtc);
12434 if (ret)
12435 return ret;
b359283a 12436
cfb23ed6 12437 ret = intel_modeset_pipe_config(crtc, pipe_config);
25aa1c39
ML
12438 if (ret) {
12439 intel_dump_pipe_config(to_intel_crtc(crtc),
12440 pipe_config, "[failed]");
c347a676 12441 return ret;
25aa1c39 12442 }
c347a676 12443
73831236 12444 if (i915.fastboot &&
6315b5d3 12445 intel_pipe_config_compare(dev_priv,
cfb23ed6 12446 to_intel_crtc_state(crtc->state),
1ed51de9 12447 pipe_config, true)) {
26495481 12448 crtc_state->mode_changed = false;
bfd16b2a 12449 to_intel_crtc_state(crtc_state)->update_pipe = true;
26495481
DV
12450 }
12451
af4a879e 12452 if (needs_modeset(crtc_state))
26495481 12453 any_ms = true;
cfb23ed6 12454
af4a879e
DV
12455 ret = drm_atomic_add_affected_planes(state, crtc);
12456 if (ret)
12457 return ret;
61333b60 12458
26495481
DV
12459 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
12460 needs_modeset(crtc_state) ?
12461 "[modeset]" : "[fastset]");
c347a676
ACO
12462 }
12463
61333b60
ML
12464 if (any_ms) {
12465 ret = intel_modeset_checks(state);
12466
12467 if (ret)
12468 return ret;
e0ca7a6b 12469 } else {
bb0f4aab 12470 intel_state->cdclk.logical = dev_priv->cdclk.logical;
e0ca7a6b 12471 }
76305b1a 12472
dd8b3bdb 12473 ret = drm_atomic_helper_check_planes(dev, state);
aa363136
MR
12474 if (ret)
12475 return ret;
12476
f51be2e0 12477 intel_fbc_choose_crtc(dev_priv, state);
55994c2c 12478 return calc_watermark_data(state);
054518dd
ACO
12479}
12480
5008e874 12481static int intel_atomic_prepare_commit(struct drm_device *dev,
d07f0e59 12482 struct drm_atomic_state *state)
5008e874 12483{
fac5e23e 12484 struct drm_i915_private *dev_priv = to_i915(dev);
5008e874
ML
12485 struct drm_crtc_state *crtc_state;
12486 struct drm_crtc *crtc;
12487 int i, ret;
12488
5a21b665
DV
12489 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12490 if (state->legacy_cursor_update)
a6747b73
ML
12491 continue;
12492
5a21b665
DV
12493 ret = intel_crtc_wait_for_pending_flips(crtc);
12494 if (ret)
12495 return ret;
5008e874 12496
5a21b665
DV
12497 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
12498 flush_workqueue(dev_priv->wq);
d55dbd06
ML
12499 }
12500
f935675f
ML
12501 ret = mutex_lock_interruptible(&dev->struct_mutex);
12502 if (ret)
12503 return ret;
12504
5008e874 12505 ret = drm_atomic_helper_prepare_planes(dev, state);
f7e5838b 12506 mutex_unlock(&dev->struct_mutex);
7580d774 12507
5008e874
ML
12508 return ret;
12509}
12510
a2991414
ML
12511u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
12512{
12513 struct drm_device *dev = crtc->base.dev;
12514
12515 if (!dev->max_vblank_count)
12516 return drm_accurate_vblank_count(&crtc->base);
12517
12518 return dev->driver->get_vblank_counter(dev, crtc->pipe);
12519}
12520
5a21b665
DV
12521static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
12522 struct drm_i915_private *dev_priv,
12523 unsigned crtc_mask)
e8861675 12524{
5a21b665
DV
12525 unsigned last_vblank_count[I915_MAX_PIPES];
12526 enum pipe pipe;
12527 int ret;
e8861675 12528
5a21b665
DV
12529 if (!crtc_mask)
12530 return;
e8861675 12531
5a21b665 12532 for_each_pipe(dev_priv, pipe) {
98187836
VS
12533 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
12534 pipe);
e8861675 12535
5a21b665 12536 if (!((1 << pipe) & crtc_mask))
e8861675
ML
12537 continue;
12538
e2af48c6 12539 ret = drm_crtc_vblank_get(&crtc->base);
5a21b665
DV
12540 if (WARN_ON(ret != 0)) {
12541 crtc_mask &= ~(1 << pipe);
12542 continue;
e8861675
ML
12543 }
12544
e2af48c6 12545 last_vblank_count[pipe] = drm_crtc_vblank_count(&crtc->base);
e8861675
ML
12546 }
12547
5a21b665 12548 for_each_pipe(dev_priv, pipe) {
98187836
VS
12549 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
12550 pipe);
5a21b665 12551 long lret;
e8861675 12552
5a21b665
DV
12553 if (!((1 << pipe) & crtc_mask))
12554 continue;
d55dbd06 12555
5a21b665
DV
12556 lret = wait_event_timeout(dev->vblank[pipe].queue,
12557 last_vblank_count[pipe] !=
e2af48c6 12558 drm_crtc_vblank_count(&crtc->base),
5a21b665 12559 msecs_to_jiffies(50));
d55dbd06 12560
5a21b665 12561 WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
d55dbd06 12562
e2af48c6 12563 drm_crtc_vblank_put(&crtc->base);
d55dbd06
ML
12564 }
12565}
12566
5a21b665 12567static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
a6747b73 12568{
5a21b665
DV
12569 /* fb updated, need to unpin old fb */
12570 if (crtc_state->fb_changed)
12571 return true;
a6747b73 12572
5a21b665
DV
12573 /* wm changes, need vblank before final wm's */
12574 if (crtc_state->update_wm_post)
12575 return true;
a6747b73 12576
5a21b665
DV
12577 /*
12578 * cxsr is re-enabled after vblank.
12579 * This is already handled by crtc_state->update_wm_post,
12580 * but added for clarity.
12581 */
12582 if (crtc_state->disable_cxsr)
12583 return true;
a6747b73 12584
5a21b665 12585 return false;
e8861675
ML
12586}
12587
896e5bb0
L
12588static void intel_update_crtc(struct drm_crtc *crtc,
12589 struct drm_atomic_state *state,
12590 struct drm_crtc_state *old_crtc_state,
12591 unsigned int *crtc_vblank_mask)
12592{
12593 struct drm_device *dev = crtc->dev;
12594 struct drm_i915_private *dev_priv = to_i915(dev);
12595 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12596 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc->state);
12597 bool modeset = needs_modeset(crtc->state);
12598
12599 if (modeset) {
12600 update_scanline_offset(intel_crtc);
12601 dev_priv->display.crtc_enable(pipe_config, state);
12602 } else {
12603 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
12604 }
12605
12606 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
12607 intel_fbc_enable(
12608 intel_crtc, pipe_config,
12609 to_intel_plane_state(crtc->primary->state));
12610 }
12611
12612 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
12613
12614 if (needs_vblank_wait(pipe_config))
12615 *crtc_vblank_mask |= drm_crtc_mask(crtc);
12616}
12617
12618static void intel_update_crtcs(struct drm_atomic_state *state,
12619 unsigned int *crtc_vblank_mask)
12620{
12621 struct drm_crtc *crtc;
12622 struct drm_crtc_state *old_crtc_state;
12623 int i;
12624
12625 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
12626 if (!crtc->state->active)
12627 continue;
12628
12629 intel_update_crtc(crtc, state, old_crtc_state,
12630 crtc_vblank_mask);
12631 }
12632}
12633
27082493
L
12634static void skl_update_crtcs(struct drm_atomic_state *state,
12635 unsigned int *crtc_vblank_mask)
12636{
0f0f74bc 12637 struct drm_i915_private *dev_priv = to_i915(state->dev);
27082493
L
12638 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
12639 struct drm_crtc *crtc;
ce0ba283 12640 struct intel_crtc *intel_crtc;
27082493 12641 struct drm_crtc_state *old_crtc_state;
ce0ba283 12642 struct intel_crtc_state *cstate;
27082493
L
12643 unsigned int updated = 0;
12644 bool progress;
12645 enum pipe pipe;
5eff503b
ML
12646 int i;
12647
12648 const struct skl_ddb_entry *entries[I915_MAX_PIPES] = {};
12649
12650 for_each_crtc_in_state(state, crtc, old_crtc_state, i)
12651 /* ignore allocations for crtc's that have been turned off. */
12652 if (crtc->state->active)
12653 entries[i] = &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb;
27082493
L
12654
12655 /*
12656 * Whenever the number of active pipes changes, we need to make sure we
12657 * update the pipes in the right order so that their ddb allocations
12658 * never overlap with eachother inbetween CRTC updates. Otherwise we'll
12659 * cause pipe underruns and other bad stuff.
12660 */
12661 do {
27082493
L
12662 progress = false;
12663
12664 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
12665 bool vbl_wait = false;
12666 unsigned int cmask = drm_crtc_mask(crtc);
ce0ba283
L
12667
12668 intel_crtc = to_intel_crtc(crtc);
12669 cstate = to_intel_crtc_state(crtc->state);
12670 pipe = intel_crtc->pipe;
27082493 12671
5eff503b 12672 if (updated & cmask || !cstate->base.active)
27082493 12673 continue;
5eff503b
ML
12674
12675 if (skl_ddb_allocation_overlaps(entries, &cstate->wm.skl.ddb, i))
27082493
L
12676 continue;
12677
12678 updated |= cmask;
5eff503b 12679 entries[i] = &cstate->wm.skl.ddb;
27082493
L
12680
12681 /*
12682 * If this is an already active pipe, it's DDB changed,
12683 * and this isn't the last pipe that needs updating
12684 * then we need to wait for a vblank to pass for the
12685 * new ddb allocation to take effect.
12686 */
ce0ba283 12687 if (!skl_ddb_entry_equal(&cstate->wm.skl.ddb,
512b5527 12688 &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb) &&
27082493
L
12689 !crtc->state->active_changed &&
12690 intel_state->wm_results.dirty_pipes != updated)
12691 vbl_wait = true;
12692
12693 intel_update_crtc(crtc, state, old_crtc_state,
12694 crtc_vblank_mask);
12695
12696 if (vbl_wait)
0f0f74bc 12697 intel_wait_for_vblank(dev_priv, pipe);
27082493
L
12698
12699 progress = true;
12700 }
12701 } while (progress);
12702}
12703
ba318c61
CW
12704static void intel_atomic_helper_free_state(struct drm_i915_private *dev_priv)
12705{
12706 struct intel_atomic_state *state, *next;
12707 struct llist_node *freed;
12708
12709 freed = llist_del_all(&dev_priv->atomic_helper.free_list);
12710 llist_for_each_entry_safe(state, next, freed, freed)
12711 drm_atomic_state_put(&state->base);
12712}
12713
12714static void intel_atomic_helper_free_state_worker(struct work_struct *work)
12715{
12716 struct drm_i915_private *dev_priv =
12717 container_of(work, typeof(*dev_priv), atomic_helper.free_work);
12718
12719 intel_atomic_helper_free_state(dev_priv);
12720}
12721
94f05024 12722static void intel_atomic_commit_tail(struct drm_atomic_state *state)
a6778b3c 12723{
94f05024 12724 struct drm_device *dev = state->dev;
565602d7 12725 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 12726 struct drm_i915_private *dev_priv = to_i915(dev);
29ceb0e6 12727 struct drm_crtc_state *old_crtc_state;
7580d774 12728 struct drm_crtc *crtc;
5a21b665 12729 struct intel_crtc_state *intel_cstate;
5a21b665 12730 bool hw_check = intel_state->modeset;
d8fc70b7 12731 u64 put_domains[I915_MAX_PIPES] = {};
5a21b665 12732 unsigned crtc_vblank_mask = 0;
e95433c7 12733 int i;
a6778b3c 12734
ea0000f0
DV
12735 drm_atomic_helper_wait_for_dependencies(state);
12736
c3b32658 12737 if (intel_state->modeset)
5a21b665 12738 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
565602d7 12739
29ceb0e6 12740 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
a539205a
ML
12741 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12742
5a21b665
DV
12743 if (needs_modeset(crtc->state) ||
12744 to_intel_crtc_state(crtc->state)->update_pipe) {
12745 hw_check = true;
12746
12747 put_domains[to_intel_crtc(crtc)->pipe] =
12748 modeset_get_crtc_power_domains(crtc,
12749 to_intel_crtc_state(crtc->state));
12750 }
12751
61333b60
ML
12752 if (!needs_modeset(crtc->state))
12753 continue;
12754
29ceb0e6 12755 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
460da916 12756
29ceb0e6
VS
12757 if (old_crtc_state->active) {
12758 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
4a806558 12759 dev_priv->display.crtc_disable(to_intel_crtc_state(old_crtc_state), state);
eddfcbcd 12760 intel_crtc->active = false;
58f9c0bc 12761 intel_fbc_disable(intel_crtc);
eddfcbcd 12762 intel_disable_shared_dpll(intel_crtc);
9bbc8258
VS
12763
12764 /*
12765 * Underruns don't always raise
12766 * interrupts, so check manually.
12767 */
12768 intel_check_cpu_fifo_underruns(dev_priv);
12769 intel_check_pch_fifo_underruns(dev_priv);
b9001114 12770
e62929b3
ML
12771 if (!crtc->state->active) {
12772 /*
12773 * Make sure we don't call initial_watermarks
12774 * for ILK-style watermark updates.
12775 */
12776 if (dev_priv->display.atomic_update_watermarks)
12777 dev_priv->display.initial_watermarks(intel_state,
12778 to_intel_crtc_state(crtc->state));
12779 else
12780 intel_update_watermarks(intel_crtc);
12781 }
a539205a 12782 }
b8cecdf5 12783 }
7758a113 12784
ea9d758d
DV
12785 /* Only after disabling all output pipelines that will be changed can we
12786 * update the the output configuration. */
4740b0f2 12787 intel_modeset_update_crtc_state(state);
f6e5b160 12788
565602d7 12789 if (intel_state->modeset) {
4740b0f2 12790 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
33c8df89 12791
b0587e4d 12792 intel_set_cdclk(dev_priv, &dev_priv->cdclk.actual);
f6d1973d 12793
656d1b89
L
12794 /*
12795 * SKL workaround: bspec recommends we disable the SAGV when we
12796 * have more then one pipe enabled
12797 */
56feca91 12798 if (!intel_can_enable_sagv(state))
16dcdc4e 12799 intel_disable_sagv(dev_priv);
656d1b89 12800
677100ce 12801 intel_modeset_verify_disabled(dev, state);
4740b0f2 12802 }
47fab737 12803
896e5bb0 12804 /* Complete the events for pipes that have now been disabled */
29ceb0e6 12805 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
f6ac4b2a 12806 bool modeset = needs_modeset(crtc->state);
80715b2f 12807
1f7528c4
DV
12808 /* Complete events for now disable pipes here. */
12809 if (modeset && !crtc->state->active && crtc->state->event) {
12810 spin_lock_irq(&dev->event_lock);
12811 drm_crtc_send_vblank_event(crtc, crtc->state->event);
12812 spin_unlock_irq(&dev->event_lock);
12813
12814 crtc->state->event = NULL;
12815 }
177246a8
MR
12816 }
12817
896e5bb0
L
12818 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
12819 dev_priv->display.update_crtcs(state, &crtc_vblank_mask);
12820
94f05024
DV
12821 /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
12822 * already, but still need the state for the delayed optimization. To
12823 * fix this:
12824 * - wrap the optimization/post_plane_update stuff into a per-crtc work.
12825 * - schedule that vblank worker _before_ calling hw_done
12826 * - at the start of commit_tail, cancel it _synchrously
12827 * - switch over to the vblank wait helper in the core after that since
12828 * we don't need out special handling any more.
12829 */
5a21b665
DV
12830 if (!state->legacy_cursor_update)
12831 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
12832
12833 /*
12834 * Now that the vblank has passed, we can go ahead and program the
12835 * optimal watermarks on platforms that need two-step watermark
12836 * programming.
12837 *
12838 * TODO: Move this (and other cleanup) to an async worker eventually.
12839 */
12840 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
12841 intel_cstate = to_intel_crtc_state(crtc->state);
12842
12843 if (dev_priv->display.optimize_watermarks)
ccf010fb
ML
12844 dev_priv->display.optimize_watermarks(intel_state,
12845 intel_cstate);
5a21b665
DV
12846 }
12847
12848 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
12849 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
12850
12851 if (put_domains[i])
12852 modeset_put_power_domains(dev_priv, put_domains[i]);
12853
677100ce 12854 intel_modeset_verify_crtc(crtc, state, old_crtc_state, crtc->state);
5a21b665
DV
12855 }
12856
56feca91 12857 if (intel_state->modeset && intel_can_enable_sagv(state))
16dcdc4e 12858 intel_enable_sagv(dev_priv);
656d1b89 12859
94f05024
DV
12860 drm_atomic_helper_commit_hw_done(state);
12861
5a21b665
DV
12862 if (intel_state->modeset)
12863 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
12864
12865 mutex_lock(&dev->struct_mutex);
12866 drm_atomic_helper_cleanup_planes(dev, state);
12867 mutex_unlock(&dev->struct_mutex);
12868
ea0000f0
DV
12869 drm_atomic_helper_commit_cleanup_done(state);
12870
0853695c 12871 drm_atomic_state_put(state);
f30da187 12872
75714940
MK
12873 /* As one of the primary mmio accessors, KMS has a high likelihood
12874 * of triggering bugs in unclaimed access. After we finish
12875 * modesetting, see if an error has been flagged, and if so
12876 * enable debugging for the next modeset - and hope we catch
12877 * the culprit.
12878 *
12879 * XXX note that we assume display power is on at this point.
12880 * This might hold true now but we need to add pm helper to check
12881 * unclaimed only when the hardware is on, as atomic commits
12882 * can happen also when the device is completely off.
12883 */
12884 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
ba318c61
CW
12885
12886 intel_atomic_helper_free_state(dev_priv);
94f05024
DV
12887}
12888
12889static void intel_atomic_commit_work(struct work_struct *work)
12890{
c004a90b
CW
12891 struct drm_atomic_state *state =
12892 container_of(work, struct drm_atomic_state, commit_work);
12893
94f05024
DV
12894 intel_atomic_commit_tail(state);
12895}
12896
c004a90b
CW
12897static int __i915_sw_fence_call
12898intel_atomic_commit_ready(struct i915_sw_fence *fence,
12899 enum i915_sw_fence_notify notify)
12900{
12901 struct intel_atomic_state *state =
12902 container_of(fence, struct intel_atomic_state, commit_ready);
12903
12904 switch (notify) {
12905 case FENCE_COMPLETE:
12906 if (state->base.commit_work.func)
12907 queue_work(system_unbound_wq, &state->base.commit_work);
12908 break;
12909
12910 case FENCE_FREE:
eb955eee
CW
12911 {
12912 struct intel_atomic_helper *helper =
12913 &to_i915(state->base.dev)->atomic_helper;
12914
12915 if (llist_add(&state->freed, &helper->free_list))
12916 schedule_work(&helper->free_work);
12917 break;
12918 }
c004a90b
CW
12919 }
12920
12921 return NOTIFY_DONE;
12922}
12923
6c9c1b38
DV
12924static void intel_atomic_track_fbs(struct drm_atomic_state *state)
12925{
12926 struct drm_plane_state *old_plane_state;
12927 struct drm_plane *plane;
6c9c1b38
DV
12928 int i;
12929
faf5bf0a
CW
12930 for_each_plane_in_state(state, plane, old_plane_state, i)
12931 i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
12932 intel_fb_obj(plane->state->fb),
12933 to_intel_plane(plane)->frontbuffer_bit);
6c9c1b38
DV
12934}
12935
94f05024
DV
12936/**
12937 * intel_atomic_commit - commit validated state object
12938 * @dev: DRM device
12939 * @state: the top-level driver state object
12940 * @nonblock: nonblocking commit
12941 *
12942 * This function commits a top-level state object that has been validated
12943 * with drm_atomic_helper_check().
12944 *
94f05024
DV
12945 * RETURNS
12946 * Zero for success or -errno.
12947 */
12948static int intel_atomic_commit(struct drm_device *dev,
12949 struct drm_atomic_state *state,
12950 bool nonblock)
12951{
12952 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 12953 struct drm_i915_private *dev_priv = to_i915(dev);
94f05024
DV
12954 int ret = 0;
12955
a5509abd
VS
12956 /*
12957 * The intel_legacy_cursor_update() fast path takes care
12958 * of avoiding the vblank waits for simple cursor
12959 * movement and flips. For cursor on/off and size changes,
12960 * we want to perform the vblank waits so that watermark
12961 * updates happen during the correct frames. Gen9+ have
12962 * double buffered watermarks and so shouldn't need this.
12963 */
12964 if (INTEL_GEN(dev_priv) < 9)
12965 state->legacy_cursor_update = false;
12966
94f05024
DV
12967 ret = drm_atomic_helper_setup_commit(state, nonblock);
12968 if (ret)
12969 return ret;
12970
c004a90b
CW
12971 drm_atomic_state_get(state);
12972 i915_sw_fence_init(&intel_state->commit_ready,
12973 intel_atomic_commit_ready);
94f05024 12974
d07f0e59 12975 ret = intel_atomic_prepare_commit(dev, state);
94f05024
DV
12976 if (ret) {
12977 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
c004a90b 12978 i915_sw_fence_commit(&intel_state->commit_ready);
94f05024
DV
12979 return ret;
12980 }
12981
12982 drm_atomic_helper_swap_state(state, true);
12983 dev_priv->wm.distrust_bios_wm = false;
3c0fb588 12984 intel_shared_dpll_swap_state(state);
6c9c1b38 12985 intel_atomic_track_fbs(state);
94f05024 12986
c3b32658
ML
12987 if (intel_state->modeset) {
12988 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
12989 sizeof(intel_state->min_pixclk));
12990 dev_priv->active_crtcs = intel_state->active_crtcs;
bb0f4aab
VS
12991 dev_priv->cdclk.logical = intel_state->cdclk.logical;
12992 dev_priv->cdclk.actual = intel_state->cdclk.actual;
c3b32658
ML
12993 }
12994
0853695c 12995 drm_atomic_state_get(state);
c004a90b
CW
12996 INIT_WORK(&state->commit_work,
12997 nonblock ? intel_atomic_commit_work : NULL);
12998
12999 i915_sw_fence_commit(&intel_state->commit_ready);
13000 if (!nonblock) {
13001 i915_sw_fence_wait(&intel_state->commit_ready);
94f05024 13002 intel_atomic_commit_tail(state);
c004a90b 13003 }
75714940 13004
74c090b1 13005 return 0;
7f27126e
JB
13006}
13007
c0c36b94
CW
13008void intel_crtc_restore_mode(struct drm_crtc *crtc)
13009{
83a57153
ACO
13010 struct drm_device *dev = crtc->dev;
13011 struct drm_atomic_state *state;
e694eb02 13012 struct drm_crtc_state *crtc_state;
2bfb4627 13013 int ret;
83a57153
ACO
13014
13015 state = drm_atomic_state_alloc(dev);
13016 if (!state) {
78108b7c
VS
13017 DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
13018 crtc->base.id, crtc->name);
83a57153
ACO
13019 return;
13020 }
13021
e694eb02 13022 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
83a57153 13023
e694eb02
ML
13024retry:
13025 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13026 ret = PTR_ERR_OR_ZERO(crtc_state);
13027 if (!ret) {
13028 if (!crtc_state->active)
13029 goto out;
83a57153 13030
e694eb02 13031 crtc_state->mode_changed = true;
74c090b1 13032 ret = drm_atomic_commit(state);
83a57153
ACO
13033 }
13034
e694eb02
ML
13035 if (ret == -EDEADLK) {
13036 drm_atomic_state_clear(state);
13037 drm_modeset_backoff(state->acquire_ctx);
13038 goto retry;
4ed9fb37 13039 }
4be07317 13040
e694eb02 13041out:
0853695c 13042 drm_atomic_state_put(state);
c0c36b94
CW
13043}
13044
a8784875
BP
13045/*
13046 * FIXME: Remove this once i915 is fully DRIVER_ATOMIC by calling
13047 * drm_atomic_helper_legacy_gamma_set() directly.
13048 */
13049static int intel_atomic_legacy_gamma_set(struct drm_crtc *crtc,
13050 u16 *red, u16 *green, u16 *blue,
13051 uint32_t size)
13052{
13053 struct drm_device *dev = crtc->dev;
13054 struct drm_mode_config *config = &dev->mode_config;
13055 struct drm_crtc_state *state;
13056 int ret;
13057
13058 ret = drm_atomic_helper_legacy_gamma_set(crtc, red, green, blue, size);
13059 if (ret)
13060 return ret;
13061
13062 /*
13063 * Make sure we update the legacy properties so this works when
13064 * atomic is not enabled.
13065 */
13066
13067 state = crtc->state;
13068
13069 drm_object_property_set_value(&crtc->base,
13070 config->degamma_lut_property,
13071 (state->degamma_lut) ?
13072 state->degamma_lut->base.id : 0);
13073
13074 drm_object_property_set_value(&crtc->base,
13075 config->ctm_property,
13076 (state->ctm) ?
13077 state->ctm->base.id : 0);
13078
13079 drm_object_property_set_value(&crtc->base,
13080 config->gamma_lut_property,
13081 (state->gamma_lut) ?
13082 state->gamma_lut->base.id : 0);
13083
13084 return 0;
13085}
13086
f6e5b160 13087static const struct drm_crtc_funcs intel_crtc_funcs = {
a8784875 13088 .gamma_set = intel_atomic_legacy_gamma_set,
74c090b1 13089 .set_config = drm_atomic_helper_set_config,
82cf435b 13090 .set_property = drm_atomic_helper_crtc_set_property,
f6e5b160 13091 .destroy = intel_crtc_destroy,
4c01ded5 13092 .page_flip = drm_atomic_helper_page_flip,
1356837e
MR
13093 .atomic_duplicate_state = intel_crtc_duplicate_state,
13094 .atomic_destroy_state = intel_crtc_destroy_state,
8c6b709d 13095 .set_crc_source = intel_crtc_set_crc_source,
f6e5b160
CW
13096};
13097
6beb8c23
MR
13098/**
13099 * intel_prepare_plane_fb - Prepare fb for usage on plane
13100 * @plane: drm plane to prepare for
13101 * @fb: framebuffer to prepare for presentation
13102 *
13103 * Prepares a framebuffer for usage on a display plane. Generally this
13104 * involves pinning the underlying object and updating the frontbuffer tracking
13105 * bits. Some older platforms need special physical address handling for
13106 * cursor planes.
13107 *
f935675f
ML
13108 * Must be called with struct_mutex held.
13109 *
6beb8c23
MR
13110 * Returns 0 on success, negative error code on failure.
13111 */
13112int
13113intel_prepare_plane_fb(struct drm_plane *plane,
1832040d 13114 struct drm_plane_state *new_state)
465c120c 13115{
c004a90b
CW
13116 struct intel_atomic_state *intel_state =
13117 to_intel_atomic_state(new_state->state);
b7f05d4a 13118 struct drm_i915_private *dev_priv = to_i915(plane->dev);
844f9111 13119 struct drm_framebuffer *fb = new_state->fb;
6beb8c23 13120 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
1ee49399 13121 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
c004a90b 13122 int ret;
465c120c 13123
57822dc6
CW
13124 if (obj) {
13125 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
13126 INTEL_INFO(dev_priv)->cursor_needs_physical) {
13127 const int align = IS_I830(dev_priv) ? 16 * 1024 : 256;
13128
13129 ret = i915_gem_object_attach_phys(obj, align);
13130 if (ret) {
13131 DRM_DEBUG_KMS("failed to attach phys object\n");
13132 return ret;
13133 }
13134 } else {
13135 struct i915_vma *vma;
13136
13137 vma = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
13138 if (IS_ERR(vma)) {
13139 DRM_DEBUG_KMS("failed to pin object\n");
13140 return PTR_ERR(vma);
13141 }
13142
13143 to_intel_plane_state(new_state)->vma = vma;
13144 }
13145 }
13146
1ee49399 13147 if (!obj && !old_obj)
465c120c
MR
13148 return 0;
13149
5008e874
ML
13150 if (old_obj) {
13151 struct drm_crtc_state *crtc_state =
c004a90b
CW
13152 drm_atomic_get_existing_crtc_state(new_state->state,
13153 plane->state->crtc);
5008e874
ML
13154
13155 /* Big Hammer, we also need to ensure that any pending
13156 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
13157 * current scanout is retired before unpinning the old
13158 * framebuffer. Note that we rely on userspace rendering
13159 * into the buffer attached to the pipe they are waiting
13160 * on. If not, userspace generates a GPU hang with IPEHR
13161 * point to the MI_WAIT_FOR_EVENT.
13162 *
13163 * This should only fail upon a hung GPU, in which case we
13164 * can safely continue.
13165 */
c004a90b
CW
13166 if (needs_modeset(crtc_state)) {
13167 ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
13168 old_obj->resv, NULL,
13169 false, 0,
13170 GFP_KERNEL);
13171 if (ret < 0)
13172 return ret;
f4457ae7 13173 }
5008e874
ML
13174 }
13175
c004a90b
CW
13176 if (new_state->fence) { /* explicit fencing */
13177 ret = i915_sw_fence_await_dma_fence(&intel_state->commit_ready,
13178 new_state->fence,
13179 I915_FENCE_TIMEOUT,
13180 GFP_KERNEL);
13181 if (ret < 0)
13182 return ret;
13183 }
13184
c37efb99
CW
13185 if (!obj)
13186 return 0;
13187
c004a90b
CW
13188 if (!new_state->fence) { /* implicit fencing */
13189 ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
13190 obj->resv, NULL,
13191 false, I915_FENCE_TIMEOUT,
13192 GFP_KERNEL);
13193 if (ret < 0)
13194 return ret;
6b5e90f5
CW
13195
13196 i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
c004a90b 13197 }
5a21b665 13198
d07f0e59 13199 return 0;
6beb8c23
MR
13200}
13201
38f3ce3a
MR
13202/**
13203 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13204 * @plane: drm plane to clean up for
13205 * @fb: old framebuffer that was on plane
13206 *
13207 * Cleans up a framebuffer that has just been removed from a plane.
f935675f
ML
13208 *
13209 * Must be called with struct_mutex held.
38f3ce3a
MR
13210 */
13211void
13212intel_cleanup_plane_fb(struct drm_plane *plane,
1832040d 13213 struct drm_plane_state *old_state)
38f3ce3a 13214{
be1e3415 13215 struct i915_vma *vma;
38f3ce3a 13216
be1e3415
CW
13217 /* Should only be called after a successful intel_prepare_plane_fb()! */
13218 vma = fetch_and_zero(&to_intel_plane_state(old_state)->vma);
13219 if (vma)
13220 intel_unpin_fb_vma(vma);
465c120c
MR
13221}
13222
6156a456
CK
13223int
13224skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13225{
5b7280f0 13226 struct drm_i915_private *dev_priv;
6156a456 13227 int max_scale;
5b7280f0 13228 int crtc_clock, max_dotclk;
6156a456 13229
bf8a0af0 13230 if (!intel_crtc || !crtc_state->base.enable)
6156a456
CK
13231 return DRM_PLANE_HELPER_NO_SCALING;
13232
5b7280f0
ACO
13233 dev_priv = to_i915(intel_crtc->base.dev);
13234
6156a456 13235 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
5b7280f0
ACO
13236 max_dotclk = to_intel_atomic_state(crtc_state->base.state)->cdclk.logical.cdclk;
13237
13238 if (IS_GEMINILAKE(dev_priv))
13239 max_dotclk *= 2;
6156a456 13240
5b7280f0 13241 if (WARN_ON_ONCE(!crtc_clock || max_dotclk < crtc_clock))
6156a456
CK
13242 return DRM_PLANE_HELPER_NO_SCALING;
13243
13244 /*
13245 * skl max scale is lower of:
13246 * close to 3 but not 3, -1 is for that purpose
13247 * or
13248 * cdclk/crtc_clock
13249 */
5b7280f0
ACO
13250 max_scale = min((1 << 16) * 3 - 1,
13251 (1 << 8) * ((max_dotclk << 8) / crtc_clock));
6156a456
CK
13252
13253 return max_scale;
13254}
13255
465c120c 13256static int
3c692a41 13257intel_check_primary_plane(struct drm_plane *plane,
061e4b8d 13258 struct intel_crtc_state *crtc_state,
3c692a41
GP
13259 struct intel_plane_state *state)
13260{
b63a16f6 13261 struct drm_i915_private *dev_priv = to_i915(plane->dev);
2b875c22 13262 struct drm_crtc *crtc = state->base.crtc;
6156a456 13263 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
061e4b8d
ML
13264 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13265 bool can_position = false;
b63a16f6 13266 int ret;
465c120c 13267
b63a16f6 13268 if (INTEL_GEN(dev_priv) >= 9) {
693bdc28
VS
13269 /* use scaler when colorkey is not required */
13270 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
13271 min_scale = 1;
13272 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
13273 }
d8106366 13274 can_position = true;
6156a456 13275 }
d8106366 13276
cc926387
DV
13277 ret = drm_plane_helper_check_state(&state->base,
13278 &state->clip,
13279 min_scale, max_scale,
13280 can_position, true);
b63a16f6
VS
13281 if (ret)
13282 return ret;
13283
cc926387 13284 if (!state->base.fb)
b63a16f6
VS
13285 return 0;
13286
13287 if (INTEL_GEN(dev_priv) >= 9) {
13288 ret = skl_check_plane_surface(state);
13289 if (ret)
13290 return ret;
13291 }
13292
13293 return 0;
14af293f
GP
13294}
13295
5a21b665
DV
13296static void intel_begin_crtc_commit(struct drm_crtc *crtc,
13297 struct drm_crtc_state *old_crtc_state)
13298{
13299 struct drm_device *dev = crtc->dev;
62e0fb88 13300 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665 13301 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b707aa50
L
13302 struct intel_crtc_state *intel_cstate =
13303 to_intel_crtc_state(crtc->state);
ccf010fb 13304 struct intel_crtc_state *old_intel_cstate =
5a21b665 13305 to_intel_crtc_state(old_crtc_state);
ccf010fb
ML
13306 struct intel_atomic_state *old_intel_state =
13307 to_intel_atomic_state(old_crtc_state->state);
5a21b665
DV
13308 bool modeset = needs_modeset(crtc->state);
13309
13310 /* Perform vblank evasion around commit operation */
13311 intel_pipe_update_start(intel_crtc);
13312
13313 if (modeset)
e62929b3 13314 goto out;
5a21b665
DV
13315
13316 if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
13317 intel_color_set_csc(crtc->state);
13318 intel_color_load_luts(crtc->state);
13319 }
13320
ccf010fb
ML
13321 if (intel_cstate->update_pipe)
13322 intel_update_pipe_config(intel_crtc, old_intel_cstate);
13323 else if (INTEL_GEN(dev_priv) >= 9)
5a21b665 13324 skl_detach_scalers(intel_crtc);
62e0fb88 13325
e62929b3 13326out:
ccf010fb
ML
13327 if (dev_priv->display.atomic_update_watermarks)
13328 dev_priv->display.atomic_update_watermarks(old_intel_state,
13329 intel_cstate);
5a21b665
DV
13330}
13331
13332static void intel_finish_crtc_commit(struct drm_crtc *crtc,
13333 struct drm_crtc_state *old_crtc_state)
13334{
13335 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13336
13337 intel_pipe_update_end(intel_crtc, NULL);
13338}
13339
cf4c7c12 13340/**
4a3b8769
MR
13341 * intel_plane_destroy - destroy a plane
13342 * @plane: plane to destroy
cf4c7c12 13343 *
4a3b8769
MR
13344 * Common destruction function for all types of planes (primary, cursor,
13345 * sprite).
cf4c7c12 13346 */
4a3b8769 13347void intel_plane_destroy(struct drm_plane *plane)
465c120c 13348{
465c120c 13349 drm_plane_cleanup(plane);
69ae561f 13350 kfree(to_intel_plane(plane));
465c120c
MR
13351}
13352
65a3fea0 13353const struct drm_plane_funcs intel_plane_funcs = {
70a101f8
MR
13354 .update_plane = drm_atomic_helper_update_plane,
13355 .disable_plane = drm_atomic_helper_disable_plane,
3d7d6510 13356 .destroy = intel_plane_destroy,
c196e1d6 13357 .set_property = drm_atomic_helper_plane_set_property,
a98b3431
MR
13358 .atomic_get_property = intel_plane_atomic_get_property,
13359 .atomic_set_property = intel_plane_atomic_set_property,
ea2c67bb
MR
13360 .atomic_duplicate_state = intel_plane_duplicate_state,
13361 .atomic_destroy_state = intel_plane_destroy_state,
465c120c
MR
13362};
13363
f79f2692
ML
13364static int
13365intel_legacy_cursor_update(struct drm_plane *plane,
13366 struct drm_crtc *crtc,
13367 struct drm_framebuffer *fb,
13368 int crtc_x, int crtc_y,
13369 unsigned int crtc_w, unsigned int crtc_h,
13370 uint32_t src_x, uint32_t src_y,
13371 uint32_t src_w, uint32_t src_h)
13372{
13373 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
13374 int ret;
13375 struct drm_plane_state *old_plane_state, *new_plane_state;
13376 struct intel_plane *intel_plane = to_intel_plane(plane);
13377 struct drm_framebuffer *old_fb;
13378 struct drm_crtc_state *crtc_state = crtc->state;
be1e3415 13379 struct i915_vma *old_vma;
f79f2692
ML
13380
13381 /*
13382 * When crtc is inactive or there is a modeset pending,
13383 * wait for it to complete in the slowpath
13384 */
13385 if (!crtc_state->active || needs_modeset(crtc_state) ||
13386 to_intel_crtc_state(crtc_state)->update_pipe)
13387 goto slow;
13388
13389 old_plane_state = plane->state;
13390
13391 /*
13392 * If any parameters change that may affect watermarks,
13393 * take the slowpath. Only changing fb or position should be
13394 * in the fastpath.
13395 */
13396 if (old_plane_state->crtc != crtc ||
13397 old_plane_state->src_w != src_w ||
13398 old_plane_state->src_h != src_h ||
13399 old_plane_state->crtc_w != crtc_w ||
13400 old_plane_state->crtc_h != crtc_h ||
a5509abd 13401 !old_plane_state->fb != !fb)
f79f2692
ML
13402 goto slow;
13403
13404 new_plane_state = intel_plane_duplicate_state(plane);
13405 if (!new_plane_state)
13406 return -ENOMEM;
13407
13408 drm_atomic_set_fb_for_plane(new_plane_state, fb);
13409
13410 new_plane_state->src_x = src_x;
13411 new_plane_state->src_y = src_y;
13412 new_plane_state->src_w = src_w;
13413 new_plane_state->src_h = src_h;
13414 new_plane_state->crtc_x = crtc_x;
13415 new_plane_state->crtc_y = crtc_y;
13416 new_plane_state->crtc_w = crtc_w;
13417 new_plane_state->crtc_h = crtc_h;
13418
13419 ret = intel_plane_atomic_check_with_state(to_intel_crtc_state(crtc->state),
13420 to_intel_plane_state(new_plane_state));
13421 if (ret)
13422 goto out_free;
13423
f79f2692
ML
13424 ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
13425 if (ret)
13426 goto out_free;
13427
13428 if (INTEL_INFO(dev_priv)->cursor_needs_physical) {
13429 int align = IS_I830(dev_priv) ? 16 * 1024 : 256;
13430
13431 ret = i915_gem_object_attach_phys(intel_fb_obj(fb), align);
13432 if (ret) {
13433 DRM_DEBUG_KMS("failed to attach phys object\n");
13434 goto out_unlock;
13435 }
13436 } else {
13437 struct i915_vma *vma;
13438
13439 vma = intel_pin_and_fence_fb_obj(fb, new_plane_state->rotation);
13440 if (IS_ERR(vma)) {
13441 DRM_DEBUG_KMS("failed to pin object\n");
13442
13443 ret = PTR_ERR(vma);
13444 goto out_unlock;
13445 }
be1e3415
CW
13446
13447 to_intel_plane_state(new_plane_state)->vma = vma;
f79f2692
ML
13448 }
13449
13450 old_fb = old_plane_state->fb;
be1e3415 13451 old_vma = to_intel_plane_state(old_plane_state)->vma;
f79f2692
ML
13452
13453 i915_gem_track_fb(intel_fb_obj(old_fb), intel_fb_obj(fb),
13454 intel_plane->frontbuffer_bit);
13455
13456 /* Swap plane state */
13457 new_plane_state->fence = old_plane_state->fence;
13458 *to_intel_plane_state(old_plane_state) = *to_intel_plane_state(new_plane_state);
13459 new_plane_state->fence = NULL;
13460 new_plane_state->fb = old_fb;
be1e3415 13461 to_intel_plane_state(new_plane_state)->vma = old_vma;
f79f2692 13462
a5509abd
VS
13463 if (plane->state->visible)
13464 intel_plane->update_plane(plane,
13465 to_intel_crtc_state(crtc->state),
13466 to_intel_plane_state(plane->state));
13467 else
13468 intel_plane->disable_plane(plane, crtc);
f79f2692
ML
13469
13470 intel_cleanup_plane_fb(plane, new_plane_state);
13471
13472out_unlock:
13473 mutex_unlock(&dev_priv->drm.struct_mutex);
13474out_free:
13475 intel_plane_destroy_state(plane, new_plane_state);
13476 return ret;
13477
f79f2692
ML
13478slow:
13479 return drm_atomic_helper_update_plane(plane, crtc, fb,
13480 crtc_x, crtc_y, crtc_w, crtc_h,
13481 src_x, src_y, src_w, src_h);
13482}
13483
13484static const struct drm_plane_funcs intel_cursor_plane_funcs = {
13485 .update_plane = intel_legacy_cursor_update,
13486 .disable_plane = drm_atomic_helper_disable_plane,
13487 .destroy = intel_plane_destroy,
13488 .set_property = drm_atomic_helper_plane_set_property,
13489 .atomic_get_property = intel_plane_atomic_get_property,
13490 .atomic_set_property = intel_plane_atomic_set_property,
13491 .atomic_duplicate_state = intel_plane_duplicate_state,
13492 .atomic_destroy_state = intel_plane_destroy_state,
13493};
13494
b079bd17 13495static struct intel_plane *
580503c7 13496intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
465c120c 13497{
fca0ce2a
VS
13498 struct intel_plane *primary = NULL;
13499 struct intel_plane_state *state = NULL;
465c120c 13500 const uint32_t *intel_primary_formats;
93ca7e00 13501 unsigned int supported_rotations;
45e3743a 13502 unsigned int num_formats;
fca0ce2a 13503 int ret;
465c120c
MR
13504
13505 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
b079bd17
VS
13506 if (!primary) {
13507 ret = -ENOMEM;
fca0ce2a 13508 goto fail;
b079bd17 13509 }
465c120c 13510
8e7d688b 13511 state = intel_create_plane_state(&primary->base);
b079bd17
VS
13512 if (!state) {
13513 ret = -ENOMEM;
fca0ce2a 13514 goto fail;
b079bd17
VS
13515 }
13516
8e7d688b 13517 primary->base.state = &state->base;
ea2c67bb 13518
465c120c
MR
13519 primary->can_scale = false;
13520 primary->max_downscale = 1;
580503c7 13521 if (INTEL_GEN(dev_priv) >= 9) {
6156a456 13522 primary->can_scale = true;
af99ceda 13523 state->scaler_id = -1;
6156a456 13524 }
465c120c 13525 primary->pipe = pipe;
e3c566df
VS
13526 /*
13527 * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
13528 * port is hooked to pipe B. Hence we want plane A feeding pipe B.
13529 */
13530 if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4)
13531 primary->plane = (enum plane) !pipe;
13532 else
13533 primary->plane = (enum plane) pipe;
b14e5848 13534 primary->id = PLANE_PRIMARY;
a9ff8714 13535 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
c59cb179 13536 primary->check_plane = intel_check_primary_plane;
465c120c 13537
580503c7 13538 if (INTEL_GEN(dev_priv) >= 9) {
6c0fd451
DL
13539 intel_primary_formats = skl_primary_formats;
13540 num_formats = ARRAY_SIZE(skl_primary_formats);
a8d201af
ML
13541
13542 primary->update_plane = skylake_update_primary_plane;
13543 primary->disable_plane = skylake_disable_primary_plane;
6e266956 13544 } else if (HAS_PCH_SPLIT(dev_priv)) {
a8d201af
ML
13545 intel_primary_formats = i965_primary_formats;
13546 num_formats = ARRAY_SIZE(i965_primary_formats);
13547
13548 primary->update_plane = ironlake_update_primary_plane;
13549 primary->disable_plane = i9xx_disable_primary_plane;
580503c7 13550 } else if (INTEL_GEN(dev_priv) >= 4) {
568db4f2
DL
13551 intel_primary_formats = i965_primary_formats;
13552 num_formats = ARRAY_SIZE(i965_primary_formats);
a8d201af
ML
13553
13554 primary->update_plane = i9xx_update_primary_plane;
13555 primary->disable_plane = i9xx_disable_primary_plane;
6c0fd451
DL
13556 } else {
13557 intel_primary_formats = i8xx_primary_formats;
13558 num_formats = ARRAY_SIZE(i8xx_primary_formats);
a8d201af
ML
13559
13560 primary->update_plane = i9xx_update_primary_plane;
13561 primary->disable_plane = i9xx_disable_primary_plane;
465c120c
MR
13562 }
13563
580503c7
VS
13564 if (INTEL_GEN(dev_priv) >= 9)
13565 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
13566 0, &intel_plane_funcs,
38573dc1
VS
13567 intel_primary_formats, num_formats,
13568 DRM_PLANE_TYPE_PRIMARY,
13569 "plane 1%c", pipe_name(pipe));
9beb5fea 13570 else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
580503c7
VS
13571 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
13572 0, &intel_plane_funcs,
38573dc1
VS
13573 intel_primary_formats, num_formats,
13574 DRM_PLANE_TYPE_PRIMARY,
13575 "primary %c", pipe_name(pipe));
13576 else
580503c7
VS
13577 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
13578 0, &intel_plane_funcs,
38573dc1
VS
13579 intel_primary_formats, num_formats,
13580 DRM_PLANE_TYPE_PRIMARY,
13581 "plane %c", plane_name(primary->plane));
fca0ce2a
VS
13582 if (ret)
13583 goto fail;
48404c1e 13584
5481e27f 13585 if (INTEL_GEN(dev_priv) >= 9) {
93ca7e00
VS
13586 supported_rotations =
13587 DRM_ROTATE_0 | DRM_ROTATE_90 |
13588 DRM_ROTATE_180 | DRM_ROTATE_270;
4ea7be2b
VS
13589 } else if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
13590 supported_rotations =
13591 DRM_ROTATE_0 | DRM_ROTATE_180 |
13592 DRM_REFLECT_X;
5481e27f 13593 } else if (INTEL_GEN(dev_priv) >= 4) {
93ca7e00
VS
13594 supported_rotations =
13595 DRM_ROTATE_0 | DRM_ROTATE_180;
13596 } else {
13597 supported_rotations = DRM_ROTATE_0;
13598 }
13599
5481e27f 13600 if (INTEL_GEN(dev_priv) >= 4)
93ca7e00
VS
13601 drm_plane_create_rotation_property(&primary->base,
13602 DRM_ROTATE_0,
13603 supported_rotations);
48404c1e 13604
ea2c67bb
MR
13605 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13606
b079bd17 13607 return primary;
fca0ce2a
VS
13608
13609fail:
13610 kfree(state);
13611 kfree(primary);
13612
b079bd17 13613 return ERR_PTR(ret);
465c120c
MR
13614}
13615
3d7d6510 13616static int
852e787c 13617intel_check_cursor_plane(struct drm_plane *plane,
061e4b8d 13618 struct intel_crtc_state *crtc_state,
852e787c 13619 struct intel_plane_state *state)
3d7d6510 13620{
2b875c22 13621 struct drm_framebuffer *fb = state->base.fb;
757f9a3e 13622 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
b29ec92c 13623 enum pipe pipe = to_intel_plane(plane)->pipe;
757f9a3e
GP
13624 unsigned stride;
13625 int ret;
3d7d6510 13626
f8856a44
VS
13627 ret = drm_plane_helper_check_state(&state->base,
13628 &state->clip,
13629 DRM_PLANE_HELPER_NO_SCALING,
13630 DRM_PLANE_HELPER_NO_SCALING,
13631 true, true);
757f9a3e
GP
13632 if (ret)
13633 return ret;
13634
757f9a3e
GP
13635 /* if we want to turn off the cursor ignore width and height */
13636 if (!obj)
da20eabd 13637 return 0;
757f9a3e 13638
757f9a3e 13639 /* Check for which cursor types we support */
50a0bc90
TU
13640 if (!cursor_size_ok(to_i915(plane->dev), state->base.crtc_w,
13641 state->base.crtc_h)) {
ea2c67bb
MR
13642 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
13643 state->base.crtc_w, state->base.crtc_h);
757f9a3e
GP
13644 return -EINVAL;
13645 }
13646
ea2c67bb
MR
13647 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
13648 if (obj->base.size < stride * state->base.crtc_h) {
757f9a3e
GP
13649 DRM_DEBUG_KMS("buffer is too small\n");
13650 return -ENOMEM;
13651 }
13652
bae781b2 13653 if (fb->modifier != DRM_FORMAT_MOD_NONE) {
757f9a3e 13654 DRM_DEBUG_KMS("cursor cannot be tiled\n");
da20eabd 13655 return -EINVAL;
32b7eeec
MR
13656 }
13657
b29ec92c
VS
13658 /*
13659 * There's something wrong with the cursor on CHV pipe C.
13660 * If it straddles the left edge of the screen then
13661 * moving it away from the edge or disabling it often
13662 * results in a pipe underrun, and often that can lead to
13663 * dead pipe (constant underrun reported, and it scans
13664 * out just a solid color). To recover from that, the
13665 * display power well must be turned off and on again.
13666 * Refuse the put the cursor into that compromised position.
13667 */
920a14b2 13668 if (IS_CHERRYVIEW(to_i915(plane->dev)) && pipe == PIPE_C &&
936e71e3 13669 state->base.visible && state->base.crtc_x < 0) {
b29ec92c
VS
13670 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
13671 return -EINVAL;
13672 }
13673
da20eabd 13674 return 0;
852e787c 13675}
3d7d6510 13676
a8ad0d8e
ML
13677static void
13678intel_disable_cursor_plane(struct drm_plane *plane,
7fabf5ef 13679 struct drm_crtc *crtc)
a8ad0d8e 13680{
f2858021
ML
13681 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13682
13683 intel_crtc->cursor_addr = 0;
55a08b3f 13684 intel_crtc_update_cursor(crtc, NULL);
a8ad0d8e
ML
13685}
13686
f4a2cf29 13687static void
55a08b3f
ML
13688intel_update_cursor_plane(struct drm_plane *plane,
13689 const struct intel_crtc_state *crtc_state,
13690 const struct intel_plane_state *state)
852e787c 13691{
55a08b3f
ML
13692 struct drm_crtc *crtc = crtc_state->base.crtc;
13693 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b7f05d4a 13694 struct drm_i915_private *dev_priv = to_i915(plane->dev);
2b875c22 13695 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
a912f12f 13696 uint32_t addr;
852e787c 13697
f4a2cf29 13698 if (!obj)
a912f12f 13699 addr = 0;
b7f05d4a 13700 else if (!INTEL_INFO(dev_priv)->cursor_needs_physical)
be1e3415 13701 addr = intel_plane_ggtt_offset(state);
f4a2cf29 13702 else
a912f12f 13703 addr = obj->phys_handle->busaddr;
852e787c 13704
a912f12f 13705 intel_crtc->cursor_addr = addr;
55a08b3f 13706 intel_crtc_update_cursor(crtc, state);
852e787c
GP
13707}
13708
b079bd17 13709static struct intel_plane *
580503c7 13710intel_cursor_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
3d7d6510 13711{
fca0ce2a
VS
13712 struct intel_plane *cursor = NULL;
13713 struct intel_plane_state *state = NULL;
13714 int ret;
3d7d6510
MR
13715
13716 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
b079bd17
VS
13717 if (!cursor) {
13718 ret = -ENOMEM;
fca0ce2a 13719 goto fail;
b079bd17 13720 }
3d7d6510 13721
8e7d688b 13722 state = intel_create_plane_state(&cursor->base);
b079bd17
VS
13723 if (!state) {
13724 ret = -ENOMEM;
fca0ce2a 13725 goto fail;
b079bd17
VS
13726 }
13727
8e7d688b 13728 cursor->base.state = &state->base;
ea2c67bb 13729
3d7d6510
MR
13730 cursor->can_scale = false;
13731 cursor->max_downscale = 1;
13732 cursor->pipe = pipe;
13733 cursor->plane = pipe;
b14e5848 13734 cursor->id = PLANE_CURSOR;
a9ff8714 13735 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
c59cb179 13736 cursor->check_plane = intel_check_cursor_plane;
55a08b3f 13737 cursor->update_plane = intel_update_cursor_plane;
a8ad0d8e 13738 cursor->disable_plane = intel_disable_cursor_plane;
3d7d6510 13739
580503c7 13740 ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
f79f2692 13741 0, &intel_cursor_plane_funcs,
fca0ce2a
VS
13742 intel_cursor_formats,
13743 ARRAY_SIZE(intel_cursor_formats),
38573dc1
VS
13744 DRM_PLANE_TYPE_CURSOR,
13745 "cursor %c", pipe_name(pipe));
fca0ce2a
VS
13746 if (ret)
13747 goto fail;
4398ad45 13748
5481e27f 13749 if (INTEL_GEN(dev_priv) >= 4)
93ca7e00
VS
13750 drm_plane_create_rotation_property(&cursor->base,
13751 DRM_ROTATE_0,
13752 DRM_ROTATE_0 |
13753 DRM_ROTATE_180);
4398ad45 13754
580503c7 13755 if (INTEL_GEN(dev_priv) >= 9)
af99ceda
CK
13756 state->scaler_id = -1;
13757
ea2c67bb
MR
13758 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
13759
b079bd17 13760 return cursor;
fca0ce2a
VS
13761
13762fail:
13763 kfree(state);
13764 kfree(cursor);
13765
b079bd17 13766 return ERR_PTR(ret);
3d7d6510
MR
13767}
13768
1c74eeaf
NM
13769static void intel_crtc_init_scalers(struct intel_crtc *crtc,
13770 struct intel_crtc_state *crtc_state)
549e2bfb 13771{
65edccce
VS
13772 struct intel_crtc_scaler_state *scaler_state =
13773 &crtc_state->scaler_state;
1c74eeaf 13774 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
549e2bfb 13775 int i;
549e2bfb 13776
1c74eeaf
NM
13777 crtc->num_scalers = dev_priv->info.num_scalers[crtc->pipe];
13778 if (!crtc->num_scalers)
13779 return;
13780
65edccce
VS
13781 for (i = 0; i < crtc->num_scalers; i++) {
13782 struct intel_scaler *scaler = &scaler_state->scalers[i];
13783
13784 scaler->in_use = 0;
13785 scaler->mode = PS_SCALER_MODE_DYN;
549e2bfb
CK
13786 }
13787
13788 scaler_state->scaler_id = -1;
13789}
13790
5ab0d85b 13791static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
79e53945
JB
13792{
13793 struct intel_crtc *intel_crtc;
f5de6e07 13794 struct intel_crtc_state *crtc_state = NULL;
b079bd17
VS
13795 struct intel_plane *primary = NULL;
13796 struct intel_plane *cursor = NULL;
a81d6fa0 13797 int sprite, ret;
79e53945 13798
955382f3 13799 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
b079bd17
VS
13800 if (!intel_crtc)
13801 return -ENOMEM;
79e53945 13802
f5de6e07 13803 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
b079bd17
VS
13804 if (!crtc_state) {
13805 ret = -ENOMEM;
f5de6e07 13806 goto fail;
b079bd17 13807 }
550acefd
ACO
13808 intel_crtc->config = crtc_state;
13809 intel_crtc->base.state = &crtc_state->base;
07878248 13810 crtc_state->base.crtc = &intel_crtc->base;
f5de6e07 13811
580503c7 13812 primary = intel_primary_plane_create(dev_priv, pipe);
b079bd17
VS
13813 if (IS_ERR(primary)) {
13814 ret = PTR_ERR(primary);
3d7d6510 13815 goto fail;
b079bd17 13816 }
d97d7b48 13817 intel_crtc->plane_ids_mask |= BIT(primary->id);
3d7d6510 13818
a81d6fa0 13819 for_each_sprite(dev_priv, pipe, sprite) {
b079bd17
VS
13820 struct intel_plane *plane;
13821
580503c7 13822 plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
d2b2cbce 13823 if (IS_ERR(plane)) {
b079bd17
VS
13824 ret = PTR_ERR(plane);
13825 goto fail;
13826 }
d97d7b48 13827 intel_crtc->plane_ids_mask |= BIT(plane->id);
a81d6fa0
VS
13828 }
13829
580503c7 13830 cursor = intel_cursor_plane_create(dev_priv, pipe);
d2b2cbce 13831 if (IS_ERR(cursor)) {
b079bd17 13832 ret = PTR_ERR(cursor);
3d7d6510 13833 goto fail;
b079bd17 13834 }
d97d7b48 13835 intel_crtc->plane_ids_mask |= BIT(cursor->id);
3d7d6510 13836
5ab0d85b 13837 ret = drm_crtc_init_with_planes(&dev_priv->drm, &intel_crtc->base,
b079bd17
VS
13838 &primary->base, &cursor->base,
13839 &intel_crtc_funcs,
4d5d72b7 13840 "pipe %c", pipe_name(pipe));
3d7d6510
MR
13841 if (ret)
13842 goto fail;
79e53945 13843
80824003 13844 intel_crtc->pipe = pipe;
e3c566df 13845 intel_crtc->plane = primary->plane;
80824003 13846
4b0e333e
CW
13847 intel_crtc->cursor_base = ~0;
13848 intel_crtc->cursor_cntl = ~0;
dc41c154 13849 intel_crtc->cursor_size = ~0;
8d7849db 13850
852eb00d
VS
13851 intel_crtc->wm.cxsr_allowed = true;
13852
1c74eeaf
NM
13853 /* initialize shared scalers */
13854 intel_crtc_init_scalers(intel_crtc, crtc_state);
13855
22fd0fab
JB
13856 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
13857 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
e2af48c6
VS
13858 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = intel_crtc;
13859 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = intel_crtc;
22fd0fab 13860
79e53945 13861 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
87b6b101 13862
8563b1e8
LL
13863 intel_color_init(&intel_crtc->base);
13864
87b6b101 13865 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
b079bd17
VS
13866
13867 return 0;
3d7d6510
MR
13868
13869fail:
b079bd17
VS
13870 /*
13871 * drm_mode_config_cleanup() will free up any
13872 * crtcs/planes already initialized.
13873 */
f5de6e07 13874 kfree(crtc_state);
3d7d6510 13875 kfree(intel_crtc);
b079bd17
VS
13876
13877 return ret;
79e53945
JB
13878}
13879
752aa88a
JB
13880enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
13881{
13882 struct drm_encoder *encoder = connector->base.encoder;
6e9f798d 13883 struct drm_device *dev = connector->base.dev;
752aa88a 13884
51fd371b 13885 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
752aa88a 13886
d3babd3f 13887 if (!encoder || WARN_ON(!encoder->crtc))
752aa88a
JB
13888 return INVALID_PIPE;
13889
13890 return to_intel_crtc(encoder->crtc)->pipe;
13891}
13892
08d7b3d1 13893int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 13894 struct drm_file *file)
08d7b3d1 13895{
08d7b3d1 13896 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7707e653 13897 struct drm_crtc *drmmode_crtc;
c05422d5 13898 struct intel_crtc *crtc;
08d7b3d1 13899
7707e653 13900 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
71240ed2 13901 if (!drmmode_crtc)
3f2c2057 13902 return -ENOENT;
08d7b3d1 13903
7707e653 13904 crtc = to_intel_crtc(drmmode_crtc);
c05422d5 13905 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 13906
c05422d5 13907 return 0;
08d7b3d1
CW
13908}
13909
66a9278e 13910static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 13911{
66a9278e
DV
13912 struct drm_device *dev = encoder->base.dev;
13913 struct intel_encoder *source_encoder;
79e53945 13914 int index_mask = 0;
79e53945
JB
13915 int entry = 0;
13916
b2784e15 13917 for_each_intel_encoder(dev, source_encoder) {
bc079e8b 13918 if (encoders_cloneable(encoder, source_encoder))
66a9278e
DV
13919 index_mask |= (1 << entry);
13920
79e53945
JB
13921 entry++;
13922 }
4ef69c7a 13923
79e53945
JB
13924 return index_mask;
13925}
13926
646d5772 13927static bool has_edp_a(struct drm_i915_private *dev_priv)
4d302442 13928{
646d5772 13929 if (!IS_MOBILE(dev_priv))
4d302442
CW
13930 return false;
13931
13932 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
13933 return false;
13934
5db94019 13935 if (IS_GEN5(dev_priv) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
13936 return false;
13937
13938 return true;
13939}
13940
6315b5d3 13941static bool intel_crt_present(struct drm_i915_private *dev_priv)
84b4e042 13942{
6315b5d3 13943 if (INTEL_GEN(dev_priv) >= 9)
884497ed
DL
13944 return false;
13945
50a0bc90 13946 if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
84b4e042
JB
13947 return false;
13948
920a14b2 13949 if (IS_CHERRYVIEW(dev_priv))
84b4e042
JB
13950 return false;
13951
4f8036a2
TU
13952 if (HAS_PCH_LPT_H(dev_priv) &&
13953 I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
65e472e4
VS
13954 return false;
13955
70ac54d0 13956 /* DDI E can't be used if DDI A requires 4 lanes */
4f8036a2 13957 if (HAS_DDI(dev_priv) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
70ac54d0
VS
13958 return false;
13959
e4abb733 13960 if (!dev_priv->vbt.int_crt_support)
84b4e042
JB
13961 return false;
13962
13963 return true;
13964}
13965
8090ba8c
ID
13966void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
13967{
13968 int pps_num;
13969 int pps_idx;
13970
13971 if (HAS_DDI(dev_priv))
13972 return;
13973 /*
13974 * This w/a is needed at least on CPT/PPT, but to be sure apply it
13975 * everywhere where registers can be write protected.
13976 */
13977 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
13978 pps_num = 2;
13979 else
13980 pps_num = 1;
13981
13982 for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
13983 u32 val = I915_READ(PP_CONTROL(pps_idx));
13984
13985 val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
13986 I915_WRITE(PP_CONTROL(pps_idx), val);
13987 }
13988}
13989
44cb734c
ID
13990static void intel_pps_init(struct drm_i915_private *dev_priv)
13991{
cc3f90f0 13992 if (HAS_PCH_SPLIT(dev_priv) || IS_GEN9_LP(dev_priv))
44cb734c
ID
13993 dev_priv->pps_mmio_base = PCH_PPS_BASE;
13994 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
13995 dev_priv->pps_mmio_base = VLV_PPS_BASE;
13996 else
13997 dev_priv->pps_mmio_base = PPS_BASE;
8090ba8c
ID
13998
13999 intel_pps_unlock_regs_wa(dev_priv);
44cb734c
ID
14000}
14001
c39055b0 14002static void intel_setup_outputs(struct drm_i915_private *dev_priv)
79e53945 14003{
4ef69c7a 14004 struct intel_encoder *encoder;
cb0953d7 14005 bool dpd_is_edp = false;
79e53945 14006
44cb734c
ID
14007 intel_pps_init(dev_priv);
14008
97a824e1
ID
14009 /*
14010 * intel_edp_init_connector() depends on this completing first, to
14011 * prevent the registeration of both eDP and LVDS and the incorrect
14012 * sharing of the PPS.
14013 */
c39055b0 14014 intel_lvds_init(dev_priv);
79e53945 14015
6315b5d3 14016 if (intel_crt_present(dev_priv))
c39055b0 14017 intel_crt_init(dev_priv);
cb0953d7 14018
cc3f90f0 14019 if (IS_GEN9_LP(dev_priv)) {
c776eb2e
VK
14020 /*
14021 * FIXME: Broxton doesn't support port detection via the
14022 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14023 * detect the ports.
14024 */
c39055b0
ACO
14025 intel_ddi_init(dev_priv, PORT_A);
14026 intel_ddi_init(dev_priv, PORT_B);
14027 intel_ddi_init(dev_priv, PORT_C);
c6c794a2 14028
c39055b0 14029 intel_dsi_init(dev_priv);
4f8036a2 14030 } else if (HAS_DDI(dev_priv)) {
0e72a5b5
ED
14031 int found;
14032
de31facd
JB
14033 /*
14034 * Haswell uses DDI functions to detect digital outputs.
14035 * On SKL pre-D0 the strap isn't connected, so we assume
14036 * it's there.
14037 */
77179400 14038 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
de31facd 14039 /* WaIgnoreDDIAStrap: skl */
b976dc53 14040 if (found || IS_GEN9_BC(dev_priv))
c39055b0 14041 intel_ddi_init(dev_priv, PORT_A);
0e72a5b5
ED
14042
14043 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14044 * register */
14045 found = I915_READ(SFUSE_STRAP);
14046
14047 if (found & SFUSE_STRAP_DDIB_DETECTED)
c39055b0 14048 intel_ddi_init(dev_priv, PORT_B);
0e72a5b5 14049 if (found & SFUSE_STRAP_DDIC_DETECTED)
c39055b0 14050 intel_ddi_init(dev_priv, PORT_C);
0e72a5b5 14051 if (found & SFUSE_STRAP_DDID_DETECTED)
c39055b0 14052 intel_ddi_init(dev_priv, PORT_D);
2800e4c2
RV
14053 /*
14054 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14055 */
b976dc53 14056 if (IS_GEN9_BC(dev_priv) &&
2800e4c2
RV
14057 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14058 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14059 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
c39055b0 14060 intel_ddi_init(dev_priv, PORT_E);
2800e4c2 14061
6e266956 14062 } else if (HAS_PCH_SPLIT(dev_priv)) {
cb0953d7 14063 int found;
dd11bc10 14064 dpd_is_edp = intel_dp_is_edp(dev_priv, PORT_D);
270b3042 14065
646d5772 14066 if (has_edp_a(dev_priv))
c39055b0 14067 intel_dp_init(dev_priv, DP_A, PORT_A);
cb0953d7 14068
dc0fa718 14069 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 14070 /* PCH SDVOB multiplex with HDMIB */
c39055b0 14071 found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
30ad48b7 14072 if (!found)
c39055b0 14073 intel_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
5eb08b69 14074 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
c39055b0 14075 intel_dp_init(dev_priv, PCH_DP_B, PORT_B);
30ad48b7
ZW
14076 }
14077
dc0fa718 14078 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
c39055b0 14079 intel_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
30ad48b7 14080
dc0fa718 14081 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
c39055b0 14082 intel_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
30ad48b7 14083
5eb08b69 14084 if (I915_READ(PCH_DP_C) & DP_DETECTED)
c39055b0 14085 intel_dp_init(dev_priv, PCH_DP_C, PORT_C);
5eb08b69 14086
270b3042 14087 if (I915_READ(PCH_DP_D) & DP_DETECTED)
c39055b0 14088 intel_dp_init(dev_priv, PCH_DP_D, PORT_D);
920a14b2 14089 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
22f35042 14090 bool has_edp, has_port;
457c52d8 14091
e17ac6db
VS
14092 /*
14093 * The DP_DETECTED bit is the latched state of the DDC
14094 * SDA pin at boot. However since eDP doesn't require DDC
14095 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14096 * eDP ports may have been muxed to an alternate function.
14097 * Thus we can't rely on the DP_DETECTED bit alone to detect
14098 * eDP ports. Consult the VBT as well as DP_DETECTED to
14099 * detect eDP ports.
22f35042
VS
14100 *
14101 * Sadly the straps seem to be missing sometimes even for HDMI
14102 * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
14103 * and VBT for the presence of the port. Additionally we can't
14104 * trust the port type the VBT declares as we've seen at least
14105 * HDMI ports that the VBT claim are DP or eDP.
e17ac6db 14106 */
dd11bc10 14107 has_edp = intel_dp_is_edp(dev_priv, PORT_B);
22f35042
VS
14108 has_port = intel_bios_is_port_present(dev_priv, PORT_B);
14109 if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
c39055b0 14110 has_edp &= intel_dp_init(dev_priv, VLV_DP_B, PORT_B);
22f35042 14111 if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
c39055b0 14112 intel_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
585a94b8 14113
dd11bc10 14114 has_edp = intel_dp_is_edp(dev_priv, PORT_C);
22f35042
VS
14115 has_port = intel_bios_is_port_present(dev_priv, PORT_C);
14116 if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
c39055b0 14117 has_edp &= intel_dp_init(dev_priv, VLV_DP_C, PORT_C);
22f35042 14118 if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
c39055b0 14119 intel_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
19c03924 14120
920a14b2 14121 if (IS_CHERRYVIEW(dev_priv)) {
22f35042
VS
14122 /*
14123 * eDP not supported on port D,
14124 * so no need to worry about it
14125 */
14126 has_port = intel_bios_is_port_present(dev_priv, PORT_D);
14127 if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
c39055b0 14128 intel_dp_init(dev_priv, CHV_DP_D, PORT_D);
22f35042 14129 if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
c39055b0 14130 intel_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
9418c1f1
VS
14131 }
14132
c39055b0 14133 intel_dsi_init(dev_priv);
5db94019 14134 } else if (!IS_GEN2(dev_priv) && !IS_PINEVIEW(dev_priv)) {
27185ae1 14135 bool found = false;
7d57382e 14136
e2debe91 14137 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14138 DRM_DEBUG_KMS("probing SDVOB\n");
c39055b0 14139 found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
9beb5fea 14140 if (!found && IS_G4X(dev_priv)) {
b01f2c3a 14141 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
c39055b0 14142 intel_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
b01f2c3a 14143 }
27185ae1 14144
9beb5fea 14145 if (!found && IS_G4X(dev_priv))
c39055b0 14146 intel_dp_init(dev_priv, DP_B, PORT_B);
725e30ad 14147 }
13520b05
KH
14148
14149 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 14150
e2debe91 14151 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14152 DRM_DEBUG_KMS("probing SDVOC\n");
c39055b0 14153 found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
b01f2c3a 14154 }
27185ae1 14155
e2debe91 14156 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 14157
9beb5fea 14158 if (IS_G4X(dev_priv)) {
b01f2c3a 14159 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
c39055b0 14160 intel_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
b01f2c3a 14161 }
9beb5fea 14162 if (IS_G4X(dev_priv))
c39055b0 14163 intel_dp_init(dev_priv, DP_C, PORT_C);
725e30ad 14164 }
27185ae1 14165
9beb5fea 14166 if (IS_G4X(dev_priv) && (I915_READ(DP_D) & DP_DETECTED))
c39055b0 14167 intel_dp_init(dev_priv, DP_D, PORT_D);
5db94019 14168 } else if (IS_GEN2(dev_priv))
c39055b0 14169 intel_dvo_init(dev_priv);
79e53945 14170
56b857a5 14171 if (SUPPORTS_TV(dev_priv))
c39055b0 14172 intel_tv_init(dev_priv);
79e53945 14173
c39055b0 14174 intel_psr_init(dev_priv);
7c8f8a70 14175
c39055b0 14176 for_each_intel_encoder(&dev_priv->drm, encoder) {
4ef69c7a
CW
14177 encoder->base.possible_crtcs = encoder->crtc_mask;
14178 encoder->base.possible_clones =
66a9278e 14179 intel_encoder_clones(encoder);
79e53945 14180 }
47356eb6 14181
c39055b0 14182 intel_init_pch_refclk(dev_priv);
270b3042 14183
c39055b0 14184 drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
79e53945
JB
14185}
14186
14187static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14188{
14189 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 14190
ef2d633e 14191 drm_framebuffer_cleanup(fb);
70001cd2 14192
dd689287
CW
14193 i915_gem_object_lock(intel_fb->obj);
14194 WARN_ON(!intel_fb->obj->framebuffer_references--);
14195 i915_gem_object_unlock(intel_fb->obj);
14196
f8c417cd 14197 i915_gem_object_put(intel_fb->obj);
70001cd2 14198
79e53945
JB
14199 kfree(intel_fb);
14200}
14201
14202static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 14203 struct drm_file *file,
79e53945
JB
14204 unsigned int *handle)
14205{
14206 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 14207 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 14208
cc917ab4
CW
14209 if (obj->userptr.mm) {
14210 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14211 return -EINVAL;
14212 }
14213
05394f39 14214 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
14215}
14216
86c98588
RV
14217static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14218 struct drm_file *file,
14219 unsigned flags, unsigned color,
14220 struct drm_clip_rect *clips,
14221 unsigned num_clips)
14222{
5a97bcc6 14223 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
86c98588 14224
5a97bcc6 14225 i915_gem_object_flush_if_display(obj);
d59b21ec 14226 intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
86c98588
RV
14227
14228 return 0;
14229}
14230
79e53945
JB
14231static const struct drm_framebuffer_funcs intel_fb_funcs = {
14232 .destroy = intel_user_framebuffer_destroy,
14233 .create_handle = intel_user_framebuffer_create_handle,
86c98588 14234 .dirty = intel_user_framebuffer_dirty,
79e53945
JB
14235};
14236
b321803d 14237static
920a14b2
TU
14238u32 intel_fb_pitch_limit(struct drm_i915_private *dev_priv,
14239 uint64_t fb_modifier, uint32_t pixel_format)
b321803d 14240{
24dbf51a 14241 u32 gen = INTEL_GEN(dev_priv);
b321803d
DL
14242
14243 if (gen >= 9) {
ac484963
VS
14244 int cpp = drm_format_plane_cpp(pixel_format, 0);
14245
b321803d
DL
14246 /* "The stride in bytes must not exceed the of the size of 8K
14247 * pixels and 32K bytes."
14248 */
ac484963 14249 return min(8192 * cpp, 32768);
6401c37d 14250 } else if (gen >= 5 && !HAS_GMCH_DISPLAY(dev_priv)) {
b321803d
DL
14251 return 32*1024;
14252 } else if (gen >= 4) {
14253 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14254 return 16*1024;
14255 else
14256 return 32*1024;
14257 } else if (gen >= 3) {
14258 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14259 return 8*1024;
14260 else
14261 return 16*1024;
14262 } else {
14263 /* XXX DSPC is limited to 4k tiled */
14264 return 8*1024;
14265 }
14266}
14267
24dbf51a
CW
14268static int intel_framebuffer_init(struct intel_framebuffer *intel_fb,
14269 struct drm_i915_gem_object *obj,
14270 struct drm_mode_fb_cmd2 *mode_cmd)
79e53945 14271{
24dbf51a 14272 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
b3c11ac2 14273 struct drm_format_name_buf format_name;
dd689287
CW
14274 u32 pitch_limit, stride_alignment;
14275 unsigned int tiling, stride;
24dbf51a 14276 int ret = -EINVAL;
79e53945 14277
dd689287
CW
14278 i915_gem_object_lock(obj);
14279 obj->framebuffer_references++;
14280 tiling = i915_gem_object_get_tiling(obj);
14281 stride = i915_gem_object_get_stride(obj);
14282 i915_gem_object_unlock(obj);
dd4916c5 14283
2a80eada 14284 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
c2ff7370
VS
14285 /*
14286 * If there's a fence, enforce that
14287 * the fb modifier and tiling mode match.
14288 */
14289 if (tiling != I915_TILING_NONE &&
14290 tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
2a80eada 14291 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
24dbf51a 14292 goto err;
2a80eada
DV
14293 }
14294 } else {
c2ff7370 14295 if (tiling == I915_TILING_X) {
2a80eada 14296 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
c2ff7370 14297 } else if (tiling == I915_TILING_Y) {
2a80eada 14298 DRM_DEBUG("No Y tiling for legacy addfb\n");
24dbf51a 14299 goto err;
2a80eada
DV
14300 }
14301 }
14302
9a8f0a12
TU
14303 /* Passed in modifier sanity checking. */
14304 switch (mode_cmd->modifier[0]) {
14305 case I915_FORMAT_MOD_Y_TILED:
14306 case I915_FORMAT_MOD_Yf_TILED:
6315b5d3 14307 if (INTEL_GEN(dev_priv) < 9) {
9a8f0a12
TU
14308 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14309 mode_cmd->modifier[0]);
24dbf51a 14310 goto err;
9a8f0a12
TU
14311 }
14312 case DRM_FORMAT_MOD_NONE:
14313 case I915_FORMAT_MOD_X_TILED:
14314 break;
14315 default:
c0f40428
JB
14316 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14317 mode_cmd->modifier[0]);
24dbf51a 14318 goto err;
c16ed4be 14319 }
57cd6508 14320
c2ff7370
VS
14321 /*
14322 * gen2/3 display engine uses the fence if present,
14323 * so the tiling mode must match the fb modifier exactly.
14324 */
14325 if (INTEL_INFO(dev_priv)->gen < 4 &&
14326 tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
14327 DRM_DEBUG("tiling_mode must match fb modifier exactly on gen2/3\n");
9aceb5c1 14328 goto err;
c2ff7370
VS
14329 }
14330
7b49f948
VS
14331 stride_alignment = intel_fb_stride_alignment(dev_priv,
14332 mode_cmd->modifier[0],
b321803d
DL
14333 mode_cmd->pixel_format);
14334 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14335 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14336 mode_cmd->pitches[0], stride_alignment);
24dbf51a 14337 goto err;
c16ed4be 14338 }
57cd6508 14339
920a14b2 14340 pitch_limit = intel_fb_pitch_limit(dev_priv, mode_cmd->modifier[0],
b321803d 14341 mode_cmd->pixel_format);
a35cdaa0 14342 if (mode_cmd->pitches[0] > pitch_limit) {
b321803d
DL
14343 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14344 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
2a80eada 14345 "tiled" : "linear",
a35cdaa0 14346 mode_cmd->pitches[0], pitch_limit);
24dbf51a 14347 goto err;
c16ed4be 14348 }
5d7bd705 14349
c2ff7370
VS
14350 /*
14351 * If there's a fence, enforce that
14352 * the fb pitch and fence stride match.
14353 */
dd689287 14354 if (tiling != I915_TILING_NONE && mode_cmd->pitches[0] != stride) {
c16ed4be 14355 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
dd689287 14356 mode_cmd->pitches[0], stride);
24dbf51a 14357 goto err;
c16ed4be 14358 }
5d7bd705 14359
57779d06 14360 /* Reject formats not supported by any plane early. */
308e5bcb 14361 switch (mode_cmd->pixel_format) {
57779d06 14362 case DRM_FORMAT_C8:
04b3924d
VS
14363 case DRM_FORMAT_RGB565:
14364 case DRM_FORMAT_XRGB8888:
14365 case DRM_FORMAT_ARGB8888:
57779d06
VS
14366 break;
14367 case DRM_FORMAT_XRGB1555:
6315b5d3 14368 if (INTEL_GEN(dev_priv) > 3) {
b3c11ac2
EE
14369 DRM_DEBUG("unsupported pixel format: %s\n",
14370 drm_get_format_name(mode_cmd->pixel_format, &format_name));
9aceb5c1 14371 goto err;
c16ed4be 14372 }
57779d06 14373 break;
57779d06 14374 case DRM_FORMAT_ABGR8888:
920a14b2 14375 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
6315b5d3 14376 INTEL_GEN(dev_priv) < 9) {
b3c11ac2
EE
14377 DRM_DEBUG("unsupported pixel format: %s\n",
14378 drm_get_format_name(mode_cmd->pixel_format, &format_name));
9aceb5c1 14379 goto err;
6c0fd451
DL
14380 }
14381 break;
14382 case DRM_FORMAT_XBGR8888:
04b3924d 14383 case DRM_FORMAT_XRGB2101010:
57779d06 14384 case DRM_FORMAT_XBGR2101010:
6315b5d3 14385 if (INTEL_GEN(dev_priv) < 4) {
b3c11ac2
EE
14386 DRM_DEBUG("unsupported pixel format: %s\n",
14387 drm_get_format_name(mode_cmd->pixel_format, &format_name));
9aceb5c1 14388 goto err;
c16ed4be 14389 }
b5626747 14390 break;
7531208b 14391 case DRM_FORMAT_ABGR2101010:
920a14b2 14392 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
b3c11ac2
EE
14393 DRM_DEBUG("unsupported pixel format: %s\n",
14394 drm_get_format_name(mode_cmd->pixel_format, &format_name));
9aceb5c1 14395 goto err;
7531208b
DL
14396 }
14397 break;
04b3924d
VS
14398 case DRM_FORMAT_YUYV:
14399 case DRM_FORMAT_UYVY:
14400 case DRM_FORMAT_YVYU:
14401 case DRM_FORMAT_VYUY:
6315b5d3 14402 if (INTEL_GEN(dev_priv) < 5) {
b3c11ac2
EE
14403 DRM_DEBUG("unsupported pixel format: %s\n",
14404 drm_get_format_name(mode_cmd->pixel_format, &format_name));
9aceb5c1 14405 goto err;
c16ed4be 14406 }
57cd6508
CW
14407 break;
14408 default:
b3c11ac2
EE
14409 DRM_DEBUG("unsupported pixel format: %s\n",
14410 drm_get_format_name(mode_cmd->pixel_format, &format_name));
9aceb5c1 14411 goto err;
57cd6508
CW
14412 }
14413
90f9a336
VS
14414 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14415 if (mode_cmd->offsets[0] != 0)
24dbf51a 14416 goto err;
90f9a336 14417
24dbf51a
CW
14418 drm_helper_mode_fill_fb_struct(&dev_priv->drm,
14419 &intel_fb->base, mode_cmd);
c7d73f6a
DV
14420 intel_fb->obj = obj;
14421
6687c906
VS
14422 ret = intel_fill_fb_info(dev_priv, &intel_fb->base);
14423 if (ret)
9aceb5c1 14424 goto err;
2d7a215f 14425
24dbf51a
CW
14426 ret = drm_framebuffer_init(obj->base.dev,
14427 &intel_fb->base,
14428 &intel_fb_funcs);
79e53945
JB
14429 if (ret) {
14430 DRM_ERROR("framebuffer init failed %d\n", ret);
24dbf51a 14431 goto err;
79e53945
JB
14432 }
14433
79e53945 14434 return 0;
24dbf51a
CW
14435
14436err:
dd689287
CW
14437 i915_gem_object_lock(obj);
14438 obj->framebuffer_references--;
14439 i915_gem_object_unlock(obj);
24dbf51a 14440 return ret;
79e53945
JB
14441}
14442
79e53945
JB
14443static struct drm_framebuffer *
14444intel_user_framebuffer_create(struct drm_device *dev,
14445 struct drm_file *filp,
1eb83451 14446 const struct drm_mode_fb_cmd2 *user_mode_cmd)
79e53945 14447{
dcb1394e 14448 struct drm_framebuffer *fb;
05394f39 14449 struct drm_i915_gem_object *obj;
76dc3769 14450 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
79e53945 14451
03ac0642
CW
14452 obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
14453 if (!obj)
cce13ff7 14454 return ERR_PTR(-ENOENT);
79e53945 14455
24dbf51a 14456 fb = intel_framebuffer_create(obj, &mode_cmd);
dcb1394e 14457 if (IS_ERR(fb))
f0cd5182 14458 i915_gem_object_put(obj);
dcb1394e
LW
14459
14460 return fb;
79e53945
JB
14461}
14462
778e23a9
CW
14463static void intel_atomic_state_free(struct drm_atomic_state *state)
14464{
14465 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
14466
14467 drm_atomic_state_default_release(state);
14468
14469 i915_sw_fence_fini(&intel_state->commit_ready);
14470
14471 kfree(state);
14472}
14473
79e53945 14474static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 14475 .fb_create = intel_user_framebuffer_create,
0632fef6 14476 .output_poll_changed = intel_fbdev_output_poll_changed,
5ee67f1c
MR
14477 .atomic_check = intel_atomic_check,
14478 .atomic_commit = intel_atomic_commit,
de419ab6
ML
14479 .atomic_state_alloc = intel_atomic_state_alloc,
14480 .atomic_state_clear = intel_atomic_state_clear,
778e23a9 14481 .atomic_state_free = intel_atomic_state_free,
79e53945
JB
14482};
14483
88212941
ID
14484/**
14485 * intel_init_display_hooks - initialize the display modesetting hooks
14486 * @dev_priv: device private
14487 */
14488void intel_init_display_hooks(struct drm_i915_private *dev_priv)
e70236a8 14489{
7ff89ca2
VS
14490 intel_init_cdclk_hooks(dev_priv);
14491
88212941 14492 if (INTEL_INFO(dev_priv)->gen >= 9) {
bc8d7dff 14493 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14494 dev_priv->display.get_initial_plane_config =
14495 skylake_get_initial_plane_config;
bc8d7dff
DL
14496 dev_priv->display.crtc_compute_clock =
14497 haswell_crtc_compute_clock;
14498 dev_priv->display.crtc_enable = haswell_crtc_enable;
14499 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 14500 } else if (HAS_DDI(dev_priv)) {
0e8ffe1b 14501 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14502 dev_priv->display.get_initial_plane_config =
14503 ironlake_get_initial_plane_config;
797d0259
ACO
14504 dev_priv->display.crtc_compute_clock =
14505 haswell_crtc_compute_clock;
4f771f10
PZ
14506 dev_priv->display.crtc_enable = haswell_crtc_enable;
14507 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 14508 } else if (HAS_PCH_SPLIT(dev_priv)) {
0e8ffe1b 14509 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
5724dbd1
DL
14510 dev_priv->display.get_initial_plane_config =
14511 ironlake_get_initial_plane_config;
3fb37703
ACO
14512 dev_priv->display.crtc_compute_clock =
14513 ironlake_crtc_compute_clock;
76e5a89c
DV
14514 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14515 dev_priv->display.crtc_disable = ironlake_crtc_disable;
65b3d6a9 14516 } else if (IS_CHERRYVIEW(dev_priv)) {
89b667f8 14517 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14518 dev_priv->display.get_initial_plane_config =
14519 i9xx_get_initial_plane_config;
65b3d6a9
ACO
14520 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
14521 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14522 dev_priv->display.crtc_disable = i9xx_crtc_disable;
14523 } else if (IS_VALLEYVIEW(dev_priv)) {
14524 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14525 dev_priv->display.get_initial_plane_config =
14526 i9xx_get_initial_plane_config;
14527 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
89b667f8
JB
14528 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14529 dev_priv->display.crtc_disable = i9xx_crtc_disable;
19ec6693
ACO
14530 } else if (IS_G4X(dev_priv)) {
14531 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14532 dev_priv->display.get_initial_plane_config =
14533 i9xx_get_initial_plane_config;
14534 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
14535 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14536 dev_priv->display.crtc_disable = i9xx_crtc_disable;
70e8aa21
ACO
14537 } else if (IS_PINEVIEW(dev_priv)) {
14538 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14539 dev_priv->display.get_initial_plane_config =
14540 i9xx_get_initial_plane_config;
14541 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
14542 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14543 dev_priv->display.crtc_disable = i9xx_crtc_disable;
81c97f52 14544 } else if (!IS_GEN2(dev_priv)) {
0e8ffe1b 14545 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14546 dev_priv->display.get_initial_plane_config =
14547 i9xx_get_initial_plane_config;
d6dfee7a 14548 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
76e5a89c
DV
14549 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14550 dev_priv->display.crtc_disable = i9xx_crtc_disable;
81c97f52
ACO
14551 } else {
14552 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14553 dev_priv->display.get_initial_plane_config =
14554 i9xx_get_initial_plane_config;
14555 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
14556 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14557 dev_priv->display.crtc_disable = i9xx_crtc_disable;
f564048e 14558 }
e70236a8 14559
88212941 14560 if (IS_GEN5(dev_priv)) {
3bb11b53 14561 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
88212941 14562 } else if (IS_GEN6(dev_priv)) {
3bb11b53 14563 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
88212941 14564 } else if (IS_IVYBRIDGE(dev_priv)) {
3bb11b53
SJ
14565 /* FIXME: detect B0+ stepping and use auto training */
14566 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
88212941 14567 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
3bb11b53 14568 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
445e780b
VS
14569 }
14570
27082493
L
14571 if (dev_priv->info.gen >= 9)
14572 dev_priv->display.update_crtcs = skl_update_crtcs;
14573 else
14574 dev_priv->display.update_crtcs = intel_update_crtcs;
14575
5a21b665
DV
14576 switch (INTEL_INFO(dev_priv)->gen) {
14577 case 2:
14578 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14579 break;
14580
14581 case 3:
14582 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14583 break;
14584
14585 case 4:
14586 case 5:
14587 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14588 break;
14589
14590 case 6:
14591 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14592 break;
14593 case 7:
14594 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
14595 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14596 break;
14597 case 9:
14598 /* Drop through - unsupported since execlist only. */
14599 default:
14600 /* Default just returns -ENODEV to indicate unsupported */
14601 dev_priv->display.queue_flip = intel_default_queue_flip;
14602 }
e70236a8
JB
14603}
14604
b690e96c
JB
14605/*
14606 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14607 * resume, or other times. This quirk makes sure that's the case for
14608 * affected systems.
14609 */
0206e353 14610static void quirk_pipea_force(struct drm_device *dev)
b690e96c 14611{
fac5e23e 14612 struct drm_i915_private *dev_priv = to_i915(dev);
b690e96c
JB
14613
14614 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
bc0daf48 14615 DRM_INFO("applying pipe a force quirk\n");
b690e96c
JB
14616}
14617
b6b5d049
VS
14618static void quirk_pipeb_force(struct drm_device *dev)
14619{
fac5e23e 14620 struct drm_i915_private *dev_priv = to_i915(dev);
b6b5d049
VS
14621
14622 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14623 DRM_INFO("applying pipe b force quirk\n");
14624}
14625
435793df
KP
14626/*
14627 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14628 */
14629static void quirk_ssc_force_disable(struct drm_device *dev)
14630{
fac5e23e 14631 struct drm_i915_private *dev_priv = to_i915(dev);
435793df 14632 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 14633 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
14634}
14635
4dca20ef 14636/*
5a15ab5b
CE
14637 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14638 * brightness value
4dca20ef
CE
14639 */
14640static void quirk_invert_brightness(struct drm_device *dev)
14641{
fac5e23e 14642 struct drm_i915_private *dev_priv = to_i915(dev);
4dca20ef 14643 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 14644 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
14645}
14646
9c72cc6f
SD
14647/* Some VBT's incorrectly indicate no backlight is present */
14648static void quirk_backlight_present(struct drm_device *dev)
14649{
fac5e23e 14650 struct drm_i915_private *dev_priv = to_i915(dev);
9c72cc6f
SD
14651 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14652 DRM_INFO("applying backlight present quirk\n");
14653}
14654
b690e96c
JB
14655struct intel_quirk {
14656 int device;
14657 int subsystem_vendor;
14658 int subsystem_device;
14659 void (*hook)(struct drm_device *dev);
14660};
14661
5f85f176
EE
14662/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14663struct intel_dmi_quirk {
14664 void (*hook)(struct drm_device *dev);
14665 const struct dmi_system_id (*dmi_id_list)[];
14666};
14667
14668static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14669{
14670 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14671 return 1;
14672}
14673
14674static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14675 {
14676 .dmi_id_list = &(const struct dmi_system_id[]) {
14677 {
14678 .callback = intel_dmi_reverse_brightness,
14679 .ident = "NCR Corporation",
14680 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14681 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14682 },
14683 },
14684 { } /* terminating entry */
14685 },
14686 .hook = quirk_invert_brightness,
14687 },
14688};
14689
c43b5634 14690static struct intel_quirk intel_quirks[] = {
b690e96c
JB
14691 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
14692 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
14693
b690e96c
JB
14694 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
14695 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
14696
5f080c0f
VS
14697 /* 830 needs to leave pipe A & dpll A up */
14698 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
14699
b6b5d049
VS
14700 /* 830 needs to leave pipe B & dpll B up */
14701 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
14702
435793df
KP
14703 /* Lenovo U160 cannot use SSC on LVDS */
14704 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
14705
14706 /* Sony Vaio Y cannot use SSC on LVDS */
14707 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 14708
be505f64
AH
14709 /* Acer Aspire 5734Z must invert backlight brightness */
14710 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14711
14712 /* Acer/eMachines G725 */
14713 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14714
14715 /* Acer/eMachines e725 */
14716 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
14717
14718 /* Acer/Packard Bell NCL20 */
14719 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
14720
14721 /* Acer Aspire 4736Z */
14722 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
14723
14724 /* Acer Aspire 5336 */
14725 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
2e93a1aa
SD
14726
14727 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
14728 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
d4967d8c 14729
dfb3d47b
SD
14730 /* Acer C720 Chromebook (Core i3 4005U) */
14731 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
14732
b2a9601c 14733 /* Apple Macbook 2,1 (Core 2 T7400) */
14734 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
14735
1b9448b0
JN
14736 /* Apple Macbook 4,1 */
14737 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
14738
d4967d8c
SD
14739 /* Toshiba CB35 Chromebook (Celeron 2955U) */
14740 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
724cb06f
SD
14741
14742 /* HP Chromebook 14 (Celeron 2955U) */
14743 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
cf6f0af9
JN
14744
14745 /* Dell Chromebook 11 */
14746 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
9be64eee
JN
14747
14748 /* Dell Chromebook 11 (2015 version) */
14749 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
b690e96c
JB
14750};
14751
14752static void intel_init_quirks(struct drm_device *dev)
14753{
14754 struct pci_dev *d = dev->pdev;
14755 int i;
14756
14757 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
14758 struct intel_quirk *q = &intel_quirks[i];
14759
14760 if (d->device == q->device &&
14761 (d->subsystem_vendor == q->subsystem_vendor ||
14762 q->subsystem_vendor == PCI_ANY_ID) &&
14763 (d->subsystem_device == q->subsystem_device ||
14764 q->subsystem_device == PCI_ANY_ID))
14765 q->hook(dev);
14766 }
5f85f176
EE
14767 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
14768 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
14769 intel_dmi_quirks[i].hook(dev);
14770 }
b690e96c
JB
14771}
14772
9cce37f4 14773/* Disable the VGA plane that we never use */
29b74b7f 14774static void i915_disable_vga(struct drm_i915_private *dev_priv)
9cce37f4 14775{
52a05c30 14776 struct pci_dev *pdev = dev_priv->drm.pdev;
9cce37f4 14777 u8 sr1;
920a14b2 14778 i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
9cce37f4 14779
2b37c616 14780 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
52a05c30 14781 vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 14782 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
14783 sr1 = inb(VGA_SR_DATA);
14784 outb(sr1 | 1<<5, VGA_SR_DATA);
52a05c30 14785 vga_put(pdev, VGA_RSRC_LEGACY_IO);
9cce37f4
JB
14786 udelay(300);
14787
01f5a626 14788 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9cce37f4
JB
14789 POSTING_READ(vga_reg);
14790}
14791
f817586c
DV
14792void intel_modeset_init_hw(struct drm_device *dev)
14793{
fac5e23e 14794 struct drm_i915_private *dev_priv = to_i915(dev);
1a617b77 14795
4c75b940 14796 intel_update_cdclk(dev_priv);
bb0f4aab 14797 dev_priv->cdclk.logical = dev_priv->cdclk.actual = dev_priv->cdclk.hw;
1a617b77 14798
46f16e63 14799 intel_init_clock_gating(dev_priv);
f817586c
DV
14800}
14801
d93c0372
MR
14802/*
14803 * Calculate what we think the watermarks should be for the state we've read
14804 * out of the hardware and then immediately program those watermarks so that
14805 * we ensure the hardware settings match our internal state.
14806 *
14807 * We can calculate what we think WM's should be by creating a duplicate of the
14808 * current state (which was constructed during hardware readout) and running it
14809 * through the atomic check code to calculate new watermark values in the
14810 * state object.
14811 */
14812static void sanitize_watermarks(struct drm_device *dev)
14813{
14814 struct drm_i915_private *dev_priv = to_i915(dev);
14815 struct drm_atomic_state *state;
ccf010fb 14816 struct intel_atomic_state *intel_state;
d93c0372
MR
14817 struct drm_crtc *crtc;
14818 struct drm_crtc_state *cstate;
14819 struct drm_modeset_acquire_ctx ctx;
14820 int ret;
14821 int i;
14822
14823 /* Only supported on platforms that use atomic watermark design */
ed4a6a7c 14824 if (!dev_priv->display.optimize_watermarks)
d93c0372
MR
14825 return;
14826
14827 /*
14828 * We need to hold connection_mutex before calling duplicate_state so
14829 * that the connector loop is protected.
14830 */
14831 drm_modeset_acquire_init(&ctx, 0);
14832retry:
0cd1262d 14833 ret = drm_modeset_lock_all_ctx(dev, &ctx);
d93c0372
MR
14834 if (ret == -EDEADLK) {
14835 drm_modeset_backoff(&ctx);
14836 goto retry;
14837 } else if (WARN_ON(ret)) {
0cd1262d 14838 goto fail;
d93c0372
MR
14839 }
14840
14841 state = drm_atomic_helper_duplicate_state(dev, &ctx);
14842 if (WARN_ON(IS_ERR(state)))
0cd1262d 14843 goto fail;
d93c0372 14844
ccf010fb
ML
14845 intel_state = to_intel_atomic_state(state);
14846
ed4a6a7c
MR
14847 /*
14848 * Hardware readout is the only time we don't want to calculate
14849 * intermediate watermarks (since we don't trust the current
14850 * watermarks).
14851 */
ccf010fb 14852 intel_state->skip_intermediate_wm = true;
ed4a6a7c 14853
d93c0372
MR
14854 ret = intel_atomic_check(dev, state);
14855 if (ret) {
14856 /*
14857 * If we fail here, it means that the hardware appears to be
14858 * programmed in a way that shouldn't be possible, given our
14859 * understanding of watermark requirements. This might mean a
14860 * mistake in the hardware readout code or a mistake in the
14861 * watermark calculations for a given platform. Raise a WARN
14862 * so that this is noticeable.
14863 *
14864 * If this actually happens, we'll have to just leave the
14865 * BIOS-programmed watermarks untouched and hope for the best.
14866 */
14867 WARN(true, "Could not determine valid watermarks for inherited state\n");
b9a1b717 14868 goto put_state;
d93c0372
MR
14869 }
14870
14871 /* Write calculated watermark values back */
d93c0372
MR
14872 for_each_crtc_in_state(state, crtc, cstate, i) {
14873 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
14874
ed4a6a7c 14875 cs->wm.need_postvbl_update = true;
ccf010fb 14876 dev_priv->display.optimize_watermarks(intel_state, cs);
d93c0372
MR
14877 }
14878
b9a1b717 14879put_state:
0853695c 14880 drm_atomic_state_put(state);
0cd1262d 14881fail:
d93c0372
MR
14882 drm_modeset_drop_locks(&ctx);
14883 drm_modeset_acquire_fini(&ctx);
14884}
14885
b079bd17 14886int intel_modeset_init(struct drm_device *dev)
79e53945 14887{
72e96d64
JL
14888 struct drm_i915_private *dev_priv = to_i915(dev);
14889 struct i915_ggtt *ggtt = &dev_priv->ggtt;
8cc87b75 14890 enum pipe pipe;
46f297fb 14891 struct intel_crtc *crtc;
79e53945
JB
14892
14893 drm_mode_config_init(dev);
14894
14895 dev->mode_config.min_width = 0;
14896 dev->mode_config.min_height = 0;
14897
019d96cb
DA
14898 dev->mode_config.preferred_depth = 24;
14899 dev->mode_config.prefer_shadow = 1;
14900
25bab385
TU
14901 dev->mode_config.allow_fb_modifiers = true;
14902
e6ecefaa 14903 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 14904
eb955eee 14905 INIT_WORK(&dev_priv->atomic_helper.free_work,
ba318c61 14906 intel_atomic_helper_free_state_worker);
eb955eee 14907
b690e96c
JB
14908 intel_init_quirks(dev);
14909
62d75df7 14910 intel_init_pm(dev_priv);
1fa61106 14911
b7f05d4a 14912 if (INTEL_INFO(dev_priv)->num_pipes == 0)
b079bd17 14913 return 0;
e3c74757 14914
69f92f67
LW
14915 /*
14916 * There may be no VBT; and if the BIOS enabled SSC we can
14917 * just keep using it to avoid unnecessary flicker. Whereas if the
14918 * BIOS isn't using it, don't assume it will work even if the VBT
14919 * indicates as much.
14920 */
6e266956 14921 if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
69f92f67
LW
14922 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
14923 DREF_SSC1_ENABLE);
14924
14925 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
14926 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
14927 bios_lvds_use_ssc ? "en" : "dis",
14928 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
14929 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
14930 }
14931 }
14932
5db94019 14933 if (IS_GEN2(dev_priv)) {
a6c45cf0
CW
14934 dev->mode_config.max_width = 2048;
14935 dev->mode_config.max_height = 2048;
5db94019 14936 } else if (IS_GEN3(dev_priv)) {
5e4d6fa7
KP
14937 dev->mode_config.max_width = 4096;
14938 dev->mode_config.max_height = 4096;
79e53945 14939 } else {
a6c45cf0
CW
14940 dev->mode_config.max_width = 8192;
14941 dev->mode_config.max_height = 8192;
79e53945 14942 }
068be561 14943
2a307c2e
JN
14944 if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
14945 dev->mode_config.cursor_width = IS_I845G(dev_priv) ? 64 : 512;
dc41c154 14946 dev->mode_config.cursor_height = 1023;
5db94019 14947 } else if (IS_GEN2(dev_priv)) {
068be561
DL
14948 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
14949 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
14950 } else {
14951 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
14952 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
14953 }
14954
72e96d64 14955 dev->mode_config.fb_base = ggtt->mappable_base;
79e53945 14956
28c97730 14957 DRM_DEBUG_KMS("%d display pipe%s available.\n",
b7f05d4a
TU
14958 INTEL_INFO(dev_priv)->num_pipes,
14959 INTEL_INFO(dev_priv)->num_pipes > 1 ? "s" : "");
79e53945 14960
055e393f 14961 for_each_pipe(dev_priv, pipe) {
b079bd17
VS
14962 int ret;
14963
5ab0d85b 14964 ret = intel_crtc_init(dev_priv, pipe);
b079bd17
VS
14965 if (ret) {
14966 drm_mode_config_cleanup(dev);
14967 return ret;
14968 }
79e53945
JB
14969 }
14970
e72f9fbf 14971 intel_shared_dpll_init(dev);
ee7b9f93 14972
5be6e334
VS
14973 intel_update_czclk(dev_priv);
14974 intel_modeset_init_hw(dev);
14975
b2045352 14976 if (dev_priv->max_cdclk_freq == 0)
4c75b940 14977 intel_update_max_cdclk(dev_priv);
b2045352 14978
9cce37f4 14979 /* Just disable it once at startup */
29b74b7f 14980 i915_disable_vga(dev_priv);
c39055b0 14981 intel_setup_outputs(dev_priv);
11be49eb 14982
6e9f798d 14983 drm_modeset_lock_all(dev);
043e9bda 14984 intel_modeset_setup_hw_state(dev);
6e9f798d 14985 drm_modeset_unlock_all(dev);
46f297fb 14986
d3fcc808 14987 for_each_intel_crtc(dev, crtc) {
eeebeac5
ML
14988 struct intel_initial_plane_config plane_config = {};
14989
46f297fb
JB
14990 if (!crtc->active)
14991 continue;
14992
46f297fb 14993 /*
46f297fb
JB
14994 * Note that reserving the BIOS fb up front prevents us
14995 * from stuffing other stolen allocations like the ring
14996 * on top. This prevents some ugliness at boot time, and
14997 * can even allow for smooth boot transitions if the BIOS
14998 * fb is large enough for the active pipe configuration.
14999 */
eeebeac5
ML
15000 dev_priv->display.get_initial_plane_config(crtc,
15001 &plane_config);
15002
15003 /*
15004 * If the fb is shared between multiple heads, we'll
15005 * just get the first one.
15006 */
15007 intel_find_initial_plane_obj(crtc, &plane_config);
46f297fb 15008 }
d93c0372
MR
15009
15010 /*
15011 * Make sure hardware watermarks really match the state we read out.
15012 * Note that we need to do this after reconstructing the BIOS fb's
15013 * since the watermark calculation done here will use pstate->fb.
15014 */
15015 sanitize_watermarks(dev);
b079bd17
VS
15016
15017 return 0;
2c7111db
CW
15018}
15019
7fad798e
DV
15020static void intel_enable_pipe_a(struct drm_device *dev)
15021{
15022 struct intel_connector *connector;
15023 struct drm_connector *crt = NULL;
15024 struct intel_load_detect_pipe load_detect_temp;
208bf9fd 15025 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
7fad798e
DV
15026
15027 /* We can't just switch on the pipe A, we need to set things up with a
15028 * proper mode and output configuration. As a gross hack, enable pipe A
15029 * by enabling the load detect pipe once. */
3a3371ff 15030 for_each_intel_connector(dev, connector) {
7fad798e
DV
15031 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15032 crt = &connector->base;
15033 break;
15034 }
15035 }
15036
15037 if (!crt)
15038 return;
15039
208bf9fd 15040 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
49172fee 15041 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
7fad798e
DV
15042}
15043
fa555837
DV
15044static bool
15045intel_check_plane_mapping(struct intel_crtc *crtc)
15046{
b7f05d4a 15047 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
649636ef 15048 u32 val;
fa555837 15049
b7f05d4a 15050 if (INTEL_INFO(dev_priv)->num_pipes == 1)
fa555837
DV
15051 return true;
15052
649636ef 15053 val = I915_READ(DSPCNTR(!crtc->plane));
fa555837
DV
15054
15055 if ((val & DISPLAY_PLANE_ENABLE) &&
15056 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15057 return false;
15058
15059 return true;
15060}
15061
02e93c35
VS
15062static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15063{
15064 struct drm_device *dev = crtc->base.dev;
15065 struct intel_encoder *encoder;
15066
15067 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15068 return true;
15069
15070 return false;
15071}
15072
496b0fc3
ML
15073static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
15074{
15075 struct drm_device *dev = encoder->base.dev;
15076 struct intel_connector *connector;
15077
15078 for_each_connector_on_encoder(dev, &encoder->base, connector)
15079 return connector;
15080
15081 return NULL;
15082}
15083
a168f5b3
VS
15084static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
15085 enum transcoder pch_transcoder)
15086{
15087 return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
15088 (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == TRANSCODER_A);
15089}
15090
24929352
DV
15091static void intel_sanitize_crtc(struct intel_crtc *crtc)
15092{
15093 struct drm_device *dev = crtc->base.dev;
fac5e23e 15094 struct drm_i915_private *dev_priv = to_i915(dev);
4d1de975 15095 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
24929352 15096
24929352 15097 /* Clear any frame start delays used for debugging left by the BIOS */
4d1de975
JN
15098 if (!transcoder_is_dsi(cpu_transcoder)) {
15099 i915_reg_t reg = PIPECONF(cpu_transcoder);
15100
15101 I915_WRITE(reg,
15102 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15103 }
24929352 15104
d3eaf884 15105 /* restore vblank interrupts to correct state */
9625604c 15106 drm_crtc_vblank_reset(&crtc->base);
d297e103 15107 if (crtc->active) {
f9cd7b88
VS
15108 struct intel_plane *plane;
15109
9625604c 15110 drm_crtc_vblank_on(&crtc->base);
f9cd7b88
VS
15111
15112 /* Disable everything but the primary plane */
15113 for_each_intel_plane_on_crtc(dev, crtc, plane) {
15114 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
15115 continue;
15116
15117 plane->disable_plane(&plane->base, &crtc->base);
15118 }
9625604c 15119 }
d3eaf884 15120
24929352 15121 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
DV
15122 * disable the crtc (and hence change the state) if it is wrong. Note
15123 * that gen4+ has a fixed plane -> pipe mapping. */
6315b5d3 15124 if (INTEL_GEN(dev_priv) < 4 && !intel_check_plane_mapping(crtc)) {
24929352
DV
15125 bool plane;
15126
78108b7c
VS
15127 DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
15128 crtc->base.base.id, crtc->base.name);
24929352
DV
15129
15130 /* Pipe has the wrong plane attached and the plane is active.
15131 * Temporarily change the plane mapping and disable everything
15132 * ... */
15133 plane = crtc->plane;
1d4258db 15134 crtc->base.primary->state->visible = true;
24929352 15135 crtc->plane = !plane;
b17d48e2 15136 intel_crtc_disable_noatomic(&crtc->base);
24929352 15137 crtc->plane = plane;
24929352 15138 }
24929352 15139
7fad798e
DV
15140 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15141 crtc->pipe == PIPE_A && !crtc->active) {
15142 /* BIOS forgot to enable pipe A, this mostly happens after
15143 * resume. Force-enable the pipe to fix this, the update_dpms
15144 * call below we restore the pipe to the right state, but leave
15145 * the required bits on. */
15146 intel_enable_pipe_a(dev);
15147 }
15148
24929352
DV
15149 /* Adjust the state of the output pipe according to whether we
15150 * have active connectors/encoders. */
842e0307 15151 if (crtc->active && !intel_crtc_has_encoders(crtc))
b17d48e2 15152 intel_crtc_disable_noatomic(&crtc->base);
24929352 15153
49cff963 15154 if (crtc->active || HAS_GMCH_DISPLAY(dev_priv)) {
4cc31489
DV
15155 /*
15156 * We start out with underrun reporting disabled to avoid races.
15157 * For correct bookkeeping mark this on active crtcs.
15158 *
c5ab3bc0
DV
15159 * Also on gmch platforms we dont have any hardware bits to
15160 * disable the underrun reporting. Which means we need to start
15161 * out with underrun reporting disabled also on inactive pipes,
15162 * since otherwise we'll complain about the garbage we read when
15163 * e.g. coming up after runtime pm.
15164 *
4cc31489
DV
15165 * No protection against concurrent access is required - at
15166 * worst a fifo underrun happens which also sets this to false.
15167 */
15168 crtc->cpu_fifo_underrun_disabled = true;
a168f5b3
VS
15169 /*
15170 * We track the PCH trancoder underrun reporting state
15171 * within the crtc. With crtc for pipe A housing the underrun
15172 * reporting state for PCH transcoder A, crtc for pipe B housing
15173 * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
15174 * and marking underrun reporting as disabled for the non-existing
15175 * PCH transcoders B and C would prevent enabling the south
15176 * error interrupt (see cpt_can_enable_serr_int()).
15177 */
15178 if (has_pch_trancoder(dev_priv, (enum transcoder)crtc->pipe))
15179 crtc->pch_fifo_underrun_disabled = true;
4cc31489 15180 }
24929352
DV
15181}
15182
15183static void intel_sanitize_encoder(struct intel_encoder *encoder)
15184{
15185 struct intel_connector *connector;
24929352
DV
15186
15187 /* We need to check both for a crtc link (meaning that the
15188 * encoder is active and trying to read from a pipe) and the
15189 * pipe itself being active. */
15190 bool has_active_crtc = encoder->base.crtc &&
15191 to_intel_crtc(encoder->base.crtc)->active;
15192
496b0fc3
ML
15193 connector = intel_encoder_find_connector(encoder);
15194 if (connector && !has_active_crtc) {
24929352
DV
15195 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15196 encoder->base.base.id,
8e329a03 15197 encoder->base.name);
24929352
DV
15198
15199 /* Connector is active, but has no active pipe. This is
15200 * fallout from our resume register restoring. Disable
15201 * the encoder manually again. */
15202 if (encoder->base.crtc) {
fd6bbda9
ML
15203 struct drm_crtc_state *crtc_state = encoder->base.crtc->state;
15204
24929352
DV
15205 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15206 encoder->base.base.id,
8e329a03 15207 encoder->base.name);
fd6bbda9 15208 encoder->disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
a62d1497 15209 if (encoder->post_disable)
fd6bbda9 15210 encoder->post_disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
24929352 15211 }
7f1950fb 15212 encoder->base.crtc = NULL;
24929352
DV
15213
15214 /* Inconsistent output/port/pipe state happens presumably due to
15215 * a bug in one of the get_hw_state functions. Or someplace else
15216 * in our code, like the register restore mess on resume. Clamp
15217 * things to off as a safer default. */
fd6bbda9
ML
15218
15219 connector->base.dpms = DRM_MODE_DPMS_OFF;
15220 connector->base.encoder = NULL;
24929352
DV
15221 }
15222 /* Enabled encoders without active connectors will be fixed in
15223 * the crtc fixup. */
15224}
15225
29b74b7f 15226void i915_redisable_vga_power_on(struct drm_i915_private *dev_priv)
0fde901f 15227{
920a14b2 15228 i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
0fde901f 15229
04098753
ID
15230 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15231 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
29b74b7f 15232 i915_disable_vga(dev_priv);
04098753
ID
15233 }
15234}
15235
29b74b7f 15236void i915_redisable_vga(struct drm_i915_private *dev_priv)
04098753 15237{
8dc8a27c
PZ
15238 /* This function can be called both from intel_modeset_setup_hw_state or
15239 * at a very early point in our resume sequence, where the power well
15240 * structures are not yet restored. Since this function is at a very
15241 * paranoid "someone might have enabled VGA while we were not looking"
15242 * level, just check if the power well is enabled instead of trying to
15243 * follow the "don't touch the power well if we don't need it" policy
15244 * the rest of the driver uses. */
6392f847 15245 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
15246 return;
15247
29b74b7f 15248 i915_redisable_vga_power_on(dev_priv);
6392f847
ID
15249
15250 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
0fde901f
KM
15251}
15252
f9cd7b88 15253static bool primary_get_hw_state(struct intel_plane *plane)
98ec7739 15254{
f9cd7b88 15255 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
98ec7739 15256
f9cd7b88 15257 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
d032ffa0
ML
15258}
15259
f9cd7b88
VS
15260/* FIXME read out full plane state for all planes */
15261static void readout_plane_state(struct intel_crtc *crtc)
d032ffa0 15262{
b26d3ea3 15263 struct drm_plane *primary = crtc->base.primary;
f9cd7b88 15264 struct intel_plane_state *plane_state =
b26d3ea3 15265 to_intel_plane_state(primary->state);
d032ffa0 15266
936e71e3 15267 plane_state->base.visible = crtc->active &&
b26d3ea3
ML
15268 primary_get_hw_state(to_intel_plane(primary));
15269
936e71e3 15270 if (plane_state->base.visible)
b26d3ea3 15271 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
98ec7739
VS
15272}
15273
30e984df 15274static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352 15275{
fac5e23e 15276 struct drm_i915_private *dev_priv = to_i915(dev);
24929352 15277 enum pipe pipe;
24929352
DV
15278 struct intel_crtc *crtc;
15279 struct intel_encoder *encoder;
15280 struct intel_connector *connector;
5358901f 15281 int i;
24929352 15282
565602d7
ML
15283 dev_priv->active_crtcs = 0;
15284
d3fcc808 15285 for_each_intel_crtc(dev, crtc) {
a8cd6da0
VS
15286 struct intel_crtc_state *crtc_state =
15287 to_intel_crtc_state(crtc->base.state);
3b117c8f 15288
ec2dc6a0 15289 __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
565602d7
ML
15290 memset(crtc_state, 0, sizeof(*crtc_state));
15291 crtc_state->base.crtc = &crtc->base;
24929352 15292
565602d7
ML
15293 crtc_state->base.active = crtc_state->base.enable =
15294 dev_priv->display.get_pipe_config(crtc, crtc_state);
15295
15296 crtc->base.enabled = crtc_state->base.enable;
15297 crtc->active = crtc_state->base.active;
15298
aca1ebf4 15299 if (crtc_state->base.active)
565602d7
ML
15300 dev_priv->active_crtcs |= 1 << crtc->pipe;
15301
f9cd7b88 15302 readout_plane_state(crtc);
24929352 15303
78108b7c
VS
15304 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
15305 crtc->base.base.id, crtc->base.name,
a8cd6da0 15306 enableddisabled(crtc_state->base.active));
24929352
DV
15307 }
15308
5358901f
DV
15309 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15310 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15311
2edd6443 15312 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
2c42e535
ACO
15313 &pll->state.hw_state);
15314 pll->state.crtc_mask = 0;
d3fcc808 15315 for_each_intel_crtc(dev, crtc) {
a8cd6da0
VS
15316 struct intel_crtc_state *crtc_state =
15317 to_intel_crtc_state(crtc->base.state);
15318
15319 if (crtc_state->base.active &&
15320 crtc_state->shared_dpll == pll)
2c42e535 15321 pll->state.crtc_mask |= 1 << crtc->pipe;
5358901f 15322 }
2c42e535 15323 pll->active_mask = pll->state.crtc_mask;
5358901f 15324
1e6f2ddc 15325 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
2c42e535 15326 pll->name, pll->state.crtc_mask, pll->on);
5358901f
DV
15327 }
15328
b2784e15 15329 for_each_intel_encoder(dev, encoder) {
24929352
DV
15330 pipe = 0;
15331
15332 if (encoder->get_hw_state(encoder, &pipe)) {
a8cd6da0
VS
15333 struct intel_crtc_state *crtc_state;
15334
98187836 15335 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
a8cd6da0 15336 crtc_state = to_intel_crtc_state(crtc->base.state);
e2af48c6 15337
045ac3b5 15338 encoder->base.crtc = &crtc->base;
a8cd6da0
VS
15339 crtc_state->output_types |= 1 << encoder->type;
15340 encoder->get_config(encoder, crtc_state);
24929352
DV
15341 } else {
15342 encoder->base.crtc = NULL;
15343 }
15344
6f2bcceb 15345 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
08c4d7fc
TU
15346 encoder->base.base.id, encoder->base.name,
15347 enableddisabled(encoder->base.crtc),
6f2bcceb 15348 pipe_name(pipe));
24929352
DV
15349 }
15350
3a3371ff 15351 for_each_intel_connector(dev, connector) {
24929352
DV
15352 if (connector->get_hw_state(connector)) {
15353 connector->base.dpms = DRM_MODE_DPMS_ON;
2aa974c9
ML
15354
15355 encoder = connector->encoder;
15356 connector->base.encoder = &encoder->base;
15357
15358 if (encoder->base.crtc &&
15359 encoder->base.crtc->state->active) {
15360 /*
15361 * This has to be done during hardware readout
15362 * because anything calling .crtc_disable may
15363 * rely on the connector_mask being accurate.
15364 */
15365 encoder->base.crtc->state->connector_mask |=
15366 1 << drm_connector_index(&connector->base);
e87a52b3
ML
15367 encoder->base.crtc->state->encoder_mask |=
15368 1 << drm_encoder_index(&encoder->base);
2aa974c9
ML
15369 }
15370
24929352
DV
15371 } else {
15372 connector->base.dpms = DRM_MODE_DPMS_OFF;
15373 connector->base.encoder = NULL;
15374 }
15375 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
08c4d7fc
TU
15376 connector->base.base.id, connector->base.name,
15377 enableddisabled(connector->base.encoder));
24929352 15378 }
7f4c6284
VS
15379
15380 for_each_intel_crtc(dev, crtc) {
a8cd6da0
VS
15381 struct intel_crtc_state *crtc_state =
15382 to_intel_crtc_state(crtc->base.state);
aca1ebf4
VS
15383 int pixclk = 0;
15384
a8cd6da0 15385 crtc->base.hwmode = crtc_state->base.adjusted_mode;
7f4c6284
VS
15386
15387 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
a8cd6da0
VS
15388 if (crtc_state->base.active) {
15389 intel_mode_from_pipe_config(&crtc->base.mode, crtc_state);
15390 intel_mode_from_pipe_config(&crtc_state->base.adjusted_mode, crtc_state);
7f4c6284
VS
15391 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
15392
15393 /*
15394 * The initial mode needs to be set in order to keep
15395 * the atomic core happy. It wants a valid mode if the
15396 * crtc's enabled, so we do the above call.
15397 *
7800fb69
DV
15398 * But we don't set all the derived state fully, hence
15399 * set a flag to indicate that a full recalculation is
15400 * needed on the next commit.
7f4c6284 15401 */
a8cd6da0 15402 crtc_state->base.mode.private_flags = I915_MODE_FLAG_INHERITED;
9eca6832 15403
a7d1b3f4
VS
15404 intel_crtc_compute_pixel_rate(crtc_state);
15405
15406 if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv) ||
15407 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
15408 pixclk = crtc_state->pixel_rate;
aca1ebf4
VS
15409 else
15410 WARN_ON(dev_priv->display.modeset_calc_cdclk);
15411
15412 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
a8cd6da0 15413 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
aca1ebf4
VS
15414 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
15415
9eca6832
VS
15416 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
15417 update_scanline_offset(crtc);
7f4c6284 15418 }
e3b247da 15419
aca1ebf4
VS
15420 dev_priv->min_pixclk[crtc->pipe] = pixclk;
15421
a8cd6da0 15422 intel_pipe_config_sanity_check(dev_priv, crtc_state);
7f4c6284 15423 }
30e984df
DV
15424}
15425
62b69566
ACO
15426static void
15427get_encoder_power_domains(struct drm_i915_private *dev_priv)
15428{
15429 struct intel_encoder *encoder;
15430
15431 for_each_intel_encoder(&dev_priv->drm, encoder) {
15432 u64 get_domains;
15433 enum intel_display_power_domain domain;
15434
15435 if (!encoder->get_power_domains)
15436 continue;
15437
15438 get_domains = encoder->get_power_domains(encoder);
15439 for_each_power_domain(domain, get_domains)
15440 intel_display_power_get(dev_priv, domain);
15441 }
15442}
15443
043e9bda
ML
15444/* Scan out the current hw modeset state,
15445 * and sanitizes it to the current state
15446 */
15447static void
15448intel_modeset_setup_hw_state(struct drm_device *dev)
30e984df 15449{
fac5e23e 15450 struct drm_i915_private *dev_priv = to_i915(dev);
30e984df 15451 enum pipe pipe;
30e984df
DV
15452 struct intel_crtc *crtc;
15453 struct intel_encoder *encoder;
35c95375 15454 int i;
30e984df
DV
15455
15456 intel_modeset_readout_hw_state(dev);
24929352
DV
15457
15458 /* HW state is read out, now we need to sanitize this mess. */
62b69566
ACO
15459 get_encoder_power_domains(dev_priv);
15460
b2784e15 15461 for_each_intel_encoder(dev, encoder) {
24929352
DV
15462 intel_sanitize_encoder(encoder);
15463 }
15464
055e393f 15465 for_each_pipe(dev_priv, pipe) {
98187836 15466 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
e2af48c6 15467
24929352 15468 intel_sanitize_crtc(crtc);
6e3c9717
ACO
15469 intel_dump_pipe_config(crtc, crtc->config,
15470 "[setup_hw_state]");
24929352 15471 }
9a935856 15472
d29b2f9d
ACO
15473 intel_modeset_update_connector_atomic_state(dev);
15474
35c95375
DV
15475 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15476 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15477
2dd66ebd 15478 if (!pll->on || pll->active_mask)
35c95375
DV
15479 continue;
15480
15481 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15482
2edd6443 15483 pll->funcs.disable(dev_priv, pll);
35c95375
DV
15484 pll->on = false;
15485 }
15486
920a14b2 15487 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
6eb1a681 15488 vlv_wm_get_hw_state(dev);
5db94019 15489 else if (IS_GEN9(dev_priv))
3078999f 15490 skl_wm_get_hw_state(dev);
6e266956 15491 else if (HAS_PCH_SPLIT(dev_priv))
243e6a44 15492 ilk_wm_get_hw_state(dev);
292b990e
ML
15493
15494 for_each_intel_crtc(dev, crtc) {
d8fc70b7 15495 u64 put_domains;
292b990e 15496
74bff5f9 15497 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
292b990e
ML
15498 if (WARN_ON(put_domains))
15499 modeset_put_power_domains(dev_priv, put_domains);
15500 }
15501 intel_display_set_init_power(dev_priv, false);
010cf73d 15502
8d8c386c
ID
15503 intel_power_domains_verify_state(dev_priv);
15504
010cf73d 15505 intel_fbc_init_pipe_state(dev_priv);
043e9bda 15506}
7d0bc1ea 15507
043e9bda
ML
15508void intel_display_resume(struct drm_device *dev)
15509{
e2c8b870
ML
15510 struct drm_i915_private *dev_priv = to_i915(dev);
15511 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
15512 struct drm_modeset_acquire_ctx ctx;
043e9bda 15513 int ret;
f30da187 15514
e2c8b870 15515 dev_priv->modeset_restore_state = NULL;
73974893
ML
15516 if (state)
15517 state->acquire_ctx = &ctx;
043e9bda 15518
ea49c9ac
ML
15519 /*
15520 * This is a cludge because with real atomic modeset mode_config.mutex
15521 * won't be taken. Unfortunately some probed state like
15522 * audio_codec_enable is still protected by mode_config.mutex, so lock
15523 * it here for now.
15524 */
15525 mutex_lock(&dev->mode_config.mutex);
e2c8b870 15526 drm_modeset_acquire_init(&ctx, 0);
043e9bda 15527
73974893
ML
15528 while (1) {
15529 ret = drm_modeset_lock_all_ctx(dev, &ctx);
15530 if (ret != -EDEADLK)
15531 break;
043e9bda 15532
e2c8b870 15533 drm_modeset_backoff(&ctx);
e2c8b870 15534 }
043e9bda 15535
73974893
ML
15536 if (!ret)
15537 ret = __intel_display_resume(dev, state);
15538
e2c8b870
ML
15539 drm_modeset_drop_locks(&ctx);
15540 drm_modeset_acquire_fini(&ctx);
ea49c9ac 15541 mutex_unlock(&dev->mode_config.mutex);
043e9bda 15542
0853695c 15543 if (ret)
e2c8b870 15544 DRM_ERROR("Restoring old state failed with %i\n", ret);
3c5e37f1
CW
15545 if (state)
15546 drm_atomic_state_put(state);
2c7111db
CW
15547}
15548
15549void intel_modeset_gem_init(struct drm_device *dev)
15550{
dc97997a 15551 struct drm_i915_private *dev_priv = to_i915(dev);
484b41dd 15552
dc97997a 15553 intel_init_gt_powersave(dev_priv);
ae48434c 15554
1ee8da6d 15555 intel_setup_overlay(dev_priv);
1ebaa0b9
CW
15556}
15557
15558int intel_connector_register(struct drm_connector *connector)
15559{
15560 struct intel_connector *intel_connector = to_intel_connector(connector);
15561 int ret;
15562
15563 ret = intel_backlight_device_register(intel_connector);
15564 if (ret)
15565 goto err;
15566
15567 return 0;
0962c3c9 15568
1ebaa0b9
CW
15569err:
15570 return ret;
79e53945
JB
15571}
15572
c191eca1 15573void intel_connector_unregister(struct drm_connector *connector)
4932e2c3 15574{
e63d87c0 15575 struct intel_connector *intel_connector = to_intel_connector(connector);
4932e2c3 15576
e63d87c0 15577 intel_backlight_device_unregister(intel_connector);
4932e2c3 15578 intel_panel_destroy_backlight(connector);
4932e2c3
ID
15579}
15580
79e53945
JB
15581void intel_modeset_cleanup(struct drm_device *dev)
15582{
fac5e23e 15583 struct drm_i915_private *dev_priv = to_i915(dev);
652c393a 15584
eb955eee
CW
15585 flush_work(&dev_priv->atomic_helper.free_work);
15586 WARN_ON(!llist_empty(&dev_priv->atomic_helper.free_list));
15587
dc97997a 15588 intel_disable_gt_powersave(dev_priv);
2eb5252e 15589
fd0c0642
DV
15590 /*
15591 * Interrupts and polling as the first thing to avoid creating havoc.
2eb5252e 15592 * Too much stuff here (turning of connectors, ...) would
fd0c0642
DV
15593 * experience fancy races otherwise.
15594 */
2aeb7d3a 15595 intel_irq_uninstall(dev_priv);
eb21b92b 15596
fd0c0642
DV
15597 /*
15598 * Due to the hpd irq storm handling the hotplug work can re-arm the
15599 * poll handlers. Hence disable polling after hpd handling is shut down.
15600 */
f87ea761 15601 drm_kms_helper_poll_fini(dev);
fd0c0642 15602
723bfd70
JB
15603 intel_unregister_dsm_handler();
15604
c937ab3e 15605 intel_fbc_global_disable(dev_priv);
69341a5e 15606
1630fe75
CW
15607 /* flush any delayed tasks or pending work */
15608 flush_scheduled_work();
15609
79e53945 15610 drm_mode_config_cleanup(dev);
4d7bb011 15611
1ee8da6d 15612 intel_cleanup_overlay(dev_priv);
ae48434c 15613
dc97997a 15614 intel_cleanup_gt_powersave(dev_priv);
f5949141 15615
40196446 15616 intel_teardown_gmbus(dev_priv);
79e53945
JB
15617}
15618
df0e9248
CW
15619void intel_connector_attach_encoder(struct intel_connector *connector,
15620 struct intel_encoder *encoder)
15621{
15622 connector->encoder = encoder;
15623 drm_mode_connector_attach_encoder(&connector->base,
15624 &encoder->base);
79e53945 15625}
28d52043
DA
15626
15627/*
15628 * set vga decode state - true == enable VGA decode
15629 */
6315b5d3 15630int intel_modeset_vga_set_state(struct drm_i915_private *dev_priv, bool state)
28d52043 15631{
6315b5d3 15632 unsigned reg = INTEL_GEN(dev_priv) >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
15633 u16 gmch_ctrl;
15634
75fa041d
CW
15635 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15636 DRM_ERROR("failed to read control word\n");
15637 return -EIO;
15638 }
15639
c0cc8a55
CW
15640 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15641 return 0;
15642
28d52043
DA
15643 if (state)
15644 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15645 else
15646 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
15647
15648 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15649 DRM_ERROR("failed to write control word\n");
15650 return -EIO;
15651 }
15652
28d52043
DA
15653 return 0;
15654}
c4a1d9e4 15655
98a2f411
CW
15656#if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
15657
c4a1d9e4 15658struct intel_display_error_state {
ff57f1b0
PZ
15659
15660 u32 power_well_driver;
15661
63b66e5b
CW
15662 int num_transcoders;
15663
c4a1d9e4
CW
15664 struct intel_cursor_error_state {
15665 u32 control;
15666 u32 position;
15667 u32 base;
15668 u32 size;
52331309 15669 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
15670
15671 struct intel_pipe_error_state {
ddf9c536 15672 bool power_domain_on;
c4a1d9e4 15673 u32 source;
f301b1e1 15674 u32 stat;
52331309 15675 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
15676
15677 struct intel_plane_error_state {
15678 u32 control;
15679 u32 stride;
15680 u32 size;
15681 u32 pos;
15682 u32 addr;
15683 u32 surface;
15684 u32 tile_offset;
52331309 15685 } plane[I915_MAX_PIPES];
63b66e5b
CW
15686
15687 struct intel_transcoder_error_state {
ddf9c536 15688 bool power_domain_on;
63b66e5b
CW
15689 enum transcoder cpu_transcoder;
15690
15691 u32 conf;
15692
15693 u32 htotal;
15694 u32 hblank;
15695 u32 hsync;
15696 u32 vtotal;
15697 u32 vblank;
15698 u32 vsync;
15699 } transcoder[4];
c4a1d9e4
CW
15700};
15701
15702struct intel_display_error_state *
c033666a 15703intel_display_capture_error_state(struct drm_i915_private *dev_priv)
c4a1d9e4 15704{
c4a1d9e4 15705 struct intel_display_error_state *error;
63b66e5b
CW
15706 int transcoders[] = {
15707 TRANSCODER_A,
15708 TRANSCODER_B,
15709 TRANSCODER_C,
15710 TRANSCODER_EDP,
15711 };
c4a1d9e4
CW
15712 int i;
15713
c033666a 15714 if (INTEL_INFO(dev_priv)->num_pipes == 0)
63b66e5b
CW
15715 return NULL;
15716
9d1cb914 15717 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
15718 if (error == NULL)
15719 return NULL;
15720
c033666a 15721 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
ff57f1b0
PZ
15722 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
15723
055e393f 15724 for_each_pipe(dev_priv, i) {
ddf9c536 15725 error->pipe[i].power_domain_on =
f458ebbc
DV
15726 __intel_display_power_is_enabled(dev_priv,
15727 POWER_DOMAIN_PIPE(i));
ddf9c536 15728 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
15729 continue;
15730
5efb3e28
VS
15731 error->cursor[i].control = I915_READ(CURCNTR(i));
15732 error->cursor[i].position = I915_READ(CURPOS(i));
15733 error->cursor[i].base = I915_READ(CURBASE(i));
c4a1d9e4
CW
15734
15735 error->plane[i].control = I915_READ(DSPCNTR(i));
15736 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
c033666a 15737 if (INTEL_GEN(dev_priv) <= 3) {
51889b35 15738 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
15739 error->plane[i].pos = I915_READ(DSPPOS(i));
15740 }
c033666a 15741 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
ca291363 15742 error->plane[i].addr = I915_READ(DSPADDR(i));
c033666a 15743 if (INTEL_GEN(dev_priv) >= 4) {
c4a1d9e4
CW
15744 error->plane[i].surface = I915_READ(DSPSURF(i));
15745 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15746 }
15747
c4a1d9e4 15748 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1 15749
c033666a 15750 if (HAS_GMCH_DISPLAY(dev_priv))
f301b1e1 15751 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
15752 }
15753
4d1de975 15754 /* Note: this does not include DSI transcoders. */
c033666a 15755 error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
2d1fe073 15756 if (HAS_DDI(dev_priv))
63b66e5b
CW
15757 error->num_transcoders++; /* Account for eDP. */
15758
15759 for (i = 0; i < error->num_transcoders; i++) {
15760 enum transcoder cpu_transcoder = transcoders[i];
15761
ddf9c536 15762 error->transcoder[i].power_domain_on =
f458ebbc 15763 __intel_display_power_is_enabled(dev_priv,
38cc1daf 15764 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 15765 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
15766 continue;
15767
63b66e5b
CW
15768 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15769
15770 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15771 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15772 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15773 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15774 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15775 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15776 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
15777 }
15778
15779 return error;
15780}
15781
edc3d884
MK
15782#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15783
c4a1d9e4 15784void
edc3d884 15785intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
15786 struct intel_display_error_state *error)
15787{
5a4c6f1b 15788 struct drm_i915_private *dev_priv = m->i915;
c4a1d9e4
CW
15789 int i;
15790
63b66e5b
CW
15791 if (!error)
15792 return;
15793
b7f05d4a 15794 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev_priv)->num_pipes);
8652744b 15795 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
edc3d884 15796 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 15797 error->power_well_driver);
055e393f 15798 for_each_pipe(dev_priv, i) {
edc3d884 15799 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536 15800 err_printf(m, " Power: %s\n",
87ad3212 15801 onoff(error->pipe[i].power_domain_on));
edc3d884 15802 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 15803 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
15804
15805 err_printf(m, "Plane [%d]:\n", i);
15806 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15807 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
5f56d5f9 15808 if (INTEL_GEN(dev_priv) <= 3) {
edc3d884
MK
15809 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15810 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 15811 }
772c2a51 15812 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
edc3d884 15813 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
5f56d5f9 15814 if (INTEL_GEN(dev_priv) >= 4) {
edc3d884
MK
15815 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15816 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
15817 }
15818
edc3d884
MK
15819 err_printf(m, "Cursor [%d]:\n", i);
15820 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15821 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15822 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 15823 }
63b66e5b
CW
15824
15825 for (i = 0; i < error->num_transcoders; i++) {
da205630 15826 err_printf(m, "CPU transcoder: %s\n",
63b66e5b 15827 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536 15828 err_printf(m, " Power: %s\n",
87ad3212 15829 onoff(error->transcoder[i].power_domain_on));
63b66e5b
CW
15830 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15831 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15832 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15833 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15834 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15835 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
15836 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
15837 }
c4a1d9e4 15838}
98a2f411
CW
15839
15840#endif