]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpu/drm/i915/intel_display.c
drm/i915: Remove dead code from runtime resume handler
[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));
ab33081a 1280 } else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv)) {
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
d88c4afd
VS
1993static unsigned int
1994intel_tile_width_bytes(const struct drm_framebuffer *fb, int plane)
7b49f948 1995{
d88c4afd
VS
1996 struct drm_i915_private *dev_priv = to_i915(fb->dev);
1997 unsigned int cpp = fb->format->cpp[plane];
1998
1999 switch (fb->modifier) {
2f075565 2000 case DRM_FORMAT_MOD_LINEAR:
7b49f948
VS
2001 return cpp;
2002 case I915_FORMAT_MOD_X_TILED:
2003 if (IS_GEN2(dev_priv))
2004 return 128;
2005 else
2006 return 512;
2007 case I915_FORMAT_MOD_Y_TILED:
2008 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2009 return 128;
2010 else
2011 return 512;
2012 case I915_FORMAT_MOD_Yf_TILED:
2013 switch (cpp) {
2014 case 1:
2015 return 64;
2016 case 2:
2017 case 4:
2018 return 128;
2019 case 8:
2020 case 16:
2021 return 256;
2022 default:
2023 MISSING_CASE(cpp);
2024 return cpp;
2025 }
2026 break;
2027 default:
d88c4afd 2028 MISSING_CASE(fb->modifier);
7b49f948
VS
2029 return cpp;
2030 }
2031}
2032
d88c4afd
VS
2033static unsigned int
2034intel_tile_height(const struct drm_framebuffer *fb, int plane)
a57ce0b2 2035{
2f075565 2036 if (fb->modifier == DRM_FORMAT_MOD_LINEAR)
832be82f
VS
2037 return 1;
2038 else
d88c4afd
VS
2039 return intel_tile_size(to_i915(fb->dev)) /
2040 intel_tile_width_bytes(fb, plane);
6761dd31
TU
2041}
2042
8d0deca8 2043/* Return the tile dimensions in pixel units */
d88c4afd 2044static void intel_tile_dims(const struct drm_framebuffer *fb, int plane,
8d0deca8 2045 unsigned int *tile_width,
d88c4afd 2046 unsigned int *tile_height)
8d0deca8 2047{
d88c4afd
VS
2048 unsigned int tile_width_bytes = intel_tile_width_bytes(fb, plane);
2049 unsigned int cpp = fb->format->cpp[plane];
8d0deca8
VS
2050
2051 *tile_width = tile_width_bytes / cpp;
d88c4afd 2052 *tile_height = intel_tile_size(to_i915(fb->dev)) / tile_width_bytes;
8d0deca8
VS
2053}
2054
6761dd31 2055unsigned int
d88c4afd
VS
2056intel_fb_align_height(const struct drm_framebuffer *fb,
2057 int plane, unsigned int height)
6761dd31 2058{
d88c4afd 2059 unsigned int tile_height = intel_tile_height(fb, plane);
832be82f
VS
2060
2061 return ALIGN(height, tile_height);
a57ce0b2
JB
2062}
2063
1663b9d6
VS
2064unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2065{
2066 unsigned int size = 0;
2067 int i;
2068
2069 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2070 size += rot_info->plane[i].width * rot_info->plane[i].height;
2071
2072 return size;
2073}
2074
75c82a53 2075static void
3465c580
VS
2076intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2077 const struct drm_framebuffer *fb,
2078 unsigned int rotation)
f64b98cd 2079{
7b92c047 2080 view->type = I915_GGTT_VIEW_NORMAL;
bd2ef25d 2081 if (drm_rotation_90_or_270(rotation)) {
7b92c047 2082 view->type = I915_GGTT_VIEW_ROTATED;
8bab1193 2083 view->rotated = to_intel_framebuffer(fb)->rot_info;
2d7a215f
VS
2084 }
2085}
50470bb0 2086
fabac484
VS
2087static unsigned int intel_cursor_alignment(const struct drm_i915_private *dev_priv)
2088{
2089 if (IS_I830(dev_priv))
2090 return 16 * 1024;
2091 else if (IS_I85X(dev_priv))
2092 return 256;
d9e1551e
VS
2093 else if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
2094 return 32;
fabac484
VS
2095 else
2096 return 4 * 1024;
2097}
2098
603525d7 2099static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
4e9a86b6
VS
2100{
2101 if (INTEL_INFO(dev_priv)->gen >= 9)
2102 return 256 * 1024;
c0f86832 2103 else if (IS_I965G(dev_priv) || IS_I965GM(dev_priv) ||
666a4537 2104 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
4e9a86b6
VS
2105 return 128 * 1024;
2106 else if (INTEL_INFO(dev_priv)->gen >= 4)
2107 return 4 * 1024;
2108 else
44c5905e 2109 return 0;
4e9a86b6
VS
2110}
2111
d88c4afd
VS
2112static unsigned int intel_surf_alignment(const struct drm_framebuffer *fb,
2113 int plane)
603525d7 2114{
d88c4afd
VS
2115 struct drm_i915_private *dev_priv = to_i915(fb->dev);
2116
b90c1ee1
VS
2117 /* AUX_DIST needs only 4K alignment */
2118 if (fb->format->format == DRM_FORMAT_NV12 && plane == 1)
2119 return 4096;
2120
d88c4afd 2121 switch (fb->modifier) {
2f075565 2122 case DRM_FORMAT_MOD_LINEAR:
603525d7
VS
2123 return intel_linear_alignment(dev_priv);
2124 case I915_FORMAT_MOD_X_TILED:
d88c4afd 2125 if (INTEL_GEN(dev_priv) >= 9)
603525d7
VS
2126 return 256 * 1024;
2127 return 0;
2128 case I915_FORMAT_MOD_Y_TILED:
2129 case I915_FORMAT_MOD_Yf_TILED:
2130 return 1 * 1024 * 1024;
2131 default:
d88c4afd 2132 MISSING_CASE(fb->modifier);
603525d7
VS
2133 return 0;
2134 }
2135}
2136
058d88c4
CW
2137struct i915_vma *
2138intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
6b95a207 2139{
850c4cdc 2140 struct drm_device *dev = fb->dev;
fac5e23e 2141 struct drm_i915_private *dev_priv = to_i915(dev);
850c4cdc 2142 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2143 struct i915_ggtt_view view;
058d88c4 2144 struct i915_vma *vma;
6b95a207 2145 u32 alignment;
6b95a207 2146
ebcdd39e
MR
2147 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2148
d88c4afd 2149 alignment = intel_surf_alignment(fb, 0);
6b95a207 2150
3465c580 2151 intel_fill_fb_ggtt_view(&view, fb, rotation);
f64b98cd 2152
693db184
CW
2153 /* Note that the w/a also requires 64 PTE of padding following the
2154 * bo. We currently fill all unused PTE with the shadow page and so
2155 * we should always have valid PTE following the scanout preventing
2156 * the VT-d warning.
2157 */
48f112fe 2158 if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
693db184
CW
2159 alignment = 256 * 1024;
2160
d6dd6843
PZ
2161 /*
2162 * Global gtt pte registers are special registers which actually forward
2163 * writes to a chunk of system memory. Which means that there is no risk
2164 * that the register values disappear as soon as we call
2165 * intel_runtime_pm_put(), so it is correct to wrap only the
2166 * pin/unpin/fence and not more.
2167 */
2168 intel_runtime_pm_get(dev_priv);
2169
058d88c4 2170 vma = i915_gem_object_pin_to_display_plane(obj, alignment, &view);
49ef5294
CW
2171 if (IS_ERR(vma))
2172 goto err;
6b95a207 2173
05a20d09 2174 if (i915_vma_is_map_and_fenceable(vma)) {
49ef5294
CW
2175 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2176 * fence, whereas 965+ only requires a fence if using
2177 * framebuffer compression. For simplicity, we always, when
2178 * possible, install a fence as the cost is not that onerous.
2179 *
2180 * If we fail to fence the tiled scanout, then either the
2181 * modeset will reject the change (which is highly unlikely as
2182 * the affected systems, all but one, do not have unmappable
2183 * space) or we will not be able to enable full powersaving
2184 * techniques (also likely not to apply due to various limits
2185 * FBC and the like impose on the size of the buffer, which
2186 * presumably we violated anyway with this unmappable buffer).
2187 * Anyway, it is presumably better to stumble onwards with
2188 * something and try to run the system in a "less than optimal"
2189 * mode that matches the user configuration.
2190 */
2191 if (i915_vma_get_fence(vma) == 0)
2192 i915_vma_pin_fence(vma);
9807216f 2193 }
6b95a207 2194
be1e3415 2195 i915_vma_get(vma);
49ef5294 2196err:
d6dd6843 2197 intel_runtime_pm_put(dev_priv);
058d88c4 2198 return vma;
6b95a207
KH
2199}
2200
be1e3415 2201void intel_unpin_fb_vma(struct i915_vma *vma)
1690e1eb 2202{
be1e3415 2203 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
f64b98cd 2204
49ef5294 2205 i915_vma_unpin_fence(vma);
058d88c4 2206 i915_gem_object_unpin_from_display_plane(vma);
be1e3415 2207 i915_vma_put(vma);
1690e1eb
CW
2208}
2209
ef78ec94
VS
2210static int intel_fb_pitch(const struct drm_framebuffer *fb, int plane,
2211 unsigned int rotation)
2212{
bd2ef25d 2213 if (drm_rotation_90_or_270(rotation))
ef78ec94
VS
2214 return to_intel_framebuffer(fb)->rotated[plane].pitch;
2215 else
2216 return fb->pitches[plane];
2217}
2218
6687c906
VS
2219/*
2220 * Convert the x/y offsets into a linear offset.
2221 * Only valid with 0/180 degree rotation, which is fine since linear
2222 * offset is only used with linear buffers on pre-hsw and tiled buffers
2223 * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
2224 */
2225u32 intel_fb_xy_to_linear(int x, int y,
2949056c
VS
2226 const struct intel_plane_state *state,
2227 int plane)
6687c906 2228{
2949056c 2229 const struct drm_framebuffer *fb = state->base.fb;
353c8598 2230 unsigned int cpp = fb->format->cpp[plane];
6687c906
VS
2231 unsigned int pitch = fb->pitches[plane];
2232
2233 return y * pitch + x * cpp;
2234}
2235
2236/*
2237 * Add the x/y offsets derived from fb->offsets[] to the user
2238 * specified plane src x/y offsets. The resulting x/y offsets
2239 * specify the start of scanout from the beginning of the gtt mapping.
2240 */
2241void intel_add_fb_offsets(int *x, int *y,
2949056c
VS
2242 const struct intel_plane_state *state,
2243 int plane)
6687c906
VS
2244
2245{
2949056c
VS
2246 const struct intel_framebuffer *intel_fb = to_intel_framebuffer(state->base.fb);
2247 unsigned int rotation = state->base.rotation;
6687c906 2248
bd2ef25d 2249 if (drm_rotation_90_or_270(rotation)) {
6687c906
VS
2250 *x += intel_fb->rotated[plane].x;
2251 *y += intel_fb->rotated[plane].y;
2252 } else {
2253 *x += intel_fb->normal[plane].x;
2254 *y += intel_fb->normal[plane].y;
2255 }
2256}
2257
29cf9491 2258/*
29cf9491
VS
2259 * Input tile dimensions and pitch must already be
2260 * rotated to match x and y, and in pixel units.
2261 */
66a2d927
VS
2262static u32 _intel_adjust_tile_offset(int *x, int *y,
2263 unsigned int tile_width,
2264 unsigned int tile_height,
2265 unsigned int tile_size,
2266 unsigned int pitch_tiles,
2267 u32 old_offset,
2268 u32 new_offset)
29cf9491 2269{
b9b24038 2270 unsigned int pitch_pixels = pitch_tiles * tile_width;
29cf9491
VS
2271 unsigned int tiles;
2272
2273 WARN_ON(old_offset & (tile_size - 1));
2274 WARN_ON(new_offset & (tile_size - 1));
2275 WARN_ON(new_offset > old_offset);
2276
2277 tiles = (old_offset - new_offset) / tile_size;
2278
2279 *y += tiles / pitch_tiles * tile_height;
2280 *x += tiles % pitch_tiles * tile_width;
2281
b9b24038
VS
2282 /* minimize x in case it got needlessly big */
2283 *y += *x / pitch_pixels * tile_height;
2284 *x %= pitch_pixels;
2285
29cf9491
VS
2286 return new_offset;
2287}
2288
66a2d927
VS
2289/*
2290 * Adjust the tile offset by moving the difference into
2291 * the x/y offsets.
2292 */
2293static u32 intel_adjust_tile_offset(int *x, int *y,
2294 const struct intel_plane_state *state, int plane,
2295 u32 old_offset, u32 new_offset)
2296{
2297 const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
2298 const struct drm_framebuffer *fb = state->base.fb;
353c8598 2299 unsigned int cpp = fb->format->cpp[plane];
66a2d927
VS
2300 unsigned int rotation = state->base.rotation;
2301 unsigned int pitch = intel_fb_pitch(fb, plane, rotation);
2302
2303 WARN_ON(new_offset > old_offset);
2304
2f075565 2305 if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
66a2d927
VS
2306 unsigned int tile_size, tile_width, tile_height;
2307 unsigned int pitch_tiles;
2308
2309 tile_size = intel_tile_size(dev_priv);
d88c4afd 2310 intel_tile_dims(fb, plane, &tile_width, &tile_height);
66a2d927 2311
bd2ef25d 2312 if (drm_rotation_90_or_270(rotation)) {
66a2d927
VS
2313 pitch_tiles = pitch / tile_height;
2314 swap(tile_width, tile_height);
2315 } else {
2316 pitch_tiles = pitch / (tile_width * cpp);
2317 }
2318
2319 _intel_adjust_tile_offset(x, y, tile_width, tile_height,
2320 tile_size, pitch_tiles,
2321 old_offset, new_offset);
2322 } else {
2323 old_offset += *y * pitch + *x * cpp;
2324
2325 *y = (old_offset - new_offset) / pitch;
2326 *x = ((old_offset - new_offset) - *y * pitch) / cpp;
2327 }
2328
2329 return new_offset;
2330}
2331
8d0deca8
VS
2332/*
2333 * Computes the linear offset to the base tile and adjusts
2334 * x, y. bytes per pixel is assumed to be a power-of-two.
2335 *
2336 * In the 90/270 rotated case, x and y are assumed
2337 * to be already rotated to match the rotated GTT view, and
2338 * pitch is the tile_height aligned framebuffer height.
6687c906
VS
2339 *
2340 * This function is used when computing the derived information
2341 * under intel_framebuffer, so using any of that information
2342 * here is not allowed. Anything under drm_framebuffer can be
2343 * used. This is why the user has to pass in the pitch since it
2344 * is specified in the rotated orientation.
8d0deca8 2345 */
6687c906
VS
2346static u32 _intel_compute_tile_offset(const struct drm_i915_private *dev_priv,
2347 int *x, int *y,
2348 const struct drm_framebuffer *fb, int plane,
2349 unsigned int pitch,
2350 unsigned int rotation,
2351 u32 alignment)
c2c75131 2352{
bae781b2 2353 uint64_t fb_modifier = fb->modifier;
353c8598 2354 unsigned int cpp = fb->format->cpp[plane];
6687c906 2355 u32 offset, offset_aligned;
29cf9491 2356
29cf9491
VS
2357 if (alignment)
2358 alignment--;
2359
2f075565 2360 if (fb_modifier != DRM_FORMAT_MOD_LINEAR) {
8d0deca8
VS
2361 unsigned int tile_size, tile_width, tile_height;
2362 unsigned int tile_rows, tiles, pitch_tiles;
c2c75131 2363
d843310d 2364 tile_size = intel_tile_size(dev_priv);
d88c4afd 2365 intel_tile_dims(fb, plane, &tile_width, &tile_height);
8d0deca8 2366
bd2ef25d 2367 if (drm_rotation_90_or_270(rotation)) {
8d0deca8
VS
2368 pitch_tiles = pitch / tile_height;
2369 swap(tile_width, tile_height);
2370 } else {
2371 pitch_tiles = pitch / (tile_width * cpp);
2372 }
d843310d
VS
2373
2374 tile_rows = *y / tile_height;
2375 *y %= tile_height;
c2c75131 2376
8d0deca8
VS
2377 tiles = *x / tile_width;
2378 *x %= tile_width;
bc752862 2379
29cf9491
VS
2380 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2381 offset_aligned = offset & ~alignment;
bc752862 2382
66a2d927
VS
2383 _intel_adjust_tile_offset(x, y, tile_width, tile_height,
2384 tile_size, pitch_tiles,
2385 offset, offset_aligned);
29cf9491 2386 } else {
bc752862 2387 offset = *y * pitch + *x * cpp;
29cf9491
VS
2388 offset_aligned = offset & ~alignment;
2389
4e9a86b6
VS
2390 *y = (offset & alignment) / pitch;
2391 *x = ((offset & alignment) - *y * pitch) / cpp;
bc752862 2392 }
29cf9491
VS
2393
2394 return offset_aligned;
c2c75131
DV
2395}
2396
6687c906 2397u32 intel_compute_tile_offset(int *x, int *y,
2949056c
VS
2398 const struct intel_plane_state *state,
2399 int plane)
6687c906 2400{
1e7b4fd8
VS
2401 struct intel_plane *intel_plane = to_intel_plane(state->base.plane);
2402 struct drm_i915_private *dev_priv = to_i915(intel_plane->base.dev);
2949056c
VS
2403 const struct drm_framebuffer *fb = state->base.fb;
2404 unsigned int rotation = state->base.rotation;
ef78ec94 2405 int pitch = intel_fb_pitch(fb, plane, rotation);
1e7b4fd8
VS
2406 u32 alignment;
2407
2408 if (intel_plane->id == PLANE_CURSOR)
2409 alignment = intel_cursor_alignment(dev_priv);
2410 else
2411 alignment = intel_surf_alignment(fb, plane);
6687c906
VS
2412
2413 return _intel_compute_tile_offset(dev_priv, x, y, fb, plane, pitch,
2414 rotation, alignment);
2415}
2416
2417/* Convert the fb->offset[] linear offset into x/y offsets */
2418static void intel_fb_offset_to_xy(int *x, int *y,
2419 const struct drm_framebuffer *fb, int plane)
2420{
353c8598 2421 unsigned int cpp = fb->format->cpp[plane];
6687c906
VS
2422 unsigned int pitch = fb->pitches[plane];
2423 u32 linear_offset = fb->offsets[plane];
2424
2425 *y = linear_offset / pitch;
2426 *x = linear_offset % pitch / cpp;
2427}
2428
72618ebf
VS
2429static unsigned int intel_fb_modifier_to_tiling(uint64_t fb_modifier)
2430{
2431 switch (fb_modifier) {
2432 case I915_FORMAT_MOD_X_TILED:
2433 return I915_TILING_X;
2434 case I915_FORMAT_MOD_Y_TILED:
2435 return I915_TILING_Y;
2436 default:
2437 return I915_TILING_NONE;
2438 }
2439}
2440
6687c906
VS
2441static int
2442intel_fill_fb_info(struct drm_i915_private *dev_priv,
2443 struct drm_framebuffer *fb)
2444{
2445 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
2446 struct intel_rotation_info *rot_info = &intel_fb->rot_info;
2447 u32 gtt_offset_rotated = 0;
2448 unsigned int max_size = 0;
bcb0b461 2449 int i, num_planes = fb->format->num_planes;
6687c906
VS
2450 unsigned int tile_size = intel_tile_size(dev_priv);
2451
2452 for (i = 0; i < num_planes; i++) {
2453 unsigned int width, height;
2454 unsigned int cpp, size;
2455 u32 offset;
2456 int x, y;
2457
353c8598 2458 cpp = fb->format->cpp[i];
145fcb11
VS
2459 width = drm_framebuffer_plane_width(fb->width, fb, i);
2460 height = drm_framebuffer_plane_height(fb->height, fb, i);
6687c906
VS
2461
2462 intel_fb_offset_to_xy(&x, &y, fb, i);
2463
60d5f2a4
VS
2464 /*
2465 * The fence (if used) is aligned to the start of the object
2466 * so having the framebuffer wrap around across the edge of the
2467 * fenced region doesn't really work. We have no API to configure
2468 * the fence start offset within the object (nor could we probably
2469 * on gen2/3). So it's just easier if we just require that the
2470 * fb layout agrees with the fence layout. We already check that the
2471 * fb stride matches the fence stride elsewhere.
2472 */
2473 if (i915_gem_object_is_tiled(intel_fb->obj) &&
2474 (x + width) * cpp > fb->pitches[i]) {
144cc143
VS
2475 DRM_DEBUG_KMS("bad fb plane %d offset: 0x%x\n",
2476 i, fb->offsets[i]);
60d5f2a4
VS
2477 return -EINVAL;
2478 }
2479
6687c906
VS
2480 /*
2481 * First pixel of the framebuffer from
2482 * the start of the normal gtt mapping.
2483 */
2484 intel_fb->normal[i].x = x;
2485 intel_fb->normal[i].y = y;
2486
2487 offset = _intel_compute_tile_offset(dev_priv, &x, &y,
3ca46c0a 2488 fb, i, fb->pitches[i],
c2c446ad 2489 DRM_MODE_ROTATE_0, tile_size);
6687c906
VS
2490 offset /= tile_size;
2491
2f075565 2492 if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
6687c906
VS
2493 unsigned int tile_width, tile_height;
2494 unsigned int pitch_tiles;
2495 struct drm_rect r;
2496
d88c4afd 2497 intel_tile_dims(fb, i, &tile_width, &tile_height);
6687c906
VS
2498
2499 rot_info->plane[i].offset = offset;
2500 rot_info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i], tile_width * cpp);
2501 rot_info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
2502 rot_info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
2503
2504 intel_fb->rotated[i].pitch =
2505 rot_info->plane[i].height * tile_height;
2506
2507 /* how many tiles does this plane need */
2508 size = rot_info->plane[i].stride * rot_info->plane[i].height;
2509 /*
2510 * If the plane isn't horizontally tile aligned,
2511 * we need one more tile.
2512 */
2513 if (x != 0)
2514 size++;
2515
2516 /* rotate the x/y offsets to match the GTT view */
2517 r.x1 = x;
2518 r.y1 = y;
2519 r.x2 = x + width;
2520 r.y2 = y + height;
2521 drm_rect_rotate(&r,
2522 rot_info->plane[i].width * tile_width,
2523 rot_info->plane[i].height * tile_height,
c2c446ad 2524 DRM_MODE_ROTATE_270);
6687c906
VS
2525 x = r.x1;
2526 y = r.y1;
2527
2528 /* rotate the tile dimensions to match the GTT view */
2529 pitch_tiles = intel_fb->rotated[i].pitch / tile_height;
2530 swap(tile_width, tile_height);
2531
2532 /*
2533 * We only keep the x/y offsets, so push all of the
2534 * gtt offset into the x/y offsets.
2535 */
46a1bd28
ACO
2536 _intel_adjust_tile_offset(&x, &y,
2537 tile_width, tile_height,
2538 tile_size, pitch_tiles,
66a2d927 2539 gtt_offset_rotated * tile_size, 0);
6687c906
VS
2540
2541 gtt_offset_rotated += rot_info->plane[i].width * rot_info->plane[i].height;
2542
2543 /*
2544 * First pixel of the framebuffer from
2545 * the start of the rotated gtt mapping.
2546 */
2547 intel_fb->rotated[i].x = x;
2548 intel_fb->rotated[i].y = y;
2549 } else {
2550 size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
2551 x * cpp, tile_size);
2552 }
2553
2554 /* how many tiles in total needed in the bo */
2555 max_size = max(max_size, offset + size);
2556 }
2557
144cc143
VS
2558 if (max_size * tile_size > intel_fb->obj->base.size) {
2559 DRM_DEBUG_KMS("fb too big for bo (need %u bytes, have %zu bytes)\n",
2560 max_size * tile_size, intel_fb->obj->base.size);
6687c906
VS
2561 return -EINVAL;
2562 }
2563
2564 return 0;
2565}
2566
b35d63fa 2567static int i9xx_format_to_fourcc(int format)
46f297fb
JB
2568{
2569 switch (format) {
2570 case DISPPLANE_8BPP:
2571 return DRM_FORMAT_C8;
2572 case DISPPLANE_BGRX555:
2573 return DRM_FORMAT_XRGB1555;
2574 case DISPPLANE_BGRX565:
2575 return DRM_FORMAT_RGB565;
2576 default:
2577 case DISPPLANE_BGRX888:
2578 return DRM_FORMAT_XRGB8888;
2579 case DISPPLANE_RGBX888:
2580 return DRM_FORMAT_XBGR8888;
2581 case DISPPLANE_BGRX101010:
2582 return DRM_FORMAT_XRGB2101010;
2583 case DISPPLANE_RGBX101010:
2584 return DRM_FORMAT_XBGR2101010;
2585 }
2586}
2587
bc8d7dff
DL
2588static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2589{
2590 switch (format) {
2591 case PLANE_CTL_FORMAT_RGB_565:
2592 return DRM_FORMAT_RGB565;
2593 default:
2594 case PLANE_CTL_FORMAT_XRGB_8888:
2595 if (rgb_order) {
2596 if (alpha)
2597 return DRM_FORMAT_ABGR8888;
2598 else
2599 return DRM_FORMAT_XBGR8888;
2600 } else {
2601 if (alpha)
2602 return DRM_FORMAT_ARGB8888;
2603 else
2604 return DRM_FORMAT_XRGB8888;
2605 }
2606 case PLANE_CTL_FORMAT_XRGB_2101010:
2607 if (rgb_order)
2608 return DRM_FORMAT_XBGR2101010;
2609 else
2610 return DRM_FORMAT_XRGB2101010;
2611 }
2612}
2613
5724dbd1 2614static bool
f6936e29
DV
2615intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2616 struct intel_initial_plane_config *plane_config)
46f297fb
JB
2617{
2618 struct drm_device *dev = crtc->base.dev;
3badb49f 2619 struct drm_i915_private *dev_priv = to_i915(dev);
72e96d64 2620 struct i915_ggtt *ggtt = &dev_priv->ggtt;
46f297fb
JB
2621 struct drm_i915_gem_object *obj = NULL;
2622 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2d14030b 2623 struct drm_framebuffer *fb = &plane_config->fb->base;
f37b5c2b
DV
2624 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2625 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2626 PAGE_SIZE);
2627
2628 size_aligned -= base_aligned;
46f297fb 2629
ff2652ea
CW
2630 if (plane_config->size == 0)
2631 return false;
2632
3badb49f
PZ
2633 /* If the FB is too big, just don't use it since fbdev is not very
2634 * important and we should probably use that space with FBC or other
2635 * features. */
72e96d64 2636 if (size_aligned * 2 > ggtt->stolen_usable_size)
3badb49f
PZ
2637 return false;
2638
12c83d99 2639 mutex_lock(&dev->struct_mutex);
187685cb 2640 obj = i915_gem_object_create_stolen_for_preallocated(dev_priv,
f37b5c2b
DV
2641 base_aligned,
2642 base_aligned,
2643 size_aligned);
24dbf51a
CW
2644 mutex_unlock(&dev->struct_mutex);
2645 if (!obj)
484b41dd 2646 return false;
46f297fb 2647
3e510a8e
CW
2648 if (plane_config->tiling == I915_TILING_X)
2649 obj->tiling_and_stride = fb->pitches[0] | I915_TILING_X;
46f297fb 2650
438b74a5 2651 mode_cmd.pixel_format = fb->format->format;
6bf129df
DL
2652 mode_cmd.width = fb->width;
2653 mode_cmd.height = fb->height;
2654 mode_cmd.pitches[0] = fb->pitches[0];
bae781b2 2655 mode_cmd.modifier[0] = fb->modifier;
18c5247e 2656 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
46f297fb 2657
24dbf51a 2658 if (intel_framebuffer_init(to_intel_framebuffer(fb), obj, &mode_cmd)) {
46f297fb
JB
2659 DRM_DEBUG_KMS("intel fb init failed\n");
2660 goto out_unref_obj;
2661 }
12c83d99 2662
484b41dd 2663
f6936e29 2664 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
484b41dd 2665 return true;
46f297fb
JB
2666
2667out_unref_obj:
f8c417cd 2668 i915_gem_object_put(obj);
484b41dd
JB
2669 return false;
2670}
2671
5a21b665
DV
2672/* Update plane->state->fb to match plane->fb after driver-internal updates */
2673static void
2674update_state_fb(struct drm_plane *plane)
2675{
2676 if (plane->fb == plane->state->fb)
2677 return;
2678
2679 if (plane->state->fb)
2680 drm_framebuffer_unreference(plane->state->fb);
2681 plane->state->fb = plane->fb;
2682 if (plane->state->fb)
2683 drm_framebuffer_reference(plane->state->fb);
2684}
2685
e9728bd8
VS
2686static void
2687intel_set_plane_visible(struct intel_crtc_state *crtc_state,
2688 struct intel_plane_state *plane_state,
2689 bool visible)
2690{
2691 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
2692
2693 plane_state->base.visible = visible;
2694
2695 /* FIXME pre-g4x don't work like this */
2696 if (visible) {
2697 crtc_state->base.plane_mask |= BIT(drm_plane_index(&plane->base));
2698 crtc_state->active_planes |= BIT(plane->id);
2699 } else {
2700 crtc_state->base.plane_mask &= ~BIT(drm_plane_index(&plane->base));
2701 crtc_state->active_planes &= ~BIT(plane->id);
2702 }
2703
2704 DRM_DEBUG_KMS("%s active planes 0x%x\n",
2705 crtc_state->base.crtc->name,
2706 crtc_state->active_planes);
2707}
2708
5724dbd1 2709static void
f6936e29
DV
2710intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2711 struct intel_initial_plane_config *plane_config)
484b41dd
JB
2712{
2713 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 2714 struct drm_i915_private *dev_priv = to_i915(dev);
484b41dd 2715 struct drm_crtc *c;
2ff8fde1 2716 struct drm_i915_gem_object *obj;
88595ac9 2717 struct drm_plane *primary = intel_crtc->base.primary;
be5651f2 2718 struct drm_plane_state *plane_state = primary->state;
200757f5
MR
2719 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2720 struct intel_plane *intel_plane = to_intel_plane(primary);
0a8d8a86
MR
2721 struct intel_plane_state *intel_state =
2722 to_intel_plane_state(plane_state);
88595ac9 2723 struct drm_framebuffer *fb;
484b41dd 2724
2d14030b 2725 if (!plane_config->fb)
484b41dd
JB
2726 return;
2727
f6936e29 2728 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
88595ac9
DV
2729 fb = &plane_config->fb->base;
2730 goto valid_fb;
f55548b5 2731 }
484b41dd 2732
2d14030b 2733 kfree(plane_config->fb);
484b41dd
JB
2734
2735 /*
2736 * Failed to alloc the obj, check to see if we should share
2737 * an fb with another CRTC instead
2738 */
70e1e0ec 2739 for_each_crtc(dev, c) {
be1e3415 2740 struct intel_plane_state *state;
484b41dd
JB
2741
2742 if (c == &intel_crtc->base)
2743 continue;
2744
be1e3415 2745 if (!to_intel_crtc(c)->active)
2ff8fde1
MR
2746 continue;
2747
be1e3415
CW
2748 state = to_intel_plane_state(c->primary->state);
2749 if (!state->vma)
484b41dd
JB
2750 continue;
2751
be1e3415
CW
2752 if (intel_plane_ggtt_offset(state) == plane_config->base) {
2753 fb = c->primary->fb;
88595ac9
DV
2754 drm_framebuffer_reference(fb);
2755 goto valid_fb;
484b41dd
JB
2756 }
2757 }
88595ac9 2758
200757f5
MR
2759 /*
2760 * We've failed to reconstruct the BIOS FB. Current display state
2761 * indicates that the primary plane is visible, but has a NULL FB,
2762 * which will lead to problems later if we don't fix it up. The
2763 * simplest solution is to just disable the primary plane now and
2764 * pretend the BIOS never had it enabled.
2765 */
e9728bd8
VS
2766 intel_set_plane_visible(to_intel_crtc_state(crtc_state),
2767 to_intel_plane_state(plane_state),
2768 false);
2622a081 2769 intel_pre_disable_primary_noatomic(&intel_crtc->base);
72259536 2770 trace_intel_disable_plane(primary, intel_crtc);
282dbf9b 2771 intel_plane->disable_plane(intel_plane, intel_crtc);
200757f5 2772
88595ac9
DV
2773 return;
2774
2775valid_fb:
be1e3415
CW
2776 mutex_lock(&dev->struct_mutex);
2777 intel_state->vma =
2778 intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
2779 mutex_unlock(&dev->struct_mutex);
2780 if (IS_ERR(intel_state->vma)) {
2781 DRM_ERROR("failed to pin boot fb on pipe %d: %li\n",
2782 intel_crtc->pipe, PTR_ERR(intel_state->vma));
2783
2784 intel_state->vma = NULL;
2785 drm_framebuffer_unreference(fb);
2786 return;
2787 }
2788
f44e2659
VS
2789 plane_state->src_x = 0;
2790 plane_state->src_y = 0;
be5651f2
ML
2791 plane_state->src_w = fb->width << 16;
2792 plane_state->src_h = fb->height << 16;
2793
f44e2659
VS
2794 plane_state->crtc_x = 0;
2795 plane_state->crtc_y = 0;
be5651f2
ML
2796 plane_state->crtc_w = fb->width;
2797 plane_state->crtc_h = fb->height;
2798
1638d30c
RC
2799 intel_state->base.src = drm_plane_state_src(plane_state);
2800 intel_state->base.dst = drm_plane_state_dest(plane_state);
0a8d8a86 2801
88595ac9 2802 obj = intel_fb_obj(fb);
3e510a8e 2803 if (i915_gem_object_is_tiled(obj))
88595ac9
DV
2804 dev_priv->preserve_bios_swizzle = true;
2805
be5651f2
ML
2806 drm_framebuffer_reference(fb);
2807 primary->fb = primary->state->fb = fb;
36750f28 2808 primary->crtc = primary->state->crtc = &intel_crtc->base;
e9728bd8
VS
2809
2810 intel_set_plane_visible(to_intel_crtc_state(crtc_state),
2811 to_intel_plane_state(plane_state),
2812 true);
2813
faf5bf0a
CW
2814 atomic_or(to_intel_plane(primary)->frontbuffer_bit,
2815 &obj->frontbuffer_bits);
46f297fb
JB
2816}
2817
b63a16f6
VS
2818static int skl_max_plane_width(const struct drm_framebuffer *fb, int plane,
2819 unsigned int rotation)
2820{
353c8598 2821 int cpp = fb->format->cpp[plane];
b63a16f6 2822
bae781b2 2823 switch (fb->modifier) {
2f075565 2824 case DRM_FORMAT_MOD_LINEAR:
b63a16f6
VS
2825 case I915_FORMAT_MOD_X_TILED:
2826 switch (cpp) {
2827 case 8:
2828 return 4096;
2829 case 4:
2830 case 2:
2831 case 1:
2832 return 8192;
2833 default:
2834 MISSING_CASE(cpp);
2835 break;
2836 }
2837 break;
2838 case I915_FORMAT_MOD_Y_TILED:
2839 case I915_FORMAT_MOD_Yf_TILED:
2840 switch (cpp) {
2841 case 8:
2842 return 2048;
2843 case 4:
2844 return 4096;
2845 case 2:
2846 case 1:
2847 return 8192;
2848 default:
2849 MISSING_CASE(cpp);
2850 break;
2851 }
2852 break;
2853 default:
bae781b2 2854 MISSING_CASE(fb->modifier);
b63a16f6
VS
2855 }
2856
2857 return 2048;
2858}
2859
2860static int skl_check_main_surface(struct intel_plane_state *plane_state)
2861{
b63a16f6
VS
2862 const struct drm_framebuffer *fb = plane_state->base.fb;
2863 unsigned int rotation = plane_state->base.rotation;
cc926387
DV
2864 int x = plane_state->base.src.x1 >> 16;
2865 int y = plane_state->base.src.y1 >> 16;
2866 int w = drm_rect_width(&plane_state->base.src) >> 16;
2867 int h = drm_rect_height(&plane_state->base.src) >> 16;
b63a16f6
VS
2868 int max_width = skl_max_plane_width(fb, 0, rotation);
2869 int max_height = 4096;
8d970654 2870 u32 alignment, offset, aux_offset = plane_state->aux.offset;
b63a16f6
VS
2871
2872 if (w > max_width || h > max_height) {
2873 DRM_DEBUG_KMS("requested Y/RGB source size %dx%d too big (limit %dx%d)\n",
2874 w, h, max_width, max_height);
2875 return -EINVAL;
2876 }
2877
2878 intel_add_fb_offsets(&x, &y, plane_state, 0);
2879 offset = intel_compute_tile_offset(&x, &y, plane_state, 0);
d88c4afd 2880 alignment = intel_surf_alignment(fb, 0);
b63a16f6 2881
8d970654
VS
2882 /*
2883 * AUX surface offset is specified as the distance from the
2884 * main surface offset, and it must be non-negative. Make
2885 * sure that is what we will get.
2886 */
2887 if (offset > aux_offset)
2888 offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
2889 offset, aux_offset & ~(alignment - 1));
2890
b63a16f6
VS
2891 /*
2892 * When using an X-tiled surface, the plane blows up
2893 * if the x offset + width exceed the stride.
2894 *
2895 * TODO: linear and Y-tiled seem fine, Yf untested,
2896 */
bae781b2 2897 if (fb->modifier == I915_FORMAT_MOD_X_TILED) {
353c8598 2898 int cpp = fb->format->cpp[0];
b63a16f6
VS
2899
2900 while ((x + w) * cpp > fb->pitches[0]) {
2901 if (offset == 0) {
2902 DRM_DEBUG_KMS("Unable to find suitable display surface offset\n");
2903 return -EINVAL;
2904 }
2905
2906 offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
2907 offset, offset - alignment);
2908 }
2909 }
2910
2911 plane_state->main.offset = offset;
2912 plane_state->main.x = x;
2913 plane_state->main.y = y;
2914
2915 return 0;
2916}
2917
8d970654
VS
2918static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
2919{
2920 const struct drm_framebuffer *fb = plane_state->base.fb;
2921 unsigned int rotation = plane_state->base.rotation;
2922 int max_width = skl_max_plane_width(fb, 1, rotation);
2923 int max_height = 4096;
cc926387
DV
2924 int x = plane_state->base.src.x1 >> 17;
2925 int y = plane_state->base.src.y1 >> 17;
2926 int w = drm_rect_width(&plane_state->base.src) >> 17;
2927 int h = drm_rect_height(&plane_state->base.src) >> 17;
8d970654
VS
2928 u32 offset;
2929
2930 intel_add_fb_offsets(&x, &y, plane_state, 1);
2931 offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
2932
2933 /* FIXME not quite sure how/if these apply to the chroma plane */
2934 if (w > max_width || h > max_height) {
2935 DRM_DEBUG_KMS("CbCr source size %dx%d too big (limit %dx%d)\n",
2936 w, h, max_width, max_height);
2937 return -EINVAL;
2938 }
2939
2940 plane_state->aux.offset = offset;
2941 plane_state->aux.x = x;
2942 plane_state->aux.y = y;
2943
2944 return 0;
2945}
2946
b63a16f6
VS
2947int skl_check_plane_surface(struct intel_plane_state *plane_state)
2948{
2949 const struct drm_framebuffer *fb = plane_state->base.fb;
2950 unsigned int rotation = plane_state->base.rotation;
2951 int ret;
2952
a5e4c7d0
VS
2953 if (!plane_state->base.visible)
2954 return 0;
2955
b63a16f6 2956 /* Rotate src coordinates to match rotated GTT view */
bd2ef25d 2957 if (drm_rotation_90_or_270(rotation))
cc926387 2958 drm_rect_rotate(&plane_state->base.src,
da064b47 2959 fb->width << 16, fb->height << 16,
c2c446ad 2960 DRM_MODE_ROTATE_270);
b63a16f6 2961
8d970654
VS
2962 /*
2963 * Handle the AUX surface first since
2964 * the main surface setup depends on it.
2965 */
438b74a5 2966 if (fb->format->format == DRM_FORMAT_NV12) {
8d970654
VS
2967 ret = skl_check_nv12_aux_surface(plane_state);
2968 if (ret)
2969 return ret;
2970 } else {
2971 plane_state->aux.offset = ~0xfff;
2972 plane_state->aux.x = 0;
2973 plane_state->aux.y = 0;
2974 }
2975
b63a16f6
VS
2976 ret = skl_check_main_surface(plane_state);
2977 if (ret)
2978 return ret;
2979
2980 return 0;
2981}
2982
7145f60a
VS
2983static u32 i9xx_plane_ctl(const struct intel_crtc_state *crtc_state,
2984 const struct intel_plane_state *plane_state)
81255565 2985{
7145f60a
VS
2986 struct drm_i915_private *dev_priv =
2987 to_i915(plane_state->base.plane->dev);
2988 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
2989 const struct drm_framebuffer *fb = plane_state->base.fb;
8d0deca8 2990 unsigned int rotation = plane_state->base.rotation;
7145f60a 2991 u32 dspcntr;
c9ba6fad 2992
7145f60a 2993 dspcntr = DISPLAY_PLANE_ENABLE | DISPPLANE_GAMMA_ENABLE;
f45651ba 2994
6a4407a6
VS
2995 if (IS_G4X(dev_priv) || IS_GEN5(dev_priv) ||
2996 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
7145f60a 2997 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
f45651ba 2998
6a4407a6
VS
2999 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
3000 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
f45651ba 3001
d509e28b
VS
3002 if (INTEL_GEN(dev_priv) < 4)
3003 dspcntr |= DISPPLANE_SEL_PIPE(crtc->pipe);
81255565 3004
438b74a5 3005 switch (fb->format->format) {
57779d06 3006 case DRM_FORMAT_C8:
81255565
JB
3007 dspcntr |= DISPPLANE_8BPP;
3008 break;
57779d06 3009 case DRM_FORMAT_XRGB1555:
57779d06 3010 dspcntr |= DISPPLANE_BGRX555;
81255565 3011 break;
57779d06
VS
3012 case DRM_FORMAT_RGB565:
3013 dspcntr |= DISPPLANE_BGRX565;
3014 break;
3015 case DRM_FORMAT_XRGB8888:
57779d06
VS
3016 dspcntr |= DISPPLANE_BGRX888;
3017 break;
3018 case DRM_FORMAT_XBGR8888:
57779d06
VS
3019 dspcntr |= DISPPLANE_RGBX888;
3020 break;
3021 case DRM_FORMAT_XRGB2101010:
57779d06
VS
3022 dspcntr |= DISPPLANE_BGRX101010;
3023 break;
3024 case DRM_FORMAT_XBGR2101010:
57779d06 3025 dspcntr |= DISPPLANE_RGBX101010;
81255565
JB
3026 break;
3027 default:
7145f60a
VS
3028 MISSING_CASE(fb->format->format);
3029 return 0;
81255565 3030 }
57779d06 3031
72618ebf 3032 if (INTEL_GEN(dev_priv) >= 4 &&
bae781b2 3033 fb->modifier == I915_FORMAT_MOD_X_TILED)
f45651ba 3034 dspcntr |= DISPPLANE_TILED;
81255565 3035
c2c446ad 3036 if (rotation & DRM_MODE_ROTATE_180)
df0cd455
VS
3037 dspcntr |= DISPPLANE_ROTATE_180;
3038
c2c446ad 3039 if (rotation & DRM_MODE_REFLECT_X)
4ea7be2b
VS
3040 dspcntr |= DISPPLANE_MIRROR;
3041
7145f60a
VS
3042 return dspcntr;
3043}
de1aa629 3044
f9407ae1 3045int i9xx_check_plane_surface(struct intel_plane_state *plane_state)
5b7fcc44
VS
3046{
3047 struct drm_i915_private *dev_priv =
3048 to_i915(plane_state->base.plane->dev);
3049 int src_x = plane_state->base.src.x1 >> 16;
3050 int src_y = plane_state->base.src.y1 >> 16;
3051 u32 offset;
81255565 3052
5b7fcc44 3053 intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
e506a0c6 3054
5b7fcc44
VS
3055 if (INTEL_GEN(dev_priv) >= 4)
3056 offset = intel_compute_tile_offset(&src_x, &src_y,
3057 plane_state, 0);
3058 else
3059 offset = 0;
3060
3061 /* HSW/BDW do this automagically in hardware */
3062 if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv)) {
3063 unsigned int rotation = plane_state->base.rotation;
3064 int src_w = drm_rect_width(&plane_state->base.src) >> 16;
3065 int src_h = drm_rect_height(&plane_state->base.src) >> 16;
3066
c2c446ad 3067 if (rotation & DRM_MODE_ROTATE_180) {
5b7fcc44
VS
3068 src_x += src_w - 1;
3069 src_y += src_h - 1;
c2c446ad 3070 } else if (rotation & DRM_MODE_REFLECT_X) {
5b7fcc44
VS
3071 src_x += src_w - 1;
3072 }
48404c1e
SJ
3073 }
3074
5b7fcc44
VS
3075 plane_state->main.offset = offset;
3076 plane_state->main.x = src_x;
3077 plane_state->main.y = src_y;
3078
3079 return 0;
3080}
3081
282dbf9b 3082static void i9xx_update_primary_plane(struct intel_plane *primary,
7145f60a
VS
3083 const struct intel_crtc_state *crtc_state,
3084 const struct intel_plane_state *plane_state)
3085{
282dbf9b
VS
3086 struct drm_i915_private *dev_priv = to_i915(primary->base.dev);
3087 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
3088 const struct drm_framebuffer *fb = plane_state->base.fb;
3089 enum plane plane = primary->plane;
7145f60a 3090 u32 linear_offset;
a0864d59 3091 u32 dspcntr = plane_state->ctl;
7145f60a 3092 i915_reg_t reg = DSPCNTR(plane);
5b7fcc44
VS
3093 int x = plane_state->main.x;
3094 int y = plane_state->main.y;
7145f60a
VS
3095 unsigned long irqflags;
3096
2949056c 3097 linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
6687c906 3098
5b7fcc44 3099 if (INTEL_GEN(dev_priv) >= 4)
282dbf9b 3100 crtc->dspaddr_offset = plane_state->main.offset;
5b7fcc44 3101 else
282dbf9b 3102 crtc->dspaddr_offset = linear_offset;
6687c906 3103
282dbf9b
VS
3104 crtc->adjusted_x = x;
3105 crtc->adjusted_y = y;
2db3366b 3106
dd584fc0
VS
3107 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
3108
78587de2
VS
3109 if (INTEL_GEN(dev_priv) < 4) {
3110 /* pipesrc and dspsize control the size that is scaled from,
3111 * which should always be the user's requested size.
3112 */
dd584fc0
VS
3113 I915_WRITE_FW(DSPSIZE(plane),
3114 ((crtc_state->pipe_src_h - 1) << 16) |
3115 (crtc_state->pipe_src_w - 1));
3116 I915_WRITE_FW(DSPPOS(plane), 0);
78587de2 3117 } else if (IS_CHERRYVIEW(dev_priv) && plane == PLANE_B) {
dd584fc0
VS
3118 I915_WRITE_FW(PRIMSIZE(plane),
3119 ((crtc_state->pipe_src_h - 1) << 16) |
3120 (crtc_state->pipe_src_w - 1));
3121 I915_WRITE_FW(PRIMPOS(plane), 0);
3122 I915_WRITE_FW(PRIMCNSTALPHA(plane), 0);
78587de2
VS
3123 }
3124
dd584fc0 3125 I915_WRITE_FW(reg, dspcntr);
48404c1e 3126
dd584fc0 3127 I915_WRITE_FW(DSPSTRIDE(plane), fb->pitches[0]);
3ba35e53
VS
3128 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
3129 I915_WRITE_FW(DSPSURF(plane),
3130 intel_plane_ggtt_offset(plane_state) +
282dbf9b 3131 crtc->dspaddr_offset);
3ba35e53
VS
3132 I915_WRITE_FW(DSPOFFSET(plane), (y << 16) | x);
3133 } else if (INTEL_GEN(dev_priv) >= 4) {
dd584fc0
VS
3134 I915_WRITE_FW(DSPSURF(plane),
3135 intel_plane_ggtt_offset(plane_state) +
282dbf9b 3136 crtc->dspaddr_offset);
dd584fc0
VS
3137 I915_WRITE_FW(DSPTILEOFF(plane), (y << 16) | x);
3138 I915_WRITE_FW(DSPLINOFF(plane), linear_offset);
bfb81049 3139 } else {
dd584fc0
VS
3140 I915_WRITE_FW(DSPADDR(plane),
3141 intel_plane_ggtt_offset(plane_state) +
282dbf9b 3142 crtc->dspaddr_offset);
bfb81049 3143 }
dd584fc0
VS
3144 POSTING_READ_FW(reg);
3145
3146 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
17638cd6
JB
3147}
3148
282dbf9b
VS
3149static void i9xx_disable_primary_plane(struct intel_plane *primary,
3150 struct intel_crtc *crtc)
17638cd6 3151{
282dbf9b
VS
3152 struct drm_i915_private *dev_priv = to_i915(primary->base.dev);
3153 enum plane plane = primary->plane;
dd584fc0
VS
3154 unsigned long irqflags;
3155
3156 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
f45651ba 3157
dd584fc0 3158 I915_WRITE_FW(DSPCNTR(plane), 0);
a8d201af 3159 if (INTEL_INFO(dev_priv)->gen >= 4)
dd584fc0 3160 I915_WRITE_FW(DSPSURF(plane), 0);
a8d201af 3161 else
dd584fc0
VS
3162 I915_WRITE_FW(DSPADDR(plane), 0);
3163 POSTING_READ_FW(DSPCNTR(plane));
3164
3165 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
a8d201af 3166}
c9ba6fad 3167
d88c4afd
VS
3168static u32
3169intel_fb_stride_alignment(const struct drm_framebuffer *fb, int plane)
b321803d 3170{
2f075565 3171 if (fb->modifier == DRM_FORMAT_MOD_LINEAR)
b321803d 3172 return 64;
d88c4afd
VS
3173 else
3174 return intel_tile_width_bytes(fb, plane);
b321803d
DL
3175}
3176
e435d6e5
ML
3177static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
3178{
3179 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 3180 struct drm_i915_private *dev_priv = to_i915(dev);
e435d6e5
ML
3181
3182 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
3183 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
3184 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
e435d6e5
ML
3185}
3186
a1b2278e
CK
3187/*
3188 * This function detaches (aka. unbinds) unused scalers in hardware
3189 */
0583236e 3190static void skl_detach_scalers(struct intel_crtc *intel_crtc)
a1b2278e 3191{
a1b2278e
CK
3192 struct intel_crtc_scaler_state *scaler_state;
3193 int i;
3194
a1b2278e
CK
3195 scaler_state = &intel_crtc->config->scaler_state;
3196
3197 /* loop through and disable scalers that aren't in use */
3198 for (i = 0; i < intel_crtc->num_scalers; i++) {
e435d6e5
ML
3199 if (!scaler_state->scalers[i].in_use)
3200 skl_detach_scaler(intel_crtc, i);
a1b2278e
CK
3201 }
3202}
3203
d2196774
VS
3204u32 skl_plane_stride(const struct drm_framebuffer *fb, int plane,
3205 unsigned int rotation)
3206{
1b500535
VS
3207 u32 stride;
3208
3209 if (plane >= fb->format->num_planes)
3210 return 0;
3211
3212 stride = intel_fb_pitch(fb, plane, rotation);
d2196774
VS
3213
3214 /*
3215 * The stride is either expressed as a multiple of 64 bytes chunks for
3216 * linear buffers or in number of tiles for tiled buffers.
3217 */
d88c4afd
VS
3218 if (drm_rotation_90_or_270(rotation))
3219 stride /= intel_tile_height(fb, plane);
3220 else
3221 stride /= intel_fb_stride_alignment(fb, plane);
d2196774
VS
3222
3223 return stride;
3224}
3225
2e881264 3226static u32 skl_plane_ctl_format(uint32_t pixel_format)
70d21f0e 3227{
6156a456 3228 switch (pixel_format) {
d161cf7a 3229 case DRM_FORMAT_C8:
c34ce3d1 3230 return PLANE_CTL_FORMAT_INDEXED;
70d21f0e 3231 case DRM_FORMAT_RGB565:
c34ce3d1 3232 return PLANE_CTL_FORMAT_RGB_565;
70d21f0e 3233 case DRM_FORMAT_XBGR8888:
c34ce3d1 3234 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
6156a456 3235 case DRM_FORMAT_XRGB8888:
c34ce3d1 3236 return PLANE_CTL_FORMAT_XRGB_8888;
6156a456
CK
3237 /*
3238 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
3239 * to be already pre-multiplied. We need to add a knob (or a different
3240 * DRM_FORMAT) for user-space to configure that.
3241 */
f75fb42a 3242 case DRM_FORMAT_ABGR8888:
c34ce3d1 3243 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
6156a456 3244 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
6156a456 3245 case DRM_FORMAT_ARGB8888:
c34ce3d1 3246 return PLANE_CTL_FORMAT_XRGB_8888 |
6156a456 3247 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
70d21f0e 3248 case DRM_FORMAT_XRGB2101010:
c34ce3d1 3249 return PLANE_CTL_FORMAT_XRGB_2101010;
70d21f0e 3250 case DRM_FORMAT_XBGR2101010:
c34ce3d1 3251 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
6156a456 3252 case DRM_FORMAT_YUYV:
c34ce3d1 3253 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
6156a456 3254 case DRM_FORMAT_YVYU:
c34ce3d1 3255 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
6156a456 3256 case DRM_FORMAT_UYVY:
c34ce3d1 3257 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
6156a456 3258 case DRM_FORMAT_VYUY:
c34ce3d1 3259 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
70d21f0e 3260 default:
4249eeef 3261 MISSING_CASE(pixel_format);
70d21f0e 3262 }
8cfcba41 3263
c34ce3d1 3264 return 0;
6156a456 3265}
70d21f0e 3266
2e881264 3267static u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
6156a456 3268{
6156a456 3269 switch (fb_modifier) {
2f075565 3270 case DRM_FORMAT_MOD_LINEAR:
70d21f0e 3271 break;
30af77c4 3272 case I915_FORMAT_MOD_X_TILED:
c34ce3d1 3273 return PLANE_CTL_TILED_X;
b321803d 3274 case I915_FORMAT_MOD_Y_TILED:
c34ce3d1 3275 return PLANE_CTL_TILED_Y;
b321803d 3276 case I915_FORMAT_MOD_Yf_TILED:
c34ce3d1 3277 return PLANE_CTL_TILED_YF;
70d21f0e 3278 default:
6156a456 3279 MISSING_CASE(fb_modifier);
70d21f0e 3280 }
8cfcba41 3281
c34ce3d1 3282 return 0;
6156a456 3283}
70d21f0e 3284
2e881264 3285static u32 skl_plane_ctl_rotation(unsigned int rotation)
6156a456 3286{
3b7a5119 3287 switch (rotation) {
c2c446ad 3288 case DRM_MODE_ROTATE_0:
6156a456 3289 break;
1e8df167 3290 /*
c2c446ad 3291 * DRM_MODE_ROTATE_ is counter clockwise to stay compatible with Xrandr
1e8df167
SJ
3292 * while i915 HW rotation is clockwise, thats why this swapping.
3293 */
c2c446ad 3294 case DRM_MODE_ROTATE_90:
1e8df167 3295 return PLANE_CTL_ROTATE_270;
c2c446ad 3296 case DRM_MODE_ROTATE_180:
c34ce3d1 3297 return PLANE_CTL_ROTATE_180;
c2c446ad 3298 case DRM_MODE_ROTATE_270:
1e8df167 3299 return PLANE_CTL_ROTATE_90;
6156a456
CK
3300 default:
3301 MISSING_CASE(rotation);
3302 }
3303
c34ce3d1 3304 return 0;
6156a456
CK
3305}
3306
2e881264
VS
3307u32 skl_plane_ctl(const struct intel_crtc_state *crtc_state,
3308 const struct intel_plane_state *plane_state)
46f788ba
VS
3309{
3310 struct drm_i915_private *dev_priv =
3311 to_i915(plane_state->base.plane->dev);
3312 const struct drm_framebuffer *fb = plane_state->base.fb;
3313 unsigned int rotation = plane_state->base.rotation;
2e881264 3314 const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
46f788ba
VS
3315 u32 plane_ctl;
3316
3317 plane_ctl = PLANE_CTL_ENABLE;
3318
3319 if (!IS_GEMINILAKE(dev_priv)) {
3320 plane_ctl |=
3321 PLANE_CTL_PIPE_GAMMA_ENABLE |
3322 PLANE_CTL_PIPE_CSC_ENABLE |
3323 PLANE_CTL_PLANE_GAMMA_DISABLE;
3324 }
3325
3326 plane_ctl |= skl_plane_ctl_format(fb->format->format);
3327 plane_ctl |= skl_plane_ctl_tiling(fb->modifier);
3328 plane_ctl |= skl_plane_ctl_rotation(rotation);
3329
2e881264
VS
3330 if (key->flags & I915_SET_COLORKEY_DESTINATION)
3331 plane_ctl |= PLANE_CTL_KEY_ENABLE_DESTINATION;
3332 else if (key->flags & I915_SET_COLORKEY_SOURCE)
3333 plane_ctl |= PLANE_CTL_KEY_ENABLE_SOURCE;
3334
46f788ba
VS
3335 return plane_ctl;
3336}
3337
282dbf9b 3338static void skylake_update_primary_plane(struct intel_plane *plane,
a8d201af
ML
3339 const struct intel_crtc_state *crtc_state,
3340 const struct intel_plane_state *plane_state)
6156a456 3341{
282dbf9b
VS
3342 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
3343 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
3344 const struct drm_framebuffer *fb = plane_state->base.fb;
3345 enum plane_id plane_id = plane->id;
3346 enum pipe pipe = plane->pipe;
a0864d59 3347 u32 plane_ctl = plane_state->ctl;
a8d201af 3348 unsigned int rotation = plane_state->base.rotation;
d2196774 3349 u32 stride = skl_plane_stride(fb, 0, rotation);
b63a16f6 3350 u32 surf_addr = plane_state->main.offset;
a8d201af 3351 int scaler_id = plane_state->scaler_id;
b63a16f6
VS
3352 int src_x = plane_state->main.x;
3353 int src_y = plane_state->main.y;
936e71e3
VS
3354 int src_w = drm_rect_width(&plane_state->base.src) >> 16;
3355 int src_h = drm_rect_height(&plane_state->base.src) >> 16;
3356 int dst_x = plane_state->base.dst.x1;
3357 int dst_y = plane_state->base.dst.y1;
3358 int dst_w = drm_rect_width(&plane_state->base.dst);
3359 int dst_h = drm_rect_height(&plane_state->base.dst);
dd584fc0 3360 unsigned long irqflags;
70d21f0e 3361
6687c906
VS
3362 /* Sizes are 0 based */
3363 src_w--;
3364 src_h--;
3365 dst_w--;
3366 dst_h--;
3367
282dbf9b 3368 crtc->dspaddr_offset = surf_addr;
4c0b8a8b 3369
282dbf9b
VS
3370 crtc->adjusted_x = src_x;
3371 crtc->adjusted_y = src_y;
2db3366b 3372
dd584fc0
VS
3373 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
3374
78587de2 3375 if (IS_GEMINILAKE(dev_priv)) {
dd584fc0
VS
3376 I915_WRITE_FW(PLANE_COLOR_CTL(pipe, plane_id),
3377 PLANE_COLOR_PIPE_GAMMA_ENABLE |
3378 PLANE_COLOR_PIPE_CSC_ENABLE |
3379 PLANE_COLOR_PLANE_GAMMA_DISABLE);
78587de2
VS
3380 }
3381
dd584fc0
VS
3382 I915_WRITE_FW(PLANE_CTL(pipe, plane_id), plane_ctl);
3383 I915_WRITE_FW(PLANE_OFFSET(pipe, plane_id), (src_y << 16) | src_x);
3384 I915_WRITE_FW(PLANE_STRIDE(pipe, plane_id), stride);
3385 I915_WRITE_FW(PLANE_SIZE(pipe, plane_id), (src_h << 16) | src_w);
6156a456
CK
3386
3387 if (scaler_id >= 0) {
3388 uint32_t ps_ctrl = 0;
3389
3390 WARN_ON(!dst_w || !dst_h);
8e816bb4 3391 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(plane_id) |
6156a456 3392 crtc_state->scaler_state.scalers[scaler_id].mode;
dd584fc0
VS
3393 I915_WRITE_FW(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3394 I915_WRITE_FW(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3395 I915_WRITE_FW(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3396 I915_WRITE_FW(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3397 I915_WRITE_FW(PLANE_POS(pipe, plane_id), 0);
6156a456 3398 } else {
dd584fc0 3399 I915_WRITE_FW(PLANE_POS(pipe, plane_id), (dst_y << 16) | dst_x);
6156a456
CK
3400 }
3401
dd584fc0
VS
3402 I915_WRITE_FW(PLANE_SURF(pipe, plane_id),
3403 intel_plane_ggtt_offset(plane_state) + surf_addr);
70d21f0e 3404
dd584fc0
VS
3405 POSTING_READ_FW(PLANE_SURF(pipe, plane_id));
3406
3407 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
70d21f0e
DL
3408}
3409
282dbf9b
VS
3410static void skylake_disable_primary_plane(struct intel_plane *primary,
3411 struct intel_crtc *crtc)
17638cd6 3412{
282dbf9b
VS
3413 struct drm_i915_private *dev_priv = to_i915(primary->base.dev);
3414 enum plane_id plane_id = primary->id;
3415 enum pipe pipe = primary->pipe;
dd584fc0
VS
3416 unsigned long irqflags;
3417
3418 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
62e0fb88 3419
dd584fc0
VS
3420 I915_WRITE_FW(PLANE_CTL(pipe, plane_id), 0);
3421 I915_WRITE_FW(PLANE_SURF(pipe, plane_id), 0);
3422 POSTING_READ_FW(PLANE_SURF(pipe, plane_id));
3423
3424 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
a8d201af 3425}
29b9bde6 3426
5a21b665
DV
3427static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
3428{
3429 struct intel_crtc *crtc;
3430
91c8a326 3431 for_each_intel_crtc(&dev_priv->drm, crtc)
5a21b665
DV
3432 intel_finish_page_flip_cs(dev_priv, crtc->pipe);
3433}
3434
7514747d
VS
3435static void intel_update_primary_planes(struct drm_device *dev)
3436{
7514747d 3437 struct drm_crtc *crtc;
96a02917 3438
70e1e0ec 3439 for_each_crtc(dev, crtc) {
11c22da6 3440 struct intel_plane *plane = to_intel_plane(crtc->primary);
73974893
ML
3441 struct intel_plane_state *plane_state =
3442 to_intel_plane_state(plane->base.state);
11c22da6 3443
72259536
VS
3444 if (plane_state->base.visible) {
3445 trace_intel_update_plane(&plane->base,
3446 to_intel_crtc(crtc));
3447
282dbf9b 3448 plane->update_plane(plane,
a8d201af
ML
3449 to_intel_crtc_state(crtc->state),
3450 plane_state);
72259536 3451 }
73974893
ML
3452 }
3453}
3454
3455static int
3456__intel_display_resume(struct drm_device *dev,
581e49fe
ML
3457 struct drm_atomic_state *state,
3458 struct drm_modeset_acquire_ctx *ctx)
73974893
ML
3459{
3460 struct drm_crtc_state *crtc_state;
3461 struct drm_crtc *crtc;
3462 int i, ret;
11c22da6 3463
73974893 3464 intel_modeset_setup_hw_state(dev);
29b74b7f 3465 i915_redisable_vga(to_i915(dev));
73974893
ML
3466
3467 if (!state)
3468 return 0;
3469
aa5e9b47
ML
3470 /*
3471 * We've duplicated the state, pointers to the old state are invalid.
3472 *
3473 * Don't attempt to use the old state until we commit the duplicated state.
3474 */
3475 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
73974893
ML
3476 /*
3477 * Force recalculation even if we restore
3478 * current state. With fast modeset this may not result
3479 * in a modeset when the state is compatible.
3480 */
3481 crtc_state->mode_changed = true;
96a02917 3482 }
73974893
ML
3483
3484 /* ignore any reset values/BIOS leftovers in the WM registers */
602ae835
VS
3485 if (!HAS_GMCH_DISPLAY(to_i915(dev)))
3486 to_intel_atomic_state(state)->skip_intermediate_wm = true;
73974893 3487
581e49fe 3488 ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
73974893
ML
3489
3490 WARN_ON(ret == -EDEADLK);
3491 return ret;
96a02917
VS
3492}
3493
4ac2ba2f
VS
3494static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
3495{
ae98104b
VS
3496 return intel_has_gpu_reset(dev_priv) &&
3497 INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
4ac2ba2f
VS
3498}
3499
c033666a 3500void intel_prepare_reset(struct drm_i915_private *dev_priv)
7514747d 3501{
73974893
ML
3502 struct drm_device *dev = &dev_priv->drm;
3503 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
3504 struct drm_atomic_state *state;
3505 int ret;
3506
73974893
ML
3507 /*
3508 * Need mode_config.mutex so that we don't
3509 * trample ongoing ->detect() and whatnot.
3510 */
3511 mutex_lock(&dev->mode_config.mutex);
3512 drm_modeset_acquire_init(ctx, 0);
3513 while (1) {
3514 ret = drm_modeset_lock_all_ctx(dev, ctx);
3515 if (ret != -EDEADLK)
3516 break;
3517
3518 drm_modeset_backoff(ctx);
3519 }
3520
3521 /* reset doesn't touch the display, but flips might get nuked anyway, */
522a63de 3522 if (!i915.force_reset_modeset_test &&
4ac2ba2f 3523 !gpu_reset_clobbers_display(dev_priv))
7514747d
VS
3524 return;
3525
f98ce92f
VS
3526 /*
3527 * Disabling the crtcs gracefully seems nicer. Also the
3528 * g33 docs say we should at least disable all the planes.
3529 */
73974893
ML
3530 state = drm_atomic_helper_duplicate_state(dev, ctx);
3531 if (IS_ERR(state)) {
3532 ret = PTR_ERR(state);
73974893 3533 DRM_ERROR("Duplicating state failed with %i\n", ret);
1e5a15d6 3534 return;
73974893
ML
3535 }
3536
3537 ret = drm_atomic_helper_disable_all(dev, ctx);
3538 if (ret) {
3539 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
1e5a15d6
ACO
3540 drm_atomic_state_put(state);
3541 return;
73974893
ML
3542 }
3543
3544 dev_priv->modeset_restore_state = state;
3545 state->acquire_ctx = ctx;
7514747d
VS
3546}
3547
c033666a 3548void intel_finish_reset(struct drm_i915_private *dev_priv)
7514747d 3549{
73974893
ML
3550 struct drm_device *dev = &dev_priv->drm;
3551 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
3552 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
3553 int ret;
3554
5a21b665
DV
3555 /*
3556 * Flips in the rings will be nuked by the reset,
3557 * so complete all pending flips so that user space
3558 * will get its events and not get stuck.
3559 */
3560 intel_complete_page_flips(dev_priv);
3561
73974893
ML
3562 dev_priv->modeset_restore_state = NULL;
3563
7514747d 3564 /* reset doesn't touch the display */
4ac2ba2f 3565 if (!gpu_reset_clobbers_display(dev_priv)) {
522a63de
ML
3566 if (!state) {
3567 /*
3568 * Flips in the rings have been nuked by the reset,
3569 * so update the base address of all primary
3570 * planes to the the last fb to make sure we're
3571 * showing the correct fb after a reset.
3572 *
3573 * FIXME: Atomic will make this obsolete since we won't schedule
3574 * CS-based flips (which might get lost in gpu resets) any more.
3575 */
3576 intel_update_primary_planes(dev);
3577 } else {
581e49fe 3578 ret = __intel_display_resume(dev, state, ctx);
522a63de
ML
3579 if (ret)
3580 DRM_ERROR("Restoring old state failed with %i\n", ret);
3581 }
73974893
ML
3582 } else {
3583 /*
3584 * The display has been reset as well,
3585 * so need a full re-initialization.
3586 */
3587 intel_runtime_pm_disable_interrupts(dev_priv);
3588 intel_runtime_pm_enable_interrupts(dev_priv);
7514747d 3589
51f59205 3590 intel_pps_unlock_regs_wa(dev_priv);
73974893 3591 intel_modeset_init_hw(dev);
7514747d 3592
73974893
ML
3593 spin_lock_irq(&dev_priv->irq_lock);
3594 if (dev_priv->display.hpd_irq_setup)
3595 dev_priv->display.hpd_irq_setup(dev_priv);
3596 spin_unlock_irq(&dev_priv->irq_lock);
7514747d 3597
581e49fe 3598 ret = __intel_display_resume(dev, state, ctx);
73974893
ML
3599 if (ret)
3600 DRM_ERROR("Restoring old state failed with %i\n", ret);
7514747d 3601
73974893
ML
3602 intel_hpd_init(dev_priv);
3603 }
7514747d 3604
0853695c
CW
3605 if (state)
3606 drm_atomic_state_put(state);
73974893
ML
3607 drm_modeset_drop_locks(ctx);
3608 drm_modeset_acquire_fini(ctx);
3609 mutex_unlock(&dev->mode_config.mutex);
7514747d
VS
3610}
3611
8af29b0c
CW
3612static bool abort_flip_on_reset(struct intel_crtc *crtc)
3613{
3614 struct i915_gpu_error *error = &to_i915(crtc->base.dev)->gpu_error;
3615
8c185eca 3616 if (i915_reset_backoff(error))
8af29b0c
CW
3617 return true;
3618
3619 if (crtc->reset_count != i915_reset_count(error))
3620 return true;
3621
3622 return false;
3623}
3624
7d5e3799
CW
3625static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3626{
5a21b665
DV
3627 struct drm_device *dev = crtc->dev;
3628 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5a21b665
DV
3629 bool pending;
3630
8af29b0c 3631 if (abort_flip_on_reset(intel_crtc))
5a21b665
DV
3632 return false;
3633
3634 spin_lock_irq(&dev->event_lock);
3635 pending = to_intel_crtc(crtc)->flip_work != NULL;
3636 spin_unlock_irq(&dev->event_lock);
3637
3638 return pending;
7d5e3799
CW
3639}
3640
bfd16b2a
ML
3641static void intel_update_pipe_config(struct intel_crtc *crtc,
3642 struct intel_crtc_state *old_crtc_state)
e30e8f75 3643{
6315b5d3 3644 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
bfd16b2a
ML
3645 struct intel_crtc_state *pipe_config =
3646 to_intel_crtc_state(crtc->base.state);
e30e8f75 3647
bfd16b2a
ML
3648 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3649 crtc->base.mode = crtc->base.state->mode;
3650
e30e8f75
GP
3651 /*
3652 * Update pipe size and adjust fitter if needed: the reason for this is
3653 * that in compute_mode_changes we check the native mode (not the pfit
3654 * mode) to see if we can flip rather than do a full mode set. In the
3655 * fastboot case, we'll flip, but if we don't update the pipesrc and
3656 * pfit state, we'll end up with a big fb scanned out into the wrong
3657 * sized surface.
e30e8f75
GP
3658 */
3659
e30e8f75 3660 I915_WRITE(PIPESRC(crtc->pipe),
bfd16b2a
ML
3661 ((pipe_config->pipe_src_w - 1) << 16) |
3662 (pipe_config->pipe_src_h - 1));
3663
3664 /* on skylake this is done by detaching scalers */
6315b5d3 3665 if (INTEL_GEN(dev_priv) >= 9) {
bfd16b2a
ML
3666 skl_detach_scalers(crtc);
3667
3668 if (pipe_config->pch_pfit.enabled)
3669 skylake_pfit_enable(crtc);
6e266956 3670 } else if (HAS_PCH_SPLIT(dev_priv)) {
bfd16b2a
ML
3671 if (pipe_config->pch_pfit.enabled)
3672 ironlake_pfit_enable(crtc);
3673 else if (old_crtc_state->pch_pfit.enabled)
3674 ironlake_pfit_disable(crtc, true);
e30e8f75 3675 }
e30e8f75
GP
3676}
3677
4cbe4b2b 3678static void intel_fdi_normal_train(struct intel_crtc *crtc)
5e84e1a4 3679{
4cbe4b2b 3680 struct drm_device *dev = crtc->base.dev;
fac5e23e 3681 struct drm_i915_private *dev_priv = to_i915(dev);
4cbe4b2b 3682 int pipe = crtc->pipe;
f0f59a00
VS
3683 i915_reg_t reg;
3684 u32 temp;
5e84e1a4
ZW
3685
3686 /* enable normal train */
3687 reg = FDI_TX_CTL(pipe);
3688 temp = I915_READ(reg);
fd6b8f43 3689 if (IS_IVYBRIDGE(dev_priv)) {
357555c0
JB
3690 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3691 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
3692 } else {
3693 temp &= ~FDI_LINK_TRAIN_NONE;
3694 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 3695 }
5e84e1a4
ZW
3696 I915_WRITE(reg, temp);
3697
3698 reg = FDI_RX_CTL(pipe);
3699 temp = I915_READ(reg);
6e266956 3700 if (HAS_PCH_CPT(dev_priv)) {
5e84e1a4
ZW
3701 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3702 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3703 } else {
3704 temp &= ~FDI_LINK_TRAIN_NONE;
3705 temp |= FDI_LINK_TRAIN_NONE;
3706 }
3707 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3708
3709 /* wait one idle pattern time */
3710 POSTING_READ(reg);
3711 udelay(1000);
357555c0
JB
3712
3713 /* IVB wants error correction enabled */
fd6b8f43 3714 if (IS_IVYBRIDGE(dev_priv))
357555c0
JB
3715 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3716 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
3717}
3718
8db9d77b 3719/* The FDI link training functions for ILK/Ibexpeak. */
dc4a1094
ACO
3720static void ironlake_fdi_link_train(struct intel_crtc *crtc,
3721 const struct intel_crtc_state *crtc_state)
8db9d77b 3722{
4cbe4b2b 3723 struct drm_device *dev = crtc->base.dev;
fac5e23e 3724 struct drm_i915_private *dev_priv = to_i915(dev);
4cbe4b2b 3725 int pipe = crtc->pipe;
f0f59a00
VS
3726 i915_reg_t reg;
3727 u32 temp, tries;
8db9d77b 3728
1c8562f6 3729 /* FDI needs bits from pipe first */
0fc932b8 3730 assert_pipe_enabled(dev_priv, pipe);
0fc932b8 3731
e1a44743
AJ
3732 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3733 for train result */
5eddb70b
CW
3734 reg = FDI_RX_IMR(pipe);
3735 temp = I915_READ(reg);
e1a44743
AJ
3736 temp &= ~FDI_RX_SYMBOL_LOCK;
3737 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3738 I915_WRITE(reg, temp);
3739 I915_READ(reg);
e1a44743
AJ
3740 udelay(150);
3741
8db9d77b 3742 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3743 reg = FDI_TX_CTL(pipe);
3744 temp = I915_READ(reg);
627eb5a3 3745 temp &= ~FDI_DP_PORT_WIDTH_MASK;
dc4a1094 3746 temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
8db9d77b
ZW
3747 temp &= ~FDI_LINK_TRAIN_NONE;
3748 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 3749 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3750
5eddb70b
CW
3751 reg = FDI_RX_CTL(pipe);
3752 temp = I915_READ(reg);
8db9d77b
ZW
3753 temp &= ~FDI_LINK_TRAIN_NONE;
3754 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b
CW
3755 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3756
3757 POSTING_READ(reg);
8db9d77b
ZW
3758 udelay(150);
3759
5b2adf89 3760 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
DV
3761 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3762 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3763 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 3764
5eddb70b 3765 reg = FDI_RX_IIR(pipe);
e1a44743 3766 for (tries = 0; tries < 5; tries++) {
5eddb70b 3767 temp = I915_READ(reg);
8db9d77b
ZW
3768 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3769
3770 if ((temp & FDI_RX_BIT_LOCK)) {
3771 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 3772 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
3773 break;
3774 }
8db9d77b 3775 }
e1a44743 3776 if (tries == 5)
5eddb70b 3777 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3778
3779 /* Train 2 */
5eddb70b
CW
3780 reg = FDI_TX_CTL(pipe);
3781 temp = I915_READ(reg);
8db9d77b
ZW
3782 temp &= ~FDI_LINK_TRAIN_NONE;
3783 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3784 I915_WRITE(reg, temp);
8db9d77b 3785
5eddb70b
CW
3786 reg = FDI_RX_CTL(pipe);
3787 temp = I915_READ(reg);
8db9d77b
ZW
3788 temp &= ~FDI_LINK_TRAIN_NONE;
3789 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3790 I915_WRITE(reg, temp);
8db9d77b 3791
5eddb70b
CW
3792 POSTING_READ(reg);
3793 udelay(150);
8db9d77b 3794
5eddb70b 3795 reg = FDI_RX_IIR(pipe);
e1a44743 3796 for (tries = 0; tries < 5; tries++) {
5eddb70b 3797 temp = I915_READ(reg);
8db9d77b
ZW
3798 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3799
3800 if (temp & FDI_RX_SYMBOL_LOCK) {
5eddb70b 3801 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
3802 DRM_DEBUG_KMS("FDI train 2 done.\n");
3803 break;
3804 }
8db9d77b 3805 }
e1a44743 3806 if (tries == 5)
5eddb70b 3807 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3808
3809 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 3810
8db9d77b
ZW
3811}
3812
0206e353 3813static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
3814 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3815 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3816 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3817 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3818};
3819
3820/* The FDI link training functions for SNB/Cougarpoint. */
dc4a1094
ACO
3821static void gen6_fdi_link_train(struct intel_crtc *crtc,
3822 const struct intel_crtc_state *crtc_state)
8db9d77b 3823{
4cbe4b2b 3824 struct drm_device *dev = crtc->base.dev;
fac5e23e 3825 struct drm_i915_private *dev_priv = to_i915(dev);
4cbe4b2b 3826 int pipe = crtc->pipe;
f0f59a00
VS
3827 i915_reg_t reg;
3828 u32 temp, i, retry;
8db9d77b 3829
e1a44743
AJ
3830 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3831 for train result */
5eddb70b
CW
3832 reg = FDI_RX_IMR(pipe);
3833 temp = I915_READ(reg);
e1a44743
AJ
3834 temp &= ~FDI_RX_SYMBOL_LOCK;
3835 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3836 I915_WRITE(reg, temp);
3837
3838 POSTING_READ(reg);
e1a44743
AJ
3839 udelay(150);
3840
8db9d77b 3841 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3842 reg = FDI_TX_CTL(pipe);
3843 temp = I915_READ(reg);
627eb5a3 3844 temp &= ~FDI_DP_PORT_WIDTH_MASK;
dc4a1094 3845 temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
8db9d77b
ZW
3846 temp &= ~FDI_LINK_TRAIN_NONE;
3847 temp |= FDI_LINK_TRAIN_PATTERN_1;
3848 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3849 /* SNB-B */
3850 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 3851 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3852
d74cf324
DV
3853 I915_WRITE(FDI_RX_MISC(pipe),
3854 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3855
5eddb70b
CW
3856 reg = FDI_RX_CTL(pipe);
3857 temp = I915_READ(reg);
6e266956 3858 if (HAS_PCH_CPT(dev_priv)) {
8db9d77b
ZW
3859 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3860 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3861 } else {
3862 temp &= ~FDI_LINK_TRAIN_NONE;
3863 temp |= FDI_LINK_TRAIN_PATTERN_1;
3864 }
5eddb70b
CW
3865 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3866
3867 POSTING_READ(reg);
8db9d77b
ZW
3868 udelay(150);
3869
0206e353 3870 for (i = 0; i < 4; i++) {
5eddb70b
CW
3871 reg = FDI_TX_CTL(pipe);
3872 temp = I915_READ(reg);
8db9d77b
ZW
3873 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3874 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3875 I915_WRITE(reg, temp);
3876
3877 POSTING_READ(reg);
8db9d77b
ZW
3878 udelay(500);
3879
fa37d39e
SP
3880 for (retry = 0; retry < 5; retry++) {
3881 reg = FDI_RX_IIR(pipe);
3882 temp = I915_READ(reg);
3883 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3884 if (temp & FDI_RX_BIT_LOCK) {
3885 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3886 DRM_DEBUG_KMS("FDI train 1 done.\n");
3887 break;
3888 }
3889 udelay(50);
8db9d77b 3890 }
fa37d39e
SP
3891 if (retry < 5)
3892 break;
8db9d77b
ZW
3893 }
3894 if (i == 4)
5eddb70b 3895 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3896
3897 /* Train 2 */
5eddb70b
CW
3898 reg = FDI_TX_CTL(pipe);
3899 temp = I915_READ(reg);
8db9d77b
ZW
3900 temp &= ~FDI_LINK_TRAIN_NONE;
3901 temp |= FDI_LINK_TRAIN_PATTERN_2;
5db94019 3902 if (IS_GEN6(dev_priv)) {
8db9d77b
ZW
3903 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3904 /* SNB-B */
3905 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3906 }
5eddb70b 3907 I915_WRITE(reg, temp);
8db9d77b 3908
5eddb70b
CW
3909 reg = FDI_RX_CTL(pipe);
3910 temp = I915_READ(reg);
6e266956 3911 if (HAS_PCH_CPT(dev_priv)) {
8db9d77b
ZW
3912 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3913 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3914 } else {
3915 temp &= ~FDI_LINK_TRAIN_NONE;
3916 temp |= FDI_LINK_TRAIN_PATTERN_2;
3917 }
5eddb70b
CW
3918 I915_WRITE(reg, temp);
3919
3920 POSTING_READ(reg);
8db9d77b
ZW
3921 udelay(150);
3922
0206e353 3923 for (i = 0; i < 4; i++) {
5eddb70b
CW
3924 reg = FDI_TX_CTL(pipe);
3925 temp = I915_READ(reg);
8db9d77b
ZW
3926 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3927 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3928 I915_WRITE(reg, temp);
3929
3930 POSTING_READ(reg);
8db9d77b
ZW
3931 udelay(500);
3932
fa37d39e
SP
3933 for (retry = 0; retry < 5; retry++) {
3934 reg = FDI_RX_IIR(pipe);
3935 temp = I915_READ(reg);
3936 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3937 if (temp & FDI_RX_SYMBOL_LOCK) {
3938 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3939 DRM_DEBUG_KMS("FDI train 2 done.\n");
3940 break;
3941 }
3942 udelay(50);
8db9d77b 3943 }
fa37d39e
SP
3944 if (retry < 5)
3945 break;
8db9d77b
ZW
3946 }
3947 if (i == 4)
5eddb70b 3948 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3949
3950 DRM_DEBUG_KMS("FDI train done.\n");
3951}
3952
357555c0 3953/* Manual link training for Ivy Bridge A0 parts */
dc4a1094
ACO
3954static void ivb_manual_fdi_link_train(struct intel_crtc *crtc,
3955 const struct intel_crtc_state *crtc_state)
357555c0 3956{
4cbe4b2b 3957 struct drm_device *dev = crtc->base.dev;
fac5e23e 3958 struct drm_i915_private *dev_priv = to_i915(dev);
4cbe4b2b 3959 int pipe = crtc->pipe;
f0f59a00
VS
3960 i915_reg_t reg;
3961 u32 temp, i, j;
357555c0
JB
3962
3963 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3964 for train result */
3965 reg = FDI_RX_IMR(pipe);
3966 temp = I915_READ(reg);
3967 temp &= ~FDI_RX_SYMBOL_LOCK;
3968 temp &= ~FDI_RX_BIT_LOCK;
3969 I915_WRITE(reg, temp);
3970
3971 POSTING_READ(reg);
3972 udelay(150);
3973
01a415fd
DV
3974 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3975 I915_READ(FDI_RX_IIR(pipe)));
3976
139ccd3f
JB
3977 /* Try each vswing and preemphasis setting twice before moving on */
3978 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3979 /* disable first in case we need to retry */
3980 reg = FDI_TX_CTL(pipe);
3981 temp = I915_READ(reg);
3982 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3983 temp &= ~FDI_TX_ENABLE;
3984 I915_WRITE(reg, temp);
357555c0 3985
139ccd3f
JB
3986 reg = FDI_RX_CTL(pipe);
3987 temp = I915_READ(reg);
3988 temp &= ~FDI_LINK_TRAIN_AUTO;
3989 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3990 temp &= ~FDI_RX_ENABLE;
3991 I915_WRITE(reg, temp);
357555c0 3992
139ccd3f 3993 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
3994 reg = FDI_TX_CTL(pipe);
3995 temp = I915_READ(reg);
139ccd3f 3996 temp &= ~FDI_DP_PORT_WIDTH_MASK;
dc4a1094 3997 temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
139ccd3f 3998 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 3999 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
4000 temp |= snb_b_fdi_train_param[j/2];
4001 temp |= FDI_COMPOSITE_SYNC;
4002 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 4003
139ccd3f
JB
4004 I915_WRITE(FDI_RX_MISC(pipe),
4005 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 4006
139ccd3f 4007 reg = FDI_RX_CTL(pipe);
357555c0 4008 temp = I915_READ(reg);
139ccd3f
JB
4009 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
4010 temp |= FDI_COMPOSITE_SYNC;
4011 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 4012
139ccd3f
JB
4013 POSTING_READ(reg);
4014 udelay(1); /* should be 0.5us */
357555c0 4015
139ccd3f
JB
4016 for (i = 0; i < 4; i++) {
4017 reg = FDI_RX_IIR(pipe);
4018 temp = I915_READ(reg);
4019 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 4020
139ccd3f
JB
4021 if (temp & FDI_RX_BIT_LOCK ||
4022 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
4023 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
4024 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
4025 i);
4026 break;
4027 }
4028 udelay(1); /* should be 0.5us */
4029 }
4030 if (i == 4) {
4031 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
4032 continue;
4033 }
357555c0 4034
139ccd3f 4035 /* Train 2 */
357555c0
JB
4036 reg = FDI_TX_CTL(pipe);
4037 temp = I915_READ(reg);
139ccd3f
JB
4038 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
4039 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
4040 I915_WRITE(reg, temp);
4041
4042 reg = FDI_RX_CTL(pipe);
4043 temp = I915_READ(reg);
4044 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4045 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
4046 I915_WRITE(reg, temp);
4047
4048 POSTING_READ(reg);
139ccd3f 4049 udelay(2); /* should be 1.5us */
357555c0 4050
139ccd3f
JB
4051 for (i = 0; i < 4; i++) {
4052 reg = FDI_RX_IIR(pipe);
4053 temp = I915_READ(reg);
4054 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 4055
139ccd3f
JB
4056 if (temp & FDI_RX_SYMBOL_LOCK ||
4057 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
4058 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
4059 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
4060 i);
4061 goto train_done;
4062 }
4063 udelay(2); /* should be 1.5us */
357555c0 4064 }
139ccd3f
JB
4065 if (i == 4)
4066 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 4067 }
357555c0 4068
139ccd3f 4069train_done:
357555c0
JB
4070 DRM_DEBUG_KMS("FDI train done.\n");
4071}
4072
88cefb6c 4073static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 4074{
88cefb6c 4075 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 4076 struct drm_i915_private *dev_priv = to_i915(dev);
2c07245f 4077 int pipe = intel_crtc->pipe;
f0f59a00
VS
4078 i915_reg_t reg;
4079 u32 temp;
c64e311e 4080
c98e9dcf 4081 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
4082 reg = FDI_RX_CTL(pipe);
4083 temp = I915_READ(reg);
627eb5a3 4084 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
6e3c9717 4085 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
dfd07d72 4086 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
4087 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
4088
4089 POSTING_READ(reg);
c98e9dcf
JB
4090 udelay(200);
4091
4092 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
4093 temp = I915_READ(reg);
4094 I915_WRITE(reg, temp | FDI_PCDCLK);
4095
4096 POSTING_READ(reg);
c98e9dcf
JB
4097 udelay(200);
4098
20749730
PZ
4099 /* Enable CPU FDI TX PLL, always on for Ironlake */
4100 reg = FDI_TX_CTL(pipe);
4101 temp = I915_READ(reg);
4102 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
4103 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 4104
20749730
PZ
4105 POSTING_READ(reg);
4106 udelay(100);
6be4a607 4107 }
0e23b99d
JB
4108}
4109
88cefb6c
DV
4110static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
4111{
4112 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 4113 struct drm_i915_private *dev_priv = to_i915(dev);
88cefb6c 4114 int pipe = intel_crtc->pipe;
f0f59a00
VS
4115 i915_reg_t reg;
4116 u32 temp;
88cefb6c
DV
4117
4118 /* Switch from PCDclk to Rawclk */
4119 reg = FDI_RX_CTL(pipe);
4120 temp = I915_READ(reg);
4121 I915_WRITE(reg, temp & ~FDI_PCDCLK);
4122
4123 /* Disable CPU FDI TX PLL */
4124 reg = FDI_TX_CTL(pipe);
4125 temp = I915_READ(reg);
4126 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
4127
4128 POSTING_READ(reg);
4129 udelay(100);
4130
4131 reg = FDI_RX_CTL(pipe);
4132 temp = I915_READ(reg);
4133 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
4134
4135 /* Wait for the clocks to turn off. */
4136 POSTING_READ(reg);
4137 udelay(100);
4138}
4139
0fc932b8
JB
4140static void ironlake_fdi_disable(struct drm_crtc *crtc)
4141{
4142 struct drm_device *dev = crtc->dev;
fac5e23e 4143 struct drm_i915_private *dev_priv = to_i915(dev);
0fc932b8
JB
4144 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4145 int pipe = intel_crtc->pipe;
f0f59a00
VS
4146 i915_reg_t reg;
4147 u32 temp;
0fc932b8
JB
4148
4149 /* disable CPU FDI tx and PCH FDI rx */
4150 reg = FDI_TX_CTL(pipe);
4151 temp = I915_READ(reg);
4152 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
4153 POSTING_READ(reg);
4154
4155 reg = FDI_RX_CTL(pipe);
4156 temp = I915_READ(reg);
4157 temp &= ~(0x7 << 16);
dfd07d72 4158 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
4159 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
4160
4161 POSTING_READ(reg);
4162 udelay(100);
4163
4164 /* Ironlake workaround, disable clock pointer after downing FDI */
6e266956 4165 if (HAS_PCH_IBX(dev_priv))
6f06ce18 4166 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
0fc932b8
JB
4167
4168 /* still set train pattern 1 */
4169 reg = FDI_TX_CTL(pipe);
4170 temp = I915_READ(reg);
4171 temp &= ~FDI_LINK_TRAIN_NONE;
4172 temp |= FDI_LINK_TRAIN_PATTERN_1;
4173 I915_WRITE(reg, temp);
4174
4175 reg = FDI_RX_CTL(pipe);
4176 temp = I915_READ(reg);
6e266956 4177 if (HAS_PCH_CPT(dev_priv)) {
0fc932b8
JB
4178 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4179 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
4180 } else {
4181 temp &= ~FDI_LINK_TRAIN_NONE;
4182 temp |= FDI_LINK_TRAIN_PATTERN_1;
4183 }
4184 /* BPC in FDI rx is consistent with that in PIPECONF */
4185 temp &= ~(0x07 << 16);
dfd07d72 4186 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
4187 I915_WRITE(reg, temp);
4188
4189 POSTING_READ(reg);
4190 udelay(100);
4191}
4192
49d73912 4193bool intel_has_pending_fb_unpin(struct drm_i915_private *dev_priv)
5dce5b93
CW
4194{
4195 struct intel_crtc *crtc;
4196
4197 /* Note that we don't need to be called with mode_config.lock here
4198 * as our list of CRTC objects is static for the lifetime of the
4199 * device and so cannot disappear as we iterate. Similarly, we can
4200 * happily treat the predicates as racy, atomic checks as userspace
4201 * cannot claim and pin a new fb without at least acquring the
4202 * struct_mutex and so serialising with us.
4203 */
49d73912 4204 for_each_intel_crtc(&dev_priv->drm, crtc) {
5dce5b93
CW
4205 if (atomic_read(&crtc->unpin_work_count) == 0)
4206 continue;
4207
5a21b665 4208 if (crtc->flip_work)
0f0f74bc 4209 intel_wait_for_vblank(dev_priv, crtc->pipe);
5dce5b93
CW
4210
4211 return true;
4212 }
4213
4214 return false;
4215}
4216
5a21b665 4217static void page_flip_completed(struct intel_crtc *intel_crtc)
d6bbafa1
CW
4218{
4219 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
5a21b665
DV
4220 struct intel_flip_work *work = intel_crtc->flip_work;
4221
4222 intel_crtc->flip_work = NULL;
d6bbafa1
CW
4223
4224 if (work->event)
560ce1dc 4225 drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
d6bbafa1
CW
4226
4227 drm_crtc_vblank_put(&intel_crtc->base);
4228
5a21b665 4229 wake_up_all(&dev_priv->pending_flip_queue);
5a21b665
DV
4230 trace_i915_flip_complete(intel_crtc->plane,
4231 work->pending_flip_obj);
05c41f92
AR
4232
4233 queue_work(dev_priv->wq, &work->unpin_work);
d6bbafa1
CW
4234}
4235
5008e874 4236static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
e6c3a2a6 4237{
0f91128d 4238 struct drm_device *dev = crtc->dev;
fac5e23e 4239 struct drm_i915_private *dev_priv = to_i915(dev);
5008e874 4240 long ret;
e6c3a2a6 4241
2c10d571 4242 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
5008e874
ML
4243
4244 ret = wait_event_interruptible_timeout(
4245 dev_priv->pending_flip_queue,
4246 !intel_crtc_has_pending_flip(crtc),
4247 60*HZ);
4248
4249 if (ret < 0)
4250 return ret;
4251
5a21b665
DV
4252 if (ret == 0) {
4253 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4254 struct intel_flip_work *work;
4255
4256 spin_lock_irq(&dev->event_lock);
4257 work = intel_crtc->flip_work;
4258 if (work && !is_mmio_work(work)) {
4259 WARN_ONCE(1, "Removing stuck page flip\n");
4260 page_flip_completed(intel_crtc);
4261 }
4262 spin_unlock_irq(&dev->event_lock);
4263 }
5bb61643 4264
5008e874 4265 return 0;
e6c3a2a6
CW
4266}
4267
b7076546 4268void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
060f02d8
VS
4269{
4270 u32 temp;
4271
4272 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
4273
4274 mutex_lock(&dev_priv->sb_lock);
4275
4276 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4277 temp |= SBI_SSCCTL_DISABLE;
4278 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
4279
4280 mutex_unlock(&dev_priv->sb_lock);
4281}
4282
e615efe4 4283/* Program iCLKIP clock to the desired frequency */
0dcdc382 4284static void lpt_program_iclkip(struct intel_crtc *crtc)
e615efe4 4285{
0dcdc382
ACO
4286 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4287 int clock = crtc->config->base.adjusted_mode.crtc_clock;
e615efe4
ED
4288 u32 divsel, phaseinc, auxdiv, phasedir = 0;
4289 u32 temp;
4290
060f02d8 4291 lpt_disable_iclkip(dev_priv);
e615efe4 4292
64b46a06
VS
4293 /* The iCLK virtual clock root frequency is in MHz,
4294 * but the adjusted_mode->crtc_clock in in KHz. To get the
4295 * divisors, it is necessary to divide one by another, so we
4296 * convert the virtual clock precision to KHz here for higher
4297 * precision.
4298 */
4299 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
e615efe4
ED
4300 u32 iclk_virtual_root_freq = 172800 * 1000;
4301 u32 iclk_pi_range = 64;
64b46a06 4302 u32 desired_divisor;
e615efe4 4303
64b46a06
VS
4304 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4305 clock << auxdiv);
4306 divsel = (desired_divisor / iclk_pi_range) - 2;
4307 phaseinc = desired_divisor % iclk_pi_range;
e615efe4 4308
64b46a06
VS
4309 /*
4310 * Near 20MHz is a corner case which is
4311 * out of range for the 7-bit divisor
4312 */
4313 if (divsel <= 0x7f)
4314 break;
e615efe4
ED
4315 }
4316
4317 /* This should not happen with any sane values */
4318 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
4319 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
4320 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
4321 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
4322
4323 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 4324 clock,
e615efe4
ED
4325 auxdiv,
4326 divsel,
4327 phasedir,
4328 phaseinc);
4329
060f02d8
VS
4330 mutex_lock(&dev_priv->sb_lock);
4331
e615efe4 4332 /* Program SSCDIVINTPHASE6 */
988d6ee8 4333 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
4334 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
4335 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
4336 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
4337 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
4338 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
4339 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 4340 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
4341
4342 /* Program SSCAUXDIV */
988d6ee8 4343 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
4344 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
4345 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 4346 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
4347
4348 /* Enable modulator and associated divider */
988d6ee8 4349 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 4350 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 4351 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4 4352
060f02d8
VS
4353 mutex_unlock(&dev_priv->sb_lock);
4354
e615efe4
ED
4355 /* Wait for initialization time */
4356 udelay(24);
4357
4358 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
4359}
4360
8802e5b6
VS
4361int lpt_get_iclkip(struct drm_i915_private *dev_priv)
4362{
4363 u32 divsel, phaseinc, auxdiv;
4364 u32 iclk_virtual_root_freq = 172800 * 1000;
4365 u32 iclk_pi_range = 64;
4366 u32 desired_divisor;
4367 u32 temp;
4368
4369 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
4370 return 0;
4371
4372 mutex_lock(&dev_priv->sb_lock);
4373
4374 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4375 if (temp & SBI_SSCCTL_DISABLE) {
4376 mutex_unlock(&dev_priv->sb_lock);
4377 return 0;
4378 }
4379
4380 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
4381 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
4382 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
4383 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
4384 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
4385
4386 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
4387 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
4388 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
4389
4390 mutex_unlock(&dev_priv->sb_lock);
4391
4392 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
4393
4394 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4395 desired_divisor << auxdiv);
4396}
4397
275f01b2
DV
4398static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4399 enum pipe pch_transcoder)
4400{
4401 struct drm_device *dev = crtc->base.dev;
fac5e23e 4402 struct drm_i915_private *dev_priv = to_i915(dev);
6e3c9717 4403 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
275f01b2
DV
4404
4405 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4406 I915_READ(HTOTAL(cpu_transcoder)));
4407 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4408 I915_READ(HBLANK(cpu_transcoder)));
4409 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4410 I915_READ(HSYNC(cpu_transcoder)));
4411
4412 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4413 I915_READ(VTOTAL(cpu_transcoder)));
4414 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4415 I915_READ(VBLANK(cpu_transcoder)));
4416 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4417 I915_READ(VSYNC(cpu_transcoder)));
4418 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4419 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4420}
4421
003632d9 4422static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
1fbc0d78 4423{
fac5e23e 4424 struct drm_i915_private *dev_priv = to_i915(dev);
1fbc0d78
DV
4425 uint32_t temp;
4426
4427 temp = I915_READ(SOUTH_CHICKEN1);
003632d9 4428 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
1fbc0d78
DV
4429 return;
4430
4431 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4432 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4433
003632d9
ACO
4434 temp &= ~FDI_BC_BIFURCATION_SELECT;
4435 if (enable)
4436 temp |= FDI_BC_BIFURCATION_SELECT;
4437
4438 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
1fbc0d78
DV
4439 I915_WRITE(SOUTH_CHICKEN1, temp);
4440 POSTING_READ(SOUTH_CHICKEN1);
4441}
4442
4443static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4444{
4445 struct drm_device *dev = intel_crtc->base.dev;
1fbc0d78
DV
4446
4447 switch (intel_crtc->pipe) {
4448 case PIPE_A:
4449 break;
4450 case PIPE_B:
6e3c9717 4451 if (intel_crtc->config->fdi_lanes > 2)
003632d9 4452 cpt_set_fdi_bc_bifurcation(dev, false);
1fbc0d78 4453 else
003632d9 4454 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4455
4456 break;
4457 case PIPE_C:
003632d9 4458 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4459
4460 break;
4461 default:
4462 BUG();
4463 }
4464}
4465
c48b5305
VS
4466/* Return which DP Port should be selected for Transcoder DP control */
4467static enum port
4cbe4b2b 4468intel_trans_dp_port_sel(struct intel_crtc *crtc)
c48b5305 4469{
4cbe4b2b 4470 struct drm_device *dev = crtc->base.dev;
c48b5305
VS
4471 struct intel_encoder *encoder;
4472
4cbe4b2b 4473 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
cca0502b 4474 if (encoder->type == INTEL_OUTPUT_DP ||
c48b5305
VS
4475 encoder->type == INTEL_OUTPUT_EDP)
4476 return enc_to_dig_port(&encoder->base)->port;
4477 }
4478
4479 return -1;
4480}
4481
f67a559d
JB
4482/*
4483 * Enable PCH resources required for PCH ports:
4484 * - PCH PLLs
4485 * - FDI training & RX/TX
4486 * - update transcoder timings
4487 * - DP transcoding bits
4488 * - transcoder
4489 */
2ce42273 4490static void ironlake_pch_enable(const struct intel_crtc_state *crtc_state)
0e23b99d 4491{
2ce42273 4492 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
4cbe4b2b 4493 struct drm_device *dev = crtc->base.dev;
fac5e23e 4494 struct drm_i915_private *dev_priv = to_i915(dev);
4cbe4b2b 4495 int pipe = crtc->pipe;
f0f59a00 4496 u32 temp;
2c07245f 4497
ab9412ba 4498 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 4499
fd6b8f43 4500 if (IS_IVYBRIDGE(dev_priv))
4cbe4b2b 4501 ivybridge_update_fdi_bc_bifurcation(crtc);
1fbc0d78 4502
cd986abb
DV
4503 /* Write the TU size bits before fdi link training, so that error
4504 * detection works. */
4505 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4506 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4507
c98e9dcf 4508 /* For PCH output, training FDI link */
dc4a1094 4509 dev_priv->display.fdi_link_train(crtc, crtc_state);
2c07245f 4510
3ad8a208
DV
4511 /* We need to program the right clock selection before writing the pixel
4512 * mutliplier into the DPLL. */
6e266956 4513 if (HAS_PCH_CPT(dev_priv)) {
ee7b9f93 4514 u32 sel;
4b645f14 4515
c98e9dcf 4516 temp = I915_READ(PCH_DPLL_SEL);
11887397
DV
4517 temp |= TRANS_DPLL_ENABLE(pipe);
4518 sel = TRANS_DPLLB_SEL(pipe);
2ce42273 4519 if (crtc_state->shared_dpll ==
8106ddbd 4520 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
ee7b9f93
JB
4521 temp |= sel;
4522 else
4523 temp &= ~sel;
c98e9dcf 4524 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 4525 }
5eddb70b 4526
3ad8a208
DV
4527 /* XXX: pch pll's can be enabled any time before we enable the PCH
4528 * transcoder, and we actually should do this to not upset any PCH
4529 * transcoder that already use the clock when we share it.
4530 *
4531 * Note that enable_shared_dpll tries to do the right thing, but
4532 * get_shared_dpll unconditionally resets the pll - we need that to have
4533 * the right LVDS enable sequence. */
4cbe4b2b 4534 intel_enable_shared_dpll(crtc);
3ad8a208 4535
d9b6cb56
JB
4536 /* set transcoder timing, panel must allow it */
4537 assert_panel_unlocked(dev_priv, pipe);
4cbe4b2b 4538 ironlake_pch_transcoder_set_timings(crtc, pipe);
8db9d77b 4539
303b81e0 4540 intel_fdi_normal_train(crtc);
5e84e1a4 4541
c98e9dcf 4542 /* For PCH DP, enable TRANS_DP_CTL */
6e266956 4543 if (HAS_PCH_CPT(dev_priv) &&
2ce42273 4544 intel_crtc_has_dp_encoder(crtc_state)) {
9c4edaee 4545 const struct drm_display_mode *adjusted_mode =
2ce42273 4546 &crtc_state->base.adjusted_mode;
dfd07d72 4547 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
f0f59a00 4548 i915_reg_t reg = TRANS_DP_CTL(pipe);
5eddb70b
CW
4549 temp = I915_READ(reg);
4550 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
4551 TRANS_DP_SYNC_MASK |
4552 TRANS_DP_BPC_MASK);
e3ef4479 4553 temp |= TRANS_DP_OUTPUT_ENABLE;
9325c9f0 4554 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf 4555
9c4edaee 4556 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 4557 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
9c4edaee 4558 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 4559 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf
JB
4560
4561 switch (intel_trans_dp_port_sel(crtc)) {
c48b5305 4562 case PORT_B:
5eddb70b 4563 temp |= TRANS_DP_PORT_SEL_B;
c98e9dcf 4564 break;
c48b5305 4565 case PORT_C:
5eddb70b 4566 temp |= TRANS_DP_PORT_SEL_C;
c98e9dcf 4567 break;
c48b5305 4568 case PORT_D:
5eddb70b 4569 temp |= TRANS_DP_PORT_SEL_D;
c98e9dcf
JB
4570 break;
4571 default:
e95d41e1 4572 BUG();
32f9d658 4573 }
2c07245f 4574
5eddb70b 4575 I915_WRITE(reg, temp);
6be4a607 4576 }
b52eb4dc 4577
b8a4f404 4578 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
4579}
4580
2ce42273 4581static void lpt_pch_enable(const struct intel_crtc_state *crtc_state)
1507e5bd 4582{
2ce42273 4583 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
0dcdc382 4584 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2ce42273 4585 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
1507e5bd 4586
ab9412ba 4587 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
1507e5bd 4588
8c52b5e8 4589 lpt_program_iclkip(crtc);
1507e5bd 4590
0540e488 4591 /* Set transcoder timing. */
0dcdc382 4592 ironlake_pch_transcoder_set_timings(crtc, PIPE_A);
1507e5bd 4593
937bb610 4594 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
4595}
4596
a1520318 4597static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57 4598{
fac5e23e 4599 struct drm_i915_private *dev_priv = to_i915(dev);
f0f59a00 4600 i915_reg_t dslreg = PIPEDSL(pipe);
d4270e57
JB
4601 u32 temp;
4602
4603 temp = I915_READ(dslreg);
4604 udelay(500);
4605 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 4606 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 4607 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
4608 }
4609}
4610
86adf9d7
ML
4611static int
4612skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4613 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4614 int src_w, int src_h, int dst_w, int dst_h)
a1b2278e 4615{
86adf9d7
ML
4616 struct intel_crtc_scaler_state *scaler_state =
4617 &crtc_state->scaler_state;
4618 struct intel_crtc *intel_crtc =
4619 to_intel_crtc(crtc_state->base.crtc);
a1b2278e 4620 int need_scaling;
6156a456 4621
bd2ef25d 4622 need_scaling = drm_rotation_90_or_270(rotation) ?
6156a456
CK
4623 (src_h != dst_w || src_w != dst_h):
4624 (src_w != dst_w || src_h != dst_h);
a1b2278e
CK
4625
4626 /*
4627 * if plane is being disabled or scaler is no more required or force detach
4628 * - free scaler binded to this plane/crtc
4629 * - in order to do this, update crtc->scaler_usage
4630 *
4631 * Here scaler state in crtc_state is set free so that
4632 * scaler can be assigned to other user. Actual register
4633 * update to free the scaler is done in plane/panel-fit programming.
4634 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4635 */
86adf9d7 4636 if (force_detach || !need_scaling) {
a1b2278e 4637 if (*scaler_id >= 0) {
86adf9d7 4638 scaler_state->scaler_users &= ~(1 << scaler_user);
a1b2278e
CK
4639 scaler_state->scalers[*scaler_id].in_use = 0;
4640
86adf9d7
ML
4641 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4642 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4643 intel_crtc->pipe, scaler_user, *scaler_id,
a1b2278e
CK
4644 scaler_state->scaler_users);
4645 *scaler_id = -1;
4646 }
4647 return 0;
4648 }
4649
4650 /* range checks */
4651 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4652 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4653
4654 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4655 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
86adf9d7 4656 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
a1b2278e 4657 "size is out of scaler range\n",
86adf9d7 4658 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
a1b2278e
CK
4659 return -EINVAL;
4660 }
4661
86adf9d7
ML
4662 /* mark this plane as a scaler user in crtc_state */
4663 scaler_state->scaler_users |= (1 << scaler_user);
4664 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4665 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4666 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4667 scaler_state->scaler_users);
4668
4669 return 0;
4670}
4671
4672/**
4673 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4674 *
4675 * @state: crtc's scaler state
86adf9d7
ML
4676 *
4677 * Return
4678 * 0 - scaler_usage updated successfully
4679 * error - requested scaling cannot be supported or other error condition
4680 */
e435d6e5 4681int skl_update_scaler_crtc(struct intel_crtc_state *state)
86adf9d7 4682{
7c5f93b0 4683 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
86adf9d7 4684
e435d6e5 4685 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
c2c446ad 4686 &state->scaler_state.scaler_id, DRM_MODE_ROTATE_0,
86adf9d7 4687 state->pipe_src_w, state->pipe_src_h,
aad941d5 4688 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
86adf9d7
ML
4689}
4690
4691/**
4692 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4693 *
4694 * @state: crtc's scaler state
86adf9d7
ML
4695 * @plane_state: atomic plane state to update
4696 *
4697 * Return
4698 * 0 - scaler_usage updated successfully
4699 * error - requested scaling cannot be supported or other error condition
4700 */
da20eabd
ML
4701static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4702 struct intel_plane_state *plane_state)
86adf9d7
ML
4703{
4704
da20eabd
ML
4705 struct intel_plane *intel_plane =
4706 to_intel_plane(plane_state->base.plane);
86adf9d7
ML
4707 struct drm_framebuffer *fb = plane_state->base.fb;
4708 int ret;
4709
936e71e3 4710 bool force_detach = !fb || !plane_state->base.visible;
86adf9d7 4711
86adf9d7
ML
4712 ret = skl_update_scaler(crtc_state, force_detach,
4713 drm_plane_index(&intel_plane->base),
4714 &plane_state->scaler_id,
4715 plane_state->base.rotation,
936e71e3
VS
4716 drm_rect_width(&plane_state->base.src) >> 16,
4717 drm_rect_height(&plane_state->base.src) >> 16,
4718 drm_rect_width(&plane_state->base.dst),
4719 drm_rect_height(&plane_state->base.dst));
86adf9d7
ML
4720
4721 if (ret || plane_state->scaler_id < 0)
4722 return ret;
4723
a1b2278e 4724 /* check colorkey */
818ed961 4725 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
72660ce0
VS
4726 DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
4727 intel_plane->base.base.id,
4728 intel_plane->base.name);
a1b2278e
CK
4729 return -EINVAL;
4730 }
4731
4732 /* Check src format */
438b74a5 4733 switch (fb->format->format) {
86adf9d7
ML
4734 case DRM_FORMAT_RGB565:
4735 case DRM_FORMAT_XBGR8888:
4736 case DRM_FORMAT_XRGB8888:
4737 case DRM_FORMAT_ABGR8888:
4738 case DRM_FORMAT_ARGB8888:
4739 case DRM_FORMAT_XRGB2101010:
4740 case DRM_FORMAT_XBGR2101010:
4741 case DRM_FORMAT_YUYV:
4742 case DRM_FORMAT_YVYU:
4743 case DRM_FORMAT_UYVY:
4744 case DRM_FORMAT_VYUY:
4745 break;
4746 default:
72660ce0
VS
4747 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
4748 intel_plane->base.base.id, intel_plane->base.name,
438b74a5 4749 fb->base.id, fb->format->format);
86adf9d7 4750 return -EINVAL;
a1b2278e
CK
4751 }
4752
a1b2278e
CK
4753 return 0;
4754}
4755
e435d6e5
ML
4756static void skylake_scaler_disable(struct intel_crtc *crtc)
4757{
4758 int i;
4759
4760 for (i = 0; i < crtc->num_scalers; i++)
4761 skl_detach_scaler(crtc, i);
4762}
4763
4764static void skylake_pfit_enable(struct intel_crtc *crtc)
bd2e244f
JB
4765{
4766 struct drm_device *dev = crtc->base.dev;
fac5e23e 4767 struct drm_i915_private *dev_priv = to_i915(dev);
bd2e244f 4768 int pipe = crtc->pipe;
a1b2278e
CK
4769 struct intel_crtc_scaler_state *scaler_state =
4770 &crtc->config->scaler_state;
4771
6e3c9717 4772 if (crtc->config->pch_pfit.enabled) {
a1b2278e
CK
4773 int id;
4774
c3f8ad57 4775 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0))
a1b2278e 4776 return;
a1b2278e
CK
4777
4778 id = scaler_state->scaler_id;
4779 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4780 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4781 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4782 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
bd2e244f
JB
4783 }
4784}
4785
b074cec8
JB
4786static void ironlake_pfit_enable(struct intel_crtc *crtc)
4787{
4788 struct drm_device *dev = crtc->base.dev;
fac5e23e 4789 struct drm_i915_private *dev_priv = to_i915(dev);
b074cec8
JB
4790 int pipe = crtc->pipe;
4791
6e3c9717 4792 if (crtc->config->pch_pfit.enabled) {
b074cec8
JB
4793 /* Force use of hard-coded filter coefficients
4794 * as some pre-programmed values are broken,
4795 * e.g. x201.
4796 */
fd6b8f43 4797 if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
b074cec8
JB
4798 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4799 PF_PIPE_SEL_IVB(pipe));
4800 else
4801 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
6e3c9717
ACO
4802 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4803 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
d4270e57
JB
4804 }
4805}
4806
20bc8673 4807void hsw_enable_ips(struct intel_crtc *crtc)
d77e4531 4808{
cea165c3 4809 struct drm_device *dev = crtc->base.dev;
fac5e23e 4810 struct drm_i915_private *dev_priv = to_i915(dev);
d77e4531 4811
6e3c9717 4812 if (!crtc->config->ips_enabled)
d77e4531
PZ
4813 return;
4814
307e4498
ML
4815 /*
4816 * We can only enable IPS after we enable a plane and wait for a vblank
4817 * This function is called from post_plane_update, which is run after
4818 * a vblank wait.
4819 */
cea165c3 4820
d77e4531 4821 assert_plane_enabled(dev_priv, crtc->plane);
8652744b 4822 if (IS_BROADWELL(dev_priv)) {
2a114cc1
BW
4823 mutex_lock(&dev_priv->rps.hw_lock);
4824 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4825 mutex_unlock(&dev_priv->rps.hw_lock);
4826 /* Quoting Art Runyan: "its not safe to expect any particular
4827 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
4828 * mailbox." Moreover, the mailbox may return a bogus state,
4829 * so we need to just enable it and continue on.
2a114cc1
BW
4830 */
4831 } else {
4832 I915_WRITE(IPS_CTL, IPS_ENABLE);
4833 /* The bit only becomes 1 in the next vblank, so this wait here
4834 * is essentially intel_wait_for_vblank. If we don't have this
4835 * and don't wait for vblanks until the end of crtc_enable, then
4836 * the HW state readout code will complain that the expected
4837 * IPS_CTL value is not the one we read. */
2ec9ba3c
CW
4838 if (intel_wait_for_register(dev_priv,
4839 IPS_CTL, IPS_ENABLE, IPS_ENABLE,
4840 50))
2a114cc1
BW
4841 DRM_ERROR("Timed out waiting for IPS enable\n");
4842 }
d77e4531
PZ
4843}
4844
20bc8673 4845void hsw_disable_ips(struct intel_crtc *crtc)
d77e4531
PZ
4846{
4847 struct drm_device *dev = crtc->base.dev;
fac5e23e 4848 struct drm_i915_private *dev_priv = to_i915(dev);
d77e4531 4849
6e3c9717 4850 if (!crtc->config->ips_enabled)
d77e4531
PZ
4851 return;
4852
4853 assert_plane_enabled(dev_priv, crtc->plane);
8652744b 4854 if (IS_BROADWELL(dev_priv)) {
2a114cc1
BW
4855 mutex_lock(&dev_priv->rps.hw_lock);
4856 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4857 mutex_unlock(&dev_priv->rps.hw_lock);
23d0b130 4858 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
b85c1ecf
CW
4859 if (intel_wait_for_register(dev_priv,
4860 IPS_CTL, IPS_ENABLE, 0,
4861 42))
23d0b130 4862 DRM_ERROR("Timed out waiting for IPS disable\n");
e59150dc 4863 } else {
2a114cc1 4864 I915_WRITE(IPS_CTL, 0);
e59150dc
JB
4865 POSTING_READ(IPS_CTL);
4866 }
d77e4531
PZ
4867
4868 /* We need to wait for a vblank before we can disable the plane. */
0f0f74bc 4869 intel_wait_for_vblank(dev_priv, crtc->pipe);
d77e4531
PZ
4870}
4871
7cac945f 4872static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
d3eedb1a 4873{
7cac945f 4874 if (intel_crtc->overlay) {
d3eedb1a 4875 struct drm_device *dev = intel_crtc->base.dev;
d3eedb1a
VS
4876
4877 mutex_lock(&dev->struct_mutex);
d3eedb1a 4878 (void) intel_overlay_switch_off(intel_crtc->overlay);
d3eedb1a
VS
4879 mutex_unlock(&dev->struct_mutex);
4880 }
4881
4882 /* Let userspace switch the overlay on again. In most cases userspace
4883 * has to recompute where to put it anyway.
4884 */
4885}
4886
87d4300a
ML
4887/**
4888 * intel_post_enable_primary - Perform operations after enabling primary plane
4889 * @crtc: the CRTC whose primary plane was just enabled
4890 *
4891 * Performs potentially sleeping operations that must be done after the primary
4892 * plane is enabled, such as updating FBC and IPS. Note that this may be
4893 * called due to an explicit primary plane update, or due to an implicit
4894 * re-enable that is caused when a sprite plane is updated to no longer
4895 * completely hide the primary plane.
4896 */
4897static void
4898intel_post_enable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4899{
4900 struct drm_device *dev = crtc->dev;
fac5e23e 4901 struct drm_i915_private *dev_priv = to_i915(dev);
a5c4d7bc
VS
4902 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4903 int pipe = intel_crtc->pipe;
a5c4d7bc 4904
87d4300a
ML
4905 /*
4906 * FIXME IPS should be fine as long as one plane is
4907 * enabled, but in practice it seems to have problems
4908 * when going from primary only to sprite only and vice
4909 * versa.
4910 */
a5c4d7bc
VS
4911 hsw_enable_ips(intel_crtc);
4912
f99d7069 4913 /*
87d4300a
ML
4914 * Gen2 reports pipe underruns whenever all planes are disabled.
4915 * So don't enable underrun reporting before at least some planes
4916 * are enabled.
4917 * FIXME: Need to fix the logic to work when we turn off all planes
4918 * but leave the pipe running.
f99d7069 4919 */
5db94019 4920 if (IS_GEN2(dev_priv))
87d4300a
ML
4921 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4922
aca7b684
VS
4923 /* Underruns don't always raise interrupts, so check manually. */
4924 intel_check_cpu_fifo_underruns(dev_priv);
4925 intel_check_pch_fifo_underruns(dev_priv);
a5c4d7bc
VS
4926}
4927
2622a081 4928/* FIXME move all this to pre_plane_update() with proper state tracking */
87d4300a
ML
4929static void
4930intel_pre_disable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4931{
4932 struct drm_device *dev = crtc->dev;
fac5e23e 4933 struct drm_i915_private *dev_priv = to_i915(dev);
a5c4d7bc
VS
4934 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4935 int pipe = intel_crtc->pipe;
a5c4d7bc 4936
87d4300a
ML
4937 /*
4938 * Gen2 reports pipe underruns whenever all planes are disabled.
4939 * So diasble underrun reporting before all the planes get disabled.
4940 * FIXME: Need to fix the logic to work when we turn off all planes
4941 * but leave the pipe running.
4942 */
5db94019 4943 if (IS_GEN2(dev_priv))
87d4300a 4944 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
a5c4d7bc 4945
2622a081
VS
4946 /*
4947 * FIXME IPS should be fine as long as one plane is
4948 * enabled, but in practice it seems to have problems
4949 * when going from primary only to sprite only and vice
4950 * versa.
4951 */
4952 hsw_disable_ips(intel_crtc);
4953}
4954
4955/* FIXME get rid of this and use pre_plane_update */
4956static void
4957intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
4958{
4959 struct drm_device *dev = crtc->dev;
fac5e23e 4960 struct drm_i915_private *dev_priv = to_i915(dev);
2622a081
VS
4961 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4962 int pipe = intel_crtc->pipe;
4963
4964 intel_pre_disable_primary(crtc);
4965
87d4300a
ML
4966 /*
4967 * Vblank time updates from the shadow to live plane control register
4968 * are blocked if the memory self-refresh mode is active at that
4969 * moment. So to make sure the plane gets truly disabled, disable
4970 * first the self-refresh mode. The self-refresh enable bit in turn
4971 * will be checked/applied by the HW only at the next frame start
4972 * event which is after the vblank start event, so we need to have a
4973 * wait-for-vblank between disabling the plane and the pipe.
4974 */
11a85d6a
VS
4975 if (HAS_GMCH_DISPLAY(dev_priv) &&
4976 intel_set_memory_cxsr(dev_priv, false))
0f0f74bc 4977 intel_wait_for_vblank(dev_priv, pipe);
87d4300a
ML
4978}
4979
5a21b665
DV
4980static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
4981{
4982 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
4983 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4984 struct intel_crtc_state *pipe_config =
4985 to_intel_crtc_state(crtc->base.state);
5a21b665
DV
4986 struct drm_plane *primary = crtc->base.primary;
4987 struct drm_plane_state *old_pri_state =
4988 drm_atomic_get_existing_plane_state(old_state, primary);
4989
5748b6a1 4990 intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
5a21b665 4991
5a21b665 4992 if (pipe_config->update_wm_post && pipe_config->base.active)
432081bc 4993 intel_update_watermarks(crtc);
5a21b665
DV
4994
4995 if (old_pri_state) {
4996 struct intel_plane_state *primary_state =
4997 to_intel_plane_state(primary->state);
4998 struct intel_plane_state *old_primary_state =
4999 to_intel_plane_state(old_pri_state);
5000
5001 intel_fbc_post_update(crtc);
5002
936e71e3 5003 if (primary_state->base.visible &&
5a21b665 5004 (needs_modeset(&pipe_config->base) ||
936e71e3 5005 !old_primary_state->base.visible))
5a21b665
DV
5006 intel_post_enable_primary(&crtc->base);
5007 }
5008}
5009
aa5e9b47
ML
5010static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state,
5011 struct intel_crtc_state *pipe_config)
ac21b225 5012{
5c74cd73 5013 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
ac21b225 5014 struct drm_device *dev = crtc->base.dev;
fac5e23e 5015 struct drm_i915_private *dev_priv = to_i915(dev);
5c74cd73
ML
5016 struct drm_atomic_state *old_state = old_crtc_state->base.state;
5017 struct drm_plane *primary = crtc->base.primary;
5018 struct drm_plane_state *old_pri_state =
5019 drm_atomic_get_existing_plane_state(old_state, primary);
5020 bool modeset = needs_modeset(&pipe_config->base);
ccf010fb
ML
5021 struct intel_atomic_state *old_intel_state =
5022 to_intel_atomic_state(old_state);
ac21b225 5023
5c74cd73
ML
5024 if (old_pri_state) {
5025 struct intel_plane_state *primary_state =
5026 to_intel_plane_state(primary->state);
5027 struct intel_plane_state *old_primary_state =
5028 to_intel_plane_state(old_pri_state);
5029
faf68d92 5030 intel_fbc_pre_update(crtc, pipe_config, primary_state);
31ae71fc 5031
936e71e3
VS
5032 if (old_primary_state->base.visible &&
5033 (modeset || !primary_state->base.visible))
5c74cd73
ML
5034 intel_pre_disable_primary(&crtc->base);
5035 }
852eb00d 5036
5eeb798b
VS
5037 /*
5038 * Vblank time updates from the shadow to live plane control register
5039 * are blocked if the memory self-refresh mode is active at that
5040 * moment. So to make sure the plane gets truly disabled, disable
5041 * first the self-refresh mode. The self-refresh enable bit in turn
5042 * will be checked/applied by the HW only at the next frame start
5043 * event which is after the vblank start event, so we need to have a
5044 * wait-for-vblank between disabling the plane and the pipe.
5045 */
5046 if (HAS_GMCH_DISPLAY(dev_priv) && old_crtc_state->base.active &&
5047 pipe_config->disable_cxsr && intel_set_memory_cxsr(dev_priv, false))
5048 intel_wait_for_vblank(dev_priv, crtc->pipe);
92826fcd 5049
ed4a6a7c
MR
5050 /*
5051 * IVB workaround: must disable low power watermarks for at least
5052 * one frame before enabling scaling. LP watermarks can be re-enabled
5053 * when scaling is disabled.
5054 *
5055 * WaCxSRDisabledForSpriteScaling:ivb
5056 */
ddd2b792 5057 if (pipe_config->disable_lp_wm && ilk_disable_lp_wm(dev))
0f0f74bc 5058 intel_wait_for_vblank(dev_priv, crtc->pipe);
ed4a6a7c
MR
5059
5060 /*
5061 * If we're doing a modeset, we're done. No need to do any pre-vblank
5062 * watermark programming here.
5063 */
5064 if (needs_modeset(&pipe_config->base))
5065 return;
5066
5067 /*
5068 * For platforms that support atomic watermarks, program the
5069 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
5070 * will be the intermediate values that are safe for both pre- and
5071 * post- vblank; when vblank happens, the 'active' values will be set
5072 * to the final 'target' values and we'll do this again to get the
5073 * optimal watermarks. For gen9+ platforms, the values we program here
5074 * will be the final target values which will get automatically latched
5075 * at vblank time; no further programming will be necessary.
5076 *
5077 * If a platform hasn't been transitioned to atomic watermarks yet,
5078 * we'll continue to update watermarks the old way, if flags tell
5079 * us to.
5080 */
5081 if (dev_priv->display.initial_watermarks != NULL)
ccf010fb
ML
5082 dev_priv->display.initial_watermarks(old_intel_state,
5083 pipe_config);
caed361d 5084 else if (pipe_config->update_wm_pre)
432081bc 5085 intel_update_watermarks(crtc);
ac21b225
ML
5086}
5087
d032ffa0 5088static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
87d4300a
ML
5089{
5090 struct drm_device *dev = crtc->dev;
5091 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
d032ffa0 5092 struct drm_plane *p;
87d4300a
ML
5093 int pipe = intel_crtc->pipe;
5094
7cac945f 5095 intel_crtc_dpms_overlay_disable(intel_crtc);
27321ae8 5096
d032ffa0 5097 drm_for_each_plane_mask(p, dev, plane_mask)
282dbf9b 5098 to_intel_plane(p)->disable_plane(to_intel_plane(p), intel_crtc);
f98551ae 5099
f99d7069
DV
5100 /*
5101 * FIXME: Once we grow proper nuclear flip support out of this we need
5102 * to compute the mask of flip planes precisely. For the time being
5103 * consider this a flip to a NULL plane.
5104 */
5748b6a1 5105 intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
5106}
5107
fb1c98b1 5108static void intel_encoders_pre_pll_enable(struct drm_crtc *crtc,
fd6bbda9 5109 struct intel_crtc_state *crtc_state,
fb1c98b1
ML
5110 struct drm_atomic_state *old_state)
5111{
aa5e9b47 5112 struct drm_connector_state *conn_state;
fb1c98b1
ML
5113 struct drm_connector *conn;
5114 int i;
5115
aa5e9b47 5116 for_each_new_connector_in_state(old_state, conn, conn_state, i) {
fb1c98b1
ML
5117 struct intel_encoder *encoder =
5118 to_intel_encoder(conn_state->best_encoder);
5119
5120 if (conn_state->crtc != crtc)
5121 continue;
5122
5123 if (encoder->pre_pll_enable)
fd6bbda9 5124 encoder->pre_pll_enable(encoder, crtc_state, conn_state);
fb1c98b1
ML
5125 }
5126}
5127
5128static void intel_encoders_pre_enable(struct drm_crtc *crtc,
fd6bbda9 5129 struct intel_crtc_state *crtc_state,
fb1c98b1
ML
5130 struct drm_atomic_state *old_state)
5131{
aa5e9b47 5132 struct drm_connector_state *conn_state;
fb1c98b1
ML
5133 struct drm_connector *conn;
5134 int i;
5135
aa5e9b47 5136 for_each_new_connector_in_state(old_state, conn, conn_state, i) {
fb1c98b1
ML
5137 struct intel_encoder *encoder =
5138 to_intel_encoder(conn_state->best_encoder);
5139
5140 if (conn_state->crtc != crtc)
5141 continue;
5142
5143 if (encoder->pre_enable)
fd6bbda9 5144 encoder->pre_enable(encoder, crtc_state, conn_state);
fb1c98b1
ML
5145 }
5146}
5147
5148static void intel_encoders_enable(struct drm_crtc *crtc,
fd6bbda9 5149 struct intel_crtc_state *crtc_state,
fb1c98b1
ML
5150 struct drm_atomic_state *old_state)
5151{
aa5e9b47 5152 struct drm_connector_state *conn_state;
fb1c98b1
ML
5153 struct drm_connector *conn;
5154 int i;
5155
aa5e9b47 5156 for_each_new_connector_in_state(old_state, conn, conn_state, i) {
fb1c98b1
ML
5157 struct intel_encoder *encoder =
5158 to_intel_encoder(conn_state->best_encoder);
5159
5160 if (conn_state->crtc != crtc)
5161 continue;
5162
fd6bbda9 5163 encoder->enable(encoder, crtc_state, conn_state);
fb1c98b1
ML
5164 intel_opregion_notify_encoder(encoder, true);
5165 }
5166}
5167
5168static void intel_encoders_disable(struct drm_crtc *crtc,
fd6bbda9 5169 struct intel_crtc_state *old_crtc_state,
fb1c98b1
ML
5170 struct drm_atomic_state *old_state)
5171{
5172 struct drm_connector_state *old_conn_state;
5173 struct drm_connector *conn;
5174 int i;
5175
aa5e9b47 5176 for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
fb1c98b1
ML
5177 struct intel_encoder *encoder =
5178 to_intel_encoder(old_conn_state->best_encoder);
5179
5180 if (old_conn_state->crtc != crtc)
5181 continue;
5182
5183 intel_opregion_notify_encoder(encoder, false);
fd6bbda9 5184 encoder->disable(encoder, old_crtc_state, old_conn_state);
fb1c98b1
ML
5185 }
5186}
5187
5188static void intel_encoders_post_disable(struct drm_crtc *crtc,
fd6bbda9 5189 struct intel_crtc_state *old_crtc_state,
fb1c98b1
ML
5190 struct drm_atomic_state *old_state)
5191{
5192 struct drm_connector_state *old_conn_state;
5193 struct drm_connector *conn;
5194 int i;
5195
aa5e9b47 5196 for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
fb1c98b1
ML
5197 struct intel_encoder *encoder =
5198 to_intel_encoder(old_conn_state->best_encoder);
5199
5200 if (old_conn_state->crtc != crtc)
5201 continue;
5202
5203 if (encoder->post_disable)
fd6bbda9 5204 encoder->post_disable(encoder, old_crtc_state, old_conn_state);
fb1c98b1
ML
5205 }
5206}
5207
5208static void intel_encoders_post_pll_disable(struct drm_crtc *crtc,
fd6bbda9 5209 struct intel_crtc_state *old_crtc_state,
fb1c98b1
ML
5210 struct drm_atomic_state *old_state)
5211{
5212 struct drm_connector_state *old_conn_state;
5213 struct drm_connector *conn;
5214 int i;
5215
aa5e9b47 5216 for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
fb1c98b1
ML
5217 struct intel_encoder *encoder =
5218 to_intel_encoder(old_conn_state->best_encoder);
5219
5220 if (old_conn_state->crtc != crtc)
5221 continue;
5222
5223 if (encoder->post_pll_disable)
fd6bbda9 5224 encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
fb1c98b1
ML
5225 }
5226}
5227
4a806558
ML
5228static void ironlake_crtc_enable(struct intel_crtc_state *pipe_config,
5229 struct drm_atomic_state *old_state)
f67a559d 5230{
4a806558 5231 struct drm_crtc *crtc = pipe_config->base.crtc;
f67a559d 5232 struct drm_device *dev = crtc->dev;
fac5e23e 5233 struct drm_i915_private *dev_priv = to_i915(dev);
f67a559d
JB
5234 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5235 int pipe = intel_crtc->pipe;
ccf010fb
ML
5236 struct intel_atomic_state *old_intel_state =
5237 to_intel_atomic_state(old_state);
f67a559d 5238
53d9f4e9 5239 if (WARN_ON(intel_crtc->active))
f67a559d
JB
5240 return;
5241
b2c0593a
VS
5242 /*
5243 * Sometimes spurious CPU pipe underruns happen during FDI
5244 * training, at least with VGA+HDMI cloning. Suppress them.
5245 *
5246 * On ILK we get an occasional spurious CPU pipe underruns
5247 * between eDP port A enable and vdd enable. Also PCH port
5248 * enable seems to result in the occasional CPU pipe underrun.
5249 *
5250 * Spurious PCH underruns also occur during PCH enabling.
5251 */
5252 if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
5253 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
81b088ca
VS
5254 if (intel_crtc->config->has_pch_encoder)
5255 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
5256
6e3c9717 5257 if (intel_crtc->config->has_pch_encoder)
b14b1055
DV
5258 intel_prepare_shared_dpll(intel_crtc);
5259
37a5650b 5260 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 5261 intel_dp_set_m_n(intel_crtc, M1_N1);
29407aab
DV
5262
5263 intel_set_pipe_timings(intel_crtc);
bc58be60 5264 intel_set_pipe_src_size(intel_crtc);
29407aab 5265
6e3c9717 5266 if (intel_crtc->config->has_pch_encoder) {
29407aab 5267 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 5268 &intel_crtc->config->fdi_m_n, NULL);
29407aab
DV
5269 }
5270
5271 ironlake_set_pipeconf(crtc);
5272
f67a559d 5273 intel_crtc->active = true;
8664281b 5274
fd6bbda9 5275 intel_encoders_pre_enable(crtc, pipe_config, old_state);
f67a559d 5276
6e3c9717 5277 if (intel_crtc->config->has_pch_encoder) {
fff367c7
DV
5278 /* Note: FDI PLL enabling _must_ be done before we enable the
5279 * cpu pipes, hence this is separate from all the other fdi/pch
5280 * enabling. */
88cefb6c 5281 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
DV
5282 } else {
5283 assert_fdi_tx_disabled(dev_priv, pipe);
5284 assert_fdi_rx_disabled(dev_priv, pipe);
5285 }
f67a559d 5286
b074cec8 5287 ironlake_pfit_enable(intel_crtc);
f67a559d 5288
9c54c0dd
JB
5289 /*
5290 * On ILK+ LUT must be loaded before the pipe is running but with
5291 * clocks enabled
5292 */
b95c5321 5293 intel_color_load_luts(&pipe_config->base);
9c54c0dd 5294
1d5bf5d9 5295 if (dev_priv->display.initial_watermarks != NULL)
ccf010fb 5296 dev_priv->display.initial_watermarks(old_intel_state, intel_crtc->config);
e1fdc473 5297 intel_enable_pipe(intel_crtc);
f67a559d 5298
6e3c9717 5299 if (intel_crtc->config->has_pch_encoder)
2ce42273 5300 ironlake_pch_enable(pipe_config);
c98e9dcf 5301
f9b61ff6
DV
5302 assert_vblank_disabled(crtc);
5303 drm_crtc_vblank_on(crtc);
5304
fd6bbda9 5305 intel_encoders_enable(crtc, pipe_config, old_state);
61b77ddd 5306
6e266956 5307 if (HAS_PCH_CPT(dev_priv))
a1520318 5308 cpt_verify_modeset(dev, intel_crtc->pipe);
37ca8d4c
VS
5309
5310 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
5311 if (intel_crtc->config->has_pch_encoder)
0f0f74bc 5312 intel_wait_for_vblank(dev_priv, pipe);
b2c0593a 5313 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
37ca8d4c 5314 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607
JB
5315}
5316
42db64ef
PZ
5317/* IPS only exists on ULT machines and is tied to pipe A. */
5318static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
5319{
50a0bc90 5320 return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
42db64ef
PZ
5321}
5322
4a806558
ML
5323static void haswell_crtc_enable(struct intel_crtc_state *pipe_config,
5324 struct drm_atomic_state *old_state)
4f771f10 5325{
4a806558 5326 struct drm_crtc *crtc = pipe_config->base.crtc;
6315b5d3 5327 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
4f771f10 5328 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
99d736a2 5329 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
4d1de975 5330 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ccf010fb
ML
5331 struct intel_atomic_state *old_intel_state =
5332 to_intel_atomic_state(old_state);
4f771f10 5333
53d9f4e9 5334 if (WARN_ON(intel_crtc->active))
4f771f10
PZ
5335 return;
5336
81b088ca
VS
5337 if (intel_crtc->config->has_pch_encoder)
5338 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5339 false);
5340
fd6bbda9 5341 intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
95a7a2ae 5342
8106ddbd 5343 if (intel_crtc->config->shared_dpll)
df8ad70c
DV
5344 intel_enable_shared_dpll(intel_crtc);
5345
37a5650b 5346 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 5347 intel_dp_set_m_n(intel_crtc, M1_N1);
229fca97 5348
d7edc4e5 5349 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975
JN
5350 intel_set_pipe_timings(intel_crtc);
5351
bc58be60 5352 intel_set_pipe_src_size(intel_crtc);
229fca97 5353
4d1de975
JN
5354 if (cpu_transcoder != TRANSCODER_EDP &&
5355 !transcoder_is_dsi(cpu_transcoder)) {
5356 I915_WRITE(PIPE_MULT(cpu_transcoder),
6e3c9717 5357 intel_crtc->config->pixel_multiplier - 1);
ebb69c95
CT
5358 }
5359
6e3c9717 5360 if (intel_crtc->config->has_pch_encoder) {
229fca97 5361 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 5362 &intel_crtc->config->fdi_m_n, NULL);
229fca97
DV
5363 }
5364
d7edc4e5 5365 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975
JN
5366 haswell_set_pipeconf(crtc);
5367
391bf048 5368 haswell_set_pipemisc(crtc);
229fca97 5369
b95c5321 5370 intel_color_set_csc(&pipe_config->base);
229fca97 5371
4f771f10 5372 intel_crtc->active = true;
8664281b 5373
6b698516
DV
5374 if (intel_crtc->config->has_pch_encoder)
5375 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
5376 else
5377 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5378
fd6bbda9 5379 intel_encoders_pre_enable(crtc, pipe_config, old_state);
4f771f10 5380
d2d65408 5381 if (intel_crtc->config->has_pch_encoder)
dc4a1094 5382 dev_priv->display.fdi_link_train(intel_crtc, pipe_config);
4fe9467d 5383
d7edc4e5 5384 if (!transcoder_is_dsi(cpu_transcoder))
3dc38eea 5385 intel_ddi_enable_pipe_clock(pipe_config);
4f771f10 5386
6315b5d3 5387 if (INTEL_GEN(dev_priv) >= 9)
e435d6e5 5388 skylake_pfit_enable(intel_crtc);
ff6d9f55 5389 else
1c132b44 5390 ironlake_pfit_enable(intel_crtc);
4f771f10
PZ
5391
5392 /*
5393 * On ILK+ LUT must be loaded before the pipe is running but with
5394 * clocks enabled
5395 */
b95c5321 5396 intel_color_load_luts(&pipe_config->base);
4f771f10 5397
3dc38eea 5398 intel_ddi_set_pipe_settings(pipe_config);
d7edc4e5 5399 if (!transcoder_is_dsi(cpu_transcoder))
3dc38eea 5400 intel_ddi_enable_transcoder_func(pipe_config);
4f771f10 5401
1d5bf5d9 5402 if (dev_priv->display.initial_watermarks != NULL)
3125d39f 5403 dev_priv->display.initial_watermarks(old_intel_state, pipe_config);
4d1de975
JN
5404
5405 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
d7edc4e5 5406 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975 5407 intel_enable_pipe(intel_crtc);
42db64ef 5408
6e3c9717 5409 if (intel_crtc->config->has_pch_encoder)
2ce42273 5410 lpt_pch_enable(pipe_config);
4f771f10 5411
0037071d 5412 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
3dc38eea 5413 intel_ddi_set_vc_payload_alloc(pipe_config, true);
0e32b39c 5414
f9b61ff6
DV
5415 assert_vblank_disabled(crtc);
5416 drm_crtc_vblank_on(crtc);
5417
fd6bbda9 5418 intel_encoders_enable(crtc, pipe_config, old_state);
4f771f10 5419
6b698516 5420 if (intel_crtc->config->has_pch_encoder) {
0f0f74bc
VS
5421 intel_wait_for_vblank(dev_priv, pipe);
5422 intel_wait_for_vblank(dev_priv, pipe);
6b698516 5423 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
d2d65408
VS
5424 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5425 true);
6b698516 5426 }
d2d65408 5427
e4916946
PZ
5428 /* If we change the relative order between pipe/planes enabling, we need
5429 * to change the workaround. */
99d736a2 5430 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
772c2a51 5431 if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
0f0f74bc
VS
5432 intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
5433 intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
99d736a2 5434 }
4f771f10
PZ
5435}
5436
bfd16b2a 5437static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
3f8dce3a
DV
5438{
5439 struct drm_device *dev = crtc->base.dev;
fac5e23e 5440 struct drm_i915_private *dev_priv = to_i915(dev);
3f8dce3a
DV
5441 int pipe = crtc->pipe;
5442
5443 /* To avoid upsetting the power well on haswell only disable the pfit if
5444 * it's in use. The hw state code will make sure we get this right. */
bfd16b2a 5445 if (force || crtc->config->pch_pfit.enabled) {
3f8dce3a
DV
5446 I915_WRITE(PF_CTL(pipe), 0);
5447 I915_WRITE(PF_WIN_POS(pipe), 0);
5448 I915_WRITE(PF_WIN_SZ(pipe), 0);
5449 }
5450}
5451
4a806558
ML
5452static void ironlake_crtc_disable(struct intel_crtc_state *old_crtc_state,
5453 struct drm_atomic_state *old_state)
6be4a607 5454{
4a806558 5455 struct drm_crtc *crtc = old_crtc_state->base.crtc;
6be4a607 5456 struct drm_device *dev = crtc->dev;
fac5e23e 5457 struct drm_i915_private *dev_priv = to_i915(dev);
6be4a607
JB
5458 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5459 int pipe = intel_crtc->pipe;
b52eb4dc 5460
b2c0593a
VS
5461 /*
5462 * Sometimes spurious CPU pipe underruns happen when the
5463 * pipe is already disabled, but FDI RX/TX is still enabled.
5464 * Happens at least with VGA+HDMI cloning. Suppress them.
5465 */
5466 if (intel_crtc->config->has_pch_encoder) {
5467 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
37ca8d4c 5468 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
b2c0593a 5469 }
37ca8d4c 5470
fd6bbda9 5471 intel_encoders_disable(crtc, old_crtc_state, old_state);
ea9d758d 5472
f9b61ff6
DV
5473 drm_crtc_vblank_off(crtc);
5474 assert_vblank_disabled(crtc);
5475
575f7ab7 5476 intel_disable_pipe(intel_crtc);
32f9d658 5477
bfd16b2a 5478 ironlake_pfit_disable(intel_crtc, false);
2c07245f 5479
b2c0593a 5480 if (intel_crtc->config->has_pch_encoder)
5a74f70a
VS
5481 ironlake_fdi_disable(crtc);
5482
fd6bbda9 5483 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
2c07245f 5484
6e3c9717 5485 if (intel_crtc->config->has_pch_encoder) {
d925c59a 5486 ironlake_disable_pch_transcoder(dev_priv, pipe);
6be4a607 5487
6e266956 5488 if (HAS_PCH_CPT(dev_priv)) {
f0f59a00
VS
5489 i915_reg_t reg;
5490 u32 temp;
5491
d925c59a
DV
5492 /* disable TRANS_DP_CTL */
5493 reg = TRANS_DP_CTL(pipe);
5494 temp = I915_READ(reg);
5495 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5496 TRANS_DP_PORT_SEL_MASK);
5497 temp |= TRANS_DP_PORT_SEL_NONE;
5498 I915_WRITE(reg, temp);
5499
5500 /* disable DPLL_SEL */
5501 temp = I915_READ(PCH_DPLL_SEL);
11887397 5502 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 5503 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 5504 }
e3421a18 5505
d925c59a
DV
5506 ironlake_fdi_pll_disable(intel_crtc);
5507 }
81b088ca 5508
b2c0593a 5509 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
81b088ca 5510 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607 5511}
1b3c7a47 5512
4a806558
ML
5513static void haswell_crtc_disable(struct intel_crtc_state *old_crtc_state,
5514 struct drm_atomic_state *old_state)
ee7b9f93 5515{
4a806558 5516 struct drm_crtc *crtc = old_crtc_state->base.crtc;
6315b5d3 5517 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
ee7b9f93 5518 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 5519 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee7b9f93 5520
d2d65408
VS
5521 if (intel_crtc->config->has_pch_encoder)
5522 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5523 false);
5524
fd6bbda9 5525 intel_encoders_disable(crtc, old_crtc_state, old_state);
4f771f10 5526
f9b61ff6
DV
5527 drm_crtc_vblank_off(crtc);
5528 assert_vblank_disabled(crtc);
5529
4d1de975 5530 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
d7edc4e5 5531 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975 5532 intel_disable_pipe(intel_crtc);
4f771f10 5533
0037071d 5534 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
3dc38eea 5535 intel_ddi_set_vc_payload_alloc(intel_crtc->config, false);
a4bf214f 5536
d7edc4e5 5537 if (!transcoder_is_dsi(cpu_transcoder))
7d4aefd0 5538 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4f771f10 5539
6315b5d3 5540 if (INTEL_GEN(dev_priv) >= 9)
e435d6e5 5541 skylake_scaler_disable(intel_crtc);
ff6d9f55 5542 else
bfd16b2a 5543 ironlake_pfit_disable(intel_crtc, false);
4f771f10 5544
d7edc4e5 5545 if (!transcoder_is_dsi(cpu_transcoder))
3dc38eea 5546 intel_ddi_disable_pipe_clock(intel_crtc->config);
4f771f10 5547
fd6bbda9 5548 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
81b088ca 5549
b7076546 5550 if (old_crtc_state->has_pch_encoder)
81b088ca
VS
5551 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5552 true);
4f771f10
PZ
5553}
5554
2dd24552
JB
5555static void i9xx_pfit_enable(struct intel_crtc *crtc)
5556{
5557 struct drm_device *dev = crtc->base.dev;
fac5e23e 5558 struct drm_i915_private *dev_priv = to_i915(dev);
6e3c9717 5559 struct intel_crtc_state *pipe_config = crtc->config;
2dd24552 5560
681a8504 5561 if (!pipe_config->gmch_pfit.control)
2dd24552
JB
5562 return;
5563
2dd24552 5564 /*
c0b03411
DV
5565 * The panel fitter should only be adjusted whilst the pipe is disabled,
5566 * according to register description and PRM.
2dd24552 5567 */
c0b03411
DV
5568 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5569 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 5570
b074cec8
JB
5571 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5572 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
DV
5573
5574 /* Border color in case we don't scale up to the full screen. Black by
5575 * default, change to something else for debugging. */
5576 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
5577}
5578
79f255a0 5579enum intel_display_power_domain intel_port_to_power_domain(enum port port)
d05410f9
DA
5580{
5581 switch (port) {
5582 case PORT_A:
6331a704 5583 return POWER_DOMAIN_PORT_DDI_A_LANES;
d05410f9 5584 case PORT_B:
6331a704 5585 return POWER_DOMAIN_PORT_DDI_B_LANES;
d05410f9 5586 case PORT_C:
6331a704 5587 return POWER_DOMAIN_PORT_DDI_C_LANES;
d05410f9 5588 case PORT_D:
6331a704 5589 return POWER_DOMAIN_PORT_DDI_D_LANES;
d8e19f99 5590 case PORT_E:
6331a704 5591 return POWER_DOMAIN_PORT_DDI_E_LANES;
d05410f9 5592 default:
b9fec167 5593 MISSING_CASE(port);
d05410f9
DA
5594 return POWER_DOMAIN_PORT_OTHER;
5595 }
5596}
5597
d8fc70b7
ACO
5598static u64 get_crtc_power_domains(struct drm_crtc *crtc,
5599 struct intel_crtc_state *crtc_state)
77d22dca 5600{
319be8ae 5601 struct drm_device *dev = crtc->dev;
37255d8d 5602 struct drm_i915_private *dev_priv = to_i915(dev);
74bff5f9 5603 struct drm_encoder *encoder;
319be8ae
ID
5604 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5605 enum pipe pipe = intel_crtc->pipe;
d8fc70b7 5606 u64 mask;
74bff5f9 5607 enum transcoder transcoder = crtc_state->cpu_transcoder;
77d22dca 5608
74bff5f9 5609 if (!crtc_state->base.active)
292b990e
ML
5610 return 0;
5611
77d22dca
ID
5612 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5613 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
74bff5f9
ML
5614 if (crtc_state->pch_pfit.enabled ||
5615 crtc_state->pch_pfit.force_thru)
d8fc70b7 5616 mask |= BIT_ULL(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
77d22dca 5617
74bff5f9
ML
5618 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5619 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5620
79f255a0 5621 mask |= BIT_ULL(intel_encoder->power_domain);
74bff5f9 5622 }
319be8ae 5623
37255d8d
ML
5624 if (HAS_DDI(dev_priv) && crtc_state->has_audio)
5625 mask |= BIT(POWER_DOMAIN_AUDIO);
5626
15e7ec29 5627 if (crtc_state->shared_dpll)
d8fc70b7 5628 mask |= BIT_ULL(POWER_DOMAIN_PLLS);
15e7ec29 5629
77d22dca
ID
5630 return mask;
5631}
5632
d2d15016 5633static u64
74bff5f9
ML
5634modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5635 struct intel_crtc_state *crtc_state)
77d22dca 5636{
fac5e23e 5637 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
292b990e
ML
5638 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5639 enum intel_display_power_domain domain;
d8fc70b7 5640 u64 domains, new_domains, old_domains;
77d22dca 5641
292b990e 5642 old_domains = intel_crtc->enabled_power_domains;
74bff5f9
ML
5643 intel_crtc->enabled_power_domains = new_domains =
5644 get_crtc_power_domains(crtc, crtc_state);
77d22dca 5645
5a21b665 5646 domains = new_domains & ~old_domains;
292b990e
ML
5647
5648 for_each_power_domain(domain, domains)
5649 intel_display_power_get(dev_priv, domain);
5650
5a21b665 5651 return old_domains & ~new_domains;
292b990e
ML
5652}
5653
5654static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
d8fc70b7 5655 u64 domains)
292b990e
ML
5656{
5657 enum intel_display_power_domain domain;
5658
5659 for_each_power_domain(domain, domains)
5660 intel_display_power_put(dev_priv, domain);
5661}
77d22dca 5662
7ff89ca2
VS
5663static void valleyview_crtc_enable(struct intel_crtc_state *pipe_config,
5664 struct drm_atomic_state *old_state)
adafdc6f 5665{
ff32c54e
VS
5666 struct intel_atomic_state *old_intel_state =
5667 to_intel_atomic_state(old_state);
7ff89ca2
VS
5668 struct drm_crtc *crtc = pipe_config->base.crtc;
5669 struct drm_device *dev = crtc->dev;
5670 struct drm_i915_private *dev_priv = to_i915(dev);
5671 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5672 int pipe = intel_crtc->pipe;
adafdc6f 5673
7ff89ca2
VS
5674 if (WARN_ON(intel_crtc->active))
5675 return;
adafdc6f 5676
7ff89ca2
VS
5677 if (intel_crtc_has_dp_encoder(intel_crtc->config))
5678 intel_dp_set_m_n(intel_crtc, M1_N1);
b2045352 5679
7ff89ca2
VS
5680 intel_set_pipe_timings(intel_crtc);
5681 intel_set_pipe_src_size(intel_crtc);
b2045352 5682
7ff89ca2
VS
5683 if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
5684 struct drm_i915_private *dev_priv = to_i915(dev);
560a7ae4 5685
7ff89ca2
VS
5686 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
5687 I915_WRITE(CHV_CANVAS(pipe), 0);
560a7ae4
DL
5688 }
5689
7ff89ca2 5690 i9xx_set_pipeconf(intel_crtc);
560a7ae4 5691
7ff89ca2 5692 intel_crtc->active = true;
92891e45 5693
7ff89ca2 5694 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5f199dfa 5695
7ff89ca2 5696 intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
5f199dfa 5697
7ff89ca2
VS
5698 if (IS_CHERRYVIEW(dev_priv)) {
5699 chv_prepare_pll(intel_crtc, intel_crtc->config);
5700 chv_enable_pll(intel_crtc, intel_crtc->config);
5701 } else {
5702 vlv_prepare_pll(intel_crtc, intel_crtc->config);
5703 vlv_enable_pll(intel_crtc, intel_crtc->config);
5f199dfa
VS
5704 }
5705
7ff89ca2 5706 intel_encoders_pre_enable(crtc, pipe_config, old_state);
5f199dfa 5707
7ff89ca2 5708 i9xx_pfit_enable(intel_crtc);
89b3c3c7 5709
7ff89ca2 5710 intel_color_load_luts(&pipe_config->base);
89b3c3c7 5711
ff32c54e
VS
5712 dev_priv->display.initial_watermarks(old_intel_state,
5713 pipe_config);
7ff89ca2
VS
5714 intel_enable_pipe(intel_crtc);
5715
5716 assert_vblank_disabled(crtc);
5717 drm_crtc_vblank_on(crtc);
89b3c3c7 5718
7ff89ca2 5719 intel_encoders_enable(crtc, pipe_config, old_state);
89b3c3c7
ACO
5720}
5721
7ff89ca2 5722static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
2b73001e 5723{
7ff89ca2
VS
5724 struct drm_device *dev = crtc->base.dev;
5725 struct drm_i915_private *dev_priv = to_i915(dev);
83d7c81f 5726
7ff89ca2
VS
5727 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
5728 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
2b73001e
VS
5729}
5730
7ff89ca2
VS
5731static void i9xx_crtc_enable(struct intel_crtc_state *pipe_config,
5732 struct drm_atomic_state *old_state)
2b73001e 5733{
04548cba
VS
5734 struct intel_atomic_state *old_intel_state =
5735 to_intel_atomic_state(old_state);
7ff89ca2
VS
5736 struct drm_crtc *crtc = pipe_config->base.crtc;
5737 struct drm_device *dev = crtc->dev;
5738 struct drm_i915_private *dev_priv = to_i915(dev);
5739 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5740 enum pipe pipe = intel_crtc->pipe;
2b73001e 5741
7ff89ca2
VS
5742 if (WARN_ON(intel_crtc->active))
5743 return;
2b73001e 5744
7ff89ca2 5745 i9xx_set_pll_dividers(intel_crtc);
2b73001e 5746
7ff89ca2
VS
5747 if (intel_crtc_has_dp_encoder(intel_crtc->config))
5748 intel_dp_set_m_n(intel_crtc, M1_N1);
83d7c81f 5749
7ff89ca2
VS
5750 intel_set_pipe_timings(intel_crtc);
5751 intel_set_pipe_src_size(intel_crtc);
2b73001e 5752
7ff89ca2 5753 i9xx_set_pipeconf(intel_crtc);
f8437dd1 5754
7ff89ca2 5755 intel_crtc->active = true;
5f199dfa 5756
7ff89ca2
VS
5757 if (!IS_GEN2(dev_priv))
5758 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5f199dfa 5759
7ff89ca2 5760 intel_encoders_pre_enable(crtc, pipe_config, old_state);
f8437dd1 5761
7ff89ca2 5762 i9xx_enable_pll(intel_crtc);
f8437dd1 5763
7ff89ca2 5764 i9xx_pfit_enable(intel_crtc);
f8437dd1 5765
7ff89ca2 5766 intel_color_load_luts(&pipe_config->base);
f8437dd1 5767
04548cba
VS
5768 if (dev_priv->display.initial_watermarks != NULL)
5769 dev_priv->display.initial_watermarks(old_intel_state,
5770 intel_crtc->config);
5771 else
5772 intel_update_watermarks(intel_crtc);
7ff89ca2 5773 intel_enable_pipe(intel_crtc);
f8437dd1 5774
7ff89ca2
VS
5775 assert_vblank_disabled(crtc);
5776 drm_crtc_vblank_on(crtc);
f8437dd1 5777
7ff89ca2
VS
5778 intel_encoders_enable(crtc, pipe_config, old_state);
5779}
f8437dd1 5780
7ff89ca2
VS
5781static void i9xx_pfit_disable(struct intel_crtc *crtc)
5782{
5783 struct drm_device *dev = crtc->base.dev;
5784 struct drm_i915_private *dev_priv = to_i915(dev);
f8437dd1 5785
7ff89ca2 5786 if (!crtc->config->gmch_pfit.control)
f8437dd1 5787 return;
f8437dd1 5788
7ff89ca2
VS
5789 assert_pipe_disabled(dev_priv, crtc->pipe);
5790
5791 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
5792 I915_READ(PFIT_CONTROL));
5793 I915_WRITE(PFIT_CONTROL, 0);
f8437dd1
VK
5794}
5795
7ff89ca2
VS
5796static void i9xx_crtc_disable(struct intel_crtc_state *old_crtc_state,
5797 struct drm_atomic_state *old_state)
f8437dd1 5798{
7ff89ca2
VS
5799 struct drm_crtc *crtc = old_crtc_state->base.crtc;
5800 struct drm_device *dev = crtc->dev;
5801 struct drm_i915_private *dev_priv = to_i915(dev);
5802 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5803 int pipe = intel_crtc->pipe;
d66a2194 5804
d66a2194 5805 /*
7ff89ca2
VS
5806 * On gen2 planes are double buffered but the pipe isn't, so we must
5807 * wait for planes to fully turn off before disabling the pipe.
d66a2194 5808 */
7ff89ca2
VS
5809 if (IS_GEN2(dev_priv))
5810 intel_wait_for_vblank(dev_priv, pipe);
d66a2194 5811
7ff89ca2 5812 intel_encoders_disable(crtc, old_crtc_state, old_state);
d66a2194 5813
7ff89ca2
VS
5814 drm_crtc_vblank_off(crtc);
5815 assert_vblank_disabled(crtc);
d66a2194 5816
7ff89ca2 5817 intel_disable_pipe(intel_crtc);
d66a2194 5818
7ff89ca2 5819 i9xx_pfit_disable(intel_crtc);
89b3c3c7 5820
7ff89ca2 5821 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
d66a2194 5822
7ff89ca2
VS
5823 if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
5824 if (IS_CHERRYVIEW(dev_priv))
5825 chv_disable_pll(dev_priv, pipe);
5826 else if (IS_VALLEYVIEW(dev_priv))
5827 vlv_disable_pll(dev_priv, pipe);
5828 else
5829 i9xx_disable_pll(intel_crtc);
5830 }
c2e001ef 5831
7ff89ca2 5832 intel_encoders_post_pll_disable(crtc, old_crtc_state, old_state);
89b3c3c7 5833
7ff89ca2
VS
5834 if (!IS_GEN2(dev_priv))
5835 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
ff32c54e
VS
5836
5837 if (!dev_priv->display.initial_watermarks)
5838 intel_update_watermarks(intel_crtc);
f8437dd1
VK
5839}
5840
7ff89ca2 5841static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
f8437dd1 5842{
7ff89ca2
VS
5843 struct intel_encoder *encoder;
5844 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5845 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
5846 enum intel_display_power_domain domain;
d2d15016 5847 u64 domains;
7ff89ca2
VS
5848 struct drm_atomic_state *state;
5849 struct intel_crtc_state *crtc_state;
5850 int ret;
f8437dd1 5851
7ff89ca2
VS
5852 if (!intel_crtc->active)
5853 return;
a8ca4934 5854
7ff89ca2
VS
5855 if (crtc->primary->state->visible) {
5856 WARN_ON(intel_crtc->flip_work);
5d96d8af 5857
7ff89ca2 5858 intel_pre_disable_primary_noatomic(crtc);
709e05c3 5859
7ff89ca2
VS
5860 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
5861 crtc->primary->state->visible = false;
5862 }
5d96d8af 5863
7ff89ca2
VS
5864 state = drm_atomic_state_alloc(crtc->dev);
5865 if (!state) {
5866 DRM_DEBUG_KMS("failed to disable [CRTC:%d:%s], out of memory",
5867 crtc->base.id, crtc->name);
1c3f7700 5868 return;
7ff89ca2 5869 }
9f7eb31a 5870
7ff89ca2 5871 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
ea61791e 5872
7ff89ca2
VS
5873 /* Everything's already locked, -EDEADLK can't happen. */
5874 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
5875 ret = drm_atomic_add_affected_connectors(state, crtc);
9f7eb31a 5876
7ff89ca2 5877 WARN_ON(IS_ERR(crtc_state) || ret);
5d96d8af 5878
7ff89ca2 5879 dev_priv->display.crtc_disable(crtc_state, state);
4a806558 5880
0853695c 5881 drm_atomic_state_put(state);
842e0307 5882
78108b7c
VS
5883 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
5884 crtc->base.id, crtc->name);
842e0307
ML
5885
5886 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
5887 crtc->state->active = false;
37d9078b 5888 intel_crtc->active = false;
842e0307
ML
5889 crtc->enabled = false;
5890 crtc->state->connector_mask = 0;
5891 crtc->state->encoder_mask = 0;
5892
5893 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
5894 encoder->base.crtc = NULL;
5895
58f9c0bc 5896 intel_fbc_disable(intel_crtc);
432081bc 5897 intel_update_watermarks(intel_crtc);
1f7457b1 5898 intel_disable_shared_dpll(intel_crtc);
b17d48e2
ML
5899
5900 domains = intel_crtc->enabled_power_domains;
5901 for_each_power_domain(domain, domains)
5902 intel_display_power_put(dev_priv, domain);
5903 intel_crtc->enabled_power_domains = 0;
565602d7
ML
5904
5905 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
5906 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
b17d48e2
ML
5907}
5908
6b72d486
ML
5909/*
5910 * turn all crtc's off, but do not adjust state
5911 * This has to be paired with a call to intel_modeset_setup_hw_state.
5912 */
70e0bd74 5913int intel_display_suspend(struct drm_device *dev)
ee7b9f93 5914{
e2c8b870 5915 struct drm_i915_private *dev_priv = to_i915(dev);
70e0bd74 5916 struct drm_atomic_state *state;
e2c8b870 5917 int ret;
70e0bd74 5918
e2c8b870
ML
5919 state = drm_atomic_helper_suspend(dev);
5920 ret = PTR_ERR_OR_ZERO(state);
70e0bd74
ML
5921 if (ret)
5922 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
e2c8b870
ML
5923 else
5924 dev_priv->modeset_restore_state = state;
70e0bd74 5925 return ret;
ee7b9f93
JB
5926}
5927
ea5b213a 5928void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 5929{
4ef69c7a 5930 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 5931
ea5b213a
CW
5932 drm_encoder_cleanup(encoder);
5933 kfree(intel_encoder);
7e7d76c3
JB
5934}
5935
0a91ca29
DV
5936/* Cross check the actual hw state with our own modeset state tracking (and it's
5937 * internal consistency). */
749d98b8
ML
5938static void intel_connector_verify_state(struct drm_crtc_state *crtc_state,
5939 struct drm_connector_state *conn_state)
79e53945 5940{
749d98b8 5941 struct intel_connector *connector = to_intel_connector(conn_state->connector);
35dd3c64
ML
5942
5943 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
5944 connector->base.base.id,
5945 connector->base.name);
5946
0a91ca29 5947 if (connector->get_hw_state(connector)) {
e85376cb 5948 struct intel_encoder *encoder = connector->encoder;
0a91ca29 5949
749d98b8 5950 I915_STATE_WARN(!crtc_state,
35dd3c64 5951 "connector enabled without attached crtc\n");
0a91ca29 5952
749d98b8 5953 if (!crtc_state)
35dd3c64
ML
5954 return;
5955
749d98b8 5956 I915_STATE_WARN(!crtc_state->active,
35dd3c64
ML
5957 "connector is active, but attached crtc isn't\n");
5958
e85376cb 5959 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
35dd3c64
ML
5960 return;
5961
e85376cb 5962 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
35dd3c64
ML
5963 "atomic encoder doesn't match attached encoder\n");
5964
e85376cb 5965 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
35dd3c64
ML
5966 "attached encoder crtc differs from connector crtc\n");
5967 } else {
749d98b8 5968 I915_STATE_WARN(crtc_state && crtc_state->active,
4d688a2a 5969 "attached crtc is active, but connector isn't\n");
749d98b8 5970 I915_STATE_WARN(!crtc_state && conn_state->best_encoder,
35dd3c64 5971 "best encoder set without crtc!\n");
0a91ca29 5972 }
79e53945
JB
5973}
5974
08d9bc92
ACO
5975int intel_connector_init(struct intel_connector *connector)
5976{
11c1a9ec 5977 struct intel_digital_connector_state *conn_state;
08d9bc92 5978
11c1a9ec
ML
5979 /*
5980 * Allocate enough memory to hold intel_digital_connector_state,
5981 * This might be a few bytes too many, but for connectors that don't
5982 * need it we'll free the state and allocate a smaller one on the first
5983 * succesful commit anyway.
5984 */
5985 conn_state = kzalloc(sizeof(*conn_state), GFP_KERNEL);
5986 if (!conn_state)
08d9bc92
ACO
5987 return -ENOMEM;
5988
11c1a9ec
ML
5989 __drm_atomic_helper_connector_reset(&connector->base,
5990 &conn_state->base);
5991
08d9bc92
ACO
5992 return 0;
5993}
5994
5995struct intel_connector *intel_connector_alloc(void)
5996{
5997 struct intel_connector *connector;
5998
5999 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6000 if (!connector)
6001 return NULL;
6002
6003 if (intel_connector_init(connector) < 0) {
6004 kfree(connector);
6005 return NULL;
6006 }
6007
6008 return connector;
6009}
6010
f0947c37
DV
6011/* Simple connector->get_hw_state implementation for encoders that support only
6012 * one connector and no cloning and hence the encoder state determines the state
6013 * of the connector. */
6014bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 6015{
24929352 6016 enum pipe pipe = 0;
f0947c37 6017 struct intel_encoder *encoder = connector->encoder;
ea5b213a 6018
f0947c37 6019 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
6020}
6021
6d293983 6022static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
d272ddfa 6023{
6d293983
ACO
6024 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6025 return crtc_state->fdi_lanes;
d272ddfa
VS
6026
6027 return 0;
6028}
6029
6d293983 6030static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5cec258b 6031 struct intel_crtc_state *pipe_config)
1857e1da 6032{
8652744b 6033 struct drm_i915_private *dev_priv = to_i915(dev);
6d293983
ACO
6034 struct drm_atomic_state *state = pipe_config->base.state;
6035 struct intel_crtc *other_crtc;
6036 struct intel_crtc_state *other_crtc_state;
6037
1857e1da
DV
6038 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6039 pipe_name(pipe), pipe_config->fdi_lanes);
6040 if (pipe_config->fdi_lanes > 4) {
6041 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6042 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6043 return -EINVAL;
1857e1da
DV
6044 }
6045
8652744b 6046 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
1857e1da
DV
6047 if (pipe_config->fdi_lanes > 2) {
6048 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6049 pipe_config->fdi_lanes);
6d293983 6050 return -EINVAL;
1857e1da 6051 } else {
6d293983 6052 return 0;
1857e1da
DV
6053 }
6054 }
6055
b7f05d4a 6056 if (INTEL_INFO(dev_priv)->num_pipes == 2)
6d293983 6057 return 0;
1857e1da
DV
6058
6059 /* Ivybridge 3 pipe is really complicated */
6060 switch (pipe) {
6061 case PIPE_A:
6d293983 6062 return 0;
1857e1da 6063 case PIPE_B:
6d293983
ACO
6064 if (pipe_config->fdi_lanes <= 2)
6065 return 0;
6066
b91eb5cc 6067 other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
6d293983
ACO
6068 other_crtc_state =
6069 intel_atomic_get_crtc_state(state, other_crtc);
6070 if (IS_ERR(other_crtc_state))
6071 return PTR_ERR(other_crtc_state);
6072
6073 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
1857e1da
DV
6074 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6075 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6076 return -EINVAL;
1857e1da 6077 }
6d293983 6078 return 0;
1857e1da 6079 case PIPE_C:
251cc67c
VS
6080 if (pipe_config->fdi_lanes > 2) {
6081 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6082 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6083 return -EINVAL;
251cc67c 6084 }
6d293983 6085
b91eb5cc 6086 other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
6d293983
ACO
6087 other_crtc_state =
6088 intel_atomic_get_crtc_state(state, other_crtc);
6089 if (IS_ERR(other_crtc_state))
6090 return PTR_ERR(other_crtc_state);
6091
6092 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
1857e1da 6093 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
6d293983 6094 return -EINVAL;
1857e1da 6095 }
6d293983 6096 return 0;
1857e1da
DV
6097 default:
6098 BUG();
6099 }
6100}
6101
e29c22c0
DV
6102#define RETRY 1
6103static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5cec258b 6104 struct intel_crtc_state *pipe_config)
877d48d5 6105{
1857e1da 6106 struct drm_device *dev = intel_crtc->base.dev;
7c5f93b0 6107 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6d293983
ACO
6108 int lane, link_bw, fdi_dotclock, ret;
6109 bool needs_recompute = false;
877d48d5 6110
e29c22c0 6111retry:
877d48d5
DV
6112 /* FDI is a binary signal running at ~2.7GHz, encoding
6113 * each output octet as 10 bits. The actual frequency
6114 * is stored as a divider into a 100MHz clock, and the
6115 * mode pixel clock is stored in units of 1KHz.
6116 * Hence the bw of each lane in terms of the mode signal
6117 * is:
6118 */
21a727b3 6119 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
877d48d5 6120
241bfc38 6121 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 6122
2bd89a07 6123 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
DV
6124 pipe_config->pipe_bpp);
6125
6126 pipe_config->fdi_lanes = lane;
6127
2bd89a07 6128 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
877d48d5 6129 link_bw, &pipe_config->fdi_m_n);
1857e1da 6130
e3b247da 6131 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
6d293983 6132 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
e29c22c0 6133 pipe_config->pipe_bpp -= 2*3;
7ff89ca2
VS
6134 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6135 pipe_config->pipe_bpp);
6136 needs_recompute = true;
6137 pipe_config->bw_constrained = true;
257a7ffc 6138
7ff89ca2 6139 goto retry;
257a7ffc 6140 }
79e53945 6141
7ff89ca2
VS
6142 if (needs_recompute)
6143 return RETRY;
e70236a8 6144
7ff89ca2 6145 return ret;
e70236a8
JB
6146}
6147
7ff89ca2
VS
6148static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6149 struct intel_crtc_state *pipe_config)
e70236a8 6150{
7ff89ca2
VS
6151 if (pipe_config->pipe_bpp > 24)
6152 return false;
e70236a8 6153
7ff89ca2
VS
6154 /* HSW can handle pixel rate up to cdclk? */
6155 if (IS_HASWELL(dev_priv))
6156 return true;
1b1d2716 6157
65cd2b3f 6158 /*
7ff89ca2
VS
6159 * We compare against max which means we must take
6160 * the increased cdclk requirement into account when
6161 * calculating the new cdclk.
6162 *
6163 * Should measure whether using a lower cdclk w/o IPS
e70236a8 6164 */
7ff89ca2
VS
6165 return pipe_config->pixel_rate <=
6166 dev_priv->max_cdclk_freq * 95 / 100;
e70236a8 6167}
79e53945 6168
7ff89ca2
VS
6169static void hsw_compute_ips_config(struct intel_crtc *crtc,
6170 struct intel_crtc_state *pipe_config)
6171{
6172 struct drm_device *dev = crtc->base.dev;
6173 struct drm_i915_private *dev_priv = to_i915(dev);
34edce2f 6174
7ff89ca2
VS
6175 pipe_config->ips_enabled = i915.enable_ips &&
6176 hsw_crtc_supports_ips(crtc) &&
6177 pipe_config_supports_ips(dev_priv, pipe_config);
34edce2f
VS
6178}
6179
7ff89ca2 6180static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
34edce2f 6181{
7ff89ca2 6182 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
34edce2f 6183
7ff89ca2
VS
6184 /* GDG double wide on either pipe, otherwise pipe A only */
6185 return INTEL_INFO(dev_priv)->gen < 4 &&
6186 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
34edce2f
VS
6187}
6188
ceb99320
VS
6189static uint32_t ilk_pipe_pixel_rate(const struct intel_crtc_state *pipe_config)
6190{
6191 uint32_t pixel_rate;
6192
6193 pixel_rate = pipe_config->base.adjusted_mode.crtc_clock;
6194
6195 /*
6196 * We only use IF-ID interlacing. If we ever use
6197 * PF-ID we'll need to adjust the pixel_rate here.
6198 */
6199
6200 if (pipe_config->pch_pfit.enabled) {
6201 uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
6202 uint32_t pfit_size = pipe_config->pch_pfit.size;
6203
6204 pipe_w = pipe_config->pipe_src_w;
6205 pipe_h = pipe_config->pipe_src_h;
6206
6207 pfit_w = (pfit_size >> 16) & 0xFFFF;
6208 pfit_h = pfit_size & 0xFFFF;
6209 if (pipe_w < pfit_w)
6210 pipe_w = pfit_w;
6211 if (pipe_h < pfit_h)
6212 pipe_h = pfit_h;
6213
6214 if (WARN_ON(!pfit_w || !pfit_h))
6215 return pixel_rate;
6216
6217 pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
6218 pfit_w * pfit_h);
6219 }
6220
6221 return pixel_rate;
6222}
6223
7ff89ca2 6224static void intel_crtc_compute_pixel_rate(struct intel_crtc_state *crtc_state)
34edce2f 6225{
7ff89ca2 6226 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
34edce2f 6227
7ff89ca2
VS
6228 if (HAS_GMCH_DISPLAY(dev_priv))
6229 /* FIXME calculate proper pipe pixel rate for GMCH pfit */
6230 crtc_state->pixel_rate =
6231 crtc_state->base.adjusted_mode.crtc_clock;
6232 else
6233 crtc_state->pixel_rate =
6234 ilk_pipe_pixel_rate(crtc_state);
6235}
34edce2f 6236
7ff89ca2
VS
6237static int intel_crtc_compute_config(struct intel_crtc *crtc,
6238 struct intel_crtc_state *pipe_config)
6239{
6240 struct drm_device *dev = crtc->base.dev;
6241 struct drm_i915_private *dev_priv = to_i915(dev);
6242 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6243 int clock_limit = dev_priv->max_dotclk_freq;
34edce2f 6244
7ff89ca2
VS
6245 if (INTEL_GEN(dev_priv) < 4) {
6246 clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
34edce2f 6247
7ff89ca2
VS
6248 /*
6249 * Enable double wide mode when the dot clock
6250 * is > 90% of the (display) core speed.
6251 */
6252 if (intel_crtc_supports_double_wide(crtc) &&
6253 adjusted_mode->crtc_clock > clock_limit) {
6254 clock_limit = dev_priv->max_dotclk_freq;
6255 pipe_config->double_wide = true;
6256 }
34edce2f
VS
6257 }
6258
7ff89ca2
VS
6259 if (adjusted_mode->crtc_clock > clock_limit) {
6260 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6261 adjusted_mode->crtc_clock, clock_limit,
6262 yesno(pipe_config->double_wide));
6263 return -EINVAL;
6264 }
34edce2f 6265
7ff89ca2
VS
6266 /*
6267 * Pipe horizontal size must be even in:
6268 * - DVO ganged mode
6269 * - LVDS dual channel mode
6270 * - Double wide pipe
6271 */
6272 if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
6273 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6274 pipe_config->pipe_src_w &= ~1;
34edce2f 6275
7ff89ca2
VS
6276 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6277 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
6278 */
6279 if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
6280 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
6281 return -EINVAL;
34edce2f 6282
7ff89ca2 6283 intel_crtc_compute_pixel_rate(pipe_config);
34edce2f 6284
7ff89ca2
VS
6285 if (HAS_IPS(dev_priv))
6286 hsw_compute_ips_config(crtc, pipe_config);
34edce2f 6287
7ff89ca2
VS
6288 if (pipe_config->has_pch_encoder)
6289 return ironlake_fdi_compute_config(crtc, pipe_config);
34edce2f 6290
7ff89ca2 6291 return 0;
34edce2f
VS
6292}
6293
2c07245f 6294static void
a65851af 6295intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 6296{
a65851af
VS
6297 while (*num > DATA_LINK_M_N_MASK ||
6298 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
6299 *num >>= 1;
6300 *den >>= 1;
6301 }
6302}
6303
a65851af
VS
6304static void compute_m_n(unsigned int m, unsigned int n,
6305 uint32_t *ret_m, uint32_t *ret_n)
6306{
9a86cda0
JN
6307 /*
6308 * Reduce M/N as much as possible without loss in precision. Several DP
6309 * dongles in particular seem to be fussy about too large *link* M/N
6310 * values. The passed in values are more likely to have the least
6311 * significant bits zero than M after rounding below, so do this first.
6312 */
6313 while ((m & 1) == 0 && (n & 1) == 0) {
6314 m >>= 1;
6315 n >>= 1;
6316 }
6317
a65851af
VS
6318 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
6319 *ret_m = div_u64((uint64_t) m * *ret_n, n);
6320 intel_reduce_m_n_ratio(ret_m, ret_n);
6321}
6322
e69d0bc1
DV
6323void
6324intel_link_compute_m_n(int bits_per_pixel, int nlanes,
6325 int pixel_clock, int link_clock,
6326 struct intel_link_m_n *m_n)
2c07245f 6327{
e69d0bc1 6328 m_n->tu = 64;
a65851af
VS
6329
6330 compute_m_n(bits_per_pixel * pixel_clock,
6331 link_clock * nlanes * 8,
6332 &m_n->gmch_m, &m_n->gmch_n);
6333
6334 compute_m_n(pixel_clock, link_clock,
6335 &m_n->link_m, &m_n->link_n);
2c07245f
ZW
6336}
6337
a7615030
CW
6338static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
6339{
d330a953
JN
6340 if (i915.panel_use_ssc >= 0)
6341 return i915.panel_use_ssc != 0;
41aa3448 6342 return dev_priv->vbt.lvds_use_ssc
435793df 6343 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
6344}
6345
7429e9d4 6346static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 6347{
7df00d7a 6348 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 6349}
f47709a9 6350
7429e9d4
DV
6351static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
6352{
6353 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
6354}
6355
f47709a9 6356static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
190f68c5 6357 struct intel_crtc_state *crtc_state,
9e2c8475 6358 struct dpll *reduced_clock)
a7516a05 6359{
9b1e14f4 6360 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
a7516a05
JB
6361 u32 fp, fp2 = 0;
6362
9b1e14f4 6363 if (IS_PINEVIEW(dev_priv)) {
190f68c5 6364 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
a7516a05 6365 if (reduced_clock)
7429e9d4 6366 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 6367 } else {
190f68c5 6368 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
a7516a05 6369 if (reduced_clock)
7429e9d4 6370 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
6371 }
6372
190f68c5 6373 crtc_state->dpll_hw_state.fp0 = fp;
a7516a05 6374
f47709a9 6375 crtc->lowfreq_avail = false;
2d84d2b3 6376 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ab585dea 6377 reduced_clock) {
190f68c5 6378 crtc_state->dpll_hw_state.fp1 = fp2;
f47709a9 6379 crtc->lowfreq_avail = true;
a7516a05 6380 } else {
190f68c5 6381 crtc_state->dpll_hw_state.fp1 = fp;
a7516a05
JB
6382 }
6383}
6384
5e69f97f
CML
6385static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
6386 pipe)
89b667f8
JB
6387{
6388 u32 reg_val;
6389
6390 /*
6391 * PLLB opamp always calibrates to max value of 0x3f, force enable it
6392 * and set it to a reasonable value instead.
6393 */
ab3c759a 6394 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
6395 reg_val &= 0xffffff00;
6396 reg_val |= 0x00000030;
ab3c759a 6397 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 6398
ab3c759a 6399 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
ed58570f
ID
6400 reg_val &= 0x00ffffff;
6401 reg_val |= 0x8c000000;
ab3c759a 6402 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 6403
ab3c759a 6404 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 6405 reg_val &= 0xffffff00;
ab3c759a 6406 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 6407
ab3c759a 6408 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
6409 reg_val &= 0x00ffffff;
6410 reg_val |= 0xb0000000;
ab3c759a 6411 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
6412}
6413
b551842d
DV
6414static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
6415 struct intel_link_m_n *m_n)
6416{
6417 struct drm_device *dev = crtc->base.dev;
fac5e23e 6418 struct drm_i915_private *dev_priv = to_i915(dev);
b551842d
DV
6419 int pipe = crtc->pipe;
6420
e3b95f1e
DV
6421 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6422 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
6423 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
6424 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
6425}
6426
6427static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
6428 struct intel_link_m_n *m_n,
6429 struct intel_link_m_n *m2_n2)
b551842d 6430{
6315b5d3 6431 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
b551842d 6432 int pipe = crtc->pipe;
6e3c9717 6433 enum transcoder transcoder = crtc->config->cpu_transcoder;
b551842d 6434
6315b5d3 6435 if (INTEL_GEN(dev_priv) >= 5) {
b551842d
DV
6436 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
6437 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
6438 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
6439 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
f769cd24
VK
6440 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
6441 * for gen < 8) and if DRRS is supported (to make sure the
6442 * registers are not unnecessarily accessed).
6443 */
920a14b2
TU
6444 if (m2_n2 && (IS_CHERRYVIEW(dev_priv) ||
6445 INTEL_GEN(dev_priv) < 8) && crtc->config->has_drrs) {
f769cd24
VK
6446 I915_WRITE(PIPE_DATA_M2(transcoder),
6447 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
6448 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
6449 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
6450 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
6451 }
b551842d 6452 } else {
e3b95f1e
DV
6453 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6454 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
6455 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
6456 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
6457 }
6458}
6459
fe3cd48d 6460void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
03afc4a2 6461{
fe3cd48d
R
6462 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
6463
6464 if (m_n == M1_N1) {
6465 dp_m_n = &crtc->config->dp_m_n;
6466 dp_m2_n2 = &crtc->config->dp_m2_n2;
6467 } else if (m_n == M2_N2) {
6468
6469 /*
6470 * M2_N2 registers are not supported. Hence m2_n2 divider value
6471 * needs to be programmed into M1_N1.
6472 */
6473 dp_m_n = &crtc->config->dp_m2_n2;
6474 } else {
6475 DRM_ERROR("Unsupported divider value\n");
6476 return;
6477 }
6478
6e3c9717
ACO
6479 if (crtc->config->has_pch_encoder)
6480 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
03afc4a2 6481 else
fe3cd48d 6482 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
03afc4a2
DV
6483}
6484
251ac862
DV
6485static void vlv_compute_dpll(struct intel_crtc *crtc,
6486 struct intel_crtc_state *pipe_config)
bdd4b6a6 6487{
03ed5cbf 6488 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
cd2d34d9 6489 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
03ed5cbf
VS
6490 if (crtc->pipe != PIPE_A)
6491 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
bdd4b6a6 6492
cd2d34d9 6493 /* DPLL not used with DSI, but still need the rest set up */
d7edc4e5 6494 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
cd2d34d9
VS
6495 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
6496 DPLL_EXT_BUFFER_ENABLE_VLV;
6497
03ed5cbf
VS
6498 pipe_config->dpll_hw_state.dpll_md =
6499 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
6500}
bdd4b6a6 6501
03ed5cbf
VS
6502static void chv_compute_dpll(struct intel_crtc *crtc,
6503 struct intel_crtc_state *pipe_config)
6504{
6505 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
cd2d34d9 6506 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
03ed5cbf
VS
6507 if (crtc->pipe != PIPE_A)
6508 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
6509
cd2d34d9 6510 /* DPLL not used with DSI, but still need the rest set up */
d7edc4e5 6511 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
cd2d34d9
VS
6512 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
6513
03ed5cbf
VS
6514 pipe_config->dpll_hw_state.dpll_md =
6515 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
bdd4b6a6
DV
6516}
6517
d288f65f 6518static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 6519 const struct intel_crtc_state *pipe_config)
a0c4da24 6520{
f47709a9 6521 struct drm_device *dev = crtc->base.dev;
fac5e23e 6522 struct drm_i915_private *dev_priv = to_i915(dev);
cd2d34d9 6523 enum pipe pipe = crtc->pipe;
bdd4b6a6 6524 u32 mdiv;
a0c4da24 6525 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 6526 u32 coreclk, reg_val;
a0c4da24 6527
cd2d34d9
VS
6528 /* Enable Refclk */
6529 I915_WRITE(DPLL(pipe),
6530 pipe_config->dpll_hw_state.dpll &
6531 ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
6532
6533 /* No need to actually set up the DPLL with DSI */
6534 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
6535 return;
6536
a580516d 6537 mutex_lock(&dev_priv->sb_lock);
09153000 6538
d288f65f
VS
6539 bestn = pipe_config->dpll.n;
6540 bestm1 = pipe_config->dpll.m1;
6541 bestm2 = pipe_config->dpll.m2;
6542 bestp1 = pipe_config->dpll.p1;
6543 bestp2 = pipe_config->dpll.p2;
a0c4da24 6544
89b667f8
JB
6545 /* See eDP HDMI DPIO driver vbios notes doc */
6546
6547 /* PLL B needs special handling */
bdd4b6a6 6548 if (pipe == PIPE_B)
5e69f97f 6549 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
6550
6551 /* Set up Tx target for periodic Rcomp update */
ab3c759a 6552 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
6553
6554 /* Disable target IRef on PLL */
ab3c759a 6555 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 6556 reg_val &= 0x00ffffff;
ab3c759a 6557 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
6558
6559 /* Disable fast lock */
ab3c759a 6560 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
6561
6562 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
6563 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
6564 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
6565 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 6566 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
6567
6568 /*
6569 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
6570 * but we don't support that).
6571 * Note: don't use the DAC post divider as it seems unstable.
6572 */
6573 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 6574 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 6575
a0c4da24 6576 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 6577 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 6578
89b667f8 6579 /* Set HBR and RBR LPF coefficients */
d288f65f 6580 if (pipe_config->port_clock == 162000 ||
2d84d2b3
VS
6581 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
6582 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
ab3c759a 6583 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 6584 0x009f0003);
89b667f8 6585 else
ab3c759a 6586 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
6587 0x00d0000f);
6588
37a5650b 6589 if (intel_crtc_has_dp_encoder(pipe_config)) {
89b667f8 6590 /* Use SSC source */
bdd4b6a6 6591 if (pipe == PIPE_A)
ab3c759a 6592 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6593 0x0df40000);
6594 else
ab3c759a 6595 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6596 0x0df70000);
6597 } else { /* HDMI or VGA */
6598 /* Use bend source */
bdd4b6a6 6599 if (pipe == PIPE_A)
ab3c759a 6600 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6601 0x0df70000);
6602 else
ab3c759a 6603 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6604 0x0df40000);
6605 }
a0c4da24 6606
ab3c759a 6607 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8 6608 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
2210ce7f 6609 if (intel_crtc_has_dp_encoder(crtc->config))
89b667f8 6610 coreclk |= 0x01000000;
ab3c759a 6611 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 6612
ab3c759a 6613 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
a580516d 6614 mutex_unlock(&dev_priv->sb_lock);
a0c4da24
JB
6615}
6616
d288f65f 6617static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 6618 const struct intel_crtc_state *pipe_config)
9d556c99
CML
6619{
6620 struct drm_device *dev = crtc->base.dev;
fac5e23e 6621 struct drm_i915_private *dev_priv = to_i915(dev);
cd2d34d9 6622 enum pipe pipe = crtc->pipe;
9d556c99 6623 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9cbe40c1 6624 u32 loopfilter, tribuf_calcntr;
9d556c99 6625 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
a945ce7e 6626 u32 dpio_val;
9cbe40c1 6627 int vco;
9d556c99 6628
cd2d34d9
VS
6629 /* Enable Refclk and SSC */
6630 I915_WRITE(DPLL(pipe),
6631 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
6632
6633 /* No need to actually set up the DPLL with DSI */
6634 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
6635 return;
6636
d288f65f
VS
6637 bestn = pipe_config->dpll.n;
6638 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
6639 bestm1 = pipe_config->dpll.m1;
6640 bestm2 = pipe_config->dpll.m2 >> 22;
6641 bestp1 = pipe_config->dpll.p1;
6642 bestp2 = pipe_config->dpll.p2;
9cbe40c1 6643 vco = pipe_config->dpll.vco;
a945ce7e 6644 dpio_val = 0;
9cbe40c1 6645 loopfilter = 0;
9d556c99 6646
a580516d 6647 mutex_lock(&dev_priv->sb_lock);
9d556c99 6648
9d556c99
CML
6649 /* p1 and p2 divider */
6650 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
6651 5 << DPIO_CHV_S1_DIV_SHIFT |
6652 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
6653 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
6654 1 << DPIO_CHV_K_DIV_SHIFT);
6655
6656 /* Feedback post-divider - m2 */
6657 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
6658
6659 /* Feedback refclk divider - n and m1 */
6660 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
6661 DPIO_CHV_M1_DIV_BY_2 |
6662 1 << DPIO_CHV_N_DIV_SHIFT);
6663
6664 /* M2 fraction division */
25a25dfc 6665 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
9d556c99
CML
6666
6667 /* M2 fraction division enable */
a945ce7e
VP
6668 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
6669 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
6670 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
6671 if (bestm2_frac)
6672 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
6673 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
9d556c99 6674
de3a0fde
VP
6675 /* Program digital lock detect threshold */
6676 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
6677 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
6678 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
6679 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
6680 if (!bestm2_frac)
6681 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
6682 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
6683
9d556c99 6684 /* Loop filter */
9cbe40c1
VP
6685 if (vco == 5400000) {
6686 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
6687 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
6688 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
6689 tribuf_calcntr = 0x9;
6690 } else if (vco <= 6200000) {
6691 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
6692 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
6693 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6694 tribuf_calcntr = 0x9;
6695 } else if (vco <= 6480000) {
6696 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
6697 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
6698 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6699 tribuf_calcntr = 0x8;
6700 } else {
6701 /* Not supported. Apply the same limits as in the max case */
6702 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
6703 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
6704 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6705 tribuf_calcntr = 0;
6706 }
9d556c99
CML
6707 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
6708
968040b2 6709 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
9cbe40c1
VP
6710 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
6711 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
6712 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
6713
9d556c99
CML
6714 /* AFC Recal */
6715 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
6716 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
6717 DPIO_AFC_RECAL);
6718
a580516d 6719 mutex_unlock(&dev_priv->sb_lock);
9d556c99
CML
6720}
6721
d288f65f
VS
6722/**
6723 * vlv_force_pll_on - forcibly enable just the PLL
6724 * @dev_priv: i915 private structure
6725 * @pipe: pipe PLL to enable
6726 * @dpll: PLL configuration
6727 *
6728 * Enable the PLL for @pipe using the supplied @dpll config. To be used
6729 * in cases where we need the PLL enabled even when @pipe is not going to
6730 * be enabled.
6731 */
30ad9814 6732int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
3f36b937 6733 const struct dpll *dpll)
d288f65f 6734{
b91eb5cc 6735 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
3f36b937
TU
6736 struct intel_crtc_state *pipe_config;
6737
6738 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
6739 if (!pipe_config)
6740 return -ENOMEM;
6741
6742 pipe_config->base.crtc = &crtc->base;
6743 pipe_config->pixel_multiplier = 1;
6744 pipe_config->dpll = *dpll;
d288f65f 6745
30ad9814 6746 if (IS_CHERRYVIEW(dev_priv)) {
3f36b937
TU
6747 chv_compute_dpll(crtc, pipe_config);
6748 chv_prepare_pll(crtc, pipe_config);
6749 chv_enable_pll(crtc, pipe_config);
d288f65f 6750 } else {
3f36b937
TU
6751 vlv_compute_dpll(crtc, pipe_config);
6752 vlv_prepare_pll(crtc, pipe_config);
6753 vlv_enable_pll(crtc, pipe_config);
d288f65f 6754 }
3f36b937
TU
6755
6756 kfree(pipe_config);
6757
6758 return 0;
d288f65f
VS
6759}
6760
6761/**
6762 * vlv_force_pll_off - forcibly disable just the PLL
6763 * @dev_priv: i915 private structure
6764 * @pipe: pipe PLL to disable
6765 *
6766 * Disable the PLL for @pipe. To be used in cases where we need
6767 * the PLL enabled even when @pipe is not going to be enabled.
6768 */
30ad9814 6769void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
d288f65f 6770{
30ad9814
VS
6771 if (IS_CHERRYVIEW(dev_priv))
6772 chv_disable_pll(dev_priv, pipe);
d288f65f 6773 else
30ad9814 6774 vlv_disable_pll(dev_priv, pipe);
d288f65f
VS
6775}
6776
251ac862
DV
6777static void i9xx_compute_dpll(struct intel_crtc *crtc,
6778 struct intel_crtc_state *crtc_state,
9e2c8475 6779 struct dpll *reduced_clock)
eb1cbe48 6780{
9b1e14f4 6781 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
eb1cbe48 6782 u32 dpll;
190f68c5 6783 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 6784
190f68c5 6785 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 6786
eb1cbe48
DV
6787 dpll = DPLL_VGA_MODE_DIS;
6788
2d84d2b3 6789 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
6790 dpll |= DPLLB_MODE_LVDS;
6791 else
6792 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 6793
73f67aa8
JN
6794 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
6795 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
190f68c5 6796 dpll |= (crtc_state->pixel_multiplier - 1)
198a037f 6797 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 6798 }
198a037f 6799
3d6e9ee0
VS
6800 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
6801 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
4a33e48d 6802 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 6803
37a5650b 6804 if (intel_crtc_has_dp_encoder(crtc_state))
4a33e48d 6805 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
6806
6807 /* compute bitmask from p1 value */
9b1e14f4 6808 if (IS_PINEVIEW(dev_priv))
eb1cbe48
DV
6809 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
6810 else {
6811 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
9beb5fea 6812 if (IS_G4X(dev_priv) && reduced_clock)
eb1cbe48
DV
6813 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
6814 }
6815 switch (clock->p2) {
6816 case 5:
6817 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
6818 break;
6819 case 7:
6820 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
6821 break;
6822 case 10:
6823 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
6824 break;
6825 case 14:
6826 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
6827 break;
6828 }
9b1e14f4 6829 if (INTEL_GEN(dev_priv) >= 4)
eb1cbe48
DV
6830 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
6831
190f68c5 6832 if (crtc_state->sdvo_tv_clock)
eb1cbe48 6833 dpll |= PLL_REF_INPUT_TVCLKINBC;
2d84d2b3 6834 else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 6835 intel_panel_use_ssc(dev_priv))
eb1cbe48
DV
6836 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
6837 else
6838 dpll |= PLL_REF_INPUT_DREFCLK;
6839
6840 dpll |= DPLL_VCO_ENABLE;
190f68c5 6841 crtc_state->dpll_hw_state.dpll = dpll;
8bcc2795 6842
9b1e14f4 6843 if (INTEL_GEN(dev_priv) >= 4) {
190f68c5 6844 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
ef1b460d 6845 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
190f68c5 6846 crtc_state->dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
DV
6847 }
6848}
6849
251ac862
DV
6850static void i8xx_compute_dpll(struct intel_crtc *crtc,
6851 struct intel_crtc_state *crtc_state,
9e2c8475 6852 struct dpll *reduced_clock)
eb1cbe48 6853{
f47709a9 6854 struct drm_device *dev = crtc->base.dev;
fac5e23e 6855 struct drm_i915_private *dev_priv = to_i915(dev);
eb1cbe48 6856 u32 dpll;
190f68c5 6857 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 6858
190f68c5 6859 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 6860
eb1cbe48
DV
6861 dpll = DPLL_VGA_MODE_DIS;
6862
2d84d2b3 6863 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
6864 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6865 } else {
6866 if (clock->p1 == 2)
6867 dpll |= PLL_P1_DIVIDE_BY_TWO;
6868 else
6869 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6870 if (clock->p2 == 4)
6871 dpll |= PLL_P2_DIVIDE_BY_4;
6872 }
6873
50a0bc90
TU
6874 if (!IS_I830(dev_priv) &&
6875 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
4a33e48d
DV
6876 dpll |= DPLL_DVO_2X_MODE;
6877
2d84d2b3 6878 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 6879 intel_panel_use_ssc(dev_priv))
eb1cbe48
DV
6880 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
6881 else
6882 dpll |= PLL_REF_INPUT_DREFCLK;
6883
6884 dpll |= DPLL_VCO_ENABLE;
190f68c5 6885 crtc_state->dpll_hw_state.dpll = dpll;
eb1cbe48
DV
6886}
6887
8a654f3b 6888static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c 6889{
6315b5d3 6890 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
b0e77b9c 6891 enum pipe pipe = intel_crtc->pipe;
6e3c9717 6892 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
7c5f93b0 6893 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
1caea6e9
VS
6894 uint32_t crtc_vtotal, crtc_vblank_end;
6895 int vsyncshift = 0;
4d8a62ea
DV
6896
6897 /* We need to be careful not to changed the adjusted mode, for otherwise
6898 * the hw state checker will get angry at the mismatch. */
6899 crtc_vtotal = adjusted_mode->crtc_vtotal;
6900 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 6901
609aeaca 6902 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 6903 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
6904 crtc_vtotal -= 1;
6905 crtc_vblank_end -= 1;
609aeaca 6906
2d84d2b3 6907 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
609aeaca
VS
6908 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
6909 else
6910 vsyncshift = adjusted_mode->crtc_hsync_start -
6911 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
6912 if (vsyncshift < 0)
6913 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
6914 }
6915
6315b5d3 6916 if (INTEL_GEN(dev_priv) > 3)
fe2b8f9d 6917 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 6918
fe2b8f9d 6919 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
6920 (adjusted_mode->crtc_hdisplay - 1) |
6921 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 6922 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
6923 (adjusted_mode->crtc_hblank_start - 1) |
6924 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 6925 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
6926 (adjusted_mode->crtc_hsync_start - 1) |
6927 ((adjusted_mode->crtc_hsync_end - 1) << 16));
6928
fe2b8f9d 6929 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 6930 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 6931 ((crtc_vtotal - 1) << 16));
fe2b8f9d 6932 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 6933 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 6934 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 6935 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
6936 (adjusted_mode->crtc_vsync_start - 1) |
6937 ((adjusted_mode->crtc_vsync_end - 1) << 16));
6938
b5e508d4
PZ
6939 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
6940 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
6941 * documented on the DDI_FUNC_CTL register description, EDP Input Select
6942 * bits. */
772c2a51 6943 if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
b5e508d4
PZ
6944 (pipe == PIPE_B || pipe == PIPE_C))
6945 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
6946
bc58be60
JN
6947}
6948
6949static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
6950{
6951 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 6952 struct drm_i915_private *dev_priv = to_i915(dev);
bc58be60
JN
6953 enum pipe pipe = intel_crtc->pipe;
6954
b0e77b9c
PZ
6955 /* pipesrc controls the size that is scaled from, which should
6956 * always be the user's requested size.
6957 */
6958 I915_WRITE(PIPESRC(pipe),
6e3c9717
ACO
6959 ((intel_crtc->config->pipe_src_w - 1) << 16) |
6960 (intel_crtc->config->pipe_src_h - 1));
b0e77b9c
PZ
6961}
6962
1bd1bd80 6963static void intel_get_pipe_timings(struct intel_crtc *crtc,
5cec258b 6964 struct intel_crtc_state *pipe_config)
1bd1bd80
DV
6965{
6966 struct drm_device *dev = crtc->base.dev;
fac5e23e 6967 struct drm_i915_private *dev_priv = to_i915(dev);
1bd1bd80
DV
6968 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
6969 uint32_t tmp;
6970
6971 tmp = I915_READ(HTOTAL(cpu_transcoder));
2d112de7
ACO
6972 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
6973 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 6974 tmp = I915_READ(HBLANK(cpu_transcoder));
2d112de7
ACO
6975 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
6976 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 6977 tmp = I915_READ(HSYNC(cpu_transcoder));
2d112de7
ACO
6978 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
6979 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
6980
6981 tmp = I915_READ(VTOTAL(cpu_transcoder));
2d112de7
ACO
6982 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
6983 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 6984 tmp = I915_READ(VBLANK(cpu_transcoder));
2d112de7
ACO
6985 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
6986 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 6987 tmp = I915_READ(VSYNC(cpu_transcoder));
2d112de7
ACO
6988 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
6989 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
6990
6991 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
2d112de7
ACO
6992 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
6993 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
6994 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
1bd1bd80 6995 }
bc58be60
JN
6996}
6997
6998static void intel_get_pipe_src_size(struct intel_crtc *crtc,
6999 struct intel_crtc_state *pipe_config)
7000{
7001 struct drm_device *dev = crtc->base.dev;
fac5e23e 7002 struct drm_i915_private *dev_priv = to_i915(dev);
bc58be60 7003 u32 tmp;
1bd1bd80
DV
7004
7005 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
7006 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7007 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7008
2d112de7
ACO
7009 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7010 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
DV
7011}
7012
f6a83288 7013void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5cec258b 7014 struct intel_crtc_state *pipe_config)
babea61d 7015{
2d112de7
ACO
7016 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7017 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7018 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7019 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
babea61d 7020
2d112de7
ACO
7021 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7022 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7023 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7024 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
babea61d 7025
2d112de7 7026 mode->flags = pipe_config->base.adjusted_mode.flags;
cd13f5ab 7027 mode->type = DRM_MODE_TYPE_DRIVER;
babea61d 7028
2d112de7 7029 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
cd13f5ab
ML
7030
7031 mode->hsync = drm_mode_hsync(mode);
7032 mode->vrefresh = drm_mode_vrefresh(mode);
7033 drm_mode_set_name(mode);
babea61d
JB
7034}
7035
84b046f3
DV
7036static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7037{
6315b5d3 7038 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
84b046f3
DV
7039 uint32_t pipeconf;
7040
9f11a9e4 7041 pipeconf = 0;
84b046f3 7042
b6b5d049
VS
7043 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7044 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7045 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
67c72a12 7046
6e3c9717 7047 if (intel_crtc->config->double_wide)
cf532bb2 7048 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 7049
ff9ce46e 7050 /* only g4x and later have fancy bpc/dither controls */
9beb5fea
TU
7051 if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
7052 IS_CHERRYVIEW(dev_priv)) {
ff9ce46e 7053 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6e3c9717 7054 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
ff9ce46e 7055 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 7056 PIPECONF_DITHER_TYPE_SP;
84b046f3 7057
6e3c9717 7058 switch (intel_crtc->config->pipe_bpp) {
ff9ce46e
DV
7059 case 18:
7060 pipeconf |= PIPECONF_6BPC;
7061 break;
7062 case 24:
7063 pipeconf |= PIPECONF_8BPC;
7064 break;
7065 case 30:
7066 pipeconf |= PIPECONF_10BPC;
7067 break;
7068 default:
7069 /* Case prevented by intel_choose_pipe_bpp_dither. */
7070 BUG();
84b046f3
DV
7071 }
7072 }
7073
56b857a5 7074 if (HAS_PIPE_CXSR(dev_priv)) {
84b046f3
DV
7075 if (intel_crtc->lowfreq_avail) {
7076 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7077 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7078 } else {
7079 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
84b046f3
DV
7080 }
7081 }
7082
6e3c9717 7083 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
6315b5d3 7084 if (INTEL_GEN(dev_priv) < 4 ||
2d84d2b3 7085 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
efc2cfff
VS
7086 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7087 else
7088 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7089 } else
84b046f3
DV
7090 pipeconf |= PIPECONF_PROGRESSIVE;
7091
920a14b2 7092 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
666a4537 7093 intel_crtc->config->limited_color_range)
9f11a9e4 7094 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 7095
84b046f3
DV
7096 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7097 POSTING_READ(PIPECONF(intel_crtc->pipe));
7098}
7099
81c97f52
ACO
7100static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
7101 struct intel_crtc_state *crtc_state)
7102{
7103 struct drm_device *dev = crtc->base.dev;
fac5e23e 7104 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 7105 const struct intel_limit *limit;
81c97f52
ACO
7106 int refclk = 48000;
7107
7108 memset(&crtc_state->dpll_hw_state, 0,
7109 sizeof(crtc_state->dpll_hw_state));
7110
2d84d2b3 7111 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
81c97f52
ACO
7112 if (intel_panel_use_ssc(dev_priv)) {
7113 refclk = dev_priv->vbt.lvds_ssc_freq;
7114 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7115 }
7116
7117 limit = &intel_limits_i8xx_lvds;
2d84d2b3 7118 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
81c97f52
ACO
7119 limit = &intel_limits_i8xx_dvo;
7120 } else {
7121 limit = &intel_limits_i8xx_dac;
7122 }
7123
7124 if (!crtc_state->clock_set &&
7125 !i9xx_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 i8xx_compute_dpll(crtc, crtc_state, NULL);
7132
7133 return 0;
7134}
7135
19ec6693
ACO
7136static int g4x_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;
19ec6693
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)) {
19ec6693
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 if (intel_is_dual_link_lvds(dev))
7154 limit = &intel_limits_g4x_dual_channel_lvds;
7155 else
7156 limit = &intel_limits_g4x_single_channel_lvds;
2d84d2b3
VS
7157 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
7158 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
19ec6693 7159 limit = &intel_limits_g4x_hdmi;
2d84d2b3 7160 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
19ec6693
ACO
7161 limit = &intel_limits_g4x_sdvo;
7162 } else {
7163 /* The option is for other outputs */
7164 limit = &intel_limits_i9xx_sdvo;
7165 }
7166
7167 if (!crtc_state->clock_set &&
7168 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7169 refclk, NULL, &crtc_state->dpll)) {
7170 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7171 return -EINVAL;
7172 }
7173
7174 i9xx_compute_dpll(crtc, crtc_state, NULL);
7175
7176 return 0;
7177}
7178
70e8aa21
ACO
7179static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
7180 struct intel_crtc_state *crtc_state)
7181{
7182 struct drm_device *dev = crtc->base.dev;
fac5e23e 7183 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 7184 const struct intel_limit *limit;
70e8aa21
ACO
7185 int refclk = 96000;
7186
7187 memset(&crtc_state->dpll_hw_state, 0,
7188 sizeof(crtc_state->dpll_hw_state));
7189
2d84d2b3 7190 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
70e8aa21
ACO
7191 if (intel_panel_use_ssc(dev_priv)) {
7192 refclk = dev_priv->vbt.lvds_ssc_freq;
7193 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7194 }
7195
7196 limit = &intel_limits_pineview_lvds;
7197 } else {
7198 limit = &intel_limits_pineview_sdvo;
7199 }
7200
7201 if (!crtc_state->clock_set &&
7202 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7203 refclk, NULL, &crtc_state->dpll)) {
7204 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7205 return -EINVAL;
7206 }
7207
7208 i9xx_compute_dpll(crtc, crtc_state, NULL);
7209
7210 return 0;
7211}
7212
190f68c5
ACO
7213static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7214 struct intel_crtc_state *crtc_state)
79e53945 7215{
c7653199 7216 struct drm_device *dev = crtc->base.dev;
fac5e23e 7217 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 7218 const struct intel_limit *limit;
81c97f52 7219 int refclk = 96000;
79e53945 7220
dd3cd74a
ACO
7221 memset(&crtc_state->dpll_hw_state, 0,
7222 sizeof(crtc_state->dpll_hw_state));
7223
2d84d2b3 7224 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
70e8aa21
ACO
7225 if (intel_panel_use_ssc(dev_priv)) {
7226 refclk = dev_priv->vbt.lvds_ssc_freq;
7227 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7228 }
43565a06 7229
70e8aa21
ACO
7230 limit = &intel_limits_i9xx_lvds;
7231 } else {
7232 limit = &intel_limits_i9xx_sdvo;
81c97f52 7233 }
79e53945 7234
70e8aa21
ACO
7235 if (!crtc_state->clock_set &&
7236 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7237 refclk, NULL, &crtc_state->dpll)) {
7238 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7239 return -EINVAL;
f47709a9 7240 }
7026d4ac 7241
81c97f52 7242 i9xx_compute_dpll(crtc, crtc_state, NULL);
79e53945 7243
c8f7a0db 7244 return 0;
f564048e
EA
7245}
7246
65b3d6a9
ACO
7247static int chv_crtc_compute_clock(struct intel_crtc *crtc,
7248 struct intel_crtc_state *crtc_state)
7249{
7250 int refclk = 100000;
1b6f4958 7251 const struct intel_limit *limit = &intel_limits_chv;
65b3d6a9
ACO
7252
7253 memset(&crtc_state->dpll_hw_state, 0,
7254 sizeof(crtc_state->dpll_hw_state));
7255
65b3d6a9
ACO
7256 if (!crtc_state->clock_set &&
7257 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7258 refclk, NULL, &crtc_state->dpll)) {
7259 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7260 return -EINVAL;
7261 }
7262
7263 chv_compute_dpll(crtc, crtc_state);
7264
7265 return 0;
7266}
7267
7268static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
7269 struct intel_crtc_state *crtc_state)
7270{
7271 int refclk = 100000;
1b6f4958 7272 const struct intel_limit *limit = &intel_limits_vlv;
65b3d6a9
ACO
7273
7274 memset(&crtc_state->dpll_hw_state, 0,
7275 sizeof(crtc_state->dpll_hw_state));
7276
65b3d6a9
ACO
7277 if (!crtc_state->clock_set &&
7278 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7279 refclk, NULL, &crtc_state->dpll)) {
7280 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7281 return -EINVAL;
7282 }
7283
7284 vlv_compute_dpll(crtc, crtc_state);
7285
7286 return 0;
7287}
7288
2fa2fe9a 7289static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5cec258b 7290 struct intel_crtc_state *pipe_config)
2fa2fe9a 7291{
6315b5d3 7292 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2fa2fe9a
DV
7293 uint32_t tmp;
7294
50a0bc90
TU
7295 if (INTEL_GEN(dev_priv) <= 3 &&
7296 (IS_I830(dev_priv) || !IS_MOBILE(dev_priv)))
dc9e7dec
VS
7297 return;
7298
2fa2fe9a 7299 tmp = I915_READ(PFIT_CONTROL);
06922821
DV
7300 if (!(tmp & PFIT_ENABLE))
7301 return;
2fa2fe9a 7302
06922821 7303 /* Check whether the pfit is attached to our pipe. */
6315b5d3 7304 if (INTEL_GEN(dev_priv) < 4) {
2fa2fe9a
DV
7305 if (crtc->pipe != PIPE_B)
7306 return;
2fa2fe9a
DV
7307 } else {
7308 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7309 return;
7310 }
7311
06922821 7312 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a 7313 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
2fa2fe9a
DV
7314}
7315
acbec814 7316static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 7317 struct intel_crtc_state *pipe_config)
acbec814
JB
7318{
7319 struct drm_device *dev = crtc->base.dev;
fac5e23e 7320 struct drm_i915_private *dev_priv = to_i915(dev);
acbec814 7321 int pipe = pipe_config->cpu_transcoder;
9e2c8475 7322 struct dpll clock;
acbec814 7323 u32 mdiv;
662c6ecb 7324 int refclk = 100000;
acbec814 7325
b521973b
VS
7326 /* In case of DSI, DPLL will not be used */
7327 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
f573de5a
SK
7328 return;
7329
a580516d 7330 mutex_lock(&dev_priv->sb_lock);
ab3c759a 7331 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
a580516d 7332 mutex_unlock(&dev_priv->sb_lock);
acbec814
JB
7333
7334 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7335 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7336 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7337 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7338 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7339
dccbea3b 7340 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
acbec814
JB
7341}
7342
5724dbd1
DL
7343static void
7344i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7345 struct intel_initial_plane_config *plane_config)
1ad292b5
JB
7346{
7347 struct drm_device *dev = crtc->base.dev;
fac5e23e 7348 struct drm_i915_private *dev_priv = to_i915(dev);
1ad292b5
JB
7349 u32 val, base, offset;
7350 int pipe = crtc->pipe, plane = crtc->plane;
7351 int fourcc, pixel_format;
6761dd31 7352 unsigned int aligned_height;
b113d5ee 7353 struct drm_framebuffer *fb;
1b842c89 7354 struct intel_framebuffer *intel_fb;
1ad292b5 7355
42a7b088
DL
7356 val = I915_READ(DSPCNTR(plane));
7357 if (!(val & DISPLAY_PLANE_ENABLE))
7358 return;
7359
d9806c9f 7360 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 7361 if (!intel_fb) {
1ad292b5
JB
7362 DRM_DEBUG_KMS("failed to alloc fb\n");
7363 return;
7364 }
7365
1b842c89
DL
7366 fb = &intel_fb->base;
7367
d2e9f5fc
VS
7368 fb->dev = dev;
7369
6315b5d3 7370 if (INTEL_GEN(dev_priv) >= 4) {
18c5247e 7371 if (val & DISPPLANE_TILED) {
49af449b 7372 plane_config->tiling = I915_TILING_X;
bae781b2 7373 fb->modifier = I915_FORMAT_MOD_X_TILED;
18c5247e
DV
7374 }
7375 }
1ad292b5
JB
7376
7377 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 7378 fourcc = i9xx_format_to_fourcc(pixel_format);
2f3f4763 7379 fb->format = drm_format_info(fourcc);
1ad292b5 7380
6315b5d3 7381 if (INTEL_GEN(dev_priv) >= 4) {
49af449b 7382 if (plane_config->tiling)
1ad292b5
JB
7383 offset = I915_READ(DSPTILEOFF(plane));
7384 else
7385 offset = I915_READ(DSPLINOFF(plane));
7386 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7387 } else {
7388 base = I915_READ(DSPADDR(plane));
7389 }
7390 plane_config->base = base;
7391
7392 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
7393 fb->width = ((val >> 16) & 0xfff) + 1;
7394 fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
7395
7396 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 7397 fb->pitches[0] = val & 0xffffffc0;
1ad292b5 7398
d88c4afd 7399 aligned_height = intel_fb_align_height(fb, 0, fb->height);
1ad292b5 7400
f37b5c2b 7401 plane_config->size = fb->pitches[0] * aligned_height;
1ad292b5 7402
2844a921
DL
7403 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7404 pipe_name(pipe), plane, fb->width, fb->height,
272725c7 7405 fb->format->cpp[0] * 8, base, fb->pitches[0],
2844a921 7406 plane_config->size);
1ad292b5 7407
2d14030b 7408 plane_config->fb = intel_fb;
1ad292b5
JB
7409}
7410
70b23a98 7411static void chv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 7412 struct intel_crtc_state *pipe_config)
70b23a98
VS
7413{
7414 struct drm_device *dev = crtc->base.dev;
fac5e23e 7415 struct drm_i915_private *dev_priv = to_i915(dev);
70b23a98
VS
7416 int pipe = pipe_config->cpu_transcoder;
7417 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9e2c8475 7418 struct dpll clock;
0d7b6b11 7419 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
70b23a98
VS
7420 int refclk = 100000;
7421
b521973b
VS
7422 /* In case of DSI, DPLL will not be used */
7423 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7424 return;
7425
a580516d 7426 mutex_lock(&dev_priv->sb_lock);
70b23a98
VS
7427 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
7428 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
7429 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
7430 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
0d7b6b11 7431 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
a580516d 7432 mutex_unlock(&dev_priv->sb_lock);
70b23a98
VS
7433
7434 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
0d7b6b11
ID
7435 clock.m2 = (pll_dw0 & 0xff) << 22;
7436 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
7437 clock.m2 |= pll_dw2 & 0x3fffff;
70b23a98
VS
7438 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
7439 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
7440 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
7441
dccbea3b 7442 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
70b23a98
VS
7443}
7444
0e8ffe1b 7445static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5cec258b 7446 struct intel_crtc_state *pipe_config)
0e8ffe1b 7447{
6315b5d3 7448 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1729050e 7449 enum intel_display_power_domain power_domain;
0e8ffe1b 7450 uint32_t tmp;
1729050e 7451 bool ret;
0e8ffe1b 7452
1729050e
ID
7453 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
7454 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0
ID
7455 return false;
7456
e143a21c 7457 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 7458 pipe_config->shared_dpll = NULL;
eccb140b 7459
1729050e
ID
7460 ret = false;
7461
0e8ffe1b
DV
7462 tmp = I915_READ(PIPECONF(crtc->pipe));
7463 if (!(tmp & PIPECONF_ENABLE))
1729050e 7464 goto out;
0e8ffe1b 7465
9beb5fea
TU
7466 if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
7467 IS_CHERRYVIEW(dev_priv)) {
42571aef
VS
7468 switch (tmp & PIPECONF_BPC_MASK) {
7469 case PIPECONF_6BPC:
7470 pipe_config->pipe_bpp = 18;
7471 break;
7472 case PIPECONF_8BPC:
7473 pipe_config->pipe_bpp = 24;
7474 break;
7475 case PIPECONF_10BPC:
7476 pipe_config->pipe_bpp = 30;
7477 break;
7478 default:
7479 break;
7480 }
7481 }
7482
920a14b2 7483 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
666a4537 7484 (tmp & PIPECONF_COLOR_RANGE_SELECT))
b5a9fa09
DV
7485 pipe_config->limited_color_range = true;
7486
6315b5d3 7487 if (INTEL_GEN(dev_priv) < 4)
282740f7
VS
7488 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
7489
1bd1bd80 7490 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 7491 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 7492
2fa2fe9a
DV
7493 i9xx_get_pfit_config(crtc, pipe_config);
7494
6315b5d3 7495 if (INTEL_GEN(dev_priv) >= 4) {
c231775c 7496 /* No way to read it out on pipes B and C */
920a14b2 7497 if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
c231775c
VS
7498 tmp = dev_priv->chv_dpll_md[crtc->pipe];
7499 else
7500 tmp = I915_READ(DPLL_MD(crtc->pipe));
6c49f241
DV
7501 pipe_config->pixel_multiplier =
7502 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
7503 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 7504 pipe_config->dpll_hw_state.dpll_md = tmp;
50a0bc90 7505 } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
73f67aa8 7506 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
6c49f241
DV
7507 tmp = I915_READ(DPLL(crtc->pipe));
7508 pipe_config->pixel_multiplier =
7509 ((tmp & SDVO_MULTIPLIER_MASK)
7510 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
7511 } else {
7512 /* Note that on i915G/GM the pixel multiplier is in the sdvo
7513 * port and will be fixed up in the encoder->get_config
7514 * function. */
7515 pipe_config->pixel_multiplier = 1;
7516 }
8bcc2795 7517 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
920a14b2 7518 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
1c4e0274
VS
7519 /*
7520 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
7521 * on 830. Filter it out here so that we don't
7522 * report errors due to that.
7523 */
50a0bc90 7524 if (IS_I830(dev_priv))
1c4e0274
VS
7525 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
7526
8bcc2795
DV
7527 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
7528 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
7529 } else {
7530 /* Mask out read-only status bits. */
7531 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
7532 DPLL_PORTC_READY_MASK |
7533 DPLL_PORTB_READY_MASK);
8bcc2795 7534 }
6c49f241 7535
920a14b2 7536 if (IS_CHERRYVIEW(dev_priv))
70b23a98 7537 chv_crtc_clock_get(crtc, pipe_config);
11a914c2 7538 else if (IS_VALLEYVIEW(dev_priv))
acbec814
JB
7539 vlv_crtc_clock_get(crtc, pipe_config);
7540 else
7541 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 7542
0f64614d
VS
7543 /*
7544 * Normally the dotclock is filled in by the encoder .get_config()
7545 * but in case the pipe is enabled w/o any ports we need a sane
7546 * default.
7547 */
7548 pipe_config->base.adjusted_mode.crtc_clock =
7549 pipe_config->port_clock / pipe_config->pixel_multiplier;
7550
1729050e
ID
7551 ret = true;
7552
7553out:
7554 intel_display_power_put(dev_priv, power_domain);
7555
7556 return ret;
0e8ffe1b
DV
7557}
7558
c39055b0 7559static void ironlake_init_pch_refclk(struct drm_i915_private *dev_priv)
13d83a67 7560{
13d83a67 7561 struct intel_encoder *encoder;
1c1a24d2 7562 int i;
74cfd7ac 7563 u32 val, final;
13d83a67 7564 bool has_lvds = false;
199e5d79 7565 bool has_cpu_edp = false;
199e5d79 7566 bool has_panel = false;
99eb6a01
KP
7567 bool has_ck505 = false;
7568 bool can_ssc = false;
1c1a24d2 7569 bool using_ssc_source = false;
13d83a67
JB
7570
7571 /* We need to take the global config into account */
c39055b0 7572 for_each_intel_encoder(&dev_priv->drm, encoder) {
199e5d79
KP
7573 switch (encoder->type) {
7574 case INTEL_OUTPUT_LVDS:
7575 has_panel = true;
7576 has_lvds = true;
7577 break;
7578 case INTEL_OUTPUT_EDP:
7579 has_panel = true;
2de6905f 7580 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
7581 has_cpu_edp = true;
7582 break;
6847d71b
PZ
7583 default:
7584 break;
13d83a67
JB
7585 }
7586 }
7587
6e266956 7588 if (HAS_PCH_IBX(dev_priv)) {
41aa3448 7589 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
7590 can_ssc = has_ck505;
7591 } else {
7592 has_ck505 = false;
7593 can_ssc = true;
7594 }
7595
1c1a24d2
L
7596 /* Check if any DPLLs are using the SSC source */
7597 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
7598 u32 temp = I915_READ(PCH_DPLL(i));
7599
7600 if (!(temp & DPLL_VCO_ENABLE))
7601 continue;
7602
7603 if ((temp & PLL_REF_INPUT_MASK) ==
7604 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
7605 using_ssc_source = true;
7606 break;
7607 }
7608 }
7609
7610 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
7611 has_panel, has_lvds, has_ck505, using_ssc_source);
13d83a67
JB
7612
7613 /* Ironlake: try to setup display ref clock before DPLL
7614 * enabling. This is only under driver's control after
7615 * PCH B stepping, previous chipset stepping should be
7616 * ignoring this setting.
7617 */
74cfd7ac
CW
7618 val = I915_READ(PCH_DREF_CONTROL);
7619
7620 /* As we must carefully and slowly disable/enable each source in turn,
7621 * compute the final state we want first and check if we need to
7622 * make any changes at all.
7623 */
7624 final = val;
7625 final &= ~DREF_NONSPREAD_SOURCE_MASK;
7626 if (has_ck505)
7627 final |= DREF_NONSPREAD_CK505_ENABLE;
7628 else
7629 final |= DREF_NONSPREAD_SOURCE_ENABLE;
7630
8c07eb68 7631 final &= ~DREF_SSC_SOURCE_MASK;
74cfd7ac 7632 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8c07eb68 7633 final &= ~DREF_SSC1_ENABLE;
74cfd7ac
CW
7634
7635 if (has_panel) {
7636 final |= DREF_SSC_SOURCE_ENABLE;
7637
7638 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7639 final |= DREF_SSC1_ENABLE;
7640
7641 if (has_cpu_edp) {
7642 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7643 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
7644 else
7645 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
7646 } else
7647 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
1c1a24d2
L
7648 } else if (using_ssc_source) {
7649 final |= DREF_SSC_SOURCE_ENABLE;
7650 final |= DREF_SSC1_ENABLE;
74cfd7ac
CW
7651 }
7652
7653 if (final == val)
7654 return;
7655
13d83a67 7656 /* Always enable nonspread source */
74cfd7ac 7657 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 7658
99eb6a01 7659 if (has_ck505)
74cfd7ac 7660 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 7661 else
74cfd7ac 7662 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 7663
199e5d79 7664 if (has_panel) {
74cfd7ac
CW
7665 val &= ~DREF_SSC_SOURCE_MASK;
7666 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 7667
199e5d79 7668 /* SSC must be turned on before enabling the CPU output */
99eb6a01 7669 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 7670 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 7671 val |= DREF_SSC1_ENABLE;
e77166b5 7672 } else
74cfd7ac 7673 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
7674
7675 /* Get SSC going before enabling the outputs */
74cfd7ac 7676 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
7677 POSTING_READ(PCH_DREF_CONTROL);
7678 udelay(200);
7679
74cfd7ac 7680 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
7681
7682 /* Enable CPU source on CPU attached eDP */
199e5d79 7683 if (has_cpu_edp) {
99eb6a01 7684 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 7685 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 7686 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 7687 } else
74cfd7ac 7688 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 7689 } else
74cfd7ac 7690 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 7691
74cfd7ac 7692 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
7693 POSTING_READ(PCH_DREF_CONTROL);
7694 udelay(200);
7695 } else {
1c1a24d2 7696 DRM_DEBUG_KMS("Disabling CPU source output\n");
199e5d79 7697
74cfd7ac 7698 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
7699
7700 /* Turn off CPU output */
74cfd7ac 7701 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 7702
74cfd7ac 7703 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
7704 POSTING_READ(PCH_DREF_CONTROL);
7705 udelay(200);
7706
1c1a24d2
L
7707 if (!using_ssc_source) {
7708 DRM_DEBUG_KMS("Disabling SSC source\n");
199e5d79 7709
1c1a24d2
L
7710 /* Turn off the SSC source */
7711 val &= ~DREF_SSC_SOURCE_MASK;
7712 val |= DREF_SSC_SOURCE_DISABLE;
f165d283 7713
1c1a24d2
L
7714 /* Turn off SSC1 */
7715 val &= ~DREF_SSC1_ENABLE;
7716
7717 I915_WRITE(PCH_DREF_CONTROL, val);
7718 POSTING_READ(PCH_DREF_CONTROL);
7719 udelay(200);
7720 }
13d83a67 7721 }
74cfd7ac
CW
7722
7723 BUG_ON(val != final);
13d83a67
JB
7724}
7725
f31f2d55 7726static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 7727{
f31f2d55 7728 uint32_t tmp;
dde86e2d 7729
0ff066a9
PZ
7730 tmp = I915_READ(SOUTH_CHICKEN2);
7731 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
7732 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 7733
cf3598c2
ID
7734 if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
7735 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
0ff066a9 7736 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 7737
0ff066a9
PZ
7738 tmp = I915_READ(SOUTH_CHICKEN2);
7739 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
7740 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 7741
cf3598c2
ID
7742 if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
7743 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
0ff066a9 7744 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
7745}
7746
7747/* WaMPhyProgramming:hsw */
7748static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
7749{
7750 uint32_t tmp;
dde86e2d
PZ
7751
7752 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
7753 tmp &= ~(0xFF << 24);
7754 tmp |= (0x12 << 24);
7755 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
7756
dde86e2d
PZ
7757 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
7758 tmp |= (1 << 11);
7759 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
7760
7761 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
7762 tmp |= (1 << 11);
7763 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
7764
dde86e2d
PZ
7765 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
7766 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7767 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
7768
7769 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
7770 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7771 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
7772
0ff066a9
PZ
7773 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
7774 tmp &= ~(7 << 13);
7775 tmp |= (5 << 13);
7776 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 7777
0ff066a9
PZ
7778 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
7779 tmp &= ~(7 << 13);
7780 tmp |= (5 << 13);
7781 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
7782
7783 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
7784 tmp &= ~0xFF;
7785 tmp |= 0x1C;
7786 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
7787
7788 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
7789 tmp &= ~0xFF;
7790 tmp |= 0x1C;
7791 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
7792
7793 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
7794 tmp &= ~(0xFF << 16);
7795 tmp |= (0x1C << 16);
7796 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
7797
7798 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
7799 tmp &= ~(0xFF << 16);
7800 tmp |= (0x1C << 16);
7801 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
7802
0ff066a9
PZ
7803 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
7804 tmp |= (1 << 27);
7805 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 7806
0ff066a9
PZ
7807 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
7808 tmp |= (1 << 27);
7809 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 7810
0ff066a9
PZ
7811 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
7812 tmp &= ~(0xF << 28);
7813 tmp |= (4 << 28);
7814 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 7815
0ff066a9
PZ
7816 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
7817 tmp &= ~(0xF << 28);
7818 tmp |= (4 << 28);
7819 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
7820}
7821
2fa86a1f
PZ
7822/* Implements 3 different sequences from BSpec chapter "Display iCLK
7823 * Programming" based on the parameters passed:
7824 * - Sequence to enable CLKOUT_DP
7825 * - Sequence to enable CLKOUT_DP without spread
7826 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
7827 */
c39055b0
ACO
7828static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
7829 bool with_spread, bool with_fdi)
f31f2d55 7830{
2fa86a1f
PZ
7831 uint32_t reg, tmp;
7832
7833 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
7834 with_spread = true;
4f8036a2
TU
7835 if (WARN(HAS_PCH_LPT_LP(dev_priv) &&
7836 with_fdi, "LP PCH doesn't have FDI\n"))
2fa86a1f 7837 with_fdi = false;
f31f2d55 7838
a580516d 7839 mutex_lock(&dev_priv->sb_lock);
f31f2d55
PZ
7840
7841 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7842 tmp &= ~SBI_SSCCTL_DISABLE;
7843 tmp |= SBI_SSCCTL_PATHALT;
7844 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7845
7846 udelay(24);
7847
2fa86a1f
PZ
7848 if (with_spread) {
7849 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7850 tmp &= ~SBI_SSCCTL_PATHALT;
7851 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 7852
2fa86a1f
PZ
7853 if (with_fdi) {
7854 lpt_reset_fdi_mphy(dev_priv);
7855 lpt_program_fdi_mphy(dev_priv);
7856 }
7857 }
dde86e2d 7858
4f8036a2 7859 reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
2fa86a1f
PZ
7860 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
7861 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
7862 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246 7863
a580516d 7864 mutex_unlock(&dev_priv->sb_lock);
dde86e2d
PZ
7865}
7866
47701c3b 7867/* Sequence to disable CLKOUT_DP */
c39055b0 7868static void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
47701c3b 7869{
47701c3b
PZ
7870 uint32_t reg, tmp;
7871
a580516d 7872 mutex_lock(&dev_priv->sb_lock);
47701c3b 7873
4f8036a2 7874 reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
47701c3b
PZ
7875 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
7876 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
7877 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
7878
7879 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7880 if (!(tmp & SBI_SSCCTL_DISABLE)) {
7881 if (!(tmp & SBI_SSCCTL_PATHALT)) {
7882 tmp |= SBI_SSCCTL_PATHALT;
7883 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7884 udelay(32);
7885 }
7886 tmp |= SBI_SSCCTL_DISABLE;
7887 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7888 }
7889
a580516d 7890 mutex_unlock(&dev_priv->sb_lock);
47701c3b
PZ
7891}
7892
f7be2c21
VS
7893#define BEND_IDX(steps) ((50 + (steps)) / 5)
7894
7895static const uint16_t sscdivintphase[] = {
7896 [BEND_IDX( 50)] = 0x3B23,
7897 [BEND_IDX( 45)] = 0x3B23,
7898 [BEND_IDX( 40)] = 0x3C23,
7899 [BEND_IDX( 35)] = 0x3C23,
7900 [BEND_IDX( 30)] = 0x3D23,
7901 [BEND_IDX( 25)] = 0x3D23,
7902 [BEND_IDX( 20)] = 0x3E23,
7903 [BEND_IDX( 15)] = 0x3E23,
7904 [BEND_IDX( 10)] = 0x3F23,
7905 [BEND_IDX( 5)] = 0x3F23,
7906 [BEND_IDX( 0)] = 0x0025,
7907 [BEND_IDX( -5)] = 0x0025,
7908 [BEND_IDX(-10)] = 0x0125,
7909 [BEND_IDX(-15)] = 0x0125,
7910 [BEND_IDX(-20)] = 0x0225,
7911 [BEND_IDX(-25)] = 0x0225,
7912 [BEND_IDX(-30)] = 0x0325,
7913 [BEND_IDX(-35)] = 0x0325,
7914 [BEND_IDX(-40)] = 0x0425,
7915 [BEND_IDX(-45)] = 0x0425,
7916 [BEND_IDX(-50)] = 0x0525,
7917};
7918
7919/*
7920 * Bend CLKOUT_DP
7921 * steps -50 to 50 inclusive, in steps of 5
7922 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
7923 * change in clock period = -(steps / 10) * 5.787 ps
7924 */
7925static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
7926{
7927 uint32_t tmp;
7928 int idx = BEND_IDX(steps);
7929
7930 if (WARN_ON(steps % 5 != 0))
7931 return;
7932
7933 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
7934 return;
7935
7936 mutex_lock(&dev_priv->sb_lock);
7937
7938 if (steps % 10 != 0)
7939 tmp = 0xAAAAAAAB;
7940 else
7941 tmp = 0x00000000;
7942 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
7943
7944 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
7945 tmp &= 0xffff0000;
7946 tmp |= sscdivintphase[idx];
7947 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
7948
7949 mutex_unlock(&dev_priv->sb_lock);
7950}
7951
7952#undef BEND_IDX
7953
c39055b0 7954static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
bf8fa3d3 7955{
bf8fa3d3
PZ
7956 struct intel_encoder *encoder;
7957 bool has_vga = false;
7958
c39055b0 7959 for_each_intel_encoder(&dev_priv->drm, encoder) {
bf8fa3d3
PZ
7960 switch (encoder->type) {
7961 case INTEL_OUTPUT_ANALOG:
7962 has_vga = true;
7963 break;
6847d71b
PZ
7964 default:
7965 break;
bf8fa3d3
PZ
7966 }
7967 }
7968
f7be2c21 7969 if (has_vga) {
c39055b0
ACO
7970 lpt_bend_clkout_dp(dev_priv, 0);
7971 lpt_enable_clkout_dp(dev_priv, true, true);
f7be2c21 7972 } else {
c39055b0 7973 lpt_disable_clkout_dp(dev_priv);
f7be2c21 7974 }
bf8fa3d3
PZ
7975}
7976
dde86e2d
PZ
7977/*
7978 * Initialize reference clocks when the driver loads
7979 */
c39055b0 7980void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
dde86e2d 7981{
6e266956 7982 if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
c39055b0 7983 ironlake_init_pch_refclk(dev_priv);
6e266956 7984 else if (HAS_PCH_LPT(dev_priv))
c39055b0 7985 lpt_init_pch_refclk(dev_priv);
dde86e2d
PZ
7986}
7987
6ff93609 7988static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 7989{
fac5e23e 7990 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
79e53945
JB
7991 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7992 int pipe = intel_crtc->pipe;
c8203565
PZ
7993 uint32_t val;
7994
78114071 7995 val = 0;
c8203565 7996
6e3c9717 7997 switch (intel_crtc->config->pipe_bpp) {
c8203565 7998 case 18:
dfd07d72 7999 val |= PIPECONF_6BPC;
c8203565
PZ
8000 break;
8001 case 24:
dfd07d72 8002 val |= PIPECONF_8BPC;
c8203565
PZ
8003 break;
8004 case 30:
dfd07d72 8005 val |= PIPECONF_10BPC;
c8203565
PZ
8006 break;
8007 case 36:
dfd07d72 8008 val |= PIPECONF_12BPC;
c8203565
PZ
8009 break;
8010 default:
cc769b62
PZ
8011 /* Case prevented by intel_choose_pipe_bpp_dither. */
8012 BUG();
c8203565
PZ
8013 }
8014
6e3c9717 8015 if (intel_crtc->config->dither)
c8203565
PZ
8016 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8017
6e3c9717 8018 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
8019 val |= PIPECONF_INTERLACED_ILK;
8020 else
8021 val |= PIPECONF_PROGRESSIVE;
8022
6e3c9717 8023 if (intel_crtc->config->limited_color_range)
3685a8f3 8024 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 8025
c8203565
PZ
8026 I915_WRITE(PIPECONF(pipe), val);
8027 POSTING_READ(PIPECONF(pipe));
8028}
8029
6ff93609 8030static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 8031{
fac5e23e 8032 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
ee2b0b38 8033 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 8034 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
391bf048 8035 u32 val = 0;
ee2b0b38 8036
391bf048 8037 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
ee2b0b38
PZ
8038 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8039
6e3c9717 8040 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
8041 val |= PIPECONF_INTERLACED_ILK;
8042 else
8043 val |= PIPECONF_PROGRESSIVE;
8044
702e7a56
PZ
8045 I915_WRITE(PIPECONF(cpu_transcoder), val);
8046 POSTING_READ(PIPECONF(cpu_transcoder));
391bf048
JN
8047}
8048
391bf048
JN
8049static void haswell_set_pipemisc(struct drm_crtc *crtc)
8050{
fac5e23e 8051 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
391bf048 8052 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756f85cf 8053
391bf048
JN
8054 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
8055 u32 val = 0;
756f85cf 8056
6e3c9717 8057 switch (intel_crtc->config->pipe_bpp) {
756f85cf
PZ
8058 case 18:
8059 val |= PIPEMISC_DITHER_6_BPC;
8060 break;
8061 case 24:
8062 val |= PIPEMISC_DITHER_8_BPC;
8063 break;
8064 case 30:
8065 val |= PIPEMISC_DITHER_10_BPC;
8066 break;
8067 case 36:
8068 val |= PIPEMISC_DITHER_12_BPC;
8069 break;
8070 default:
8071 /* Case prevented by pipe_config_set_bpp. */
8072 BUG();
8073 }
8074
6e3c9717 8075 if (intel_crtc->config->dither)
756f85cf
PZ
8076 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8077
391bf048 8078 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
756f85cf 8079 }
ee2b0b38
PZ
8080}
8081
d4b1931c
PZ
8082int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8083{
8084 /*
8085 * Account for spread spectrum to avoid
8086 * oversubscribing the link. Max center spread
8087 * is 2.5%; use 5% for safety's sake.
8088 */
8089 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 8090 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
8091}
8092
7429e9d4 8093static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 8094{
7429e9d4 8095 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
8096}
8097
b75ca6f6
ACO
8098static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
8099 struct intel_crtc_state *crtc_state,
9e2c8475 8100 struct dpll *reduced_clock)
79e53945 8101{
de13a2e3 8102 struct drm_crtc *crtc = &intel_crtc->base;
79e53945 8103 struct drm_device *dev = crtc->dev;
fac5e23e 8104 struct drm_i915_private *dev_priv = to_i915(dev);
b75ca6f6 8105 u32 dpll, fp, fp2;
3d6e9ee0 8106 int factor;
79e53945 8107
c1858123 8108 /* Enable autotuning of the PLL clock (if permissible) */
8febb297 8109 factor = 21;
3d6e9ee0 8110 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8febb297 8111 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 8112 dev_priv->vbt.lvds_ssc_freq == 100000) ||
6e266956 8113 (HAS_PCH_IBX(dev_priv) && intel_is_dual_link_lvds(dev)))
8febb297 8114 factor = 25;
190f68c5 8115 } else if (crtc_state->sdvo_tv_clock)
8febb297 8116 factor = 20;
c1858123 8117
b75ca6f6
ACO
8118 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
8119
190f68c5 8120 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
b75ca6f6
ACO
8121 fp |= FP_CB_TUNE;
8122
8123 if (reduced_clock) {
8124 fp2 = i9xx_dpll_compute_fp(reduced_clock);
2c07245f 8125
b75ca6f6
ACO
8126 if (reduced_clock->m < factor * reduced_clock->n)
8127 fp2 |= FP_CB_TUNE;
8128 } else {
8129 fp2 = fp;
8130 }
9a7c7890 8131
5eddb70b 8132 dpll = 0;
2c07245f 8133
3d6e9ee0 8134 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
a07d6787
EA
8135 dpll |= DPLLB_MODE_LVDS;
8136 else
8137 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 8138
190f68c5 8139 dpll |= (crtc_state->pixel_multiplier - 1)
ef1b460d 8140 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f 8141
3d6e9ee0
VS
8142 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
8143 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
4a33e48d 8144 dpll |= DPLL_SDVO_HIGH_SPEED;
3d6e9ee0 8145
37a5650b 8146 if (intel_crtc_has_dp_encoder(crtc_state))
4a33e48d 8147 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 8148
7d7f8633
VS
8149 /*
8150 * The high speed IO clock is only really required for
8151 * SDVO/HDMI/DP, but we also enable it for CRT to make it
8152 * possible to share the DPLL between CRT and HDMI. Enabling
8153 * the clock needlessly does no real harm, except use up a
8154 * bit of power potentially.
8155 *
8156 * We'll limit this to IVB with 3 pipes, since it has only two
8157 * DPLLs and so DPLL sharing is the only way to get three pipes
8158 * driving PCH ports at the same time. On SNB we could do this,
8159 * and potentially avoid enabling the second DPLL, but it's not
8160 * clear if it''s a win or loss power wise. No point in doing
8161 * this on ILK at all since it has a fixed DPLL<->pipe mapping.
8162 */
8163 if (INTEL_INFO(dev_priv)->num_pipes == 3 &&
8164 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
8165 dpll |= DPLL_SDVO_HIGH_SPEED;
8166
a07d6787 8167 /* compute bitmask from p1 value */
190f68c5 8168 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 8169 /* also FPA1 */
190f68c5 8170 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 8171
190f68c5 8172 switch (crtc_state->dpll.p2) {
a07d6787
EA
8173 case 5:
8174 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8175 break;
8176 case 7:
8177 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8178 break;
8179 case 10:
8180 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8181 break;
8182 case 14:
8183 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8184 break;
79e53945
JB
8185 }
8186
3d6e9ee0
VS
8187 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8188 intel_panel_use_ssc(dev_priv))
43565a06 8189 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
8190 else
8191 dpll |= PLL_REF_INPUT_DREFCLK;
8192
b75ca6f6
ACO
8193 dpll |= DPLL_VCO_ENABLE;
8194
8195 crtc_state->dpll_hw_state.dpll = dpll;
8196 crtc_state->dpll_hw_state.fp0 = fp;
8197 crtc_state->dpll_hw_state.fp1 = fp2;
de13a2e3
PZ
8198}
8199
190f68c5
ACO
8200static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8201 struct intel_crtc_state *crtc_state)
de13a2e3 8202{
997c030c 8203 struct drm_device *dev = crtc->base.dev;
fac5e23e 8204 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 8205 const struct intel_limit *limit;
997c030c 8206 int refclk = 120000;
de13a2e3 8207
dd3cd74a
ACO
8208 memset(&crtc_state->dpll_hw_state, 0,
8209 sizeof(crtc_state->dpll_hw_state));
8210
ded220e2
ACO
8211 crtc->lowfreq_avail = false;
8212
8213 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
8214 if (!crtc_state->has_pch_encoder)
8215 return 0;
79e53945 8216
2d84d2b3 8217 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
997c030c
ACO
8218 if (intel_panel_use_ssc(dev_priv)) {
8219 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
8220 dev_priv->vbt.lvds_ssc_freq);
8221 refclk = dev_priv->vbt.lvds_ssc_freq;
8222 }
8223
8224 if (intel_is_dual_link_lvds(dev)) {
8225 if (refclk == 100000)
8226 limit = &intel_limits_ironlake_dual_lvds_100m;
8227 else
8228 limit = &intel_limits_ironlake_dual_lvds;
8229 } else {
8230 if (refclk == 100000)
8231 limit = &intel_limits_ironlake_single_lvds_100m;
8232 else
8233 limit = &intel_limits_ironlake_single_lvds;
8234 }
8235 } else {
8236 limit = &intel_limits_ironlake_dac;
8237 }
8238
364ee29d 8239 if (!crtc_state->clock_set &&
997c030c
ACO
8240 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8241 refclk, NULL, &crtc_state->dpll)) {
364ee29d
ACO
8242 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8243 return -EINVAL;
f47709a9 8244 }
79e53945 8245
cbaa3315 8246 ironlake_compute_dpll(crtc, crtc_state, NULL);
66e985c0 8247
efd38b68 8248 if (!intel_get_shared_dpll(crtc, crtc_state, NULL)) {
ded220e2
ACO
8249 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
8250 pipe_name(crtc->pipe));
8251 return -EINVAL;
3fb37703 8252 }
79e53945 8253
c8f7a0db 8254 return 0;
79e53945
JB
8255}
8256
eb14cb74
VS
8257static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8258 struct intel_link_m_n *m_n)
8259{
8260 struct drm_device *dev = crtc->base.dev;
fac5e23e 8261 struct drm_i915_private *dev_priv = to_i915(dev);
eb14cb74
VS
8262 enum pipe pipe = crtc->pipe;
8263
8264 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8265 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8266 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8267 & ~TU_SIZE_MASK;
8268 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8269 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8270 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8271}
8272
8273static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8274 enum transcoder transcoder,
b95af8be
VK
8275 struct intel_link_m_n *m_n,
8276 struct intel_link_m_n *m2_n2)
72419203 8277{
6315b5d3 8278 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
eb14cb74 8279 enum pipe pipe = crtc->pipe;
72419203 8280
6315b5d3 8281 if (INTEL_GEN(dev_priv) >= 5) {
eb14cb74
VS
8282 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8283 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8284 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8285 & ~TU_SIZE_MASK;
8286 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8287 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8288 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
b95af8be
VK
8289 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8290 * gen < 8) and if DRRS is supported (to make sure the
8291 * registers are not unnecessarily read).
8292 */
6315b5d3 8293 if (m2_n2 && INTEL_GEN(dev_priv) < 8 &&
6e3c9717 8294 crtc->config->has_drrs) {
b95af8be
VK
8295 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8296 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8297 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8298 & ~TU_SIZE_MASK;
8299 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8300 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8301 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8302 }
eb14cb74
VS
8303 } else {
8304 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8305 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8306 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8307 & ~TU_SIZE_MASK;
8308 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8309 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8310 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8311 }
8312}
8313
8314void intel_dp_get_m_n(struct intel_crtc *crtc,
5cec258b 8315 struct intel_crtc_state *pipe_config)
eb14cb74 8316{
681a8504 8317 if (pipe_config->has_pch_encoder)
eb14cb74
VS
8318 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8319 else
8320 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be
VK
8321 &pipe_config->dp_m_n,
8322 &pipe_config->dp_m2_n2);
eb14cb74 8323}
72419203 8324
eb14cb74 8325static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5cec258b 8326 struct intel_crtc_state *pipe_config)
eb14cb74
VS
8327{
8328 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be 8329 &pipe_config->fdi_m_n, NULL);
72419203
DV
8330}
8331
bd2e244f 8332static void skylake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 8333 struct intel_crtc_state *pipe_config)
bd2e244f
JB
8334{
8335 struct drm_device *dev = crtc->base.dev;
fac5e23e 8336 struct drm_i915_private *dev_priv = to_i915(dev);
a1b2278e
CK
8337 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
8338 uint32_t ps_ctrl = 0;
8339 int id = -1;
8340 int i;
bd2e244f 8341
a1b2278e
CK
8342 /* find scaler attached to this pipe */
8343 for (i = 0; i < crtc->num_scalers; i++) {
8344 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
8345 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
8346 id = i;
8347 pipe_config->pch_pfit.enabled = true;
8348 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
8349 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
8350 break;
8351 }
8352 }
bd2e244f 8353
a1b2278e
CK
8354 scaler_state->scaler_id = id;
8355 if (id >= 0) {
8356 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
8357 } else {
8358 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
bd2e244f
JB
8359 }
8360}
8361
5724dbd1
DL
8362static void
8363skylake_get_initial_plane_config(struct intel_crtc *crtc,
8364 struct intel_initial_plane_config *plane_config)
bc8d7dff
DL
8365{
8366 struct drm_device *dev = crtc->base.dev;
fac5e23e 8367 struct drm_i915_private *dev_priv = to_i915(dev);
40f46283 8368 u32 val, base, offset, stride_mult, tiling;
bc8d7dff
DL
8369 int pipe = crtc->pipe;
8370 int fourcc, pixel_format;
6761dd31 8371 unsigned int aligned_height;
bc8d7dff 8372 struct drm_framebuffer *fb;
1b842c89 8373 struct intel_framebuffer *intel_fb;
bc8d7dff 8374
d9806c9f 8375 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 8376 if (!intel_fb) {
bc8d7dff
DL
8377 DRM_DEBUG_KMS("failed to alloc fb\n");
8378 return;
8379 }
8380
1b842c89
DL
8381 fb = &intel_fb->base;
8382
d2e9f5fc
VS
8383 fb->dev = dev;
8384
bc8d7dff 8385 val = I915_READ(PLANE_CTL(pipe, 0));
42a7b088
DL
8386 if (!(val & PLANE_CTL_ENABLE))
8387 goto error;
8388
bc8d7dff
DL
8389 pixel_format = val & PLANE_CTL_FORMAT_MASK;
8390 fourcc = skl_format_to_fourcc(pixel_format,
8391 val & PLANE_CTL_ORDER_RGBX,
8392 val & PLANE_CTL_ALPHA_MASK);
2f3f4763 8393 fb->format = drm_format_info(fourcc);
bc8d7dff 8394
40f46283
DL
8395 tiling = val & PLANE_CTL_TILED_MASK;
8396 switch (tiling) {
8397 case PLANE_CTL_TILED_LINEAR:
2f075565 8398 fb->modifier = DRM_FORMAT_MOD_LINEAR;
40f46283
DL
8399 break;
8400 case PLANE_CTL_TILED_X:
8401 plane_config->tiling = I915_TILING_X;
bae781b2 8402 fb->modifier = I915_FORMAT_MOD_X_TILED;
40f46283
DL
8403 break;
8404 case PLANE_CTL_TILED_Y:
bae781b2 8405 fb->modifier = I915_FORMAT_MOD_Y_TILED;
40f46283
DL
8406 break;
8407 case PLANE_CTL_TILED_YF:
bae781b2 8408 fb->modifier = I915_FORMAT_MOD_Yf_TILED;
40f46283
DL
8409 break;
8410 default:
8411 MISSING_CASE(tiling);
8412 goto error;
8413 }
8414
bc8d7dff
DL
8415 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
8416 plane_config->base = base;
8417
8418 offset = I915_READ(PLANE_OFFSET(pipe, 0));
8419
8420 val = I915_READ(PLANE_SIZE(pipe, 0));
8421 fb->height = ((val >> 16) & 0xfff) + 1;
8422 fb->width = ((val >> 0) & 0x1fff) + 1;
8423
8424 val = I915_READ(PLANE_STRIDE(pipe, 0));
d88c4afd 8425 stride_mult = intel_fb_stride_alignment(fb, 0);
bc8d7dff
DL
8426 fb->pitches[0] = (val & 0x3ff) * stride_mult;
8427
d88c4afd 8428 aligned_height = intel_fb_align_height(fb, 0, fb->height);
bc8d7dff 8429
f37b5c2b 8430 plane_config->size = fb->pitches[0] * aligned_height;
bc8d7dff
DL
8431
8432 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8433 pipe_name(pipe), fb->width, fb->height,
272725c7 8434 fb->format->cpp[0] * 8, base, fb->pitches[0],
bc8d7dff
DL
8435 plane_config->size);
8436
2d14030b 8437 plane_config->fb = intel_fb;
bc8d7dff
DL
8438 return;
8439
8440error:
d1a3a036 8441 kfree(intel_fb);
bc8d7dff
DL
8442}
8443
2fa2fe9a 8444static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 8445 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
8446{
8447 struct drm_device *dev = crtc->base.dev;
fac5e23e 8448 struct drm_i915_private *dev_priv = to_i915(dev);
2fa2fe9a
DV
8449 uint32_t tmp;
8450
8451 tmp = I915_READ(PF_CTL(crtc->pipe));
8452
8453 if (tmp & PF_ENABLE) {
fd4daa9c 8454 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
DV
8455 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
8456 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
DV
8457
8458 /* We currently do not free assignements of panel fitters on
8459 * ivb/hsw (since we don't use the higher upscaling modes which
8460 * differentiates them) so just WARN about this case for now. */
5db94019 8461 if (IS_GEN7(dev_priv)) {
cb8b2a30
DV
8462 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
8463 PF_PIPE_SEL_IVB(crtc->pipe));
8464 }
2fa2fe9a 8465 }
79e53945
JB
8466}
8467
5724dbd1
DL
8468static void
8469ironlake_get_initial_plane_config(struct intel_crtc *crtc,
8470 struct intel_initial_plane_config *plane_config)
4c6baa59
JB
8471{
8472 struct drm_device *dev = crtc->base.dev;
fac5e23e 8473 struct drm_i915_private *dev_priv = to_i915(dev);
4c6baa59 8474 u32 val, base, offset;
aeee5a49 8475 int pipe = crtc->pipe;
4c6baa59 8476 int fourcc, pixel_format;
6761dd31 8477 unsigned int aligned_height;
b113d5ee 8478 struct drm_framebuffer *fb;
1b842c89 8479 struct intel_framebuffer *intel_fb;
4c6baa59 8480
42a7b088
DL
8481 val = I915_READ(DSPCNTR(pipe));
8482 if (!(val & DISPLAY_PLANE_ENABLE))
8483 return;
8484
d9806c9f 8485 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 8486 if (!intel_fb) {
4c6baa59
JB
8487 DRM_DEBUG_KMS("failed to alloc fb\n");
8488 return;
8489 }
8490
1b842c89
DL
8491 fb = &intel_fb->base;
8492
d2e9f5fc
VS
8493 fb->dev = dev;
8494
6315b5d3 8495 if (INTEL_GEN(dev_priv) >= 4) {
18c5247e 8496 if (val & DISPPLANE_TILED) {
49af449b 8497 plane_config->tiling = I915_TILING_X;
bae781b2 8498 fb->modifier = I915_FORMAT_MOD_X_TILED;
18c5247e
DV
8499 }
8500 }
4c6baa59
JB
8501
8502 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 8503 fourcc = i9xx_format_to_fourcc(pixel_format);
2f3f4763 8504 fb->format = drm_format_info(fourcc);
4c6baa59 8505
aeee5a49 8506 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
8652744b 8507 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
aeee5a49 8508 offset = I915_READ(DSPOFFSET(pipe));
4c6baa59 8509 } else {
49af449b 8510 if (plane_config->tiling)
aeee5a49 8511 offset = I915_READ(DSPTILEOFF(pipe));
4c6baa59 8512 else
aeee5a49 8513 offset = I915_READ(DSPLINOFF(pipe));
4c6baa59
JB
8514 }
8515 plane_config->base = base;
8516
8517 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
8518 fb->width = ((val >> 16) & 0xfff) + 1;
8519 fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
8520
8521 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 8522 fb->pitches[0] = val & 0xffffffc0;
4c6baa59 8523
d88c4afd 8524 aligned_height = intel_fb_align_height(fb, 0, fb->height);
4c6baa59 8525
f37b5c2b 8526 plane_config->size = fb->pitches[0] * aligned_height;
4c6baa59 8527
2844a921
DL
8528 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8529 pipe_name(pipe), fb->width, fb->height,
272725c7 8530 fb->format->cpp[0] * 8, base, fb->pitches[0],
2844a921 8531 plane_config->size);
b113d5ee 8532
2d14030b 8533 plane_config->fb = intel_fb;
4c6baa59
JB
8534}
8535
0e8ffe1b 8536static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5cec258b 8537 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
8538{
8539 struct drm_device *dev = crtc->base.dev;
fac5e23e 8540 struct drm_i915_private *dev_priv = to_i915(dev);
1729050e 8541 enum intel_display_power_domain power_domain;
0e8ffe1b 8542 uint32_t tmp;
1729050e 8543 bool ret;
0e8ffe1b 8544
1729050e
ID
8545 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8546 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
930e8c9e
PZ
8547 return false;
8548
e143a21c 8549 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 8550 pipe_config->shared_dpll = NULL;
eccb140b 8551
1729050e 8552 ret = false;
0e8ffe1b
DV
8553 tmp = I915_READ(PIPECONF(crtc->pipe));
8554 if (!(tmp & PIPECONF_ENABLE))
1729050e 8555 goto out;
0e8ffe1b 8556
42571aef
VS
8557 switch (tmp & PIPECONF_BPC_MASK) {
8558 case PIPECONF_6BPC:
8559 pipe_config->pipe_bpp = 18;
8560 break;
8561 case PIPECONF_8BPC:
8562 pipe_config->pipe_bpp = 24;
8563 break;
8564 case PIPECONF_10BPC:
8565 pipe_config->pipe_bpp = 30;
8566 break;
8567 case PIPECONF_12BPC:
8568 pipe_config->pipe_bpp = 36;
8569 break;
8570 default:
8571 break;
8572 }
8573
b5a9fa09
DV
8574 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
8575 pipe_config->limited_color_range = true;
8576
ab9412ba 8577 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0 8578 struct intel_shared_dpll *pll;
8106ddbd 8579 enum intel_dpll_id pll_id;
66e985c0 8580
88adfff1
DV
8581 pipe_config->has_pch_encoder = true;
8582
627eb5a3
DV
8583 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
8584 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
8585 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
8586
8587 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 8588
2d1fe073 8589 if (HAS_PCH_IBX(dev_priv)) {
d9a7bc67
ID
8590 /*
8591 * The pipe->pch transcoder and pch transcoder->pll
8592 * mapping is fixed.
8593 */
8106ddbd 8594 pll_id = (enum intel_dpll_id) crtc->pipe;
c0d43d62
DV
8595 } else {
8596 tmp = I915_READ(PCH_DPLL_SEL);
8597 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
8106ddbd 8598 pll_id = DPLL_ID_PCH_PLL_B;
c0d43d62 8599 else
8106ddbd 8600 pll_id= DPLL_ID_PCH_PLL_A;
c0d43d62 8601 }
66e985c0 8602
8106ddbd
ACO
8603 pipe_config->shared_dpll =
8604 intel_get_shared_dpll_by_id(dev_priv, pll_id);
8605 pll = pipe_config->shared_dpll;
66e985c0 8606
2edd6443
ACO
8607 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
8608 &pipe_config->dpll_hw_state));
c93f54cf
DV
8609
8610 tmp = pipe_config->dpll_hw_state.dpll;
8611 pipe_config->pixel_multiplier =
8612 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
8613 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
8614
8615 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
DV
8616 } else {
8617 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
8618 }
8619
1bd1bd80 8620 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 8621 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 8622
2fa2fe9a
DV
8623 ironlake_get_pfit_config(crtc, pipe_config);
8624
1729050e
ID
8625 ret = true;
8626
8627out:
8628 intel_display_power_put(dev_priv, power_domain);
8629
8630 return ret;
0e8ffe1b
DV
8631}
8632
be256dc7
PZ
8633static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
8634{
91c8a326 8635 struct drm_device *dev = &dev_priv->drm;
be256dc7 8636 struct intel_crtc *crtc;
be256dc7 8637
d3fcc808 8638 for_each_intel_crtc(dev, crtc)
e2c719b7 8639 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
8640 pipe_name(crtc->pipe));
8641
e2c719b7
RC
8642 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
8643 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
01403de3
VS
8644 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
8645 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
44cb734c 8646 I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
e2c719b7 8647 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
be256dc7 8648 "CPU PWM1 enabled\n");
772c2a51 8649 if (IS_HASWELL(dev_priv))
e2c719b7 8650 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
c5107b87 8651 "CPU PWM2 enabled\n");
e2c719b7 8652 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
be256dc7 8653 "PCH PWM1 enabled\n");
e2c719b7 8654 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
be256dc7 8655 "Utility pin enabled\n");
e2c719b7 8656 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
be256dc7 8657
9926ada1
PZ
8658 /*
8659 * In theory we can still leave IRQs enabled, as long as only the HPD
8660 * interrupts remain enabled. We used to check for that, but since it's
8661 * gen-specific and since we only disable LCPLL after we fully disable
8662 * the interrupts, the check below should be enough.
8663 */
e2c719b7 8664 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
be256dc7
PZ
8665}
8666
9ccd5aeb
PZ
8667static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
8668{
772c2a51 8669 if (IS_HASWELL(dev_priv))
9ccd5aeb
PZ
8670 return I915_READ(D_COMP_HSW);
8671 else
8672 return I915_READ(D_COMP_BDW);
8673}
8674
3c4c9b81
PZ
8675static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
8676{
772c2a51 8677 if (IS_HASWELL(dev_priv)) {
3c4c9b81
PZ
8678 mutex_lock(&dev_priv->rps.hw_lock);
8679 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
8680 val))
79cf219a 8681 DRM_DEBUG_KMS("Failed to write to D_COMP\n");
3c4c9b81
PZ
8682 mutex_unlock(&dev_priv->rps.hw_lock);
8683 } else {
9ccd5aeb
PZ
8684 I915_WRITE(D_COMP_BDW, val);
8685 POSTING_READ(D_COMP_BDW);
3c4c9b81 8686 }
be256dc7
PZ
8687}
8688
8689/*
8690 * This function implements pieces of two sequences from BSpec:
8691 * - Sequence for display software to disable LCPLL
8692 * - Sequence for display software to allow package C8+
8693 * The steps implemented here are just the steps that actually touch the LCPLL
8694 * register. Callers should take care of disabling all the display engine
8695 * functions, doing the mode unset, fixing interrupts, etc.
8696 */
6ff58d53
PZ
8697static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
8698 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
8699{
8700 uint32_t val;
8701
8702 assert_can_disable_lcpll(dev_priv);
8703
8704 val = I915_READ(LCPLL_CTL);
8705
8706 if (switch_to_fclk) {
8707 val |= LCPLL_CD_SOURCE_FCLK;
8708 I915_WRITE(LCPLL_CTL, val);
8709
f53dd63f
ID
8710 if (wait_for_us(I915_READ(LCPLL_CTL) &
8711 LCPLL_CD_SOURCE_FCLK_DONE, 1))
be256dc7
PZ
8712 DRM_ERROR("Switching to FCLK failed\n");
8713
8714 val = I915_READ(LCPLL_CTL);
8715 }
8716
8717 val |= LCPLL_PLL_DISABLE;
8718 I915_WRITE(LCPLL_CTL, val);
8719 POSTING_READ(LCPLL_CTL);
8720
24d8441d 8721 if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
be256dc7
PZ
8722 DRM_ERROR("LCPLL still locked\n");
8723
9ccd5aeb 8724 val = hsw_read_dcomp(dev_priv);
be256dc7 8725 val |= D_COMP_COMP_DISABLE;
3c4c9b81 8726 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
8727 ndelay(100);
8728
9ccd5aeb
PZ
8729 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
8730 1))
be256dc7
PZ
8731 DRM_ERROR("D_COMP RCOMP still in progress\n");
8732
8733 if (allow_power_down) {
8734 val = I915_READ(LCPLL_CTL);
8735 val |= LCPLL_POWER_DOWN_ALLOW;
8736 I915_WRITE(LCPLL_CTL, val);
8737 POSTING_READ(LCPLL_CTL);
8738 }
8739}
8740
8741/*
8742 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
8743 * source.
8744 */
6ff58d53 8745static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
8746{
8747 uint32_t val;
8748
8749 val = I915_READ(LCPLL_CTL);
8750
8751 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
8752 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
8753 return;
8754
a8a8bd54
PZ
8755 /*
8756 * Make sure we're not on PC8 state before disabling PC8, otherwise
8757 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
a8a8bd54 8758 */
59bad947 8759 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
215733fa 8760
be256dc7
PZ
8761 if (val & LCPLL_POWER_DOWN_ALLOW) {
8762 val &= ~LCPLL_POWER_DOWN_ALLOW;
8763 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 8764 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
8765 }
8766
9ccd5aeb 8767 val = hsw_read_dcomp(dev_priv);
be256dc7
PZ
8768 val |= D_COMP_COMP_FORCE;
8769 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 8770 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
8771
8772 val = I915_READ(LCPLL_CTL);
8773 val &= ~LCPLL_PLL_DISABLE;
8774 I915_WRITE(LCPLL_CTL, val);
8775
93220c08
CW
8776 if (intel_wait_for_register(dev_priv,
8777 LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
8778 5))
be256dc7
PZ
8779 DRM_ERROR("LCPLL not locked yet\n");
8780
8781 if (val & LCPLL_CD_SOURCE_FCLK) {
8782 val = I915_READ(LCPLL_CTL);
8783 val &= ~LCPLL_CD_SOURCE_FCLK;
8784 I915_WRITE(LCPLL_CTL, val);
8785
f53dd63f
ID
8786 if (wait_for_us((I915_READ(LCPLL_CTL) &
8787 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
be256dc7
PZ
8788 DRM_ERROR("Switching back to LCPLL failed\n");
8789 }
215733fa 8790
59bad947 8791 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4c75b940 8792 intel_update_cdclk(dev_priv);
be256dc7
PZ
8793}
8794
765dab67
PZ
8795/*
8796 * Package states C8 and deeper are really deep PC states that can only be
8797 * reached when all the devices on the system allow it, so even if the graphics
8798 * device allows PC8+, it doesn't mean the system will actually get to these
8799 * states. Our driver only allows PC8+ when going into runtime PM.
8800 *
8801 * The requirements for PC8+ are that all the outputs are disabled, the power
8802 * well is disabled and most interrupts are disabled, and these are also
8803 * requirements for runtime PM. When these conditions are met, we manually do
8804 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
8805 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
8806 * hang the machine.
8807 *
8808 * When we really reach PC8 or deeper states (not just when we allow it) we lose
8809 * the state of some registers, so when we come back from PC8+ we need to
8810 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
8811 * need to take care of the registers kept by RC6. Notice that this happens even
8812 * if we don't put the device in PCI D3 state (which is what currently happens
8813 * because of the runtime PM support).
8814 *
8815 * For more, read "Display Sequences for Package C8" on the hardware
8816 * documentation.
8817 */
a14cb6fc 8818void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 8819{
c67a470b
PZ
8820 uint32_t val;
8821
c67a470b
PZ
8822 DRM_DEBUG_KMS("Enabling package C8+\n");
8823
4f8036a2 8824 if (HAS_PCH_LPT_LP(dev_priv)) {
c67a470b
PZ
8825 val = I915_READ(SOUTH_DSPCLK_GATE_D);
8826 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
8827 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
8828 }
8829
c39055b0 8830 lpt_disable_clkout_dp(dev_priv);
c67a470b
PZ
8831 hsw_disable_lcpll(dev_priv, true, true);
8832}
8833
a14cb6fc 8834void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b 8835{
c67a470b
PZ
8836 uint32_t val;
8837
c67a470b
PZ
8838 DRM_DEBUG_KMS("Disabling package C8+\n");
8839
8840 hsw_restore_lcpll(dev_priv);
c39055b0 8841 lpt_init_pch_refclk(dev_priv);
c67a470b 8842
4f8036a2 8843 if (HAS_PCH_LPT_LP(dev_priv)) {
c67a470b
PZ
8844 val = I915_READ(SOUTH_DSPCLK_GATE_D);
8845 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
8846 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
8847 }
c67a470b
PZ
8848}
8849
190f68c5
ACO
8850static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
8851 struct intel_crtc_state *crtc_state)
09b4ddf9 8852{
d7edc4e5 8853 if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
44a126ba
PZ
8854 struct intel_encoder *encoder =
8855 intel_ddi_get_crtc_new_encoder(crtc_state);
8856
8857 if (!intel_get_shared_dpll(crtc, crtc_state, encoder)) {
8858 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
8859 pipe_name(crtc->pipe));
af3997b5 8860 return -EINVAL;
44a126ba 8861 }
af3997b5 8862 }
716c2e55 8863
c7653199 8864 crtc->lowfreq_avail = false;
644cef34 8865
c8f7a0db 8866 return 0;
79e53945
JB
8867}
8868
3760b59c
S
8869static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
8870 enum port port,
8871 struct intel_crtc_state *pipe_config)
8872{
8106ddbd
ACO
8873 enum intel_dpll_id id;
8874
3760b59c
S
8875 switch (port) {
8876 case PORT_A:
08250c4b 8877 id = DPLL_ID_SKL_DPLL0;
3760b59c
S
8878 break;
8879 case PORT_B:
08250c4b 8880 id = DPLL_ID_SKL_DPLL1;
3760b59c
S
8881 break;
8882 case PORT_C:
08250c4b 8883 id = DPLL_ID_SKL_DPLL2;
3760b59c
S
8884 break;
8885 default:
8886 DRM_ERROR("Incorrect port type\n");
8106ddbd 8887 return;
3760b59c 8888 }
8106ddbd
ACO
8889
8890 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
3760b59c
S
8891}
8892
96b7dfb7
S
8893static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
8894 enum port port,
5cec258b 8895 struct intel_crtc_state *pipe_config)
96b7dfb7 8896{
8106ddbd 8897 enum intel_dpll_id id;
a3c988ea 8898 u32 temp;
96b7dfb7
S
8899
8900 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
c856052a 8901 id = temp >> (port * 3 + 1);
96b7dfb7 8902
c856052a 8903 if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL3))
8106ddbd 8904 return;
8106ddbd
ACO
8905
8906 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
96b7dfb7
S
8907}
8908
7d2c8175
DL
8909static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
8910 enum port port,
5cec258b 8911 struct intel_crtc_state *pipe_config)
7d2c8175 8912{
8106ddbd 8913 enum intel_dpll_id id;
c856052a 8914 uint32_t ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
8106ddbd 8915
c856052a 8916 switch (ddi_pll_sel) {
7d2c8175 8917 case PORT_CLK_SEL_WRPLL1:
8106ddbd 8918 id = DPLL_ID_WRPLL1;
7d2c8175
DL
8919 break;
8920 case PORT_CLK_SEL_WRPLL2:
8106ddbd 8921 id = DPLL_ID_WRPLL2;
7d2c8175 8922 break;
00490c22 8923 case PORT_CLK_SEL_SPLL:
8106ddbd 8924 id = DPLL_ID_SPLL;
79bd23da 8925 break;
9d16da65
ACO
8926 case PORT_CLK_SEL_LCPLL_810:
8927 id = DPLL_ID_LCPLL_810;
8928 break;
8929 case PORT_CLK_SEL_LCPLL_1350:
8930 id = DPLL_ID_LCPLL_1350;
8931 break;
8932 case PORT_CLK_SEL_LCPLL_2700:
8933 id = DPLL_ID_LCPLL_2700;
8934 break;
8106ddbd 8935 default:
c856052a 8936 MISSING_CASE(ddi_pll_sel);
8106ddbd
ACO
8937 /* fall through */
8938 case PORT_CLK_SEL_NONE:
8106ddbd 8939 return;
7d2c8175 8940 }
8106ddbd
ACO
8941
8942 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
7d2c8175
DL
8943}
8944
cf30429e
JN
8945static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
8946 struct intel_crtc_state *pipe_config,
d8fc70b7 8947 u64 *power_domain_mask)
cf30429e
JN
8948{
8949 struct drm_device *dev = crtc->base.dev;
fac5e23e 8950 struct drm_i915_private *dev_priv = to_i915(dev);
cf30429e
JN
8951 enum intel_display_power_domain power_domain;
8952 u32 tmp;
8953
d9a7bc67
ID
8954 /*
8955 * The pipe->transcoder mapping is fixed with the exception of the eDP
8956 * transcoder handled below.
8957 */
cf30429e
JN
8958 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8959
8960 /*
8961 * XXX: Do intel_display_power_get_if_enabled before reading this (for
8962 * consistency and less surprising code; it's in always on power).
8963 */
8964 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
8965 if (tmp & TRANS_DDI_FUNC_ENABLE) {
8966 enum pipe trans_edp_pipe;
8967 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
8968 default:
8969 WARN(1, "unknown pipe linked to edp transcoder\n");
8970 case TRANS_DDI_EDP_INPUT_A_ONOFF:
8971 case TRANS_DDI_EDP_INPUT_A_ON:
8972 trans_edp_pipe = PIPE_A;
8973 break;
8974 case TRANS_DDI_EDP_INPUT_B_ONOFF:
8975 trans_edp_pipe = PIPE_B;
8976 break;
8977 case TRANS_DDI_EDP_INPUT_C_ONOFF:
8978 trans_edp_pipe = PIPE_C;
8979 break;
8980 }
8981
8982 if (trans_edp_pipe == crtc->pipe)
8983 pipe_config->cpu_transcoder = TRANSCODER_EDP;
8984 }
8985
8986 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
8987 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
8988 return false;
d8fc70b7 8989 *power_domain_mask |= BIT_ULL(power_domain);
cf30429e
JN
8990
8991 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
8992
8993 return tmp & PIPECONF_ENABLE;
8994}
8995
4d1de975
JN
8996static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
8997 struct intel_crtc_state *pipe_config,
d8fc70b7 8998 u64 *power_domain_mask)
4d1de975
JN
8999{
9000 struct drm_device *dev = crtc->base.dev;
fac5e23e 9001 struct drm_i915_private *dev_priv = to_i915(dev);
4d1de975
JN
9002 enum intel_display_power_domain power_domain;
9003 enum port port;
9004 enum transcoder cpu_transcoder;
9005 u32 tmp;
9006
4d1de975
JN
9007 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
9008 if (port == PORT_A)
9009 cpu_transcoder = TRANSCODER_DSI_A;
9010 else
9011 cpu_transcoder = TRANSCODER_DSI_C;
9012
9013 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
9014 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9015 continue;
d8fc70b7 9016 *power_domain_mask |= BIT_ULL(power_domain);
4d1de975 9017
db18b6a6
ID
9018 /*
9019 * The PLL needs to be enabled with a valid divider
9020 * configuration, otherwise accessing DSI registers will hang
9021 * the machine. See BSpec North Display Engine
9022 * registers/MIPI[BXT]. We can break out here early, since we
9023 * need the same DSI PLL to be enabled for both DSI ports.
9024 */
9025 if (!intel_dsi_pll_is_enabled(dev_priv))
9026 break;
9027
4d1de975
JN
9028 /* XXX: this works for video mode only */
9029 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
9030 if (!(tmp & DPI_ENABLE))
9031 continue;
9032
9033 tmp = I915_READ(MIPI_CTRL(port));
9034 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
9035 continue;
9036
9037 pipe_config->cpu_transcoder = cpu_transcoder;
4d1de975
JN
9038 break;
9039 }
9040
d7edc4e5 9041 return transcoder_is_dsi(pipe_config->cpu_transcoder);
4d1de975
JN
9042}
9043
26804afd 9044static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
5cec258b 9045 struct intel_crtc_state *pipe_config)
26804afd 9046{
6315b5d3 9047 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
d452c5b6 9048 struct intel_shared_dpll *pll;
26804afd
DV
9049 enum port port;
9050 uint32_t tmp;
9051
9052 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9053
9054 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9055
b976dc53 9056 if (IS_GEN9_BC(dev_priv))
96b7dfb7 9057 skylake_get_ddi_pll(dev_priv, port, pipe_config);
cc3f90f0 9058 else if (IS_GEN9_LP(dev_priv))
3760b59c 9059 bxt_get_ddi_pll(dev_priv, port, pipe_config);
96b7dfb7
S
9060 else
9061 haswell_get_ddi_pll(dev_priv, port, pipe_config);
9cd86933 9062
8106ddbd
ACO
9063 pll = pipe_config->shared_dpll;
9064 if (pll) {
2edd6443
ACO
9065 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9066 &pipe_config->dpll_hw_state));
d452c5b6
DV
9067 }
9068
26804afd
DV
9069 /*
9070 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9071 * DDI E. So just check whether this pipe is wired to DDI E and whether
9072 * the PCH transcoder is on.
9073 */
6315b5d3 9074 if (INTEL_GEN(dev_priv) < 9 &&
ca370455 9075 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
26804afd
DV
9076 pipe_config->has_pch_encoder = true;
9077
9078 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9079 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9080 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9081
9082 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9083 }
9084}
9085
0e8ffe1b 9086static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9087 struct intel_crtc_state *pipe_config)
0e8ffe1b 9088{
6315b5d3 9089 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1729050e 9090 enum intel_display_power_domain power_domain;
d8fc70b7 9091 u64 power_domain_mask;
cf30429e 9092 bool active;
0e8ffe1b 9093
1729050e
ID
9094 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9095 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0 9096 return false;
d8fc70b7 9097 power_domain_mask = BIT_ULL(power_domain);
1729050e 9098
8106ddbd 9099 pipe_config->shared_dpll = NULL;
c0d43d62 9100
cf30429e 9101 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
eccb140b 9102
cc3f90f0 9103 if (IS_GEN9_LP(dev_priv) &&
d7edc4e5
VS
9104 bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
9105 WARN_ON(active);
9106 active = true;
4d1de975
JN
9107 }
9108
cf30429e 9109 if (!active)
1729050e 9110 goto out;
0e8ffe1b 9111
d7edc4e5 9112 if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
4d1de975
JN
9113 haswell_get_ddi_port_state(crtc, pipe_config);
9114 intel_get_pipe_timings(crtc, pipe_config);
9115 }
627eb5a3 9116
bc58be60 9117 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 9118
05dc698c
LL
9119 pipe_config->gamma_mode =
9120 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
9121
6315b5d3 9122 if (INTEL_GEN(dev_priv) >= 9) {
1c74eeaf 9123 intel_crtc_init_scalers(crtc, pipe_config);
a1b2278e 9124
af99ceda
CK
9125 pipe_config->scaler_state.scaler_id = -1;
9126 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9127 }
9128
1729050e
ID
9129 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
9130 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
d8fc70b7 9131 power_domain_mask |= BIT_ULL(power_domain);
6315b5d3 9132 if (INTEL_GEN(dev_priv) >= 9)
bd2e244f 9133 skylake_get_pfit_config(crtc, pipe_config);
ff6d9f55 9134 else
1c132b44 9135 ironlake_get_pfit_config(crtc, pipe_config);
bd2e244f 9136 }
88adfff1 9137
772c2a51 9138 if (IS_HASWELL(dev_priv))
e59150dc
JB
9139 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9140 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 9141
4d1de975
JN
9142 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
9143 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
ebb69c95
CT
9144 pipe_config->pixel_multiplier =
9145 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9146 } else {
9147 pipe_config->pixel_multiplier = 1;
9148 }
6c49f241 9149
1729050e
ID
9150out:
9151 for_each_power_domain(power_domain, power_domain_mask)
9152 intel_display_power_put(dev_priv, power_domain);
9153
cf30429e 9154 return active;
0e8ffe1b
DV
9155}
9156
cd5dcbf1 9157static u32 intel_cursor_base(const struct intel_plane_state *plane_state)
1cecc830
VS
9158{
9159 struct drm_i915_private *dev_priv =
9160 to_i915(plane_state->base.plane->dev);
9161 const struct drm_framebuffer *fb = plane_state->base.fb;
9162 const struct drm_i915_gem_object *obj = intel_fb_obj(fb);
9163 u32 base;
9164
9165 if (INTEL_INFO(dev_priv)->cursor_needs_physical)
9166 base = obj->phys_handle->busaddr;
9167 else
9168 base = intel_plane_ggtt_offset(plane_state);
9169
1e7b4fd8
VS
9170 base += plane_state->main.offset;
9171
1cecc830
VS
9172 /* ILK+ do this automagically */
9173 if (HAS_GMCH_DISPLAY(dev_priv) &&
a82256bc 9174 plane_state->base.rotation & DRM_MODE_ROTATE_180)
1cecc830
VS
9175 base += (plane_state->base.crtc_h *
9176 plane_state->base.crtc_w - 1) * fb->format->cpp[0];
9177
9178 return base;
9179}
9180
ed270223
VS
9181static u32 intel_cursor_position(const struct intel_plane_state *plane_state)
9182{
9183 int x = plane_state->base.crtc_x;
9184 int y = plane_state->base.crtc_y;
9185 u32 pos = 0;
9186
9187 if (x < 0) {
9188 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
9189 x = -x;
9190 }
9191 pos |= x << CURSOR_X_SHIFT;
9192
9193 if (y < 0) {
9194 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
9195 y = -y;
9196 }
9197 pos |= y << CURSOR_Y_SHIFT;
9198
9199 return pos;
9200}
9201
3637ecf0
VS
9202static bool intel_cursor_size_ok(const struct intel_plane_state *plane_state)
9203{
9204 const struct drm_mode_config *config =
9205 &plane_state->base.plane->dev->mode_config;
9206 int width = plane_state->base.crtc_w;
9207 int height = plane_state->base.crtc_h;
9208
9209 return width > 0 && width <= config->cursor_width &&
9210 height > 0 && height <= config->cursor_height;
9211}
9212
659056f2
VS
9213static int intel_check_cursor(struct intel_crtc_state *crtc_state,
9214 struct intel_plane_state *plane_state)
9215{
9216 const struct drm_framebuffer *fb = plane_state->base.fb;
1e7b4fd8
VS
9217 int src_x, src_y;
9218 u32 offset;
659056f2
VS
9219 int ret;
9220
9221 ret = drm_plane_helper_check_state(&plane_state->base,
9222 &plane_state->clip,
9223 DRM_PLANE_HELPER_NO_SCALING,
9224 DRM_PLANE_HELPER_NO_SCALING,
9225 true, true);
9226 if (ret)
9227 return ret;
9228
9229 if (!fb)
9230 return 0;
9231
9232 if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
9233 DRM_DEBUG_KMS("cursor cannot be tiled\n");
9234 return -EINVAL;
9235 }
9236
1e7b4fd8
VS
9237 src_x = plane_state->base.src_x >> 16;
9238 src_y = plane_state->base.src_y >> 16;
9239
9240 intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
9241 offset = intel_compute_tile_offset(&src_x, &src_y, plane_state, 0);
9242
9243 if (src_x != 0 || src_y != 0) {
9244 DRM_DEBUG_KMS("Arbitrary cursor panning not supported\n");
9245 return -EINVAL;
9246 }
9247
9248 plane_state->main.offset = offset;
9249
659056f2
VS
9250 return 0;
9251}
9252
292889e1
VS
9253static u32 i845_cursor_ctl(const struct intel_crtc_state *crtc_state,
9254 const struct intel_plane_state *plane_state)
9255{
1e1bb871 9256 const struct drm_framebuffer *fb = plane_state->base.fb;
292889e1 9257
292889e1
VS
9258 return CURSOR_ENABLE |
9259 CURSOR_GAMMA_ENABLE |
9260 CURSOR_FORMAT_ARGB |
1e1bb871 9261 CURSOR_STRIDE(fb->pitches[0]);
292889e1
VS
9262}
9263
659056f2
VS
9264static bool i845_cursor_size_ok(const struct intel_plane_state *plane_state)
9265{
659056f2 9266 int width = plane_state->base.crtc_w;
659056f2
VS
9267
9268 /*
9269 * 845g/865g are only limited by the width of their cursors,
9270 * the height is arbitrary up to the precision of the register.
9271 */
3637ecf0 9272 return intel_cursor_size_ok(plane_state) && IS_ALIGNED(width, 64);
659056f2
VS
9273}
9274
9275static int i845_check_cursor(struct intel_plane *plane,
9276 struct intel_crtc_state *crtc_state,
9277 struct intel_plane_state *plane_state)
9278{
9279 const struct drm_framebuffer *fb = plane_state->base.fb;
659056f2
VS
9280 int ret;
9281
9282 ret = intel_check_cursor(crtc_state, plane_state);
9283 if (ret)
9284 return ret;
9285
9286 /* if we want to turn off the cursor ignore width and height */
1e1bb871 9287 if (!fb)
659056f2
VS
9288 return 0;
9289
9290 /* Check for which cursor types we support */
9291 if (!i845_cursor_size_ok(plane_state)) {
9292 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
9293 plane_state->base.crtc_w,
9294 plane_state->base.crtc_h);
9295 return -EINVAL;
9296 }
9297
1e1bb871 9298 switch (fb->pitches[0]) {
292889e1
VS
9299 case 256:
9300 case 512:
9301 case 1024:
9302 case 2048:
9303 break;
1e1bb871
VS
9304 default:
9305 DRM_DEBUG_KMS("Invalid cursor stride (%u)\n",
9306 fb->pitches[0]);
9307 return -EINVAL;
292889e1
VS
9308 }
9309
659056f2
VS
9310 plane_state->ctl = i845_cursor_ctl(crtc_state, plane_state);
9311
9312 return 0;
292889e1
VS
9313}
9314
b2d03b0d
VS
9315static void i845_update_cursor(struct intel_plane *plane,
9316 const struct intel_crtc_state *crtc_state,
55a08b3f 9317 const struct intel_plane_state *plane_state)
560b85bb 9318{
cd5dcbf1 9319 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
b2d03b0d
VS
9320 u32 cntl = 0, base = 0, pos = 0, size = 0;
9321 unsigned long irqflags;
560b85bb 9322
936e71e3 9323 if (plane_state && plane_state->base.visible) {
55a08b3f
ML
9324 unsigned int width = plane_state->base.crtc_w;
9325 unsigned int height = plane_state->base.crtc_h;
dc41c154 9326
a0864d59 9327 cntl = plane_state->ctl;
dc41c154 9328 size = (height << 12) | width;
560b85bb 9329
b2d03b0d
VS
9330 base = intel_cursor_base(plane_state);
9331 pos = intel_cursor_position(plane_state);
4b0e333e 9332 }
560b85bb 9333
b2d03b0d 9334 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
4726e0b0 9335
e11ffddb
VS
9336 /* On these chipsets we can only modify the base/size/stride
9337 * whilst the cursor is disabled.
9338 */
9339 if (plane->cursor.base != base ||
9340 plane->cursor.size != size ||
9341 plane->cursor.cntl != cntl) {
dd584fc0 9342 I915_WRITE_FW(CURCNTR(PIPE_A), 0);
dd584fc0 9343 I915_WRITE_FW(CURBASE(PIPE_A), base);
dd584fc0 9344 I915_WRITE_FW(CURSIZE, size);
b2d03b0d 9345 I915_WRITE_FW(CURPOS(PIPE_A), pos);
dd584fc0 9346 I915_WRITE_FW(CURCNTR(PIPE_A), cntl);
75343a44 9347
e11ffddb
VS
9348 plane->cursor.base = base;
9349 plane->cursor.size = size;
9350 plane->cursor.cntl = cntl;
9351 } else {
9352 I915_WRITE_FW(CURPOS(PIPE_A), pos);
560b85bb 9353 }
e11ffddb 9354
75343a44 9355 POSTING_READ_FW(CURCNTR(PIPE_A));
b2d03b0d
VS
9356
9357 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
9358}
9359
9360static void i845_disable_cursor(struct intel_plane *plane,
9361 struct intel_crtc *crtc)
9362{
9363 i845_update_cursor(plane, NULL, NULL);
560b85bb
CW
9364}
9365
292889e1
VS
9366static u32 i9xx_cursor_ctl(const struct intel_crtc_state *crtc_state,
9367 const struct intel_plane_state *plane_state)
9368{
9369 struct drm_i915_private *dev_priv =
9370 to_i915(plane_state->base.plane->dev);
9371 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
292889e1
VS
9372 u32 cntl;
9373
9374 cntl = MCURSOR_GAMMA_ENABLE;
9375
9376 if (HAS_DDI(dev_priv))
9377 cntl |= CURSOR_PIPE_CSC_ENABLE;
9378
d509e28b 9379 cntl |= MCURSOR_PIPE_SELECT(crtc->pipe);
292889e1
VS
9380
9381 switch (plane_state->base.crtc_w) {
9382 case 64:
9383 cntl |= CURSOR_MODE_64_ARGB_AX;
9384 break;
9385 case 128:
9386 cntl |= CURSOR_MODE_128_ARGB_AX;
9387 break;
9388 case 256:
9389 cntl |= CURSOR_MODE_256_ARGB_AX;
9390 break;
9391 default:
9392 MISSING_CASE(plane_state->base.crtc_w);
9393 return 0;
9394 }
9395
c2c446ad 9396 if (plane_state->base.rotation & DRM_MODE_ROTATE_180)
292889e1
VS
9397 cntl |= CURSOR_ROTATE_180;
9398
9399 return cntl;
9400}
9401
659056f2 9402static bool i9xx_cursor_size_ok(const struct intel_plane_state *plane_state)
65a21cd6 9403{
024faac7
VS
9404 struct drm_i915_private *dev_priv =
9405 to_i915(plane_state->base.plane->dev);
659056f2
VS
9406 int width = plane_state->base.crtc_w;
9407 int height = plane_state->base.crtc_h;
4b0e333e 9408
3637ecf0 9409 if (!intel_cursor_size_ok(plane_state))
659056f2 9410 return false;
4398ad45 9411
024faac7
VS
9412 /* Cursor width is limited to a few power-of-two sizes */
9413 switch (width) {
659056f2
VS
9414 case 256:
9415 case 128:
659056f2
VS
9416 case 64:
9417 break;
9418 default:
9419 return false;
65a21cd6 9420 }
4b0e333e 9421
024faac7
VS
9422 /*
9423 * IVB+ have CUR_FBC_CTL which allows an arbitrary cursor
9424 * height from 8 lines up to the cursor width, when the
9425 * cursor is not rotated. Everything else requires square
9426 * cursors.
9427 */
9428 if (HAS_CUR_FBC(dev_priv) &&
a82256bc 9429 plane_state->base.rotation & DRM_MODE_ROTATE_0) {
024faac7
VS
9430 if (height < 8 || height > width)
9431 return false;
9432 } else {
9433 if (height != width)
9434 return false;
9435 }
99d1f387 9436
659056f2 9437 return true;
65a21cd6
JB
9438}
9439
659056f2
VS
9440static int i9xx_check_cursor(struct intel_plane *plane,
9441 struct intel_crtc_state *crtc_state,
9442 struct intel_plane_state *plane_state)
cda4b7d3 9443{
659056f2
VS
9444 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
9445 const struct drm_framebuffer *fb = plane_state->base.fb;
659056f2 9446 enum pipe pipe = plane->pipe;
659056f2 9447 int ret;
cda4b7d3 9448
659056f2
VS
9449 ret = intel_check_cursor(crtc_state, plane_state);
9450 if (ret)
9451 return ret;
cda4b7d3 9452
659056f2 9453 /* if we want to turn off the cursor ignore width and height */
1e1bb871 9454 if (!fb)
659056f2 9455 return 0;
55a08b3f 9456
659056f2
VS
9457 /* Check for which cursor types we support */
9458 if (!i9xx_cursor_size_ok(plane_state)) {
9459 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
9460 plane_state->base.crtc_w,
9461 plane_state->base.crtc_h);
9462 return -EINVAL;
cda4b7d3 9463 }
cda4b7d3 9464
1e1bb871
VS
9465 if (fb->pitches[0] != plane_state->base.crtc_w * fb->format->cpp[0]) {
9466 DRM_DEBUG_KMS("Invalid cursor stride (%u) (cursor width %d)\n",
9467 fb->pitches[0], plane_state->base.crtc_w);
9468 return -EINVAL;
659056f2 9469 }
dd584fc0 9470
659056f2
VS
9471 /*
9472 * There's something wrong with the cursor on CHV pipe C.
9473 * If it straddles the left edge of the screen then
9474 * moving it away from the edge or disabling it often
9475 * results in a pipe underrun, and often that can lead to
9476 * dead pipe (constant underrun reported, and it scans
9477 * out just a solid color). To recover from that, the
9478 * display power well must be turned off and on again.
9479 * Refuse the put the cursor into that compromised position.
9480 */
9481 if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_C &&
9482 plane_state->base.visible && plane_state->base.crtc_x < 0) {
9483 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
9484 return -EINVAL;
9485 }
5efb3e28 9486
659056f2 9487 plane_state->ctl = i9xx_cursor_ctl(crtc_state, plane_state);
dd584fc0 9488
659056f2 9489 return 0;
cda4b7d3
CW
9490}
9491
b2d03b0d
VS
9492static void i9xx_update_cursor(struct intel_plane *plane,
9493 const struct intel_crtc_state *crtc_state,
55a08b3f 9494 const struct intel_plane_state *plane_state)
dc41c154 9495{
cd5dcbf1
VS
9496 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
9497 enum pipe pipe = plane->pipe;
024faac7 9498 u32 cntl = 0, base = 0, pos = 0, fbc_ctl = 0;
b2d03b0d 9499 unsigned long irqflags;
dc41c154 9500
b2d03b0d 9501 if (plane_state && plane_state->base.visible) {
a0864d59 9502 cntl = plane_state->ctl;
dc41c154 9503
024faac7
VS
9504 if (plane_state->base.crtc_h != plane_state->base.crtc_w)
9505 fbc_ctl = CUR_FBC_CTL_EN | (plane_state->base.crtc_h - 1);
dc41c154 9506
b2d03b0d
VS
9507 base = intel_cursor_base(plane_state);
9508 pos = intel_cursor_position(plane_state);
9509 }
9510
9511 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
9512
e11ffddb
VS
9513 /*
9514 * On some platforms writing CURCNTR first will also
9515 * cause CURPOS to be armed by the CURBASE write.
9516 * Without the CURCNTR write the CURPOS write would
9517 * arm itself.
9518 *
9519 * CURCNTR and CUR_FBC_CTL are always
9520 * armed by the CURBASE write only.
9521 */
9522 if (plane->cursor.base != base ||
9523 plane->cursor.size != fbc_ctl ||
9524 plane->cursor.cntl != cntl) {
dd584fc0 9525 I915_WRITE_FW(CURCNTR(pipe), cntl);
e11ffddb
VS
9526 if (HAS_CUR_FBC(dev_priv))
9527 I915_WRITE_FW(CUR_FBC_CTL(pipe), fbc_ctl);
b2d03b0d 9528 I915_WRITE_FW(CURPOS(pipe), pos);
75343a44
VS
9529 I915_WRITE_FW(CURBASE(pipe), base);
9530
e11ffddb
VS
9531 plane->cursor.base = base;
9532 plane->cursor.size = fbc_ctl;
9533 plane->cursor.cntl = cntl;
dc41c154 9534 } else {
e11ffddb 9535 I915_WRITE_FW(CURPOS(pipe), pos);
dc41c154
VS
9536 }
9537
dd584fc0 9538 POSTING_READ_FW(CURBASE(pipe));
99d1f387 9539
b2d03b0d 9540 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
65a21cd6
JB
9541}
9542
b2d03b0d
VS
9543static void i9xx_disable_cursor(struct intel_plane *plane,
9544 struct intel_crtc *crtc)
cda4b7d3 9545{
b2d03b0d 9546 i9xx_update_cursor(plane, NULL, NULL);
dc41c154
VS
9547}
9548
dc41c154 9549
79e53945
JB
9550/* VESA 640x480x72Hz mode to set on the pipe */
9551static struct drm_display_mode load_detect_mode = {
9552 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
9553 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
9554};
9555
a8bb6818 9556struct drm_framebuffer *
24dbf51a
CW
9557intel_framebuffer_create(struct drm_i915_gem_object *obj,
9558 struct drm_mode_fb_cmd2 *mode_cmd)
d2dff872
CW
9559{
9560 struct intel_framebuffer *intel_fb;
9561 int ret;
9562
9563 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
dcb1394e 9564 if (!intel_fb)
d2dff872 9565 return ERR_PTR(-ENOMEM);
d2dff872 9566
24dbf51a 9567 ret = intel_framebuffer_init(intel_fb, obj, mode_cmd);
dd4916c5
DV
9568 if (ret)
9569 goto err;
d2dff872
CW
9570
9571 return &intel_fb->base;
dcb1394e 9572
dd4916c5 9573err:
dd4916c5 9574 kfree(intel_fb);
dd4916c5 9575 return ERR_PTR(ret);
d2dff872
CW
9576}
9577
9578static u32
9579intel_framebuffer_pitch_for_width(int width, int bpp)
9580{
9581 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
9582 return ALIGN(pitch, 64);
9583}
9584
9585static u32
9586intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
9587{
9588 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
1267a26b 9589 return PAGE_ALIGN(pitch * mode->vdisplay);
d2dff872
CW
9590}
9591
9592static struct drm_framebuffer *
9593intel_framebuffer_create_for_mode(struct drm_device *dev,
9594 struct drm_display_mode *mode,
9595 int depth, int bpp)
9596{
dcb1394e 9597 struct drm_framebuffer *fb;
d2dff872 9598 struct drm_i915_gem_object *obj;
0fed39bd 9599 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872 9600
12d79d78 9601 obj = i915_gem_object_create(to_i915(dev),
d2dff872 9602 intel_framebuffer_size_for_mode(mode, bpp));
fe3db79b
CW
9603 if (IS_ERR(obj))
9604 return ERR_CAST(obj);
d2dff872
CW
9605
9606 mode_cmd.width = mode->hdisplay;
9607 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
9608 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
9609 bpp);
5ca0c34a 9610 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872 9611
24dbf51a 9612 fb = intel_framebuffer_create(obj, &mode_cmd);
dcb1394e 9613 if (IS_ERR(fb))
f0cd5182 9614 i915_gem_object_put(obj);
dcb1394e
LW
9615
9616 return fb;
d2dff872
CW
9617}
9618
9619static struct drm_framebuffer *
9620mode_fits_in_fbdev(struct drm_device *dev,
9621 struct drm_display_mode *mode)
9622{
0695726e 9623#ifdef CONFIG_DRM_FBDEV_EMULATION
fac5e23e 9624 struct drm_i915_private *dev_priv = to_i915(dev);
d2dff872
CW
9625 struct drm_i915_gem_object *obj;
9626 struct drm_framebuffer *fb;
9627
4c0e5528 9628 if (!dev_priv->fbdev)
d2dff872
CW
9629 return NULL;
9630
4c0e5528 9631 if (!dev_priv->fbdev->fb)
d2dff872
CW
9632 return NULL;
9633
4c0e5528
DV
9634 obj = dev_priv->fbdev->fb->obj;
9635 BUG_ON(!obj);
9636
8bcd4553 9637 fb = &dev_priv->fbdev->fb->base;
01f2c773 9638 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
272725c7 9639 fb->format->cpp[0] * 8))
d2dff872
CW
9640 return NULL;
9641
01f2c773 9642 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
9643 return NULL;
9644
edde3617 9645 drm_framebuffer_reference(fb);
d2dff872 9646 return fb;
4520f53a
DV
9647#else
9648 return NULL;
9649#endif
d2dff872
CW
9650}
9651
d3a40d1b
ACO
9652static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
9653 struct drm_crtc *crtc,
9654 struct drm_display_mode *mode,
9655 struct drm_framebuffer *fb,
9656 int x, int y)
9657{
9658 struct drm_plane_state *plane_state;
9659 int hdisplay, vdisplay;
9660 int ret;
9661
9662 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
9663 if (IS_ERR(plane_state))
9664 return PTR_ERR(plane_state);
9665
9666 if (mode)
196cd5d3 9667 drm_mode_get_hv_timing(mode, &hdisplay, &vdisplay);
d3a40d1b
ACO
9668 else
9669 hdisplay = vdisplay = 0;
9670
9671 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
9672 if (ret)
9673 return ret;
9674 drm_atomic_set_fb_for_plane(plane_state, fb);
9675 plane_state->crtc_x = 0;
9676 plane_state->crtc_y = 0;
9677 plane_state->crtc_w = hdisplay;
9678 plane_state->crtc_h = vdisplay;
9679 plane_state->src_x = x << 16;
9680 plane_state->src_y = y << 16;
9681 plane_state->src_w = hdisplay << 16;
9682 plane_state->src_h = vdisplay << 16;
9683
9684 return 0;
9685}
9686
6c5ed5ae
ML
9687int intel_get_load_detect_pipe(struct drm_connector *connector,
9688 struct drm_display_mode *mode,
9689 struct intel_load_detect_pipe *old,
9690 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
9691{
9692 struct intel_crtc *intel_crtc;
d2434ab7
DV
9693 struct intel_encoder *intel_encoder =
9694 intel_attached_encoder(connector);
79e53945 9695 struct drm_crtc *possible_crtc;
4ef69c7a 9696 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
9697 struct drm_crtc *crtc = NULL;
9698 struct drm_device *dev = encoder->dev;
0f0f74bc 9699 struct drm_i915_private *dev_priv = to_i915(dev);
94352cf9 9700 struct drm_framebuffer *fb;
51fd371b 9701 struct drm_mode_config *config = &dev->mode_config;
edde3617 9702 struct drm_atomic_state *state = NULL, *restore_state = NULL;
944b0c76 9703 struct drm_connector_state *connector_state;
4be07317 9704 struct intel_crtc_state *crtc_state;
51fd371b 9705 int ret, i = -1;
79e53945 9706
d2dff872 9707 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 9708 connector->base.id, connector->name,
8e329a03 9709 encoder->base.id, encoder->name);
d2dff872 9710
edde3617
ML
9711 old->restore_state = NULL;
9712
6c5ed5ae 9713 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
6e9f798d 9714
79e53945
JB
9715 /*
9716 * Algorithm gets a little messy:
7a5e4805 9717 *
79e53945
JB
9718 * - if the connector already has an assigned crtc, use it (but make
9719 * sure it's on first)
7a5e4805 9720 *
79e53945
JB
9721 * - try to find the first unused crtc that can drive this connector,
9722 * and use that if we find one
79e53945
JB
9723 */
9724
9725 /* See if we already have a CRTC for this connector */
edde3617
ML
9726 if (connector->state->crtc) {
9727 crtc = connector->state->crtc;
8261b191 9728
51fd371b 9729 ret = drm_modeset_lock(&crtc->mutex, ctx);
4d02e2de 9730 if (ret)
ad3c558f 9731 goto fail;
8261b191
CW
9732
9733 /* Make sure the crtc and connector are running */
edde3617 9734 goto found;
79e53945
JB
9735 }
9736
9737 /* Find an unused one (if possible) */
70e1e0ec 9738 for_each_crtc(dev, possible_crtc) {
79e53945
JB
9739 i++;
9740 if (!(encoder->possible_crtcs & (1 << i)))
9741 continue;
edde3617
ML
9742
9743 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
9744 if (ret)
9745 goto fail;
9746
9747 if (possible_crtc->state->enable) {
9748 drm_modeset_unlock(&possible_crtc->mutex);
a459249c 9749 continue;
edde3617 9750 }
a459249c
VS
9751
9752 crtc = possible_crtc;
9753 break;
79e53945
JB
9754 }
9755
9756 /*
9757 * If we didn't find an unused CRTC, don't use any.
9758 */
9759 if (!crtc) {
7173188d 9760 DRM_DEBUG_KMS("no pipe available for load-detect\n");
f4bf77b4 9761 ret = -ENODEV;
ad3c558f 9762 goto fail;
79e53945
JB
9763 }
9764
edde3617
ML
9765found:
9766 intel_crtc = to_intel_crtc(crtc);
9767
4d02e2de
DV
9768 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
9769 if (ret)
ad3c558f 9770 goto fail;
79e53945 9771
83a57153 9772 state = drm_atomic_state_alloc(dev);
edde3617
ML
9773 restore_state = drm_atomic_state_alloc(dev);
9774 if (!state || !restore_state) {
9775 ret = -ENOMEM;
9776 goto fail;
9777 }
83a57153
ACO
9778
9779 state->acquire_ctx = ctx;
edde3617 9780 restore_state->acquire_ctx = ctx;
83a57153 9781
944b0c76
ACO
9782 connector_state = drm_atomic_get_connector_state(state, connector);
9783 if (IS_ERR(connector_state)) {
9784 ret = PTR_ERR(connector_state);
9785 goto fail;
9786 }
9787
edde3617
ML
9788 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
9789 if (ret)
9790 goto fail;
944b0c76 9791
4be07317
ACO
9792 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
9793 if (IS_ERR(crtc_state)) {
9794 ret = PTR_ERR(crtc_state);
9795 goto fail;
9796 }
9797
49d6fa21 9798 crtc_state->base.active = crtc_state->base.enable = true;
4be07317 9799
6492711d
CW
9800 if (!mode)
9801 mode = &load_detect_mode;
79e53945 9802
d2dff872
CW
9803 /* We need a framebuffer large enough to accommodate all accesses
9804 * that the plane may generate whilst we perform load detection.
9805 * We can not rely on the fbcon either being present (we get called
9806 * during its initialisation to detect all boot displays, or it may
9807 * not even exist) or that it is large enough to satisfy the
9808 * requested mode.
9809 */
94352cf9
DV
9810 fb = mode_fits_in_fbdev(dev, mode);
9811 if (fb == NULL) {
d2dff872 9812 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9 9813 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
d2dff872
CW
9814 } else
9815 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 9816 if (IS_ERR(fb)) {
d2dff872 9817 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
f4bf77b4 9818 ret = PTR_ERR(fb);
412b61d8 9819 goto fail;
79e53945 9820 }
79e53945 9821
d3a40d1b
ACO
9822 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
9823 if (ret)
9824 goto fail;
9825
edde3617
ML
9826 drm_framebuffer_unreference(fb);
9827
9828 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
9829 if (ret)
9830 goto fail;
9831
9832 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
9833 if (!ret)
9834 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
9835 if (!ret)
9836 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
9837 if (ret) {
9838 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
9839 goto fail;
9840 }
8c7b5ccb 9841
3ba86073
ML
9842 ret = drm_atomic_commit(state);
9843 if (ret) {
6492711d 9844 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
412b61d8 9845 goto fail;
79e53945 9846 }
edde3617
ML
9847
9848 old->restore_state = restore_state;
7abbd11f 9849 drm_atomic_state_put(state);
7173188d 9850
79e53945 9851 /* let the connector get through one full cycle before testing */
0f0f74bc 9852 intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
7173188d 9853 return true;
412b61d8 9854
ad3c558f 9855fail:
7fb71c8f
CW
9856 if (state) {
9857 drm_atomic_state_put(state);
9858 state = NULL;
9859 }
9860 if (restore_state) {
9861 drm_atomic_state_put(restore_state);
9862 restore_state = NULL;
9863 }
83a57153 9864
6c5ed5ae
ML
9865 if (ret == -EDEADLK)
9866 return ret;
51fd371b 9867
412b61d8 9868 return false;
79e53945
JB
9869}
9870
d2434ab7 9871void intel_release_load_detect_pipe(struct drm_connector *connector,
49172fee
ACO
9872 struct intel_load_detect_pipe *old,
9873 struct drm_modeset_acquire_ctx *ctx)
79e53945 9874{
d2434ab7
DV
9875 struct intel_encoder *intel_encoder =
9876 intel_attached_encoder(connector);
4ef69c7a 9877 struct drm_encoder *encoder = &intel_encoder->base;
edde3617 9878 struct drm_atomic_state *state = old->restore_state;
d3a40d1b 9879 int ret;
79e53945 9880
d2dff872 9881 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 9882 connector->base.id, connector->name,
8e329a03 9883 encoder->base.id, encoder->name);
d2dff872 9884
edde3617 9885 if (!state)
0622a53c 9886 return;
79e53945 9887
581e49fe 9888 ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
0853695c 9889 if (ret)
edde3617 9890 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
0853695c 9891 drm_atomic_state_put(state);
79e53945
JB
9892}
9893
da4a1efa 9894static int i9xx_pll_refclk(struct drm_device *dev,
5cec258b 9895 const struct intel_crtc_state *pipe_config)
da4a1efa 9896{
fac5e23e 9897 struct drm_i915_private *dev_priv = to_i915(dev);
da4a1efa
VS
9898 u32 dpll = pipe_config->dpll_hw_state.dpll;
9899
9900 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 9901 return dev_priv->vbt.lvds_ssc_freq;
6e266956 9902 else if (HAS_PCH_SPLIT(dev_priv))
da4a1efa 9903 return 120000;
5db94019 9904 else if (!IS_GEN2(dev_priv))
da4a1efa
VS
9905 return 96000;
9906 else
9907 return 48000;
9908}
9909
79e53945 9910/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc 9911static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 9912 struct intel_crtc_state *pipe_config)
79e53945 9913{
f1f644dc 9914 struct drm_device *dev = crtc->base.dev;
fac5e23e 9915 struct drm_i915_private *dev_priv = to_i915(dev);
f1f644dc 9916 int pipe = pipe_config->cpu_transcoder;
293623f7 9917 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945 9918 u32 fp;
9e2c8475 9919 struct dpll clock;
dccbea3b 9920 int port_clock;
da4a1efa 9921 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
9922
9923 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 9924 fp = pipe_config->dpll_hw_state.fp0;
79e53945 9925 else
293623f7 9926 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
9927
9928 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
9b1e14f4 9929 if (IS_PINEVIEW(dev_priv)) {
f2b115e6
AJ
9930 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
9931 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
9932 } else {
9933 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
9934 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
9935 }
9936
5db94019 9937 if (!IS_GEN2(dev_priv)) {
9b1e14f4 9938 if (IS_PINEVIEW(dev_priv))
f2b115e6
AJ
9939 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
9940 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
9941 else
9942 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
9943 DPLL_FPA01_P1_POST_DIV_SHIFT);
9944
9945 switch (dpll & DPLL_MODE_MASK) {
9946 case DPLLB_MODE_DAC_SERIAL:
9947 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
9948 5 : 10;
9949 break;
9950 case DPLLB_MODE_LVDS:
9951 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
9952 7 : 14;
9953 break;
9954 default:
28c97730 9955 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 9956 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 9957 return;
79e53945
JB
9958 }
9959
9b1e14f4 9960 if (IS_PINEVIEW(dev_priv))
dccbea3b 9961 port_clock = pnv_calc_dpll_params(refclk, &clock);
ac58c3f0 9962 else
dccbea3b 9963 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945 9964 } else {
50a0bc90 9965 u32 lvds = IS_I830(dev_priv) ? 0 : I915_READ(LVDS);
b1c560d1 9966 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
9967
9968 if (is_lvds) {
9969 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
9970 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
9971
9972 if (lvds & LVDS_CLKB_POWER_UP)
9973 clock.p2 = 7;
9974 else
9975 clock.p2 = 14;
79e53945
JB
9976 } else {
9977 if (dpll & PLL_P1_DIVIDE_BY_TWO)
9978 clock.p1 = 2;
9979 else {
9980 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
9981 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
9982 }
9983 if (dpll & PLL_P2_DIVIDE_BY_4)
9984 clock.p2 = 4;
9985 else
9986 clock.p2 = 2;
79e53945 9987 }
da4a1efa 9988
dccbea3b 9989 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945
JB
9990 }
9991
18442d08
VS
9992 /*
9993 * This value includes pixel_multiplier. We will use
241bfc38 9994 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
9995 * encoder's get_config() function.
9996 */
dccbea3b 9997 pipe_config->port_clock = port_clock;
f1f644dc
JB
9998}
9999
6878da05
VS
10000int intel_dotclock_calculate(int link_freq,
10001 const struct intel_link_m_n *m_n)
f1f644dc 10002{
f1f644dc
JB
10003 /*
10004 * The calculation for the data clock is:
1041a02f 10005 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 10006 * But we want to avoid losing precison if possible, so:
1041a02f 10007 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
10008 *
10009 * and the link clock is simpler:
1041a02f 10010 * link_clock = (m * link_clock) / n
f1f644dc
JB
10011 */
10012
6878da05
VS
10013 if (!m_n->link_n)
10014 return 0;
f1f644dc 10015
6878da05
VS
10016 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10017}
f1f644dc 10018
18442d08 10019static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 10020 struct intel_crtc_state *pipe_config)
6878da05 10021{
e3b247da 10022 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
79e53945 10023
18442d08
VS
10024 /* read out port_clock from the DPLL */
10025 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 10026
f1f644dc 10027 /*
e3b247da
VS
10028 * In case there is an active pipe without active ports,
10029 * we may need some idea for the dotclock anyway.
10030 * Calculate one based on the FDI configuration.
79e53945 10031 */
2d112de7 10032 pipe_config->base.adjusted_mode.crtc_clock =
21a727b3 10033 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
18442d08 10034 &pipe_config->fdi_m_n);
79e53945
JB
10035}
10036
10037/** Returns the currently programmed mode of the given pipe. */
10038struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10039 struct drm_crtc *crtc)
10040{
fac5e23e 10041 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 10042 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 10043 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
79e53945 10044 struct drm_display_mode *mode;
3f36b937 10045 struct intel_crtc_state *pipe_config;
fe2b8f9d
PZ
10046 int htot = I915_READ(HTOTAL(cpu_transcoder));
10047 int hsync = I915_READ(HSYNC(cpu_transcoder));
10048 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10049 int vsync = I915_READ(VSYNC(cpu_transcoder));
293623f7 10050 enum pipe pipe = intel_crtc->pipe;
79e53945
JB
10051
10052 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10053 if (!mode)
10054 return NULL;
10055
3f36b937
TU
10056 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10057 if (!pipe_config) {
10058 kfree(mode);
10059 return NULL;
10060 }
10061
f1f644dc
JB
10062 /*
10063 * Construct a pipe_config sufficient for getting the clock info
10064 * back out of crtc_clock_get.
10065 *
10066 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10067 * to use a real value here instead.
10068 */
3f36b937
TU
10069 pipe_config->cpu_transcoder = (enum transcoder) pipe;
10070 pipe_config->pixel_multiplier = 1;
10071 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10072 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10073 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
10074 i9xx_crtc_clock_get(intel_crtc, pipe_config);
10075
10076 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
79e53945
JB
10077 mode->hdisplay = (htot & 0xffff) + 1;
10078 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10079 mode->hsync_start = (hsync & 0xffff) + 1;
10080 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10081 mode->vdisplay = (vtot & 0xffff) + 1;
10082 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10083 mode->vsync_start = (vsync & 0xffff) + 1;
10084 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10085
10086 drm_mode_set_name(mode);
79e53945 10087
3f36b937
TU
10088 kfree(pipe_config);
10089
79e53945
JB
10090 return mode;
10091}
10092
10093static void intel_crtc_destroy(struct drm_crtc *crtc)
10094{
10095 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
67e77c5a 10096 struct drm_device *dev = crtc->dev;
51cbaf01 10097 struct intel_flip_work *work;
67e77c5a 10098
5e2d7afc 10099 spin_lock_irq(&dev->event_lock);
5a21b665
DV
10100 work = intel_crtc->flip_work;
10101 intel_crtc->flip_work = NULL;
10102 spin_unlock_irq(&dev->event_lock);
67e77c5a 10103
5a21b665 10104 if (work) {
51cbaf01
ML
10105 cancel_work_sync(&work->mmio_work);
10106 cancel_work_sync(&work->unpin_work);
5a21b665 10107 kfree(work);
67e77c5a 10108 }
79e53945
JB
10109
10110 drm_crtc_cleanup(crtc);
67e77c5a 10111
79e53945
JB
10112 kfree(intel_crtc);
10113}
10114
6b95a207
KH
10115static void intel_unpin_work_fn(struct work_struct *__work)
10116{
51cbaf01
ML
10117 struct intel_flip_work *work =
10118 container_of(__work, struct intel_flip_work, unpin_work);
5a21b665
DV
10119 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10120 struct drm_device *dev = crtc->base.dev;
10121 struct drm_plane *primary = crtc->base.primary;
03f476e1 10122
5a21b665
DV
10123 if (is_mmio_work(work))
10124 flush_work(&work->mmio_work);
03f476e1 10125
5a21b665 10126 mutex_lock(&dev->struct_mutex);
be1e3415 10127 intel_unpin_fb_vma(work->old_vma);
f8c417cd 10128 i915_gem_object_put(work->pending_flip_obj);
5a21b665 10129 mutex_unlock(&dev->struct_mutex);
143f73b3 10130
e8a261ea
CW
10131 i915_gem_request_put(work->flip_queued_req);
10132
5748b6a1
CW
10133 intel_frontbuffer_flip_complete(to_i915(dev),
10134 to_intel_plane(primary)->frontbuffer_bit);
5a21b665
DV
10135 intel_fbc_post_update(crtc);
10136 drm_framebuffer_unreference(work->old_fb);
143f73b3 10137
5a21b665
DV
10138 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
10139 atomic_dec(&crtc->unpin_work_count);
a6747b73 10140
5a21b665
DV
10141 kfree(work);
10142}
d9e86c0e 10143
5a21b665
DV
10144/* Is 'a' after or equal to 'b'? */
10145static bool g4x_flip_count_after_eq(u32 a, u32 b)
10146{
10147 return !((a - b) & 0x80000000);
10148}
143f73b3 10149
5a21b665
DV
10150static bool __pageflip_finished_cs(struct intel_crtc *crtc,
10151 struct intel_flip_work *work)
10152{
10153 struct drm_device *dev = crtc->base.dev;
fac5e23e 10154 struct drm_i915_private *dev_priv = to_i915(dev);
143f73b3 10155
8af29b0c 10156 if (abort_flip_on_reset(crtc))
5a21b665 10157 return true;
143f73b3 10158
5a21b665
DV
10159 /*
10160 * The relevant registers doen't exist on pre-ctg.
10161 * As the flip done interrupt doesn't trigger for mmio
10162 * flips on gmch platforms, a flip count check isn't
10163 * really needed there. But since ctg has the registers,
10164 * include it in the check anyway.
10165 */
9beb5fea 10166 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
5a21b665 10167 return true;
b4a98e57 10168
5a21b665
DV
10169 /*
10170 * BDW signals flip done immediately if the plane
10171 * is disabled, even if the plane enable is already
10172 * armed to occur at the next vblank :(
10173 */
f99d7069 10174
5a21b665
DV
10175 /*
10176 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10177 * used the same base address. In that case the mmio flip might
10178 * have completed, but the CS hasn't even executed the flip yet.
10179 *
10180 * A flip count check isn't enough as the CS might have updated
10181 * the base address just after start of vblank, but before we
10182 * managed to process the interrupt. This means we'd complete the
10183 * CS flip too soon.
10184 *
10185 * Combining both checks should get us a good enough result. It may
10186 * still happen that the CS flip has been executed, but has not
10187 * yet actually completed. But in case the base address is the same
10188 * anyway, we don't really care.
10189 */
10190 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10191 crtc->flip_work->gtt_offset &&
10192 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
10193 crtc->flip_work->flip_count);
10194}
b4a98e57 10195
5a21b665
DV
10196static bool
10197__pageflip_finished_mmio(struct intel_crtc *crtc,
10198 struct intel_flip_work *work)
10199{
10200 /*
10201 * MMIO work completes when vblank is different from
10202 * flip_queued_vblank.
10203 *
10204 * Reset counter value doesn't matter, this is handled by
10205 * i915_wait_request finishing early, so no need to handle
10206 * reset here.
10207 */
10208 return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
6b95a207
KH
10209}
10210
51cbaf01
ML
10211
10212static bool pageflip_finished(struct intel_crtc *crtc,
10213 struct intel_flip_work *work)
10214{
10215 if (!atomic_read(&work->pending))
10216 return false;
10217
10218 smp_rmb();
10219
5a21b665
DV
10220 if (is_mmio_work(work))
10221 return __pageflip_finished_mmio(crtc, work);
10222 else
10223 return __pageflip_finished_cs(crtc, work);
10224}
10225
10226void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
10227{
91c8a326 10228 struct drm_device *dev = &dev_priv->drm;
98187836 10229 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
5a21b665
DV
10230 struct intel_flip_work *work;
10231 unsigned long flags;
10232
10233 /* Ignore early vblank irqs */
10234 if (!crtc)
10235 return;
10236
51cbaf01 10237 /*
5a21b665
DV
10238 * This is called both by irq handlers and the reset code (to complete
10239 * lost pageflips) so needs the full irqsave spinlocks.
51cbaf01 10240 */
5a21b665 10241 spin_lock_irqsave(&dev->event_lock, flags);
e2af48c6 10242 work = crtc->flip_work;
5a21b665
DV
10243
10244 if (work != NULL &&
10245 !is_mmio_work(work) &&
e2af48c6
VS
10246 pageflip_finished(crtc, work))
10247 page_flip_completed(crtc);
5a21b665
DV
10248
10249 spin_unlock_irqrestore(&dev->event_lock, flags);
75f7f3ec
VS
10250}
10251
51cbaf01 10252void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
6b95a207 10253{
91c8a326 10254 struct drm_device *dev = &dev_priv->drm;
98187836 10255 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
51cbaf01 10256 struct intel_flip_work *work;
6b95a207
KH
10257 unsigned long flags;
10258
5251f04e
ML
10259 /* Ignore early vblank irqs */
10260 if (!crtc)
10261 return;
f326038a
DV
10262
10263 /*
10264 * This is called both by irq handlers and the reset code (to complete
10265 * lost pageflips) so needs the full irqsave spinlocks.
e7d841ca 10266 */
6b95a207 10267 spin_lock_irqsave(&dev->event_lock, flags);
e2af48c6 10268 work = crtc->flip_work;
5251f04e 10269
5a21b665
DV
10270 if (work != NULL &&
10271 is_mmio_work(work) &&
e2af48c6
VS
10272 pageflip_finished(crtc, work))
10273 page_flip_completed(crtc);
5251f04e 10274
6b95a207
KH
10275 spin_unlock_irqrestore(&dev->event_lock, flags);
10276}
10277
5a21b665
DV
10278static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
10279 struct intel_flip_work *work)
84c33a64 10280{
5a21b665 10281 work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
84c33a64 10282
5a21b665
DV
10283 /* Ensure that the work item is consistent when activating it ... */
10284 smp_mb__before_atomic();
10285 atomic_set(&work->pending, 1);
10286}
a6747b73 10287
5a21b665
DV
10288static int intel_gen2_queue_flip(struct drm_device *dev,
10289 struct drm_crtc *crtc,
10290 struct drm_framebuffer *fb,
10291 struct drm_i915_gem_object *obj,
10292 struct drm_i915_gem_request *req,
10293 uint32_t flags)
10294{
5a21b665 10295 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
73dec95e 10296 u32 flip_mask, *cs;
143f73b3 10297
73dec95e
TU
10298 cs = intel_ring_begin(req, 6);
10299 if (IS_ERR(cs))
10300 return PTR_ERR(cs);
143f73b3 10301
5a21b665
DV
10302 /* Can't queue multiple flips, so wait for the previous
10303 * one to finish before executing the next.
10304 */
10305 if (intel_crtc->plane)
10306 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10307 else
10308 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
73dec95e
TU
10309 *cs++ = MI_WAIT_FOR_EVENT | flip_mask;
10310 *cs++ = MI_NOOP;
10311 *cs++ = MI_DISPLAY_FLIP | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
10312 *cs++ = fb->pitches[0];
10313 *cs++ = intel_crtc->flip_work->gtt_offset;
10314 *cs++ = 0; /* aux display base address, unused */
143f73b3 10315
5a21b665
DV
10316 return 0;
10317}
84c33a64 10318
5a21b665
DV
10319static int intel_gen3_queue_flip(struct drm_device *dev,
10320 struct drm_crtc *crtc,
10321 struct drm_framebuffer *fb,
10322 struct drm_i915_gem_object *obj,
10323 struct drm_i915_gem_request *req,
10324 uint32_t flags)
10325{
5a21b665 10326 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
73dec95e 10327 u32 flip_mask, *cs;
d55dbd06 10328
73dec95e
TU
10329 cs = intel_ring_begin(req, 6);
10330 if (IS_ERR(cs))
10331 return PTR_ERR(cs);
d55dbd06 10332
5a21b665
DV
10333 if (intel_crtc->plane)
10334 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10335 else
10336 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
73dec95e
TU
10337 *cs++ = MI_WAIT_FOR_EVENT | flip_mask;
10338 *cs++ = MI_NOOP;
10339 *cs++ = MI_DISPLAY_FLIP_I915 | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
10340 *cs++ = fb->pitches[0];
10341 *cs++ = intel_crtc->flip_work->gtt_offset;
10342 *cs++ = MI_NOOP;
fd8e058a 10343
5a21b665
DV
10344 return 0;
10345}
84c33a64 10346
5a21b665
DV
10347static int intel_gen4_queue_flip(struct drm_device *dev,
10348 struct drm_crtc *crtc,
10349 struct drm_framebuffer *fb,
10350 struct drm_i915_gem_object *obj,
10351 struct drm_i915_gem_request *req,
10352 uint32_t flags)
10353{
fac5e23e 10354 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665 10355 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
73dec95e 10356 u32 pf, pipesrc, *cs;
143f73b3 10357
73dec95e
TU
10358 cs = intel_ring_begin(req, 4);
10359 if (IS_ERR(cs))
10360 return PTR_ERR(cs);
143f73b3 10361
5a21b665
DV
10362 /* i965+ uses the linear or tiled offsets from the
10363 * Display Registers (which do not change across a page-flip)
10364 * so we need only reprogram the base address.
10365 */
73dec95e
TU
10366 *cs++ = MI_DISPLAY_FLIP | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
10367 *cs++ = fb->pitches[0];
10368 *cs++ = intel_crtc->flip_work->gtt_offset |
10369 intel_fb_modifier_to_tiling(fb->modifier);
5a21b665
DV
10370
10371 /* XXX Enabling the panel-fitter across page-flip is so far
10372 * untested on non-native modes, so ignore it for now.
10373 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
10374 */
10375 pf = 0;
10376 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
73dec95e 10377 *cs++ = pf | pipesrc;
143f73b3 10378
5a21b665 10379 return 0;
8c9f3aaf
JB
10380}
10381
5a21b665
DV
10382static int intel_gen6_queue_flip(struct drm_device *dev,
10383 struct drm_crtc *crtc,
10384 struct drm_framebuffer *fb,
10385 struct drm_i915_gem_object *obj,
10386 struct drm_i915_gem_request *req,
10387 uint32_t flags)
da20eabd 10388{
fac5e23e 10389 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665 10390 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
73dec95e 10391 u32 pf, pipesrc, *cs;
d21fbe87 10392
73dec95e
TU
10393 cs = intel_ring_begin(req, 4);
10394 if (IS_ERR(cs))
10395 return PTR_ERR(cs);
92826fcd 10396
73dec95e
TU
10397 *cs++ = MI_DISPLAY_FLIP | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
10398 *cs++ = fb->pitches[0] | intel_fb_modifier_to_tiling(fb->modifier);
10399 *cs++ = intel_crtc->flip_work->gtt_offset;
92826fcd 10400
5a21b665
DV
10401 /* Contrary to the suggestions in the documentation,
10402 * "Enable Panel Fitter" does not seem to be required when page
10403 * flipping with a non-native mode, and worse causes a normal
10404 * modeset to fail.
10405 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
10406 */
10407 pf = 0;
10408 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
73dec95e 10409 *cs++ = pf | pipesrc;
7809e5ae 10410
5a21b665 10411 return 0;
7809e5ae
MR
10412}
10413
5a21b665
DV
10414static int intel_gen7_queue_flip(struct drm_device *dev,
10415 struct drm_crtc *crtc,
10416 struct drm_framebuffer *fb,
10417 struct drm_i915_gem_object *obj,
10418 struct drm_i915_gem_request *req,
10419 uint32_t flags)
d21fbe87 10420{
5db94019 10421 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665 10422 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
73dec95e 10423 u32 *cs, plane_bit = 0;
5a21b665 10424 int len, ret;
d21fbe87 10425
5a21b665
DV
10426 switch (intel_crtc->plane) {
10427 case PLANE_A:
10428 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
10429 break;
10430 case PLANE_B:
10431 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
10432 break;
10433 case PLANE_C:
10434 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
10435 break;
10436 default:
10437 WARN_ONCE(1, "unknown plane in flip command\n");
10438 return -ENODEV;
10439 }
10440
10441 len = 4;
b5321f30 10442 if (req->engine->id == RCS) {
5a21b665
DV
10443 len += 6;
10444 /*
10445 * On Gen 8, SRM is now taking an extra dword to accommodate
10446 * 48bits addresses, and we need a NOOP for the batch size to
10447 * stay even.
10448 */
5db94019 10449 if (IS_GEN8(dev_priv))
5a21b665
DV
10450 len += 2;
10451 }
10452
10453 /*
10454 * BSpec MI_DISPLAY_FLIP for IVB:
10455 * "The full packet must be contained within the same cache line."
10456 *
10457 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
10458 * cacheline, if we ever start emitting more commands before
10459 * the MI_DISPLAY_FLIP we may need to first emit everything else,
10460 * then do the cacheline alignment, and finally emit the
10461 * MI_DISPLAY_FLIP.
10462 */
10463 ret = intel_ring_cacheline_align(req);
10464 if (ret)
10465 return ret;
10466
73dec95e
TU
10467 cs = intel_ring_begin(req, len);
10468 if (IS_ERR(cs))
10469 return PTR_ERR(cs);
5a21b665
DV
10470
10471 /* Unmask the flip-done completion message. Note that the bspec says that
10472 * we should do this for both the BCS and RCS, and that we must not unmask
10473 * more than one flip event at any time (or ensure that one flip message
10474 * can be sent by waiting for flip-done prior to queueing new flips).
10475 * Experimentation says that BCS works despite DERRMR masking all
10476 * flip-done completion events and that unmasking all planes at once
10477 * for the RCS also doesn't appear to drop events. Setting the DERRMR
10478 * to zero does lead to lockups within MI_DISPLAY_FLIP.
10479 */
b5321f30 10480 if (req->engine->id == RCS) {
73dec95e
TU
10481 *cs++ = MI_LOAD_REGISTER_IMM(1);
10482 *cs++ = i915_mmio_reg_offset(DERRMR);
10483 *cs++ = ~(DERRMR_PIPEA_PRI_FLIP_DONE |
10484 DERRMR_PIPEB_PRI_FLIP_DONE |
10485 DERRMR_PIPEC_PRI_FLIP_DONE);
5db94019 10486 if (IS_GEN8(dev_priv))
73dec95e
TU
10487 *cs++ = MI_STORE_REGISTER_MEM_GEN8 |
10488 MI_SRM_LRM_GLOBAL_GTT;
5a21b665 10489 else
73dec95e
TU
10490 *cs++ = MI_STORE_REGISTER_MEM | MI_SRM_LRM_GLOBAL_GTT;
10491 *cs++ = i915_mmio_reg_offset(DERRMR);
10492 *cs++ = i915_ggtt_offset(req->engine->scratch) + 256;
5db94019 10493 if (IS_GEN8(dev_priv)) {
73dec95e
TU
10494 *cs++ = 0;
10495 *cs++ = MI_NOOP;
5a21b665
DV
10496 }
10497 }
10498
73dec95e
TU
10499 *cs++ = MI_DISPLAY_FLIP_I915 | plane_bit;
10500 *cs++ = fb->pitches[0] | intel_fb_modifier_to_tiling(fb->modifier);
10501 *cs++ = intel_crtc->flip_work->gtt_offset;
10502 *cs++ = MI_NOOP;
5a21b665
DV
10503
10504 return 0;
10505}
10506
10507static bool use_mmio_flip(struct intel_engine_cs *engine,
10508 struct drm_i915_gem_object *obj)
10509{
10510 /*
10511 * This is not being used for older platforms, because
10512 * non-availability of flip done interrupt forces us to use
10513 * CS flips. Older platforms derive flip done using some clever
10514 * tricks involving the flip_pending status bits and vblank irqs.
10515 * So using MMIO flips there would disrupt this mechanism.
10516 */
10517
10518 if (engine == NULL)
10519 return true;
10520
10521 if (INTEL_GEN(engine->i915) < 5)
10522 return false;
10523
10524 if (i915.use_mmio_flip < 0)
10525 return false;
10526 else if (i915.use_mmio_flip > 0)
10527 return true;
10528 else if (i915.enable_execlists)
10529 return true;
c37efb99 10530
d07f0e59 10531 return engine != i915_gem_object_last_write_engine(obj);
5a21b665
DV
10532}
10533
10534static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
10535 unsigned int rotation,
10536 struct intel_flip_work *work)
10537{
10538 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 10539 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
DV
10540 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
10541 const enum pipe pipe = intel_crtc->pipe;
d2196774 10542 u32 ctl, stride = skl_plane_stride(fb, 0, rotation);
5a21b665
DV
10543
10544 ctl = I915_READ(PLANE_CTL(pipe, 0));
10545 ctl &= ~PLANE_CTL_TILED_MASK;
bae781b2 10546 switch (fb->modifier) {
2f075565 10547 case DRM_FORMAT_MOD_LINEAR:
5a21b665
DV
10548 break;
10549 case I915_FORMAT_MOD_X_TILED:
10550 ctl |= PLANE_CTL_TILED_X;
10551 break;
10552 case I915_FORMAT_MOD_Y_TILED:
10553 ctl |= PLANE_CTL_TILED_Y;
10554 break;
10555 case I915_FORMAT_MOD_Yf_TILED:
10556 ctl |= PLANE_CTL_TILED_YF;
10557 break;
10558 default:
bae781b2 10559 MISSING_CASE(fb->modifier);
5a21b665
DV
10560 }
10561
5a21b665
DV
10562 /*
10563 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
10564 * PLANE_SURF updates, the update is then guaranteed to be atomic.
10565 */
10566 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
10567 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
10568
10569 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
10570 POSTING_READ(PLANE_SURF(pipe, 0));
10571}
10572
10573static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
10574 struct intel_flip_work *work)
10575{
10576 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 10577 struct drm_i915_private *dev_priv = to_i915(dev);
72618ebf 10578 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
5a21b665
DV
10579 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
10580 u32 dspcntr;
10581
10582 dspcntr = I915_READ(reg);
10583
bae781b2 10584 if (fb->modifier == I915_FORMAT_MOD_X_TILED)
5a21b665
DV
10585 dspcntr |= DISPPLANE_TILED;
10586 else
10587 dspcntr &= ~DISPPLANE_TILED;
10588
10589 I915_WRITE(reg, dspcntr);
10590
10591 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
10592 POSTING_READ(DSPSURF(intel_crtc->plane));
10593}
10594
10595static void intel_mmio_flip_work_func(struct work_struct *w)
10596{
10597 struct intel_flip_work *work =
10598 container_of(w, struct intel_flip_work, mmio_work);
10599 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10600 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10601 struct intel_framebuffer *intel_fb =
10602 to_intel_framebuffer(crtc->base.primary->fb);
10603 struct drm_i915_gem_object *obj = intel_fb->obj;
10604
d07f0e59 10605 WARN_ON(i915_gem_object_wait(obj, 0, MAX_SCHEDULE_TIMEOUT, NULL) < 0);
5a21b665
DV
10606
10607 intel_pipe_update_start(crtc);
10608
10609 if (INTEL_GEN(dev_priv) >= 9)
10610 skl_do_mmio_flip(crtc, work->rotation, work);
10611 else
10612 /* use_mmio_flip() retricts MMIO flips to ilk+ */
10613 ilk_do_mmio_flip(crtc, work);
10614
10615 intel_pipe_update_end(crtc, work);
10616}
10617
10618static int intel_default_queue_flip(struct drm_device *dev,
10619 struct drm_crtc *crtc,
10620 struct drm_framebuffer *fb,
10621 struct drm_i915_gem_object *obj,
10622 struct drm_i915_gem_request *req,
10623 uint32_t flags)
10624{
10625 return -ENODEV;
10626}
10627
10628static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
10629 struct intel_crtc *intel_crtc,
10630 struct intel_flip_work *work)
10631{
10632 u32 addr, vblank;
10633
10634 if (!atomic_read(&work->pending))
10635 return false;
10636
10637 smp_rmb();
10638
10639 vblank = intel_crtc_get_vblank_counter(intel_crtc);
10640 if (work->flip_ready_vblank == 0) {
10641 if (work->flip_queued_req &&
f69a02c9 10642 !i915_gem_request_completed(work->flip_queued_req))
5a21b665
DV
10643 return false;
10644
10645 work->flip_ready_vblank = vblank;
10646 }
10647
10648 if (vblank - work->flip_ready_vblank < 3)
10649 return false;
10650
10651 /* Potential stall - if we see that the flip has happened,
10652 * assume a missed interrupt. */
10653 if (INTEL_GEN(dev_priv) >= 4)
10654 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
10655 else
10656 addr = I915_READ(DSPADDR(intel_crtc->plane));
10657
10658 /* There is a potential issue here with a false positive after a flip
10659 * to the same address. We could address this by checking for a
10660 * non-incrementing frame counter.
10661 */
10662 return addr == work->gtt_offset;
10663}
10664
10665void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
10666{
91c8a326 10667 struct drm_device *dev = &dev_priv->drm;
98187836 10668 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
5a21b665
DV
10669 struct intel_flip_work *work;
10670
10671 WARN_ON(!in_interrupt());
10672
10673 if (crtc == NULL)
10674 return;
10675
10676 spin_lock(&dev->event_lock);
e2af48c6 10677 work = crtc->flip_work;
5a21b665
DV
10678
10679 if (work != NULL && !is_mmio_work(work) &&
e2af48c6 10680 __pageflip_stall_check_cs(dev_priv, crtc, work)) {
5a21b665
DV
10681 WARN_ONCE(1,
10682 "Kicking stuck page flip: queued at %d, now %d\n",
e2af48c6
VS
10683 work->flip_queued_vblank, intel_crtc_get_vblank_counter(crtc));
10684 page_flip_completed(crtc);
5a21b665
DV
10685 work = NULL;
10686 }
10687
10688 if (work != NULL && !is_mmio_work(work) &&
e2af48c6 10689 intel_crtc_get_vblank_counter(crtc) - work->flip_queued_vblank > 1)
5a21b665
DV
10690 intel_queue_rps_boost_for_request(work->flip_queued_req);
10691 spin_unlock(&dev->event_lock);
10692}
10693
4c01ded5 10694__maybe_unused
5a21b665
DV
10695static int intel_crtc_page_flip(struct drm_crtc *crtc,
10696 struct drm_framebuffer *fb,
10697 struct drm_pending_vblank_event *event,
10698 uint32_t page_flip_flags)
10699{
10700 struct drm_device *dev = crtc->dev;
fac5e23e 10701 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
DV
10702 struct drm_framebuffer *old_fb = crtc->primary->fb;
10703 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
10704 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10705 struct drm_plane *primary = crtc->primary;
10706 enum pipe pipe = intel_crtc->pipe;
10707 struct intel_flip_work *work;
10708 struct intel_engine_cs *engine;
10709 bool mmio_flip;
8e637178 10710 struct drm_i915_gem_request *request;
058d88c4 10711 struct i915_vma *vma;
5a21b665
DV
10712 int ret;
10713
10714 /*
10715 * drm_mode_page_flip_ioctl() should already catch this, but double
10716 * check to be safe. In the future we may enable pageflipping from
10717 * a disabled primary plane.
10718 */
10719 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
10720 return -EBUSY;
10721
10722 /* Can't change pixel format via MI display flips. */
dbd4d576 10723 if (fb->format != crtc->primary->fb->format)
5a21b665
DV
10724 return -EINVAL;
10725
10726 /*
10727 * TILEOFF/LINOFF registers can't be changed via MI display flips.
10728 * Note that pitch changes could also affect these register.
10729 */
6315b5d3 10730 if (INTEL_GEN(dev_priv) > 3 &&
5a21b665
DV
10731 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
10732 fb->pitches[0] != crtc->primary->fb->pitches[0]))
10733 return -EINVAL;
10734
10735 if (i915_terminally_wedged(&dev_priv->gpu_error))
10736 goto out_hang;
10737
10738 work = kzalloc(sizeof(*work), GFP_KERNEL);
10739 if (work == NULL)
10740 return -ENOMEM;
10741
10742 work->event = event;
10743 work->crtc = crtc;
10744 work->old_fb = old_fb;
10745 INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
10746
10747 ret = drm_crtc_vblank_get(crtc);
10748 if (ret)
10749 goto free_work;
10750
10751 /* We borrow the event spin lock for protecting flip_work */
10752 spin_lock_irq(&dev->event_lock);
10753 if (intel_crtc->flip_work) {
10754 /* Before declaring the flip queue wedged, check if
10755 * the hardware completed the operation behind our backs.
10756 */
10757 if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
10758 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
10759 page_flip_completed(intel_crtc);
10760 } else {
10761 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
10762 spin_unlock_irq(&dev->event_lock);
10763
10764 drm_crtc_vblank_put(crtc);
10765 kfree(work);
10766 return -EBUSY;
10767 }
10768 }
10769 intel_crtc->flip_work = work;
10770 spin_unlock_irq(&dev->event_lock);
10771
10772 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
10773 flush_workqueue(dev_priv->wq);
10774
10775 /* Reference the objects for the scheduled work. */
10776 drm_framebuffer_reference(work->old_fb);
5a21b665
DV
10777
10778 crtc->primary->fb = fb;
10779 update_state_fb(crtc->primary);
faf68d92 10780
25dc556a 10781 work->pending_flip_obj = i915_gem_object_get(obj);
5a21b665
DV
10782
10783 ret = i915_mutex_lock_interruptible(dev);
10784 if (ret)
10785 goto cleanup;
10786
8af29b0c 10787 intel_crtc->reset_count = i915_reset_count(&dev_priv->gpu_error);
8c185eca 10788 if (i915_reset_backoff_or_wedged(&dev_priv->gpu_error)) {
5a21b665 10789 ret = -EIO;
ddbb271a 10790 goto unlock;
5a21b665
DV
10791 }
10792
10793 atomic_inc(&intel_crtc->unpin_work_count);
10794
9beb5fea 10795 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
5a21b665
DV
10796 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
10797
920a14b2 10798 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
3b3f1650 10799 engine = dev_priv->engine[BCS];
bae781b2 10800 if (fb->modifier != old_fb->modifier)
5a21b665
DV
10801 /* vlv: DISPLAY_FLIP fails to change tiling */
10802 engine = NULL;
fd6b8f43 10803 } else if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
3b3f1650 10804 engine = dev_priv->engine[BCS];
6315b5d3 10805 } else if (INTEL_GEN(dev_priv) >= 7) {
d07f0e59 10806 engine = i915_gem_object_last_write_engine(obj);
5a21b665 10807 if (engine == NULL || engine->id != RCS)
3b3f1650 10808 engine = dev_priv->engine[BCS];
5a21b665 10809 } else {
3b3f1650 10810 engine = dev_priv->engine[RCS];
5a21b665
DV
10811 }
10812
10813 mmio_flip = use_mmio_flip(engine, obj);
10814
058d88c4
CW
10815 vma = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
10816 if (IS_ERR(vma)) {
10817 ret = PTR_ERR(vma);
5a21b665 10818 goto cleanup_pending;
058d88c4 10819 }
5a21b665 10820
be1e3415
CW
10821 work->old_vma = to_intel_plane_state(primary->state)->vma;
10822 to_intel_plane_state(primary->state)->vma = vma;
10823
10824 work->gtt_offset = i915_ggtt_offset(vma) + intel_crtc->dspaddr_offset;
5a21b665
DV
10825 work->rotation = crtc->primary->state->rotation;
10826
1f061316
PZ
10827 /*
10828 * There's the potential that the next frame will not be compatible with
10829 * FBC, so we want to call pre_update() before the actual page flip.
10830 * The problem is that pre_update() caches some information about the fb
10831 * object, so we want to do this only after the object is pinned. Let's
10832 * be on the safe side and do this immediately before scheduling the
10833 * flip.
10834 */
10835 intel_fbc_pre_update(intel_crtc, intel_crtc->config,
10836 to_intel_plane_state(primary->state));
10837
5a21b665
DV
10838 if (mmio_flip) {
10839 INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
6277c8d0 10840 queue_work(system_unbound_wq, &work->mmio_work);
5a21b665 10841 } else {
e8a9c58f
CW
10842 request = i915_gem_request_alloc(engine,
10843 dev_priv->kernel_context);
8e637178
CW
10844 if (IS_ERR(request)) {
10845 ret = PTR_ERR(request);
10846 goto cleanup_unpin;
10847 }
10848
a2bc4695 10849 ret = i915_gem_request_await_object(request, obj, false);
8e637178
CW
10850 if (ret)
10851 goto cleanup_request;
10852
5a21b665
DV
10853 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
10854 page_flip_flags);
10855 if (ret)
8e637178 10856 goto cleanup_request;
5a21b665
DV
10857
10858 intel_mark_page_flip_active(intel_crtc, work);
10859
8e637178 10860 work->flip_queued_req = i915_gem_request_get(request);
e642c85b 10861 i915_add_request(request);
5a21b665
DV
10862 }
10863
92117f0b 10864 i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
5a21b665
DV
10865 i915_gem_track_fb(intel_fb_obj(old_fb), obj,
10866 to_intel_plane(primary)->frontbuffer_bit);
10867 mutex_unlock(&dev->struct_mutex);
10868
5748b6a1 10869 intel_frontbuffer_flip_prepare(to_i915(dev),
5a21b665
DV
10870 to_intel_plane(primary)->frontbuffer_bit);
10871
10872 trace_i915_flip_request(intel_crtc->plane, obj);
10873
10874 return 0;
10875
8e637178 10876cleanup_request:
e642c85b 10877 i915_add_request(request);
5a21b665 10878cleanup_unpin:
be1e3415
CW
10879 to_intel_plane_state(primary->state)->vma = work->old_vma;
10880 intel_unpin_fb_vma(vma);
5a21b665 10881cleanup_pending:
5a21b665 10882 atomic_dec(&intel_crtc->unpin_work_count);
ddbb271a 10883unlock:
5a21b665
DV
10884 mutex_unlock(&dev->struct_mutex);
10885cleanup:
10886 crtc->primary->fb = old_fb;
10887 update_state_fb(crtc->primary);
10888
f0cd5182 10889 i915_gem_object_put(obj);
5a21b665
DV
10890 drm_framebuffer_unreference(work->old_fb);
10891
10892 spin_lock_irq(&dev->event_lock);
10893 intel_crtc->flip_work = NULL;
10894 spin_unlock_irq(&dev->event_lock);
10895
10896 drm_crtc_vblank_put(crtc);
10897free_work:
10898 kfree(work);
10899
10900 if (ret == -EIO) {
10901 struct drm_atomic_state *state;
10902 struct drm_plane_state *plane_state;
10903
10904out_hang:
10905 state = drm_atomic_state_alloc(dev);
10906 if (!state)
10907 return -ENOMEM;
b260ac3e 10908 state->acquire_ctx = dev->mode_config.acquire_ctx;
5a21b665
DV
10909
10910retry:
10911 plane_state = drm_atomic_get_plane_state(state, primary);
10912 ret = PTR_ERR_OR_ZERO(plane_state);
10913 if (!ret) {
10914 drm_atomic_set_fb_for_plane(plane_state, fb);
10915
10916 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
10917 if (!ret)
10918 ret = drm_atomic_commit(state);
10919 }
10920
10921 if (ret == -EDEADLK) {
10922 drm_modeset_backoff(state->acquire_ctx);
10923 drm_atomic_state_clear(state);
10924 goto retry;
10925 }
10926
0853695c 10927 drm_atomic_state_put(state);
5a21b665
DV
10928
10929 if (ret == 0 && event) {
10930 spin_lock_irq(&dev->event_lock);
10931 drm_crtc_send_vblank_event(crtc, event);
10932 spin_unlock_irq(&dev->event_lock);
10933 }
10934 }
10935 return ret;
10936}
10937
10938
10939/**
10940 * intel_wm_need_update - Check whether watermarks need updating
10941 * @plane: drm plane
10942 * @state: new plane state
10943 *
10944 * Check current plane state versus the new one to determine whether
10945 * watermarks need to be recalculated.
10946 *
10947 * Returns true or false.
10948 */
10949static bool intel_wm_need_update(struct drm_plane *plane,
10950 struct drm_plane_state *state)
10951{
10952 struct intel_plane_state *new = to_intel_plane_state(state);
10953 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
10954
10955 /* Update watermarks on tiling or size changes. */
936e71e3 10956 if (new->base.visible != cur->base.visible)
5a21b665
DV
10957 return true;
10958
10959 if (!cur->base.fb || !new->base.fb)
10960 return false;
10961
bae781b2 10962 if (cur->base.fb->modifier != new->base.fb->modifier ||
5a21b665 10963 cur->base.rotation != new->base.rotation ||
936e71e3
VS
10964 drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
10965 drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
10966 drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
10967 drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
5a21b665
DV
10968 return true;
10969
10970 return false;
10971}
10972
10973static bool needs_scaling(struct intel_plane_state *state)
10974{
936e71e3
VS
10975 int src_w = drm_rect_width(&state->base.src) >> 16;
10976 int src_h = drm_rect_height(&state->base.src) >> 16;
10977 int dst_w = drm_rect_width(&state->base.dst);
10978 int dst_h = drm_rect_height(&state->base.dst);
5a21b665
DV
10979
10980 return (src_w != dst_w || src_h != dst_h);
10981}
d21fbe87 10982
da20eabd
ML
10983int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
10984 struct drm_plane_state *plane_state)
10985{
ab1d3a0e 10986 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
da20eabd
ML
10987 struct drm_crtc *crtc = crtc_state->crtc;
10988 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
e9728bd8 10989 struct intel_plane *plane = to_intel_plane(plane_state->plane);
da20eabd 10990 struct drm_device *dev = crtc->dev;
ed4a6a7c 10991 struct drm_i915_private *dev_priv = to_i915(dev);
da20eabd 10992 struct intel_plane_state *old_plane_state =
e9728bd8 10993 to_intel_plane_state(plane->base.state);
da20eabd
ML
10994 bool mode_changed = needs_modeset(crtc_state);
10995 bool was_crtc_enabled = crtc->state->active;
10996 bool is_crtc_enabled = crtc_state->active;
da20eabd
ML
10997 bool turn_off, turn_on, visible, was_visible;
10998 struct drm_framebuffer *fb = plane_state->fb;
78108b7c 10999 int ret;
da20eabd 11000
e9728bd8 11001 if (INTEL_GEN(dev_priv) >= 9 && plane->id != PLANE_CURSOR) {
da20eabd
ML
11002 ret = skl_update_scaler_plane(
11003 to_intel_crtc_state(crtc_state),
11004 to_intel_plane_state(plane_state));
11005 if (ret)
11006 return ret;
11007 }
11008
936e71e3 11009 was_visible = old_plane_state->base.visible;
1d4258db 11010 visible = plane_state->visible;
da20eabd
ML
11011
11012 if (!was_crtc_enabled && WARN_ON(was_visible))
11013 was_visible = false;
11014
35c08f43
ML
11015 /*
11016 * Visibility is calculated as if the crtc was on, but
11017 * after scaler setup everything depends on it being off
11018 * when the crtc isn't active.
f818ffea
VS
11019 *
11020 * FIXME this is wrong for watermarks. Watermarks should also
11021 * be computed as if the pipe would be active. Perhaps move
11022 * per-plane wm computation to the .check_plane() hook, and
11023 * only combine the results from all planes in the current place?
35c08f43 11024 */
e9728bd8 11025 if (!is_crtc_enabled) {
1d4258db 11026 plane_state->visible = visible = false;
e9728bd8
VS
11027 to_intel_crtc_state(crtc_state)->active_planes &= ~BIT(plane->id);
11028 }
da20eabd
ML
11029
11030 if (!was_visible && !visible)
11031 return 0;
11032
e8861675
ML
11033 if (fb != old_plane_state->base.fb)
11034 pipe_config->fb_changed = true;
11035
da20eabd
ML
11036 turn_off = was_visible && (!visible || mode_changed);
11037 turn_on = visible && (!was_visible || mode_changed);
11038
72660ce0 11039 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
e9728bd8
VS
11040 intel_crtc->base.base.id, intel_crtc->base.name,
11041 plane->base.base.id, plane->base.name,
72660ce0 11042 fb ? fb->base.id : -1);
da20eabd 11043
72660ce0 11044 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
e9728bd8 11045 plane->base.base.id, plane->base.name,
72660ce0 11046 was_visible, visible,
da20eabd
ML
11047 turn_off, turn_on, mode_changed);
11048
caed361d 11049 if (turn_on) {
04548cba 11050 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
b4ede6df 11051 pipe_config->update_wm_pre = true;
caed361d
VS
11052
11053 /* must disable cxsr around plane enable/disable */
e9728bd8 11054 if (plane->id != PLANE_CURSOR)
caed361d
VS
11055 pipe_config->disable_cxsr = true;
11056 } else if (turn_off) {
04548cba 11057 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
b4ede6df 11058 pipe_config->update_wm_post = true;
92826fcd 11059
852eb00d 11060 /* must disable cxsr around plane enable/disable */
e9728bd8 11061 if (plane->id != PLANE_CURSOR)
ab1d3a0e 11062 pipe_config->disable_cxsr = true;
e9728bd8 11063 } else if (intel_wm_need_update(&plane->base, plane_state)) {
04548cba 11064 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
b4ede6df
VS
11065 /* FIXME bollocks */
11066 pipe_config->update_wm_pre = true;
11067 pipe_config->update_wm_post = true;
11068 }
852eb00d 11069 }
da20eabd 11070
8be6ca85 11071 if (visible || was_visible)
e9728bd8 11072 pipe_config->fb_bits |= plane->frontbuffer_bit;
a9ff8714 11073
31ae71fc
ML
11074 /*
11075 * WaCxSRDisabledForSpriteScaling:ivb
11076 *
11077 * cstate->update_wm was already set above, so this flag will
11078 * take effect when we commit and program watermarks.
11079 */
e9728bd8 11080 if (plane->id == PLANE_SPRITE0 && IS_IVYBRIDGE(dev_priv) &&
31ae71fc
ML
11081 needs_scaling(to_intel_plane_state(plane_state)) &&
11082 !needs_scaling(old_plane_state))
11083 pipe_config->disable_lp_wm = true;
d21fbe87 11084
da20eabd
ML
11085 return 0;
11086}
11087
6d3a1ce7
ML
11088static bool encoders_cloneable(const struct intel_encoder *a,
11089 const struct intel_encoder *b)
11090{
11091 /* masks could be asymmetric, so check both ways */
11092 return a == b || (a->cloneable & (1 << b->type) &&
11093 b->cloneable & (1 << a->type));
11094}
11095
11096static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11097 struct intel_crtc *crtc,
11098 struct intel_encoder *encoder)
11099{
11100 struct intel_encoder *source_encoder;
11101 struct drm_connector *connector;
11102 struct drm_connector_state *connector_state;
11103 int i;
11104
aa5e9b47 11105 for_each_new_connector_in_state(state, connector, connector_state, i) {
6d3a1ce7
ML
11106 if (connector_state->crtc != &crtc->base)
11107 continue;
11108
11109 source_encoder =
11110 to_intel_encoder(connector_state->best_encoder);
11111 if (!encoders_cloneable(encoder, source_encoder))
11112 return false;
11113 }
11114
11115 return true;
11116}
11117
6d3a1ce7
ML
11118static int intel_crtc_atomic_check(struct drm_crtc *crtc,
11119 struct drm_crtc_state *crtc_state)
11120{
cf5a15be 11121 struct drm_device *dev = crtc->dev;
fac5e23e 11122 struct drm_i915_private *dev_priv = to_i915(dev);
6d3a1ce7 11123 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cf5a15be
ML
11124 struct intel_crtc_state *pipe_config =
11125 to_intel_crtc_state(crtc_state);
6d3a1ce7 11126 struct drm_atomic_state *state = crtc_state->state;
4d20cd86 11127 int ret;
6d3a1ce7
ML
11128 bool mode_changed = needs_modeset(crtc_state);
11129
852eb00d 11130 if (mode_changed && !crtc_state->active)
caed361d 11131 pipe_config->update_wm_post = true;
eddfcbcd 11132
ad421372
ML
11133 if (mode_changed && crtc_state->enable &&
11134 dev_priv->display.crtc_compute_clock &&
8106ddbd 11135 !WARN_ON(pipe_config->shared_dpll)) {
ad421372
ML
11136 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
11137 pipe_config);
11138 if (ret)
11139 return ret;
11140 }
11141
82cf435b
LL
11142 if (crtc_state->color_mgmt_changed) {
11143 ret = intel_color_check(crtc, crtc_state);
11144 if (ret)
11145 return ret;
e7852a4b
LL
11146
11147 /*
11148 * Changing color management on Intel hardware is
11149 * handled as part of planes update.
11150 */
11151 crtc_state->planes_changed = true;
82cf435b
LL
11152 }
11153
e435d6e5 11154 ret = 0;
86c8bbbe 11155 if (dev_priv->display.compute_pipe_wm) {
e3bddded 11156 ret = dev_priv->display.compute_pipe_wm(pipe_config);
ed4a6a7c
MR
11157 if (ret) {
11158 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
11159 return ret;
11160 }
11161 }
11162
11163 if (dev_priv->display.compute_intermediate_wm &&
11164 !to_intel_atomic_state(state)->skip_intermediate_wm) {
11165 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
11166 return 0;
11167
11168 /*
11169 * Calculate 'intermediate' watermarks that satisfy both the
11170 * old state and the new state. We can program these
11171 * immediately.
11172 */
6315b5d3 11173 ret = dev_priv->display.compute_intermediate_wm(dev,
ed4a6a7c
MR
11174 intel_crtc,
11175 pipe_config);
11176 if (ret) {
11177 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
86c8bbbe 11178 return ret;
ed4a6a7c 11179 }
e3d5457c
VS
11180 } else if (dev_priv->display.compute_intermediate_wm) {
11181 if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
11182 pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
86c8bbbe
MR
11183 }
11184
6315b5d3 11185 if (INTEL_GEN(dev_priv) >= 9) {
e435d6e5
ML
11186 if (mode_changed)
11187 ret = skl_update_scaler_crtc(pipe_config);
11188
73b0ca8e
MK
11189 if (!ret)
11190 ret = skl_check_pipe_max_pixel_rate(intel_crtc,
11191 pipe_config);
e435d6e5 11192 if (!ret)
6ebc6923 11193 ret = intel_atomic_setup_scalers(dev_priv, intel_crtc,
e435d6e5
ML
11194 pipe_config);
11195 }
11196
11197 return ret;
6d3a1ce7
ML
11198}
11199
65b38e0d 11200static const struct drm_crtc_helper_funcs intel_helper_funcs = {
5a21b665
DV
11201 .atomic_begin = intel_begin_crtc_commit,
11202 .atomic_flush = intel_finish_crtc_commit,
6d3a1ce7 11203 .atomic_check = intel_crtc_atomic_check,
f6e5b160
CW
11204};
11205
d29b2f9d
ACO
11206static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11207{
11208 struct intel_connector *connector;
f9e905ca 11209 struct drm_connector_list_iter conn_iter;
d29b2f9d 11210
f9e905ca
DV
11211 drm_connector_list_iter_begin(dev, &conn_iter);
11212 for_each_intel_connector_iter(connector, &conn_iter) {
8863dc7f
DV
11213 if (connector->base.state->crtc)
11214 drm_connector_unreference(&connector->base);
11215
d29b2f9d
ACO
11216 if (connector->base.encoder) {
11217 connector->base.state->best_encoder =
11218 connector->base.encoder;
11219 connector->base.state->crtc =
11220 connector->base.encoder->crtc;
8863dc7f
DV
11221
11222 drm_connector_reference(&connector->base);
d29b2f9d
ACO
11223 } else {
11224 connector->base.state->best_encoder = NULL;
11225 connector->base.state->crtc = NULL;
11226 }
11227 }
f9e905ca 11228 drm_connector_list_iter_end(&conn_iter);
d29b2f9d
ACO
11229}
11230
050f7aeb 11231static void
eba905b2 11232connected_sink_compute_bpp(struct intel_connector *connector,
5cec258b 11233 struct intel_crtc_state *pipe_config)
050f7aeb 11234{
6a2a5c5d 11235 const struct drm_display_info *info = &connector->base.display_info;
050f7aeb
DV
11236 int bpp = pipe_config->pipe_bpp;
11237
11238 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
6a2a5c5d
VS
11239 connector->base.base.id,
11240 connector->base.name);
050f7aeb
DV
11241
11242 /* Don't use an invalid EDID bpc value */
6a2a5c5d 11243 if (info->bpc != 0 && info->bpc * 3 < bpp) {
050f7aeb 11244 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
6a2a5c5d
VS
11245 bpp, info->bpc * 3);
11246 pipe_config->pipe_bpp = info->bpc * 3;
050f7aeb
DV
11247 }
11248
196f954e 11249 /* Clamp bpp to 8 on screens without EDID 1.4 */
6a2a5c5d 11250 if (info->bpc == 0 && bpp > 24) {
196f954e
MK
11251 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
11252 bpp);
11253 pipe_config->pipe_bpp = 24;
050f7aeb
DV
11254 }
11255}
11256
4e53c2e0 11257static int
050f7aeb 11258compute_baseline_pipe_bpp(struct intel_crtc *crtc,
5cec258b 11259 struct intel_crtc_state *pipe_config)
4e53c2e0 11260{
9beb5fea 11261 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1486017f 11262 struct drm_atomic_state *state;
da3ced29
ACO
11263 struct drm_connector *connector;
11264 struct drm_connector_state *connector_state;
1486017f 11265 int bpp, i;
4e53c2e0 11266
9beb5fea
TU
11267 if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
11268 IS_CHERRYVIEW(dev_priv)))
4e53c2e0 11269 bpp = 10*3;
9beb5fea 11270 else if (INTEL_GEN(dev_priv) >= 5)
d328c9d7
DV
11271 bpp = 12*3;
11272 else
11273 bpp = 8*3;
11274
4e53c2e0 11275
4e53c2e0
DV
11276 pipe_config->pipe_bpp = bpp;
11277
1486017f
ACO
11278 state = pipe_config->base.state;
11279
4e53c2e0 11280 /* Clamp display bpp to EDID value */
aa5e9b47 11281 for_each_new_connector_in_state(state, connector, connector_state, i) {
da3ced29 11282 if (connector_state->crtc != &crtc->base)
4e53c2e0
DV
11283 continue;
11284
da3ced29
ACO
11285 connected_sink_compute_bpp(to_intel_connector(connector),
11286 pipe_config);
4e53c2e0
DV
11287 }
11288
11289 return bpp;
11290}
11291
644db711
DV
11292static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11293{
11294 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11295 "type: 0x%x flags: 0x%x\n",
1342830c 11296 mode->crtc_clock,
644db711
DV
11297 mode->crtc_hdisplay, mode->crtc_hsync_start,
11298 mode->crtc_hsync_end, mode->crtc_htotal,
11299 mode->crtc_vdisplay, mode->crtc_vsync_start,
11300 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
11301}
11302
f6982332
TU
11303static inline void
11304intel_dump_m_n_config(struct intel_crtc_state *pipe_config, char *id,
a4309657 11305 unsigned int lane_count, struct intel_link_m_n *m_n)
f6982332 11306{
a4309657
TU
11307 DRM_DEBUG_KMS("%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11308 id, lane_count,
f6982332
TU
11309 m_n->gmch_m, m_n->gmch_n,
11310 m_n->link_m, m_n->link_n, m_n->tu);
11311}
11312
c0b03411 11313static void intel_dump_pipe_config(struct intel_crtc *crtc,
5cec258b 11314 struct intel_crtc_state *pipe_config,
c0b03411
DV
11315 const char *context)
11316{
6a60cd87 11317 struct drm_device *dev = crtc->base.dev;
4f8036a2 11318 struct drm_i915_private *dev_priv = to_i915(dev);
6a60cd87
CK
11319 struct drm_plane *plane;
11320 struct intel_plane *intel_plane;
11321 struct intel_plane_state *state;
11322 struct drm_framebuffer *fb;
11323
66766e4f
TU
11324 DRM_DEBUG_KMS("[CRTC:%d:%s]%s\n",
11325 crtc->base.base.id, crtc->base.name, context);
c0b03411 11326
2c89429e
TU
11327 DRM_DEBUG_KMS("cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
11328 transcoder_name(pipe_config->cpu_transcoder),
c0b03411 11329 pipe_config->pipe_bpp, pipe_config->dither);
a4309657
TU
11330
11331 if (pipe_config->has_pch_encoder)
11332 intel_dump_m_n_config(pipe_config, "fdi",
11333 pipe_config->fdi_lanes,
11334 &pipe_config->fdi_m_n);
f6982332
TU
11335
11336 if (intel_crtc_has_dp_encoder(pipe_config)) {
a4309657
TU
11337 intel_dump_m_n_config(pipe_config, "dp m_n",
11338 pipe_config->lane_count, &pipe_config->dp_m_n);
d806e682
TU
11339 if (pipe_config->has_drrs)
11340 intel_dump_m_n_config(pipe_config, "dp m2_n2",
11341 pipe_config->lane_count,
11342 &pipe_config->dp_m2_n2);
f6982332 11343 }
b95af8be 11344
55072d19 11345 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
2c89429e 11346 pipe_config->has_audio, pipe_config->has_infoframe);
55072d19 11347
c0b03411 11348 DRM_DEBUG_KMS("requested mode:\n");
2d112de7 11349 drm_mode_debug_printmodeline(&pipe_config->base.mode);
c0b03411 11350 DRM_DEBUG_KMS("adjusted mode:\n");
2d112de7
ACO
11351 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
11352 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
a7d1b3f4 11353 DRM_DEBUG_KMS("port clock: %d, pipe src size: %dx%d, pixel rate %d\n",
2c89429e 11354 pipe_config->port_clock,
a7d1b3f4
VS
11355 pipe_config->pipe_src_w, pipe_config->pipe_src_h,
11356 pipe_config->pixel_rate);
dd2f616d
TU
11357
11358 if (INTEL_GEN(dev_priv) >= 9)
11359 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
11360 crtc->num_scalers,
11361 pipe_config->scaler_state.scaler_users,
11362 pipe_config->scaler_state.scaler_id);
a74f8375
TU
11363
11364 if (HAS_GMCH_DISPLAY(dev_priv))
11365 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
11366 pipe_config->gmch_pfit.control,
11367 pipe_config->gmch_pfit.pgm_ratios,
11368 pipe_config->gmch_pfit.lvds_border_bits);
11369 else
11370 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
11371 pipe_config->pch_pfit.pos,
11372 pipe_config->pch_pfit.size,
08c4d7fc 11373 enableddisabled(pipe_config->pch_pfit.enabled));
a74f8375 11374
2c89429e
TU
11375 DRM_DEBUG_KMS("ips: %i, double wide: %i\n",
11376 pipe_config->ips_enabled, pipe_config->double_wide);
6a60cd87 11377
f50b79f0 11378 intel_dpll_dump_hw_state(dev_priv, &pipe_config->dpll_hw_state);
415ff0f6 11379
6a60cd87
CK
11380 DRM_DEBUG_KMS("planes on this crtc\n");
11381 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
b3c11ac2 11382 struct drm_format_name_buf format_name;
6a60cd87
CK
11383 intel_plane = to_intel_plane(plane);
11384 if (intel_plane->pipe != crtc->pipe)
11385 continue;
11386
11387 state = to_intel_plane_state(plane->state);
11388 fb = state->base.fb;
11389 if (!fb) {
1d577e02
VS
11390 DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
11391 plane->base.id, plane->name, state->scaler_id);
6a60cd87
CK
11392 continue;
11393 }
11394
dd2f616d
TU
11395 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d, fb = %ux%u format = %s\n",
11396 plane->base.id, plane->name,
b3c11ac2 11397 fb->base.id, fb->width, fb->height,
438b74a5 11398 drm_get_format_name(fb->format->format, &format_name));
dd2f616d
TU
11399 if (INTEL_GEN(dev_priv) >= 9)
11400 DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
11401 state->scaler_id,
11402 state->base.src.x1 >> 16,
11403 state->base.src.y1 >> 16,
11404 drm_rect_width(&state->base.src) >> 16,
11405 drm_rect_height(&state->base.src) >> 16,
11406 state->base.dst.x1, state->base.dst.y1,
11407 drm_rect_width(&state->base.dst),
11408 drm_rect_height(&state->base.dst));
6a60cd87 11409 }
c0b03411
DV
11410}
11411
5448a00d 11412static bool check_digital_port_conflicts(struct drm_atomic_state *state)
00f0b378 11413{
5448a00d 11414 struct drm_device *dev = state->dev;
da3ced29 11415 struct drm_connector *connector;
00f0b378 11416 unsigned int used_ports = 0;
477321e0 11417 unsigned int used_mst_ports = 0;
00f0b378
VS
11418
11419 /*
11420 * Walk the connector list instead of the encoder
11421 * list to detect the problem on ddi platforms
11422 * where there's just one encoder per digital port.
11423 */
0bff4858
VS
11424 drm_for_each_connector(connector, dev) {
11425 struct drm_connector_state *connector_state;
11426 struct intel_encoder *encoder;
11427
11428 connector_state = drm_atomic_get_existing_connector_state(state, connector);
11429 if (!connector_state)
11430 connector_state = connector->state;
11431
5448a00d 11432 if (!connector_state->best_encoder)
00f0b378
VS
11433 continue;
11434
5448a00d
ACO
11435 encoder = to_intel_encoder(connector_state->best_encoder);
11436
11437 WARN_ON(!connector_state->crtc);
00f0b378
VS
11438
11439 switch (encoder->type) {
11440 unsigned int port_mask;
11441 case INTEL_OUTPUT_UNKNOWN:
4f8036a2 11442 if (WARN_ON(!HAS_DDI(to_i915(dev))))
00f0b378 11443 break;
cca0502b 11444 case INTEL_OUTPUT_DP:
00f0b378
VS
11445 case INTEL_OUTPUT_HDMI:
11446 case INTEL_OUTPUT_EDP:
11447 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
11448
11449 /* the same port mustn't appear more than once */
11450 if (used_ports & port_mask)
11451 return false;
11452
11453 used_ports |= port_mask;
477321e0
VS
11454 break;
11455 case INTEL_OUTPUT_DP_MST:
11456 used_mst_ports |=
11457 1 << enc_to_mst(&encoder->base)->primary->port;
11458 break;
00f0b378
VS
11459 default:
11460 break;
11461 }
11462 }
11463
477321e0
VS
11464 /* can't mix MST and SST/HDMI on the same port */
11465 if (used_ports & used_mst_ports)
11466 return false;
11467
00f0b378
VS
11468 return true;
11469}
11470
83a57153
ACO
11471static void
11472clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
11473{
ff32c54e
VS
11474 struct drm_i915_private *dev_priv =
11475 to_i915(crtc_state->base.crtc->dev);
663a3640 11476 struct intel_crtc_scaler_state scaler_state;
4978cc93 11477 struct intel_dpll_hw_state dpll_hw_state;
8106ddbd 11478 struct intel_shared_dpll *shared_dpll;
ff32c54e 11479 struct intel_crtc_wm_state wm_state;
c4e2d043 11480 bool force_thru;
83a57153 11481
7546a384
ACO
11482 /* FIXME: before the switch to atomic started, a new pipe_config was
11483 * kzalloc'd. Code that depends on any field being zero should be
11484 * fixed, so that the crtc_state can be safely duplicated. For now,
11485 * only fields that are know to not cause problems are preserved. */
11486
663a3640 11487 scaler_state = crtc_state->scaler_state;
4978cc93
ACO
11488 shared_dpll = crtc_state->shared_dpll;
11489 dpll_hw_state = crtc_state->dpll_hw_state;
c4e2d043 11490 force_thru = crtc_state->pch_pfit.force_thru;
04548cba
VS
11491 if (IS_G4X(dev_priv) ||
11492 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
ff32c54e 11493 wm_state = crtc_state->wm;
4978cc93 11494
d2fa80a5
CW
11495 /* Keep base drm_crtc_state intact, only clear our extended struct */
11496 BUILD_BUG_ON(offsetof(struct intel_crtc_state, base));
11497 memset(&crtc_state->base + 1, 0,
11498 sizeof(*crtc_state) - sizeof(crtc_state->base));
4978cc93 11499
663a3640 11500 crtc_state->scaler_state = scaler_state;
4978cc93
ACO
11501 crtc_state->shared_dpll = shared_dpll;
11502 crtc_state->dpll_hw_state = dpll_hw_state;
c4e2d043 11503 crtc_state->pch_pfit.force_thru = force_thru;
04548cba
VS
11504 if (IS_G4X(dev_priv) ||
11505 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
ff32c54e 11506 crtc_state->wm = wm_state;
83a57153
ACO
11507}
11508
548ee15b 11509static int
b8cecdf5 11510intel_modeset_pipe_config(struct drm_crtc *crtc,
b359283a 11511 struct intel_crtc_state *pipe_config)
ee7b9f93 11512{
b359283a 11513 struct drm_atomic_state *state = pipe_config->base.state;
7758a113 11514 struct intel_encoder *encoder;
da3ced29 11515 struct drm_connector *connector;
0b901879 11516 struct drm_connector_state *connector_state;
d328c9d7 11517 int base_bpp, ret = -EINVAL;
0b901879 11518 int i;
e29c22c0 11519 bool retry = true;
ee7b9f93 11520
83a57153 11521 clear_intel_crtc_state(pipe_config);
7758a113 11522
e143a21c
DV
11523 pipe_config->cpu_transcoder =
11524 (enum transcoder) to_intel_crtc(crtc)->pipe;
b8cecdf5 11525
2960bc9c
ID
11526 /*
11527 * Sanitize sync polarity flags based on requested ones. If neither
11528 * positive or negative polarity is requested, treat this as meaning
11529 * negative polarity.
11530 */
2d112de7 11531 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 11532 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
2d112de7 11533 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
2960bc9c 11534
2d112de7 11535 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 11536 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
2d112de7 11537 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
2960bc9c 11538
d328c9d7
DV
11539 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
11540 pipe_config);
11541 if (base_bpp < 0)
4e53c2e0
DV
11542 goto fail;
11543
e41a56be
VS
11544 /*
11545 * Determine the real pipe dimensions. Note that stereo modes can
11546 * increase the actual pipe size due to the frame doubling and
11547 * insertion of additional space for blanks between the frame. This
11548 * is stored in the crtc timings. We use the requested mode to do this
11549 * computation to clearly distinguish it from the adjusted mode, which
11550 * can be changed by the connectors in the below retry loop.
11551 */
196cd5d3 11552 drm_mode_get_hv_timing(&pipe_config->base.mode,
ecb7e16b
GP
11553 &pipe_config->pipe_src_w,
11554 &pipe_config->pipe_src_h);
e41a56be 11555
aa5e9b47 11556 for_each_new_connector_in_state(state, connector, connector_state, i) {
253c84c8
VS
11557 if (connector_state->crtc != crtc)
11558 continue;
11559
11560 encoder = to_intel_encoder(connector_state->best_encoder);
11561
e25148d0
VS
11562 if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
11563 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11564 goto fail;
11565 }
11566
253c84c8
VS
11567 /*
11568 * Determine output_types before calling the .compute_config()
11569 * hooks so that the hooks can use this information safely.
11570 */
11571 pipe_config->output_types |= 1 << encoder->type;
11572 }
11573
e29c22c0 11574encoder_retry:
ef1b460d 11575 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 11576 pipe_config->port_clock = 0;
ef1b460d 11577 pipe_config->pixel_multiplier = 1;
ff9a6750 11578
135c81b8 11579 /* Fill in default crtc timings, allow encoders to overwrite them. */
2d112de7
ACO
11580 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
11581 CRTC_STEREO_DOUBLE);
135c81b8 11582
7758a113
DV
11583 /* Pass our mode to the connectors and the CRTC to give them a chance to
11584 * adjust it according to limitations or connector properties, and also
11585 * a chance to reject the mode entirely.
47f1c6c9 11586 */
aa5e9b47 11587 for_each_new_connector_in_state(state, connector, connector_state, i) {
0b901879 11588 if (connector_state->crtc != crtc)
7758a113 11589 continue;
7ae89233 11590
0b901879
ACO
11591 encoder = to_intel_encoder(connector_state->best_encoder);
11592
0a478c27 11593 if (!(encoder->compute_config(encoder, pipe_config, connector_state))) {
efea6e8e 11594 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
DV
11595 goto fail;
11596 }
ee7b9f93 11597 }
47f1c6c9 11598
ff9a6750
DV
11599 /* Set default port clock if not overwritten by the encoder. Needs to be
11600 * done afterwards in case the encoder adjusts the mode. */
11601 if (!pipe_config->port_clock)
2d112de7 11602 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
241bfc38 11603 * pipe_config->pixel_multiplier;
ff9a6750 11604
a43f6e0f 11605 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 11606 if (ret < 0) {
7758a113
DV
11607 DRM_DEBUG_KMS("CRTC fixup failed\n");
11608 goto fail;
ee7b9f93 11609 }
e29c22c0
DV
11610
11611 if (ret == RETRY) {
11612 if (WARN(!retry, "loop in pipe configuration computation\n")) {
11613 ret = -EINVAL;
11614 goto fail;
11615 }
11616
11617 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
11618 retry = false;
11619 goto encoder_retry;
11620 }
11621
e8fa4270 11622 /* Dithering seems to not pass-through bits correctly when it should, so
611032bf
MN
11623 * only enable it on 6bpc panels and when its not a compliance
11624 * test requesting 6bpc video pattern.
11625 */
11626 pipe_config->dither = (pipe_config->pipe_bpp == 6*3) &&
11627 !pipe_config->dither_force_disable;
62f0ace5 11628 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
d328c9d7 11629 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
4e53c2e0 11630
7758a113 11631fail:
548ee15b 11632 return ret;
ee7b9f93 11633}
47f1c6c9 11634
ea9d758d 11635static void
4740b0f2 11636intel_modeset_update_crtc_state(struct drm_atomic_state *state)
ea9d758d 11637{
0a9ab303 11638 struct drm_crtc *crtc;
aa5e9b47 11639 struct drm_crtc_state *new_crtc_state;
8a75d157 11640 int i;
ea9d758d 11641
7668851f 11642 /* Double check state. */
aa5e9b47
ML
11643 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
11644 to_intel_crtc(crtc)->config = to_intel_crtc_state(new_crtc_state);
fc467a22 11645
61067a5e
ML
11646 /*
11647 * Update legacy state to satisfy fbc code. This can
11648 * be removed when fbc uses the atomic state.
11649 */
11650 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
11651 struct drm_plane_state *plane_state = crtc->primary->state;
11652
11653 crtc->primary->fb = plane_state->fb;
11654 crtc->x = plane_state->src_x >> 16;
11655 crtc->y = plane_state->src_y >> 16;
11656 }
ea9d758d 11657 }
ea9d758d
DV
11658}
11659
3bd26263 11660static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 11661{
3bd26263 11662 int diff;
f1f644dc
JB
11663
11664 if (clock1 == clock2)
11665 return true;
11666
11667 if (!clock1 || !clock2)
11668 return false;
11669
11670 diff = abs(clock1 - clock2);
11671
11672 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
11673 return true;
11674
11675 return false;
11676}
11677
cfb23ed6
ML
11678static bool
11679intel_compare_m_n(unsigned int m, unsigned int n,
11680 unsigned int m2, unsigned int n2,
11681 bool exact)
11682{
11683 if (m == m2 && n == n2)
11684 return true;
11685
11686 if (exact || !m || !n || !m2 || !n2)
11687 return false;
11688
11689 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
11690
31d10b57
ML
11691 if (n > n2) {
11692 while (n > n2) {
cfb23ed6
ML
11693 m2 <<= 1;
11694 n2 <<= 1;
11695 }
31d10b57
ML
11696 } else if (n < n2) {
11697 while (n < n2) {
cfb23ed6
ML
11698 m <<= 1;
11699 n <<= 1;
11700 }
11701 }
11702
31d10b57
ML
11703 if (n != n2)
11704 return false;
11705
11706 return intel_fuzzy_clock_check(m, m2);
cfb23ed6
ML
11707}
11708
11709static bool
11710intel_compare_link_m_n(const struct intel_link_m_n *m_n,
11711 struct intel_link_m_n *m2_n2,
11712 bool adjust)
11713{
11714 if (m_n->tu == m2_n2->tu &&
11715 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
11716 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
11717 intel_compare_m_n(m_n->link_m, m_n->link_n,
11718 m2_n2->link_m, m2_n2->link_n, !adjust)) {
11719 if (adjust)
11720 *m2_n2 = *m_n;
11721
11722 return true;
11723 }
11724
11725 return false;
11726}
11727
4e8048f8
TU
11728static void __printf(3, 4)
11729pipe_config_err(bool adjust, const char *name, const char *format, ...)
11730{
11731 char *level;
11732 unsigned int category;
11733 struct va_format vaf;
11734 va_list args;
11735
11736 if (adjust) {
11737 level = KERN_DEBUG;
11738 category = DRM_UT_KMS;
11739 } else {
11740 level = KERN_ERR;
11741 category = DRM_UT_NONE;
11742 }
11743
11744 va_start(args, format);
11745 vaf.fmt = format;
11746 vaf.va = &args;
11747
11748 drm_printk(level, category, "mismatch in %s %pV", name, &vaf);
11749
11750 va_end(args);
11751}
11752
0e8ffe1b 11753static bool
6315b5d3 11754intel_pipe_config_compare(struct drm_i915_private *dev_priv,
5cec258b 11755 struct intel_crtc_state *current_config,
cfb23ed6
ML
11756 struct intel_crtc_state *pipe_config,
11757 bool adjust)
0e8ffe1b 11758{
cfb23ed6
ML
11759 bool ret = true;
11760
66e985c0
DV
11761#define PIPE_CONF_CHECK_X(name) \
11762 if (current_config->name != pipe_config->name) { \
4e8048f8 11763 pipe_config_err(adjust, __stringify(name), \
66e985c0
DV
11764 "(expected 0x%08x, found 0x%08x)\n", \
11765 current_config->name, \
11766 pipe_config->name); \
cfb23ed6 11767 ret = false; \
66e985c0
DV
11768 }
11769
08a24034
DV
11770#define PIPE_CONF_CHECK_I(name) \
11771 if (current_config->name != pipe_config->name) { \
4e8048f8 11772 pipe_config_err(adjust, __stringify(name), \
08a24034
DV
11773 "(expected %i, found %i)\n", \
11774 current_config->name, \
11775 pipe_config->name); \
cfb23ed6
ML
11776 ret = false; \
11777 }
11778
8106ddbd
ACO
11779#define PIPE_CONF_CHECK_P(name) \
11780 if (current_config->name != pipe_config->name) { \
4e8048f8 11781 pipe_config_err(adjust, __stringify(name), \
8106ddbd
ACO
11782 "(expected %p, found %p)\n", \
11783 current_config->name, \
11784 pipe_config->name); \
11785 ret = false; \
11786 }
11787
cfb23ed6
ML
11788#define PIPE_CONF_CHECK_M_N(name) \
11789 if (!intel_compare_link_m_n(&current_config->name, \
11790 &pipe_config->name,\
11791 adjust)) { \
4e8048f8 11792 pipe_config_err(adjust, __stringify(name), \
cfb23ed6
ML
11793 "(expected tu %i gmch %i/%i link %i/%i, " \
11794 "found tu %i, gmch %i/%i link %i/%i)\n", \
11795 current_config->name.tu, \
11796 current_config->name.gmch_m, \
11797 current_config->name.gmch_n, \
11798 current_config->name.link_m, \
11799 current_config->name.link_n, \
11800 pipe_config->name.tu, \
11801 pipe_config->name.gmch_m, \
11802 pipe_config->name.gmch_n, \
11803 pipe_config->name.link_m, \
11804 pipe_config->name.link_n); \
11805 ret = false; \
11806 }
11807
55c561a7
DV
11808/* This is required for BDW+ where there is only one set of registers for
11809 * switching between high and low RR.
11810 * This macro can be used whenever a comparison has to be made between one
11811 * hw state and multiple sw state variables.
11812 */
cfb23ed6
ML
11813#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
11814 if (!intel_compare_link_m_n(&current_config->name, \
11815 &pipe_config->name, adjust) && \
11816 !intel_compare_link_m_n(&current_config->alt_name, \
11817 &pipe_config->name, adjust)) { \
4e8048f8 11818 pipe_config_err(adjust, __stringify(name), \
cfb23ed6
ML
11819 "(expected tu %i gmch %i/%i link %i/%i, " \
11820 "or tu %i gmch %i/%i link %i/%i, " \
11821 "found tu %i, gmch %i/%i link %i/%i)\n", \
11822 current_config->name.tu, \
11823 current_config->name.gmch_m, \
11824 current_config->name.gmch_n, \
11825 current_config->name.link_m, \
11826 current_config->name.link_n, \
11827 current_config->alt_name.tu, \
11828 current_config->alt_name.gmch_m, \
11829 current_config->alt_name.gmch_n, \
11830 current_config->alt_name.link_m, \
11831 current_config->alt_name.link_n, \
11832 pipe_config->name.tu, \
11833 pipe_config->name.gmch_m, \
11834 pipe_config->name.gmch_n, \
11835 pipe_config->name.link_m, \
11836 pipe_config->name.link_n); \
11837 ret = false; \
88adfff1
DV
11838 }
11839
1bd1bd80
DV
11840#define PIPE_CONF_CHECK_FLAGS(name, mask) \
11841 if ((current_config->name ^ pipe_config->name) & (mask)) { \
4e8048f8
TU
11842 pipe_config_err(adjust, __stringify(name), \
11843 "(%x) (expected %i, found %i)\n", \
11844 (mask), \
1bd1bd80
DV
11845 current_config->name & (mask), \
11846 pipe_config->name & (mask)); \
cfb23ed6 11847 ret = false; \
1bd1bd80
DV
11848 }
11849
5e550656
VS
11850#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
11851 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
4e8048f8 11852 pipe_config_err(adjust, __stringify(name), \
5e550656
VS
11853 "(expected %i, found %i)\n", \
11854 current_config->name, \
11855 pipe_config->name); \
cfb23ed6 11856 ret = false; \
5e550656
VS
11857 }
11858
bb760063
DV
11859#define PIPE_CONF_QUIRK(quirk) \
11860 ((current_config->quirks | pipe_config->quirks) & (quirk))
11861
eccb140b
DV
11862 PIPE_CONF_CHECK_I(cpu_transcoder);
11863
08a24034
DV
11864 PIPE_CONF_CHECK_I(has_pch_encoder);
11865 PIPE_CONF_CHECK_I(fdi_lanes);
cfb23ed6 11866 PIPE_CONF_CHECK_M_N(fdi_m_n);
08a24034 11867
90a6b7b0 11868 PIPE_CONF_CHECK_I(lane_count);
95a7a2ae 11869 PIPE_CONF_CHECK_X(lane_lat_optim_mask);
b95af8be 11870
6315b5d3 11871 if (INTEL_GEN(dev_priv) < 8) {
cfb23ed6
ML
11872 PIPE_CONF_CHECK_M_N(dp_m_n);
11873
cfb23ed6
ML
11874 if (current_config->has_drrs)
11875 PIPE_CONF_CHECK_M_N(dp_m2_n2);
11876 } else
11877 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
eb14cb74 11878
253c84c8 11879 PIPE_CONF_CHECK_X(output_types);
a65347ba 11880
2d112de7
ACO
11881 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
11882 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
11883 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
11884 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
11885 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
11886 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
1bd1bd80 11887
2d112de7
ACO
11888 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
11889 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
11890 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
11891 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
11892 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
11893 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
1bd1bd80 11894
c93f54cf 11895 PIPE_CONF_CHECK_I(pixel_multiplier);
6897b4b5 11896 PIPE_CONF_CHECK_I(has_hdmi_sink);
772c2a51 11897 if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
920a14b2 11898 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
b5a9fa09 11899 PIPE_CONF_CHECK_I(limited_color_range);
15953637
SS
11900
11901 PIPE_CONF_CHECK_I(hdmi_scrambling);
11902 PIPE_CONF_CHECK_I(hdmi_high_tmds_clock_ratio);
e43823ec 11903 PIPE_CONF_CHECK_I(has_infoframe);
6c49f241 11904
9ed109a7
DV
11905 PIPE_CONF_CHECK_I(has_audio);
11906
2d112de7 11907 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
1bd1bd80
DV
11908 DRM_MODE_FLAG_INTERLACE);
11909
bb760063 11910 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
2d112de7 11911 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 11912 DRM_MODE_FLAG_PHSYNC);
2d112de7 11913 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 11914 DRM_MODE_FLAG_NHSYNC);
2d112de7 11915 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 11916 DRM_MODE_FLAG_PVSYNC);
2d112de7 11917 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063
DV
11918 DRM_MODE_FLAG_NVSYNC);
11919 }
045ac3b5 11920
333b8ca8 11921 PIPE_CONF_CHECK_X(gmch_pfit.control);
e2ff2d4a 11922 /* pfit ratios are autocomputed by the hw on gen4+ */
6315b5d3 11923 if (INTEL_GEN(dev_priv) < 4)
7f7d8dd6 11924 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
333b8ca8 11925 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
9953599b 11926
bfd16b2a
ML
11927 if (!adjust) {
11928 PIPE_CONF_CHECK_I(pipe_src_w);
11929 PIPE_CONF_CHECK_I(pipe_src_h);
11930
11931 PIPE_CONF_CHECK_I(pch_pfit.enabled);
11932 if (current_config->pch_pfit.enabled) {
11933 PIPE_CONF_CHECK_X(pch_pfit.pos);
11934 PIPE_CONF_CHECK_X(pch_pfit.size);
11935 }
2fa2fe9a 11936
7aefe2b5 11937 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
a7d1b3f4 11938 PIPE_CONF_CHECK_CLOCK_FUZZY(pixel_rate);
7aefe2b5 11939 }
a1b2278e 11940
e59150dc 11941 /* BDW+ don't expose a synchronous way to read the state */
772c2a51 11942 if (IS_HASWELL(dev_priv))
e59150dc 11943 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 11944
282740f7
VS
11945 PIPE_CONF_CHECK_I(double_wide);
11946
8106ddbd 11947 PIPE_CONF_CHECK_P(shared_dpll);
66e985c0 11948 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 11949 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
DV
11950 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
11951 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
d452c5b6 11952 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
00490c22 11953 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
3f4cd19f
DL
11954 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
11955 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
11956 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
c0d43d62 11957
47eacbab
VS
11958 PIPE_CONF_CHECK_X(dsi_pll.ctrl);
11959 PIPE_CONF_CHECK_X(dsi_pll.div);
11960
9beb5fea 11961 if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
42571aef
VS
11962 PIPE_CONF_CHECK_I(pipe_bpp);
11963
2d112de7 11964 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
a9a7e98a 11965 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 11966
66e985c0 11967#undef PIPE_CONF_CHECK_X
08a24034 11968#undef PIPE_CONF_CHECK_I
8106ddbd 11969#undef PIPE_CONF_CHECK_P
1bd1bd80 11970#undef PIPE_CONF_CHECK_FLAGS
5e550656 11971#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 11972#undef PIPE_CONF_QUIRK
88adfff1 11973
cfb23ed6 11974 return ret;
0e8ffe1b
DV
11975}
11976
e3b247da
VS
11977static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
11978 const struct intel_crtc_state *pipe_config)
11979{
11980 if (pipe_config->has_pch_encoder) {
21a727b3 11981 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
e3b247da
VS
11982 &pipe_config->fdi_m_n);
11983 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
11984
11985 /*
11986 * FDI already provided one idea for the dotclock.
11987 * Yell if the encoder disagrees.
11988 */
11989 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
11990 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
11991 fdi_dotclock, dotclock);
11992 }
11993}
11994
c0ead703
ML
11995static void verify_wm_state(struct drm_crtc *crtc,
11996 struct drm_crtc_state *new_state)
08db6652 11997{
6315b5d3 11998 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
08db6652 11999 struct skl_ddb_allocation hw_ddb, *sw_ddb;
3de8a14c 12000 struct skl_pipe_wm hw_wm, *sw_wm;
12001 struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
12002 struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
e7c84544
ML
12003 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12004 const enum pipe pipe = intel_crtc->pipe;
3de8a14c 12005 int plane, level, max_level = ilk_wm_max_level(dev_priv);
08db6652 12006
6315b5d3 12007 if (INTEL_GEN(dev_priv) < 9 || !new_state->active)
08db6652
DL
12008 return;
12009
3de8a14c 12010 skl_pipe_wm_get_hw_state(crtc, &hw_wm);
03af79e0 12011 sw_wm = &to_intel_crtc_state(new_state)->wm.skl.optimal;
3de8a14c 12012
08db6652
DL
12013 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12014 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12015
e7c84544 12016 /* planes */
8b364b41 12017 for_each_universal_plane(dev_priv, pipe, plane) {
3de8a14c 12018 hw_plane_wm = &hw_wm.planes[plane];
12019 sw_plane_wm = &sw_wm->planes[plane];
08db6652 12020
3de8a14c 12021 /* Watermarks */
12022 for (level = 0; level <= max_level; level++) {
12023 if (skl_wm_level_equals(&hw_plane_wm->wm[level],
12024 &sw_plane_wm->wm[level]))
12025 continue;
12026
12027 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",
12028 pipe_name(pipe), plane + 1, level,
12029 sw_plane_wm->wm[level].plane_en,
12030 sw_plane_wm->wm[level].plane_res_b,
12031 sw_plane_wm->wm[level].plane_res_l,
12032 hw_plane_wm->wm[level].plane_en,
12033 hw_plane_wm->wm[level].plane_res_b,
12034 hw_plane_wm->wm[level].plane_res_l);
12035 }
08db6652 12036
3de8a14c 12037 if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
12038 &sw_plane_wm->trans_wm)) {
12039 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",
12040 pipe_name(pipe), plane + 1,
12041 sw_plane_wm->trans_wm.plane_en,
12042 sw_plane_wm->trans_wm.plane_res_b,
12043 sw_plane_wm->trans_wm.plane_res_l,
12044 hw_plane_wm->trans_wm.plane_en,
12045 hw_plane_wm->trans_wm.plane_res_b,
12046 hw_plane_wm->trans_wm.plane_res_l);
12047 }
12048
12049 /* DDB */
12050 hw_ddb_entry = &hw_ddb.plane[pipe][plane];
12051 sw_ddb_entry = &sw_ddb->plane[pipe][plane];
12052
12053 if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
faccd994 12054 DRM_ERROR("mismatch in DDB state pipe %c plane %d (expected (%u,%u), found (%u,%u))\n",
3de8a14c 12055 pipe_name(pipe), plane + 1,
12056 sw_ddb_entry->start, sw_ddb_entry->end,
12057 hw_ddb_entry->start, hw_ddb_entry->end);
12058 }
e7c84544 12059 }
08db6652 12060
27082493
L
12061 /*
12062 * cursor
12063 * If the cursor plane isn't active, we may not have updated it's ddb
12064 * allocation. In that case since the ddb allocation will be updated
12065 * once the plane becomes visible, we can skip this check
12066 */
cd5dcbf1 12067 if (1) {
3de8a14c 12068 hw_plane_wm = &hw_wm.planes[PLANE_CURSOR];
12069 sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
12070
12071 /* Watermarks */
12072 for (level = 0; level <= max_level; level++) {
12073 if (skl_wm_level_equals(&hw_plane_wm->wm[level],
12074 &sw_plane_wm->wm[level]))
12075 continue;
12076
12077 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",
12078 pipe_name(pipe), level,
12079 sw_plane_wm->wm[level].plane_en,
12080 sw_plane_wm->wm[level].plane_res_b,
12081 sw_plane_wm->wm[level].plane_res_l,
12082 hw_plane_wm->wm[level].plane_en,
12083 hw_plane_wm->wm[level].plane_res_b,
12084 hw_plane_wm->wm[level].plane_res_l);
12085 }
12086
12087 if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
12088 &sw_plane_wm->trans_wm)) {
12089 DRM_ERROR("mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
12090 pipe_name(pipe),
12091 sw_plane_wm->trans_wm.plane_en,
12092 sw_plane_wm->trans_wm.plane_res_b,
12093 sw_plane_wm->trans_wm.plane_res_l,
12094 hw_plane_wm->trans_wm.plane_en,
12095 hw_plane_wm->trans_wm.plane_res_b,
12096 hw_plane_wm->trans_wm.plane_res_l);
12097 }
12098
12099 /* DDB */
12100 hw_ddb_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
12101 sw_ddb_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
27082493 12102
3de8a14c 12103 if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
faccd994 12104 DRM_ERROR("mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
27082493 12105 pipe_name(pipe),
3de8a14c 12106 sw_ddb_entry->start, sw_ddb_entry->end,
12107 hw_ddb_entry->start, hw_ddb_entry->end);
27082493 12108 }
08db6652
DL
12109 }
12110}
12111
91d1b4bd 12112static void
677100ce
ML
12113verify_connector_state(struct drm_device *dev,
12114 struct drm_atomic_state *state,
12115 struct drm_crtc *crtc)
8af6cf88 12116{
35dd3c64 12117 struct drm_connector *connector;
aa5e9b47 12118 struct drm_connector_state *new_conn_state;
677100ce 12119 int i;
8af6cf88 12120
aa5e9b47 12121 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
35dd3c64 12122 struct drm_encoder *encoder = connector->encoder;
749d98b8 12123 struct drm_crtc_state *crtc_state = NULL;
ad3c558f 12124
aa5e9b47 12125 if (new_conn_state->crtc != crtc)
e7c84544
ML
12126 continue;
12127
749d98b8
ML
12128 if (crtc)
12129 crtc_state = drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
12130
12131 intel_connector_verify_state(crtc_state, new_conn_state);
8af6cf88 12132
aa5e9b47 12133 I915_STATE_WARN(new_conn_state->best_encoder != encoder,
35dd3c64 12134 "connector's atomic encoder doesn't match legacy encoder\n");
8af6cf88 12135 }
91d1b4bd
DV
12136}
12137
12138static void
86b04268 12139verify_encoder_state(struct drm_device *dev, struct drm_atomic_state *state)
91d1b4bd
DV
12140{
12141 struct intel_encoder *encoder;
86b04268
DV
12142 struct drm_connector *connector;
12143 struct drm_connector_state *old_conn_state, *new_conn_state;
12144 int i;
8af6cf88 12145
b2784e15 12146 for_each_intel_encoder(dev, encoder) {
86b04268 12147 bool enabled = false, found = false;
4d20cd86 12148 enum pipe pipe;
8af6cf88
DV
12149
12150 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12151 encoder->base.base.id,
8e329a03 12152 encoder->base.name);
8af6cf88 12153
86b04268
DV
12154 for_each_oldnew_connector_in_state(state, connector, old_conn_state,
12155 new_conn_state, i) {
12156 if (old_conn_state->best_encoder == &encoder->base)
12157 found = true;
12158
12159 if (new_conn_state->best_encoder != &encoder->base)
8af6cf88 12160 continue;
86b04268 12161 found = enabled = true;
ad3c558f 12162
86b04268 12163 I915_STATE_WARN(new_conn_state->crtc !=
ad3c558f
ML
12164 encoder->base.crtc,
12165 "connector's crtc doesn't match encoder crtc\n");
8af6cf88 12166 }
86b04268
DV
12167
12168 if (!found)
12169 continue;
0e32b39c 12170
e2c719b7 12171 I915_STATE_WARN(!!encoder->base.crtc != enabled,
8af6cf88
DV
12172 "encoder's enabled state mismatch "
12173 "(expected %i, found %i)\n",
12174 !!encoder->base.crtc, enabled);
7c60d198
ML
12175
12176 if (!encoder->base.crtc) {
4d20cd86 12177 bool active;
7c60d198 12178
4d20cd86
ML
12179 active = encoder->get_hw_state(encoder, &pipe);
12180 I915_STATE_WARN(active,
12181 "encoder detached but still enabled on pipe %c.\n",
12182 pipe_name(pipe));
7c60d198 12183 }
8af6cf88 12184 }
91d1b4bd
DV
12185}
12186
12187static void
c0ead703
ML
12188verify_crtc_state(struct drm_crtc *crtc,
12189 struct drm_crtc_state *old_crtc_state,
12190 struct drm_crtc_state *new_crtc_state)
91d1b4bd 12191{
e7c84544 12192 struct drm_device *dev = crtc->dev;
fac5e23e 12193 struct drm_i915_private *dev_priv = to_i915(dev);
91d1b4bd 12194 struct intel_encoder *encoder;
e7c84544
ML
12195 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12196 struct intel_crtc_state *pipe_config, *sw_config;
12197 struct drm_atomic_state *old_state;
12198 bool active;
045ac3b5 12199
e7c84544 12200 old_state = old_crtc_state->state;
ec2dc6a0 12201 __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
e7c84544
ML
12202 pipe_config = to_intel_crtc_state(old_crtc_state);
12203 memset(pipe_config, 0, sizeof(*pipe_config));
12204 pipe_config->base.crtc = crtc;
12205 pipe_config->base.state = old_state;
8af6cf88 12206
78108b7c 12207 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
8af6cf88 12208
e7c84544 12209 active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
d62cf62a 12210
e7c84544
ML
12211 /* hw state is inconsistent with the pipe quirk */
12212 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12213 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
12214 active = new_crtc_state->active;
6c49f241 12215
e7c84544
ML
12216 I915_STATE_WARN(new_crtc_state->active != active,
12217 "crtc active state doesn't match with hw state "
12218 "(expected %i, found %i)\n", new_crtc_state->active, active);
0e8ffe1b 12219
e7c84544
ML
12220 I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
12221 "transitional active state does not match atomic hw state "
12222 "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
4d20cd86 12223
e7c84544
ML
12224 for_each_encoder_on_crtc(dev, crtc, encoder) {
12225 enum pipe pipe;
4d20cd86 12226
e7c84544
ML
12227 active = encoder->get_hw_state(encoder, &pipe);
12228 I915_STATE_WARN(active != new_crtc_state->active,
12229 "[ENCODER:%i] active %i with crtc active %i\n",
12230 encoder->base.base.id, active, new_crtc_state->active);
4d20cd86 12231
e7c84544
ML
12232 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
12233 "Encoder connected to wrong pipe %c\n",
12234 pipe_name(pipe));
4d20cd86 12235
253c84c8
VS
12236 if (active) {
12237 pipe_config->output_types |= 1 << encoder->type;
e7c84544 12238 encoder->get_config(encoder, pipe_config);
253c84c8 12239 }
e7c84544 12240 }
53d9f4e9 12241
a7d1b3f4
VS
12242 intel_crtc_compute_pixel_rate(pipe_config);
12243
e7c84544
ML
12244 if (!new_crtc_state->active)
12245 return;
cfb23ed6 12246
e7c84544 12247 intel_pipe_config_sanity_check(dev_priv, pipe_config);
e3b247da 12248
749d98b8 12249 sw_config = to_intel_crtc_state(new_crtc_state);
6315b5d3 12250 if (!intel_pipe_config_compare(dev_priv, sw_config,
e7c84544
ML
12251 pipe_config, false)) {
12252 I915_STATE_WARN(1, "pipe state doesn't match!\n");
12253 intel_dump_pipe_config(intel_crtc, pipe_config,
12254 "[hw state]");
12255 intel_dump_pipe_config(intel_crtc, sw_config,
12256 "[sw state]");
8af6cf88
DV
12257 }
12258}
12259
91d1b4bd 12260static void
c0ead703
ML
12261verify_single_dpll_state(struct drm_i915_private *dev_priv,
12262 struct intel_shared_dpll *pll,
12263 struct drm_crtc *crtc,
12264 struct drm_crtc_state *new_state)
91d1b4bd 12265{
91d1b4bd 12266 struct intel_dpll_hw_state dpll_hw_state;
e7c84544
ML
12267 unsigned crtc_mask;
12268 bool active;
5358901f 12269
e7c84544 12270 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
5358901f 12271
e7c84544 12272 DRM_DEBUG_KMS("%s\n", pll->name);
5358901f 12273
e7c84544 12274 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
5358901f 12275
e7c84544
ML
12276 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
12277 I915_STATE_WARN(!pll->on && pll->active_mask,
12278 "pll in active use but not on in sw tracking\n");
12279 I915_STATE_WARN(pll->on && !pll->active_mask,
12280 "pll is on but not used by any active crtc\n");
12281 I915_STATE_WARN(pll->on != active,
12282 "pll on state mismatch (expected %i, found %i)\n",
12283 pll->on, active);
12284 }
5358901f 12285
e7c84544 12286 if (!crtc) {
2c42e535 12287 I915_STATE_WARN(pll->active_mask & ~pll->state.crtc_mask,
e7c84544 12288 "more active pll users than references: %x vs %x\n",
2c42e535 12289 pll->active_mask, pll->state.crtc_mask);
5358901f 12290
e7c84544
ML
12291 return;
12292 }
12293
12294 crtc_mask = 1 << drm_crtc_index(crtc);
12295
12296 if (new_state->active)
12297 I915_STATE_WARN(!(pll->active_mask & crtc_mask),
12298 "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
12299 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
12300 else
12301 I915_STATE_WARN(pll->active_mask & crtc_mask,
12302 "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
12303 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
2dd66ebd 12304
2c42e535 12305 I915_STATE_WARN(!(pll->state.crtc_mask & crtc_mask),
e7c84544 12306 "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
2c42e535 12307 crtc_mask, pll->state.crtc_mask);
66e985c0 12308
2c42e535 12309 I915_STATE_WARN(pll->on && memcmp(&pll->state.hw_state,
e7c84544
ML
12310 &dpll_hw_state,
12311 sizeof(dpll_hw_state)),
12312 "pll hw state mismatch\n");
12313}
12314
12315static void
c0ead703
ML
12316verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
12317 struct drm_crtc_state *old_crtc_state,
12318 struct drm_crtc_state *new_crtc_state)
e7c84544 12319{
fac5e23e 12320 struct drm_i915_private *dev_priv = to_i915(dev);
e7c84544
ML
12321 struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
12322 struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
12323
12324 if (new_state->shared_dpll)
c0ead703 12325 verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
e7c84544
ML
12326
12327 if (old_state->shared_dpll &&
12328 old_state->shared_dpll != new_state->shared_dpll) {
12329 unsigned crtc_mask = 1 << drm_crtc_index(crtc);
12330 struct intel_shared_dpll *pll = old_state->shared_dpll;
12331
12332 I915_STATE_WARN(pll->active_mask & crtc_mask,
12333 "pll active mismatch (didn't expect pipe %c in active mask)\n",
12334 pipe_name(drm_crtc_index(crtc)));
2c42e535 12335 I915_STATE_WARN(pll->state.crtc_mask & crtc_mask,
e7c84544
ML
12336 "pll enabled crtcs mismatch (found %x in enabled mask)\n",
12337 pipe_name(drm_crtc_index(crtc)));
5358901f 12338 }
8af6cf88
DV
12339}
12340
e7c84544 12341static void
c0ead703 12342intel_modeset_verify_crtc(struct drm_crtc *crtc,
677100ce
ML
12343 struct drm_atomic_state *state,
12344 struct drm_crtc_state *old_state,
12345 struct drm_crtc_state *new_state)
e7c84544 12346{
5a21b665
DV
12347 if (!needs_modeset(new_state) &&
12348 !to_intel_crtc_state(new_state)->update_pipe)
12349 return;
12350
c0ead703 12351 verify_wm_state(crtc, new_state);
677100ce 12352 verify_connector_state(crtc->dev, state, crtc);
c0ead703
ML
12353 verify_crtc_state(crtc, old_state, new_state);
12354 verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
e7c84544
ML
12355}
12356
12357static void
c0ead703 12358verify_disabled_dpll_state(struct drm_device *dev)
e7c84544 12359{
fac5e23e 12360 struct drm_i915_private *dev_priv = to_i915(dev);
e7c84544
ML
12361 int i;
12362
12363 for (i = 0; i < dev_priv->num_shared_dpll; i++)
c0ead703 12364 verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
e7c84544
ML
12365}
12366
12367static void
677100ce
ML
12368intel_modeset_verify_disabled(struct drm_device *dev,
12369 struct drm_atomic_state *state)
e7c84544 12370{
86b04268 12371 verify_encoder_state(dev, state);
677100ce 12372 verify_connector_state(dev, state, NULL);
c0ead703 12373 verify_disabled_dpll_state(dev);
e7c84544
ML
12374}
12375
80715b2f
VS
12376static void update_scanline_offset(struct intel_crtc *crtc)
12377{
4f8036a2 12378 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
80715b2f
VS
12379
12380 /*
12381 * The scanline counter increments at the leading edge of hsync.
12382 *
12383 * On most platforms it starts counting from vtotal-1 on the
12384 * first active line. That means the scanline counter value is
12385 * always one less than what we would expect. Ie. just after
12386 * start of vblank, which also occurs at start of hsync (on the
12387 * last active line), the scanline counter will read vblank_start-1.
12388 *
12389 * On gen2 the scanline counter starts counting from 1 instead
12390 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12391 * to keep the value positive), instead of adding one.
12392 *
12393 * On HSW+ the behaviour of the scanline counter depends on the output
12394 * type. For DP ports it behaves like most other platforms, but on HDMI
12395 * there's an extra 1 line difference. So we need to add two instead of
12396 * one to the value.
12397 */
4f8036a2 12398 if (IS_GEN2(dev_priv)) {
124abe07 12399 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
80715b2f
VS
12400 int vtotal;
12401
124abe07
VS
12402 vtotal = adjusted_mode->crtc_vtotal;
12403 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
80715b2f
VS
12404 vtotal /= 2;
12405
12406 crtc->scanline_offset = vtotal - 1;
4f8036a2 12407 } else if (HAS_DDI(dev_priv) &&
2d84d2b3 12408 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
80715b2f
VS
12409 crtc->scanline_offset = 2;
12410 } else
12411 crtc->scanline_offset = 1;
12412}
12413
ad421372 12414static void intel_modeset_clear_plls(struct drm_atomic_state *state)
ed6739ef 12415{
225da59b 12416 struct drm_device *dev = state->dev;
ed6739ef 12417 struct drm_i915_private *dev_priv = to_i915(dev);
0a9ab303 12418 struct drm_crtc *crtc;
aa5e9b47 12419 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
0a9ab303 12420 int i;
ed6739ef
ACO
12421
12422 if (!dev_priv->display.crtc_compute_clock)
ad421372 12423 return;
ed6739ef 12424
aa5e9b47 12425 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
fb1a38a9 12426 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8106ddbd 12427 struct intel_shared_dpll *old_dpll =
aa5e9b47 12428 to_intel_crtc_state(old_crtc_state)->shared_dpll;
0a9ab303 12429
aa5e9b47 12430 if (!needs_modeset(new_crtc_state))
225da59b
ACO
12431 continue;
12432
aa5e9b47 12433 to_intel_crtc_state(new_crtc_state)->shared_dpll = NULL;
fb1a38a9 12434
8106ddbd 12435 if (!old_dpll)
fb1a38a9 12436 continue;
0a9ab303 12437
a1c414ee 12438 intel_release_shared_dpll(old_dpll, intel_crtc, state);
ad421372 12439 }
ed6739ef
ACO
12440}
12441
99d736a2
ML
12442/*
12443 * This implements the workaround described in the "notes" section of the mode
12444 * set sequence documentation. When going from no pipes or single pipe to
12445 * multiple pipes, and planes are enabled after the pipe, we need to wait at
12446 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
12447 */
12448static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
12449{
12450 struct drm_crtc_state *crtc_state;
12451 struct intel_crtc *intel_crtc;
12452 struct drm_crtc *crtc;
12453 struct intel_crtc_state *first_crtc_state = NULL;
12454 struct intel_crtc_state *other_crtc_state = NULL;
12455 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
12456 int i;
12457
12458 /* look at all crtc's that are going to be enabled in during modeset */
aa5e9b47 12459 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
99d736a2
ML
12460 intel_crtc = to_intel_crtc(crtc);
12461
12462 if (!crtc_state->active || !needs_modeset(crtc_state))
12463 continue;
12464
12465 if (first_crtc_state) {
12466 other_crtc_state = to_intel_crtc_state(crtc_state);
12467 break;
12468 } else {
12469 first_crtc_state = to_intel_crtc_state(crtc_state);
12470 first_pipe = intel_crtc->pipe;
12471 }
12472 }
12473
12474 /* No workaround needed? */
12475 if (!first_crtc_state)
12476 return 0;
12477
12478 /* w/a possibly needed, check how many crtc's are already enabled. */
12479 for_each_intel_crtc(state->dev, intel_crtc) {
12480 struct intel_crtc_state *pipe_config;
12481
12482 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
12483 if (IS_ERR(pipe_config))
12484 return PTR_ERR(pipe_config);
12485
12486 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
12487
12488 if (!pipe_config->base.active ||
12489 needs_modeset(&pipe_config->base))
12490 continue;
12491
12492 /* 2 or more enabled crtcs means no need for w/a */
12493 if (enabled_pipe != INVALID_PIPE)
12494 return 0;
12495
12496 enabled_pipe = intel_crtc->pipe;
12497 }
12498
12499 if (enabled_pipe != INVALID_PIPE)
12500 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
12501 else if (other_crtc_state)
12502 other_crtc_state->hsw_workaround_pipe = first_pipe;
12503
12504 return 0;
12505}
12506
8d96561a
VS
12507static int intel_lock_all_pipes(struct drm_atomic_state *state)
12508{
12509 struct drm_crtc *crtc;
12510
12511 /* Add all pipes to the state */
12512 for_each_crtc(state->dev, crtc) {
12513 struct drm_crtc_state *crtc_state;
12514
12515 crtc_state = drm_atomic_get_crtc_state(state, crtc);
12516 if (IS_ERR(crtc_state))
12517 return PTR_ERR(crtc_state);
12518 }
12519
12520 return 0;
12521}
12522
27c329ed
ML
12523static int intel_modeset_all_pipes(struct drm_atomic_state *state)
12524{
12525 struct drm_crtc *crtc;
27c329ed 12526
8d96561a
VS
12527 /*
12528 * Add all pipes to the state, and force
12529 * a modeset on all the active ones.
12530 */
27c329ed 12531 for_each_crtc(state->dev, crtc) {
9780aad5
VS
12532 struct drm_crtc_state *crtc_state;
12533 int ret;
12534
27c329ed
ML
12535 crtc_state = drm_atomic_get_crtc_state(state, crtc);
12536 if (IS_ERR(crtc_state))
12537 return PTR_ERR(crtc_state);
12538
12539 if (!crtc_state->active || needs_modeset(crtc_state))
12540 continue;
12541
12542 crtc_state->mode_changed = true;
12543
12544 ret = drm_atomic_add_affected_connectors(state, crtc);
12545 if (ret)
9780aad5 12546 return ret;
27c329ed
ML
12547
12548 ret = drm_atomic_add_affected_planes(state, crtc);
12549 if (ret)
9780aad5 12550 return ret;
27c329ed
ML
12551 }
12552
9780aad5 12553 return 0;
27c329ed
ML
12554}
12555
c347a676 12556static int intel_modeset_checks(struct drm_atomic_state *state)
054518dd 12557{
565602d7 12558 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 12559 struct drm_i915_private *dev_priv = to_i915(state->dev);
565602d7 12560 struct drm_crtc *crtc;
aa5e9b47 12561 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
565602d7 12562 int ret = 0, i;
054518dd 12563
b359283a
ML
12564 if (!check_digital_port_conflicts(state)) {
12565 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
12566 return -EINVAL;
12567 }
12568
565602d7
ML
12569 intel_state->modeset = true;
12570 intel_state->active_crtcs = dev_priv->active_crtcs;
bb0f4aab
VS
12571 intel_state->cdclk.logical = dev_priv->cdclk.logical;
12572 intel_state->cdclk.actual = dev_priv->cdclk.actual;
565602d7 12573
aa5e9b47
ML
12574 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
12575 if (new_crtc_state->active)
565602d7
ML
12576 intel_state->active_crtcs |= 1 << i;
12577 else
12578 intel_state->active_crtcs &= ~(1 << i);
8b4a7d05 12579
aa5e9b47 12580 if (old_crtc_state->active != new_crtc_state->active)
8b4a7d05 12581 intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
565602d7
ML
12582 }
12583
054518dd
ACO
12584 /*
12585 * See if the config requires any additional preparation, e.g.
12586 * to adjust global state with pipes off. We need to do this
12587 * here so we can get the modeset_pipe updated config for the new
12588 * mode set on this crtc. For other crtcs we need to use the
12589 * adjusted_mode bits in the crtc directly.
12590 */
27c329ed 12591 if (dev_priv->display.modeset_calc_cdclk) {
27c329ed 12592 ret = dev_priv->display.modeset_calc_cdclk(state);
c89e39f3
CT
12593 if (ret < 0)
12594 return ret;
27c329ed 12595
8d96561a 12596 /*
bb0f4aab 12597 * Writes to dev_priv->cdclk.logical must protected by
8d96561a
VS
12598 * holding all the crtc locks, even if we don't end up
12599 * touching the hardware
12600 */
bb0f4aab
VS
12601 if (!intel_cdclk_state_compare(&dev_priv->cdclk.logical,
12602 &intel_state->cdclk.logical)) {
8d96561a
VS
12603 ret = intel_lock_all_pipes(state);
12604 if (ret < 0)
12605 return ret;
12606 }
12607
12608 /* All pipes must be switched off while we change the cdclk. */
bb0f4aab
VS
12609 if (!intel_cdclk_state_compare(&dev_priv->cdclk.actual,
12610 &intel_state->cdclk.actual)) {
27c329ed 12611 ret = intel_modeset_all_pipes(state);
8d96561a
VS
12612 if (ret < 0)
12613 return ret;
12614 }
e8788cbc 12615
bb0f4aab
VS
12616 DRM_DEBUG_KMS("New cdclk calculated to be logical %u kHz, actual %u kHz\n",
12617 intel_state->cdclk.logical.cdclk,
12618 intel_state->cdclk.actual.cdclk);
e0ca7a6b 12619 } else {
bb0f4aab 12620 to_intel_atomic_state(state)->cdclk.logical = dev_priv->cdclk.logical;
e0ca7a6b 12621 }
054518dd 12622
ad421372 12623 intel_modeset_clear_plls(state);
054518dd 12624
565602d7 12625 if (IS_HASWELL(dev_priv))
ad421372 12626 return haswell_mode_set_planes_workaround(state);
99d736a2 12627
ad421372 12628 return 0;
c347a676
ACO
12629}
12630
aa363136
MR
12631/*
12632 * Handle calculation of various watermark data at the end of the atomic check
12633 * phase. The code here should be run after the per-crtc and per-plane 'check'
12634 * handlers to ensure that all derived state has been updated.
12635 */
55994c2c 12636static int calc_watermark_data(struct drm_atomic_state *state)
aa363136
MR
12637{
12638 struct drm_device *dev = state->dev;
98d39494 12639 struct drm_i915_private *dev_priv = to_i915(dev);
98d39494
MR
12640
12641 /* Is there platform-specific watermark information to calculate? */
12642 if (dev_priv->display.compute_global_watermarks)
55994c2c
MR
12643 return dev_priv->display.compute_global_watermarks(state);
12644
12645 return 0;
aa363136
MR
12646}
12647
74c090b1
ML
12648/**
12649 * intel_atomic_check - validate state object
12650 * @dev: drm device
12651 * @state: state to validate
12652 */
12653static int intel_atomic_check(struct drm_device *dev,
12654 struct drm_atomic_state *state)
c347a676 12655{
dd8b3bdb 12656 struct drm_i915_private *dev_priv = to_i915(dev);
aa363136 12657 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
c347a676 12658 struct drm_crtc *crtc;
aa5e9b47 12659 struct drm_crtc_state *old_crtc_state, *crtc_state;
c347a676 12660 int ret, i;
61333b60 12661 bool any_ms = false;
c347a676 12662
74c090b1 12663 ret = drm_atomic_helper_check_modeset(dev, state);
054518dd
ACO
12664 if (ret)
12665 return ret;
12666
aa5e9b47 12667 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, crtc_state, i) {
cfb23ed6
ML
12668 struct intel_crtc_state *pipe_config =
12669 to_intel_crtc_state(crtc_state);
1ed51de9
DV
12670
12671 /* Catch I915_MODE_FLAG_INHERITED */
aa5e9b47 12672 if (crtc_state->mode.private_flags != old_crtc_state->mode.private_flags)
1ed51de9 12673 crtc_state->mode_changed = true;
cfb23ed6 12674
af4a879e 12675 if (!needs_modeset(crtc_state))
c347a676
ACO
12676 continue;
12677
af4a879e
DV
12678 if (!crtc_state->enable) {
12679 any_ms = true;
cfb23ed6 12680 continue;
af4a879e 12681 }
cfb23ed6 12682
26495481
DV
12683 /* FIXME: For only active_changed we shouldn't need to do any
12684 * state recomputation at all. */
12685
1ed51de9
DV
12686 ret = drm_atomic_add_affected_connectors(state, crtc);
12687 if (ret)
12688 return ret;
b359283a 12689
cfb23ed6 12690 ret = intel_modeset_pipe_config(crtc, pipe_config);
25aa1c39
ML
12691 if (ret) {
12692 intel_dump_pipe_config(to_intel_crtc(crtc),
12693 pipe_config, "[failed]");
c347a676 12694 return ret;
25aa1c39 12695 }
c347a676 12696
73831236 12697 if (i915.fastboot &&
6315b5d3 12698 intel_pipe_config_compare(dev_priv,
aa5e9b47 12699 to_intel_crtc_state(old_crtc_state),
1ed51de9 12700 pipe_config, true)) {
26495481 12701 crtc_state->mode_changed = false;
aa5e9b47 12702 pipe_config->update_pipe = true;
26495481
DV
12703 }
12704
af4a879e 12705 if (needs_modeset(crtc_state))
26495481 12706 any_ms = true;
cfb23ed6 12707
af4a879e
DV
12708 ret = drm_atomic_add_affected_planes(state, crtc);
12709 if (ret)
12710 return ret;
61333b60 12711
26495481
DV
12712 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
12713 needs_modeset(crtc_state) ?
12714 "[modeset]" : "[fastset]");
c347a676
ACO
12715 }
12716
61333b60
ML
12717 if (any_ms) {
12718 ret = intel_modeset_checks(state);
12719
12720 if (ret)
12721 return ret;
e0ca7a6b 12722 } else {
bb0f4aab 12723 intel_state->cdclk.logical = dev_priv->cdclk.logical;
e0ca7a6b 12724 }
76305b1a 12725
dd8b3bdb 12726 ret = drm_atomic_helper_check_planes(dev, state);
aa363136
MR
12727 if (ret)
12728 return ret;
12729
f51be2e0 12730 intel_fbc_choose_crtc(dev_priv, state);
55994c2c 12731 return calc_watermark_data(state);
054518dd
ACO
12732}
12733
5008e874 12734static int intel_atomic_prepare_commit(struct drm_device *dev,
d07f0e59 12735 struct drm_atomic_state *state)
5008e874 12736{
fac5e23e 12737 struct drm_i915_private *dev_priv = to_i915(dev);
5008e874
ML
12738 struct drm_crtc_state *crtc_state;
12739 struct drm_crtc *crtc;
12740 int i, ret;
12741
aa5e9b47 12742 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
5a21b665 12743 if (state->legacy_cursor_update)
a6747b73
ML
12744 continue;
12745
5a21b665
DV
12746 ret = intel_crtc_wait_for_pending_flips(crtc);
12747 if (ret)
12748 return ret;
5008e874 12749
5a21b665
DV
12750 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
12751 flush_workqueue(dev_priv->wq);
d55dbd06
ML
12752 }
12753
f935675f
ML
12754 ret = mutex_lock_interruptible(&dev->struct_mutex);
12755 if (ret)
12756 return ret;
12757
5008e874 12758 ret = drm_atomic_helper_prepare_planes(dev, state);
f7e5838b 12759 mutex_unlock(&dev->struct_mutex);
7580d774 12760
5008e874
ML
12761 return ret;
12762}
12763
a2991414
ML
12764u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
12765{
12766 struct drm_device *dev = crtc->base.dev;
12767
12768 if (!dev->max_vblank_count)
12769 return drm_accurate_vblank_count(&crtc->base);
12770
12771 return dev->driver->get_vblank_counter(dev, crtc->pipe);
12772}
12773
5a21b665
DV
12774static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
12775 struct drm_i915_private *dev_priv,
12776 unsigned crtc_mask)
e8861675 12777{
5a21b665
DV
12778 unsigned last_vblank_count[I915_MAX_PIPES];
12779 enum pipe pipe;
12780 int ret;
e8861675 12781
5a21b665
DV
12782 if (!crtc_mask)
12783 return;
e8861675 12784
5a21b665 12785 for_each_pipe(dev_priv, pipe) {
98187836
VS
12786 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
12787 pipe);
e8861675 12788
5a21b665 12789 if (!((1 << pipe) & crtc_mask))
e8861675
ML
12790 continue;
12791
e2af48c6 12792 ret = drm_crtc_vblank_get(&crtc->base);
5a21b665
DV
12793 if (WARN_ON(ret != 0)) {
12794 crtc_mask &= ~(1 << pipe);
12795 continue;
e8861675
ML
12796 }
12797
e2af48c6 12798 last_vblank_count[pipe] = drm_crtc_vblank_count(&crtc->base);
e8861675
ML
12799 }
12800
5a21b665 12801 for_each_pipe(dev_priv, pipe) {
98187836
VS
12802 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
12803 pipe);
5a21b665 12804 long lret;
e8861675 12805
5a21b665
DV
12806 if (!((1 << pipe) & crtc_mask))
12807 continue;
d55dbd06 12808
5a21b665
DV
12809 lret = wait_event_timeout(dev->vblank[pipe].queue,
12810 last_vblank_count[pipe] !=
e2af48c6 12811 drm_crtc_vblank_count(&crtc->base),
5a21b665 12812 msecs_to_jiffies(50));
d55dbd06 12813
5a21b665 12814 WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
d55dbd06 12815
e2af48c6 12816 drm_crtc_vblank_put(&crtc->base);
d55dbd06
ML
12817 }
12818}
12819
5a21b665 12820static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
a6747b73 12821{
5a21b665
DV
12822 /* fb updated, need to unpin old fb */
12823 if (crtc_state->fb_changed)
12824 return true;
a6747b73 12825
5a21b665
DV
12826 /* wm changes, need vblank before final wm's */
12827 if (crtc_state->update_wm_post)
12828 return true;
a6747b73 12829
5eeb798b 12830 if (crtc_state->wm.need_postvbl_update)
5a21b665 12831 return true;
a6747b73 12832
5a21b665 12833 return false;
e8861675
ML
12834}
12835
896e5bb0
L
12836static void intel_update_crtc(struct drm_crtc *crtc,
12837 struct drm_atomic_state *state,
12838 struct drm_crtc_state *old_crtc_state,
aa5e9b47 12839 struct drm_crtc_state *new_crtc_state,
896e5bb0
L
12840 unsigned int *crtc_vblank_mask)
12841{
12842 struct drm_device *dev = crtc->dev;
12843 struct drm_i915_private *dev_priv = to_i915(dev);
12844 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
aa5e9b47
ML
12845 struct intel_crtc_state *pipe_config = to_intel_crtc_state(new_crtc_state);
12846 bool modeset = needs_modeset(new_crtc_state);
896e5bb0
L
12847
12848 if (modeset) {
12849 update_scanline_offset(intel_crtc);
12850 dev_priv->display.crtc_enable(pipe_config, state);
12851 } else {
aa5e9b47
ML
12852 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
12853 pipe_config);
896e5bb0
L
12854 }
12855
12856 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
12857 intel_fbc_enable(
12858 intel_crtc, pipe_config,
12859 to_intel_plane_state(crtc->primary->state));
12860 }
12861
12862 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
12863
12864 if (needs_vblank_wait(pipe_config))
12865 *crtc_vblank_mask |= drm_crtc_mask(crtc);
12866}
12867
12868static void intel_update_crtcs(struct drm_atomic_state *state,
12869 unsigned int *crtc_vblank_mask)
12870{
12871 struct drm_crtc *crtc;
aa5e9b47 12872 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
896e5bb0
L
12873 int i;
12874
aa5e9b47
ML
12875 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
12876 if (!new_crtc_state->active)
896e5bb0
L
12877 continue;
12878
12879 intel_update_crtc(crtc, state, old_crtc_state,
aa5e9b47 12880 new_crtc_state, crtc_vblank_mask);
896e5bb0
L
12881 }
12882}
12883
27082493
L
12884static void skl_update_crtcs(struct drm_atomic_state *state,
12885 unsigned int *crtc_vblank_mask)
12886{
0f0f74bc 12887 struct drm_i915_private *dev_priv = to_i915(state->dev);
27082493
L
12888 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
12889 struct drm_crtc *crtc;
ce0ba283 12890 struct intel_crtc *intel_crtc;
aa5e9b47 12891 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
ce0ba283 12892 struct intel_crtc_state *cstate;
27082493
L
12893 unsigned int updated = 0;
12894 bool progress;
12895 enum pipe pipe;
5eff503b
ML
12896 int i;
12897
12898 const struct skl_ddb_entry *entries[I915_MAX_PIPES] = {};
12899
aa5e9b47 12900 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i)
5eff503b 12901 /* ignore allocations for crtc's that have been turned off. */
aa5e9b47 12902 if (new_crtc_state->active)
5eff503b 12903 entries[i] = &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb;
27082493
L
12904
12905 /*
12906 * Whenever the number of active pipes changes, we need to make sure we
12907 * update the pipes in the right order so that their ddb allocations
12908 * never overlap with eachother inbetween CRTC updates. Otherwise we'll
12909 * cause pipe underruns and other bad stuff.
12910 */
12911 do {
27082493
L
12912 progress = false;
12913
aa5e9b47 12914 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
27082493
L
12915 bool vbl_wait = false;
12916 unsigned int cmask = drm_crtc_mask(crtc);
ce0ba283
L
12917
12918 intel_crtc = to_intel_crtc(crtc);
12919 cstate = to_intel_crtc_state(crtc->state);
12920 pipe = intel_crtc->pipe;
27082493 12921
5eff503b 12922 if (updated & cmask || !cstate->base.active)
27082493 12923 continue;
5eff503b
ML
12924
12925 if (skl_ddb_allocation_overlaps(entries, &cstate->wm.skl.ddb, i))
27082493
L
12926 continue;
12927
12928 updated |= cmask;
5eff503b 12929 entries[i] = &cstate->wm.skl.ddb;
27082493
L
12930
12931 /*
12932 * If this is an already active pipe, it's DDB changed,
12933 * and this isn't the last pipe that needs updating
12934 * then we need to wait for a vblank to pass for the
12935 * new ddb allocation to take effect.
12936 */
ce0ba283 12937 if (!skl_ddb_entry_equal(&cstate->wm.skl.ddb,
512b5527 12938 &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb) &&
aa5e9b47 12939 !new_crtc_state->active_changed &&
27082493
L
12940 intel_state->wm_results.dirty_pipes != updated)
12941 vbl_wait = true;
12942
12943 intel_update_crtc(crtc, state, old_crtc_state,
aa5e9b47 12944 new_crtc_state, crtc_vblank_mask);
27082493
L
12945
12946 if (vbl_wait)
0f0f74bc 12947 intel_wait_for_vblank(dev_priv, pipe);
27082493
L
12948
12949 progress = true;
12950 }
12951 } while (progress);
12952}
12953
ba318c61
CW
12954static void intel_atomic_helper_free_state(struct drm_i915_private *dev_priv)
12955{
12956 struct intel_atomic_state *state, *next;
12957 struct llist_node *freed;
12958
12959 freed = llist_del_all(&dev_priv->atomic_helper.free_list);
12960 llist_for_each_entry_safe(state, next, freed, freed)
12961 drm_atomic_state_put(&state->base);
12962}
12963
12964static void intel_atomic_helper_free_state_worker(struct work_struct *work)
12965{
12966 struct drm_i915_private *dev_priv =
12967 container_of(work, typeof(*dev_priv), atomic_helper.free_work);
12968
12969 intel_atomic_helper_free_state(dev_priv);
12970}
12971
94f05024 12972static void intel_atomic_commit_tail(struct drm_atomic_state *state)
a6778b3c 12973{
94f05024 12974 struct drm_device *dev = state->dev;
565602d7 12975 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 12976 struct drm_i915_private *dev_priv = to_i915(dev);
aa5e9b47 12977 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
7580d774 12978 struct drm_crtc *crtc;
5a21b665 12979 struct intel_crtc_state *intel_cstate;
5a21b665 12980 bool hw_check = intel_state->modeset;
d8fc70b7 12981 u64 put_domains[I915_MAX_PIPES] = {};
5a21b665 12982 unsigned crtc_vblank_mask = 0;
e95433c7 12983 int i;
a6778b3c 12984
ea0000f0
DV
12985 drm_atomic_helper_wait_for_dependencies(state);
12986
c3b32658 12987 if (intel_state->modeset)
5a21b665 12988 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
565602d7 12989
aa5e9b47 12990 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
a539205a
ML
12991 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12992
aa5e9b47
ML
12993 if (needs_modeset(new_crtc_state) ||
12994 to_intel_crtc_state(new_crtc_state)->update_pipe) {
5a21b665
DV
12995 hw_check = true;
12996
12997 put_domains[to_intel_crtc(crtc)->pipe] =
12998 modeset_get_crtc_power_domains(crtc,
aa5e9b47 12999 to_intel_crtc_state(new_crtc_state));
5a21b665
DV
13000 }
13001
aa5e9b47 13002 if (!needs_modeset(new_crtc_state))
61333b60
ML
13003 continue;
13004
aa5e9b47
ML
13005 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
13006 to_intel_crtc_state(new_crtc_state));
460da916 13007
29ceb0e6
VS
13008 if (old_crtc_state->active) {
13009 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
4a806558 13010 dev_priv->display.crtc_disable(to_intel_crtc_state(old_crtc_state), state);
eddfcbcd 13011 intel_crtc->active = false;
58f9c0bc 13012 intel_fbc_disable(intel_crtc);
eddfcbcd 13013 intel_disable_shared_dpll(intel_crtc);
9bbc8258
VS
13014
13015 /*
13016 * Underruns don't always raise
13017 * interrupts, so check manually.
13018 */
13019 intel_check_cpu_fifo_underruns(dev_priv);
13020 intel_check_pch_fifo_underruns(dev_priv);
b9001114 13021
e62929b3
ML
13022 if (!crtc->state->active) {
13023 /*
13024 * Make sure we don't call initial_watermarks
13025 * for ILK-style watermark updates.
ff32c54e
VS
13026 *
13027 * No clue what this is supposed to achieve.
e62929b3 13028 */
ff32c54e 13029 if (INTEL_GEN(dev_priv) >= 9)
e62929b3
ML
13030 dev_priv->display.initial_watermarks(intel_state,
13031 to_intel_crtc_state(crtc->state));
e62929b3 13032 }
a539205a 13033 }
b8cecdf5 13034 }
7758a113 13035
ea9d758d
DV
13036 /* Only after disabling all output pipelines that will be changed can we
13037 * update the the output configuration. */
4740b0f2 13038 intel_modeset_update_crtc_state(state);
f6e5b160 13039
565602d7 13040 if (intel_state->modeset) {
4740b0f2 13041 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
33c8df89 13042
b0587e4d 13043 intel_set_cdclk(dev_priv, &dev_priv->cdclk.actual);
f6d1973d 13044
656d1b89
L
13045 /*
13046 * SKL workaround: bspec recommends we disable the SAGV when we
13047 * have more then one pipe enabled
13048 */
56feca91 13049 if (!intel_can_enable_sagv(state))
16dcdc4e 13050 intel_disable_sagv(dev_priv);
656d1b89 13051
677100ce 13052 intel_modeset_verify_disabled(dev, state);
4740b0f2 13053 }
47fab737 13054
896e5bb0 13055 /* Complete the events for pipes that have now been disabled */
aa5e9b47
ML
13056 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
13057 bool modeset = needs_modeset(new_crtc_state);
80715b2f 13058
1f7528c4 13059 /* Complete events for now disable pipes here. */
aa5e9b47 13060 if (modeset && !new_crtc_state->active && new_crtc_state->event) {
1f7528c4 13061 spin_lock_irq(&dev->event_lock);
aa5e9b47 13062 drm_crtc_send_vblank_event(crtc, new_crtc_state->event);
1f7528c4
DV
13063 spin_unlock_irq(&dev->event_lock);
13064
aa5e9b47 13065 new_crtc_state->event = NULL;
1f7528c4 13066 }
177246a8
MR
13067 }
13068
896e5bb0
L
13069 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
13070 dev_priv->display.update_crtcs(state, &crtc_vblank_mask);
13071
94f05024
DV
13072 /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
13073 * already, but still need the state for the delayed optimization. To
13074 * fix this:
13075 * - wrap the optimization/post_plane_update stuff into a per-crtc work.
13076 * - schedule that vblank worker _before_ calling hw_done
13077 * - at the start of commit_tail, cancel it _synchrously
13078 * - switch over to the vblank wait helper in the core after that since
13079 * we don't need out special handling any more.
13080 */
5a21b665
DV
13081 if (!state->legacy_cursor_update)
13082 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
13083
13084 /*
13085 * Now that the vblank has passed, we can go ahead and program the
13086 * optimal watermarks on platforms that need two-step watermark
13087 * programming.
13088 *
13089 * TODO: Move this (and other cleanup) to an async worker eventually.
13090 */
aa5e9b47
ML
13091 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
13092 intel_cstate = to_intel_crtc_state(new_crtc_state);
5a21b665
DV
13093
13094 if (dev_priv->display.optimize_watermarks)
ccf010fb
ML
13095 dev_priv->display.optimize_watermarks(intel_state,
13096 intel_cstate);
5a21b665
DV
13097 }
13098
aa5e9b47 13099 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
5a21b665
DV
13100 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
13101
13102 if (put_domains[i])
13103 modeset_put_power_domains(dev_priv, put_domains[i]);
13104
aa5e9b47 13105 intel_modeset_verify_crtc(crtc, state, old_crtc_state, new_crtc_state);
5a21b665
DV
13106 }
13107
56feca91 13108 if (intel_state->modeset && intel_can_enable_sagv(state))
16dcdc4e 13109 intel_enable_sagv(dev_priv);
656d1b89 13110
94f05024
DV
13111 drm_atomic_helper_commit_hw_done(state);
13112
5a21b665
DV
13113 if (intel_state->modeset)
13114 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
13115
13116 mutex_lock(&dev->struct_mutex);
13117 drm_atomic_helper_cleanup_planes(dev, state);
13118 mutex_unlock(&dev->struct_mutex);
13119
ea0000f0
DV
13120 drm_atomic_helper_commit_cleanup_done(state);
13121
0853695c 13122 drm_atomic_state_put(state);
f30da187 13123
75714940
MK
13124 /* As one of the primary mmio accessors, KMS has a high likelihood
13125 * of triggering bugs in unclaimed access. After we finish
13126 * modesetting, see if an error has been flagged, and if so
13127 * enable debugging for the next modeset - and hope we catch
13128 * the culprit.
13129 *
13130 * XXX note that we assume display power is on at this point.
13131 * This might hold true now but we need to add pm helper to check
13132 * unclaimed only when the hardware is on, as atomic commits
13133 * can happen also when the device is completely off.
13134 */
13135 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
ba318c61
CW
13136
13137 intel_atomic_helper_free_state(dev_priv);
94f05024
DV
13138}
13139
13140static void intel_atomic_commit_work(struct work_struct *work)
13141{
c004a90b
CW
13142 struct drm_atomic_state *state =
13143 container_of(work, struct drm_atomic_state, commit_work);
13144
94f05024
DV
13145 intel_atomic_commit_tail(state);
13146}
13147
c004a90b
CW
13148static int __i915_sw_fence_call
13149intel_atomic_commit_ready(struct i915_sw_fence *fence,
13150 enum i915_sw_fence_notify notify)
13151{
13152 struct intel_atomic_state *state =
13153 container_of(fence, struct intel_atomic_state, commit_ready);
13154
13155 switch (notify) {
13156 case FENCE_COMPLETE:
13157 if (state->base.commit_work.func)
13158 queue_work(system_unbound_wq, &state->base.commit_work);
13159 break;
13160
13161 case FENCE_FREE:
eb955eee
CW
13162 {
13163 struct intel_atomic_helper *helper =
13164 &to_i915(state->base.dev)->atomic_helper;
13165
13166 if (llist_add(&state->freed, &helper->free_list))
13167 schedule_work(&helper->free_work);
13168 break;
13169 }
c004a90b
CW
13170 }
13171
13172 return NOTIFY_DONE;
13173}
13174
6c9c1b38
DV
13175static void intel_atomic_track_fbs(struct drm_atomic_state *state)
13176{
aa5e9b47 13177 struct drm_plane_state *old_plane_state, *new_plane_state;
6c9c1b38 13178 struct drm_plane *plane;
6c9c1b38
DV
13179 int i;
13180
aa5e9b47 13181 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i)
faf5bf0a 13182 i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
aa5e9b47 13183 intel_fb_obj(new_plane_state->fb),
faf5bf0a 13184 to_intel_plane(plane)->frontbuffer_bit);
6c9c1b38
DV
13185}
13186
94f05024
DV
13187/**
13188 * intel_atomic_commit - commit validated state object
13189 * @dev: DRM device
13190 * @state: the top-level driver state object
13191 * @nonblock: nonblocking commit
13192 *
13193 * This function commits a top-level state object that has been validated
13194 * with drm_atomic_helper_check().
13195 *
94f05024
DV
13196 * RETURNS
13197 * Zero for success or -errno.
13198 */
13199static int intel_atomic_commit(struct drm_device *dev,
13200 struct drm_atomic_state *state,
13201 bool nonblock)
13202{
13203 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 13204 struct drm_i915_private *dev_priv = to_i915(dev);
94f05024
DV
13205 int ret = 0;
13206
94f05024
DV
13207 ret = drm_atomic_helper_setup_commit(state, nonblock);
13208 if (ret)
13209 return ret;
13210
c004a90b
CW
13211 drm_atomic_state_get(state);
13212 i915_sw_fence_init(&intel_state->commit_ready,
13213 intel_atomic_commit_ready);
94f05024 13214
d07f0e59 13215 ret = intel_atomic_prepare_commit(dev, state);
94f05024
DV
13216 if (ret) {
13217 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
c004a90b 13218 i915_sw_fence_commit(&intel_state->commit_ready);
94f05024
DV
13219 return ret;
13220 }
13221
440df938
VS
13222 /*
13223 * The intel_legacy_cursor_update() fast path takes care
13224 * of avoiding the vblank waits for simple cursor
13225 * movement and flips. For cursor on/off and size changes,
13226 * we want to perform the vblank waits so that watermark
13227 * updates happen during the correct frames. Gen9+ have
13228 * double buffered watermarks and so shouldn't need this.
13229 *
13230 * Do this after drm_atomic_helper_setup_commit() and
13231 * intel_atomic_prepare_commit() because we still want
13232 * to skip the flip and fb cleanup waits. Although that
13233 * does risk yanking the mapping from under the display
13234 * engine.
13235 *
13236 * FIXME doing watermarks and fb cleanup from a vblank worker
13237 * (assuming we had any) would solve these problems.
13238 */
13239 if (INTEL_GEN(dev_priv) < 9)
13240 state->legacy_cursor_update = false;
13241
94f05024
DV
13242 drm_atomic_helper_swap_state(state, true);
13243 dev_priv->wm.distrust_bios_wm = false;
3c0fb588 13244 intel_shared_dpll_swap_state(state);
6c9c1b38 13245 intel_atomic_track_fbs(state);
94f05024 13246
c3b32658
ML
13247 if (intel_state->modeset) {
13248 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
13249 sizeof(intel_state->min_pixclk));
13250 dev_priv->active_crtcs = intel_state->active_crtcs;
bb0f4aab
VS
13251 dev_priv->cdclk.logical = intel_state->cdclk.logical;
13252 dev_priv->cdclk.actual = intel_state->cdclk.actual;
c3b32658
ML
13253 }
13254
0853695c 13255 drm_atomic_state_get(state);
c004a90b
CW
13256 INIT_WORK(&state->commit_work,
13257 nonblock ? intel_atomic_commit_work : NULL);
13258
13259 i915_sw_fence_commit(&intel_state->commit_ready);
13260 if (!nonblock) {
13261 i915_sw_fence_wait(&intel_state->commit_ready);
94f05024 13262 intel_atomic_commit_tail(state);
c004a90b 13263 }
75714940 13264
74c090b1 13265 return 0;
7f27126e
JB
13266}
13267
f6e5b160 13268static const struct drm_crtc_funcs intel_crtc_funcs = {
3fab2f09 13269 .gamma_set = drm_atomic_helper_legacy_gamma_set,
74c090b1 13270 .set_config = drm_atomic_helper_set_config,
82cf435b 13271 .set_property = drm_atomic_helper_crtc_set_property,
f6e5b160 13272 .destroy = intel_crtc_destroy,
4c01ded5 13273 .page_flip = drm_atomic_helper_page_flip,
1356837e
MR
13274 .atomic_duplicate_state = intel_crtc_duplicate_state,
13275 .atomic_destroy_state = intel_crtc_destroy_state,
8c6b709d 13276 .set_crc_source = intel_crtc_set_crc_source,
f6e5b160
CW
13277};
13278
6beb8c23
MR
13279/**
13280 * intel_prepare_plane_fb - Prepare fb for usage on plane
13281 * @plane: drm plane to prepare for
13282 * @fb: framebuffer to prepare for presentation
13283 *
13284 * Prepares a framebuffer for usage on a display plane. Generally this
13285 * involves pinning the underlying object and updating the frontbuffer tracking
13286 * bits. Some older platforms need special physical address handling for
13287 * cursor planes.
13288 *
f935675f
ML
13289 * Must be called with struct_mutex held.
13290 *
6beb8c23
MR
13291 * Returns 0 on success, negative error code on failure.
13292 */
13293int
13294intel_prepare_plane_fb(struct drm_plane *plane,
1832040d 13295 struct drm_plane_state *new_state)
465c120c 13296{
c004a90b
CW
13297 struct intel_atomic_state *intel_state =
13298 to_intel_atomic_state(new_state->state);
b7f05d4a 13299 struct drm_i915_private *dev_priv = to_i915(plane->dev);
844f9111 13300 struct drm_framebuffer *fb = new_state->fb;
6beb8c23 13301 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
1ee49399 13302 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
c004a90b 13303 int ret;
465c120c 13304
57822dc6
CW
13305 if (obj) {
13306 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
13307 INTEL_INFO(dev_priv)->cursor_needs_physical) {
fabac484 13308 const int align = intel_cursor_alignment(dev_priv);
57822dc6
CW
13309
13310 ret = i915_gem_object_attach_phys(obj, align);
13311 if (ret) {
13312 DRM_DEBUG_KMS("failed to attach phys object\n");
13313 return ret;
13314 }
13315 } else {
13316 struct i915_vma *vma;
13317
13318 vma = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
13319 if (IS_ERR(vma)) {
13320 DRM_DEBUG_KMS("failed to pin object\n");
13321 return PTR_ERR(vma);
13322 }
13323
13324 to_intel_plane_state(new_state)->vma = vma;
13325 }
13326 }
13327
1ee49399 13328 if (!obj && !old_obj)
465c120c
MR
13329 return 0;
13330
5008e874
ML
13331 if (old_obj) {
13332 struct drm_crtc_state *crtc_state =
c004a90b
CW
13333 drm_atomic_get_existing_crtc_state(new_state->state,
13334 plane->state->crtc);
5008e874
ML
13335
13336 /* Big Hammer, we also need to ensure that any pending
13337 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
13338 * current scanout is retired before unpinning the old
13339 * framebuffer. Note that we rely on userspace rendering
13340 * into the buffer attached to the pipe they are waiting
13341 * on. If not, userspace generates a GPU hang with IPEHR
13342 * point to the MI_WAIT_FOR_EVENT.
13343 *
13344 * This should only fail upon a hung GPU, in which case we
13345 * can safely continue.
13346 */
c004a90b
CW
13347 if (needs_modeset(crtc_state)) {
13348 ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
13349 old_obj->resv, NULL,
13350 false, 0,
13351 GFP_KERNEL);
13352 if (ret < 0)
13353 return ret;
f4457ae7 13354 }
5008e874
ML
13355 }
13356
c004a90b
CW
13357 if (new_state->fence) { /* explicit fencing */
13358 ret = i915_sw_fence_await_dma_fence(&intel_state->commit_ready,
13359 new_state->fence,
13360 I915_FENCE_TIMEOUT,
13361 GFP_KERNEL);
13362 if (ret < 0)
13363 return ret;
13364 }
13365
c37efb99
CW
13366 if (!obj)
13367 return 0;
13368
c004a90b
CW
13369 if (!new_state->fence) { /* implicit fencing */
13370 ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
13371 obj->resv, NULL,
13372 false, I915_FENCE_TIMEOUT,
13373 GFP_KERNEL);
13374 if (ret < 0)
13375 return ret;
6b5e90f5
CW
13376
13377 i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
c004a90b 13378 }
5a21b665 13379
d07f0e59 13380 return 0;
6beb8c23
MR
13381}
13382
38f3ce3a
MR
13383/**
13384 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13385 * @plane: drm plane to clean up for
13386 * @fb: old framebuffer that was on plane
13387 *
13388 * Cleans up a framebuffer that has just been removed from a plane.
f935675f
ML
13389 *
13390 * Must be called with struct_mutex held.
38f3ce3a
MR
13391 */
13392void
13393intel_cleanup_plane_fb(struct drm_plane *plane,
1832040d 13394 struct drm_plane_state *old_state)
38f3ce3a 13395{
be1e3415 13396 struct i915_vma *vma;
38f3ce3a 13397
be1e3415
CW
13398 /* Should only be called after a successful intel_prepare_plane_fb()! */
13399 vma = fetch_and_zero(&to_intel_plane_state(old_state)->vma);
13400 if (vma)
13401 intel_unpin_fb_vma(vma);
465c120c
MR
13402}
13403
6156a456
CK
13404int
13405skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13406{
5b7280f0 13407 struct drm_i915_private *dev_priv;
6156a456 13408 int max_scale;
5b7280f0 13409 int crtc_clock, max_dotclk;
6156a456 13410
bf8a0af0 13411 if (!intel_crtc || !crtc_state->base.enable)
6156a456
CK
13412 return DRM_PLANE_HELPER_NO_SCALING;
13413
5b7280f0
ACO
13414 dev_priv = to_i915(intel_crtc->base.dev);
13415
6156a456 13416 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
5b7280f0
ACO
13417 max_dotclk = to_intel_atomic_state(crtc_state->base.state)->cdclk.logical.cdclk;
13418
13419 if (IS_GEMINILAKE(dev_priv))
13420 max_dotclk *= 2;
6156a456 13421
5b7280f0 13422 if (WARN_ON_ONCE(!crtc_clock || max_dotclk < crtc_clock))
6156a456
CK
13423 return DRM_PLANE_HELPER_NO_SCALING;
13424
13425 /*
13426 * skl max scale is lower of:
13427 * close to 3 but not 3, -1 is for that purpose
13428 * or
13429 * cdclk/crtc_clock
13430 */
5b7280f0
ACO
13431 max_scale = min((1 << 16) * 3 - 1,
13432 (1 << 8) * ((max_dotclk << 8) / crtc_clock));
6156a456
CK
13433
13434 return max_scale;
13435}
13436
465c120c 13437static int
282dbf9b 13438intel_check_primary_plane(struct intel_plane *plane,
061e4b8d 13439 struct intel_crtc_state *crtc_state,
3c692a41
GP
13440 struct intel_plane_state *state)
13441{
282dbf9b 13442 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
2b875c22 13443 struct drm_crtc *crtc = state->base.crtc;
6156a456 13444 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
061e4b8d
ML
13445 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13446 bool can_position = false;
b63a16f6 13447 int ret;
465c120c 13448
b63a16f6 13449 if (INTEL_GEN(dev_priv) >= 9) {
693bdc28
VS
13450 /* use scaler when colorkey is not required */
13451 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
13452 min_scale = 1;
13453 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
13454 }
d8106366 13455 can_position = true;
6156a456 13456 }
d8106366 13457
cc926387
DV
13458 ret = drm_plane_helper_check_state(&state->base,
13459 &state->clip,
13460 min_scale, max_scale,
13461 can_position, true);
b63a16f6
VS
13462 if (ret)
13463 return ret;
13464
cc926387 13465 if (!state->base.fb)
b63a16f6
VS
13466 return 0;
13467
13468 if (INTEL_GEN(dev_priv) >= 9) {
13469 ret = skl_check_plane_surface(state);
13470 if (ret)
13471 return ret;
a0864d59
VS
13472
13473 state->ctl = skl_plane_ctl(crtc_state, state);
13474 } else {
5b7fcc44
VS
13475 ret = i9xx_check_plane_surface(state);
13476 if (ret)
13477 return ret;
13478
a0864d59 13479 state->ctl = i9xx_plane_ctl(crtc_state, state);
b63a16f6
VS
13480 }
13481
13482 return 0;
14af293f
GP
13483}
13484
5a21b665
DV
13485static void intel_begin_crtc_commit(struct drm_crtc *crtc,
13486 struct drm_crtc_state *old_crtc_state)
13487{
13488 struct drm_device *dev = crtc->dev;
62e0fb88 13489 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665 13490 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b707aa50
L
13491 struct intel_crtc_state *intel_cstate =
13492 to_intel_crtc_state(crtc->state);
ccf010fb 13493 struct intel_crtc_state *old_intel_cstate =
5a21b665 13494 to_intel_crtc_state(old_crtc_state);
ccf010fb
ML
13495 struct intel_atomic_state *old_intel_state =
13496 to_intel_atomic_state(old_crtc_state->state);
5a21b665
DV
13497 bool modeset = needs_modeset(crtc->state);
13498
567f0792
ML
13499 if (!modeset &&
13500 (intel_cstate->base.color_mgmt_changed ||
13501 intel_cstate->update_pipe)) {
13502 intel_color_set_csc(crtc->state);
13503 intel_color_load_luts(crtc->state);
13504 }
13505
5a21b665
DV
13506 /* Perform vblank evasion around commit operation */
13507 intel_pipe_update_start(intel_crtc);
13508
13509 if (modeset)
e62929b3 13510 goto out;
5a21b665 13511
ccf010fb
ML
13512 if (intel_cstate->update_pipe)
13513 intel_update_pipe_config(intel_crtc, old_intel_cstate);
13514 else if (INTEL_GEN(dev_priv) >= 9)
5a21b665 13515 skl_detach_scalers(intel_crtc);
62e0fb88 13516
e62929b3 13517out:
ccf010fb
ML
13518 if (dev_priv->display.atomic_update_watermarks)
13519 dev_priv->display.atomic_update_watermarks(old_intel_state,
13520 intel_cstate);
5a21b665
DV
13521}
13522
13523static void intel_finish_crtc_commit(struct drm_crtc *crtc,
13524 struct drm_crtc_state *old_crtc_state)
13525{
13526 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13527
13528 intel_pipe_update_end(intel_crtc, NULL);
13529}
13530
cf4c7c12 13531/**
4a3b8769
MR
13532 * intel_plane_destroy - destroy a plane
13533 * @plane: plane to destroy
cf4c7c12 13534 *
4a3b8769
MR
13535 * Common destruction function for all types of planes (primary, cursor,
13536 * sprite).
cf4c7c12 13537 */
4a3b8769 13538void intel_plane_destroy(struct drm_plane *plane)
465c120c 13539{
465c120c 13540 drm_plane_cleanup(plane);
69ae561f 13541 kfree(to_intel_plane(plane));
465c120c
MR
13542}
13543
65a3fea0 13544const struct drm_plane_funcs intel_plane_funcs = {
70a101f8
MR
13545 .update_plane = drm_atomic_helper_update_plane,
13546 .disable_plane = drm_atomic_helper_disable_plane,
3d7d6510 13547 .destroy = intel_plane_destroy,
c196e1d6 13548 .set_property = drm_atomic_helper_plane_set_property,
a98b3431
MR
13549 .atomic_get_property = intel_plane_atomic_get_property,
13550 .atomic_set_property = intel_plane_atomic_set_property,
ea2c67bb
MR
13551 .atomic_duplicate_state = intel_plane_duplicate_state,
13552 .atomic_destroy_state = intel_plane_destroy_state,
465c120c
MR
13553};
13554
f79f2692
ML
13555static int
13556intel_legacy_cursor_update(struct drm_plane *plane,
13557 struct drm_crtc *crtc,
13558 struct drm_framebuffer *fb,
13559 int crtc_x, int crtc_y,
13560 unsigned int crtc_w, unsigned int crtc_h,
13561 uint32_t src_x, uint32_t src_y,
34a2ab5e
DV
13562 uint32_t src_w, uint32_t src_h,
13563 struct drm_modeset_acquire_ctx *ctx)
f79f2692
ML
13564{
13565 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
13566 int ret;
13567 struct drm_plane_state *old_plane_state, *new_plane_state;
13568 struct intel_plane *intel_plane = to_intel_plane(plane);
13569 struct drm_framebuffer *old_fb;
13570 struct drm_crtc_state *crtc_state = crtc->state;
be1e3415 13571 struct i915_vma *old_vma;
f79f2692
ML
13572
13573 /*
13574 * When crtc is inactive or there is a modeset pending,
13575 * wait for it to complete in the slowpath
13576 */
13577 if (!crtc_state->active || needs_modeset(crtc_state) ||
13578 to_intel_crtc_state(crtc_state)->update_pipe)
13579 goto slow;
13580
13581 old_plane_state = plane->state;
13582
13583 /*
13584 * If any parameters change that may affect watermarks,
13585 * take the slowpath. Only changing fb or position should be
13586 * in the fastpath.
13587 */
13588 if (old_plane_state->crtc != crtc ||
13589 old_plane_state->src_w != src_w ||
13590 old_plane_state->src_h != src_h ||
13591 old_plane_state->crtc_w != crtc_w ||
13592 old_plane_state->crtc_h != crtc_h ||
a5509abd 13593 !old_plane_state->fb != !fb)
f79f2692
ML
13594 goto slow;
13595
13596 new_plane_state = intel_plane_duplicate_state(plane);
13597 if (!new_plane_state)
13598 return -ENOMEM;
13599
13600 drm_atomic_set_fb_for_plane(new_plane_state, fb);
13601
13602 new_plane_state->src_x = src_x;
13603 new_plane_state->src_y = src_y;
13604 new_plane_state->src_w = src_w;
13605 new_plane_state->src_h = src_h;
13606 new_plane_state->crtc_x = crtc_x;
13607 new_plane_state->crtc_y = crtc_y;
13608 new_plane_state->crtc_w = crtc_w;
13609 new_plane_state->crtc_h = crtc_h;
13610
13611 ret = intel_plane_atomic_check_with_state(to_intel_crtc_state(crtc->state),
13612 to_intel_plane_state(new_plane_state));
13613 if (ret)
13614 goto out_free;
13615
f79f2692
ML
13616 ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
13617 if (ret)
13618 goto out_free;
13619
13620 if (INTEL_INFO(dev_priv)->cursor_needs_physical) {
fabac484 13621 int align = intel_cursor_alignment(dev_priv);
f79f2692
ML
13622
13623 ret = i915_gem_object_attach_phys(intel_fb_obj(fb), align);
13624 if (ret) {
13625 DRM_DEBUG_KMS("failed to attach phys object\n");
13626 goto out_unlock;
13627 }
13628 } else {
13629 struct i915_vma *vma;
13630
13631 vma = intel_pin_and_fence_fb_obj(fb, new_plane_state->rotation);
13632 if (IS_ERR(vma)) {
13633 DRM_DEBUG_KMS("failed to pin object\n");
13634
13635 ret = PTR_ERR(vma);
13636 goto out_unlock;
13637 }
be1e3415
CW
13638
13639 to_intel_plane_state(new_plane_state)->vma = vma;
f79f2692
ML
13640 }
13641
13642 old_fb = old_plane_state->fb;
be1e3415 13643 old_vma = to_intel_plane_state(old_plane_state)->vma;
f79f2692
ML
13644
13645 i915_gem_track_fb(intel_fb_obj(old_fb), intel_fb_obj(fb),
13646 intel_plane->frontbuffer_bit);
13647
13648 /* Swap plane state */
13649 new_plane_state->fence = old_plane_state->fence;
13650 *to_intel_plane_state(old_plane_state) = *to_intel_plane_state(new_plane_state);
13651 new_plane_state->fence = NULL;
13652 new_plane_state->fb = old_fb;
be1e3415 13653 to_intel_plane_state(new_plane_state)->vma = old_vma;
f79f2692 13654
72259536
VS
13655 if (plane->state->visible) {
13656 trace_intel_update_plane(plane, to_intel_crtc(crtc));
282dbf9b 13657 intel_plane->update_plane(intel_plane,
a5509abd
VS
13658 to_intel_crtc_state(crtc->state),
13659 to_intel_plane_state(plane->state));
72259536
VS
13660 } else {
13661 trace_intel_disable_plane(plane, to_intel_crtc(crtc));
282dbf9b 13662 intel_plane->disable_plane(intel_plane, to_intel_crtc(crtc));
72259536 13663 }
f79f2692
ML
13664
13665 intel_cleanup_plane_fb(plane, new_plane_state);
13666
13667out_unlock:
13668 mutex_unlock(&dev_priv->drm.struct_mutex);
13669out_free:
13670 intel_plane_destroy_state(plane, new_plane_state);
13671 return ret;
13672
f79f2692
ML
13673slow:
13674 return drm_atomic_helper_update_plane(plane, crtc, fb,
13675 crtc_x, crtc_y, crtc_w, crtc_h,
34a2ab5e 13676 src_x, src_y, src_w, src_h, ctx);
f79f2692
ML
13677}
13678
13679static const struct drm_plane_funcs intel_cursor_plane_funcs = {
13680 .update_plane = intel_legacy_cursor_update,
13681 .disable_plane = drm_atomic_helper_disable_plane,
13682 .destroy = intel_plane_destroy,
13683 .set_property = drm_atomic_helper_plane_set_property,
13684 .atomic_get_property = intel_plane_atomic_get_property,
13685 .atomic_set_property = intel_plane_atomic_set_property,
13686 .atomic_duplicate_state = intel_plane_duplicate_state,
13687 .atomic_destroy_state = intel_plane_destroy_state,
13688};
13689
b079bd17 13690static struct intel_plane *
580503c7 13691intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
465c120c 13692{
fca0ce2a
VS
13693 struct intel_plane *primary = NULL;
13694 struct intel_plane_state *state = NULL;
465c120c 13695 const uint32_t *intel_primary_formats;
93ca7e00 13696 unsigned int supported_rotations;
45e3743a 13697 unsigned int num_formats;
fca0ce2a 13698 int ret;
465c120c
MR
13699
13700 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
b079bd17
VS
13701 if (!primary) {
13702 ret = -ENOMEM;
fca0ce2a 13703 goto fail;
b079bd17 13704 }
465c120c 13705
8e7d688b 13706 state = intel_create_plane_state(&primary->base);
b079bd17
VS
13707 if (!state) {
13708 ret = -ENOMEM;
fca0ce2a 13709 goto fail;
b079bd17
VS
13710 }
13711
8e7d688b 13712 primary->base.state = &state->base;
ea2c67bb 13713
465c120c
MR
13714 primary->can_scale = false;
13715 primary->max_downscale = 1;
580503c7 13716 if (INTEL_GEN(dev_priv) >= 9) {
6156a456 13717 primary->can_scale = true;
af99ceda 13718 state->scaler_id = -1;
6156a456 13719 }
465c120c 13720 primary->pipe = pipe;
e3c566df
VS
13721 /*
13722 * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
13723 * port is hooked to pipe B. Hence we want plane A feeding pipe B.
13724 */
13725 if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4)
13726 primary->plane = (enum plane) !pipe;
13727 else
13728 primary->plane = (enum plane) pipe;
b14e5848 13729 primary->id = PLANE_PRIMARY;
a9ff8714 13730 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
c59cb179 13731 primary->check_plane = intel_check_primary_plane;
465c120c 13732
580503c7 13733 if (INTEL_GEN(dev_priv) >= 9) {
6c0fd451
DL
13734 intel_primary_formats = skl_primary_formats;
13735 num_formats = ARRAY_SIZE(skl_primary_formats);
a8d201af
ML
13736
13737 primary->update_plane = skylake_update_primary_plane;
13738 primary->disable_plane = skylake_disable_primary_plane;
580503c7 13739 } else if (INTEL_GEN(dev_priv) >= 4) {
568db4f2
DL
13740 intel_primary_formats = i965_primary_formats;
13741 num_formats = ARRAY_SIZE(i965_primary_formats);
a8d201af
ML
13742
13743 primary->update_plane = i9xx_update_primary_plane;
13744 primary->disable_plane = i9xx_disable_primary_plane;
6c0fd451
DL
13745 } else {
13746 intel_primary_formats = i8xx_primary_formats;
13747 num_formats = ARRAY_SIZE(i8xx_primary_formats);
a8d201af
ML
13748
13749 primary->update_plane = i9xx_update_primary_plane;
13750 primary->disable_plane = i9xx_disable_primary_plane;
465c120c
MR
13751 }
13752
580503c7
VS
13753 if (INTEL_GEN(dev_priv) >= 9)
13754 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
13755 0, &intel_plane_funcs,
38573dc1
VS
13756 intel_primary_formats, num_formats,
13757 DRM_PLANE_TYPE_PRIMARY,
13758 "plane 1%c", pipe_name(pipe));
9beb5fea 13759 else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
580503c7
VS
13760 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
13761 0, &intel_plane_funcs,
38573dc1
VS
13762 intel_primary_formats, num_formats,
13763 DRM_PLANE_TYPE_PRIMARY,
13764 "primary %c", pipe_name(pipe));
13765 else
580503c7
VS
13766 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
13767 0, &intel_plane_funcs,
38573dc1
VS
13768 intel_primary_formats, num_formats,
13769 DRM_PLANE_TYPE_PRIMARY,
13770 "plane %c", plane_name(primary->plane));
fca0ce2a
VS
13771 if (ret)
13772 goto fail;
48404c1e 13773
5481e27f 13774 if (INTEL_GEN(dev_priv) >= 9) {
93ca7e00 13775 supported_rotations =
c2c446ad
RF
13776 DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 |
13777 DRM_MODE_ROTATE_180 | DRM_MODE_ROTATE_270;
4ea7be2b
VS
13778 } else if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
13779 supported_rotations =
c2c446ad
RF
13780 DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180 |
13781 DRM_MODE_REFLECT_X;
5481e27f 13782 } else if (INTEL_GEN(dev_priv) >= 4) {
93ca7e00 13783 supported_rotations =
c2c446ad 13784 DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180;
93ca7e00 13785 } else {
c2c446ad 13786 supported_rotations = DRM_MODE_ROTATE_0;
93ca7e00
VS
13787 }
13788
5481e27f 13789 if (INTEL_GEN(dev_priv) >= 4)
93ca7e00 13790 drm_plane_create_rotation_property(&primary->base,
c2c446ad 13791 DRM_MODE_ROTATE_0,
93ca7e00 13792 supported_rotations);
48404c1e 13793
ea2c67bb
MR
13794 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13795
b079bd17 13796 return primary;
fca0ce2a
VS
13797
13798fail:
13799 kfree(state);
13800 kfree(primary);
13801
b079bd17 13802 return ERR_PTR(ret);
465c120c
MR
13803}
13804
b079bd17 13805static struct intel_plane *
b2d03b0d
VS
13806intel_cursor_plane_create(struct drm_i915_private *dev_priv,
13807 enum pipe pipe)
3d7d6510 13808{
fca0ce2a
VS
13809 struct intel_plane *cursor = NULL;
13810 struct intel_plane_state *state = NULL;
13811 int ret;
3d7d6510
MR
13812
13813 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
b079bd17
VS
13814 if (!cursor) {
13815 ret = -ENOMEM;
fca0ce2a 13816 goto fail;
b079bd17 13817 }
3d7d6510 13818
8e7d688b 13819 state = intel_create_plane_state(&cursor->base);
b079bd17
VS
13820 if (!state) {
13821 ret = -ENOMEM;
fca0ce2a 13822 goto fail;
b079bd17
VS
13823 }
13824
8e7d688b 13825 cursor->base.state = &state->base;
ea2c67bb 13826
3d7d6510
MR
13827 cursor->can_scale = false;
13828 cursor->max_downscale = 1;
13829 cursor->pipe = pipe;
13830 cursor->plane = pipe;
b14e5848 13831 cursor->id = PLANE_CURSOR;
a9ff8714 13832 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
b2d03b0d
VS
13833
13834 if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
13835 cursor->update_plane = i845_update_cursor;
13836 cursor->disable_plane = i845_disable_cursor;
659056f2 13837 cursor->check_plane = i845_check_cursor;
b2d03b0d
VS
13838 } else {
13839 cursor->update_plane = i9xx_update_cursor;
13840 cursor->disable_plane = i9xx_disable_cursor;
659056f2 13841 cursor->check_plane = i9xx_check_cursor;
b2d03b0d 13842 }
3d7d6510 13843
cd5dcbf1
VS
13844 cursor->cursor.base = ~0;
13845 cursor->cursor.cntl = ~0;
024faac7
VS
13846
13847 if (IS_I845G(dev_priv) || IS_I865G(dev_priv) || HAS_CUR_FBC(dev_priv))
13848 cursor->cursor.size = ~0;
3d7d6510 13849
580503c7 13850 ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
f79f2692 13851 0, &intel_cursor_plane_funcs,
fca0ce2a
VS
13852 intel_cursor_formats,
13853 ARRAY_SIZE(intel_cursor_formats),
38573dc1
VS
13854 DRM_PLANE_TYPE_CURSOR,
13855 "cursor %c", pipe_name(pipe));
fca0ce2a
VS
13856 if (ret)
13857 goto fail;
4398ad45 13858
5481e27f 13859 if (INTEL_GEN(dev_priv) >= 4)
93ca7e00 13860 drm_plane_create_rotation_property(&cursor->base,
c2c446ad
RF
13861 DRM_MODE_ROTATE_0,
13862 DRM_MODE_ROTATE_0 |
13863 DRM_MODE_ROTATE_180);
4398ad45 13864
580503c7 13865 if (INTEL_GEN(dev_priv) >= 9)
af99ceda
CK
13866 state->scaler_id = -1;
13867
ea2c67bb
MR
13868 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
13869
b079bd17 13870 return cursor;
fca0ce2a
VS
13871
13872fail:
13873 kfree(state);
13874 kfree(cursor);
13875
b079bd17 13876 return ERR_PTR(ret);
3d7d6510
MR
13877}
13878
1c74eeaf
NM
13879static void intel_crtc_init_scalers(struct intel_crtc *crtc,
13880 struct intel_crtc_state *crtc_state)
549e2bfb 13881{
65edccce
VS
13882 struct intel_crtc_scaler_state *scaler_state =
13883 &crtc_state->scaler_state;
1c74eeaf 13884 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
549e2bfb 13885 int i;
549e2bfb 13886
1c74eeaf
NM
13887 crtc->num_scalers = dev_priv->info.num_scalers[crtc->pipe];
13888 if (!crtc->num_scalers)
13889 return;
13890
65edccce
VS
13891 for (i = 0; i < crtc->num_scalers; i++) {
13892 struct intel_scaler *scaler = &scaler_state->scalers[i];
13893
13894 scaler->in_use = 0;
13895 scaler->mode = PS_SCALER_MODE_DYN;
549e2bfb
CK
13896 }
13897
13898 scaler_state->scaler_id = -1;
13899}
13900
5ab0d85b 13901static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
79e53945
JB
13902{
13903 struct intel_crtc *intel_crtc;
f5de6e07 13904 struct intel_crtc_state *crtc_state = NULL;
b079bd17
VS
13905 struct intel_plane *primary = NULL;
13906 struct intel_plane *cursor = NULL;
a81d6fa0 13907 int sprite, ret;
79e53945 13908
955382f3 13909 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
b079bd17
VS
13910 if (!intel_crtc)
13911 return -ENOMEM;
79e53945 13912
f5de6e07 13913 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
b079bd17
VS
13914 if (!crtc_state) {
13915 ret = -ENOMEM;
f5de6e07 13916 goto fail;
b079bd17 13917 }
550acefd
ACO
13918 intel_crtc->config = crtc_state;
13919 intel_crtc->base.state = &crtc_state->base;
07878248 13920 crtc_state->base.crtc = &intel_crtc->base;
f5de6e07 13921
580503c7 13922 primary = intel_primary_plane_create(dev_priv, pipe);
b079bd17
VS
13923 if (IS_ERR(primary)) {
13924 ret = PTR_ERR(primary);
3d7d6510 13925 goto fail;
b079bd17 13926 }
d97d7b48 13927 intel_crtc->plane_ids_mask |= BIT(primary->id);
3d7d6510 13928
a81d6fa0 13929 for_each_sprite(dev_priv, pipe, sprite) {
b079bd17
VS
13930 struct intel_plane *plane;
13931
580503c7 13932 plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
d2b2cbce 13933 if (IS_ERR(plane)) {
b079bd17
VS
13934 ret = PTR_ERR(plane);
13935 goto fail;
13936 }
d97d7b48 13937 intel_crtc->plane_ids_mask |= BIT(plane->id);
a81d6fa0
VS
13938 }
13939
580503c7 13940 cursor = intel_cursor_plane_create(dev_priv, pipe);
d2b2cbce 13941 if (IS_ERR(cursor)) {
b079bd17 13942 ret = PTR_ERR(cursor);
3d7d6510 13943 goto fail;
b079bd17 13944 }
d97d7b48 13945 intel_crtc->plane_ids_mask |= BIT(cursor->id);
3d7d6510 13946
5ab0d85b 13947 ret = drm_crtc_init_with_planes(&dev_priv->drm, &intel_crtc->base,
b079bd17
VS
13948 &primary->base, &cursor->base,
13949 &intel_crtc_funcs,
4d5d72b7 13950 "pipe %c", pipe_name(pipe));
3d7d6510
MR
13951 if (ret)
13952 goto fail;
79e53945 13953
80824003 13954 intel_crtc->pipe = pipe;
e3c566df 13955 intel_crtc->plane = primary->plane;
80824003 13956
1c74eeaf
NM
13957 /* initialize shared scalers */
13958 intel_crtc_init_scalers(intel_crtc, crtc_state);
13959
22fd0fab
JB
13960 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
13961 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
e2af48c6
VS
13962 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = intel_crtc;
13963 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = intel_crtc;
22fd0fab 13964
79e53945 13965 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
87b6b101 13966
8563b1e8
LL
13967 intel_color_init(&intel_crtc->base);
13968
87b6b101 13969 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
b079bd17
VS
13970
13971 return 0;
3d7d6510
MR
13972
13973fail:
b079bd17
VS
13974 /*
13975 * drm_mode_config_cleanup() will free up any
13976 * crtcs/planes already initialized.
13977 */
f5de6e07 13978 kfree(crtc_state);
3d7d6510 13979 kfree(intel_crtc);
b079bd17
VS
13980
13981 return ret;
79e53945
JB
13982}
13983
752aa88a
JB
13984enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
13985{
6e9f798d 13986 struct drm_device *dev = connector->base.dev;
752aa88a 13987
51fd371b 13988 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
752aa88a 13989
51ec53da 13990 if (!connector->base.state->crtc)
752aa88a
JB
13991 return INVALID_PIPE;
13992
51ec53da 13993 return to_intel_crtc(connector->base.state->crtc)->pipe;
752aa88a
JB
13994}
13995
08d7b3d1 13996int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 13997 struct drm_file *file)
08d7b3d1 13998{
08d7b3d1 13999 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7707e653 14000 struct drm_crtc *drmmode_crtc;
c05422d5 14001 struct intel_crtc *crtc;
08d7b3d1 14002
7707e653 14003 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
71240ed2 14004 if (!drmmode_crtc)
3f2c2057 14005 return -ENOENT;
08d7b3d1 14006
7707e653 14007 crtc = to_intel_crtc(drmmode_crtc);
c05422d5 14008 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 14009
c05422d5 14010 return 0;
08d7b3d1
CW
14011}
14012
66a9278e 14013static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 14014{
66a9278e
DV
14015 struct drm_device *dev = encoder->base.dev;
14016 struct intel_encoder *source_encoder;
79e53945 14017 int index_mask = 0;
79e53945
JB
14018 int entry = 0;
14019
b2784e15 14020 for_each_intel_encoder(dev, source_encoder) {
bc079e8b 14021 if (encoders_cloneable(encoder, source_encoder))
66a9278e
DV
14022 index_mask |= (1 << entry);
14023
79e53945
JB
14024 entry++;
14025 }
4ef69c7a 14026
79e53945
JB
14027 return index_mask;
14028}
14029
646d5772 14030static bool has_edp_a(struct drm_i915_private *dev_priv)
4d302442 14031{
646d5772 14032 if (!IS_MOBILE(dev_priv))
4d302442
CW
14033 return false;
14034
14035 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14036 return false;
14037
5db94019 14038 if (IS_GEN5(dev_priv) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
14039 return false;
14040
14041 return true;
14042}
14043
6315b5d3 14044static bool intel_crt_present(struct drm_i915_private *dev_priv)
84b4e042 14045{
6315b5d3 14046 if (INTEL_GEN(dev_priv) >= 9)
884497ed
DL
14047 return false;
14048
50a0bc90 14049 if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
84b4e042
JB
14050 return false;
14051
920a14b2 14052 if (IS_CHERRYVIEW(dev_priv))
84b4e042
JB
14053 return false;
14054
4f8036a2
TU
14055 if (HAS_PCH_LPT_H(dev_priv) &&
14056 I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
65e472e4
VS
14057 return false;
14058
70ac54d0 14059 /* DDI E can't be used if DDI A requires 4 lanes */
4f8036a2 14060 if (HAS_DDI(dev_priv) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
70ac54d0
VS
14061 return false;
14062
e4abb733 14063 if (!dev_priv->vbt.int_crt_support)
84b4e042
JB
14064 return false;
14065
14066 return true;
14067}
14068
8090ba8c
ID
14069void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
14070{
14071 int pps_num;
14072 int pps_idx;
14073
14074 if (HAS_DDI(dev_priv))
14075 return;
14076 /*
14077 * This w/a is needed at least on CPT/PPT, but to be sure apply it
14078 * everywhere where registers can be write protected.
14079 */
14080 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
14081 pps_num = 2;
14082 else
14083 pps_num = 1;
14084
14085 for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
14086 u32 val = I915_READ(PP_CONTROL(pps_idx));
14087
14088 val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
14089 I915_WRITE(PP_CONTROL(pps_idx), val);
14090 }
14091}
14092
44cb734c
ID
14093static void intel_pps_init(struct drm_i915_private *dev_priv)
14094{
cc3f90f0 14095 if (HAS_PCH_SPLIT(dev_priv) || IS_GEN9_LP(dev_priv))
44cb734c
ID
14096 dev_priv->pps_mmio_base = PCH_PPS_BASE;
14097 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
14098 dev_priv->pps_mmio_base = VLV_PPS_BASE;
14099 else
14100 dev_priv->pps_mmio_base = PPS_BASE;
8090ba8c
ID
14101
14102 intel_pps_unlock_regs_wa(dev_priv);
44cb734c
ID
14103}
14104
c39055b0 14105static void intel_setup_outputs(struct drm_i915_private *dev_priv)
79e53945 14106{
4ef69c7a 14107 struct intel_encoder *encoder;
cb0953d7 14108 bool dpd_is_edp = false;
79e53945 14109
44cb734c
ID
14110 intel_pps_init(dev_priv);
14111
97a824e1
ID
14112 /*
14113 * intel_edp_init_connector() depends on this completing first, to
14114 * prevent the registeration of both eDP and LVDS and the incorrect
14115 * sharing of the PPS.
14116 */
c39055b0 14117 intel_lvds_init(dev_priv);
79e53945 14118
6315b5d3 14119 if (intel_crt_present(dev_priv))
c39055b0 14120 intel_crt_init(dev_priv);
cb0953d7 14121
cc3f90f0 14122 if (IS_GEN9_LP(dev_priv)) {
c776eb2e
VK
14123 /*
14124 * FIXME: Broxton doesn't support port detection via the
14125 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14126 * detect the ports.
14127 */
c39055b0
ACO
14128 intel_ddi_init(dev_priv, PORT_A);
14129 intel_ddi_init(dev_priv, PORT_B);
14130 intel_ddi_init(dev_priv, PORT_C);
c6c794a2 14131
c39055b0 14132 intel_dsi_init(dev_priv);
4f8036a2 14133 } else if (HAS_DDI(dev_priv)) {
0e72a5b5
ED
14134 int found;
14135
de31facd
JB
14136 /*
14137 * Haswell uses DDI functions to detect digital outputs.
14138 * On SKL pre-D0 the strap isn't connected, so we assume
14139 * it's there.
14140 */
77179400 14141 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
de31facd 14142 /* WaIgnoreDDIAStrap: skl */
b976dc53 14143 if (found || IS_GEN9_BC(dev_priv))
c39055b0 14144 intel_ddi_init(dev_priv, PORT_A);
0e72a5b5
ED
14145
14146 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14147 * register */
14148 found = I915_READ(SFUSE_STRAP);
14149
14150 if (found & SFUSE_STRAP_DDIB_DETECTED)
c39055b0 14151 intel_ddi_init(dev_priv, PORT_B);
0e72a5b5 14152 if (found & SFUSE_STRAP_DDIC_DETECTED)
c39055b0 14153 intel_ddi_init(dev_priv, PORT_C);
0e72a5b5 14154 if (found & SFUSE_STRAP_DDID_DETECTED)
c39055b0 14155 intel_ddi_init(dev_priv, PORT_D);
2800e4c2
RV
14156 /*
14157 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14158 */
b976dc53 14159 if (IS_GEN9_BC(dev_priv) &&
2800e4c2
RV
14160 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14161 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14162 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
c39055b0 14163 intel_ddi_init(dev_priv, PORT_E);
2800e4c2 14164
6e266956 14165 } else if (HAS_PCH_SPLIT(dev_priv)) {
cb0953d7 14166 int found;
dd11bc10 14167 dpd_is_edp = intel_dp_is_edp(dev_priv, PORT_D);
270b3042 14168
646d5772 14169 if (has_edp_a(dev_priv))
c39055b0 14170 intel_dp_init(dev_priv, DP_A, PORT_A);
cb0953d7 14171
dc0fa718 14172 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 14173 /* PCH SDVOB multiplex with HDMIB */
c39055b0 14174 found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
30ad48b7 14175 if (!found)
c39055b0 14176 intel_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
5eb08b69 14177 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
c39055b0 14178 intel_dp_init(dev_priv, PCH_DP_B, PORT_B);
30ad48b7
ZW
14179 }
14180
dc0fa718 14181 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
c39055b0 14182 intel_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
30ad48b7 14183
dc0fa718 14184 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
c39055b0 14185 intel_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
30ad48b7 14186
5eb08b69 14187 if (I915_READ(PCH_DP_C) & DP_DETECTED)
c39055b0 14188 intel_dp_init(dev_priv, PCH_DP_C, PORT_C);
5eb08b69 14189
270b3042 14190 if (I915_READ(PCH_DP_D) & DP_DETECTED)
c39055b0 14191 intel_dp_init(dev_priv, PCH_DP_D, PORT_D);
920a14b2 14192 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
22f35042 14193 bool has_edp, has_port;
457c52d8 14194
e17ac6db
VS
14195 /*
14196 * The DP_DETECTED bit is the latched state of the DDC
14197 * SDA pin at boot. However since eDP doesn't require DDC
14198 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14199 * eDP ports may have been muxed to an alternate function.
14200 * Thus we can't rely on the DP_DETECTED bit alone to detect
14201 * eDP ports. Consult the VBT as well as DP_DETECTED to
14202 * detect eDP ports.
22f35042
VS
14203 *
14204 * Sadly the straps seem to be missing sometimes even for HDMI
14205 * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
14206 * and VBT for the presence of the port. Additionally we can't
14207 * trust the port type the VBT declares as we've seen at least
14208 * HDMI ports that the VBT claim are DP or eDP.
e17ac6db 14209 */
dd11bc10 14210 has_edp = intel_dp_is_edp(dev_priv, PORT_B);
22f35042
VS
14211 has_port = intel_bios_is_port_present(dev_priv, PORT_B);
14212 if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
c39055b0 14213 has_edp &= intel_dp_init(dev_priv, VLV_DP_B, PORT_B);
22f35042 14214 if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
c39055b0 14215 intel_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
585a94b8 14216
dd11bc10 14217 has_edp = intel_dp_is_edp(dev_priv, PORT_C);
22f35042
VS
14218 has_port = intel_bios_is_port_present(dev_priv, PORT_C);
14219 if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
c39055b0 14220 has_edp &= intel_dp_init(dev_priv, VLV_DP_C, PORT_C);
22f35042 14221 if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
c39055b0 14222 intel_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
19c03924 14223
920a14b2 14224 if (IS_CHERRYVIEW(dev_priv)) {
22f35042
VS
14225 /*
14226 * eDP not supported on port D,
14227 * so no need to worry about it
14228 */
14229 has_port = intel_bios_is_port_present(dev_priv, PORT_D);
14230 if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
c39055b0 14231 intel_dp_init(dev_priv, CHV_DP_D, PORT_D);
22f35042 14232 if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
c39055b0 14233 intel_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
9418c1f1
VS
14234 }
14235
c39055b0 14236 intel_dsi_init(dev_priv);
5db94019 14237 } else if (!IS_GEN2(dev_priv) && !IS_PINEVIEW(dev_priv)) {
27185ae1 14238 bool found = false;
7d57382e 14239
e2debe91 14240 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14241 DRM_DEBUG_KMS("probing SDVOB\n");
c39055b0 14242 found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
9beb5fea 14243 if (!found && IS_G4X(dev_priv)) {
b01f2c3a 14244 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
c39055b0 14245 intel_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
b01f2c3a 14246 }
27185ae1 14247
9beb5fea 14248 if (!found && IS_G4X(dev_priv))
c39055b0 14249 intel_dp_init(dev_priv, DP_B, PORT_B);
725e30ad 14250 }
13520b05
KH
14251
14252 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 14253
e2debe91 14254 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14255 DRM_DEBUG_KMS("probing SDVOC\n");
c39055b0 14256 found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
b01f2c3a 14257 }
27185ae1 14258
e2debe91 14259 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 14260
9beb5fea 14261 if (IS_G4X(dev_priv)) {
b01f2c3a 14262 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
c39055b0 14263 intel_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
b01f2c3a 14264 }
9beb5fea 14265 if (IS_G4X(dev_priv))
c39055b0 14266 intel_dp_init(dev_priv, DP_C, PORT_C);
725e30ad 14267 }
27185ae1 14268
9beb5fea 14269 if (IS_G4X(dev_priv) && (I915_READ(DP_D) & DP_DETECTED))
c39055b0 14270 intel_dp_init(dev_priv, DP_D, PORT_D);
5db94019 14271 } else if (IS_GEN2(dev_priv))
c39055b0 14272 intel_dvo_init(dev_priv);
79e53945 14273
56b857a5 14274 if (SUPPORTS_TV(dev_priv))
c39055b0 14275 intel_tv_init(dev_priv);
79e53945 14276
c39055b0 14277 intel_psr_init(dev_priv);
7c8f8a70 14278
c39055b0 14279 for_each_intel_encoder(&dev_priv->drm, encoder) {
4ef69c7a
CW
14280 encoder->base.possible_crtcs = encoder->crtc_mask;
14281 encoder->base.possible_clones =
66a9278e 14282 intel_encoder_clones(encoder);
79e53945 14283 }
47356eb6 14284
c39055b0 14285 intel_init_pch_refclk(dev_priv);
270b3042 14286
c39055b0 14287 drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
79e53945
JB
14288}
14289
14290static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14291{
14292 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 14293
ef2d633e 14294 drm_framebuffer_cleanup(fb);
70001cd2 14295
dd689287
CW
14296 i915_gem_object_lock(intel_fb->obj);
14297 WARN_ON(!intel_fb->obj->framebuffer_references--);
14298 i915_gem_object_unlock(intel_fb->obj);
14299
f8c417cd 14300 i915_gem_object_put(intel_fb->obj);
70001cd2 14301
79e53945
JB
14302 kfree(intel_fb);
14303}
14304
14305static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 14306 struct drm_file *file,
79e53945
JB
14307 unsigned int *handle)
14308{
14309 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 14310 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 14311
cc917ab4
CW
14312 if (obj->userptr.mm) {
14313 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14314 return -EINVAL;
14315 }
14316
05394f39 14317 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
14318}
14319
86c98588
RV
14320static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14321 struct drm_file *file,
14322 unsigned flags, unsigned color,
14323 struct drm_clip_rect *clips,
14324 unsigned num_clips)
14325{
5a97bcc6 14326 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
86c98588 14327
5a97bcc6 14328 i915_gem_object_flush_if_display(obj);
d59b21ec 14329 intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
86c98588
RV
14330
14331 return 0;
14332}
14333
79e53945
JB
14334static const struct drm_framebuffer_funcs intel_fb_funcs = {
14335 .destroy = intel_user_framebuffer_destroy,
14336 .create_handle = intel_user_framebuffer_create_handle,
86c98588 14337 .dirty = intel_user_framebuffer_dirty,
79e53945
JB
14338};
14339
b321803d 14340static
920a14b2
TU
14341u32 intel_fb_pitch_limit(struct drm_i915_private *dev_priv,
14342 uint64_t fb_modifier, uint32_t pixel_format)
b321803d 14343{
24dbf51a 14344 u32 gen = INTEL_GEN(dev_priv);
b321803d
DL
14345
14346 if (gen >= 9) {
ac484963
VS
14347 int cpp = drm_format_plane_cpp(pixel_format, 0);
14348
b321803d
DL
14349 /* "The stride in bytes must not exceed the of the size of 8K
14350 * pixels and 32K bytes."
14351 */
ac484963 14352 return min(8192 * cpp, 32768);
6401c37d 14353 } else if (gen >= 5 && !HAS_GMCH_DISPLAY(dev_priv)) {
b321803d
DL
14354 return 32*1024;
14355 } else if (gen >= 4) {
14356 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14357 return 16*1024;
14358 else
14359 return 32*1024;
14360 } else if (gen >= 3) {
14361 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14362 return 8*1024;
14363 else
14364 return 16*1024;
14365 } else {
14366 /* XXX DSPC is limited to 4k tiled */
14367 return 8*1024;
14368 }
14369}
14370
24dbf51a
CW
14371static int intel_framebuffer_init(struct intel_framebuffer *intel_fb,
14372 struct drm_i915_gem_object *obj,
14373 struct drm_mode_fb_cmd2 *mode_cmd)
79e53945 14374{
24dbf51a 14375 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
b3c11ac2 14376 struct drm_format_name_buf format_name;
dd689287
CW
14377 u32 pitch_limit, stride_alignment;
14378 unsigned int tiling, stride;
24dbf51a 14379 int ret = -EINVAL;
79e53945 14380
dd689287
CW
14381 i915_gem_object_lock(obj);
14382 obj->framebuffer_references++;
14383 tiling = i915_gem_object_get_tiling(obj);
14384 stride = i915_gem_object_get_stride(obj);
14385 i915_gem_object_unlock(obj);
dd4916c5 14386
2a80eada 14387 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
c2ff7370
VS
14388 /*
14389 * If there's a fence, enforce that
14390 * the fb modifier and tiling mode match.
14391 */
14392 if (tiling != I915_TILING_NONE &&
14393 tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
144cc143 14394 DRM_DEBUG_KMS("tiling_mode doesn't match fb modifier\n");
24dbf51a 14395 goto err;
2a80eada
DV
14396 }
14397 } else {
c2ff7370 14398 if (tiling == I915_TILING_X) {
2a80eada 14399 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
c2ff7370 14400 } else if (tiling == I915_TILING_Y) {
144cc143 14401 DRM_DEBUG_KMS("No Y tiling for legacy addfb\n");
24dbf51a 14402 goto err;
2a80eada
DV
14403 }
14404 }
14405
9a8f0a12
TU
14406 /* Passed in modifier sanity checking. */
14407 switch (mode_cmd->modifier[0]) {
14408 case I915_FORMAT_MOD_Y_TILED:
14409 case I915_FORMAT_MOD_Yf_TILED:
6315b5d3 14410 if (INTEL_GEN(dev_priv) < 9) {
144cc143
VS
14411 DRM_DEBUG_KMS("Unsupported tiling 0x%llx!\n",
14412 mode_cmd->modifier[0]);
24dbf51a 14413 goto err;
9a8f0a12 14414 }
2f075565 14415 case DRM_FORMAT_MOD_LINEAR:
9a8f0a12
TU
14416 case I915_FORMAT_MOD_X_TILED:
14417 break;
14418 default:
144cc143
VS
14419 DRM_DEBUG_KMS("Unsupported fb modifier 0x%llx!\n",
14420 mode_cmd->modifier[0]);
24dbf51a 14421 goto err;
c16ed4be 14422 }
57cd6508 14423
c2ff7370
VS
14424 /*
14425 * gen2/3 display engine uses the fence if present,
14426 * so the tiling mode must match the fb modifier exactly.
14427 */
14428 if (INTEL_INFO(dev_priv)->gen < 4 &&
14429 tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
144cc143 14430 DRM_DEBUG_KMS("tiling_mode must match fb modifier exactly on gen2/3\n");
9aceb5c1 14431 goto err;
c2ff7370
VS
14432 }
14433
920a14b2 14434 pitch_limit = intel_fb_pitch_limit(dev_priv, mode_cmd->modifier[0],
b321803d 14435 mode_cmd->pixel_format);
a35cdaa0 14436 if (mode_cmd->pitches[0] > pitch_limit) {
144cc143 14437 DRM_DEBUG_KMS("%s pitch (%u) must be at most %d\n",
2f075565 14438 mode_cmd->modifier[0] != DRM_FORMAT_MOD_LINEAR ?
144cc143
VS
14439 "tiled" : "linear",
14440 mode_cmd->pitches[0], pitch_limit);
24dbf51a 14441 goto err;
c16ed4be 14442 }
5d7bd705 14443
c2ff7370
VS
14444 /*
14445 * If there's a fence, enforce that
14446 * the fb pitch and fence stride match.
14447 */
144cc143
VS
14448 if (tiling != I915_TILING_NONE && mode_cmd->pitches[0] != stride) {
14449 DRM_DEBUG_KMS("pitch (%d) must match tiling stride (%d)\n",
14450 mode_cmd->pitches[0], stride);
24dbf51a 14451 goto err;
c16ed4be 14452 }
5d7bd705 14453
57779d06 14454 /* Reject formats not supported by any plane early. */
308e5bcb 14455 switch (mode_cmd->pixel_format) {
57779d06 14456 case DRM_FORMAT_C8:
04b3924d
VS
14457 case DRM_FORMAT_RGB565:
14458 case DRM_FORMAT_XRGB8888:
14459 case DRM_FORMAT_ARGB8888:
57779d06
VS
14460 break;
14461 case DRM_FORMAT_XRGB1555:
6315b5d3 14462 if (INTEL_GEN(dev_priv) > 3) {
144cc143
VS
14463 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14464 drm_get_format_name(mode_cmd->pixel_format, &format_name));
9aceb5c1 14465 goto err;
c16ed4be 14466 }
57779d06 14467 break;
57779d06 14468 case DRM_FORMAT_ABGR8888:
920a14b2 14469 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
6315b5d3 14470 INTEL_GEN(dev_priv) < 9) {
144cc143
VS
14471 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14472 drm_get_format_name(mode_cmd->pixel_format, &format_name));
9aceb5c1 14473 goto err;
6c0fd451
DL
14474 }
14475 break;
14476 case DRM_FORMAT_XBGR8888:
04b3924d 14477 case DRM_FORMAT_XRGB2101010:
57779d06 14478 case DRM_FORMAT_XBGR2101010:
6315b5d3 14479 if (INTEL_GEN(dev_priv) < 4) {
144cc143
VS
14480 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14481 drm_get_format_name(mode_cmd->pixel_format, &format_name));
9aceb5c1 14482 goto err;
c16ed4be 14483 }
b5626747 14484 break;
7531208b 14485 case DRM_FORMAT_ABGR2101010:
920a14b2 14486 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
144cc143
VS
14487 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14488 drm_get_format_name(mode_cmd->pixel_format, &format_name));
9aceb5c1 14489 goto err;
7531208b
DL
14490 }
14491 break;
04b3924d
VS
14492 case DRM_FORMAT_YUYV:
14493 case DRM_FORMAT_UYVY:
14494 case DRM_FORMAT_YVYU:
14495 case DRM_FORMAT_VYUY:
ab33081a 14496 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
144cc143
VS
14497 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14498 drm_get_format_name(mode_cmd->pixel_format, &format_name));
9aceb5c1 14499 goto err;
c16ed4be 14500 }
57cd6508
CW
14501 break;
14502 default:
144cc143
VS
14503 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14504 drm_get_format_name(mode_cmd->pixel_format, &format_name));
9aceb5c1 14505 goto err;
57cd6508
CW
14506 }
14507
90f9a336
VS
14508 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14509 if (mode_cmd->offsets[0] != 0)
24dbf51a 14510 goto err;
90f9a336 14511
24dbf51a
CW
14512 drm_helper_mode_fill_fb_struct(&dev_priv->drm,
14513 &intel_fb->base, mode_cmd);
d88c4afd
VS
14514
14515 stride_alignment = intel_fb_stride_alignment(&intel_fb->base, 0);
14516 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
144cc143
VS
14517 DRM_DEBUG_KMS("pitch (%d) must be at least %u byte aligned\n",
14518 mode_cmd->pitches[0], stride_alignment);
d88c4afd
VS
14519 goto err;
14520 }
14521
c7d73f6a
DV
14522 intel_fb->obj = obj;
14523
6687c906
VS
14524 ret = intel_fill_fb_info(dev_priv, &intel_fb->base);
14525 if (ret)
9aceb5c1 14526 goto err;
2d7a215f 14527
24dbf51a
CW
14528 ret = drm_framebuffer_init(obj->base.dev,
14529 &intel_fb->base,
14530 &intel_fb_funcs);
79e53945
JB
14531 if (ret) {
14532 DRM_ERROR("framebuffer init failed %d\n", ret);
24dbf51a 14533 goto err;
79e53945
JB
14534 }
14535
79e53945 14536 return 0;
24dbf51a
CW
14537
14538err:
dd689287
CW
14539 i915_gem_object_lock(obj);
14540 obj->framebuffer_references--;
14541 i915_gem_object_unlock(obj);
24dbf51a 14542 return ret;
79e53945
JB
14543}
14544
79e53945
JB
14545static struct drm_framebuffer *
14546intel_user_framebuffer_create(struct drm_device *dev,
14547 struct drm_file *filp,
1eb83451 14548 const struct drm_mode_fb_cmd2 *user_mode_cmd)
79e53945 14549{
dcb1394e 14550 struct drm_framebuffer *fb;
05394f39 14551 struct drm_i915_gem_object *obj;
76dc3769 14552 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
79e53945 14553
03ac0642
CW
14554 obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
14555 if (!obj)
cce13ff7 14556 return ERR_PTR(-ENOENT);
79e53945 14557
24dbf51a 14558 fb = intel_framebuffer_create(obj, &mode_cmd);
dcb1394e 14559 if (IS_ERR(fb))
f0cd5182 14560 i915_gem_object_put(obj);
dcb1394e
LW
14561
14562 return fb;
79e53945
JB
14563}
14564
778e23a9
CW
14565static void intel_atomic_state_free(struct drm_atomic_state *state)
14566{
14567 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
14568
14569 drm_atomic_state_default_release(state);
14570
14571 i915_sw_fence_fini(&intel_state->commit_ready);
14572
14573 kfree(state);
14574}
14575
79e53945 14576static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 14577 .fb_create = intel_user_framebuffer_create,
0632fef6 14578 .output_poll_changed = intel_fbdev_output_poll_changed,
5ee67f1c
MR
14579 .atomic_check = intel_atomic_check,
14580 .atomic_commit = intel_atomic_commit,
de419ab6
ML
14581 .atomic_state_alloc = intel_atomic_state_alloc,
14582 .atomic_state_clear = intel_atomic_state_clear,
778e23a9 14583 .atomic_state_free = intel_atomic_state_free,
79e53945
JB
14584};
14585
88212941
ID
14586/**
14587 * intel_init_display_hooks - initialize the display modesetting hooks
14588 * @dev_priv: device private
14589 */
14590void intel_init_display_hooks(struct drm_i915_private *dev_priv)
e70236a8 14591{
7ff89ca2
VS
14592 intel_init_cdclk_hooks(dev_priv);
14593
88212941 14594 if (INTEL_INFO(dev_priv)->gen >= 9) {
bc8d7dff 14595 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14596 dev_priv->display.get_initial_plane_config =
14597 skylake_get_initial_plane_config;
bc8d7dff
DL
14598 dev_priv->display.crtc_compute_clock =
14599 haswell_crtc_compute_clock;
14600 dev_priv->display.crtc_enable = haswell_crtc_enable;
14601 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 14602 } else if (HAS_DDI(dev_priv)) {
0e8ffe1b 14603 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14604 dev_priv->display.get_initial_plane_config =
14605 ironlake_get_initial_plane_config;
797d0259
ACO
14606 dev_priv->display.crtc_compute_clock =
14607 haswell_crtc_compute_clock;
4f771f10
PZ
14608 dev_priv->display.crtc_enable = haswell_crtc_enable;
14609 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 14610 } else if (HAS_PCH_SPLIT(dev_priv)) {
0e8ffe1b 14611 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
5724dbd1
DL
14612 dev_priv->display.get_initial_plane_config =
14613 ironlake_get_initial_plane_config;
3fb37703
ACO
14614 dev_priv->display.crtc_compute_clock =
14615 ironlake_crtc_compute_clock;
76e5a89c
DV
14616 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14617 dev_priv->display.crtc_disable = ironlake_crtc_disable;
65b3d6a9 14618 } else if (IS_CHERRYVIEW(dev_priv)) {
89b667f8 14619 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14620 dev_priv->display.get_initial_plane_config =
14621 i9xx_get_initial_plane_config;
65b3d6a9
ACO
14622 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
14623 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14624 dev_priv->display.crtc_disable = i9xx_crtc_disable;
14625 } else if (IS_VALLEYVIEW(dev_priv)) {
14626 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14627 dev_priv->display.get_initial_plane_config =
14628 i9xx_get_initial_plane_config;
14629 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
89b667f8
JB
14630 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14631 dev_priv->display.crtc_disable = i9xx_crtc_disable;
19ec6693
ACO
14632 } else if (IS_G4X(dev_priv)) {
14633 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14634 dev_priv->display.get_initial_plane_config =
14635 i9xx_get_initial_plane_config;
14636 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
14637 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14638 dev_priv->display.crtc_disable = i9xx_crtc_disable;
70e8aa21
ACO
14639 } else if (IS_PINEVIEW(dev_priv)) {
14640 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14641 dev_priv->display.get_initial_plane_config =
14642 i9xx_get_initial_plane_config;
14643 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
14644 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14645 dev_priv->display.crtc_disable = i9xx_crtc_disable;
81c97f52 14646 } else if (!IS_GEN2(dev_priv)) {
0e8ffe1b 14647 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14648 dev_priv->display.get_initial_plane_config =
14649 i9xx_get_initial_plane_config;
d6dfee7a 14650 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
76e5a89c
DV
14651 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14652 dev_priv->display.crtc_disable = i9xx_crtc_disable;
81c97f52
ACO
14653 } else {
14654 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14655 dev_priv->display.get_initial_plane_config =
14656 i9xx_get_initial_plane_config;
14657 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
14658 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14659 dev_priv->display.crtc_disable = i9xx_crtc_disable;
f564048e 14660 }
e70236a8 14661
88212941 14662 if (IS_GEN5(dev_priv)) {
3bb11b53 14663 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
88212941 14664 } else if (IS_GEN6(dev_priv)) {
3bb11b53 14665 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
88212941 14666 } else if (IS_IVYBRIDGE(dev_priv)) {
3bb11b53
SJ
14667 /* FIXME: detect B0+ stepping and use auto training */
14668 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
88212941 14669 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
3bb11b53 14670 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
445e780b
VS
14671 }
14672
27082493
L
14673 if (dev_priv->info.gen >= 9)
14674 dev_priv->display.update_crtcs = skl_update_crtcs;
14675 else
14676 dev_priv->display.update_crtcs = intel_update_crtcs;
14677
5a21b665
DV
14678 switch (INTEL_INFO(dev_priv)->gen) {
14679 case 2:
14680 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14681 break;
14682
14683 case 3:
14684 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14685 break;
14686
14687 case 4:
14688 case 5:
14689 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14690 break;
14691
14692 case 6:
14693 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14694 break;
14695 case 7:
14696 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
14697 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14698 break;
14699 case 9:
14700 /* Drop through - unsupported since execlist only. */
14701 default:
14702 /* Default just returns -ENODEV to indicate unsupported */
14703 dev_priv->display.queue_flip = intel_default_queue_flip;
14704 }
e70236a8
JB
14705}
14706
b690e96c
JB
14707/*
14708 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14709 * resume, or other times. This quirk makes sure that's the case for
14710 * affected systems.
14711 */
0206e353 14712static void quirk_pipea_force(struct drm_device *dev)
b690e96c 14713{
fac5e23e 14714 struct drm_i915_private *dev_priv = to_i915(dev);
b690e96c
JB
14715
14716 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
bc0daf48 14717 DRM_INFO("applying pipe a force quirk\n");
b690e96c
JB
14718}
14719
b6b5d049
VS
14720static void quirk_pipeb_force(struct drm_device *dev)
14721{
fac5e23e 14722 struct drm_i915_private *dev_priv = to_i915(dev);
b6b5d049
VS
14723
14724 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14725 DRM_INFO("applying pipe b force quirk\n");
14726}
14727
435793df
KP
14728/*
14729 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14730 */
14731static void quirk_ssc_force_disable(struct drm_device *dev)
14732{
fac5e23e 14733 struct drm_i915_private *dev_priv = to_i915(dev);
435793df 14734 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 14735 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
14736}
14737
4dca20ef 14738/*
5a15ab5b
CE
14739 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14740 * brightness value
4dca20ef
CE
14741 */
14742static void quirk_invert_brightness(struct drm_device *dev)
14743{
fac5e23e 14744 struct drm_i915_private *dev_priv = to_i915(dev);
4dca20ef 14745 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 14746 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
14747}
14748
9c72cc6f
SD
14749/* Some VBT's incorrectly indicate no backlight is present */
14750static void quirk_backlight_present(struct drm_device *dev)
14751{
fac5e23e 14752 struct drm_i915_private *dev_priv = to_i915(dev);
9c72cc6f
SD
14753 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14754 DRM_INFO("applying backlight present quirk\n");
14755}
14756
b690e96c
JB
14757struct intel_quirk {
14758 int device;
14759 int subsystem_vendor;
14760 int subsystem_device;
14761 void (*hook)(struct drm_device *dev);
14762};
14763
5f85f176
EE
14764/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14765struct intel_dmi_quirk {
14766 void (*hook)(struct drm_device *dev);
14767 const struct dmi_system_id (*dmi_id_list)[];
14768};
14769
14770static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14771{
14772 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14773 return 1;
14774}
14775
14776static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14777 {
14778 .dmi_id_list = &(const struct dmi_system_id[]) {
14779 {
14780 .callback = intel_dmi_reverse_brightness,
14781 .ident = "NCR Corporation",
14782 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14783 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14784 },
14785 },
14786 { } /* terminating entry */
14787 },
14788 .hook = quirk_invert_brightness,
14789 },
14790};
14791
c43b5634 14792static struct intel_quirk intel_quirks[] = {
b690e96c
JB
14793 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
14794 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
14795
b690e96c
JB
14796 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
14797 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
14798
5f080c0f
VS
14799 /* 830 needs to leave pipe A & dpll A up */
14800 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
14801
b6b5d049
VS
14802 /* 830 needs to leave pipe B & dpll B up */
14803 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
14804
435793df
KP
14805 /* Lenovo U160 cannot use SSC on LVDS */
14806 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
14807
14808 /* Sony Vaio Y cannot use SSC on LVDS */
14809 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 14810
be505f64
AH
14811 /* Acer Aspire 5734Z must invert backlight brightness */
14812 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14813
14814 /* Acer/eMachines G725 */
14815 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14816
14817 /* Acer/eMachines e725 */
14818 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
14819
14820 /* Acer/Packard Bell NCL20 */
14821 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
14822
14823 /* Acer Aspire 4736Z */
14824 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
14825
14826 /* Acer Aspire 5336 */
14827 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
2e93a1aa
SD
14828
14829 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
14830 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
d4967d8c 14831
dfb3d47b
SD
14832 /* Acer C720 Chromebook (Core i3 4005U) */
14833 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
14834
b2a9601c 14835 /* Apple Macbook 2,1 (Core 2 T7400) */
14836 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
14837
1b9448b0
JN
14838 /* Apple Macbook 4,1 */
14839 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
14840
d4967d8c
SD
14841 /* Toshiba CB35 Chromebook (Celeron 2955U) */
14842 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
724cb06f
SD
14843
14844 /* HP Chromebook 14 (Celeron 2955U) */
14845 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
cf6f0af9
JN
14846
14847 /* Dell Chromebook 11 */
14848 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
9be64eee
JN
14849
14850 /* Dell Chromebook 11 (2015 version) */
14851 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
b690e96c
JB
14852};
14853
14854static void intel_init_quirks(struct drm_device *dev)
14855{
14856 struct pci_dev *d = dev->pdev;
14857 int i;
14858
14859 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
14860 struct intel_quirk *q = &intel_quirks[i];
14861
14862 if (d->device == q->device &&
14863 (d->subsystem_vendor == q->subsystem_vendor ||
14864 q->subsystem_vendor == PCI_ANY_ID) &&
14865 (d->subsystem_device == q->subsystem_device ||
14866 q->subsystem_device == PCI_ANY_ID))
14867 q->hook(dev);
14868 }
5f85f176
EE
14869 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
14870 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
14871 intel_dmi_quirks[i].hook(dev);
14872 }
b690e96c
JB
14873}
14874
9cce37f4 14875/* Disable the VGA plane that we never use */
29b74b7f 14876static void i915_disable_vga(struct drm_i915_private *dev_priv)
9cce37f4 14877{
52a05c30 14878 struct pci_dev *pdev = dev_priv->drm.pdev;
9cce37f4 14879 u8 sr1;
920a14b2 14880 i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
9cce37f4 14881
2b37c616 14882 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
52a05c30 14883 vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 14884 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
14885 sr1 = inb(VGA_SR_DATA);
14886 outb(sr1 | 1<<5, VGA_SR_DATA);
52a05c30 14887 vga_put(pdev, VGA_RSRC_LEGACY_IO);
9cce37f4
JB
14888 udelay(300);
14889
01f5a626 14890 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9cce37f4
JB
14891 POSTING_READ(vga_reg);
14892}
14893
f817586c
DV
14894void intel_modeset_init_hw(struct drm_device *dev)
14895{
fac5e23e 14896 struct drm_i915_private *dev_priv = to_i915(dev);
1a617b77 14897
4c75b940 14898 intel_update_cdclk(dev_priv);
bb0f4aab 14899 dev_priv->cdclk.logical = dev_priv->cdclk.actual = dev_priv->cdclk.hw;
1a617b77 14900
46f16e63 14901 intel_init_clock_gating(dev_priv);
f817586c
DV
14902}
14903
d93c0372
MR
14904/*
14905 * Calculate what we think the watermarks should be for the state we've read
14906 * out of the hardware and then immediately program those watermarks so that
14907 * we ensure the hardware settings match our internal state.
14908 *
14909 * We can calculate what we think WM's should be by creating a duplicate of the
14910 * current state (which was constructed during hardware readout) and running it
14911 * through the atomic check code to calculate new watermark values in the
14912 * state object.
14913 */
14914static void sanitize_watermarks(struct drm_device *dev)
14915{
14916 struct drm_i915_private *dev_priv = to_i915(dev);
14917 struct drm_atomic_state *state;
ccf010fb 14918 struct intel_atomic_state *intel_state;
d93c0372
MR
14919 struct drm_crtc *crtc;
14920 struct drm_crtc_state *cstate;
14921 struct drm_modeset_acquire_ctx ctx;
14922 int ret;
14923 int i;
14924
14925 /* Only supported on platforms that use atomic watermark design */
ed4a6a7c 14926 if (!dev_priv->display.optimize_watermarks)
d93c0372
MR
14927 return;
14928
14929 /*
14930 * We need to hold connection_mutex before calling duplicate_state so
14931 * that the connector loop is protected.
14932 */
14933 drm_modeset_acquire_init(&ctx, 0);
14934retry:
0cd1262d 14935 ret = drm_modeset_lock_all_ctx(dev, &ctx);
d93c0372
MR
14936 if (ret == -EDEADLK) {
14937 drm_modeset_backoff(&ctx);
14938 goto retry;
14939 } else if (WARN_ON(ret)) {
0cd1262d 14940 goto fail;
d93c0372
MR
14941 }
14942
14943 state = drm_atomic_helper_duplicate_state(dev, &ctx);
14944 if (WARN_ON(IS_ERR(state)))
0cd1262d 14945 goto fail;
d93c0372 14946
ccf010fb
ML
14947 intel_state = to_intel_atomic_state(state);
14948
ed4a6a7c
MR
14949 /*
14950 * Hardware readout is the only time we don't want to calculate
14951 * intermediate watermarks (since we don't trust the current
14952 * watermarks).
14953 */
602ae835
VS
14954 if (!HAS_GMCH_DISPLAY(dev_priv))
14955 intel_state->skip_intermediate_wm = true;
ed4a6a7c 14956
d93c0372
MR
14957 ret = intel_atomic_check(dev, state);
14958 if (ret) {
14959 /*
14960 * If we fail here, it means that the hardware appears to be
14961 * programmed in a way that shouldn't be possible, given our
14962 * understanding of watermark requirements. This might mean a
14963 * mistake in the hardware readout code or a mistake in the
14964 * watermark calculations for a given platform. Raise a WARN
14965 * so that this is noticeable.
14966 *
14967 * If this actually happens, we'll have to just leave the
14968 * BIOS-programmed watermarks untouched and hope for the best.
14969 */
14970 WARN(true, "Could not determine valid watermarks for inherited state\n");
b9a1b717 14971 goto put_state;
d93c0372
MR
14972 }
14973
14974 /* Write calculated watermark values back */
aa5e9b47 14975 for_each_new_crtc_in_state(state, crtc, cstate, i) {
d93c0372
MR
14976 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
14977
ed4a6a7c 14978 cs->wm.need_postvbl_update = true;
ccf010fb 14979 dev_priv->display.optimize_watermarks(intel_state, cs);
d93c0372
MR
14980 }
14981
b9a1b717 14982put_state:
0853695c 14983 drm_atomic_state_put(state);
0cd1262d 14984fail:
d93c0372
MR
14985 drm_modeset_drop_locks(&ctx);
14986 drm_modeset_acquire_fini(&ctx);
14987}
14988
b079bd17 14989int intel_modeset_init(struct drm_device *dev)
79e53945 14990{
72e96d64
JL
14991 struct drm_i915_private *dev_priv = to_i915(dev);
14992 struct i915_ggtt *ggtt = &dev_priv->ggtt;
8cc87b75 14993 enum pipe pipe;
46f297fb 14994 struct intel_crtc *crtc;
79e53945
JB
14995
14996 drm_mode_config_init(dev);
14997
14998 dev->mode_config.min_width = 0;
14999 dev->mode_config.min_height = 0;
15000
019d96cb
DA
15001 dev->mode_config.preferred_depth = 24;
15002 dev->mode_config.prefer_shadow = 1;
15003
25bab385
TU
15004 dev->mode_config.allow_fb_modifiers = true;
15005
e6ecefaa 15006 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 15007
400c19d9 15008 init_llist_head(&dev_priv->atomic_helper.free_list);
eb955eee 15009 INIT_WORK(&dev_priv->atomic_helper.free_work,
ba318c61 15010 intel_atomic_helper_free_state_worker);
eb955eee 15011
b690e96c
JB
15012 intel_init_quirks(dev);
15013
62d75df7 15014 intel_init_pm(dev_priv);
1fa61106 15015
b7f05d4a 15016 if (INTEL_INFO(dev_priv)->num_pipes == 0)
b079bd17 15017 return 0;
e3c74757 15018
69f92f67
LW
15019 /*
15020 * There may be no VBT; and if the BIOS enabled SSC we can
15021 * just keep using it to avoid unnecessary flicker. Whereas if the
15022 * BIOS isn't using it, don't assume it will work even if the VBT
15023 * indicates as much.
15024 */
6e266956 15025 if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
69f92f67
LW
15026 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15027 DREF_SSC1_ENABLE);
15028
15029 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
15030 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
15031 bios_lvds_use_ssc ? "en" : "dis",
15032 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
15033 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
15034 }
15035 }
15036
5db94019 15037 if (IS_GEN2(dev_priv)) {
a6c45cf0
CW
15038 dev->mode_config.max_width = 2048;
15039 dev->mode_config.max_height = 2048;
5db94019 15040 } else if (IS_GEN3(dev_priv)) {
5e4d6fa7
KP
15041 dev->mode_config.max_width = 4096;
15042 dev->mode_config.max_height = 4096;
79e53945 15043 } else {
a6c45cf0
CW
15044 dev->mode_config.max_width = 8192;
15045 dev->mode_config.max_height = 8192;
79e53945 15046 }
068be561 15047
2a307c2e
JN
15048 if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
15049 dev->mode_config.cursor_width = IS_I845G(dev_priv) ? 64 : 512;
dc41c154 15050 dev->mode_config.cursor_height = 1023;
5db94019 15051 } else if (IS_GEN2(dev_priv)) {
068be561
DL
15052 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15053 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15054 } else {
15055 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15056 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15057 }
15058
72e96d64 15059 dev->mode_config.fb_base = ggtt->mappable_base;
79e53945 15060
28c97730 15061 DRM_DEBUG_KMS("%d display pipe%s available.\n",
b7f05d4a
TU
15062 INTEL_INFO(dev_priv)->num_pipes,
15063 INTEL_INFO(dev_priv)->num_pipes > 1 ? "s" : "");
79e53945 15064
055e393f 15065 for_each_pipe(dev_priv, pipe) {
b079bd17
VS
15066 int ret;
15067
5ab0d85b 15068 ret = intel_crtc_init(dev_priv, pipe);
b079bd17
VS
15069 if (ret) {
15070 drm_mode_config_cleanup(dev);
15071 return ret;
15072 }
79e53945
JB
15073 }
15074
e72f9fbf 15075 intel_shared_dpll_init(dev);
ee7b9f93 15076
5be6e334
VS
15077 intel_update_czclk(dev_priv);
15078 intel_modeset_init_hw(dev);
15079
b2045352 15080 if (dev_priv->max_cdclk_freq == 0)
4c75b940 15081 intel_update_max_cdclk(dev_priv);
b2045352 15082
9cce37f4 15083 /* Just disable it once at startup */
29b74b7f 15084 i915_disable_vga(dev_priv);
c39055b0 15085 intel_setup_outputs(dev_priv);
11be49eb 15086
6e9f798d 15087 drm_modeset_lock_all(dev);
043e9bda 15088 intel_modeset_setup_hw_state(dev);
6e9f798d 15089 drm_modeset_unlock_all(dev);
46f297fb 15090
d3fcc808 15091 for_each_intel_crtc(dev, crtc) {
eeebeac5
ML
15092 struct intel_initial_plane_config plane_config = {};
15093
46f297fb
JB
15094 if (!crtc->active)
15095 continue;
15096
46f297fb 15097 /*
46f297fb
JB
15098 * Note that reserving the BIOS fb up front prevents us
15099 * from stuffing other stolen allocations like the ring
15100 * on top. This prevents some ugliness at boot time, and
15101 * can even allow for smooth boot transitions if the BIOS
15102 * fb is large enough for the active pipe configuration.
15103 */
eeebeac5
ML
15104 dev_priv->display.get_initial_plane_config(crtc,
15105 &plane_config);
15106
15107 /*
15108 * If the fb is shared between multiple heads, we'll
15109 * just get the first one.
15110 */
15111 intel_find_initial_plane_obj(crtc, &plane_config);
46f297fb 15112 }
d93c0372
MR
15113
15114 /*
15115 * Make sure hardware watermarks really match the state we read out.
15116 * Note that we need to do this after reconstructing the BIOS fb's
15117 * since the watermark calculation done here will use pstate->fb.
15118 */
602ae835
VS
15119 if (!HAS_GMCH_DISPLAY(dev_priv))
15120 sanitize_watermarks(dev);
b079bd17
VS
15121
15122 return 0;
2c7111db
CW
15123}
15124
7fad798e
DV
15125static void intel_enable_pipe_a(struct drm_device *dev)
15126{
15127 struct intel_connector *connector;
f9e905ca 15128 struct drm_connector_list_iter conn_iter;
7fad798e
DV
15129 struct drm_connector *crt = NULL;
15130 struct intel_load_detect_pipe load_detect_temp;
208bf9fd 15131 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
6c5ed5ae 15132 int ret;
7fad798e
DV
15133
15134 /* We can't just switch on the pipe A, we need to set things up with a
15135 * proper mode and output configuration. As a gross hack, enable pipe A
15136 * by enabling the load detect pipe once. */
f9e905ca
DV
15137 drm_connector_list_iter_begin(dev, &conn_iter);
15138 for_each_intel_connector_iter(connector, &conn_iter) {
7fad798e
DV
15139 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15140 crt = &connector->base;
15141 break;
15142 }
15143 }
f9e905ca 15144 drm_connector_list_iter_end(&conn_iter);
7fad798e
DV
15145
15146 if (!crt)
15147 return;
15148
6c5ed5ae
ML
15149 ret = intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx);
15150 WARN(ret < 0, "All modeset mutexes are locked, but intel_get_load_detect_pipe failed\n");
15151
15152 if (ret > 0)
49172fee 15153 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
7fad798e
DV
15154}
15155
fa555837
DV
15156static bool
15157intel_check_plane_mapping(struct intel_crtc *crtc)
15158{
b7f05d4a 15159 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
649636ef 15160 u32 val;
fa555837 15161
b7f05d4a 15162 if (INTEL_INFO(dev_priv)->num_pipes == 1)
fa555837
DV
15163 return true;
15164
649636ef 15165 val = I915_READ(DSPCNTR(!crtc->plane));
fa555837
DV
15166
15167 if ((val & DISPLAY_PLANE_ENABLE) &&
15168 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15169 return false;
15170
15171 return true;
15172}
15173
02e93c35
VS
15174static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15175{
15176 struct drm_device *dev = crtc->base.dev;
15177 struct intel_encoder *encoder;
15178
15179 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15180 return true;
15181
15182 return false;
15183}
15184
496b0fc3
ML
15185static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
15186{
15187 struct drm_device *dev = encoder->base.dev;
15188 struct intel_connector *connector;
15189
15190 for_each_connector_on_encoder(dev, &encoder->base, connector)
15191 return connector;
15192
15193 return NULL;
15194}
15195
a168f5b3
VS
15196static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
15197 enum transcoder pch_transcoder)
15198{
15199 return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
15200 (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == TRANSCODER_A);
15201}
15202
24929352
DV
15203static void intel_sanitize_crtc(struct intel_crtc *crtc)
15204{
15205 struct drm_device *dev = crtc->base.dev;
fac5e23e 15206 struct drm_i915_private *dev_priv = to_i915(dev);
4d1de975 15207 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
24929352 15208
24929352 15209 /* Clear any frame start delays used for debugging left by the BIOS */
4d1de975
JN
15210 if (!transcoder_is_dsi(cpu_transcoder)) {
15211 i915_reg_t reg = PIPECONF(cpu_transcoder);
15212
15213 I915_WRITE(reg,
15214 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15215 }
24929352 15216
d3eaf884 15217 /* restore vblank interrupts to correct state */
9625604c 15218 drm_crtc_vblank_reset(&crtc->base);
d297e103 15219 if (crtc->active) {
f9cd7b88
VS
15220 struct intel_plane *plane;
15221
9625604c 15222 drm_crtc_vblank_on(&crtc->base);
f9cd7b88
VS
15223
15224 /* Disable everything but the primary plane */
15225 for_each_intel_plane_on_crtc(dev, crtc, plane) {
15226 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
15227 continue;
15228
72259536 15229 trace_intel_disable_plane(&plane->base, crtc);
282dbf9b 15230 plane->disable_plane(plane, crtc);
f9cd7b88 15231 }
9625604c 15232 }
d3eaf884 15233
24929352 15234 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
DV
15235 * disable the crtc (and hence change the state) if it is wrong. Note
15236 * that gen4+ has a fixed plane -> pipe mapping. */
6315b5d3 15237 if (INTEL_GEN(dev_priv) < 4 && !intel_check_plane_mapping(crtc)) {
24929352
DV
15238 bool plane;
15239
78108b7c
VS
15240 DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
15241 crtc->base.base.id, crtc->base.name);
24929352
DV
15242
15243 /* Pipe has the wrong plane attached and the plane is active.
15244 * Temporarily change the plane mapping and disable everything
15245 * ... */
15246 plane = crtc->plane;
1d4258db 15247 crtc->base.primary->state->visible = true;
24929352 15248 crtc->plane = !plane;
b17d48e2 15249 intel_crtc_disable_noatomic(&crtc->base);
24929352 15250 crtc->plane = plane;
24929352 15251 }
24929352 15252
7fad798e
DV
15253 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15254 crtc->pipe == PIPE_A && !crtc->active) {
15255 /* BIOS forgot to enable pipe A, this mostly happens after
15256 * resume. Force-enable the pipe to fix this, the update_dpms
15257 * call below we restore the pipe to the right state, but leave
15258 * the required bits on. */
15259 intel_enable_pipe_a(dev);
15260 }
15261
24929352
DV
15262 /* Adjust the state of the output pipe according to whether we
15263 * have active connectors/encoders. */
842e0307 15264 if (crtc->active && !intel_crtc_has_encoders(crtc))
b17d48e2 15265 intel_crtc_disable_noatomic(&crtc->base);
24929352 15266
49cff963 15267 if (crtc->active || HAS_GMCH_DISPLAY(dev_priv)) {
4cc31489
DV
15268 /*
15269 * We start out with underrun reporting disabled to avoid races.
15270 * For correct bookkeeping mark this on active crtcs.
15271 *
c5ab3bc0
DV
15272 * Also on gmch platforms we dont have any hardware bits to
15273 * disable the underrun reporting. Which means we need to start
15274 * out with underrun reporting disabled also on inactive pipes,
15275 * since otherwise we'll complain about the garbage we read when
15276 * e.g. coming up after runtime pm.
15277 *
4cc31489
DV
15278 * No protection against concurrent access is required - at
15279 * worst a fifo underrun happens which also sets this to false.
15280 */
15281 crtc->cpu_fifo_underrun_disabled = true;
a168f5b3
VS
15282 /*
15283 * We track the PCH trancoder underrun reporting state
15284 * within the crtc. With crtc for pipe A housing the underrun
15285 * reporting state for PCH transcoder A, crtc for pipe B housing
15286 * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
15287 * and marking underrun reporting as disabled for the non-existing
15288 * PCH transcoders B and C would prevent enabling the south
15289 * error interrupt (see cpt_can_enable_serr_int()).
15290 */
15291 if (has_pch_trancoder(dev_priv, (enum transcoder)crtc->pipe))
15292 crtc->pch_fifo_underrun_disabled = true;
4cc31489 15293 }
24929352
DV
15294}
15295
15296static void intel_sanitize_encoder(struct intel_encoder *encoder)
15297{
15298 struct intel_connector *connector;
24929352
DV
15299
15300 /* We need to check both for a crtc link (meaning that the
15301 * encoder is active and trying to read from a pipe) and the
15302 * pipe itself being active. */
15303 bool has_active_crtc = encoder->base.crtc &&
15304 to_intel_crtc(encoder->base.crtc)->active;
15305
496b0fc3
ML
15306 connector = intel_encoder_find_connector(encoder);
15307 if (connector && !has_active_crtc) {
24929352
DV
15308 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15309 encoder->base.base.id,
8e329a03 15310 encoder->base.name);
24929352
DV
15311
15312 /* Connector is active, but has no active pipe. This is
15313 * fallout from our resume register restoring. Disable
15314 * the encoder manually again. */
15315 if (encoder->base.crtc) {
fd6bbda9
ML
15316 struct drm_crtc_state *crtc_state = encoder->base.crtc->state;
15317
24929352
DV
15318 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15319 encoder->base.base.id,
8e329a03 15320 encoder->base.name);
fd6bbda9 15321 encoder->disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
a62d1497 15322 if (encoder->post_disable)
fd6bbda9 15323 encoder->post_disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
24929352 15324 }
7f1950fb 15325 encoder->base.crtc = NULL;
24929352
DV
15326
15327 /* Inconsistent output/port/pipe state happens presumably due to
15328 * a bug in one of the get_hw_state functions. Or someplace else
15329 * in our code, like the register restore mess on resume. Clamp
15330 * things to off as a safer default. */
fd6bbda9
ML
15331
15332 connector->base.dpms = DRM_MODE_DPMS_OFF;
15333 connector->base.encoder = NULL;
24929352
DV
15334 }
15335 /* Enabled encoders without active connectors will be fixed in
15336 * the crtc fixup. */
15337}
15338
29b74b7f 15339void i915_redisable_vga_power_on(struct drm_i915_private *dev_priv)
0fde901f 15340{
920a14b2 15341 i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
0fde901f 15342
04098753
ID
15343 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15344 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
29b74b7f 15345 i915_disable_vga(dev_priv);
04098753
ID
15346 }
15347}
15348
29b74b7f 15349void i915_redisable_vga(struct drm_i915_private *dev_priv)
04098753 15350{
8dc8a27c
PZ
15351 /* This function can be called both from intel_modeset_setup_hw_state or
15352 * at a very early point in our resume sequence, where the power well
15353 * structures are not yet restored. Since this function is at a very
15354 * paranoid "someone might have enabled VGA while we were not looking"
15355 * level, just check if the power well is enabled instead of trying to
15356 * follow the "don't touch the power well if we don't need it" policy
15357 * the rest of the driver uses. */
6392f847 15358 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
15359 return;
15360
29b74b7f 15361 i915_redisable_vga_power_on(dev_priv);
6392f847
ID
15362
15363 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
0fde901f
KM
15364}
15365
f9cd7b88 15366static bool primary_get_hw_state(struct intel_plane *plane)
98ec7739 15367{
f9cd7b88 15368 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
98ec7739 15369
f9cd7b88 15370 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
d032ffa0
ML
15371}
15372
f9cd7b88
VS
15373/* FIXME read out full plane state for all planes */
15374static void readout_plane_state(struct intel_crtc *crtc)
d032ffa0 15375{
e9728bd8
VS
15376 struct intel_plane *primary = to_intel_plane(crtc->base.primary);
15377 bool visible;
d032ffa0 15378
e9728bd8 15379 visible = crtc->active && primary_get_hw_state(primary);
b26d3ea3 15380
e9728bd8
VS
15381 intel_set_plane_visible(to_intel_crtc_state(crtc->base.state),
15382 to_intel_plane_state(primary->base.state),
15383 visible);
98ec7739
VS
15384}
15385
30e984df 15386static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352 15387{
fac5e23e 15388 struct drm_i915_private *dev_priv = to_i915(dev);
24929352 15389 enum pipe pipe;
24929352
DV
15390 struct intel_crtc *crtc;
15391 struct intel_encoder *encoder;
15392 struct intel_connector *connector;
f9e905ca 15393 struct drm_connector_list_iter conn_iter;
5358901f 15394 int i;
24929352 15395
565602d7
ML
15396 dev_priv->active_crtcs = 0;
15397
d3fcc808 15398 for_each_intel_crtc(dev, crtc) {
a8cd6da0
VS
15399 struct intel_crtc_state *crtc_state =
15400 to_intel_crtc_state(crtc->base.state);
3b117c8f 15401
ec2dc6a0 15402 __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
565602d7
ML
15403 memset(crtc_state, 0, sizeof(*crtc_state));
15404 crtc_state->base.crtc = &crtc->base;
24929352 15405
565602d7
ML
15406 crtc_state->base.active = crtc_state->base.enable =
15407 dev_priv->display.get_pipe_config(crtc, crtc_state);
15408
15409 crtc->base.enabled = crtc_state->base.enable;
15410 crtc->active = crtc_state->base.active;
15411
aca1ebf4 15412 if (crtc_state->base.active)
565602d7
ML
15413 dev_priv->active_crtcs |= 1 << crtc->pipe;
15414
f9cd7b88 15415 readout_plane_state(crtc);
24929352 15416
78108b7c
VS
15417 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
15418 crtc->base.base.id, crtc->base.name,
a8cd6da0 15419 enableddisabled(crtc_state->base.active));
24929352
DV
15420 }
15421
5358901f
DV
15422 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15423 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15424
2edd6443 15425 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
2c42e535
ACO
15426 &pll->state.hw_state);
15427 pll->state.crtc_mask = 0;
d3fcc808 15428 for_each_intel_crtc(dev, crtc) {
a8cd6da0
VS
15429 struct intel_crtc_state *crtc_state =
15430 to_intel_crtc_state(crtc->base.state);
15431
15432 if (crtc_state->base.active &&
15433 crtc_state->shared_dpll == pll)
2c42e535 15434 pll->state.crtc_mask |= 1 << crtc->pipe;
5358901f 15435 }
2c42e535 15436 pll->active_mask = pll->state.crtc_mask;
5358901f 15437
1e6f2ddc 15438 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
2c42e535 15439 pll->name, pll->state.crtc_mask, pll->on);
5358901f
DV
15440 }
15441
b2784e15 15442 for_each_intel_encoder(dev, encoder) {
24929352
DV
15443 pipe = 0;
15444
15445 if (encoder->get_hw_state(encoder, &pipe)) {
a8cd6da0
VS
15446 struct intel_crtc_state *crtc_state;
15447
98187836 15448 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
a8cd6da0 15449 crtc_state = to_intel_crtc_state(crtc->base.state);
e2af48c6 15450
045ac3b5 15451 encoder->base.crtc = &crtc->base;
a8cd6da0
VS
15452 crtc_state->output_types |= 1 << encoder->type;
15453 encoder->get_config(encoder, crtc_state);
24929352
DV
15454 } else {
15455 encoder->base.crtc = NULL;
15456 }
15457
6f2bcceb 15458 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
08c4d7fc
TU
15459 encoder->base.base.id, encoder->base.name,
15460 enableddisabled(encoder->base.crtc),
6f2bcceb 15461 pipe_name(pipe));
24929352
DV
15462 }
15463
f9e905ca
DV
15464 drm_connector_list_iter_begin(dev, &conn_iter);
15465 for_each_intel_connector_iter(connector, &conn_iter) {
24929352
DV
15466 if (connector->get_hw_state(connector)) {
15467 connector->base.dpms = DRM_MODE_DPMS_ON;
2aa974c9
ML
15468
15469 encoder = connector->encoder;
15470 connector->base.encoder = &encoder->base;
15471
15472 if (encoder->base.crtc &&
15473 encoder->base.crtc->state->active) {
15474 /*
15475 * This has to be done during hardware readout
15476 * because anything calling .crtc_disable may
15477 * rely on the connector_mask being accurate.
15478 */
15479 encoder->base.crtc->state->connector_mask |=
15480 1 << drm_connector_index(&connector->base);
e87a52b3
ML
15481 encoder->base.crtc->state->encoder_mask |=
15482 1 << drm_encoder_index(&encoder->base);
2aa974c9
ML
15483 }
15484
24929352
DV
15485 } else {
15486 connector->base.dpms = DRM_MODE_DPMS_OFF;
15487 connector->base.encoder = NULL;
15488 }
15489 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
08c4d7fc
TU
15490 connector->base.base.id, connector->base.name,
15491 enableddisabled(connector->base.encoder));
24929352 15492 }
f9e905ca 15493 drm_connector_list_iter_end(&conn_iter);
7f4c6284
VS
15494
15495 for_each_intel_crtc(dev, crtc) {
a8cd6da0
VS
15496 struct intel_crtc_state *crtc_state =
15497 to_intel_crtc_state(crtc->base.state);
aca1ebf4
VS
15498 int pixclk = 0;
15499
7f4c6284 15500 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
a8cd6da0
VS
15501 if (crtc_state->base.active) {
15502 intel_mode_from_pipe_config(&crtc->base.mode, crtc_state);
15503 intel_mode_from_pipe_config(&crtc_state->base.adjusted_mode, crtc_state);
7f4c6284
VS
15504 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
15505
15506 /*
15507 * The initial mode needs to be set in order to keep
15508 * the atomic core happy. It wants a valid mode if the
15509 * crtc's enabled, so we do the above call.
15510 *
7800fb69
DV
15511 * But we don't set all the derived state fully, hence
15512 * set a flag to indicate that a full recalculation is
15513 * needed on the next commit.
7f4c6284 15514 */
a8cd6da0 15515 crtc_state->base.mode.private_flags = I915_MODE_FLAG_INHERITED;
9eca6832 15516
a7d1b3f4
VS
15517 intel_crtc_compute_pixel_rate(crtc_state);
15518
15519 if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv) ||
15520 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
15521 pixclk = crtc_state->pixel_rate;
aca1ebf4
VS
15522 else
15523 WARN_ON(dev_priv->display.modeset_calc_cdclk);
15524
15525 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
a8cd6da0 15526 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
aca1ebf4
VS
15527 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
15528
5caa0fea
DV
15529 drm_calc_timestamping_constants(&crtc->base,
15530 &crtc_state->base.adjusted_mode);
9eca6832 15531 update_scanline_offset(crtc);
7f4c6284 15532 }
e3b247da 15533
aca1ebf4
VS
15534 dev_priv->min_pixclk[crtc->pipe] = pixclk;
15535
a8cd6da0 15536 intel_pipe_config_sanity_check(dev_priv, crtc_state);
7f4c6284 15537 }
30e984df
DV
15538}
15539
62b69566
ACO
15540static void
15541get_encoder_power_domains(struct drm_i915_private *dev_priv)
15542{
15543 struct intel_encoder *encoder;
15544
15545 for_each_intel_encoder(&dev_priv->drm, encoder) {
15546 u64 get_domains;
15547 enum intel_display_power_domain domain;
15548
15549 if (!encoder->get_power_domains)
15550 continue;
15551
15552 get_domains = encoder->get_power_domains(encoder);
15553 for_each_power_domain(domain, get_domains)
15554 intel_display_power_get(dev_priv, domain);
15555 }
15556}
15557
043e9bda
ML
15558/* Scan out the current hw modeset state,
15559 * and sanitizes it to the current state
15560 */
15561static void
15562intel_modeset_setup_hw_state(struct drm_device *dev)
30e984df 15563{
fac5e23e 15564 struct drm_i915_private *dev_priv = to_i915(dev);
30e984df 15565 enum pipe pipe;
30e984df
DV
15566 struct intel_crtc *crtc;
15567 struct intel_encoder *encoder;
35c95375 15568 int i;
30e984df
DV
15569
15570 intel_modeset_readout_hw_state(dev);
24929352
DV
15571
15572 /* HW state is read out, now we need to sanitize this mess. */
62b69566
ACO
15573 get_encoder_power_domains(dev_priv);
15574
b2784e15 15575 for_each_intel_encoder(dev, encoder) {
24929352
DV
15576 intel_sanitize_encoder(encoder);
15577 }
15578
055e393f 15579 for_each_pipe(dev_priv, pipe) {
98187836 15580 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
e2af48c6 15581
24929352 15582 intel_sanitize_crtc(crtc);
6e3c9717
ACO
15583 intel_dump_pipe_config(crtc, crtc->config,
15584 "[setup_hw_state]");
24929352 15585 }
9a935856 15586
d29b2f9d
ACO
15587 intel_modeset_update_connector_atomic_state(dev);
15588
35c95375
DV
15589 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15590 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15591
2dd66ebd 15592 if (!pll->on || pll->active_mask)
35c95375
DV
15593 continue;
15594
15595 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15596
2edd6443 15597 pll->funcs.disable(dev_priv, pll);
35c95375
DV
15598 pll->on = false;
15599 }
15600
04548cba
VS
15601 if (IS_G4X(dev_priv)) {
15602 g4x_wm_get_hw_state(dev);
15603 g4x_wm_sanitize(dev_priv);
15604 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
6eb1a681 15605 vlv_wm_get_hw_state(dev);
602ae835
VS
15606 vlv_wm_sanitize(dev_priv);
15607 } else if (IS_GEN9(dev_priv)) {
3078999f 15608 skl_wm_get_hw_state(dev);
602ae835 15609 } else if (HAS_PCH_SPLIT(dev_priv)) {
243e6a44 15610 ilk_wm_get_hw_state(dev);
602ae835 15611 }
292b990e
ML
15612
15613 for_each_intel_crtc(dev, crtc) {
d8fc70b7 15614 u64 put_domains;
292b990e 15615
74bff5f9 15616 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
292b990e
ML
15617 if (WARN_ON(put_domains))
15618 modeset_put_power_domains(dev_priv, put_domains);
15619 }
15620 intel_display_set_init_power(dev_priv, false);
010cf73d 15621
8d8c386c
ID
15622 intel_power_domains_verify_state(dev_priv);
15623
010cf73d 15624 intel_fbc_init_pipe_state(dev_priv);
043e9bda 15625}
7d0bc1ea 15626
043e9bda
ML
15627void intel_display_resume(struct drm_device *dev)
15628{
e2c8b870
ML
15629 struct drm_i915_private *dev_priv = to_i915(dev);
15630 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
15631 struct drm_modeset_acquire_ctx ctx;
043e9bda 15632 int ret;
f30da187 15633
e2c8b870 15634 dev_priv->modeset_restore_state = NULL;
73974893
ML
15635 if (state)
15636 state->acquire_ctx = &ctx;
043e9bda 15637
e2c8b870 15638 drm_modeset_acquire_init(&ctx, 0);
043e9bda 15639
73974893
ML
15640 while (1) {
15641 ret = drm_modeset_lock_all_ctx(dev, &ctx);
15642 if (ret != -EDEADLK)
15643 break;
043e9bda 15644
e2c8b870 15645 drm_modeset_backoff(&ctx);
e2c8b870 15646 }
043e9bda 15647
73974893 15648 if (!ret)
581e49fe 15649 ret = __intel_display_resume(dev, state, &ctx);
73974893 15650
e2c8b870
ML
15651 drm_modeset_drop_locks(&ctx);
15652 drm_modeset_acquire_fini(&ctx);
043e9bda 15653
0853695c 15654 if (ret)
e2c8b870 15655 DRM_ERROR("Restoring old state failed with %i\n", ret);
3c5e37f1
CW
15656 if (state)
15657 drm_atomic_state_put(state);
2c7111db
CW
15658}
15659
15660void intel_modeset_gem_init(struct drm_device *dev)
15661{
dc97997a 15662 struct drm_i915_private *dev_priv = to_i915(dev);
484b41dd 15663
dc97997a 15664 intel_init_gt_powersave(dev_priv);
ae48434c 15665
1ee8da6d 15666 intel_setup_overlay(dev_priv);
1ebaa0b9
CW
15667}
15668
15669int intel_connector_register(struct drm_connector *connector)
15670{
15671 struct intel_connector *intel_connector = to_intel_connector(connector);
15672 int ret;
15673
15674 ret = intel_backlight_device_register(intel_connector);
15675 if (ret)
15676 goto err;
15677
15678 return 0;
0962c3c9 15679
1ebaa0b9
CW
15680err:
15681 return ret;
79e53945
JB
15682}
15683
c191eca1 15684void intel_connector_unregister(struct drm_connector *connector)
4932e2c3 15685{
e63d87c0 15686 struct intel_connector *intel_connector = to_intel_connector(connector);
4932e2c3 15687
e63d87c0 15688 intel_backlight_device_unregister(intel_connector);
4932e2c3 15689 intel_panel_destroy_backlight(connector);
4932e2c3
ID
15690}
15691
79e53945
JB
15692void intel_modeset_cleanup(struct drm_device *dev)
15693{
fac5e23e 15694 struct drm_i915_private *dev_priv = to_i915(dev);
652c393a 15695
eb955eee
CW
15696 flush_work(&dev_priv->atomic_helper.free_work);
15697 WARN_ON(!llist_empty(&dev_priv->atomic_helper.free_list));
15698
dc97997a 15699 intel_disable_gt_powersave(dev_priv);
2eb5252e 15700
fd0c0642
DV
15701 /*
15702 * Interrupts and polling as the first thing to avoid creating havoc.
2eb5252e 15703 * Too much stuff here (turning of connectors, ...) would
fd0c0642
DV
15704 * experience fancy races otherwise.
15705 */
2aeb7d3a 15706 intel_irq_uninstall(dev_priv);
eb21b92b 15707
fd0c0642
DV
15708 /*
15709 * Due to the hpd irq storm handling the hotplug work can re-arm the
15710 * poll handlers. Hence disable polling after hpd handling is shut down.
15711 */
f87ea761 15712 drm_kms_helper_poll_fini(dev);
fd0c0642 15713
723bfd70
JB
15714 intel_unregister_dsm_handler();
15715
c937ab3e 15716 intel_fbc_global_disable(dev_priv);
69341a5e 15717
1630fe75
CW
15718 /* flush any delayed tasks or pending work */
15719 flush_scheduled_work();
15720
79e53945 15721 drm_mode_config_cleanup(dev);
4d7bb011 15722
1ee8da6d 15723 intel_cleanup_overlay(dev_priv);
ae48434c 15724
dc97997a 15725 intel_cleanup_gt_powersave(dev_priv);
f5949141 15726
40196446 15727 intel_teardown_gmbus(dev_priv);
79e53945
JB
15728}
15729
df0e9248
CW
15730void intel_connector_attach_encoder(struct intel_connector *connector,
15731 struct intel_encoder *encoder)
15732{
15733 connector->encoder = encoder;
15734 drm_mode_connector_attach_encoder(&connector->base,
15735 &encoder->base);
79e53945 15736}
28d52043
DA
15737
15738/*
15739 * set vga decode state - true == enable VGA decode
15740 */
6315b5d3 15741int intel_modeset_vga_set_state(struct drm_i915_private *dev_priv, bool state)
28d52043 15742{
6315b5d3 15743 unsigned reg = INTEL_GEN(dev_priv) >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
15744 u16 gmch_ctrl;
15745
75fa041d
CW
15746 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15747 DRM_ERROR("failed to read control word\n");
15748 return -EIO;
15749 }
15750
c0cc8a55
CW
15751 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15752 return 0;
15753
28d52043
DA
15754 if (state)
15755 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15756 else
15757 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
15758
15759 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15760 DRM_ERROR("failed to write control word\n");
15761 return -EIO;
15762 }
15763
28d52043
DA
15764 return 0;
15765}
c4a1d9e4 15766
98a2f411
CW
15767#if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
15768
c4a1d9e4 15769struct intel_display_error_state {
ff57f1b0
PZ
15770
15771 u32 power_well_driver;
15772
63b66e5b
CW
15773 int num_transcoders;
15774
c4a1d9e4
CW
15775 struct intel_cursor_error_state {
15776 u32 control;
15777 u32 position;
15778 u32 base;
15779 u32 size;
52331309 15780 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
15781
15782 struct intel_pipe_error_state {
ddf9c536 15783 bool power_domain_on;
c4a1d9e4 15784 u32 source;
f301b1e1 15785 u32 stat;
52331309 15786 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
15787
15788 struct intel_plane_error_state {
15789 u32 control;
15790 u32 stride;
15791 u32 size;
15792 u32 pos;
15793 u32 addr;
15794 u32 surface;
15795 u32 tile_offset;
52331309 15796 } plane[I915_MAX_PIPES];
63b66e5b
CW
15797
15798 struct intel_transcoder_error_state {
ddf9c536 15799 bool power_domain_on;
63b66e5b
CW
15800 enum transcoder cpu_transcoder;
15801
15802 u32 conf;
15803
15804 u32 htotal;
15805 u32 hblank;
15806 u32 hsync;
15807 u32 vtotal;
15808 u32 vblank;
15809 u32 vsync;
15810 } transcoder[4];
c4a1d9e4
CW
15811};
15812
15813struct intel_display_error_state *
c033666a 15814intel_display_capture_error_state(struct drm_i915_private *dev_priv)
c4a1d9e4 15815{
c4a1d9e4 15816 struct intel_display_error_state *error;
63b66e5b
CW
15817 int transcoders[] = {
15818 TRANSCODER_A,
15819 TRANSCODER_B,
15820 TRANSCODER_C,
15821 TRANSCODER_EDP,
15822 };
c4a1d9e4
CW
15823 int i;
15824
c033666a 15825 if (INTEL_INFO(dev_priv)->num_pipes == 0)
63b66e5b
CW
15826 return NULL;
15827
9d1cb914 15828 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
15829 if (error == NULL)
15830 return NULL;
15831
c033666a 15832 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
ff57f1b0
PZ
15833 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
15834
055e393f 15835 for_each_pipe(dev_priv, i) {
ddf9c536 15836 error->pipe[i].power_domain_on =
f458ebbc
DV
15837 __intel_display_power_is_enabled(dev_priv,
15838 POWER_DOMAIN_PIPE(i));
ddf9c536 15839 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
15840 continue;
15841
5efb3e28
VS
15842 error->cursor[i].control = I915_READ(CURCNTR(i));
15843 error->cursor[i].position = I915_READ(CURPOS(i));
15844 error->cursor[i].base = I915_READ(CURBASE(i));
c4a1d9e4
CW
15845
15846 error->plane[i].control = I915_READ(DSPCNTR(i));
15847 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
c033666a 15848 if (INTEL_GEN(dev_priv) <= 3) {
51889b35 15849 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
15850 error->plane[i].pos = I915_READ(DSPPOS(i));
15851 }
c033666a 15852 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
ca291363 15853 error->plane[i].addr = I915_READ(DSPADDR(i));
c033666a 15854 if (INTEL_GEN(dev_priv) >= 4) {
c4a1d9e4
CW
15855 error->plane[i].surface = I915_READ(DSPSURF(i));
15856 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15857 }
15858
c4a1d9e4 15859 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1 15860
c033666a 15861 if (HAS_GMCH_DISPLAY(dev_priv))
f301b1e1 15862 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
15863 }
15864
4d1de975 15865 /* Note: this does not include DSI transcoders. */
c033666a 15866 error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
2d1fe073 15867 if (HAS_DDI(dev_priv))
63b66e5b
CW
15868 error->num_transcoders++; /* Account for eDP. */
15869
15870 for (i = 0; i < error->num_transcoders; i++) {
15871 enum transcoder cpu_transcoder = transcoders[i];
15872
ddf9c536 15873 error->transcoder[i].power_domain_on =
f458ebbc 15874 __intel_display_power_is_enabled(dev_priv,
38cc1daf 15875 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 15876 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
15877 continue;
15878
63b66e5b
CW
15879 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15880
15881 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15882 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15883 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15884 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15885 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15886 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15887 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
15888 }
15889
15890 return error;
15891}
15892
edc3d884
MK
15893#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15894
c4a1d9e4 15895void
edc3d884 15896intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
15897 struct intel_display_error_state *error)
15898{
5a4c6f1b 15899 struct drm_i915_private *dev_priv = m->i915;
c4a1d9e4
CW
15900 int i;
15901
63b66e5b
CW
15902 if (!error)
15903 return;
15904
b7f05d4a 15905 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev_priv)->num_pipes);
8652744b 15906 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
edc3d884 15907 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 15908 error->power_well_driver);
055e393f 15909 for_each_pipe(dev_priv, i) {
edc3d884 15910 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536 15911 err_printf(m, " Power: %s\n",
87ad3212 15912 onoff(error->pipe[i].power_domain_on));
edc3d884 15913 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 15914 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
15915
15916 err_printf(m, "Plane [%d]:\n", i);
15917 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15918 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
5f56d5f9 15919 if (INTEL_GEN(dev_priv) <= 3) {
edc3d884
MK
15920 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15921 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 15922 }
772c2a51 15923 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
edc3d884 15924 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
5f56d5f9 15925 if (INTEL_GEN(dev_priv) >= 4) {
edc3d884
MK
15926 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15927 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
15928 }
15929
edc3d884
MK
15930 err_printf(m, "Cursor [%d]:\n", i);
15931 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15932 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15933 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 15934 }
63b66e5b
CW
15935
15936 for (i = 0; i < error->num_transcoders; i++) {
da205630 15937 err_printf(m, "CPU transcoder: %s\n",
63b66e5b 15938 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536 15939 err_printf(m, " Power: %s\n",
87ad3212 15940 onoff(error->transcoder[i].power_domain_on));
63b66e5b
CW
15941 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15942 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15943 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15944 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15945 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15946 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
15947 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
15948 }
c4a1d9e4 15949}
98a2f411
CW
15950
15951#endif