]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpu/drm/i915/intel_display.c
drm/i915: Differentiate between hangcheck waiting for timer or scheduler
[mirror_ubuntu-bionic-kernel.git] / drivers / gpu / drm / i915 / intel_display.c
CommitLineData
79e53945
JB
1/*
2 * Copyright © 2006-2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 */
26
618563e3 27#include <linux/dmi.h>
c1c7af60
JB
28#include <linux/module.h>
29#include <linux/input.h>
79e53945 30#include <linux/i2c.h>
7662c8bd 31#include <linux/kernel.h>
5a0e3ad6 32#include <linux/slab.h>
9cce37f4 33#include <linux/vgaarb.h>
e0dac65e 34#include <drm/drm_edid.h>
760285e7 35#include <drm/drmP.h>
79e53945 36#include "intel_drv.h"
5d723d7a 37#include "intel_frontbuffer.h"
760285e7 38#include <drm/i915_drm.h>
79e53945 39#include "i915_drv.h"
57822dc6 40#include "i915_gem_clflush.h"
db18b6a6 41#include "intel_dsi.h"
e5510fac 42#include "i915_trace.h"
319c1d42 43#include <drm/drm_atomic.h>
c196e1d6 44#include <drm/drm_atomic_helper.h>
760285e7
DH
45#include <drm/drm_dp_helper.h>
46#include <drm/drm_crtc_helper.h>
465c120c
MR
47#include <drm/drm_plane_helper.h>
48#include <drm/drm_rect.h>
c0f372b3 49#include <linux/dma_remapping.h>
fd8e058a 50#include <linux/reservation.h>
79e53945 51
5a21b665
DV
52static bool is_mmio_work(struct intel_flip_work *work)
53{
54 return work->mmio_work.func;
55}
56
465c120c 57/* Primary plane formats for gen <= 3 */
568db4f2 58static const uint32_t i8xx_primary_formats[] = {
67fe7dc5
DL
59 DRM_FORMAT_C8,
60 DRM_FORMAT_RGB565,
465c120c 61 DRM_FORMAT_XRGB1555,
67fe7dc5 62 DRM_FORMAT_XRGB8888,
465c120c
MR
63};
64
65/* Primary plane formats for gen >= 4 */
568db4f2 66static const uint32_t i965_primary_formats[] = {
6c0fd451
DL
67 DRM_FORMAT_C8,
68 DRM_FORMAT_RGB565,
69 DRM_FORMAT_XRGB8888,
70 DRM_FORMAT_XBGR8888,
71 DRM_FORMAT_XRGB2101010,
72 DRM_FORMAT_XBGR2101010,
73};
74
75static const uint32_t skl_primary_formats[] = {
67fe7dc5
DL
76 DRM_FORMAT_C8,
77 DRM_FORMAT_RGB565,
78 DRM_FORMAT_XRGB8888,
465c120c 79 DRM_FORMAT_XBGR8888,
67fe7dc5 80 DRM_FORMAT_ARGB8888,
465c120c
MR
81 DRM_FORMAT_ABGR8888,
82 DRM_FORMAT_XRGB2101010,
465c120c 83 DRM_FORMAT_XBGR2101010,
ea916ea0
KM
84 DRM_FORMAT_YUYV,
85 DRM_FORMAT_YVYU,
86 DRM_FORMAT_UYVY,
87 DRM_FORMAT_VYUY,
465c120c
MR
88};
89
3d7d6510
MR
90/* Cursor formats */
91static const uint32_t intel_cursor_formats[] = {
92 DRM_FORMAT_ARGB8888,
93};
94
f1f644dc 95static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 96 struct intel_crtc_state *pipe_config);
18442d08 97static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 98 struct intel_crtc_state *pipe_config);
f1f644dc 99
24dbf51a
CW
100static int intel_framebuffer_init(struct intel_framebuffer *ifb,
101 struct drm_i915_gem_object *obj,
102 struct drm_mode_fb_cmd2 *mode_cmd);
5b18e57c
DV
103static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
104static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
bc58be60 105static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
29407aab 106static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
107 struct intel_link_m_n *m_n,
108 struct intel_link_m_n *m2_n2);
29407aab 109static void ironlake_set_pipeconf(struct drm_crtc *crtc);
229fca97 110static void haswell_set_pipeconf(struct drm_crtc *crtc);
391bf048 111static void haswell_set_pipemisc(struct drm_crtc *crtc);
d288f65f 112static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 113 const struct intel_crtc_state *pipe_config);
d288f65f 114static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 115 const struct intel_crtc_state *pipe_config);
5a21b665
DV
116static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
117static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
1c74eeaf
NM
118static void intel_crtc_init_scalers(struct intel_crtc *crtc,
119 struct intel_crtc_state *crtc_state);
bfd16b2a
ML
120static void skylake_pfit_enable(struct intel_crtc *crtc);
121static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
122static void ironlake_pfit_enable(struct intel_crtc *crtc);
043e9bda 123static void intel_modeset_setup_hw_state(struct drm_device *dev);
2622a081 124static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
e7457a9a 125
d4906093 126struct intel_limit {
4c5def93
ACO
127 struct {
128 int min, max;
129 } dot, vco, n, m, m1, m2, p, p1;
130
131 struct {
132 int dot_limit;
133 int p2_slow, p2_fast;
134 } p2;
d4906093 135};
79e53945 136
bfa7df01 137/* returns HPLL frequency in kHz */
49cd97a3 138int vlv_get_hpll_vco(struct drm_i915_private *dev_priv)
bfa7df01
VS
139{
140 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
141
142 /* Obtain SKU information */
143 mutex_lock(&dev_priv->sb_lock);
144 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
145 CCK_FUSE_HPLL_FREQ_MASK;
146 mutex_unlock(&dev_priv->sb_lock);
147
148 return vco_freq[hpll_freq] * 1000;
149}
150
c30fec65
VS
151int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
152 const char *name, u32 reg, int ref_freq)
bfa7df01
VS
153{
154 u32 val;
155 int divider;
156
bfa7df01
VS
157 mutex_lock(&dev_priv->sb_lock);
158 val = vlv_cck_read(dev_priv, reg);
159 mutex_unlock(&dev_priv->sb_lock);
160
161 divider = val & CCK_FREQUENCY_VALUES;
162
163 WARN((val & CCK_FREQUENCY_STATUS) !=
164 (divider << CCK_FREQUENCY_STATUS_SHIFT),
165 "%s change in progress\n", name);
166
c30fec65
VS
167 return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
168}
169
7ff89ca2
VS
170int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
171 const char *name, u32 reg)
c30fec65
VS
172{
173 if (dev_priv->hpll_freq == 0)
49cd97a3 174 dev_priv->hpll_freq = vlv_get_hpll_vco(dev_priv);
c30fec65
VS
175
176 return vlv_get_cck_clock(dev_priv, name, reg,
177 dev_priv->hpll_freq);
bfa7df01
VS
178}
179
bfa7df01
VS
180static void intel_update_czclk(struct drm_i915_private *dev_priv)
181{
666a4537 182 if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
bfa7df01
VS
183 return;
184
185 dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
186 CCK_CZ_CLOCK_CONTROL);
187
188 DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
189}
190
021357ac 191static inline u32 /* units of 100MHz */
21a727b3
VS
192intel_fdi_link_freq(struct drm_i915_private *dev_priv,
193 const struct intel_crtc_state *pipe_config)
021357ac 194{
21a727b3
VS
195 if (HAS_DDI(dev_priv))
196 return pipe_config->port_clock; /* SPLL */
197 else if (IS_GEN5(dev_priv))
198 return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
e3b247da 199 else
21a727b3 200 return 270000;
021357ac
CW
201}
202
1b6f4958 203static const struct intel_limit intel_limits_i8xx_dac = {
0206e353 204 .dot = { .min = 25000, .max = 350000 },
9c333719 205 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 206 .n = { .min = 2, .max = 16 },
0206e353
AJ
207 .m = { .min = 96, .max = 140 },
208 .m1 = { .min = 18, .max = 26 },
209 .m2 = { .min = 6, .max = 16 },
210 .p = { .min = 4, .max = 128 },
211 .p1 = { .min = 2, .max = 33 },
273e27ca
EA
212 .p2 = { .dot_limit = 165000,
213 .p2_slow = 4, .p2_fast = 2 },
e4b36699
KP
214};
215
1b6f4958 216static const struct intel_limit intel_limits_i8xx_dvo = {
5d536e28 217 .dot = { .min = 25000, .max = 350000 },
9c333719 218 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 219 .n = { .min = 2, .max = 16 },
5d536e28
DV
220 .m = { .min = 96, .max = 140 },
221 .m1 = { .min = 18, .max = 26 },
222 .m2 = { .min = 6, .max = 16 },
223 .p = { .min = 4, .max = 128 },
224 .p1 = { .min = 2, .max = 33 },
225 .p2 = { .dot_limit = 165000,
226 .p2_slow = 4, .p2_fast = 4 },
227};
228
1b6f4958 229static const struct intel_limit intel_limits_i8xx_lvds = {
0206e353 230 .dot = { .min = 25000, .max = 350000 },
9c333719 231 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 232 .n = { .min = 2, .max = 16 },
0206e353
AJ
233 .m = { .min = 96, .max = 140 },
234 .m1 = { .min = 18, .max = 26 },
235 .m2 = { .min = 6, .max = 16 },
236 .p = { .min = 4, .max = 128 },
237 .p1 = { .min = 1, .max = 6 },
273e27ca
EA
238 .p2 = { .dot_limit = 165000,
239 .p2_slow = 14, .p2_fast = 7 },
e4b36699 240};
273e27ca 241
1b6f4958 242static const struct intel_limit intel_limits_i9xx_sdvo = {
0206e353
AJ
243 .dot = { .min = 20000, .max = 400000 },
244 .vco = { .min = 1400000, .max = 2800000 },
245 .n = { .min = 1, .max = 6 },
246 .m = { .min = 70, .max = 120 },
4f7dfb67
PJ
247 .m1 = { .min = 8, .max = 18 },
248 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
249 .p = { .min = 5, .max = 80 },
250 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
251 .p2 = { .dot_limit = 200000,
252 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
253};
254
1b6f4958 255static const struct intel_limit intel_limits_i9xx_lvds = {
0206e353
AJ
256 .dot = { .min = 20000, .max = 400000 },
257 .vco = { .min = 1400000, .max = 2800000 },
258 .n = { .min = 1, .max = 6 },
259 .m = { .min = 70, .max = 120 },
53a7d2d1
PJ
260 .m1 = { .min = 8, .max = 18 },
261 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
262 .p = { .min = 7, .max = 98 },
263 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
264 .p2 = { .dot_limit = 112000,
265 .p2_slow = 14, .p2_fast = 7 },
e4b36699
KP
266};
267
273e27ca 268
1b6f4958 269static const struct intel_limit intel_limits_g4x_sdvo = {
273e27ca
EA
270 .dot = { .min = 25000, .max = 270000 },
271 .vco = { .min = 1750000, .max = 3500000},
272 .n = { .min = 1, .max = 4 },
273 .m = { .min = 104, .max = 138 },
274 .m1 = { .min = 17, .max = 23 },
275 .m2 = { .min = 5, .max = 11 },
276 .p = { .min = 10, .max = 30 },
277 .p1 = { .min = 1, .max = 3},
278 .p2 = { .dot_limit = 270000,
279 .p2_slow = 10,
280 .p2_fast = 10
044c7c41 281 },
e4b36699
KP
282};
283
1b6f4958 284static const struct intel_limit intel_limits_g4x_hdmi = {
273e27ca
EA
285 .dot = { .min = 22000, .max = 400000 },
286 .vco = { .min = 1750000, .max = 3500000},
287 .n = { .min = 1, .max = 4 },
288 .m = { .min = 104, .max = 138 },
289 .m1 = { .min = 16, .max = 23 },
290 .m2 = { .min = 5, .max = 11 },
291 .p = { .min = 5, .max = 80 },
292 .p1 = { .min = 1, .max = 8},
293 .p2 = { .dot_limit = 165000,
294 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
295};
296
1b6f4958 297static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
273e27ca
EA
298 .dot = { .min = 20000, .max = 115000 },
299 .vco = { .min = 1750000, .max = 3500000 },
300 .n = { .min = 1, .max = 3 },
301 .m = { .min = 104, .max = 138 },
302 .m1 = { .min = 17, .max = 23 },
303 .m2 = { .min = 5, .max = 11 },
304 .p = { .min = 28, .max = 112 },
305 .p1 = { .min = 2, .max = 8 },
306 .p2 = { .dot_limit = 0,
307 .p2_slow = 14, .p2_fast = 14
044c7c41 308 },
e4b36699
KP
309};
310
1b6f4958 311static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
273e27ca
EA
312 .dot = { .min = 80000, .max = 224000 },
313 .vco = { .min = 1750000, .max = 3500000 },
314 .n = { .min = 1, .max = 3 },
315 .m = { .min = 104, .max = 138 },
316 .m1 = { .min = 17, .max = 23 },
317 .m2 = { .min = 5, .max = 11 },
318 .p = { .min = 14, .max = 42 },
319 .p1 = { .min = 2, .max = 6 },
320 .p2 = { .dot_limit = 0,
321 .p2_slow = 7, .p2_fast = 7
044c7c41 322 },
e4b36699
KP
323};
324
1b6f4958 325static const struct intel_limit intel_limits_pineview_sdvo = {
0206e353
AJ
326 .dot = { .min = 20000, .max = 400000},
327 .vco = { .min = 1700000, .max = 3500000 },
273e27ca 328 /* Pineview's Ncounter is a ring counter */
0206e353
AJ
329 .n = { .min = 3, .max = 6 },
330 .m = { .min = 2, .max = 256 },
273e27ca 331 /* Pineview only has one combined m divider, which we treat as m2. */
0206e353
AJ
332 .m1 = { .min = 0, .max = 0 },
333 .m2 = { .min = 0, .max = 254 },
334 .p = { .min = 5, .max = 80 },
335 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
336 .p2 = { .dot_limit = 200000,
337 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
338};
339
1b6f4958 340static const struct intel_limit intel_limits_pineview_lvds = {
0206e353
AJ
341 .dot = { .min = 20000, .max = 400000 },
342 .vco = { .min = 1700000, .max = 3500000 },
343 .n = { .min = 3, .max = 6 },
344 .m = { .min = 2, .max = 256 },
345 .m1 = { .min = 0, .max = 0 },
346 .m2 = { .min = 0, .max = 254 },
347 .p = { .min = 7, .max = 112 },
348 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
349 .p2 = { .dot_limit = 112000,
350 .p2_slow = 14, .p2_fast = 14 },
e4b36699
KP
351};
352
273e27ca
EA
353/* Ironlake / Sandybridge
354 *
355 * We calculate clock using (register_value + 2) for N/M1/M2, so here
356 * the range value for them is (actual_value - 2).
357 */
1b6f4958 358static const struct intel_limit intel_limits_ironlake_dac = {
273e27ca
EA
359 .dot = { .min = 25000, .max = 350000 },
360 .vco = { .min = 1760000, .max = 3510000 },
361 .n = { .min = 1, .max = 5 },
362 .m = { .min = 79, .max = 127 },
363 .m1 = { .min = 12, .max = 22 },
364 .m2 = { .min = 5, .max = 9 },
365 .p = { .min = 5, .max = 80 },
366 .p1 = { .min = 1, .max = 8 },
367 .p2 = { .dot_limit = 225000,
368 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
369};
370
1b6f4958 371static const struct intel_limit intel_limits_ironlake_single_lvds = {
273e27ca
EA
372 .dot = { .min = 25000, .max = 350000 },
373 .vco = { .min = 1760000, .max = 3510000 },
374 .n = { .min = 1, .max = 3 },
375 .m = { .min = 79, .max = 118 },
376 .m1 = { .min = 12, .max = 22 },
377 .m2 = { .min = 5, .max = 9 },
378 .p = { .min = 28, .max = 112 },
379 .p1 = { .min = 2, .max = 8 },
380 .p2 = { .dot_limit = 225000,
381 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
382};
383
1b6f4958 384static const struct intel_limit intel_limits_ironlake_dual_lvds = {
273e27ca
EA
385 .dot = { .min = 25000, .max = 350000 },
386 .vco = { .min = 1760000, .max = 3510000 },
387 .n = { .min = 1, .max = 3 },
388 .m = { .min = 79, .max = 127 },
389 .m1 = { .min = 12, .max = 22 },
390 .m2 = { .min = 5, .max = 9 },
391 .p = { .min = 14, .max = 56 },
392 .p1 = { .min = 2, .max = 8 },
393 .p2 = { .dot_limit = 225000,
394 .p2_slow = 7, .p2_fast = 7 },
b91ad0ec
ZW
395};
396
273e27ca 397/* LVDS 100mhz refclk limits. */
1b6f4958 398static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
273e27ca
EA
399 .dot = { .min = 25000, .max = 350000 },
400 .vco = { .min = 1760000, .max = 3510000 },
401 .n = { .min = 1, .max = 2 },
402 .m = { .min = 79, .max = 126 },
403 .m1 = { .min = 12, .max = 22 },
404 .m2 = { .min = 5, .max = 9 },
405 .p = { .min = 28, .max = 112 },
0206e353 406 .p1 = { .min = 2, .max = 8 },
273e27ca
EA
407 .p2 = { .dot_limit = 225000,
408 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
409};
410
1b6f4958 411static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
273e27ca
EA
412 .dot = { .min = 25000, .max = 350000 },
413 .vco = { .min = 1760000, .max = 3510000 },
414 .n = { .min = 1, .max = 3 },
415 .m = { .min = 79, .max = 126 },
416 .m1 = { .min = 12, .max = 22 },
417 .m2 = { .min = 5, .max = 9 },
418 .p = { .min = 14, .max = 42 },
0206e353 419 .p1 = { .min = 2, .max = 6 },
273e27ca
EA
420 .p2 = { .dot_limit = 225000,
421 .p2_slow = 7, .p2_fast = 7 },
4547668a
ZY
422};
423
1b6f4958 424static const struct intel_limit intel_limits_vlv = {
f01b7962
VS
425 /*
426 * These are the data rate limits (measured in fast clocks)
427 * since those are the strictest limits we have. The fast
428 * clock and actual rate limits are more relaxed, so checking
429 * them would make no difference.
430 */
431 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
75e53986 432 .vco = { .min = 4000000, .max = 6000000 },
a0c4da24 433 .n = { .min = 1, .max = 7 },
a0c4da24
JB
434 .m1 = { .min = 2, .max = 3 },
435 .m2 = { .min = 11, .max = 156 },
b99ab663 436 .p1 = { .min = 2, .max = 3 },
5fdc9c49 437 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
a0c4da24
JB
438};
439
1b6f4958 440static const struct intel_limit intel_limits_chv = {
ef9348c8
CML
441 /*
442 * These are the data rate limits (measured in fast clocks)
443 * since those are the strictest limits we have. The fast
444 * clock and actual rate limits are more relaxed, so checking
445 * them would make no difference.
446 */
447 .dot = { .min = 25000 * 5, .max = 540000 * 5},
17fe1021 448 .vco = { .min = 4800000, .max = 6480000 },
ef9348c8
CML
449 .n = { .min = 1, .max = 1 },
450 .m1 = { .min = 2, .max = 2 },
451 .m2 = { .min = 24 << 22, .max = 175 << 22 },
452 .p1 = { .min = 2, .max = 4 },
453 .p2 = { .p2_slow = 1, .p2_fast = 14 },
454};
455
1b6f4958 456static const struct intel_limit intel_limits_bxt = {
5ab7b0b7
ID
457 /* FIXME: find real dot limits */
458 .dot = { .min = 0, .max = INT_MAX },
e6292556 459 .vco = { .min = 4800000, .max = 6700000 },
5ab7b0b7
ID
460 .n = { .min = 1, .max = 1 },
461 .m1 = { .min = 2, .max = 2 },
462 /* FIXME: find real m2 limits */
463 .m2 = { .min = 2 << 22, .max = 255 << 22 },
464 .p1 = { .min = 2, .max = 4 },
465 .p2 = { .p2_slow = 1, .p2_fast = 20 },
466};
467
cdba954e
ACO
468static bool
469needs_modeset(struct drm_crtc_state *state)
470{
fc596660 471 return drm_atomic_crtc_needs_modeset(state);
cdba954e
ACO
472}
473
dccbea3b
ID
474/*
475 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
476 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
477 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
478 * The helpers' return value is the rate of the clock that is fed to the
479 * display engine's pipe which can be the above fast dot clock rate or a
480 * divided-down version of it.
481 */
f2b115e6 482/* m1 is reserved as 0 in Pineview, n is a ring counter */
9e2c8475 483static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
79e53945 484{
2177832f
SL
485 clock->m = clock->m2 + 2;
486 clock->p = clock->p1 * clock->p2;
ed5ca77e 487 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 488 return 0;
fb03ac01
VS
489 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
490 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
491
492 return clock->dot;
2177832f
SL
493}
494
7429e9d4
DV
495static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
496{
497 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
498}
499
9e2c8475 500static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
2177832f 501{
7429e9d4 502 clock->m = i9xx_dpll_compute_m(clock);
79e53945 503 clock->p = clock->p1 * clock->p2;
ed5ca77e 504 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
dccbea3b 505 return 0;
fb03ac01
VS
506 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
507 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
508
509 return clock->dot;
79e53945
JB
510}
511
9e2c8475 512static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
589eca67
ID
513{
514 clock->m = clock->m1 * clock->m2;
515 clock->p = clock->p1 * clock->p2;
516 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 517 return 0;
589eca67
ID
518 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
519 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
520
521 return clock->dot / 5;
589eca67
ID
522}
523
9e2c8475 524int chv_calc_dpll_params(int refclk, struct dpll *clock)
ef9348c8
CML
525{
526 clock->m = clock->m1 * clock->m2;
527 clock->p = clock->p1 * clock->p2;
528 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 529 return 0;
ef9348c8
CML
530 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
531 clock->n << 22);
532 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
533
534 return clock->dot / 5;
ef9348c8
CML
535}
536
7c04d1d9 537#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
79e53945
JB
538/**
539 * Returns whether the given set of divisors are valid for a given refclk with
540 * the given connectors.
541 */
542
e2d214ae 543static bool intel_PLL_is_valid(struct drm_i915_private *dev_priv,
1b6f4958 544 const struct intel_limit *limit,
9e2c8475 545 const struct dpll *clock)
79e53945 546{
f01b7962
VS
547 if (clock->n < limit->n.min || limit->n.max < clock->n)
548 INTELPllInvalid("n out of range\n");
79e53945 549 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
0206e353 550 INTELPllInvalid("p1 out of range\n");
79e53945 551 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
0206e353 552 INTELPllInvalid("m2 out of range\n");
79e53945 553 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
0206e353 554 INTELPllInvalid("m1 out of range\n");
f01b7962 555
e2d214ae 556 if (!IS_PINEVIEW(dev_priv) && !IS_VALLEYVIEW(dev_priv) &&
cc3f90f0 557 !IS_CHERRYVIEW(dev_priv) && !IS_GEN9_LP(dev_priv))
f01b7962
VS
558 if (clock->m1 <= clock->m2)
559 INTELPllInvalid("m1 <= m2\n");
560
e2d214ae 561 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
cc3f90f0 562 !IS_GEN9_LP(dev_priv)) {
f01b7962
VS
563 if (clock->p < limit->p.min || limit->p.max < clock->p)
564 INTELPllInvalid("p out of range\n");
565 if (clock->m < limit->m.min || limit->m.max < clock->m)
566 INTELPllInvalid("m out of range\n");
567 }
568
79e53945 569 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
0206e353 570 INTELPllInvalid("vco out of range\n");
79e53945
JB
571 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
572 * connector, etc., rather than just a single range.
573 */
574 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
0206e353 575 INTELPllInvalid("dot out of range\n");
79e53945
JB
576
577 return true;
578}
579
3b1429d9 580static int
1b6f4958 581i9xx_select_p2_div(const struct intel_limit *limit,
3b1429d9
VS
582 const struct intel_crtc_state *crtc_state,
583 int target)
79e53945 584{
3b1429d9 585 struct drm_device *dev = crtc_state->base.crtc->dev;
79e53945 586
2d84d2b3 587 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
79e53945 588 /*
a210b028
DV
589 * For LVDS just rely on its current settings for dual-channel.
590 * We haven't figured out how to reliably set up different
591 * single/dual channel state, if we even can.
79e53945 592 */
1974cad0 593 if (intel_is_dual_link_lvds(dev))
3b1429d9 594 return limit->p2.p2_fast;
79e53945 595 else
3b1429d9 596 return limit->p2.p2_slow;
79e53945
JB
597 } else {
598 if (target < limit->p2.dot_limit)
3b1429d9 599 return limit->p2.p2_slow;
79e53945 600 else
3b1429d9 601 return limit->p2.p2_fast;
79e53945 602 }
3b1429d9
VS
603}
604
70e8aa21
ACO
605/*
606 * Returns a set of divisors for the desired target clock with the given
607 * refclk, or FALSE. The returned values represent the clock equation:
608 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
609 *
610 * Target and reference clocks are specified in kHz.
611 *
612 * If match_clock is provided, then best_clock P divider must match the P
613 * divider from @match_clock used for LVDS downclocking.
614 */
3b1429d9 615static bool
1b6f4958 616i9xx_find_best_dpll(const struct intel_limit *limit,
3b1429d9 617 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
618 int target, int refclk, struct dpll *match_clock,
619 struct dpll *best_clock)
3b1429d9
VS
620{
621 struct drm_device *dev = crtc_state->base.crtc->dev;
9e2c8475 622 struct dpll clock;
3b1429d9 623 int err = target;
79e53945 624
0206e353 625 memset(best_clock, 0, sizeof(*best_clock));
79e53945 626
3b1429d9
VS
627 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
628
42158660
ZY
629 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
630 clock.m1++) {
631 for (clock.m2 = limit->m2.min;
632 clock.m2 <= limit->m2.max; clock.m2++) {
c0efc387 633 if (clock.m2 >= clock.m1)
42158660
ZY
634 break;
635 for (clock.n = limit->n.min;
636 clock.n <= limit->n.max; clock.n++) {
637 for (clock.p1 = limit->p1.min;
638 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
639 int this_err;
640
dccbea3b 641 i9xx_calc_dpll_params(refclk, &clock);
e2d214ae
TU
642 if (!intel_PLL_is_valid(to_i915(dev),
643 limit,
ac58c3f0
DV
644 &clock))
645 continue;
646 if (match_clock &&
647 clock.p != match_clock->p)
648 continue;
649
650 this_err = abs(clock.dot - target);
651 if (this_err < err) {
652 *best_clock = clock;
653 err = this_err;
654 }
655 }
656 }
657 }
658 }
659
660 return (err != target);
661}
662
70e8aa21
ACO
663/*
664 * Returns a set of divisors for the desired target clock with the given
665 * refclk, or FALSE. The returned values represent the clock equation:
666 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
667 *
668 * Target and reference clocks are specified in kHz.
669 *
670 * If match_clock is provided, then best_clock P divider must match the P
671 * divider from @match_clock used for LVDS downclocking.
672 */
ac58c3f0 673static bool
1b6f4958 674pnv_find_best_dpll(const struct intel_limit *limit,
a93e255f 675 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
676 int target, int refclk, struct dpll *match_clock,
677 struct dpll *best_clock)
79e53945 678{
3b1429d9 679 struct drm_device *dev = crtc_state->base.crtc->dev;
9e2c8475 680 struct dpll clock;
79e53945
JB
681 int err = target;
682
0206e353 683 memset(best_clock, 0, sizeof(*best_clock));
79e53945 684
3b1429d9
VS
685 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
686
42158660
ZY
687 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
688 clock.m1++) {
689 for (clock.m2 = limit->m2.min;
690 clock.m2 <= limit->m2.max; clock.m2++) {
42158660
ZY
691 for (clock.n = limit->n.min;
692 clock.n <= limit->n.max; clock.n++) {
693 for (clock.p1 = limit->p1.min;
694 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
695 int this_err;
696
dccbea3b 697 pnv_calc_dpll_params(refclk, &clock);
e2d214ae
TU
698 if (!intel_PLL_is_valid(to_i915(dev),
699 limit,
1b894b59 700 &clock))
79e53945 701 continue;
cec2f356
SP
702 if (match_clock &&
703 clock.p != match_clock->p)
704 continue;
79e53945
JB
705
706 this_err = abs(clock.dot - target);
707 if (this_err < err) {
708 *best_clock = clock;
709 err = this_err;
710 }
711 }
712 }
713 }
714 }
715
716 return (err != target);
717}
718
997c030c
ACO
719/*
720 * Returns a set of divisors for the desired target clock with the given
721 * refclk, or FALSE. The returned values represent the clock equation:
722 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
70e8aa21
ACO
723 *
724 * Target and reference clocks are specified in kHz.
725 *
726 * If match_clock is provided, then best_clock P divider must match the P
727 * divider from @match_clock used for LVDS downclocking.
997c030c 728 */
d4906093 729static bool
1b6f4958 730g4x_find_best_dpll(const struct intel_limit *limit,
a93e255f 731 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
732 int target, int refclk, struct dpll *match_clock,
733 struct dpll *best_clock)
d4906093 734{
3b1429d9 735 struct drm_device *dev = crtc_state->base.crtc->dev;
9e2c8475 736 struct dpll clock;
d4906093 737 int max_n;
3b1429d9 738 bool found = false;
6ba770dc
AJ
739 /* approximately equals target * 0.00585 */
740 int err_most = (target >> 8) + (target >> 9);
d4906093
ML
741
742 memset(best_clock, 0, sizeof(*best_clock));
3b1429d9
VS
743
744 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
745
d4906093 746 max_n = limit->n.max;
f77f13e2 747 /* based on hardware requirement, prefer smaller n to precision */
d4906093 748 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
f77f13e2 749 /* based on hardware requirement, prefere larger m1,m2 */
d4906093
ML
750 for (clock.m1 = limit->m1.max;
751 clock.m1 >= limit->m1.min; clock.m1--) {
752 for (clock.m2 = limit->m2.max;
753 clock.m2 >= limit->m2.min; clock.m2--) {
754 for (clock.p1 = limit->p1.max;
755 clock.p1 >= limit->p1.min; clock.p1--) {
756 int this_err;
757
dccbea3b 758 i9xx_calc_dpll_params(refclk, &clock);
e2d214ae
TU
759 if (!intel_PLL_is_valid(to_i915(dev),
760 limit,
1b894b59 761 &clock))
d4906093 762 continue;
1b894b59
CW
763
764 this_err = abs(clock.dot - target);
d4906093
ML
765 if (this_err < err_most) {
766 *best_clock = clock;
767 err_most = this_err;
768 max_n = clock.n;
769 found = true;
770 }
771 }
772 }
773 }
774 }
2c07245f
ZW
775 return found;
776}
777
d5dd62bd
ID
778/*
779 * Check if the calculated PLL configuration is more optimal compared to the
780 * best configuration and error found so far. Return the calculated error.
781 */
782static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
9e2c8475
ACO
783 const struct dpll *calculated_clock,
784 const struct dpll *best_clock,
d5dd62bd
ID
785 unsigned int best_error_ppm,
786 unsigned int *error_ppm)
787{
9ca3ba01
ID
788 /*
789 * For CHV ignore the error and consider only the P value.
790 * Prefer a bigger P value based on HW requirements.
791 */
920a14b2 792 if (IS_CHERRYVIEW(to_i915(dev))) {
9ca3ba01
ID
793 *error_ppm = 0;
794
795 return calculated_clock->p > best_clock->p;
796 }
797
24be4e46
ID
798 if (WARN_ON_ONCE(!target_freq))
799 return false;
800
d5dd62bd
ID
801 *error_ppm = div_u64(1000000ULL *
802 abs(target_freq - calculated_clock->dot),
803 target_freq);
804 /*
805 * Prefer a better P value over a better (smaller) error if the error
806 * is small. Ensure this preference for future configurations too by
807 * setting the error to 0.
808 */
809 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
810 *error_ppm = 0;
811
812 return true;
813 }
814
815 return *error_ppm + 10 < best_error_ppm;
816}
817
65b3d6a9
ACO
818/*
819 * Returns a set of divisors for the desired target clock with the given
820 * refclk, or FALSE. The returned values represent the clock equation:
821 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
822 */
a0c4da24 823static bool
1b6f4958 824vlv_find_best_dpll(const struct intel_limit *limit,
a93e255f 825 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
826 int target, int refclk, struct dpll *match_clock,
827 struct dpll *best_clock)
a0c4da24 828{
a93e255f 829 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 830 struct drm_device *dev = crtc->base.dev;
9e2c8475 831 struct dpll clock;
69e4f900 832 unsigned int bestppm = 1000000;
27e639bf
VS
833 /* min update 19.2 MHz */
834 int max_n = min(limit->n.max, refclk / 19200);
49e497ef 835 bool found = false;
a0c4da24 836
6b4bf1c4
VS
837 target *= 5; /* fast clock */
838
839 memset(best_clock, 0, sizeof(*best_clock));
a0c4da24
JB
840
841 /* based on hardware requirement, prefer smaller n to precision */
27e639bf 842 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
811bbf05 843 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
889059d8 844 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
c1a9ae43 845 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
6b4bf1c4 846 clock.p = clock.p1 * clock.p2;
a0c4da24 847 /* based on hardware requirement, prefer bigger m1,m2 values */
6b4bf1c4 848 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
d5dd62bd 849 unsigned int ppm;
69e4f900 850
6b4bf1c4
VS
851 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
852 refclk * clock.m1);
853
dccbea3b 854 vlv_calc_dpll_params(refclk, &clock);
43b0ac53 855
e2d214ae
TU
856 if (!intel_PLL_is_valid(to_i915(dev),
857 limit,
f01b7962 858 &clock))
43b0ac53
VS
859 continue;
860
d5dd62bd
ID
861 if (!vlv_PLL_is_optimal(dev, target,
862 &clock,
863 best_clock,
864 bestppm, &ppm))
865 continue;
6b4bf1c4 866
d5dd62bd
ID
867 *best_clock = clock;
868 bestppm = ppm;
869 found = true;
a0c4da24
JB
870 }
871 }
872 }
873 }
a0c4da24 874
49e497ef 875 return found;
a0c4da24 876}
a4fc5ed6 877
65b3d6a9
ACO
878/*
879 * Returns a set of divisors for the desired target clock with the given
880 * refclk, or FALSE. The returned values represent the clock equation:
881 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
882 */
ef9348c8 883static bool
1b6f4958 884chv_find_best_dpll(const struct intel_limit *limit,
a93e255f 885 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
886 int target, int refclk, struct dpll *match_clock,
887 struct dpll *best_clock)
ef9348c8 888{
a93e255f 889 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 890 struct drm_device *dev = crtc->base.dev;
9ca3ba01 891 unsigned int best_error_ppm;
9e2c8475 892 struct dpll clock;
ef9348c8
CML
893 uint64_t m2;
894 int found = false;
895
896 memset(best_clock, 0, sizeof(*best_clock));
9ca3ba01 897 best_error_ppm = 1000000;
ef9348c8
CML
898
899 /*
900 * Based on hardware doc, the n always set to 1, and m1 always
901 * set to 2. If requires to support 200Mhz refclk, we need to
902 * revisit this because n may not 1 anymore.
903 */
904 clock.n = 1, clock.m1 = 2;
905 target *= 5; /* fast clock */
906
907 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
908 for (clock.p2 = limit->p2.p2_fast;
909 clock.p2 >= limit->p2.p2_slow;
910 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
9ca3ba01 911 unsigned int error_ppm;
ef9348c8
CML
912
913 clock.p = clock.p1 * clock.p2;
914
915 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
916 clock.n) << 22, refclk * clock.m1);
917
918 if (m2 > INT_MAX/clock.m1)
919 continue;
920
921 clock.m2 = m2;
922
dccbea3b 923 chv_calc_dpll_params(refclk, &clock);
ef9348c8 924
e2d214ae 925 if (!intel_PLL_is_valid(to_i915(dev), limit, &clock))
ef9348c8
CML
926 continue;
927
9ca3ba01
ID
928 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
929 best_error_ppm, &error_ppm))
930 continue;
931
932 *best_clock = clock;
933 best_error_ppm = error_ppm;
934 found = true;
ef9348c8
CML
935 }
936 }
937
938 return found;
939}
940
5ab7b0b7 941bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
9e2c8475 942 struct dpll *best_clock)
5ab7b0b7 943{
65b3d6a9 944 int refclk = 100000;
1b6f4958 945 const struct intel_limit *limit = &intel_limits_bxt;
5ab7b0b7 946
65b3d6a9 947 return chv_find_best_dpll(limit, crtc_state,
5ab7b0b7
ID
948 target_clock, refclk, NULL, best_clock);
949}
950
525b9311 951bool intel_crtc_active(struct intel_crtc *crtc)
20ddf665 952{
20ddf665
VS
953 /* Be paranoid as we can arrive here with only partial
954 * state retrieved from the hardware during setup.
955 *
241bfc38 956 * We can ditch the adjusted_mode.crtc_clock check as soon
20ddf665
VS
957 * as Haswell has gained clock readout/fastboot support.
958 *
66e514c1 959 * We can ditch the crtc->primary->fb check as soon as we can
20ddf665 960 * properly reconstruct framebuffers.
c3d1f436
MR
961 *
962 * FIXME: The intel_crtc->active here should be switched to
963 * crtc->state->active once we have proper CRTC states wired up
964 * for atomic.
20ddf665 965 */
525b9311
VS
966 return crtc->active && crtc->base.primary->state->fb &&
967 crtc->config->base.adjusted_mode.crtc_clock;
20ddf665
VS
968}
969
a5c961d1
PZ
970enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
971 enum pipe pipe)
972{
98187836 973 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
a5c961d1 974
e2af48c6 975 return crtc->config->cpu_transcoder;
a5c961d1
PZ
976}
977
6315b5d3 978static bool pipe_dsl_stopped(struct drm_i915_private *dev_priv, enum pipe pipe)
fbf49ea2 979{
f0f59a00 980 i915_reg_t reg = PIPEDSL(pipe);
fbf49ea2
VS
981 u32 line1, line2;
982 u32 line_mask;
983
5db94019 984 if (IS_GEN2(dev_priv))
fbf49ea2
VS
985 line_mask = DSL_LINEMASK_GEN2;
986 else
987 line_mask = DSL_LINEMASK_GEN3;
988
989 line1 = I915_READ(reg) & line_mask;
6adfb1ef 990 msleep(5);
fbf49ea2
VS
991 line2 = I915_READ(reg) & line_mask;
992
993 return line1 == line2;
994}
995
ab7ad7f6
KP
996/*
997 * intel_wait_for_pipe_off - wait for pipe to turn off
575f7ab7 998 * @crtc: crtc whose pipe to wait for
9d0498a2
JB
999 *
1000 * After disabling a pipe, we can't wait for vblank in the usual way,
1001 * spinning on the vblank interrupt status bit, since we won't actually
1002 * see an interrupt when the pipe is disabled.
1003 *
ab7ad7f6
KP
1004 * On Gen4 and above:
1005 * wait for the pipe register state bit to turn off
1006 *
1007 * Otherwise:
1008 * wait for the display line value to settle (it usually
1009 * ends up stopping at the start of the next frame).
58e10eb9 1010 *
9d0498a2 1011 */
575f7ab7 1012static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
9d0498a2 1013{
6315b5d3 1014 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6e3c9717 1015 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 1016 enum pipe pipe = crtc->pipe;
ab7ad7f6 1017
6315b5d3 1018 if (INTEL_GEN(dev_priv) >= 4) {
f0f59a00 1019 i915_reg_t reg = PIPECONF(cpu_transcoder);
ab7ad7f6
KP
1020
1021 /* Wait for the Pipe State to go off */
b8511f53
CW
1022 if (intel_wait_for_register(dev_priv,
1023 reg, I965_PIPECONF_ACTIVE, 0,
1024 100))
284637d9 1025 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1026 } else {
ab7ad7f6 1027 /* Wait for the display line to settle */
6315b5d3 1028 if (wait_for(pipe_dsl_stopped(dev_priv, pipe), 100))
284637d9 1029 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1030 }
79e53945
JB
1031}
1032
b24e7179 1033/* Only for pre-ILK configs */
55607e8a
DV
1034void assert_pll(struct drm_i915_private *dev_priv,
1035 enum pipe pipe, bool state)
b24e7179 1036{
b24e7179
JB
1037 u32 val;
1038 bool cur_state;
1039
649636ef 1040 val = I915_READ(DPLL(pipe));
b24e7179 1041 cur_state = !!(val & DPLL_VCO_ENABLE);
e2c719b7 1042 I915_STATE_WARN(cur_state != state,
b24e7179 1043 "PLL state assertion failure (expected %s, current %s)\n",
87ad3212 1044 onoff(state), onoff(cur_state));
b24e7179 1045}
b24e7179 1046
23538ef1 1047/* XXX: the dsi pll is shared between MIPI DSI ports */
8563b1e8 1048void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
23538ef1
JN
1049{
1050 u32 val;
1051 bool cur_state;
1052
a580516d 1053 mutex_lock(&dev_priv->sb_lock);
23538ef1 1054 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
a580516d 1055 mutex_unlock(&dev_priv->sb_lock);
23538ef1
JN
1056
1057 cur_state = val & DSI_PLL_VCO_EN;
e2c719b7 1058 I915_STATE_WARN(cur_state != state,
23538ef1 1059 "DSI PLL state assertion failure (expected %s, current %s)\n",
87ad3212 1060 onoff(state), onoff(cur_state));
23538ef1 1061}
23538ef1 1062
040484af
JB
1063static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1064 enum pipe pipe, bool state)
1065{
040484af 1066 bool cur_state;
ad80a810
PZ
1067 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1068 pipe);
040484af 1069
2d1fe073 1070 if (HAS_DDI(dev_priv)) {
affa9354 1071 /* DDI does not have a specific FDI_TX register */
649636ef 1072 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
ad80a810 1073 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
bf507ef7 1074 } else {
649636ef 1075 u32 val = I915_READ(FDI_TX_CTL(pipe));
bf507ef7
ED
1076 cur_state = !!(val & FDI_TX_ENABLE);
1077 }
e2c719b7 1078 I915_STATE_WARN(cur_state != state,
040484af 1079 "FDI TX state assertion failure (expected %s, current %s)\n",
87ad3212 1080 onoff(state), onoff(cur_state));
040484af
JB
1081}
1082#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1083#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1084
1085static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1086 enum pipe pipe, bool state)
1087{
040484af
JB
1088 u32 val;
1089 bool cur_state;
1090
649636ef 1091 val = I915_READ(FDI_RX_CTL(pipe));
d63fa0dc 1092 cur_state = !!(val & FDI_RX_ENABLE);
e2c719b7 1093 I915_STATE_WARN(cur_state != state,
040484af 1094 "FDI RX state assertion failure (expected %s, current %s)\n",
87ad3212 1095 onoff(state), onoff(cur_state));
040484af
JB
1096}
1097#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1098#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1099
1100static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1101 enum pipe pipe)
1102{
040484af
JB
1103 u32 val;
1104
1105 /* ILK FDI PLL is always enabled */
7e22dbbb 1106 if (IS_GEN5(dev_priv))
040484af
JB
1107 return;
1108
bf507ef7 1109 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
2d1fe073 1110 if (HAS_DDI(dev_priv))
bf507ef7
ED
1111 return;
1112
649636ef 1113 val = I915_READ(FDI_TX_CTL(pipe));
e2c719b7 1114 I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
040484af
JB
1115}
1116
55607e8a
DV
1117void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1118 enum pipe pipe, bool state)
040484af 1119{
040484af 1120 u32 val;
55607e8a 1121 bool cur_state;
040484af 1122
649636ef 1123 val = I915_READ(FDI_RX_CTL(pipe));
55607e8a 1124 cur_state = !!(val & FDI_RX_PLL_ENABLE);
e2c719b7 1125 I915_STATE_WARN(cur_state != state,
55607e8a 1126 "FDI RX PLL assertion failure (expected %s, current %s)\n",
87ad3212 1127 onoff(state), onoff(cur_state));
040484af
JB
1128}
1129
4f8036a2 1130void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
ea0760cf 1131{
f0f59a00 1132 i915_reg_t pp_reg;
ea0760cf
JB
1133 u32 val;
1134 enum pipe panel_pipe = PIPE_A;
0de3b485 1135 bool locked = true;
ea0760cf 1136
4f8036a2 1137 if (WARN_ON(HAS_DDI(dev_priv)))
bedd4dba
JN
1138 return;
1139
4f8036a2 1140 if (HAS_PCH_SPLIT(dev_priv)) {
bedd4dba
JN
1141 u32 port_sel;
1142
44cb734c
ID
1143 pp_reg = PP_CONTROL(0);
1144 port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
bedd4dba
JN
1145
1146 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1147 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1148 panel_pipe = PIPE_B;
1149 /* XXX: else fix for eDP */
4f8036a2 1150 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
bedd4dba 1151 /* presumably write lock depends on pipe, not port select */
44cb734c 1152 pp_reg = PP_CONTROL(pipe);
bedd4dba 1153 panel_pipe = pipe;
ea0760cf 1154 } else {
44cb734c 1155 pp_reg = PP_CONTROL(0);
bedd4dba
JN
1156 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1157 panel_pipe = PIPE_B;
ea0760cf
JB
1158 }
1159
1160 val = I915_READ(pp_reg);
1161 if (!(val & PANEL_POWER_ON) ||
ec49ba2d 1162 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
ea0760cf
JB
1163 locked = false;
1164
e2c719b7 1165 I915_STATE_WARN(panel_pipe == pipe && locked,
ea0760cf 1166 "panel assertion failure, pipe %c regs locked\n",
9db4a9c7 1167 pipe_name(pipe));
ea0760cf
JB
1168}
1169
93ce0ba6
JN
1170static void assert_cursor(struct drm_i915_private *dev_priv,
1171 enum pipe pipe, bool state)
1172{
93ce0ba6
JN
1173 bool cur_state;
1174
2a307c2e 1175 if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
0b87c24e 1176 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
d9d82081 1177 else
5efb3e28 1178 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
93ce0ba6 1179
e2c719b7 1180 I915_STATE_WARN(cur_state != state,
93ce0ba6 1181 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
87ad3212 1182 pipe_name(pipe), onoff(state), onoff(cur_state));
93ce0ba6
JN
1183}
1184#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1185#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1186
b840d907
JB
1187void assert_pipe(struct drm_i915_private *dev_priv,
1188 enum pipe pipe, bool state)
b24e7179 1189{
63d7bbe9 1190 bool cur_state;
702e7a56
PZ
1191 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1192 pipe);
4feed0eb 1193 enum intel_display_power_domain power_domain;
b24e7179 1194
b6b5d049
VS
1195 /* if we need the pipe quirk it must be always on */
1196 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1197 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
8e636784
DV
1198 state = true;
1199
4feed0eb
ID
1200 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1201 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
649636ef 1202 u32 val = I915_READ(PIPECONF(cpu_transcoder));
69310161 1203 cur_state = !!(val & PIPECONF_ENABLE);
4feed0eb
ID
1204
1205 intel_display_power_put(dev_priv, power_domain);
1206 } else {
1207 cur_state = false;
69310161
PZ
1208 }
1209
e2c719b7 1210 I915_STATE_WARN(cur_state != state,
63d7bbe9 1211 "pipe %c assertion failure (expected %s, current %s)\n",
87ad3212 1212 pipe_name(pipe), onoff(state), onoff(cur_state));
b24e7179
JB
1213}
1214
931872fc
CW
1215static void assert_plane(struct drm_i915_private *dev_priv,
1216 enum plane plane, bool state)
b24e7179 1217{
b24e7179 1218 u32 val;
931872fc 1219 bool cur_state;
b24e7179 1220
649636ef 1221 val = I915_READ(DSPCNTR(plane));
931872fc 1222 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
e2c719b7 1223 I915_STATE_WARN(cur_state != state,
931872fc 1224 "plane %c assertion failure (expected %s, current %s)\n",
87ad3212 1225 plane_name(plane), onoff(state), onoff(cur_state));
b24e7179
JB
1226}
1227
931872fc
CW
1228#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1229#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1230
b24e7179
JB
1231static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1232 enum pipe pipe)
1233{
649636ef 1234 int i;
b24e7179 1235
653e1026 1236 /* Primary planes are fixed to pipes on gen4+ */
6315b5d3 1237 if (INTEL_GEN(dev_priv) >= 4) {
649636ef 1238 u32 val = I915_READ(DSPCNTR(pipe));
e2c719b7 1239 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
28c05794
AJ
1240 "plane %c assertion failure, should be disabled but not\n",
1241 plane_name(pipe));
19ec1358 1242 return;
28c05794 1243 }
19ec1358 1244
b24e7179 1245 /* Need to check both planes against the pipe */
055e393f 1246 for_each_pipe(dev_priv, i) {
649636ef
VS
1247 u32 val = I915_READ(DSPCNTR(i));
1248 enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
b24e7179 1249 DISPPLANE_SEL_PIPE_SHIFT;
e2c719b7 1250 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
9db4a9c7
JB
1251 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1252 plane_name(i), pipe_name(pipe));
b24e7179
JB
1253 }
1254}
1255
19332d7a
JB
1256static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1257 enum pipe pipe)
1258{
649636ef 1259 int sprite;
19332d7a 1260
6315b5d3 1261 if (INTEL_GEN(dev_priv) >= 9) {
3bdcfc0c 1262 for_each_sprite(dev_priv, pipe, sprite) {
649636ef 1263 u32 val = I915_READ(PLANE_CTL(pipe, sprite));
e2c719b7 1264 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
7feb8b88
DL
1265 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1266 sprite, pipe_name(pipe));
1267 }
920a14b2 1268 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
3bdcfc0c 1269 for_each_sprite(dev_priv, pipe, sprite) {
83c04a62 1270 u32 val = I915_READ(SPCNTR(pipe, PLANE_SPRITE0 + sprite));
e2c719b7 1271 I915_STATE_WARN(val & SP_ENABLE,
20674eef 1272 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1fe47785 1273 sprite_name(pipe, sprite), pipe_name(pipe));
20674eef 1274 }
6315b5d3 1275 } else if (INTEL_GEN(dev_priv) >= 7) {
649636ef 1276 u32 val = I915_READ(SPRCTL(pipe));
e2c719b7 1277 I915_STATE_WARN(val & SPRITE_ENABLE,
06da8da2 1278 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef 1279 plane_name(pipe), pipe_name(pipe));
6315b5d3 1280 } else if (INTEL_GEN(dev_priv) >= 5) {
649636ef 1281 u32 val = I915_READ(DVSCNTR(pipe));
e2c719b7 1282 I915_STATE_WARN(val & DVS_ENABLE,
06da8da2 1283 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef 1284 plane_name(pipe), pipe_name(pipe));
19332d7a
JB
1285 }
1286}
1287
08c71e5e
VS
1288static void assert_vblank_disabled(struct drm_crtc *crtc)
1289{
e2c719b7 1290 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
08c71e5e
VS
1291 drm_crtc_vblank_put(crtc);
1292}
1293
7abd4b35
ACO
1294void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1295 enum pipe pipe)
92f2584a 1296{
92f2584a
JB
1297 u32 val;
1298 bool enabled;
1299
649636ef 1300 val = I915_READ(PCH_TRANSCONF(pipe));
92f2584a 1301 enabled = !!(val & TRANS_ENABLE);
e2c719b7 1302 I915_STATE_WARN(enabled,
9db4a9c7
JB
1303 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1304 pipe_name(pipe));
92f2584a
JB
1305}
1306
4e634389
KP
1307static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1308 enum pipe pipe, u32 port_sel, u32 val)
f0575e92
KP
1309{
1310 if ((val & DP_PORT_EN) == 0)
1311 return false;
1312
2d1fe073 1313 if (HAS_PCH_CPT(dev_priv)) {
f0f59a00 1314 u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
f0575e92
KP
1315 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1316 return false;
2d1fe073 1317 } else if (IS_CHERRYVIEW(dev_priv)) {
44f37d1f
CML
1318 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1319 return false;
f0575e92
KP
1320 } else {
1321 if ((val & DP_PIPE_MASK) != (pipe << 30))
1322 return false;
1323 }
1324 return true;
1325}
1326
1519b995
KP
1327static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1328 enum pipe pipe, u32 val)
1329{
dc0fa718 1330 if ((val & SDVO_ENABLE) == 0)
1519b995
KP
1331 return false;
1332
2d1fe073 1333 if (HAS_PCH_CPT(dev_priv)) {
dc0fa718 1334 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1519b995 1335 return false;
2d1fe073 1336 } else if (IS_CHERRYVIEW(dev_priv)) {
44f37d1f
CML
1337 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1338 return false;
1519b995 1339 } else {
dc0fa718 1340 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1519b995
KP
1341 return false;
1342 }
1343 return true;
1344}
1345
1346static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1347 enum pipe pipe, u32 val)
1348{
1349 if ((val & LVDS_PORT_EN) == 0)
1350 return false;
1351
2d1fe073 1352 if (HAS_PCH_CPT(dev_priv)) {
1519b995
KP
1353 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1354 return false;
1355 } else {
1356 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1357 return false;
1358 }
1359 return true;
1360}
1361
1362static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1363 enum pipe pipe, u32 val)
1364{
1365 if ((val & ADPA_DAC_ENABLE) == 0)
1366 return false;
2d1fe073 1367 if (HAS_PCH_CPT(dev_priv)) {
1519b995
KP
1368 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1369 return false;
1370 } else {
1371 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1372 return false;
1373 }
1374 return true;
1375}
1376
291906f1 1377static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
f0f59a00
VS
1378 enum pipe pipe, i915_reg_t reg,
1379 u32 port_sel)
291906f1 1380{
47a05eca 1381 u32 val = I915_READ(reg);
e2c719b7 1382 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
291906f1 1383 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
f0f59a00 1384 i915_mmio_reg_offset(reg), pipe_name(pipe));
de9a35ab 1385
2d1fe073 1386 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
75c5da27 1387 && (val & DP_PIPEB_SELECT),
de9a35ab 1388 "IBX PCH dp port still using transcoder B\n");
291906f1
JB
1389}
1390
1391static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
f0f59a00 1392 enum pipe pipe, i915_reg_t reg)
291906f1 1393{
47a05eca 1394 u32 val = I915_READ(reg);
e2c719b7 1395 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
23c99e77 1396 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
f0f59a00 1397 i915_mmio_reg_offset(reg), pipe_name(pipe));
de9a35ab 1398
2d1fe073 1399 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
75c5da27 1400 && (val & SDVO_PIPE_B_SELECT),
de9a35ab 1401 "IBX PCH hdmi port still using transcoder B\n");
291906f1
JB
1402}
1403
1404static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1405 enum pipe pipe)
1406{
291906f1 1407 u32 val;
291906f1 1408
f0575e92
KP
1409 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1410 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1411 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
291906f1 1412
649636ef 1413 val = I915_READ(PCH_ADPA);
e2c719b7 1414 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
291906f1 1415 "PCH VGA enabled on transcoder %c, should be disabled\n",
9db4a9c7 1416 pipe_name(pipe));
291906f1 1417
649636ef 1418 val = I915_READ(PCH_LVDS);
e2c719b7 1419 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
291906f1 1420 "PCH LVDS enabled on transcoder %c, should be disabled\n",
9db4a9c7 1421 pipe_name(pipe));
291906f1 1422
e2debe91
PZ
1423 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1424 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1425 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
291906f1
JB
1426}
1427
cd2d34d9
VS
1428static void _vlv_enable_pll(struct intel_crtc *crtc,
1429 const struct intel_crtc_state *pipe_config)
1430{
1431 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1432 enum pipe pipe = crtc->pipe;
1433
1434 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1435 POSTING_READ(DPLL(pipe));
1436 udelay(150);
1437
2c30b43b
CW
1438 if (intel_wait_for_register(dev_priv,
1439 DPLL(pipe),
1440 DPLL_LOCK_VLV,
1441 DPLL_LOCK_VLV,
1442 1))
cd2d34d9
VS
1443 DRM_ERROR("DPLL %d failed to lock\n", pipe);
1444}
1445
d288f65f 1446static void vlv_enable_pll(struct intel_crtc *crtc,
5cec258b 1447 const struct intel_crtc_state *pipe_config)
87442f73 1448{
cd2d34d9 1449 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8bd3f301 1450 enum pipe pipe = crtc->pipe;
87442f73 1451
8bd3f301 1452 assert_pipe_disabled(dev_priv, pipe);
87442f73 1453
87442f73 1454 /* PLL is protected by panel, make sure we can write it */
7d1a83cb 1455 assert_panel_unlocked(dev_priv, pipe);
87442f73 1456
cd2d34d9
VS
1457 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1458 _vlv_enable_pll(crtc, pipe_config);
426115cf 1459
8bd3f301
VS
1460 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1461 POSTING_READ(DPLL_MD(pipe));
87442f73
DV
1462}
1463
cd2d34d9
VS
1464
1465static void _chv_enable_pll(struct intel_crtc *crtc,
1466 const struct intel_crtc_state *pipe_config)
9d556c99 1467{
cd2d34d9 1468 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8bd3f301 1469 enum pipe pipe = crtc->pipe;
9d556c99 1470 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9d556c99
CML
1471 u32 tmp;
1472
a580516d 1473 mutex_lock(&dev_priv->sb_lock);
9d556c99
CML
1474
1475 /* Enable back the 10bit clock to display controller */
1476 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1477 tmp |= DPIO_DCLKP_EN;
1478 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1479
54433e91
VS
1480 mutex_unlock(&dev_priv->sb_lock);
1481
9d556c99
CML
1482 /*
1483 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1484 */
1485 udelay(1);
1486
1487 /* Enable PLL */
d288f65f 1488 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
9d556c99
CML
1489
1490 /* Check PLL is locked */
6b18826a
CW
1491 if (intel_wait_for_register(dev_priv,
1492 DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
1493 1))
9d556c99 1494 DRM_ERROR("PLL %d failed to lock\n", pipe);
cd2d34d9
VS
1495}
1496
1497static void chv_enable_pll(struct intel_crtc *crtc,
1498 const struct intel_crtc_state *pipe_config)
1499{
1500 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1501 enum pipe pipe = crtc->pipe;
1502
1503 assert_pipe_disabled(dev_priv, pipe);
1504
1505 /* PLL is protected by panel, make sure we can write it */
1506 assert_panel_unlocked(dev_priv, pipe);
1507
1508 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1509 _chv_enable_pll(crtc, pipe_config);
9d556c99 1510
c231775c
VS
1511 if (pipe != PIPE_A) {
1512 /*
1513 * WaPixelRepeatModeFixForC0:chv
1514 *
1515 * DPLLCMD is AWOL. Use chicken bits to propagate
1516 * the value from DPLLBMD to either pipe B or C.
1517 */
1518 I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
1519 I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
1520 I915_WRITE(CBR4_VLV, 0);
1521 dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
1522
1523 /*
1524 * DPLLB VGA mode also seems to cause problems.
1525 * We should always have it disabled.
1526 */
1527 WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
1528 } else {
1529 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1530 POSTING_READ(DPLL_MD(pipe));
1531 }
9d556c99
CML
1532}
1533
6315b5d3 1534static int intel_num_dvo_pipes(struct drm_i915_private *dev_priv)
1c4e0274
VS
1535{
1536 struct intel_crtc *crtc;
1537 int count = 0;
1538
6315b5d3 1539 for_each_intel_crtc(&dev_priv->drm, crtc) {
3538b9df 1540 count += crtc->base.state->active &&
2d84d2b3
VS
1541 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
1542 }
1c4e0274
VS
1543
1544 return count;
1545}
1546
66e3d5c0 1547static void i9xx_enable_pll(struct intel_crtc *crtc)
63d7bbe9 1548{
6315b5d3 1549 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
f0f59a00 1550 i915_reg_t reg = DPLL(crtc->pipe);
6e3c9717 1551 u32 dpll = crtc->config->dpll_hw_state.dpll;
63d7bbe9 1552
66e3d5c0 1553 assert_pipe_disabled(dev_priv, crtc->pipe);
58c6eaa2 1554
63d7bbe9 1555 /* PLL is protected by panel, make sure we can write it */
50a0bc90 1556 if (IS_MOBILE(dev_priv) && !IS_I830(dev_priv))
66e3d5c0 1557 assert_panel_unlocked(dev_priv, crtc->pipe);
63d7bbe9 1558
1c4e0274 1559 /* Enable DVO 2x clock on both PLLs if necessary */
6315b5d3 1560 if (IS_I830(dev_priv) && intel_num_dvo_pipes(dev_priv) > 0) {
1c4e0274
VS
1561 /*
1562 * It appears to be important that we don't enable this
1563 * for the current pipe before otherwise configuring the
1564 * PLL. No idea how this should be handled if multiple
1565 * DVO outputs are enabled simultaneosly.
1566 */
1567 dpll |= DPLL_DVO_2X_MODE;
1568 I915_WRITE(DPLL(!crtc->pipe),
1569 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1570 }
66e3d5c0 1571
c2b63374
VS
1572 /*
1573 * Apparently we need to have VGA mode enabled prior to changing
1574 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1575 * dividers, even though the register value does change.
1576 */
1577 I915_WRITE(reg, 0);
1578
8e7a65aa
VS
1579 I915_WRITE(reg, dpll);
1580
66e3d5c0
DV
1581 /* Wait for the clocks to stabilize. */
1582 POSTING_READ(reg);
1583 udelay(150);
1584
6315b5d3 1585 if (INTEL_GEN(dev_priv) >= 4) {
66e3d5c0 1586 I915_WRITE(DPLL_MD(crtc->pipe),
6e3c9717 1587 crtc->config->dpll_hw_state.dpll_md);
66e3d5c0
DV
1588 } else {
1589 /* The pixel multiplier can only be updated once the
1590 * DPLL is enabled and the clocks are stable.
1591 *
1592 * So write it again.
1593 */
1594 I915_WRITE(reg, dpll);
1595 }
63d7bbe9
JB
1596
1597 /* We do this three times for luck */
66e3d5c0 1598 I915_WRITE(reg, dpll);
63d7bbe9
JB
1599 POSTING_READ(reg);
1600 udelay(150); /* wait for warmup */
66e3d5c0 1601 I915_WRITE(reg, dpll);
63d7bbe9
JB
1602 POSTING_READ(reg);
1603 udelay(150); /* wait for warmup */
66e3d5c0 1604 I915_WRITE(reg, dpll);
63d7bbe9
JB
1605 POSTING_READ(reg);
1606 udelay(150); /* wait for warmup */
1607}
1608
1609/**
50b44a44 1610 * i9xx_disable_pll - disable a PLL
63d7bbe9
JB
1611 * @dev_priv: i915 private structure
1612 * @pipe: pipe PLL to disable
1613 *
1614 * Disable the PLL for @pipe, making sure the pipe is off first.
1615 *
1616 * Note! This is for pre-ILK only.
1617 */
1c4e0274 1618static void i9xx_disable_pll(struct intel_crtc *crtc)
63d7bbe9 1619{
6315b5d3 1620 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1c4e0274
VS
1621 enum pipe pipe = crtc->pipe;
1622
1623 /* Disable DVO 2x clock on both PLLs if necessary */
50a0bc90 1624 if (IS_I830(dev_priv) &&
2d84d2b3 1625 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
6315b5d3 1626 !intel_num_dvo_pipes(dev_priv)) {
1c4e0274
VS
1627 I915_WRITE(DPLL(PIPE_B),
1628 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1629 I915_WRITE(DPLL(PIPE_A),
1630 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1631 }
1632
b6b5d049
VS
1633 /* Don't disable pipe or pipe PLLs if needed */
1634 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1635 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
63d7bbe9
JB
1636 return;
1637
1638 /* Make sure the pipe isn't still relying on us */
1639 assert_pipe_disabled(dev_priv, pipe);
1640
b8afb911 1641 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
50b44a44 1642 POSTING_READ(DPLL(pipe));
63d7bbe9
JB
1643}
1644
f6071166
JB
1645static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1646{
b8afb911 1647 u32 val;
f6071166
JB
1648
1649 /* Make sure the pipe isn't still relying on us */
1650 assert_pipe_disabled(dev_priv, pipe);
1651
03ed5cbf
VS
1652 val = DPLL_INTEGRATED_REF_CLK_VLV |
1653 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1654 if (pipe != PIPE_A)
1655 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1656
f6071166
JB
1657 I915_WRITE(DPLL(pipe), val);
1658 POSTING_READ(DPLL(pipe));
076ed3b2
CML
1659}
1660
1661static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1662{
d752048d 1663 enum dpio_channel port = vlv_pipe_to_channel(pipe);
076ed3b2
CML
1664 u32 val;
1665
a11b0703
VS
1666 /* Make sure the pipe isn't still relying on us */
1667 assert_pipe_disabled(dev_priv, pipe);
076ed3b2 1668
60bfe44f
VS
1669 val = DPLL_SSC_REF_CLK_CHV |
1670 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
a11b0703
VS
1671 if (pipe != PIPE_A)
1672 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
03ed5cbf 1673
a11b0703
VS
1674 I915_WRITE(DPLL(pipe), val);
1675 POSTING_READ(DPLL(pipe));
d752048d 1676
a580516d 1677 mutex_lock(&dev_priv->sb_lock);
d752048d
VS
1678
1679 /* Disable 10bit clock to display controller */
1680 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1681 val &= ~DPIO_DCLKP_EN;
1682 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1683
a580516d 1684 mutex_unlock(&dev_priv->sb_lock);
f6071166
JB
1685}
1686
e4607fcf 1687void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
9b6de0a1
VS
1688 struct intel_digital_port *dport,
1689 unsigned int expected_mask)
89b667f8
JB
1690{
1691 u32 port_mask;
f0f59a00 1692 i915_reg_t dpll_reg;
89b667f8 1693
e4607fcf
CML
1694 switch (dport->port) {
1695 case PORT_B:
89b667f8 1696 port_mask = DPLL_PORTB_READY_MASK;
00fc31b7 1697 dpll_reg = DPLL(0);
e4607fcf
CML
1698 break;
1699 case PORT_C:
89b667f8 1700 port_mask = DPLL_PORTC_READY_MASK;
00fc31b7 1701 dpll_reg = DPLL(0);
9b6de0a1 1702 expected_mask <<= 4;
00fc31b7
CML
1703 break;
1704 case PORT_D:
1705 port_mask = DPLL_PORTD_READY_MASK;
1706 dpll_reg = DPIO_PHY_STATUS;
e4607fcf
CML
1707 break;
1708 default:
1709 BUG();
1710 }
89b667f8 1711
370004d3
CW
1712 if (intel_wait_for_register(dev_priv,
1713 dpll_reg, port_mask, expected_mask,
1714 1000))
9b6de0a1
VS
1715 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1716 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
89b667f8
JB
1717}
1718
b8a4f404
PZ
1719static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1720 enum pipe pipe)
040484af 1721{
98187836
VS
1722 struct intel_crtc *intel_crtc = intel_get_crtc_for_pipe(dev_priv,
1723 pipe);
f0f59a00
VS
1724 i915_reg_t reg;
1725 uint32_t val, pipeconf_val;
040484af 1726
040484af 1727 /* Make sure PCH DPLL is enabled */
8106ddbd 1728 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
040484af
JB
1729
1730 /* FDI must be feeding us bits for PCH ports */
1731 assert_fdi_tx_enabled(dev_priv, pipe);
1732 assert_fdi_rx_enabled(dev_priv, pipe);
1733
6e266956 1734 if (HAS_PCH_CPT(dev_priv)) {
23670b32
DV
1735 /* Workaround: Set the timing override bit before enabling the
1736 * pch transcoder. */
1737 reg = TRANS_CHICKEN2(pipe);
1738 val = I915_READ(reg);
1739 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1740 I915_WRITE(reg, val);
59c859d6 1741 }
23670b32 1742
ab9412ba 1743 reg = PCH_TRANSCONF(pipe);
040484af 1744 val = I915_READ(reg);
5f7f726d 1745 pipeconf_val = I915_READ(PIPECONF(pipe));
e9bcff5c 1746
2d1fe073 1747 if (HAS_PCH_IBX(dev_priv)) {
e9bcff5c 1748 /*
c5de7c6f
VS
1749 * Make the BPC in transcoder be consistent with
1750 * that in pipeconf reg. For HDMI we must use 8bpc
1751 * here for both 8bpc and 12bpc.
e9bcff5c 1752 */
dfd07d72 1753 val &= ~PIPECONF_BPC_MASK;
2d84d2b3 1754 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
c5de7c6f
VS
1755 val |= PIPECONF_8BPC;
1756 else
1757 val |= pipeconf_val & PIPECONF_BPC_MASK;
e9bcff5c 1758 }
5f7f726d
PZ
1759
1760 val &= ~TRANS_INTERLACE_MASK;
1761 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
2d1fe073 1762 if (HAS_PCH_IBX(dev_priv) &&
2d84d2b3 1763 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
7c26e5c6
PZ
1764 val |= TRANS_LEGACY_INTERLACED_ILK;
1765 else
1766 val |= TRANS_INTERLACED;
5f7f726d
PZ
1767 else
1768 val |= TRANS_PROGRESSIVE;
1769
040484af 1770 I915_WRITE(reg, val | TRANS_ENABLE);
650fbd84
CW
1771 if (intel_wait_for_register(dev_priv,
1772 reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
1773 100))
4bb6f1f3 1774 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
040484af
JB
1775}
1776
8fb033d7 1777static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
937bb610 1778 enum transcoder cpu_transcoder)
040484af 1779{
8fb033d7 1780 u32 val, pipeconf_val;
8fb033d7 1781
8fb033d7 1782 /* FDI must be feeding us bits for PCH ports */
1a240d4d 1783 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
937bb610 1784 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
8fb033d7 1785
223a6fdf 1786 /* Workaround: set timing override bit. */
36c0d0cf 1787 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
23670b32 1788 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
36c0d0cf 1789 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
223a6fdf 1790
25f3ef11 1791 val = TRANS_ENABLE;
937bb610 1792 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
8fb033d7 1793
9a76b1c6
PZ
1794 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1795 PIPECONF_INTERLACED_ILK)
a35f2679 1796 val |= TRANS_INTERLACED;
8fb033d7
PZ
1797 else
1798 val |= TRANS_PROGRESSIVE;
1799
ab9412ba 1800 I915_WRITE(LPT_TRANSCONF, val);
d9f96244
CW
1801 if (intel_wait_for_register(dev_priv,
1802 LPT_TRANSCONF,
1803 TRANS_STATE_ENABLE,
1804 TRANS_STATE_ENABLE,
1805 100))
937bb610 1806 DRM_ERROR("Failed to enable PCH transcoder\n");
8fb033d7
PZ
1807}
1808
b8a4f404
PZ
1809static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1810 enum pipe pipe)
040484af 1811{
f0f59a00
VS
1812 i915_reg_t reg;
1813 uint32_t val;
040484af
JB
1814
1815 /* FDI relies on the transcoder */
1816 assert_fdi_tx_disabled(dev_priv, pipe);
1817 assert_fdi_rx_disabled(dev_priv, pipe);
1818
291906f1
JB
1819 /* Ports must be off as well */
1820 assert_pch_ports_disabled(dev_priv, pipe);
1821
ab9412ba 1822 reg = PCH_TRANSCONF(pipe);
040484af
JB
1823 val = I915_READ(reg);
1824 val &= ~TRANS_ENABLE;
1825 I915_WRITE(reg, val);
1826 /* wait for PCH transcoder off, transcoder state */
a7d04662
CW
1827 if (intel_wait_for_register(dev_priv,
1828 reg, TRANS_STATE_ENABLE, 0,
1829 50))
4bb6f1f3 1830 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
23670b32 1831
6e266956 1832 if (HAS_PCH_CPT(dev_priv)) {
23670b32
DV
1833 /* Workaround: Clear the timing override chicken bit again. */
1834 reg = TRANS_CHICKEN2(pipe);
1835 val = I915_READ(reg);
1836 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1837 I915_WRITE(reg, val);
1838 }
040484af
JB
1839}
1840
b7076546 1841void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
8fb033d7 1842{
8fb033d7
PZ
1843 u32 val;
1844
ab9412ba 1845 val = I915_READ(LPT_TRANSCONF);
8fb033d7 1846 val &= ~TRANS_ENABLE;
ab9412ba 1847 I915_WRITE(LPT_TRANSCONF, val);
8fb033d7 1848 /* wait for PCH transcoder off, transcoder state */
dfdb4749
CW
1849 if (intel_wait_for_register(dev_priv,
1850 LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
1851 50))
8a52fd9f 1852 DRM_ERROR("Failed to disable PCH transcoder\n");
223a6fdf
PZ
1853
1854 /* Workaround: clear timing override bit. */
36c0d0cf 1855 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
23670b32 1856 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
36c0d0cf 1857 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
040484af
JB
1858}
1859
65f2130c
VS
1860enum transcoder intel_crtc_pch_transcoder(struct intel_crtc *crtc)
1861{
1862 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1863
1864 WARN_ON(!crtc->config->has_pch_encoder);
1865
1866 if (HAS_PCH_LPT(dev_priv))
1867 return TRANSCODER_A;
1868 else
1869 return (enum transcoder) crtc->pipe;
1870}
1871
b24e7179 1872/**
309cfea8 1873 * intel_enable_pipe - enable a pipe, asserting requirements
0372264a 1874 * @crtc: crtc responsible for the pipe
b24e7179 1875 *
0372264a 1876 * Enable @crtc's pipe, making sure that various hardware specific requirements
b24e7179 1877 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
b24e7179 1878 */
e1fdc473 1879static void intel_enable_pipe(struct intel_crtc *crtc)
b24e7179 1880{
0372264a 1881 struct drm_device *dev = crtc->base.dev;
fac5e23e 1882 struct drm_i915_private *dev_priv = to_i915(dev);
0372264a 1883 enum pipe pipe = crtc->pipe;
1a70a728 1884 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
f0f59a00 1885 i915_reg_t reg;
b24e7179
JB
1886 u32 val;
1887
9e2ee2dd
VS
1888 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1889
58c6eaa2 1890 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 1891 assert_cursor_disabled(dev_priv, pipe);
58c6eaa2
DV
1892 assert_sprites_disabled(dev_priv, pipe);
1893
b24e7179
JB
1894 /*
1895 * A pipe without a PLL won't actually be able to drive bits from
1896 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1897 * need the check.
1898 */
09fa8bb9 1899 if (HAS_GMCH_DISPLAY(dev_priv)) {
d7edc4e5 1900 if (intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DSI))
23538ef1
JN
1901 assert_dsi_pll_enabled(dev_priv);
1902 else
1903 assert_pll_enabled(dev_priv, pipe);
09fa8bb9 1904 } else {
6e3c9717 1905 if (crtc->config->has_pch_encoder) {
040484af 1906 /* if driving the PCH, we need FDI enabled */
65f2130c
VS
1907 assert_fdi_rx_pll_enabled(dev_priv,
1908 (enum pipe) intel_crtc_pch_transcoder(crtc));
1a240d4d
DV
1909 assert_fdi_tx_pll_enabled(dev_priv,
1910 (enum pipe) cpu_transcoder);
040484af
JB
1911 }
1912 /* FIXME: assert CPU port conditions for SNB+ */
1913 }
b24e7179 1914
702e7a56 1915 reg = PIPECONF(cpu_transcoder);
b24e7179 1916 val = I915_READ(reg);
7ad25d48 1917 if (val & PIPECONF_ENABLE) {
b6b5d049
VS
1918 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1919 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
00d70b15 1920 return;
7ad25d48 1921 }
00d70b15
CW
1922
1923 I915_WRITE(reg, val | PIPECONF_ENABLE);
851855d8 1924 POSTING_READ(reg);
b7792d8b
VS
1925
1926 /*
1927 * Until the pipe starts DSL will read as 0, which would cause
1928 * an apparent vblank timestamp jump, which messes up also the
1929 * frame count when it's derived from the timestamps. So let's
1930 * wait for the pipe to start properly before we call
1931 * drm_crtc_vblank_on()
1932 */
1933 if (dev->max_vblank_count == 0 &&
1934 wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
1935 DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
b24e7179
JB
1936}
1937
1938/**
309cfea8 1939 * intel_disable_pipe - disable a pipe, asserting requirements
575f7ab7 1940 * @crtc: crtc whose pipes is to be disabled
b24e7179 1941 *
575f7ab7
VS
1942 * Disable the pipe of @crtc, making sure that various hardware
1943 * specific requirements are met, if applicable, e.g. plane
1944 * disabled, panel fitter off, etc.
b24e7179
JB
1945 *
1946 * Will wait until the pipe has shut down before returning.
1947 */
575f7ab7 1948static void intel_disable_pipe(struct intel_crtc *crtc)
b24e7179 1949{
fac5e23e 1950 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6e3c9717 1951 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 1952 enum pipe pipe = crtc->pipe;
f0f59a00 1953 i915_reg_t reg;
b24e7179
JB
1954 u32 val;
1955
9e2ee2dd
VS
1956 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
1957
b24e7179
JB
1958 /*
1959 * Make sure planes won't keep trying to pump pixels to us,
1960 * or we might hang the display.
1961 */
1962 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 1963 assert_cursor_disabled(dev_priv, pipe);
19332d7a 1964 assert_sprites_disabled(dev_priv, pipe);
b24e7179 1965
702e7a56 1966 reg = PIPECONF(cpu_transcoder);
b24e7179 1967 val = I915_READ(reg);
00d70b15
CW
1968 if ((val & PIPECONF_ENABLE) == 0)
1969 return;
1970
67adc644
VS
1971 /*
1972 * Double wide has implications for planes
1973 * so best keep it disabled when not needed.
1974 */
6e3c9717 1975 if (crtc->config->double_wide)
67adc644
VS
1976 val &= ~PIPECONF_DOUBLE_WIDE;
1977
1978 /* Don't disable pipe or pipe PLLs if needed */
b6b5d049
VS
1979 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
1980 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
67adc644
VS
1981 val &= ~PIPECONF_ENABLE;
1982
1983 I915_WRITE(reg, val);
1984 if ((val & PIPECONF_ENABLE) == 0)
1985 intel_wait_for_pipe_off(crtc);
b24e7179
JB
1986}
1987
832be82f
VS
1988static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
1989{
1990 return IS_GEN2(dev_priv) ? 2048 : 4096;
1991}
1992
27ba3910
VS
1993static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
1994 uint64_t fb_modifier, unsigned int cpp)
7b49f948
VS
1995{
1996 switch (fb_modifier) {
1997 case DRM_FORMAT_MOD_NONE:
1998 return cpp;
1999 case I915_FORMAT_MOD_X_TILED:
2000 if (IS_GEN2(dev_priv))
2001 return 128;
2002 else
2003 return 512;
2004 case I915_FORMAT_MOD_Y_TILED:
2005 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2006 return 128;
2007 else
2008 return 512;
2009 case I915_FORMAT_MOD_Yf_TILED:
2010 switch (cpp) {
2011 case 1:
2012 return 64;
2013 case 2:
2014 case 4:
2015 return 128;
2016 case 8:
2017 case 16:
2018 return 256;
2019 default:
2020 MISSING_CASE(cpp);
2021 return cpp;
2022 }
2023 break;
2024 default:
2025 MISSING_CASE(fb_modifier);
2026 return cpp;
2027 }
2028}
2029
832be82f
VS
2030unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
2031 uint64_t fb_modifier, unsigned int cpp)
a57ce0b2 2032{
832be82f
VS
2033 if (fb_modifier == DRM_FORMAT_MOD_NONE)
2034 return 1;
2035 else
2036 return intel_tile_size(dev_priv) /
27ba3910 2037 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
6761dd31
TU
2038}
2039
8d0deca8
VS
2040/* Return the tile dimensions in pixel units */
2041static void intel_tile_dims(const struct drm_i915_private *dev_priv,
2042 unsigned int *tile_width,
2043 unsigned int *tile_height,
2044 uint64_t fb_modifier,
2045 unsigned int cpp)
2046{
2047 unsigned int tile_width_bytes =
2048 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
2049
2050 *tile_width = tile_width_bytes / cpp;
2051 *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
2052}
2053
6761dd31 2054unsigned int
24dbf51a
CW
2055intel_fb_align_height(struct drm_i915_private *dev_priv,
2056 unsigned int height,
2057 uint32_t pixel_format,
2058 uint64_t fb_modifier)
6761dd31 2059{
832be82f 2060 unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
24dbf51a 2061 unsigned int tile_height = intel_tile_height(dev_priv, fb_modifier, cpp);
832be82f
VS
2062
2063 return ALIGN(height, tile_height);
a57ce0b2
JB
2064}
2065
1663b9d6
VS
2066unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2067{
2068 unsigned int size = 0;
2069 int i;
2070
2071 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2072 size += rot_info->plane[i].width * rot_info->plane[i].height;
2073
2074 return size;
2075}
2076
75c82a53 2077static void
3465c580
VS
2078intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2079 const struct drm_framebuffer *fb,
2080 unsigned int rotation)
f64b98cd 2081{
7b92c047 2082 view->type = I915_GGTT_VIEW_NORMAL;
bd2ef25d 2083 if (drm_rotation_90_or_270(rotation)) {
7b92c047 2084 view->type = I915_GGTT_VIEW_ROTATED;
8bab1193 2085 view->rotated = to_intel_framebuffer(fb)->rot_info;
2d7a215f
VS
2086 }
2087}
50470bb0 2088
603525d7 2089static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
4e9a86b6
VS
2090{
2091 if (INTEL_INFO(dev_priv)->gen >= 9)
2092 return 256 * 1024;
c0f86832 2093 else if (IS_I965G(dev_priv) || IS_I965GM(dev_priv) ||
666a4537 2094 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
4e9a86b6
VS
2095 return 128 * 1024;
2096 else if (INTEL_INFO(dev_priv)->gen >= 4)
2097 return 4 * 1024;
2098 else
44c5905e 2099 return 0;
4e9a86b6
VS
2100}
2101
603525d7
VS
2102static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
2103 uint64_t fb_modifier)
2104{
2105 switch (fb_modifier) {
2106 case DRM_FORMAT_MOD_NONE:
2107 return intel_linear_alignment(dev_priv);
2108 case I915_FORMAT_MOD_X_TILED:
2109 if (INTEL_INFO(dev_priv)->gen >= 9)
2110 return 256 * 1024;
2111 return 0;
2112 case I915_FORMAT_MOD_Y_TILED:
2113 case I915_FORMAT_MOD_Yf_TILED:
2114 return 1 * 1024 * 1024;
2115 default:
2116 MISSING_CASE(fb_modifier);
2117 return 0;
2118 }
2119}
2120
058d88c4
CW
2121struct i915_vma *
2122intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
6b95a207 2123{
850c4cdc 2124 struct drm_device *dev = fb->dev;
fac5e23e 2125 struct drm_i915_private *dev_priv = to_i915(dev);
850c4cdc 2126 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2127 struct i915_ggtt_view view;
058d88c4 2128 struct i915_vma *vma;
6b95a207 2129 u32 alignment;
6b95a207 2130
ebcdd39e
MR
2131 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2132
bae781b2 2133 alignment = intel_surf_alignment(dev_priv, fb->modifier);
6b95a207 2134
3465c580 2135 intel_fill_fb_ggtt_view(&view, fb, rotation);
f64b98cd 2136
693db184
CW
2137 /* Note that the w/a also requires 64 PTE of padding following the
2138 * bo. We currently fill all unused PTE with the shadow page and so
2139 * we should always have valid PTE following the scanout preventing
2140 * the VT-d warning.
2141 */
48f112fe 2142 if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
693db184
CW
2143 alignment = 256 * 1024;
2144
d6dd6843
PZ
2145 /*
2146 * Global gtt pte registers are special registers which actually forward
2147 * writes to a chunk of system memory. Which means that there is no risk
2148 * that the register values disappear as soon as we call
2149 * intel_runtime_pm_put(), so it is correct to wrap only the
2150 * pin/unpin/fence and not more.
2151 */
2152 intel_runtime_pm_get(dev_priv);
2153
058d88c4 2154 vma = i915_gem_object_pin_to_display_plane(obj, alignment, &view);
49ef5294
CW
2155 if (IS_ERR(vma))
2156 goto err;
6b95a207 2157
05a20d09 2158 if (i915_vma_is_map_and_fenceable(vma)) {
49ef5294
CW
2159 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2160 * fence, whereas 965+ only requires a fence if using
2161 * framebuffer compression. For simplicity, we always, when
2162 * possible, install a fence as the cost is not that onerous.
2163 *
2164 * If we fail to fence the tiled scanout, then either the
2165 * modeset will reject the change (which is highly unlikely as
2166 * the affected systems, all but one, do not have unmappable
2167 * space) or we will not be able to enable full powersaving
2168 * techniques (also likely not to apply due to various limits
2169 * FBC and the like impose on the size of the buffer, which
2170 * presumably we violated anyway with this unmappable buffer).
2171 * Anyway, it is presumably better to stumble onwards with
2172 * something and try to run the system in a "less than optimal"
2173 * mode that matches the user configuration.
2174 */
2175 if (i915_vma_get_fence(vma) == 0)
2176 i915_vma_pin_fence(vma);
9807216f 2177 }
6b95a207 2178
be1e3415 2179 i915_vma_get(vma);
49ef5294 2180err:
d6dd6843 2181 intel_runtime_pm_put(dev_priv);
058d88c4 2182 return vma;
6b95a207
KH
2183}
2184
be1e3415 2185void intel_unpin_fb_vma(struct i915_vma *vma)
1690e1eb 2186{
be1e3415 2187 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
f64b98cd 2188
49ef5294 2189 i915_vma_unpin_fence(vma);
058d88c4 2190 i915_gem_object_unpin_from_display_plane(vma);
be1e3415 2191 i915_vma_put(vma);
1690e1eb
CW
2192}
2193
ef78ec94
VS
2194static int intel_fb_pitch(const struct drm_framebuffer *fb, int plane,
2195 unsigned int rotation)
2196{
bd2ef25d 2197 if (drm_rotation_90_or_270(rotation))
ef78ec94
VS
2198 return to_intel_framebuffer(fb)->rotated[plane].pitch;
2199 else
2200 return fb->pitches[plane];
2201}
2202
6687c906
VS
2203/*
2204 * Convert the x/y offsets into a linear offset.
2205 * Only valid with 0/180 degree rotation, which is fine since linear
2206 * offset is only used with linear buffers on pre-hsw and tiled buffers
2207 * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
2208 */
2209u32 intel_fb_xy_to_linear(int x, int y,
2949056c
VS
2210 const struct intel_plane_state *state,
2211 int plane)
6687c906 2212{
2949056c 2213 const struct drm_framebuffer *fb = state->base.fb;
353c8598 2214 unsigned int cpp = fb->format->cpp[plane];
6687c906
VS
2215 unsigned int pitch = fb->pitches[plane];
2216
2217 return y * pitch + x * cpp;
2218}
2219
2220/*
2221 * Add the x/y offsets derived from fb->offsets[] to the user
2222 * specified plane src x/y offsets. The resulting x/y offsets
2223 * specify the start of scanout from the beginning of the gtt mapping.
2224 */
2225void intel_add_fb_offsets(int *x, int *y,
2949056c
VS
2226 const struct intel_plane_state *state,
2227 int plane)
6687c906
VS
2228
2229{
2949056c
VS
2230 const struct intel_framebuffer *intel_fb = to_intel_framebuffer(state->base.fb);
2231 unsigned int rotation = state->base.rotation;
6687c906 2232
bd2ef25d 2233 if (drm_rotation_90_or_270(rotation)) {
6687c906
VS
2234 *x += intel_fb->rotated[plane].x;
2235 *y += intel_fb->rotated[plane].y;
2236 } else {
2237 *x += intel_fb->normal[plane].x;
2238 *y += intel_fb->normal[plane].y;
2239 }
2240}
2241
29cf9491 2242/*
29cf9491
VS
2243 * Input tile dimensions and pitch must already be
2244 * rotated to match x and y, and in pixel units.
2245 */
66a2d927
VS
2246static u32 _intel_adjust_tile_offset(int *x, int *y,
2247 unsigned int tile_width,
2248 unsigned int tile_height,
2249 unsigned int tile_size,
2250 unsigned int pitch_tiles,
2251 u32 old_offset,
2252 u32 new_offset)
29cf9491 2253{
b9b24038 2254 unsigned int pitch_pixels = pitch_tiles * tile_width;
29cf9491
VS
2255 unsigned int tiles;
2256
2257 WARN_ON(old_offset & (tile_size - 1));
2258 WARN_ON(new_offset & (tile_size - 1));
2259 WARN_ON(new_offset > old_offset);
2260
2261 tiles = (old_offset - new_offset) / tile_size;
2262
2263 *y += tiles / pitch_tiles * tile_height;
2264 *x += tiles % pitch_tiles * tile_width;
2265
b9b24038
VS
2266 /* minimize x in case it got needlessly big */
2267 *y += *x / pitch_pixels * tile_height;
2268 *x %= pitch_pixels;
2269
29cf9491
VS
2270 return new_offset;
2271}
2272
66a2d927
VS
2273/*
2274 * Adjust the tile offset by moving the difference into
2275 * the x/y offsets.
2276 */
2277static u32 intel_adjust_tile_offset(int *x, int *y,
2278 const struct intel_plane_state *state, int plane,
2279 u32 old_offset, u32 new_offset)
2280{
2281 const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
2282 const struct drm_framebuffer *fb = state->base.fb;
353c8598 2283 unsigned int cpp = fb->format->cpp[plane];
66a2d927
VS
2284 unsigned int rotation = state->base.rotation;
2285 unsigned int pitch = intel_fb_pitch(fb, plane, rotation);
2286
2287 WARN_ON(new_offset > old_offset);
2288
bae781b2 2289 if (fb->modifier != DRM_FORMAT_MOD_NONE) {
66a2d927
VS
2290 unsigned int tile_size, tile_width, tile_height;
2291 unsigned int pitch_tiles;
2292
2293 tile_size = intel_tile_size(dev_priv);
2294 intel_tile_dims(dev_priv, &tile_width, &tile_height,
bae781b2 2295 fb->modifier, cpp);
66a2d927 2296
bd2ef25d 2297 if (drm_rotation_90_or_270(rotation)) {
66a2d927
VS
2298 pitch_tiles = pitch / tile_height;
2299 swap(tile_width, tile_height);
2300 } else {
2301 pitch_tiles = pitch / (tile_width * cpp);
2302 }
2303
2304 _intel_adjust_tile_offset(x, y, tile_width, tile_height,
2305 tile_size, pitch_tiles,
2306 old_offset, new_offset);
2307 } else {
2308 old_offset += *y * pitch + *x * cpp;
2309
2310 *y = (old_offset - new_offset) / pitch;
2311 *x = ((old_offset - new_offset) - *y * pitch) / cpp;
2312 }
2313
2314 return new_offset;
2315}
2316
8d0deca8
VS
2317/*
2318 * Computes the linear offset to the base tile and adjusts
2319 * x, y. bytes per pixel is assumed to be a power-of-two.
2320 *
2321 * In the 90/270 rotated case, x and y are assumed
2322 * to be already rotated to match the rotated GTT view, and
2323 * pitch is the tile_height aligned framebuffer height.
6687c906
VS
2324 *
2325 * This function is used when computing the derived information
2326 * under intel_framebuffer, so using any of that information
2327 * here is not allowed. Anything under drm_framebuffer can be
2328 * used. This is why the user has to pass in the pitch since it
2329 * is specified in the rotated orientation.
8d0deca8 2330 */
6687c906
VS
2331static u32 _intel_compute_tile_offset(const struct drm_i915_private *dev_priv,
2332 int *x, int *y,
2333 const struct drm_framebuffer *fb, int plane,
2334 unsigned int pitch,
2335 unsigned int rotation,
2336 u32 alignment)
c2c75131 2337{
bae781b2 2338 uint64_t fb_modifier = fb->modifier;
353c8598 2339 unsigned int cpp = fb->format->cpp[plane];
6687c906 2340 u32 offset, offset_aligned;
29cf9491 2341
29cf9491
VS
2342 if (alignment)
2343 alignment--;
2344
b5c65338 2345 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
8d0deca8
VS
2346 unsigned int tile_size, tile_width, tile_height;
2347 unsigned int tile_rows, tiles, pitch_tiles;
c2c75131 2348
d843310d 2349 tile_size = intel_tile_size(dev_priv);
8d0deca8
VS
2350 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2351 fb_modifier, cpp);
2352
bd2ef25d 2353 if (drm_rotation_90_or_270(rotation)) {
8d0deca8
VS
2354 pitch_tiles = pitch / tile_height;
2355 swap(tile_width, tile_height);
2356 } else {
2357 pitch_tiles = pitch / (tile_width * cpp);
2358 }
d843310d
VS
2359
2360 tile_rows = *y / tile_height;
2361 *y %= tile_height;
c2c75131 2362
8d0deca8
VS
2363 tiles = *x / tile_width;
2364 *x %= tile_width;
bc752862 2365
29cf9491
VS
2366 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2367 offset_aligned = offset & ~alignment;
bc752862 2368
66a2d927
VS
2369 _intel_adjust_tile_offset(x, y, tile_width, tile_height,
2370 tile_size, pitch_tiles,
2371 offset, offset_aligned);
29cf9491 2372 } else {
bc752862 2373 offset = *y * pitch + *x * cpp;
29cf9491
VS
2374 offset_aligned = offset & ~alignment;
2375
4e9a86b6
VS
2376 *y = (offset & alignment) / pitch;
2377 *x = ((offset & alignment) - *y * pitch) / cpp;
bc752862 2378 }
29cf9491
VS
2379
2380 return offset_aligned;
c2c75131
DV
2381}
2382
6687c906 2383u32 intel_compute_tile_offset(int *x, int *y,
2949056c
VS
2384 const struct intel_plane_state *state,
2385 int plane)
6687c906 2386{
2949056c
VS
2387 const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
2388 const struct drm_framebuffer *fb = state->base.fb;
2389 unsigned int rotation = state->base.rotation;
ef78ec94 2390 int pitch = intel_fb_pitch(fb, plane, rotation);
8d970654
VS
2391 u32 alignment;
2392
2393 /* AUX_DIST needs only 4K alignment */
438b74a5 2394 if (fb->format->format == DRM_FORMAT_NV12 && plane == 1)
8d970654
VS
2395 alignment = 4096;
2396 else
bae781b2 2397 alignment = intel_surf_alignment(dev_priv, fb->modifier);
6687c906
VS
2398
2399 return _intel_compute_tile_offset(dev_priv, x, y, fb, plane, pitch,
2400 rotation, alignment);
2401}
2402
2403/* Convert the fb->offset[] linear offset into x/y offsets */
2404static void intel_fb_offset_to_xy(int *x, int *y,
2405 const struct drm_framebuffer *fb, int plane)
2406{
353c8598 2407 unsigned int cpp = fb->format->cpp[plane];
6687c906
VS
2408 unsigned int pitch = fb->pitches[plane];
2409 u32 linear_offset = fb->offsets[plane];
2410
2411 *y = linear_offset / pitch;
2412 *x = linear_offset % pitch / cpp;
2413}
2414
72618ebf
VS
2415static unsigned int intel_fb_modifier_to_tiling(uint64_t fb_modifier)
2416{
2417 switch (fb_modifier) {
2418 case I915_FORMAT_MOD_X_TILED:
2419 return I915_TILING_X;
2420 case I915_FORMAT_MOD_Y_TILED:
2421 return I915_TILING_Y;
2422 default:
2423 return I915_TILING_NONE;
2424 }
2425}
2426
6687c906
VS
2427static int
2428intel_fill_fb_info(struct drm_i915_private *dev_priv,
2429 struct drm_framebuffer *fb)
2430{
2431 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
2432 struct intel_rotation_info *rot_info = &intel_fb->rot_info;
2433 u32 gtt_offset_rotated = 0;
2434 unsigned int max_size = 0;
bcb0b461 2435 int i, num_planes = fb->format->num_planes;
6687c906
VS
2436 unsigned int tile_size = intel_tile_size(dev_priv);
2437
2438 for (i = 0; i < num_planes; i++) {
2439 unsigned int width, height;
2440 unsigned int cpp, size;
2441 u32 offset;
2442 int x, y;
2443
353c8598 2444 cpp = fb->format->cpp[i];
145fcb11
VS
2445 width = drm_framebuffer_plane_width(fb->width, fb, i);
2446 height = drm_framebuffer_plane_height(fb->height, fb, i);
6687c906
VS
2447
2448 intel_fb_offset_to_xy(&x, &y, fb, i);
2449
60d5f2a4
VS
2450 /*
2451 * The fence (if used) is aligned to the start of the object
2452 * so having the framebuffer wrap around across the edge of the
2453 * fenced region doesn't really work. We have no API to configure
2454 * the fence start offset within the object (nor could we probably
2455 * on gen2/3). So it's just easier if we just require that the
2456 * fb layout agrees with the fence layout. We already check that the
2457 * fb stride matches the fence stride elsewhere.
2458 */
2459 if (i915_gem_object_is_tiled(intel_fb->obj) &&
2460 (x + width) * cpp > fb->pitches[i]) {
2461 DRM_DEBUG("bad fb plane %d offset: 0x%x\n",
2462 i, fb->offsets[i]);
2463 return -EINVAL;
2464 }
2465
6687c906
VS
2466 /*
2467 * First pixel of the framebuffer from
2468 * the start of the normal gtt mapping.
2469 */
2470 intel_fb->normal[i].x = x;
2471 intel_fb->normal[i].y = y;
2472
2473 offset = _intel_compute_tile_offset(dev_priv, &x, &y,
2474 fb, 0, fb->pitches[i],
cc926387 2475 DRM_ROTATE_0, tile_size);
6687c906
VS
2476 offset /= tile_size;
2477
bae781b2 2478 if (fb->modifier != DRM_FORMAT_MOD_NONE) {
6687c906
VS
2479 unsigned int tile_width, tile_height;
2480 unsigned int pitch_tiles;
2481 struct drm_rect r;
2482
2483 intel_tile_dims(dev_priv, &tile_width, &tile_height,
bae781b2 2484 fb->modifier, cpp);
6687c906
VS
2485
2486 rot_info->plane[i].offset = offset;
2487 rot_info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i], tile_width * cpp);
2488 rot_info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
2489 rot_info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
2490
2491 intel_fb->rotated[i].pitch =
2492 rot_info->plane[i].height * tile_height;
2493
2494 /* how many tiles does this plane need */
2495 size = rot_info->plane[i].stride * rot_info->plane[i].height;
2496 /*
2497 * If the plane isn't horizontally tile aligned,
2498 * we need one more tile.
2499 */
2500 if (x != 0)
2501 size++;
2502
2503 /* rotate the x/y offsets to match the GTT view */
2504 r.x1 = x;
2505 r.y1 = y;
2506 r.x2 = x + width;
2507 r.y2 = y + height;
2508 drm_rect_rotate(&r,
2509 rot_info->plane[i].width * tile_width,
2510 rot_info->plane[i].height * tile_height,
cc926387 2511 DRM_ROTATE_270);
6687c906
VS
2512 x = r.x1;
2513 y = r.y1;
2514
2515 /* rotate the tile dimensions to match the GTT view */
2516 pitch_tiles = intel_fb->rotated[i].pitch / tile_height;
2517 swap(tile_width, tile_height);
2518
2519 /*
2520 * We only keep the x/y offsets, so push all of the
2521 * gtt offset into the x/y offsets.
2522 */
46a1bd28
ACO
2523 _intel_adjust_tile_offset(&x, &y,
2524 tile_width, tile_height,
2525 tile_size, pitch_tiles,
66a2d927 2526 gtt_offset_rotated * tile_size, 0);
6687c906
VS
2527
2528 gtt_offset_rotated += rot_info->plane[i].width * rot_info->plane[i].height;
2529
2530 /*
2531 * First pixel of the framebuffer from
2532 * the start of the rotated gtt mapping.
2533 */
2534 intel_fb->rotated[i].x = x;
2535 intel_fb->rotated[i].y = y;
2536 } else {
2537 size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
2538 x * cpp, tile_size);
2539 }
2540
2541 /* how many tiles in total needed in the bo */
2542 max_size = max(max_size, offset + size);
2543 }
2544
2545 if (max_size * tile_size > to_intel_framebuffer(fb)->obj->base.size) {
2546 DRM_DEBUG("fb too big for bo (need %u bytes, have %zu bytes)\n",
2547 max_size * tile_size, to_intel_framebuffer(fb)->obj->base.size);
2548 return -EINVAL;
2549 }
2550
2551 return 0;
2552}
2553
b35d63fa 2554static int i9xx_format_to_fourcc(int format)
46f297fb
JB
2555{
2556 switch (format) {
2557 case DISPPLANE_8BPP:
2558 return DRM_FORMAT_C8;
2559 case DISPPLANE_BGRX555:
2560 return DRM_FORMAT_XRGB1555;
2561 case DISPPLANE_BGRX565:
2562 return DRM_FORMAT_RGB565;
2563 default:
2564 case DISPPLANE_BGRX888:
2565 return DRM_FORMAT_XRGB8888;
2566 case DISPPLANE_RGBX888:
2567 return DRM_FORMAT_XBGR8888;
2568 case DISPPLANE_BGRX101010:
2569 return DRM_FORMAT_XRGB2101010;
2570 case DISPPLANE_RGBX101010:
2571 return DRM_FORMAT_XBGR2101010;
2572 }
2573}
2574
bc8d7dff
DL
2575static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2576{
2577 switch (format) {
2578 case PLANE_CTL_FORMAT_RGB_565:
2579 return DRM_FORMAT_RGB565;
2580 default:
2581 case PLANE_CTL_FORMAT_XRGB_8888:
2582 if (rgb_order) {
2583 if (alpha)
2584 return DRM_FORMAT_ABGR8888;
2585 else
2586 return DRM_FORMAT_XBGR8888;
2587 } else {
2588 if (alpha)
2589 return DRM_FORMAT_ARGB8888;
2590 else
2591 return DRM_FORMAT_XRGB8888;
2592 }
2593 case PLANE_CTL_FORMAT_XRGB_2101010:
2594 if (rgb_order)
2595 return DRM_FORMAT_XBGR2101010;
2596 else
2597 return DRM_FORMAT_XRGB2101010;
2598 }
2599}
2600
5724dbd1 2601static bool
f6936e29
DV
2602intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2603 struct intel_initial_plane_config *plane_config)
46f297fb
JB
2604{
2605 struct drm_device *dev = crtc->base.dev;
3badb49f 2606 struct drm_i915_private *dev_priv = to_i915(dev);
72e96d64 2607 struct i915_ggtt *ggtt = &dev_priv->ggtt;
46f297fb
JB
2608 struct drm_i915_gem_object *obj = NULL;
2609 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2d14030b 2610 struct drm_framebuffer *fb = &plane_config->fb->base;
f37b5c2b
DV
2611 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2612 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2613 PAGE_SIZE);
2614
2615 size_aligned -= base_aligned;
46f297fb 2616
ff2652ea
CW
2617 if (plane_config->size == 0)
2618 return false;
2619
3badb49f
PZ
2620 /* If the FB is too big, just don't use it since fbdev is not very
2621 * important and we should probably use that space with FBC or other
2622 * features. */
72e96d64 2623 if (size_aligned * 2 > ggtt->stolen_usable_size)
3badb49f
PZ
2624 return false;
2625
12c83d99 2626 mutex_lock(&dev->struct_mutex);
187685cb 2627 obj = i915_gem_object_create_stolen_for_preallocated(dev_priv,
f37b5c2b
DV
2628 base_aligned,
2629 base_aligned,
2630 size_aligned);
24dbf51a
CW
2631 mutex_unlock(&dev->struct_mutex);
2632 if (!obj)
484b41dd 2633 return false;
46f297fb 2634
3e510a8e
CW
2635 if (plane_config->tiling == I915_TILING_X)
2636 obj->tiling_and_stride = fb->pitches[0] | I915_TILING_X;
46f297fb 2637
438b74a5 2638 mode_cmd.pixel_format = fb->format->format;
6bf129df
DL
2639 mode_cmd.width = fb->width;
2640 mode_cmd.height = fb->height;
2641 mode_cmd.pitches[0] = fb->pitches[0];
bae781b2 2642 mode_cmd.modifier[0] = fb->modifier;
18c5247e 2643 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
46f297fb 2644
24dbf51a 2645 if (intel_framebuffer_init(to_intel_framebuffer(fb), obj, &mode_cmd)) {
46f297fb
JB
2646 DRM_DEBUG_KMS("intel fb init failed\n");
2647 goto out_unref_obj;
2648 }
12c83d99 2649
484b41dd 2650
f6936e29 2651 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
484b41dd 2652 return true;
46f297fb
JB
2653
2654out_unref_obj:
f8c417cd 2655 i915_gem_object_put(obj);
484b41dd
JB
2656 return false;
2657}
2658
5a21b665
DV
2659/* Update plane->state->fb to match plane->fb after driver-internal updates */
2660static void
2661update_state_fb(struct drm_plane *plane)
2662{
2663 if (plane->fb == plane->state->fb)
2664 return;
2665
2666 if (plane->state->fb)
2667 drm_framebuffer_unreference(plane->state->fb);
2668 plane->state->fb = plane->fb;
2669 if (plane->state->fb)
2670 drm_framebuffer_reference(plane->state->fb);
2671}
2672
5724dbd1 2673static void
f6936e29
DV
2674intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2675 struct intel_initial_plane_config *plane_config)
484b41dd
JB
2676{
2677 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 2678 struct drm_i915_private *dev_priv = to_i915(dev);
484b41dd 2679 struct drm_crtc *c;
2ff8fde1 2680 struct drm_i915_gem_object *obj;
88595ac9 2681 struct drm_plane *primary = intel_crtc->base.primary;
be5651f2 2682 struct drm_plane_state *plane_state = primary->state;
200757f5
MR
2683 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2684 struct intel_plane *intel_plane = to_intel_plane(primary);
0a8d8a86
MR
2685 struct intel_plane_state *intel_state =
2686 to_intel_plane_state(plane_state);
88595ac9 2687 struct drm_framebuffer *fb;
484b41dd 2688
2d14030b 2689 if (!plane_config->fb)
484b41dd
JB
2690 return;
2691
f6936e29 2692 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
88595ac9
DV
2693 fb = &plane_config->fb->base;
2694 goto valid_fb;
f55548b5 2695 }
484b41dd 2696
2d14030b 2697 kfree(plane_config->fb);
484b41dd
JB
2698
2699 /*
2700 * Failed to alloc the obj, check to see if we should share
2701 * an fb with another CRTC instead
2702 */
70e1e0ec 2703 for_each_crtc(dev, c) {
be1e3415 2704 struct intel_plane_state *state;
484b41dd
JB
2705
2706 if (c == &intel_crtc->base)
2707 continue;
2708
be1e3415 2709 if (!to_intel_crtc(c)->active)
2ff8fde1
MR
2710 continue;
2711
be1e3415
CW
2712 state = to_intel_plane_state(c->primary->state);
2713 if (!state->vma)
484b41dd
JB
2714 continue;
2715
be1e3415
CW
2716 if (intel_plane_ggtt_offset(state) == plane_config->base) {
2717 fb = c->primary->fb;
88595ac9
DV
2718 drm_framebuffer_reference(fb);
2719 goto valid_fb;
484b41dd
JB
2720 }
2721 }
88595ac9 2722
200757f5
MR
2723 /*
2724 * We've failed to reconstruct the BIOS FB. Current display state
2725 * indicates that the primary plane is visible, but has a NULL FB,
2726 * which will lead to problems later if we don't fix it up. The
2727 * simplest solution is to just disable the primary plane now and
2728 * pretend the BIOS never had it enabled.
2729 */
1d4258db 2730 plane_state->visible = false;
200757f5 2731 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
2622a081 2732 intel_pre_disable_primary_noatomic(&intel_crtc->base);
200757f5
MR
2733 intel_plane->disable_plane(primary, &intel_crtc->base);
2734
88595ac9
DV
2735 return;
2736
2737valid_fb:
be1e3415
CW
2738 mutex_lock(&dev->struct_mutex);
2739 intel_state->vma =
2740 intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
2741 mutex_unlock(&dev->struct_mutex);
2742 if (IS_ERR(intel_state->vma)) {
2743 DRM_ERROR("failed to pin boot fb on pipe %d: %li\n",
2744 intel_crtc->pipe, PTR_ERR(intel_state->vma));
2745
2746 intel_state->vma = NULL;
2747 drm_framebuffer_unreference(fb);
2748 return;
2749 }
2750
f44e2659
VS
2751 plane_state->src_x = 0;
2752 plane_state->src_y = 0;
be5651f2
ML
2753 plane_state->src_w = fb->width << 16;
2754 plane_state->src_h = fb->height << 16;
2755
f44e2659
VS
2756 plane_state->crtc_x = 0;
2757 plane_state->crtc_y = 0;
be5651f2
ML
2758 plane_state->crtc_w = fb->width;
2759 plane_state->crtc_h = fb->height;
2760
1638d30c
RC
2761 intel_state->base.src = drm_plane_state_src(plane_state);
2762 intel_state->base.dst = drm_plane_state_dest(plane_state);
0a8d8a86 2763
88595ac9 2764 obj = intel_fb_obj(fb);
3e510a8e 2765 if (i915_gem_object_is_tiled(obj))
88595ac9
DV
2766 dev_priv->preserve_bios_swizzle = true;
2767
be5651f2
ML
2768 drm_framebuffer_reference(fb);
2769 primary->fb = primary->state->fb = fb;
36750f28 2770 primary->crtc = primary->state->crtc = &intel_crtc->base;
36750f28 2771 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
faf5bf0a
CW
2772 atomic_or(to_intel_plane(primary)->frontbuffer_bit,
2773 &obj->frontbuffer_bits);
46f297fb
JB
2774}
2775
b63a16f6
VS
2776static int skl_max_plane_width(const struct drm_framebuffer *fb, int plane,
2777 unsigned int rotation)
2778{
353c8598 2779 int cpp = fb->format->cpp[plane];
b63a16f6 2780
bae781b2 2781 switch (fb->modifier) {
b63a16f6
VS
2782 case DRM_FORMAT_MOD_NONE:
2783 case I915_FORMAT_MOD_X_TILED:
2784 switch (cpp) {
2785 case 8:
2786 return 4096;
2787 case 4:
2788 case 2:
2789 case 1:
2790 return 8192;
2791 default:
2792 MISSING_CASE(cpp);
2793 break;
2794 }
2795 break;
2796 case I915_FORMAT_MOD_Y_TILED:
2797 case I915_FORMAT_MOD_Yf_TILED:
2798 switch (cpp) {
2799 case 8:
2800 return 2048;
2801 case 4:
2802 return 4096;
2803 case 2:
2804 case 1:
2805 return 8192;
2806 default:
2807 MISSING_CASE(cpp);
2808 break;
2809 }
2810 break;
2811 default:
bae781b2 2812 MISSING_CASE(fb->modifier);
b63a16f6
VS
2813 }
2814
2815 return 2048;
2816}
2817
2818static int skl_check_main_surface(struct intel_plane_state *plane_state)
2819{
2820 const struct drm_i915_private *dev_priv = to_i915(plane_state->base.plane->dev);
2821 const struct drm_framebuffer *fb = plane_state->base.fb;
2822 unsigned int rotation = plane_state->base.rotation;
cc926387
DV
2823 int x = plane_state->base.src.x1 >> 16;
2824 int y = plane_state->base.src.y1 >> 16;
2825 int w = drm_rect_width(&plane_state->base.src) >> 16;
2826 int h = drm_rect_height(&plane_state->base.src) >> 16;
b63a16f6
VS
2827 int max_width = skl_max_plane_width(fb, 0, rotation);
2828 int max_height = 4096;
8d970654 2829 u32 alignment, offset, aux_offset = plane_state->aux.offset;
b63a16f6
VS
2830
2831 if (w > max_width || h > max_height) {
2832 DRM_DEBUG_KMS("requested Y/RGB source size %dx%d too big (limit %dx%d)\n",
2833 w, h, max_width, max_height);
2834 return -EINVAL;
2835 }
2836
2837 intel_add_fb_offsets(&x, &y, plane_state, 0);
2838 offset = intel_compute_tile_offset(&x, &y, plane_state, 0);
2839
bae781b2 2840 alignment = intel_surf_alignment(dev_priv, fb->modifier);
b63a16f6 2841
8d970654
VS
2842 /*
2843 * AUX surface offset is specified as the distance from the
2844 * main surface offset, and it must be non-negative. Make
2845 * sure that is what we will get.
2846 */
2847 if (offset > aux_offset)
2848 offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
2849 offset, aux_offset & ~(alignment - 1));
2850
b63a16f6
VS
2851 /*
2852 * When using an X-tiled surface, the plane blows up
2853 * if the x offset + width exceed the stride.
2854 *
2855 * TODO: linear and Y-tiled seem fine, Yf untested,
2856 */
bae781b2 2857 if (fb->modifier == I915_FORMAT_MOD_X_TILED) {
353c8598 2858 int cpp = fb->format->cpp[0];
b63a16f6
VS
2859
2860 while ((x + w) * cpp > fb->pitches[0]) {
2861 if (offset == 0) {
2862 DRM_DEBUG_KMS("Unable to find suitable display surface offset\n");
2863 return -EINVAL;
2864 }
2865
2866 offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
2867 offset, offset - alignment);
2868 }
2869 }
2870
2871 plane_state->main.offset = offset;
2872 plane_state->main.x = x;
2873 plane_state->main.y = y;
2874
2875 return 0;
2876}
2877
8d970654
VS
2878static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
2879{
2880 const struct drm_framebuffer *fb = plane_state->base.fb;
2881 unsigned int rotation = plane_state->base.rotation;
2882 int max_width = skl_max_plane_width(fb, 1, rotation);
2883 int max_height = 4096;
cc926387
DV
2884 int x = plane_state->base.src.x1 >> 17;
2885 int y = plane_state->base.src.y1 >> 17;
2886 int w = drm_rect_width(&plane_state->base.src) >> 17;
2887 int h = drm_rect_height(&plane_state->base.src) >> 17;
8d970654
VS
2888 u32 offset;
2889
2890 intel_add_fb_offsets(&x, &y, plane_state, 1);
2891 offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
2892
2893 /* FIXME not quite sure how/if these apply to the chroma plane */
2894 if (w > max_width || h > max_height) {
2895 DRM_DEBUG_KMS("CbCr source size %dx%d too big (limit %dx%d)\n",
2896 w, h, max_width, max_height);
2897 return -EINVAL;
2898 }
2899
2900 plane_state->aux.offset = offset;
2901 plane_state->aux.x = x;
2902 plane_state->aux.y = y;
2903
2904 return 0;
2905}
2906
b63a16f6
VS
2907int skl_check_plane_surface(struct intel_plane_state *plane_state)
2908{
2909 const struct drm_framebuffer *fb = plane_state->base.fb;
2910 unsigned int rotation = plane_state->base.rotation;
2911 int ret;
2912
a5e4c7d0
VS
2913 if (!plane_state->base.visible)
2914 return 0;
2915
b63a16f6 2916 /* Rotate src coordinates to match rotated GTT view */
bd2ef25d 2917 if (drm_rotation_90_or_270(rotation))
cc926387 2918 drm_rect_rotate(&plane_state->base.src,
da064b47
VS
2919 fb->width << 16, fb->height << 16,
2920 DRM_ROTATE_270);
b63a16f6 2921
8d970654
VS
2922 /*
2923 * Handle the AUX surface first since
2924 * the main surface setup depends on it.
2925 */
438b74a5 2926 if (fb->format->format == DRM_FORMAT_NV12) {
8d970654
VS
2927 ret = skl_check_nv12_aux_surface(plane_state);
2928 if (ret)
2929 return ret;
2930 } else {
2931 plane_state->aux.offset = ~0xfff;
2932 plane_state->aux.x = 0;
2933 plane_state->aux.y = 0;
2934 }
2935
b63a16f6
VS
2936 ret = skl_check_main_surface(plane_state);
2937 if (ret)
2938 return ret;
2939
2940 return 0;
2941}
2942
a8d201af
ML
2943static void i9xx_update_primary_plane(struct drm_plane *primary,
2944 const struct intel_crtc_state *crtc_state,
2945 const struct intel_plane_state *plane_state)
81255565 2946{
6315b5d3 2947 struct drm_i915_private *dev_priv = to_i915(primary->dev);
a8d201af
ML
2948 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2949 struct drm_framebuffer *fb = plane_state->base.fb;
81255565 2950 int plane = intel_crtc->plane;
54ea9da8 2951 u32 linear_offset;
81255565 2952 u32 dspcntr;
f0f59a00 2953 i915_reg_t reg = DSPCNTR(plane);
8d0deca8 2954 unsigned int rotation = plane_state->base.rotation;
936e71e3
VS
2955 int x = plane_state->base.src.x1 >> 16;
2956 int y = plane_state->base.src.y1 >> 16;
c9ba6fad 2957
f45651ba
VS
2958 dspcntr = DISPPLANE_GAMMA_ENABLE;
2959
fdd508a6 2960 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba 2961
6315b5d3 2962 if (INTEL_GEN(dev_priv) < 4) {
f45651ba
VS
2963 if (intel_crtc->pipe == PIPE_B)
2964 dspcntr |= DISPPLANE_SEL_PIPE_B;
2965
2966 /* pipesrc and dspsize control the size that is scaled from,
2967 * which should always be the user's requested size.
2968 */
2969 I915_WRITE(DSPSIZE(plane),
a8d201af
ML
2970 ((crtc_state->pipe_src_h - 1) << 16) |
2971 (crtc_state->pipe_src_w - 1));
f45651ba 2972 I915_WRITE(DSPPOS(plane), 0);
920a14b2 2973 } else if (IS_CHERRYVIEW(dev_priv) && plane == PLANE_B) {
c14b0485 2974 I915_WRITE(PRIMSIZE(plane),
a8d201af
ML
2975 ((crtc_state->pipe_src_h - 1) << 16) |
2976 (crtc_state->pipe_src_w - 1));
c14b0485
VS
2977 I915_WRITE(PRIMPOS(plane), 0);
2978 I915_WRITE(PRIMCNSTALPHA(plane), 0);
f45651ba 2979 }
81255565 2980
438b74a5 2981 switch (fb->format->format) {
57779d06 2982 case DRM_FORMAT_C8:
81255565
JB
2983 dspcntr |= DISPPLANE_8BPP;
2984 break;
57779d06 2985 case DRM_FORMAT_XRGB1555:
57779d06 2986 dspcntr |= DISPPLANE_BGRX555;
81255565 2987 break;
57779d06
VS
2988 case DRM_FORMAT_RGB565:
2989 dspcntr |= DISPPLANE_BGRX565;
2990 break;
2991 case DRM_FORMAT_XRGB8888:
57779d06
VS
2992 dspcntr |= DISPPLANE_BGRX888;
2993 break;
2994 case DRM_FORMAT_XBGR8888:
57779d06
VS
2995 dspcntr |= DISPPLANE_RGBX888;
2996 break;
2997 case DRM_FORMAT_XRGB2101010:
57779d06
VS
2998 dspcntr |= DISPPLANE_BGRX101010;
2999 break;
3000 case DRM_FORMAT_XBGR2101010:
57779d06 3001 dspcntr |= DISPPLANE_RGBX101010;
81255565
JB
3002 break;
3003 default:
baba133a 3004 BUG();
81255565 3005 }
57779d06 3006
72618ebf 3007 if (INTEL_GEN(dev_priv) >= 4 &&
bae781b2 3008 fb->modifier == I915_FORMAT_MOD_X_TILED)
f45651ba 3009 dspcntr |= DISPPLANE_TILED;
81255565 3010
df0cd455
VS
3011 if (rotation & DRM_ROTATE_180)
3012 dspcntr |= DISPPLANE_ROTATE_180;
3013
4ea7be2b
VS
3014 if (rotation & DRM_REFLECT_X)
3015 dspcntr |= DISPPLANE_MIRROR;
3016
9beb5fea 3017 if (IS_G4X(dev_priv))
de1aa629
VS
3018 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
3019
2949056c 3020 intel_add_fb_offsets(&x, &y, plane_state, 0);
81255565 3021
6315b5d3 3022 if (INTEL_GEN(dev_priv) >= 4)
c2c75131 3023 intel_crtc->dspaddr_offset =
2949056c 3024 intel_compute_tile_offset(&x, &y, plane_state, 0);
e506a0c6 3025
f22aa143 3026 if (rotation & DRM_ROTATE_180) {
df0cd455
VS
3027 x += crtc_state->pipe_src_w - 1;
3028 y += crtc_state->pipe_src_h - 1;
4ea7be2b
VS
3029 } else if (rotation & DRM_REFLECT_X) {
3030 x += crtc_state->pipe_src_w - 1;
48404c1e
SJ
3031 }
3032
2949056c 3033 linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
6687c906 3034
6315b5d3 3035 if (INTEL_GEN(dev_priv) < 4)
6687c906
VS
3036 intel_crtc->dspaddr_offset = linear_offset;
3037
2db3366b
PZ
3038 intel_crtc->adjusted_x = x;
3039 intel_crtc->adjusted_y = y;
3040
48404c1e
SJ
3041 I915_WRITE(reg, dspcntr);
3042
01f2c773 3043 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
6315b5d3 3044 if (INTEL_GEN(dev_priv) >= 4) {
85ba7b7d 3045 I915_WRITE(DSPSURF(plane),
be1e3415 3046 intel_plane_ggtt_offset(plane_state) +
6687c906 3047 intel_crtc->dspaddr_offset);
5eddb70b 3048 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
e506a0c6 3049 I915_WRITE(DSPLINOFF(plane), linear_offset);
bfb81049
VS
3050 } else {
3051 I915_WRITE(DSPADDR(plane),
be1e3415 3052 intel_plane_ggtt_offset(plane_state) +
bfb81049
VS
3053 intel_crtc->dspaddr_offset);
3054 }
5eddb70b 3055 POSTING_READ(reg);
17638cd6
JB
3056}
3057
a8d201af
ML
3058static void i9xx_disable_primary_plane(struct drm_plane *primary,
3059 struct drm_crtc *crtc)
17638cd6
JB
3060{
3061 struct drm_device *dev = crtc->dev;
fac5e23e 3062 struct drm_i915_private *dev_priv = to_i915(dev);
17638cd6 3063 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
17638cd6 3064 int plane = intel_crtc->plane;
f45651ba 3065
a8d201af
ML
3066 I915_WRITE(DSPCNTR(plane), 0);
3067 if (INTEL_INFO(dev_priv)->gen >= 4)
fdd508a6 3068 I915_WRITE(DSPSURF(plane), 0);
a8d201af
ML
3069 else
3070 I915_WRITE(DSPADDR(plane), 0);
3071 POSTING_READ(DSPCNTR(plane));
3072}
c9ba6fad 3073
a8d201af
ML
3074static void ironlake_update_primary_plane(struct drm_plane *primary,
3075 const struct intel_crtc_state *crtc_state,
3076 const struct intel_plane_state *plane_state)
3077{
3078 struct drm_device *dev = primary->dev;
fac5e23e 3079 struct drm_i915_private *dev_priv = to_i915(dev);
a8d201af
ML
3080 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3081 struct drm_framebuffer *fb = plane_state->base.fb;
a8d201af 3082 int plane = intel_crtc->plane;
54ea9da8 3083 u32 linear_offset;
a8d201af
ML
3084 u32 dspcntr;
3085 i915_reg_t reg = DSPCNTR(plane);
8d0deca8 3086 unsigned int rotation = plane_state->base.rotation;
936e71e3
VS
3087 int x = plane_state->base.src.x1 >> 16;
3088 int y = plane_state->base.src.y1 >> 16;
c9ba6fad 3089
f45651ba 3090 dspcntr = DISPPLANE_GAMMA_ENABLE;
fdd508a6 3091 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba 3092
8652744b 3093 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
f45651ba 3094 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
17638cd6 3095
438b74a5 3096 switch (fb->format->format) {
57779d06 3097 case DRM_FORMAT_C8:
17638cd6
JB
3098 dspcntr |= DISPPLANE_8BPP;
3099 break;
57779d06
VS
3100 case DRM_FORMAT_RGB565:
3101 dspcntr |= DISPPLANE_BGRX565;
17638cd6 3102 break;
57779d06 3103 case DRM_FORMAT_XRGB8888:
57779d06
VS
3104 dspcntr |= DISPPLANE_BGRX888;
3105 break;
3106 case DRM_FORMAT_XBGR8888:
57779d06
VS
3107 dspcntr |= DISPPLANE_RGBX888;
3108 break;
3109 case DRM_FORMAT_XRGB2101010:
57779d06
VS
3110 dspcntr |= DISPPLANE_BGRX101010;
3111 break;
3112 case DRM_FORMAT_XBGR2101010:
57779d06 3113 dspcntr |= DISPPLANE_RGBX101010;
17638cd6
JB
3114 break;
3115 default:
baba133a 3116 BUG();
17638cd6
JB
3117 }
3118
bae781b2 3119 if (fb->modifier == I915_FORMAT_MOD_X_TILED)
17638cd6 3120 dspcntr |= DISPPLANE_TILED;
17638cd6 3121
df0cd455
VS
3122 if (rotation & DRM_ROTATE_180)
3123 dspcntr |= DISPPLANE_ROTATE_180;
3124
8652744b 3125 if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv))
1f5d76db 3126 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
17638cd6 3127
2949056c 3128 intel_add_fb_offsets(&x, &y, plane_state, 0);
6687c906 3129
c2c75131 3130 intel_crtc->dspaddr_offset =
2949056c 3131 intel_compute_tile_offset(&x, &y, plane_state, 0);
6687c906 3132
df0cd455
VS
3133 /* HSW+ does this automagically in hardware */
3134 if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv) &&
3135 rotation & DRM_ROTATE_180) {
3136 x += crtc_state->pipe_src_w - 1;
3137 y += crtc_state->pipe_src_h - 1;
48404c1e
SJ
3138 }
3139
2949056c 3140 linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
6687c906 3141
2db3366b
PZ
3142 intel_crtc->adjusted_x = x;
3143 intel_crtc->adjusted_y = y;
3144
48404c1e 3145 I915_WRITE(reg, dspcntr);
17638cd6 3146
01f2c773 3147 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
85ba7b7d 3148 I915_WRITE(DSPSURF(plane),
be1e3415 3149 intel_plane_ggtt_offset(plane_state) +
6687c906 3150 intel_crtc->dspaddr_offset);
8652744b 3151 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
bc1c91eb
DL
3152 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
3153 } else {
3154 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
3155 I915_WRITE(DSPLINOFF(plane), linear_offset);
3156 }
17638cd6 3157 POSTING_READ(reg);
17638cd6
JB
3158}
3159
7b49f948
VS
3160u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
3161 uint64_t fb_modifier, uint32_t pixel_format)
b321803d 3162{
7b49f948 3163 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
b321803d 3164 return 64;
7b49f948
VS
3165 } else {
3166 int cpp = drm_format_plane_cpp(pixel_format, 0);
3167
27ba3910 3168 return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
b321803d
DL
3169 }
3170}
3171
e435d6e5
ML
3172static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
3173{
3174 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 3175 struct drm_i915_private *dev_priv = to_i915(dev);
e435d6e5
ML
3176
3177 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
3178 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
3179 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
e435d6e5
ML
3180}
3181
a1b2278e
CK
3182/*
3183 * This function detaches (aka. unbinds) unused scalers in hardware
3184 */
0583236e 3185static void skl_detach_scalers(struct intel_crtc *intel_crtc)
a1b2278e 3186{
a1b2278e
CK
3187 struct intel_crtc_scaler_state *scaler_state;
3188 int i;
3189
a1b2278e
CK
3190 scaler_state = &intel_crtc->config->scaler_state;
3191
3192 /* loop through and disable scalers that aren't in use */
3193 for (i = 0; i < intel_crtc->num_scalers; i++) {
e435d6e5
ML
3194 if (!scaler_state->scalers[i].in_use)
3195 skl_detach_scaler(intel_crtc, i);
a1b2278e
CK
3196 }
3197}
3198
d2196774
VS
3199u32 skl_plane_stride(const struct drm_framebuffer *fb, int plane,
3200 unsigned int rotation)
3201{
3202 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
3203 u32 stride = intel_fb_pitch(fb, plane, rotation);
3204
3205 /*
3206 * The stride is either expressed as a multiple of 64 bytes chunks for
3207 * linear buffers or in number of tiles for tiled buffers.
3208 */
bd2ef25d 3209 if (drm_rotation_90_or_270(rotation)) {
353c8598 3210 int cpp = fb->format->cpp[plane];
d2196774 3211
bae781b2 3212 stride /= intel_tile_height(dev_priv, fb->modifier, cpp);
d2196774 3213 } else {
bae781b2 3214 stride /= intel_fb_stride_alignment(dev_priv, fb->modifier,
438b74a5 3215 fb->format->format);
d2196774
VS
3216 }
3217
3218 return stride;
3219}
3220
6156a456 3221u32 skl_plane_ctl_format(uint32_t pixel_format)
70d21f0e 3222{
6156a456 3223 switch (pixel_format) {
d161cf7a 3224 case DRM_FORMAT_C8:
c34ce3d1 3225 return PLANE_CTL_FORMAT_INDEXED;
70d21f0e 3226 case DRM_FORMAT_RGB565:
c34ce3d1 3227 return PLANE_CTL_FORMAT_RGB_565;
70d21f0e 3228 case DRM_FORMAT_XBGR8888:
c34ce3d1 3229 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
6156a456 3230 case DRM_FORMAT_XRGB8888:
c34ce3d1 3231 return PLANE_CTL_FORMAT_XRGB_8888;
6156a456
CK
3232 /*
3233 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
3234 * to be already pre-multiplied. We need to add a knob (or a different
3235 * DRM_FORMAT) for user-space to configure that.
3236 */
f75fb42a 3237 case DRM_FORMAT_ABGR8888:
c34ce3d1 3238 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
6156a456 3239 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
6156a456 3240 case DRM_FORMAT_ARGB8888:
c34ce3d1 3241 return PLANE_CTL_FORMAT_XRGB_8888 |
6156a456 3242 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
70d21f0e 3243 case DRM_FORMAT_XRGB2101010:
c34ce3d1 3244 return PLANE_CTL_FORMAT_XRGB_2101010;
70d21f0e 3245 case DRM_FORMAT_XBGR2101010:
c34ce3d1 3246 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
6156a456 3247 case DRM_FORMAT_YUYV:
c34ce3d1 3248 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
6156a456 3249 case DRM_FORMAT_YVYU:
c34ce3d1 3250 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
6156a456 3251 case DRM_FORMAT_UYVY:
c34ce3d1 3252 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
6156a456 3253 case DRM_FORMAT_VYUY:
c34ce3d1 3254 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
70d21f0e 3255 default:
4249eeef 3256 MISSING_CASE(pixel_format);
70d21f0e 3257 }
8cfcba41 3258
c34ce3d1 3259 return 0;
6156a456 3260}
70d21f0e 3261
6156a456
CK
3262u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
3263{
6156a456 3264 switch (fb_modifier) {
30af77c4 3265 case DRM_FORMAT_MOD_NONE:
70d21f0e 3266 break;
30af77c4 3267 case I915_FORMAT_MOD_X_TILED:
c34ce3d1 3268 return PLANE_CTL_TILED_X;
b321803d 3269 case I915_FORMAT_MOD_Y_TILED:
c34ce3d1 3270 return PLANE_CTL_TILED_Y;
b321803d 3271 case I915_FORMAT_MOD_Yf_TILED:
c34ce3d1 3272 return PLANE_CTL_TILED_YF;
70d21f0e 3273 default:
6156a456 3274 MISSING_CASE(fb_modifier);
70d21f0e 3275 }
8cfcba41 3276
c34ce3d1 3277 return 0;
6156a456 3278}
70d21f0e 3279
6156a456
CK
3280u32 skl_plane_ctl_rotation(unsigned int rotation)
3281{
3b7a5119 3282 switch (rotation) {
31ad61e4 3283 case DRM_ROTATE_0:
6156a456 3284 break;
1e8df167
SJ
3285 /*
3286 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
3287 * while i915 HW rotation is clockwise, thats why this swapping.
3288 */
31ad61e4 3289 case DRM_ROTATE_90:
1e8df167 3290 return PLANE_CTL_ROTATE_270;
31ad61e4 3291 case DRM_ROTATE_180:
c34ce3d1 3292 return PLANE_CTL_ROTATE_180;
31ad61e4 3293 case DRM_ROTATE_270:
1e8df167 3294 return PLANE_CTL_ROTATE_90;
6156a456
CK
3295 default:
3296 MISSING_CASE(rotation);
3297 }
3298
c34ce3d1 3299 return 0;
6156a456
CK
3300}
3301
a8d201af
ML
3302static void skylake_update_primary_plane(struct drm_plane *plane,
3303 const struct intel_crtc_state *crtc_state,
3304 const struct intel_plane_state *plane_state)
6156a456 3305{
a8d201af 3306 struct drm_device *dev = plane->dev;
fac5e23e 3307 struct drm_i915_private *dev_priv = to_i915(dev);
a8d201af
ML
3308 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3309 struct drm_framebuffer *fb = plane_state->base.fb;
8e816bb4
VS
3310 enum plane_id plane_id = to_intel_plane(plane)->id;
3311 enum pipe pipe = to_intel_plane(plane)->pipe;
d2196774 3312 u32 plane_ctl;
a8d201af 3313 unsigned int rotation = plane_state->base.rotation;
d2196774 3314 u32 stride = skl_plane_stride(fb, 0, rotation);
b63a16f6 3315 u32 surf_addr = plane_state->main.offset;
a8d201af 3316 int scaler_id = plane_state->scaler_id;
b63a16f6
VS
3317 int src_x = plane_state->main.x;
3318 int src_y = plane_state->main.y;
936e71e3
VS
3319 int src_w = drm_rect_width(&plane_state->base.src) >> 16;
3320 int src_h = drm_rect_height(&plane_state->base.src) >> 16;
3321 int dst_x = plane_state->base.dst.x1;
3322 int dst_y = plane_state->base.dst.y1;
3323 int dst_w = drm_rect_width(&plane_state->base.dst);
3324 int dst_h = drm_rect_height(&plane_state->base.dst);
70d21f0e 3325
47f9ea8b
ACO
3326 plane_ctl = PLANE_CTL_ENABLE;
3327
3328 if (IS_GEMINILAKE(dev_priv)) {
3329 I915_WRITE(PLANE_COLOR_CTL(pipe, plane_id),
3330 PLANE_COLOR_PIPE_GAMMA_ENABLE |
3bb56da7 3331 PLANE_COLOR_PIPE_CSC_ENABLE |
47f9ea8b
ACO
3332 PLANE_COLOR_PLANE_GAMMA_DISABLE);
3333 } else {
3334 plane_ctl |=
3335 PLANE_CTL_PIPE_GAMMA_ENABLE |
3336 PLANE_CTL_PIPE_CSC_ENABLE |
3337 PLANE_CTL_PLANE_GAMMA_DISABLE;
3338 }
6156a456 3339
438b74a5 3340 plane_ctl |= skl_plane_ctl_format(fb->format->format);
bae781b2 3341 plane_ctl |= skl_plane_ctl_tiling(fb->modifier);
6156a456
CK
3342 plane_ctl |= skl_plane_ctl_rotation(rotation);
3343
6687c906
VS
3344 /* Sizes are 0 based */
3345 src_w--;
3346 src_h--;
3347 dst_w--;
3348 dst_h--;
3349
4c0b8a8b
PZ
3350 intel_crtc->dspaddr_offset = surf_addr;
3351
6687c906
VS
3352 intel_crtc->adjusted_x = src_x;
3353 intel_crtc->adjusted_y = src_y;
2db3366b 3354
8e816bb4
VS
3355 I915_WRITE(PLANE_CTL(pipe, plane_id), plane_ctl);
3356 I915_WRITE(PLANE_OFFSET(pipe, plane_id), (src_y << 16) | src_x);
3357 I915_WRITE(PLANE_STRIDE(pipe, plane_id), stride);
3358 I915_WRITE(PLANE_SIZE(pipe, plane_id), (src_h << 16) | src_w);
6156a456
CK
3359
3360 if (scaler_id >= 0) {
3361 uint32_t ps_ctrl = 0;
3362
3363 WARN_ON(!dst_w || !dst_h);
8e816bb4 3364 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(plane_id) |
6156a456
CK
3365 crtc_state->scaler_state.scalers[scaler_id].mode;
3366 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3367 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3368 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3369 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
8e816bb4 3370 I915_WRITE(PLANE_POS(pipe, plane_id), 0);
6156a456 3371 } else {
8e816bb4 3372 I915_WRITE(PLANE_POS(pipe, plane_id), (dst_y << 16) | dst_x);
6156a456
CK
3373 }
3374
8e816bb4 3375 I915_WRITE(PLANE_SURF(pipe, plane_id),
be1e3415 3376 intel_plane_ggtt_offset(plane_state) + surf_addr);
70d21f0e 3377
8e816bb4 3378 POSTING_READ(PLANE_SURF(pipe, plane_id));
70d21f0e
DL
3379}
3380
a8d201af
ML
3381static void skylake_disable_primary_plane(struct drm_plane *primary,
3382 struct drm_crtc *crtc)
17638cd6
JB
3383{
3384 struct drm_device *dev = crtc->dev;
fac5e23e 3385 struct drm_i915_private *dev_priv = to_i915(dev);
8e816bb4
VS
3386 enum plane_id plane_id = to_intel_plane(primary)->id;
3387 enum pipe pipe = to_intel_plane(primary)->pipe;
62e0fb88 3388
8e816bb4
VS
3389 I915_WRITE(PLANE_CTL(pipe, plane_id), 0);
3390 I915_WRITE(PLANE_SURF(pipe, plane_id), 0);
3391 POSTING_READ(PLANE_SURF(pipe, plane_id));
a8d201af 3392}
29b9bde6 3393
a8d201af
ML
3394/* Assume fb object is pinned & idle & fenced and just update base pointers */
3395static int
3396intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3397 int x, int y, enum mode_set_atomic state)
3398{
3399 /* Support for kgdboc is disabled, this needs a major rework. */
3400 DRM_ERROR("legacy panic handler not supported any more.\n");
3401
3402 return -ENODEV;
81255565
JB
3403}
3404
5a21b665
DV
3405static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
3406{
3407 struct intel_crtc *crtc;
3408
91c8a326 3409 for_each_intel_crtc(&dev_priv->drm, crtc)
5a21b665
DV
3410 intel_finish_page_flip_cs(dev_priv, crtc->pipe);
3411}
3412
7514747d
VS
3413static void intel_update_primary_planes(struct drm_device *dev)
3414{
7514747d 3415 struct drm_crtc *crtc;
96a02917 3416
70e1e0ec 3417 for_each_crtc(dev, crtc) {
11c22da6 3418 struct intel_plane *plane = to_intel_plane(crtc->primary);
73974893
ML
3419 struct intel_plane_state *plane_state =
3420 to_intel_plane_state(plane->base.state);
11c22da6 3421
936e71e3 3422 if (plane_state->base.visible)
a8d201af
ML
3423 plane->update_plane(&plane->base,
3424 to_intel_crtc_state(crtc->state),
3425 plane_state);
73974893
ML
3426 }
3427}
3428
3429static int
3430__intel_display_resume(struct drm_device *dev,
3431 struct drm_atomic_state *state)
3432{
3433 struct drm_crtc_state *crtc_state;
3434 struct drm_crtc *crtc;
3435 int i, ret;
11c22da6 3436
73974893 3437 intel_modeset_setup_hw_state(dev);
29b74b7f 3438 i915_redisable_vga(to_i915(dev));
73974893
ML
3439
3440 if (!state)
3441 return 0;
3442
3443 for_each_crtc_in_state(state, crtc, crtc_state, i) {
3444 /*
3445 * Force recalculation even if we restore
3446 * current state. With fast modeset this may not result
3447 * in a modeset when the state is compatible.
3448 */
3449 crtc_state->mode_changed = true;
96a02917 3450 }
73974893
ML
3451
3452 /* ignore any reset values/BIOS leftovers in the WM registers */
3453 to_intel_atomic_state(state)->skip_intermediate_wm = true;
3454
3455 ret = drm_atomic_commit(state);
3456
3457 WARN_ON(ret == -EDEADLK);
3458 return ret;
96a02917
VS
3459}
3460
4ac2ba2f
VS
3461static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
3462{
ae98104b
VS
3463 return intel_has_gpu_reset(dev_priv) &&
3464 INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
4ac2ba2f
VS
3465}
3466
c033666a 3467void intel_prepare_reset(struct drm_i915_private *dev_priv)
7514747d 3468{
73974893
ML
3469 struct drm_device *dev = &dev_priv->drm;
3470 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
3471 struct drm_atomic_state *state;
3472 int ret;
3473
73974893
ML
3474 /*
3475 * Need mode_config.mutex so that we don't
3476 * trample ongoing ->detect() and whatnot.
3477 */
3478 mutex_lock(&dev->mode_config.mutex);
3479 drm_modeset_acquire_init(ctx, 0);
3480 while (1) {
3481 ret = drm_modeset_lock_all_ctx(dev, ctx);
3482 if (ret != -EDEADLK)
3483 break;
3484
3485 drm_modeset_backoff(ctx);
3486 }
3487
3488 /* reset doesn't touch the display, but flips might get nuked anyway, */
522a63de 3489 if (!i915.force_reset_modeset_test &&
4ac2ba2f 3490 !gpu_reset_clobbers_display(dev_priv))
7514747d
VS
3491 return;
3492
f98ce92f
VS
3493 /*
3494 * Disabling the crtcs gracefully seems nicer. Also the
3495 * g33 docs say we should at least disable all the planes.
3496 */
73974893
ML
3497 state = drm_atomic_helper_duplicate_state(dev, ctx);
3498 if (IS_ERR(state)) {
3499 ret = PTR_ERR(state);
73974893 3500 DRM_ERROR("Duplicating state failed with %i\n", ret);
1e5a15d6 3501 return;
73974893
ML
3502 }
3503
3504 ret = drm_atomic_helper_disable_all(dev, ctx);
3505 if (ret) {
3506 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
1e5a15d6
ACO
3507 drm_atomic_state_put(state);
3508 return;
73974893
ML
3509 }
3510
3511 dev_priv->modeset_restore_state = state;
3512 state->acquire_ctx = ctx;
7514747d
VS
3513}
3514
c033666a 3515void intel_finish_reset(struct drm_i915_private *dev_priv)
7514747d 3516{
73974893
ML
3517 struct drm_device *dev = &dev_priv->drm;
3518 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
3519 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
3520 int ret;
3521
5a21b665
DV
3522 /*
3523 * Flips in the rings will be nuked by the reset,
3524 * so complete all pending flips so that user space
3525 * will get its events and not get stuck.
3526 */
3527 intel_complete_page_flips(dev_priv);
3528
73974893
ML
3529 dev_priv->modeset_restore_state = NULL;
3530
7514747d 3531 /* reset doesn't touch the display */
4ac2ba2f 3532 if (!gpu_reset_clobbers_display(dev_priv)) {
522a63de
ML
3533 if (!state) {
3534 /*
3535 * Flips in the rings have been nuked by the reset,
3536 * so update the base address of all primary
3537 * planes to the the last fb to make sure we're
3538 * showing the correct fb after a reset.
3539 *
3540 * FIXME: Atomic will make this obsolete since we won't schedule
3541 * CS-based flips (which might get lost in gpu resets) any more.
3542 */
3543 intel_update_primary_planes(dev);
3544 } else {
3545 ret = __intel_display_resume(dev, state);
3546 if (ret)
3547 DRM_ERROR("Restoring old state failed with %i\n", ret);
3548 }
73974893
ML
3549 } else {
3550 /*
3551 * The display has been reset as well,
3552 * so need a full re-initialization.
3553 */
3554 intel_runtime_pm_disable_interrupts(dev_priv);
3555 intel_runtime_pm_enable_interrupts(dev_priv);
7514747d 3556
51f59205 3557 intel_pps_unlock_regs_wa(dev_priv);
73974893 3558 intel_modeset_init_hw(dev);
7514747d 3559
73974893
ML
3560 spin_lock_irq(&dev_priv->irq_lock);
3561 if (dev_priv->display.hpd_irq_setup)
3562 dev_priv->display.hpd_irq_setup(dev_priv);
3563 spin_unlock_irq(&dev_priv->irq_lock);
7514747d 3564
73974893
ML
3565 ret = __intel_display_resume(dev, state);
3566 if (ret)
3567 DRM_ERROR("Restoring old state failed with %i\n", ret);
7514747d 3568
73974893
ML
3569 intel_hpd_init(dev_priv);
3570 }
7514747d 3571
0853695c
CW
3572 if (state)
3573 drm_atomic_state_put(state);
73974893
ML
3574 drm_modeset_drop_locks(ctx);
3575 drm_modeset_acquire_fini(ctx);
3576 mutex_unlock(&dev->mode_config.mutex);
7514747d
VS
3577}
3578
8af29b0c
CW
3579static bool abort_flip_on_reset(struct intel_crtc *crtc)
3580{
3581 struct i915_gpu_error *error = &to_i915(crtc->base.dev)->gpu_error;
3582
3583 if (i915_reset_in_progress(error))
3584 return true;
3585
3586 if (crtc->reset_count != i915_reset_count(error))
3587 return true;
3588
3589 return false;
3590}
3591
7d5e3799
CW
3592static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3593{
5a21b665
DV
3594 struct drm_device *dev = crtc->dev;
3595 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5a21b665
DV
3596 bool pending;
3597
8af29b0c 3598 if (abort_flip_on_reset(intel_crtc))
5a21b665
DV
3599 return false;
3600
3601 spin_lock_irq(&dev->event_lock);
3602 pending = to_intel_crtc(crtc)->flip_work != NULL;
3603 spin_unlock_irq(&dev->event_lock);
3604
3605 return pending;
7d5e3799
CW
3606}
3607
bfd16b2a
ML
3608static void intel_update_pipe_config(struct intel_crtc *crtc,
3609 struct intel_crtc_state *old_crtc_state)
e30e8f75 3610{
6315b5d3 3611 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
bfd16b2a
ML
3612 struct intel_crtc_state *pipe_config =
3613 to_intel_crtc_state(crtc->base.state);
e30e8f75 3614
bfd16b2a
ML
3615 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3616 crtc->base.mode = crtc->base.state->mode;
3617
3618 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3619 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3620 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
e30e8f75
GP
3621
3622 /*
3623 * Update pipe size and adjust fitter if needed: the reason for this is
3624 * that in compute_mode_changes we check the native mode (not the pfit
3625 * mode) to see if we can flip rather than do a full mode set. In the
3626 * fastboot case, we'll flip, but if we don't update the pipesrc and
3627 * pfit state, we'll end up with a big fb scanned out into the wrong
3628 * sized surface.
e30e8f75
GP
3629 */
3630
e30e8f75 3631 I915_WRITE(PIPESRC(crtc->pipe),
bfd16b2a
ML
3632 ((pipe_config->pipe_src_w - 1) << 16) |
3633 (pipe_config->pipe_src_h - 1));
3634
3635 /* on skylake this is done by detaching scalers */
6315b5d3 3636 if (INTEL_GEN(dev_priv) >= 9) {
bfd16b2a
ML
3637 skl_detach_scalers(crtc);
3638
3639 if (pipe_config->pch_pfit.enabled)
3640 skylake_pfit_enable(crtc);
6e266956 3641 } else if (HAS_PCH_SPLIT(dev_priv)) {
bfd16b2a
ML
3642 if (pipe_config->pch_pfit.enabled)
3643 ironlake_pfit_enable(crtc);
3644 else if (old_crtc_state->pch_pfit.enabled)
3645 ironlake_pfit_disable(crtc, true);
e30e8f75 3646 }
e30e8f75
GP
3647}
3648
5e84e1a4
ZW
3649static void intel_fdi_normal_train(struct drm_crtc *crtc)
3650{
3651 struct drm_device *dev = crtc->dev;
fac5e23e 3652 struct drm_i915_private *dev_priv = to_i915(dev);
5e84e1a4
ZW
3653 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3654 int pipe = intel_crtc->pipe;
f0f59a00
VS
3655 i915_reg_t reg;
3656 u32 temp;
5e84e1a4
ZW
3657
3658 /* enable normal train */
3659 reg = FDI_TX_CTL(pipe);
3660 temp = I915_READ(reg);
fd6b8f43 3661 if (IS_IVYBRIDGE(dev_priv)) {
357555c0
JB
3662 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3663 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
3664 } else {
3665 temp &= ~FDI_LINK_TRAIN_NONE;
3666 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 3667 }
5e84e1a4
ZW
3668 I915_WRITE(reg, temp);
3669
3670 reg = FDI_RX_CTL(pipe);
3671 temp = I915_READ(reg);
6e266956 3672 if (HAS_PCH_CPT(dev_priv)) {
5e84e1a4
ZW
3673 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3674 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3675 } else {
3676 temp &= ~FDI_LINK_TRAIN_NONE;
3677 temp |= FDI_LINK_TRAIN_NONE;
3678 }
3679 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3680
3681 /* wait one idle pattern time */
3682 POSTING_READ(reg);
3683 udelay(1000);
357555c0
JB
3684
3685 /* IVB wants error correction enabled */
fd6b8f43 3686 if (IS_IVYBRIDGE(dev_priv))
357555c0
JB
3687 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3688 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
3689}
3690
8db9d77b
ZW
3691/* The FDI link training functions for ILK/Ibexpeak. */
3692static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3693{
3694 struct drm_device *dev = crtc->dev;
fac5e23e 3695 struct drm_i915_private *dev_priv = to_i915(dev);
8db9d77b
ZW
3696 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3697 int pipe = intel_crtc->pipe;
f0f59a00
VS
3698 i915_reg_t reg;
3699 u32 temp, tries;
8db9d77b 3700
1c8562f6 3701 /* FDI needs bits from pipe first */
0fc932b8 3702 assert_pipe_enabled(dev_priv, pipe);
0fc932b8 3703
e1a44743
AJ
3704 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3705 for train result */
5eddb70b
CW
3706 reg = FDI_RX_IMR(pipe);
3707 temp = I915_READ(reg);
e1a44743
AJ
3708 temp &= ~FDI_RX_SYMBOL_LOCK;
3709 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3710 I915_WRITE(reg, temp);
3711 I915_READ(reg);
e1a44743
AJ
3712 udelay(150);
3713
8db9d77b 3714 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3715 reg = FDI_TX_CTL(pipe);
3716 temp = I915_READ(reg);
627eb5a3 3717 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3718 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3719 temp &= ~FDI_LINK_TRAIN_NONE;
3720 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 3721 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3722
5eddb70b
CW
3723 reg = FDI_RX_CTL(pipe);
3724 temp = I915_READ(reg);
8db9d77b
ZW
3725 temp &= ~FDI_LINK_TRAIN_NONE;
3726 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b
CW
3727 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3728
3729 POSTING_READ(reg);
8db9d77b
ZW
3730 udelay(150);
3731
5b2adf89 3732 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
DV
3733 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3734 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3735 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 3736
5eddb70b 3737 reg = FDI_RX_IIR(pipe);
e1a44743 3738 for (tries = 0; tries < 5; tries++) {
5eddb70b 3739 temp = I915_READ(reg);
8db9d77b
ZW
3740 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3741
3742 if ((temp & FDI_RX_BIT_LOCK)) {
3743 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 3744 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
3745 break;
3746 }
8db9d77b 3747 }
e1a44743 3748 if (tries == 5)
5eddb70b 3749 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3750
3751 /* Train 2 */
5eddb70b
CW
3752 reg = FDI_TX_CTL(pipe);
3753 temp = I915_READ(reg);
8db9d77b
ZW
3754 temp &= ~FDI_LINK_TRAIN_NONE;
3755 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3756 I915_WRITE(reg, temp);
8db9d77b 3757
5eddb70b
CW
3758 reg = FDI_RX_CTL(pipe);
3759 temp = I915_READ(reg);
8db9d77b
ZW
3760 temp &= ~FDI_LINK_TRAIN_NONE;
3761 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3762 I915_WRITE(reg, temp);
8db9d77b 3763
5eddb70b
CW
3764 POSTING_READ(reg);
3765 udelay(150);
8db9d77b 3766
5eddb70b 3767 reg = FDI_RX_IIR(pipe);
e1a44743 3768 for (tries = 0; tries < 5; tries++) {
5eddb70b 3769 temp = I915_READ(reg);
8db9d77b
ZW
3770 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3771
3772 if (temp & FDI_RX_SYMBOL_LOCK) {
5eddb70b 3773 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
3774 DRM_DEBUG_KMS("FDI train 2 done.\n");
3775 break;
3776 }
8db9d77b 3777 }
e1a44743 3778 if (tries == 5)
5eddb70b 3779 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3780
3781 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 3782
8db9d77b
ZW
3783}
3784
0206e353 3785static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
3786 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3787 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3788 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3789 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3790};
3791
3792/* The FDI link training functions for SNB/Cougarpoint. */
3793static void gen6_fdi_link_train(struct drm_crtc *crtc)
3794{
3795 struct drm_device *dev = crtc->dev;
fac5e23e 3796 struct drm_i915_private *dev_priv = to_i915(dev);
8db9d77b
ZW
3797 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3798 int pipe = intel_crtc->pipe;
f0f59a00
VS
3799 i915_reg_t reg;
3800 u32 temp, i, retry;
8db9d77b 3801
e1a44743
AJ
3802 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3803 for train result */
5eddb70b
CW
3804 reg = FDI_RX_IMR(pipe);
3805 temp = I915_READ(reg);
e1a44743
AJ
3806 temp &= ~FDI_RX_SYMBOL_LOCK;
3807 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3808 I915_WRITE(reg, temp);
3809
3810 POSTING_READ(reg);
e1a44743
AJ
3811 udelay(150);
3812
8db9d77b 3813 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3814 reg = FDI_TX_CTL(pipe);
3815 temp = I915_READ(reg);
627eb5a3 3816 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3817 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3818 temp &= ~FDI_LINK_TRAIN_NONE;
3819 temp |= FDI_LINK_TRAIN_PATTERN_1;
3820 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3821 /* SNB-B */
3822 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 3823 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3824
d74cf324
DV
3825 I915_WRITE(FDI_RX_MISC(pipe),
3826 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3827
5eddb70b
CW
3828 reg = FDI_RX_CTL(pipe);
3829 temp = I915_READ(reg);
6e266956 3830 if (HAS_PCH_CPT(dev_priv)) {
8db9d77b
ZW
3831 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3832 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3833 } else {
3834 temp &= ~FDI_LINK_TRAIN_NONE;
3835 temp |= FDI_LINK_TRAIN_PATTERN_1;
3836 }
5eddb70b
CW
3837 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3838
3839 POSTING_READ(reg);
8db9d77b
ZW
3840 udelay(150);
3841
0206e353 3842 for (i = 0; i < 4; i++) {
5eddb70b
CW
3843 reg = FDI_TX_CTL(pipe);
3844 temp = I915_READ(reg);
8db9d77b
ZW
3845 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3846 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3847 I915_WRITE(reg, temp);
3848
3849 POSTING_READ(reg);
8db9d77b
ZW
3850 udelay(500);
3851
fa37d39e
SP
3852 for (retry = 0; retry < 5; retry++) {
3853 reg = FDI_RX_IIR(pipe);
3854 temp = I915_READ(reg);
3855 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3856 if (temp & FDI_RX_BIT_LOCK) {
3857 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3858 DRM_DEBUG_KMS("FDI train 1 done.\n");
3859 break;
3860 }
3861 udelay(50);
8db9d77b 3862 }
fa37d39e
SP
3863 if (retry < 5)
3864 break;
8db9d77b
ZW
3865 }
3866 if (i == 4)
5eddb70b 3867 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3868
3869 /* Train 2 */
5eddb70b
CW
3870 reg = FDI_TX_CTL(pipe);
3871 temp = I915_READ(reg);
8db9d77b
ZW
3872 temp &= ~FDI_LINK_TRAIN_NONE;
3873 temp |= FDI_LINK_TRAIN_PATTERN_2;
5db94019 3874 if (IS_GEN6(dev_priv)) {
8db9d77b
ZW
3875 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3876 /* SNB-B */
3877 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3878 }
5eddb70b 3879 I915_WRITE(reg, temp);
8db9d77b 3880
5eddb70b
CW
3881 reg = FDI_RX_CTL(pipe);
3882 temp = I915_READ(reg);
6e266956 3883 if (HAS_PCH_CPT(dev_priv)) {
8db9d77b
ZW
3884 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3885 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3886 } else {
3887 temp &= ~FDI_LINK_TRAIN_NONE;
3888 temp |= FDI_LINK_TRAIN_PATTERN_2;
3889 }
5eddb70b
CW
3890 I915_WRITE(reg, temp);
3891
3892 POSTING_READ(reg);
8db9d77b
ZW
3893 udelay(150);
3894
0206e353 3895 for (i = 0; i < 4; i++) {
5eddb70b
CW
3896 reg = FDI_TX_CTL(pipe);
3897 temp = I915_READ(reg);
8db9d77b
ZW
3898 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3899 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3900 I915_WRITE(reg, temp);
3901
3902 POSTING_READ(reg);
8db9d77b
ZW
3903 udelay(500);
3904
fa37d39e
SP
3905 for (retry = 0; retry < 5; retry++) {
3906 reg = FDI_RX_IIR(pipe);
3907 temp = I915_READ(reg);
3908 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3909 if (temp & FDI_RX_SYMBOL_LOCK) {
3910 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3911 DRM_DEBUG_KMS("FDI train 2 done.\n");
3912 break;
3913 }
3914 udelay(50);
8db9d77b 3915 }
fa37d39e
SP
3916 if (retry < 5)
3917 break;
8db9d77b
ZW
3918 }
3919 if (i == 4)
5eddb70b 3920 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3921
3922 DRM_DEBUG_KMS("FDI train done.\n");
3923}
3924
357555c0
JB
3925/* Manual link training for Ivy Bridge A0 parts */
3926static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3927{
3928 struct drm_device *dev = crtc->dev;
fac5e23e 3929 struct drm_i915_private *dev_priv = to_i915(dev);
357555c0
JB
3930 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3931 int pipe = intel_crtc->pipe;
f0f59a00
VS
3932 i915_reg_t reg;
3933 u32 temp, i, j;
357555c0
JB
3934
3935 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3936 for train result */
3937 reg = FDI_RX_IMR(pipe);
3938 temp = I915_READ(reg);
3939 temp &= ~FDI_RX_SYMBOL_LOCK;
3940 temp &= ~FDI_RX_BIT_LOCK;
3941 I915_WRITE(reg, temp);
3942
3943 POSTING_READ(reg);
3944 udelay(150);
3945
01a415fd
DV
3946 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3947 I915_READ(FDI_RX_IIR(pipe)));
3948
139ccd3f
JB
3949 /* Try each vswing and preemphasis setting twice before moving on */
3950 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3951 /* disable first in case we need to retry */
3952 reg = FDI_TX_CTL(pipe);
3953 temp = I915_READ(reg);
3954 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3955 temp &= ~FDI_TX_ENABLE;
3956 I915_WRITE(reg, temp);
357555c0 3957
139ccd3f
JB
3958 reg = FDI_RX_CTL(pipe);
3959 temp = I915_READ(reg);
3960 temp &= ~FDI_LINK_TRAIN_AUTO;
3961 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3962 temp &= ~FDI_RX_ENABLE;
3963 I915_WRITE(reg, temp);
357555c0 3964
139ccd3f 3965 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
3966 reg = FDI_TX_CTL(pipe);
3967 temp = I915_READ(reg);
139ccd3f 3968 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3969 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
139ccd3f 3970 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 3971 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
3972 temp |= snb_b_fdi_train_param[j/2];
3973 temp |= FDI_COMPOSITE_SYNC;
3974 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 3975
139ccd3f
JB
3976 I915_WRITE(FDI_RX_MISC(pipe),
3977 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 3978
139ccd3f 3979 reg = FDI_RX_CTL(pipe);
357555c0 3980 temp = I915_READ(reg);
139ccd3f
JB
3981 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3982 temp |= FDI_COMPOSITE_SYNC;
3983 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 3984
139ccd3f
JB
3985 POSTING_READ(reg);
3986 udelay(1); /* should be 0.5us */
357555c0 3987
139ccd3f
JB
3988 for (i = 0; i < 4; i++) {
3989 reg = FDI_RX_IIR(pipe);
3990 temp = I915_READ(reg);
3991 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3992
139ccd3f
JB
3993 if (temp & FDI_RX_BIT_LOCK ||
3994 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3995 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3996 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3997 i);
3998 break;
3999 }
4000 udelay(1); /* should be 0.5us */
4001 }
4002 if (i == 4) {
4003 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
4004 continue;
4005 }
357555c0 4006
139ccd3f 4007 /* Train 2 */
357555c0
JB
4008 reg = FDI_TX_CTL(pipe);
4009 temp = I915_READ(reg);
139ccd3f
JB
4010 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
4011 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
4012 I915_WRITE(reg, temp);
4013
4014 reg = FDI_RX_CTL(pipe);
4015 temp = I915_READ(reg);
4016 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4017 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
4018 I915_WRITE(reg, temp);
4019
4020 POSTING_READ(reg);
139ccd3f 4021 udelay(2); /* should be 1.5us */
357555c0 4022
139ccd3f
JB
4023 for (i = 0; i < 4; i++) {
4024 reg = FDI_RX_IIR(pipe);
4025 temp = I915_READ(reg);
4026 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 4027
139ccd3f
JB
4028 if (temp & FDI_RX_SYMBOL_LOCK ||
4029 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
4030 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
4031 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
4032 i);
4033 goto train_done;
4034 }
4035 udelay(2); /* should be 1.5us */
357555c0 4036 }
139ccd3f
JB
4037 if (i == 4)
4038 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 4039 }
357555c0 4040
139ccd3f 4041train_done:
357555c0
JB
4042 DRM_DEBUG_KMS("FDI train done.\n");
4043}
4044
88cefb6c 4045static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 4046{
88cefb6c 4047 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 4048 struct drm_i915_private *dev_priv = to_i915(dev);
2c07245f 4049 int pipe = intel_crtc->pipe;
f0f59a00
VS
4050 i915_reg_t reg;
4051 u32 temp;
c64e311e 4052
c98e9dcf 4053 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
4054 reg = FDI_RX_CTL(pipe);
4055 temp = I915_READ(reg);
627eb5a3 4056 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
6e3c9717 4057 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
dfd07d72 4058 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
4059 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
4060
4061 POSTING_READ(reg);
c98e9dcf
JB
4062 udelay(200);
4063
4064 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
4065 temp = I915_READ(reg);
4066 I915_WRITE(reg, temp | FDI_PCDCLK);
4067
4068 POSTING_READ(reg);
c98e9dcf
JB
4069 udelay(200);
4070
20749730
PZ
4071 /* Enable CPU FDI TX PLL, always on for Ironlake */
4072 reg = FDI_TX_CTL(pipe);
4073 temp = I915_READ(reg);
4074 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
4075 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 4076
20749730
PZ
4077 POSTING_READ(reg);
4078 udelay(100);
6be4a607 4079 }
0e23b99d
JB
4080}
4081
88cefb6c
DV
4082static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
4083{
4084 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 4085 struct drm_i915_private *dev_priv = to_i915(dev);
88cefb6c 4086 int pipe = intel_crtc->pipe;
f0f59a00
VS
4087 i915_reg_t reg;
4088 u32 temp;
88cefb6c
DV
4089
4090 /* Switch from PCDclk to Rawclk */
4091 reg = FDI_RX_CTL(pipe);
4092 temp = I915_READ(reg);
4093 I915_WRITE(reg, temp & ~FDI_PCDCLK);
4094
4095 /* Disable CPU FDI TX PLL */
4096 reg = FDI_TX_CTL(pipe);
4097 temp = I915_READ(reg);
4098 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
4099
4100 POSTING_READ(reg);
4101 udelay(100);
4102
4103 reg = FDI_RX_CTL(pipe);
4104 temp = I915_READ(reg);
4105 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
4106
4107 /* Wait for the clocks to turn off. */
4108 POSTING_READ(reg);
4109 udelay(100);
4110}
4111
0fc932b8
JB
4112static void ironlake_fdi_disable(struct drm_crtc *crtc)
4113{
4114 struct drm_device *dev = crtc->dev;
fac5e23e 4115 struct drm_i915_private *dev_priv = to_i915(dev);
0fc932b8
JB
4116 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4117 int pipe = intel_crtc->pipe;
f0f59a00
VS
4118 i915_reg_t reg;
4119 u32 temp;
0fc932b8
JB
4120
4121 /* disable CPU FDI tx and PCH FDI rx */
4122 reg = FDI_TX_CTL(pipe);
4123 temp = I915_READ(reg);
4124 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
4125 POSTING_READ(reg);
4126
4127 reg = FDI_RX_CTL(pipe);
4128 temp = I915_READ(reg);
4129 temp &= ~(0x7 << 16);
dfd07d72 4130 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
4131 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
4132
4133 POSTING_READ(reg);
4134 udelay(100);
4135
4136 /* Ironlake workaround, disable clock pointer after downing FDI */
6e266956 4137 if (HAS_PCH_IBX(dev_priv))
6f06ce18 4138 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
0fc932b8
JB
4139
4140 /* still set train pattern 1 */
4141 reg = FDI_TX_CTL(pipe);
4142 temp = I915_READ(reg);
4143 temp &= ~FDI_LINK_TRAIN_NONE;
4144 temp |= FDI_LINK_TRAIN_PATTERN_1;
4145 I915_WRITE(reg, temp);
4146
4147 reg = FDI_RX_CTL(pipe);
4148 temp = I915_READ(reg);
6e266956 4149 if (HAS_PCH_CPT(dev_priv)) {
0fc932b8
JB
4150 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4151 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
4152 } else {
4153 temp &= ~FDI_LINK_TRAIN_NONE;
4154 temp |= FDI_LINK_TRAIN_PATTERN_1;
4155 }
4156 /* BPC in FDI rx is consistent with that in PIPECONF */
4157 temp &= ~(0x07 << 16);
dfd07d72 4158 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
4159 I915_WRITE(reg, temp);
4160
4161 POSTING_READ(reg);
4162 udelay(100);
4163}
4164
49d73912 4165bool intel_has_pending_fb_unpin(struct drm_i915_private *dev_priv)
5dce5b93
CW
4166{
4167 struct intel_crtc *crtc;
4168
4169 /* Note that we don't need to be called with mode_config.lock here
4170 * as our list of CRTC objects is static for the lifetime of the
4171 * device and so cannot disappear as we iterate. Similarly, we can
4172 * happily treat the predicates as racy, atomic checks as userspace
4173 * cannot claim and pin a new fb without at least acquring the
4174 * struct_mutex and so serialising with us.
4175 */
49d73912 4176 for_each_intel_crtc(&dev_priv->drm, crtc) {
5dce5b93
CW
4177 if (atomic_read(&crtc->unpin_work_count) == 0)
4178 continue;
4179
5a21b665 4180 if (crtc->flip_work)
0f0f74bc 4181 intel_wait_for_vblank(dev_priv, crtc->pipe);
5dce5b93
CW
4182
4183 return true;
4184 }
4185
4186 return false;
4187}
4188
5a21b665 4189static void page_flip_completed(struct intel_crtc *intel_crtc)
d6bbafa1
CW
4190{
4191 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
5a21b665
DV
4192 struct intel_flip_work *work = intel_crtc->flip_work;
4193
4194 intel_crtc->flip_work = NULL;
d6bbafa1
CW
4195
4196 if (work->event)
560ce1dc 4197 drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
d6bbafa1
CW
4198
4199 drm_crtc_vblank_put(&intel_crtc->base);
4200
5a21b665 4201 wake_up_all(&dev_priv->pending_flip_queue);
5a21b665
DV
4202 trace_i915_flip_complete(intel_crtc->plane,
4203 work->pending_flip_obj);
05c41f92
AR
4204
4205 queue_work(dev_priv->wq, &work->unpin_work);
d6bbafa1
CW
4206}
4207
5008e874 4208static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
e6c3a2a6 4209{
0f91128d 4210 struct drm_device *dev = crtc->dev;
fac5e23e 4211 struct drm_i915_private *dev_priv = to_i915(dev);
5008e874 4212 long ret;
e6c3a2a6 4213
2c10d571 4214 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
5008e874
ML
4215
4216 ret = wait_event_interruptible_timeout(
4217 dev_priv->pending_flip_queue,
4218 !intel_crtc_has_pending_flip(crtc),
4219 60*HZ);
4220
4221 if (ret < 0)
4222 return ret;
4223
5a21b665
DV
4224 if (ret == 0) {
4225 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4226 struct intel_flip_work *work;
4227
4228 spin_lock_irq(&dev->event_lock);
4229 work = intel_crtc->flip_work;
4230 if (work && !is_mmio_work(work)) {
4231 WARN_ONCE(1, "Removing stuck page flip\n");
4232 page_flip_completed(intel_crtc);
4233 }
4234 spin_unlock_irq(&dev->event_lock);
4235 }
5bb61643 4236
5008e874 4237 return 0;
e6c3a2a6
CW
4238}
4239
b7076546 4240void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
060f02d8
VS
4241{
4242 u32 temp;
4243
4244 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
4245
4246 mutex_lock(&dev_priv->sb_lock);
4247
4248 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4249 temp |= SBI_SSCCTL_DISABLE;
4250 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
4251
4252 mutex_unlock(&dev_priv->sb_lock);
4253}
4254
e615efe4
ED
4255/* Program iCLKIP clock to the desired frequency */
4256static void lpt_program_iclkip(struct drm_crtc *crtc)
4257{
64b46a06 4258 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6e3c9717 4259 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
e615efe4
ED
4260 u32 divsel, phaseinc, auxdiv, phasedir = 0;
4261 u32 temp;
4262
060f02d8 4263 lpt_disable_iclkip(dev_priv);
e615efe4 4264
64b46a06
VS
4265 /* The iCLK virtual clock root frequency is in MHz,
4266 * but the adjusted_mode->crtc_clock in in KHz. To get the
4267 * divisors, it is necessary to divide one by another, so we
4268 * convert the virtual clock precision to KHz here for higher
4269 * precision.
4270 */
4271 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
e615efe4
ED
4272 u32 iclk_virtual_root_freq = 172800 * 1000;
4273 u32 iclk_pi_range = 64;
64b46a06 4274 u32 desired_divisor;
e615efe4 4275
64b46a06
VS
4276 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4277 clock << auxdiv);
4278 divsel = (desired_divisor / iclk_pi_range) - 2;
4279 phaseinc = desired_divisor % iclk_pi_range;
e615efe4 4280
64b46a06
VS
4281 /*
4282 * Near 20MHz is a corner case which is
4283 * out of range for the 7-bit divisor
4284 */
4285 if (divsel <= 0x7f)
4286 break;
e615efe4
ED
4287 }
4288
4289 /* This should not happen with any sane values */
4290 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
4291 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
4292 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
4293 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
4294
4295 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 4296 clock,
e615efe4
ED
4297 auxdiv,
4298 divsel,
4299 phasedir,
4300 phaseinc);
4301
060f02d8
VS
4302 mutex_lock(&dev_priv->sb_lock);
4303
e615efe4 4304 /* Program SSCDIVINTPHASE6 */
988d6ee8 4305 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
4306 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
4307 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
4308 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
4309 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
4310 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
4311 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 4312 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
4313
4314 /* Program SSCAUXDIV */
988d6ee8 4315 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
4316 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
4317 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 4318 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
4319
4320 /* Enable modulator and associated divider */
988d6ee8 4321 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 4322 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 4323 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4 4324
060f02d8
VS
4325 mutex_unlock(&dev_priv->sb_lock);
4326
e615efe4
ED
4327 /* Wait for initialization time */
4328 udelay(24);
4329
4330 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
4331}
4332
8802e5b6
VS
4333int lpt_get_iclkip(struct drm_i915_private *dev_priv)
4334{
4335 u32 divsel, phaseinc, auxdiv;
4336 u32 iclk_virtual_root_freq = 172800 * 1000;
4337 u32 iclk_pi_range = 64;
4338 u32 desired_divisor;
4339 u32 temp;
4340
4341 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
4342 return 0;
4343
4344 mutex_lock(&dev_priv->sb_lock);
4345
4346 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4347 if (temp & SBI_SSCCTL_DISABLE) {
4348 mutex_unlock(&dev_priv->sb_lock);
4349 return 0;
4350 }
4351
4352 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
4353 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
4354 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
4355 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
4356 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
4357
4358 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
4359 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
4360 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
4361
4362 mutex_unlock(&dev_priv->sb_lock);
4363
4364 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
4365
4366 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4367 desired_divisor << auxdiv);
4368}
4369
275f01b2
DV
4370static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4371 enum pipe pch_transcoder)
4372{
4373 struct drm_device *dev = crtc->base.dev;
fac5e23e 4374 struct drm_i915_private *dev_priv = to_i915(dev);
6e3c9717 4375 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
275f01b2
DV
4376
4377 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4378 I915_READ(HTOTAL(cpu_transcoder)));
4379 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4380 I915_READ(HBLANK(cpu_transcoder)));
4381 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4382 I915_READ(HSYNC(cpu_transcoder)));
4383
4384 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4385 I915_READ(VTOTAL(cpu_transcoder)));
4386 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4387 I915_READ(VBLANK(cpu_transcoder)));
4388 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4389 I915_READ(VSYNC(cpu_transcoder)));
4390 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4391 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4392}
4393
003632d9 4394static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
1fbc0d78 4395{
fac5e23e 4396 struct drm_i915_private *dev_priv = to_i915(dev);
1fbc0d78
DV
4397 uint32_t temp;
4398
4399 temp = I915_READ(SOUTH_CHICKEN1);
003632d9 4400 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
1fbc0d78
DV
4401 return;
4402
4403 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4404 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4405
003632d9
ACO
4406 temp &= ~FDI_BC_BIFURCATION_SELECT;
4407 if (enable)
4408 temp |= FDI_BC_BIFURCATION_SELECT;
4409
4410 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
1fbc0d78
DV
4411 I915_WRITE(SOUTH_CHICKEN1, temp);
4412 POSTING_READ(SOUTH_CHICKEN1);
4413}
4414
4415static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4416{
4417 struct drm_device *dev = intel_crtc->base.dev;
1fbc0d78
DV
4418
4419 switch (intel_crtc->pipe) {
4420 case PIPE_A:
4421 break;
4422 case PIPE_B:
6e3c9717 4423 if (intel_crtc->config->fdi_lanes > 2)
003632d9 4424 cpt_set_fdi_bc_bifurcation(dev, false);
1fbc0d78 4425 else
003632d9 4426 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4427
4428 break;
4429 case PIPE_C:
003632d9 4430 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4431
4432 break;
4433 default:
4434 BUG();
4435 }
4436}
4437
c48b5305
VS
4438/* Return which DP Port should be selected for Transcoder DP control */
4439static enum port
4440intel_trans_dp_port_sel(struct drm_crtc *crtc)
4441{
4442 struct drm_device *dev = crtc->dev;
4443 struct intel_encoder *encoder;
4444
4445 for_each_encoder_on_crtc(dev, crtc, encoder) {
cca0502b 4446 if (encoder->type == INTEL_OUTPUT_DP ||
c48b5305
VS
4447 encoder->type == INTEL_OUTPUT_EDP)
4448 return enc_to_dig_port(&encoder->base)->port;
4449 }
4450
4451 return -1;
4452}
4453
f67a559d
JB
4454/*
4455 * Enable PCH resources required for PCH ports:
4456 * - PCH PLLs
4457 * - FDI training & RX/TX
4458 * - update transcoder timings
4459 * - DP transcoding bits
4460 * - transcoder
4461 */
4462static void ironlake_pch_enable(struct drm_crtc *crtc)
0e23b99d
JB
4463{
4464 struct drm_device *dev = crtc->dev;
fac5e23e 4465 struct drm_i915_private *dev_priv = to_i915(dev);
0e23b99d
JB
4466 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4467 int pipe = intel_crtc->pipe;
f0f59a00 4468 u32 temp;
2c07245f 4469
ab9412ba 4470 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 4471
fd6b8f43 4472 if (IS_IVYBRIDGE(dev_priv))
1fbc0d78
DV
4473 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4474
cd986abb
DV
4475 /* Write the TU size bits before fdi link training, so that error
4476 * detection works. */
4477 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4478 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4479
c98e9dcf 4480 /* For PCH output, training FDI link */
674cf967 4481 dev_priv->display.fdi_link_train(crtc);
2c07245f 4482
3ad8a208
DV
4483 /* We need to program the right clock selection before writing the pixel
4484 * mutliplier into the DPLL. */
6e266956 4485 if (HAS_PCH_CPT(dev_priv)) {
ee7b9f93 4486 u32 sel;
4b645f14 4487
c98e9dcf 4488 temp = I915_READ(PCH_DPLL_SEL);
11887397
DV
4489 temp |= TRANS_DPLL_ENABLE(pipe);
4490 sel = TRANS_DPLLB_SEL(pipe);
8106ddbd
ACO
4491 if (intel_crtc->config->shared_dpll ==
4492 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
ee7b9f93
JB
4493 temp |= sel;
4494 else
4495 temp &= ~sel;
c98e9dcf 4496 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 4497 }
5eddb70b 4498
3ad8a208
DV
4499 /* XXX: pch pll's can be enabled any time before we enable the PCH
4500 * transcoder, and we actually should do this to not upset any PCH
4501 * transcoder that already use the clock when we share it.
4502 *
4503 * Note that enable_shared_dpll tries to do the right thing, but
4504 * get_shared_dpll unconditionally resets the pll - we need that to have
4505 * the right LVDS enable sequence. */
85b3894f 4506 intel_enable_shared_dpll(intel_crtc);
3ad8a208 4507
d9b6cb56
JB
4508 /* set transcoder timing, panel must allow it */
4509 assert_panel_unlocked(dev_priv, pipe);
275f01b2 4510 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
8db9d77b 4511
303b81e0 4512 intel_fdi_normal_train(crtc);
5e84e1a4 4513
c98e9dcf 4514 /* For PCH DP, enable TRANS_DP_CTL */
6e266956
TU
4515 if (HAS_PCH_CPT(dev_priv) &&
4516 intel_crtc_has_dp_encoder(intel_crtc->config)) {
9c4edaee
VS
4517 const struct drm_display_mode *adjusted_mode =
4518 &intel_crtc->config->base.adjusted_mode;
dfd07d72 4519 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
f0f59a00 4520 i915_reg_t reg = TRANS_DP_CTL(pipe);
5eddb70b
CW
4521 temp = I915_READ(reg);
4522 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
4523 TRANS_DP_SYNC_MASK |
4524 TRANS_DP_BPC_MASK);
e3ef4479 4525 temp |= TRANS_DP_OUTPUT_ENABLE;
9325c9f0 4526 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf 4527
9c4edaee 4528 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 4529 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
9c4edaee 4530 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 4531 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf
JB
4532
4533 switch (intel_trans_dp_port_sel(crtc)) {
c48b5305 4534 case PORT_B:
5eddb70b 4535 temp |= TRANS_DP_PORT_SEL_B;
c98e9dcf 4536 break;
c48b5305 4537 case PORT_C:
5eddb70b 4538 temp |= TRANS_DP_PORT_SEL_C;
c98e9dcf 4539 break;
c48b5305 4540 case PORT_D:
5eddb70b 4541 temp |= TRANS_DP_PORT_SEL_D;
c98e9dcf
JB
4542 break;
4543 default:
e95d41e1 4544 BUG();
32f9d658 4545 }
2c07245f 4546
5eddb70b 4547 I915_WRITE(reg, temp);
6be4a607 4548 }
b52eb4dc 4549
b8a4f404 4550 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
4551}
4552
1507e5bd
PZ
4553static void lpt_pch_enable(struct drm_crtc *crtc)
4554{
4555 struct drm_device *dev = crtc->dev;
fac5e23e 4556 struct drm_i915_private *dev_priv = to_i915(dev);
1507e5bd 4557 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 4558 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
1507e5bd 4559
ab9412ba 4560 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
1507e5bd 4561
8c52b5e8 4562 lpt_program_iclkip(crtc);
1507e5bd 4563
0540e488 4564 /* Set transcoder timing. */
275f01b2 4565 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
1507e5bd 4566
937bb610 4567 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
4568}
4569
a1520318 4570static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57 4571{
fac5e23e 4572 struct drm_i915_private *dev_priv = to_i915(dev);
f0f59a00 4573 i915_reg_t dslreg = PIPEDSL(pipe);
d4270e57
JB
4574 u32 temp;
4575
4576 temp = I915_READ(dslreg);
4577 udelay(500);
4578 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 4579 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 4580 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
4581 }
4582}
4583
86adf9d7
ML
4584static int
4585skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4586 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4587 int src_w, int src_h, int dst_w, int dst_h)
a1b2278e 4588{
86adf9d7
ML
4589 struct intel_crtc_scaler_state *scaler_state =
4590 &crtc_state->scaler_state;
4591 struct intel_crtc *intel_crtc =
4592 to_intel_crtc(crtc_state->base.crtc);
a1b2278e 4593 int need_scaling;
6156a456 4594
bd2ef25d 4595 need_scaling = drm_rotation_90_or_270(rotation) ?
6156a456
CK
4596 (src_h != dst_w || src_w != dst_h):
4597 (src_w != dst_w || src_h != dst_h);
a1b2278e
CK
4598
4599 /*
4600 * if plane is being disabled or scaler is no more required or force detach
4601 * - free scaler binded to this plane/crtc
4602 * - in order to do this, update crtc->scaler_usage
4603 *
4604 * Here scaler state in crtc_state is set free so that
4605 * scaler can be assigned to other user. Actual register
4606 * update to free the scaler is done in plane/panel-fit programming.
4607 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4608 */
86adf9d7 4609 if (force_detach || !need_scaling) {
a1b2278e 4610 if (*scaler_id >= 0) {
86adf9d7 4611 scaler_state->scaler_users &= ~(1 << scaler_user);
a1b2278e
CK
4612 scaler_state->scalers[*scaler_id].in_use = 0;
4613
86adf9d7
ML
4614 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4615 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4616 intel_crtc->pipe, scaler_user, *scaler_id,
a1b2278e
CK
4617 scaler_state->scaler_users);
4618 *scaler_id = -1;
4619 }
4620 return 0;
4621 }
4622
4623 /* range checks */
4624 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4625 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4626
4627 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4628 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
86adf9d7 4629 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
a1b2278e 4630 "size is out of scaler range\n",
86adf9d7 4631 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
a1b2278e
CK
4632 return -EINVAL;
4633 }
4634
86adf9d7
ML
4635 /* mark this plane as a scaler user in crtc_state */
4636 scaler_state->scaler_users |= (1 << scaler_user);
4637 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4638 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4639 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4640 scaler_state->scaler_users);
4641
4642 return 0;
4643}
4644
4645/**
4646 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4647 *
4648 * @state: crtc's scaler state
86adf9d7
ML
4649 *
4650 * Return
4651 * 0 - scaler_usage updated successfully
4652 * error - requested scaling cannot be supported or other error condition
4653 */
e435d6e5 4654int skl_update_scaler_crtc(struct intel_crtc_state *state)
86adf9d7 4655{
7c5f93b0 4656 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
86adf9d7 4657
e435d6e5 4658 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
31ad61e4 4659 &state->scaler_state.scaler_id, DRM_ROTATE_0,
86adf9d7 4660 state->pipe_src_w, state->pipe_src_h,
aad941d5 4661 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
86adf9d7
ML
4662}
4663
4664/**
4665 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4666 *
4667 * @state: crtc's scaler state
86adf9d7
ML
4668 * @plane_state: atomic plane state to update
4669 *
4670 * Return
4671 * 0 - scaler_usage updated successfully
4672 * error - requested scaling cannot be supported or other error condition
4673 */
da20eabd
ML
4674static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4675 struct intel_plane_state *plane_state)
86adf9d7
ML
4676{
4677
da20eabd
ML
4678 struct intel_plane *intel_plane =
4679 to_intel_plane(plane_state->base.plane);
86adf9d7
ML
4680 struct drm_framebuffer *fb = plane_state->base.fb;
4681 int ret;
4682
936e71e3 4683 bool force_detach = !fb || !plane_state->base.visible;
86adf9d7 4684
86adf9d7
ML
4685 ret = skl_update_scaler(crtc_state, force_detach,
4686 drm_plane_index(&intel_plane->base),
4687 &plane_state->scaler_id,
4688 plane_state->base.rotation,
936e71e3
VS
4689 drm_rect_width(&plane_state->base.src) >> 16,
4690 drm_rect_height(&plane_state->base.src) >> 16,
4691 drm_rect_width(&plane_state->base.dst),
4692 drm_rect_height(&plane_state->base.dst));
86adf9d7
ML
4693
4694 if (ret || plane_state->scaler_id < 0)
4695 return ret;
4696
a1b2278e 4697 /* check colorkey */
818ed961 4698 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
72660ce0
VS
4699 DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
4700 intel_plane->base.base.id,
4701 intel_plane->base.name);
a1b2278e
CK
4702 return -EINVAL;
4703 }
4704
4705 /* Check src format */
438b74a5 4706 switch (fb->format->format) {
86adf9d7
ML
4707 case DRM_FORMAT_RGB565:
4708 case DRM_FORMAT_XBGR8888:
4709 case DRM_FORMAT_XRGB8888:
4710 case DRM_FORMAT_ABGR8888:
4711 case DRM_FORMAT_ARGB8888:
4712 case DRM_FORMAT_XRGB2101010:
4713 case DRM_FORMAT_XBGR2101010:
4714 case DRM_FORMAT_YUYV:
4715 case DRM_FORMAT_YVYU:
4716 case DRM_FORMAT_UYVY:
4717 case DRM_FORMAT_VYUY:
4718 break;
4719 default:
72660ce0
VS
4720 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
4721 intel_plane->base.base.id, intel_plane->base.name,
438b74a5 4722 fb->base.id, fb->format->format);
86adf9d7 4723 return -EINVAL;
a1b2278e
CK
4724 }
4725
a1b2278e
CK
4726 return 0;
4727}
4728
e435d6e5
ML
4729static void skylake_scaler_disable(struct intel_crtc *crtc)
4730{
4731 int i;
4732
4733 for (i = 0; i < crtc->num_scalers; i++)
4734 skl_detach_scaler(crtc, i);
4735}
4736
4737static void skylake_pfit_enable(struct intel_crtc *crtc)
bd2e244f
JB
4738{
4739 struct drm_device *dev = crtc->base.dev;
fac5e23e 4740 struct drm_i915_private *dev_priv = to_i915(dev);
bd2e244f 4741 int pipe = crtc->pipe;
a1b2278e
CK
4742 struct intel_crtc_scaler_state *scaler_state =
4743 &crtc->config->scaler_state;
4744
4745 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4746
6e3c9717 4747 if (crtc->config->pch_pfit.enabled) {
a1b2278e
CK
4748 int id;
4749
4750 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4751 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4752 return;
4753 }
4754
4755 id = scaler_state->scaler_id;
4756 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4757 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4758 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4759 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4760
4761 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
bd2e244f
JB
4762 }
4763}
4764
b074cec8
JB
4765static void ironlake_pfit_enable(struct intel_crtc *crtc)
4766{
4767 struct drm_device *dev = crtc->base.dev;
fac5e23e 4768 struct drm_i915_private *dev_priv = to_i915(dev);
b074cec8
JB
4769 int pipe = crtc->pipe;
4770
6e3c9717 4771 if (crtc->config->pch_pfit.enabled) {
b074cec8
JB
4772 /* Force use of hard-coded filter coefficients
4773 * as some pre-programmed values are broken,
4774 * e.g. x201.
4775 */
fd6b8f43 4776 if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
b074cec8
JB
4777 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4778 PF_PIPE_SEL_IVB(pipe));
4779 else
4780 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
6e3c9717
ACO
4781 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4782 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
d4270e57
JB
4783 }
4784}
4785
20bc8673 4786void hsw_enable_ips(struct intel_crtc *crtc)
d77e4531 4787{
cea165c3 4788 struct drm_device *dev = crtc->base.dev;
fac5e23e 4789 struct drm_i915_private *dev_priv = to_i915(dev);
d77e4531 4790
6e3c9717 4791 if (!crtc->config->ips_enabled)
d77e4531
PZ
4792 return;
4793
307e4498
ML
4794 /*
4795 * We can only enable IPS after we enable a plane and wait for a vblank
4796 * This function is called from post_plane_update, which is run after
4797 * a vblank wait.
4798 */
cea165c3 4799
d77e4531 4800 assert_plane_enabled(dev_priv, crtc->plane);
8652744b 4801 if (IS_BROADWELL(dev_priv)) {
2a114cc1
BW
4802 mutex_lock(&dev_priv->rps.hw_lock);
4803 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4804 mutex_unlock(&dev_priv->rps.hw_lock);
4805 /* Quoting Art Runyan: "its not safe to expect any particular
4806 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
4807 * mailbox." Moreover, the mailbox may return a bogus state,
4808 * so we need to just enable it and continue on.
2a114cc1
BW
4809 */
4810 } else {
4811 I915_WRITE(IPS_CTL, IPS_ENABLE);
4812 /* The bit only becomes 1 in the next vblank, so this wait here
4813 * is essentially intel_wait_for_vblank. If we don't have this
4814 * and don't wait for vblanks until the end of crtc_enable, then
4815 * the HW state readout code will complain that the expected
4816 * IPS_CTL value is not the one we read. */
2ec9ba3c
CW
4817 if (intel_wait_for_register(dev_priv,
4818 IPS_CTL, IPS_ENABLE, IPS_ENABLE,
4819 50))
2a114cc1
BW
4820 DRM_ERROR("Timed out waiting for IPS enable\n");
4821 }
d77e4531
PZ
4822}
4823
20bc8673 4824void hsw_disable_ips(struct intel_crtc *crtc)
d77e4531
PZ
4825{
4826 struct drm_device *dev = crtc->base.dev;
fac5e23e 4827 struct drm_i915_private *dev_priv = to_i915(dev);
d77e4531 4828
6e3c9717 4829 if (!crtc->config->ips_enabled)
d77e4531
PZ
4830 return;
4831
4832 assert_plane_enabled(dev_priv, crtc->plane);
8652744b 4833 if (IS_BROADWELL(dev_priv)) {
2a114cc1
BW
4834 mutex_lock(&dev_priv->rps.hw_lock);
4835 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4836 mutex_unlock(&dev_priv->rps.hw_lock);
23d0b130 4837 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
b85c1ecf
CW
4838 if (intel_wait_for_register(dev_priv,
4839 IPS_CTL, IPS_ENABLE, 0,
4840 42))
23d0b130 4841 DRM_ERROR("Timed out waiting for IPS disable\n");
e59150dc 4842 } else {
2a114cc1 4843 I915_WRITE(IPS_CTL, 0);
e59150dc
JB
4844 POSTING_READ(IPS_CTL);
4845 }
d77e4531
PZ
4846
4847 /* We need to wait for a vblank before we can disable the plane. */
0f0f74bc 4848 intel_wait_for_vblank(dev_priv, crtc->pipe);
d77e4531
PZ
4849}
4850
7cac945f 4851static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
d3eedb1a 4852{
7cac945f 4853 if (intel_crtc->overlay) {
d3eedb1a 4854 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 4855 struct drm_i915_private *dev_priv = to_i915(dev);
d3eedb1a
VS
4856
4857 mutex_lock(&dev->struct_mutex);
4858 dev_priv->mm.interruptible = false;
4859 (void) intel_overlay_switch_off(intel_crtc->overlay);
4860 dev_priv->mm.interruptible = true;
4861 mutex_unlock(&dev->struct_mutex);
4862 }
4863
4864 /* Let userspace switch the overlay on again. In most cases userspace
4865 * has to recompute where to put it anyway.
4866 */
4867}
4868
87d4300a
ML
4869/**
4870 * intel_post_enable_primary - Perform operations after enabling primary plane
4871 * @crtc: the CRTC whose primary plane was just enabled
4872 *
4873 * Performs potentially sleeping operations that must be done after the primary
4874 * plane is enabled, such as updating FBC and IPS. Note that this may be
4875 * called due to an explicit primary plane update, or due to an implicit
4876 * re-enable that is caused when a sprite plane is updated to no longer
4877 * completely hide the primary plane.
4878 */
4879static void
4880intel_post_enable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4881{
4882 struct drm_device *dev = crtc->dev;
fac5e23e 4883 struct drm_i915_private *dev_priv = to_i915(dev);
a5c4d7bc
VS
4884 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4885 int pipe = intel_crtc->pipe;
a5c4d7bc 4886
87d4300a
ML
4887 /*
4888 * FIXME IPS should be fine as long as one plane is
4889 * enabled, but in practice it seems to have problems
4890 * when going from primary only to sprite only and vice
4891 * versa.
4892 */
a5c4d7bc
VS
4893 hsw_enable_ips(intel_crtc);
4894
f99d7069 4895 /*
87d4300a
ML
4896 * Gen2 reports pipe underruns whenever all planes are disabled.
4897 * So don't enable underrun reporting before at least some planes
4898 * are enabled.
4899 * FIXME: Need to fix the logic to work when we turn off all planes
4900 * but leave the pipe running.
f99d7069 4901 */
5db94019 4902 if (IS_GEN2(dev_priv))
87d4300a
ML
4903 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4904
aca7b684
VS
4905 /* Underruns don't always raise interrupts, so check manually. */
4906 intel_check_cpu_fifo_underruns(dev_priv);
4907 intel_check_pch_fifo_underruns(dev_priv);
a5c4d7bc
VS
4908}
4909
2622a081 4910/* FIXME move all this to pre_plane_update() with proper state tracking */
87d4300a
ML
4911static void
4912intel_pre_disable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4913{
4914 struct drm_device *dev = crtc->dev;
fac5e23e 4915 struct drm_i915_private *dev_priv = to_i915(dev);
a5c4d7bc
VS
4916 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4917 int pipe = intel_crtc->pipe;
a5c4d7bc 4918
87d4300a
ML
4919 /*
4920 * Gen2 reports pipe underruns whenever all planes are disabled.
4921 * So diasble underrun reporting before all the planes get disabled.
4922 * FIXME: Need to fix the logic to work when we turn off all planes
4923 * but leave the pipe running.
4924 */
5db94019 4925 if (IS_GEN2(dev_priv))
87d4300a 4926 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
a5c4d7bc 4927
2622a081
VS
4928 /*
4929 * FIXME IPS should be fine as long as one plane is
4930 * enabled, but in practice it seems to have problems
4931 * when going from primary only to sprite only and vice
4932 * versa.
4933 */
4934 hsw_disable_ips(intel_crtc);
4935}
4936
4937/* FIXME get rid of this and use pre_plane_update */
4938static void
4939intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
4940{
4941 struct drm_device *dev = crtc->dev;
fac5e23e 4942 struct drm_i915_private *dev_priv = to_i915(dev);
2622a081
VS
4943 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4944 int pipe = intel_crtc->pipe;
4945
4946 intel_pre_disable_primary(crtc);
4947
87d4300a
ML
4948 /*
4949 * Vblank time updates from the shadow to live plane control register
4950 * are blocked if the memory self-refresh mode is active at that
4951 * moment. So to make sure the plane gets truly disabled, disable
4952 * first the self-refresh mode. The self-refresh enable bit in turn
4953 * will be checked/applied by the HW only at the next frame start
4954 * event which is after the vblank start event, so we need to have a
4955 * wait-for-vblank between disabling the plane and the pipe.
4956 */
11a85d6a
VS
4957 if (HAS_GMCH_DISPLAY(dev_priv) &&
4958 intel_set_memory_cxsr(dev_priv, false))
0f0f74bc 4959 intel_wait_for_vblank(dev_priv, pipe);
87d4300a
ML
4960}
4961
5a21b665
DV
4962static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
4963{
4964 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
4965 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4966 struct intel_crtc_state *pipe_config =
4967 to_intel_crtc_state(crtc->base.state);
5a21b665
DV
4968 struct drm_plane *primary = crtc->base.primary;
4969 struct drm_plane_state *old_pri_state =
4970 drm_atomic_get_existing_plane_state(old_state, primary);
4971
5748b6a1 4972 intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
5a21b665
DV
4973
4974 crtc->wm.cxsr_allowed = true;
4975
4976 if (pipe_config->update_wm_post && pipe_config->base.active)
432081bc 4977 intel_update_watermarks(crtc);
5a21b665
DV
4978
4979 if (old_pri_state) {
4980 struct intel_plane_state *primary_state =
4981 to_intel_plane_state(primary->state);
4982 struct intel_plane_state *old_primary_state =
4983 to_intel_plane_state(old_pri_state);
4984
4985 intel_fbc_post_update(crtc);
4986
936e71e3 4987 if (primary_state->base.visible &&
5a21b665 4988 (needs_modeset(&pipe_config->base) ||
936e71e3 4989 !old_primary_state->base.visible))
5a21b665
DV
4990 intel_post_enable_primary(&crtc->base);
4991 }
4992}
4993
5c74cd73 4994static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
ac21b225 4995{
5c74cd73 4996 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
ac21b225 4997 struct drm_device *dev = crtc->base.dev;
fac5e23e 4998 struct drm_i915_private *dev_priv = to_i915(dev);
ab1d3a0e
ML
4999 struct intel_crtc_state *pipe_config =
5000 to_intel_crtc_state(crtc->base.state);
5c74cd73
ML
5001 struct drm_atomic_state *old_state = old_crtc_state->base.state;
5002 struct drm_plane *primary = crtc->base.primary;
5003 struct drm_plane_state *old_pri_state =
5004 drm_atomic_get_existing_plane_state(old_state, primary);
5005 bool modeset = needs_modeset(&pipe_config->base);
ccf010fb
ML
5006 struct intel_atomic_state *old_intel_state =
5007 to_intel_atomic_state(old_state);
ac21b225 5008
5c74cd73
ML
5009 if (old_pri_state) {
5010 struct intel_plane_state *primary_state =
5011 to_intel_plane_state(primary->state);
5012 struct intel_plane_state *old_primary_state =
5013 to_intel_plane_state(old_pri_state);
5014
faf68d92 5015 intel_fbc_pre_update(crtc, pipe_config, primary_state);
31ae71fc 5016
936e71e3
VS
5017 if (old_primary_state->base.visible &&
5018 (modeset || !primary_state->base.visible))
5c74cd73
ML
5019 intel_pre_disable_primary(&crtc->base);
5020 }
852eb00d 5021
49cff963 5022 if (pipe_config->disable_cxsr && HAS_GMCH_DISPLAY(dev_priv)) {
852eb00d 5023 crtc->wm.cxsr_allowed = false;
2dfd178d 5024
2622a081
VS
5025 /*
5026 * Vblank time updates from the shadow to live plane control register
5027 * are blocked if the memory self-refresh mode is active at that
5028 * moment. So to make sure the plane gets truly disabled, disable
5029 * first the self-refresh mode. The self-refresh enable bit in turn
5030 * will be checked/applied by the HW only at the next frame start
5031 * event which is after the vblank start event, so we need to have a
5032 * wait-for-vblank between disabling the plane and the pipe.
5033 */
11a85d6a
VS
5034 if (old_crtc_state->base.active &&
5035 intel_set_memory_cxsr(dev_priv, false))
0f0f74bc 5036 intel_wait_for_vblank(dev_priv, crtc->pipe);
852eb00d 5037 }
92826fcd 5038
ed4a6a7c
MR
5039 /*
5040 * IVB workaround: must disable low power watermarks for at least
5041 * one frame before enabling scaling. LP watermarks can be re-enabled
5042 * when scaling is disabled.
5043 *
5044 * WaCxSRDisabledForSpriteScaling:ivb
5045 */
ddd2b792 5046 if (pipe_config->disable_lp_wm && ilk_disable_lp_wm(dev))
0f0f74bc 5047 intel_wait_for_vblank(dev_priv, crtc->pipe);
ed4a6a7c
MR
5048
5049 /*
5050 * If we're doing a modeset, we're done. No need to do any pre-vblank
5051 * watermark programming here.
5052 */
5053 if (needs_modeset(&pipe_config->base))
5054 return;
5055
5056 /*
5057 * For platforms that support atomic watermarks, program the
5058 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
5059 * will be the intermediate values that are safe for both pre- and
5060 * post- vblank; when vblank happens, the 'active' values will be set
5061 * to the final 'target' values and we'll do this again to get the
5062 * optimal watermarks. For gen9+ platforms, the values we program here
5063 * will be the final target values which will get automatically latched
5064 * at vblank time; no further programming will be necessary.
5065 *
5066 * If a platform hasn't been transitioned to atomic watermarks yet,
5067 * we'll continue to update watermarks the old way, if flags tell
5068 * us to.
5069 */
5070 if (dev_priv->display.initial_watermarks != NULL)
ccf010fb
ML
5071 dev_priv->display.initial_watermarks(old_intel_state,
5072 pipe_config);
caed361d 5073 else if (pipe_config->update_wm_pre)
432081bc 5074 intel_update_watermarks(crtc);
ac21b225
ML
5075}
5076
d032ffa0 5077static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
87d4300a
ML
5078{
5079 struct drm_device *dev = crtc->dev;
5080 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
d032ffa0 5081 struct drm_plane *p;
87d4300a
ML
5082 int pipe = intel_crtc->pipe;
5083
7cac945f 5084 intel_crtc_dpms_overlay_disable(intel_crtc);
27321ae8 5085
d032ffa0
ML
5086 drm_for_each_plane_mask(p, dev, plane_mask)
5087 to_intel_plane(p)->disable_plane(p, crtc);
f98551ae 5088
f99d7069
DV
5089 /*
5090 * FIXME: Once we grow proper nuclear flip support out of this we need
5091 * to compute the mask of flip planes precisely. For the time being
5092 * consider this a flip to a NULL plane.
5093 */
5748b6a1 5094 intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
5095}
5096
fb1c98b1 5097static void intel_encoders_pre_pll_enable(struct drm_crtc *crtc,
fd6bbda9 5098 struct intel_crtc_state *crtc_state,
fb1c98b1
ML
5099 struct drm_atomic_state *old_state)
5100{
5101 struct drm_connector_state *old_conn_state;
5102 struct drm_connector *conn;
5103 int i;
5104
5105 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5106 struct drm_connector_state *conn_state = conn->state;
5107 struct intel_encoder *encoder =
5108 to_intel_encoder(conn_state->best_encoder);
5109
5110 if (conn_state->crtc != crtc)
5111 continue;
5112
5113 if (encoder->pre_pll_enable)
fd6bbda9 5114 encoder->pre_pll_enable(encoder, crtc_state, conn_state);
fb1c98b1
ML
5115 }
5116}
5117
5118static void intel_encoders_pre_enable(struct drm_crtc *crtc,
fd6bbda9 5119 struct intel_crtc_state *crtc_state,
fb1c98b1
ML
5120 struct drm_atomic_state *old_state)
5121{
5122 struct drm_connector_state *old_conn_state;
5123 struct drm_connector *conn;
5124 int i;
5125
5126 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5127 struct drm_connector_state *conn_state = conn->state;
5128 struct intel_encoder *encoder =
5129 to_intel_encoder(conn_state->best_encoder);
5130
5131 if (conn_state->crtc != crtc)
5132 continue;
5133
5134 if (encoder->pre_enable)
fd6bbda9 5135 encoder->pre_enable(encoder, crtc_state, conn_state);
fb1c98b1
ML
5136 }
5137}
5138
5139static void intel_encoders_enable(struct drm_crtc *crtc,
fd6bbda9 5140 struct intel_crtc_state *crtc_state,
fb1c98b1
ML
5141 struct drm_atomic_state *old_state)
5142{
5143 struct drm_connector_state *old_conn_state;
5144 struct drm_connector *conn;
5145 int i;
5146
5147 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5148 struct drm_connector_state *conn_state = conn->state;
5149 struct intel_encoder *encoder =
5150 to_intel_encoder(conn_state->best_encoder);
5151
5152 if (conn_state->crtc != crtc)
5153 continue;
5154
fd6bbda9 5155 encoder->enable(encoder, crtc_state, conn_state);
fb1c98b1
ML
5156 intel_opregion_notify_encoder(encoder, true);
5157 }
5158}
5159
5160static void intel_encoders_disable(struct drm_crtc *crtc,
fd6bbda9 5161 struct intel_crtc_state *old_crtc_state,
fb1c98b1
ML
5162 struct drm_atomic_state *old_state)
5163{
5164 struct drm_connector_state *old_conn_state;
5165 struct drm_connector *conn;
5166 int i;
5167
5168 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5169 struct intel_encoder *encoder =
5170 to_intel_encoder(old_conn_state->best_encoder);
5171
5172 if (old_conn_state->crtc != crtc)
5173 continue;
5174
5175 intel_opregion_notify_encoder(encoder, false);
fd6bbda9 5176 encoder->disable(encoder, old_crtc_state, old_conn_state);
fb1c98b1
ML
5177 }
5178}
5179
5180static void intel_encoders_post_disable(struct drm_crtc *crtc,
fd6bbda9 5181 struct intel_crtc_state *old_crtc_state,
fb1c98b1
ML
5182 struct drm_atomic_state *old_state)
5183{
5184 struct drm_connector_state *old_conn_state;
5185 struct drm_connector *conn;
5186 int i;
5187
5188 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5189 struct intel_encoder *encoder =
5190 to_intel_encoder(old_conn_state->best_encoder);
5191
5192 if (old_conn_state->crtc != crtc)
5193 continue;
5194
5195 if (encoder->post_disable)
fd6bbda9 5196 encoder->post_disable(encoder, old_crtc_state, old_conn_state);
fb1c98b1
ML
5197 }
5198}
5199
5200static void intel_encoders_post_pll_disable(struct drm_crtc *crtc,
fd6bbda9 5201 struct intel_crtc_state *old_crtc_state,
fb1c98b1
ML
5202 struct drm_atomic_state *old_state)
5203{
5204 struct drm_connector_state *old_conn_state;
5205 struct drm_connector *conn;
5206 int i;
5207
5208 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5209 struct intel_encoder *encoder =
5210 to_intel_encoder(old_conn_state->best_encoder);
5211
5212 if (old_conn_state->crtc != crtc)
5213 continue;
5214
5215 if (encoder->post_pll_disable)
fd6bbda9 5216 encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
fb1c98b1
ML
5217 }
5218}
5219
4a806558
ML
5220static void ironlake_crtc_enable(struct intel_crtc_state *pipe_config,
5221 struct drm_atomic_state *old_state)
f67a559d 5222{
4a806558 5223 struct drm_crtc *crtc = pipe_config->base.crtc;
f67a559d 5224 struct drm_device *dev = crtc->dev;
fac5e23e 5225 struct drm_i915_private *dev_priv = to_i915(dev);
f67a559d
JB
5226 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5227 int pipe = intel_crtc->pipe;
ccf010fb
ML
5228 struct intel_atomic_state *old_intel_state =
5229 to_intel_atomic_state(old_state);
f67a559d 5230
53d9f4e9 5231 if (WARN_ON(intel_crtc->active))
f67a559d
JB
5232 return;
5233
b2c0593a
VS
5234 /*
5235 * Sometimes spurious CPU pipe underruns happen during FDI
5236 * training, at least with VGA+HDMI cloning. Suppress them.
5237 *
5238 * On ILK we get an occasional spurious CPU pipe underruns
5239 * between eDP port A enable and vdd enable. Also PCH port
5240 * enable seems to result in the occasional CPU pipe underrun.
5241 *
5242 * Spurious PCH underruns also occur during PCH enabling.
5243 */
5244 if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
5245 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
81b088ca
VS
5246 if (intel_crtc->config->has_pch_encoder)
5247 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
5248
6e3c9717 5249 if (intel_crtc->config->has_pch_encoder)
b14b1055
DV
5250 intel_prepare_shared_dpll(intel_crtc);
5251
37a5650b 5252 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 5253 intel_dp_set_m_n(intel_crtc, M1_N1);
29407aab
DV
5254
5255 intel_set_pipe_timings(intel_crtc);
bc58be60 5256 intel_set_pipe_src_size(intel_crtc);
29407aab 5257
6e3c9717 5258 if (intel_crtc->config->has_pch_encoder) {
29407aab 5259 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 5260 &intel_crtc->config->fdi_m_n, NULL);
29407aab
DV
5261 }
5262
5263 ironlake_set_pipeconf(crtc);
5264
f67a559d 5265 intel_crtc->active = true;
8664281b 5266
fd6bbda9 5267 intel_encoders_pre_enable(crtc, pipe_config, old_state);
f67a559d 5268
6e3c9717 5269 if (intel_crtc->config->has_pch_encoder) {
fff367c7
DV
5270 /* Note: FDI PLL enabling _must_ be done before we enable the
5271 * cpu pipes, hence this is separate from all the other fdi/pch
5272 * enabling. */
88cefb6c 5273 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
DV
5274 } else {
5275 assert_fdi_tx_disabled(dev_priv, pipe);
5276 assert_fdi_rx_disabled(dev_priv, pipe);
5277 }
f67a559d 5278
b074cec8 5279 ironlake_pfit_enable(intel_crtc);
f67a559d 5280
9c54c0dd
JB
5281 /*
5282 * On ILK+ LUT must be loaded before the pipe is running but with
5283 * clocks enabled
5284 */
b95c5321 5285 intel_color_load_luts(&pipe_config->base);
9c54c0dd 5286
1d5bf5d9 5287 if (dev_priv->display.initial_watermarks != NULL)
ccf010fb 5288 dev_priv->display.initial_watermarks(old_intel_state, intel_crtc->config);
e1fdc473 5289 intel_enable_pipe(intel_crtc);
f67a559d 5290
6e3c9717 5291 if (intel_crtc->config->has_pch_encoder)
f67a559d 5292 ironlake_pch_enable(crtc);
c98e9dcf 5293
f9b61ff6
DV
5294 assert_vblank_disabled(crtc);
5295 drm_crtc_vblank_on(crtc);
5296
fd6bbda9 5297 intel_encoders_enable(crtc, pipe_config, old_state);
61b77ddd 5298
6e266956 5299 if (HAS_PCH_CPT(dev_priv))
a1520318 5300 cpt_verify_modeset(dev, intel_crtc->pipe);
37ca8d4c
VS
5301
5302 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
5303 if (intel_crtc->config->has_pch_encoder)
0f0f74bc 5304 intel_wait_for_vblank(dev_priv, pipe);
b2c0593a 5305 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
37ca8d4c 5306 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607
JB
5307}
5308
42db64ef
PZ
5309/* IPS only exists on ULT machines and is tied to pipe A. */
5310static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
5311{
50a0bc90 5312 return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
42db64ef
PZ
5313}
5314
4a806558
ML
5315static void haswell_crtc_enable(struct intel_crtc_state *pipe_config,
5316 struct drm_atomic_state *old_state)
4f771f10 5317{
4a806558 5318 struct drm_crtc *crtc = pipe_config->base.crtc;
6315b5d3 5319 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
4f771f10 5320 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
99d736a2 5321 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
4d1de975 5322 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ccf010fb
ML
5323 struct intel_atomic_state *old_intel_state =
5324 to_intel_atomic_state(old_state);
4f771f10 5325
53d9f4e9 5326 if (WARN_ON(intel_crtc->active))
4f771f10
PZ
5327 return;
5328
81b088ca
VS
5329 if (intel_crtc->config->has_pch_encoder)
5330 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5331 false);
5332
fd6bbda9 5333 intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
95a7a2ae 5334
8106ddbd 5335 if (intel_crtc->config->shared_dpll)
df8ad70c
DV
5336 intel_enable_shared_dpll(intel_crtc);
5337
37a5650b 5338 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 5339 intel_dp_set_m_n(intel_crtc, M1_N1);
229fca97 5340
d7edc4e5 5341 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975
JN
5342 intel_set_pipe_timings(intel_crtc);
5343
bc58be60 5344 intel_set_pipe_src_size(intel_crtc);
229fca97 5345
4d1de975
JN
5346 if (cpu_transcoder != TRANSCODER_EDP &&
5347 !transcoder_is_dsi(cpu_transcoder)) {
5348 I915_WRITE(PIPE_MULT(cpu_transcoder),
6e3c9717 5349 intel_crtc->config->pixel_multiplier - 1);
ebb69c95
CT
5350 }
5351
6e3c9717 5352 if (intel_crtc->config->has_pch_encoder) {
229fca97 5353 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 5354 &intel_crtc->config->fdi_m_n, NULL);
229fca97
DV
5355 }
5356
d7edc4e5 5357 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975
JN
5358 haswell_set_pipeconf(crtc);
5359
391bf048 5360 haswell_set_pipemisc(crtc);
229fca97 5361
b95c5321 5362 intel_color_set_csc(&pipe_config->base);
229fca97 5363
4f771f10 5364 intel_crtc->active = true;
8664281b 5365
6b698516
DV
5366 if (intel_crtc->config->has_pch_encoder)
5367 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
5368 else
5369 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5370
fd6bbda9 5371 intel_encoders_pre_enable(crtc, pipe_config, old_state);
4f771f10 5372
d2d65408 5373 if (intel_crtc->config->has_pch_encoder)
4fe9467d 5374 dev_priv->display.fdi_link_train(crtc);
4fe9467d 5375
d7edc4e5 5376 if (!transcoder_is_dsi(cpu_transcoder))
7d4aefd0 5377 intel_ddi_enable_pipe_clock(intel_crtc);
4f771f10 5378
6315b5d3 5379 if (INTEL_GEN(dev_priv) >= 9)
e435d6e5 5380 skylake_pfit_enable(intel_crtc);
ff6d9f55 5381 else
1c132b44 5382 ironlake_pfit_enable(intel_crtc);
4f771f10
PZ
5383
5384 /*
5385 * On ILK+ LUT must be loaded before the pipe is running but with
5386 * clocks enabled
5387 */
b95c5321 5388 intel_color_load_luts(&pipe_config->base);
4f771f10 5389
1f544388 5390 intel_ddi_set_pipe_settings(crtc);
d7edc4e5 5391 if (!transcoder_is_dsi(cpu_transcoder))
7d4aefd0 5392 intel_ddi_enable_transcoder_func(crtc);
4f771f10 5393
1d5bf5d9 5394 if (dev_priv->display.initial_watermarks != NULL)
3125d39f 5395 dev_priv->display.initial_watermarks(old_intel_state, pipe_config);
4d1de975
JN
5396
5397 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
d7edc4e5 5398 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975 5399 intel_enable_pipe(intel_crtc);
42db64ef 5400
6e3c9717 5401 if (intel_crtc->config->has_pch_encoder)
1507e5bd 5402 lpt_pch_enable(crtc);
4f771f10 5403
0037071d 5404 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
0e32b39c
DA
5405 intel_ddi_set_vc_payload_alloc(crtc, true);
5406
f9b61ff6
DV
5407 assert_vblank_disabled(crtc);
5408 drm_crtc_vblank_on(crtc);
5409
fd6bbda9 5410 intel_encoders_enable(crtc, pipe_config, old_state);
4f771f10 5411
6b698516 5412 if (intel_crtc->config->has_pch_encoder) {
0f0f74bc
VS
5413 intel_wait_for_vblank(dev_priv, pipe);
5414 intel_wait_for_vblank(dev_priv, pipe);
6b698516 5415 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
d2d65408
VS
5416 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5417 true);
6b698516 5418 }
d2d65408 5419
e4916946
PZ
5420 /* If we change the relative order between pipe/planes enabling, we need
5421 * to change the workaround. */
99d736a2 5422 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
772c2a51 5423 if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
0f0f74bc
VS
5424 intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
5425 intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
99d736a2 5426 }
4f771f10
PZ
5427}
5428
bfd16b2a 5429static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
3f8dce3a
DV
5430{
5431 struct drm_device *dev = crtc->base.dev;
fac5e23e 5432 struct drm_i915_private *dev_priv = to_i915(dev);
3f8dce3a
DV
5433 int pipe = crtc->pipe;
5434
5435 /* To avoid upsetting the power well on haswell only disable the pfit if
5436 * it's in use. The hw state code will make sure we get this right. */
bfd16b2a 5437 if (force || crtc->config->pch_pfit.enabled) {
3f8dce3a
DV
5438 I915_WRITE(PF_CTL(pipe), 0);
5439 I915_WRITE(PF_WIN_POS(pipe), 0);
5440 I915_WRITE(PF_WIN_SZ(pipe), 0);
5441 }
5442}
5443
4a806558
ML
5444static void ironlake_crtc_disable(struct intel_crtc_state *old_crtc_state,
5445 struct drm_atomic_state *old_state)
6be4a607 5446{
4a806558 5447 struct drm_crtc *crtc = old_crtc_state->base.crtc;
6be4a607 5448 struct drm_device *dev = crtc->dev;
fac5e23e 5449 struct drm_i915_private *dev_priv = to_i915(dev);
6be4a607
JB
5450 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5451 int pipe = intel_crtc->pipe;
b52eb4dc 5452
b2c0593a
VS
5453 /*
5454 * Sometimes spurious CPU pipe underruns happen when the
5455 * pipe is already disabled, but FDI RX/TX is still enabled.
5456 * Happens at least with VGA+HDMI cloning. Suppress them.
5457 */
5458 if (intel_crtc->config->has_pch_encoder) {
5459 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
37ca8d4c 5460 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
b2c0593a 5461 }
37ca8d4c 5462
fd6bbda9 5463 intel_encoders_disable(crtc, old_crtc_state, old_state);
ea9d758d 5464
f9b61ff6
DV
5465 drm_crtc_vblank_off(crtc);
5466 assert_vblank_disabled(crtc);
5467
575f7ab7 5468 intel_disable_pipe(intel_crtc);
32f9d658 5469
bfd16b2a 5470 ironlake_pfit_disable(intel_crtc, false);
2c07245f 5471
b2c0593a 5472 if (intel_crtc->config->has_pch_encoder)
5a74f70a
VS
5473 ironlake_fdi_disable(crtc);
5474
fd6bbda9 5475 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
2c07245f 5476
6e3c9717 5477 if (intel_crtc->config->has_pch_encoder) {
d925c59a 5478 ironlake_disable_pch_transcoder(dev_priv, pipe);
6be4a607 5479
6e266956 5480 if (HAS_PCH_CPT(dev_priv)) {
f0f59a00
VS
5481 i915_reg_t reg;
5482 u32 temp;
5483
d925c59a
DV
5484 /* disable TRANS_DP_CTL */
5485 reg = TRANS_DP_CTL(pipe);
5486 temp = I915_READ(reg);
5487 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5488 TRANS_DP_PORT_SEL_MASK);
5489 temp |= TRANS_DP_PORT_SEL_NONE;
5490 I915_WRITE(reg, temp);
5491
5492 /* disable DPLL_SEL */
5493 temp = I915_READ(PCH_DPLL_SEL);
11887397 5494 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 5495 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 5496 }
e3421a18 5497
d925c59a
DV
5498 ironlake_fdi_pll_disable(intel_crtc);
5499 }
81b088ca 5500
b2c0593a 5501 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
81b088ca 5502 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607 5503}
1b3c7a47 5504
4a806558
ML
5505static void haswell_crtc_disable(struct intel_crtc_state *old_crtc_state,
5506 struct drm_atomic_state *old_state)
ee7b9f93 5507{
4a806558 5508 struct drm_crtc *crtc = old_crtc_state->base.crtc;
6315b5d3 5509 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
ee7b9f93 5510 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 5511 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee7b9f93 5512
d2d65408
VS
5513 if (intel_crtc->config->has_pch_encoder)
5514 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5515 false);
5516
fd6bbda9 5517 intel_encoders_disable(crtc, old_crtc_state, old_state);
4f771f10 5518
f9b61ff6
DV
5519 drm_crtc_vblank_off(crtc);
5520 assert_vblank_disabled(crtc);
5521
4d1de975 5522 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
d7edc4e5 5523 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975 5524 intel_disable_pipe(intel_crtc);
4f771f10 5525
0037071d 5526 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
a4bf214f
VS
5527 intel_ddi_set_vc_payload_alloc(crtc, false);
5528
d7edc4e5 5529 if (!transcoder_is_dsi(cpu_transcoder))
7d4aefd0 5530 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4f771f10 5531
6315b5d3 5532 if (INTEL_GEN(dev_priv) >= 9)
e435d6e5 5533 skylake_scaler_disable(intel_crtc);
ff6d9f55 5534 else
bfd16b2a 5535 ironlake_pfit_disable(intel_crtc, false);
4f771f10 5536
d7edc4e5 5537 if (!transcoder_is_dsi(cpu_transcoder))
7d4aefd0 5538 intel_ddi_disable_pipe_clock(intel_crtc);
4f771f10 5539
fd6bbda9 5540 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
81b088ca 5541
b7076546 5542 if (old_crtc_state->has_pch_encoder)
81b088ca
VS
5543 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5544 true);
4f771f10
PZ
5545}
5546
2dd24552
JB
5547static void i9xx_pfit_enable(struct intel_crtc *crtc)
5548{
5549 struct drm_device *dev = crtc->base.dev;
fac5e23e 5550 struct drm_i915_private *dev_priv = to_i915(dev);
6e3c9717 5551 struct intel_crtc_state *pipe_config = crtc->config;
2dd24552 5552
681a8504 5553 if (!pipe_config->gmch_pfit.control)
2dd24552
JB
5554 return;
5555
2dd24552 5556 /*
c0b03411
DV
5557 * The panel fitter should only be adjusted whilst the pipe is disabled,
5558 * according to register description and PRM.
2dd24552 5559 */
c0b03411
DV
5560 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5561 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 5562
b074cec8
JB
5563 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5564 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
DV
5565
5566 /* Border color in case we don't scale up to the full screen. Black by
5567 * default, change to something else for debugging. */
5568 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
5569}
5570
79f255a0 5571enum intel_display_power_domain intel_port_to_power_domain(enum port port)
d05410f9
DA
5572{
5573 switch (port) {
5574 case PORT_A:
6331a704 5575 return POWER_DOMAIN_PORT_DDI_A_LANES;
d05410f9 5576 case PORT_B:
6331a704 5577 return POWER_DOMAIN_PORT_DDI_B_LANES;
d05410f9 5578 case PORT_C:
6331a704 5579 return POWER_DOMAIN_PORT_DDI_C_LANES;
d05410f9 5580 case PORT_D:
6331a704 5581 return POWER_DOMAIN_PORT_DDI_D_LANES;
d8e19f99 5582 case PORT_E:
6331a704 5583 return POWER_DOMAIN_PORT_DDI_E_LANES;
d05410f9 5584 default:
b9fec167 5585 MISSING_CASE(port);
d05410f9
DA
5586 return POWER_DOMAIN_PORT_OTHER;
5587 }
5588}
5589
d8fc70b7
ACO
5590static u64 get_crtc_power_domains(struct drm_crtc *crtc,
5591 struct intel_crtc_state *crtc_state)
77d22dca 5592{
319be8ae 5593 struct drm_device *dev = crtc->dev;
37255d8d 5594 struct drm_i915_private *dev_priv = to_i915(dev);
74bff5f9 5595 struct drm_encoder *encoder;
319be8ae
ID
5596 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5597 enum pipe pipe = intel_crtc->pipe;
d8fc70b7 5598 u64 mask;
74bff5f9 5599 enum transcoder transcoder = crtc_state->cpu_transcoder;
77d22dca 5600
74bff5f9 5601 if (!crtc_state->base.active)
292b990e
ML
5602 return 0;
5603
77d22dca
ID
5604 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5605 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
74bff5f9
ML
5606 if (crtc_state->pch_pfit.enabled ||
5607 crtc_state->pch_pfit.force_thru)
d8fc70b7 5608 mask |= BIT_ULL(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
77d22dca 5609
74bff5f9
ML
5610 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5611 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5612
79f255a0 5613 mask |= BIT_ULL(intel_encoder->power_domain);
74bff5f9 5614 }
319be8ae 5615
37255d8d
ML
5616 if (HAS_DDI(dev_priv) && crtc_state->has_audio)
5617 mask |= BIT(POWER_DOMAIN_AUDIO);
5618
15e7ec29 5619 if (crtc_state->shared_dpll)
d8fc70b7 5620 mask |= BIT_ULL(POWER_DOMAIN_PLLS);
15e7ec29 5621
77d22dca
ID
5622 return mask;
5623}
5624
d2d15016 5625static u64
74bff5f9
ML
5626modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5627 struct intel_crtc_state *crtc_state)
77d22dca 5628{
fac5e23e 5629 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
292b990e
ML
5630 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5631 enum intel_display_power_domain domain;
d8fc70b7 5632 u64 domains, new_domains, old_domains;
77d22dca 5633
292b990e 5634 old_domains = intel_crtc->enabled_power_domains;
74bff5f9
ML
5635 intel_crtc->enabled_power_domains = new_domains =
5636 get_crtc_power_domains(crtc, crtc_state);
77d22dca 5637
5a21b665 5638 domains = new_domains & ~old_domains;
292b990e
ML
5639
5640 for_each_power_domain(domain, domains)
5641 intel_display_power_get(dev_priv, domain);
5642
5a21b665 5643 return old_domains & ~new_domains;
292b990e
ML
5644}
5645
5646static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
d8fc70b7 5647 u64 domains)
292b990e
ML
5648{
5649 enum intel_display_power_domain domain;
5650
5651 for_each_power_domain(domain, domains)
5652 intel_display_power_put(dev_priv, domain);
5653}
77d22dca 5654
7ff89ca2
VS
5655static void valleyview_crtc_enable(struct intel_crtc_state *pipe_config,
5656 struct drm_atomic_state *old_state)
adafdc6f 5657{
7ff89ca2
VS
5658 struct drm_crtc *crtc = pipe_config->base.crtc;
5659 struct drm_device *dev = crtc->dev;
5660 struct drm_i915_private *dev_priv = to_i915(dev);
5661 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5662 int pipe = intel_crtc->pipe;
adafdc6f 5663
7ff89ca2
VS
5664 if (WARN_ON(intel_crtc->active))
5665 return;
adafdc6f 5666
7ff89ca2
VS
5667 if (intel_crtc_has_dp_encoder(intel_crtc->config))
5668 intel_dp_set_m_n(intel_crtc, M1_N1);
b2045352 5669
7ff89ca2
VS
5670 intel_set_pipe_timings(intel_crtc);
5671 intel_set_pipe_src_size(intel_crtc);
b2045352 5672
7ff89ca2
VS
5673 if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
5674 struct drm_i915_private *dev_priv = to_i915(dev);
560a7ae4 5675
7ff89ca2
VS
5676 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
5677 I915_WRITE(CHV_CANVAS(pipe), 0);
560a7ae4
DL
5678 }
5679
7ff89ca2 5680 i9xx_set_pipeconf(intel_crtc);
560a7ae4 5681
7ff89ca2 5682 intel_crtc->active = true;
92891e45 5683
7ff89ca2 5684 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5f199dfa 5685
7ff89ca2 5686 intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
5f199dfa 5687
7ff89ca2
VS
5688 if (IS_CHERRYVIEW(dev_priv)) {
5689 chv_prepare_pll(intel_crtc, intel_crtc->config);
5690 chv_enable_pll(intel_crtc, intel_crtc->config);
5691 } else {
5692 vlv_prepare_pll(intel_crtc, intel_crtc->config);
5693 vlv_enable_pll(intel_crtc, intel_crtc->config);
5f199dfa
VS
5694 }
5695
7ff89ca2 5696 intel_encoders_pre_enable(crtc, pipe_config, old_state);
5f199dfa 5697
7ff89ca2 5698 i9xx_pfit_enable(intel_crtc);
89b3c3c7 5699
7ff89ca2 5700 intel_color_load_luts(&pipe_config->base);
89b3c3c7 5701
7ff89ca2
VS
5702 intel_update_watermarks(intel_crtc);
5703 intel_enable_pipe(intel_crtc);
5704
5705 assert_vblank_disabled(crtc);
5706 drm_crtc_vblank_on(crtc);
89b3c3c7 5707
7ff89ca2 5708 intel_encoders_enable(crtc, pipe_config, old_state);
89b3c3c7
ACO
5709}
5710
7ff89ca2 5711static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
2b73001e 5712{
7ff89ca2
VS
5713 struct drm_device *dev = crtc->base.dev;
5714 struct drm_i915_private *dev_priv = to_i915(dev);
83d7c81f 5715
7ff89ca2
VS
5716 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
5717 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
2b73001e
VS
5718}
5719
7ff89ca2
VS
5720static void i9xx_crtc_enable(struct intel_crtc_state *pipe_config,
5721 struct drm_atomic_state *old_state)
2b73001e 5722{
7ff89ca2
VS
5723 struct drm_crtc *crtc = pipe_config->base.crtc;
5724 struct drm_device *dev = crtc->dev;
5725 struct drm_i915_private *dev_priv = to_i915(dev);
5726 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5727 enum pipe pipe = intel_crtc->pipe;
2b73001e 5728
7ff89ca2
VS
5729 if (WARN_ON(intel_crtc->active))
5730 return;
2b73001e 5731
7ff89ca2 5732 i9xx_set_pll_dividers(intel_crtc);
2b73001e 5733
7ff89ca2
VS
5734 if (intel_crtc_has_dp_encoder(intel_crtc->config))
5735 intel_dp_set_m_n(intel_crtc, M1_N1);
83d7c81f 5736
7ff89ca2
VS
5737 intel_set_pipe_timings(intel_crtc);
5738 intel_set_pipe_src_size(intel_crtc);
2b73001e 5739
7ff89ca2 5740 i9xx_set_pipeconf(intel_crtc);
f8437dd1 5741
7ff89ca2 5742 intel_crtc->active = true;
5f199dfa 5743
7ff89ca2
VS
5744 if (!IS_GEN2(dev_priv))
5745 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5f199dfa 5746
7ff89ca2 5747 intel_encoders_pre_enable(crtc, pipe_config, old_state);
f8437dd1 5748
7ff89ca2 5749 i9xx_enable_pll(intel_crtc);
f8437dd1 5750
7ff89ca2 5751 i9xx_pfit_enable(intel_crtc);
f8437dd1 5752
7ff89ca2 5753 intel_color_load_luts(&pipe_config->base);
f8437dd1 5754
7ff89ca2
VS
5755 intel_update_watermarks(intel_crtc);
5756 intel_enable_pipe(intel_crtc);
f8437dd1 5757
7ff89ca2
VS
5758 assert_vblank_disabled(crtc);
5759 drm_crtc_vblank_on(crtc);
f8437dd1 5760
7ff89ca2
VS
5761 intel_encoders_enable(crtc, pipe_config, old_state);
5762}
f8437dd1 5763
7ff89ca2
VS
5764static void i9xx_pfit_disable(struct intel_crtc *crtc)
5765{
5766 struct drm_device *dev = crtc->base.dev;
5767 struct drm_i915_private *dev_priv = to_i915(dev);
f8437dd1 5768
7ff89ca2 5769 if (!crtc->config->gmch_pfit.control)
f8437dd1 5770 return;
f8437dd1 5771
7ff89ca2
VS
5772 assert_pipe_disabled(dev_priv, crtc->pipe);
5773
5774 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
5775 I915_READ(PFIT_CONTROL));
5776 I915_WRITE(PFIT_CONTROL, 0);
f8437dd1
VK
5777}
5778
7ff89ca2
VS
5779static void i9xx_crtc_disable(struct intel_crtc_state *old_crtc_state,
5780 struct drm_atomic_state *old_state)
f8437dd1 5781{
7ff89ca2
VS
5782 struct drm_crtc *crtc = old_crtc_state->base.crtc;
5783 struct drm_device *dev = crtc->dev;
5784 struct drm_i915_private *dev_priv = to_i915(dev);
5785 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5786 int pipe = intel_crtc->pipe;
d66a2194 5787
d66a2194 5788 /*
7ff89ca2
VS
5789 * On gen2 planes are double buffered but the pipe isn't, so we must
5790 * wait for planes to fully turn off before disabling the pipe.
d66a2194 5791 */
7ff89ca2
VS
5792 if (IS_GEN2(dev_priv))
5793 intel_wait_for_vblank(dev_priv, pipe);
d66a2194 5794
7ff89ca2 5795 intel_encoders_disable(crtc, old_crtc_state, old_state);
d66a2194 5796
7ff89ca2
VS
5797 drm_crtc_vblank_off(crtc);
5798 assert_vblank_disabled(crtc);
d66a2194 5799
7ff89ca2 5800 intel_disable_pipe(intel_crtc);
d66a2194 5801
7ff89ca2 5802 i9xx_pfit_disable(intel_crtc);
89b3c3c7 5803
7ff89ca2 5804 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
d66a2194 5805
7ff89ca2
VS
5806 if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
5807 if (IS_CHERRYVIEW(dev_priv))
5808 chv_disable_pll(dev_priv, pipe);
5809 else if (IS_VALLEYVIEW(dev_priv))
5810 vlv_disable_pll(dev_priv, pipe);
5811 else
5812 i9xx_disable_pll(intel_crtc);
5813 }
c2e001ef 5814
7ff89ca2 5815 intel_encoders_post_pll_disable(crtc, old_crtc_state, old_state);
89b3c3c7 5816
7ff89ca2
VS
5817 if (!IS_GEN2(dev_priv))
5818 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
f8437dd1
VK
5819}
5820
7ff89ca2 5821static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
f8437dd1 5822{
7ff89ca2
VS
5823 struct intel_encoder *encoder;
5824 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5825 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
5826 enum intel_display_power_domain domain;
d2d15016 5827 u64 domains;
7ff89ca2
VS
5828 struct drm_atomic_state *state;
5829 struct intel_crtc_state *crtc_state;
5830 int ret;
f8437dd1 5831
7ff89ca2
VS
5832 if (!intel_crtc->active)
5833 return;
a8ca4934 5834
7ff89ca2
VS
5835 if (crtc->primary->state->visible) {
5836 WARN_ON(intel_crtc->flip_work);
5d96d8af 5837
7ff89ca2 5838 intel_pre_disable_primary_noatomic(crtc);
709e05c3 5839
7ff89ca2
VS
5840 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
5841 crtc->primary->state->visible = false;
5842 }
5d96d8af 5843
7ff89ca2
VS
5844 state = drm_atomic_state_alloc(crtc->dev);
5845 if (!state) {
5846 DRM_DEBUG_KMS("failed to disable [CRTC:%d:%s], out of memory",
5847 crtc->base.id, crtc->name);
1c3f7700 5848 return;
7ff89ca2 5849 }
9f7eb31a 5850
7ff89ca2 5851 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
ea61791e 5852
7ff89ca2
VS
5853 /* Everything's already locked, -EDEADLK can't happen. */
5854 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
5855 ret = drm_atomic_add_affected_connectors(state, crtc);
9f7eb31a 5856
7ff89ca2 5857 WARN_ON(IS_ERR(crtc_state) || ret);
5d96d8af 5858
7ff89ca2 5859 dev_priv->display.crtc_disable(crtc_state, state);
4a806558 5860
0853695c 5861 drm_atomic_state_put(state);
842e0307 5862
78108b7c
VS
5863 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
5864 crtc->base.id, crtc->name);
842e0307
ML
5865
5866 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
5867 crtc->state->active = false;
37d9078b 5868 intel_crtc->active = false;
842e0307
ML
5869 crtc->enabled = false;
5870 crtc->state->connector_mask = 0;
5871 crtc->state->encoder_mask = 0;
5872
5873 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
5874 encoder->base.crtc = NULL;
5875
58f9c0bc 5876 intel_fbc_disable(intel_crtc);
432081bc 5877 intel_update_watermarks(intel_crtc);
1f7457b1 5878 intel_disable_shared_dpll(intel_crtc);
b17d48e2
ML
5879
5880 domains = intel_crtc->enabled_power_domains;
5881 for_each_power_domain(domain, domains)
5882 intel_display_power_put(dev_priv, domain);
5883 intel_crtc->enabled_power_domains = 0;
565602d7
ML
5884
5885 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
5886 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
b17d48e2
ML
5887}
5888
6b72d486
ML
5889/*
5890 * turn all crtc's off, but do not adjust state
5891 * This has to be paired with a call to intel_modeset_setup_hw_state.
5892 */
70e0bd74 5893int intel_display_suspend(struct drm_device *dev)
ee7b9f93 5894{
e2c8b870 5895 struct drm_i915_private *dev_priv = to_i915(dev);
70e0bd74 5896 struct drm_atomic_state *state;
e2c8b870 5897 int ret;
70e0bd74 5898
e2c8b870
ML
5899 state = drm_atomic_helper_suspend(dev);
5900 ret = PTR_ERR_OR_ZERO(state);
70e0bd74
ML
5901 if (ret)
5902 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
e2c8b870
ML
5903 else
5904 dev_priv->modeset_restore_state = state;
70e0bd74 5905 return ret;
ee7b9f93
JB
5906}
5907
ea5b213a 5908void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 5909{
4ef69c7a 5910 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 5911
ea5b213a
CW
5912 drm_encoder_cleanup(encoder);
5913 kfree(intel_encoder);
7e7d76c3
JB
5914}
5915
0a91ca29
DV
5916/* Cross check the actual hw state with our own modeset state tracking (and it's
5917 * internal consistency). */
5a21b665 5918static void intel_connector_verify_state(struct intel_connector *connector)
79e53945 5919{
5a21b665 5920 struct drm_crtc *crtc = connector->base.state->crtc;
35dd3c64
ML
5921
5922 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
5923 connector->base.base.id,
5924 connector->base.name);
5925
0a91ca29 5926 if (connector->get_hw_state(connector)) {
e85376cb 5927 struct intel_encoder *encoder = connector->encoder;
5a21b665 5928 struct drm_connector_state *conn_state = connector->base.state;
0a91ca29 5929
35dd3c64
ML
5930 I915_STATE_WARN(!crtc,
5931 "connector enabled without attached crtc\n");
0a91ca29 5932
35dd3c64
ML
5933 if (!crtc)
5934 return;
5935
5936 I915_STATE_WARN(!crtc->state->active,
5937 "connector is active, but attached crtc isn't\n");
5938
e85376cb 5939 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
35dd3c64
ML
5940 return;
5941
e85376cb 5942 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
35dd3c64
ML
5943 "atomic encoder doesn't match attached encoder\n");
5944
e85376cb 5945 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
35dd3c64
ML
5946 "attached encoder crtc differs from connector crtc\n");
5947 } else {
4d688a2a
ML
5948 I915_STATE_WARN(crtc && crtc->state->active,
5949 "attached crtc is active, but connector isn't\n");
5a21b665 5950 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
35dd3c64 5951 "best encoder set without crtc!\n");
0a91ca29 5952 }
79e53945
JB
5953}
5954
08d9bc92
ACO
5955int intel_connector_init(struct intel_connector *connector)
5956{
5350a031 5957 drm_atomic_helper_connector_reset(&connector->base);
08d9bc92 5958
5350a031 5959 if (!connector->base.state)
08d9bc92
ACO
5960 return -ENOMEM;
5961
08d9bc92
ACO
5962 return 0;
5963}
5964
5965struct intel_connector *intel_connector_alloc(void)
5966{
5967 struct intel_connector *connector;
5968
5969 connector = kzalloc(sizeof *connector, GFP_KERNEL);
5970 if (!connector)
5971 return NULL;
5972
5973 if (intel_connector_init(connector) < 0) {
5974 kfree(connector);
5975 return NULL;
5976 }
5977
5978 return connector;
5979}
5980
f0947c37
DV
5981/* Simple connector->get_hw_state implementation for encoders that support only
5982 * one connector and no cloning and hence the encoder state determines the state
5983 * of the connector. */
5984bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 5985{
24929352 5986 enum pipe pipe = 0;
f0947c37 5987 struct intel_encoder *encoder = connector->encoder;
ea5b213a 5988
f0947c37 5989 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
5990}
5991
6d293983 5992static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
d272ddfa 5993{
6d293983
ACO
5994 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
5995 return crtc_state->fdi_lanes;
d272ddfa
VS
5996
5997 return 0;
5998}
5999
6d293983 6000static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5cec258b 6001 struct intel_crtc_state *pipe_config)
1857e1da 6002{
8652744b 6003 struct drm_i915_private *dev_priv = to_i915(dev);
6d293983
ACO
6004 struct drm_atomic_state *state = pipe_config->base.state;
6005 struct intel_crtc *other_crtc;
6006 struct intel_crtc_state *other_crtc_state;
6007
1857e1da
DV
6008 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6009 pipe_name(pipe), pipe_config->fdi_lanes);
6010 if (pipe_config->fdi_lanes > 4) {
6011 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6012 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6013 return -EINVAL;
1857e1da
DV
6014 }
6015
8652744b 6016 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
1857e1da
DV
6017 if (pipe_config->fdi_lanes > 2) {
6018 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6019 pipe_config->fdi_lanes);
6d293983 6020 return -EINVAL;
1857e1da 6021 } else {
6d293983 6022 return 0;
1857e1da
DV
6023 }
6024 }
6025
b7f05d4a 6026 if (INTEL_INFO(dev_priv)->num_pipes == 2)
6d293983 6027 return 0;
1857e1da
DV
6028
6029 /* Ivybridge 3 pipe is really complicated */
6030 switch (pipe) {
6031 case PIPE_A:
6d293983 6032 return 0;
1857e1da 6033 case PIPE_B:
6d293983
ACO
6034 if (pipe_config->fdi_lanes <= 2)
6035 return 0;
6036
b91eb5cc 6037 other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
6d293983
ACO
6038 other_crtc_state =
6039 intel_atomic_get_crtc_state(state, other_crtc);
6040 if (IS_ERR(other_crtc_state))
6041 return PTR_ERR(other_crtc_state);
6042
6043 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
1857e1da
DV
6044 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6045 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6046 return -EINVAL;
1857e1da 6047 }
6d293983 6048 return 0;
1857e1da 6049 case PIPE_C:
251cc67c
VS
6050 if (pipe_config->fdi_lanes > 2) {
6051 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6052 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6053 return -EINVAL;
251cc67c 6054 }
6d293983 6055
b91eb5cc 6056 other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
6d293983
ACO
6057 other_crtc_state =
6058 intel_atomic_get_crtc_state(state, other_crtc);
6059 if (IS_ERR(other_crtc_state))
6060 return PTR_ERR(other_crtc_state);
6061
6062 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
1857e1da 6063 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
6d293983 6064 return -EINVAL;
1857e1da 6065 }
6d293983 6066 return 0;
1857e1da
DV
6067 default:
6068 BUG();
6069 }
6070}
6071
e29c22c0
DV
6072#define RETRY 1
6073static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5cec258b 6074 struct intel_crtc_state *pipe_config)
877d48d5 6075{
1857e1da 6076 struct drm_device *dev = intel_crtc->base.dev;
7c5f93b0 6077 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6d293983
ACO
6078 int lane, link_bw, fdi_dotclock, ret;
6079 bool needs_recompute = false;
877d48d5 6080
e29c22c0 6081retry:
877d48d5
DV
6082 /* FDI is a binary signal running at ~2.7GHz, encoding
6083 * each output octet as 10 bits. The actual frequency
6084 * is stored as a divider into a 100MHz clock, and the
6085 * mode pixel clock is stored in units of 1KHz.
6086 * Hence the bw of each lane in terms of the mode signal
6087 * is:
6088 */
21a727b3 6089 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
877d48d5 6090
241bfc38 6091 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 6092
2bd89a07 6093 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
DV
6094 pipe_config->pipe_bpp);
6095
6096 pipe_config->fdi_lanes = lane;
6097
2bd89a07 6098 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
877d48d5 6099 link_bw, &pipe_config->fdi_m_n);
1857e1da 6100
e3b247da 6101 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
6d293983 6102 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
e29c22c0 6103 pipe_config->pipe_bpp -= 2*3;
7ff89ca2
VS
6104 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6105 pipe_config->pipe_bpp);
6106 needs_recompute = true;
6107 pipe_config->bw_constrained = true;
257a7ffc 6108
7ff89ca2 6109 goto retry;
257a7ffc 6110 }
79e53945 6111
7ff89ca2
VS
6112 if (needs_recompute)
6113 return RETRY;
e70236a8 6114
7ff89ca2 6115 return ret;
e70236a8
JB
6116}
6117
7ff89ca2
VS
6118static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6119 struct intel_crtc_state *pipe_config)
e70236a8 6120{
7ff89ca2
VS
6121 if (pipe_config->pipe_bpp > 24)
6122 return false;
e70236a8 6123
7ff89ca2
VS
6124 /* HSW can handle pixel rate up to cdclk? */
6125 if (IS_HASWELL(dev_priv))
6126 return true;
1b1d2716 6127
65cd2b3f 6128 /*
7ff89ca2
VS
6129 * We compare against max which means we must take
6130 * the increased cdclk requirement into account when
6131 * calculating the new cdclk.
6132 *
6133 * Should measure whether using a lower cdclk w/o IPS
e70236a8 6134 */
7ff89ca2
VS
6135 return pipe_config->pixel_rate <=
6136 dev_priv->max_cdclk_freq * 95 / 100;
e70236a8 6137}
79e53945 6138
7ff89ca2
VS
6139static void hsw_compute_ips_config(struct intel_crtc *crtc,
6140 struct intel_crtc_state *pipe_config)
6141{
6142 struct drm_device *dev = crtc->base.dev;
6143 struct drm_i915_private *dev_priv = to_i915(dev);
34edce2f 6144
7ff89ca2
VS
6145 pipe_config->ips_enabled = i915.enable_ips &&
6146 hsw_crtc_supports_ips(crtc) &&
6147 pipe_config_supports_ips(dev_priv, pipe_config);
34edce2f
VS
6148}
6149
7ff89ca2 6150static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
34edce2f 6151{
7ff89ca2 6152 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
34edce2f 6153
7ff89ca2
VS
6154 /* GDG double wide on either pipe, otherwise pipe A only */
6155 return INTEL_INFO(dev_priv)->gen < 4 &&
6156 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
34edce2f
VS
6157}
6158
ceb99320
VS
6159static uint32_t ilk_pipe_pixel_rate(const struct intel_crtc_state *pipe_config)
6160{
6161 uint32_t pixel_rate;
6162
6163 pixel_rate = pipe_config->base.adjusted_mode.crtc_clock;
6164
6165 /*
6166 * We only use IF-ID interlacing. If we ever use
6167 * PF-ID we'll need to adjust the pixel_rate here.
6168 */
6169
6170 if (pipe_config->pch_pfit.enabled) {
6171 uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
6172 uint32_t pfit_size = pipe_config->pch_pfit.size;
6173
6174 pipe_w = pipe_config->pipe_src_w;
6175 pipe_h = pipe_config->pipe_src_h;
6176
6177 pfit_w = (pfit_size >> 16) & 0xFFFF;
6178 pfit_h = pfit_size & 0xFFFF;
6179 if (pipe_w < pfit_w)
6180 pipe_w = pfit_w;
6181 if (pipe_h < pfit_h)
6182 pipe_h = pfit_h;
6183
6184 if (WARN_ON(!pfit_w || !pfit_h))
6185 return pixel_rate;
6186
6187 pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
6188 pfit_w * pfit_h);
6189 }
6190
6191 return pixel_rate;
6192}
6193
7ff89ca2 6194static void intel_crtc_compute_pixel_rate(struct intel_crtc_state *crtc_state)
34edce2f 6195{
7ff89ca2 6196 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
34edce2f 6197
7ff89ca2
VS
6198 if (HAS_GMCH_DISPLAY(dev_priv))
6199 /* FIXME calculate proper pipe pixel rate for GMCH pfit */
6200 crtc_state->pixel_rate =
6201 crtc_state->base.adjusted_mode.crtc_clock;
6202 else
6203 crtc_state->pixel_rate =
6204 ilk_pipe_pixel_rate(crtc_state);
6205}
34edce2f 6206
7ff89ca2
VS
6207static int intel_crtc_compute_config(struct intel_crtc *crtc,
6208 struct intel_crtc_state *pipe_config)
6209{
6210 struct drm_device *dev = crtc->base.dev;
6211 struct drm_i915_private *dev_priv = to_i915(dev);
6212 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6213 int clock_limit = dev_priv->max_dotclk_freq;
34edce2f 6214
7ff89ca2
VS
6215 if (INTEL_GEN(dev_priv) < 4) {
6216 clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
34edce2f 6217
7ff89ca2
VS
6218 /*
6219 * Enable double wide mode when the dot clock
6220 * is > 90% of the (display) core speed.
6221 */
6222 if (intel_crtc_supports_double_wide(crtc) &&
6223 adjusted_mode->crtc_clock > clock_limit) {
6224 clock_limit = dev_priv->max_dotclk_freq;
6225 pipe_config->double_wide = true;
6226 }
34edce2f
VS
6227 }
6228
7ff89ca2
VS
6229 if (adjusted_mode->crtc_clock > clock_limit) {
6230 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6231 adjusted_mode->crtc_clock, clock_limit,
6232 yesno(pipe_config->double_wide));
6233 return -EINVAL;
6234 }
34edce2f 6235
7ff89ca2
VS
6236 /*
6237 * Pipe horizontal size must be even in:
6238 * - DVO ganged mode
6239 * - LVDS dual channel mode
6240 * - Double wide pipe
6241 */
6242 if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
6243 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6244 pipe_config->pipe_src_w &= ~1;
34edce2f 6245
7ff89ca2
VS
6246 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6247 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
6248 */
6249 if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
6250 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
6251 return -EINVAL;
34edce2f 6252
7ff89ca2 6253 intel_crtc_compute_pixel_rate(pipe_config);
34edce2f 6254
7ff89ca2
VS
6255 if (HAS_IPS(dev_priv))
6256 hsw_compute_ips_config(crtc, pipe_config);
34edce2f 6257
7ff89ca2
VS
6258 if (pipe_config->has_pch_encoder)
6259 return ironlake_fdi_compute_config(crtc, pipe_config);
34edce2f 6260
7ff89ca2 6261 return 0;
34edce2f
VS
6262}
6263
2c07245f 6264static void
a65851af 6265intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 6266{
a65851af
VS
6267 while (*num > DATA_LINK_M_N_MASK ||
6268 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
6269 *num >>= 1;
6270 *den >>= 1;
6271 }
6272}
6273
a65851af
VS
6274static void compute_m_n(unsigned int m, unsigned int n,
6275 uint32_t *ret_m, uint32_t *ret_n)
6276{
6277 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
6278 *ret_m = div_u64((uint64_t) m * *ret_n, n);
6279 intel_reduce_m_n_ratio(ret_m, ret_n);
6280}
6281
e69d0bc1
DV
6282void
6283intel_link_compute_m_n(int bits_per_pixel, int nlanes,
6284 int pixel_clock, int link_clock,
6285 struct intel_link_m_n *m_n)
2c07245f 6286{
e69d0bc1 6287 m_n->tu = 64;
a65851af
VS
6288
6289 compute_m_n(bits_per_pixel * pixel_clock,
6290 link_clock * nlanes * 8,
6291 &m_n->gmch_m, &m_n->gmch_n);
6292
6293 compute_m_n(pixel_clock, link_clock,
6294 &m_n->link_m, &m_n->link_n);
2c07245f
ZW
6295}
6296
a7615030
CW
6297static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
6298{
d330a953
JN
6299 if (i915.panel_use_ssc >= 0)
6300 return i915.panel_use_ssc != 0;
41aa3448 6301 return dev_priv->vbt.lvds_use_ssc
435793df 6302 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
6303}
6304
7429e9d4 6305static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 6306{
7df00d7a 6307 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 6308}
f47709a9 6309
7429e9d4
DV
6310static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
6311{
6312 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
6313}
6314
f47709a9 6315static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
190f68c5 6316 struct intel_crtc_state *crtc_state,
9e2c8475 6317 struct dpll *reduced_clock)
a7516a05 6318{
9b1e14f4 6319 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
a7516a05
JB
6320 u32 fp, fp2 = 0;
6321
9b1e14f4 6322 if (IS_PINEVIEW(dev_priv)) {
190f68c5 6323 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
a7516a05 6324 if (reduced_clock)
7429e9d4 6325 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 6326 } else {
190f68c5 6327 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
a7516a05 6328 if (reduced_clock)
7429e9d4 6329 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
6330 }
6331
190f68c5 6332 crtc_state->dpll_hw_state.fp0 = fp;
a7516a05 6333
f47709a9 6334 crtc->lowfreq_avail = false;
2d84d2b3 6335 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ab585dea 6336 reduced_clock) {
190f68c5 6337 crtc_state->dpll_hw_state.fp1 = fp2;
f47709a9 6338 crtc->lowfreq_avail = true;
a7516a05 6339 } else {
190f68c5 6340 crtc_state->dpll_hw_state.fp1 = fp;
a7516a05
JB
6341 }
6342}
6343
5e69f97f
CML
6344static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
6345 pipe)
89b667f8
JB
6346{
6347 u32 reg_val;
6348
6349 /*
6350 * PLLB opamp always calibrates to max value of 0x3f, force enable it
6351 * and set it to a reasonable value instead.
6352 */
ab3c759a 6353 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
6354 reg_val &= 0xffffff00;
6355 reg_val |= 0x00000030;
ab3c759a 6356 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 6357
ab3c759a 6358 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
6359 reg_val &= 0x8cffffff;
6360 reg_val = 0x8c000000;
ab3c759a 6361 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 6362
ab3c759a 6363 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 6364 reg_val &= 0xffffff00;
ab3c759a 6365 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 6366
ab3c759a 6367 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
6368 reg_val &= 0x00ffffff;
6369 reg_val |= 0xb0000000;
ab3c759a 6370 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
6371}
6372
b551842d
DV
6373static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
6374 struct intel_link_m_n *m_n)
6375{
6376 struct drm_device *dev = crtc->base.dev;
fac5e23e 6377 struct drm_i915_private *dev_priv = to_i915(dev);
b551842d
DV
6378 int pipe = crtc->pipe;
6379
e3b95f1e
DV
6380 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6381 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
6382 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
6383 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
6384}
6385
6386static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
6387 struct intel_link_m_n *m_n,
6388 struct intel_link_m_n *m2_n2)
b551842d 6389{
6315b5d3 6390 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
b551842d 6391 int pipe = crtc->pipe;
6e3c9717 6392 enum transcoder transcoder = crtc->config->cpu_transcoder;
b551842d 6393
6315b5d3 6394 if (INTEL_GEN(dev_priv) >= 5) {
b551842d
DV
6395 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
6396 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
6397 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
6398 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
f769cd24
VK
6399 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
6400 * for gen < 8) and if DRRS is supported (to make sure the
6401 * registers are not unnecessarily accessed).
6402 */
920a14b2
TU
6403 if (m2_n2 && (IS_CHERRYVIEW(dev_priv) ||
6404 INTEL_GEN(dev_priv) < 8) && crtc->config->has_drrs) {
f769cd24
VK
6405 I915_WRITE(PIPE_DATA_M2(transcoder),
6406 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
6407 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
6408 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
6409 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
6410 }
b551842d 6411 } else {
e3b95f1e
DV
6412 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6413 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
6414 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
6415 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
6416 }
6417}
6418
fe3cd48d 6419void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
03afc4a2 6420{
fe3cd48d
R
6421 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
6422
6423 if (m_n == M1_N1) {
6424 dp_m_n = &crtc->config->dp_m_n;
6425 dp_m2_n2 = &crtc->config->dp_m2_n2;
6426 } else if (m_n == M2_N2) {
6427
6428 /*
6429 * M2_N2 registers are not supported. Hence m2_n2 divider value
6430 * needs to be programmed into M1_N1.
6431 */
6432 dp_m_n = &crtc->config->dp_m2_n2;
6433 } else {
6434 DRM_ERROR("Unsupported divider value\n");
6435 return;
6436 }
6437
6e3c9717
ACO
6438 if (crtc->config->has_pch_encoder)
6439 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
03afc4a2 6440 else
fe3cd48d 6441 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
03afc4a2
DV
6442}
6443
251ac862
DV
6444static void vlv_compute_dpll(struct intel_crtc *crtc,
6445 struct intel_crtc_state *pipe_config)
bdd4b6a6 6446{
03ed5cbf 6447 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
cd2d34d9 6448 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
03ed5cbf
VS
6449 if (crtc->pipe != PIPE_A)
6450 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
bdd4b6a6 6451
cd2d34d9 6452 /* DPLL not used with DSI, but still need the rest set up */
d7edc4e5 6453 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
cd2d34d9
VS
6454 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
6455 DPLL_EXT_BUFFER_ENABLE_VLV;
6456
03ed5cbf
VS
6457 pipe_config->dpll_hw_state.dpll_md =
6458 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
6459}
bdd4b6a6 6460
03ed5cbf
VS
6461static void chv_compute_dpll(struct intel_crtc *crtc,
6462 struct intel_crtc_state *pipe_config)
6463{
6464 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
cd2d34d9 6465 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
03ed5cbf
VS
6466 if (crtc->pipe != PIPE_A)
6467 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
6468
cd2d34d9 6469 /* DPLL not used with DSI, but still need the rest set up */
d7edc4e5 6470 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
cd2d34d9
VS
6471 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
6472
03ed5cbf
VS
6473 pipe_config->dpll_hw_state.dpll_md =
6474 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
bdd4b6a6
DV
6475}
6476
d288f65f 6477static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 6478 const struct intel_crtc_state *pipe_config)
a0c4da24 6479{
f47709a9 6480 struct drm_device *dev = crtc->base.dev;
fac5e23e 6481 struct drm_i915_private *dev_priv = to_i915(dev);
cd2d34d9 6482 enum pipe pipe = crtc->pipe;
bdd4b6a6 6483 u32 mdiv;
a0c4da24 6484 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 6485 u32 coreclk, reg_val;
a0c4da24 6486
cd2d34d9
VS
6487 /* Enable Refclk */
6488 I915_WRITE(DPLL(pipe),
6489 pipe_config->dpll_hw_state.dpll &
6490 ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
6491
6492 /* No need to actually set up the DPLL with DSI */
6493 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
6494 return;
6495
a580516d 6496 mutex_lock(&dev_priv->sb_lock);
09153000 6497
d288f65f
VS
6498 bestn = pipe_config->dpll.n;
6499 bestm1 = pipe_config->dpll.m1;
6500 bestm2 = pipe_config->dpll.m2;
6501 bestp1 = pipe_config->dpll.p1;
6502 bestp2 = pipe_config->dpll.p2;
a0c4da24 6503
89b667f8
JB
6504 /* See eDP HDMI DPIO driver vbios notes doc */
6505
6506 /* PLL B needs special handling */
bdd4b6a6 6507 if (pipe == PIPE_B)
5e69f97f 6508 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
6509
6510 /* Set up Tx target for periodic Rcomp update */
ab3c759a 6511 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
6512
6513 /* Disable target IRef on PLL */
ab3c759a 6514 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 6515 reg_val &= 0x00ffffff;
ab3c759a 6516 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
6517
6518 /* Disable fast lock */
ab3c759a 6519 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
6520
6521 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
6522 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
6523 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
6524 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 6525 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
6526
6527 /*
6528 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
6529 * but we don't support that).
6530 * Note: don't use the DAC post divider as it seems unstable.
6531 */
6532 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 6533 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 6534
a0c4da24 6535 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 6536 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 6537
89b667f8 6538 /* Set HBR and RBR LPF coefficients */
d288f65f 6539 if (pipe_config->port_clock == 162000 ||
2d84d2b3
VS
6540 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
6541 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
ab3c759a 6542 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 6543 0x009f0003);
89b667f8 6544 else
ab3c759a 6545 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
6546 0x00d0000f);
6547
37a5650b 6548 if (intel_crtc_has_dp_encoder(pipe_config)) {
89b667f8 6549 /* Use SSC source */
bdd4b6a6 6550 if (pipe == PIPE_A)
ab3c759a 6551 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6552 0x0df40000);
6553 else
ab3c759a 6554 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6555 0x0df70000);
6556 } else { /* HDMI or VGA */
6557 /* Use bend source */
bdd4b6a6 6558 if (pipe == PIPE_A)
ab3c759a 6559 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6560 0x0df70000);
6561 else
ab3c759a 6562 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6563 0x0df40000);
6564 }
a0c4da24 6565
ab3c759a 6566 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8 6567 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
2210ce7f 6568 if (intel_crtc_has_dp_encoder(crtc->config))
89b667f8 6569 coreclk |= 0x01000000;
ab3c759a 6570 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 6571
ab3c759a 6572 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
a580516d 6573 mutex_unlock(&dev_priv->sb_lock);
a0c4da24
JB
6574}
6575
d288f65f 6576static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 6577 const struct intel_crtc_state *pipe_config)
9d556c99
CML
6578{
6579 struct drm_device *dev = crtc->base.dev;
fac5e23e 6580 struct drm_i915_private *dev_priv = to_i915(dev);
cd2d34d9 6581 enum pipe pipe = crtc->pipe;
9d556c99 6582 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9cbe40c1 6583 u32 loopfilter, tribuf_calcntr;
9d556c99 6584 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
a945ce7e 6585 u32 dpio_val;
9cbe40c1 6586 int vco;
9d556c99 6587
cd2d34d9
VS
6588 /* Enable Refclk and SSC */
6589 I915_WRITE(DPLL(pipe),
6590 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
6591
6592 /* No need to actually set up the DPLL with DSI */
6593 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
6594 return;
6595
d288f65f
VS
6596 bestn = pipe_config->dpll.n;
6597 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
6598 bestm1 = pipe_config->dpll.m1;
6599 bestm2 = pipe_config->dpll.m2 >> 22;
6600 bestp1 = pipe_config->dpll.p1;
6601 bestp2 = pipe_config->dpll.p2;
9cbe40c1 6602 vco = pipe_config->dpll.vco;
a945ce7e 6603 dpio_val = 0;
9cbe40c1 6604 loopfilter = 0;
9d556c99 6605
a580516d 6606 mutex_lock(&dev_priv->sb_lock);
9d556c99 6607
9d556c99
CML
6608 /* p1 and p2 divider */
6609 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
6610 5 << DPIO_CHV_S1_DIV_SHIFT |
6611 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
6612 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
6613 1 << DPIO_CHV_K_DIV_SHIFT);
6614
6615 /* Feedback post-divider - m2 */
6616 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
6617
6618 /* Feedback refclk divider - n and m1 */
6619 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
6620 DPIO_CHV_M1_DIV_BY_2 |
6621 1 << DPIO_CHV_N_DIV_SHIFT);
6622
6623 /* M2 fraction division */
25a25dfc 6624 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
9d556c99
CML
6625
6626 /* M2 fraction division enable */
a945ce7e
VP
6627 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
6628 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
6629 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
6630 if (bestm2_frac)
6631 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
6632 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
9d556c99 6633
de3a0fde
VP
6634 /* Program digital lock detect threshold */
6635 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
6636 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
6637 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
6638 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
6639 if (!bestm2_frac)
6640 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
6641 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
6642
9d556c99 6643 /* Loop filter */
9cbe40c1
VP
6644 if (vco == 5400000) {
6645 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
6646 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
6647 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
6648 tribuf_calcntr = 0x9;
6649 } else if (vco <= 6200000) {
6650 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
6651 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
6652 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6653 tribuf_calcntr = 0x9;
6654 } else if (vco <= 6480000) {
6655 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
6656 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
6657 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6658 tribuf_calcntr = 0x8;
6659 } else {
6660 /* Not supported. Apply the same limits as in the max case */
6661 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
6662 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
6663 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6664 tribuf_calcntr = 0;
6665 }
9d556c99
CML
6666 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
6667
968040b2 6668 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
9cbe40c1
VP
6669 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
6670 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
6671 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
6672
9d556c99
CML
6673 /* AFC Recal */
6674 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
6675 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
6676 DPIO_AFC_RECAL);
6677
a580516d 6678 mutex_unlock(&dev_priv->sb_lock);
9d556c99
CML
6679}
6680
d288f65f
VS
6681/**
6682 * vlv_force_pll_on - forcibly enable just the PLL
6683 * @dev_priv: i915 private structure
6684 * @pipe: pipe PLL to enable
6685 * @dpll: PLL configuration
6686 *
6687 * Enable the PLL for @pipe using the supplied @dpll config. To be used
6688 * in cases where we need the PLL enabled even when @pipe is not going to
6689 * be enabled.
6690 */
30ad9814 6691int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
3f36b937 6692 const struct dpll *dpll)
d288f65f 6693{
b91eb5cc 6694 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
3f36b937
TU
6695 struct intel_crtc_state *pipe_config;
6696
6697 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
6698 if (!pipe_config)
6699 return -ENOMEM;
6700
6701 pipe_config->base.crtc = &crtc->base;
6702 pipe_config->pixel_multiplier = 1;
6703 pipe_config->dpll = *dpll;
d288f65f 6704
30ad9814 6705 if (IS_CHERRYVIEW(dev_priv)) {
3f36b937
TU
6706 chv_compute_dpll(crtc, pipe_config);
6707 chv_prepare_pll(crtc, pipe_config);
6708 chv_enable_pll(crtc, pipe_config);
d288f65f 6709 } else {
3f36b937
TU
6710 vlv_compute_dpll(crtc, pipe_config);
6711 vlv_prepare_pll(crtc, pipe_config);
6712 vlv_enable_pll(crtc, pipe_config);
d288f65f 6713 }
3f36b937
TU
6714
6715 kfree(pipe_config);
6716
6717 return 0;
d288f65f
VS
6718}
6719
6720/**
6721 * vlv_force_pll_off - forcibly disable just the PLL
6722 * @dev_priv: i915 private structure
6723 * @pipe: pipe PLL to disable
6724 *
6725 * Disable the PLL for @pipe. To be used in cases where we need
6726 * the PLL enabled even when @pipe is not going to be enabled.
6727 */
30ad9814 6728void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
d288f65f 6729{
30ad9814
VS
6730 if (IS_CHERRYVIEW(dev_priv))
6731 chv_disable_pll(dev_priv, pipe);
d288f65f 6732 else
30ad9814 6733 vlv_disable_pll(dev_priv, pipe);
d288f65f
VS
6734}
6735
251ac862
DV
6736static void i9xx_compute_dpll(struct intel_crtc *crtc,
6737 struct intel_crtc_state *crtc_state,
9e2c8475 6738 struct dpll *reduced_clock)
eb1cbe48 6739{
9b1e14f4 6740 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
eb1cbe48 6741 u32 dpll;
190f68c5 6742 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 6743
190f68c5 6744 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 6745
eb1cbe48
DV
6746 dpll = DPLL_VGA_MODE_DIS;
6747
2d84d2b3 6748 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
6749 dpll |= DPLLB_MODE_LVDS;
6750 else
6751 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 6752
73f67aa8
JN
6753 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
6754 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
190f68c5 6755 dpll |= (crtc_state->pixel_multiplier - 1)
198a037f 6756 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 6757 }
198a037f 6758
3d6e9ee0
VS
6759 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
6760 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
4a33e48d 6761 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 6762
37a5650b 6763 if (intel_crtc_has_dp_encoder(crtc_state))
4a33e48d 6764 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
6765
6766 /* compute bitmask from p1 value */
9b1e14f4 6767 if (IS_PINEVIEW(dev_priv))
eb1cbe48
DV
6768 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
6769 else {
6770 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
9beb5fea 6771 if (IS_G4X(dev_priv) && reduced_clock)
eb1cbe48
DV
6772 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
6773 }
6774 switch (clock->p2) {
6775 case 5:
6776 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
6777 break;
6778 case 7:
6779 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
6780 break;
6781 case 10:
6782 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
6783 break;
6784 case 14:
6785 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
6786 break;
6787 }
9b1e14f4 6788 if (INTEL_GEN(dev_priv) >= 4)
eb1cbe48
DV
6789 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
6790
190f68c5 6791 if (crtc_state->sdvo_tv_clock)
eb1cbe48 6792 dpll |= PLL_REF_INPUT_TVCLKINBC;
2d84d2b3 6793 else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 6794 intel_panel_use_ssc(dev_priv))
eb1cbe48
DV
6795 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
6796 else
6797 dpll |= PLL_REF_INPUT_DREFCLK;
6798
6799 dpll |= DPLL_VCO_ENABLE;
190f68c5 6800 crtc_state->dpll_hw_state.dpll = dpll;
8bcc2795 6801
9b1e14f4 6802 if (INTEL_GEN(dev_priv) >= 4) {
190f68c5 6803 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
ef1b460d 6804 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
190f68c5 6805 crtc_state->dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
DV
6806 }
6807}
6808
251ac862
DV
6809static void i8xx_compute_dpll(struct intel_crtc *crtc,
6810 struct intel_crtc_state *crtc_state,
9e2c8475 6811 struct dpll *reduced_clock)
eb1cbe48 6812{
f47709a9 6813 struct drm_device *dev = crtc->base.dev;
fac5e23e 6814 struct drm_i915_private *dev_priv = to_i915(dev);
eb1cbe48 6815 u32 dpll;
190f68c5 6816 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 6817
190f68c5 6818 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 6819
eb1cbe48
DV
6820 dpll = DPLL_VGA_MODE_DIS;
6821
2d84d2b3 6822 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
6823 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6824 } else {
6825 if (clock->p1 == 2)
6826 dpll |= PLL_P1_DIVIDE_BY_TWO;
6827 else
6828 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6829 if (clock->p2 == 4)
6830 dpll |= PLL_P2_DIVIDE_BY_4;
6831 }
6832
50a0bc90
TU
6833 if (!IS_I830(dev_priv) &&
6834 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
4a33e48d
DV
6835 dpll |= DPLL_DVO_2X_MODE;
6836
2d84d2b3 6837 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 6838 intel_panel_use_ssc(dev_priv))
eb1cbe48
DV
6839 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
6840 else
6841 dpll |= PLL_REF_INPUT_DREFCLK;
6842
6843 dpll |= DPLL_VCO_ENABLE;
190f68c5 6844 crtc_state->dpll_hw_state.dpll = dpll;
eb1cbe48
DV
6845}
6846
8a654f3b 6847static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c 6848{
6315b5d3 6849 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
b0e77b9c 6850 enum pipe pipe = intel_crtc->pipe;
6e3c9717 6851 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
7c5f93b0 6852 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
1caea6e9
VS
6853 uint32_t crtc_vtotal, crtc_vblank_end;
6854 int vsyncshift = 0;
4d8a62ea
DV
6855
6856 /* We need to be careful not to changed the adjusted mode, for otherwise
6857 * the hw state checker will get angry at the mismatch. */
6858 crtc_vtotal = adjusted_mode->crtc_vtotal;
6859 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 6860
609aeaca 6861 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 6862 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
6863 crtc_vtotal -= 1;
6864 crtc_vblank_end -= 1;
609aeaca 6865
2d84d2b3 6866 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
609aeaca
VS
6867 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
6868 else
6869 vsyncshift = adjusted_mode->crtc_hsync_start -
6870 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
6871 if (vsyncshift < 0)
6872 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
6873 }
6874
6315b5d3 6875 if (INTEL_GEN(dev_priv) > 3)
fe2b8f9d 6876 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 6877
fe2b8f9d 6878 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
6879 (adjusted_mode->crtc_hdisplay - 1) |
6880 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 6881 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
6882 (adjusted_mode->crtc_hblank_start - 1) |
6883 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 6884 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
6885 (adjusted_mode->crtc_hsync_start - 1) |
6886 ((adjusted_mode->crtc_hsync_end - 1) << 16));
6887
fe2b8f9d 6888 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 6889 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 6890 ((crtc_vtotal - 1) << 16));
fe2b8f9d 6891 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 6892 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 6893 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 6894 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
6895 (adjusted_mode->crtc_vsync_start - 1) |
6896 ((adjusted_mode->crtc_vsync_end - 1) << 16));
6897
b5e508d4
PZ
6898 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
6899 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
6900 * documented on the DDI_FUNC_CTL register description, EDP Input Select
6901 * bits. */
772c2a51 6902 if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
b5e508d4
PZ
6903 (pipe == PIPE_B || pipe == PIPE_C))
6904 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
6905
bc58be60
JN
6906}
6907
6908static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
6909{
6910 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 6911 struct drm_i915_private *dev_priv = to_i915(dev);
bc58be60
JN
6912 enum pipe pipe = intel_crtc->pipe;
6913
b0e77b9c
PZ
6914 /* pipesrc controls the size that is scaled from, which should
6915 * always be the user's requested size.
6916 */
6917 I915_WRITE(PIPESRC(pipe),
6e3c9717
ACO
6918 ((intel_crtc->config->pipe_src_w - 1) << 16) |
6919 (intel_crtc->config->pipe_src_h - 1));
b0e77b9c
PZ
6920}
6921
1bd1bd80 6922static void intel_get_pipe_timings(struct intel_crtc *crtc,
5cec258b 6923 struct intel_crtc_state *pipe_config)
1bd1bd80
DV
6924{
6925 struct drm_device *dev = crtc->base.dev;
fac5e23e 6926 struct drm_i915_private *dev_priv = to_i915(dev);
1bd1bd80
DV
6927 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
6928 uint32_t tmp;
6929
6930 tmp = I915_READ(HTOTAL(cpu_transcoder));
2d112de7
ACO
6931 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
6932 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 6933 tmp = I915_READ(HBLANK(cpu_transcoder));
2d112de7
ACO
6934 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
6935 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 6936 tmp = I915_READ(HSYNC(cpu_transcoder));
2d112de7
ACO
6937 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
6938 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
6939
6940 tmp = I915_READ(VTOTAL(cpu_transcoder));
2d112de7
ACO
6941 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
6942 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 6943 tmp = I915_READ(VBLANK(cpu_transcoder));
2d112de7
ACO
6944 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
6945 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 6946 tmp = I915_READ(VSYNC(cpu_transcoder));
2d112de7
ACO
6947 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
6948 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
6949
6950 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
2d112de7
ACO
6951 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
6952 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
6953 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
1bd1bd80 6954 }
bc58be60
JN
6955}
6956
6957static void intel_get_pipe_src_size(struct intel_crtc *crtc,
6958 struct intel_crtc_state *pipe_config)
6959{
6960 struct drm_device *dev = crtc->base.dev;
fac5e23e 6961 struct drm_i915_private *dev_priv = to_i915(dev);
bc58be60 6962 u32 tmp;
1bd1bd80
DV
6963
6964 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
6965 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
6966 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
6967
2d112de7
ACO
6968 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
6969 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
DV
6970}
6971
f6a83288 6972void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5cec258b 6973 struct intel_crtc_state *pipe_config)
babea61d 6974{
2d112de7
ACO
6975 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
6976 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
6977 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
6978 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
babea61d 6979
2d112de7
ACO
6980 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
6981 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
6982 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
6983 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
babea61d 6984
2d112de7 6985 mode->flags = pipe_config->base.adjusted_mode.flags;
cd13f5ab 6986 mode->type = DRM_MODE_TYPE_DRIVER;
babea61d 6987
2d112de7 6988 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
cd13f5ab
ML
6989
6990 mode->hsync = drm_mode_hsync(mode);
6991 mode->vrefresh = drm_mode_vrefresh(mode);
6992 drm_mode_set_name(mode);
babea61d
JB
6993}
6994
84b046f3
DV
6995static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
6996{
6315b5d3 6997 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
84b046f3
DV
6998 uint32_t pipeconf;
6999
9f11a9e4 7000 pipeconf = 0;
84b046f3 7001
b6b5d049
VS
7002 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7003 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7004 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
67c72a12 7005
6e3c9717 7006 if (intel_crtc->config->double_wide)
cf532bb2 7007 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 7008
ff9ce46e 7009 /* only g4x and later have fancy bpc/dither controls */
9beb5fea
TU
7010 if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
7011 IS_CHERRYVIEW(dev_priv)) {
ff9ce46e 7012 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6e3c9717 7013 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
ff9ce46e 7014 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 7015 PIPECONF_DITHER_TYPE_SP;
84b046f3 7016
6e3c9717 7017 switch (intel_crtc->config->pipe_bpp) {
ff9ce46e
DV
7018 case 18:
7019 pipeconf |= PIPECONF_6BPC;
7020 break;
7021 case 24:
7022 pipeconf |= PIPECONF_8BPC;
7023 break;
7024 case 30:
7025 pipeconf |= PIPECONF_10BPC;
7026 break;
7027 default:
7028 /* Case prevented by intel_choose_pipe_bpp_dither. */
7029 BUG();
84b046f3
DV
7030 }
7031 }
7032
56b857a5 7033 if (HAS_PIPE_CXSR(dev_priv)) {
84b046f3
DV
7034 if (intel_crtc->lowfreq_avail) {
7035 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7036 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7037 } else {
7038 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
84b046f3
DV
7039 }
7040 }
7041
6e3c9717 7042 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
6315b5d3 7043 if (INTEL_GEN(dev_priv) < 4 ||
2d84d2b3 7044 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
efc2cfff
VS
7045 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7046 else
7047 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7048 } else
84b046f3
DV
7049 pipeconf |= PIPECONF_PROGRESSIVE;
7050
920a14b2 7051 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
666a4537 7052 intel_crtc->config->limited_color_range)
9f11a9e4 7053 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 7054
84b046f3
DV
7055 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7056 POSTING_READ(PIPECONF(intel_crtc->pipe));
7057}
7058
81c97f52
ACO
7059static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
7060 struct intel_crtc_state *crtc_state)
7061{
7062 struct drm_device *dev = crtc->base.dev;
fac5e23e 7063 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 7064 const struct intel_limit *limit;
81c97f52
ACO
7065 int refclk = 48000;
7066
7067 memset(&crtc_state->dpll_hw_state, 0,
7068 sizeof(crtc_state->dpll_hw_state));
7069
2d84d2b3 7070 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
81c97f52
ACO
7071 if (intel_panel_use_ssc(dev_priv)) {
7072 refclk = dev_priv->vbt.lvds_ssc_freq;
7073 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7074 }
7075
7076 limit = &intel_limits_i8xx_lvds;
2d84d2b3 7077 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
81c97f52
ACO
7078 limit = &intel_limits_i8xx_dvo;
7079 } else {
7080 limit = &intel_limits_i8xx_dac;
7081 }
7082
7083 if (!crtc_state->clock_set &&
7084 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7085 refclk, NULL, &crtc_state->dpll)) {
7086 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7087 return -EINVAL;
7088 }
7089
7090 i8xx_compute_dpll(crtc, crtc_state, NULL);
7091
7092 return 0;
7093}
7094
19ec6693
ACO
7095static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
7096 struct intel_crtc_state *crtc_state)
7097{
7098 struct drm_device *dev = crtc->base.dev;
fac5e23e 7099 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 7100 const struct intel_limit *limit;
19ec6693
ACO
7101 int refclk = 96000;
7102
7103 memset(&crtc_state->dpll_hw_state, 0,
7104 sizeof(crtc_state->dpll_hw_state));
7105
2d84d2b3 7106 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
19ec6693
ACO
7107 if (intel_panel_use_ssc(dev_priv)) {
7108 refclk = dev_priv->vbt.lvds_ssc_freq;
7109 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7110 }
7111
7112 if (intel_is_dual_link_lvds(dev))
7113 limit = &intel_limits_g4x_dual_channel_lvds;
7114 else
7115 limit = &intel_limits_g4x_single_channel_lvds;
2d84d2b3
VS
7116 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
7117 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
19ec6693 7118 limit = &intel_limits_g4x_hdmi;
2d84d2b3 7119 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
19ec6693
ACO
7120 limit = &intel_limits_g4x_sdvo;
7121 } else {
7122 /* The option is for other outputs */
7123 limit = &intel_limits_i9xx_sdvo;
7124 }
7125
7126 if (!crtc_state->clock_set &&
7127 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7128 refclk, NULL, &crtc_state->dpll)) {
7129 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7130 return -EINVAL;
7131 }
7132
7133 i9xx_compute_dpll(crtc, crtc_state, NULL);
7134
7135 return 0;
7136}
7137
70e8aa21
ACO
7138static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
7139 struct intel_crtc_state *crtc_state)
7140{
7141 struct drm_device *dev = crtc->base.dev;
fac5e23e 7142 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 7143 const struct intel_limit *limit;
70e8aa21
ACO
7144 int refclk = 96000;
7145
7146 memset(&crtc_state->dpll_hw_state, 0,
7147 sizeof(crtc_state->dpll_hw_state));
7148
2d84d2b3 7149 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
70e8aa21
ACO
7150 if (intel_panel_use_ssc(dev_priv)) {
7151 refclk = dev_priv->vbt.lvds_ssc_freq;
7152 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7153 }
7154
7155 limit = &intel_limits_pineview_lvds;
7156 } else {
7157 limit = &intel_limits_pineview_sdvo;
7158 }
7159
7160 if (!crtc_state->clock_set &&
7161 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7162 refclk, NULL, &crtc_state->dpll)) {
7163 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7164 return -EINVAL;
7165 }
7166
7167 i9xx_compute_dpll(crtc, crtc_state, NULL);
7168
7169 return 0;
7170}
7171
190f68c5
ACO
7172static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7173 struct intel_crtc_state *crtc_state)
79e53945 7174{
c7653199 7175 struct drm_device *dev = crtc->base.dev;
fac5e23e 7176 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 7177 const struct intel_limit *limit;
81c97f52 7178 int refclk = 96000;
79e53945 7179
dd3cd74a
ACO
7180 memset(&crtc_state->dpll_hw_state, 0,
7181 sizeof(crtc_state->dpll_hw_state));
7182
2d84d2b3 7183 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
70e8aa21
ACO
7184 if (intel_panel_use_ssc(dev_priv)) {
7185 refclk = dev_priv->vbt.lvds_ssc_freq;
7186 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7187 }
43565a06 7188
70e8aa21
ACO
7189 limit = &intel_limits_i9xx_lvds;
7190 } else {
7191 limit = &intel_limits_i9xx_sdvo;
81c97f52 7192 }
79e53945 7193
70e8aa21
ACO
7194 if (!crtc_state->clock_set &&
7195 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7196 refclk, NULL, &crtc_state->dpll)) {
7197 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7198 return -EINVAL;
f47709a9 7199 }
7026d4ac 7200
81c97f52 7201 i9xx_compute_dpll(crtc, crtc_state, NULL);
79e53945 7202
c8f7a0db 7203 return 0;
f564048e
EA
7204}
7205
65b3d6a9
ACO
7206static int chv_crtc_compute_clock(struct intel_crtc *crtc,
7207 struct intel_crtc_state *crtc_state)
7208{
7209 int refclk = 100000;
1b6f4958 7210 const struct intel_limit *limit = &intel_limits_chv;
65b3d6a9
ACO
7211
7212 memset(&crtc_state->dpll_hw_state, 0,
7213 sizeof(crtc_state->dpll_hw_state));
7214
65b3d6a9
ACO
7215 if (!crtc_state->clock_set &&
7216 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7217 refclk, NULL, &crtc_state->dpll)) {
7218 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7219 return -EINVAL;
7220 }
7221
7222 chv_compute_dpll(crtc, crtc_state);
7223
7224 return 0;
7225}
7226
7227static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
7228 struct intel_crtc_state *crtc_state)
7229{
7230 int refclk = 100000;
1b6f4958 7231 const struct intel_limit *limit = &intel_limits_vlv;
65b3d6a9
ACO
7232
7233 memset(&crtc_state->dpll_hw_state, 0,
7234 sizeof(crtc_state->dpll_hw_state));
7235
65b3d6a9
ACO
7236 if (!crtc_state->clock_set &&
7237 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7238 refclk, NULL, &crtc_state->dpll)) {
7239 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7240 return -EINVAL;
7241 }
7242
7243 vlv_compute_dpll(crtc, crtc_state);
7244
7245 return 0;
7246}
7247
2fa2fe9a 7248static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5cec258b 7249 struct intel_crtc_state *pipe_config)
2fa2fe9a 7250{
6315b5d3 7251 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2fa2fe9a
DV
7252 uint32_t tmp;
7253
50a0bc90
TU
7254 if (INTEL_GEN(dev_priv) <= 3 &&
7255 (IS_I830(dev_priv) || !IS_MOBILE(dev_priv)))
dc9e7dec
VS
7256 return;
7257
2fa2fe9a 7258 tmp = I915_READ(PFIT_CONTROL);
06922821
DV
7259 if (!(tmp & PFIT_ENABLE))
7260 return;
2fa2fe9a 7261
06922821 7262 /* Check whether the pfit is attached to our pipe. */
6315b5d3 7263 if (INTEL_GEN(dev_priv) < 4) {
2fa2fe9a
DV
7264 if (crtc->pipe != PIPE_B)
7265 return;
2fa2fe9a
DV
7266 } else {
7267 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7268 return;
7269 }
7270
06922821 7271 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a 7272 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
2fa2fe9a
DV
7273}
7274
acbec814 7275static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 7276 struct intel_crtc_state *pipe_config)
acbec814
JB
7277{
7278 struct drm_device *dev = crtc->base.dev;
fac5e23e 7279 struct drm_i915_private *dev_priv = to_i915(dev);
acbec814 7280 int pipe = pipe_config->cpu_transcoder;
9e2c8475 7281 struct dpll clock;
acbec814 7282 u32 mdiv;
662c6ecb 7283 int refclk = 100000;
acbec814 7284
b521973b
VS
7285 /* In case of DSI, DPLL will not be used */
7286 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
f573de5a
SK
7287 return;
7288
a580516d 7289 mutex_lock(&dev_priv->sb_lock);
ab3c759a 7290 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
a580516d 7291 mutex_unlock(&dev_priv->sb_lock);
acbec814
JB
7292
7293 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7294 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7295 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7296 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7297 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7298
dccbea3b 7299 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
acbec814
JB
7300}
7301
5724dbd1
DL
7302static void
7303i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7304 struct intel_initial_plane_config *plane_config)
1ad292b5
JB
7305{
7306 struct drm_device *dev = crtc->base.dev;
fac5e23e 7307 struct drm_i915_private *dev_priv = to_i915(dev);
1ad292b5
JB
7308 u32 val, base, offset;
7309 int pipe = crtc->pipe, plane = crtc->plane;
7310 int fourcc, pixel_format;
6761dd31 7311 unsigned int aligned_height;
b113d5ee 7312 struct drm_framebuffer *fb;
1b842c89 7313 struct intel_framebuffer *intel_fb;
1ad292b5 7314
42a7b088
DL
7315 val = I915_READ(DSPCNTR(plane));
7316 if (!(val & DISPLAY_PLANE_ENABLE))
7317 return;
7318
d9806c9f 7319 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 7320 if (!intel_fb) {
1ad292b5
JB
7321 DRM_DEBUG_KMS("failed to alloc fb\n");
7322 return;
7323 }
7324
1b842c89
DL
7325 fb = &intel_fb->base;
7326
d2e9f5fc
VS
7327 fb->dev = dev;
7328
6315b5d3 7329 if (INTEL_GEN(dev_priv) >= 4) {
18c5247e 7330 if (val & DISPPLANE_TILED) {
49af449b 7331 plane_config->tiling = I915_TILING_X;
bae781b2 7332 fb->modifier = I915_FORMAT_MOD_X_TILED;
18c5247e
DV
7333 }
7334 }
1ad292b5
JB
7335
7336 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 7337 fourcc = i9xx_format_to_fourcc(pixel_format);
2f3f4763 7338 fb->format = drm_format_info(fourcc);
1ad292b5 7339
6315b5d3 7340 if (INTEL_GEN(dev_priv) >= 4) {
49af449b 7341 if (plane_config->tiling)
1ad292b5
JB
7342 offset = I915_READ(DSPTILEOFF(plane));
7343 else
7344 offset = I915_READ(DSPLINOFF(plane));
7345 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7346 } else {
7347 base = I915_READ(DSPADDR(plane));
7348 }
7349 plane_config->base = base;
7350
7351 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
7352 fb->width = ((val >> 16) & 0xfff) + 1;
7353 fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
7354
7355 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 7356 fb->pitches[0] = val & 0xffffffc0;
1ad292b5 7357
24dbf51a
CW
7358 aligned_height = intel_fb_align_height(dev_priv,
7359 fb->height,
438b74a5 7360 fb->format->format,
bae781b2 7361 fb->modifier);
1ad292b5 7362
f37b5c2b 7363 plane_config->size = fb->pitches[0] * aligned_height;
1ad292b5 7364
2844a921
DL
7365 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7366 pipe_name(pipe), plane, fb->width, fb->height,
272725c7 7367 fb->format->cpp[0] * 8, base, fb->pitches[0],
2844a921 7368 plane_config->size);
1ad292b5 7369
2d14030b 7370 plane_config->fb = intel_fb;
1ad292b5
JB
7371}
7372
70b23a98 7373static void chv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 7374 struct intel_crtc_state *pipe_config)
70b23a98
VS
7375{
7376 struct drm_device *dev = crtc->base.dev;
fac5e23e 7377 struct drm_i915_private *dev_priv = to_i915(dev);
70b23a98
VS
7378 int pipe = pipe_config->cpu_transcoder;
7379 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9e2c8475 7380 struct dpll clock;
0d7b6b11 7381 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
70b23a98
VS
7382 int refclk = 100000;
7383
b521973b
VS
7384 /* In case of DSI, DPLL will not be used */
7385 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7386 return;
7387
a580516d 7388 mutex_lock(&dev_priv->sb_lock);
70b23a98
VS
7389 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
7390 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
7391 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
7392 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
0d7b6b11 7393 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
a580516d 7394 mutex_unlock(&dev_priv->sb_lock);
70b23a98
VS
7395
7396 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
0d7b6b11
ID
7397 clock.m2 = (pll_dw0 & 0xff) << 22;
7398 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
7399 clock.m2 |= pll_dw2 & 0x3fffff;
70b23a98
VS
7400 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
7401 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
7402 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
7403
dccbea3b 7404 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
70b23a98
VS
7405}
7406
0e8ffe1b 7407static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5cec258b 7408 struct intel_crtc_state *pipe_config)
0e8ffe1b 7409{
6315b5d3 7410 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1729050e 7411 enum intel_display_power_domain power_domain;
0e8ffe1b 7412 uint32_t tmp;
1729050e 7413 bool ret;
0e8ffe1b 7414
1729050e
ID
7415 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
7416 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0
ID
7417 return false;
7418
e143a21c 7419 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 7420 pipe_config->shared_dpll = NULL;
eccb140b 7421
1729050e
ID
7422 ret = false;
7423
0e8ffe1b
DV
7424 tmp = I915_READ(PIPECONF(crtc->pipe));
7425 if (!(tmp & PIPECONF_ENABLE))
1729050e 7426 goto out;
0e8ffe1b 7427
9beb5fea
TU
7428 if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
7429 IS_CHERRYVIEW(dev_priv)) {
42571aef
VS
7430 switch (tmp & PIPECONF_BPC_MASK) {
7431 case PIPECONF_6BPC:
7432 pipe_config->pipe_bpp = 18;
7433 break;
7434 case PIPECONF_8BPC:
7435 pipe_config->pipe_bpp = 24;
7436 break;
7437 case PIPECONF_10BPC:
7438 pipe_config->pipe_bpp = 30;
7439 break;
7440 default:
7441 break;
7442 }
7443 }
7444
920a14b2 7445 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
666a4537 7446 (tmp & PIPECONF_COLOR_RANGE_SELECT))
b5a9fa09
DV
7447 pipe_config->limited_color_range = true;
7448
6315b5d3 7449 if (INTEL_GEN(dev_priv) < 4)
282740f7
VS
7450 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
7451
1bd1bd80 7452 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 7453 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 7454
2fa2fe9a
DV
7455 i9xx_get_pfit_config(crtc, pipe_config);
7456
6315b5d3 7457 if (INTEL_GEN(dev_priv) >= 4) {
c231775c 7458 /* No way to read it out on pipes B and C */
920a14b2 7459 if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
c231775c
VS
7460 tmp = dev_priv->chv_dpll_md[crtc->pipe];
7461 else
7462 tmp = I915_READ(DPLL_MD(crtc->pipe));
6c49f241
DV
7463 pipe_config->pixel_multiplier =
7464 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
7465 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 7466 pipe_config->dpll_hw_state.dpll_md = tmp;
50a0bc90 7467 } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
73f67aa8 7468 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
6c49f241
DV
7469 tmp = I915_READ(DPLL(crtc->pipe));
7470 pipe_config->pixel_multiplier =
7471 ((tmp & SDVO_MULTIPLIER_MASK)
7472 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
7473 } else {
7474 /* Note that on i915G/GM the pixel multiplier is in the sdvo
7475 * port and will be fixed up in the encoder->get_config
7476 * function. */
7477 pipe_config->pixel_multiplier = 1;
7478 }
8bcc2795 7479 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
920a14b2 7480 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
1c4e0274
VS
7481 /*
7482 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
7483 * on 830. Filter it out here so that we don't
7484 * report errors due to that.
7485 */
50a0bc90 7486 if (IS_I830(dev_priv))
1c4e0274
VS
7487 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
7488
8bcc2795
DV
7489 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
7490 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
7491 } else {
7492 /* Mask out read-only status bits. */
7493 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
7494 DPLL_PORTC_READY_MASK |
7495 DPLL_PORTB_READY_MASK);
8bcc2795 7496 }
6c49f241 7497
920a14b2 7498 if (IS_CHERRYVIEW(dev_priv))
70b23a98 7499 chv_crtc_clock_get(crtc, pipe_config);
11a914c2 7500 else if (IS_VALLEYVIEW(dev_priv))
acbec814
JB
7501 vlv_crtc_clock_get(crtc, pipe_config);
7502 else
7503 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 7504
0f64614d
VS
7505 /*
7506 * Normally the dotclock is filled in by the encoder .get_config()
7507 * but in case the pipe is enabled w/o any ports we need a sane
7508 * default.
7509 */
7510 pipe_config->base.adjusted_mode.crtc_clock =
7511 pipe_config->port_clock / pipe_config->pixel_multiplier;
7512
1729050e
ID
7513 ret = true;
7514
7515out:
7516 intel_display_power_put(dev_priv, power_domain);
7517
7518 return ret;
0e8ffe1b
DV
7519}
7520
c39055b0 7521static void ironlake_init_pch_refclk(struct drm_i915_private *dev_priv)
13d83a67 7522{
13d83a67 7523 struct intel_encoder *encoder;
1c1a24d2 7524 int i;
74cfd7ac 7525 u32 val, final;
13d83a67 7526 bool has_lvds = false;
199e5d79 7527 bool has_cpu_edp = false;
199e5d79 7528 bool has_panel = false;
99eb6a01
KP
7529 bool has_ck505 = false;
7530 bool can_ssc = false;
1c1a24d2 7531 bool using_ssc_source = false;
13d83a67
JB
7532
7533 /* We need to take the global config into account */
c39055b0 7534 for_each_intel_encoder(&dev_priv->drm, encoder) {
199e5d79
KP
7535 switch (encoder->type) {
7536 case INTEL_OUTPUT_LVDS:
7537 has_panel = true;
7538 has_lvds = true;
7539 break;
7540 case INTEL_OUTPUT_EDP:
7541 has_panel = true;
2de6905f 7542 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
7543 has_cpu_edp = true;
7544 break;
6847d71b
PZ
7545 default:
7546 break;
13d83a67
JB
7547 }
7548 }
7549
6e266956 7550 if (HAS_PCH_IBX(dev_priv)) {
41aa3448 7551 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
7552 can_ssc = has_ck505;
7553 } else {
7554 has_ck505 = false;
7555 can_ssc = true;
7556 }
7557
1c1a24d2
L
7558 /* Check if any DPLLs are using the SSC source */
7559 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
7560 u32 temp = I915_READ(PCH_DPLL(i));
7561
7562 if (!(temp & DPLL_VCO_ENABLE))
7563 continue;
7564
7565 if ((temp & PLL_REF_INPUT_MASK) ==
7566 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
7567 using_ssc_source = true;
7568 break;
7569 }
7570 }
7571
7572 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
7573 has_panel, has_lvds, has_ck505, using_ssc_source);
13d83a67
JB
7574
7575 /* Ironlake: try to setup display ref clock before DPLL
7576 * enabling. This is only under driver's control after
7577 * PCH B stepping, previous chipset stepping should be
7578 * ignoring this setting.
7579 */
74cfd7ac
CW
7580 val = I915_READ(PCH_DREF_CONTROL);
7581
7582 /* As we must carefully and slowly disable/enable each source in turn,
7583 * compute the final state we want first and check if we need to
7584 * make any changes at all.
7585 */
7586 final = val;
7587 final &= ~DREF_NONSPREAD_SOURCE_MASK;
7588 if (has_ck505)
7589 final |= DREF_NONSPREAD_CK505_ENABLE;
7590 else
7591 final |= DREF_NONSPREAD_SOURCE_ENABLE;
7592
8c07eb68 7593 final &= ~DREF_SSC_SOURCE_MASK;
74cfd7ac 7594 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8c07eb68 7595 final &= ~DREF_SSC1_ENABLE;
74cfd7ac
CW
7596
7597 if (has_panel) {
7598 final |= DREF_SSC_SOURCE_ENABLE;
7599
7600 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7601 final |= DREF_SSC1_ENABLE;
7602
7603 if (has_cpu_edp) {
7604 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7605 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
7606 else
7607 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
7608 } else
7609 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
1c1a24d2
L
7610 } else if (using_ssc_source) {
7611 final |= DREF_SSC_SOURCE_ENABLE;
7612 final |= DREF_SSC1_ENABLE;
74cfd7ac
CW
7613 }
7614
7615 if (final == val)
7616 return;
7617
13d83a67 7618 /* Always enable nonspread source */
74cfd7ac 7619 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 7620
99eb6a01 7621 if (has_ck505)
74cfd7ac 7622 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 7623 else
74cfd7ac 7624 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 7625
199e5d79 7626 if (has_panel) {
74cfd7ac
CW
7627 val &= ~DREF_SSC_SOURCE_MASK;
7628 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 7629
199e5d79 7630 /* SSC must be turned on before enabling the CPU output */
99eb6a01 7631 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 7632 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 7633 val |= DREF_SSC1_ENABLE;
e77166b5 7634 } else
74cfd7ac 7635 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
7636
7637 /* Get SSC going before enabling the outputs */
74cfd7ac 7638 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
7639 POSTING_READ(PCH_DREF_CONTROL);
7640 udelay(200);
7641
74cfd7ac 7642 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
7643
7644 /* Enable CPU source on CPU attached eDP */
199e5d79 7645 if (has_cpu_edp) {
99eb6a01 7646 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 7647 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 7648 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 7649 } else
74cfd7ac 7650 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 7651 } else
74cfd7ac 7652 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 7653
74cfd7ac 7654 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
7655 POSTING_READ(PCH_DREF_CONTROL);
7656 udelay(200);
7657 } else {
1c1a24d2 7658 DRM_DEBUG_KMS("Disabling CPU source output\n");
199e5d79 7659
74cfd7ac 7660 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
7661
7662 /* Turn off CPU output */
74cfd7ac 7663 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 7664
74cfd7ac 7665 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
7666 POSTING_READ(PCH_DREF_CONTROL);
7667 udelay(200);
7668
1c1a24d2
L
7669 if (!using_ssc_source) {
7670 DRM_DEBUG_KMS("Disabling SSC source\n");
199e5d79 7671
1c1a24d2
L
7672 /* Turn off the SSC source */
7673 val &= ~DREF_SSC_SOURCE_MASK;
7674 val |= DREF_SSC_SOURCE_DISABLE;
f165d283 7675
1c1a24d2
L
7676 /* Turn off SSC1 */
7677 val &= ~DREF_SSC1_ENABLE;
7678
7679 I915_WRITE(PCH_DREF_CONTROL, val);
7680 POSTING_READ(PCH_DREF_CONTROL);
7681 udelay(200);
7682 }
13d83a67 7683 }
74cfd7ac
CW
7684
7685 BUG_ON(val != final);
13d83a67
JB
7686}
7687
f31f2d55 7688static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 7689{
f31f2d55 7690 uint32_t tmp;
dde86e2d 7691
0ff066a9
PZ
7692 tmp = I915_READ(SOUTH_CHICKEN2);
7693 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
7694 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 7695
cf3598c2
ID
7696 if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
7697 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
0ff066a9 7698 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 7699
0ff066a9
PZ
7700 tmp = I915_READ(SOUTH_CHICKEN2);
7701 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
7702 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 7703
cf3598c2
ID
7704 if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
7705 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
0ff066a9 7706 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
7707}
7708
7709/* WaMPhyProgramming:hsw */
7710static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
7711{
7712 uint32_t tmp;
dde86e2d
PZ
7713
7714 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
7715 tmp &= ~(0xFF << 24);
7716 tmp |= (0x12 << 24);
7717 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
7718
dde86e2d
PZ
7719 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
7720 tmp |= (1 << 11);
7721 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
7722
7723 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
7724 tmp |= (1 << 11);
7725 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
7726
dde86e2d
PZ
7727 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
7728 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7729 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
7730
7731 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
7732 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7733 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
7734
0ff066a9
PZ
7735 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
7736 tmp &= ~(7 << 13);
7737 tmp |= (5 << 13);
7738 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 7739
0ff066a9
PZ
7740 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
7741 tmp &= ~(7 << 13);
7742 tmp |= (5 << 13);
7743 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
7744
7745 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
7746 tmp &= ~0xFF;
7747 tmp |= 0x1C;
7748 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
7749
7750 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
7751 tmp &= ~0xFF;
7752 tmp |= 0x1C;
7753 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
7754
7755 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
7756 tmp &= ~(0xFF << 16);
7757 tmp |= (0x1C << 16);
7758 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
7759
7760 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
7761 tmp &= ~(0xFF << 16);
7762 tmp |= (0x1C << 16);
7763 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
7764
0ff066a9
PZ
7765 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
7766 tmp |= (1 << 27);
7767 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 7768
0ff066a9
PZ
7769 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
7770 tmp |= (1 << 27);
7771 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 7772
0ff066a9
PZ
7773 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
7774 tmp &= ~(0xF << 28);
7775 tmp |= (4 << 28);
7776 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 7777
0ff066a9
PZ
7778 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
7779 tmp &= ~(0xF << 28);
7780 tmp |= (4 << 28);
7781 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
7782}
7783
2fa86a1f
PZ
7784/* Implements 3 different sequences from BSpec chapter "Display iCLK
7785 * Programming" based on the parameters passed:
7786 * - Sequence to enable CLKOUT_DP
7787 * - Sequence to enable CLKOUT_DP without spread
7788 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
7789 */
c39055b0
ACO
7790static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
7791 bool with_spread, bool with_fdi)
f31f2d55 7792{
2fa86a1f
PZ
7793 uint32_t reg, tmp;
7794
7795 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
7796 with_spread = true;
4f8036a2
TU
7797 if (WARN(HAS_PCH_LPT_LP(dev_priv) &&
7798 with_fdi, "LP PCH doesn't have FDI\n"))
2fa86a1f 7799 with_fdi = false;
f31f2d55 7800
a580516d 7801 mutex_lock(&dev_priv->sb_lock);
f31f2d55
PZ
7802
7803 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7804 tmp &= ~SBI_SSCCTL_DISABLE;
7805 tmp |= SBI_SSCCTL_PATHALT;
7806 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7807
7808 udelay(24);
7809
2fa86a1f
PZ
7810 if (with_spread) {
7811 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7812 tmp &= ~SBI_SSCCTL_PATHALT;
7813 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 7814
2fa86a1f
PZ
7815 if (with_fdi) {
7816 lpt_reset_fdi_mphy(dev_priv);
7817 lpt_program_fdi_mphy(dev_priv);
7818 }
7819 }
dde86e2d 7820
4f8036a2 7821 reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
2fa86a1f
PZ
7822 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
7823 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
7824 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246 7825
a580516d 7826 mutex_unlock(&dev_priv->sb_lock);
dde86e2d
PZ
7827}
7828
47701c3b 7829/* Sequence to disable CLKOUT_DP */
c39055b0 7830static void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
47701c3b 7831{
47701c3b
PZ
7832 uint32_t reg, tmp;
7833
a580516d 7834 mutex_lock(&dev_priv->sb_lock);
47701c3b 7835
4f8036a2 7836 reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
47701c3b
PZ
7837 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
7838 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
7839 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
7840
7841 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7842 if (!(tmp & SBI_SSCCTL_DISABLE)) {
7843 if (!(tmp & SBI_SSCCTL_PATHALT)) {
7844 tmp |= SBI_SSCCTL_PATHALT;
7845 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7846 udelay(32);
7847 }
7848 tmp |= SBI_SSCCTL_DISABLE;
7849 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7850 }
7851
a580516d 7852 mutex_unlock(&dev_priv->sb_lock);
47701c3b
PZ
7853}
7854
f7be2c21
VS
7855#define BEND_IDX(steps) ((50 + (steps)) / 5)
7856
7857static const uint16_t sscdivintphase[] = {
7858 [BEND_IDX( 50)] = 0x3B23,
7859 [BEND_IDX( 45)] = 0x3B23,
7860 [BEND_IDX( 40)] = 0x3C23,
7861 [BEND_IDX( 35)] = 0x3C23,
7862 [BEND_IDX( 30)] = 0x3D23,
7863 [BEND_IDX( 25)] = 0x3D23,
7864 [BEND_IDX( 20)] = 0x3E23,
7865 [BEND_IDX( 15)] = 0x3E23,
7866 [BEND_IDX( 10)] = 0x3F23,
7867 [BEND_IDX( 5)] = 0x3F23,
7868 [BEND_IDX( 0)] = 0x0025,
7869 [BEND_IDX( -5)] = 0x0025,
7870 [BEND_IDX(-10)] = 0x0125,
7871 [BEND_IDX(-15)] = 0x0125,
7872 [BEND_IDX(-20)] = 0x0225,
7873 [BEND_IDX(-25)] = 0x0225,
7874 [BEND_IDX(-30)] = 0x0325,
7875 [BEND_IDX(-35)] = 0x0325,
7876 [BEND_IDX(-40)] = 0x0425,
7877 [BEND_IDX(-45)] = 0x0425,
7878 [BEND_IDX(-50)] = 0x0525,
7879};
7880
7881/*
7882 * Bend CLKOUT_DP
7883 * steps -50 to 50 inclusive, in steps of 5
7884 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
7885 * change in clock period = -(steps / 10) * 5.787 ps
7886 */
7887static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
7888{
7889 uint32_t tmp;
7890 int idx = BEND_IDX(steps);
7891
7892 if (WARN_ON(steps % 5 != 0))
7893 return;
7894
7895 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
7896 return;
7897
7898 mutex_lock(&dev_priv->sb_lock);
7899
7900 if (steps % 10 != 0)
7901 tmp = 0xAAAAAAAB;
7902 else
7903 tmp = 0x00000000;
7904 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
7905
7906 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
7907 tmp &= 0xffff0000;
7908 tmp |= sscdivintphase[idx];
7909 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
7910
7911 mutex_unlock(&dev_priv->sb_lock);
7912}
7913
7914#undef BEND_IDX
7915
c39055b0 7916static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
bf8fa3d3 7917{
bf8fa3d3
PZ
7918 struct intel_encoder *encoder;
7919 bool has_vga = false;
7920
c39055b0 7921 for_each_intel_encoder(&dev_priv->drm, encoder) {
bf8fa3d3
PZ
7922 switch (encoder->type) {
7923 case INTEL_OUTPUT_ANALOG:
7924 has_vga = true;
7925 break;
6847d71b
PZ
7926 default:
7927 break;
bf8fa3d3
PZ
7928 }
7929 }
7930
f7be2c21 7931 if (has_vga) {
c39055b0
ACO
7932 lpt_bend_clkout_dp(dev_priv, 0);
7933 lpt_enable_clkout_dp(dev_priv, true, true);
f7be2c21 7934 } else {
c39055b0 7935 lpt_disable_clkout_dp(dev_priv);
f7be2c21 7936 }
bf8fa3d3
PZ
7937}
7938
dde86e2d
PZ
7939/*
7940 * Initialize reference clocks when the driver loads
7941 */
c39055b0 7942void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
dde86e2d 7943{
6e266956 7944 if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
c39055b0 7945 ironlake_init_pch_refclk(dev_priv);
6e266956 7946 else if (HAS_PCH_LPT(dev_priv))
c39055b0 7947 lpt_init_pch_refclk(dev_priv);
dde86e2d
PZ
7948}
7949
6ff93609 7950static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 7951{
fac5e23e 7952 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
79e53945
JB
7953 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7954 int pipe = intel_crtc->pipe;
c8203565
PZ
7955 uint32_t val;
7956
78114071 7957 val = 0;
c8203565 7958
6e3c9717 7959 switch (intel_crtc->config->pipe_bpp) {
c8203565 7960 case 18:
dfd07d72 7961 val |= PIPECONF_6BPC;
c8203565
PZ
7962 break;
7963 case 24:
dfd07d72 7964 val |= PIPECONF_8BPC;
c8203565
PZ
7965 break;
7966 case 30:
dfd07d72 7967 val |= PIPECONF_10BPC;
c8203565
PZ
7968 break;
7969 case 36:
dfd07d72 7970 val |= PIPECONF_12BPC;
c8203565
PZ
7971 break;
7972 default:
cc769b62
PZ
7973 /* Case prevented by intel_choose_pipe_bpp_dither. */
7974 BUG();
c8203565
PZ
7975 }
7976
6e3c9717 7977 if (intel_crtc->config->dither)
c8203565
PZ
7978 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
7979
6e3c9717 7980 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
7981 val |= PIPECONF_INTERLACED_ILK;
7982 else
7983 val |= PIPECONF_PROGRESSIVE;
7984
6e3c9717 7985 if (intel_crtc->config->limited_color_range)
3685a8f3 7986 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 7987
c8203565
PZ
7988 I915_WRITE(PIPECONF(pipe), val);
7989 POSTING_READ(PIPECONF(pipe));
7990}
7991
6ff93609 7992static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 7993{
fac5e23e 7994 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
ee2b0b38 7995 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 7996 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
391bf048 7997 u32 val = 0;
ee2b0b38 7998
391bf048 7999 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
ee2b0b38
PZ
8000 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8001
6e3c9717 8002 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
8003 val |= PIPECONF_INTERLACED_ILK;
8004 else
8005 val |= PIPECONF_PROGRESSIVE;
8006
702e7a56
PZ
8007 I915_WRITE(PIPECONF(cpu_transcoder), val);
8008 POSTING_READ(PIPECONF(cpu_transcoder));
391bf048
JN
8009}
8010
391bf048
JN
8011static void haswell_set_pipemisc(struct drm_crtc *crtc)
8012{
fac5e23e 8013 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
391bf048 8014 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756f85cf 8015
391bf048
JN
8016 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
8017 u32 val = 0;
756f85cf 8018
6e3c9717 8019 switch (intel_crtc->config->pipe_bpp) {
756f85cf
PZ
8020 case 18:
8021 val |= PIPEMISC_DITHER_6_BPC;
8022 break;
8023 case 24:
8024 val |= PIPEMISC_DITHER_8_BPC;
8025 break;
8026 case 30:
8027 val |= PIPEMISC_DITHER_10_BPC;
8028 break;
8029 case 36:
8030 val |= PIPEMISC_DITHER_12_BPC;
8031 break;
8032 default:
8033 /* Case prevented by pipe_config_set_bpp. */
8034 BUG();
8035 }
8036
6e3c9717 8037 if (intel_crtc->config->dither)
756f85cf
PZ
8038 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8039
391bf048 8040 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
756f85cf 8041 }
ee2b0b38
PZ
8042}
8043
d4b1931c
PZ
8044int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8045{
8046 /*
8047 * Account for spread spectrum to avoid
8048 * oversubscribing the link. Max center spread
8049 * is 2.5%; use 5% for safety's sake.
8050 */
8051 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 8052 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
8053}
8054
7429e9d4 8055static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 8056{
7429e9d4 8057 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
8058}
8059
b75ca6f6
ACO
8060static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
8061 struct intel_crtc_state *crtc_state,
9e2c8475 8062 struct dpll *reduced_clock)
79e53945 8063{
de13a2e3 8064 struct drm_crtc *crtc = &intel_crtc->base;
79e53945 8065 struct drm_device *dev = crtc->dev;
fac5e23e 8066 struct drm_i915_private *dev_priv = to_i915(dev);
b75ca6f6 8067 u32 dpll, fp, fp2;
3d6e9ee0 8068 int factor;
79e53945 8069
c1858123 8070 /* Enable autotuning of the PLL clock (if permissible) */
8febb297 8071 factor = 21;
3d6e9ee0 8072 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8febb297 8073 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 8074 dev_priv->vbt.lvds_ssc_freq == 100000) ||
6e266956 8075 (HAS_PCH_IBX(dev_priv) && intel_is_dual_link_lvds(dev)))
8febb297 8076 factor = 25;
190f68c5 8077 } else if (crtc_state->sdvo_tv_clock)
8febb297 8078 factor = 20;
c1858123 8079
b75ca6f6
ACO
8080 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
8081
190f68c5 8082 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
b75ca6f6
ACO
8083 fp |= FP_CB_TUNE;
8084
8085 if (reduced_clock) {
8086 fp2 = i9xx_dpll_compute_fp(reduced_clock);
2c07245f 8087
b75ca6f6
ACO
8088 if (reduced_clock->m < factor * reduced_clock->n)
8089 fp2 |= FP_CB_TUNE;
8090 } else {
8091 fp2 = fp;
8092 }
9a7c7890 8093
5eddb70b 8094 dpll = 0;
2c07245f 8095
3d6e9ee0 8096 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
a07d6787
EA
8097 dpll |= DPLLB_MODE_LVDS;
8098 else
8099 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 8100
190f68c5 8101 dpll |= (crtc_state->pixel_multiplier - 1)
ef1b460d 8102 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f 8103
3d6e9ee0
VS
8104 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
8105 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
4a33e48d 8106 dpll |= DPLL_SDVO_HIGH_SPEED;
3d6e9ee0 8107
37a5650b 8108 if (intel_crtc_has_dp_encoder(crtc_state))
4a33e48d 8109 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 8110
7d7f8633
VS
8111 /*
8112 * The high speed IO clock is only really required for
8113 * SDVO/HDMI/DP, but we also enable it for CRT to make it
8114 * possible to share the DPLL between CRT and HDMI. Enabling
8115 * the clock needlessly does no real harm, except use up a
8116 * bit of power potentially.
8117 *
8118 * We'll limit this to IVB with 3 pipes, since it has only two
8119 * DPLLs and so DPLL sharing is the only way to get three pipes
8120 * driving PCH ports at the same time. On SNB we could do this,
8121 * and potentially avoid enabling the second DPLL, but it's not
8122 * clear if it''s a win or loss power wise. No point in doing
8123 * this on ILK at all since it has a fixed DPLL<->pipe mapping.
8124 */
8125 if (INTEL_INFO(dev_priv)->num_pipes == 3 &&
8126 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
8127 dpll |= DPLL_SDVO_HIGH_SPEED;
8128
a07d6787 8129 /* compute bitmask from p1 value */
190f68c5 8130 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 8131 /* also FPA1 */
190f68c5 8132 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 8133
190f68c5 8134 switch (crtc_state->dpll.p2) {
a07d6787
EA
8135 case 5:
8136 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8137 break;
8138 case 7:
8139 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8140 break;
8141 case 10:
8142 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8143 break;
8144 case 14:
8145 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8146 break;
79e53945
JB
8147 }
8148
3d6e9ee0
VS
8149 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8150 intel_panel_use_ssc(dev_priv))
43565a06 8151 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
8152 else
8153 dpll |= PLL_REF_INPUT_DREFCLK;
8154
b75ca6f6
ACO
8155 dpll |= DPLL_VCO_ENABLE;
8156
8157 crtc_state->dpll_hw_state.dpll = dpll;
8158 crtc_state->dpll_hw_state.fp0 = fp;
8159 crtc_state->dpll_hw_state.fp1 = fp2;
de13a2e3
PZ
8160}
8161
190f68c5
ACO
8162static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8163 struct intel_crtc_state *crtc_state)
de13a2e3 8164{
997c030c 8165 struct drm_device *dev = crtc->base.dev;
fac5e23e 8166 struct drm_i915_private *dev_priv = to_i915(dev);
9e2c8475 8167 struct dpll reduced_clock;
7ed9f894 8168 bool has_reduced_clock = false;
e2b78267 8169 struct intel_shared_dpll *pll;
1b6f4958 8170 const struct intel_limit *limit;
997c030c 8171 int refclk = 120000;
de13a2e3 8172
dd3cd74a
ACO
8173 memset(&crtc_state->dpll_hw_state, 0,
8174 sizeof(crtc_state->dpll_hw_state));
8175
ded220e2
ACO
8176 crtc->lowfreq_avail = false;
8177
8178 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
8179 if (!crtc_state->has_pch_encoder)
8180 return 0;
79e53945 8181
2d84d2b3 8182 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
997c030c
ACO
8183 if (intel_panel_use_ssc(dev_priv)) {
8184 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
8185 dev_priv->vbt.lvds_ssc_freq);
8186 refclk = dev_priv->vbt.lvds_ssc_freq;
8187 }
8188
8189 if (intel_is_dual_link_lvds(dev)) {
8190 if (refclk == 100000)
8191 limit = &intel_limits_ironlake_dual_lvds_100m;
8192 else
8193 limit = &intel_limits_ironlake_dual_lvds;
8194 } else {
8195 if (refclk == 100000)
8196 limit = &intel_limits_ironlake_single_lvds_100m;
8197 else
8198 limit = &intel_limits_ironlake_single_lvds;
8199 }
8200 } else {
8201 limit = &intel_limits_ironlake_dac;
8202 }
8203
364ee29d 8204 if (!crtc_state->clock_set &&
997c030c
ACO
8205 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8206 refclk, NULL, &crtc_state->dpll)) {
364ee29d
ACO
8207 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8208 return -EINVAL;
f47709a9 8209 }
79e53945 8210
b75ca6f6
ACO
8211 ironlake_compute_dpll(crtc, crtc_state,
8212 has_reduced_clock ? &reduced_clock : NULL);
66e985c0 8213
ded220e2
ACO
8214 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
8215 if (pll == NULL) {
8216 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
8217 pipe_name(crtc->pipe));
8218 return -EINVAL;
3fb37703 8219 }
79e53945 8220
2d84d2b3 8221 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ded220e2 8222 has_reduced_clock)
c7653199 8223 crtc->lowfreq_avail = true;
e2b78267 8224
c8f7a0db 8225 return 0;
79e53945
JB
8226}
8227
eb14cb74
VS
8228static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8229 struct intel_link_m_n *m_n)
8230{
8231 struct drm_device *dev = crtc->base.dev;
fac5e23e 8232 struct drm_i915_private *dev_priv = to_i915(dev);
eb14cb74
VS
8233 enum pipe pipe = crtc->pipe;
8234
8235 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8236 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8237 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8238 & ~TU_SIZE_MASK;
8239 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8240 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8241 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8242}
8243
8244static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8245 enum transcoder transcoder,
b95af8be
VK
8246 struct intel_link_m_n *m_n,
8247 struct intel_link_m_n *m2_n2)
72419203 8248{
6315b5d3 8249 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
eb14cb74 8250 enum pipe pipe = crtc->pipe;
72419203 8251
6315b5d3 8252 if (INTEL_GEN(dev_priv) >= 5) {
eb14cb74
VS
8253 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8254 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8255 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8256 & ~TU_SIZE_MASK;
8257 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8258 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8259 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
b95af8be
VK
8260 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8261 * gen < 8) and if DRRS is supported (to make sure the
8262 * registers are not unnecessarily read).
8263 */
6315b5d3 8264 if (m2_n2 && INTEL_GEN(dev_priv) < 8 &&
6e3c9717 8265 crtc->config->has_drrs) {
b95af8be
VK
8266 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8267 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8268 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8269 & ~TU_SIZE_MASK;
8270 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8271 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8272 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8273 }
eb14cb74
VS
8274 } else {
8275 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8276 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8277 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8278 & ~TU_SIZE_MASK;
8279 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8280 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8281 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8282 }
8283}
8284
8285void intel_dp_get_m_n(struct intel_crtc *crtc,
5cec258b 8286 struct intel_crtc_state *pipe_config)
eb14cb74 8287{
681a8504 8288 if (pipe_config->has_pch_encoder)
eb14cb74
VS
8289 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8290 else
8291 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be
VK
8292 &pipe_config->dp_m_n,
8293 &pipe_config->dp_m2_n2);
eb14cb74 8294}
72419203 8295
eb14cb74 8296static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5cec258b 8297 struct intel_crtc_state *pipe_config)
eb14cb74
VS
8298{
8299 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be 8300 &pipe_config->fdi_m_n, NULL);
72419203
DV
8301}
8302
bd2e244f 8303static void skylake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 8304 struct intel_crtc_state *pipe_config)
bd2e244f
JB
8305{
8306 struct drm_device *dev = crtc->base.dev;
fac5e23e 8307 struct drm_i915_private *dev_priv = to_i915(dev);
a1b2278e
CK
8308 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
8309 uint32_t ps_ctrl = 0;
8310 int id = -1;
8311 int i;
bd2e244f 8312
a1b2278e
CK
8313 /* find scaler attached to this pipe */
8314 for (i = 0; i < crtc->num_scalers; i++) {
8315 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
8316 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
8317 id = i;
8318 pipe_config->pch_pfit.enabled = true;
8319 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
8320 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
8321 break;
8322 }
8323 }
bd2e244f 8324
a1b2278e
CK
8325 scaler_state->scaler_id = id;
8326 if (id >= 0) {
8327 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
8328 } else {
8329 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
bd2e244f
JB
8330 }
8331}
8332
5724dbd1
DL
8333static void
8334skylake_get_initial_plane_config(struct intel_crtc *crtc,
8335 struct intel_initial_plane_config *plane_config)
bc8d7dff
DL
8336{
8337 struct drm_device *dev = crtc->base.dev;
fac5e23e 8338 struct drm_i915_private *dev_priv = to_i915(dev);
40f46283 8339 u32 val, base, offset, stride_mult, tiling;
bc8d7dff
DL
8340 int pipe = crtc->pipe;
8341 int fourcc, pixel_format;
6761dd31 8342 unsigned int aligned_height;
bc8d7dff 8343 struct drm_framebuffer *fb;
1b842c89 8344 struct intel_framebuffer *intel_fb;
bc8d7dff 8345
d9806c9f 8346 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 8347 if (!intel_fb) {
bc8d7dff
DL
8348 DRM_DEBUG_KMS("failed to alloc fb\n");
8349 return;
8350 }
8351
1b842c89
DL
8352 fb = &intel_fb->base;
8353
d2e9f5fc
VS
8354 fb->dev = dev;
8355
bc8d7dff 8356 val = I915_READ(PLANE_CTL(pipe, 0));
42a7b088
DL
8357 if (!(val & PLANE_CTL_ENABLE))
8358 goto error;
8359
bc8d7dff
DL
8360 pixel_format = val & PLANE_CTL_FORMAT_MASK;
8361 fourcc = skl_format_to_fourcc(pixel_format,
8362 val & PLANE_CTL_ORDER_RGBX,
8363 val & PLANE_CTL_ALPHA_MASK);
2f3f4763 8364 fb->format = drm_format_info(fourcc);
bc8d7dff 8365
40f46283
DL
8366 tiling = val & PLANE_CTL_TILED_MASK;
8367 switch (tiling) {
8368 case PLANE_CTL_TILED_LINEAR:
bae781b2 8369 fb->modifier = DRM_FORMAT_MOD_NONE;
40f46283
DL
8370 break;
8371 case PLANE_CTL_TILED_X:
8372 plane_config->tiling = I915_TILING_X;
bae781b2 8373 fb->modifier = I915_FORMAT_MOD_X_TILED;
40f46283
DL
8374 break;
8375 case PLANE_CTL_TILED_Y:
bae781b2 8376 fb->modifier = I915_FORMAT_MOD_Y_TILED;
40f46283
DL
8377 break;
8378 case PLANE_CTL_TILED_YF:
bae781b2 8379 fb->modifier = I915_FORMAT_MOD_Yf_TILED;
40f46283
DL
8380 break;
8381 default:
8382 MISSING_CASE(tiling);
8383 goto error;
8384 }
8385
bc8d7dff
DL
8386 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
8387 plane_config->base = base;
8388
8389 offset = I915_READ(PLANE_OFFSET(pipe, 0));
8390
8391 val = I915_READ(PLANE_SIZE(pipe, 0));
8392 fb->height = ((val >> 16) & 0xfff) + 1;
8393 fb->width = ((val >> 0) & 0x1fff) + 1;
8394
8395 val = I915_READ(PLANE_STRIDE(pipe, 0));
bae781b2 8396 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier,
438b74a5 8397 fb->format->format);
bc8d7dff
DL
8398 fb->pitches[0] = (val & 0x3ff) * stride_mult;
8399
24dbf51a
CW
8400 aligned_height = intel_fb_align_height(dev_priv,
8401 fb->height,
438b74a5 8402 fb->format->format,
bae781b2 8403 fb->modifier);
bc8d7dff 8404
f37b5c2b 8405 plane_config->size = fb->pitches[0] * aligned_height;
bc8d7dff
DL
8406
8407 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8408 pipe_name(pipe), fb->width, fb->height,
272725c7 8409 fb->format->cpp[0] * 8, base, fb->pitches[0],
bc8d7dff
DL
8410 plane_config->size);
8411
2d14030b 8412 plane_config->fb = intel_fb;
bc8d7dff
DL
8413 return;
8414
8415error:
d1a3a036 8416 kfree(intel_fb);
bc8d7dff
DL
8417}
8418
2fa2fe9a 8419static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 8420 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
8421{
8422 struct drm_device *dev = crtc->base.dev;
fac5e23e 8423 struct drm_i915_private *dev_priv = to_i915(dev);
2fa2fe9a
DV
8424 uint32_t tmp;
8425
8426 tmp = I915_READ(PF_CTL(crtc->pipe));
8427
8428 if (tmp & PF_ENABLE) {
fd4daa9c 8429 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
DV
8430 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
8431 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
DV
8432
8433 /* We currently do not free assignements of panel fitters on
8434 * ivb/hsw (since we don't use the higher upscaling modes which
8435 * differentiates them) so just WARN about this case for now. */
5db94019 8436 if (IS_GEN7(dev_priv)) {
cb8b2a30
DV
8437 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
8438 PF_PIPE_SEL_IVB(crtc->pipe));
8439 }
2fa2fe9a 8440 }
79e53945
JB
8441}
8442
5724dbd1
DL
8443static void
8444ironlake_get_initial_plane_config(struct intel_crtc *crtc,
8445 struct intel_initial_plane_config *plane_config)
4c6baa59
JB
8446{
8447 struct drm_device *dev = crtc->base.dev;
fac5e23e 8448 struct drm_i915_private *dev_priv = to_i915(dev);
4c6baa59 8449 u32 val, base, offset;
aeee5a49 8450 int pipe = crtc->pipe;
4c6baa59 8451 int fourcc, pixel_format;
6761dd31 8452 unsigned int aligned_height;
b113d5ee 8453 struct drm_framebuffer *fb;
1b842c89 8454 struct intel_framebuffer *intel_fb;
4c6baa59 8455
42a7b088
DL
8456 val = I915_READ(DSPCNTR(pipe));
8457 if (!(val & DISPLAY_PLANE_ENABLE))
8458 return;
8459
d9806c9f 8460 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 8461 if (!intel_fb) {
4c6baa59
JB
8462 DRM_DEBUG_KMS("failed to alloc fb\n");
8463 return;
8464 }
8465
1b842c89
DL
8466 fb = &intel_fb->base;
8467
d2e9f5fc
VS
8468 fb->dev = dev;
8469
6315b5d3 8470 if (INTEL_GEN(dev_priv) >= 4) {
18c5247e 8471 if (val & DISPPLANE_TILED) {
49af449b 8472 plane_config->tiling = I915_TILING_X;
bae781b2 8473 fb->modifier = I915_FORMAT_MOD_X_TILED;
18c5247e
DV
8474 }
8475 }
4c6baa59
JB
8476
8477 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 8478 fourcc = i9xx_format_to_fourcc(pixel_format);
2f3f4763 8479 fb->format = drm_format_info(fourcc);
4c6baa59 8480
aeee5a49 8481 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
8652744b 8482 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
aeee5a49 8483 offset = I915_READ(DSPOFFSET(pipe));
4c6baa59 8484 } else {
49af449b 8485 if (plane_config->tiling)
aeee5a49 8486 offset = I915_READ(DSPTILEOFF(pipe));
4c6baa59 8487 else
aeee5a49 8488 offset = I915_READ(DSPLINOFF(pipe));
4c6baa59
JB
8489 }
8490 plane_config->base = base;
8491
8492 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
8493 fb->width = ((val >> 16) & 0xfff) + 1;
8494 fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
8495
8496 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 8497 fb->pitches[0] = val & 0xffffffc0;
4c6baa59 8498
24dbf51a
CW
8499 aligned_height = intel_fb_align_height(dev_priv,
8500 fb->height,
438b74a5 8501 fb->format->format,
bae781b2 8502 fb->modifier);
4c6baa59 8503
f37b5c2b 8504 plane_config->size = fb->pitches[0] * aligned_height;
4c6baa59 8505
2844a921
DL
8506 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8507 pipe_name(pipe), fb->width, fb->height,
272725c7 8508 fb->format->cpp[0] * 8, base, fb->pitches[0],
2844a921 8509 plane_config->size);
b113d5ee 8510
2d14030b 8511 plane_config->fb = intel_fb;
4c6baa59
JB
8512}
8513
0e8ffe1b 8514static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5cec258b 8515 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
8516{
8517 struct drm_device *dev = crtc->base.dev;
fac5e23e 8518 struct drm_i915_private *dev_priv = to_i915(dev);
1729050e 8519 enum intel_display_power_domain power_domain;
0e8ffe1b 8520 uint32_t tmp;
1729050e 8521 bool ret;
0e8ffe1b 8522
1729050e
ID
8523 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8524 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
930e8c9e
PZ
8525 return false;
8526
e143a21c 8527 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 8528 pipe_config->shared_dpll = NULL;
eccb140b 8529
1729050e 8530 ret = false;
0e8ffe1b
DV
8531 tmp = I915_READ(PIPECONF(crtc->pipe));
8532 if (!(tmp & PIPECONF_ENABLE))
1729050e 8533 goto out;
0e8ffe1b 8534
42571aef
VS
8535 switch (tmp & PIPECONF_BPC_MASK) {
8536 case PIPECONF_6BPC:
8537 pipe_config->pipe_bpp = 18;
8538 break;
8539 case PIPECONF_8BPC:
8540 pipe_config->pipe_bpp = 24;
8541 break;
8542 case PIPECONF_10BPC:
8543 pipe_config->pipe_bpp = 30;
8544 break;
8545 case PIPECONF_12BPC:
8546 pipe_config->pipe_bpp = 36;
8547 break;
8548 default:
8549 break;
8550 }
8551
b5a9fa09
DV
8552 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
8553 pipe_config->limited_color_range = true;
8554
ab9412ba 8555 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0 8556 struct intel_shared_dpll *pll;
8106ddbd 8557 enum intel_dpll_id pll_id;
66e985c0 8558
88adfff1
DV
8559 pipe_config->has_pch_encoder = true;
8560
627eb5a3
DV
8561 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
8562 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
8563 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
8564
8565 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 8566
2d1fe073 8567 if (HAS_PCH_IBX(dev_priv)) {
d9a7bc67
ID
8568 /*
8569 * The pipe->pch transcoder and pch transcoder->pll
8570 * mapping is fixed.
8571 */
8106ddbd 8572 pll_id = (enum intel_dpll_id) crtc->pipe;
c0d43d62
DV
8573 } else {
8574 tmp = I915_READ(PCH_DPLL_SEL);
8575 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
8106ddbd 8576 pll_id = DPLL_ID_PCH_PLL_B;
c0d43d62 8577 else
8106ddbd 8578 pll_id= DPLL_ID_PCH_PLL_A;
c0d43d62 8579 }
66e985c0 8580
8106ddbd
ACO
8581 pipe_config->shared_dpll =
8582 intel_get_shared_dpll_by_id(dev_priv, pll_id);
8583 pll = pipe_config->shared_dpll;
66e985c0 8584
2edd6443
ACO
8585 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
8586 &pipe_config->dpll_hw_state));
c93f54cf
DV
8587
8588 tmp = pipe_config->dpll_hw_state.dpll;
8589 pipe_config->pixel_multiplier =
8590 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
8591 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
8592
8593 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
DV
8594 } else {
8595 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
8596 }
8597
1bd1bd80 8598 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 8599 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 8600
2fa2fe9a
DV
8601 ironlake_get_pfit_config(crtc, pipe_config);
8602
1729050e
ID
8603 ret = true;
8604
8605out:
8606 intel_display_power_put(dev_priv, power_domain);
8607
8608 return ret;
0e8ffe1b
DV
8609}
8610
be256dc7
PZ
8611static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
8612{
91c8a326 8613 struct drm_device *dev = &dev_priv->drm;
be256dc7 8614 struct intel_crtc *crtc;
be256dc7 8615
d3fcc808 8616 for_each_intel_crtc(dev, crtc)
e2c719b7 8617 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
8618 pipe_name(crtc->pipe));
8619
e2c719b7
RC
8620 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
8621 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
01403de3
VS
8622 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
8623 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
44cb734c 8624 I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
e2c719b7 8625 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
be256dc7 8626 "CPU PWM1 enabled\n");
772c2a51 8627 if (IS_HASWELL(dev_priv))
e2c719b7 8628 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
c5107b87 8629 "CPU PWM2 enabled\n");
e2c719b7 8630 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
be256dc7 8631 "PCH PWM1 enabled\n");
e2c719b7 8632 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
be256dc7 8633 "Utility pin enabled\n");
e2c719b7 8634 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
be256dc7 8635
9926ada1
PZ
8636 /*
8637 * In theory we can still leave IRQs enabled, as long as only the HPD
8638 * interrupts remain enabled. We used to check for that, but since it's
8639 * gen-specific and since we only disable LCPLL after we fully disable
8640 * the interrupts, the check below should be enough.
8641 */
e2c719b7 8642 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
be256dc7
PZ
8643}
8644
9ccd5aeb
PZ
8645static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
8646{
772c2a51 8647 if (IS_HASWELL(dev_priv))
9ccd5aeb
PZ
8648 return I915_READ(D_COMP_HSW);
8649 else
8650 return I915_READ(D_COMP_BDW);
8651}
8652
3c4c9b81
PZ
8653static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
8654{
772c2a51 8655 if (IS_HASWELL(dev_priv)) {
3c4c9b81
PZ
8656 mutex_lock(&dev_priv->rps.hw_lock);
8657 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
8658 val))
79cf219a 8659 DRM_DEBUG_KMS("Failed to write to D_COMP\n");
3c4c9b81
PZ
8660 mutex_unlock(&dev_priv->rps.hw_lock);
8661 } else {
9ccd5aeb
PZ
8662 I915_WRITE(D_COMP_BDW, val);
8663 POSTING_READ(D_COMP_BDW);
3c4c9b81 8664 }
be256dc7
PZ
8665}
8666
8667/*
8668 * This function implements pieces of two sequences from BSpec:
8669 * - Sequence for display software to disable LCPLL
8670 * - Sequence for display software to allow package C8+
8671 * The steps implemented here are just the steps that actually touch the LCPLL
8672 * register. Callers should take care of disabling all the display engine
8673 * functions, doing the mode unset, fixing interrupts, etc.
8674 */
6ff58d53
PZ
8675static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
8676 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
8677{
8678 uint32_t val;
8679
8680 assert_can_disable_lcpll(dev_priv);
8681
8682 val = I915_READ(LCPLL_CTL);
8683
8684 if (switch_to_fclk) {
8685 val |= LCPLL_CD_SOURCE_FCLK;
8686 I915_WRITE(LCPLL_CTL, val);
8687
f53dd63f
ID
8688 if (wait_for_us(I915_READ(LCPLL_CTL) &
8689 LCPLL_CD_SOURCE_FCLK_DONE, 1))
be256dc7
PZ
8690 DRM_ERROR("Switching to FCLK failed\n");
8691
8692 val = I915_READ(LCPLL_CTL);
8693 }
8694
8695 val |= LCPLL_PLL_DISABLE;
8696 I915_WRITE(LCPLL_CTL, val);
8697 POSTING_READ(LCPLL_CTL);
8698
24d8441d 8699 if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
be256dc7
PZ
8700 DRM_ERROR("LCPLL still locked\n");
8701
9ccd5aeb 8702 val = hsw_read_dcomp(dev_priv);
be256dc7 8703 val |= D_COMP_COMP_DISABLE;
3c4c9b81 8704 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
8705 ndelay(100);
8706
9ccd5aeb
PZ
8707 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
8708 1))
be256dc7
PZ
8709 DRM_ERROR("D_COMP RCOMP still in progress\n");
8710
8711 if (allow_power_down) {
8712 val = I915_READ(LCPLL_CTL);
8713 val |= LCPLL_POWER_DOWN_ALLOW;
8714 I915_WRITE(LCPLL_CTL, val);
8715 POSTING_READ(LCPLL_CTL);
8716 }
8717}
8718
8719/*
8720 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
8721 * source.
8722 */
6ff58d53 8723static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
8724{
8725 uint32_t val;
8726
8727 val = I915_READ(LCPLL_CTL);
8728
8729 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
8730 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
8731 return;
8732
a8a8bd54
PZ
8733 /*
8734 * Make sure we're not on PC8 state before disabling PC8, otherwise
8735 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
a8a8bd54 8736 */
59bad947 8737 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
215733fa 8738
be256dc7
PZ
8739 if (val & LCPLL_POWER_DOWN_ALLOW) {
8740 val &= ~LCPLL_POWER_DOWN_ALLOW;
8741 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 8742 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
8743 }
8744
9ccd5aeb 8745 val = hsw_read_dcomp(dev_priv);
be256dc7
PZ
8746 val |= D_COMP_COMP_FORCE;
8747 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 8748 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
8749
8750 val = I915_READ(LCPLL_CTL);
8751 val &= ~LCPLL_PLL_DISABLE;
8752 I915_WRITE(LCPLL_CTL, val);
8753
93220c08
CW
8754 if (intel_wait_for_register(dev_priv,
8755 LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
8756 5))
be256dc7
PZ
8757 DRM_ERROR("LCPLL not locked yet\n");
8758
8759 if (val & LCPLL_CD_SOURCE_FCLK) {
8760 val = I915_READ(LCPLL_CTL);
8761 val &= ~LCPLL_CD_SOURCE_FCLK;
8762 I915_WRITE(LCPLL_CTL, val);
8763
f53dd63f
ID
8764 if (wait_for_us((I915_READ(LCPLL_CTL) &
8765 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
be256dc7
PZ
8766 DRM_ERROR("Switching back to LCPLL failed\n");
8767 }
215733fa 8768
59bad947 8769 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4c75b940 8770 intel_update_cdclk(dev_priv);
be256dc7
PZ
8771}
8772
765dab67
PZ
8773/*
8774 * Package states C8 and deeper are really deep PC states that can only be
8775 * reached when all the devices on the system allow it, so even if the graphics
8776 * device allows PC8+, it doesn't mean the system will actually get to these
8777 * states. Our driver only allows PC8+ when going into runtime PM.
8778 *
8779 * The requirements for PC8+ are that all the outputs are disabled, the power
8780 * well is disabled and most interrupts are disabled, and these are also
8781 * requirements for runtime PM. When these conditions are met, we manually do
8782 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
8783 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
8784 * hang the machine.
8785 *
8786 * When we really reach PC8 or deeper states (not just when we allow it) we lose
8787 * the state of some registers, so when we come back from PC8+ we need to
8788 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
8789 * need to take care of the registers kept by RC6. Notice that this happens even
8790 * if we don't put the device in PCI D3 state (which is what currently happens
8791 * because of the runtime PM support).
8792 *
8793 * For more, read "Display Sequences for Package C8" on the hardware
8794 * documentation.
8795 */
a14cb6fc 8796void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 8797{
c67a470b
PZ
8798 uint32_t val;
8799
c67a470b
PZ
8800 DRM_DEBUG_KMS("Enabling package C8+\n");
8801
4f8036a2 8802 if (HAS_PCH_LPT_LP(dev_priv)) {
c67a470b
PZ
8803 val = I915_READ(SOUTH_DSPCLK_GATE_D);
8804 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
8805 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
8806 }
8807
c39055b0 8808 lpt_disable_clkout_dp(dev_priv);
c67a470b
PZ
8809 hsw_disable_lcpll(dev_priv, true, true);
8810}
8811
a14cb6fc 8812void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b 8813{
c67a470b
PZ
8814 uint32_t val;
8815
c67a470b
PZ
8816 DRM_DEBUG_KMS("Disabling package C8+\n");
8817
8818 hsw_restore_lcpll(dev_priv);
c39055b0 8819 lpt_init_pch_refclk(dev_priv);
c67a470b 8820
4f8036a2 8821 if (HAS_PCH_LPT_LP(dev_priv)) {
c67a470b
PZ
8822 val = I915_READ(SOUTH_DSPCLK_GATE_D);
8823 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
8824 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
8825 }
c67a470b
PZ
8826}
8827
190f68c5
ACO
8828static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
8829 struct intel_crtc_state *crtc_state)
09b4ddf9 8830{
d7edc4e5 8831 if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
af3997b5
MK
8832 if (!intel_ddi_pll_select(crtc, crtc_state))
8833 return -EINVAL;
8834 }
716c2e55 8835
c7653199 8836 crtc->lowfreq_avail = false;
644cef34 8837
c8f7a0db 8838 return 0;
79e53945
JB
8839}
8840
3760b59c
S
8841static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
8842 enum port port,
8843 struct intel_crtc_state *pipe_config)
8844{
8106ddbd
ACO
8845 enum intel_dpll_id id;
8846
3760b59c
S
8847 switch (port) {
8848 case PORT_A:
08250c4b 8849 id = DPLL_ID_SKL_DPLL0;
3760b59c
S
8850 break;
8851 case PORT_B:
08250c4b 8852 id = DPLL_ID_SKL_DPLL1;
3760b59c
S
8853 break;
8854 case PORT_C:
08250c4b 8855 id = DPLL_ID_SKL_DPLL2;
3760b59c
S
8856 break;
8857 default:
8858 DRM_ERROR("Incorrect port type\n");
8106ddbd 8859 return;
3760b59c 8860 }
8106ddbd
ACO
8861
8862 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
3760b59c
S
8863}
8864
96b7dfb7
S
8865static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
8866 enum port port,
5cec258b 8867 struct intel_crtc_state *pipe_config)
96b7dfb7 8868{
8106ddbd 8869 enum intel_dpll_id id;
a3c988ea 8870 u32 temp;
96b7dfb7
S
8871
8872 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
c856052a 8873 id = temp >> (port * 3 + 1);
96b7dfb7 8874
c856052a 8875 if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL3))
8106ddbd 8876 return;
8106ddbd
ACO
8877
8878 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
96b7dfb7
S
8879}
8880
7d2c8175
DL
8881static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
8882 enum port port,
5cec258b 8883 struct intel_crtc_state *pipe_config)
7d2c8175 8884{
8106ddbd 8885 enum intel_dpll_id id;
c856052a 8886 uint32_t ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
8106ddbd 8887
c856052a 8888 switch (ddi_pll_sel) {
7d2c8175 8889 case PORT_CLK_SEL_WRPLL1:
8106ddbd 8890 id = DPLL_ID_WRPLL1;
7d2c8175
DL
8891 break;
8892 case PORT_CLK_SEL_WRPLL2:
8106ddbd 8893 id = DPLL_ID_WRPLL2;
7d2c8175 8894 break;
00490c22 8895 case PORT_CLK_SEL_SPLL:
8106ddbd 8896 id = DPLL_ID_SPLL;
79bd23da 8897 break;
9d16da65
ACO
8898 case PORT_CLK_SEL_LCPLL_810:
8899 id = DPLL_ID_LCPLL_810;
8900 break;
8901 case PORT_CLK_SEL_LCPLL_1350:
8902 id = DPLL_ID_LCPLL_1350;
8903 break;
8904 case PORT_CLK_SEL_LCPLL_2700:
8905 id = DPLL_ID_LCPLL_2700;
8906 break;
8106ddbd 8907 default:
c856052a 8908 MISSING_CASE(ddi_pll_sel);
8106ddbd
ACO
8909 /* fall through */
8910 case PORT_CLK_SEL_NONE:
8106ddbd 8911 return;
7d2c8175 8912 }
8106ddbd
ACO
8913
8914 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
7d2c8175
DL
8915}
8916
cf30429e
JN
8917static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
8918 struct intel_crtc_state *pipe_config,
d8fc70b7 8919 u64 *power_domain_mask)
cf30429e
JN
8920{
8921 struct drm_device *dev = crtc->base.dev;
fac5e23e 8922 struct drm_i915_private *dev_priv = to_i915(dev);
cf30429e
JN
8923 enum intel_display_power_domain power_domain;
8924 u32 tmp;
8925
d9a7bc67
ID
8926 /*
8927 * The pipe->transcoder mapping is fixed with the exception of the eDP
8928 * transcoder handled below.
8929 */
cf30429e
JN
8930 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8931
8932 /*
8933 * XXX: Do intel_display_power_get_if_enabled before reading this (for
8934 * consistency and less surprising code; it's in always on power).
8935 */
8936 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
8937 if (tmp & TRANS_DDI_FUNC_ENABLE) {
8938 enum pipe trans_edp_pipe;
8939 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
8940 default:
8941 WARN(1, "unknown pipe linked to edp transcoder\n");
8942 case TRANS_DDI_EDP_INPUT_A_ONOFF:
8943 case TRANS_DDI_EDP_INPUT_A_ON:
8944 trans_edp_pipe = PIPE_A;
8945 break;
8946 case TRANS_DDI_EDP_INPUT_B_ONOFF:
8947 trans_edp_pipe = PIPE_B;
8948 break;
8949 case TRANS_DDI_EDP_INPUT_C_ONOFF:
8950 trans_edp_pipe = PIPE_C;
8951 break;
8952 }
8953
8954 if (trans_edp_pipe == crtc->pipe)
8955 pipe_config->cpu_transcoder = TRANSCODER_EDP;
8956 }
8957
8958 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
8959 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
8960 return false;
d8fc70b7 8961 *power_domain_mask |= BIT_ULL(power_domain);
cf30429e
JN
8962
8963 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
8964
8965 return tmp & PIPECONF_ENABLE;
8966}
8967
4d1de975
JN
8968static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
8969 struct intel_crtc_state *pipe_config,
d8fc70b7 8970 u64 *power_domain_mask)
4d1de975
JN
8971{
8972 struct drm_device *dev = crtc->base.dev;
fac5e23e 8973 struct drm_i915_private *dev_priv = to_i915(dev);
4d1de975
JN
8974 enum intel_display_power_domain power_domain;
8975 enum port port;
8976 enum transcoder cpu_transcoder;
8977 u32 tmp;
8978
4d1de975
JN
8979 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
8980 if (port == PORT_A)
8981 cpu_transcoder = TRANSCODER_DSI_A;
8982 else
8983 cpu_transcoder = TRANSCODER_DSI_C;
8984
8985 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
8986 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
8987 continue;
d8fc70b7 8988 *power_domain_mask |= BIT_ULL(power_domain);
4d1de975 8989
db18b6a6
ID
8990 /*
8991 * The PLL needs to be enabled with a valid divider
8992 * configuration, otherwise accessing DSI registers will hang
8993 * the machine. See BSpec North Display Engine
8994 * registers/MIPI[BXT]. We can break out here early, since we
8995 * need the same DSI PLL to be enabled for both DSI ports.
8996 */
8997 if (!intel_dsi_pll_is_enabled(dev_priv))
8998 break;
8999
4d1de975
JN
9000 /* XXX: this works for video mode only */
9001 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
9002 if (!(tmp & DPI_ENABLE))
9003 continue;
9004
9005 tmp = I915_READ(MIPI_CTRL(port));
9006 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
9007 continue;
9008
9009 pipe_config->cpu_transcoder = cpu_transcoder;
4d1de975
JN
9010 break;
9011 }
9012
d7edc4e5 9013 return transcoder_is_dsi(pipe_config->cpu_transcoder);
4d1de975
JN
9014}
9015
26804afd 9016static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
5cec258b 9017 struct intel_crtc_state *pipe_config)
26804afd 9018{
6315b5d3 9019 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
d452c5b6 9020 struct intel_shared_dpll *pll;
26804afd
DV
9021 enum port port;
9022 uint32_t tmp;
9023
9024 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9025
9026 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9027
b976dc53 9028 if (IS_GEN9_BC(dev_priv))
96b7dfb7 9029 skylake_get_ddi_pll(dev_priv, port, pipe_config);
cc3f90f0 9030 else if (IS_GEN9_LP(dev_priv))
3760b59c 9031 bxt_get_ddi_pll(dev_priv, port, pipe_config);
96b7dfb7
S
9032 else
9033 haswell_get_ddi_pll(dev_priv, port, pipe_config);
9cd86933 9034
8106ddbd
ACO
9035 pll = pipe_config->shared_dpll;
9036 if (pll) {
2edd6443
ACO
9037 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9038 &pipe_config->dpll_hw_state));
d452c5b6
DV
9039 }
9040
26804afd
DV
9041 /*
9042 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9043 * DDI E. So just check whether this pipe is wired to DDI E and whether
9044 * the PCH transcoder is on.
9045 */
6315b5d3 9046 if (INTEL_GEN(dev_priv) < 9 &&
ca370455 9047 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
26804afd
DV
9048 pipe_config->has_pch_encoder = true;
9049
9050 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9051 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9052 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9053
9054 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9055 }
9056}
9057
0e8ffe1b 9058static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9059 struct intel_crtc_state *pipe_config)
0e8ffe1b 9060{
6315b5d3 9061 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1729050e 9062 enum intel_display_power_domain power_domain;
d8fc70b7 9063 u64 power_domain_mask;
cf30429e 9064 bool active;
0e8ffe1b 9065
1729050e
ID
9066 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9067 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0 9068 return false;
d8fc70b7 9069 power_domain_mask = BIT_ULL(power_domain);
1729050e 9070
8106ddbd 9071 pipe_config->shared_dpll = NULL;
c0d43d62 9072
cf30429e 9073 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
eccb140b 9074
cc3f90f0 9075 if (IS_GEN9_LP(dev_priv) &&
d7edc4e5
VS
9076 bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
9077 WARN_ON(active);
9078 active = true;
4d1de975
JN
9079 }
9080
cf30429e 9081 if (!active)
1729050e 9082 goto out;
0e8ffe1b 9083
d7edc4e5 9084 if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
4d1de975
JN
9085 haswell_get_ddi_port_state(crtc, pipe_config);
9086 intel_get_pipe_timings(crtc, pipe_config);
9087 }
627eb5a3 9088
bc58be60 9089 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 9090
05dc698c
LL
9091 pipe_config->gamma_mode =
9092 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
9093
6315b5d3 9094 if (INTEL_GEN(dev_priv) >= 9) {
1c74eeaf 9095 intel_crtc_init_scalers(crtc, pipe_config);
a1b2278e 9096
af99ceda
CK
9097 pipe_config->scaler_state.scaler_id = -1;
9098 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9099 }
9100
1729050e
ID
9101 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
9102 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
d8fc70b7 9103 power_domain_mask |= BIT_ULL(power_domain);
6315b5d3 9104 if (INTEL_GEN(dev_priv) >= 9)
bd2e244f 9105 skylake_get_pfit_config(crtc, pipe_config);
ff6d9f55 9106 else
1c132b44 9107 ironlake_get_pfit_config(crtc, pipe_config);
bd2e244f 9108 }
88adfff1 9109
772c2a51 9110 if (IS_HASWELL(dev_priv))
e59150dc
JB
9111 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9112 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 9113
4d1de975
JN
9114 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
9115 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
ebb69c95
CT
9116 pipe_config->pixel_multiplier =
9117 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9118 } else {
9119 pipe_config->pixel_multiplier = 1;
9120 }
6c49f241 9121
1729050e
ID
9122out:
9123 for_each_power_domain(power_domain, power_domain_mask)
9124 intel_display_power_put(dev_priv, power_domain);
9125
cf30429e 9126 return active;
0e8ffe1b
DV
9127}
9128
55a08b3f
ML
9129static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
9130 const struct intel_plane_state *plane_state)
560b85bb
CW
9131{
9132 struct drm_device *dev = crtc->dev;
fac5e23e 9133 struct drm_i915_private *dev_priv = to_i915(dev);
560b85bb 9134 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
dc41c154 9135 uint32_t cntl = 0, size = 0;
560b85bb 9136
936e71e3 9137 if (plane_state && plane_state->base.visible) {
55a08b3f
ML
9138 unsigned int width = plane_state->base.crtc_w;
9139 unsigned int height = plane_state->base.crtc_h;
dc41c154
VS
9140 unsigned int stride = roundup_pow_of_two(width) * 4;
9141
9142 switch (stride) {
9143 default:
9144 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
9145 width, stride);
9146 stride = 256;
9147 /* fallthrough */
9148 case 256:
9149 case 512:
9150 case 1024:
9151 case 2048:
9152 break;
4b0e333e
CW
9153 }
9154
dc41c154
VS
9155 cntl |= CURSOR_ENABLE |
9156 CURSOR_GAMMA_ENABLE |
9157 CURSOR_FORMAT_ARGB |
9158 CURSOR_STRIDE(stride);
9159
9160 size = (height << 12) | width;
4b0e333e 9161 }
560b85bb 9162
dc41c154
VS
9163 if (intel_crtc->cursor_cntl != 0 &&
9164 (intel_crtc->cursor_base != base ||
9165 intel_crtc->cursor_size != size ||
9166 intel_crtc->cursor_cntl != cntl)) {
9167 /* On these chipsets we can only modify the base/size/stride
9168 * whilst the cursor is disabled.
9169 */
0b87c24e
VS
9170 I915_WRITE(CURCNTR(PIPE_A), 0);
9171 POSTING_READ(CURCNTR(PIPE_A));
dc41c154 9172 intel_crtc->cursor_cntl = 0;
4b0e333e 9173 }
560b85bb 9174
99d1f387 9175 if (intel_crtc->cursor_base != base) {
0b87c24e 9176 I915_WRITE(CURBASE(PIPE_A), base);
99d1f387
VS
9177 intel_crtc->cursor_base = base;
9178 }
4726e0b0 9179
dc41c154
VS
9180 if (intel_crtc->cursor_size != size) {
9181 I915_WRITE(CURSIZE, size);
9182 intel_crtc->cursor_size = size;
4b0e333e 9183 }
560b85bb 9184
4b0e333e 9185 if (intel_crtc->cursor_cntl != cntl) {
0b87c24e
VS
9186 I915_WRITE(CURCNTR(PIPE_A), cntl);
9187 POSTING_READ(CURCNTR(PIPE_A));
4b0e333e 9188 intel_crtc->cursor_cntl = cntl;
560b85bb 9189 }
560b85bb
CW
9190}
9191
55a08b3f
ML
9192static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
9193 const struct intel_plane_state *plane_state)
65a21cd6
JB
9194{
9195 struct drm_device *dev = crtc->dev;
fac5e23e 9196 struct drm_i915_private *dev_priv = to_i915(dev);
65a21cd6
JB
9197 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9198 int pipe = intel_crtc->pipe;
663f3122 9199 uint32_t cntl = 0;
4b0e333e 9200
936e71e3 9201 if (plane_state && plane_state->base.visible) {
4b0e333e 9202 cntl = MCURSOR_GAMMA_ENABLE;
55a08b3f 9203 switch (plane_state->base.crtc_w) {
4726e0b0
SK
9204 case 64:
9205 cntl |= CURSOR_MODE_64_ARGB_AX;
9206 break;
9207 case 128:
9208 cntl |= CURSOR_MODE_128_ARGB_AX;
9209 break;
9210 case 256:
9211 cntl |= CURSOR_MODE_256_ARGB_AX;
9212 break;
9213 default:
55a08b3f 9214 MISSING_CASE(plane_state->base.crtc_w);
4726e0b0 9215 return;
65a21cd6 9216 }
4b0e333e 9217 cntl |= pipe << 28; /* Connect to correct pipe */
47bf17a7 9218
4f8036a2 9219 if (HAS_DDI(dev_priv))
47bf17a7 9220 cntl |= CURSOR_PIPE_CSC_ENABLE;
65a21cd6 9221
f22aa143 9222 if (plane_state->base.rotation & DRM_ROTATE_180)
55a08b3f
ML
9223 cntl |= CURSOR_ROTATE_180;
9224 }
4398ad45 9225
4b0e333e
CW
9226 if (intel_crtc->cursor_cntl != cntl) {
9227 I915_WRITE(CURCNTR(pipe), cntl);
9228 POSTING_READ(CURCNTR(pipe));
9229 intel_crtc->cursor_cntl = cntl;
65a21cd6 9230 }
4b0e333e 9231
65a21cd6 9232 /* and commit changes on next vblank */
5efb3e28
VS
9233 I915_WRITE(CURBASE(pipe), base);
9234 POSTING_READ(CURBASE(pipe));
99d1f387
VS
9235
9236 intel_crtc->cursor_base = base;
65a21cd6
JB
9237}
9238
cda4b7d3 9239/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6b383a7f 9240static void intel_crtc_update_cursor(struct drm_crtc *crtc,
55a08b3f 9241 const struct intel_plane_state *plane_state)
cda4b7d3
CW
9242{
9243 struct drm_device *dev = crtc->dev;
fac5e23e 9244 struct drm_i915_private *dev_priv = to_i915(dev);
cda4b7d3
CW
9245 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9246 int pipe = intel_crtc->pipe;
55a08b3f
ML
9247 u32 base = intel_crtc->cursor_addr;
9248 u32 pos = 0;
cda4b7d3 9249
55a08b3f
ML
9250 if (plane_state) {
9251 int x = plane_state->base.crtc_x;
9252 int y = plane_state->base.crtc_y;
cda4b7d3 9253
55a08b3f
ML
9254 if (x < 0) {
9255 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
9256 x = -x;
9257 }
9258 pos |= x << CURSOR_X_SHIFT;
cda4b7d3 9259
55a08b3f
ML
9260 if (y < 0) {
9261 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
9262 y = -y;
9263 }
9264 pos |= y << CURSOR_Y_SHIFT;
9265
9266 /* ILK+ do this automagically */
49cff963 9267 if (HAS_GMCH_DISPLAY(dev_priv) &&
f22aa143 9268 plane_state->base.rotation & DRM_ROTATE_180) {
55a08b3f
ML
9269 base += (plane_state->base.crtc_h *
9270 plane_state->base.crtc_w - 1) * 4;
9271 }
cda4b7d3 9272 }
cda4b7d3 9273
5efb3e28
VS
9274 I915_WRITE(CURPOS(pipe), pos);
9275
2a307c2e 9276 if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
55a08b3f 9277 i845_update_cursor(crtc, base, plane_state);
5efb3e28 9278 else
55a08b3f 9279 i9xx_update_cursor(crtc, base, plane_state);
cda4b7d3
CW
9280}
9281
50a0bc90 9282static bool cursor_size_ok(struct drm_i915_private *dev_priv,
dc41c154
VS
9283 uint32_t width, uint32_t height)
9284{
9285 if (width == 0 || height == 0)
9286 return false;
9287
9288 /*
9289 * 845g/865g are special in that they are only limited by
9290 * the width of their cursors, the height is arbitrary up to
9291 * the precision of the register. Everything else requires
9292 * square cursors, limited to a few power-of-two sizes.
9293 */
2a307c2e 9294 if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
dc41c154
VS
9295 if ((width & 63) != 0)
9296 return false;
9297
2a307c2e 9298 if (width > (IS_I845G(dev_priv) ? 64 : 512))
dc41c154
VS
9299 return false;
9300
9301 if (height > 1023)
9302 return false;
9303 } else {
9304 switch (width | height) {
9305 case 256:
9306 case 128:
50a0bc90 9307 if (IS_GEN2(dev_priv))
dc41c154
VS
9308 return false;
9309 case 64:
9310 break;
9311 default:
9312 return false;
9313 }
9314 }
9315
9316 return true;
9317}
9318
79e53945
JB
9319/* VESA 640x480x72Hz mode to set on the pipe */
9320static struct drm_display_mode load_detect_mode = {
9321 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
9322 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
9323};
9324
a8bb6818 9325struct drm_framebuffer *
24dbf51a
CW
9326intel_framebuffer_create(struct drm_i915_gem_object *obj,
9327 struct drm_mode_fb_cmd2 *mode_cmd)
d2dff872
CW
9328{
9329 struct intel_framebuffer *intel_fb;
9330 int ret;
9331
9332 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
dcb1394e 9333 if (!intel_fb)
d2dff872 9334 return ERR_PTR(-ENOMEM);
d2dff872 9335
24dbf51a 9336 ret = intel_framebuffer_init(intel_fb, obj, mode_cmd);
dd4916c5
DV
9337 if (ret)
9338 goto err;
d2dff872
CW
9339
9340 return &intel_fb->base;
dcb1394e 9341
dd4916c5 9342err:
dd4916c5 9343 kfree(intel_fb);
dd4916c5 9344 return ERR_PTR(ret);
d2dff872
CW
9345}
9346
9347static u32
9348intel_framebuffer_pitch_for_width(int width, int bpp)
9349{
9350 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
9351 return ALIGN(pitch, 64);
9352}
9353
9354static u32
9355intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
9356{
9357 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
1267a26b 9358 return PAGE_ALIGN(pitch * mode->vdisplay);
d2dff872
CW
9359}
9360
9361static struct drm_framebuffer *
9362intel_framebuffer_create_for_mode(struct drm_device *dev,
9363 struct drm_display_mode *mode,
9364 int depth, int bpp)
9365{
dcb1394e 9366 struct drm_framebuffer *fb;
d2dff872 9367 struct drm_i915_gem_object *obj;
0fed39bd 9368 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872 9369
12d79d78 9370 obj = i915_gem_object_create(to_i915(dev),
d2dff872 9371 intel_framebuffer_size_for_mode(mode, bpp));
fe3db79b
CW
9372 if (IS_ERR(obj))
9373 return ERR_CAST(obj);
d2dff872
CW
9374
9375 mode_cmd.width = mode->hdisplay;
9376 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
9377 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
9378 bpp);
5ca0c34a 9379 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872 9380
24dbf51a 9381 fb = intel_framebuffer_create(obj, &mode_cmd);
dcb1394e 9382 if (IS_ERR(fb))
f0cd5182 9383 i915_gem_object_put(obj);
dcb1394e
LW
9384
9385 return fb;
d2dff872
CW
9386}
9387
9388static struct drm_framebuffer *
9389mode_fits_in_fbdev(struct drm_device *dev,
9390 struct drm_display_mode *mode)
9391{
0695726e 9392#ifdef CONFIG_DRM_FBDEV_EMULATION
fac5e23e 9393 struct drm_i915_private *dev_priv = to_i915(dev);
d2dff872
CW
9394 struct drm_i915_gem_object *obj;
9395 struct drm_framebuffer *fb;
9396
4c0e5528 9397 if (!dev_priv->fbdev)
d2dff872
CW
9398 return NULL;
9399
4c0e5528 9400 if (!dev_priv->fbdev->fb)
d2dff872
CW
9401 return NULL;
9402
4c0e5528
DV
9403 obj = dev_priv->fbdev->fb->obj;
9404 BUG_ON(!obj);
9405
8bcd4553 9406 fb = &dev_priv->fbdev->fb->base;
01f2c773 9407 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
272725c7 9408 fb->format->cpp[0] * 8))
d2dff872
CW
9409 return NULL;
9410
01f2c773 9411 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
9412 return NULL;
9413
edde3617 9414 drm_framebuffer_reference(fb);
d2dff872 9415 return fb;
4520f53a
DV
9416#else
9417 return NULL;
9418#endif
d2dff872
CW
9419}
9420
d3a40d1b
ACO
9421static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
9422 struct drm_crtc *crtc,
9423 struct drm_display_mode *mode,
9424 struct drm_framebuffer *fb,
9425 int x, int y)
9426{
9427 struct drm_plane_state *plane_state;
9428 int hdisplay, vdisplay;
9429 int ret;
9430
9431 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
9432 if (IS_ERR(plane_state))
9433 return PTR_ERR(plane_state);
9434
9435 if (mode)
196cd5d3 9436 drm_mode_get_hv_timing(mode, &hdisplay, &vdisplay);
d3a40d1b
ACO
9437 else
9438 hdisplay = vdisplay = 0;
9439
9440 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
9441 if (ret)
9442 return ret;
9443 drm_atomic_set_fb_for_plane(plane_state, fb);
9444 plane_state->crtc_x = 0;
9445 plane_state->crtc_y = 0;
9446 plane_state->crtc_w = hdisplay;
9447 plane_state->crtc_h = vdisplay;
9448 plane_state->src_x = x << 16;
9449 plane_state->src_y = y << 16;
9450 plane_state->src_w = hdisplay << 16;
9451 plane_state->src_h = vdisplay << 16;
9452
9453 return 0;
9454}
9455
d2434ab7 9456bool intel_get_load_detect_pipe(struct drm_connector *connector,
7173188d 9457 struct drm_display_mode *mode,
51fd371b
RC
9458 struct intel_load_detect_pipe *old,
9459 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
9460{
9461 struct intel_crtc *intel_crtc;
d2434ab7
DV
9462 struct intel_encoder *intel_encoder =
9463 intel_attached_encoder(connector);
79e53945 9464 struct drm_crtc *possible_crtc;
4ef69c7a 9465 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
9466 struct drm_crtc *crtc = NULL;
9467 struct drm_device *dev = encoder->dev;
0f0f74bc 9468 struct drm_i915_private *dev_priv = to_i915(dev);
94352cf9 9469 struct drm_framebuffer *fb;
51fd371b 9470 struct drm_mode_config *config = &dev->mode_config;
edde3617 9471 struct drm_atomic_state *state = NULL, *restore_state = NULL;
944b0c76 9472 struct drm_connector_state *connector_state;
4be07317 9473 struct intel_crtc_state *crtc_state;
51fd371b 9474 int ret, i = -1;
79e53945 9475
d2dff872 9476 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 9477 connector->base.id, connector->name,
8e329a03 9478 encoder->base.id, encoder->name);
d2dff872 9479
edde3617
ML
9480 old->restore_state = NULL;
9481
51fd371b
RC
9482retry:
9483 ret = drm_modeset_lock(&config->connection_mutex, ctx);
9484 if (ret)
ad3c558f 9485 goto fail;
6e9f798d 9486
79e53945
JB
9487 /*
9488 * Algorithm gets a little messy:
7a5e4805 9489 *
79e53945
JB
9490 * - if the connector already has an assigned crtc, use it (but make
9491 * sure it's on first)
7a5e4805 9492 *
79e53945
JB
9493 * - try to find the first unused crtc that can drive this connector,
9494 * and use that if we find one
79e53945
JB
9495 */
9496
9497 /* See if we already have a CRTC for this connector */
edde3617
ML
9498 if (connector->state->crtc) {
9499 crtc = connector->state->crtc;
8261b191 9500
51fd371b 9501 ret = drm_modeset_lock(&crtc->mutex, ctx);
4d02e2de 9502 if (ret)
ad3c558f 9503 goto fail;
8261b191
CW
9504
9505 /* Make sure the crtc and connector are running */
edde3617 9506 goto found;
79e53945
JB
9507 }
9508
9509 /* Find an unused one (if possible) */
70e1e0ec 9510 for_each_crtc(dev, possible_crtc) {
79e53945
JB
9511 i++;
9512 if (!(encoder->possible_crtcs & (1 << i)))
9513 continue;
edde3617
ML
9514
9515 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
9516 if (ret)
9517 goto fail;
9518
9519 if (possible_crtc->state->enable) {
9520 drm_modeset_unlock(&possible_crtc->mutex);
a459249c 9521 continue;
edde3617 9522 }
a459249c
VS
9523
9524 crtc = possible_crtc;
9525 break;
79e53945
JB
9526 }
9527
9528 /*
9529 * If we didn't find an unused CRTC, don't use any.
9530 */
9531 if (!crtc) {
7173188d 9532 DRM_DEBUG_KMS("no pipe available for load-detect\n");
ad3c558f 9533 goto fail;
79e53945
JB
9534 }
9535
edde3617
ML
9536found:
9537 intel_crtc = to_intel_crtc(crtc);
9538
4d02e2de
DV
9539 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
9540 if (ret)
ad3c558f 9541 goto fail;
79e53945 9542
83a57153 9543 state = drm_atomic_state_alloc(dev);
edde3617
ML
9544 restore_state = drm_atomic_state_alloc(dev);
9545 if (!state || !restore_state) {
9546 ret = -ENOMEM;
9547 goto fail;
9548 }
83a57153
ACO
9549
9550 state->acquire_ctx = ctx;
edde3617 9551 restore_state->acquire_ctx = ctx;
83a57153 9552
944b0c76
ACO
9553 connector_state = drm_atomic_get_connector_state(state, connector);
9554 if (IS_ERR(connector_state)) {
9555 ret = PTR_ERR(connector_state);
9556 goto fail;
9557 }
9558
edde3617
ML
9559 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
9560 if (ret)
9561 goto fail;
944b0c76 9562
4be07317
ACO
9563 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
9564 if (IS_ERR(crtc_state)) {
9565 ret = PTR_ERR(crtc_state);
9566 goto fail;
9567 }
9568
49d6fa21 9569 crtc_state->base.active = crtc_state->base.enable = true;
4be07317 9570
6492711d
CW
9571 if (!mode)
9572 mode = &load_detect_mode;
79e53945 9573
d2dff872
CW
9574 /* We need a framebuffer large enough to accommodate all accesses
9575 * that the plane may generate whilst we perform load detection.
9576 * We can not rely on the fbcon either being present (we get called
9577 * during its initialisation to detect all boot displays, or it may
9578 * not even exist) or that it is large enough to satisfy the
9579 * requested mode.
9580 */
94352cf9
DV
9581 fb = mode_fits_in_fbdev(dev, mode);
9582 if (fb == NULL) {
d2dff872 9583 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9 9584 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
d2dff872
CW
9585 } else
9586 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 9587 if (IS_ERR(fb)) {
d2dff872 9588 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
412b61d8 9589 goto fail;
79e53945 9590 }
79e53945 9591
d3a40d1b
ACO
9592 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
9593 if (ret)
9594 goto fail;
9595
edde3617
ML
9596 drm_framebuffer_unreference(fb);
9597
9598 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
9599 if (ret)
9600 goto fail;
9601
9602 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
9603 if (!ret)
9604 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
9605 if (!ret)
9606 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
9607 if (ret) {
9608 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
9609 goto fail;
9610 }
8c7b5ccb 9611
3ba86073
ML
9612 ret = drm_atomic_commit(state);
9613 if (ret) {
6492711d 9614 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
412b61d8 9615 goto fail;
79e53945 9616 }
edde3617
ML
9617
9618 old->restore_state = restore_state;
7abbd11f 9619 drm_atomic_state_put(state);
7173188d 9620
79e53945 9621 /* let the connector get through one full cycle before testing */
0f0f74bc 9622 intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
7173188d 9623 return true;
412b61d8 9624
ad3c558f 9625fail:
7fb71c8f
CW
9626 if (state) {
9627 drm_atomic_state_put(state);
9628 state = NULL;
9629 }
9630 if (restore_state) {
9631 drm_atomic_state_put(restore_state);
9632 restore_state = NULL;
9633 }
83a57153 9634
51fd371b
RC
9635 if (ret == -EDEADLK) {
9636 drm_modeset_backoff(ctx);
9637 goto retry;
9638 }
9639
412b61d8 9640 return false;
79e53945
JB
9641}
9642
d2434ab7 9643void intel_release_load_detect_pipe(struct drm_connector *connector,
49172fee
ACO
9644 struct intel_load_detect_pipe *old,
9645 struct drm_modeset_acquire_ctx *ctx)
79e53945 9646{
d2434ab7
DV
9647 struct intel_encoder *intel_encoder =
9648 intel_attached_encoder(connector);
4ef69c7a 9649 struct drm_encoder *encoder = &intel_encoder->base;
edde3617 9650 struct drm_atomic_state *state = old->restore_state;
d3a40d1b 9651 int ret;
79e53945 9652
d2dff872 9653 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 9654 connector->base.id, connector->name,
8e329a03 9655 encoder->base.id, encoder->name);
d2dff872 9656
edde3617 9657 if (!state)
0622a53c 9658 return;
79e53945 9659
edde3617 9660 ret = drm_atomic_commit(state);
0853695c 9661 if (ret)
edde3617 9662 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
0853695c 9663 drm_atomic_state_put(state);
79e53945
JB
9664}
9665
da4a1efa 9666static int i9xx_pll_refclk(struct drm_device *dev,
5cec258b 9667 const struct intel_crtc_state *pipe_config)
da4a1efa 9668{
fac5e23e 9669 struct drm_i915_private *dev_priv = to_i915(dev);
da4a1efa
VS
9670 u32 dpll = pipe_config->dpll_hw_state.dpll;
9671
9672 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 9673 return dev_priv->vbt.lvds_ssc_freq;
6e266956 9674 else if (HAS_PCH_SPLIT(dev_priv))
da4a1efa 9675 return 120000;
5db94019 9676 else if (!IS_GEN2(dev_priv))
da4a1efa
VS
9677 return 96000;
9678 else
9679 return 48000;
9680}
9681
79e53945 9682/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc 9683static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 9684 struct intel_crtc_state *pipe_config)
79e53945 9685{
f1f644dc 9686 struct drm_device *dev = crtc->base.dev;
fac5e23e 9687 struct drm_i915_private *dev_priv = to_i915(dev);
f1f644dc 9688 int pipe = pipe_config->cpu_transcoder;
293623f7 9689 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945 9690 u32 fp;
9e2c8475 9691 struct dpll clock;
dccbea3b 9692 int port_clock;
da4a1efa 9693 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
9694
9695 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 9696 fp = pipe_config->dpll_hw_state.fp0;
79e53945 9697 else
293623f7 9698 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
9699
9700 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
9b1e14f4 9701 if (IS_PINEVIEW(dev_priv)) {
f2b115e6
AJ
9702 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
9703 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
9704 } else {
9705 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
9706 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
9707 }
9708
5db94019 9709 if (!IS_GEN2(dev_priv)) {
9b1e14f4 9710 if (IS_PINEVIEW(dev_priv))
f2b115e6
AJ
9711 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
9712 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
9713 else
9714 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
9715 DPLL_FPA01_P1_POST_DIV_SHIFT);
9716
9717 switch (dpll & DPLL_MODE_MASK) {
9718 case DPLLB_MODE_DAC_SERIAL:
9719 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
9720 5 : 10;
9721 break;
9722 case DPLLB_MODE_LVDS:
9723 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
9724 7 : 14;
9725 break;
9726 default:
28c97730 9727 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 9728 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 9729 return;
79e53945
JB
9730 }
9731
9b1e14f4 9732 if (IS_PINEVIEW(dev_priv))
dccbea3b 9733 port_clock = pnv_calc_dpll_params(refclk, &clock);
ac58c3f0 9734 else
dccbea3b 9735 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945 9736 } else {
50a0bc90 9737 u32 lvds = IS_I830(dev_priv) ? 0 : I915_READ(LVDS);
b1c560d1 9738 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
9739
9740 if (is_lvds) {
9741 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
9742 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
9743
9744 if (lvds & LVDS_CLKB_POWER_UP)
9745 clock.p2 = 7;
9746 else
9747 clock.p2 = 14;
79e53945
JB
9748 } else {
9749 if (dpll & PLL_P1_DIVIDE_BY_TWO)
9750 clock.p1 = 2;
9751 else {
9752 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
9753 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
9754 }
9755 if (dpll & PLL_P2_DIVIDE_BY_4)
9756 clock.p2 = 4;
9757 else
9758 clock.p2 = 2;
79e53945 9759 }
da4a1efa 9760
dccbea3b 9761 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945
JB
9762 }
9763
18442d08
VS
9764 /*
9765 * This value includes pixel_multiplier. We will use
241bfc38 9766 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
9767 * encoder's get_config() function.
9768 */
dccbea3b 9769 pipe_config->port_clock = port_clock;
f1f644dc
JB
9770}
9771
6878da05
VS
9772int intel_dotclock_calculate(int link_freq,
9773 const struct intel_link_m_n *m_n)
f1f644dc 9774{
f1f644dc
JB
9775 /*
9776 * The calculation for the data clock is:
1041a02f 9777 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 9778 * But we want to avoid losing precison if possible, so:
1041a02f 9779 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
9780 *
9781 * and the link clock is simpler:
1041a02f 9782 * link_clock = (m * link_clock) / n
f1f644dc
JB
9783 */
9784
6878da05
VS
9785 if (!m_n->link_n)
9786 return 0;
f1f644dc 9787
6878da05
VS
9788 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
9789}
f1f644dc 9790
18442d08 9791static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 9792 struct intel_crtc_state *pipe_config)
6878da05 9793{
e3b247da 9794 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
79e53945 9795
18442d08
VS
9796 /* read out port_clock from the DPLL */
9797 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 9798
f1f644dc 9799 /*
e3b247da
VS
9800 * In case there is an active pipe without active ports,
9801 * we may need some idea for the dotclock anyway.
9802 * Calculate one based on the FDI configuration.
79e53945 9803 */
2d112de7 9804 pipe_config->base.adjusted_mode.crtc_clock =
21a727b3 9805 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
18442d08 9806 &pipe_config->fdi_m_n);
79e53945
JB
9807}
9808
9809/** Returns the currently programmed mode of the given pipe. */
9810struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
9811 struct drm_crtc *crtc)
9812{
fac5e23e 9813 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 9814 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 9815 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
79e53945 9816 struct drm_display_mode *mode;
3f36b937 9817 struct intel_crtc_state *pipe_config;
fe2b8f9d
PZ
9818 int htot = I915_READ(HTOTAL(cpu_transcoder));
9819 int hsync = I915_READ(HSYNC(cpu_transcoder));
9820 int vtot = I915_READ(VTOTAL(cpu_transcoder));
9821 int vsync = I915_READ(VSYNC(cpu_transcoder));
293623f7 9822 enum pipe pipe = intel_crtc->pipe;
79e53945
JB
9823
9824 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
9825 if (!mode)
9826 return NULL;
9827
3f36b937
TU
9828 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
9829 if (!pipe_config) {
9830 kfree(mode);
9831 return NULL;
9832 }
9833
f1f644dc
JB
9834 /*
9835 * Construct a pipe_config sufficient for getting the clock info
9836 * back out of crtc_clock_get.
9837 *
9838 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
9839 * to use a real value here instead.
9840 */
3f36b937
TU
9841 pipe_config->cpu_transcoder = (enum transcoder) pipe;
9842 pipe_config->pixel_multiplier = 1;
9843 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
9844 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
9845 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
9846 i9xx_crtc_clock_get(intel_crtc, pipe_config);
9847
9848 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
79e53945
JB
9849 mode->hdisplay = (htot & 0xffff) + 1;
9850 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
9851 mode->hsync_start = (hsync & 0xffff) + 1;
9852 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
9853 mode->vdisplay = (vtot & 0xffff) + 1;
9854 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
9855 mode->vsync_start = (vsync & 0xffff) + 1;
9856 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
9857
9858 drm_mode_set_name(mode);
79e53945 9859
3f36b937
TU
9860 kfree(pipe_config);
9861
79e53945
JB
9862 return mode;
9863}
9864
9865static void intel_crtc_destroy(struct drm_crtc *crtc)
9866{
9867 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
67e77c5a 9868 struct drm_device *dev = crtc->dev;
51cbaf01 9869 struct intel_flip_work *work;
67e77c5a 9870
5e2d7afc 9871 spin_lock_irq(&dev->event_lock);
5a21b665
DV
9872 work = intel_crtc->flip_work;
9873 intel_crtc->flip_work = NULL;
9874 spin_unlock_irq(&dev->event_lock);
67e77c5a 9875
5a21b665 9876 if (work) {
51cbaf01
ML
9877 cancel_work_sync(&work->mmio_work);
9878 cancel_work_sync(&work->unpin_work);
5a21b665 9879 kfree(work);
67e77c5a 9880 }
79e53945
JB
9881
9882 drm_crtc_cleanup(crtc);
67e77c5a 9883
79e53945
JB
9884 kfree(intel_crtc);
9885}
9886
6b95a207
KH
9887static void intel_unpin_work_fn(struct work_struct *__work)
9888{
51cbaf01
ML
9889 struct intel_flip_work *work =
9890 container_of(__work, struct intel_flip_work, unpin_work);
5a21b665
DV
9891 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
9892 struct drm_device *dev = crtc->base.dev;
9893 struct drm_plane *primary = crtc->base.primary;
03f476e1 9894
5a21b665
DV
9895 if (is_mmio_work(work))
9896 flush_work(&work->mmio_work);
03f476e1 9897
5a21b665 9898 mutex_lock(&dev->struct_mutex);
be1e3415 9899 intel_unpin_fb_vma(work->old_vma);
f8c417cd 9900 i915_gem_object_put(work->pending_flip_obj);
5a21b665 9901 mutex_unlock(&dev->struct_mutex);
143f73b3 9902
e8a261ea
CW
9903 i915_gem_request_put(work->flip_queued_req);
9904
5748b6a1
CW
9905 intel_frontbuffer_flip_complete(to_i915(dev),
9906 to_intel_plane(primary)->frontbuffer_bit);
5a21b665
DV
9907 intel_fbc_post_update(crtc);
9908 drm_framebuffer_unreference(work->old_fb);
143f73b3 9909
5a21b665
DV
9910 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
9911 atomic_dec(&crtc->unpin_work_count);
a6747b73 9912
5a21b665
DV
9913 kfree(work);
9914}
d9e86c0e 9915
5a21b665
DV
9916/* Is 'a' after or equal to 'b'? */
9917static bool g4x_flip_count_after_eq(u32 a, u32 b)
9918{
9919 return !((a - b) & 0x80000000);
9920}
143f73b3 9921
5a21b665
DV
9922static bool __pageflip_finished_cs(struct intel_crtc *crtc,
9923 struct intel_flip_work *work)
9924{
9925 struct drm_device *dev = crtc->base.dev;
fac5e23e 9926 struct drm_i915_private *dev_priv = to_i915(dev);
143f73b3 9927
8af29b0c 9928 if (abort_flip_on_reset(crtc))
5a21b665 9929 return true;
143f73b3 9930
5a21b665
DV
9931 /*
9932 * The relevant registers doen't exist on pre-ctg.
9933 * As the flip done interrupt doesn't trigger for mmio
9934 * flips on gmch platforms, a flip count check isn't
9935 * really needed there. But since ctg has the registers,
9936 * include it in the check anyway.
9937 */
9beb5fea 9938 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
5a21b665 9939 return true;
b4a98e57 9940
5a21b665
DV
9941 /*
9942 * BDW signals flip done immediately if the plane
9943 * is disabled, even if the plane enable is already
9944 * armed to occur at the next vblank :(
9945 */
f99d7069 9946
5a21b665
DV
9947 /*
9948 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
9949 * used the same base address. In that case the mmio flip might
9950 * have completed, but the CS hasn't even executed the flip yet.
9951 *
9952 * A flip count check isn't enough as the CS might have updated
9953 * the base address just after start of vblank, but before we
9954 * managed to process the interrupt. This means we'd complete the
9955 * CS flip too soon.
9956 *
9957 * Combining both checks should get us a good enough result. It may
9958 * still happen that the CS flip has been executed, but has not
9959 * yet actually completed. But in case the base address is the same
9960 * anyway, we don't really care.
9961 */
9962 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
9963 crtc->flip_work->gtt_offset &&
9964 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
9965 crtc->flip_work->flip_count);
9966}
b4a98e57 9967
5a21b665
DV
9968static bool
9969__pageflip_finished_mmio(struct intel_crtc *crtc,
9970 struct intel_flip_work *work)
9971{
9972 /*
9973 * MMIO work completes when vblank is different from
9974 * flip_queued_vblank.
9975 *
9976 * Reset counter value doesn't matter, this is handled by
9977 * i915_wait_request finishing early, so no need to handle
9978 * reset here.
9979 */
9980 return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
6b95a207
KH
9981}
9982
51cbaf01
ML
9983
9984static bool pageflip_finished(struct intel_crtc *crtc,
9985 struct intel_flip_work *work)
9986{
9987 if (!atomic_read(&work->pending))
9988 return false;
9989
9990 smp_rmb();
9991
5a21b665
DV
9992 if (is_mmio_work(work))
9993 return __pageflip_finished_mmio(crtc, work);
9994 else
9995 return __pageflip_finished_cs(crtc, work);
9996}
9997
9998void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
9999{
91c8a326 10000 struct drm_device *dev = &dev_priv->drm;
98187836 10001 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
5a21b665
DV
10002 struct intel_flip_work *work;
10003 unsigned long flags;
10004
10005 /* Ignore early vblank irqs */
10006 if (!crtc)
10007 return;
10008
51cbaf01 10009 /*
5a21b665
DV
10010 * This is called both by irq handlers and the reset code (to complete
10011 * lost pageflips) so needs the full irqsave spinlocks.
51cbaf01 10012 */
5a21b665 10013 spin_lock_irqsave(&dev->event_lock, flags);
e2af48c6 10014 work = crtc->flip_work;
5a21b665
DV
10015
10016 if (work != NULL &&
10017 !is_mmio_work(work) &&
e2af48c6
VS
10018 pageflip_finished(crtc, work))
10019 page_flip_completed(crtc);
5a21b665
DV
10020
10021 spin_unlock_irqrestore(&dev->event_lock, flags);
75f7f3ec
VS
10022}
10023
51cbaf01 10024void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
6b95a207 10025{
91c8a326 10026 struct drm_device *dev = &dev_priv->drm;
98187836 10027 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
51cbaf01 10028 struct intel_flip_work *work;
6b95a207
KH
10029 unsigned long flags;
10030
5251f04e
ML
10031 /* Ignore early vblank irqs */
10032 if (!crtc)
10033 return;
f326038a
DV
10034
10035 /*
10036 * This is called both by irq handlers and the reset code (to complete
10037 * lost pageflips) so needs the full irqsave spinlocks.
e7d841ca 10038 */
6b95a207 10039 spin_lock_irqsave(&dev->event_lock, flags);
e2af48c6 10040 work = crtc->flip_work;
5251f04e 10041
5a21b665
DV
10042 if (work != NULL &&
10043 is_mmio_work(work) &&
e2af48c6
VS
10044 pageflip_finished(crtc, work))
10045 page_flip_completed(crtc);
5251f04e 10046
6b95a207
KH
10047 spin_unlock_irqrestore(&dev->event_lock, flags);
10048}
10049
5a21b665
DV
10050static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
10051 struct intel_flip_work *work)
84c33a64 10052{
5a21b665 10053 work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
84c33a64 10054
5a21b665
DV
10055 /* Ensure that the work item is consistent when activating it ... */
10056 smp_mb__before_atomic();
10057 atomic_set(&work->pending, 1);
10058}
a6747b73 10059
5a21b665
DV
10060static int intel_gen2_queue_flip(struct drm_device *dev,
10061 struct drm_crtc *crtc,
10062 struct drm_framebuffer *fb,
10063 struct drm_i915_gem_object *obj,
10064 struct drm_i915_gem_request *req,
10065 uint32_t flags)
10066{
5a21b665 10067 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
73dec95e 10068 u32 flip_mask, *cs;
143f73b3 10069
73dec95e
TU
10070 cs = intel_ring_begin(req, 6);
10071 if (IS_ERR(cs))
10072 return PTR_ERR(cs);
143f73b3 10073
5a21b665
DV
10074 /* Can't queue multiple flips, so wait for the previous
10075 * one to finish before executing the next.
10076 */
10077 if (intel_crtc->plane)
10078 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10079 else
10080 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
73dec95e
TU
10081 *cs++ = MI_WAIT_FOR_EVENT | flip_mask;
10082 *cs++ = MI_NOOP;
10083 *cs++ = MI_DISPLAY_FLIP | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
10084 *cs++ = fb->pitches[0];
10085 *cs++ = intel_crtc->flip_work->gtt_offset;
10086 *cs++ = 0; /* aux display base address, unused */
143f73b3 10087
5a21b665
DV
10088 return 0;
10089}
84c33a64 10090
5a21b665
DV
10091static int intel_gen3_queue_flip(struct drm_device *dev,
10092 struct drm_crtc *crtc,
10093 struct drm_framebuffer *fb,
10094 struct drm_i915_gem_object *obj,
10095 struct drm_i915_gem_request *req,
10096 uint32_t flags)
10097{
5a21b665 10098 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
73dec95e 10099 u32 flip_mask, *cs;
d55dbd06 10100
73dec95e
TU
10101 cs = intel_ring_begin(req, 6);
10102 if (IS_ERR(cs))
10103 return PTR_ERR(cs);
d55dbd06 10104
5a21b665
DV
10105 if (intel_crtc->plane)
10106 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10107 else
10108 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
73dec95e
TU
10109 *cs++ = MI_WAIT_FOR_EVENT | flip_mask;
10110 *cs++ = MI_NOOP;
10111 *cs++ = MI_DISPLAY_FLIP_I915 | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
10112 *cs++ = fb->pitches[0];
10113 *cs++ = intel_crtc->flip_work->gtt_offset;
10114 *cs++ = MI_NOOP;
fd8e058a 10115
5a21b665
DV
10116 return 0;
10117}
84c33a64 10118
5a21b665
DV
10119static int intel_gen4_queue_flip(struct drm_device *dev,
10120 struct drm_crtc *crtc,
10121 struct drm_framebuffer *fb,
10122 struct drm_i915_gem_object *obj,
10123 struct drm_i915_gem_request *req,
10124 uint32_t flags)
10125{
fac5e23e 10126 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665 10127 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
73dec95e 10128 u32 pf, pipesrc, *cs;
143f73b3 10129
73dec95e
TU
10130 cs = intel_ring_begin(req, 4);
10131 if (IS_ERR(cs))
10132 return PTR_ERR(cs);
143f73b3 10133
5a21b665
DV
10134 /* i965+ uses the linear or tiled offsets from the
10135 * Display Registers (which do not change across a page-flip)
10136 * so we need only reprogram the base address.
10137 */
73dec95e
TU
10138 *cs++ = MI_DISPLAY_FLIP | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
10139 *cs++ = fb->pitches[0];
10140 *cs++ = intel_crtc->flip_work->gtt_offset |
10141 intel_fb_modifier_to_tiling(fb->modifier);
5a21b665
DV
10142
10143 /* XXX Enabling the panel-fitter across page-flip is so far
10144 * untested on non-native modes, so ignore it for now.
10145 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
10146 */
10147 pf = 0;
10148 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
73dec95e 10149 *cs++ = pf | pipesrc;
143f73b3 10150
5a21b665 10151 return 0;
8c9f3aaf
JB
10152}
10153
5a21b665
DV
10154static int intel_gen6_queue_flip(struct drm_device *dev,
10155 struct drm_crtc *crtc,
10156 struct drm_framebuffer *fb,
10157 struct drm_i915_gem_object *obj,
10158 struct drm_i915_gem_request *req,
10159 uint32_t flags)
da20eabd 10160{
fac5e23e 10161 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665 10162 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
73dec95e 10163 u32 pf, pipesrc, *cs;
d21fbe87 10164
73dec95e
TU
10165 cs = intel_ring_begin(req, 4);
10166 if (IS_ERR(cs))
10167 return PTR_ERR(cs);
92826fcd 10168
73dec95e
TU
10169 *cs++ = MI_DISPLAY_FLIP | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
10170 *cs++ = fb->pitches[0] | intel_fb_modifier_to_tiling(fb->modifier);
10171 *cs++ = intel_crtc->flip_work->gtt_offset;
92826fcd 10172
5a21b665
DV
10173 /* Contrary to the suggestions in the documentation,
10174 * "Enable Panel Fitter" does not seem to be required when page
10175 * flipping with a non-native mode, and worse causes a normal
10176 * modeset to fail.
10177 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
10178 */
10179 pf = 0;
10180 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
73dec95e 10181 *cs++ = pf | pipesrc;
7809e5ae 10182
5a21b665 10183 return 0;
7809e5ae
MR
10184}
10185
5a21b665
DV
10186static int intel_gen7_queue_flip(struct drm_device *dev,
10187 struct drm_crtc *crtc,
10188 struct drm_framebuffer *fb,
10189 struct drm_i915_gem_object *obj,
10190 struct drm_i915_gem_request *req,
10191 uint32_t flags)
d21fbe87 10192{
5db94019 10193 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665 10194 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
73dec95e 10195 u32 *cs, plane_bit = 0;
5a21b665 10196 int len, ret;
d21fbe87 10197
5a21b665
DV
10198 switch (intel_crtc->plane) {
10199 case PLANE_A:
10200 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
10201 break;
10202 case PLANE_B:
10203 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
10204 break;
10205 case PLANE_C:
10206 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
10207 break;
10208 default:
10209 WARN_ONCE(1, "unknown plane in flip command\n");
10210 return -ENODEV;
10211 }
10212
10213 len = 4;
b5321f30 10214 if (req->engine->id == RCS) {
5a21b665
DV
10215 len += 6;
10216 /*
10217 * On Gen 8, SRM is now taking an extra dword to accommodate
10218 * 48bits addresses, and we need a NOOP for the batch size to
10219 * stay even.
10220 */
5db94019 10221 if (IS_GEN8(dev_priv))
5a21b665
DV
10222 len += 2;
10223 }
10224
10225 /*
10226 * BSpec MI_DISPLAY_FLIP for IVB:
10227 * "The full packet must be contained within the same cache line."
10228 *
10229 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
10230 * cacheline, if we ever start emitting more commands before
10231 * the MI_DISPLAY_FLIP we may need to first emit everything else,
10232 * then do the cacheline alignment, and finally emit the
10233 * MI_DISPLAY_FLIP.
10234 */
10235 ret = intel_ring_cacheline_align(req);
10236 if (ret)
10237 return ret;
10238
73dec95e
TU
10239 cs = intel_ring_begin(req, len);
10240 if (IS_ERR(cs))
10241 return PTR_ERR(cs);
5a21b665
DV
10242
10243 /* Unmask the flip-done completion message. Note that the bspec says that
10244 * we should do this for both the BCS and RCS, and that we must not unmask
10245 * more than one flip event at any time (or ensure that one flip message
10246 * can be sent by waiting for flip-done prior to queueing new flips).
10247 * Experimentation says that BCS works despite DERRMR masking all
10248 * flip-done completion events and that unmasking all planes at once
10249 * for the RCS also doesn't appear to drop events. Setting the DERRMR
10250 * to zero does lead to lockups within MI_DISPLAY_FLIP.
10251 */
b5321f30 10252 if (req->engine->id == RCS) {
73dec95e
TU
10253 *cs++ = MI_LOAD_REGISTER_IMM(1);
10254 *cs++ = i915_mmio_reg_offset(DERRMR);
10255 *cs++ = ~(DERRMR_PIPEA_PRI_FLIP_DONE |
10256 DERRMR_PIPEB_PRI_FLIP_DONE |
10257 DERRMR_PIPEC_PRI_FLIP_DONE);
5db94019 10258 if (IS_GEN8(dev_priv))
73dec95e
TU
10259 *cs++ = MI_STORE_REGISTER_MEM_GEN8 |
10260 MI_SRM_LRM_GLOBAL_GTT;
5a21b665 10261 else
73dec95e
TU
10262 *cs++ = MI_STORE_REGISTER_MEM | MI_SRM_LRM_GLOBAL_GTT;
10263 *cs++ = i915_mmio_reg_offset(DERRMR);
10264 *cs++ = i915_ggtt_offset(req->engine->scratch) + 256;
5db94019 10265 if (IS_GEN8(dev_priv)) {
73dec95e
TU
10266 *cs++ = 0;
10267 *cs++ = MI_NOOP;
5a21b665
DV
10268 }
10269 }
10270
73dec95e
TU
10271 *cs++ = MI_DISPLAY_FLIP_I915 | plane_bit;
10272 *cs++ = fb->pitches[0] | intel_fb_modifier_to_tiling(fb->modifier);
10273 *cs++ = intel_crtc->flip_work->gtt_offset;
10274 *cs++ = MI_NOOP;
5a21b665
DV
10275
10276 return 0;
10277}
10278
10279static bool use_mmio_flip(struct intel_engine_cs *engine,
10280 struct drm_i915_gem_object *obj)
10281{
10282 /*
10283 * This is not being used for older platforms, because
10284 * non-availability of flip done interrupt forces us to use
10285 * CS flips. Older platforms derive flip done using some clever
10286 * tricks involving the flip_pending status bits and vblank irqs.
10287 * So using MMIO flips there would disrupt this mechanism.
10288 */
10289
10290 if (engine == NULL)
10291 return true;
10292
10293 if (INTEL_GEN(engine->i915) < 5)
10294 return false;
10295
10296 if (i915.use_mmio_flip < 0)
10297 return false;
10298 else if (i915.use_mmio_flip > 0)
10299 return true;
10300 else if (i915.enable_execlists)
10301 return true;
c37efb99 10302
d07f0e59 10303 return engine != i915_gem_object_last_write_engine(obj);
5a21b665
DV
10304}
10305
10306static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
10307 unsigned int rotation,
10308 struct intel_flip_work *work)
10309{
10310 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 10311 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
DV
10312 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
10313 const enum pipe pipe = intel_crtc->pipe;
d2196774 10314 u32 ctl, stride = skl_plane_stride(fb, 0, rotation);
5a21b665
DV
10315
10316 ctl = I915_READ(PLANE_CTL(pipe, 0));
10317 ctl &= ~PLANE_CTL_TILED_MASK;
bae781b2 10318 switch (fb->modifier) {
5a21b665
DV
10319 case DRM_FORMAT_MOD_NONE:
10320 break;
10321 case I915_FORMAT_MOD_X_TILED:
10322 ctl |= PLANE_CTL_TILED_X;
10323 break;
10324 case I915_FORMAT_MOD_Y_TILED:
10325 ctl |= PLANE_CTL_TILED_Y;
10326 break;
10327 case I915_FORMAT_MOD_Yf_TILED:
10328 ctl |= PLANE_CTL_TILED_YF;
10329 break;
10330 default:
bae781b2 10331 MISSING_CASE(fb->modifier);
5a21b665
DV
10332 }
10333
5a21b665
DV
10334 /*
10335 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
10336 * PLANE_SURF updates, the update is then guaranteed to be atomic.
10337 */
10338 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
10339 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
10340
10341 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
10342 POSTING_READ(PLANE_SURF(pipe, 0));
10343}
10344
10345static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
10346 struct intel_flip_work *work)
10347{
10348 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 10349 struct drm_i915_private *dev_priv = to_i915(dev);
72618ebf 10350 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
5a21b665
DV
10351 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
10352 u32 dspcntr;
10353
10354 dspcntr = I915_READ(reg);
10355
bae781b2 10356 if (fb->modifier == I915_FORMAT_MOD_X_TILED)
5a21b665
DV
10357 dspcntr |= DISPPLANE_TILED;
10358 else
10359 dspcntr &= ~DISPPLANE_TILED;
10360
10361 I915_WRITE(reg, dspcntr);
10362
10363 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
10364 POSTING_READ(DSPSURF(intel_crtc->plane));
10365}
10366
10367static void intel_mmio_flip_work_func(struct work_struct *w)
10368{
10369 struct intel_flip_work *work =
10370 container_of(w, struct intel_flip_work, mmio_work);
10371 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10372 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10373 struct intel_framebuffer *intel_fb =
10374 to_intel_framebuffer(crtc->base.primary->fb);
10375 struct drm_i915_gem_object *obj = intel_fb->obj;
10376
d07f0e59 10377 WARN_ON(i915_gem_object_wait(obj, 0, MAX_SCHEDULE_TIMEOUT, NULL) < 0);
5a21b665
DV
10378
10379 intel_pipe_update_start(crtc);
10380
10381 if (INTEL_GEN(dev_priv) >= 9)
10382 skl_do_mmio_flip(crtc, work->rotation, work);
10383 else
10384 /* use_mmio_flip() retricts MMIO flips to ilk+ */
10385 ilk_do_mmio_flip(crtc, work);
10386
10387 intel_pipe_update_end(crtc, work);
10388}
10389
10390static int intel_default_queue_flip(struct drm_device *dev,
10391 struct drm_crtc *crtc,
10392 struct drm_framebuffer *fb,
10393 struct drm_i915_gem_object *obj,
10394 struct drm_i915_gem_request *req,
10395 uint32_t flags)
10396{
10397 return -ENODEV;
10398}
10399
10400static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
10401 struct intel_crtc *intel_crtc,
10402 struct intel_flip_work *work)
10403{
10404 u32 addr, vblank;
10405
10406 if (!atomic_read(&work->pending))
10407 return false;
10408
10409 smp_rmb();
10410
10411 vblank = intel_crtc_get_vblank_counter(intel_crtc);
10412 if (work->flip_ready_vblank == 0) {
10413 if (work->flip_queued_req &&
f69a02c9 10414 !i915_gem_request_completed(work->flip_queued_req))
5a21b665
DV
10415 return false;
10416
10417 work->flip_ready_vblank = vblank;
10418 }
10419
10420 if (vblank - work->flip_ready_vblank < 3)
10421 return false;
10422
10423 /* Potential stall - if we see that the flip has happened,
10424 * assume a missed interrupt. */
10425 if (INTEL_GEN(dev_priv) >= 4)
10426 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
10427 else
10428 addr = I915_READ(DSPADDR(intel_crtc->plane));
10429
10430 /* There is a potential issue here with a false positive after a flip
10431 * to the same address. We could address this by checking for a
10432 * non-incrementing frame counter.
10433 */
10434 return addr == work->gtt_offset;
10435}
10436
10437void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
10438{
91c8a326 10439 struct drm_device *dev = &dev_priv->drm;
98187836 10440 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
5a21b665
DV
10441 struct intel_flip_work *work;
10442
10443 WARN_ON(!in_interrupt());
10444
10445 if (crtc == NULL)
10446 return;
10447
10448 spin_lock(&dev->event_lock);
e2af48c6 10449 work = crtc->flip_work;
5a21b665
DV
10450
10451 if (work != NULL && !is_mmio_work(work) &&
e2af48c6 10452 __pageflip_stall_check_cs(dev_priv, crtc, work)) {
5a21b665
DV
10453 WARN_ONCE(1,
10454 "Kicking stuck page flip: queued at %d, now %d\n",
e2af48c6
VS
10455 work->flip_queued_vblank, intel_crtc_get_vblank_counter(crtc));
10456 page_flip_completed(crtc);
5a21b665
DV
10457 work = NULL;
10458 }
10459
10460 if (work != NULL && !is_mmio_work(work) &&
e2af48c6 10461 intel_crtc_get_vblank_counter(crtc) - work->flip_queued_vblank > 1)
5a21b665
DV
10462 intel_queue_rps_boost_for_request(work->flip_queued_req);
10463 spin_unlock(&dev->event_lock);
10464}
10465
4c01ded5 10466__maybe_unused
5a21b665
DV
10467static int intel_crtc_page_flip(struct drm_crtc *crtc,
10468 struct drm_framebuffer *fb,
10469 struct drm_pending_vblank_event *event,
10470 uint32_t page_flip_flags)
10471{
10472 struct drm_device *dev = crtc->dev;
fac5e23e 10473 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
DV
10474 struct drm_framebuffer *old_fb = crtc->primary->fb;
10475 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
10476 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10477 struct drm_plane *primary = crtc->primary;
10478 enum pipe pipe = intel_crtc->pipe;
10479 struct intel_flip_work *work;
10480 struct intel_engine_cs *engine;
10481 bool mmio_flip;
8e637178 10482 struct drm_i915_gem_request *request;
058d88c4 10483 struct i915_vma *vma;
5a21b665
DV
10484 int ret;
10485
10486 /*
10487 * drm_mode_page_flip_ioctl() should already catch this, but double
10488 * check to be safe. In the future we may enable pageflipping from
10489 * a disabled primary plane.
10490 */
10491 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
10492 return -EBUSY;
10493
10494 /* Can't change pixel format via MI display flips. */
dbd4d576 10495 if (fb->format != crtc->primary->fb->format)
5a21b665
DV
10496 return -EINVAL;
10497
10498 /*
10499 * TILEOFF/LINOFF registers can't be changed via MI display flips.
10500 * Note that pitch changes could also affect these register.
10501 */
6315b5d3 10502 if (INTEL_GEN(dev_priv) > 3 &&
5a21b665
DV
10503 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
10504 fb->pitches[0] != crtc->primary->fb->pitches[0]))
10505 return -EINVAL;
10506
10507 if (i915_terminally_wedged(&dev_priv->gpu_error))
10508 goto out_hang;
10509
10510 work = kzalloc(sizeof(*work), GFP_KERNEL);
10511 if (work == NULL)
10512 return -ENOMEM;
10513
10514 work->event = event;
10515 work->crtc = crtc;
10516 work->old_fb = old_fb;
10517 INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
10518
10519 ret = drm_crtc_vblank_get(crtc);
10520 if (ret)
10521 goto free_work;
10522
10523 /* We borrow the event spin lock for protecting flip_work */
10524 spin_lock_irq(&dev->event_lock);
10525 if (intel_crtc->flip_work) {
10526 /* Before declaring the flip queue wedged, check if
10527 * the hardware completed the operation behind our backs.
10528 */
10529 if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
10530 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
10531 page_flip_completed(intel_crtc);
10532 } else {
10533 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
10534 spin_unlock_irq(&dev->event_lock);
10535
10536 drm_crtc_vblank_put(crtc);
10537 kfree(work);
10538 return -EBUSY;
10539 }
10540 }
10541 intel_crtc->flip_work = work;
10542 spin_unlock_irq(&dev->event_lock);
10543
10544 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
10545 flush_workqueue(dev_priv->wq);
10546
10547 /* Reference the objects for the scheduled work. */
10548 drm_framebuffer_reference(work->old_fb);
5a21b665
DV
10549
10550 crtc->primary->fb = fb;
10551 update_state_fb(crtc->primary);
faf68d92 10552
25dc556a 10553 work->pending_flip_obj = i915_gem_object_get(obj);
5a21b665
DV
10554
10555 ret = i915_mutex_lock_interruptible(dev);
10556 if (ret)
10557 goto cleanup;
10558
8af29b0c
CW
10559 intel_crtc->reset_count = i915_reset_count(&dev_priv->gpu_error);
10560 if (i915_reset_in_progress_or_wedged(&dev_priv->gpu_error)) {
5a21b665 10561 ret = -EIO;
ddbb271a 10562 goto unlock;
5a21b665
DV
10563 }
10564
10565 atomic_inc(&intel_crtc->unpin_work_count);
10566
9beb5fea 10567 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
5a21b665
DV
10568 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
10569
920a14b2 10570 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
3b3f1650 10571 engine = dev_priv->engine[BCS];
bae781b2 10572 if (fb->modifier != old_fb->modifier)
5a21b665
DV
10573 /* vlv: DISPLAY_FLIP fails to change tiling */
10574 engine = NULL;
fd6b8f43 10575 } else if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
3b3f1650 10576 engine = dev_priv->engine[BCS];
6315b5d3 10577 } else if (INTEL_GEN(dev_priv) >= 7) {
d07f0e59 10578 engine = i915_gem_object_last_write_engine(obj);
5a21b665 10579 if (engine == NULL || engine->id != RCS)
3b3f1650 10580 engine = dev_priv->engine[BCS];
5a21b665 10581 } else {
3b3f1650 10582 engine = dev_priv->engine[RCS];
5a21b665
DV
10583 }
10584
10585 mmio_flip = use_mmio_flip(engine, obj);
10586
058d88c4
CW
10587 vma = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
10588 if (IS_ERR(vma)) {
10589 ret = PTR_ERR(vma);
5a21b665 10590 goto cleanup_pending;
058d88c4 10591 }
5a21b665 10592
be1e3415
CW
10593 work->old_vma = to_intel_plane_state(primary->state)->vma;
10594 to_intel_plane_state(primary->state)->vma = vma;
10595
10596 work->gtt_offset = i915_ggtt_offset(vma) + intel_crtc->dspaddr_offset;
5a21b665
DV
10597 work->rotation = crtc->primary->state->rotation;
10598
1f061316
PZ
10599 /*
10600 * There's the potential that the next frame will not be compatible with
10601 * FBC, so we want to call pre_update() before the actual page flip.
10602 * The problem is that pre_update() caches some information about the fb
10603 * object, so we want to do this only after the object is pinned. Let's
10604 * be on the safe side and do this immediately before scheduling the
10605 * flip.
10606 */
10607 intel_fbc_pre_update(intel_crtc, intel_crtc->config,
10608 to_intel_plane_state(primary->state));
10609
5a21b665
DV
10610 if (mmio_flip) {
10611 INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
6277c8d0 10612 queue_work(system_unbound_wq, &work->mmio_work);
5a21b665 10613 } else {
e8a9c58f
CW
10614 request = i915_gem_request_alloc(engine,
10615 dev_priv->kernel_context);
8e637178
CW
10616 if (IS_ERR(request)) {
10617 ret = PTR_ERR(request);
10618 goto cleanup_unpin;
10619 }
10620
a2bc4695 10621 ret = i915_gem_request_await_object(request, obj, false);
8e637178
CW
10622 if (ret)
10623 goto cleanup_request;
10624
5a21b665
DV
10625 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
10626 page_flip_flags);
10627 if (ret)
8e637178 10628 goto cleanup_request;
5a21b665
DV
10629
10630 intel_mark_page_flip_active(intel_crtc, work);
10631
8e637178 10632 work->flip_queued_req = i915_gem_request_get(request);
5a21b665
DV
10633 i915_add_request_no_flush(request);
10634 }
10635
92117f0b 10636 i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
5a21b665
DV
10637 i915_gem_track_fb(intel_fb_obj(old_fb), obj,
10638 to_intel_plane(primary)->frontbuffer_bit);
10639 mutex_unlock(&dev->struct_mutex);
10640
5748b6a1 10641 intel_frontbuffer_flip_prepare(to_i915(dev),
5a21b665
DV
10642 to_intel_plane(primary)->frontbuffer_bit);
10643
10644 trace_i915_flip_request(intel_crtc->plane, obj);
10645
10646 return 0;
10647
8e637178
CW
10648cleanup_request:
10649 i915_add_request_no_flush(request);
5a21b665 10650cleanup_unpin:
be1e3415
CW
10651 to_intel_plane_state(primary->state)->vma = work->old_vma;
10652 intel_unpin_fb_vma(vma);
5a21b665 10653cleanup_pending:
5a21b665 10654 atomic_dec(&intel_crtc->unpin_work_count);
ddbb271a 10655unlock:
5a21b665
DV
10656 mutex_unlock(&dev->struct_mutex);
10657cleanup:
10658 crtc->primary->fb = old_fb;
10659 update_state_fb(crtc->primary);
10660
f0cd5182 10661 i915_gem_object_put(obj);
5a21b665
DV
10662 drm_framebuffer_unreference(work->old_fb);
10663
10664 spin_lock_irq(&dev->event_lock);
10665 intel_crtc->flip_work = NULL;
10666 spin_unlock_irq(&dev->event_lock);
10667
10668 drm_crtc_vblank_put(crtc);
10669free_work:
10670 kfree(work);
10671
10672 if (ret == -EIO) {
10673 struct drm_atomic_state *state;
10674 struct drm_plane_state *plane_state;
10675
10676out_hang:
10677 state = drm_atomic_state_alloc(dev);
10678 if (!state)
10679 return -ENOMEM;
10680 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
10681
10682retry:
10683 plane_state = drm_atomic_get_plane_state(state, primary);
10684 ret = PTR_ERR_OR_ZERO(plane_state);
10685 if (!ret) {
10686 drm_atomic_set_fb_for_plane(plane_state, fb);
10687
10688 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
10689 if (!ret)
10690 ret = drm_atomic_commit(state);
10691 }
10692
10693 if (ret == -EDEADLK) {
10694 drm_modeset_backoff(state->acquire_ctx);
10695 drm_atomic_state_clear(state);
10696 goto retry;
10697 }
10698
0853695c 10699 drm_atomic_state_put(state);
5a21b665
DV
10700
10701 if (ret == 0 && event) {
10702 spin_lock_irq(&dev->event_lock);
10703 drm_crtc_send_vblank_event(crtc, event);
10704 spin_unlock_irq(&dev->event_lock);
10705 }
10706 }
10707 return ret;
10708}
10709
10710
10711/**
10712 * intel_wm_need_update - Check whether watermarks need updating
10713 * @plane: drm plane
10714 * @state: new plane state
10715 *
10716 * Check current plane state versus the new one to determine whether
10717 * watermarks need to be recalculated.
10718 *
10719 * Returns true or false.
10720 */
10721static bool intel_wm_need_update(struct drm_plane *plane,
10722 struct drm_plane_state *state)
10723{
10724 struct intel_plane_state *new = to_intel_plane_state(state);
10725 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
10726
10727 /* Update watermarks on tiling or size changes. */
936e71e3 10728 if (new->base.visible != cur->base.visible)
5a21b665
DV
10729 return true;
10730
10731 if (!cur->base.fb || !new->base.fb)
10732 return false;
10733
bae781b2 10734 if (cur->base.fb->modifier != new->base.fb->modifier ||
5a21b665 10735 cur->base.rotation != new->base.rotation ||
936e71e3
VS
10736 drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
10737 drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
10738 drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
10739 drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
5a21b665
DV
10740 return true;
10741
10742 return false;
10743}
10744
10745static bool needs_scaling(struct intel_plane_state *state)
10746{
936e71e3
VS
10747 int src_w = drm_rect_width(&state->base.src) >> 16;
10748 int src_h = drm_rect_height(&state->base.src) >> 16;
10749 int dst_w = drm_rect_width(&state->base.dst);
10750 int dst_h = drm_rect_height(&state->base.dst);
5a21b665
DV
10751
10752 return (src_w != dst_w || src_h != dst_h);
10753}
d21fbe87 10754
da20eabd
ML
10755int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
10756 struct drm_plane_state *plane_state)
10757{
ab1d3a0e 10758 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
da20eabd
ML
10759 struct drm_crtc *crtc = crtc_state->crtc;
10760 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10761 struct drm_plane *plane = plane_state->plane;
10762 struct drm_device *dev = crtc->dev;
ed4a6a7c 10763 struct drm_i915_private *dev_priv = to_i915(dev);
da20eabd
ML
10764 struct intel_plane_state *old_plane_state =
10765 to_intel_plane_state(plane->state);
da20eabd
ML
10766 bool mode_changed = needs_modeset(crtc_state);
10767 bool was_crtc_enabled = crtc->state->active;
10768 bool is_crtc_enabled = crtc_state->active;
da20eabd
ML
10769 bool turn_off, turn_on, visible, was_visible;
10770 struct drm_framebuffer *fb = plane_state->fb;
78108b7c 10771 int ret;
da20eabd 10772
55b8f2a7 10773 if (INTEL_GEN(dev_priv) >= 9 && plane->type != DRM_PLANE_TYPE_CURSOR) {
da20eabd
ML
10774 ret = skl_update_scaler_plane(
10775 to_intel_crtc_state(crtc_state),
10776 to_intel_plane_state(plane_state));
10777 if (ret)
10778 return ret;
10779 }
10780
936e71e3 10781 was_visible = old_plane_state->base.visible;
1d4258db 10782 visible = plane_state->visible;
da20eabd
ML
10783
10784 if (!was_crtc_enabled && WARN_ON(was_visible))
10785 was_visible = false;
10786
35c08f43
ML
10787 /*
10788 * Visibility is calculated as if the crtc was on, but
10789 * after scaler setup everything depends on it being off
10790 * when the crtc isn't active.
f818ffea
VS
10791 *
10792 * FIXME this is wrong for watermarks. Watermarks should also
10793 * be computed as if the pipe would be active. Perhaps move
10794 * per-plane wm computation to the .check_plane() hook, and
10795 * only combine the results from all planes in the current place?
35c08f43
ML
10796 */
10797 if (!is_crtc_enabled)
1d4258db 10798 plane_state->visible = visible = false;
da20eabd
ML
10799
10800 if (!was_visible && !visible)
10801 return 0;
10802
e8861675
ML
10803 if (fb != old_plane_state->base.fb)
10804 pipe_config->fb_changed = true;
10805
da20eabd
ML
10806 turn_off = was_visible && (!visible || mode_changed);
10807 turn_on = visible && (!was_visible || mode_changed);
10808
72660ce0 10809 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
78108b7c
VS
10810 intel_crtc->base.base.id,
10811 intel_crtc->base.name,
72660ce0
VS
10812 plane->base.id, plane->name,
10813 fb ? fb->base.id : -1);
da20eabd 10814
72660ce0
VS
10815 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
10816 plane->base.id, plane->name,
10817 was_visible, visible,
da20eabd
ML
10818 turn_off, turn_on, mode_changed);
10819
caed361d
VS
10820 if (turn_on) {
10821 pipe_config->update_wm_pre = true;
10822
10823 /* must disable cxsr around plane enable/disable */
10824 if (plane->type != DRM_PLANE_TYPE_CURSOR)
10825 pipe_config->disable_cxsr = true;
10826 } else if (turn_off) {
10827 pipe_config->update_wm_post = true;
92826fcd 10828
852eb00d 10829 /* must disable cxsr around plane enable/disable */
e8861675 10830 if (plane->type != DRM_PLANE_TYPE_CURSOR)
ab1d3a0e 10831 pipe_config->disable_cxsr = true;
852eb00d 10832 } else if (intel_wm_need_update(plane, plane_state)) {
caed361d
VS
10833 /* FIXME bollocks */
10834 pipe_config->update_wm_pre = true;
10835 pipe_config->update_wm_post = true;
852eb00d 10836 }
da20eabd 10837
ed4a6a7c 10838 /* Pre-gen9 platforms need two-step watermark updates */
caed361d 10839 if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
6315b5d3 10840 INTEL_GEN(dev_priv) < 9 && dev_priv->display.optimize_watermarks)
ed4a6a7c
MR
10841 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
10842
8be6ca85 10843 if (visible || was_visible)
cd202f69 10844 pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
a9ff8714 10845
31ae71fc
ML
10846 /*
10847 * WaCxSRDisabledForSpriteScaling:ivb
10848 *
10849 * cstate->update_wm was already set above, so this flag will
10850 * take effect when we commit and program watermarks.
10851 */
fd6b8f43 10852 if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev_priv) &&
31ae71fc
ML
10853 needs_scaling(to_intel_plane_state(plane_state)) &&
10854 !needs_scaling(old_plane_state))
10855 pipe_config->disable_lp_wm = true;
d21fbe87 10856
da20eabd
ML
10857 return 0;
10858}
10859
6d3a1ce7
ML
10860static bool encoders_cloneable(const struct intel_encoder *a,
10861 const struct intel_encoder *b)
10862{
10863 /* masks could be asymmetric, so check both ways */
10864 return a == b || (a->cloneable & (1 << b->type) &&
10865 b->cloneable & (1 << a->type));
10866}
10867
10868static bool check_single_encoder_cloning(struct drm_atomic_state *state,
10869 struct intel_crtc *crtc,
10870 struct intel_encoder *encoder)
10871{
10872 struct intel_encoder *source_encoder;
10873 struct drm_connector *connector;
10874 struct drm_connector_state *connector_state;
10875 int i;
10876
10877 for_each_connector_in_state(state, connector, connector_state, i) {
10878 if (connector_state->crtc != &crtc->base)
10879 continue;
10880
10881 source_encoder =
10882 to_intel_encoder(connector_state->best_encoder);
10883 if (!encoders_cloneable(encoder, source_encoder))
10884 return false;
10885 }
10886
10887 return true;
10888}
10889
6d3a1ce7
ML
10890static int intel_crtc_atomic_check(struct drm_crtc *crtc,
10891 struct drm_crtc_state *crtc_state)
10892{
cf5a15be 10893 struct drm_device *dev = crtc->dev;
fac5e23e 10894 struct drm_i915_private *dev_priv = to_i915(dev);
6d3a1ce7 10895 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cf5a15be
ML
10896 struct intel_crtc_state *pipe_config =
10897 to_intel_crtc_state(crtc_state);
6d3a1ce7 10898 struct drm_atomic_state *state = crtc_state->state;
4d20cd86 10899 int ret;
6d3a1ce7
ML
10900 bool mode_changed = needs_modeset(crtc_state);
10901
852eb00d 10902 if (mode_changed && !crtc_state->active)
caed361d 10903 pipe_config->update_wm_post = true;
eddfcbcd 10904
ad421372
ML
10905 if (mode_changed && crtc_state->enable &&
10906 dev_priv->display.crtc_compute_clock &&
8106ddbd 10907 !WARN_ON(pipe_config->shared_dpll)) {
ad421372
ML
10908 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
10909 pipe_config);
10910 if (ret)
10911 return ret;
10912 }
10913
82cf435b
LL
10914 if (crtc_state->color_mgmt_changed) {
10915 ret = intel_color_check(crtc, crtc_state);
10916 if (ret)
10917 return ret;
e7852a4b
LL
10918
10919 /*
10920 * Changing color management on Intel hardware is
10921 * handled as part of planes update.
10922 */
10923 crtc_state->planes_changed = true;
82cf435b
LL
10924 }
10925
e435d6e5 10926 ret = 0;
86c8bbbe 10927 if (dev_priv->display.compute_pipe_wm) {
e3bddded 10928 ret = dev_priv->display.compute_pipe_wm(pipe_config);
ed4a6a7c
MR
10929 if (ret) {
10930 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
10931 return ret;
10932 }
10933 }
10934
10935 if (dev_priv->display.compute_intermediate_wm &&
10936 !to_intel_atomic_state(state)->skip_intermediate_wm) {
10937 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
10938 return 0;
10939
10940 /*
10941 * Calculate 'intermediate' watermarks that satisfy both the
10942 * old state and the new state. We can program these
10943 * immediately.
10944 */
6315b5d3 10945 ret = dev_priv->display.compute_intermediate_wm(dev,
ed4a6a7c
MR
10946 intel_crtc,
10947 pipe_config);
10948 if (ret) {
10949 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
86c8bbbe 10950 return ret;
ed4a6a7c 10951 }
e3d5457c
VS
10952 } else if (dev_priv->display.compute_intermediate_wm) {
10953 if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
10954 pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
86c8bbbe
MR
10955 }
10956
6315b5d3 10957 if (INTEL_GEN(dev_priv) >= 9) {
e435d6e5
ML
10958 if (mode_changed)
10959 ret = skl_update_scaler_crtc(pipe_config);
10960
10961 if (!ret)
6ebc6923 10962 ret = intel_atomic_setup_scalers(dev_priv, intel_crtc,
e435d6e5
ML
10963 pipe_config);
10964 }
10965
10966 return ret;
6d3a1ce7
ML
10967}
10968
65b38e0d 10969static const struct drm_crtc_helper_funcs intel_helper_funcs = {
f6e5b160 10970 .mode_set_base_atomic = intel_pipe_set_base_atomic,
5a21b665
DV
10971 .atomic_begin = intel_begin_crtc_commit,
10972 .atomic_flush = intel_finish_crtc_commit,
6d3a1ce7 10973 .atomic_check = intel_crtc_atomic_check,
f6e5b160
CW
10974};
10975
d29b2f9d
ACO
10976static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
10977{
10978 struct intel_connector *connector;
10979
10980 for_each_intel_connector(dev, connector) {
8863dc7f
DV
10981 if (connector->base.state->crtc)
10982 drm_connector_unreference(&connector->base);
10983
d29b2f9d
ACO
10984 if (connector->base.encoder) {
10985 connector->base.state->best_encoder =
10986 connector->base.encoder;
10987 connector->base.state->crtc =
10988 connector->base.encoder->crtc;
8863dc7f
DV
10989
10990 drm_connector_reference(&connector->base);
d29b2f9d
ACO
10991 } else {
10992 connector->base.state->best_encoder = NULL;
10993 connector->base.state->crtc = NULL;
10994 }
10995 }
10996}
10997
050f7aeb 10998static void
eba905b2 10999connected_sink_compute_bpp(struct intel_connector *connector,
5cec258b 11000 struct intel_crtc_state *pipe_config)
050f7aeb 11001{
6a2a5c5d 11002 const struct drm_display_info *info = &connector->base.display_info;
050f7aeb
DV
11003 int bpp = pipe_config->pipe_bpp;
11004
11005 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
6a2a5c5d
VS
11006 connector->base.base.id,
11007 connector->base.name);
050f7aeb
DV
11008
11009 /* Don't use an invalid EDID bpc value */
6a2a5c5d 11010 if (info->bpc != 0 && info->bpc * 3 < bpp) {
050f7aeb 11011 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
6a2a5c5d
VS
11012 bpp, info->bpc * 3);
11013 pipe_config->pipe_bpp = info->bpc * 3;
050f7aeb
DV
11014 }
11015
196f954e 11016 /* Clamp bpp to 8 on screens without EDID 1.4 */
6a2a5c5d 11017 if (info->bpc == 0 && bpp > 24) {
196f954e
MK
11018 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
11019 bpp);
11020 pipe_config->pipe_bpp = 24;
050f7aeb
DV
11021 }
11022}
11023
4e53c2e0 11024static int
050f7aeb 11025compute_baseline_pipe_bpp(struct intel_crtc *crtc,
5cec258b 11026 struct intel_crtc_state *pipe_config)
4e53c2e0 11027{
9beb5fea 11028 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1486017f 11029 struct drm_atomic_state *state;
da3ced29
ACO
11030 struct drm_connector *connector;
11031 struct drm_connector_state *connector_state;
1486017f 11032 int bpp, i;
4e53c2e0 11033
9beb5fea
TU
11034 if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
11035 IS_CHERRYVIEW(dev_priv)))
4e53c2e0 11036 bpp = 10*3;
9beb5fea 11037 else if (INTEL_GEN(dev_priv) >= 5)
d328c9d7
DV
11038 bpp = 12*3;
11039 else
11040 bpp = 8*3;
11041
4e53c2e0 11042
4e53c2e0
DV
11043 pipe_config->pipe_bpp = bpp;
11044
1486017f
ACO
11045 state = pipe_config->base.state;
11046
4e53c2e0 11047 /* Clamp display bpp to EDID value */
da3ced29
ACO
11048 for_each_connector_in_state(state, connector, connector_state, i) {
11049 if (connector_state->crtc != &crtc->base)
4e53c2e0
DV
11050 continue;
11051
da3ced29
ACO
11052 connected_sink_compute_bpp(to_intel_connector(connector),
11053 pipe_config);
4e53c2e0
DV
11054 }
11055
11056 return bpp;
11057}
11058
644db711
DV
11059static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11060{
11061 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11062 "type: 0x%x flags: 0x%x\n",
1342830c 11063 mode->crtc_clock,
644db711
DV
11064 mode->crtc_hdisplay, mode->crtc_hsync_start,
11065 mode->crtc_hsync_end, mode->crtc_htotal,
11066 mode->crtc_vdisplay, mode->crtc_vsync_start,
11067 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
11068}
11069
f6982332
TU
11070static inline void
11071intel_dump_m_n_config(struct intel_crtc_state *pipe_config, char *id,
a4309657 11072 unsigned int lane_count, struct intel_link_m_n *m_n)
f6982332 11073{
a4309657
TU
11074 DRM_DEBUG_KMS("%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11075 id, lane_count,
f6982332
TU
11076 m_n->gmch_m, m_n->gmch_n,
11077 m_n->link_m, m_n->link_n, m_n->tu);
11078}
11079
c0b03411 11080static void intel_dump_pipe_config(struct intel_crtc *crtc,
5cec258b 11081 struct intel_crtc_state *pipe_config,
c0b03411
DV
11082 const char *context)
11083{
6a60cd87 11084 struct drm_device *dev = crtc->base.dev;
4f8036a2 11085 struct drm_i915_private *dev_priv = to_i915(dev);
6a60cd87
CK
11086 struct drm_plane *plane;
11087 struct intel_plane *intel_plane;
11088 struct intel_plane_state *state;
11089 struct drm_framebuffer *fb;
11090
66766e4f
TU
11091 DRM_DEBUG_KMS("[CRTC:%d:%s]%s\n",
11092 crtc->base.base.id, crtc->base.name, context);
c0b03411 11093
2c89429e
TU
11094 DRM_DEBUG_KMS("cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
11095 transcoder_name(pipe_config->cpu_transcoder),
c0b03411 11096 pipe_config->pipe_bpp, pipe_config->dither);
a4309657
TU
11097
11098 if (pipe_config->has_pch_encoder)
11099 intel_dump_m_n_config(pipe_config, "fdi",
11100 pipe_config->fdi_lanes,
11101 &pipe_config->fdi_m_n);
f6982332
TU
11102
11103 if (intel_crtc_has_dp_encoder(pipe_config)) {
a4309657
TU
11104 intel_dump_m_n_config(pipe_config, "dp m_n",
11105 pipe_config->lane_count, &pipe_config->dp_m_n);
d806e682
TU
11106 if (pipe_config->has_drrs)
11107 intel_dump_m_n_config(pipe_config, "dp m2_n2",
11108 pipe_config->lane_count,
11109 &pipe_config->dp_m2_n2);
f6982332 11110 }
b95af8be 11111
55072d19 11112 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
2c89429e 11113 pipe_config->has_audio, pipe_config->has_infoframe);
55072d19 11114
c0b03411 11115 DRM_DEBUG_KMS("requested mode:\n");
2d112de7 11116 drm_mode_debug_printmodeline(&pipe_config->base.mode);
c0b03411 11117 DRM_DEBUG_KMS("adjusted mode:\n");
2d112de7
ACO
11118 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
11119 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
a7d1b3f4 11120 DRM_DEBUG_KMS("port clock: %d, pipe src size: %dx%d, pixel rate %d\n",
2c89429e 11121 pipe_config->port_clock,
a7d1b3f4
VS
11122 pipe_config->pipe_src_w, pipe_config->pipe_src_h,
11123 pipe_config->pixel_rate);
dd2f616d
TU
11124
11125 if (INTEL_GEN(dev_priv) >= 9)
11126 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
11127 crtc->num_scalers,
11128 pipe_config->scaler_state.scaler_users,
11129 pipe_config->scaler_state.scaler_id);
a74f8375
TU
11130
11131 if (HAS_GMCH_DISPLAY(dev_priv))
11132 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
11133 pipe_config->gmch_pfit.control,
11134 pipe_config->gmch_pfit.pgm_ratios,
11135 pipe_config->gmch_pfit.lvds_border_bits);
11136 else
11137 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
11138 pipe_config->pch_pfit.pos,
11139 pipe_config->pch_pfit.size,
08c4d7fc 11140 enableddisabled(pipe_config->pch_pfit.enabled));
a74f8375 11141
2c89429e
TU
11142 DRM_DEBUG_KMS("ips: %i, double wide: %i\n",
11143 pipe_config->ips_enabled, pipe_config->double_wide);
6a60cd87 11144
f50b79f0 11145 intel_dpll_dump_hw_state(dev_priv, &pipe_config->dpll_hw_state);
415ff0f6 11146
6a60cd87
CK
11147 DRM_DEBUG_KMS("planes on this crtc\n");
11148 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
b3c11ac2 11149 struct drm_format_name_buf format_name;
6a60cd87
CK
11150 intel_plane = to_intel_plane(plane);
11151 if (intel_plane->pipe != crtc->pipe)
11152 continue;
11153
11154 state = to_intel_plane_state(plane->state);
11155 fb = state->base.fb;
11156 if (!fb) {
1d577e02
VS
11157 DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
11158 plane->base.id, plane->name, state->scaler_id);
6a60cd87
CK
11159 continue;
11160 }
11161
dd2f616d
TU
11162 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d, fb = %ux%u format = %s\n",
11163 plane->base.id, plane->name,
b3c11ac2 11164 fb->base.id, fb->width, fb->height,
438b74a5 11165 drm_get_format_name(fb->format->format, &format_name));
dd2f616d
TU
11166 if (INTEL_GEN(dev_priv) >= 9)
11167 DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
11168 state->scaler_id,
11169 state->base.src.x1 >> 16,
11170 state->base.src.y1 >> 16,
11171 drm_rect_width(&state->base.src) >> 16,
11172 drm_rect_height(&state->base.src) >> 16,
11173 state->base.dst.x1, state->base.dst.y1,
11174 drm_rect_width(&state->base.dst),
11175 drm_rect_height(&state->base.dst));
6a60cd87 11176 }
c0b03411
DV
11177}
11178
5448a00d 11179static bool check_digital_port_conflicts(struct drm_atomic_state *state)
00f0b378 11180{
5448a00d 11181 struct drm_device *dev = state->dev;
da3ced29 11182 struct drm_connector *connector;
00f0b378 11183 unsigned int used_ports = 0;
477321e0 11184 unsigned int used_mst_ports = 0;
00f0b378
VS
11185
11186 /*
11187 * Walk the connector list instead of the encoder
11188 * list to detect the problem on ddi platforms
11189 * where there's just one encoder per digital port.
11190 */
0bff4858
VS
11191 drm_for_each_connector(connector, dev) {
11192 struct drm_connector_state *connector_state;
11193 struct intel_encoder *encoder;
11194
11195 connector_state = drm_atomic_get_existing_connector_state(state, connector);
11196 if (!connector_state)
11197 connector_state = connector->state;
11198
5448a00d 11199 if (!connector_state->best_encoder)
00f0b378
VS
11200 continue;
11201
5448a00d
ACO
11202 encoder = to_intel_encoder(connector_state->best_encoder);
11203
11204 WARN_ON(!connector_state->crtc);
00f0b378
VS
11205
11206 switch (encoder->type) {
11207 unsigned int port_mask;
11208 case INTEL_OUTPUT_UNKNOWN:
4f8036a2 11209 if (WARN_ON(!HAS_DDI(to_i915(dev))))
00f0b378 11210 break;
cca0502b 11211 case INTEL_OUTPUT_DP:
00f0b378
VS
11212 case INTEL_OUTPUT_HDMI:
11213 case INTEL_OUTPUT_EDP:
11214 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
11215
11216 /* the same port mustn't appear more than once */
11217 if (used_ports & port_mask)
11218 return false;
11219
11220 used_ports |= port_mask;
477321e0
VS
11221 break;
11222 case INTEL_OUTPUT_DP_MST:
11223 used_mst_ports |=
11224 1 << enc_to_mst(&encoder->base)->primary->port;
11225 break;
00f0b378
VS
11226 default:
11227 break;
11228 }
11229 }
11230
477321e0
VS
11231 /* can't mix MST and SST/HDMI on the same port */
11232 if (used_ports & used_mst_ports)
11233 return false;
11234
00f0b378
VS
11235 return true;
11236}
11237
83a57153
ACO
11238static void
11239clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
11240{
11241 struct drm_crtc_state tmp_state;
663a3640 11242 struct intel_crtc_scaler_state scaler_state;
4978cc93 11243 struct intel_dpll_hw_state dpll_hw_state;
8106ddbd 11244 struct intel_shared_dpll *shared_dpll;
c4e2d043 11245 bool force_thru;
83a57153 11246
7546a384
ACO
11247 /* FIXME: before the switch to atomic started, a new pipe_config was
11248 * kzalloc'd. Code that depends on any field being zero should be
11249 * fixed, so that the crtc_state can be safely duplicated. For now,
11250 * only fields that are know to not cause problems are preserved. */
11251
83a57153 11252 tmp_state = crtc_state->base;
663a3640 11253 scaler_state = crtc_state->scaler_state;
4978cc93
ACO
11254 shared_dpll = crtc_state->shared_dpll;
11255 dpll_hw_state = crtc_state->dpll_hw_state;
c4e2d043 11256 force_thru = crtc_state->pch_pfit.force_thru;
4978cc93 11257
83a57153 11258 memset(crtc_state, 0, sizeof *crtc_state);
4978cc93 11259
83a57153 11260 crtc_state->base = tmp_state;
663a3640 11261 crtc_state->scaler_state = scaler_state;
4978cc93
ACO
11262 crtc_state->shared_dpll = shared_dpll;
11263 crtc_state->dpll_hw_state = dpll_hw_state;
c4e2d043 11264 crtc_state->pch_pfit.force_thru = force_thru;
83a57153
ACO
11265}
11266
548ee15b 11267static int
b8cecdf5 11268intel_modeset_pipe_config(struct drm_crtc *crtc,
b359283a 11269 struct intel_crtc_state *pipe_config)
ee7b9f93 11270{
b359283a 11271 struct drm_atomic_state *state = pipe_config->base.state;
7758a113 11272 struct intel_encoder *encoder;
da3ced29 11273 struct drm_connector *connector;
0b901879 11274 struct drm_connector_state *connector_state;
d328c9d7 11275 int base_bpp, ret = -EINVAL;
0b901879 11276 int i;
e29c22c0 11277 bool retry = true;
ee7b9f93 11278
83a57153 11279 clear_intel_crtc_state(pipe_config);
7758a113 11280
e143a21c
DV
11281 pipe_config->cpu_transcoder =
11282 (enum transcoder) to_intel_crtc(crtc)->pipe;
b8cecdf5 11283
2960bc9c
ID
11284 /*
11285 * Sanitize sync polarity flags based on requested ones. If neither
11286 * positive or negative polarity is requested, treat this as meaning
11287 * negative polarity.
11288 */
2d112de7 11289 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 11290 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
2d112de7 11291 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
2960bc9c 11292
2d112de7 11293 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 11294 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
2d112de7 11295 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
2960bc9c 11296
d328c9d7
DV
11297 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
11298 pipe_config);
11299 if (base_bpp < 0)
4e53c2e0
DV
11300 goto fail;
11301
e41a56be
VS
11302 /*
11303 * Determine the real pipe dimensions. Note that stereo modes can
11304 * increase the actual pipe size due to the frame doubling and
11305 * insertion of additional space for blanks between the frame. This
11306 * is stored in the crtc timings. We use the requested mode to do this
11307 * computation to clearly distinguish it from the adjusted mode, which
11308 * can be changed by the connectors in the below retry loop.
11309 */
196cd5d3 11310 drm_mode_get_hv_timing(&pipe_config->base.mode,
ecb7e16b
GP
11311 &pipe_config->pipe_src_w,
11312 &pipe_config->pipe_src_h);
e41a56be 11313
253c84c8
VS
11314 for_each_connector_in_state(state, connector, connector_state, i) {
11315 if (connector_state->crtc != crtc)
11316 continue;
11317
11318 encoder = to_intel_encoder(connector_state->best_encoder);
11319
e25148d0
VS
11320 if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
11321 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11322 goto fail;
11323 }
11324
253c84c8
VS
11325 /*
11326 * Determine output_types before calling the .compute_config()
11327 * hooks so that the hooks can use this information safely.
11328 */
11329 pipe_config->output_types |= 1 << encoder->type;
11330 }
11331
e29c22c0 11332encoder_retry:
ef1b460d 11333 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 11334 pipe_config->port_clock = 0;
ef1b460d 11335 pipe_config->pixel_multiplier = 1;
ff9a6750 11336
135c81b8 11337 /* Fill in default crtc timings, allow encoders to overwrite them. */
2d112de7
ACO
11338 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
11339 CRTC_STEREO_DOUBLE);
135c81b8 11340
7758a113
DV
11341 /* Pass our mode to the connectors and the CRTC to give them a chance to
11342 * adjust it according to limitations or connector properties, and also
11343 * a chance to reject the mode entirely.
47f1c6c9 11344 */
da3ced29 11345 for_each_connector_in_state(state, connector, connector_state, i) {
0b901879 11346 if (connector_state->crtc != crtc)
7758a113 11347 continue;
7ae89233 11348
0b901879
ACO
11349 encoder = to_intel_encoder(connector_state->best_encoder);
11350
0a478c27 11351 if (!(encoder->compute_config(encoder, pipe_config, connector_state))) {
efea6e8e 11352 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
DV
11353 goto fail;
11354 }
ee7b9f93 11355 }
47f1c6c9 11356
ff9a6750
DV
11357 /* Set default port clock if not overwritten by the encoder. Needs to be
11358 * done afterwards in case the encoder adjusts the mode. */
11359 if (!pipe_config->port_clock)
2d112de7 11360 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
241bfc38 11361 * pipe_config->pixel_multiplier;
ff9a6750 11362
a43f6e0f 11363 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 11364 if (ret < 0) {
7758a113
DV
11365 DRM_DEBUG_KMS("CRTC fixup failed\n");
11366 goto fail;
ee7b9f93 11367 }
e29c22c0
DV
11368
11369 if (ret == RETRY) {
11370 if (WARN(!retry, "loop in pipe configuration computation\n")) {
11371 ret = -EINVAL;
11372 goto fail;
11373 }
11374
11375 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
11376 retry = false;
11377 goto encoder_retry;
11378 }
11379
e8fa4270 11380 /* Dithering seems to not pass-through bits correctly when it should, so
611032bf
MN
11381 * only enable it on 6bpc panels and when its not a compliance
11382 * test requesting 6bpc video pattern.
11383 */
11384 pipe_config->dither = (pipe_config->pipe_bpp == 6*3) &&
11385 !pipe_config->dither_force_disable;
62f0ace5 11386 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
d328c9d7 11387 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
4e53c2e0 11388
7758a113 11389fail:
548ee15b 11390 return ret;
ee7b9f93 11391}
47f1c6c9 11392
ea9d758d 11393static void
4740b0f2 11394intel_modeset_update_crtc_state(struct drm_atomic_state *state)
ea9d758d 11395{
0a9ab303
ACO
11396 struct drm_crtc *crtc;
11397 struct drm_crtc_state *crtc_state;
8a75d157 11398 int i;
ea9d758d 11399
7668851f 11400 /* Double check state. */
8a75d157 11401 for_each_crtc_in_state(state, crtc, crtc_state, i) {
3cb480bc 11402 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
fc467a22
ML
11403
11404 /* Update hwmode for vblank functions */
11405 if (crtc->state->active)
11406 crtc->hwmode = crtc->state->adjusted_mode;
11407 else
11408 crtc->hwmode.crtc_clock = 0;
61067a5e
ML
11409
11410 /*
11411 * Update legacy state to satisfy fbc code. This can
11412 * be removed when fbc uses the atomic state.
11413 */
11414 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
11415 struct drm_plane_state *plane_state = crtc->primary->state;
11416
11417 crtc->primary->fb = plane_state->fb;
11418 crtc->x = plane_state->src_x >> 16;
11419 crtc->y = plane_state->src_y >> 16;
11420 }
ea9d758d 11421 }
ea9d758d
DV
11422}
11423
3bd26263 11424static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 11425{
3bd26263 11426 int diff;
f1f644dc
JB
11427
11428 if (clock1 == clock2)
11429 return true;
11430
11431 if (!clock1 || !clock2)
11432 return false;
11433
11434 diff = abs(clock1 - clock2);
11435
11436 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
11437 return true;
11438
11439 return false;
11440}
11441
cfb23ed6
ML
11442static bool
11443intel_compare_m_n(unsigned int m, unsigned int n,
11444 unsigned int m2, unsigned int n2,
11445 bool exact)
11446{
11447 if (m == m2 && n == n2)
11448 return true;
11449
11450 if (exact || !m || !n || !m2 || !n2)
11451 return false;
11452
11453 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
11454
31d10b57
ML
11455 if (n > n2) {
11456 while (n > n2) {
cfb23ed6
ML
11457 m2 <<= 1;
11458 n2 <<= 1;
11459 }
31d10b57
ML
11460 } else if (n < n2) {
11461 while (n < n2) {
cfb23ed6
ML
11462 m <<= 1;
11463 n <<= 1;
11464 }
11465 }
11466
31d10b57
ML
11467 if (n != n2)
11468 return false;
11469
11470 return intel_fuzzy_clock_check(m, m2);
cfb23ed6
ML
11471}
11472
11473static bool
11474intel_compare_link_m_n(const struct intel_link_m_n *m_n,
11475 struct intel_link_m_n *m2_n2,
11476 bool adjust)
11477{
11478 if (m_n->tu == m2_n2->tu &&
11479 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
11480 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
11481 intel_compare_m_n(m_n->link_m, m_n->link_n,
11482 m2_n2->link_m, m2_n2->link_n, !adjust)) {
11483 if (adjust)
11484 *m2_n2 = *m_n;
11485
11486 return true;
11487 }
11488
11489 return false;
11490}
11491
4e8048f8
TU
11492static void __printf(3, 4)
11493pipe_config_err(bool adjust, const char *name, const char *format, ...)
11494{
11495 char *level;
11496 unsigned int category;
11497 struct va_format vaf;
11498 va_list args;
11499
11500 if (adjust) {
11501 level = KERN_DEBUG;
11502 category = DRM_UT_KMS;
11503 } else {
11504 level = KERN_ERR;
11505 category = DRM_UT_NONE;
11506 }
11507
11508 va_start(args, format);
11509 vaf.fmt = format;
11510 vaf.va = &args;
11511
11512 drm_printk(level, category, "mismatch in %s %pV", name, &vaf);
11513
11514 va_end(args);
11515}
11516
0e8ffe1b 11517static bool
6315b5d3 11518intel_pipe_config_compare(struct drm_i915_private *dev_priv,
5cec258b 11519 struct intel_crtc_state *current_config,
cfb23ed6
ML
11520 struct intel_crtc_state *pipe_config,
11521 bool adjust)
0e8ffe1b 11522{
cfb23ed6
ML
11523 bool ret = true;
11524
66e985c0
DV
11525#define PIPE_CONF_CHECK_X(name) \
11526 if (current_config->name != pipe_config->name) { \
4e8048f8 11527 pipe_config_err(adjust, __stringify(name), \
66e985c0
DV
11528 "(expected 0x%08x, found 0x%08x)\n", \
11529 current_config->name, \
11530 pipe_config->name); \
cfb23ed6 11531 ret = false; \
66e985c0
DV
11532 }
11533
08a24034
DV
11534#define PIPE_CONF_CHECK_I(name) \
11535 if (current_config->name != pipe_config->name) { \
4e8048f8 11536 pipe_config_err(adjust, __stringify(name), \
08a24034
DV
11537 "(expected %i, found %i)\n", \
11538 current_config->name, \
11539 pipe_config->name); \
cfb23ed6
ML
11540 ret = false; \
11541 }
11542
8106ddbd
ACO
11543#define PIPE_CONF_CHECK_P(name) \
11544 if (current_config->name != pipe_config->name) { \
4e8048f8 11545 pipe_config_err(adjust, __stringify(name), \
8106ddbd
ACO
11546 "(expected %p, found %p)\n", \
11547 current_config->name, \
11548 pipe_config->name); \
11549 ret = false; \
11550 }
11551
cfb23ed6
ML
11552#define PIPE_CONF_CHECK_M_N(name) \
11553 if (!intel_compare_link_m_n(&current_config->name, \
11554 &pipe_config->name,\
11555 adjust)) { \
4e8048f8 11556 pipe_config_err(adjust, __stringify(name), \
cfb23ed6
ML
11557 "(expected tu %i gmch %i/%i link %i/%i, " \
11558 "found tu %i, gmch %i/%i link %i/%i)\n", \
11559 current_config->name.tu, \
11560 current_config->name.gmch_m, \
11561 current_config->name.gmch_n, \
11562 current_config->name.link_m, \
11563 current_config->name.link_n, \
11564 pipe_config->name.tu, \
11565 pipe_config->name.gmch_m, \
11566 pipe_config->name.gmch_n, \
11567 pipe_config->name.link_m, \
11568 pipe_config->name.link_n); \
11569 ret = false; \
11570 }
11571
55c561a7
DV
11572/* This is required for BDW+ where there is only one set of registers for
11573 * switching between high and low RR.
11574 * This macro can be used whenever a comparison has to be made between one
11575 * hw state and multiple sw state variables.
11576 */
cfb23ed6
ML
11577#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
11578 if (!intel_compare_link_m_n(&current_config->name, \
11579 &pipe_config->name, adjust) && \
11580 !intel_compare_link_m_n(&current_config->alt_name, \
11581 &pipe_config->name, adjust)) { \
4e8048f8 11582 pipe_config_err(adjust, __stringify(name), \
cfb23ed6
ML
11583 "(expected tu %i gmch %i/%i link %i/%i, " \
11584 "or tu %i gmch %i/%i link %i/%i, " \
11585 "found tu %i, gmch %i/%i link %i/%i)\n", \
11586 current_config->name.tu, \
11587 current_config->name.gmch_m, \
11588 current_config->name.gmch_n, \
11589 current_config->name.link_m, \
11590 current_config->name.link_n, \
11591 current_config->alt_name.tu, \
11592 current_config->alt_name.gmch_m, \
11593 current_config->alt_name.gmch_n, \
11594 current_config->alt_name.link_m, \
11595 current_config->alt_name.link_n, \
11596 pipe_config->name.tu, \
11597 pipe_config->name.gmch_m, \
11598 pipe_config->name.gmch_n, \
11599 pipe_config->name.link_m, \
11600 pipe_config->name.link_n); \
11601 ret = false; \
88adfff1
DV
11602 }
11603
1bd1bd80
DV
11604#define PIPE_CONF_CHECK_FLAGS(name, mask) \
11605 if ((current_config->name ^ pipe_config->name) & (mask)) { \
4e8048f8
TU
11606 pipe_config_err(adjust, __stringify(name), \
11607 "(%x) (expected %i, found %i)\n", \
11608 (mask), \
1bd1bd80
DV
11609 current_config->name & (mask), \
11610 pipe_config->name & (mask)); \
cfb23ed6 11611 ret = false; \
1bd1bd80
DV
11612 }
11613
5e550656
VS
11614#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
11615 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
4e8048f8 11616 pipe_config_err(adjust, __stringify(name), \
5e550656
VS
11617 "(expected %i, found %i)\n", \
11618 current_config->name, \
11619 pipe_config->name); \
cfb23ed6 11620 ret = false; \
5e550656
VS
11621 }
11622
bb760063
DV
11623#define PIPE_CONF_QUIRK(quirk) \
11624 ((current_config->quirks | pipe_config->quirks) & (quirk))
11625
eccb140b
DV
11626 PIPE_CONF_CHECK_I(cpu_transcoder);
11627
08a24034
DV
11628 PIPE_CONF_CHECK_I(has_pch_encoder);
11629 PIPE_CONF_CHECK_I(fdi_lanes);
cfb23ed6 11630 PIPE_CONF_CHECK_M_N(fdi_m_n);
08a24034 11631
90a6b7b0 11632 PIPE_CONF_CHECK_I(lane_count);
95a7a2ae 11633 PIPE_CONF_CHECK_X(lane_lat_optim_mask);
b95af8be 11634
6315b5d3 11635 if (INTEL_GEN(dev_priv) < 8) {
cfb23ed6
ML
11636 PIPE_CONF_CHECK_M_N(dp_m_n);
11637
cfb23ed6
ML
11638 if (current_config->has_drrs)
11639 PIPE_CONF_CHECK_M_N(dp_m2_n2);
11640 } else
11641 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
eb14cb74 11642
253c84c8 11643 PIPE_CONF_CHECK_X(output_types);
a65347ba 11644
2d112de7
ACO
11645 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
11646 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
11647 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
11648 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
11649 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
11650 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
1bd1bd80 11651
2d112de7
ACO
11652 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
11653 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
11654 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
11655 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
11656 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
11657 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
1bd1bd80 11658
c93f54cf 11659 PIPE_CONF_CHECK_I(pixel_multiplier);
6897b4b5 11660 PIPE_CONF_CHECK_I(has_hdmi_sink);
772c2a51 11661 if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
920a14b2 11662 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
b5a9fa09 11663 PIPE_CONF_CHECK_I(limited_color_range);
e43823ec 11664 PIPE_CONF_CHECK_I(has_infoframe);
6c49f241 11665
9ed109a7
DV
11666 PIPE_CONF_CHECK_I(has_audio);
11667
2d112de7 11668 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
1bd1bd80
DV
11669 DRM_MODE_FLAG_INTERLACE);
11670
bb760063 11671 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
2d112de7 11672 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 11673 DRM_MODE_FLAG_PHSYNC);
2d112de7 11674 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 11675 DRM_MODE_FLAG_NHSYNC);
2d112de7 11676 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 11677 DRM_MODE_FLAG_PVSYNC);
2d112de7 11678 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063
DV
11679 DRM_MODE_FLAG_NVSYNC);
11680 }
045ac3b5 11681
333b8ca8 11682 PIPE_CONF_CHECK_X(gmch_pfit.control);
e2ff2d4a 11683 /* pfit ratios are autocomputed by the hw on gen4+ */
6315b5d3 11684 if (INTEL_GEN(dev_priv) < 4)
7f7d8dd6 11685 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
333b8ca8 11686 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
9953599b 11687
bfd16b2a
ML
11688 if (!adjust) {
11689 PIPE_CONF_CHECK_I(pipe_src_w);
11690 PIPE_CONF_CHECK_I(pipe_src_h);
11691
11692 PIPE_CONF_CHECK_I(pch_pfit.enabled);
11693 if (current_config->pch_pfit.enabled) {
11694 PIPE_CONF_CHECK_X(pch_pfit.pos);
11695 PIPE_CONF_CHECK_X(pch_pfit.size);
11696 }
2fa2fe9a 11697
7aefe2b5 11698 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
a7d1b3f4 11699 PIPE_CONF_CHECK_CLOCK_FUZZY(pixel_rate);
7aefe2b5 11700 }
a1b2278e 11701
e59150dc 11702 /* BDW+ don't expose a synchronous way to read the state */
772c2a51 11703 if (IS_HASWELL(dev_priv))
e59150dc 11704 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 11705
282740f7
VS
11706 PIPE_CONF_CHECK_I(double_wide);
11707
8106ddbd 11708 PIPE_CONF_CHECK_P(shared_dpll);
66e985c0 11709 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 11710 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
DV
11711 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
11712 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
d452c5b6 11713 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
00490c22 11714 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
3f4cd19f
DL
11715 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
11716 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
11717 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
c0d43d62 11718
47eacbab
VS
11719 PIPE_CONF_CHECK_X(dsi_pll.ctrl);
11720 PIPE_CONF_CHECK_X(dsi_pll.div);
11721
9beb5fea 11722 if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
42571aef
VS
11723 PIPE_CONF_CHECK_I(pipe_bpp);
11724
2d112de7 11725 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
a9a7e98a 11726 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 11727
66e985c0 11728#undef PIPE_CONF_CHECK_X
08a24034 11729#undef PIPE_CONF_CHECK_I
8106ddbd 11730#undef PIPE_CONF_CHECK_P
1bd1bd80 11731#undef PIPE_CONF_CHECK_FLAGS
5e550656 11732#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 11733#undef PIPE_CONF_QUIRK
88adfff1 11734
cfb23ed6 11735 return ret;
0e8ffe1b
DV
11736}
11737
e3b247da
VS
11738static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
11739 const struct intel_crtc_state *pipe_config)
11740{
11741 if (pipe_config->has_pch_encoder) {
21a727b3 11742 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
e3b247da
VS
11743 &pipe_config->fdi_m_n);
11744 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
11745
11746 /*
11747 * FDI already provided one idea for the dotclock.
11748 * Yell if the encoder disagrees.
11749 */
11750 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
11751 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
11752 fdi_dotclock, dotclock);
11753 }
11754}
11755
c0ead703
ML
11756static void verify_wm_state(struct drm_crtc *crtc,
11757 struct drm_crtc_state *new_state)
08db6652 11758{
6315b5d3 11759 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
08db6652 11760 struct skl_ddb_allocation hw_ddb, *sw_ddb;
3de8a14c 11761 struct skl_pipe_wm hw_wm, *sw_wm;
11762 struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
11763 struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
e7c84544
ML
11764 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11765 const enum pipe pipe = intel_crtc->pipe;
3de8a14c 11766 int plane, level, max_level = ilk_wm_max_level(dev_priv);
08db6652 11767
6315b5d3 11768 if (INTEL_GEN(dev_priv) < 9 || !new_state->active)
08db6652
DL
11769 return;
11770
3de8a14c 11771 skl_pipe_wm_get_hw_state(crtc, &hw_wm);
03af79e0 11772 sw_wm = &to_intel_crtc_state(new_state)->wm.skl.optimal;
3de8a14c 11773
08db6652
DL
11774 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
11775 sw_ddb = &dev_priv->wm.skl_hw.ddb;
11776
e7c84544 11777 /* planes */
8b364b41 11778 for_each_universal_plane(dev_priv, pipe, plane) {
3de8a14c 11779 hw_plane_wm = &hw_wm.planes[plane];
11780 sw_plane_wm = &sw_wm->planes[plane];
08db6652 11781
3de8a14c 11782 /* Watermarks */
11783 for (level = 0; level <= max_level; level++) {
11784 if (skl_wm_level_equals(&hw_plane_wm->wm[level],
11785 &sw_plane_wm->wm[level]))
11786 continue;
11787
11788 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",
11789 pipe_name(pipe), plane + 1, level,
11790 sw_plane_wm->wm[level].plane_en,
11791 sw_plane_wm->wm[level].plane_res_b,
11792 sw_plane_wm->wm[level].plane_res_l,
11793 hw_plane_wm->wm[level].plane_en,
11794 hw_plane_wm->wm[level].plane_res_b,
11795 hw_plane_wm->wm[level].plane_res_l);
11796 }
08db6652 11797
3de8a14c 11798 if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
11799 &sw_plane_wm->trans_wm)) {
11800 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",
11801 pipe_name(pipe), plane + 1,
11802 sw_plane_wm->trans_wm.plane_en,
11803 sw_plane_wm->trans_wm.plane_res_b,
11804 sw_plane_wm->trans_wm.plane_res_l,
11805 hw_plane_wm->trans_wm.plane_en,
11806 hw_plane_wm->trans_wm.plane_res_b,
11807 hw_plane_wm->trans_wm.plane_res_l);
11808 }
11809
11810 /* DDB */
11811 hw_ddb_entry = &hw_ddb.plane[pipe][plane];
11812 sw_ddb_entry = &sw_ddb->plane[pipe][plane];
11813
11814 if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
faccd994 11815 DRM_ERROR("mismatch in DDB state pipe %c plane %d (expected (%u,%u), found (%u,%u))\n",
3de8a14c 11816 pipe_name(pipe), plane + 1,
11817 sw_ddb_entry->start, sw_ddb_entry->end,
11818 hw_ddb_entry->start, hw_ddb_entry->end);
11819 }
e7c84544 11820 }
08db6652 11821
27082493
L
11822 /*
11823 * cursor
11824 * If the cursor plane isn't active, we may not have updated it's ddb
11825 * allocation. In that case since the ddb allocation will be updated
11826 * once the plane becomes visible, we can skip this check
11827 */
11828 if (intel_crtc->cursor_addr) {
3de8a14c 11829 hw_plane_wm = &hw_wm.planes[PLANE_CURSOR];
11830 sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
11831
11832 /* Watermarks */
11833 for (level = 0; level <= max_level; level++) {
11834 if (skl_wm_level_equals(&hw_plane_wm->wm[level],
11835 &sw_plane_wm->wm[level]))
11836 continue;
11837
11838 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",
11839 pipe_name(pipe), level,
11840 sw_plane_wm->wm[level].plane_en,
11841 sw_plane_wm->wm[level].plane_res_b,
11842 sw_plane_wm->wm[level].plane_res_l,
11843 hw_plane_wm->wm[level].plane_en,
11844 hw_plane_wm->wm[level].plane_res_b,
11845 hw_plane_wm->wm[level].plane_res_l);
11846 }
11847
11848 if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
11849 &sw_plane_wm->trans_wm)) {
11850 DRM_ERROR("mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
11851 pipe_name(pipe),
11852 sw_plane_wm->trans_wm.plane_en,
11853 sw_plane_wm->trans_wm.plane_res_b,
11854 sw_plane_wm->trans_wm.plane_res_l,
11855 hw_plane_wm->trans_wm.plane_en,
11856 hw_plane_wm->trans_wm.plane_res_b,
11857 hw_plane_wm->trans_wm.plane_res_l);
11858 }
11859
11860 /* DDB */
11861 hw_ddb_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
11862 sw_ddb_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
27082493 11863
3de8a14c 11864 if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
faccd994 11865 DRM_ERROR("mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
27082493 11866 pipe_name(pipe),
3de8a14c 11867 sw_ddb_entry->start, sw_ddb_entry->end,
11868 hw_ddb_entry->start, hw_ddb_entry->end);
27082493 11869 }
08db6652
DL
11870 }
11871}
11872
91d1b4bd 11873static void
677100ce
ML
11874verify_connector_state(struct drm_device *dev,
11875 struct drm_atomic_state *state,
11876 struct drm_crtc *crtc)
8af6cf88 11877{
35dd3c64 11878 struct drm_connector *connector;
677100ce
ML
11879 struct drm_connector_state *old_conn_state;
11880 int i;
8af6cf88 11881
677100ce 11882 for_each_connector_in_state(state, connector, old_conn_state, i) {
35dd3c64
ML
11883 struct drm_encoder *encoder = connector->encoder;
11884 struct drm_connector_state *state = connector->state;
ad3c558f 11885
e7c84544
ML
11886 if (state->crtc != crtc)
11887 continue;
11888
5a21b665 11889 intel_connector_verify_state(to_intel_connector(connector));
8af6cf88 11890
ad3c558f 11891 I915_STATE_WARN(state->best_encoder != encoder,
35dd3c64 11892 "connector's atomic encoder doesn't match legacy encoder\n");
8af6cf88 11893 }
91d1b4bd
DV
11894}
11895
11896static void
c0ead703 11897verify_encoder_state(struct drm_device *dev)
91d1b4bd
DV
11898{
11899 struct intel_encoder *encoder;
11900 struct intel_connector *connector;
8af6cf88 11901
b2784e15 11902 for_each_intel_encoder(dev, encoder) {
8af6cf88 11903 bool enabled = false;
4d20cd86 11904 enum pipe pipe;
8af6cf88
DV
11905
11906 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
11907 encoder->base.base.id,
8e329a03 11908 encoder->base.name);
8af6cf88 11909
3a3371ff 11910 for_each_intel_connector(dev, connector) {
4d20cd86 11911 if (connector->base.state->best_encoder != &encoder->base)
8af6cf88
DV
11912 continue;
11913 enabled = true;
ad3c558f
ML
11914
11915 I915_STATE_WARN(connector->base.state->crtc !=
11916 encoder->base.crtc,
11917 "connector's crtc doesn't match encoder crtc\n");
8af6cf88 11918 }
0e32b39c 11919
e2c719b7 11920 I915_STATE_WARN(!!encoder->base.crtc != enabled,
8af6cf88
DV
11921 "encoder's enabled state mismatch "
11922 "(expected %i, found %i)\n",
11923 !!encoder->base.crtc, enabled);
7c60d198
ML
11924
11925 if (!encoder->base.crtc) {
4d20cd86 11926 bool active;
7c60d198 11927
4d20cd86
ML
11928 active = encoder->get_hw_state(encoder, &pipe);
11929 I915_STATE_WARN(active,
11930 "encoder detached but still enabled on pipe %c.\n",
11931 pipe_name(pipe));
7c60d198 11932 }
8af6cf88 11933 }
91d1b4bd
DV
11934}
11935
11936static void
c0ead703
ML
11937verify_crtc_state(struct drm_crtc *crtc,
11938 struct drm_crtc_state *old_crtc_state,
11939 struct drm_crtc_state *new_crtc_state)
91d1b4bd 11940{
e7c84544 11941 struct drm_device *dev = crtc->dev;
fac5e23e 11942 struct drm_i915_private *dev_priv = to_i915(dev);
91d1b4bd 11943 struct intel_encoder *encoder;
e7c84544
ML
11944 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11945 struct intel_crtc_state *pipe_config, *sw_config;
11946 struct drm_atomic_state *old_state;
11947 bool active;
045ac3b5 11948
e7c84544 11949 old_state = old_crtc_state->state;
ec2dc6a0 11950 __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
e7c84544
ML
11951 pipe_config = to_intel_crtc_state(old_crtc_state);
11952 memset(pipe_config, 0, sizeof(*pipe_config));
11953 pipe_config->base.crtc = crtc;
11954 pipe_config->base.state = old_state;
8af6cf88 11955
78108b7c 11956 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
8af6cf88 11957
e7c84544 11958 active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
d62cf62a 11959
e7c84544
ML
11960 /* hw state is inconsistent with the pipe quirk */
11961 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
11962 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
11963 active = new_crtc_state->active;
6c49f241 11964
e7c84544
ML
11965 I915_STATE_WARN(new_crtc_state->active != active,
11966 "crtc active state doesn't match with hw state "
11967 "(expected %i, found %i)\n", new_crtc_state->active, active);
0e8ffe1b 11968
e7c84544
ML
11969 I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
11970 "transitional active state does not match atomic hw state "
11971 "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
4d20cd86 11972
e7c84544
ML
11973 for_each_encoder_on_crtc(dev, crtc, encoder) {
11974 enum pipe pipe;
4d20cd86 11975
e7c84544
ML
11976 active = encoder->get_hw_state(encoder, &pipe);
11977 I915_STATE_WARN(active != new_crtc_state->active,
11978 "[ENCODER:%i] active %i with crtc active %i\n",
11979 encoder->base.base.id, active, new_crtc_state->active);
4d20cd86 11980
e7c84544
ML
11981 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
11982 "Encoder connected to wrong pipe %c\n",
11983 pipe_name(pipe));
4d20cd86 11984
253c84c8
VS
11985 if (active) {
11986 pipe_config->output_types |= 1 << encoder->type;
e7c84544 11987 encoder->get_config(encoder, pipe_config);
253c84c8 11988 }
e7c84544 11989 }
53d9f4e9 11990
a7d1b3f4
VS
11991 intel_crtc_compute_pixel_rate(pipe_config);
11992
e7c84544
ML
11993 if (!new_crtc_state->active)
11994 return;
cfb23ed6 11995
e7c84544 11996 intel_pipe_config_sanity_check(dev_priv, pipe_config);
e3b247da 11997
e7c84544 11998 sw_config = to_intel_crtc_state(crtc->state);
6315b5d3 11999 if (!intel_pipe_config_compare(dev_priv, sw_config,
e7c84544
ML
12000 pipe_config, false)) {
12001 I915_STATE_WARN(1, "pipe state doesn't match!\n");
12002 intel_dump_pipe_config(intel_crtc, pipe_config,
12003 "[hw state]");
12004 intel_dump_pipe_config(intel_crtc, sw_config,
12005 "[sw state]");
8af6cf88
DV
12006 }
12007}
12008
91d1b4bd 12009static void
c0ead703
ML
12010verify_single_dpll_state(struct drm_i915_private *dev_priv,
12011 struct intel_shared_dpll *pll,
12012 struct drm_crtc *crtc,
12013 struct drm_crtc_state *new_state)
91d1b4bd 12014{
91d1b4bd 12015 struct intel_dpll_hw_state dpll_hw_state;
e7c84544
ML
12016 unsigned crtc_mask;
12017 bool active;
5358901f 12018
e7c84544 12019 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
5358901f 12020
e7c84544 12021 DRM_DEBUG_KMS("%s\n", pll->name);
5358901f 12022
e7c84544 12023 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
5358901f 12024
e7c84544
ML
12025 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
12026 I915_STATE_WARN(!pll->on && pll->active_mask,
12027 "pll in active use but not on in sw tracking\n");
12028 I915_STATE_WARN(pll->on && !pll->active_mask,
12029 "pll is on but not used by any active crtc\n");
12030 I915_STATE_WARN(pll->on != active,
12031 "pll on state mismatch (expected %i, found %i)\n",
12032 pll->on, active);
12033 }
5358901f 12034
e7c84544 12035 if (!crtc) {
2c42e535 12036 I915_STATE_WARN(pll->active_mask & ~pll->state.crtc_mask,
e7c84544 12037 "more active pll users than references: %x vs %x\n",
2c42e535 12038 pll->active_mask, pll->state.crtc_mask);
5358901f 12039
e7c84544
ML
12040 return;
12041 }
12042
12043 crtc_mask = 1 << drm_crtc_index(crtc);
12044
12045 if (new_state->active)
12046 I915_STATE_WARN(!(pll->active_mask & crtc_mask),
12047 "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
12048 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
12049 else
12050 I915_STATE_WARN(pll->active_mask & crtc_mask,
12051 "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
12052 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
2dd66ebd 12053
2c42e535 12054 I915_STATE_WARN(!(pll->state.crtc_mask & crtc_mask),
e7c84544 12055 "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
2c42e535 12056 crtc_mask, pll->state.crtc_mask);
66e985c0 12057
2c42e535 12058 I915_STATE_WARN(pll->on && memcmp(&pll->state.hw_state,
e7c84544
ML
12059 &dpll_hw_state,
12060 sizeof(dpll_hw_state)),
12061 "pll hw state mismatch\n");
12062}
12063
12064static void
c0ead703
ML
12065verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
12066 struct drm_crtc_state *old_crtc_state,
12067 struct drm_crtc_state *new_crtc_state)
e7c84544 12068{
fac5e23e 12069 struct drm_i915_private *dev_priv = to_i915(dev);
e7c84544
ML
12070 struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
12071 struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
12072
12073 if (new_state->shared_dpll)
c0ead703 12074 verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
e7c84544
ML
12075
12076 if (old_state->shared_dpll &&
12077 old_state->shared_dpll != new_state->shared_dpll) {
12078 unsigned crtc_mask = 1 << drm_crtc_index(crtc);
12079 struct intel_shared_dpll *pll = old_state->shared_dpll;
12080
12081 I915_STATE_WARN(pll->active_mask & crtc_mask,
12082 "pll active mismatch (didn't expect pipe %c in active mask)\n",
12083 pipe_name(drm_crtc_index(crtc)));
2c42e535 12084 I915_STATE_WARN(pll->state.crtc_mask & crtc_mask,
e7c84544
ML
12085 "pll enabled crtcs mismatch (found %x in enabled mask)\n",
12086 pipe_name(drm_crtc_index(crtc)));
5358901f 12087 }
8af6cf88
DV
12088}
12089
e7c84544 12090static void
c0ead703 12091intel_modeset_verify_crtc(struct drm_crtc *crtc,
677100ce
ML
12092 struct drm_atomic_state *state,
12093 struct drm_crtc_state *old_state,
12094 struct drm_crtc_state *new_state)
e7c84544 12095{
5a21b665
DV
12096 if (!needs_modeset(new_state) &&
12097 !to_intel_crtc_state(new_state)->update_pipe)
12098 return;
12099
c0ead703 12100 verify_wm_state(crtc, new_state);
677100ce 12101 verify_connector_state(crtc->dev, state, crtc);
c0ead703
ML
12102 verify_crtc_state(crtc, old_state, new_state);
12103 verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
e7c84544
ML
12104}
12105
12106static void
c0ead703 12107verify_disabled_dpll_state(struct drm_device *dev)
e7c84544 12108{
fac5e23e 12109 struct drm_i915_private *dev_priv = to_i915(dev);
e7c84544
ML
12110 int i;
12111
12112 for (i = 0; i < dev_priv->num_shared_dpll; i++)
c0ead703 12113 verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
e7c84544
ML
12114}
12115
12116static void
677100ce
ML
12117intel_modeset_verify_disabled(struct drm_device *dev,
12118 struct drm_atomic_state *state)
e7c84544 12119{
c0ead703 12120 verify_encoder_state(dev);
677100ce 12121 verify_connector_state(dev, state, NULL);
c0ead703 12122 verify_disabled_dpll_state(dev);
e7c84544
ML
12123}
12124
80715b2f
VS
12125static void update_scanline_offset(struct intel_crtc *crtc)
12126{
4f8036a2 12127 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
80715b2f
VS
12128
12129 /*
12130 * The scanline counter increments at the leading edge of hsync.
12131 *
12132 * On most platforms it starts counting from vtotal-1 on the
12133 * first active line. That means the scanline counter value is
12134 * always one less than what we would expect. Ie. just after
12135 * start of vblank, which also occurs at start of hsync (on the
12136 * last active line), the scanline counter will read vblank_start-1.
12137 *
12138 * On gen2 the scanline counter starts counting from 1 instead
12139 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12140 * to keep the value positive), instead of adding one.
12141 *
12142 * On HSW+ the behaviour of the scanline counter depends on the output
12143 * type. For DP ports it behaves like most other platforms, but on HDMI
12144 * there's an extra 1 line difference. So we need to add two instead of
12145 * one to the value.
12146 */
4f8036a2 12147 if (IS_GEN2(dev_priv)) {
124abe07 12148 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
80715b2f
VS
12149 int vtotal;
12150
124abe07
VS
12151 vtotal = adjusted_mode->crtc_vtotal;
12152 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
80715b2f
VS
12153 vtotal /= 2;
12154
12155 crtc->scanline_offset = vtotal - 1;
4f8036a2 12156 } else if (HAS_DDI(dev_priv) &&
2d84d2b3 12157 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
80715b2f
VS
12158 crtc->scanline_offset = 2;
12159 } else
12160 crtc->scanline_offset = 1;
12161}
12162
ad421372 12163static void intel_modeset_clear_plls(struct drm_atomic_state *state)
ed6739ef 12164{
225da59b 12165 struct drm_device *dev = state->dev;
ed6739ef 12166 struct drm_i915_private *dev_priv = to_i915(dev);
0a9ab303
ACO
12167 struct drm_crtc *crtc;
12168 struct drm_crtc_state *crtc_state;
0a9ab303 12169 int i;
ed6739ef
ACO
12170
12171 if (!dev_priv->display.crtc_compute_clock)
ad421372 12172 return;
ed6739ef 12173
0a9ab303 12174 for_each_crtc_in_state(state, crtc, crtc_state, i) {
fb1a38a9 12175 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8106ddbd
ACO
12176 struct intel_shared_dpll *old_dpll =
12177 to_intel_crtc_state(crtc->state)->shared_dpll;
0a9ab303 12178
fb1a38a9 12179 if (!needs_modeset(crtc_state))
225da59b
ACO
12180 continue;
12181
8106ddbd 12182 to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
fb1a38a9 12183
8106ddbd 12184 if (!old_dpll)
fb1a38a9 12185 continue;
0a9ab303 12186
a1c414ee 12187 intel_release_shared_dpll(old_dpll, intel_crtc, state);
ad421372 12188 }
ed6739ef
ACO
12189}
12190
99d736a2
ML
12191/*
12192 * This implements the workaround described in the "notes" section of the mode
12193 * set sequence documentation. When going from no pipes or single pipe to
12194 * multiple pipes, and planes are enabled after the pipe, we need to wait at
12195 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
12196 */
12197static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
12198{
12199 struct drm_crtc_state *crtc_state;
12200 struct intel_crtc *intel_crtc;
12201 struct drm_crtc *crtc;
12202 struct intel_crtc_state *first_crtc_state = NULL;
12203 struct intel_crtc_state *other_crtc_state = NULL;
12204 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
12205 int i;
12206
12207 /* look at all crtc's that are going to be enabled in during modeset */
12208 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12209 intel_crtc = to_intel_crtc(crtc);
12210
12211 if (!crtc_state->active || !needs_modeset(crtc_state))
12212 continue;
12213
12214 if (first_crtc_state) {
12215 other_crtc_state = to_intel_crtc_state(crtc_state);
12216 break;
12217 } else {
12218 first_crtc_state = to_intel_crtc_state(crtc_state);
12219 first_pipe = intel_crtc->pipe;
12220 }
12221 }
12222
12223 /* No workaround needed? */
12224 if (!first_crtc_state)
12225 return 0;
12226
12227 /* w/a possibly needed, check how many crtc's are already enabled. */
12228 for_each_intel_crtc(state->dev, intel_crtc) {
12229 struct intel_crtc_state *pipe_config;
12230
12231 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
12232 if (IS_ERR(pipe_config))
12233 return PTR_ERR(pipe_config);
12234
12235 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
12236
12237 if (!pipe_config->base.active ||
12238 needs_modeset(&pipe_config->base))
12239 continue;
12240
12241 /* 2 or more enabled crtcs means no need for w/a */
12242 if (enabled_pipe != INVALID_PIPE)
12243 return 0;
12244
12245 enabled_pipe = intel_crtc->pipe;
12246 }
12247
12248 if (enabled_pipe != INVALID_PIPE)
12249 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
12250 else if (other_crtc_state)
12251 other_crtc_state->hsw_workaround_pipe = first_pipe;
12252
12253 return 0;
12254}
12255
8d96561a
VS
12256static int intel_lock_all_pipes(struct drm_atomic_state *state)
12257{
12258 struct drm_crtc *crtc;
12259
12260 /* Add all pipes to the state */
12261 for_each_crtc(state->dev, crtc) {
12262 struct drm_crtc_state *crtc_state;
12263
12264 crtc_state = drm_atomic_get_crtc_state(state, crtc);
12265 if (IS_ERR(crtc_state))
12266 return PTR_ERR(crtc_state);
12267 }
12268
12269 return 0;
12270}
12271
27c329ed
ML
12272static int intel_modeset_all_pipes(struct drm_atomic_state *state)
12273{
12274 struct drm_crtc *crtc;
27c329ed 12275
8d96561a
VS
12276 /*
12277 * Add all pipes to the state, and force
12278 * a modeset on all the active ones.
12279 */
27c329ed 12280 for_each_crtc(state->dev, crtc) {
9780aad5
VS
12281 struct drm_crtc_state *crtc_state;
12282 int ret;
12283
27c329ed
ML
12284 crtc_state = drm_atomic_get_crtc_state(state, crtc);
12285 if (IS_ERR(crtc_state))
12286 return PTR_ERR(crtc_state);
12287
12288 if (!crtc_state->active || needs_modeset(crtc_state))
12289 continue;
12290
12291 crtc_state->mode_changed = true;
12292
12293 ret = drm_atomic_add_affected_connectors(state, crtc);
12294 if (ret)
9780aad5 12295 return ret;
27c329ed
ML
12296
12297 ret = drm_atomic_add_affected_planes(state, crtc);
12298 if (ret)
9780aad5 12299 return ret;
27c329ed
ML
12300 }
12301
9780aad5 12302 return 0;
27c329ed
ML
12303}
12304
c347a676 12305static int intel_modeset_checks(struct drm_atomic_state *state)
054518dd 12306{
565602d7 12307 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 12308 struct drm_i915_private *dev_priv = to_i915(state->dev);
565602d7
ML
12309 struct drm_crtc *crtc;
12310 struct drm_crtc_state *crtc_state;
12311 int ret = 0, i;
054518dd 12312
b359283a
ML
12313 if (!check_digital_port_conflicts(state)) {
12314 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
12315 return -EINVAL;
12316 }
12317
565602d7
ML
12318 intel_state->modeset = true;
12319 intel_state->active_crtcs = dev_priv->active_crtcs;
bb0f4aab
VS
12320 intel_state->cdclk.logical = dev_priv->cdclk.logical;
12321 intel_state->cdclk.actual = dev_priv->cdclk.actual;
565602d7
ML
12322
12323 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12324 if (crtc_state->active)
12325 intel_state->active_crtcs |= 1 << i;
12326 else
12327 intel_state->active_crtcs &= ~(1 << i);
8b4a7d05
MR
12328
12329 if (crtc_state->active != crtc->state->active)
12330 intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
565602d7
ML
12331 }
12332
054518dd
ACO
12333 /*
12334 * See if the config requires any additional preparation, e.g.
12335 * to adjust global state with pipes off. We need to do this
12336 * here so we can get the modeset_pipe updated config for the new
12337 * mode set on this crtc. For other crtcs we need to use the
12338 * adjusted_mode bits in the crtc directly.
12339 */
27c329ed 12340 if (dev_priv->display.modeset_calc_cdclk) {
27c329ed 12341 ret = dev_priv->display.modeset_calc_cdclk(state);
c89e39f3
CT
12342 if (ret < 0)
12343 return ret;
27c329ed 12344
8d96561a 12345 /*
bb0f4aab 12346 * Writes to dev_priv->cdclk.logical must protected by
8d96561a
VS
12347 * holding all the crtc locks, even if we don't end up
12348 * touching the hardware
12349 */
bb0f4aab
VS
12350 if (!intel_cdclk_state_compare(&dev_priv->cdclk.logical,
12351 &intel_state->cdclk.logical)) {
8d96561a
VS
12352 ret = intel_lock_all_pipes(state);
12353 if (ret < 0)
12354 return ret;
12355 }
12356
12357 /* All pipes must be switched off while we change the cdclk. */
bb0f4aab
VS
12358 if (!intel_cdclk_state_compare(&dev_priv->cdclk.actual,
12359 &intel_state->cdclk.actual)) {
27c329ed 12360 ret = intel_modeset_all_pipes(state);
8d96561a
VS
12361 if (ret < 0)
12362 return ret;
12363 }
e8788cbc 12364
bb0f4aab
VS
12365 DRM_DEBUG_KMS("New cdclk calculated to be logical %u kHz, actual %u kHz\n",
12366 intel_state->cdclk.logical.cdclk,
12367 intel_state->cdclk.actual.cdclk);
e0ca7a6b 12368 } else {
bb0f4aab 12369 to_intel_atomic_state(state)->cdclk.logical = dev_priv->cdclk.logical;
e0ca7a6b 12370 }
054518dd 12371
ad421372 12372 intel_modeset_clear_plls(state);
054518dd 12373
565602d7 12374 if (IS_HASWELL(dev_priv))
ad421372 12375 return haswell_mode_set_planes_workaround(state);
99d736a2 12376
ad421372 12377 return 0;
c347a676
ACO
12378}
12379
aa363136
MR
12380/*
12381 * Handle calculation of various watermark data at the end of the atomic check
12382 * phase. The code here should be run after the per-crtc and per-plane 'check'
12383 * handlers to ensure that all derived state has been updated.
12384 */
55994c2c 12385static int calc_watermark_data(struct drm_atomic_state *state)
aa363136
MR
12386{
12387 struct drm_device *dev = state->dev;
98d39494 12388 struct drm_i915_private *dev_priv = to_i915(dev);
98d39494
MR
12389
12390 /* Is there platform-specific watermark information to calculate? */
12391 if (dev_priv->display.compute_global_watermarks)
55994c2c
MR
12392 return dev_priv->display.compute_global_watermarks(state);
12393
12394 return 0;
aa363136
MR
12395}
12396
74c090b1
ML
12397/**
12398 * intel_atomic_check - validate state object
12399 * @dev: drm device
12400 * @state: state to validate
12401 */
12402static int intel_atomic_check(struct drm_device *dev,
12403 struct drm_atomic_state *state)
c347a676 12404{
dd8b3bdb 12405 struct drm_i915_private *dev_priv = to_i915(dev);
aa363136 12406 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
c347a676
ACO
12407 struct drm_crtc *crtc;
12408 struct drm_crtc_state *crtc_state;
12409 int ret, i;
61333b60 12410 bool any_ms = false;
c347a676 12411
74c090b1 12412 ret = drm_atomic_helper_check_modeset(dev, state);
054518dd
ACO
12413 if (ret)
12414 return ret;
12415
c347a676 12416 for_each_crtc_in_state(state, crtc, crtc_state, i) {
cfb23ed6
ML
12417 struct intel_crtc_state *pipe_config =
12418 to_intel_crtc_state(crtc_state);
1ed51de9
DV
12419
12420 /* Catch I915_MODE_FLAG_INHERITED */
12421 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
12422 crtc_state->mode_changed = true;
cfb23ed6 12423
af4a879e 12424 if (!needs_modeset(crtc_state))
c347a676
ACO
12425 continue;
12426
af4a879e
DV
12427 if (!crtc_state->enable) {
12428 any_ms = true;
cfb23ed6 12429 continue;
af4a879e 12430 }
cfb23ed6 12431
26495481
DV
12432 /* FIXME: For only active_changed we shouldn't need to do any
12433 * state recomputation at all. */
12434
1ed51de9
DV
12435 ret = drm_atomic_add_affected_connectors(state, crtc);
12436 if (ret)
12437 return ret;
b359283a 12438
cfb23ed6 12439 ret = intel_modeset_pipe_config(crtc, pipe_config);
25aa1c39
ML
12440 if (ret) {
12441 intel_dump_pipe_config(to_intel_crtc(crtc),
12442 pipe_config, "[failed]");
c347a676 12443 return ret;
25aa1c39 12444 }
c347a676 12445
73831236 12446 if (i915.fastboot &&
6315b5d3 12447 intel_pipe_config_compare(dev_priv,
cfb23ed6 12448 to_intel_crtc_state(crtc->state),
1ed51de9 12449 pipe_config, true)) {
26495481 12450 crtc_state->mode_changed = false;
bfd16b2a 12451 to_intel_crtc_state(crtc_state)->update_pipe = true;
26495481
DV
12452 }
12453
af4a879e 12454 if (needs_modeset(crtc_state))
26495481 12455 any_ms = true;
cfb23ed6 12456
af4a879e
DV
12457 ret = drm_atomic_add_affected_planes(state, crtc);
12458 if (ret)
12459 return ret;
61333b60 12460
26495481
DV
12461 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
12462 needs_modeset(crtc_state) ?
12463 "[modeset]" : "[fastset]");
c347a676
ACO
12464 }
12465
61333b60
ML
12466 if (any_ms) {
12467 ret = intel_modeset_checks(state);
12468
12469 if (ret)
12470 return ret;
e0ca7a6b 12471 } else {
bb0f4aab 12472 intel_state->cdclk.logical = dev_priv->cdclk.logical;
e0ca7a6b 12473 }
76305b1a 12474
dd8b3bdb 12475 ret = drm_atomic_helper_check_planes(dev, state);
aa363136
MR
12476 if (ret)
12477 return ret;
12478
f51be2e0 12479 intel_fbc_choose_crtc(dev_priv, state);
55994c2c 12480 return calc_watermark_data(state);
054518dd
ACO
12481}
12482
5008e874 12483static int intel_atomic_prepare_commit(struct drm_device *dev,
d07f0e59 12484 struct drm_atomic_state *state)
5008e874 12485{
fac5e23e 12486 struct drm_i915_private *dev_priv = to_i915(dev);
5008e874
ML
12487 struct drm_crtc_state *crtc_state;
12488 struct drm_crtc *crtc;
12489 int i, ret;
12490
5a21b665
DV
12491 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12492 if (state->legacy_cursor_update)
a6747b73
ML
12493 continue;
12494
5a21b665
DV
12495 ret = intel_crtc_wait_for_pending_flips(crtc);
12496 if (ret)
12497 return ret;
5008e874 12498
5a21b665
DV
12499 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
12500 flush_workqueue(dev_priv->wq);
d55dbd06
ML
12501 }
12502
f935675f
ML
12503 ret = mutex_lock_interruptible(&dev->struct_mutex);
12504 if (ret)
12505 return ret;
12506
5008e874 12507 ret = drm_atomic_helper_prepare_planes(dev, state);
f7e5838b 12508 mutex_unlock(&dev->struct_mutex);
7580d774 12509
5008e874
ML
12510 return ret;
12511}
12512
a2991414
ML
12513u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
12514{
12515 struct drm_device *dev = crtc->base.dev;
12516
12517 if (!dev->max_vblank_count)
12518 return drm_accurate_vblank_count(&crtc->base);
12519
12520 return dev->driver->get_vblank_counter(dev, crtc->pipe);
12521}
12522
5a21b665
DV
12523static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
12524 struct drm_i915_private *dev_priv,
12525 unsigned crtc_mask)
e8861675 12526{
5a21b665
DV
12527 unsigned last_vblank_count[I915_MAX_PIPES];
12528 enum pipe pipe;
12529 int ret;
e8861675 12530
5a21b665
DV
12531 if (!crtc_mask)
12532 return;
e8861675 12533
5a21b665 12534 for_each_pipe(dev_priv, pipe) {
98187836
VS
12535 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
12536 pipe);
e8861675 12537
5a21b665 12538 if (!((1 << pipe) & crtc_mask))
e8861675
ML
12539 continue;
12540
e2af48c6 12541 ret = drm_crtc_vblank_get(&crtc->base);
5a21b665
DV
12542 if (WARN_ON(ret != 0)) {
12543 crtc_mask &= ~(1 << pipe);
12544 continue;
e8861675
ML
12545 }
12546
e2af48c6 12547 last_vblank_count[pipe] = drm_crtc_vblank_count(&crtc->base);
e8861675
ML
12548 }
12549
5a21b665 12550 for_each_pipe(dev_priv, pipe) {
98187836
VS
12551 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
12552 pipe);
5a21b665 12553 long lret;
e8861675 12554
5a21b665
DV
12555 if (!((1 << pipe) & crtc_mask))
12556 continue;
d55dbd06 12557
5a21b665
DV
12558 lret = wait_event_timeout(dev->vblank[pipe].queue,
12559 last_vblank_count[pipe] !=
e2af48c6 12560 drm_crtc_vblank_count(&crtc->base),
5a21b665 12561 msecs_to_jiffies(50));
d55dbd06 12562
5a21b665 12563 WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
d55dbd06 12564
e2af48c6 12565 drm_crtc_vblank_put(&crtc->base);
d55dbd06
ML
12566 }
12567}
12568
5a21b665 12569static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
a6747b73 12570{
5a21b665
DV
12571 /* fb updated, need to unpin old fb */
12572 if (crtc_state->fb_changed)
12573 return true;
a6747b73 12574
5a21b665
DV
12575 /* wm changes, need vblank before final wm's */
12576 if (crtc_state->update_wm_post)
12577 return true;
a6747b73 12578
5a21b665
DV
12579 /*
12580 * cxsr is re-enabled after vblank.
12581 * This is already handled by crtc_state->update_wm_post,
12582 * but added for clarity.
12583 */
12584 if (crtc_state->disable_cxsr)
12585 return true;
a6747b73 12586
5a21b665 12587 return false;
e8861675
ML
12588}
12589
896e5bb0
L
12590static void intel_update_crtc(struct drm_crtc *crtc,
12591 struct drm_atomic_state *state,
12592 struct drm_crtc_state *old_crtc_state,
12593 unsigned int *crtc_vblank_mask)
12594{
12595 struct drm_device *dev = crtc->dev;
12596 struct drm_i915_private *dev_priv = to_i915(dev);
12597 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12598 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc->state);
12599 bool modeset = needs_modeset(crtc->state);
12600
12601 if (modeset) {
12602 update_scanline_offset(intel_crtc);
12603 dev_priv->display.crtc_enable(pipe_config, state);
12604 } else {
12605 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
12606 }
12607
12608 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
12609 intel_fbc_enable(
12610 intel_crtc, pipe_config,
12611 to_intel_plane_state(crtc->primary->state));
12612 }
12613
12614 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
12615
12616 if (needs_vblank_wait(pipe_config))
12617 *crtc_vblank_mask |= drm_crtc_mask(crtc);
12618}
12619
12620static void intel_update_crtcs(struct drm_atomic_state *state,
12621 unsigned int *crtc_vblank_mask)
12622{
12623 struct drm_crtc *crtc;
12624 struct drm_crtc_state *old_crtc_state;
12625 int i;
12626
12627 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
12628 if (!crtc->state->active)
12629 continue;
12630
12631 intel_update_crtc(crtc, state, old_crtc_state,
12632 crtc_vblank_mask);
12633 }
12634}
12635
27082493
L
12636static void skl_update_crtcs(struct drm_atomic_state *state,
12637 unsigned int *crtc_vblank_mask)
12638{
0f0f74bc 12639 struct drm_i915_private *dev_priv = to_i915(state->dev);
27082493
L
12640 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
12641 struct drm_crtc *crtc;
ce0ba283 12642 struct intel_crtc *intel_crtc;
27082493 12643 struct drm_crtc_state *old_crtc_state;
ce0ba283 12644 struct intel_crtc_state *cstate;
27082493
L
12645 unsigned int updated = 0;
12646 bool progress;
12647 enum pipe pipe;
5eff503b
ML
12648 int i;
12649
12650 const struct skl_ddb_entry *entries[I915_MAX_PIPES] = {};
12651
12652 for_each_crtc_in_state(state, crtc, old_crtc_state, i)
12653 /* ignore allocations for crtc's that have been turned off. */
12654 if (crtc->state->active)
12655 entries[i] = &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb;
27082493
L
12656
12657 /*
12658 * Whenever the number of active pipes changes, we need to make sure we
12659 * update the pipes in the right order so that their ddb allocations
12660 * never overlap with eachother inbetween CRTC updates. Otherwise we'll
12661 * cause pipe underruns and other bad stuff.
12662 */
12663 do {
27082493
L
12664 progress = false;
12665
12666 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
12667 bool vbl_wait = false;
12668 unsigned int cmask = drm_crtc_mask(crtc);
ce0ba283
L
12669
12670 intel_crtc = to_intel_crtc(crtc);
12671 cstate = to_intel_crtc_state(crtc->state);
12672 pipe = intel_crtc->pipe;
27082493 12673
5eff503b 12674 if (updated & cmask || !cstate->base.active)
27082493 12675 continue;
5eff503b
ML
12676
12677 if (skl_ddb_allocation_overlaps(entries, &cstate->wm.skl.ddb, i))
27082493
L
12678 continue;
12679
12680 updated |= cmask;
5eff503b 12681 entries[i] = &cstate->wm.skl.ddb;
27082493
L
12682
12683 /*
12684 * If this is an already active pipe, it's DDB changed,
12685 * and this isn't the last pipe that needs updating
12686 * then we need to wait for a vblank to pass for the
12687 * new ddb allocation to take effect.
12688 */
ce0ba283 12689 if (!skl_ddb_entry_equal(&cstate->wm.skl.ddb,
512b5527 12690 &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb) &&
27082493
L
12691 !crtc->state->active_changed &&
12692 intel_state->wm_results.dirty_pipes != updated)
12693 vbl_wait = true;
12694
12695 intel_update_crtc(crtc, state, old_crtc_state,
12696 crtc_vblank_mask);
12697
12698 if (vbl_wait)
0f0f74bc 12699 intel_wait_for_vblank(dev_priv, pipe);
27082493
L
12700
12701 progress = true;
12702 }
12703 } while (progress);
12704}
12705
ba318c61
CW
12706static void intel_atomic_helper_free_state(struct drm_i915_private *dev_priv)
12707{
12708 struct intel_atomic_state *state, *next;
12709 struct llist_node *freed;
12710
12711 freed = llist_del_all(&dev_priv->atomic_helper.free_list);
12712 llist_for_each_entry_safe(state, next, freed, freed)
12713 drm_atomic_state_put(&state->base);
12714}
12715
12716static void intel_atomic_helper_free_state_worker(struct work_struct *work)
12717{
12718 struct drm_i915_private *dev_priv =
12719 container_of(work, typeof(*dev_priv), atomic_helper.free_work);
12720
12721 intel_atomic_helper_free_state(dev_priv);
12722}
12723
94f05024 12724static void intel_atomic_commit_tail(struct drm_atomic_state *state)
a6778b3c 12725{
94f05024 12726 struct drm_device *dev = state->dev;
565602d7 12727 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 12728 struct drm_i915_private *dev_priv = to_i915(dev);
29ceb0e6 12729 struct drm_crtc_state *old_crtc_state;
7580d774 12730 struct drm_crtc *crtc;
5a21b665 12731 struct intel_crtc_state *intel_cstate;
5a21b665 12732 bool hw_check = intel_state->modeset;
d8fc70b7 12733 u64 put_domains[I915_MAX_PIPES] = {};
5a21b665 12734 unsigned crtc_vblank_mask = 0;
e95433c7 12735 int i;
a6778b3c 12736
ea0000f0
DV
12737 drm_atomic_helper_wait_for_dependencies(state);
12738
c3b32658 12739 if (intel_state->modeset)
5a21b665 12740 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
565602d7 12741
29ceb0e6 12742 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
a539205a
ML
12743 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12744
5a21b665
DV
12745 if (needs_modeset(crtc->state) ||
12746 to_intel_crtc_state(crtc->state)->update_pipe) {
12747 hw_check = true;
12748
12749 put_domains[to_intel_crtc(crtc)->pipe] =
12750 modeset_get_crtc_power_domains(crtc,
12751 to_intel_crtc_state(crtc->state));
12752 }
12753
61333b60
ML
12754 if (!needs_modeset(crtc->state))
12755 continue;
12756
29ceb0e6 12757 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
460da916 12758
29ceb0e6
VS
12759 if (old_crtc_state->active) {
12760 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
4a806558 12761 dev_priv->display.crtc_disable(to_intel_crtc_state(old_crtc_state), state);
eddfcbcd 12762 intel_crtc->active = false;
58f9c0bc 12763 intel_fbc_disable(intel_crtc);
eddfcbcd 12764 intel_disable_shared_dpll(intel_crtc);
9bbc8258
VS
12765
12766 /*
12767 * Underruns don't always raise
12768 * interrupts, so check manually.
12769 */
12770 intel_check_cpu_fifo_underruns(dev_priv);
12771 intel_check_pch_fifo_underruns(dev_priv);
b9001114 12772
e62929b3
ML
12773 if (!crtc->state->active) {
12774 /*
12775 * Make sure we don't call initial_watermarks
12776 * for ILK-style watermark updates.
12777 */
12778 if (dev_priv->display.atomic_update_watermarks)
12779 dev_priv->display.initial_watermarks(intel_state,
12780 to_intel_crtc_state(crtc->state));
12781 else
12782 intel_update_watermarks(intel_crtc);
12783 }
a539205a 12784 }
b8cecdf5 12785 }
7758a113 12786
ea9d758d
DV
12787 /* Only after disabling all output pipelines that will be changed can we
12788 * update the the output configuration. */
4740b0f2 12789 intel_modeset_update_crtc_state(state);
f6e5b160 12790
565602d7 12791 if (intel_state->modeset) {
4740b0f2 12792 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
33c8df89 12793
b0587e4d 12794 intel_set_cdclk(dev_priv, &dev_priv->cdclk.actual);
f6d1973d 12795
656d1b89
L
12796 /*
12797 * SKL workaround: bspec recommends we disable the SAGV when we
12798 * have more then one pipe enabled
12799 */
56feca91 12800 if (!intel_can_enable_sagv(state))
16dcdc4e 12801 intel_disable_sagv(dev_priv);
656d1b89 12802
677100ce 12803 intel_modeset_verify_disabled(dev, state);
4740b0f2 12804 }
47fab737 12805
896e5bb0 12806 /* Complete the events for pipes that have now been disabled */
29ceb0e6 12807 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
f6ac4b2a 12808 bool modeset = needs_modeset(crtc->state);
80715b2f 12809
1f7528c4
DV
12810 /* Complete events for now disable pipes here. */
12811 if (modeset && !crtc->state->active && crtc->state->event) {
12812 spin_lock_irq(&dev->event_lock);
12813 drm_crtc_send_vblank_event(crtc, crtc->state->event);
12814 spin_unlock_irq(&dev->event_lock);
12815
12816 crtc->state->event = NULL;
12817 }
177246a8
MR
12818 }
12819
896e5bb0
L
12820 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
12821 dev_priv->display.update_crtcs(state, &crtc_vblank_mask);
12822
94f05024
DV
12823 /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
12824 * already, but still need the state for the delayed optimization. To
12825 * fix this:
12826 * - wrap the optimization/post_plane_update stuff into a per-crtc work.
12827 * - schedule that vblank worker _before_ calling hw_done
12828 * - at the start of commit_tail, cancel it _synchrously
12829 * - switch over to the vblank wait helper in the core after that since
12830 * we don't need out special handling any more.
12831 */
5a21b665
DV
12832 if (!state->legacy_cursor_update)
12833 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
12834
12835 /*
12836 * Now that the vblank has passed, we can go ahead and program the
12837 * optimal watermarks on platforms that need two-step watermark
12838 * programming.
12839 *
12840 * TODO: Move this (and other cleanup) to an async worker eventually.
12841 */
12842 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
12843 intel_cstate = to_intel_crtc_state(crtc->state);
12844
12845 if (dev_priv->display.optimize_watermarks)
ccf010fb
ML
12846 dev_priv->display.optimize_watermarks(intel_state,
12847 intel_cstate);
5a21b665
DV
12848 }
12849
12850 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
12851 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
12852
12853 if (put_domains[i])
12854 modeset_put_power_domains(dev_priv, put_domains[i]);
12855
677100ce 12856 intel_modeset_verify_crtc(crtc, state, old_crtc_state, crtc->state);
5a21b665
DV
12857 }
12858
56feca91 12859 if (intel_state->modeset && intel_can_enable_sagv(state))
16dcdc4e 12860 intel_enable_sagv(dev_priv);
656d1b89 12861
94f05024
DV
12862 drm_atomic_helper_commit_hw_done(state);
12863
5a21b665
DV
12864 if (intel_state->modeset)
12865 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
12866
12867 mutex_lock(&dev->struct_mutex);
12868 drm_atomic_helper_cleanup_planes(dev, state);
12869 mutex_unlock(&dev->struct_mutex);
12870
ea0000f0
DV
12871 drm_atomic_helper_commit_cleanup_done(state);
12872
0853695c 12873 drm_atomic_state_put(state);
f30da187 12874
75714940
MK
12875 /* As one of the primary mmio accessors, KMS has a high likelihood
12876 * of triggering bugs in unclaimed access. After we finish
12877 * modesetting, see if an error has been flagged, and if so
12878 * enable debugging for the next modeset - and hope we catch
12879 * the culprit.
12880 *
12881 * XXX note that we assume display power is on at this point.
12882 * This might hold true now but we need to add pm helper to check
12883 * unclaimed only when the hardware is on, as atomic commits
12884 * can happen also when the device is completely off.
12885 */
12886 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
ba318c61
CW
12887
12888 intel_atomic_helper_free_state(dev_priv);
94f05024
DV
12889}
12890
12891static void intel_atomic_commit_work(struct work_struct *work)
12892{
c004a90b
CW
12893 struct drm_atomic_state *state =
12894 container_of(work, struct drm_atomic_state, commit_work);
12895
94f05024
DV
12896 intel_atomic_commit_tail(state);
12897}
12898
c004a90b
CW
12899static int __i915_sw_fence_call
12900intel_atomic_commit_ready(struct i915_sw_fence *fence,
12901 enum i915_sw_fence_notify notify)
12902{
12903 struct intel_atomic_state *state =
12904 container_of(fence, struct intel_atomic_state, commit_ready);
12905
12906 switch (notify) {
12907 case FENCE_COMPLETE:
12908 if (state->base.commit_work.func)
12909 queue_work(system_unbound_wq, &state->base.commit_work);
12910 break;
12911
12912 case FENCE_FREE:
eb955eee
CW
12913 {
12914 struct intel_atomic_helper *helper =
12915 &to_i915(state->base.dev)->atomic_helper;
12916
12917 if (llist_add(&state->freed, &helper->free_list))
12918 schedule_work(&helper->free_work);
12919 break;
12920 }
c004a90b
CW
12921 }
12922
12923 return NOTIFY_DONE;
12924}
12925
6c9c1b38
DV
12926static void intel_atomic_track_fbs(struct drm_atomic_state *state)
12927{
12928 struct drm_plane_state *old_plane_state;
12929 struct drm_plane *plane;
6c9c1b38
DV
12930 int i;
12931
faf5bf0a
CW
12932 for_each_plane_in_state(state, plane, old_plane_state, i)
12933 i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
12934 intel_fb_obj(plane->state->fb),
12935 to_intel_plane(plane)->frontbuffer_bit);
6c9c1b38
DV
12936}
12937
94f05024
DV
12938/**
12939 * intel_atomic_commit - commit validated state object
12940 * @dev: DRM device
12941 * @state: the top-level driver state object
12942 * @nonblock: nonblocking commit
12943 *
12944 * This function commits a top-level state object that has been validated
12945 * with drm_atomic_helper_check().
12946 *
94f05024
DV
12947 * RETURNS
12948 * Zero for success or -errno.
12949 */
12950static int intel_atomic_commit(struct drm_device *dev,
12951 struct drm_atomic_state *state,
12952 bool nonblock)
12953{
12954 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 12955 struct drm_i915_private *dev_priv = to_i915(dev);
94f05024
DV
12956 int ret = 0;
12957
a5509abd
VS
12958 /*
12959 * The intel_legacy_cursor_update() fast path takes care
12960 * of avoiding the vblank waits for simple cursor
12961 * movement and flips. For cursor on/off and size changes,
12962 * we want to perform the vblank waits so that watermark
12963 * updates happen during the correct frames. Gen9+ have
12964 * double buffered watermarks and so shouldn't need this.
12965 */
12966 if (INTEL_GEN(dev_priv) < 9)
12967 state->legacy_cursor_update = false;
12968
94f05024
DV
12969 ret = drm_atomic_helper_setup_commit(state, nonblock);
12970 if (ret)
12971 return ret;
12972
c004a90b
CW
12973 drm_atomic_state_get(state);
12974 i915_sw_fence_init(&intel_state->commit_ready,
12975 intel_atomic_commit_ready);
94f05024 12976
d07f0e59 12977 ret = intel_atomic_prepare_commit(dev, state);
94f05024
DV
12978 if (ret) {
12979 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
c004a90b 12980 i915_sw_fence_commit(&intel_state->commit_ready);
94f05024
DV
12981 return ret;
12982 }
12983
12984 drm_atomic_helper_swap_state(state, true);
12985 dev_priv->wm.distrust_bios_wm = false;
3c0fb588 12986 intel_shared_dpll_swap_state(state);
6c9c1b38 12987 intel_atomic_track_fbs(state);
94f05024 12988
c3b32658
ML
12989 if (intel_state->modeset) {
12990 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
12991 sizeof(intel_state->min_pixclk));
12992 dev_priv->active_crtcs = intel_state->active_crtcs;
bb0f4aab
VS
12993 dev_priv->cdclk.logical = intel_state->cdclk.logical;
12994 dev_priv->cdclk.actual = intel_state->cdclk.actual;
c3b32658
ML
12995 }
12996
0853695c 12997 drm_atomic_state_get(state);
c004a90b
CW
12998 INIT_WORK(&state->commit_work,
12999 nonblock ? intel_atomic_commit_work : NULL);
13000
13001 i915_sw_fence_commit(&intel_state->commit_ready);
13002 if (!nonblock) {
13003 i915_sw_fence_wait(&intel_state->commit_ready);
94f05024 13004 intel_atomic_commit_tail(state);
c004a90b 13005 }
75714940 13006
74c090b1 13007 return 0;
7f27126e
JB
13008}
13009
c0c36b94
CW
13010void intel_crtc_restore_mode(struct drm_crtc *crtc)
13011{
83a57153
ACO
13012 struct drm_device *dev = crtc->dev;
13013 struct drm_atomic_state *state;
e694eb02 13014 struct drm_crtc_state *crtc_state;
2bfb4627 13015 int ret;
83a57153
ACO
13016
13017 state = drm_atomic_state_alloc(dev);
13018 if (!state) {
78108b7c
VS
13019 DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
13020 crtc->base.id, crtc->name);
83a57153
ACO
13021 return;
13022 }
13023
e694eb02 13024 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
83a57153 13025
e694eb02
ML
13026retry:
13027 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13028 ret = PTR_ERR_OR_ZERO(crtc_state);
13029 if (!ret) {
13030 if (!crtc_state->active)
13031 goto out;
83a57153 13032
e694eb02 13033 crtc_state->mode_changed = true;
74c090b1 13034 ret = drm_atomic_commit(state);
83a57153
ACO
13035 }
13036
e694eb02
ML
13037 if (ret == -EDEADLK) {
13038 drm_atomic_state_clear(state);
13039 drm_modeset_backoff(state->acquire_ctx);
13040 goto retry;
4ed9fb37 13041 }
4be07317 13042
e694eb02 13043out:
0853695c 13044 drm_atomic_state_put(state);
c0c36b94
CW
13045}
13046
a8784875
BP
13047/*
13048 * FIXME: Remove this once i915 is fully DRIVER_ATOMIC by calling
13049 * drm_atomic_helper_legacy_gamma_set() directly.
13050 */
13051static int intel_atomic_legacy_gamma_set(struct drm_crtc *crtc,
13052 u16 *red, u16 *green, u16 *blue,
13053 uint32_t size)
13054{
13055 struct drm_device *dev = crtc->dev;
13056 struct drm_mode_config *config = &dev->mode_config;
13057 struct drm_crtc_state *state;
13058 int ret;
13059
13060 ret = drm_atomic_helper_legacy_gamma_set(crtc, red, green, blue, size);
13061 if (ret)
13062 return ret;
13063
13064 /*
13065 * Make sure we update the legacy properties so this works when
13066 * atomic is not enabled.
13067 */
13068
13069 state = crtc->state;
13070
13071 drm_object_property_set_value(&crtc->base,
13072 config->degamma_lut_property,
13073 (state->degamma_lut) ?
13074 state->degamma_lut->base.id : 0);
13075
13076 drm_object_property_set_value(&crtc->base,
13077 config->ctm_property,
13078 (state->ctm) ?
13079 state->ctm->base.id : 0);
13080
13081 drm_object_property_set_value(&crtc->base,
13082 config->gamma_lut_property,
13083 (state->gamma_lut) ?
13084 state->gamma_lut->base.id : 0);
13085
13086 return 0;
13087}
13088
f6e5b160 13089static const struct drm_crtc_funcs intel_crtc_funcs = {
a8784875 13090 .gamma_set = intel_atomic_legacy_gamma_set,
74c090b1 13091 .set_config = drm_atomic_helper_set_config,
82cf435b 13092 .set_property = drm_atomic_helper_crtc_set_property,
f6e5b160 13093 .destroy = intel_crtc_destroy,
4c01ded5 13094 .page_flip = drm_atomic_helper_page_flip,
1356837e
MR
13095 .atomic_duplicate_state = intel_crtc_duplicate_state,
13096 .atomic_destroy_state = intel_crtc_destroy_state,
8c6b709d 13097 .set_crc_source = intel_crtc_set_crc_source,
f6e5b160
CW
13098};
13099
6beb8c23
MR
13100/**
13101 * intel_prepare_plane_fb - Prepare fb for usage on plane
13102 * @plane: drm plane to prepare for
13103 * @fb: framebuffer to prepare for presentation
13104 *
13105 * Prepares a framebuffer for usage on a display plane. Generally this
13106 * involves pinning the underlying object and updating the frontbuffer tracking
13107 * bits. Some older platforms need special physical address handling for
13108 * cursor planes.
13109 *
f935675f
ML
13110 * Must be called with struct_mutex held.
13111 *
6beb8c23
MR
13112 * Returns 0 on success, negative error code on failure.
13113 */
13114int
13115intel_prepare_plane_fb(struct drm_plane *plane,
1832040d 13116 struct drm_plane_state *new_state)
465c120c 13117{
c004a90b
CW
13118 struct intel_atomic_state *intel_state =
13119 to_intel_atomic_state(new_state->state);
b7f05d4a 13120 struct drm_i915_private *dev_priv = to_i915(plane->dev);
844f9111 13121 struct drm_framebuffer *fb = new_state->fb;
6beb8c23 13122 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
1ee49399 13123 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
c004a90b 13124 int ret;
465c120c 13125
57822dc6
CW
13126 if (obj) {
13127 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
13128 INTEL_INFO(dev_priv)->cursor_needs_physical) {
13129 const int align = IS_I830(dev_priv) ? 16 * 1024 : 256;
13130
13131 ret = i915_gem_object_attach_phys(obj, align);
13132 if (ret) {
13133 DRM_DEBUG_KMS("failed to attach phys object\n");
13134 return ret;
13135 }
13136 } else {
13137 struct i915_vma *vma;
13138
13139 vma = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
13140 if (IS_ERR(vma)) {
13141 DRM_DEBUG_KMS("failed to pin object\n");
13142 return PTR_ERR(vma);
13143 }
13144
13145 to_intel_plane_state(new_state)->vma = vma;
13146 }
13147 }
13148
1ee49399 13149 if (!obj && !old_obj)
465c120c
MR
13150 return 0;
13151
5008e874
ML
13152 if (old_obj) {
13153 struct drm_crtc_state *crtc_state =
c004a90b
CW
13154 drm_atomic_get_existing_crtc_state(new_state->state,
13155 plane->state->crtc);
5008e874
ML
13156
13157 /* Big Hammer, we also need to ensure that any pending
13158 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
13159 * current scanout is retired before unpinning the old
13160 * framebuffer. Note that we rely on userspace rendering
13161 * into the buffer attached to the pipe they are waiting
13162 * on. If not, userspace generates a GPU hang with IPEHR
13163 * point to the MI_WAIT_FOR_EVENT.
13164 *
13165 * This should only fail upon a hung GPU, in which case we
13166 * can safely continue.
13167 */
c004a90b
CW
13168 if (needs_modeset(crtc_state)) {
13169 ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
13170 old_obj->resv, NULL,
13171 false, 0,
13172 GFP_KERNEL);
13173 if (ret < 0)
13174 return ret;
f4457ae7 13175 }
5008e874
ML
13176 }
13177
c004a90b
CW
13178 if (new_state->fence) { /* explicit fencing */
13179 ret = i915_sw_fence_await_dma_fence(&intel_state->commit_ready,
13180 new_state->fence,
13181 I915_FENCE_TIMEOUT,
13182 GFP_KERNEL);
13183 if (ret < 0)
13184 return ret;
13185 }
13186
c37efb99
CW
13187 if (!obj)
13188 return 0;
13189
c004a90b
CW
13190 if (!new_state->fence) { /* implicit fencing */
13191 ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
13192 obj->resv, NULL,
13193 false, I915_FENCE_TIMEOUT,
13194 GFP_KERNEL);
13195 if (ret < 0)
13196 return ret;
6b5e90f5
CW
13197
13198 i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
c004a90b 13199 }
5a21b665 13200
d07f0e59 13201 return 0;
6beb8c23
MR
13202}
13203
38f3ce3a
MR
13204/**
13205 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13206 * @plane: drm plane to clean up for
13207 * @fb: old framebuffer that was on plane
13208 *
13209 * Cleans up a framebuffer that has just been removed from a plane.
f935675f
ML
13210 *
13211 * Must be called with struct_mutex held.
38f3ce3a
MR
13212 */
13213void
13214intel_cleanup_plane_fb(struct drm_plane *plane,
1832040d 13215 struct drm_plane_state *old_state)
38f3ce3a 13216{
be1e3415 13217 struct i915_vma *vma;
38f3ce3a 13218
be1e3415
CW
13219 /* Should only be called after a successful intel_prepare_plane_fb()! */
13220 vma = fetch_and_zero(&to_intel_plane_state(old_state)->vma);
13221 if (vma)
13222 intel_unpin_fb_vma(vma);
465c120c
MR
13223}
13224
6156a456
CK
13225int
13226skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13227{
5b7280f0 13228 struct drm_i915_private *dev_priv;
6156a456 13229 int max_scale;
5b7280f0 13230 int crtc_clock, max_dotclk;
6156a456 13231
bf8a0af0 13232 if (!intel_crtc || !crtc_state->base.enable)
6156a456
CK
13233 return DRM_PLANE_HELPER_NO_SCALING;
13234
5b7280f0
ACO
13235 dev_priv = to_i915(intel_crtc->base.dev);
13236
6156a456 13237 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
5b7280f0
ACO
13238 max_dotclk = to_intel_atomic_state(crtc_state->base.state)->cdclk.logical.cdclk;
13239
13240 if (IS_GEMINILAKE(dev_priv))
13241 max_dotclk *= 2;
6156a456 13242
5b7280f0 13243 if (WARN_ON_ONCE(!crtc_clock || max_dotclk < crtc_clock))
6156a456
CK
13244 return DRM_PLANE_HELPER_NO_SCALING;
13245
13246 /*
13247 * skl max scale is lower of:
13248 * close to 3 but not 3, -1 is for that purpose
13249 * or
13250 * cdclk/crtc_clock
13251 */
5b7280f0
ACO
13252 max_scale = min((1 << 16) * 3 - 1,
13253 (1 << 8) * ((max_dotclk << 8) / crtc_clock));
6156a456
CK
13254
13255 return max_scale;
13256}
13257
465c120c 13258static int
3c692a41 13259intel_check_primary_plane(struct drm_plane *plane,
061e4b8d 13260 struct intel_crtc_state *crtc_state,
3c692a41
GP
13261 struct intel_plane_state *state)
13262{
b63a16f6 13263 struct drm_i915_private *dev_priv = to_i915(plane->dev);
2b875c22 13264 struct drm_crtc *crtc = state->base.crtc;
6156a456 13265 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
061e4b8d
ML
13266 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13267 bool can_position = false;
b63a16f6 13268 int ret;
465c120c 13269
b63a16f6 13270 if (INTEL_GEN(dev_priv) >= 9) {
693bdc28
VS
13271 /* use scaler when colorkey is not required */
13272 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
13273 min_scale = 1;
13274 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
13275 }
d8106366 13276 can_position = true;
6156a456 13277 }
d8106366 13278
cc926387
DV
13279 ret = drm_plane_helper_check_state(&state->base,
13280 &state->clip,
13281 min_scale, max_scale,
13282 can_position, true);
b63a16f6
VS
13283 if (ret)
13284 return ret;
13285
cc926387 13286 if (!state->base.fb)
b63a16f6
VS
13287 return 0;
13288
13289 if (INTEL_GEN(dev_priv) >= 9) {
13290 ret = skl_check_plane_surface(state);
13291 if (ret)
13292 return ret;
13293 }
13294
13295 return 0;
14af293f
GP
13296}
13297
5a21b665
DV
13298static void intel_begin_crtc_commit(struct drm_crtc *crtc,
13299 struct drm_crtc_state *old_crtc_state)
13300{
13301 struct drm_device *dev = crtc->dev;
62e0fb88 13302 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665 13303 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b707aa50
L
13304 struct intel_crtc_state *intel_cstate =
13305 to_intel_crtc_state(crtc->state);
ccf010fb 13306 struct intel_crtc_state *old_intel_cstate =
5a21b665 13307 to_intel_crtc_state(old_crtc_state);
ccf010fb
ML
13308 struct intel_atomic_state *old_intel_state =
13309 to_intel_atomic_state(old_crtc_state->state);
5a21b665
DV
13310 bool modeset = needs_modeset(crtc->state);
13311
13312 /* Perform vblank evasion around commit operation */
13313 intel_pipe_update_start(intel_crtc);
13314
13315 if (modeset)
e62929b3 13316 goto out;
5a21b665
DV
13317
13318 if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
13319 intel_color_set_csc(crtc->state);
13320 intel_color_load_luts(crtc->state);
13321 }
13322
ccf010fb
ML
13323 if (intel_cstate->update_pipe)
13324 intel_update_pipe_config(intel_crtc, old_intel_cstate);
13325 else if (INTEL_GEN(dev_priv) >= 9)
5a21b665 13326 skl_detach_scalers(intel_crtc);
62e0fb88 13327
e62929b3 13328out:
ccf010fb
ML
13329 if (dev_priv->display.atomic_update_watermarks)
13330 dev_priv->display.atomic_update_watermarks(old_intel_state,
13331 intel_cstate);
5a21b665
DV
13332}
13333
13334static void intel_finish_crtc_commit(struct drm_crtc *crtc,
13335 struct drm_crtc_state *old_crtc_state)
13336{
13337 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13338
13339 intel_pipe_update_end(intel_crtc, NULL);
13340}
13341
cf4c7c12 13342/**
4a3b8769
MR
13343 * intel_plane_destroy - destroy a plane
13344 * @plane: plane to destroy
cf4c7c12 13345 *
4a3b8769
MR
13346 * Common destruction function for all types of planes (primary, cursor,
13347 * sprite).
cf4c7c12 13348 */
4a3b8769 13349void intel_plane_destroy(struct drm_plane *plane)
465c120c 13350{
465c120c 13351 drm_plane_cleanup(plane);
69ae561f 13352 kfree(to_intel_plane(plane));
465c120c
MR
13353}
13354
65a3fea0 13355const struct drm_plane_funcs intel_plane_funcs = {
70a101f8
MR
13356 .update_plane = drm_atomic_helper_update_plane,
13357 .disable_plane = drm_atomic_helper_disable_plane,
3d7d6510 13358 .destroy = intel_plane_destroy,
c196e1d6 13359 .set_property = drm_atomic_helper_plane_set_property,
a98b3431
MR
13360 .atomic_get_property = intel_plane_atomic_get_property,
13361 .atomic_set_property = intel_plane_atomic_set_property,
ea2c67bb
MR
13362 .atomic_duplicate_state = intel_plane_duplicate_state,
13363 .atomic_destroy_state = intel_plane_destroy_state,
465c120c
MR
13364};
13365
f79f2692
ML
13366static int
13367intel_legacy_cursor_update(struct drm_plane *plane,
13368 struct drm_crtc *crtc,
13369 struct drm_framebuffer *fb,
13370 int crtc_x, int crtc_y,
13371 unsigned int crtc_w, unsigned int crtc_h,
13372 uint32_t src_x, uint32_t src_y,
13373 uint32_t src_w, uint32_t src_h)
13374{
13375 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
13376 int ret;
13377 struct drm_plane_state *old_plane_state, *new_plane_state;
13378 struct intel_plane *intel_plane = to_intel_plane(plane);
13379 struct drm_framebuffer *old_fb;
13380 struct drm_crtc_state *crtc_state = crtc->state;
be1e3415 13381 struct i915_vma *old_vma;
f79f2692
ML
13382
13383 /*
13384 * When crtc is inactive or there is a modeset pending,
13385 * wait for it to complete in the slowpath
13386 */
13387 if (!crtc_state->active || needs_modeset(crtc_state) ||
13388 to_intel_crtc_state(crtc_state)->update_pipe)
13389 goto slow;
13390
13391 old_plane_state = plane->state;
13392
13393 /*
13394 * If any parameters change that may affect watermarks,
13395 * take the slowpath. Only changing fb or position should be
13396 * in the fastpath.
13397 */
13398 if (old_plane_state->crtc != crtc ||
13399 old_plane_state->src_w != src_w ||
13400 old_plane_state->src_h != src_h ||
13401 old_plane_state->crtc_w != crtc_w ||
13402 old_plane_state->crtc_h != crtc_h ||
a5509abd 13403 !old_plane_state->fb != !fb)
f79f2692
ML
13404 goto slow;
13405
13406 new_plane_state = intel_plane_duplicate_state(plane);
13407 if (!new_plane_state)
13408 return -ENOMEM;
13409
13410 drm_atomic_set_fb_for_plane(new_plane_state, fb);
13411
13412 new_plane_state->src_x = src_x;
13413 new_plane_state->src_y = src_y;
13414 new_plane_state->src_w = src_w;
13415 new_plane_state->src_h = src_h;
13416 new_plane_state->crtc_x = crtc_x;
13417 new_plane_state->crtc_y = crtc_y;
13418 new_plane_state->crtc_w = crtc_w;
13419 new_plane_state->crtc_h = crtc_h;
13420
13421 ret = intel_plane_atomic_check_with_state(to_intel_crtc_state(crtc->state),
13422 to_intel_plane_state(new_plane_state));
13423 if (ret)
13424 goto out_free;
13425
f79f2692
ML
13426 ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
13427 if (ret)
13428 goto out_free;
13429
13430 if (INTEL_INFO(dev_priv)->cursor_needs_physical) {
13431 int align = IS_I830(dev_priv) ? 16 * 1024 : 256;
13432
13433 ret = i915_gem_object_attach_phys(intel_fb_obj(fb), align);
13434 if (ret) {
13435 DRM_DEBUG_KMS("failed to attach phys object\n");
13436 goto out_unlock;
13437 }
13438 } else {
13439 struct i915_vma *vma;
13440
13441 vma = intel_pin_and_fence_fb_obj(fb, new_plane_state->rotation);
13442 if (IS_ERR(vma)) {
13443 DRM_DEBUG_KMS("failed to pin object\n");
13444
13445 ret = PTR_ERR(vma);
13446 goto out_unlock;
13447 }
be1e3415
CW
13448
13449 to_intel_plane_state(new_plane_state)->vma = vma;
f79f2692
ML
13450 }
13451
13452 old_fb = old_plane_state->fb;
be1e3415 13453 old_vma = to_intel_plane_state(old_plane_state)->vma;
f79f2692
ML
13454
13455 i915_gem_track_fb(intel_fb_obj(old_fb), intel_fb_obj(fb),
13456 intel_plane->frontbuffer_bit);
13457
13458 /* Swap plane state */
13459 new_plane_state->fence = old_plane_state->fence;
13460 *to_intel_plane_state(old_plane_state) = *to_intel_plane_state(new_plane_state);
13461 new_plane_state->fence = NULL;
13462 new_plane_state->fb = old_fb;
be1e3415 13463 to_intel_plane_state(new_plane_state)->vma = old_vma;
f79f2692 13464
a5509abd
VS
13465 if (plane->state->visible)
13466 intel_plane->update_plane(plane,
13467 to_intel_crtc_state(crtc->state),
13468 to_intel_plane_state(plane->state));
13469 else
13470 intel_plane->disable_plane(plane, crtc);
f79f2692
ML
13471
13472 intel_cleanup_plane_fb(plane, new_plane_state);
13473
13474out_unlock:
13475 mutex_unlock(&dev_priv->drm.struct_mutex);
13476out_free:
13477 intel_plane_destroy_state(plane, new_plane_state);
13478 return ret;
13479
f79f2692
ML
13480slow:
13481 return drm_atomic_helper_update_plane(plane, crtc, fb,
13482 crtc_x, crtc_y, crtc_w, crtc_h,
13483 src_x, src_y, src_w, src_h);
13484}
13485
13486static const struct drm_plane_funcs intel_cursor_plane_funcs = {
13487 .update_plane = intel_legacy_cursor_update,
13488 .disable_plane = drm_atomic_helper_disable_plane,
13489 .destroy = intel_plane_destroy,
13490 .set_property = drm_atomic_helper_plane_set_property,
13491 .atomic_get_property = intel_plane_atomic_get_property,
13492 .atomic_set_property = intel_plane_atomic_set_property,
13493 .atomic_duplicate_state = intel_plane_duplicate_state,
13494 .atomic_destroy_state = intel_plane_destroy_state,
13495};
13496
b079bd17 13497static struct intel_plane *
580503c7 13498intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
465c120c 13499{
fca0ce2a
VS
13500 struct intel_plane *primary = NULL;
13501 struct intel_plane_state *state = NULL;
465c120c 13502 const uint32_t *intel_primary_formats;
93ca7e00 13503 unsigned int supported_rotations;
45e3743a 13504 unsigned int num_formats;
fca0ce2a 13505 int ret;
465c120c
MR
13506
13507 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
b079bd17
VS
13508 if (!primary) {
13509 ret = -ENOMEM;
fca0ce2a 13510 goto fail;
b079bd17 13511 }
465c120c 13512
8e7d688b 13513 state = intel_create_plane_state(&primary->base);
b079bd17
VS
13514 if (!state) {
13515 ret = -ENOMEM;
fca0ce2a 13516 goto fail;
b079bd17
VS
13517 }
13518
8e7d688b 13519 primary->base.state = &state->base;
ea2c67bb 13520
465c120c
MR
13521 primary->can_scale = false;
13522 primary->max_downscale = 1;
580503c7 13523 if (INTEL_GEN(dev_priv) >= 9) {
6156a456 13524 primary->can_scale = true;
af99ceda 13525 state->scaler_id = -1;
6156a456 13526 }
465c120c 13527 primary->pipe = pipe;
e3c566df
VS
13528 /*
13529 * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
13530 * port is hooked to pipe B. Hence we want plane A feeding pipe B.
13531 */
13532 if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4)
13533 primary->plane = (enum plane) !pipe;
13534 else
13535 primary->plane = (enum plane) pipe;
b14e5848 13536 primary->id = PLANE_PRIMARY;
a9ff8714 13537 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
c59cb179 13538 primary->check_plane = intel_check_primary_plane;
465c120c 13539
580503c7 13540 if (INTEL_GEN(dev_priv) >= 9) {
6c0fd451
DL
13541 intel_primary_formats = skl_primary_formats;
13542 num_formats = ARRAY_SIZE(skl_primary_formats);
a8d201af
ML
13543
13544 primary->update_plane = skylake_update_primary_plane;
13545 primary->disable_plane = skylake_disable_primary_plane;
6e266956 13546 } else if (HAS_PCH_SPLIT(dev_priv)) {
a8d201af
ML
13547 intel_primary_formats = i965_primary_formats;
13548 num_formats = ARRAY_SIZE(i965_primary_formats);
13549
13550 primary->update_plane = ironlake_update_primary_plane;
13551 primary->disable_plane = i9xx_disable_primary_plane;
580503c7 13552 } else if (INTEL_GEN(dev_priv) >= 4) {
568db4f2
DL
13553 intel_primary_formats = i965_primary_formats;
13554 num_formats = ARRAY_SIZE(i965_primary_formats);
a8d201af
ML
13555
13556 primary->update_plane = i9xx_update_primary_plane;
13557 primary->disable_plane = i9xx_disable_primary_plane;
6c0fd451
DL
13558 } else {
13559 intel_primary_formats = i8xx_primary_formats;
13560 num_formats = ARRAY_SIZE(i8xx_primary_formats);
a8d201af
ML
13561
13562 primary->update_plane = i9xx_update_primary_plane;
13563 primary->disable_plane = i9xx_disable_primary_plane;
465c120c
MR
13564 }
13565
580503c7
VS
13566 if (INTEL_GEN(dev_priv) >= 9)
13567 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
13568 0, &intel_plane_funcs,
38573dc1
VS
13569 intel_primary_formats, num_formats,
13570 DRM_PLANE_TYPE_PRIMARY,
13571 "plane 1%c", pipe_name(pipe));
9beb5fea 13572 else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
580503c7
VS
13573 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
13574 0, &intel_plane_funcs,
38573dc1
VS
13575 intel_primary_formats, num_formats,
13576 DRM_PLANE_TYPE_PRIMARY,
13577 "primary %c", pipe_name(pipe));
13578 else
580503c7
VS
13579 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
13580 0, &intel_plane_funcs,
38573dc1
VS
13581 intel_primary_formats, num_formats,
13582 DRM_PLANE_TYPE_PRIMARY,
13583 "plane %c", plane_name(primary->plane));
fca0ce2a
VS
13584 if (ret)
13585 goto fail;
48404c1e 13586
5481e27f 13587 if (INTEL_GEN(dev_priv) >= 9) {
93ca7e00
VS
13588 supported_rotations =
13589 DRM_ROTATE_0 | DRM_ROTATE_90 |
13590 DRM_ROTATE_180 | DRM_ROTATE_270;
4ea7be2b
VS
13591 } else if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
13592 supported_rotations =
13593 DRM_ROTATE_0 | DRM_ROTATE_180 |
13594 DRM_REFLECT_X;
5481e27f 13595 } else if (INTEL_GEN(dev_priv) >= 4) {
93ca7e00
VS
13596 supported_rotations =
13597 DRM_ROTATE_0 | DRM_ROTATE_180;
13598 } else {
13599 supported_rotations = DRM_ROTATE_0;
13600 }
13601
5481e27f 13602 if (INTEL_GEN(dev_priv) >= 4)
93ca7e00
VS
13603 drm_plane_create_rotation_property(&primary->base,
13604 DRM_ROTATE_0,
13605 supported_rotations);
48404c1e 13606
ea2c67bb
MR
13607 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13608
b079bd17 13609 return primary;
fca0ce2a
VS
13610
13611fail:
13612 kfree(state);
13613 kfree(primary);
13614
b079bd17 13615 return ERR_PTR(ret);
465c120c
MR
13616}
13617
3d7d6510 13618static int
852e787c 13619intel_check_cursor_plane(struct drm_plane *plane,
061e4b8d 13620 struct intel_crtc_state *crtc_state,
852e787c 13621 struct intel_plane_state *state)
3d7d6510 13622{
2b875c22 13623 struct drm_framebuffer *fb = state->base.fb;
757f9a3e 13624 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
b29ec92c 13625 enum pipe pipe = to_intel_plane(plane)->pipe;
757f9a3e
GP
13626 unsigned stride;
13627 int ret;
3d7d6510 13628
f8856a44
VS
13629 ret = drm_plane_helper_check_state(&state->base,
13630 &state->clip,
13631 DRM_PLANE_HELPER_NO_SCALING,
13632 DRM_PLANE_HELPER_NO_SCALING,
13633 true, true);
757f9a3e
GP
13634 if (ret)
13635 return ret;
13636
757f9a3e
GP
13637 /* if we want to turn off the cursor ignore width and height */
13638 if (!obj)
da20eabd 13639 return 0;
757f9a3e 13640
757f9a3e 13641 /* Check for which cursor types we support */
50a0bc90
TU
13642 if (!cursor_size_ok(to_i915(plane->dev), state->base.crtc_w,
13643 state->base.crtc_h)) {
ea2c67bb
MR
13644 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
13645 state->base.crtc_w, state->base.crtc_h);
757f9a3e
GP
13646 return -EINVAL;
13647 }
13648
ea2c67bb
MR
13649 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
13650 if (obj->base.size < stride * state->base.crtc_h) {
757f9a3e
GP
13651 DRM_DEBUG_KMS("buffer is too small\n");
13652 return -ENOMEM;
13653 }
13654
bae781b2 13655 if (fb->modifier != DRM_FORMAT_MOD_NONE) {
757f9a3e 13656 DRM_DEBUG_KMS("cursor cannot be tiled\n");
da20eabd 13657 return -EINVAL;
32b7eeec
MR
13658 }
13659
b29ec92c
VS
13660 /*
13661 * There's something wrong with the cursor on CHV pipe C.
13662 * If it straddles the left edge of the screen then
13663 * moving it away from the edge or disabling it often
13664 * results in a pipe underrun, and often that can lead to
13665 * dead pipe (constant underrun reported, and it scans
13666 * out just a solid color). To recover from that, the
13667 * display power well must be turned off and on again.
13668 * Refuse the put the cursor into that compromised position.
13669 */
920a14b2 13670 if (IS_CHERRYVIEW(to_i915(plane->dev)) && pipe == PIPE_C &&
936e71e3 13671 state->base.visible && state->base.crtc_x < 0) {
b29ec92c
VS
13672 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
13673 return -EINVAL;
13674 }
13675
da20eabd 13676 return 0;
852e787c 13677}
3d7d6510 13678
a8ad0d8e
ML
13679static void
13680intel_disable_cursor_plane(struct drm_plane *plane,
7fabf5ef 13681 struct drm_crtc *crtc)
a8ad0d8e 13682{
f2858021
ML
13683 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13684
13685 intel_crtc->cursor_addr = 0;
55a08b3f 13686 intel_crtc_update_cursor(crtc, NULL);
a8ad0d8e
ML
13687}
13688
f4a2cf29 13689static void
55a08b3f
ML
13690intel_update_cursor_plane(struct drm_plane *plane,
13691 const struct intel_crtc_state *crtc_state,
13692 const struct intel_plane_state *state)
852e787c 13693{
55a08b3f
ML
13694 struct drm_crtc *crtc = crtc_state->base.crtc;
13695 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b7f05d4a 13696 struct drm_i915_private *dev_priv = to_i915(plane->dev);
2b875c22 13697 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
a912f12f 13698 uint32_t addr;
852e787c 13699
f4a2cf29 13700 if (!obj)
a912f12f 13701 addr = 0;
b7f05d4a 13702 else if (!INTEL_INFO(dev_priv)->cursor_needs_physical)
be1e3415 13703 addr = intel_plane_ggtt_offset(state);
f4a2cf29 13704 else
a912f12f 13705 addr = obj->phys_handle->busaddr;
852e787c 13706
a912f12f 13707 intel_crtc->cursor_addr = addr;
55a08b3f 13708 intel_crtc_update_cursor(crtc, state);
852e787c
GP
13709}
13710
b079bd17 13711static struct intel_plane *
580503c7 13712intel_cursor_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
3d7d6510 13713{
fca0ce2a
VS
13714 struct intel_plane *cursor = NULL;
13715 struct intel_plane_state *state = NULL;
13716 int ret;
3d7d6510
MR
13717
13718 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
b079bd17
VS
13719 if (!cursor) {
13720 ret = -ENOMEM;
fca0ce2a 13721 goto fail;
b079bd17 13722 }
3d7d6510 13723
8e7d688b 13724 state = intel_create_plane_state(&cursor->base);
b079bd17
VS
13725 if (!state) {
13726 ret = -ENOMEM;
fca0ce2a 13727 goto fail;
b079bd17
VS
13728 }
13729
8e7d688b 13730 cursor->base.state = &state->base;
ea2c67bb 13731
3d7d6510
MR
13732 cursor->can_scale = false;
13733 cursor->max_downscale = 1;
13734 cursor->pipe = pipe;
13735 cursor->plane = pipe;
b14e5848 13736 cursor->id = PLANE_CURSOR;
a9ff8714 13737 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
c59cb179 13738 cursor->check_plane = intel_check_cursor_plane;
55a08b3f 13739 cursor->update_plane = intel_update_cursor_plane;
a8ad0d8e 13740 cursor->disable_plane = intel_disable_cursor_plane;
3d7d6510 13741
580503c7 13742 ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
f79f2692 13743 0, &intel_cursor_plane_funcs,
fca0ce2a
VS
13744 intel_cursor_formats,
13745 ARRAY_SIZE(intel_cursor_formats),
38573dc1
VS
13746 DRM_PLANE_TYPE_CURSOR,
13747 "cursor %c", pipe_name(pipe));
fca0ce2a
VS
13748 if (ret)
13749 goto fail;
4398ad45 13750
5481e27f 13751 if (INTEL_GEN(dev_priv) >= 4)
93ca7e00
VS
13752 drm_plane_create_rotation_property(&cursor->base,
13753 DRM_ROTATE_0,
13754 DRM_ROTATE_0 |
13755 DRM_ROTATE_180);
4398ad45 13756
580503c7 13757 if (INTEL_GEN(dev_priv) >= 9)
af99ceda
CK
13758 state->scaler_id = -1;
13759
ea2c67bb
MR
13760 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
13761
b079bd17 13762 return cursor;
fca0ce2a
VS
13763
13764fail:
13765 kfree(state);
13766 kfree(cursor);
13767
b079bd17 13768 return ERR_PTR(ret);
3d7d6510
MR
13769}
13770
1c74eeaf
NM
13771static void intel_crtc_init_scalers(struct intel_crtc *crtc,
13772 struct intel_crtc_state *crtc_state)
549e2bfb 13773{
65edccce
VS
13774 struct intel_crtc_scaler_state *scaler_state =
13775 &crtc_state->scaler_state;
1c74eeaf 13776 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
549e2bfb 13777 int i;
549e2bfb 13778
1c74eeaf
NM
13779 crtc->num_scalers = dev_priv->info.num_scalers[crtc->pipe];
13780 if (!crtc->num_scalers)
13781 return;
13782
65edccce
VS
13783 for (i = 0; i < crtc->num_scalers; i++) {
13784 struct intel_scaler *scaler = &scaler_state->scalers[i];
13785
13786 scaler->in_use = 0;
13787 scaler->mode = PS_SCALER_MODE_DYN;
549e2bfb
CK
13788 }
13789
13790 scaler_state->scaler_id = -1;
13791}
13792
5ab0d85b 13793static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
79e53945
JB
13794{
13795 struct intel_crtc *intel_crtc;
f5de6e07 13796 struct intel_crtc_state *crtc_state = NULL;
b079bd17
VS
13797 struct intel_plane *primary = NULL;
13798 struct intel_plane *cursor = NULL;
a81d6fa0 13799 int sprite, ret;
79e53945 13800
955382f3 13801 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
b079bd17
VS
13802 if (!intel_crtc)
13803 return -ENOMEM;
79e53945 13804
f5de6e07 13805 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
b079bd17
VS
13806 if (!crtc_state) {
13807 ret = -ENOMEM;
f5de6e07 13808 goto fail;
b079bd17 13809 }
550acefd
ACO
13810 intel_crtc->config = crtc_state;
13811 intel_crtc->base.state = &crtc_state->base;
07878248 13812 crtc_state->base.crtc = &intel_crtc->base;
f5de6e07 13813
580503c7 13814 primary = intel_primary_plane_create(dev_priv, pipe);
b079bd17
VS
13815 if (IS_ERR(primary)) {
13816 ret = PTR_ERR(primary);
3d7d6510 13817 goto fail;
b079bd17 13818 }
d97d7b48 13819 intel_crtc->plane_ids_mask |= BIT(primary->id);
3d7d6510 13820
a81d6fa0 13821 for_each_sprite(dev_priv, pipe, sprite) {
b079bd17
VS
13822 struct intel_plane *plane;
13823
580503c7 13824 plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
d2b2cbce 13825 if (IS_ERR(plane)) {
b079bd17
VS
13826 ret = PTR_ERR(plane);
13827 goto fail;
13828 }
d97d7b48 13829 intel_crtc->plane_ids_mask |= BIT(plane->id);
a81d6fa0
VS
13830 }
13831
580503c7 13832 cursor = intel_cursor_plane_create(dev_priv, pipe);
d2b2cbce 13833 if (IS_ERR(cursor)) {
b079bd17 13834 ret = PTR_ERR(cursor);
3d7d6510 13835 goto fail;
b079bd17 13836 }
d97d7b48 13837 intel_crtc->plane_ids_mask |= BIT(cursor->id);
3d7d6510 13838
5ab0d85b 13839 ret = drm_crtc_init_with_planes(&dev_priv->drm, &intel_crtc->base,
b079bd17
VS
13840 &primary->base, &cursor->base,
13841 &intel_crtc_funcs,
4d5d72b7 13842 "pipe %c", pipe_name(pipe));
3d7d6510
MR
13843 if (ret)
13844 goto fail;
79e53945 13845
80824003 13846 intel_crtc->pipe = pipe;
e3c566df 13847 intel_crtc->plane = primary->plane;
80824003 13848
4b0e333e
CW
13849 intel_crtc->cursor_base = ~0;
13850 intel_crtc->cursor_cntl = ~0;
dc41c154 13851 intel_crtc->cursor_size = ~0;
8d7849db 13852
852eb00d
VS
13853 intel_crtc->wm.cxsr_allowed = true;
13854
1c74eeaf
NM
13855 /* initialize shared scalers */
13856 intel_crtc_init_scalers(intel_crtc, crtc_state);
13857
22fd0fab
JB
13858 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
13859 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
e2af48c6
VS
13860 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = intel_crtc;
13861 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = intel_crtc;
22fd0fab 13862
79e53945 13863 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
87b6b101 13864
8563b1e8
LL
13865 intel_color_init(&intel_crtc->base);
13866
87b6b101 13867 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
b079bd17
VS
13868
13869 return 0;
3d7d6510
MR
13870
13871fail:
b079bd17
VS
13872 /*
13873 * drm_mode_config_cleanup() will free up any
13874 * crtcs/planes already initialized.
13875 */
f5de6e07 13876 kfree(crtc_state);
3d7d6510 13877 kfree(intel_crtc);
b079bd17
VS
13878
13879 return ret;
79e53945
JB
13880}
13881
752aa88a
JB
13882enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
13883{
13884 struct drm_encoder *encoder = connector->base.encoder;
6e9f798d 13885 struct drm_device *dev = connector->base.dev;
752aa88a 13886
51fd371b 13887 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
752aa88a 13888
d3babd3f 13889 if (!encoder || WARN_ON(!encoder->crtc))
752aa88a
JB
13890 return INVALID_PIPE;
13891
13892 return to_intel_crtc(encoder->crtc)->pipe;
13893}
13894
08d7b3d1 13895int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 13896 struct drm_file *file)
08d7b3d1 13897{
08d7b3d1 13898 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7707e653 13899 struct drm_crtc *drmmode_crtc;
c05422d5 13900 struct intel_crtc *crtc;
08d7b3d1 13901
7707e653 13902 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
71240ed2 13903 if (!drmmode_crtc)
3f2c2057 13904 return -ENOENT;
08d7b3d1 13905
7707e653 13906 crtc = to_intel_crtc(drmmode_crtc);
c05422d5 13907 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 13908
c05422d5 13909 return 0;
08d7b3d1
CW
13910}
13911
66a9278e 13912static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 13913{
66a9278e
DV
13914 struct drm_device *dev = encoder->base.dev;
13915 struct intel_encoder *source_encoder;
79e53945 13916 int index_mask = 0;
79e53945
JB
13917 int entry = 0;
13918
b2784e15 13919 for_each_intel_encoder(dev, source_encoder) {
bc079e8b 13920 if (encoders_cloneable(encoder, source_encoder))
66a9278e
DV
13921 index_mask |= (1 << entry);
13922
79e53945
JB
13923 entry++;
13924 }
4ef69c7a 13925
79e53945
JB
13926 return index_mask;
13927}
13928
646d5772 13929static bool has_edp_a(struct drm_i915_private *dev_priv)
4d302442 13930{
646d5772 13931 if (!IS_MOBILE(dev_priv))
4d302442
CW
13932 return false;
13933
13934 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
13935 return false;
13936
5db94019 13937 if (IS_GEN5(dev_priv) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
13938 return false;
13939
13940 return true;
13941}
13942
6315b5d3 13943static bool intel_crt_present(struct drm_i915_private *dev_priv)
84b4e042 13944{
6315b5d3 13945 if (INTEL_GEN(dev_priv) >= 9)
884497ed
DL
13946 return false;
13947
50a0bc90 13948 if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
84b4e042
JB
13949 return false;
13950
920a14b2 13951 if (IS_CHERRYVIEW(dev_priv))
84b4e042
JB
13952 return false;
13953
4f8036a2
TU
13954 if (HAS_PCH_LPT_H(dev_priv) &&
13955 I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
65e472e4
VS
13956 return false;
13957
70ac54d0 13958 /* DDI E can't be used if DDI A requires 4 lanes */
4f8036a2 13959 if (HAS_DDI(dev_priv) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
70ac54d0
VS
13960 return false;
13961
e4abb733 13962 if (!dev_priv->vbt.int_crt_support)
84b4e042
JB
13963 return false;
13964
13965 return true;
13966}
13967
8090ba8c
ID
13968void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
13969{
13970 int pps_num;
13971 int pps_idx;
13972
13973 if (HAS_DDI(dev_priv))
13974 return;
13975 /*
13976 * This w/a is needed at least on CPT/PPT, but to be sure apply it
13977 * everywhere where registers can be write protected.
13978 */
13979 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
13980 pps_num = 2;
13981 else
13982 pps_num = 1;
13983
13984 for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
13985 u32 val = I915_READ(PP_CONTROL(pps_idx));
13986
13987 val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
13988 I915_WRITE(PP_CONTROL(pps_idx), val);
13989 }
13990}
13991
44cb734c
ID
13992static void intel_pps_init(struct drm_i915_private *dev_priv)
13993{
cc3f90f0 13994 if (HAS_PCH_SPLIT(dev_priv) || IS_GEN9_LP(dev_priv))
44cb734c
ID
13995 dev_priv->pps_mmio_base = PCH_PPS_BASE;
13996 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
13997 dev_priv->pps_mmio_base = VLV_PPS_BASE;
13998 else
13999 dev_priv->pps_mmio_base = PPS_BASE;
8090ba8c
ID
14000
14001 intel_pps_unlock_regs_wa(dev_priv);
44cb734c
ID
14002}
14003
c39055b0 14004static void intel_setup_outputs(struct drm_i915_private *dev_priv)
79e53945 14005{
4ef69c7a 14006 struct intel_encoder *encoder;
cb0953d7 14007 bool dpd_is_edp = false;
79e53945 14008
44cb734c
ID
14009 intel_pps_init(dev_priv);
14010
97a824e1
ID
14011 /*
14012 * intel_edp_init_connector() depends on this completing first, to
14013 * prevent the registeration of both eDP and LVDS and the incorrect
14014 * sharing of the PPS.
14015 */
c39055b0 14016 intel_lvds_init(dev_priv);
79e53945 14017
6315b5d3 14018 if (intel_crt_present(dev_priv))
c39055b0 14019 intel_crt_init(dev_priv);
cb0953d7 14020
cc3f90f0 14021 if (IS_GEN9_LP(dev_priv)) {
c776eb2e
VK
14022 /*
14023 * FIXME: Broxton doesn't support port detection via the
14024 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14025 * detect the ports.
14026 */
c39055b0
ACO
14027 intel_ddi_init(dev_priv, PORT_A);
14028 intel_ddi_init(dev_priv, PORT_B);
14029 intel_ddi_init(dev_priv, PORT_C);
c6c794a2 14030
c39055b0 14031 intel_dsi_init(dev_priv);
4f8036a2 14032 } else if (HAS_DDI(dev_priv)) {
0e72a5b5
ED
14033 int found;
14034
de31facd
JB
14035 /*
14036 * Haswell uses DDI functions to detect digital outputs.
14037 * On SKL pre-D0 the strap isn't connected, so we assume
14038 * it's there.
14039 */
77179400 14040 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
de31facd 14041 /* WaIgnoreDDIAStrap: skl */
b976dc53 14042 if (found || IS_GEN9_BC(dev_priv))
c39055b0 14043 intel_ddi_init(dev_priv, PORT_A);
0e72a5b5
ED
14044
14045 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14046 * register */
14047 found = I915_READ(SFUSE_STRAP);
14048
14049 if (found & SFUSE_STRAP_DDIB_DETECTED)
c39055b0 14050 intel_ddi_init(dev_priv, PORT_B);
0e72a5b5 14051 if (found & SFUSE_STRAP_DDIC_DETECTED)
c39055b0 14052 intel_ddi_init(dev_priv, PORT_C);
0e72a5b5 14053 if (found & SFUSE_STRAP_DDID_DETECTED)
c39055b0 14054 intel_ddi_init(dev_priv, PORT_D);
2800e4c2
RV
14055 /*
14056 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14057 */
b976dc53 14058 if (IS_GEN9_BC(dev_priv) &&
2800e4c2
RV
14059 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14060 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14061 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
c39055b0 14062 intel_ddi_init(dev_priv, PORT_E);
2800e4c2 14063
6e266956 14064 } else if (HAS_PCH_SPLIT(dev_priv)) {
cb0953d7 14065 int found;
dd11bc10 14066 dpd_is_edp = intel_dp_is_edp(dev_priv, PORT_D);
270b3042 14067
646d5772 14068 if (has_edp_a(dev_priv))
c39055b0 14069 intel_dp_init(dev_priv, DP_A, PORT_A);
cb0953d7 14070
dc0fa718 14071 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 14072 /* PCH SDVOB multiplex with HDMIB */
c39055b0 14073 found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
30ad48b7 14074 if (!found)
c39055b0 14075 intel_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
5eb08b69 14076 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
c39055b0 14077 intel_dp_init(dev_priv, PCH_DP_B, PORT_B);
30ad48b7
ZW
14078 }
14079
dc0fa718 14080 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
c39055b0 14081 intel_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
30ad48b7 14082
dc0fa718 14083 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
c39055b0 14084 intel_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
30ad48b7 14085
5eb08b69 14086 if (I915_READ(PCH_DP_C) & DP_DETECTED)
c39055b0 14087 intel_dp_init(dev_priv, PCH_DP_C, PORT_C);
5eb08b69 14088
270b3042 14089 if (I915_READ(PCH_DP_D) & DP_DETECTED)
c39055b0 14090 intel_dp_init(dev_priv, PCH_DP_D, PORT_D);
920a14b2 14091 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
22f35042 14092 bool has_edp, has_port;
457c52d8 14093
e17ac6db
VS
14094 /*
14095 * The DP_DETECTED bit is the latched state of the DDC
14096 * SDA pin at boot. However since eDP doesn't require DDC
14097 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14098 * eDP ports may have been muxed to an alternate function.
14099 * Thus we can't rely on the DP_DETECTED bit alone to detect
14100 * eDP ports. Consult the VBT as well as DP_DETECTED to
14101 * detect eDP ports.
22f35042
VS
14102 *
14103 * Sadly the straps seem to be missing sometimes even for HDMI
14104 * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
14105 * and VBT for the presence of the port. Additionally we can't
14106 * trust the port type the VBT declares as we've seen at least
14107 * HDMI ports that the VBT claim are DP or eDP.
e17ac6db 14108 */
dd11bc10 14109 has_edp = intel_dp_is_edp(dev_priv, PORT_B);
22f35042
VS
14110 has_port = intel_bios_is_port_present(dev_priv, PORT_B);
14111 if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
c39055b0 14112 has_edp &= intel_dp_init(dev_priv, VLV_DP_B, PORT_B);
22f35042 14113 if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
c39055b0 14114 intel_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
585a94b8 14115
dd11bc10 14116 has_edp = intel_dp_is_edp(dev_priv, PORT_C);
22f35042
VS
14117 has_port = intel_bios_is_port_present(dev_priv, PORT_C);
14118 if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
c39055b0 14119 has_edp &= intel_dp_init(dev_priv, VLV_DP_C, PORT_C);
22f35042 14120 if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
c39055b0 14121 intel_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
19c03924 14122
920a14b2 14123 if (IS_CHERRYVIEW(dev_priv)) {
22f35042
VS
14124 /*
14125 * eDP not supported on port D,
14126 * so no need to worry about it
14127 */
14128 has_port = intel_bios_is_port_present(dev_priv, PORT_D);
14129 if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
c39055b0 14130 intel_dp_init(dev_priv, CHV_DP_D, PORT_D);
22f35042 14131 if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
c39055b0 14132 intel_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
9418c1f1
VS
14133 }
14134
c39055b0 14135 intel_dsi_init(dev_priv);
5db94019 14136 } else if (!IS_GEN2(dev_priv) && !IS_PINEVIEW(dev_priv)) {
27185ae1 14137 bool found = false;
7d57382e 14138
e2debe91 14139 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14140 DRM_DEBUG_KMS("probing SDVOB\n");
c39055b0 14141 found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
9beb5fea 14142 if (!found && IS_G4X(dev_priv)) {
b01f2c3a 14143 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
c39055b0 14144 intel_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
b01f2c3a 14145 }
27185ae1 14146
9beb5fea 14147 if (!found && IS_G4X(dev_priv))
c39055b0 14148 intel_dp_init(dev_priv, DP_B, PORT_B);
725e30ad 14149 }
13520b05
KH
14150
14151 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 14152
e2debe91 14153 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14154 DRM_DEBUG_KMS("probing SDVOC\n");
c39055b0 14155 found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
b01f2c3a 14156 }
27185ae1 14157
e2debe91 14158 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 14159
9beb5fea 14160 if (IS_G4X(dev_priv)) {
b01f2c3a 14161 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
c39055b0 14162 intel_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
b01f2c3a 14163 }
9beb5fea 14164 if (IS_G4X(dev_priv))
c39055b0 14165 intel_dp_init(dev_priv, DP_C, PORT_C);
725e30ad 14166 }
27185ae1 14167
9beb5fea 14168 if (IS_G4X(dev_priv) && (I915_READ(DP_D) & DP_DETECTED))
c39055b0 14169 intel_dp_init(dev_priv, DP_D, PORT_D);
5db94019 14170 } else if (IS_GEN2(dev_priv))
c39055b0 14171 intel_dvo_init(dev_priv);
79e53945 14172
56b857a5 14173 if (SUPPORTS_TV(dev_priv))
c39055b0 14174 intel_tv_init(dev_priv);
79e53945 14175
c39055b0 14176 intel_psr_init(dev_priv);
7c8f8a70 14177
c39055b0 14178 for_each_intel_encoder(&dev_priv->drm, encoder) {
4ef69c7a
CW
14179 encoder->base.possible_crtcs = encoder->crtc_mask;
14180 encoder->base.possible_clones =
66a9278e 14181 intel_encoder_clones(encoder);
79e53945 14182 }
47356eb6 14183
c39055b0 14184 intel_init_pch_refclk(dev_priv);
270b3042 14185
c39055b0 14186 drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
79e53945
JB
14187}
14188
14189static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14190{
14191 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 14192
ef2d633e 14193 drm_framebuffer_cleanup(fb);
70001cd2 14194
dd689287
CW
14195 i915_gem_object_lock(intel_fb->obj);
14196 WARN_ON(!intel_fb->obj->framebuffer_references--);
14197 i915_gem_object_unlock(intel_fb->obj);
14198
f8c417cd 14199 i915_gem_object_put(intel_fb->obj);
70001cd2 14200
79e53945
JB
14201 kfree(intel_fb);
14202}
14203
14204static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 14205 struct drm_file *file,
79e53945
JB
14206 unsigned int *handle)
14207{
14208 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 14209 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 14210
cc917ab4
CW
14211 if (obj->userptr.mm) {
14212 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14213 return -EINVAL;
14214 }
14215
05394f39 14216 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
14217}
14218
86c98588
RV
14219static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14220 struct drm_file *file,
14221 unsigned flags, unsigned color,
14222 struct drm_clip_rect *clips,
14223 unsigned num_clips)
14224{
5a97bcc6 14225 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
86c98588 14226
5a97bcc6 14227 i915_gem_object_flush_if_display(obj);
d59b21ec 14228 intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
86c98588
RV
14229
14230 return 0;
14231}
14232
79e53945
JB
14233static const struct drm_framebuffer_funcs intel_fb_funcs = {
14234 .destroy = intel_user_framebuffer_destroy,
14235 .create_handle = intel_user_framebuffer_create_handle,
86c98588 14236 .dirty = intel_user_framebuffer_dirty,
79e53945
JB
14237};
14238
b321803d 14239static
920a14b2
TU
14240u32 intel_fb_pitch_limit(struct drm_i915_private *dev_priv,
14241 uint64_t fb_modifier, uint32_t pixel_format)
b321803d 14242{
24dbf51a 14243 u32 gen = INTEL_GEN(dev_priv);
b321803d
DL
14244
14245 if (gen >= 9) {
ac484963
VS
14246 int cpp = drm_format_plane_cpp(pixel_format, 0);
14247
b321803d
DL
14248 /* "The stride in bytes must not exceed the of the size of 8K
14249 * pixels and 32K bytes."
14250 */
ac484963 14251 return min(8192 * cpp, 32768);
6401c37d 14252 } else if (gen >= 5 && !HAS_GMCH_DISPLAY(dev_priv)) {
b321803d
DL
14253 return 32*1024;
14254 } else if (gen >= 4) {
14255 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14256 return 16*1024;
14257 else
14258 return 32*1024;
14259 } else if (gen >= 3) {
14260 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14261 return 8*1024;
14262 else
14263 return 16*1024;
14264 } else {
14265 /* XXX DSPC is limited to 4k tiled */
14266 return 8*1024;
14267 }
14268}
14269
24dbf51a
CW
14270static int intel_framebuffer_init(struct intel_framebuffer *intel_fb,
14271 struct drm_i915_gem_object *obj,
14272 struct drm_mode_fb_cmd2 *mode_cmd)
79e53945 14273{
24dbf51a 14274 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
b3c11ac2 14275 struct drm_format_name_buf format_name;
dd689287
CW
14276 u32 pitch_limit, stride_alignment;
14277 unsigned int tiling, stride;
24dbf51a 14278 int ret = -EINVAL;
79e53945 14279
dd689287
CW
14280 i915_gem_object_lock(obj);
14281 obj->framebuffer_references++;
14282 tiling = i915_gem_object_get_tiling(obj);
14283 stride = i915_gem_object_get_stride(obj);
14284 i915_gem_object_unlock(obj);
dd4916c5 14285
2a80eada 14286 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
c2ff7370
VS
14287 /*
14288 * If there's a fence, enforce that
14289 * the fb modifier and tiling mode match.
14290 */
14291 if (tiling != I915_TILING_NONE &&
14292 tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
2a80eada 14293 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
24dbf51a 14294 goto err;
2a80eada
DV
14295 }
14296 } else {
c2ff7370 14297 if (tiling == I915_TILING_X) {
2a80eada 14298 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
c2ff7370 14299 } else if (tiling == I915_TILING_Y) {
2a80eada 14300 DRM_DEBUG("No Y tiling for legacy addfb\n");
24dbf51a 14301 goto err;
2a80eada
DV
14302 }
14303 }
14304
9a8f0a12
TU
14305 /* Passed in modifier sanity checking. */
14306 switch (mode_cmd->modifier[0]) {
14307 case I915_FORMAT_MOD_Y_TILED:
14308 case I915_FORMAT_MOD_Yf_TILED:
6315b5d3 14309 if (INTEL_GEN(dev_priv) < 9) {
9a8f0a12
TU
14310 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14311 mode_cmd->modifier[0]);
24dbf51a 14312 goto err;
9a8f0a12
TU
14313 }
14314 case DRM_FORMAT_MOD_NONE:
14315 case I915_FORMAT_MOD_X_TILED:
14316 break;
14317 default:
c0f40428
JB
14318 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14319 mode_cmd->modifier[0]);
24dbf51a 14320 goto err;
c16ed4be 14321 }
57cd6508 14322
c2ff7370
VS
14323 /*
14324 * gen2/3 display engine uses the fence if present,
14325 * so the tiling mode must match the fb modifier exactly.
14326 */
14327 if (INTEL_INFO(dev_priv)->gen < 4 &&
14328 tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
14329 DRM_DEBUG("tiling_mode must match fb modifier exactly on gen2/3\n");
9aceb5c1 14330 goto err;
c2ff7370
VS
14331 }
14332
7b49f948
VS
14333 stride_alignment = intel_fb_stride_alignment(dev_priv,
14334 mode_cmd->modifier[0],
b321803d
DL
14335 mode_cmd->pixel_format);
14336 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14337 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14338 mode_cmd->pitches[0], stride_alignment);
24dbf51a 14339 goto err;
c16ed4be 14340 }
57cd6508 14341
920a14b2 14342 pitch_limit = intel_fb_pitch_limit(dev_priv, mode_cmd->modifier[0],
b321803d 14343 mode_cmd->pixel_format);
a35cdaa0 14344 if (mode_cmd->pitches[0] > pitch_limit) {
b321803d
DL
14345 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14346 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
2a80eada 14347 "tiled" : "linear",
a35cdaa0 14348 mode_cmd->pitches[0], pitch_limit);
24dbf51a 14349 goto err;
c16ed4be 14350 }
5d7bd705 14351
c2ff7370
VS
14352 /*
14353 * If there's a fence, enforce that
14354 * the fb pitch and fence stride match.
14355 */
dd689287 14356 if (tiling != I915_TILING_NONE && mode_cmd->pitches[0] != stride) {
c16ed4be 14357 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
dd689287 14358 mode_cmd->pitches[0], stride);
24dbf51a 14359 goto err;
c16ed4be 14360 }
5d7bd705 14361
57779d06 14362 /* Reject formats not supported by any plane early. */
308e5bcb 14363 switch (mode_cmd->pixel_format) {
57779d06 14364 case DRM_FORMAT_C8:
04b3924d
VS
14365 case DRM_FORMAT_RGB565:
14366 case DRM_FORMAT_XRGB8888:
14367 case DRM_FORMAT_ARGB8888:
57779d06
VS
14368 break;
14369 case DRM_FORMAT_XRGB1555:
6315b5d3 14370 if (INTEL_GEN(dev_priv) > 3) {
b3c11ac2
EE
14371 DRM_DEBUG("unsupported pixel format: %s\n",
14372 drm_get_format_name(mode_cmd->pixel_format, &format_name));
9aceb5c1 14373 goto err;
c16ed4be 14374 }
57779d06 14375 break;
57779d06 14376 case DRM_FORMAT_ABGR8888:
920a14b2 14377 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
6315b5d3 14378 INTEL_GEN(dev_priv) < 9) {
b3c11ac2
EE
14379 DRM_DEBUG("unsupported pixel format: %s\n",
14380 drm_get_format_name(mode_cmd->pixel_format, &format_name));
9aceb5c1 14381 goto err;
6c0fd451
DL
14382 }
14383 break;
14384 case DRM_FORMAT_XBGR8888:
04b3924d 14385 case DRM_FORMAT_XRGB2101010:
57779d06 14386 case DRM_FORMAT_XBGR2101010:
6315b5d3 14387 if (INTEL_GEN(dev_priv) < 4) {
b3c11ac2
EE
14388 DRM_DEBUG("unsupported pixel format: %s\n",
14389 drm_get_format_name(mode_cmd->pixel_format, &format_name));
9aceb5c1 14390 goto err;
c16ed4be 14391 }
b5626747 14392 break;
7531208b 14393 case DRM_FORMAT_ABGR2101010:
920a14b2 14394 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
b3c11ac2
EE
14395 DRM_DEBUG("unsupported pixel format: %s\n",
14396 drm_get_format_name(mode_cmd->pixel_format, &format_name));
9aceb5c1 14397 goto err;
7531208b
DL
14398 }
14399 break;
04b3924d
VS
14400 case DRM_FORMAT_YUYV:
14401 case DRM_FORMAT_UYVY:
14402 case DRM_FORMAT_YVYU:
14403 case DRM_FORMAT_VYUY:
6315b5d3 14404 if (INTEL_GEN(dev_priv) < 5) {
b3c11ac2
EE
14405 DRM_DEBUG("unsupported pixel format: %s\n",
14406 drm_get_format_name(mode_cmd->pixel_format, &format_name));
9aceb5c1 14407 goto err;
c16ed4be 14408 }
57cd6508
CW
14409 break;
14410 default:
b3c11ac2
EE
14411 DRM_DEBUG("unsupported pixel format: %s\n",
14412 drm_get_format_name(mode_cmd->pixel_format, &format_name));
9aceb5c1 14413 goto err;
57cd6508
CW
14414 }
14415
90f9a336
VS
14416 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14417 if (mode_cmd->offsets[0] != 0)
24dbf51a 14418 goto err;
90f9a336 14419
24dbf51a
CW
14420 drm_helper_mode_fill_fb_struct(&dev_priv->drm,
14421 &intel_fb->base, mode_cmd);
c7d73f6a
DV
14422 intel_fb->obj = obj;
14423
6687c906
VS
14424 ret = intel_fill_fb_info(dev_priv, &intel_fb->base);
14425 if (ret)
9aceb5c1 14426 goto err;
2d7a215f 14427
24dbf51a
CW
14428 ret = drm_framebuffer_init(obj->base.dev,
14429 &intel_fb->base,
14430 &intel_fb_funcs);
79e53945
JB
14431 if (ret) {
14432 DRM_ERROR("framebuffer init failed %d\n", ret);
24dbf51a 14433 goto err;
79e53945
JB
14434 }
14435
79e53945 14436 return 0;
24dbf51a
CW
14437
14438err:
dd689287
CW
14439 i915_gem_object_lock(obj);
14440 obj->framebuffer_references--;
14441 i915_gem_object_unlock(obj);
24dbf51a 14442 return ret;
79e53945
JB
14443}
14444
79e53945
JB
14445static struct drm_framebuffer *
14446intel_user_framebuffer_create(struct drm_device *dev,
14447 struct drm_file *filp,
1eb83451 14448 const struct drm_mode_fb_cmd2 *user_mode_cmd)
79e53945 14449{
dcb1394e 14450 struct drm_framebuffer *fb;
05394f39 14451 struct drm_i915_gem_object *obj;
76dc3769 14452 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
79e53945 14453
03ac0642
CW
14454 obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
14455 if (!obj)
cce13ff7 14456 return ERR_PTR(-ENOENT);
79e53945 14457
24dbf51a 14458 fb = intel_framebuffer_create(obj, &mode_cmd);
dcb1394e 14459 if (IS_ERR(fb))
f0cd5182 14460 i915_gem_object_put(obj);
dcb1394e
LW
14461
14462 return fb;
79e53945
JB
14463}
14464
778e23a9
CW
14465static void intel_atomic_state_free(struct drm_atomic_state *state)
14466{
14467 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
14468
14469 drm_atomic_state_default_release(state);
14470
14471 i915_sw_fence_fini(&intel_state->commit_ready);
14472
14473 kfree(state);
14474}
14475
79e53945 14476static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 14477 .fb_create = intel_user_framebuffer_create,
0632fef6 14478 .output_poll_changed = intel_fbdev_output_poll_changed,
5ee67f1c
MR
14479 .atomic_check = intel_atomic_check,
14480 .atomic_commit = intel_atomic_commit,
de419ab6
ML
14481 .atomic_state_alloc = intel_atomic_state_alloc,
14482 .atomic_state_clear = intel_atomic_state_clear,
778e23a9 14483 .atomic_state_free = intel_atomic_state_free,
79e53945
JB
14484};
14485
88212941
ID
14486/**
14487 * intel_init_display_hooks - initialize the display modesetting hooks
14488 * @dev_priv: device private
14489 */
14490void intel_init_display_hooks(struct drm_i915_private *dev_priv)
e70236a8 14491{
7ff89ca2
VS
14492 intel_init_cdclk_hooks(dev_priv);
14493
88212941 14494 if (INTEL_INFO(dev_priv)->gen >= 9) {
bc8d7dff 14495 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14496 dev_priv->display.get_initial_plane_config =
14497 skylake_get_initial_plane_config;
bc8d7dff
DL
14498 dev_priv->display.crtc_compute_clock =
14499 haswell_crtc_compute_clock;
14500 dev_priv->display.crtc_enable = haswell_crtc_enable;
14501 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 14502 } else if (HAS_DDI(dev_priv)) {
0e8ffe1b 14503 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14504 dev_priv->display.get_initial_plane_config =
14505 ironlake_get_initial_plane_config;
797d0259
ACO
14506 dev_priv->display.crtc_compute_clock =
14507 haswell_crtc_compute_clock;
4f771f10
PZ
14508 dev_priv->display.crtc_enable = haswell_crtc_enable;
14509 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 14510 } else if (HAS_PCH_SPLIT(dev_priv)) {
0e8ffe1b 14511 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
5724dbd1
DL
14512 dev_priv->display.get_initial_plane_config =
14513 ironlake_get_initial_plane_config;
3fb37703
ACO
14514 dev_priv->display.crtc_compute_clock =
14515 ironlake_crtc_compute_clock;
76e5a89c
DV
14516 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14517 dev_priv->display.crtc_disable = ironlake_crtc_disable;
65b3d6a9 14518 } else if (IS_CHERRYVIEW(dev_priv)) {
89b667f8 14519 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14520 dev_priv->display.get_initial_plane_config =
14521 i9xx_get_initial_plane_config;
65b3d6a9
ACO
14522 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
14523 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14524 dev_priv->display.crtc_disable = i9xx_crtc_disable;
14525 } else if (IS_VALLEYVIEW(dev_priv)) {
14526 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14527 dev_priv->display.get_initial_plane_config =
14528 i9xx_get_initial_plane_config;
14529 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
89b667f8
JB
14530 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14531 dev_priv->display.crtc_disable = i9xx_crtc_disable;
19ec6693
ACO
14532 } else if (IS_G4X(dev_priv)) {
14533 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14534 dev_priv->display.get_initial_plane_config =
14535 i9xx_get_initial_plane_config;
14536 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
14537 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14538 dev_priv->display.crtc_disable = i9xx_crtc_disable;
70e8aa21
ACO
14539 } else if (IS_PINEVIEW(dev_priv)) {
14540 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14541 dev_priv->display.get_initial_plane_config =
14542 i9xx_get_initial_plane_config;
14543 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
14544 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14545 dev_priv->display.crtc_disable = i9xx_crtc_disable;
81c97f52 14546 } else if (!IS_GEN2(dev_priv)) {
0e8ffe1b 14547 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14548 dev_priv->display.get_initial_plane_config =
14549 i9xx_get_initial_plane_config;
d6dfee7a 14550 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
76e5a89c
DV
14551 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14552 dev_priv->display.crtc_disable = i9xx_crtc_disable;
81c97f52
ACO
14553 } else {
14554 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14555 dev_priv->display.get_initial_plane_config =
14556 i9xx_get_initial_plane_config;
14557 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
14558 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14559 dev_priv->display.crtc_disable = i9xx_crtc_disable;
f564048e 14560 }
e70236a8 14561
88212941 14562 if (IS_GEN5(dev_priv)) {
3bb11b53 14563 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
88212941 14564 } else if (IS_GEN6(dev_priv)) {
3bb11b53 14565 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
88212941 14566 } else if (IS_IVYBRIDGE(dev_priv)) {
3bb11b53
SJ
14567 /* FIXME: detect B0+ stepping and use auto training */
14568 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
88212941 14569 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
3bb11b53 14570 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
445e780b
VS
14571 }
14572
27082493
L
14573 if (dev_priv->info.gen >= 9)
14574 dev_priv->display.update_crtcs = skl_update_crtcs;
14575 else
14576 dev_priv->display.update_crtcs = intel_update_crtcs;
14577
5a21b665
DV
14578 switch (INTEL_INFO(dev_priv)->gen) {
14579 case 2:
14580 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14581 break;
14582
14583 case 3:
14584 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14585 break;
14586
14587 case 4:
14588 case 5:
14589 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14590 break;
14591
14592 case 6:
14593 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14594 break;
14595 case 7:
14596 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
14597 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14598 break;
14599 case 9:
14600 /* Drop through - unsupported since execlist only. */
14601 default:
14602 /* Default just returns -ENODEV to indicate unsupported */
14603 dev_priv->display.queue_flip = intel_default_queue_flip;
14604 }
e70236a8
JB
14605}
14606
b690e96c
JB
14607/*
14608 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14609 * resume, or other times. This quirk makes sure that's the case for
14610 * affected systems.
14611 */
0206e353 14612static void quirk_pipea_force(struct drm_device *dev)
b690e96c 14613{
fac5e23e 14614 struct drm_i915_private *dev_priv = to_i915(dev);
b690e96c
JB
14615
14616 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
bc0daf48 14617 DRM_INFO("applying pipe a force quirk\n");
b690e96c
JB
14618}
14619
b6b5d049
VS
14620static void quirk_pipeb_force(struct drm_device *dev)
14621{
fac5e23e 14622 struct drm_i915_private *dev_priv = to_i915(dev);
b6b5d049
VS
14623
14624 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14625 DRM_INFO("applying pipe b force quirk\n");
14626}
14627
435793df
KP
14628/*
14629 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14630 */
14631static void quirk_ssc_force_disable(struct drm_device *dev)
14632{
fac5e23e 14633 struct drm_i915_private *dev_priv = to_i915(dev);
435793df 14634 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 14635 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
14636}
14637
4dca20ef 14638/*
5a15ab5b
CE
14639 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14640 * brightness value
4dca20ef
CE
14641 */
14642static void quirk_invert_brightness(struct drm_device *dev)
14643{
fac5e23e 14644 struct drm_i915_private *dev_priv = to_i915(dev);
4dca20ef 14645 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 14646 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
14647}
14648
9c72cc6f
SD
14649/* Some VBT's incorrectly indicate no backlight is present */
14650static void quirk_backlight_present(struct drm_device *dev)
14651{
fac5e23e 14652 struct drm_i915_private *dev_priv = to_i915(dev);
9c72cc6f
SD
14653 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14654 DRM_INFO("applying backlight present quirk\n");
14655}
14656
b690e96c
JB
14657struct intel_quirk {
14658 int device;
14659 int subsystem_vendor;
14660 int subsystem_device;
14661 void (*hook)(struct drm_device *dev);
14662};
14663
5f85f176
EE
14664/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14665struct intel_dmi_quirk {
14666 void (*hook)(struct drm_device *dev);
14667 const struct dmi_system_id (*dmi_id_list)[];
14668};
14669
14670static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14671{
14672 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14673 return 1;
14674}
14675
14676static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14677 {
14678 .dmi_id_list = &(const struct dmi_system_id[]) {
14679 {
14680 .callback = intel_dmi_reverse_brightness,
14681 .ident = "NCR Corporation",
14682 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14683 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14684 },
14685 },
14686 { } /* terminating entry */
14687 },
14688 .hook = quirk_invert_brightness,
14689 },
14690};
14691
c43b5634 14692static struct intel_quirk intel_quirks[] = {
b690e96c
JB
14693 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
14694 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
14695
b690e96c
JB
14696 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
14697 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
14698
5f080c0f
VS
14699 /* 830 needs to leave pipe A & dpll A up */
14700 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
14701
b6b5d049
VS
14702 /* 830 needs to leave pipe B & dpll B up */
14703 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
14704
435793df
KP
14705 /* Lenovo U160 cannot use SSC on LVDS */
14706 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
14707
14708 /* Sony Vaio Y cannot use SSC on LVDS */
14709 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 14710
be505f64
AH
14711 /* Acer Aspire 5734Z must invert backlight brightness */
14712 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14713
14714 /* Acer/eMachines G725 */
14715 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14716
14717 /* Acer/eMachines e725 */
14718 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
14719
14720 /* Acer/Packard Bell NCL20 */
14721 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
14722
14723 /* Acer Aspire 4736Z */
14724 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
14725
14726 /* Acer Aspire 5336 */
14727 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
2e93a1aa
SD
14728
14729 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
14730 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
d4967d8c 14731
dfb3d47b
SD
14732 /* Acer C720 Chromebook (Core i3 4005U) */
14733 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
14734
b2a9601c 14735 /* Apple Macbook 2,1 (Core 2 T7400) */
14736 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
14737
1b9448b0
JN
14738 /* Apple Macbook 4,1 */
14739 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
14740
d4967d8c
SD
14741 /* Toshiba CB35 Chromebook (Celeron 2955U) */
14742 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
724cb06f
SD
14743
14744 /* HP Chromebook 14 (Celeron 2955U) */
14745 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
cf6f0af9
JN
14746
14747 /* Dell Chromebook 11 */
14748 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
9be64eee
JN
14749
14750 /* Dell Chromebook 11 (2015 version) */
14751 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
b690e96c
JB
14752};
14753
14754static void intel_init_quirks(struct drm_device *dev)
14755{
14756 struct pci_dev *d = dev->pdev;
14757 int i;
14758
14759 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
14760 struct intel_quirk *q = &intel_quirks[i];
14761
14762 if (d->device == q->device &&
14763 (d->subsystem_vendor == q->subsystem_vendor ||
14764 q->subsystem_vendor == PCI_ANY_ID) &&
14765 (d->subsystem_device == q->subsystem_device ||
14766 q->subsystem_device == PCI_ANY_ID))
14767 q->hook(dev);
14768 }
5f85f176
EE
14769 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
14770 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
14771 intel_dmi_quirks[i].hook(dev);
14772 }
b690e96c
JB
14773}
14774
9cce37f4 14775/* Disable the VGA plane that we never use */
29b74b7f 14776static void i915_disable_vga(struct drm_i915_private *dev_priv)
9cce37f4 14777{
52a05c30 14778 struct pci_dev *pdev = dev_priv->drm.pdev;
9cce37f4 14779 u8 sr1;
920a14b2 14780 i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
9cce37f4 14781
2b37c616 14782 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
52a05c30 14783 vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 14784 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
14785 sr1 = inb(VGA_SR_DATA);
14786 outb(sr1 | 1<<5, VGA_SR_DATA);
52a05c30 14787 vga_put(pdev, VGA_RSRC_LEGACY_IO);
9cce37f4
JB
14788 udelay(300);
14789
01f5a626 14790 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9cce37f4
JB
14791 POSTING_READ(vga_reg);
14792}
14793
f817586c
DV
14794void intel_modeset_init_hw(struct drm_device *dev)
14795{
fac5e23e 14796 struct drm_i915_private *dev_priv = to_i915(dev);
1a617b77 14797
4c75b940 14798 intel_update_cdclk(dev_priv);
bb0f4aab 14799 dev_priv->cdclk.logical = dev_priv->cdclk.actual = dev_priv->cdclk.hw;
1a617b77 14800
46f16e63 14801 intel_init_clock_gating(dev_priv);
f817586c
DV
14802}
14803
d93c0372
MR
14804/*
14805 * Calculate what we think the watermarks should be for the state we've read
14806 * out of the hardware and then immediately program those watermarks so that
14807 * we ensure the hardware settings match our internal state.
14808 *
14809 * We can calculate what we think WM's should be by creating a duplicate of the
14810 * current state (which was constructed during hardware readout) and running it
14811 * through the atomic check code to calculate new watermark values in the
14812 * state object.
14813 */
14814static void sanitize_watermarks(struct drm_device *dev)
14815{
14816 struct drm_i915_private *dev_priv = to_i915(dev);
14817 struct drm_atomic_state *state;
ccf010fb 14818 struct intel_atomic_state *intel_state;
d93c0372
MR
14819 struct drm_crtc *crtc;
14820 struct drm_crtc_state *cstate;
14821 struct drm_modeset_acquire_ctx ctx;
14822 int ret;
14823 int i;
14824
14825 /* Only supported on platforms that use atomic watermark design */
ed4a6a7c 14826 if (!dev_priv->display.optimize_watermarks)
d93c0372
MR
14827 return;
14828
14829 /*
14830 * We need to hold connection_mutex before calling duplicate_state so
14831 * that the connector loop is protected.
14832 */
14833 drm_modeset_acquire_init(&ctx, 0);
14834retry:
0cd1262d 14835 ret = drm_modeset_lock_all_ctx(dev, &ctx);
d93c0372
MR
14836 if (ret == -EDEADLK) {
14837 drm_modeset_backoff(&ctx);
14838 goto retry;
14839 } else if (WARN_ON(ret)) {
0cd1262d 14840 goto fail;
d93c0372
MR
14841 }
14842
14843 state = drm_atomic_helper_duplicate_state(dev, &ctx);
14844 if (WARN_ON(IS_ERR(state)))
0cd1262d 14845 goto fail;
d93c0372 14846
ccf010fb
ML
14847 intel_state = to_intel_atomic_state(state);
14848
ed4a6a7c
MR
14849 /*
14850 * Hardware readout is the only time we don't want to calculate
14851 * intermediate watermarks (since we don't trust the current
14852 * watermarks).
14853 */
ccf010fb 14854 intel_state->skip_intermediate_wm = true;
ed4a6a7c 14855
d93c0372
MR
14856 ret = intel_atomic_check(dev, state);
14857 if (ret) {
14858 /*
14859 * If we fail here, it means that the hardware appears to be
14860 * programmed in a way that shouldn't be possible, given our
14861 * understanding of watermark requirements. This might mean a
14862 * mistake in the hardware readout code or a mistake in the
14863 * watermark calculations for a given platform. Raise a WARN
14864 * so that this is noticeable.
14865 *
14866 * If this actually happens, we'll have to just leave the
14867 * BIOS-programmed watermarks untouched and hope for the best.
14868 */
14869 WARN(true, "Could not determine valid watermarks for inherited state\n");
b9a1b717 14870 goto put_state;
d93c0372
MR
14871 }
14872
14873 /* Write calculated watermark values back */
d93c0372
MR
14874 for_each_crtc_in_state(state, crtc, cstate, i) {
14875 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
14876
ed4a6a7c 14877 cs->wm.need_postvbl_update = true;
ccf010fb 14878 dev_priv->display.optimize_watermarks(intel_state, cs);
d93c0372
MR
14879 }
14880
b9a1b717 14881put_state:
0853695c 14882 drm_atomic_state_put(state);
0cd1262d 14883fail:
d93c0372
MR
14884 drm_modeset_drop_locks(&ctx);
14885 drm_modeset_acquire_fini(&ctx);
14886}
14887
b079bd17 14888int intel_modeset_init(struct drm_device *dev)
79e53945 14889{
72e96d64
JL
14890 struct drm_i915_private *dev_priv = to_i915(dev);
14891 struct i915_ggtt *ggtt = &dev_priv->ggtt;
8cc87b75 14892 enum pipe pipe;
46f297fb 14893 struct intel_crtc *crtc;
79e53945
JB
14894
14895 drm_mode_config_init(dev);
14896
14897 dev->mode_config.min_width = 0;
14898 dev->mode_config.min_height = 0;
14899
019d96cb
DA
14900 dev->mode_config.preferred_depth = 24;
14901 dev->mode_config.prefer_shadow = 1;
14902
25bab385
TU
14903 dev->mode_config.allow_fb_modifiers = true;
14904
e6ecefaa 14905 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 14906
eb955eee 14907 INIT_WORK(&dev_priv->atomic_helper.free_work,
ba318c61 14908 intel_atomic_helper_free_state_worker);
eb955eee 14909
b690e96c
JB
14910 intel_init_quirks(dev);
14911
62d75df7 14912 intel_init_pm(dev_priv);
1fa61106 14913
b7f05d4a 14914 if (INTEL_INFO(dev_priv)->num_pipes == 0)
b079bd17 14915 return 0;
e3c74757 14916
69f92f67
LW
14917 /*
14918 * There may be no VBT; and if the BIOS enabled SSC we can
14919 * just keep using it to avoid unnecessary flicker. Whereas if the
14920 * BIOS isn't using it, don't assume it will work even if the VBT
14921 * indicates as much.
14922 */
6e266956 14923 if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
69f92f67
LW
14924 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
14925 DREF_SSC1_ENABLE);
14926
14927 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
14928 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
14929 bios_lvds_use_ssc ? "en" : "dis",
14930 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
14931 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
14932 }
14933 }
14934
5db94019 14935 if (IS_GEN2(dev_priv)) {
a6c45cf0
CW
14936 dev->mode_config.max_width = 2048;
14937 dev->mode_config.max_height = 2048;
5db94019 14938 } else if (IS_GEN3(dev_priv)) {
5e4d6fa7
KP
14939 dev->mode_config.max_width = 4096;
14940 dev->mode_config.max_height = 4096;
79e53945 14941 } else {
a6c45cf0
CW
14942 dev->mode_config.max_width = 8192;
14943 dev->mode_config.max_height = 8192;
79e53945 14944 }
068be561 14945
2a307c2e
JN
14946 if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
14947 dev->mode_config.cursor_width = IS_I845G(dev_priv) ? 64 : 512;
dc41c154 14948 dev->mode_config.cursor_height = 1023;
5db94019 14949 } else if (IS_GEN2(dev_priv)) {
068be561
DL
14950 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
14951 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
14952 } else {
14953 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
14954 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
14955 }
14956
72e96d64 14957 dev->mode_config.fb_base = ggtt->mappable_base;
79e53945 14958
28c97730 14959 DRM_DEBUG_KMS("%d display pipe%s available.\n",
b7f05d4a
TU
14960 INTEL_INFO(dev_priv)->num_pipes,
14961 INTEL_INFO(dev_priv)->num_pipes > 1 ? "s" : "");
79e53945 14962
055e393f 14963 for_each_pipe(dev_priv, pipe) {
b079bd17
VS
14964 int ret;
14965
5ab0d85b 14966 ret = intel_crtc_init(dev_priv, pipe);
b079bd17
VS
14967 if (ret) {
14968 drm_mode_config_cleanup(dev);
14969 return ret;
14970 }
79e53945
JB
14971 }
14972
e72f9fbf 14973 intel_shared_dpll_init(dev);
ee7b9f93 14974
5be6e334
VS
14975 intel_update_czclk(dev_priv);
14976 intel_modeset_init_hw(dev);
14977
b2045352 14978 if (dev_priv->max_cdclk_freq == 0)
4c75b940 14979 intel_update_max_cdclk(dev_priv);
b2045352 14980
9cce37f4 14981 /* Just disable it once at startup */
29b74b7f 14982 i915_disable_vga(dev_priv);
c39055b0 14983 intel_setup_outputs(dev_priv);
11be49eb 14984
6e9f798d 14985 drm_modeset_lock_all(dev);
043e9bda 14986 intel_modeset_setup_hw_state(dev);
6e9f798d 14987 drm_modeset_unlock_all(dev);
46f297fb 14988
d3fcc808 14989 for_each_intel_crtc(dev, crtc) {
eeebeac5
ML
14990 struct intel_initial_plane_config plane_config = {};
14991
46f297fb
JB
14992 if (!crtc->active)
14993 continue;
14994
46f297fb 14995 /*
46f297fb
JB
14996 * Note that reserving the BIOS fb up front prevents us
14997 * from stuffing other stolen allocations like the ring
14998 * on top. This prevents some ugliness at boot time, and
14999 * can even allow for smooth boot transitions if the BIOS
15000 * fb is large enough for the active pipe configuration.
15001 */
eeebeac5
ML
15002 dev_priv->display.get_initial_plane_config(crtc,
15003 &plane_config);
15004
15005 /*
15006 * If the fb is shared between multiple heads, we'll
15007 * just get the first one.
15008 */
15009 intel_find_initial_plane_obj(crtc, &plane_config);
46f297fb 15010 }
d93c0372
MR
15011
15012 /*
15013 * Make sure hardware watermarks really match the state we read out.
15014 * Note that we need to do this after reconstructing the BIOS fb's
15015 * since the watermark calculation done here will use pstate->fb.
15016 */
15017 sanitize_watermarks(dev);
b079bd17
VS
15018
15019 return 0;
2c7111db
CW
15020}
15021
7fad798e
DV
15022static void intel_enable_pipe_a(struct drm_device *dev)
15023{
15024 struct intel_connector *connector;
15025 struct drm_connector *crt = NULL;
15026 struct intel_load_detect_pipe load_detect_temp;
208bf9fd 15027 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
7fad798e
DV
15028
15029 /* We can't just switch on the pipe A, we need to set things up with a
15030 * proper mode and output configuration. As a gross hack, enable pipe A
15031 * by enabling the load detect pipe once. */
3a3371ff 15032 for_each_intel_connector(dev, connector) {
7fad798e
DV
15033 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15034 crt = &connector->base;
15035 break;
15036 }
15037 }
15038
15039 if (!crt)
15040 return;
15041
208bf9fd 15042 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
49172fee 15043 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
7fad798e
DV
15044}
15045
fa555837
DV
15046static bool
15047intel_check_plane_mapping(struct intel_crtc *crtc)
15048{
b7f05d4a 15049 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
649636ef 15050 u32 val;
fa555837 15051
b7f05d4a 15052 if (INTEL_INFO(dev_priv)->num_pipes == 1)
fa555837
DV
15053 return true;
15054
649636ef 15055 val = I915_READ(DSPCNTR(!crtc->plane));
fa555837
DV
15056
15057 if ((val & DISPLAY_PLANE_ENABLE) &&
15058 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15059 return false;
15060
15061 return true;
15062}
15063
02e93c35
VS
15064static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15065{
15066 struct drm_device *dev = crtc->base.dev;
15067 struct intel_encoder *encoder;
15068
15069 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15070 return true;
15071
15072 return false;
15073}
15074
496b0fc3
ML
15075static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
15076{
15077 struct drm_device *dev = encoder->base.dev;
15078 struct intel_connector *connector;
15079
15080 for_each_connector_on_encoder(dev, &encoder->base, connector)
15081 return connector;
15082
15083 return NULL;
15084}
15085
a168f5b3
VS
15086static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
15087 enum transcoder pch_transcoder)
15088{
15089 return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
15090 (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == TRANSCODER_A);
15091}
15092
24929352
DV
15093static void intel_sanitize_crtc(struct intel_crtc *crtc)
15094{
15095 struct drm_device *dev = crtc->base.dev;
fac5e23e 15096 struct drm_i915_private *dev_priv = to_i915(dev);
4d1de975 15097 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
24929352 15098
24929352 15099 /* Clear any frame start delays used for debugging left by the BIOS */
4d1de975
JN
15100 if (!transcoder_is_dsi(cpu_transcoder)) {
15101 i915_reg_t reg = PIPECONF(cpu_transcoder);
15102
15103 I915_WRITE(reg,
15104 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15105 }
24929352 15106
d3eaf884 15107 /* restore vblank interrupts to correct state */
9625604c 15108 drm_crtc_vblank_reset(&crtc->base);
d297e103 15109 if (crtc->active) {
f9cd7b88
VS
15110 struct intel_plane *plane;
15111
9625604c 15112 drm_crtc_vblank_on(&crtc->base);
f9cd7b88
VS
15113
15114 /* Disable everything but the primary plane */
15115 for_each_intel_plane_on_crtc(dev, crtc, plane) {
15116 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
15117 continue;
15118
15119 plane->disable_plane(&plane->base, &crtc->base);
15120 }
9625604c 15121 }
d3eaf884 15122
24929352 15123 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
DV
15124 * disable the crtc (and hence change the state) if it is wrong. Note
15125 * that gen4+ has a fixed plane -> pipe mapping. */
6315b5d3 15126 if (INTEL_GEN(dev_priv) < 4 && !intel_check_plane_mapping(crtc)) {
24929352
DV
15127 bool plane;
15128
78108b7c
VS
15129 DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
15130 crtc->base.base.id, crtc->base.name);
24929352
DV
15131
15132 /* Pipe has the wrong plane attached and the plane is active.
15133 * Temporarily change the plane mapping and disable everything
15134 * ... */
15135 plane = crtc->plane;
1d4258db 15136 crtc->base.primary->state->visible = true;
24929352 15137 crtc->plane = !plane;
b17d48e2 15138 intel_crtc_disable_noatomic(&crtc->base);
24929352 15139 crtc->plane = plane;
24929352 15140 }
24929352 15141
7fad798e
DV
15142 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15143 crtc->pipe == PIPE_A && !crtc->active) {
15144 /* BIOS forgot to enable pipe A, this mostly happens after
15145 * resume. Force-enable the pipe to fix this, the update_dpms
15146 * call below we restore the pipe to the right state, but leave
15147 * the required bits on. */
15148 intel_enable_pipe_a(dev);
15149 }
15150
24929352
DV
15151 /* Adjust the state of the output pipe according to whether we
15152 * have active connectors/encoders. */
842e0307 15153 if (crtc->active && !intel_crtc_has_encoders(crtc))
b17d48e2 15154 intel_crtc_disable_noatomic(&crtc->base);
24929352 15155
49cff963 15156 if (crtc->active || HAS_GMCH_DISPLAY(dev_priv)) {
4cc31489
DV
15157 /*
15158 * We start out with underrun reporting disabled to avoid races.
15159 * For correct bookkeeping mark this on active crtcs.
15160 *
c5ab3bc0
DV
15161 * Also on gmch platforms we dont have any hardware bits to
15162 * disable the underrun reporting. Which means we need to start
15163 * out with underrun reporting disabled also on inactive pipes,
15164 * since otherwise we'll complain about the garbage we read when
15165 * e.g. coming up after runtime pm.
15166 *
4cc31489
DV
15167 * No protection against concurrent access is required - at
15168 * worst a fifo underrun happens which also sets this to false.
15169 */
15170 crtc->cpu_fifo_underrun_disabled = true;
a168f5b3
VS
15171 /*
15172 * We track the PCH trancoder underrun reporting state
15173 * within the crtc. With crtc for pipe A housing the underrun
15174 * reporting state for PCH transcoder A, crtc for pipe B housing
15175 * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
15176 * and marking underrun reporting as disabled for the non-existing
15177 * PCH transcoders B and C would prevent enabling the south
15178 * error interrupt (see cpt_can_enable_serr_int()).
15179 */
15180 if (has_pch_trancoder(dev_priv, (enum transcoder)crtc->pipe))
15181 crtc->pch_fifo_underrun_disabled = true;
4cc31489 15182 }
24929352
DV
15183}
15184
15185static void intel_sanitize_encoder(struct intel_encoder *encoder)
15186{
15187 struct intel_connector *connector;
24929352
DV
15188
15189 /* We need to check both for a crtc link (meaning that the
15190 * encoder is active and trying to read from a pipe) and the
15191 * pipe itself being active. */
15192 bool has_active_crtc = encoder->base.crtc &&
15193 to_intel_crtc(encoder->base.crtc)->active;
15194
496b0fc3
ML
15195 connector = intel_encoder_find_connector(encoder);
15196 if (connector && !has_active_crtc) {
24929352
DV
15197 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15198 encoder->base.base.id,
8e329a03 15199 encoder->base.name);
24929352
DV
15200
15201 /* Connector is active, but has no active pipe. This is
15202 * fallout from our resume register restoring. Disable
15203 * the encoder manually again. */
15204 if (encoder->base.crtc) {
fd6bbda9
ML
15205 struct drm_crtc_state *crtc_state = encoder->base.crtc->state;
15206
24929352
DV
15207 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15208 encoder->base.base.id,
8e329a03 15209 encoder->base.name);
fd6bbda9 15210 encoder->disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
a62d1497 15211 if (encoder->post_disable)
fd6bbda9 15212 encoder->post_disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
24929352 15213 }
7f1950fb 15214 encoder->base.crtc = NULL;
24929352
DV
15215
15216 /* Inconsistent output/port/pipe state happens presumably due to
15217 * a bug in one of the get_hw_state functions. Or someplace else
15218 * in our code, like the register restore mess on resume. Clamp
15219 * things to off as a safer default. */
fd6bbda9
ML
15220
15221 connector->base.dpms = DRM_MODE_DPMS_OFF;
15222 connector->base.encoder = NULL;
24929352
DV
15223 }
15224 /* Enabled encoders without active connectors will be fixed in
15225 * the crtc fixup. */
15226}
15227
29b74b7f 15228void i915_redisable_vga_power_on(struct drm_i915_private *dev_priv)
0fde901f 15229{
920a14b2 15230 i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
0fde901f 15231
04098753
ID
15232 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15233 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
29b74b7f 15234 i915_disable_vga(dev_priv);
04098753
ID
15235 }
15236}
15237
29b74b7f 15238void i915_redisable_vga(struct drm_i915_private *dev_priv)
04098753 15239{
8dc8a27c
PZ
15240 /* This function can be called both from intel_modeset_setup_hw_state or
15241 * at a very early point in our resume sequence, where the power well
15242 * structures are not yet restored. Since this function is at a very
15243 * paranoid "someone might have enabled VGA while we were not looking"
15244 * level, just check if the power well is enabled instead of trying to
15245 * follow the "don't touch the power well if we don't need it" policy
15246 * the rest of the driver uses. */
6392f847 15247 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
15248 return;
15249
29b74b7f 15250 i915_redisable_vga_power_on(dev_priv);
6392f847
ID
15251
15252 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
0fde901f
KM
15253}
15254
f9cd7b88 15255static bool primary_get_hw_state(struct intel_plane *plane)
98ec7739 15256{
f9cd7b88 15257 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
98ec7739 15258
f9cd7b88 15259 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
d032ffa0
ML
15260}
15261
f9cd7b88
VS
15262/* FIXME read out full plane state for all planes */
15263static void readout_plane_state(struct intel_crtc *crtc)
d032ffa0 15264{
b26d3ea3 15265 struct drm_plane *primary = crtc->base.primary;
f9cd7b88 15266 struct intel_plane_state *plane_state =
b26d3ea3 15267 to_intel_plane_state(primary->state);
d032ffa0 15268
936e71e3 15269 plane_state->base.visible = crtc->active &&
b26d3ea3
ML
15270 primary_get_hw_state(to_intel_plane(primary));
15271
936e71e3 15272 if (plane_state->base.visible)
b26d3ea3 15273 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
98ec7739
VS
15274}
15275
30e984df 15276static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352 15277{
fac5e23e 15278 struct drm_i915_private *dev_priv = to_i915(dev);
24929352 15279 enum pipe pipe;
24929352
DV
15280 struct intel_crtc *crtc;
15281 struct intel_encoder *encoder;
15282 struct intel_connector *connector;
5358901f 15283 int i;
24929352 15284
565602d7
ML
15285 dev_priv->active_crtcs = 0;
15286
d3fcc808 15287 for_each_intel_crtc(dev, crtc) {
a8cd6da0
VS
15288 struct intel_crtc_state *crtc_state =
15289 to_intel_crtc_state(crtc->base.state);
3b117c8f 15290
ec2dc6a0 15291 __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
565602d7
ML
15292 memset(crtc_state, 0, sizeof(*crtc_state));
15293 crtc_state->base.crtc = &crtc->base;
24929352 15294
565602d7
ML
15295 crtc_state->base.active = crtc_state->base.enable =
15296 dev_priv->display.get_pipe_config(crtc, crtc_state);
15297
15298 crtc->base.enabled = crtc_state->base.enable;
15299 crtc->active = crtc_state->base.active;
15300
aca1ebf4 15301 if (crtc_state->base.active)
565602d7
ML
15302 dev_priv->active_crtcs |= 1 << crtc->pipe;
15303
f9cd7b88 15304 readout_plane_state(crtc);
24929352 15305
78108b7c
VS
15306 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
15307 crtc->base.base.id, crtc->base.name,
a8cd6da0 15308 enableddisabled(crtc_state->base.active));
24929352
DV
15309 }
15310
5358901f
DV
15311 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15312 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15313
2edd6443 15314 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
2c42e535
ACO
15315 &pll->state.hw_state);
15316 pll->state.crtc_mask = 0;
d3fcc808 15317 for_each_intel_crtc(dev, crtc) {
a8cd6da0
VS
15318 struct intel_crtc_state *crtc_state =
15319 to_intel_crtc_state(crtc->base.state);
15320
15321 if (crtc_state->base.active &&
15322 crtc_state->shared_dpll == pll)
2c42e535 15323 pll->state.crtc_mask |= 1 << crtc->pipe;
5358901f 15324 }
2c42e535 15325 pll->active_mask = pll->state.crtc_mask;
5358901f 15326
1e6f2ddc 15327 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
2c42e535 15328 pll->name, pll->state.crtc_mask, pll->on);
5358901f
DV
15329 }
15330
b2784e15 15331 for_each_intel_encoder(dev, encoder) {
24929352
DV
15332 pipe = 0;
15333
15334 if (encoder->get_hw_state(encoder, &pipe)) {
a8cd6da0
VS
15335 struct intel_crtc_state *crtc_state;
15336
98187836 15337 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
a8cd6da0 15338 crtc_state = to_intel_crtc_state(crtc->base.state);
e2af48c6 15339
045ac3b5 15340 encoder->base.crtc = &crtc->base;
a8cd6da0
VS
15341 crtc_state->output_types |= 1 << encoder->type;
15342 encoder->get_config(encoder, crtc_state);
24929352
DV
15343 } else {
15344 encoder->base.crtc = NULL;
15345 }
15346
6f2bcceb 15347 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
08c4d7fc
TU
15348 encoder->base.base.id, encoder->base.name,
15349 enableddisabled(encoder->base.crtc),
6f2bcceb 15350 pipe_name(pipe));
24929352
DV
15351 }
15352
3a3371ff 15353 for_each_intel_connector(dev, connector) {
24929352
DV
15354 if (connector->get_hw_state(connector)) {
15355 connector->base.dpms = DRM_MODE_DPMS_ON;
2aa974c9
ML
15356
15357 encoder = connector->encoder;
15358 connector->base.encoder = &encoder->base;
15359
15360 if (encoder->base.crtc &&
15361 encoder->base.crtc->state->active) {
15362 /*
15363 * This has to be done during hardware readout
15364 * because anything calling .crtc_disable may
15365 * rely on the connector_mask being accurate.
15366 */
15367 encoder->base.crtc->state->connector_mask |=
15368 1 << drm_connector_index(&connector->base);
e87a52b3
ML
15369 encoder->base.crtc->state->encoder_mask |=
15370 1 << drm_encoder_index(&encoder->base);
2aa974c9
ML
15371 }
15372
24929352
DV
15373 } else {
15374 connector->base.dpms = DRM_MODE_DPMS_OFF;
15375 connector->base.encoder = NULL;
15376 }
15377 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
08c4d7fc
TU
15378 connector->base.base.id, connector->base.name,
15379 enableddisabled(connector->base.encoder));
24929352 15380 }
7f4c6284
VS
15381
15382 for_each_intel_crtc(dev, crtc) {
a8cd6da0
VS
15383 struct intel_crtc_state *crtc_state =
15384 to_intel_crtc_state(crtc->base.state);
aca1ebf4
VS
15385 int pixclk = 0;
15386
a8cd6da0 15387 crtc->base.hwmode = crtc_state->base.adjusted_mode;
7f4c6284
VS
15388
15389 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
a8cd6da0
VS
15390 if (crtc_state->base.active) {
15391 intel_mode_from_pipe_config(&crtc->base.mode, crtc_state);
15392 intel_mode_from_pipe_config(&crtc_state->base.adjusted_mode, crtc_state);
7f4c6284
VS
15393 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
15394
15395 /*
15396 * The initial mode needs to be set in order to keep
15397 * the atomic core happy. It wants a valid mode if the
15398 * crtc's enabled, so we do the above call.
15399 *
7800fb69
DV
15400 * But we don't set all the derived state fully, hence
15401 * set a flag to indicate that a full recalculation is
15402 * needed on the next commit.
7f4c6284 15403 */
a8cd6da0 15404 crtc_state->base.mode.private_flags = I915_MODE_FLAG_INHERITED;
9eca6832 15405
a7d1b3f4
VS
15406 intel_crtc_compute_pixel_rate(crtc_state);
15407
15408 if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv) ||
15409 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
15410 pixclk = crtc_state->pixel_rate;
aca1ebf4
VS
15411 else
15412 WARN_ON(dev_priv->display.modeset_calc_cdclk);
15413
15414 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
a8cd6da0 15415 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
aca1ebf4
VS
15416 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
15417
9eca6832
VS
15418 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
15419 update_scanline_offset(crtc);
7f4c6284 15420 }
e3b247da 15421
aca1ebf4
VS
15422 dev_priv->min_pixclk[crtc->pipe] = pixclk;
15423
a8cd6da0 15424 intel_pipe_config_sanity_check(dev_priv, crtc_state);
7f4c6284 15425 }
30e984df
DV
15426}
15427
62b69566
ACO
15428static void
15429get_encoder_power_domains(struct drm_i915_private *dev_priv)
15430{
15431 struct intel_encoder *encoder;
15432
15433 for_each_intel_encoder(&dev_priv->drm, encoder) {
15434 u64 get_domains;
15435 enum intel_display_power_domain domain;
15436
15437 if (!encoder->get_power_domains)
15438 continue;
15439
15440 get_domains = encoder->get_power_domains(encoder);
15441 for_each_power_domain(domain, get_domains)
15442 intel_display_power_get(dev_priv, domain);
15443 }
15444}
15445
043e9bda
ML
15446/* Scan out the current hw modeset state,
15447 * and sanitizes it to the current state
15448 */
15449static void
15450intel_modeset_setup_hw_state(struct drm_device *dev)
30e984df 15451{
fac5e23e 15452 struct drm_i915_private *dev_priv = to_i915(dev);
30e984df 15453 enum pipe pipe;
30e984df
DV
15454 struct intel_crtc *crtc;
15455 struct intel_encoder *encoder;
35c95375 15456 int i;
30e984df
DV
15457
15458 intel_modeset_readout_hw_state(dev);
24929352
DV
15459
15460 /* HW state is read out, now we need to sanitize this mess. */
62b69566
ACO
15461 get_encoder_power_domains(dev_priv);
15462
b2784e15 15463 for_each_intel_encoder(dev, encoder) {
24929352
DV
15464 intel_sanitize_encoder(encoder);
15465 }
15466
055e393f 15467 for_each_pipe(dev_priv, pipe) {
98187836 15468 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
e2af48c6 15469
24929352 15470 intel_sanitize_crtc(crtc);
6e3c9717
ACO
15471 intel_dump_pipe_config(crtc, crtc->config,
15472 "[setup_hw_state]");
24929352 15473 }
9a935856 15474
d29b2f9d
ACO
15475 intel_modeset_update_connector_atomic_state(dev);
15476
35c95375
DV
15477 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15478 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15479
2dd66ebd 15480 if (!pll->on || pll->active_mask)
35c95375
DV
15481 continue;
15482
15483 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15484
2edd6443 15485 pll->funcs.disable(dev_priv, pll);
35c95375
DV
15486 pll->on = false;
15487 }
15488
920a14b2 15489 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
6eb1a681 15490 vlv_wm_get_hw_state(dev);
5db94019 15491 else if (IS_GEN9(dev_priv))
3078999f 15492 skl_wm_get_hw_state(dev);
6e266956 15493 else if (HAS_PCH_SPLIT(dev_priv))
243e6a44 15494 ilk_wm_get_hw_state(dev);
292b990e
ML
15495
15496 for_each_intel_crtc(dev, crtc) {
d8fc70b7 15497 u64 put_domains;
292b990e 15498
74bff5f9 15499 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
292b990e
ML
15500 if (WARN_ON(put_domains))
15501 modeset_put_power_domains(dev_priv, put_domains);
15502 }
15503 intel_display_set_init_power(dev_priv, false);
010cf73d 15504
8d8c386c
ID
15505 intel_power_domains_verify_state(dev_priv);
15506
010cf73d 15507 intel_fbc_init_pipe_state(dev_priv);
043e9bda 15508}
7d0bc1ea 15509
043e9bda
ML
15510void intel_display_resume(struct drm_device *dev)
15511{
e2c8b870
ML
15512 struct drm_i915_private *dev_priv = to_i915(dev);
15513 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
15514 struct drm_modeset_acquire_ctx ctx;
043e9bda 15515 int ret;
f30da187 15516
e2c8b870 15517 dev_priv->modeset_restore_state = NULL;
73974893
ML
15518 if (state)
15519 state->acquire_ctx = &ctx;
043e9bda 15520
ea49c9ac
ML
15521 /*
15522 * This is a cludge because with real atomic modeset mode_config.mutex
15523 * won't be taken. Unfortunately some probed state like
15524 * audio_codec_enable is still protected by mode_config.mutex, so lock
15525 * it here for now.
15526 */
15527 mutex_lock(&dev->mode_config.mutex);
e2c8b870 15528 drm_modeset_acquire_init(&ctx, 0);
043e9bda 15529
73974893
ML
15530 while (1) {
15531 ret = drm_modeset_lock_all_ctx(dev, &ctx);
15532 if (ret != -EDEADLK)
15533 break;
043e9bda 15534
e2c8b870 15535 drm_modeset_backoff(&ctx);
e2c8b870 15536 }
043e9bda 15537
73974893
ML
15538 if (!ret)
15539 ret = __intel_display_resume(dev, state);
15540
e2c8b870
ML
15541 drm_modeset_drop_locks(&ctx);
15542 drm_modeset_acquire_fini(&ctx);
ea49c9ac 15543 mutex_unlock(&dev->mode_config.mutex);
043e9bda 15544
0853695c 15545 if (ret)
e2c8b870 15546 DRM_ERROR("Restoring old state failed with %i\n", ret);
3c5e37f1
CW
15547 if (state)
15548 drm_atomic_state_put(state);
2c7111db
CW
15549}
15550
15551void intel_modeset_gem_init(struct drm_device *dev)
15552{
dc97997a 15553 struct drm_i915_private *dev_priv = to_i915(dev);
484b41dd 15554
dc97997a 15555 intel_init_gt_powersave(dev_priv);
ae48434c 15556
1ee8da6d 15557 intel_setup_overlay(dev_priv);
1ebaa0b9
CW
15558}
15559
15560int intel_connector_register(struct drm_connector *connector)
15561{
15562 struct intel_connector *intel_connector = to_intel_connector(connector);
15563 int ret;
15564
15565 ret = intel_backlight_device_register(intel_connector);
15566 if (ret)
15567 goto err;
15568
15569 return 0;
0962c3c9 15570
1ebaa0b9
CW
15571err:
15572 return ret;
79e53945
JB
15573}
15574
c191eca1 15575void intel_connector_unregister(struct drm_connector *connector)
4932e2c3 15576{
e63d87c0 15577 struct intel_connector *intel_connector = to_intel_connector(connector);
4932e2c3 15578
e63d87c0 15579 intel_backlight_device_unregister(intel_connector);
4932e2c3 15580 intel_panel_destroy_backlight(connector);
4932e2c3
ID
15581}
15582
79e53945
JB
15583void intel_modeset_cleanup(struct drm_device *dev)
15584{
fac5e23e 15585 struct drm_i915_private *dev_priv = to_i915(dev);
652c393a 15586
eb955eee
CW
15587 flush_work(&dev_priv->atomic_helper.free_work);
15588 WARN_ON(!llist_empty(&dev_priv->atomic_helper.free_list));
15589
dc97997a 15590 intel_disable_gt_powersave(dev_priv);
2eb5252e 15591
fd0c0642
DV
15592 /*
15593 * Interrupts and polling as the first thing to avoid creating havoc.
2eb5252e 15594 * Too much stuff here (turning of connectors, ...) would
fd0c0642
DV
15595 * experience fancy races otherwise.
15596 */
2aeb7d3a 15597 intel_irq_uninstall(dev_priv);
eb21b92b 15598
fd0c0642
DV
15599 /*
15600 * Due to the hpd irq storm handling the hotplug work can re-arm the
15601 * poll handlers. Hence disable polling after hpd handling is shut down.
15602 */
f87ea761 15603 drm_kms_helper_poll_fini(dev);
fd0c0642 15604
723bfd70
JB
15605 intel_unregister_dsm_handler();
15606
c937ab3e 15607 intel_fbc_global_disable(dev_priv);
69341a5e 15608
1630fe75
CW
15609 /* flush any delayed tasks or pending work */
15610 flush_scheduled_work();
15611
79e53945 15612 drm_mode_config_cleanup(dev);
4d7bb011 15613
1ee8da6d 15614 intel_cleanup_overlay(dev_priv);
ae48434c 15615
dc97997a 15616 intel_cleanup_gt_powersave(dev_priv);
f5949141 15617
40196446 15618 intel_teardown_gmbus(dev_priv);
79e53945
JB
15619}
15620
df0e9248
CW
15621void intel_connector_attach_encoder(struct intel_connector *connector,
15622 struct intel_encoder *encoder)
15623{
15624 connector->encoder = encoder;
15625 drm_mode_connector_attach_encoder(&connector->base,
15626 &encoder->base);
79e53945 15627}
28d52043
DA
15628
15629/*
15630 * set vga decode state - true == enable VGA decode
15631 */
6315b5d3 15632int intel_modeset_vga_set_state(struct drm_i915_private *dev_priv, bool state)
28d52043 15633{
6315b5d3 15634 unsigned reg = INTEL_GEN(dev_priv) >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
15635 u16 gmch_ctrl;
15636
75fa041d
CW
15637 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15638 DRM_ERROR("failed to read control word\n");
15639 return -EIO;
15640 }
15641
c0cc8a55
CW
15642 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15643 return 0;
15644
28d52043
DA
15645 if (state)
15646 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15647 else
15648 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
15649
15650 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15651 DRM_ERROR("failed to write control word\n");
15652 return -EIO;
15653 }
15654
28d52043
DA
15655 return 0;
15656}
c4a1d9e4 15657
98a2f411
CW
15658#if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
15659
c4a1d9e4 15660struct intel_display_error_state {
ff57f1b0
PZ
15661
15662 u32 power_well_driver;
15663
63b66e5b
CW
15664 int num_transcoders;
15665
c4a1d9e4
CW
15666 struct intel_cursor_error_state {
15667 u32 control;
15668 u32 position;
15669 u32 base;
15670 u32 size;
52331309 15671 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
15672
15673 struct intel_pipe_error_state {
ddf9c536 15674 bool power_domain_on;
c4a1d9e4 15675 u32 source;
f301b1e1 15676 u32 stat;
52331309 15677 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
15678
15679 struct intel_plane_error_state {
15680 u32 control;
15681 u32 stride;
15682 u32 size;
15683 u32 pos;
15684 u32 addr;
15685 u32 surface;
15686 u32 tile_offset;
52331309 15687 } plane[I915_MAX_PIPES];
63b66e5b
CW
15688
15689 struct intel_transcoder_error_state {
ddf9c536 15690 bool power_domain_on;
63b66e5b
CW
15691 enum transcoder cpu_transcoder;
15692
15693 u32 conf;
15694
15695 u32 htotal;
15696 u32 hblank;
15697 u32 hsync;
15698 u32 vtotal;
15699 u32 vblank;
15700 u32 vsync;
15701 } transcoder[4];
c4a1d9e4
CW
15702};
15703
15704struct intel_display_error_state *
c033666a 15705intel_display_capture_error_state(struct drm_i915_private *dev_priv)
c4a1d9e4 15706{
c4a1d9e4 15707 struct intel_display_error_state *error;
63b66e5b
CW
15708 int transcoders[] = {
15709 TRANSCODER_A,
15710 TRANSCODER_B,
15711 TRANSCODER_C,
15712 TRANSCODER_EDP,
15713 };
c4a1d9e4
CW
15714 int i;
15715
c033666a 15716 if (INTEL_INFO(dev_priv)->num_pipes == 0)
63b66e5b
CW
15717 return NULL;
15718
9d1cb914 15719 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
15720 if (error == NULL)
15721 return NULL;
15722
c033666a 15723 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
ff57f1b0
PZ
15724 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
15725
055e393f 15726 for_each_pipe(dev_priv, i) {
ddf9c536 15727 error->pipe[i].power_domain_on =
f458ebbc
DV
15728 __intel_display_power_is_enabled(dev_priv,
15729 POWER_DOMAIN_PIPE(i));
ddf9c536 15730 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
15731 continue;
15732
5efb3e28
VS
15733 error->cursor[i].control = I915_READ(CURCNTR(i));
15734 error->cursor[i].position = I915_READ(CURPOS(i));
15735 error->cursor[i].base = I915_READ(CURBASE(i));
c4a1d9e4
CW
15736
15737 error->plane[i].control = I915_READ(DSPCNTR(i));
15738 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
c033666a 15739 if (INTEL_GEN(dev_priv) <= 3) {
51889b35 15740 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
15741 error->plane[i].pos = I915_READ(DSPPOS(i));
15742 }
c033666a 15743 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
ca291363 15744 error->plane[i].addr = I915_READ(DSPADDR(i));
c033666a 15745 if (INTEL_GEN(dev_priv) >= 4) {
c4a1d9e4
CW
15746 error->plane[i].surface = I915_READ(DSPSURF(i));
15747 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15748 }
15749
c4a1d9e4 15750 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1 15751
c033666a 15752 if (HAS_GMCH_DISPLAY(dev_priv))
f301b1e1 15753 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
15754 }
15755
4d1de975 15756 /* Note: this does not include DSI transcoders. */
c033666a 15757 error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
2d1fe073 15758 if (HAS_DDI(dev_priv))
63b66e5b
CW
15759 error->num_transcoders++; /* Account for eDP. */
15760
15761 for (i = 0; i < error->num_transcoders; i++) {
15762 enum transcoder cpu_transcoder = transcoders[i];
15763
ddf9c536 15764 error->transcoder[i].power_domain_on =
f458ebbc 15765 __intel_display_power_is_enabled(dev_priv,
38cc1daf 15766 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 15767 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
15768 continue;
15769
63b66e5b
CW
15770 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15771
15772 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15773 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15774 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15775 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15776 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15777 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15778 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
15779 }
15780
15781 return error;
15782}
15783
edc3d884
MK
15784#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15785
c4a1d9e4 15786void
edc3d884 15787intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
15788 struct intel_display_error_state *error)
15789{
5a4c6f1b 15790 struct drm_i915_private *dev_priv = m->i915;
c4a1d9e4
CW
15791 int i;
15792
63b66e5b
CW
15793 if (!error)
15794 return;
15795
b7f05d4a 15796 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev_priv)->num_pipes);
8652744b 15797 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
edc3d884 15798 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 15799 error->power_well_driver);
055e393f 15800 for_each_pipe(dev_priv, i) {
edc3d884 15801 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536 15802 err_printf(m, " Power: %s\n",
87ad3212 15803 onoff(error->pipe[i].power_domain_on));
edc3d884 15804 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 15805 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
15806
15807 err_printf(m, "Plane [%d]:\n", i);
15808 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15809 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
5f56d5f9 15810 if (INTEL_GEN(dev_priv) <= 3) {
edc3d884
MK
15811 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15812 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 15813 }
772c2a51 15814 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
edc3d884 15815 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
5f56d5f9 15816 if (INTEL_GEN(dev_priv) >= 4) {
edc3d884
MK
15817 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15818 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
15819 }
15820
edc3d884
MK
15821 err_printf(m, "Cursor [%d]:\n", i);
15822 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15823 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15824 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 15825 }
63b66e5b
CW
15826
15827 for (i = 0; i < error->num_transcoders; i++) {
da205630 15828 err_printf(m, "CPU transcoder: %s\n",
63b66e5b 15829 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536 15830 err_printf(m, " Power: %s\n",
87ad3212 15831 onoff(error->transcoder[i].power_domain_on));
63b66e5b
CW
15832 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15833 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15834 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15835 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15836 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15837 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
15838 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
15839 }
c4a1d9e4 15840}
98a2f411
CW
15841
15842#endif