]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpu/drm/i915/intel_display.c
drm: Nuke drm_atomic_helper_crtc_set_property
[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);
aecd36b8
VS
123static void intel_modeset_setup_hw_state(struct drm_device *dev,
124 struct drm_modeset_acquire_ctx *ctx);
2622a081 125static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
e7457a9a 126
d4906093 127struct intel_limit {
4c5def93
ACO
128 struct {
129 int min, max;
130 } dot, vco, n, m, m1, m2, p, p1;
131
132 struct {
133 int dot_limit;
134 int p2_slow, p2_fast;
135 } p2;
d4906093 136};
79e53945 137
bfa7df01 138/* returns HPLL frequency in kHz */
49cd97a3 139int vlv_get_hpll_vco(struct drm_i915_private *dev_priv)
bfa7df01
VS
140{
141 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
142
143 /* Obtain SKU information */
144 mutex_lock(&dev_priv->sb_lock);
145 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
146 CCK_FUSE_HPLL_FREQ_MASK;
147 mutex_unlock(&dev_priv->sb_lock);
148
149 return vco_freq[hpll_freq] * 1000;
150}
151
c30fec65
VS
152int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
153 const char *name, u32 reg, int ref_freq)
bfa7df01
VS
154{
155 u32 val;
156 int divider;
157
bfa7df01
VS
158 mutex_lock(&dev_priv->sb_lock);
159 val = vlv_cck_read(dev_priv, reg);
160 mutex_unlock(&dev_priv->sb_lock);
161
162 divider = val & CCK_FREQUENCY_VALUES;
163
164 WARN((val & CCK_FREQUENCY_STATUS) !=
165 (divider << CCK_FREQUENCY_STATUS_SHIFT),
166 "%s change in progress\n", name);
167
c30fec65
VS
168 return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
169}
170
7ff89ca2
VS
171int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
172 const char *name, u32 reg)
c30fec65
VS
173{
174 if (dev_priv->hpll_freq == 0)
49cd97a3 175 dev_priv->hpll_freq = vlv_get_hpll_vco(dev_priv);
c30fec65
VS
176
177 return vlv_get_cck_clock(dev_priv, name, reg,
178 dev_priv->hpll_freq);
bfa7df01
VS
179}
180
bfa7df01
VS
181static void intel_update_czclk(struct drm_i915_private *dev_priv)
182{
666a4537 183 if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
bfa7df01
VS
184 return;
185
186 dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
187 CCK_CZ_CLOCK_CONTROL);
188
189 DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
190}
191
021357ac 192static inline u32 /* units of 100MHz */
21a727b3
VS
193intel_fdi_link_freq(struct drm_i915_private *dev_priv,
194 const struct intel_crtc_state *pipe_config)
021357ac 195{
21a727b3
VS
196 if (HAS_DDI(dev_priv))
197 return pipe_config->port_clock; /* SPLL */
198 else if (IS_GEN5(dev_priv))
199 return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
e3b247da 200 else
21a727b3 201 return 270000;
021357ac
CW
202}
203
1b6f4958 204static const struct intel_limit intel_limits_i8xx_dac = {
0206e353 205 .dot = { .min = 25000, .max = 350000 },
9c333719 206 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 207 .n = { .min = 2, .max = 16 },
0206e353
AJ
208 .m = { .min = 96, .max = 140 },
209 .m1 = { .min = 18, .max = 26 },
210 .m2 = { .min = 6, .max = 16 },
211 .p = { .min = 4, .max = 128 },
212 .p1 = { .min = 2, .max = 33 },
273e27ca
EA
213 .p2 = { .dot_limit = 165000,
214 .p2_slow = 4, .p2_fast = 2 },
e4b36699
KP
215};
216
1b6f4958 217static const struct intel_limit intel_limits_i8xx_dvo = {
5d536e28 218 .dot = { .min = 25000, .max = 350000 },
9c333719 219 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 220 .n = { .min = 2, .max = 16 },
5d536e28
DV
221 .m = { .min = 96, .max = 140 },
222 .m1 = { .min = 18, .max = 26 },
223 .m2 = { .min = 6, .max = 16 },
224 .p = { .min = 4, .max = 128 },
225 .p1 = { .min = 2, .max = 33 },
226 .p2 = { .dot_limit = 165000,
227 .p2_slow = 4, .p2_fast = 4 },
228};
229
1b6f4958 230static const struct intel_limit intel_limits_i8xx_lvds = {
0206e353 231 .dot = { .min = 25000, .max = 350000 },
9c333719 232 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 233 .n = { .min = 2, .max = 16 },
0206e353
AJ
234 .m = { .min = 96, .max = 140 },
235 .m1 = { .min = 18, .max = 26 },
236 .m2 = { .min = 6, .max = 16 },
237 .p = { .min = 4, .max = 128 },
238 .p1 = { .min = 1, .max = 6 },
273e27ca
EA
239 .p2 = { .dot_limit = 165000,
240 .p2_slow = 14, .p2_fast = 7 },
e4b36699 241};
273e27ca 242
1b6f4958 243static const struct intel_limit intel_limits_i9xx_sdvo = {
0206e353
AJ
244 .dot = { .min = 20000, .max = 400000 },
245 .vco = { .min = 1400000, .max = 2800000 },
246 .n = { .min = 1, .max = 6 },
247 .m = { .min = 70, .max = 120 },
4f7dfb67
PJ
248 .m1 = { .min = 8, .max = 18 },
249 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
250 .p = { .min = 5, .max = 80 },
251 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
252 .p2 = { .dot_limit = 200000,
253 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
254};
255
1b6f4958 256static const struct intel_limit intel_limits_i9xx_lvds = {
0206e353
AJ
257 .dot = { .min = 20000, .max = 400000 },
258 .vco = { .min = 1400000, .max = 2800000 },
259 .n = { .min = 1, .max = 6 },
260 .m = { .min = 70, .max = 120 },
53a7d2d1
PJ
261 .m1 = { .min = 8, .max = 18 },
262 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
263 .p = { .min = 7, .max = 98 },
264 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
265 .p2 = { .dot_limit = 112000,
266 .p2_slow = 14, .p2_fast = 7 },
e4b36699
KP
267};
268
273e27ca 269
1b6f4958 270static const struct intel_limit intel_limits_g4x_sdvo = {
273e27ca
EA
271 .dot = { .min = 25000, .max = 270000 },
272 .vco = { .min = 1750000, .max = 3500000},
273 .n = { .min = 1, .max = 4 },
274 .m = { .min = 104, .max = 138 },
275 .m1 = { .min = 17, .max = 23 },
276 .m2 = { .min = 5, .max = 11 },
277 .p = { .min = 10, .max = 30 },
278 .p1 = { .min = 1, .max = 3},
279 .p2 = { .dot_limit = 270000,
280 .p2_slow = 10,
281 .p2_fast = 10
044c7c41 282 },
e4b36699
KP
283};
284
1b6f4958 285static const struct intel_limit intel_limits_g4x_hdmi = {
273e27ca
EA
286 .dot = { .min = 22000, .max = 400000 },
287 .vco = { .min = 1750000, .max = 3500000},
288 .n = { .min = 1, .max = 4 },
289 .m = { .min = 104, .max = 138 },
290 .m1 = { .min = 16, .max = 23 },
291 .m2 = { .min = 5, .max = 11 },
292 .p = { .min = 5, .max = 80 },
293 .p1 = { .min = 1, .max = 8},
294 .p2 = { .dot_limit = 165000,
295 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
296};
297
1b6f4958 298static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
273e27ca
EA
299 .dot = { .min = 20000, .max = 115000 },
300 .vco = { .min = 1750000, .max = 3500000 },
301 .n = { .min = 1, .max = 3 },
302 .m = { .min = 104, .max = 138 },
303 .m1 = { .min = 17, .max = 23 },
304 .m2 = { .min = 5, .max = 11 },
305 .p = { .min = 28, .max = 112 },
306 .p1 = { .min = 2, .max = 8 },
307 .p2 = { .dot_limit = 0,
308 .p2_slow = 14, .p2_fast = 14
044c7c41 309 },
e4b36699
KP
310};
311
1b6f4958 312static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
273e27ca
EA
313 .dot = { .min = 80000, .max = 224000 },
314 .vco = { .min = 1750000, .max = 3500000 },
315 .n = { .min = 1, .max = 3 },
316 .m = { .min = 104, .max = 138 },
317 .m1 = { .min = 17, .max = 23 },
318 .m2 = { .min = 5, .max = 11 },
319 .p = { .min = 14, .max = 42 },
320 .p1 = { .min = 2, .max = 6 },
321 .p2 = { .dot_limit = 0,
322 .p2_slow = 7, .p2_fast = 7
044c7c41 323 },
e4b36699
KP
324};
325
1b6f4958 326static const struct intel_limit intel_limits_pineview_sdvo = {
0206e353
AJ
327 .dot = { .min = 20000, .max = 400000},
328 .vco = { .min = 1700000, .max = 3500000 },
273e27ca 329 /* Pineview's Ncounter is a ring counter */
0206e353
AJ
330 .n = { .min = 3, .max = 6 },
331 .m = { .min = 2, .max = 256 },
273e27ca 332 /* Pineview only has one combined m divider, which we treat as m2. */
0206e353
AJ
333 .m1 = { .min = 0, .max = 0 },
334 .m2 = { .min = 0, .max = 254 },
335 .p = { .min = 5, .max = 80 },
336 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
337 .p2 = { .dot_limit = 200000,
338 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
339};
340
1b6f4958 341static const struct intel_limit intel_limits_pineview_lvds = {
0206e353
AJ
342 .dot = { .min = 20000, .max = 400000 },
343 .vco = { .min = 1700000, .max = 3500000 },
344 .n = { .min = 3, .max = 6 },
345 .m = { .min = 2, .max = 256 },
346 .m1 = { .min = 0, .max = 0 },
347 .m2 = { .min = 0, .max = 254 },
348 .p = { .min = 7, .max = 112 },
349 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
350 .p2 = { .dot_limit = 112000,
351 .p2_slow = 14, .p2_fast = 14 },
e4b36699
KP
352};
353
273e27ca
EA
354/* Ironlake / Sandybridge
355 *
356 * We calculate clock using (register_value + 2) for N/M1/M2, so here
357 * the range value for them is (actual_value - 2).
358 */
1b6f4958 359static const struct intel_limit intel_limits_ironlake_dac = {
273e27ca
EA
360 .dot = { .min = 25000, .max = 350000 },
361 .vco = { .min = 1760000, .max = 3510000 },
362 .n = { .min = 1, .max = 5 },
363 .m = { .min = 79, .max = 127 },
364 .m1 = { .min = 12, .max = 22 },
365 .m2 = { .min = 5, .max = 9 },
366 .p = { .min = 5, .max = 80 },
367 .p1 = { .min = 1, .max = 8 },
368 .p2 = { .dot_limit = 225000,
369 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
370};
371
1b6f4958 372static const struct intel_limit intel_limits_ironlake_single_lvds = {
273e27ca
EA
373 .dot = { .min = 25000, .max = 350000 },
374 .vco = { .min = 1760000, .max = 3510000 },
375 .n = { .min = 1, .max = 3 },
376 .m = { .min = 79, .max = 118 },
377 .m1 = { .min = 12, .max = 22 },
378 .m2 = { .min = 5, .max = 9 },
379 .p = { .min = 28, .max = 112 },
380 .p1 = { .min = 2, .max = 8 },
381 .p2 = { .dot_limit = 225000,
382 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
383};
384
1b6f4958 385static const struct intel_limit intel_limits_ironlake_dual_lvds = {
273e27ca
EA
386 .dot = { .min = 25000, .max = 350000 },
387 .vco = { .min = 1760000, .max = 3510000 },
388 .n = { .min = 1, .max = 3 },
389 .m = { .min = 79, .max = 127 },
390 .m1 = { .min = 12, .max = 22 },
391 .m2 = { .min = 5, .max = 9 },
392 .p = { .min = 14, .max = 56 },
393 .p1 = { .min = 2, .max = 8 },
394 .p2 = { .dot_limit = 225000,
395 .p2_slow = 7, .p2_fast = 7 },
b91ad0ec
ZW
396};
397
273e27ca 398/* LVDS 100mhz refclk limits. */
1b6f4958 399static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
273e27ca
EA
400 .dot = { .min = 25000, .max = 350000 },
401 .vco = { .min = 1760000, .max = 3510000 },
402 .n = { .min = 1, .max = 2 },
403 .m = { .min = 79, .max = 126 },
404 .m1 = { .min = 12, .max = 22 },
405 .m2 = { .min = 5, .max = 9 },
406 .p = { .min = 28, .max = 112 },
0206e353 407 .p1 = { .min = 2, .max = 8 },
273e27ca
EA
408 .p2 = { .dot_limit = 225000,
409 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
410};
411
1b6f4958 412static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
273e27ca
EA
413 .dot = { .min = 25000, .max = 350000 },
414 .vco = { .min = 1760000, .max = 3510000 },
415 .n = { .min = 1, .max = 3 },
416 .m = { .min = 79, .max = 126 },
417 .m1 = { .min = 12, .max = 22 },
418 .m2 = { .min = 5, .max = 9 },
419 .p = { .min = 14, .max = 42 },
0206e353 420 .p1 = { .min = 2, .max = 6 },
273e27ca
EA
421 .p2 = { .dot_limit = 225000,
422 .p2_slow = 7, .p2_fast = 7 },
4547668a
ZY
423};
424
1b6f4958 425static const struct intel_limit intel_limits_vlv = {
f01b7962
VS
426 /*
427 * These are the data rate limits (measured in fast clocks)
428 * since those are the strictest limits we have. The fast
429 * clock and actual rate limits are more relaxed, so checking
430 * them would make no difference.
431 */
432 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
75e53986 433 .vco = { .min = 4000000, .max = 6000000 },
a0c4da24 434 .n = { .min = 1, .max = 7 },
a0c4da24
JB
435 .m1 = { .min = 2, .max = 3 },
436 .m2 = { .min = 11, .max = 156 },
b99ab663 437 .p1 = { .min = 2, .max = 3 },
5fdc9c49 438 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
a0c4da24
JB
439};
440
1b6f4958 441static const struct intel_limit intel_limits_chv = {
ef9348c8
CML
442 /*
443 * These are the data rate limits (measured in fast clocks)
444 * since those are the strictest limits we have. The fast
445 * clock and actual rate limits are more relaxed, so checking
446 * them would make no difference.
447 */
448 .dot = { .min = 25000 * 5, .max = 540000 * 5},
17fe1021 449 .vco = { .min = 4800000, .max = 6480000 },
ef9348c8
CML
450 .n = { .min = 1, .max = 1 },
451 .m1 = { .min = 2, .max = 2 },
452 .m2 = { .min = 24 << 22, .max = 175 << 22 },
453 .p1 = { .min = 2, .max = 4 },
454 .p2 = { .p2_slow = 1, .p2_fast = 14 },
455};
456
1b6f4958 457static const struct intel_limit intel_limits_bxt = {
5ab7b0b7
ID
458 /* FIXME: find real dot limits */
459 .dot = { .min = 0, .max = INT_MAX },
e6292556 460 .vco = { .min = 4800000, .max = 6700000 },
5ab7b0b7
ID
461 .n = { .min = 1, .max = 1 },
462 .m1 = { .min = 2, .max = 2 },
463 /* FIXME: find real m2 limits */
464 .m2 = { .min = 2 << 22, .max = 255 << 22 },
465 .p1 = { .min = 2, .max = 4 },
466 .p2 = { .p2_slow = 1, .p2_fast = 20 },
467};
468
cdba954e
ACO
469static bool
470needs_modeset(struct drm_crtc_state *state)
471{
fc596660 472 return drm_atomic_crtc_needs_modeset(state);
cdba954e
ACO
473}
474
dccbea3b
ID
475/*
476 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
477 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
478 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
479 * The helpers' return value is the rate of the clock that is fed to the
480 * display engine's pipe which can be the above fast dot clock rate or a
481 * divided-down version of it.
482 */
f2b115e6 483/* m1 is reserved as 0 in Pineview, n is a ring counter */
9e2c8475 484static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
79e53945 485{
2177832f
SL
486 clock->m = clock->m2 + 2;
487 clock->p = clock->p1 * clock->p2;
ed5ca77e 488 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 489 return 0;
fb03ac01
VS
490 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
491 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
492
493 return clock->dot;
2177832f
SL
494}
495
7429e9d4
DV
496static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
497{
498 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
499}
500
9e2c8475 501static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
2177832f 502{
7429e9d4 503 clock->m = i9xx_dpll_compute_m(clock);
79e53945 504 clock->p = clock->p1 * clock->p2;
ed5ca77e 505 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
dccbea3b 506 return 0;
fb03ac01
VS
507 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
508 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
509
510 return clock->dot;
79e53945
JB
511}
512
9e2c8475 513static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
589eca67
ID
514{
515 clock->m = clock->m1 * clock->m2;
516 clock->p = clock->p1 * clock->p2;
517 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 518 return 0;
589eca67
ID
519 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
520 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
521
522 return clock->dot / 5;
589eca67
ID
523}
524
9e2c8475 525int chv_calc_dpll_params(int refclk, struct dpll *clock)
ef9348c8
CML
526{
527 clock->m = clock->m1 * clock->m2;
528 clock->p = clock->p1 * clock->p2;
529 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 530 return 0;
ef9348c8
CML
531 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
532 clock->n << 22);
533 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
534
535 return clock->dot / 5;
ef9348c8
CML
536}
537
7c04d1d9 538#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
79e53945
JB
539/**
540 * Returns whether the given set of divisors are valid for a given refclk with
541 * the given connectors.
542 */
543
e2d214ae 544static bool intel_PLL_is_valid(struct drm_i915_private *dev_priv,
1b6f4958 545 const struct intel_limit *limit,
9e2c8475 546 const struct dpll *clock)
79e53945 547{
f01b7962
VS
548 if (clock->n < limit->n.min || limit->n.max < clock->n)
549 INTELPllInvalid("n out of range\n");
79e53945 550 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
0206e353 551 INTELPllInvalid("p1 out of range\n");
79e53945 552 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
0206e353 553 INTELPllInvalid("m2 out of range\n");
79e53945 554 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
0206e353 555 INTELPllInvalid("m1 out of range\n");
f01b7962 556
e2d214ae 557 if (!IS_PINEVIEW(dev_priv) && !IS_VALLEYVIEW(dev_priv) &&
cc3f90f0 558 !IS_CHERRYVIEW(dev_priv) && !IS_GEN9_LP(dev_priv))
f01b7962
VS
559 if (clock->m1 <= clock->m2)
560 INTELPllInvalid("m1 <= m2\n");
561
e2d214ae 562 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
cc3f90f0 563 !IS_GEN9_LP(dev_priv)) {
f01b7962
VS
564 if (clock->p < limit->p.min || limit->p.max < clock->p)
565 INTELPllInvalid("p out of range\n");
566 if (clock->m < limit->m.min || limit->m.max < clock->m)
567 INTELPllInvalid("m out of range\n");
568 }
569
79e53945 570 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
0206e353 571 INTELPllInvalid("vco out of range\n");
79e53945
JB
572 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
573 * connector, etc., rather than just a single range.
574 */
575 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
0206e353 576 INTELPllInvalid("dot out of range\n");
79e53945
JB
577
578 return true;
579}
580
3b1429d9 581static int
1b6f4958 582i9xx_select_p2_div(const struct intel_limit *limit,
3b1429d9
VS
583 const struct intel_crtc_state *crtc_state,
584 int target)
79e53945 585{
3b1429d9 586 struct drm_device *dev = crtc_state->base.crtc->dev;
79e53945 587
2d84d2b3 588 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
79e53945 589 /*
a210b028
DV
590 * For LVDS just rely on its current settings for dual-channel.
591 * We haven't figured out how to reliably set up different
592 * single/dual channel state, if we even can.
79e53945 593 */
1974cad0 594 if (intel_is_dual_link_lvds(dev))
3b1429d9 595 return limit->p2.p2_fast;
79e53945 596 else
3b1429d9 597 return limit->p2.p2_slow;
79e53945
JB
598 } else {
599 if (target < limit->p2.dot_limit)
3b1429d9 600 return limit->p2.p2_slow;
79e53945 601 else
3b1429d9 602 return limit->p2.p2_fast;
79e53945 603 }
3b1429d9
VS
604}
605
70e8aa21
ACO
606/*
607 * Returns a set of divisors for the desired target clock with the given
608 * refclk, or FALSE. The returned values represent the clock equation:
609 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
610 *
611 * Target and reference clocks are specified in kHz.
612 *
613 * If match_clock is provided, then best_clock P divider must match the P
614 * divider from @match_clock used for LVDS downclocking.
615 */
3b1429d9 616static bool
1b6f4958 617i9xx_find_best_dpll(const struct intel_limit *limit,
3b1429d9 618 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
619 int target, int refclk, struct dpll *match_clock,
620 struct dpll *best_clock)
3b1429d9
VS
621{
622 struct drm_device *dev = crtc_state->base.crtc->dev;
9e2c8475 623 struct dpll clock;
3b1429d9 624 int err = target;
79e53945 625
0206e353 626 memset(best_clock, 0, sizeof(*best_clock));
79e53945 627
3b1429d9
VS
628 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
629
42158660
ZY
630 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
631 clock.m1++) {
632 for (clock.m2 = limit->m2.min;
633 clock.m2 <= limit->m2.max; clock.m2++) {
c0efc387 634 if (clock.m2 >= clock.m1)
42158660
ZY
635 break;
636 for (clock.n = limit->n.min;
637 clock.n <= limit->n.max; clock.n++) {
638 for (clock.p1 = limit->p1.min;
639 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
640 int this_err;
641
dccbea3b 642 i9xx_calc_dpll_params(refclk, &clock);
e2d214ae
TU
643 if (!intel_PLL_is_valid(to_i915(dev),
644 limit,
ac58c3f0
DV
645 &clock))
646 continue;
647 if (match_clock &&
648 clock.p != match_clock->p)
649 continue;
650
651 this_err = abs(clock.dot - target);
652 if (this_err < err) {
653 *best_clock = clock;
654 err = this_err;
655 }
656 }
657 }
658 }
659 }
660
661 return (err != target);
662}
663
70e8aa21
ACO
664/*
665 * Returns a set of divisors for the desired target clock with the given
666 * refclk, or FALSE. The returned values represent the clock equation:
667 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
668 *
669 * Target and reference clocks are specified in kHz.
670 *
671 * If match_clock is provided, then best_clock P divider must match the P
672 * divider from @match_clock used for LVDS downclocking.
673 */
ac58c3f0 674static bool
1b6f4958 675pnv_find_best_dpll(const struct intel_limit *limit,
a93e255f 676 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
677 int target, int refclk, struct dpll *match_clock,
678 struct dpll *best_clock)
79e53945 679{
3b1429d9 680 struct drm_device *dev = crtc_state->base.crtc->dev;
9e2c8475 681 struct dpll clock;
79e53945
JB
682 int err = target;
683
0206e353 684 memset(best_clock, 0, sizeof(*best_clock));
79e53945 685
3b1429d9
VS
686 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
687
42158660
ZY
688 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
689 clock.m1++) {
690 for (clock.m2 = limit->m2.min;
691 clock.m2 <= limit->m2.max; clock.m2++) {
42158660
ZY
692 for (clock.n = limit->n.min;
693 clock.n <= limit->n.max; clock.n++) {
694 for (clock.p1 = limit->p1.min;
695 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
696 int this_err;
697
dccbea3b 698 pnv_calc_dpll_params(refclk, &clock);
e2d214ae
TU
699 if (!intel_PLL_is_valid(to_i915(dev),
700 limit,
1b894b59 701 &clock))
79e53945 702 continue;
cec2f356
SP
703 if (match_clock &&
704 clock.p != match_clock->p)
705 continue;
79e53945
JB
706
707 this_err = abs(clock.dot - target);
708 if (this_err < err) {
709 *best_clock = clock;
710 err = this_err;
711 }
712 }
713 }
714 }
715 }
716
717 return (err != target);
718}
719
997c030c
ACO
720/*
721 * Returns a set of divisors for the desired target clock with the given
722 * refclk, or FALSE. The returned values represent the clock equation:
723 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
70e8aa21
ACO
724 *
725 * Target and reference clocks are specified in kHz.
726 *
727 * If match_clock is provided, then best_clock P divider must match the P
728 * divider from @match_clock used for LVDS downclocking.
997c030c 729 */
d4906093 730static bool
1b6f4958 731g4x_find_best_dpll(const struct intel_limit *limit,
a93e255f 732 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
733 int target, int refclk, struct dpll *match_clock,
734 struct dpll *best_clock)
d4906093 735{
3b1429d9 736 struct drm_device *dev = crtc_state->base.crtc->dev;
9e2c8475 737 struct dpll clock;
d4906093 738 int max_n;
3b1429d9 739 bool found = false;
6ba770dc
AJ
740 /* approximately equals target * 0.00585 */
741 int err_most = (target >> 8) + (target >> 9);
d4906093
ML
742
743 memset(best_clock, 0, sizeof(*best_clock));
3b1429d9
VS
744
745 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
746
d4906093 747 max_n = limit->n.max;
f77f13e2 748 /* based on hardware requirement, prefer smaller n to precision */
d4906093 749 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
f77f13e2 750 /* based on hardware requirement, prefere larger m1,m2 */
d4906093
ML
751 for (clock.m1 = limit->m1.max;
752 clock.m1 >= limit->m1.min; clock.m1--) {
753 for (clock.m2 = limit->m2.max;
754 clock.m2 >= limit->m2.min; clock.m2--) {
755 for (clock.p1 = limit->p1.max;
756 clock.p1 >= limit->p1.min; clock.p1--) {
757 int this_err;
758
dccbea3b 759 i9xx_calc_dpll_params(refclk, &clock);
e2d214ae
TU
760 if (!intel_PLL_is_valid(to_i915(dev),
761 limit,
1b894b59 762 &clock))
d4906093 763 continue;
1b894b59
CW
764
765 this_err = abs(clock.dot - target);
d4906093
ML
766 if (this_err < err_most) {
767 *best_clock = clock;
768 err_most = this_err;
769 max_n = clock.n;
770 found = true;
771 }
772 }
773 }
774 }
775 }
2c07245f
ZW
776 return found;
777}
778
d5dd62bd
ID
779/*
780 * Check if the calculated PLL configuration is more optimal compared to the
781 * best configuration and error found so far. Return the calculated error.
782 */
783static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
9e2c8475
ACO
784 const struct dpll *calculated_clock,
785 const struct dpll *best_clock,
d5dd62bd
ID
786 unsigned int best_error_ppm,
787 unsigned int *error_ppm)
788{
9ca3ba01
ID
789 /*
790 * For CHV ignore the error and consider only the P value.
791 * Prefer a bigger P value based on HW requirements.
792 */
920a14b2 793 if (IS_CHERRYVIEW(to_i915(dev))) {
9ca3ba01
ID
794 *error_ppm = 0;
795
796 return calculated_clock->p > best_clock->p;
797 }
798
24be4e46
ID
799 if (WARN_ON_ONCE(!target_freq))
800 return false;
801
d5dd62bd
ID
802 *error_ppm = div_u64(1000000ULL *
803 abs(target_freq - calculated_clock->dot),
804 target_freq);
805 /*
806 * Prefer a better P value over a better (smaller) error if the error
807 * is small. Ensure this preference for future configurations too by
808 * setting the error to 0.
809 */
810 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
811 *error_ppm = 0;
812
813 return true;
814 }
815
816 return *error_ppm + 10 < best_error_ppm;
817}
818
65b3d6a9
ACO
819/*
820 * Returns a set of divisors for the desired target clock with the given
821 * refclk, or FALSE. The returned values represent the clock equation:
822 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
823 */
a0c4da24 824static bool
1b6f4958 825vlv_find_best_dpll(const struct intel_limit *limit,
a93e255f 826 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
827 int target, int refclk, struct dpll *match_clock,
828 struct dpll *best_clock)
a0c4da24 829{
a93e255f 830 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 831 struct drm_device *dev = crtc->base.dev;
9e2c8475 832 struct dpll clock;
69e4f900 833 unsigned int bestppm = 1000000;
27e639bf
VS
834 /* min update 19.2 MHz */
835 int max_n = min(limit->n.max, refclk / 19200);
49e497ef 836 bool found = false;
a0c4da24 837
6b4bf1c4
VS
838 target *= 5; /* fast clock */
839
840 memset(best_clock, 0, sizeof(*best_clock));
a0c4da24
JB
841
842 /* based on hardware requirement, prefer smaller n to precision */
27e639bf 843 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
811bbf05 844 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
889059d8 845 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
c1a9ae43 846 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
6b4bf1c4 847 clock.p = clock.p1 * clock.p2;
a0c4da24 848 /* based on hardware requirement, prefer bigger m1,m2 values */
6b4bf1c4 849 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
d5dd62bd 850 unsigned int ppm;
69e4f900 851
6b4bf1c4
VS
852 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
853 refclk * clock.m1);
854
dccbea3b 855 vlv_calc_dpll_params(refclk, &clock);
43b0ac53 856
e2d214ae
TU
857 if (!intel_PLL_is_valid(to_i915(dev),
858 limit,
f01b7962 859 &clock))
43b0ac53
VS
860 continue;
861
d5dd62bd
ID
862 if (!vlv_PLL_is_optimal(dev, target,
863 &clock,
864 best_clock,
865 bestppm, &ppm))
866 continue;
6b4bf1c4 867
d5dd62bd
ID
868 *best_clock = clock;
869 bestppm = ppm;
870 found = true;
a0c4da24
JB
871 }
872 }
873 }
874 }
a0c4da24 875
49e497ef 876 return found;
a0c4da24 877}
a4fc5ed6 878
65b3d6a9
ACO
879/*
880 * Returns a set of divisors for the desired target clock with the given
881 * refclk, or FALSE. The returned values represent the clock equation:
882 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
883 */
ef9348c8 884static bool
1b6f4958 885chv_find_best_dpll(const struct intel_limit *limit,
a93e255f 886 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
887 int target, int refclk, struct dpll *match_clock,
888 struct dpll *best_clock)
ef9348c8 889{
a93e255f 890 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 891 struct drm_device *dev = crtc->base.dev;
9ca3ba01 892 unsigned int best_error_ppm;
9e2c8475 893 struct dpll clock;
ef9348c8
CML
894 uint64_t m2;
895 int found = false;
896
897 memset(best_clock, 0, sizeof(*best_clock));
9ca3ba01 898 best_error_ppm = 1000000;
ef9348c8
CML
899
900 /*
901 * Based on hardware doc, the n always set to 1, and m1 always
902 * set to 2. If requires to support 200Mhz refclk, we need to
903 * revisit this because n may not 1 anymore.
904 */
905 clock.n = 1, clock.m1 = 2;
906 target *= 5; /* fast clock */
907
908 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
909 for (clock.p2 = limit->p2.p2_fast;
910 clock.p2 >= limit->p2.p2_slow;
911 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
9ca3ba01 912 unsigned int error_ppm;
ef9348c8
CML
913
914 clock.p = clock.p1 * clock.p2;
915
916 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
917 clock.n) << 22, refclk * clock.m1);
918
919 if (m2 > INT_MAX/clock.m1)
920 continue;
921
922 clock.m2 = m2;
923
dccbea3b 924 chv_calc_dpll_params(refclk, &clock);
ef9348c8 925
e2d214ae 926 if (!intel_PLL_is_valid(to_i915(dev), limit, &clock))
ef9348c8
CML
927 continue;
928
9ca3ba01
ID
929 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
930 best_error_ppm, &error_ppm))
931 continue;
932
933 *best_clock = clock;
934 best_error_ppm = error_ppm;
935 found = true;
ef9348c8
CML
936 }
937 }
938
939 return found;
940}
941
5ab7b0b7 942bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
9e2c8475 943 struct dpll *best_clock)
5ab7b0b7 944{
65b3d6a9 945 int refclk = 100000;
1b6f4958 946 const struct intel_limit *limit = &intel_limits_bxt;
5ab7b0b7 947
65b3d6a9 948 return chv_find_best_dpll(limit, crtc_state,
5ab7b0b7
ID
949 target_clock, refclk, NULL, best_clock);
950}
951
525b9311 952bool intel_crtc_active(struct intel_crtc *crtc)
20ddf665 953{
20ddf665
VS
954 /* Be paranoid as we can arrive here with only partial
955 * state retrieved from the hardware during setup.
956 *
241bfc38 957 * We can ditch the adjusted_mode.crtc_clock check as soon
20ddf665
VS
958 * as Haswell has gained clock readout/fastboot support.
959 *
66e514c1 960 * We can ditch the crtc->primary->fb check as soon as we can
20ddf665 961 * properly reconstruct framebuffers.
c3d1f436
MR
962 *
963 * FIXME: The intel_crtc->active here should be switched to
964 * crtc->state->active once we have proper CRTC states wired up
965 * for atomic.
20ddf665 966 */
525b9311
VS
967 return crtc->active && crtc->base.primary->state->fb &&
968 crtc->config->base.adjusted_mode.crtc_clock;
20ddf665
VS
969}
970
a5c961d1
PZ
971enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
972 enum pipe pipe)
973{
98187836 974 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
a5c961d1 975
e2af48c6 976 return crtc->config->cpu_transcoder;
a5c961d1
PZ
977}
978
6315b5d3 979static bool pipe_dsl_stopped(struct drm_i915_private *dev_priv, enum pipe pipe)
fbf49ea2 980{
f0f59a00 981 i915_reg_t reg = PIPEDSL(pipe);
fbf49ea2
VS
982 u32 line1, line2;
983 u32 line_mask;
984
5db94019 985 if (IS_GEN2(dev_priv))
fbf49ea2
VS
986 line_mask = DSL_LINEMASK_GEN2;
987 else
988 line_mask = DSL_LINEMASK_GEN3;
989
990 line1 = I915_READ(reg) & line_mask;
6adfb1ef 991 msleep(5);
fbf49ea2
VS
992 line2 = I915_READ(reg) & line_mask;
993
994 return line1 == line2;
995}
996
ab7ad7f6
KP
997/*
998 * intel_wait_for_pipe_off - wait for pipe to turn off
575f7ab7 999 * @crtc: crtc whose pipe to wait for
9d0498a2
JB
1000 *
1001 * After disabling a pipe, we can't wait for vblank in the usual way,
1002 * spinning on the vblank interrupt status bit, since we won't actually
1003 * see an interrupt when the pipe is disabled.
1004 *
ab7ad7f6
KP
1005 * On Gen4 and above:
1006 * wait for the pipe register state bit to turn off
1007 *
1008 * Otherwise:
1009 * wait for the display line value to settle (it usually
1010 * ends up stopping at the start of the next frame).
58e10eb9 1011 *
9d0498a2 1012 */
575f7ab7 1013static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
9d0498a2 1014{
6315b5d3 1015 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6e3c9717 1016 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 1017 enum pipe pipe = crtc->pipe;
ab7ad7f6 1018
6315b5d3 1019 if (INTEL_GEN(dev_priv) >= 4) {
f0f59a00 1020 i915_reg_t reg = PIPECONF(cpu_transcoder);
ab7ad7f6
KP
1021
1022 /* Wait for the Pipe State to go off */
b8511f53
CW
1023 if (intel_wait_for_register(dev_priv,
1024 reg, I965_PIPECONF_ACTIVE, 0,
1025 100))
284637d9 1026 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1027 } else {
ab7ad7f6 1028 /* Wait for the display line to settle */
6315b5d3 1029 if (wait_for(pipe_dsl_stopped(dev_priv, pipe), 100))
284637d9 1030 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1031 }
79e53945
JB
1032}
1033
b24e7179 1034/* Only for pre-ILK configs */
55607e8a
DV
1035void assert_pll(struct drm_i915_private *dev_priv,
1036 enum pipe pipe, bool state)
b24e7179 1037{
b24e7179
JB
1038 u32 val;
1039 bool cur_state;
1040
649636ef 1041 val = I915_READ(DPLL(pipe));
b24e7179 1042 cur_state = !!(val & DPLL_VCO_ENABLE);
e2c719b7 1043 I915_STATE_WARN(cur_state != state,
b24e7179 1044 "PLL state assertion failure (expected %s, current %s)\n",
87ad3212 1045 onoff(state), onoff(cur_state));
b24e7179 1046}
b24e7179 1047
23538ef1 1048/* XXX: the dsi pll is shared between MIPI DSI ports */
8563b1e8 1049void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
23538ef1
JN
1050{
1051 u32 val;
1052 bool cur_state;
1053
a580516d 1054 mutex_lock(&dev_priv->sb_lock);
23538ef1 1055 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
a580516d 1056 mutex_unlock(&dev_priv->sb_lock);
23538ef1
JN
1057
1058 cur_state = val & DSI_PLL_VCO_EN;
e2c719b7 1059 I915_STATE_WARN(cur_state != state,
23538ef1 1060 "DSI PLL state assertion failure (expected %s, current %s)\n",
87ad3212 1061 onoff(state), onoff(cur_state));
23538ef1 1062}
23538ef1 1063
040484af
JB
1064static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1065 enum pipe pipe, bool state)
1066{
040484af 1067 bool cur_state;
ad80a810
PZ
1068 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1069 pipe);
040484af 1070
2d1fe073 1071 if (HAS_DDI(dev_priv)) {
affa9354 1072 /* DDI does not have a specific FDI_TX register */
649636ef 1073 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
ad80a810 1074 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
bf507ef7 1075 } else {
649636ef 1076 u32 val = I915_READ(FDI_TX_CTL(pipe));
bf507ef7
ED
1077 cur_state = !!(val & FDI_TX_ENABLE);
1078 }
e2c719b7 1079 I915_STATE_WARN(cur_state != state,
040484af 1080 "FDI TX state assertion failure (expected %s, current %s)\n",
87ad3212 1081 onoff(state), onoff(cur_state));
040484af
JB
1082}
1083#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1084#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1085
1086static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1087 enum pipe pipe, bool state)
1088{
040484af
JB
1089 u32 val;
1090 bool cur_state;
1091
649636ef 1092 val = I915_READ(FDI_RX_CTL(pipe));
d63fa0dc 1093 cur_state = !!(val & FDI_RX_ENABLE);
e2c719b7 1094 I915_STATE_WARN(cur_state != state,
040484af 1095 "FDI RX state assertion failure (expected %s, current %s)\n",
87ad3212 1096 onoff(state), onoff(cur_state));
040484af
JB
1097}
1098#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1099#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1100
1101static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1102 enum pipe pipe)
1103{
040484af
JB
1104 u32 val;
1105
1106 /* ILK FDI PLL is always enabled */
7e22dbbb 1107 if (IS_GEN5(dev_priv))
040484af
JB
1108 return;
1109
bf507ef7 1110 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
2d1fe073 1111 if (HAS_DDI(dev_priv))
bf507ef7
ED
1112 return;
1113
649636ef 1114 val = I915_READ(FDI_TX_CTL(pipe));
e2c719b7 1115 I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
040484af
JB
1116}
1117
55607e8a
DV
1118void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1119 enum pipe pipe, bool state)
040484af 1120{
040484af 1121 u32 val;
55607e8a 1122 bool cur_state;
040484af 1123
649636ef 1124 val = I915_READ(FDI_RX_CTL(pipe));
55607e8a 1125 cur_state = !!(val & FDI_RX_PLL_ENABLE);
e2c719b7 1126 I915_STATE_WARN(cur_state != state,
55607e8a 1127 "FDI RX PLL assertion failure (expected %s, current %s)\n",
87ad3212 1128 onoff(state), onoff(cur_state));
040484af
JB
1129}
1130
4f8036a2 1131void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
ea0760cf 1132{
f0f59a00 1133 i915_reg_t pp_reg;
ea0760cf
JB
1134 u32 val;
1135 enum pipe panel_pipe = PIPE_A;
0de3b485 1136 bool locked = true;
ea0760cf 1137
4f8036a2 1138 if (WARN_ON(HAS_DDI(dev_priv)))
bedd4dba
JN
1139 return;
1140
4f8036a2 1141 if (HAS_PCH_SPLIT(dev_priv)) {
bedd4dba
JN
1142 u32 port_sel;
1143
44cb734c
ID
1144 pp_reg = PP_CONTROL(0);
1145 port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
bedd4dba
JN
1146
1147 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1148 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1149 panel_pipe = PIPE_B;
1150 /* XXX: else fix for eDP */
4f8036a2 1151 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
bedd4dba 1152 /* presumably write lock depends on pipe, not port select */
44cb734c 1153 pp_reg = PP_CONTROL(pipe);
bedd4dba 1154 panel_pipe = pipe;
ea0760cf 1155 } else {
44cb734c 1156 pp_reg = PP_CONTROL(0);
bedd4dba
JN
1157 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1158 panel_pipe = PIPE_B;
ea0760cf
JB
1159 }
1160
1161 val = I915_READ(pp_reg);
1162 if (!(val & PANEL_POWER_ON) ||
ec49ba2d 1163 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
ea0760cf
JB
1164 locked = false;
1165
e2c719b7 1166 I915_STATE_WARN(panel_pipe == pipe && locked,
ea0760cf 1167 "panel assertion failure, pipe %c regs locked\n",
9db4a9c7 1168 pipe_name(pipe));
ea0760cf
JB
1169}
1170
93ce0ba6
JN
1171static void assert_cursor(struct drm_i915_private *dev_priv,
1172 enum pipe pipe, bool state)
1173{
93ce0ba6
JN
1174 bool cur_state;
1175
2a307c2e 1176 if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
0b87c24e 1177 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
d9d82081 1178 else
5efb3e28 1179 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
93ce0ba6 1180
e2c719b7 1181 I915_STATE_WARN(cur_state != state,
93ce0ba6 1182 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
87ad3212 1183 pipe_name(pipe), onoff(state), onoff(cur_state));
93ce0ba6
JN
1184}
1185#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1186#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1187
b840d907
JB
1188void assert_pipe(struct drm_i915_private *dev_priv,
1189 enum pipe pipe, bool state)
b24e7179 1190{
63d7bbe9 1191 bool cur_state;
702e7a56
PZ
1192 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1193 pipe);
4feed0eb 1194 enum intel_display_power_domain power_domain;
b24e7179 1195
e56134bc
VS
1196 /* we keep both pipes enabled on 830 */
1197 if (IS_I830(dev_priv))
8e636784
DV
1198 state = true;
1199
4feed0eb
ID
1200 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1201 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
649636ef 1202 u32 val = I915_READ(PIPECONF(cpu_transcoder));
69310161 1203 cur_state = !!(val & PIPECONF_ENABLE);
4feed0eb
ID
1204
1205 intel_display_power_put(dev_priv, power_domain);
1206 } else {
1207 cur_state = false;
69310161
PZ
1208 }
1209
e2c719b7 1210 I915_STATE_WARN(cur_state != state,
63d7bbe9 1211 "pipe %c assertion failure (expected %s, current %s)\n",
87ad3212 1212 pipe_name(pipe), onoff(state), onoff(cur_state));
b24e7179
JB
1213}
1214
931872fc
CW
1215static void assert_plane(struct drm_i915_private *dev_priv,
1216 enum plane plane, bool state)
b24e7179 1217{
b24e7179 1218 u32 val;
931872fc 1219 bool cur_state;
b24e7179 1220
649636ef 1221 val = I915_READ(DSPCNTR(plane));
931872fc 1222 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
e2c719b7 1223 I915_STATE_WARN(cur_state != state,
931872fc 1224 "plane %c assertion failure (expected %s, current %s)\n",
87ad3212 1225 plane_name(plane), onoff(state), onoff(cur_state));
b24e7179
JB
1226}
1227
931872fc
CW
1228#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1229#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1230
b24e7179
JB
1231static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1232 enum pipe pipe)
1233{
649636ef 1234 int i;
b24e7179 1235
653e1026 1236 /* Primary planes are fixed to pipes on gen4+ */
6315b5d3 1237 if (INTEL_GEN(dev_priv) >= 4) {
649636ef 1238 u32 val = I915_READ(DSPCNTR(pipe));
e2c719b7 1239 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
28c05794
AJ
1240 "plane %c assertion failure, should be disabled but not\n",
1241 plane_name(pipe));
19ec1358 1242 return;
28c05794 1243 }
19ec1358 1244
b24e7179 1245 /* Need to check both planes against the pipe */
055e393f 1246 for_each_pipe(dev_priv, i) {
649636ef
VS
1247 u32 val = I915_READ(DSPCNTR(i));
1248 enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
b24e7179 1249 DISPPLANE_SEL_PIPE_SHIFT;
e2c719b7 1250 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
9db4a9c7
JB
1251 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1252 plane_name(i), pipe_name(pipe));
b24e7179
JB
1253 }
1254}
1255
19332d7a
JB
1256static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1257 enum pipe pipe)
1258{
649636ef 1259 int sprite;
19332d7a 1260
6315b5d3 1261 if (INTEL_GEN(dev_priv) >= 9) {
3bdcfc0c 1262 for_each_sprite(dev_priv, pipe, sprite) {
649636ef 1263 u32 val = I915_READ(PLANE_CTL(pipe, sprite));
e2c719b7 1264 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
7feb8b88
DL
1265 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1266 sprite, pipe_name(pipe));
1267 }
920a14b2 1268 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
3bdcfc0c 1269 for_each_sprite(dev_priv, pipe, sprite) {
83c04a62 1270 u32 val = I915_READ(SPCNTR(pipe, PLANE_SPRITE0 + sprite));
e2c719b7 1271 I915_STATE_WARN(val & SP_ENABLE,
20674eef 1272 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1fe47785 1273 sprite_name(pipe, sprite), pipe_name(pipe));
20674eef 1274 }
6315b5d3 1275 } else if (INTEL_GEN(dev_priv) >= 7) {
649636ef 1276 u32 val = I915_READ(SPRCTL(pipe));
e2c719b7 1277 I915_STATE_WARN(val & SPRITE_ENABLE,
06da8da2 1278 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef 1279 plane_name(pipe), pipe_name(pipe));
ab33081a 1280 } else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv)) {
649636ef 1281 u32 val = I915_READ(DVSCNTR(pipe));
e2c719b7 1282 I915_STATE_WARN(val & DVS_ENABLE,
06da8da2 1283 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef 1284 plane_name(pipe), pipe_name(pipe));
19332d7a
JB
1285 }
1286}
1287
08c71e5e
VS
1288static void assert_vblank_disabled(struct drm_crtc *crtc)
1289{
e2c719b7 1290 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
08c71e5e
VS
1291 drm_crtc_vblank_put(crtc);
1292}
1293
7abd4b35
ACO
1294void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1295 enum pipe pipe)
92f2584a 1296{
92f2584a
JB
1297 u32 val;
1298 bool enabled;
1299
649636ef 1300 val = I915_READ(PCH_TRANSCONF(pipe));
92f2584a 1301 enabled = !!(val & TRANS_ENABLE);
e2c719b7 1302 I915_STATE_WARN(enabled,
9db4a9c7
JB
1303 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1304 pipe_name(pipe));
92f2584a
JB
1305}
1306
4e634389
KP
1307static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1308 enum pipe pipe, u32 port_sel, u32 val)
f0575e92
KP
1309{
1310 if ((val & DP_PORT_EN) == 0)
1311 return false;
1312
2d1fe073 1313 if (HAS_PCH_CPT(dev_priv)) {
f0f59a00 1314 u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
f0575e92
KP
1315 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1316 return false;
2d1fe073 1317 } else if (IS_CHERRYVIEW(dev_priv)) {
44f37d1f
CML
1318 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1319 return false;
f0575e92
KP
1320 } else {
1321 if ((val & DP_PIPE_MASK) != (pipe << 30))
1322 return false;
1323 }
1324 return true;
1325}
1326
1519b995
KP
1327static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1328 enum pipe pipe, u32 val)
1329{
dc0fa718 1330 if ((val & SDVO_ENABLE) == 0)
1519b995
KP
1331 return false;
1332
2d1fe073 1333 if (HAS_PCH_CPT(dev_priv)) {
dc0fa718 1334 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1519b995 1335 return false;
2d1fe073 1336 } else if (IS_CHERRYVIEW(dev_priv)) {
44f37d1f
CML
1337 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1338 return false;
1519b995 1339 } else {
dc0fa718 1340 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1519b995
KP
1341 return false;
1342 }
1343 return true;
1344}
1345
1346static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1347 enum pipe pipe, u32 val)
1348{
1349 if ((val & LVDS_PORT_EN) == 0)
1350 return false;
1351
2d1fe073 1352 if (HAS_PCH_CPT(dev_priv)) {
1519b995
KP
1353 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1354 return false;
1355 } else {
1356 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1357 return false;
1358 }
1359 return true;
1360}
1361
1362static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1363 enum pipe pipe, u32 val)
1364{
1365 if ((val & ADPA_DAC_ENABLE) == 0)
1366 return false;
2d1fe073 1367 if (HAS_PCH_CPT(dev_priv)) {
1519b995
KP
1368 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1369 return false;
1370 } else {
1371 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1372 return false;
1373 }
1374 return true;
1375}
1376
291906f1 1377static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
f0f59a00
VS
1378 enum pipe pipe, i915_reg_t reg,
1379 u32 port_sel)
291906f1 1380{
47a05eca 1381 u32 val = I915_READ(reg);
e2c719b7 1382 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
291906f1 1383 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
f0f59a00 1384 i915_mmio_reg_offset(reg), pipe_name(pipe));
de9a35ab 1385
2d1fe073 1386 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
75c5da27 1387 && (val & DP_PIPEB_SELECT),
de9a35ab 1388 "IBX PCH dp port still using transcoder B\n");
291906f1
JB
1389}
1390
1391static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
f0f59a00 1392 enum pipe pipe, i915_reg_t reg)
291906f1 1393{
47a05eca 1394 u32 val = I915_READ(reg);
e2c719b7 1395 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
23c99e77 1396 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
f0f59a00 1397 i915_mmio_reg_offset(reg), pipe_name(pipe));
de9a35ab 1398
2d1fe073 1399 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
75c5da27 1400 && (val & SDVO_PIPE_B_SELECT),
de9a35ab 1401 "IBX PCH hdmi port still using transcoder B\n");
291906f1
JB
1402}
1403
1404static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1405 enum pipe pipe)
1406{
291906f1 1407 u32 val;
291906f1 1408
f0575e92
KP
1409 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1410 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1411 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
291906f1 1412
649636ef 1413 val = I915_READ(PCH_ADPA);
e2c719b7 1414 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
291906f1 1415 "PCH VGA enabled on transcoder %c, should be disabled\n",
9db4a9c7 1416 pipe_name(pipe));
291906f1 1417
649636ef 1418 val = I915_READ(PCH_LVDS);
e2c719b7 1419 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
291906f1 1420 "PCH LVDS enabled on transcoder %c, should be disabled\n",
9db4a9c7 1421 pipe_name(pipe));
291906f1 1422
e2debe91
PZ
1423 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1424 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1425 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
291906f1
JB
1426}
1427
cd2d34d9
VS
1428static void _vlv_enable_pll(struct intel_crtc *crtc,
1429 const struct intel_crtc_state *pipe_config)
1430{
1431 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1432 enum pipe pipe = crtc->pipe;
1433
1434 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1435 POSTING_READ(DPLL(pipe));
1436 udelay(150);
1437
2c30b43b
CW
1438 if (intel_wait_for_register(dev_priv,
1439 DPLL(pipe),
1440 DPLL_LOCK_VLV,
1441 DPLL_LOCK_VLV,
1442 1))
cd2d34d9
VS
1443 DRM_ERROR("DPLL %d failed to lock\n", pipe);
1444}
1445
d288f65f 1446static void vlv_enable_pll(struct intel_crtc *crtc,
5cec258b 1447 const struct intel_crtc_state *pipe_config)
87442f73 1448{
cd2d34d9 1449 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8bd3f301 1450 enum pipe pipe = crtc->pipe;
87442f73 1451
8bd3f301 1452 assert_pipe_disabled(dev_priv, pipe);
87442f73 1453
87442f73 1454 /* PLL is protected by panel, make sure we can write it */
7d1a83cb 1455 assert_panel_unlocked(dev_priv, pipe);
87442f73 1456
cd2d34d9
VS
1457 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1458 _vlv_enable_pll(crtc, pipe_config);
426115cf 1459
8bd3f301
VS
1460 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1461 POSTING_READ(DPLL_MD(pipe));
87442f73
DV
1462}
1463
cd2d34d9
VS
1464
1465static void _chv_enable_pll(struct intel_crtc *crtc,
1466 const struct intel_crtc_state *pipe_config)
9d556c99 1467{
cd2d34d9 1468 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8bd3f301 1469 enum pipe pipe = crtc->pipe;
9d556c99 1470 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9d556c99
CML
1471 u32 tmp;
1472
a580516d 1473 mutex_lock(&dev_priv->sb_lock);
9d556c99
CML
1474
1475 /* Enable back the 10bit clock to display controller */
1476 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1477 tmp |= DPIO_DCLKP_EN;
1478 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1479
54433e91
VS
1480 mutex_unlock(&dev_priv->sb_lock);
1481
9d556c99
CML
1482 /*
1483 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1484 */
1485 udelay(1);
1486
1487 /* Enable PLL */
d288f65f 1488 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
9d556c99
CML
1489
1490 /* Check PLL is locked */
6b18826a
CW
1491 if (intel_wait_for_register(dev_priv,
1492 DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
1493 1))
9d556c99 1494 DRM_ERROR("PLL %d failed to lock\n", pipe);
cd2d34d9
VS
1495}
1496
1497static void chv_enable_pll(struct intel_crtc *crtc,
1498 const struct intel_crtc_state *pipe_config)
1499{
1500 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1501 enum pipe pipe = crtc->pipe;
1502
1503 assert_pipe_disabled(dev_priv, pipe);
1504
1505 /* PLL is protected by panel, make sure we can write it */
1506 assert_panel_unlocked(dev_priv, pipe);
1507
1508 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1509 _chv_enable_pll(crtc, pipe_config);
9d556c99 1510
c231775c
VS
1511 if (pipe != PIPE_A) {
1512 /*
1513 * WaPixelRepeatModeFixForC0:chv
1514 *
1515 * DPLLCMD is AWOL. Use chicken bits to propagate
1516 * the value from DPLLBMD to either pipe B or C.
1517 */
1518 I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
1519 I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
1520 I915_WRITE(CBR4_VLV, 0);
1521 dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
1522
1523 /*
1524 * DPLLB VGA mode also seems to cause problems.
1525 * We should always have it disabled.
1526 */
1527 WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
1528 } else {
1529 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1530 POSTING_READ(DPLL_MD(pipe));
1531 }
9d556c99
CML
1532}
1533
6315b5d3 1534static int intel_num_dvo_pipes(struct drm_i915_private *dev_priv)
1c4e0274
VS
1535{
1536 struct intel_crtc *crtc;
1537 int count = 0;
1538
6315b5d3 1539 for_each_intel_crtc(&dev_priv->drm, crtc) {
3538b9df 1540 count += crtc->base.state->active &&
2d84d2b3
VS
1541 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
1542 }
1c4e0274
VS
1543
1544 return count;
1545}
1546
66e3d5c0 1547static void i9xx_enable_pll(struct intel_crtc *crtc)
63d7bbe9 1548{
6315b5d3 1549 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
f0f59a00 1550 i915_reg_t reg = DPLL(crtc->pipe);
6e3c9717 1551 u32 dpll = crtc->config->dpll_hw_state.dpll;
bb408dd2 1552 int i;
63d7bbe9 1553
66e3d5c0 1554 assert_pipe_disabled(dev_priv, crtc->pipe);
58c6eaa2 1555
63d7bbe9 1556 /* PLL is protected by panel, make sure we can write it */
50a0bc90 1557 if (IS_MOBILE(dev_priv) && !IS_I830(dev_priv))
66e3d5c0 1558 assert_panel_unlocked(dev_priv, crtc->pipe);
63d7bbe9 1559
1c4e0274 1560 /* Enable DVO 2x clock on both PLLs if necessary */
6315b5d3 1561 if (IS_I830(dev_priv) && intel_num_dvo_pipes(dev_priv) > 0) {
1c4e0274
VS
1562 /*
1563 * It appears to be important that we don't enable this
1564 * for the current pipe before otherwise configuring the
1565 * PLL. No idea how this should be handled if multiple
1566 * DVO outputs are enabled simultaneosly.
1567 */
1568 dpll |= DPLL_DVO_2X_MODE;
1569 I915_WRITE(DPLL(!crtc->pipe),
1570 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1571 }
66e3d5c0 1572
c2b63374
VS
1573 /*
1574 * Apparently we need to have VGA mode enabled prior to changing
1575 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1576 * dividers, even though the register value does change.
1577 */
1578 I915_WRITE(reg, 0);
1579
8e7a65aa
VS
1580 I915_WRITE(reg, dpll);
1581
66e3d5c0
DV
1582 /* Wait for the clocks to stabilize. */
1583 POSTING_READ(reg);
1584 udelay(150);
1585
6315b5d3 1586 if (INTEL_GEN(dev_priv) >= 4) {
66e3d5c0 1587 I915_WRITE(DPLL_MD(crtc->pipe),
6e3c9717 1588 crtc->config->dpll_hw_state.dpll_md);
66e3d5c0
DV
1589 } else {
1590 /* The pixel multiplier can only be updated once the
1591 * DPLL is enabled and the clocks are stable.
1592 *
1593 * So write it again.
1594 */
1595 I915_WRITE(reg, dpll);
1596 }
63d7bbe9
JB
1597
1598 /* We do this three times for luck */
bb408dd2
VS
1599 for (i = 0; i < 3; i++) {
1600 I915_WRITE(reg, dpll);
1601 POSTING_READ(reg);
1602 udelay(150); /* wait for warmup */
1603 }
63d7bbe9
JB
1604}
1605
1606/**
50b44a44 1607 * i9xx_disable_pll - disable a PLL
63d7bbe9
JB
1608 * @dev_priv: i915 private structure
1609 * @pipe: pipe PLL to disable
1610 *
1611 * Disable the PLL for @pipe, making sure the pipe is off first.
1612 *
1613 * Note! This is for pre-ILK only.
1614 */
1c4e0274 1615static void i9xx_disable_pll(struct intel_crtc *crtc)
63d7bbe9 1616{
6315b5d3 1617 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1c4e0274
VS
1618 enum pipe pipe = crtc->pipe;
1619
1620 /* Disable DVO 2x clock on both PLLs if necessary */
50a0bc90 1621 if (IS_I830(dev_priv) &&
2d84d2b3 1622 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
6315b5d3 1623 !intel_num_dvo_pipes(dev_priv)) {
1c4e0274
VS
1624 I915_WRITE(DPLL(PIPE_B),
1625 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1626 I915_WRITE(DPLL(PIPE_A),
1627 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1628 }
1629
b6b5d049 1630 /* Don't disable pipe or pipe PLLs if needed */
e56134bc 1631 if (IS_I830(dev_priv))
63d7bbe9
JB
1632 return;
1633
1634 /* Make sure the pipe isn't still relying on us */
1635 assert_pipe_disabled(dev_priv, pipe);
1636
b8afb911 1637 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
50b44a44 1638 POSTING_READ(DPLL(pipe));
63d7bbe9
JB
1639}
1640
f6071166
JB
1641static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1642{
b8afb911 1643 u32 val;
f6071166
JB
1644
1645 /* Make sure the pipe isn't still relying on us */
1646 assert_pipe_disabled(dev_priv, pipe);
1647
03ed5cbf
VS
1648 val = DPLL_INTEGRATED_REF_CLK_VLV |
1649 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1650 if (pipe != PIPE_A)
1651 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1652
f6071166
JB
1653 I915_WRITE(DPLL(pipe), val);
1654 POSTING_READ(DPLL(pipe));
076ed3b2
CML
1655}
1656
1657static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1658{
d752048d 1659 enum dpio_channel port = vlv_pipe_to_channel(pipe);
076ed3b2
CML
1660 u32 val;
1661
a11b0703
VS
1662 /* Make sure the pipe isn't still relying on us */
1663 assert_pipe_disabled(dev_priv, pipe);
076ed3b2 1664
60bfe44f
VS
1665 val = DPLL_SSC_REF_CLK_CHV |
1666 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
a11b0703
VS
1667 if (pipe != PIPE_A)
1668 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
03ed5cbf 1669
a11b0703
VS
1670 I915_WRITE(DPLL(pipe), val);
1671 POSTING_READ(DPLL(pipe));
d752048d 1672
a580516d 1673 mutex_lock(&dev_priv->sb_lock);
d752048d
VS
1674
1675 /* Disable 10bit clock to display controller */
1676 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1677 val &= ~DPIO_DCLKP_EN;
1678 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1679
a580516d 1680 mutex_unlock(&dev_priv->sb_lock);
f6071166
JB
1681}
1682
e4607fcf 1683void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
9b6de0a1
VS
1684 struct intel_digital_port *dport,
1685 unsigned int expected_mask)
89b667f8
JB
1686{
1687 u32 port_mask;
f0f59a00 1688 i915_reg_t dpll_reg;
89b667f8 1689
e4607fcf
CML
1690 switch (dport->port) {
1691 case PORT_B:
89b667f8 1692 port_mask = DPLL_PORTB_READY_MASK;
00fc31b7 1693 dpll_reg = DPLL(0);
e4607fcf
CML
1694 break;
1695 case PORT_C:
89b667f8 1696 port_mask = DPLL_PORTC_READY_MASK;
00fc31b7 1697 dpll_reg = DPLL(0);
9b6de0a1 1698 expected_mask <<= 4;
00fc31b7
CML
1699 break;
1700 case PORT_D:
1701 port_mask = DPLL_PORTD_READY_MASK;
1702 dpll_reg = DPIO_PHY_STATUS;
e4607fcf
CML
1703 break;
1704 default:
1705 BUG();
1706 }
89b667f8 1707
370004d3
CW
1708 if (intel_wait_for_register(dev_priv,
1709 dpll_reg, port_mask, expected_mask,
1710 1000))
9b6de0a1
VS
1711 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1712 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
89b667f8
JB
1713}
1714
b8a4f404
PZ
1715static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1716 enum pipe pipe)
040484af 1717{
98187836
VS
1718 struct intel_crtc *intel_crtc = intel_get_crtc_for_pipe(dev_priv,
1719 pipe);
f0f59a00
VS
1720 i915_reg_t reg;
1721 uint32_t val, pipeconf_val;
040484af 1722
040484af 1723 /* Make sure PCH DPLL is enabled */
8106ddbd 1724 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
040484af
JB
1725
1726 /* FDI must be feeding us bits for PCH ports */
1727 assert_fdi_tx_enabled(dev_priv, pipe);
1728 assert_fdi_rx_enabled(dev_priv, pipe);
1729
6e266956 1730 if (HAS_PCH_CPT(dev_priv)) {
23670b32
DV
1731 /* Workaround: Set the timing override bit before enabling the
1732 * pch transcoder. */
1733 reg = TRANS_CHICKEN2(pipe);
1734 val = I915_READ(reg);
1735 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1736 I915_WRITE(reg, val);
59c859d6 1737 }
23670b32 1738
ab9412ba 1739 reg = PCH_TRANSCONF(pipe);
040484af 1740 val = I915_READ(reg);
5f7f726d 1741 pipeconf_val = I915_READ(PIPECONF(pipe));
e9bcff5c 1742
2d1fe073 1743 if (HAS_PCH_IBX(dev_priv)) {
e9bcff5c 1744 /*
c5de7c6f
VS
1745 * Make the BPC in transcoder be consistent with
1746 * that in pipeconf reg. For HDMI we must use 8bpc
1747 * here for both 8bpc and 12bpc.
e9bcff5c 1748 */
dfd07d72 1749 val &= ~PIPECONF_BPC_MASK;
2d84d2b3 1750 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
c5de7c6f
VS
1751 val |= PIPECONF_8BPC;
1752 else
1753 val |= pipeconf_val & PIPECONF_BPC_MASK;
e9bcff5c 1754 }
5f7f726d
PZ
1755
1756 val &= ~TRANS_INTERLACE_MASK;
1757 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
2d1fe073 1758 if (HAS_PCH_IBX(dev_priv) &&
2d84d2b3 1759 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
7c26e5c6
PZ
1760 val |= TRANS_LEGACY_INTERLACED_ILK;
1761 else
1762 val |= TRANS_INTERLACED;
5f7f726d
PZ
1763 else
1764 val |= TRANS_PROGRESSIVE;
1765
040484af 1766 I915_WRITE(reg, val | TRANS_ENABLE);
650fbd84
CW
1767 if (intel_wait_for_register(dev_priv,
1768 reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
1769 100))
4bb6f1f3 1770 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
040484af
JB
1771}
1772
8fb033d7 1773static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
937bb610 1774 enum transcoder cpu_transcoder)
040484af 1775{
8fb033d7 1776 u32 val, pipeconf_val;
8fb033d7 1777
8fb033d7 1778 /* FDI must be feeding us bits for PCH ports */
1a240d4d 1779 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
937bb610 1780 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
8fb033d7 1781
223a6fdf 1782 /* Workaround: set timing override bit. */
36c0d0cf 1783 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
23670b32 1784 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
36c0d0cf 1785 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
223a6fdf 1786
25f3ef11 1787 val = TRANS_ENABLE;
937bb610 1788 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
8fb033d7 1789
9a76b1c6
PZ
1790 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1791 PIPECONF_INTERLACED_ILK)
a35f2679 1792 val |= TRANS_INTERLACED;
8fb033d7
PZ
1793 else
1794 val |= TRANS_PROGRESSIVE;
1795
ab9412ba 1796 I915_WRITE(LPT_TRANSCONF, val);
d9f96244
CW
1797 if (intel_wait_for_register(dev_priv,
1798 LPT_TRANSCONF,
1799 TRANS_STATE_ENABLE,
1800 TRANS_STATE_ENABLE,
1801 100))
937bb610 1802 DRM_ERROR("Failed to enable PCH transcoder\n");
8fb033d7
PZ
1803}
1804
b8a4f404
PZ
1805static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1806 enum pipe pipe)
040484af 1807{
f0f59a00
VS
1808 i915_reg_t reg;
1809 uint32_t val;
040484af
JB
1810
1811 /* FDI relies on the transcoder */
1812 assert_fdi_tx_disabled(dev_priv, pipe);
1813 assert_fdi_rx_disabled(dev_priv, pipe);
1814
291906f1
JB
1815 /* Ports must be off as well */
1816 assert_pch_ports_disabled(dev_priv, pipe);
1817
ab9412ba 1818 reg = PCH_TRANSCONF(pipe);
040484af
JB
1819 val = I915_READ(reg);
1820 val &= ~TRANS_ENABLE;
1821 I915_WRITE(reg, val);
1822 /* wait for PCH transcoder off, transcoder state */
a7d04662
CW
1823 if (intel_wait_for_register(dev_priv,
1824 reg, TRANS_STATE_ENABLE, 0,
1825 50))
4bb6f1f3 1826 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
23670b32 1827
6e266956 1828 if (HAS_PCH_CPT(dev_priv)) {
23670b32
DV
1829 /* Workaround: Clear the timing override chicken bit again. */
1830 reg = TRANS_CHICKEN2(pipe);
1831 val = I915_READ(reg);
1832 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1833 I915_WRITE(reg, val);
1834 }
040484af
JB
1835}
1836
b7076546 1837void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
8fb033d7 1838{
8fb033d7
PZ
1839 u32 val;
1840
ab9412ba 1841 val = I915_READ(LPT_TRANSCONF);
8fb033d7 1842 val &= ~TRANS_ENABLE;
ab9412ba 1843 I915_WRITE(LPT_TRANSCONF, val);
8fb033d7 1844 /* wait for PCH transcoder off, transcoder state */
dfdb4749
CW
1845 if (intel_wait_for_register(dev_priv,
1846 LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
1847 50))
8a52fd9f 1848 DRM_ERROR("Failed to disable PCH transcoder\n");
223a6fdf
PZ
1849
1850 /* Workaround: clear timing override bit. */
36c0d0cf 1851 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
23670b32 1852 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
36c0d0cf 1853 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
040484af
JB
1854}
1855
65f2130c
VS
1856enum transcoder intel_crtc_pch_transcoder(struct intel_crtc *crtc)
1857{
1858 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1859
1860 WARN_ON(!crtc->config->has_pch_encoder);
1861
1862 if (HAS_PCH_LPT(dev_priv))
1863 return TRANSCODER_A;
1864 else
1865 return (enum transcoder) crtc->pipe;
1866}
1867
b24e7179 1868/**
309cfea8 1869 * intel_enable_pipe - enable a pipe, asserting requirements
0372264a 1870 * @crtc: crtc responsible for the pipe
b24e7179 1871 *
0372264a 1872 * Enable @crtc's pipe, making sure that various hardware specific requirements
b24e7179 1873 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
b24e7179 1874 */
e1fdc473 1875static void intel_enable_pipe(struct intel_crtc *crtc)
b24e7179 1876{
0372264a 1877 struct drm_device *dev = crtc->base.dev;
fac5e23e 1878 struct drm_i915_private *dev_priv = to_i915(dev);
0372264a 1879 enum pipe pipe = crtc->pipe;
1a70a728 1880 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
f0f59a00 1881 i915_reg_t reg;
b24e7179
JB
1882 u32 val;
1883
9e2ee2dd
VS
1884 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1885
58c6eaa2 1886 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 1887 assert_cursor_disabled(dev_priv, pipe);
58c6eaa2
DV
1888 assert_sprites_disabled(dev_priv, pipe);
1889
b24e7179
JB
1890 /*
1891 * A pipe without a PLL won't actually be able to drive bits from
1892 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1893 * need the check.
1894 */
09fa8bb9 1895 if (HAS_GMCH_DISPLAY(dev_priv)) {
d7edc4e5 1896 if (intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DSI))
23538ef1
JN
1897 assert_dsi_pll_enabled(dev_priv);
1898 else
1899 assert_pll_enabled(dev_priv, pipe);
09fa8bb9 1900 } else {
6e3c9717 1901 if (crtc->config->has_pch_encoder) {
040484af 1902 /* if driving the PCH, we need FDI enabled */
65f2130c
VS
1903 assert_fdi_rx_pll_enabled(dev_priv,
1904 (enum pipe) intel_crtc_pch_transcoder(crtc));
1a240d4d
DV
1905 assert_fdi_tx_pll_enabled(dev_priv,
1906 (enum pipe) cpu_transcoder);
040484af
JB
1907 }
1908 /* FIXME: assert CPU port conditions for SNB+ */
1909 }
b24e7179 1910
702e7a56 1911 reg = PIPECONF(cpu_transcoder);
b24e7179 1912 val = I915_READ(reg);
7ad25d48 1913 if (val & PIPECONF_ENABLE) {
e56134bc
VS
1914 /* we keep both pipes enabled on 830 */
1915 WARN_ON(!IS_I830(dev_priv));
00d70b15 1916 return;
7ad25d48 1917 }
00d70b15
CW
1918
1919 I915_WRITE(reg, val | PIPECONF_ENABLE);
851855d8 1920 POSTING_READ(reg);
b7792d8b
VS
1921
1922 /*
1923 * Until the pipe starts DSL will read as 0, which would cause
1924 * an apparent vblank timestamp jump, which messes up also the
1925 * frame count when it's derived from the timestamps. So let's
1926 * wait for the pipe to start properly before we call
1927 * drm_crtc_vblank_on()
1928 */
1929 if (dev->max_vblank_count == 0 &&
1930 wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
1931 DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
b24e7179
JB
1932}
1933
1934/**
309cfea8 1935 * intel_disable_pipe - disable a pipe, asserting requirements
575f7ab7 1936 * @crtc: crtc whose pipes is to be disabled
b24e7179 1937 *
575f7ab7
VS
1938 * Disable the pipe of @crtc, making sure that various hardware
1939 * specific requirements are met, if applicable, e.g. plane
1940 * disabled, panel fitter off, etc.
b24e7179
JB
1941 *
1942 * Will wait until the pipe has shut down before returning.
1943 */
575f7ab7 1944static void intel_disable_pipe(struct intel_crtc *crtc)
b24e7179 1945{
fac5e23e 1946 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6e3c9717 1947 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 1948 enum pipe pipe = crtc->pipe;
f0f59a00 1949 i915_reg_t reg;
b24e7179
JB
1950 u32 val;
1951
9e2ee2dd
VS
1952 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
1953
b24e7179
JB
1954 /*
1955 * Make sure planes won't keep trying to pump pixels to us,
1956 * or we might hang the display.
1957 */
1958 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 1959 assert_cursor_disabled(dev_priv, pipe);
19332d7a 1960 assert_sprites_disabled(dev_priv, pipe);
b24e7179 1961
702e7a56 1962 reg = PIPECONF(cpu_transcoder);
b24e7179 1963 val = I915_READ(reg);
00d70b15
CW
1964 if ((val & PIPECONF_ENABLE) == 0)
1965 return;
1966
67adc644
VS
1967 /*
1968 * Double wide has implications for planes
1969 * so best keep it disabled when not needed.
1970 */
6e3c9717 1971 if (crtc->config->double_wide)
67adc644
VS
1972 val &= ~PIPECONF_DOUBLE_WIDE;
1973
1974 /* Don't disable pipe or pipe PLLs if needed */
e56134bc 1975 if (!IS_I830(dev_priv))
67adc644
VS
1976 val &= ~PIPECONF_ENABLE;
1977
1978 I915_WRITE(reg, val);
1979 if ((val & PIPECONF_ENABLE) == 0)
1980 intel_wait_for_pipe_off(crtc);
b24e7179
JB
1981}
1982
832be82f
VS
1983static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
1984{
1985 return IS_GEN2(dev_priv) ? 2048 : 4096;
1986}
1987
d88c4afd
VS
1988static unsigned int
1989intel_tile_width_bytes(const struct drm_framebuffer *fb, int plane)
7b49f948 1990{
d88c4afd
VS
1991 struct drm_i915_private *dev_priv = to_i915(fb->dev);
1992 unsigned int cpp = fb->format->cpp[plane];
1993
1994 switch (fb->modifier) {
2f075565 1995 case DRM_FORMAT_MOD_LINEAR:
7b49f948
VS
1996 return cpp;
1997 case I915_FORMAT_MOD_X_TILED:
1998 if (IS_GEN2(dev_priv))
1999 return 128;
2000 else
2001 return 512;
2002 case I915_FORMAT_MOD_Y_TILED:
2003 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2004 return 128;
2005 else
2006 return 512;
2007 case I915_FORMAT_MOD_Yf_TILED:
2008 switch (cpp) {
2009 case 1:
2010 return 64;
2011 case 2:
2012 case 4:
2013 return 128;
2014 case 8:
2015 case 16:
2016 return 256;
2017 default:
2018 MISSING_CASE(cpp);
2019 return cpp;
2020 }
2021 break;
2022 default:
d88c4afd 2023 MISSING_CASE(fb->modifier);
7b49f948
VS
2024 return cpp;
2025 }
2026}
2027
d88c4afd
VS
2028static unsigned int
2029intel_tile_height(const struct drm_framebuffer *fb, int plane)
a57ce0b2 2030{
2f075565 2031 if (fb->modifier == DRM_FORMAT_MOD_LINEAR)
832be82f
VS
2032 return 1;
2033 else
d88c4afd
VS
2034 return intel_tile_size(to_i915(fb->dev)) /
2035 intel_tile_width_bytes(fb, plane);
6761dd31
TU
2036}
2037
8d0deca8 2038/* Return the tile dimensions in pixel units */
d88c4afd 2039static void intel_tile_dims(const struct drm_framebuffer *fb, int plane,
8d0deca8 2040 unsigned int *tile_width,
d88c4afd 2041 unsigned int *tile_height)
8d0deca8 2042{
d88c4afd
VS
2043 unsigned int tile_width_bytes = intel_tile_width_bytes(fb, plane);
2044 unsigned int cpp = fb->format->cpp[plane];
8d0deca8
VS
2045
2046 *tile_width = tile_width_bytes / cpp;
d88c4afd 2047 *tile_height = intel_tile_size(to_i915(fb->dev)) / tile_width_bytes;
8d0deca8
VS
2048}
2049
6761dd31 2050unsigned int
d88c4afd
VS
2051intel_fb_align_height(const struct drm_framebuffer *fb,
2052 int plane, unsigned int height)
6761dd31 2053{
d88c4afd 2054 unsigned int tile_height = intel_tile_height(fb, plane);
832be82f
VS
2055
2056 return ALIGN(height, tile_height);
a57ce0b2
JB
2057}
2058
1663b9d6
VS
2059unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2060{
2061 unsigned int size = 0;
2062 int i;
2063
2064 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2065 size += rot_info->plane[i].width * rot_info->plane[i].height;
2066
2067 return size;
2068}
2069
75c82a53 2070static void
3465c580
VS
2071intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2072 const struct drm_framebuffer *fb,
2073 unsigned int rotation)
f64b98cd 2074{
7b92c047 2075 view->type = I915_GGTT_VIEW_NORMAL;
bd2ef25d 2076 if (drm_rotation_90_or_270(rotation)) {
7b92c047 2077 view->type = I915_GGTT_VIEW_ROTATED;
8bab1193 2078 view->rotated = to_intel_framebuffer(fb)->rot_info;
2d7a215f
VS
2079 }
2080}
50470bb0 2081
fabac484
VS
2082static unsigned int intel_cursor_alignment(const struct drm_i915_private *dev_priv)
2083{
2084 if (IS_I830(dev_priv))
2085 return 16 * 1024;
2086 else if (IS_I85X(dev_priv))
2087 return 256;
d9e1551e
VS
2088 else if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
2089 return 32;
fabac484
VS
2090 else
2091 return 4 * 1024;
2092}
2093
603525d7 2094static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
4e9a86b6
VS
2095{
2096 if (INTEL_INFO(dev_priv)->gen >= 9)
2097 return 256 * 1024;
c0f86832 2098 else if (IS_I965G(dev_priv) || IS_I965GM(dev_priv) ||
666a4537 2099 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
4e9a86b6
VS
2100 return 128 * 1024;
2101 else if (INTEL_INFO(dev_priv)->gen >= 4)
2102 return 4 * 1024;
2103 else
44c5905e 2104 return 0;
4e9a86b6
VS
2105}
2106
d88c4afd
VS
2107static unsigned int intel_surf_alignment(const struct drm_framebuffer *fb,
2108 int plane)
603525d7 2109{
d88c4afd
VS
2110 struct drm_i915_private *dev_priv = to_i915(fb->dev);
2111
b90c1ee1
VS
2112 /* AUX_DIST needs only 4K alignment */
2113 if (fb->format->format == DRM_FORMAT_NV12 && plane == 1)
2114 return 4096;
2115
d88c4afd 2116 switch (fb->modifier) {
2f075565 2117 case DRM_FORMAT_MOD_LINEAR:
603525d7
VS
2118 return intel_linear_alignment(dev_priv);
2119 case I915_FORMAT_MOD_X_TILED:
d88c4afd 2120 if (INTEL_GEN(dev_priv) >= 9)
603525d7
VS
2121 return 256 * 1024;
2122 return 0;
2123 case I915_FORMAT_MOD_Y_TILED:
2124 case I915_FORMAT_MOD_Yf_TILED:
2125 return 1 * 1024 * 1024;
2126 default:
d88c4afd 2127 MISSING_CASE(fb->modifier);
603525d7
VS
2128 return 0;
2129 }
2130}
2131
058d88c4
CW
2132struct i915_vma *
2133intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
6b95a207 2134{
850c4cdc 2135 struct drm_device *dev = fb->dev;
fac5e23e 2136 struct drm_i915_private *dev_priv = to_i915(dev);
850c4cdc 2137 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2138 struct i915_ggtt_view view;
058d88c4 2139 struct i915_vma *vma;
6b95a207 2140 u32 alignment;
6b95a207 2141
ebcdd39e
MR
2142 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2143
d88c4afd 2144 alignment = intel_surf_alignment(fb, 0);
6b95a207 2145
3465c580 2146 intel_fill_fb_ggtt_view(&view, fb, rotation);
f64b98cd 2147
693db184
CW
2148 /* Note that the w/a also requires 64 PTE of padding following the
2149 * bo. We currently fill all unused PTE with the shadow page and so
2150 * we should always have valid PTE following the scanout preventing
2151 * the VT-d warning.
2152 */
48f112fe 2153 if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
693db184
CW
2154 alignment = 256 * 1024;
2155
d6dd6843
PZ
2156 /*
2157 * Global gtt pte registers are special registers which actually forward
2158 * writes to a chunk of system memory. Which means that there is no risk
2159 * that the register values disappear as soon as we call
2160 * intel_runtime_pm_put(), so it is correct to wrap only the
2161 * pin/unpin/fence and not more.
2162 */
2163 intel_runtime_pm_get(dev_priv);
2164
058d88c4 2165 vma = i915_gem_object_pin_to_display_plane(obj, alignment, &view);
49ef5294
CW
2166 if (IS_ERR(vma))
2167 goto err;
6b95a207 2168
05a20d09 2169 if (i915_vma_is_map_and_fenceable(vma)) {
49ef5294
CW
2170 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2171 * fence, whereas 965+ only requires a fence if using
2172 * framebuffer compression. For simplicity, we always, when
2173 * possible, install a fence as the cost is not that onerous.
2174 *
2175 * If we fail to fence the tiled scanout, then either the
2176 * modeset will reject the change (which is highly unlikely as
2177 * the affected systems, all but one, do not have unmappable
2178 * space) or we will not be able to enable full powersaving
2179 * techniques (also likely not to apply due to various limits
2180 * FBC and the like impose on the size of the buffer, which
2181 * presumably we violated anyway with this unmappable buffer).
2182 * Anyway, it is presumably better to stumble onwards with
2183 * something and try to run the system in a "less than optimal"
2184 * mode that matches the user configuration.
2185 */
2186 if (i915_vma_get_fence(vma) == 0)
2187 i915_vma_pin_fence(vma);
9807216f 2188 }
6b95a207 2189
be1e3415 2190 i915_vma_get(vma);
49ef5294 2191err:
d6dd6843 2192 intel_runtime_pm_put(dev_priv);
058d88c4 2193 return vma;
6b95a207
KH
2194}
2195
be1e3415 2196void intel_unpin_fb_vma(struct i915_vma *vma)
1690e1eb 2197{
be1e3415 2198 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
f64b98cd 2199
49ef5294 2200 i915_vma_unpin_fence(vma);
058d88c4 2201 i915_gem_object_unpin_from_display_plane(vma);
be1e3415 2202 i915_vma_put(vma);
1690e1eb
CW
2203}
2204
ef78ec94
VS
2205static int intel_fb_pitch(const struct drm_framebuffer *fb, int plane,
2206 unsigned int rotation)
2207{
bd2ef25d 2208 if (drm_rotation_90_or_270(rotation))
ef78ec94
VS
2209 return to_intel_framebuffer(fb)->rotated[plane].pitch;
2210 else
2211 return fb->pitches[plane];
2212}
2213
6687c906
VS
2214/*
2215 * Convert the x/y offsets into a linear offset.
2216 * Only valid with 0/180 degree rotation, which is fine since linear
2217 * offset is only used with linear buffers on pre-hsw and tiled buffers
2218 * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
2219 */
2220u32 intel_fb_xy_to_linear(int x, int y,
2949056c
VS
2221 const struct intel_plane_state *state,
2222 int plane)
6687c906 2223{
2949056c 2224 const struct drm_framebuffer *fb = state->base.fb;
353c8598 2225 unsigned int cpp = fb->format->cpp[plane];
6687c906
VS
2226 unsigned int pitch = fb->pitches[plane];
2227
2228 return y * pitch + x * cpp;
2229}
2230
2231/*
2232 * Add the x/y offsets derived from fb->offsets[] to the user
2233 * specified plane src x/y offsets. The resulting x/y offsets
2234 * specify the start of scanout from the beginning of the gtt mapping.
2235 */
2236void intel_add_fb_offsets(int *x, int *y,
2949056c
VS
2237 const struct intel_plane_state *state,
2238 int plane)
6687c906
VS
2239
2240{
2949056c
VS
2241 const struct intel_framebuffer *intel_fb = to_intel_framebuffer(state->base.fb);
2242 unsigned int rotation = state->base.rotation;
6687c906 2243
bd2ef25d 2244 if (drm_rotation_90_or_270(rotation)) {
6687c906
VS
2245 *x += intel_fb->rotated[plane].x;
2246 *y += intel_fb->rotated[plane].y;
2247 } else {
2248 *x += intel_fb->normal[plane].x;
2249 *y += intel_fb->normal[plane].y;
2250 }
2251}
2252
29cf9491 2253/*
29cf9491
VS
2254 * Input tile dimensions and pitch must already be
2255 * rotated to match x and y, and in pixel units.
2256 */
66a2d927
VS
2257static u32 _intel_adjust_tile_offset(int *x, int *y,
2258 unsigned int tile_width,
2259 unsigned int tile_height,
2260 unsigned int tile_size,
2261 unsigned int pitch_tiles,
2262 u32 old_offset,
2263 u32 new_offset)
29cf9491 2264{
b9b24038 2265 unsigned int pitch_pixels = pitch_tiles * tile_width;
29cf9491
VS
2266 unsigned int tiles;
2267
2268 WARN_ON(old_offset & (tile_size - 1));
2269 WARN_ON(new_offset & (tile_size - 1));
2270 WARN_ON(new_offset > old_offset);
2271
2272 tiles = (old_offset - new_offset) / tile_size;
2273
2274 *y += tiles / pitch_tiles * tile_height;
2275 *x += tiles % pitch_tiles * tile_width;
2276
b9b24038
VS
2277 /* minimize x in case it got needlessly big */
2278 *y += *x / pitch_pixels * tile_height;
2279 *x %= pitch_pixels;
2280
29cf9491
VS
2281 return new_offset;
2282}
2283
66a2d927
VS
2284/*
2285 * Adjust the tile offset by moving the difference into
2286 * the x/y offsets.
2287 */
2288static u32 intel_adjust_tile_offset(int *x, int *y,
2289 const struct intel_plane_state *state, int plane,
2290 u32 old_offset, u32 new_offset)
2291{
2292 const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
2293 const struct drm_framebuffer *fb = state->base.fb;
353c8598 2294 unsigned int cpp = fb->format->cpp[plane];
66a2d927
VS
2295 unsigned int rotation = state->base.rotation;
2296 unsigned int pitch = intel_fb_pitch(fb, plane, rotation);
2297
2298 WARN_ON(new_offset > old_offset);
2299
2f075565 2300 if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
66a2d927
VS
2301 unsigned int tile_size, tile_width, tile_height;
2302 unsigned int pitch_tiles;
2303
2304 tile_size = intel_tile_size(dev_priv);
d88c4afd 2305 intel_tile_dims(fb, plane, &tile_width, &tile_height);
66a2d927 2306
bd2ef25d 2307 if (drm_rotation_90_or_270(rotation)) {
66a2d927
VS
2308 pitch_tiles = pitch / tile_height;
2309 swap(tile_width, tile_height);
2310 } else {
2311 pitch_tiles = pitch / (tile_width * cpp);
2312 }
2313
2314 _intel_adjust_tile_offset(x, y, tile_width, tile_height,
2315 tile_size, pitch_tiles,
2316 old_offset, new_offset);
2317 } else {
2318 old_offset += *y * pitch + *x * cpp;
2319
2320 *y = (old_offset - new_offset) / pitch;
2321 *x = ((old_offset - new_offset) - *y * pitch) / cpp;
2322 }
2323
2324 return new_offset;
2325}
2326
8d0deca8
VS
2327/*
2328 * Computes the linear offset to the base tile and adjusts
2329 * x, y. bytes per pixel is assumed to be a power-of-two.
2330 *
2331 * In the 90/270 rotated case, x and y are assumed
2332 * to be already rotated to match the rotated GTT view, and
2333 * pitch is the tile_height aligned framebuffer height.
6687c906
VS
2334 *
2335 * This function is used when computing the derived information
2336 * under intel_framebuffer, so using any of that information
2337 * here is not allowed. Anything under drm_framebuffer can be
2338 * used. This is why the user has to pass in the pitch since it
2339 * is specified in the rotated orientation.
8d0deca8 2340 */
6687c906
VS
2341static u32 _intel_compute_tile_offset(const struct drm_i915_private *dev_priv,
2342 int *x, int *y,
2343 const struct drm_framebuffer *fb, int plane,
2344 unsigned int pitch,
2345 unsigned int rotation,
2346 u32 alignment)
c2c75131 2347{
bae781b2 2348 uint64_t fb_modifier = fb->modifier;
353c8598 2349 unsigned int cpp = fb->format->cpp[plane];
6687c906 2350 u32 offset, offset_aligned;
29cf9491 2351
29cf9491
VS
2352 if (alignment)
2353 alignment--;
2354
2f075565 2355 if (fb_modifier != DRM_FORMAT_MOD_LINEAR) {
8d0deca8
VS
2356 unsigned int tile_size, tile_width, tile_height;
2357 unsigned int tile_rows, tiles, pitch_tiles;
c2c75131 2358
d843310d 2359 tile_size = intel_tile_size(dev_priv);
d88c4afd 2360 intel_tile_dims(fb, plane, &tile_width, &tile_height);
8d0deca8 2361
bd2ef25d 2362 if (drm_rotation_90_or_270(rotation)) {
8d0deca8
VS
2363 pitch_tiles = pitch / tile_height;
2364 swap(tile_width, tile_height);
2365 } else {
2366 pitch_tiles = pitch / (tile_width * cpp);
2367 }
d843310d
VS
2368
2369 tile_rows = *y / tile_height;
2370 *y %= tile_height;
c2c75131 2371
8d0deca8
VS
2372 tiles = *x / tile_width;
2373 *x %= tile_width;
bc752862 2374
29cf9491
VS
2375 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2376 offset_aligned = offset & ~alignment;
bc752862 2377
66a2d927
VS
2378 _intel_adjust_tile_offset(x, y, tile_width, tile_height,
2379 tile_size, pitch_tiles,
2380 offset, offset_aligned);
29cf9491 2381 } else {
bc752862 2382 offset = *y * pitch + *x * cpp;
29cf9491
VS
2383 offset_aligned = offset & ~alignment;
2384
4e9a86b6
VS
2385 *y = (offset & alignment) / pitch;
2386 *x = ((offset & alignment) - *y * pitch) / cpp;
bc752862 2387 }
29cf9491
VS
2388
2389 return offset_aligned;
c2c75131
DV
2390}
2391
6687c906 2392u32 intel_compute_tile_offset(int *x, int *y,
2949056c
VS
2393 const struct intel_plane_state *state,
2394 int plane)
6687c906 2395{
1e7b4fd8
VS
2396 struct intel_plane *intel_plane = to_intel_plane(state->base.plane);
2397 struct drm_i915_private *dev_priv = to_i915(intel_plane->base.dev);
2949056c
VS
2398 const struct drm_framebuffer *fb = state->base.fb;
2399 unsigned int rotation = state->base.rotation;
ef78ec94 2400 int pitch = intel_fb_pitch(fb, plane, rotation);
1e7b4fd8
VS
2401 u32 alignment;
2402
2403 if (intel_plane->id == PLANE_CURSOR)
2404 alignment = intel_cursor_alignment(dev_priv);
2405 else
2406 alignment = intel_surf_alignment(fb, plane);
6687c906
VS
2407
2408 return _intel_compute_tile_offset(dev_priv, x, y, fb, plane, pitch,
2409 rotation, alignment);
2410}
2411
2412/* Convert the fb->offset[] linear offset into x/y offsets */
2413static void intel_fb_offset_to_xy(int *x, int *y,
2414 const struct drm_framebuffer *fb, int plane)
2415{
353c8598 2416 unsigned int cpp = fb->format->cpp[plane];
6687c906
VS
2417 unsigned int pitch = fb->pitches[plane];
2418 u32 linear_offset = fb->offsets[plane];
2419
2420 *y = linear_offset / pitch;
2421 *x = linear_offset % pitch / cpp;
2422}
2423
72618ebf
VS
2424static unsigned int intel_fb_modifier_to_tiling(uint64_t fb_modifier)
2425{
2426 switch (fb_modifier) {
2427 case I915_FORMAT_MOD_X_TILED:
2428 return I915_TILING_X;
2429 case I915_FORMAT_MOD_Y_TILED:
2430 return I915_TILING_Y;
2431 default:
2432 return I915_TILING_NONE;
2433 }
2434}
2435
6687c906
VS
2436static int
2437intel_fill_fb_info(struct drm_i915_private *dev_priv,
2438 struct drm_framebuffer *fb)
2439{
2440 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
2441 struct intel_rotation_info *rot_info = &intel_fb->rot_info;
2442 u32 gtt_offset_rotated = 0;
2443 unsigned int max_size = 0;
bcb0b461 2444 int i, num_planes = fb->format->num_planes;
6687c906
VS
2445 unsigned int tile_size = intel_tile_size(dev_priv);
2446
2447 for (i = 0; i < num_planes; i++) {
2448 unsigned int width, height;
2449 unsigned int cpp, size;
2450 u32 offset;
2451 int x, y;
2452
353c8598 2453 cpp = fb->format->cpp[i];
145fcb11
VS
2454 width = drm_framebuffer_plane_width(fb->width, fb, i);
2455 height = drm_framebuffer_plane_height(fb->height, fb, i);
6687c906
VS
2456
2457 intel_fb_offset_to_xy(&x, &y, fb, i);
2458
60d5f2a4
VS
2459 /*
2460 * The fence (if used) is aligned to the start of the object
2461 * so having the framebuffer wrap around across the edge of the
2462 * fenced region doesn't really work. We have no API to configure
2463 * the fence start offset within the object (nor could we probably
2464 * on gen2/3). So it's just easier if we just require that the
2465 * fb layout agrees with the fence layout. We already check that the
2466 * fb stride matches the fence stride elsewhere.
2467 */
2468 if (i915_gem_object_is_tiled(intel_fb->obj) &&
2469 (x + width) * cpp > fb->pitches[i]) {
144cc143
VS
2470 DRM_DEBUG_KMS("bad fb plane %d offset: 0x%x\n",
2471 i, fb->offsets[i]);
60d5f2a4
VS
2472 return -EINVAL;
2473 }
2474
6687c906
VS
2475 /*
2476 * First pixel of the framebuffer from
2477 * the start of the normal gtt mapping.
2478 */
2479 intel_fb->normal[i].x = x;
2480 intel_fb->normal[i].y = y;
2481
2482 offset = _intel_compute_tile_offset(dev_priv, &x, &y,
3ca46c0a 2483 fb, i, fb->pitches[i],
c2c446ad 2484 DRM_MODE_ROTATE_0, tile_size);
6687c906
VS
2485 offset /= tile_size;
2486
2f075565 2487 if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
6687c906
VS
2488 unsigned int tile_width, tile_height;
2489 unsigned int pitch_tiles;
2490 struct drm_rect r;
2491
d88c4afd 2492 intel_tile_dims(fb, i, &tile_width, &tile_height);
6687c906
VS
2493
2494 rot_info->plane[i].offset = offset;
2495 rot_info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i], tile_width * cpp);
2496 rot_info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
2497 rot_info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
2498
2499 intel_fb->rotated[i].pitch =
2500 rot_info->plane[i].height * tile_height;
2501
2502 /* how many tiles does this plane need */
2503 size = rot_info->plane[i].stride * rot_info->plane[i].height;
2504 /*
2505 * If the plane isn't horizontally tile aligned,
2506 * we need one more tile.
2507 */
2508 if (x != 0)
2509 size++;
2510
2511 /* rotate the x/y offsets to match the GTT view */
2512 r.x1 = x;
2513 r.y1 = y;
2514 r.x2 = x + width;
2515 r.y2 = y + height;
2516 drm_rect_rotate(&r,
2517 rot_info->plane[i].width * tile_width,
2518 rot_info->plane[i].height * tile_height,
c2c446ad 2519 DRM_MODE_ROTATE_270);
6687c906
VS
2520 x = r.x1;
2521 y = r.y1;
2522
2523 /* rotate the tile dimensions to match the GTT view */
2524 pitch_tiles = intel_fb->rotated[i].pitch / tile_height;
2525 swap(tile_width, tile_height);
2526
2527 /*
2528 * We only keep the x/y offsets, so push all of the
2529 * gtt offset into the x/y offsets.
2530 */
46a1bd28
ACO
2531 _intel_adjust_tile_offset(&x, &y,
2532 tile_width, tile_height,
2533 tile_size, pitch_tiles,
66a2d927 2534 gtt_offset_rotated * tile_size, 0);
6687c906
VS
2535
2536 gtt_offset_rotated += rot_info->plane[i].width * rot_info->plane[i].height;
2537
2538 /*
2539 * First pixel of the framebuffer from
2540 * the start of the rotated gtt mapping.
2541 */
2542 intel_fb->rotated[i].x = x;
2543 intel_fb->rotated[i].y = y;
2544 } else {
2545 size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
2546 x * cpp, tile_size);
2547 }
2548
2549 /* how many tiles in total needed in the bo */
2550 max_size = max(max_size, offset + size);
2551 }
2552
144cc143
VS
2553 if (max_size * tile_size > intel_fb->obj->base.size) {
2554 DRM_DEBUG_KMS("fb too big for bo (need %u bytes, have %zu bytes)\n",
2555 max_size * tile_size, intel_fb->obj->base.size);
6687c906
VS
2556 return -EINVAL;
2557 }
2558
2559 return 0;
2560}
2561
b35d63fa 2562static int i9xx_format_to_fourcc(int format)
46f297fb
JB
2563{
2564 switch (format) {
2565 case DISPPLANE_8BPP:
2566 return DRM_FORMAT_C8;
2567 case DISPPLANE_BGRX555:
2568 return DRM_FORMAT_XRGB1555;
2569 case DISPPLANE_BGRX565:
2570 return DRM_FORMAT_RGB565;
2571 default:
2572 case DISPPLANE_BGRX888:
2573 return DRM_FORMAT_XRGB8888;
2574 case DISPPLANE_RGBX888:
2575 return DRM_FORMAT_XBGR8888;
2576 case DISPPLANE_BGRX101010:
2577 return DRM_FORMAT_XRGB2101010;
2578 case DISPPLANE_RGBX101010:
2579 return DRM_FORMAT_XBGR2101010;
2580 }
2581}
2582
bc8d7dff
DL
2583static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2584{
2585 switch (format) {
2586 case PLANE_CTL_FORMAT_RGB_565:
2587 return DRM_FORMAT_RGB565;
2588 default:
2589 case PLANE_CTL_FORMAT_XRGB_8888:
2590 if (rgb_order) {
2591 if (alpha)
2592 return DRM_FORMAT_ABGR8888;
2593 else
2594 return DRM_FORMAT_XBGR8888;
2595 } else {
2596 if (alpha)
2597 return DRM_FORMAT_ARGB8888;
2598 else
2599 return DRM_FORMAT_XRGB8888;
2600 }
2601 case PLANE_CTL_FORMAT_XRGB_2101010:
2602 if (rgb_order)
2603 return DRM_FORMAT_XBGR2101010;
2604 else
2605 return DRM_FORMAT_XRGB2101010;
2606 }
2607}
2608
5724dbd1 2609static bool
f6936e29
DV
2610intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2611 struct intel_initial_plane_config *plane_config)
46f297fb
JB
2612{
2613 struct drm_device *dev = crtc->base.dev;
3badb49f 2614 struct drm_i915_private *dev_priv = to_i915(dev);
72e96d64 2615 struct i915_ggtt *ggtt = &dev_priv->ggtt;
46f297fb
JB
2616 struct drm_i915_gem_object *obj = NULL;
2617 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2d14030b 2618 struct drm_framebuffer *fb = &plane_config->fb->base;
f37b5c2b
DV
2619 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2620 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2621 PAGE_SIZE);
2622
2623 size_aligned -= base_aligned;
46f297fb 2624
ff2652ea
CW
2625 if (plane_config->size == 0)
2626 return false;
2627
3badb49f
PZ
2628 /* If the FB is too big, just don't use it since fbdev is not very
2629 * important and we should probably use that space with FBC or other
2630 * features. */
72e96d64 2631 if (size_aligned * 2 > ggtt->stolen_usable_size)
3badb49f
PZ
2632 return false;
2633
12c83d99 2634 mutex_lock(&dev->struct_mutex);
187685cb 2635 obj = i915_gem_object_create_stolen_for_preallocated(dev_priv,
f37b5c2b
DV
2636 base_aligned,
2637 base_aligned,
2638 size_aligned);
24dbf51a
CW
2639 mutex_unlock(&dev->struct_mutex);
2640 if (!obj)
484b41dd 2641 return false;
46f297fb 2642
3e510a8e
CW
2643 if (plane_config->tiling == I915_TILING_X)
2644 obj->tiling_and_stride = fb->pitches[0] | I915_TILING_X;
46f297fb 2645
438b74a5 2646 mode_cmd.pixel_format = fb->format->format;
6bf129df
DL
2647 mode_cmd.width = fb->width;
2648 mode_cmd.height = fb->height;
2649 mode_cmd.pitches[0] = fb->pitches[0];
bae781b2 2650 mode_cmd.modifier[0] = fb->modifier;
18c5247e 2651 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
46f297fb 2652
24dbf51a 2653 if (intel_framebuffer_init(to_intel_framebuffer(fb), obj, &mode_cmd)) {
46f297fb
JB
2654 DRM_DEBUG_KMS("intel fb init failed\n");
2655 goto out_unref_obj;
2656 }
12c83d99 2657
484b41dd 2658
f6936e29 2659 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
484b41dd 2660 return true;
46f297fb
JB
2661
2662out_unref_obj:
f8c417cd 2663 i915_gem_object_put(obj);
484b41dd
JB
2664 return false;
2665}
2666
5a21b665
DV
2667/* Update plane->state->fb to match plane->fb after driver-internal updates */
2668static void
2669update_state_fb(struct drm_plane *plane)
2670{
2671 if (plane->fb == plane->state->fb)
2672 return;
2673
2674 if (plane->state->fb)
2675 drm_framebuffer_unreference(plane->state->fb);
2676 plane->state->fb = plane->fb;
2677 if (plane->state->fb)
2678 drm_framebuffer_reference(plane->state->fb);
2679}
2680
e9728bd8
VS
2681static void
2682intel_set_plane_visible(struct intel_crtc_state *crtc_state,
2683 struct intel_plane_state *plane_state,
2684 bool visible)
2685{
2686 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
2687
2688 plane_state->base.visible = visible;
2689
2690 /* FIXME pre-g4x don't work like this */
2691 if (visible) {
2692 crtc_state->base.plane_mask |= BIT(drm_plane_index(&plane->base));
2693 crtc_state->active_planes |= BIT(plane->id);
2694 } else {
2695 crtc_state->base.plane_mask &= ~BIT(drm_plane_index(&plane->base));
2696 crtc_state->active_planes &= ~BIT(plane->id);
2697 }
2698
2699 DRM_DEBUG_KMS("%s active planes 0x%x\n",
2700 crtc_state->base.crtc->name,
2701 crtc_state->active_planes);
2702}
2703
5724dbd1 2704static void
f6936e29
DV
2705intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2706 struct intel_initial_plane_config *plane_config)
484b41dd
JB
2707{
2708 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 2709 struct drm_i915_private *dev_priv = to_i915(dev);
484b41dd 2710 struct drm_crtc *c;
2ff8fde1 2711 struct drm_i915_gem_object *obj;
88595ac9 2712 struct drm_plane *primary = intel_crtc->base.primary;
be5651f2 2713 struct drm_plane_state *plane_state = primary->state;
200757f5
MR
2714 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2715 struct intel_plane *intel_plane = to_intel_plane(primary);
0a8d8a86
MR
2716 struct intel_plane_state *intel_state =
2717 to_intel_plane_state(plane_state);
88595ac9 2718 struct drm_framebuffer *fb;
484b41dd 2719
2d14030b 2720 if (!plane_config->fb)
484b41dd
JB
2721 return;
2722
f6936e29 2723 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
88595ac9
DV
2724 fb = &plane_config->fb->base;
2725 goto valid_fb;
f55548b5 2726 }
484b41dd 2727
2d14030b 2728 kfree(plane_config->fb);
484b41dd
JB
2729
2730 /*
2731 * Failed to alloc the obj, check to see if we should share
2732 * an fb with another CRTC instead
2733 */
70e1e0ec 2734 for_each_crtc(dev, c) {
be1e3415 2735 struct intel_plane_state *state;
484b41dd
JB
2736
2737 if (c == &intel_crtc->base)
2738 continue;
2739
be1e3415 2740 if (!to_intel_crtc(c)->active)
2ff8fde1
MR
2741 continue;
2742
be1e3415
CW
2743 state = to_intel_plane_state(c->primary->state);
2744 if (!state->vma)
484b41dd
JB
2745 continue;
2746
be1e3415
CW
2747 if (intel_plane_ggtt_offset(state) == plane_config->base) {
2748 fb = c->primary->fb;
88595ac9
DV
2749 drm_framebuffer_reference(fb);
2750 goto valid_fb;
484b41dd
JB
2751 }
2752 }
88595ac9 2753
200757f5
MR
2754 /*
2755 * We've failed to reconstruct the BIOS FB. Current display state
2756 * indicates that the primary plane is visible, but has a NULL FB,
2757 * which will lead to problems later if we don't fix it up. The
2758 * simplest solution is to just disable the primary plane now and
2759 * pretend the BIOS never had it enabled.
2760 */
e9728bd8
VS
2761 intel_set_plane_visible(to_intel_crtc_state(crtc_state),
2762 to_intel_plane_state(plane_state),
2763 false);
2622a081 2764 intel_pre_disable_primary_noatomic(&intel_crtc->base);
72259536 2765 trace_intel_disable_plane(primary, intel_crtc);
282dbf9b 2766 intel_plane->disable_plane(intel_plane, intel_crtc);
200757f5 2767
88595ac9
DV
2768 return;
2769
2770valid_fb:
be1e3415
CW
2771 mutex_lock(&dev->struct_mutex);
2772 intel_state->vma =
2773 intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
2774 mutex_unlock(&dev->struct_mutex);
2775 if (IS_ERR(intel_state->vma)) {
2776 DRM_ERROR("failed to pin boot fb on pipe %d: %li\n",
2777 intel_crtc->pipe, PTR_ERR(intel_state->vma));
2778
2779 intel_state->vma = NULL;
2780 drm_framebuffer_unreference(fb);
2781 return;
2782 }
2783
f44e2659
VS
2784 plane_state->src_x = 0;
2785 plane_state->src_y = 0;
be5651f2
ML
2786 plane_state->src_w = fb->width << 16;
2787 plane_state->src_h = fb->height << 16;
2788
f44e2659
VS
2789 plane_state->crtc_x = 0;
2790 plane_state->crtc_y = 0;
be5651f2
ML
2791 plane_state->crtc_w = fb->width;
2792 plane_state->crtc_h = fb->height;
2793
1638d30c
RC
2794 intel_state->base.src = drm_plane_state_src(plane_state);
2795 intel_state->base.dst = drm_plane_state_dest(plane_state);
0a8d8a86 2796
88595ac9 2797 obj = intel_fb_obj(fb);
3e510a8e 2798 if (i915_gem_object_is_tiled(obj))
88595ac9
DV
2799 dev_priv->preserve_bios_swizzle = true;
2800
be5651f2
ML
2801 drm_framebuffer_reference(fb);
2802 primary->fb = primary->state->fb = fb;
36750f28 2803 primary->crtc = primary->state->crtc = &intel_crtc->base;
e9728bd8
VS
2804
2805 intel_set_plane_visible(to_intel_crtc_state(crtc_state),
2806 to_intel_plane_state(plane_state),
2807 true);
2808
faf5bf0a
CW
2809 atomic_or(to_intel_plane(primary)->frontbuffer_bit,
2810 &obj->frontbuffer_bits);
46f297fb
JB
2811}
2812
b63a16f6
VS
2813static int skl_max_plane_width(const struct drm_framebuffer *fb, int plane,
2814 unsigned int rotation)
2815{
353c8598 2816 int cpp = fb->format->cpp[plane];
b63a16f6 2817
bae781b2 2818 switch (fb->modifier) {
2f075565 2819 case DRM_FORMAT_MOD_LINEAR:
b63a16f6
VS
2820 case I915_FORMAT_MOD_X_TILED:
2821 switch (cpp) {
2822 case 8:
2823 return 4096;
2824 case 4:
2825 case 2:
2826 case 1:
2827 return 8192;
2828 default:
2829 MISSING_CASE(cpp);
2830 break;
2831 }
2832 break;
2833 case I915_FORMAT_MOD_Y_TILED:
2834 case I915_FORMAT_MOD_Yf_TILED:
2835 switch (cpp) {
2836 case 8:
2837 return 2048;
2838 case 4:
2839 return 4096;
2840 case 2:
2841 case 1:
2842 return 8192;
2843 default:
2844 MISSING_CASE(cpp);
2845 break;
2846 }
2847 break;
2848 default:
bae781b2 2849 MISSING_CASE(fb->modifier);
b63a16f6
VS
2850 }
2851
2852 return 2048;
2853}
2854
2855static int skl_check_main_surface(struct intel_plane_state *plane_state)
2856{
b63a16f6
VS
2857 const struct drm_framebuffer *fb = plane_state->base.fb;
2858 unsigned int rotation = plane_state->base.rotation;
cc926387
DV
2859 int x = plane_state->base.src.x1 >> 16;
2860 int y = plane_state->base.src.y1 >> 16;
2861 int w = drm_rect_width(&plane_state->base.src) >> 16;
2862 int h = drm_rect_height(&plane_state->base.src) >> 16;
b63a16f6
VS
2863 int max_width = skl_max_plane_width(fb, 0, rotation);
2864 int max_height = 4096;
8d970654 2865 u32 alignment, offset, aux_offset = plane_state->aux.offset;
b63a16f6
VS
2866
2867 if (w > max_width || h > max_height) {
2868 DRM_DEBUG_KMS("requested Y/RGB source size %dx%d too big (limit %dx%d)\n",
2869 w, h, max_width, max_height);
2870 return -EINVAL;
2871 }
2872
2873 intel_add_fb_offsets(&x, &y, plane_state, 0);
2874 offset = intel_compute_tile_offset(&x, &y, plane_state, 0);
d88c4afd 2875 alignment = intel_surf_alignment(fb, 0);
b63a16f6 2876
8d970654
VS
2877 /*
2878 * AUX surface offset is specified as the distance from the
2879 * main surface offset, and it must be non-negative. Make
2880 * sure that is what we will get.
2881 */
2882 if (offset > aux_offset)
2883 offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
2884 offset, aux_offset & ~(alignment - 1));
2885
b63a16f6
VS
2886 /*
2887 * When using an X-tiled surface, the plane blows up
2888 * if the x offset + width exceed the stride.
2889 *
2890 * TODO: linear and Y-tiled seem fine, Yf untested,
2891 */
bae781b2 2892 if (fb->modifier == I915_FORMAT_MOD_X_TILED) {
353c8598 2893 int cpp = fb->format->cpp[0];
b63a16f6
VS
2894
2895 while ((x + w) * cpp > fb->pitches[0]) {
2896 if (offset == 0) {
2897 DRM_DEBUG_KMS("Unable to find suitable display surface offset\n");
2898 return -EINVAL;
2899 }
2900
2901 offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
2902 offset, offset - alignment);
2903 }
2904 }
2905
2906 plane_state->main.offset = offset;
2907 plane_state->main.x = x;
2908 plane_state->main.y = y;
2909
2910 return 0;
2911}
2912
8d970654
VS
2913static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
2914{
2915 const struct drm_framebuffer *fb = plane_state->base.fb;
2916 unsigned int rotation = plane_state->base.rotation;
2917 int max_width = skl_max_plane_width(fb, 1, rotation);
2918 int max_height = 4096;
cc926387
DV
2919 int x = plane_state->base.src.x1 >> 17;
2920 int y = plane_state->base.src.y1 >> 17;
2921 int w = drm_rect_width(&plane_state->base.src) >> 17;
2922 int h = drm_rect_height(&plane_state->base.src) >> 17;
8d970654
VS
2923 u32 offset;
2924
2925 intel_add_fb_offsets(&x, &y, plane_state, 1);
2926 offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
2927
2928 /* FIXME not quite sure how/if these apply to the chroma plane */
2929 if (w > max_width || h > max_height) {
2930 DRM_DEBUG_KMS("CbCr source size %dx%d too big (limit %dx%d)\n",
2931 w, h, max_width, max_height);
2932 return -EINVAL;
2933 }
2934
2935 plane_state->aux.offset = offset;
2936 plane_state->aux.x = x;
2937 plane_state->aux.y = y;
2938
2939 return 0;
2940}
2941
b63a16f6
VS
2942int skl_check_plane_surface(struct intel_plane_state *plane_state)
2943{
2944 const struct drm_framebuffer *fb = plane_state->base.fb;
2945 unsigned int rotation = plane_state->base.rotation;
2946 int ret;
2947
a5e4c7d0
VS
2948 if (!plane_state->base.visible)
2949 return 0;
2950
b63a16f6 2951 /* Rotate src coordinates to match rotated GTT view */
bd2ef25d 2952 if (drm_rotation_90_or_270(rotation))
cc926387 2953 drm_rect_rotate(&plane_state->base.src,
da064b47 2954 fb->width << 16, fb->height << 16,
c2c446ad 2955 DRM_MODE_ROTATE_270);
b63a16f6 2956
8d970654
VS
2957 /*
2958 * Handle the AUX surface first since
2959 * the main surface setup depends on it.
2960 */
438b74a5 2961 if (fb->format->format == DRM_FORMAT_NV12) {
8d970654
VS
2962 ret = skl_check_nv12_aux_surface(plane_state);
2963 if (ret)
2964 return ret;
2965 } else {
2966 plane_state->aux.offset = ~0xfff;
2967 plane_state->aux.x = 0;
2968 plane_state->aux.y = 0;
2969 }
2970
b63a16f6
VS
2971 ret = skl_check_main_surface(plane_state);
2972 if (ret)
2973 return ret;
2974
2975 return 0;
2976}
2977
7145f60a
VS
2978static u32 i9xx_plane_ctl(const struct intel_crtc_state *crtc_state,
2979 const struct intel_plane_state *plane_state)
81255565 2980{
7145f60a
VS
2981 struct drm_i915_private *dev_priv =
2982 to_i915(plane_state->base.plane->dev);
2983 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
2984 const struct drm_framebuffer *fb = plane_state->base.fb;
8d0deca8 2985 unsigned int rotation = plane_state->base.rotation;
7145f60a 2986 u32 dspcntr;
c9ba6fad 2987
7145f60a 2988 dspcntr = DISPLAY_PLANE_ENABLE | DISPPLANE_GAMMA_ENABLE;
f45651ba 2989
6a4407a6
VS
2990 if (IS_G4X(dev_priv) || IS_GEN5(dev_priv) ||
2991 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
7145f60a 2992 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
f45651ba 2993
6a4407a6
VS
2994 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
2995 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
f45651ba 2996
d509e28b
VS
2997 if (INTEL_GEN(dev_priv) < 4)
2998 dspcntr |= DISPPLANE_SEL_PIPE(crtc->pipe);
81255565 2999
438b74a5 3000 switch (fb->format->format) {
57779d06 3001 case DRM_FORMAT_C8:
81255565
JB
3002 dspcntr |= DISPPLANE_8BPP;
3003 break;
57779d06 3004 case DRM_FORMAT_XRGB1555:
57779d06 3005 dspcntr |= DISPPLANE_BGRX555;
81255565 3006 break;
57779d06
VS
3007 case DRM_FORMAT_RGB565:
3008 dspcntr |= DISPPLANE_BGRX565;
3009 break;
3010 case DRM_FORMAT_XRGB8888:
57779d06
VS
3011 dspcntr |= DISPPLANE_BGRX888;
3012 break;
3013 case DRM_FORMAT_XBGR8888:
57779d06
VS
3014 dspcntr |= DISPPLANE_RGBX888;
3015 break;
3016 case DRM_FORMAT_XRGB2101010:
57779d06
VS
3017 dspcntr |= DISPPLANE_BGRX101010;
3018 break;
3019 case DRM_FORMAT_XBGR2101010:
57779d06 3020 dspcntr |= DISPPLANE_RGBX101010;
81255565
JB
3021 break;
3022 default:
7145f60a
VS
3023 MISSING_CASE(fb->format->format);
3024 return 0;
81255565 3025 }
57779d06 3026
72618ebf 3027 if (INTEL_GEN(dev_priv) >= 4 &&
bae781b2 3028 fb->modifier == I915_FORMAT_MOD_X_TILED)
f45651ba 3029 dspcntr |= DISPPLANE_TILED;
81255565 3030
c2c446ad 3031 if (rotation & DRM_MODE_ROTATE_180)
df0cd455
VS
3032 dspcntr |= DISPPLANE_ROTATE_180;
3033
c2c446ad 3034 if (rotation & DRM_MODE_REFLECT_X)
4ea7be2b
VS
3035 dspcntr |= DISPPLANE_MIRROR;
3036
7145f60a
VS
3037 return dspcntr;
3038}
de1aa629 3039
f9407ae1 3040int i9xx_check_plane_surface(struct intel_plane_state *plane_state)
5b7fcc44
VS
3041{
3042 struct drm_i915_private *dev_priv =
3043 to_i915(plane_state->base.plane->dev);
3044 int src_x = plane_state->base.src.x1 >> 16;
3045 int src_y = plane_state->base.src.y1 >> 16;
3046 u32 offset;
81255565 3047
5b7fcc44 3048 intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
e506a0c6 3049
5b7fcc44
VS
3050 if (INTEL_GEN(dev_priv) >= 4)
3051 offset = intel_compute_tile_offset(&src_x, &src_y,
3052 plane_state, 0);
3053 else
3054 offset = 0;
3055
3056 /* HSW/BDW do this automagically in hardware */
3057 if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv)) {
3058 unsigned int rotation = plane_state->base.rotation;
3059 int src_w = drm_rect_width(&plane_state->base.src) >> 16;
3060 int src_h = drm_rect_height(&plane_state->base.src) >> 16;
3061
c2c446ad 3062 if (rotation & DRM_MODE_ROTATE_180) {
5b7fcc44
VS
3063 src_x += src_w - 1;
3064 src_y += src_h - 1;
c2c446ad 3065 } else if (rotation & DRM_MODE_REFLECT_X) {
5b7fcc44
VS
3066 src_x += src_w - 1;
3067 }
48404c1e
SJ
3068 }
3069
5b7fcc44
VS
3070 plane_state->main.offset = offset;
3071 plane_state->main.x = src_x;
3072 plane_state->main.y = src_y;
3073
3074 return 0;
3075}
3076
282dbf9b 3077static void i9xx_update_primary_plane(struct intel_plane *primary,
7145f60a
VS
3078 const struct intel_crtc_state *crtc_state,
3079 const struct intel_plane_state *plane_state)
3080{
282dbf9b
VS
3081 struct drm_i915_private *dev_priv = to_i915(primary->base.dev);
3082 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
3083 const struct drm_framebuffer *fb = plane_state->base.fb;
3084 enum plane plane = primary->plane;
7145f60a 3085 u32 linear_offset;
a0864d59 3086 u32 dspcntr = plane_state->ctl;
7145f60a 3087 i915_reg_t reg = DSPCNTR(plane);
5b7fcc44
VS
3088 int x = plane_state->main.x;
3089 int y = plane_state->main.y;
7145f60a
VS
3090 unsigned long irqflags;
3091
2949056c 3092 linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
6687c906 3093
5b7fcc44 3094 if (INTEL_GEN(dev_priv) >= 4)
282dbf9b 3095 crtc->dspaddr_offset = plane_state->main.offset;
5b7fcc44 3096 else
282dbf9b 3097 crtc->dspaddr_offset = linear_offset;
6687c906 3098
282dbf9b
VS
3099 crtc->adjusted_x = x;
3100 crtc->adjusted_y = y;
2db3366b 3101
dd584fc0
VS
3102 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
3103
78587de2
VS
3104 if (INTEL_GEN(dev_priv) < 4) {
3105 /* pipesrc and dspsize control the size that is scaled from,
3106 * which should always be the user's requested size.
3107 */
dd584fc0
VS
3108 I915_WRITE_FW(DSPSIZE(plane),
3109 ((crtc_state->pipe_src_h - 1) << 16) |
3110 (crtc_state->pipe_src_w - 1));
3111 I915_WRITE_FW(DSPPOS(plane), 0);
78587de2 3112 } else if (IS_CHERRYVIEW(dev_priv) && plane == PLANE_B) {
dd584fc0
VS
3113 I915_WRITE_FW(PRIMSIZE(plane),
3114 ((crtc_state->pipe_src_h - 1) << 16) |
3115 (crtc_state->pipe_src_w - 1));
3116 I915_WRITE_FW(PRIMPOS(plane), 0);
3117 I915_WRITE_FW(PRIMCNSTALPHA(plane), 0);
78587de2
VS
3118 }
3119
dd584fc0 3120 I915_WRITE_FW(reg, dspcntr);
48404c1e 3121
dd584fc0 3122 I915_WRITE_FW(DSPSTRIDE(plane), fb->pitches[0]);
3ba35e53
VS
3123 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
3124 I915_WRITE_FW(DSPSURF(plane),
3125 intel_plane_ggtt_offset(plane_state) +
282dbf9b 3126 crtc->dspaddr_offset);
3ba35e53
VS
3127 I915_WRITE_FW(DSPOFFSET(plane), (y << 16) | x);
3128 } else if (INTEL_GEN(dev_priv) >= 4) {
dd584fc0
VS
3129 I915_WRITE_FW(DSPSURF(plane),
3130 intel_plane_ggtt_offset(plane_state) +
282dbf9b 3131 crtc->dspaddr_offset);
dd584fc0
VS
3132 I915_WRITE_FW(DSPTILEOFF(plane), (y << 16) | x);
3133 I915_WRITE_FW(DSPLINOFF(plane), linear_offset);
bfb81049 3134 } else {
dd584fc0
VS
3135 I915_WRITE_FW(DSPADDR(plane),
3136 intel_plane_ggtt_offset(plane_state) +
282dbf9b 3137 crtc->dspaddr_offset);
bfb81049 3138 }
dd584fc0
VS
3139 POSTING_READ_FW(reg);
3140
3141 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
17638cd6
JB
3142}
3143
282dbf9b
VS
3144static void i9xx_disable_primary_plane(struct intel_plane *primary,
3145 struct intel_crtc *crtc)
17638cd6 3146{
282dbf9b
VS
3147 struct drm_i915_private *dev_priv = to_i915(primary->base.dev);
3148 enum plane plane = primary->plane;
dd584fc0
VS
3149 unsigned long irqflags;
3150
3151 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
f45651ba 3152
dd584fc0 3153 I915_WRITE_FW(DSPCNTR(plane), 0);
a8d201af 3154 if (INTEL_INFO(dev_priv)->gen >= 4)
dd584fc0 3155 I915_WRITE_FW(DSPSURF(plane), 0);
a8d201af 3156 else
dd584fc0
VS
3157 I915_WRITE_FW(DSPADDR(plane), 0);
3158 POSTING_READ_FW(DSPCNTR(plane));
3159
3160 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
a8d201af 3161}
c9ba6fad 3162
d88c4afd
VS
3163static u32
3164intel_fb_stride_alignment(const struct drm_framebuffer *fb, int plane)
b321803d 3165{
2f075565 3166 if (fb->modifier == DRM_FORMAT_MOD_LINEAR)
b321803d 3167 return 64;
d88c4afd
VS
3168 else
3169 return intel_tile_width_bytes(fb, plane);
b321803d
DL
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{
1b500535
VS
3202 u32 stride;
3203
3204 if (plane >= fb->format->num_planes)
3205 return 0;
3206
3207 stride = intel_fb_pitch(fb, plane, rotation);
d2196774
VS
3208
3209 /*
3210 * The stride is either expressed as a multiple of 64 bytes chunks for
3211 * linear buffers or in number of tiles for tiled buffers.
3212 */
d88c4afd
VS
3213 if (drm_rotation_90_or_270(rotation))
3214 stride /= intel_tile_height(fb, plane);
3215 else
3216 stride /= intel_fb_stride_alignment(fb, plane);
d2196774
VS
3217
3218 return stride;
3219}
3220
2e881264 3221static u32 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
2e881264 3262static u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
6156a456 3263{
6156a456 3264 switch (fb_modifier) {
2f075565 3265 case DRM_FORMAT_MOD_LINEAR:
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
2e881264 3280static u32 skl_plane_ctl_rotation(unsigned int rotation)
6156a456 3281{
3b7a5119 3282 switch (rotation) {
c2c446ad 3283 case DRM_MODE_ROTATE_0:
6156a456 3284 break;
1e8df167 3285 /*
c2c446ad 3286 * DRM_MODE_ROTATE_ is counter clockwise to stay compatible with Xrandr
1e8df167
SJ
3287 * while i915 HW rotation is clockwise, thats why this swapping.
3288 */
c2c446ad 3289 case DRM_MODE_ROTATE_90:
1e8df167 3290 return PLANE_CTL_ROTATE_270;
c2c446ad 3291 case DRM_MODE_ROTATE_180:
c34ce3d1 3292 return PLANE_CTL_ROTATE_180;
c2c446ad 3293 case DRM_MODE_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
2e881264
VS
3302u32 skl_plane_ctl(const struct intel_crtc_state *crtc_state,
3303 const struct intel_plane_state *plane_state)
46f788ba
VS
3304{
3305 struct drm_i915_private *dev_priv =
3306 to_i915(plane_state->base.plane->dev);
3307 const struct drm_framebuffer *fb = plane_state->base.fb;
3308 unsigned int rotation = plane_state->base.rotation;
2e881264 3309 const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
46f788ba
VS
3310 u32 plane_ctl;
3311
3312 plane_ctl = PLANE_CTL_ENABLE;
3313
6602be0e 3314 if (!IS_GEMINILAKE(dev_priv) && !IS_CANNONLAKE(dev_priv)) {
46f788ba
VS
3315 plane_ctl |=
3316 PLANE_CTL_PIPE_GAMMA_ENABLE |
3317 PLANE_CTL_PIPE_CSC_ENABLE |
3318 PLANE_CTL_PLANE_GAMMA_DISABLE;
3319 }
3320
3321 plane_ctl |= skl_plane_ctl_format(fb->format->format);
3322 plane_ctl |= skl_plane_ctl_tiling(fb->modifier);
3323 plane_ctl |= skl_plane_ctl_rotation(rotation);
3324
2e881264
VS
3325 if (key->flags & I915_SET_COLORKEY_DESTINATION)
3326 plane_ctl |= PLANE_CTL_KEY_ENABLE_DESTINATION;
3327 else if (key->flags & I915_SET_COLORKEY_SOURCE)
3328 plane_ctl |= PLANE_CTL_KEY_ENABLE_SOURCE;
3329
46f788ba
VS
3330 return plane_ctl;
3331}
3332
282dbf9b 3333static void skylake_update_primary_plane(struct intel_plane *plane,
a8d201af
ML
3334 const struct intel_crtc_state *crtc_state,
3335 const struct intel_plane_state *plane_state)
6156a456 3336{
282dbf9b
VS
3337 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
3338 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
3339 const struct drm_framebuffer *fb = plane_state->base.fb;
3340 enum plane_id plane_id = plane->id;
3341 enum pipe pipe = plane->pipe;
a0864d59 3342 u32 plane_ctl = plane_state->ctl;
a8d201af 3343 unsigned int rotation = plane_state->base.rotation;
d2196774 3344 u32 stride = skl_plane_stride(fb, 0, rotation);
b63a16f6 3345 u32 surf_addr = plane_state->main.offset;
a8d201af 3346 int scaler_id = plane_state->scaler_id;
b63a16f6
VS
3347 int src_x = plane_state->main.x;
3348 int src_y = plane_state->main.y;
936e71e3
VS
3349 int src_w = drm_rect_width(&plane_state->base.src) >> 16;
3350 int src_h = drm_rect_height(&plane_state->base.src) >> 16;
3351 int dst_x = plane_state->base.dst.x1;
3352 int dst_y = plane_state->base.dst.y1;
3353 int dst_w = drm_rect_width(&plane_state->base.dst);
3354 int dst_h = drm_rect_height(&plane_state->base.dst);
dd584fc0 3355 unsigned long irqflags;
70d21f0e 3356
6687c906
VS
3357 /* Sizes are 0 based */
3358 src_w--;
3359 src_h--;
3360 dst_w--;
3361 dst_h--;
3362
282dbf9b 3363 crtc->dspaddr_offset = surf_addr;
4c0b8a8b 3364
282dbf9b
VS
3365 crtc->adjusted_x = src_x;
3366 crtc->adjusted_y = src_y;
2db3366b 3367
dd584fc0
VS
3368 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
3369
6602be0e 3370 if (IS_GEMINILAKE(dev_priv) || IS_CANNONLAKE(dev_priv)) {
dd584fc0
VS
3371 I915_WRITE_FW(PLANE_COLOR_CTL(pipe, plane_id),
3372 PLANE_COLOR_PIPE_GAMMA_ENABLE |
3373 PLANE_COLOR_PIPE_CSC_ENABLE |
3374 PLANE_COLOR_PLANE_GAMMA_DISABLE);
78587de2
VS
3375 }
3376
dd584fc0
VS
3377 I915_WRITE_FW(PLANE_CTL(pipe, plane_id), plane_ctl);
3378 I915_WRITE_FW(PLANE_OFFSET(pipe, plane_id), (src_y << 16) | src_x);
3379 I915_WRITE_FW(PLANE_STRIDE(pipe, plane_id), stride);
3380 I915_WRITE_FW(PLANE_SIZE(pipe, plane_id), (src_h << 16) | src_w);
6156a456
CK
3381
3382 if (scaler_id >= 0) {
3383 uint32_t ps_ctrl = 0;
3384
3385 WARN_ON(!dst_w || !dst_h);
8e816bb4 3386 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(plane_id) |
6156a456 3387 crtc_state->scaler_state.scalers[scaler_id].mode;
dd584fc0
VS
3388 I915_WRITE_FW(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3389 I915_WRITE_FW(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3390 I915_WRITE_FW(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3391 I915_WRITE_FW(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3392 I915_WRITE_FW(PLANE_POS(pipe, plane_id), 0);
6156a456 3393 } else {
dd584fc0 3394 I915_WRITE_FW(PLANE_POS(pipe, plane_id), (dst_y << 16) | dst_x);
6156a456
CK
3395 }
3396
dd584fc0
VS
3397 I915_WRITE_FW(PLANE_SURF(pipe, plane_id),
3398 intel_plane_ggtt_offset(plane_state) + surf_addr);
70d21f0e 3399
dd584fc0
VS
3400 POSTING_READ_FW(PLANE_SURF(pipe, plane_id));
3401
3402 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
70d21f0e
DL
3403}
3404
282dbf9b
VS
3405static void skylake_disable_primary_plane(struct intel_plane *primary,
3406 struct intel_crtc *crtc)
17638cd6 3407{
282dbf9b
VS
3408 struct drm_i915_private *dev_priv = to_i915(primary->base.dev);
3409 enum plane_id plane_id = primary->id;
3410 enum pipe pipe = primary->pipe;
dd584fc0
VS
3411 unsigned long irqflags;
3412
3413 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
62e0fb88 3414
dd584fc0
VS
3415 I915_WRITE_FW(PLANE_CTL(pipe, plane_id), 0);
3416 I915_WRITE_FW(PLANE_SURF(pipe, plane_id), 0);
3417 POSTING_READ_FW(PLANE_SURF(pipe, plane_id));
3418
3419 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
a8d201af 3420}
29b9bde6 3421
5a21b665
DV
3422static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
3423{
3424 struct intel_crtc *crtc;
3425
91c8a326 3426 for_each_intel_crtc(&dev_priv->drm, crtc)
5a21b665
DV
3427 intel_finish_page_flip_cs(dev_priv, crtc->pipe);
3428}
3429
7514747d
VS
3430static void intel_update_primary_planes(struct drm_device *dev)
3431{
7514747d 3432 struct drm_crtc *crtc;
96a02917 3433
70e1e0ec 3434 for_each_crtc(dev, crtc) {
11c22da6 3435 struct intel_plane *plane = to_intel_plane(crtc->primary);
73974893
ML
3436 struct intel_plane_state *plane_state =
3437 to_intel_plane_state(plane->base.state);
11c22da6 3438
72259536
VS
3439 if (plane_state->base.visible) {
3440 trace_intel_update_plane(&plane->base,
3441 to_intel_crtc(crtc));
3442
282dbf9b 3443 plane->update_plane(plane,
a8d201af
ML
3444 to_intel_crtc_state(crtc->state),
3445 plane_state);
72259536 3446 }
73974893
ML
3447 }
3448}
3449
3450static int
3451__intel_display_resume(struct drm_device *dev,
581e49fe
ML
3452 struct drm_atomic_state *state,
3453 struct drm_modeset_acquire_ctx *ctx)
73974893
ML
3454{
3455 struct drm_crtc_state *crtc_state;
3456 struct drm_crtc *crtc;
3457 int i, ret;
11c22da6 3458
aecd36b8 3459 intel_modeset_setup_hw_state(dev, ctx);
29b74b7f 3460 i915_redisable_vga(to_i915(dev));
73974893
ML
3461
3462 if (!state)
3463 return 0;
3464
aa5e9b47
ML
3465 /*
3466 * We've duplicated the state, pointers to the old state are invalid.
3467 *
3468 * Don't attempt to use the old state until we commit the duplicated state.
3469 */
3470 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
73974893
ML
3471 /*
3472 * Force recalculation even if we restore
3473 * current state. With fast modeset this may not result
3474 * in a modeset when the state is compatible.
3475 */
3476 crtc_state->mode_changed = true;
96a02917 3477 }
73974893
ML
3478
3479 /* ignore any reset values/BIOS leftovers in the WM registers */
602ae835
VS
3480 if (!HAS_GMCH_DISPLAY(to_i915(dev)))
3481 to_intel_atomic_state(state)->skip_intermediate_wm = true;
73974893 3482
581e49fe 3483 ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
73974893
ML
3484
3485 WARN_ON(ret == -EDEADLK);
3486 return ret;
96a02917
VS
3487}
3488
4ac2ba2f
VS
3489static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
3490{
ae98104b
VS
3491 return intel_has_gpu_reset(dev_priv) &&
3492 INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
4ac2ba2f
VS
3493}
3494
c033666a 3495void intel_prepare_reset(struct drm_i915_private *dev_priv)
7514747d 3496{
73974893
ML
3497 struct drm_device *dev = &dev_priv->drm;
3498 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
3499 struct drm_atomic_state *state;
3500 int ret;
3501
73974893
ML
3502 /*
3503 * Need mode_config.mutex so that we don't
3504 * trample ongoing ->detect() and whatnot.
3505 */
3506 mutex_lock(&dev->mode_config.mutex);
3507 drm_modeset_acquire_init(ctx, 0);
3508 while (1) {
3509 ret = drm_modeset_lock_all_ctx(dev, ctx);
3510 if (ret != -EDEADLK)
3511 break;
3512
3513 drm_modeset_backoff(ctx);
3514 }
3515
3516 /* reset doesn't touch the display, but flips might get nuked anyway, */
522a63de 3517 if (!i915.force_reset_modeset_test &&
4ac2ba2f 3518 !gpu_reset_clobbers_display(dev_priv))
7514747d
VS
3519 return;
3520
f98ce92f
VS
3521 /*
3522 * Disabling the crtcs gracefully seems nicer. Also the
3523 * g33 docs say we should at least disable all the planes.
3524 */
73974893
ML
3525 state = drm_atomic_helper_duplicate_state(dev, ctx);
3526 if (IS_ERR(state)) {
3527 ret = PTR_ERR(state);
73974893 3528 DRM_ERROR("Duplicating state failed with %i\n", ret);
1e5a15d6 3529 return;
73974893
ML
3530 }
3531
3532 ret = drm_atomic_helper_disable_all(dev, ctx);
3533 if (ret) {
3534 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
1e5a15d6
ACO
3535 drm_atomic_state_put(state);
3536 return;
73974893
ML
3537 }
3538
3539 dev_priv->modeset_restore_state = state;
3540 state->acquire_ctx = ctx;
7514747d
VS
3541}
3542
c033666a 3543void intel_finish_reset(struct drm_i915_private *dev_priv)
7514747d 3544{
73974893
ML
3545 struct drm_device *dev = &dev_priv->drm;
3546 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
3547 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
3548 int ret;
3549
5a21b665
DV
3550 /*
3551 * Flips in the rings will be nuked by the reset,
3552 * so complete all pending flips so that user space
3553 * will get its events and not get stuck.
3554 */
3555 intel_complete_page_flips(dev_priv);
3556
73974893
ML
3557 dev_priv->modeset_restore_state = NULL;
3558
7514747d 3559 /* reset doesn't touch the display */
4ac2ba2f 3560 if (!gpu_reset_clobbers_display(dev_priv)) {
522a63de
ML
3561 if (!state) {
3562 /*
3563 * Flips in the rings have been nuked by the reset,
3564 * so update the base address of all primary
3565 * planes to the the last fb to make sure we're
3566 * showing the correct fb after a reset.
3567 *
3568 * FIXME: Atomic will make this obsolete since we won't schedule
3569 * CS-based flips (which might get lost in gpu resets) any more.
3570 */
3571 intel_update_primary_planes(dev);
3572 } else {
581e49fe 3573 ret = __intel_display_resume(dev, state, ctx);
522a63de
ML
3574 if (ret)
3575 DRM_ERROR("Restoring old state failed with %i\n", ret);
3576 }
73974893
ML
3577 } else {
3578 /*
3579 * The display has been reset as well,
3580 * so need a full re-initialization.
3581 */
3582 intel_runtime_pm_disable_interrupts(dev_priv);
3583 intel_runtime_pm_enable_interrupts(dev_priv);
7514747d 3584
51f59205 3585 intel_pps_unlock_regs_wa(dev_priv);
73974893 3586 intel_modeset_init_hw(dev);
7514747d 3587
73974893
ML
3588 spin_lock_irq(&dev_priv->irq_lock);
3589 if (dev_priv->display.hpd_irq_setup)
3590 dev_priv->display.hpd_irq_setup(dev_priv);
3591 spin_unlock_irq(&dev_priv->irq_lock);
7514747d 3592
581e49fe 3593 ret = __intel_display_resume(dev, state, ctx);
73974893
ML
3594 if (ret)
3595 DRM_ERROR("Restoring old state failed with %i\n", ret);
7514747d 3596
73974893
ML
3597 intel_hpd_init(dev_priv);
3598 }
7514747d 3599
0853695c
CW
3600 if (state)
3601 drm_atomic_state_put(state);
73974893
ML
3602 drm_modeset_drop_locks(ctx);
3603 drm_modeset_acquire_fini(ctx);
3604 mutex_unlock(&dev->mode_config.mutex);
7514747d
VS
3605}
3606
8af29b0c
CW
3607static bool abort_flip_on_reset(struct intel_crtc *crtc)
3608{
3609 struct i915_gpu_error *error = &to_i915(crtc->base.dev)->gpu_error;
3610
8c185eca 3611 if (i915_reset_backoff(error))
8af29b0c
CW
3612 return true;
3613
3614 if (crtc->reset_count != i915_reset_count(error))
3615 return true;
3616
3617 return false;
3618}
3619
7d5e3799
CW
3620static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3621{
5a21b665
DV
3622 struct drm_device *dev = crtc->dev;
3623 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5a21b665
DV
3624 bool pending;
3625
8af29b0c 3626 if (abort_flip_on_reset(intel_crtc))
5a21b665
DV
3627 return false;
3628
3629 spin_lock_irq(&dev->event_lock);
3630 pending = to_intel_crtc(crtc)->flip_work != NULL;
3631 spin_unlock_irq(&dev->event_lock);
3632
3633 return pending;
7d5e3799
CW
3634}
3635
bfd16b2a
ML
3636static void intel_update_pipe_config(struct intel_crtc *crtc,
3637 struct intel_crtc_state *old_crtc_state)
e30e8f75 3638{
6315b5d3 3639 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
bfd16b2a
ML
3640 struct intel_crtc_state *pipe_config =
3641 to_intel_crtc_state(crtc->base.state);
e30e8f75 3642
bfd16b2a
ML
3643 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3644 crtc->base.mode = crtc->base.state->mode;
3645
e30e8f75
GP
3646 /*
3647 * Update pipe size and adjust fitter if needed: the reason for this is
3648 * that in compute_mode_changes we check the native mode (not the pfit
3649 * mode) to see if we can flip rather than do a full mode set. In the
3650 * fastboot case, we'll flip, but if we don't update the pipesrc and
3651 * pfit state, we'll end up with a big fb scanned out into the wrong
3652 * sized surface.
e30e8f75
GP
3653 */
3654
e30e8f75 3655 I915_WRITE(PIPESRC(crtc->pipe),
bfd16b2a
ML
3656 ((pipe_config->pipe_src_w - 1) << 16) |
3657 (pipe_config->pipe_src_h - 1));
3658
3659 /* on skylake this is done by detaching scalers */
6315b5d3 3660 if (INTEL_GEN(dev_priv) >= 9) {
bfd16b2a
ML
3661 skl_detach_scalers(crtc);
3662
3663 if (pipe_config->pch_pfit.enabled)
3664 skylake_pfit_enable(crtc);
6e266956 3665 } else if (HAS_PCH_SPLIT(dev_priv)) {
bfd16b2a
ML
3666 if (pipe_config->pch_pfit.enabled)
3667 ironlake_pfit_enable(crtc);
3668 else if (old_crtc_state->pch_pfit.enabled)
3669 ironlake_pfit_disable(crtc, true);
e30e8f75 3670 }
e30e8f75
GP
3671}
3672
4cbe4b2b 3673static void intel_fdi_normal_train(struct intel_crtc *crtc)
5e84e1a4 3674{
4cbe4b2b 3675 struct drm_device *dev = crtc->base.dev;
fac5e23e 3676 struct drm_i915_private *dev_priv = to_i915(dev);
4cbe4b2b 3677 int pipe = crtc->pipe;
f0f59a00
VS
3678 i915_reg_t reg;
3679 u32 temp;
5e84e1a4
ZW
3680
3681 /* enable normal train */
3682 reg = FDI_TX_CTL(pipe);
3683 temp = I915_READ(reg);
fd6b8f43 3684 if (IS_IVYBRIDGE(dev_priv)) {
357555c0
JB
3685 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3686 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
3687 } else {
3688 temp &= ~FDI_LINK_TRAIN_NONE;
3689 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 3690 }
5e84e1a4
ZW
3691 I915_WRITE(reg, temp);
3692
3693 reg = FDI_RX_CTL(pipe);
3694 temp = I915_READ(reg);
6e266956 3695 if (HAS_PCH_CPT(dev_priv)) {
5e84e1a4
ZW
3696 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3697 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3698 } else {
3699 temp &= ~FDI_LINK_TRAIN_NONE;
3700 temp |= FDI_LINK_TRAIN_NONE;
3701 }
3702 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3703
3704 /* wait one idle pattern time */
3705 POSTING_READ(reg);
3706 udelay(1000);
357555c0
JB
3707
3708 /* IVB wants error correction enabled */
fd6b8f43 3709 if (IS_IVYBRIDGE(dev_priv))
357555c0
JB
3710 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3711 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
3712}
3713
8db9d77b 3714/* The FDI link training functions for ILK/Ibexpeak. */
dc4a1094
ACO
3715static void ironlake_fdi_link_train(struct intel_crtc *crtc,
3716 const struct intel_crtc_state *crtc_state)
8db9d77b 3717{
4cbe4b2b 3718 struct drm_device *dev = crtc->base.dev;
fac5e23e 3719 struct drm_i915_private *dev_priv = to_i915(dev);
4cbe4b2b 3720 int pipe = crtc->pipe;
f0f59a00
VS
3721 i915_reg_t reg;
3722 u32 temp, tries;
8db9d77b 3723
1c8562f6 3724 /* FDI needs bits from pipe first */
0fc932b8 3725 assert_pipe_enabled(dev_priv, pipe);
0fc932b8 3726
e1a44743
AJ
3727 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3728 for train result */
5eddb70b
CW
3729 reg = FDI_RX_IMR(pipe);
3730 temp = I915_READ(reg);
e1a44743
AJ
3731 temp &= ~FDI_RX_SYMBOL_LOCK;
3732 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3733 I915_WRITE(reg, temp);
3734 I915_READ(reg);
e1a44743
AJ
3735 udelay(150);
3736
8db9d77b 3737 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3738 reg = FDI_TX_CTL(pipe);
3739 temp = I915_READ(reg);
627eb5a3 3740 temp &= ~FDI_DP_PORT_WIDTH_MASK;
dc4a1094 3741 temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
8db9d77b
ZW
3742 temp &= ~FDI_LINK_TRAIN_NONE;
3743 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 3744 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3745
5eddb70b
CW
3746 reg = FDI_RX_CTL(pipe);
3747 temp = I915_READ(reg);
8db9d77b
ZW
3748 temp &= ~FDI_LINK_TRAIN_NONE;
3749 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b
CW
3750 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3751
3752 POSTING_READ(reg);
8db9d77b
ZW
3753 udelay(150);
3754
5b2adf89 3755 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
DV
3756 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3757 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3758 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 3759
5eddb70b 3760 reg = FDI_RX_IIR(pipe);
e1a44743 3761 for (tries = 0; tries < 5; tries++) {
5eddb70b 3762 temp = I915_READ(reg);
8db9d77b
ZW
3763 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3764
3765 if ((temp & FDI_RX_BIT_LOCK)) {
3766 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 3767 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
3768 break;
3769 }
8db9d77b 3770 }
e1a44743 3771 if (tries == 5)
5eddb70b 3772 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3773
3774 /* Train 2 */
5eddb70b
CW
3775 reg = FDI_TX_CTL(pipe);
3776 temp = I915_READ(reg);
8db9d77b
ZW
3777 temp &= ~FDI_LINK_TRAIN_NONE;
3778 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3779 I915_WRITE(reg, temp);
8db9d77b 3780
5eddb70b
CW
3781 reg = FDI_RX_CTL(pipe);
3782 temp = I915_READ(reg);
8db9d77b
ZW
3783 temp &= ~FDI_LINK_TRAIN_NONE;
3784 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3785 I915_WRITE(reg, temp);
8db9d77b 3786
5eddb70b
CW
3787 POSTING_READ(reg);
3788 udelay(150);
8db9d77b 3789
5eddb70b 3790 reg = FDI_RX_IIR(pipe);
e1a44743 3791 for (tries = 0; tries < 5; tries++) {
5eddb70b 3792 temp = I915_READ(reg);
8db9d77b
ZW
3793 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3794
3795 if (temp & FDI_RX_SYMBOL_LOCK) {
5eddb70b 3796 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
3797 DRM_DEBUG_KMS("FDI train 2 done.\n");
3798 break;
3799 }
8db9d77b 3800 }
e1a44743 3801 if (tries == 5)
5eddb70b 3802 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3803
3804 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 3805
8db9d77b
ZW
3806}
3807
0206e353 3808static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
3809 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3810 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3811 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3812 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3813};
3814
3815/* The FDI link training functions for SNB/Cougarpoint. */
dc4a1094
ACO
3816static void gen6_fdi_link_train(struct intel_crtc *crtc,
3817 const struct intel_crtc_state *crtc_state)
8db9d77b 3818{
4cbe4b2b 3819 struct drm_device *dev = crtc->base.dev;
fac5e23e 3820 struct drm_i915_private *dev_priv = to_i915(dev);
4cbe4b2b 3821 int pipe = crtc->pipe;
f0f59a00
VS
3822 i915_reg_t reg;
3823 u32 temp, i, retry;
8db9d77b 3824
e1a44743
AJ
3825 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3826 for train result */
5eddb70b
CW
3827 reg = FDI_RX_IMR(pipe);
3828 temp = I915_READ(reg);
e1a44743
AJ
3829 temp &= ~FDI_RX_SYMBOL_LOCK;
3830 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3831 I915_WRITE(reg, temp);
3832
3833 POSTING_READ(reg);
e1a44743
AJ
3834 udelay(150);
3835
8db9d77b 3836 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3837 reg = FDI_TX_CTL(pipe);
3838 temp = I915_READ(reg);
627eb5a3 3839 temp &= ~FDI_DP_PORT_WIDTH_MASK;
dc4a1094 3840 temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
8db9d77b
ZW
3841 temp &= ~FDI_LINK_TRAIN_NONE;
3842 temp |= FDI_LINK_TRAIN_PATTERN_1;
3843 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3844 /* SNB-B */
3845 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 3846 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3847
d74cf324
DV
3848 I915_WRITE(FDI_RX_MISC(pipe),
3849 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3850
5eddb70b
CW
3851 reg = FDI_RX_CTL(pipe);
3852 temp = I915_READ(reg);
6e266956 3853 if (HAS_PCH_CPT(dev_priv)) {
8db9d77b
ZW
3854 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3855 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3856 } else {
3857 temp &= ~FDI_LINK_TRAIN_NONE;
3858 temp |= FDI_LINK_TRAIN_PATTERN_1;
3859 }
5eddb70b
CW
3860 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3861
3862 POSTING_READ(reg);
8db9d77b
ZW
3863 udelay(150);
3864
0206e353 3865 for (i = 0; i < 4; i++) {
5eddb70b
CW
3866 reg = FDI_TX_CTL(pipe);
3867 temp = I915_READ(reg);
8db9d77b
ZW
3868 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3869 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3870 I915_WRITE(reg, temp);
3871
3872 POSTING_READ(reg);
8db9d77b
ZW
3873 udelay(500);
3874
fa37d39e
SP
3875 for (retry = 0; retry < 5; retry++) {
3876 reg = FDI_RX_IIR(pipe);
3877 temp = I915_READ(reg);
3878 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3879 if (temp & FDI_RX_BIT_LOCK) {
3880 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3881 DRM_DEBUG_KMS("FDI train 1 done.\n");
3882 break;
3883 }
3884 udelay(50);
8db9d77b 3885 }
fa37d39e
SP
3886 if (retry < 5)
3887 break;
8db9d77b
ZW
3888 }
3889 if (i == 4)
5eddb70b 3890 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3891
3892 /* Train 2 */
5eddb70b
CW
3893 reg = FDI_TX_CTL(pipe);
3894 temp = I915_READ(reg);
8db9d77b
ZW
3895 temp &= ~FDI_LINK_TRAIN_NONE;
3896 temp |= FDI_LINK_TRAIN_PATTERN_2;
5db94019 3897 if (IS_GEN6(dev_priv)) {
8db9d77b
ZW
3898 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3899 /* SNB-B */
3900 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3901 }
5eddb70b 3902 I915_WRITE(reg, temp);
8db9d77b 3903
5eddb70b
CW
3904 reg = FDI_RX_CTL(pipe);
3905 temp = I915_READ(reg);
6e266956 3906 if (HAS_PCH_CPT(dev_priv)) {
8db9d77b
ZW
3907 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3908 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3909 } else {
3910 temp &= ~FDI_LINK_TRAIN_NONE;
3911 temp |= FDI_LINK_TRAIN_PATTERN_2;
3912 }
5eddb70b
CW
3913 I915_WRITE(reg, temp);
3914
3915 POSTING_READ(reg);
8db9d77b
ZW
3916 udelay(150);
3917
0206e353 3918 for (i = 0; i < 4; i++) {
5eddb70b
CW
3919 reg = FDI_TX_CTL(pipe);
3920 temp = I915_READ(reg);
8db9d77b
ZW
3921 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3922 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3923 I915_WRITE(reg, temp);
3924
3925 POSTING_READ(reg);
8db9d77b
ZW
3926 udelay(500);
3927
fa37d39e
SP
3928 for (retry = 0; retry < 5; retry++) {
3929 reg = FDI_RX_IIR(pipe);
3930 temp = I915_READ(reg);
3931 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3932 if (temp & FDI_RX_SYMBOL_LOCK) {
3933 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3934 DRM_DEBUG_KMS("FDI train 2 done.\n");
3935 break;
3936 }
3937 udelay(50);
8db9d77b 3938 }
fa37d39e
SP
3939 if (retry < 5)
3940 break;
8db9d77b
ZW
3941 }
3942 if (i == 4)
5eddb70b 3943 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3944
3945 DRM_DEBUG_KMS("FDI train done.\n");
3946}
3947
357555c0 3948/* Manual link training for Ivy Bridge A0 parts */
dc4a1094
ACO
3949static void ivb_manual_fdi_link_train(struct intel_crtc *crtc,
3950 const struct intel_crtc_state *crtc_state)
357555c0 3951{
4cbe4b2b 3952 struct drm_device *dev = crtc->base.dev;
fac5e23e 3953 struct drm_i915_private *dev_priv = to_i915(dev);
4cbe4b2b 3954 int pipe = crtc->pipe;
f0f59a00
VS
3955 i915_reg_t reg;
3956 u32 temp, i, j;
357555c0
JB
3957
3958 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3959 for train result */
3960 reg = FDI_RX_IMR(pipe);
3961 temp = I915_READ(reg);
3962 temp &= ~FDI_RX_SYMBOL_LOCK;
3963 temp &= ~FDI_RX_BIT_LOCK;
3964 I915_WRITE(reg, temp);
3965
3966 POSTING_READ(reg);
3967 udelay(150);
3968
01a415fd
DV
3969 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3970 I915_READ(FDI_RX_IIR(pipe)));
3971
139ccd3f
JB
3972 /* Try each vswing and preemphasis setting twice before moving on */
3973 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3974 /* disable first in case we need to retry */
3975 reg = FDI_TX_CTL(pipe);
3976 temp = I915_READ(reg);
3977 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3978 temp &= ~FDI_TX_ENABLE;
3979 I915_WRITE(reg, temp);
357555c0 3980
139ccd3f
JB
3981 reg = FDI_RX_CTL(pipe);
3982 temp = I915_READ(reg);
3983 temp &= ~FDI_LINK_TRAIN_AUTO;
3984 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3985 temp &= ~FDI_RX_ENABLE;
3986 I915_WRITE(reg, temp);
357555c0 3987
139ccd3f 3988 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
3989 reg = FDI_TX_CTL(pipe);
3990 temp = I915_READ(reg);
139ccd3f 3991 temp &= ~FDI_DP_PORT_WIDTH_MASK;
dc4a1094 3992 temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
139ccd3f 3993 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 3994 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
3995 temp |= snb_b_fdi_train_param[j/2];
3996 temp |= FDI_COMPOSITE_SYNC;
3997 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 3998
139ccd3f
JB
3999 I915_WRITE(FDI_RX_MISC(pipe),
4000 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 4001
139ccd3f 4002 reg = FDI_RX_CTL(pipe);
357555c0 4003 temp = I915_READ(reg);
139ccd3f
JB
4004 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
4005 temp |= FDI_COMPOSITE_SYNC;
4006 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 4007
139ccd3f
JB
4008 POSTING_READ(reg);
4009 udelay(1); /* should be 0.5us */
357555c0 4010
139ccd3f
JB
4011 for (i = 0; i < 4; i++) {
4012 reg = FDI_RX_IIR(pipe);
4013 temp = I915_READ(reg);
4014 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 4015
139ccd3f
JB
4016 if (temp & FDI_RX_BIT_LOCK ||
4017 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
4018 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
4019 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
4020 i);
4021 break;
4022 }
4023 udelay(1); /* should be 0.5us */
4024 }
4025 if (i == 4) {
4026 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
4027 continue;
4028 }
357555c0 4029
139ccd3f 4030 /* Train 2 */
357555c0
JB
4031 reg = FDI_TX_CTL(pipe);
4032 temp = I915_READ(reg);
139ccd3f
JB
4033 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
4034 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
4035 I915_WRITE(reg, temp);
4036
4037 reg = FDI_RX_CTL(pipe);
4038 temp = I915_READ(reg);
4039 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4040 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
4041 I915_WRITE(reg, temp);
4042
4043 POSTING_READ(reg);
139ccd3f 4044 udelay(2); /* should be 1.5us */
357555c0 4045
139ccd3f
JB
4046 for (i = 0; i < 4; i++) {
4047 reg = FDI_RX_IIR(pipe);
4048 temp = I915_READ(reg);
4049 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 4050
139ccd3f
JB
4051 if (temp & FDI_RX_SYMBOL_LOCK ||
4052 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
4053 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
4054 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
4055 i);
4056 goto train_done;
4057 }
4058 udelay(2); /* should be 1.5us */
357555c0 4059 }
139ccd3f
JB
4060 if (i == 4)
4061 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 4062 }
357555c0 4063
139ccd3f 4064train_done:
357555c0
JB
4065 DRM_DEBUG_KMS("FDI train done.\n");
4066}
4067
88cefb6c 4068static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 4069{
88cefb6c 4070 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 4071 struct drm_i915_private *dev_priv = to_i915(dev);
2c07245f 4072 int pipe = intel_crtc->pipe;
f0f59a00
VS
4073 i915_reg_t reg;
4074 u32 temp;
c64e311e 4075
c98e9dcf 4076 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
4077 reg = FDI_RX_CTL(pipe);
4078 temp = I915_READ(reg);
627eb5a3 4079 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
6e3c9717 4080 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
dfd07d72 4081 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
4082 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
4083
4084 POSTING_READ(reg);
c98e9dcf
JB
4085 udelay(200);
4086
4087 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
4088 temp = I915_READ(reg);
4089 I915_WRITE(reg, temp | FDI_PCDCLK);
4090
4091 POSTING_READ(reg);
c98e9dcf
JB
4092 udelay(200);
4093
20749730
PZ
4094 /* Enable CPU FDI TX PLL, always on for Ironlake */
4095 reg = FDI_TX_CTL(pipe);
4096 temp = I915_READ(reg);
4097 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
4098 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 4099
20749730
PZ
4100 POSTING_READ(reg);
4101 udelay(100);
6be4a607 4102 }
0e23b99d
JB
4103}
4104
88cefb6c
DV
4105static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
4106{
4107 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 4108 struct drm_i915_private *dev_priv = to_i915(dev);
88cefb6c 4109 int pipe = intel_crtc->pipe;
f0f59a00
VS
4110 i915_reg_t reg;
4111 u32 temp;
88cefb6c
DV
4112
4113 /* Switch from PCDclk to Rawclk */
4114 reg = FDI_RX_CTL(pipe);
4115 temp = I915_READ(reg);
4116 I915_WRITE(reg, temp & ~FDI_PCDCLK);
4117
4118 /* Disable CPU FDI TX PLL */
4119 reg = FDI_TX_CTL(pipe);
4120 temp = I915_READ(reg);
4121 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
4122
4123 POSTING_READ(reg);
4124 udelay(100);
4125
4126 reg = FDI_RX_CTL(pipe);
4127 temp = I915_READ(reg);
4128 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
4129
4130 /* Wait for the clocks to turn off. */
4131 POSTING_READ(reg);
4132 udelay(100);
4133}
4134
0fc932b8
JB
4135static void ironlake_fdi_disable(struct drm_crtc *crtc)
4136{
4137 struct drm_device *dev = crtc->dev;
fac5e23e 4138 struct drm_i915_private *dev_priv = to_i915(dev);
0fc932b8
JB
4139 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4140 int pipe = intel_crtc->pipe;
f0f59a00
VS
4141 i915_reg_t reg;
4142 u32 temp;
0fc932b8
JB
4143
4144 /* disable CPU FDI tx and PCH FDI rx */
4145 reg = FDI_TX_CTL(pipe);
4146 temp = I915_READ(reg);
4147 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
4148 POSTING_READ(reg);
4149
4150 reg = FDI_RX_CTL(pipe);
4151 temp = I915_READ(reg);
4152 temp &= ~(0x7 << 16);
dfd07d72 4153 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
4154 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
4155
4156 POSTING_READ(reg);
4157 udelay(100);
4158
4159 /* Ironlake workaround, disable clock pointer after downing FDI */
6e266956 4160 if (HAS_PCH_IBX(dev_priv))
6f06ce18 4161 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
0fc932b8
JB
4162
4163 /* still set train pattern 1 */
4164 reg = FDI_TX_CTL(pipe);
4165 temp = I915_READ(reg);
4166 temp &= ~FDI_LINK_TRAIN_NONE;
4167 temp |= FDI_LINK_TRAIN_PATTERN_1;
4168 I915_WRITE(reg, temp);
4169
4170 reg = FDI_RX_CTL(pipe);
4171 temp = I915_READ(reg);
6e266956 4172 if (HAS_PCH_CPT(dev_priv)) {
0fc932b8
JB
4173 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4174 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
4175 } else {
4176 temp &= ~FDI_LINK_TRAIN_NONE;
4177 temp |= FDI_LINK_TRAIN_PATTERN_1;
4178 }
4179 /* BPC in FDI rx is consistent with that in PIPECONF */
4180 temp &= ~(0x07 << 16);
dfd07d72 4181 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
4182 I915_WRITE(reg, temp);
4183
4184 POSTING_READ(reg);
4185 udelay(100);
4186}
4187
49d73912 4188bool intel_has_pending_fb_unpin(struct drm_i915_private *dev_priv)
5dce5b93
CW
4189{
4190 struct intel_crtc *crtc;
4191
4192 /* Note that we don't need to be called with mode_config.lock here
4193 * as our list of CRTC objects is static for the lifetime of the
4194 * device and so cannot disappear as we iterate. Similarly, we can
4195 * happily treat the predicates as racy, atomic checks as userspace
4196 * cannot claim and pin a new fb without at least acquring the
4197 * struct_mutex and so serialising with us.
4198 */
49d73912 4199 for_each_intel_crtc(&dev_priv->drm, crtc) {
5dce5b93
CW
4200 if (atomic_read(&crtc->unpin_work_count) == 0)
4201 continue;
4202
5a21b665 4203 if (crtc->flip_work)
0f0f74bc 4204 intel_wait_for_vblank(dev_priv, crtc->pipe);
5dce5b93
CW
4205
4206 return true;
4207 }
4208
4209 return false;
4210}
4211
5a21b665 4212static void page_flip_completed(struct intel_crtc *intel_crtc)
d6bbafa1
CW
4213{
4214 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
5a21b665
DV
4215 struct intel_flip_work *work = intel_crtc->flip_work;
4216
4217 intel_crtc->flip_work = NULL;
d6bbafa1
CW
4218
4219 if (work->event)
560ce1dc 4220 drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
d6bbafa1
CW
4221
4222 drm_crtc_vblank_put(&intel_crtc->base);
4223
5a21b665 4224 wake_up_all(&dev_priv->pending_flip_queue);
5a21b665
DV
4225 trace_i915_flip_complete(intel_crtc->plane,
4226 work->pending_flip_obj);
05c41f92
AR
4227
4228 queue_work(dev_priv->wq, &work->unpin_work);
d6bbafa1
CW
4229}
4230
5008e874 4231static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
e6c3a2a6 4232{
0f91128d 4233 struct drm_device *dev = crtc->dev;
fac5e23e 4234 struct drm_i915_private *dev_priv = to_i915(dev);
5008e874 4235 long ret;
e6c3a2a6 4236
2c10d571 4237 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
5008e874
ML
4238
4239 ret = wait_event_interruptible_timeout(
4240 dev_priv->pending_flip_queue,
4241 !intel_crtc_has_pending_flip(crtc),
4242 60*HZ);
4243
4244 if (ret < 0)
4245 return ret;
4246
5a21b665
DV
4247 if (ret == 0) {
4248 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4249 struct intel_flip_work *work;
4250
4251 spin_lock_irq(&dev->event_lock);
4252 work = intel_crtc->flip_work;
4253 if (work && !is_mmio_work(work)) {
4254 WARN_ONCE(1, "Removing stuck page flip\n");
4255 page_flip_completed(intel_crtc);
4256 }
4257 spin_unlock_irq(&dev->event_lock);
4258 }
5bb61643 4259
5008e874 4260 return 0;
e6c3a2a6
CW
4261}
4262
b7076546 4263void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
060f02d8
VS
4264{
4265 u32 temp;
4266
4267 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
4268
4269 mutex_lock(&dev_priv->sb_lock);
4270
4271 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4272 temp |= SBI_SSCCTL_DISABLE;
4273 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
4274
4275 mutex_unlock(&dev_priv->sb_lock);
4276}
4277
e615efe4 4278/* Program iCLKIP clock to the desired frequency */
0dcdc382 4279static void lpt_program_iclkip(struct intel_crtc *crtc)
e615efe4 4280{
0dcdc382
ACO
4281 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4282 int clock = crtc->config->base.adjusted_mode.crtc_clock;
e615efe4
ED
4283 u32 divsel, phaseinc, auxdiv, phasedir = 0;
4284 u32 temp;
4285
060f02d8 4286 lpt_disable_iclkip(dev_priv);
e615efe4 4287
64b46a06
VS
4288 /* The iCLK virtual clock root frequency is in MHz,
4289 * but the adjusted_mode->crtc_clock in in KHz. To get the
4290 * divisors, it is necessary to divide one by another, so we
4291 * convert the virtual clock precision to KHz here for higher
4292 * precision.
4293 */
4294 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
e615efe4
ED
4295 u32 iclk_virtual_root_freq = 172800 * 1000;
4296 u32 iclk_pi_range = 64;
64b46a06 4297 u32 desired_divisor;
e615efe4 4298
64b46a06
VS
4299 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4300 clock << auxdiv);
4301 divsel = (desired_divisor / iclk_pi_range) - 2;
4302 phaseinc = desired_divisor % iclk_pi_range;
e615efe4 4303
64b46a06
VS
4304 /*
4305 * Near 20MHz is a corner case which is
4306 * out of range for the 7-bit divisor
4307 */
4308 if (divsel <= 0x7f)
4309 break;
e615efe4
ED
4310 }
4311
4312 /* This should not happen with any sane values */
4313 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
4314 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
4315 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
4316 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
4317
4318 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 4319 clock,
e615efe4
ED
4320 auxdiv,
4321 divsel,
4322 phasedir,
4323 phaseinc);
4324
060f02d8
VS
4325 mutex_lock(&dev_priv->sb_lock);
4326
e615efe4 4327 /* Program SSCDIVINTPHASE6 */
988d6ee8 4328 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
4329 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
4330 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
4331 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
4332 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
4333 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
4334 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 4335 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
4336
4337 /* Program SSCAUXDIV */
988d6ee8 4338 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
4339 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
4340 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 4341 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
4342
4343 /* Enable modulator and associated divider */
988d6ee8 4344 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 4345 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 4346 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4 4347
060f02d8
VS
4348 mutex_unlock(&dev_priv->sb_lock);
4349
e615efe4
ED
4350 /* Wait for initialization time */
4351 udelay(24);
4352
4353 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
4354}
4355
8802e5b6
VS
4356int lpt_get_iclkip(struct drm_i915_private *dev_priv)
4357{
4358 u32 divsel, phaseinc, auxdiv;
4359 u32 iclk_virtual_root_freq = 172800 * 1000;
4360 u32 iclk_pi_range = 64;
4361 u32 desired_divisor;
4362 u32 temp;
4363
4364 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
4365 return 0;
4366
4367 mutex_lock(&dev_priv->sb_lock);
4368
4369 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4370 if (temp & SBI_SSCCTL_DISABLE) {
4371 mutex_unlock(&dev_priv->sb_lock);
4372 return 0;
4373 }
4374
4375 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
4376 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
4377 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
4378 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
4379 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
4380
4381 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
4382 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
4383 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
4384
4385 mutex_unlock(&dev_priv->sb_lock);
4386
4387 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
4388
4389 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4390 desired_divisor << auxdiv);
4391}
4392
275f01b2
DV
4393static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4394 enum pipe pch_transcoder)
4395{
4396 struct drm_device *dev = crtc->base.dev;
fac5e23e 4397 struct drm_i915_private *dev_priv = to_i915(dev);
6e3c9717 4398 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
275f01b2
DV
4399
4400 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4401 I915_READ(HTOTAL(cpu_transcoder)));
4402 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4403 I915_READ(HBLANK(cpu_transcoder)));
4404 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4405 I915_READ(HSYNC(cpu_transcoder)));
4406
4407 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4408 I915_READ(VTOTAL(cpu_transcoder)));
4409 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4410 I915_READ(VBLANK(cpu_transcoder)));
4411 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4412 I915_READ(VSYNC(cpu_transcoder)));
4413 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4414 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4415}
4416
003632d9 4417static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
1fbc0d78 4418{
fac5e23e 4419 struct drm_i915_private *dev_priv = to_i915(dev);
1fbc0d78
DV
4420 uint32_t temp;
4421
4422 temp = I915_READ(SOUTH_CHICKEN1);
003632d9 4423 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
1fbc0d78
DV
4424 return;
4425
4426 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4427 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4428
003632d9
ACO
4429 temp &= ~FDI_BC_BIFURCATION_SELECT;
4430 if (enable)
4431 temp |= FDI_BC_BIFURCATION_SELECT;
4432
4433 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
1fbc0d78
DV
4434 I915_WRITE(SOUTH_CHICKEN1, temp);
4435 POSTING_READ(SOUTH_CHICKEN1);
4436}
4437
4438static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4439{
4440 struct drm_device *dev = intel_crtc->base.dev;
1fbc0d78
DV
4441
4442 switch (intel_crtc->pipe) {
4443 case PIPE_A:
4444 break;
4445 case PIPE_B:
6e3c9717 4446 if (intel_crtc->config->fdi_lanes > 2)
003632d9 4447 cpt_set_fdi_bc_bifurcation(dev, false);
1fbc0d78 4448 else
003632d9 4449 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4450
4451 break;
4452 case PIPE_C:
003632d9 4453 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4454
4455 break;
4456 default:
4457 BUG();
4458 }
4459}
4460
c48b5305
VS
4461/* Return which DP Port should be selected for Transcoder DP control */
4462static enum port
4cbe4b2b 4463intel_trans_dp_port_sel(struct intel_crtc *crtc)
c48b5305 4464{
4cbe4b2b 4465 struct drm_device *dev = crtc->base.dev;
c48b5305
VS
4466 struct intel_encoder *encoder;
4467
4cbe4b2b 4468 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
cca0502b 4469 if (encoder->type == INTEL_OUTPUT_DP ||
c48b5305
VS
4470 encoder->type == INTEL_OUTPUT_EDP)
4471 return enc_to_dig_port(&encoder->base)->port;
4472 }
4473
4474 return -1;
4475}
4476
f67a559d
JB
4477/*
4478 * Enable PCH resources required for PCH ports:
4479 * - PCH PLLs
4480 * - FDI training & RX/TX
4481 * - update transcoder timings
4482 * - DP transcoding bits
4483 * - transcoder
4484 */
2ce42273 4485static void ironlake_pch_enable(const struct intel_crtc_state *crtc_state)
0e23b99d 4486{
2ce42273 4487 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
4cbe4b2b 4488 struct drm_device *dev = crtc->base.dev;
fac5e23e 4489 struct drm_i915_private *dev_priv = to_i915(dev);
4cbe4b2b 4490 int pipe = crtc->pipe;
f0f59a00 4491 u32 temp;
2c07245f 4492
ab9412ba 4493 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 4494
fd6b8f43 4495 if (IS_IVYBRIDGE(dev_priv))
4cbe4b2b 4496 ivybridge_update_fdi_bc_bifurcation(crtc);
1fbc0d78 4497
cd986abb
DV
4498 /* Write the TU size bits before fdi link training, so that error
4499 * detection works. */
4500 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4501 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4502
c98e9dcf 4503 /* For PCH output, training FDI link */
dc4a1094 4504 dev_priv->display.fdi_link_train(crtc, crtc_state);
2c07245f 4505
3ad8a208
DV
4506 /* We need to program the right clock selection before writing the pixel
4507 * mutliplier into the DPLL. */
6e266956 4508 if (HAS_PCH_CPT(dev_priv)) {
ee7b9f93 4509 u32 sel;
4b645f14 4510
c98e9dcf 4511 temp = I915_READ(PCH_DPLL_SEL);
11887397
DV
4512 temp |= TRANS_DPLL_ENABLE(pipe);
4513 sel = TRANS_DPLLB_SEL(pipe);
2ce42273 4514 if (crtc_state->shared_dpll ==
8106ddbd 4515 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
ee7b9f93
JB
4516 temp |= sel;
4517 else
4518 temp &= ~sel;
c98e9dcf 4519 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 4520 }
5eddb70b 4521
3ad8a208
DV
4522 /* XXX: pch pll's can be enabled any time before we enable the PCH
4523 * transcoder, and we actually should do this to not upset any PCH
4524 * transcoder that already use the clock when we share it.
4525 *
4526 * Note that enable_shared_dpll tries to do the right thing, but
4527 * get_shared_dpll unconditionally resets the pll - we need that to have
4528 * the right LVDS enable sequence. */
4cbe4b2b 4529 intel_enable_shared_dpll(crtc);
3ad8a208 4530
d9b6cb56
JB
4531 /* set transcoder timing, panel must allow it */
4532 assert_panel_unlocked(dev_priv, pipe);
4cbe4b2b 4533 ironlake_pch_transcoder_set_timings(crtc, pipe);
8db9d77b 4534
303b81e0 4535 intel_fdi_normal_train(crtc);
5e84e1a4 4536
c98e9dcf 4537 /* For PCH DP, enable TRANS_DP_CTL */
6e266956 4538 if (HAS_PCH_CPT(dev_priv) &&
2ce42273 4539 intel_crtc_has_dp_encoder(crtc_state)) {
9c4edaee 4540 const struct drm_display_mode *adjusted_mode =
2ce42273 4541 &crtc_state->base.adjusted_mode;
dfd07d72 4542 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
f0f59a00 4543 i915_reg_t reg = TRANS_DP_CTL(pipe);
5eddb70b
CW
4544 temp = I915_READ(reg);
4545 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
4546 TRANS_DP_SYNC_MASK |
4547 TRANS_DP_BPC_MASK);
e3ef4479 4548 temp |= TRANS_DP_OUTPUT_ENABLE;
9325c9f0 4549 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf 4550
9c4edaee 4551 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 4552 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
9c4edaee 4553 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 4554 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf
JB
4555
4556 switch (intel_trans_dp_port_sel(crtc)) {
c48b5305 4557 case PORT_B:
5eddb70b 4558 temp |= TRANS_DP_PORT_SEL_B;
c98e9dcf 4559 break;
c48b5305 4560 case PORT_C:
5eddb70b 4561 temp |= TRANS_DP_PORT_SEL_C;
c98e9dcf 4562 break;
c48b5305 4563 case PORT_D:
5eddb70b 4564 temp |= TRANS_DP_PORT_SEL_D;
c98e9dcf
JB
4565 break;
4566 default:
e95d41e1 4567 BUG();
32f9d658 4568 }
2c07245f 4569
5eddb70b 4570 I915_WRITE(reg, temp);
6be4a607 4571 }
b52eb4dc 4572
b8a4f404 4573 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
4574}
4575
2ce42273 4576static void lpt_pch_enable(const struct intel_crtc_state *crtc_state)
1507e5bd 4577{
2ce42273 4578 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
0dcdc382 4579 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2ce42273 4580 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
1507e5bd 4581
ab9412ba 4582 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
1507e5bd 4583
8c52b5e8 4584 lpt_program_iclkip(crtc);
1507e5bd 4585
0540e488 4586 /* Set transcoder timing. */
0dcdc382 4587 ironlake_pch_transcoder_set_timings(crtc, PIPE_A);
1507e5bd 4588
937bb610 4589 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
4590}
4591
a1520318 4592static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57 4593{
fac5e23e 4594 struct drm_i915_private *dev_priv = to_i915(dev);
f0f59a00 4595 i915_reg_t dslreg = PIPEDSL(pipe);
d4270e57
JB
4596 u32 temp;
4597
4598 temp = I915_READ(dslreg);
4599 udelay(500);
4600 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 4601 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 4602 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
4603 }
4604}
4605
86adf9d7
ML
4606static int
4607skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
d96a7d2a 4608 unsigned int scaler_user, int *scaler_id,
86adf9d7 4609 int src_w, int src_h, int dst_w, int dst_h)
a1b2278e 4610{
86adf9d7
ML
4611 struct intel_crtc_scaler_state *scaler_state =
4612 &crtc_state->scaler_state;
4613 struct intel_crtc *intel_crtc =
4614 to_intel_crtc(crtc_state->base.crtc);
7f58cbb1
MK
4615 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
4616 const struct drm_display_mode *adjusted_mode =
4617 &crtc_state->base.adjusted_mode;
a1b2278e 4618 int need_scaling;
6156a456 4619
d96a7d2a
VS
4620 /*
4621 * Src coordinates are already rotated by 270 degrees for
4622 * the 90/270 degree plane rotation cases (to match the
4623 * GTT mapping), hence no need to account for rotation here.
4624 */
4625 need_scaling = src_w != dst_w || src_h != dst_h;
a1b2278e 4626
7f58cbb1
MK
4627 /*
4628 * Scaling/fitting not supported in IF-ID mode in GEN9+
4629 * TODO: Interlace fetch mode doesn't support YUV420 planar formats.
4630 * Once NV12 is enabled, handle it here while allocating scaler
4631 * for NV12.
4632 */
4633 if (INTEL_GEN(dev_priv) >= 9 && crtc_state->base.enable &&
4634 need_scaling && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
4635 DRM_DEBUG_KMS("Pipe/Plane scaling not supported with IF-ID mode\n");
4636 return -EINVAL;
4637 }
4638
a1b2278e
CK
4639 /*
4640 * if plane is being disabled or scaler is no more required or force detach
4641 * - free scaler binded to this plane/crtc
4642 * - in order to do this, update crtc->scaler_usage
4643 *
4644 * Here scaler state in crtc_state is set free so that
4645 * scaler can be assigned to other user. Actual register
4646 * update to free the scaler is done in plane/panel-fit programming.
4647 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4648 */
86adf9d7 4649 if (force_detach || !need_scaling) {
a1b2278e 4650 if (*scaler_id >= 0) {
86adf9d7 4651 scaler_state->scaler_users &= ~(1 << scaler_user);
a1b2278e
CK
4652 scaler_state->scalers[*scaler_id].in_use = 0;
4653
86adf9d7
ML
4654 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4655 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4656 intel_crtc->pipe, scaler_user, *scaler_id,
a1b2278e
CK
4657 scaler_state->scaler_users);
4658 *scaler_id = -1;
4659 }
4660 return 0;
4661 }
4662
4663 /* range checks */
4664 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4665 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4666
4667 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4668 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
86adf9d7 4669 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
a1b2278e 4670 "size is out of scaler range\n",
86adf9d7 4671 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
a1b2278e
CK
4672 return -EINVAL;
4673 }
4674
86adf9d7
ML
4675 /* mark this plane as a scaler user in crtc_state */
4676 scaler_state->scaler_users |= (1 << scaler_user);
4677 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4678 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4679 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4680 scaler_state->scaler_users);
4681
4682 return 0;
4683}
4684
4685/**
4686 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4687 *
4688 * @state: crtc's scaler state
86adf9d7
ML
4689 *
4690 * Return
4691 * 0 - scaler_usage updated successfully
4692 * error - requested scaling cannot be supported or other error condition
4693 */
e435d6e5 4694int skl_update_scaler_crtc(struct intel_crtc_state *state)
86adf9d7 4695{
7c5f93b0 4696 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
86adf9d7 4697
e435d6e5 4698 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
d96a7d2a 4699 &state->scaler_state.scaler_id,
86adf9d7 4700 state->pipe_src_w, state->pipe_src_h,
aad941d5 4701 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
86adf9d7
ML
4702}
4703
4704/**
4705 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4706 *
4707 * @state: crtc's scaler state
86adf9d7
ML
4708 * @plane_state: atomic plane state to update
4709 *
4710 * Return
4711 * 0 - scaler_usage updated successfully
4712 * error - requested scaling cannot be supported or other error condition
4713 */
da20eabd
ML
4714static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4715 struct intel_plane_state *plane_state)
86adf9d7
ML
4716{
4717
da20eabd
ML
4718 struct intel_plane *intel_plane =
4719 to_intel_plane(plane_state->base.plane);
86adf9d7
ML
4720 struct drm_framebuffer *fb = plane_state->base.fb;
4721 int ret;
4722
936e71e3 4723 bool force_detach = !fb || !plane_state->base.visible;
86adf9d7 4724
86adf9d7
ML
4725 ret = skl_update_scaler(crtc_state, force_detach,
4726 drm_plane_index(&intel_plane->base),
4727 &plane_state->scaler_id,
936e71e3
VS
4728 drm_rect_width(&plane_state->base.src) >> 16,
4729 drm_rect_height(&plane_state->base.src) >> 16,
4730 drm_rect_width(&plane_state->base.dst),
4731 drm_rect_height(&plane_state->base.dst));
86adf9d7
ML
4732
4733 if (ret || plane_state->scaler_id < 0)
4734 return ret;
4735
a1b2278e 4736 /* check colorkey */
818ed961 4737 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
72660ce0
VS
4738 DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
4739 intel_plane->base.base.id,
4740 intel_plane->base.name);
a1b2278e
CK
4741 return -EINVAL;
4742 }
4743
4744 /* Check src format */
438b74a5 4745 switch (fb->format->format) {
86adf9d7
ML
4746 case DRM_FORMAT_RGB565:
4747 case DRM_FORMAT_XBGR8888:
4748 case DRM_FORMAT_XRGB8888:
4749 case DRM_FORMAT_ABGR8888:
4750 case DRM_FORMAT_ARGB8888:
4751 case DRM_FORMAT_XRGB2101010:
4752 case DRM_FORMAT_XBGR2101010:
4753 case DRM_FORMAT_YUYV:
4754 case DRM_FORMAT_YVYU:
4755 case DRM_FORMAT_UYVY:
4756 case DRM_FORMAT_VYUY:
4757 break;
4758 default:
72660ce0
VS
4759 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
4760 intel_plane->base.base.id, intel_plane->base.name,
438b74a5 4761 fb->base.id, fb->format->format);
86adf9d7 4762 return -EINVAL;
a1b2278e
CK
4763 }
4764
a1b2278e
CK
4765 return 0;
4766}
4767
e435d6e5
ML
4768static void skylake_scaler_disable(struct intel_crtc *crtc)
4769{
4770 int i;
4771
4772 for (i = 0; i < crtc->num_scalers; i++)
4773 skl_detach_scaler(crtc, i);
4774}
4775
4776static void skylake_pfit_enable(struct intel_crtc *crtc)
bd2e244f
JB
4777{
4778 struct drm_device *dev = crtc->base.dev;
fac5e23e 4779 struct drm_i915_private *dev_priv = to_i915(dev);
bd2e244f 4780 int pipe = crtc->pipe;
a1b2278e
CK
4781 struct intel_crtc_scaler_state *scaler_state =
4782 &crtc->config->scaler_state;
4783
6e3c9717 4784 if (crtc->config->pch_pfit.enabled) {
a1b2278e
CK
4785 int id;
4786
c3f8ad57 4787 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0))
a1b2278e 4788 return;
a1b2278e
CK
4789
4790 id = scaler_state->scaler_id;
4791 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4792 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4793 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4794 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
bd2e244f
JB
4795 }
4796}
4797
b074cec8
JB
4798static void ironlake_pfit_enable(struct intel_crtc *crtc)
4799{
4800 struct drm_device *dev = crtc->base.dev;
fac5e23e 4801 struct drm_i915_private *dev_priv = to_i915(dev);
b074cec8
JB
4802 int pipe = crtc->pipe;
4803
6e3c9717 4804 if (crtc->config->pch_pfit.enabled) {
b074cec8
JB
4805 /* Force use of hard-coded filter coefficients
4806 * as some pre-programmed values are broken,
4807 * e.g. x201.
4808 */
fd6b8f43 4809 if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
b074cec8
JB
4810 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4811 PF_PIPE_SEL_IVB(pipe));
4812 else
4813 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
6e3c9717
ACO
4814 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4815 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
d4270e57
JB
4816 }
4817}
4818
20bc8673 4819void hsw_enable_ips(struct intel_crtc *crtc)
d77e4531 4820{
cea165c3 4821 struct drm_device *dev = crtc->base.dev;
fac5e23e 4822 struct drm_i915_private *dev_priv = to_i915(dev);
d77e4531 4823
6e3c9717 4824 if (!crtc->config->ips_enabled)
d77e4531
PZ
4825 return;
4826
307e4498
ML
4827 /*
4828 * We can only enable IPS after we enable a plane and wait for a vblank
4829 * This function is called from post_plane_update, which is run after
4830 * a vblank wait.
4831 */
cea165c3 4832
d77e4531 4833 assert_plane_enabled(dev_priv, crtc->plane);
8652744b 4834 if (IS_BROADWELL(dev_priv)) {
2a114cc1
BW
4835 mutex_lock(&dev_priv->rps.hw_lock);
4836 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4837 mutex_unlock(&dev_priv->rps.hw_lock);
4838 /* Quoting Art Runyan: "its not safe to expect any particular
4839 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
4840 * mailbox." Moreover, the mailbox may return a bogus state,
4841 * so we need to just enable it and continue on.
2a114cc1
BW
4842 */
4843 } else {
4844 I915_WRITE(IPS_CTL, IPS_ENABLE);
4845 /* The bit only becomes 1 in the next vblank, so this wait here
4846 * is essentially intel_wait_for_vblank. If we don't have this
4847 * and don't wait for vblanks until the end of crtc_enable, then
4848 * the HW state readout code will complain that the expected
4849 * IPS_CTL value is not the one we read. */
2ec9ba3c
CW
4850 if (intel_wait_for_register(dev_priv,
4851 IPS_CTL, IPS_ENABLE, IPS_ENABLE,
4852 50))
2a114cc1
BW
4853 DRM_ERROR("Timed out waiting for IPS enable\n");
4854 }
d77e4531
PZ
4855}
4856
20bc8673 4857void hsw_disable_ips(struct intel_crtc *crtc)
d77e4531
PZ
4858{
4859 struct drm_device *dev = crtc->base.dev;
fac5e23e 4860 struct drm_i915_private *dev_priv = to_i915(dev);
d77e4531 4861
6e3c9717 4862 if (!crtc->config->ips_enabled)
d77e4531
PZ
4863 return;
4864
4865 assert_plane_enabled(dev_priv, crtc->plane);
8652744b 4866 if (IS_BROADWELL(dev_priv)) {
2a114cc1
BW
4867 mutex_lock(&dev_priv->rps.hw_lock);
4868 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4869 mutex_unlock(&dev_priv->rps.hw_lock);
23d0b130 4870 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
b85c1ecf
CW
4871 if (intel_wait_for_register(dev_priv,
4872 IPS_CTL, IPS_ENABLE, 0,
4873 42))
23d0b130 4874 DRM_ERROR("Timed out waiting for IPS disable\n");
e59150dc 4875 } else {
2a114cc1 4876 I915_WRITE(IPS_CTL, 0);
e59150dc
JB
4877 POSTING_READ(IPS_CTL);
4878 }
d77e4531
PZ
4879
4880 /* We need to wait for a vblank before we can disable the plane. */
0f0f74bc 4881 intel_wait_for_vblank(dev_priv, crtc->pipe);
d77e4531
PZ
4882}
4883
7cac945f 4884static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
d3eedb1a 4885{
7cac945f 4886 if (intel_crtc->overlay) {
d3eedb1a 4887 struct drm_device *dev = intel_crtc->base.dev;
d3eedb1a
VS
4888
4889 mutex_lock(&dev->struct_mutex);
d3eedb1a 4890 (void) intel_overlay_switch_off(intel_crtc->overlay);
d3eedb1a
VS
4891 mutex_unlock(&dev->struct_mutex);
4892 }
4893
4894 /* Let userspace switch the overlay on again. In most cases userspace
4895 * has to recompute where to put it anyway.
4896 */
4897}
4898
87d4300a
ML
4899/**
4900 * intel_post_enable_primary - Perform operations after enabling primary plane
4901 * @crtc: the CRTC whose primary plane was just enabled
4902 *
4903 * Performs potentially sleeping operations that must be done after the primary
4904 * plane is enabled, such as updating FBC and IPS. Note that this may be
4905 * called due to an explicit primary plane update, or due to an implicit
4906 * re-enable that is caused when a sprite plane is updated to no longer
4907 * completely hide the primary plane.
4908 */
4909static void
4910intel_post_enable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4911{
4912 struct drm_device *dev = crtc->dev;
fac5e23e 4913 struct drm_i915_private *dev_priv = to_i915(dev);
a5c4d7bc
VS
4914 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4915 int pipe = intel_crtc->pipe;
a5c4d7bc 4916
87d4300a
ML
4917 /*
4918 * FIXME IPS should be fine as long as one plane is
4919 * enabled, but in practice it seems to have problems
4920 * when going from primary only to sprite only and vice
4921 * versa.
4922 */
a5c4d7bc
VS
4923 hsw_enable_ips(intel_crtc);
4924
f99d7069 4925 /*
87d4300a
ML
4926 * Gen2 reports pipe underruns whenever all planes are disabled.
4927 * So don't enable underrun reporting before at least some planes
4928 * are enabled.
4929 * FIXME: Need to fix the logic to work when we turn off all planes
4930 * but leave the pipe running.
f99d7069 4931 */
5db94019 4932 if (IS_GEN2(dev_priv))
87d4300a
ML
4933 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4934
aca7b684
VS
4935 /* Underruns don't always raise interrupts, so check manually. */
4936 intel_check_cpu_fifo_underruns(dev_priv);
4937 intel_check_pch_fifo_underruns(dev_priv);
a5c4d7bc
VS
4938}
4939
2622a081 4940/* FIXME move all this to pre_plane_update() with proper state tracking */
87d4300a
ML
4941static void
4942intel_pre_disable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4943{
4944 struct drm_device *dev = crtc->dev;
fac5e23e 4945 struct drm_i915_private *dev_priv = to_i915(dev);
a5c4d7bc
VS
4946 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4947 int pipe = intel_crtc->pipe;
a5c4d7bc 4948
87d4300a
ML
4949 /*
4950 * Gen2 reports pipe underruns whenever all planes are disabled.
4951 * So diasble underrun reporting before all the planes get disabled.
4952 * FIXME: Need to fix the logic to work when we turn off all planes
4953 * but leave the pipe running.
4954 */
5db94019 4955 if (IS_GEN2(dev_priv))
87d4300a 4956 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
a5c4d7bc 4957
2622a081
VS
4958 /*
4959 * FIXME IPS should be fine as long as one plane is
4960 * enabled, but in practice it seems to have problems
4961 * when going from primary only to sprite only and vice
4962 * versa.
4963 */
4964 hsw_disable_ips(intel_crtc);
4965}
4966
4967/* FIXME get rid of this and use pre_plane_update */
4968static void
4969intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
4970{
4971 struct drm_device *dev = crtc->dev;
fac5e23e 4972 struct drm_i915_private *dev_priv = to_i915(dev);
2622a081
VS
4973 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4974 int pipe = intel_crtc->pipe;
4975
4976 intel_pre_disable_primary(crtc);
4977
87d4300a
ML
4978 /*
4979 * Vblank time updates from the shadow to live plane control register
4980 * are blocked if the memory self-refresh mode is active at that
4981 * moment. So to make sure the plane gets truly disabled, disable
4982 * first the self-refresh mode. The self-refresh enable bit in turn
4983 * will be checked/applied by the HW only at the next frame start
4984 * event which is after the vblank start event, so we need to have a
4985 * wait-for-vblank between disabling the plane and the pipe.
4986 */
11a85d6a
VS
4987 if (HAS_GMCH_DISPLAY(dev_priv) &&
4988 intel_set_memory_cxsr(dev_priv, false))
0f0f74bc 4989 intel_wait_for_vblank(dev_priv, pipe);
87d4300a
ML
4990}
4991
5a21b665
DV
4992static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
4993{
4994 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
4995 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4996 struct intel_crtc_state *pipe_config =
4997 to_intel_crtc_state(crtc->base.state);
5a21b665
DV
4998 struct drm_plane *primary = crtc->base.primary;
4999 struct drm_plane_state *old_pri_state =
5000 drm_atomic_get_existing_plane_state(old_state, primary);
5001
5748b6a1 5002 intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
5a21b665 5003
5a21b665 5004 if (pipe_config->update_wm_post && pipe_config->base.active)
432081bc 5005 intel_update_watermarks(crtc);
5a21b665
DV
5006
5007 if (old_pri_state) {
5008 struct intel_plane_state *primary_state =
5009 to_intel_plane_state(primary->state);
5010 struct intel_plane_state *old_primary_state =
5011 to_intel_plane_state(old_pri_state);
5012
5013 intel_fbc_post_update(crtc);
5014
936e71e3 5015 if (primary_state->base.visible &&
5a21b665 5016 (needs_modeset(&pipe_config->base) ||
936e71e3 5017 !old_primary_state->base.visible))
5a21b665
DV
5018 intel_post_enable_primary(&crtc->base);
5019 }
5020}
5021
aa5e9b47
ML
5022static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state,
5023 struct intel_crtc_state *pipe_config)
ac21b225 5024{
5c74cd73 5025 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
ac21b225 5026 struct drm_device *dev = crtc->base.dev;
fac5e23e 5027 struct drm_i915_private *dev_priv = to_i915(dev);
5c74cd73
ML
5028 struct drm_atomic_state *old_state = old_crtc_state->base.state;
5029 struct drm_plane *primary = crtc->base.primary;
5030 struct drm_plane_state *old_pri_state =
5031 drm_atomic_get_existing_plane_state(old_state, primary);
5032 bool modeset = needs_modeset(&pipe_config->base);
ccf010fb
ML
5033 struct intel_atomic_state *old_intel_state =
5034 to_intel_atomic_state(old_state);
ac21b225 5035
5c74cd73
ML
5036 if (old_pri_state) {
5037 struct intel_plane_state *primary_state =
5038 to_intel_plane_state(primary->state);
5039 struct intel_plane_state *old_primary_state =
5040 to_intel_plane_state(old_pri_state);
5041
faf68d92 5042 intel_fbc_pre_update(crtc, pipe_config, primary_state);
31ae71fc 5043
936e71e3
VS
5044 if (old_primary_state->base.visible &&
5045 (modeset || !primary_state->base.visible))
5c74cd73
ML
5046 intel_pre_disable_primary(&crtc->base);
5047 }
852eb00d 5048
5eeb798b
VS
5049 /*
5050 * Vblank time updates from the shadow to live plane control register
5051 * are blocked if the memory self-refresh mode is active at that
5052 * moment. So to make sure the plane gets truly disabled, disable
5053 * first the self-refresh mode. The self-refresh enable bit in turn
5054 * will be checked/applied by the HW only at the next frame start
5055 * event which is after the vblank start event, so we need to have a
5056 * wait-for-vblank between disabling the plane and the pipe.
5057 */
5058 if (HAS_GMCH_DISPLAY(dev_priv) && old_crtc_state->base.active &&
5059 pipe_config->disable_cxsr && intel_set_memory_cxsr(dev_priv, false))
5060 intel_wait_for_vblank(dev_priv, crtc->pipe);
92826fcd 5061
ed4a6a7c
MR
5062 /*
5063 * IVB workaround: must disable low power watermarks for at least
5064 * one frame before enabling scaling. LP watermarks can be re-enabled
5065 * when scaling is disabled.
5066 *
5067 * WaCxSRDisabledForSpriteScaling:ivb
5068 */
ddd2b792 5069 if (pipe_config->disable_lp_wm && ilk_disable_lp_wm(dev))
0f0f74bc 5070 intel_wait_for_vblank(dev_priv, crtc->pipe);
ed4a6a7c
MR
5071
5072 /*
5073 * If we're doing a modeset, we're done. No need to do any pre-vblank
5074 * watermark programming here.
5075 */
5076 if (needs_modeset(&pipe_config->base))
5077 return;
5078
5079 /*
5080 * For platforms that support atomic watermarks, program the
5081 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
5082 * will be the intermediate values that are safe for both pre- and
5083 * post- vblank; when vblank happens, the 'active' values will be set
5084 * to the final 'target' values and we'll do this again to get the
5085 * optimal watermarks. For gen9+ platforms, the values we program here
5086 * will be the final target values which will get automatically latched
5087 * at vblank time; no further programming will be necessary.
5088 *
5089 * If a platform hasn't been transitioned to atomic watermarks yet,
5090 * we'll continue to update watermarks the old way, if flags tell
5091 * us to.
5092 */
5093 if (dev_priv->display.initial_watermarks != NULL)
ccf010fb
ML
5094 dev_priv->display.initial_watermarks(old_intel_state,
5095 pipe_config);
caed361d 5096 else if (pipe_config->update_wm_pre)
432081bc 5097 intel_update_watermarks(crtc);
ac21b225
ML
5098}
5099
d032ffa0 5100static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
87d4300a
ML
5101{
5102 struct drm_device *dev = crtc->dev;
5103 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
d032ffa0 5104 struct drm_plane *p;
87d4300a
ML
5105 int pipe = intel_crtc->pipe;
5106
7cac945f 5107 intel_crtc_dpms_overlay_disable(intel_crtc);
27321ae8 5108
d032ffa0 5109 drm_for_each_plane_mask(p, dev, plane_mask)
282dbf9b 5110 to_intel_plane(p)->disable_plane(to_intel_plane(p), intel_crtc);
f98551ae 5111
f99d7069
DV
5112 /*
5113 * FIXME: Once we grow proper nuclear flip support out of this we need
5114 * to compute the mask of flip planes precisely. For the time being
5115 * consider this a flip to a NULL plane.
5116 */
5748b6a1 5117 intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
5118}
5119
fb1c98b1 5120static void intel_encoders_pre_pll_enable(struct drm_crtc *crtc,
fd6bbda9 5121 struct intel_crtc_state *crtc_state,
fb1c98b1
ML
5122 struct drm_atomic_state *old_state)
5123{
aa5e9b47 5124 struct drm_connector_state *conn_state;
fb1c98b1
ML
5125 struct drm_connector *conn;
5126 int i;
5127
aa5e9b47 5128 for_each_new_connector_in_state(old_state, conn, conn_state, i) {
fb1c98b1
ML
5129 struct intel_encoder *encoder =
5130 to_intel_encoder(conn_state->best_encoder);
5131
5132 if (conn_state->crtc != crtc)
5133 continue;
5134
5135 if (encoder->pre_pll_enable)
fd6bbda9 5136 encoder->pre_pll_enable(encoder, crtc_state, conn_state);
fb1c98b1
ML
5137 }
5138}
5139
5140static void intel_encoders_pre_enable(struct drm_crtc *crtc,
fd6bbda9 5141 struct intel_crtc_state *crtc_state,
fb1c98b1
ML
5142 struct drm_atomic_state *old_state)
5143{
aa5e9b47 5144 struct drm_connector_state *conn_state;
fb1c98b1
ML
5145 struct drm_connector *conn;
5146 int i;
5147
aa5e9b47 5148 for_each_new_connector_in_state(old_state, conn, conn_state, i) {
fb1c98b1
ML
5149 struct intel_encoder *encoder =
5150 to_intel_encoder(conn_state->best_encoder);
5151
5152 if (conn_state->crtc != crtc)
5153 continue;
5154
5155 if (encoder->pre_enable)
fd6bbda9 5156 encoder->pre_enable(encoder, crtc_state, conn_state);
fb1c98b1
ML
5157 }
5158}
5159
5160static void intel_encoders_enable(struct drm_crtc *crtc,
fd6bbda9 5161 struct intel_crtc_state *crtc_state,
fb1c98b1
ML
5162 struct drm_atomic_state *old_state)
5163{
aa5e9b47 5164 struct drm_connector_state *conn_state;
fb1c98b1
ML
5165 struct drm_connector *conn;
5166 int i;
5167
aa5e9b47 5168 for_each_new_connector_in_state(old_state, conn, conn_state, i) {
fb1c98b1
ML
5169 struct intel_encoder *encoder =
5170 to_intel_encoder(conn_state->best_encoder);
5171
5172 if (conn_state->crtc != crtc)
5173 continue;
5174
fd6bbda9 5175 encoder->enable(encoder, crtc_state, conn_state);
fb1c98b1
ML
5176 intel_opregion_notify_encoder(encoder, true);
5177 }
5178}
5179
5180static void intel_encoders_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
aa5e9b47 5188 for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
fb1c98b1
ML
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 intel_opregion_notify_encoder(encoder, false);
fd6bbda9 5196 encoder->disable(encoder, old_crtc_state, old_conn_state);
fb1c98b1
ML
5197 }
5198}
5199
5200static void intel_encoders_post_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
aa5e9b47 5208 for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
fb1c98b1
ML
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_disable)
fd6bbda9 5216 encoder->post_disable(encoder, old_crtc_state, old_conn_state);
fb1c98b1
ML
5217 }
5218}
5219
5220static void intel_encoders_post_pll_disable(struct drm_crtc *crtc,
fd6bbda9 5221 struct intel_crtc_state *old_crtc_state,
fb1c98b1
ML
5222 struct drm_atomic_state *old_state)
5223{
5224 struct drm_connector_state *old_conn_state;
5225 struct drm_connector *conn;
5226 int i;
5227
aa5e9b47 5228 for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
fb1c98b1
ML
5229 struct intel_encoder *encoder =
5230 to_intel_encoder(old_conn_state->best_encoder);
5231
5232 if (old_conn_state->crtc != crtc)
5233 continue;
5234
5235 if (encoder->post_pll_disable)
fd6bbda9 5236 encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
fb1c98b1
ML
5237 }
5238}
5239
4a806558
ML
5240static void ironlake_crtc_enable(struct intel_crtc_state *pipe_config,
5241 struct drm_atomic_state *old_state)
f67a559d 5242{
4a806558 5243 struct drm_crtc *crtc = pipe_config->base.crtc;
f67a559d 5244 struct drm_device *dev = crtc->dev;
fac5e23e 5245 struct drm_i915_private *dev_priv = to_i915(dev);
f67a559d
JB
5246 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5247 int pipe = intel_crtc->pipe;
ccf010fb
ML
5248 struct intel_atomic_state *old_intel_state =
5249 to_intel_atomic_state(old_state);
f67a559d 5250
53d9f4e9 5251 if (WARN_ON(intel_crtc->active))
f67a559d
JB
5252 return;
5253
b2c0593a
VS
5254 /*
5255 * Sometimes spurious CPU pipe underruns happen during FDI
5256 * training, at least with VGA+HDMI cloning. Suppress them.
5257 *
5258 * On ILK we get an occasional spurious CPU pipe underruns
5259 * between eDP port A enable and vdd enable. Also PCH port
5260 * enable seems to result in the occasional CPU pipe underrun.
5261 *
5262 * Spurious PCH underruns also occur during PCH enabling.
5263 */
5264 if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
5265 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
81b088ca
VS
5266 if (intel_crtc->config->has_pch_encoder)
5267 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
5268
6e3c9717 5269 if (intel_crtc->config->has_pch_encoder)
b14b1055
DV
5270 intel_prepare_shared_dpll(intel_crtc);
5271
37a5650b 5272 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 5273 intel_dp_set_m_n(intel_crtc, M1_N1);
29407aab
DV
5274
5275 intel_set_pipe_timings(intel_crtc);
bc58be60 5276 intel_set_pipe_src_size(intel_crtc);
29407aab 5277
6e3c9717 5278 if (intel_crtc->config->has_pch_encoder) {
29407aab 5279 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 5280 &intel_crtc->config->fdi_m_n, NULL);
29407aab
DV
5281 }
5282
5283 ironlake_set_pipeconf(crtc);
5284
f67a559d 5285 intel_crtc->active = true;
8664281b 5286
fd6bbda9 5287 intel_encoders_pre_enable(crtc, pipe_config, old_state);
f67a559d 5288
6e3c9717 5289 if (intel_crtc->config->has_pch_encoder) {
fff367c7
DV
5290 /* Note: FDI PLL enabling _must_ be done before we enable the
5291 * cpu pipes, hence this is separate from all the other fdi/pch
5292 * enabling. */
88cefb6c 5293 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
DV
5294 } else {
5295 assert_fdi_tx_disabled(dev_priv, pipe);
5296 assert_fdi_rx_disabled(dev_priv, pipe);
5297 }
f67a559d 5298
b074cec8 5299 ironlake_pfit_enable(intel_crtc);
f67a559d 5300
9c54c0dd
JB
5301 /*
5302 * On ILK+ LUT must be loaded before the pipe is running but with
5303 * clocks enabled
5304 */
b95c5321 5305 intel_color_load_luts(&pipe_config->base);
9c54c0dd 5306
1d5bf5d9 5307 if (dev_priv->display.initial_watermarks != NULL)
ccf010fb 5308 dev_priv->display.initial_watermarks(old_intel_state, intel_crtc->config);
e1fdc473 5309 intel_enable_pipe(intel_crtc);
f67a559d 5310
6e3c9717 5311 if (intel_crtc->config->has_pch_encoder)
2ce42273 5312 ironlake_pch_enable(pipe_config);
c98e9dcf 5313
f9b61ff6
DV
5314 assert_vblank_disabled(crtc);
5315 drm_crtc_vblank_on(crtc);
5316
fd6bbda9 5317 intel_encoders_enable(crtc, pipe_config, old_state);
61b77ddd 5318
6e266956 5319 if (HAS_PCH_CPT(dev_priv))
a1520318 5320 cpt_verify_modeset(dev, intel_crtc->pipe);
37ca8d4c
VS
5321
5322 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
5323 if (intel_crtc->config->has_pch_encoder)
0f0f74bc 5324 intel_wait_for_vblank(dev_priv, pipe);
b2c0593a 5325 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
37ca8d4c 5326 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607
JB
5327}
5328
42db64ef
PZ
5329/* IPS only exists on ULT machines and is tied to pipe A. */
5330static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
5331{
50a0bc90 5332 return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
42db64ef
PZ
5333}
5334
4a806558
ML
5335static void haswell_crtc_enable(struct intel_crtc_state *pipe_config,
5336 struct drm_atomic_state *old_state)
4f771f10 5337{
4a806558 5338 struct drm_crtc *crtc = pipe_config->base.crtc;
6315b5d3 5339 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
4f771f10 5340 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
99d736a2 5341 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
4d1de975 5342 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ccf010fb
ML
5343 struct intel_atomic_state *old_intel_state =
5344 to_intel_atomic_state(old_state);
4f771f10 5345
53d9f4e9 5346 if (WARN_ON(intel_crtc->active))
4f771f10
PZ
5347 return;
5348
81b088ca
VS
5349 if (intel_crtc->config->has_pch_encoder)
5350 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5351 false);
5352
fd6bbda9 5353 intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
95a7a2ae 5354
8106ddbd 5355 if (intel_crtc->config->shared_dpll)
df8ad70c
DV
5356 intel_enable_shared_dpll(intel_crtc);
5357
37a5650b 5358 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 5359 intel_dp_set_m_n(intel_crtc, M1_N1);
229fca97 5360
d7edc4e5 5361 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975
JN
5362 intel_set_pipe_timings(intel_crtc);
5363
bc58be60 5364 intel_set_pipe_src_size(intel_crtc);
229fca97 5365
4d1de975
JN
5366 if (cpu_transcoder != TRANSCODER_EDP &&
5367 !transcoder_is_dsi(cpu_transcoder)) {
5368 I915_WRITE(PIPE_MULT(cpu_transcoder),
6e3c9717 5369 intel_crtc->config->pixel_multiplier - 1);
ebb69c95
CT
5370 }
5371
6e3c9717 5372 if (intel_crtc->config->has_pch_encoder) {
229fca97 5373 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 5374 &intel_crtc->config->fdi_m_n, NULL);
229fca97
DV
5375 }
5376
d7edc4e5 5377 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975
JN
5378 haswell_set_pipeconf(crtc);
5379
391bf048 5380 haswell_set_pipemisc(crtc);
229fca97 5381
b95c5321 5382 intel_color_set_csc(&pipe_config->base);
229fca97 5383
4f771f10 5384 intel_crtc->active = true;
8664281b 5385
6b698516
DV
5386 if (intel_crtc->config->has_pch_encoder)
5387 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
5388 else
5389 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5390
fd6bbda9 5391 intel_encoders_pre_enable(crtc, pipe_config, old_state);
4f771f10 5392
d2d65408 5393 if (intel_crtc->config->has_pch_encoder)
dc4a1094 5394 dev_priv->display.fdi_link_train(intel_crtc, pipe_config);
4fe9467d 5395
d7edc4e5 5396 if (!transcoder_is_dsi(cpu_transcoder))
3dc38eea 5397 intel_ddi_enable_pipe_clock(pipe_config);
4f771f10 5398
6315b5d3 5399 if (INTEL_GEN(dev_priv) >= 9)
e435d6e5 5400 skylake_pfit_enable(intel_crtc);
ff6d9f55 5401 else
1c132b44 5402 ironlake_pfit_enable(intel_crtc);
4f771f10
PZ
5403
5404 /*
5405 * On ILK+ LUT must be loaded before the pipe is running but with
5406 * clocks enabled
5407 */
b95c5321 5408 intel_color_load_luts(&pipe_config->base);
4f771f10 5409
3dc38eea 5410 intel_ddi_set_pipe_settings(pipe_config);
d7edc4e5 5411 if (!transcoder_is_dsi(cpu_transcoder))
3dc38eea 5412 intel_ddi_enable_transcoder_func(pipe_config);
4f771f10 5413
1d5bf5d9 5414 if (dev_priv->display.initial_watermarks != NULL)
3125d39f 5415 dev_priv->display.initial_watermarks(old_intel_state, pipe_config);
4d1de975
JN
5416
5417 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
d7edc4e5 5418 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975 5419 intel_enable_pipe(intel_crtc);
42db64ef 5420
6e3c9717 5421 if (intel_crtc->config->has_pch_encoder)
2ce42273 5422 lpt_pch_enable(pipe_config);
4f771f10 5423
0037071d 5424 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
3dc38eea 5425 intel_ddi_set_vc_payload_alloc(pipe_config, true);
0e32b39c 5426
f9b61ff6
DV
5427 assert_vblank_disabled(crtc);
5428 drm_crtc_vblank_on(crtc);
5429
fd6bbda9 5430 intel_encoders_enable(crtc, pipe_config, old_state);
4f771f10 5431
6b698516 5432 if (intel_crtc->config->has_pch_encoder) {
0f0f74bc
VS
5433 intel_wait_for_vblank(dev_priv, pipe);
5434 intel_wait_for_vblank(dev_priv, pipe);
6b698516 5435 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
d2d65408
VS
5436 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5437 true);
6b698516 5438 }
d2d65408 5439
e4916946
PZ
5440 /* If we change the relative order between pipe/planes enabling, we need
5441 * to change the workaround. */
99d736a2 5442 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
772c2a51 5443 if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
0f0f74bc
VS
5444 intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
5445 intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
99d736a2 5446 }
4f771f10
PZ
5447}
5448
bfd16b2a 5449static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
3f8dce3a
DV
5450{
5451 struct drm_device *dev = crtc->base.dev;
fac5e23e 5452 struct drm_i915_private *dev_priv = to_i915(dev);
3f8dce3a
DV
5453 int pipe = crtc->pipe;
5454
5455 /* To avoid upsetting the power well on haswell only disable the pfit if
5456 * it's in use. The hw state code will make sure we get this right. */
bfd16b2a 5457 if (force || crtc->config->pch_pfit.enabled) {
3f8dce3a
DV
5458 I915_WRITE(PF_CTL(pipe), 0);
5459 I915_WRITE(PF_WIN_POS(pipe), 0);
5460 I915_WRITE(PF_WIN_SZ(pipe), 0);
5461 }
5462}
5463
4a806558
ML
5464static void ironlake_crtc_disable(struct intel_crtc_state *old_crtc_state,
5465 struct drm_atomic_state *old_state)
6be4a607 5466{
4a806558 5467 struct drm_crtc *crtc = old_crtc_state->base.crtc;
6be4a607 5468 struct drm_device *dev = crtc->dev;
fac5e23e 5469 struct drm_i915_private *dev_priv = to_i915(dev);
6be4a607
JB
5470 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5471 int pipe = intel_crtc->pipe;
b52eb4dc 5472
b2c0593a
VS
5473 /*
5474 * Sometimes spurious CPU pipe underruns happen when the
5475 * pipe is already disabled, but FDI RX/TX is still enabled.
5476 * Happens at least with VGA+HDMI cloning. Suppress them.
5477 */
5478 if (intel_crtc->config->has_pch_encoder) {
5479 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
37ca8d4c 5480 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
b2c0593a 5481 }
37ca8d4c 5482
fd6bbda9 5483 intel_encoders_disable(crtc, old_crtc_state, old_state);
ea9d758d 5484
f9b61ff6
DV
5485 drm_crtc_vblank_off(crtc);
5486 assert_vblank_disabled(crtc);
5487
575f7ab7 5488 intel_disable_pipe(intel_crtc);
32f9d658 5489
bfd16b2a 5490 ironlake_pfit_disable(intel_crtc, false);
2c07245f 5491
b2c0593a 5492 if (intel_crtc->config->has_pch_encoder)
5a74f70a
VS
5493 ironlake_fdi_disable(crtc);
5494
fd6bbda9 5495 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
2c07245f 5496
6e3c9717 5497 if (intel_crtc->config->has_pch_encoder) {
d925c59a 5498 ironlake_disable_pch_transcoder(dev_priv, pipe);
6be4a607 5499
6e266956 5500 if (HAS_PCH_CPT(dev_priv)) {
f0f59a00
VS
5501 i915_reg_t reg;
5502 u32 temp;
5503
d925c59a
DV
5504 /* disable TRANS_DP_CTL */
5505 reg = TRANS_DP_CTL(pipe);
5506 temp = I915_READ(reg);
5507 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5508 TRANS_DP_PORT_SEL_MASK);
5509 temp |= TRANS_DP_PORT_SEL_NONE;
5510 I915_WRITE(reg, temp);
5511
5512 /* disable DPLL_SEL */
5513 temp = I915_READ(PCH_DPLL_SEL);
11887397 5514 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 5515 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 5516 }
e3421a18 5517
d925c59a
DV
5518 ironlake_fdi_pll_disable(intel_crtc);
5519 }
81b088ca 5520
b2c0593a 5521 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
81b088ca 5522 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607 5523}
1b3c7a47 5524
4a806558
ML
5525static void haswell_crtc_disable(struct intel_crtc_state *old_crtc_state,
5526 struct drm_atomic_state *old_state)
ee7b9f93 5527{
4a806558 5528 struct drm_crtc *crtc = old_crtc_state->base.crtc;
6315b5d3 5529 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
ee7b9f93 5530 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 5531 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee7b9f93 5532
d2d65408
VS
5533 if (intel_crtc->config->has_pch_encoder)
5534 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5535 false);
5536
fd6bbda9 5537 intel_encoders_disable(crtc, old_crtc_state, old_state);
4f771f10 5538
f9b61ff6
DV
5539 drm_crtc_vblank_off(crtc);
5540 assert_vblank_disabled(crtc);
5541
4d1de975 5542 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
d7edc4e5 5543 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975 5544 intel_disable_pipe(intel_crtc);
4f771f10 5545
0037071d 5546 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
3dc38eea 5547 intel_ddi_set_vc_payload_alloc(intel_crtc->config, false);
a4bf214f 5548
d7edc4e5 5549 if (!transcoder_is_dsi(cpu_transcoder))
7d4aefd0 5550 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4f771f10 5551
6315b5d3 5552 if (INTEL_GEN(dev_priv) >= 9)
e435d6e5 5553 skylake_scaler_disable(intel_crtc);
ff6d9f55 5554 else
bfd16b2a 5555 ironlake_pfit_disable(intel_crtc, false);
4f771f10 5556
d7edc4e5 5557 if (!transcoder_is_dsi(cpu_transcoder))
3dc38eea 5558 intel_ddi_disable_pipe_clock(intel_crtc->config);
4f771f10 5559
fd6bbda9 5560 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
81b088ca 5561
b7076546 5562 if (old_crtc_state->has_pch_encoder)
81b088ca
VS
5563 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5564 true);
4f771f10
PZ
5565}
5566
2dd24552
JB
5567static void i9xx_pfit_enable(struct intel_crtc *crtc)
5568{
5569 struct drm_device *dev = crtc->base.dev;
fac5e23e 5570 struct drm_i915_private *dev_priv = to_i915(dev);
6e3c9717 5571 struct intel_crtc_state *pipe_config = crtc->config;
2dd24552 5572
681a8504 5573 if (!pipe_config->gmch_pfit.control)
2dd24552
JB
5574 return;
5575
2dd24552 5576 /*
c0b03411
DV
5577 * The panel fitter should only be adjusted whilst the pipe is disabled,
5578 * according to register description and PRM.
2dd24552 5579 */
c0b03411
DV
5580 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5581 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 5582
b074cec8
JB
5583 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5584 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
DV
5585
5586 /* Border color in case we don't scale up to the full screen. Black by
5587 * default, change to something else for debugging. */
5588 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
5589}
5590
79f255a0 5591enum intel_display_power_domain intel_port_to_power_domain(enum port port)
d05410f9
DA
5592{
5593 switch (port) {
5594 case PORT_A:
6331a704 5595 return POWER_DOMAIN_PORT_DDI_A_LANES;
d05410f9 5596 case PORT_B:
6331a704 5597 return POWER_DOMAIN_PORT_DDI_B_LANES;
d05410f9 5598 case PORT_C:
6331a704 5599 return POWER_DOMAIN_PORT_DDI_C_LANES;
d05410f9 5600 case PORT_D:
6331a704 5601 return POWER_DOMAIN_PORT_DDI_D_LANES;
d8e19f99 5602 case PORT_E:
6331a704 5603 return POWER_DOMAIN_PORT_DDI_E_LANES;
d05410f9 5604 default:
b9fec167 5605 MISSING_CASE(port);
d05410f9
DA
5606 return POWER_DOMAIN_PORT_OTHER;
5607 }
5608}
5609
d8fc70b7
ACO
5610static u64 get_crtc_power_domains(struct drm_crtc *crtc,
5611 struct intel_crtc_state *crtc_state)
77d22dca 5612{
319be8ae 5613 struct drm_device *dev = crtc->dev;
37255d8d 5614 struct drm_i915_private *dev_priv = to_i915(dev);
74bff5f9 5615 struct drm_encoder *encoder;
319be8ae
ID
5616 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5617 enum pipe pipe = intel_crtc->pipe;
d8fc70b7 5618 u64 mask;
74bff5f9 5619 enum transcoder transcoder = crtc_state->cpu_transcoder;
77d22dca 5620
74bff5f9 5621 if (!crtc_state->base.active)
292b990e
ML
5622 return 0;
5623
77d22dca
ID
5624 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5625 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
74bff5f9
ML
5626 if (crtc_state->pch_pfit.enabled ||
5627 crtc_state->pch_pfit.force_thru)
d8fc70b7 5628 mask |= BIT_ULL(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
77d22dca 5629
74bff5f9
ML
5630 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5631 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5632
79f255a0 5633 mask |= BIT_ULL(intel_encoder->power_domain);
74bff5f9 5634 }
319be8ae 5635
37255d8d
ML
5636 if (HAS_DDI(dev_priv) && crtc_state->has_audio)
5637 mask |= BIT(POWER_DOMAIN_AUDIO);
5638
15e7ec29 5639 if (crtc_state->shared_dpll)
d8fc70b7 5640 mask |= BIT_ULL(POWER_DOMAIN_PLLS);
15e7ec29 5641
77d22dca
ID
5642 return mask;
5643}
5644
d2d15016 5645static u64
74bff5f9
ML
5646modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5647 struct intel_crtc_state *crtc_state)
77d22dca 5648{
fac5e23e 5649 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
292b990e
ML
5650 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5651 enum intel_display_power_domain domain;
d8fc70b7 5652 u64 domains, new_domains, old_domains;
77d22dca 5653
292b990e 5654 old_domains = intel_crtc->enabled_power_domains;
74bff5f9
ML
5655 intel_crtc->enabled_power_domains = new_domains =
5656 get_crtc_power_domains(crtc, crtc_state);
77d22dca 5657
5a21b665 5658 domains = new_domains & ~old_domains;
292b990e
ML
5659
5660 for_each_power_domain(domain, domains)
5661 intel_display_power_get(dev_priv, domain);
5662
5a21b665 5663 return old_domains & ~new_domains;
292b990e
ML
5664}
5665
5666static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
d8fc70b7 5667 u64 domains)
292b990e
ML
5668{
5669 enum intel_display_power_domain domain;
5670
5671 for_each_power_domain(domain, domains)
5672 intel_display_power_put(dev_priv, domain);
5673}
77d22dca 5674
7ff89ca2
VS
5675static void valleyview_crtc_enable(struct intel_crtc_state *pipe_config,
5676 struct drm_atomic_state *old_state)
adafdc6f 5677{
ff32c54e
VS
5678 struct intel_atomic_state *old_intel_state =
5679 to_intel_atomic_state(old_state);
7ff89ca2
VS
5680 struct drm_crtc *crtc = pipe_config->base.crtc;
5681 struct drm_device *dev = crtc->dev;
5682 struct drm_i915_private *dev_priv = to_i915(dev);
5683 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5684 int pipe = intel_crtc->pipe;
adafdc6f 5685
7ff89ca2
VS
5686 if (WARN_ON(intel_crtc->active))
5687 return;
adafdc6f 5688
7ff89ca2
VS
5689 if (intel_crtc_has_dp_encoder(intel_crtc->config))
5690 intel_dp_set_m_n(intel_crtc, M1_N1);
b2045352 5691
7ff89ca2
VS
5692 intel_set_pipe_timings(intel_crtc);
5693 intel_set_pipe_src_size(intel_crtc);
b2045352 5694
7ff89ca2
VS
5695 if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
5696 struct drm_i915_private *dev_priv = to_i915(dev);
560a7ae4 5697
7ff89ca2
VS
5698 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
5699 I915_WRITE(CHV_CANVAS(pipe), 0);
560a7ae4
DL
5700 }
5701
7ff89ca2 5702 i9xx_set_pipeconf(intel_crtc);
560a7ae4 5703
7ff89ca2 5704 intel_crtc->active = true;
92891e45 5705
7ff89ca2 5706 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5f199dfa 5707
7ff89ca2 5708 intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
5f199dfa 5709
7ff89ca2
VS
5710 if (IS_CHERRYVIEW(dev_priv)) {
5711 chv_prepare_pll(intel_crtc, intel_crtc->config);
5712 chv_enable_pll(intel_crtc, intel_crtc->config);
5713 } else {
5714 vlv_prepare_pll(intel_crtc, intel_crtc->config);
5715 vlv_enable_pll(intel_crtc, intel_crtc->config);
5f199dfa
VS
5716 }
5717
7ff89ca2 5718 intel_encoders_pre_enable(crtc, pipe_config, old_state);
5f199dfa 5719
7ff89ca2 5720 i9xx_pfit_enable(intel_crtc);
89b3c3c7 5721
7ff89ca2 5722 intel_color_load_luts(&pipe_config->base);
89b3c3c7 5723
ff32c54e
VS
5724 dev_priv->display.initial_watermarks(old_intel_state,
5725 pipe_config);
7ff89ca2
VS
5726 intel_enable_pipe(intel_crtc);
5727
5728 assert_vblank_disabled(crtc);
5729 drm_crtc_vblank_on(crtc);
89b3c3c7 5730
7ff89ca2 5731 intel_encoders_enable(crtc, pipe_config, old_state);
89b3c3c7
ACO
5732}
5733
7ff89ca2 5734static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
2b73001e 5735{
7ff89ca2
VS
5736 struct drm_device *dev = crtc->base.dev;
5737 struct drm_i915_private *dev_priv = to_i915(dev);
83d7c81f 5738
7ff89ca2
VS
5739 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
5740 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
2b73001e
VS
5741}
5742
7ff89ca2
VS
5743static void i9xx_crtc_enable(struct intel_crtc_state *pipe_config,
5744 struct drm_atomic_state *old_state)
2b73001e 5745{
04548cba
VS
5746 struct intel_atomic_state *old_intel_state =
5747 to_intel_atomic_state(old_state);
7ff89ca2
VS
5748 struct drm_crtc *crtc = pipe_config->base.crtc;
5749 struct drm_device *dev = crtc->dev;
5750 struct drm_i915_private *dev_priv = to_i915(dev);
5751 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5752 enum pipe pipe = intel_crtc->pipe;
2b73001e 5753
7ff89ca2
VS
5754 if (WARN_ON(intel_crtc->active))
5755 return;
2b73001e 5756
7ff89ca2 5757 i9xx_set_pll_dividers(intel_crtc);
2b73001e 5758
7ff89ca2
VS
5759 if (intel_crtc_has_dp_encoder(intel_crtc->config))
5760 intel_dp_set_m_n(intel_crtc, M1_N1);
83d7c81f 5761
7ff89ca2
VS
5762 intel_set_pipe_timings(intel_crtc);
5763 intel_set_pipe_src_size(intel_crtc);
2b73001e 5764
7ff89ca2 5765 i9xx_set_pipeconf(intel_crtc);
f8437dd1 5766
7ff89ca2 5767 intel_crtc->active = true;
5f199dfa 5768
7ff89ca2
VS
5769 if (!IS_GEN2(dev_priv))
5770 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5f199dfa 5771
7ff89ca2 5772 intel_encoders_pre_enable(crtc, pipe_config, old_state);
f8437dd1 5773
7ff89ca2 5774 i9xx_enable_pll(intel_crtc);
f8437dd1 5775
7ff89ca2 5776 i9xx_pfit_enable(intel_crtc);
f8437dd1 5777
7ff89ca2 5778 intel_color_load_luts(&pipe_config->base);
f8437dd1 5779
04548cba
VS
5780 if (dev_priv->display.initial_watermarks != NULL)
5781 dev_priv->display.initial_watermarks(old_intel_state,
5782 intel_crtc->config);
5783 else
5784 intel_update_watermarks(intel_crtc);
7ff89ca2 5785 intel_enable_pipe(intel_crtc);
f8437dd1 5786
7ff89ca2
VS
5787 assert_vblank_disabled(crtc);
5788 drm_crtc_vblank_on(crtc);
f8437dd1 5789
7ff89ca2
VS
5790 intel_encoders_enable(crtc, pipe_config, old_state);
5791}
f8437dd1 5792
7ff89ca2
VS
5793static void i9xx_pfit_disable(struct intel_crtc *crtc)
5794{
5795 struct drm_device *dev = crtc->base.dev;
5796 struct drm_i915_private *dev_priv = to_i915(dev);
f8437dd1 5797
7ff89ca2 5798 if (!crtc->config->gmch_pfit.control)
f8437dd1 5799 return;
f8437dd1 5800
7ff89ca2
VS
5801 assert_pipe_disabled(dev_priv, crtc->pipe);
5802
5803 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
5804 I915_READ(PFIT_CONTROL));
5805 I915_WRITE(PFIT_CONTROL, 0);
f8437dd1
VK
5806}
5807
7ff89ca2
VS
5808static void i9xx_crtc_disable(struct intel_crtc_state *old_crtc_state,
5809 struct drm_atomic_state *old_state)
f8437dd1 5810{
7ff89ca2
VS
5811 struct drm_crtc *crtc = old_crtc_state->base.crtc;
5812 struct drm_device *dev = crtc->dev;
5813 struct drm_i915_private *dev_priv = to_i915(dev);
5814 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5815 int pipe = intel_crtc->pipe;
d66a2194 5816
d66a2194 5817 /*
7ff89ca2
VS
5818 * On gen2 planes are double buffered but the pipe isn't, so we must
5819 * wait for planes to fully turn off before disabling the pipe.
d66a2194 5820 */
7ff89ca2
VS
5821 if (IS_GEN2(dev_priv))
5822 intel_wait_for_vblank(dev_priv, pipe);
d66a2194 5823
7ff89ca2 5824 intel_encoders_disable(crtc, old_crtc_state, old_state);
d66a2194 5825
7ff89ca2
VS
5826 drm_crtc_vblank_off(crtc);
5827 assert_vblank_disabled(crtc);
d66a2194 5828
7ff89ca2 5829 intel_disable_pipe(intel_crtc);
d66a2194 5830
7ff89ca2 5831 i9xx_pfit_disable(intel_crtc);
89b3c3c7 5832
7ff89ca2 5833 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
d66a2194 5834
7ff89ca2
VS
5835 if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
5836 if (IS_CHERRYVIEW(dev_priv))
5837 chv_disable_pll(dev_priv, pipe);
5838 else if (IS_VALLEYVIEW(dev_priv))
5839 vlv_disable_pll(dev_priv, pipe);
5840 else
5841 i9xx_disable_pll(intel_crtc);
5842 }
c2e001ef 5843
7ff89ca2 5844 intel_encoders_post_pll_disable(crtc, old_crtc_state, old_state);
89b3c3c7 5845
7ff89ca2
VS
5846 if (!IS_GEN2(dev_priv))
5847 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
ff32c54e
VS
5848
5849 if (!dev_priv->display.initial_watermarks)
5850 intel_update_watermarks(intel_crtc);
2ee0da16
VS
5851
5852 /* clock the pipe down to 640x480@60 to potentially save power */
5853 if (IS_I830(dev_priv))
5854 i830_enable_pipe(dev_priv, pipe);
f8437dd1
VK
5855}
5856
da1d0e26
VS
5857static void intel_crtc_disable_noatomic(struct drm_crtc *crtc,
5858 struct drm_modeset_acquire_ctx *ctx)
f8437dd1 5859{
7ff89ca2
VS
5860 struct intel_encoder *encoder;
5861 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5862 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
5863 enum intel_display_power_domain domain;
d2d15016 5864 u64 domains;
7ff89ca2
VS
5865 struct drm_atomic_state *state;
5866 struct intel_crtc_state *crtc_state;
5867 int ret;
f8437dd1 5868
7ff89ca2
VS
5869 if (!intel_crtc->active)
5870 return;
a8ca4934 5871
7ff89ca2
VS
5872 if (crtc->primary->state->visible) {
5873 WARN_ON(intel_crtc->flip_work);
5d96d8af 5874
7ff89ca2 5875 intel_pre_disable_primary_noatomic(crtc);
709e05c3 5876
7ff89ca2
VS
5877 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
5878 crtc->primary->state->visible = false;
5879 }
5d96d8af 5880
7ff89ca2
VS
5881 state = drm_atomic_state_alloc(crtc->dev);
5882 if (!state) {
5883 DRM_DEBUG_KMS("failed to disable [CRTC:%d:%s], out of memory",
5884 crtc->base.id, crtc->name);
1c3f7700 5885 return;
7ff89ca2 5886 }
9f7eb31a 5887
da1d0e26 5888 state->acquire_ctx = ctx;
ea61791e 5889
7ff89ca2
VS
5890 /* Everything's already locked, -EDEADLK can't happen. */
5891 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
5892 ret = drm_atomic_add_affected_connectors(state, crtc);
9f7eb31a 5893
7ff89ca2 5894 WARN_ON(IS_ERR(crtc_state) || ret);
5d96d8af 5895
7ff89ca2 5896 dev_priv->display.crtc_disable(crtc_state, state);
4a806558 5897
0853695c 5898 drm_atomic_state_put(state);
842e0307 5899
78108b7c
VS
5900 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
5901 crtc->base.id, crtc->name);
842e0307
ML
5902
5903 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
5904 crtc->state->active = false;
37d9078b 5905 intel_crtc->active = false;
842e0307
ML
5906 crtc->enabled = false;
5907 crtc->state->connector_mask = 0;
5908 crtc->state->encoder_mask = 0;
5909
5910 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
5911 encoder->base.crtc = NULL;
5912
58f9c0bc 5913 intel_fbc_disable(intel_crtc);
432081bc 5914 intel_update_watermarks(intel_crtc);
1f7457b1 5915 intel_disable_shared_dpll(intel_crtc);
b17d48e2
ML
5916
5917 domains = intel_crtc->enabled_power_domains;
5918 for_each_power_domain(domain, domains)
5919 intel_display_power_put(dev_priv, domain);
5920 intel_crtc->enabled_power_domains = 0;
565602d7
ML
5921
5922 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
5923 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
b17d48e2
ML
5924}
5925
6b72d486
ML
5926/*
5927 * turn all crtc's off, but do not adjust state
5928 * This has to be paired with a call to intel_modeset_setup_hw_state.
5929 */
70e0bd74 5930int intel_display_suspend(struct drm_device *dev)
ee7b9f93 5931{
e2c8b870 5932 struct drm_i915_private *dev_priv = to_i915(dev);
70e0bd74 5933 struct drm_atomic_state *state;
e2c8b870 5934 int ret;
70e0bd74 5935
e2c8b870
ML
5936 state = drm_atomic_helper_suspend(dev);
5937 ret = PTR_ERR_OR_ZERO(state);
70e0bd74
ML
5938 if (ret)
5939 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
e2c8b870
ML
5940 else
5941 dev_priv->modeset_restore_state = state;
70e0bd74 5942 return ret;
ee7b9f93
JB
5943}
5944
ea5b213a 5945void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 5946{
4ef69c7a 5947 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 5948
ea5b213a
CW
5949 drm_encoder_cleanup(encoder);
5950 kfree(intel_encoder);
7e7d76c3
JB
5951}
5952
0a91ca29
DV
5953/* Cross check the actual hw state with our own modeset state tracking (and it's
5954 * internal consistency). */
749d98b8
ML
5955static void intel_connector_verify_state(struct drm_crtc_state *crtc_state,
5956 struct drm_connector_state *conn_state)
79e53945 5957{
749d98b8 5958 struct intel_connector *connector = to_intel_connector(conn_state->connector);
35dd3c64
ML
5959
5960 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
5961 connector->base.base.id,
5962 connector->base.name);
5963
0a91ca29 5964 if (connector->get_hw_state(connector)) {
e85376cb 5965 struct intel_encoder *encoder = connector->encoder;
0a91ca29 5966
749d98b8 5967 I915_STATE_WARN(!crtc_state,
35dd3c64 5968 "connector enabled without attached crtc\n");
0a91ca29 5969
749d98b8 5970 if (!crtc_state)
35dd3c64
ML
5971 return;
5972
749d98b8 5973 I915_STATE_WARN(!crtc_state->active,
35dd3c64
ML
5974 "connector is active, but attached crtc isn't\n");
5975
e85376cb 5976 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
35dd3c64
ML
5977 return;
5978
e85376cb 5979 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
35dd3c64
ML
5980 "atomic encoder doesn't match attached encoder\n");
5981
e85376cb 5982 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
35dd3c64
ML
5983 "attached encoder crtc differs from connector crtc\n");
5984 } else {
749d98b8 5985 I915_STATE_WARN(crtc_state && crtc_state->active,
4d688a2a 5986 "attached crtc is active, but connector isn't\n");
749d98b8 5987 I915_STATE_WARN(!crtc_state && conn_state->best_encoder,
35dd3c64 5988 "best encoder set without crtc!\n");
0a91ca29 5989 }
79e53945
JB
5990}
5991
08d9bc92
ACO
5992int intel_connector_init(struct intel_connector *connector)
5993{
11c1a9ec 5994 struct intel_digital_connector_state *conn_state;
08d9bc92 5995
11c1a9ec
ML
5996 /*
5997 * Allocate enough memory to hold intel_digital_connector_state,
5998 * This might be a few bytes too many, but for connectors that don't
5999 * need it we'll free the state and allocate a smaller one on the first
6000 * succesful commit anyway.
6001 */
6002 conn_state = kzalloc(sizeof(*conn_state), GFP_KERNEL);
6003 if (!conn_state)
08d9bc92
ACO
6004 return -ENOMEM;
6005
11c1a9ec
ML
6006 __drm_atomic_helper_connector_reset(&connector->base,
6007 &conn_state->base);
6008
08d9bc92
ACO
6009 return 0;
6010}
6011
6012struct intel_connector *intel_connector_alloc(void)
6013{
6014 struct intel_connector *connector;
6015
6016 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6017 if (!connector)
6018 return NULL;
6019
6020 if (intel_connector_init(connector) < 0) {
6021 kfree(connector);
6022 return NULL;
6023 }
6024
6025 return connector;
6026}
6027
f0947c37
DV
6028/* Simple connector->get_hw_state implementation for encoders that support only
6029 * one connector and no cloning and hence the encoder state determines the state
6030 * of the connector. */
6031bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 6032{
24929352 6033 enum pipe pipe = 0;
f0947c37 6034 struct intel_encoder *encoder = connector->encoder;
ea5b213a 6035
f0947c37 6036 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
6037}
6038
6d293983 6039static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
d272ddfa 6040{
6d293983
ACO
6041 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6042 return crtc_state->fdi_lanes;
d272ddfa
VS
6043
6044 return 0;
6045}
6046
6d293983 6047static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5cec258b 6048 struct intel_crtc_state *pipe_config)
1857e1da 6049{
8652744b 6050 struct drm_i915_private *dev_priv = to_i915(dev);
6d293983
ACO
6051 struct drm_atomic_state *state = pipe_config->base.state;
6052 struct intel_crtc *other_crtc;
6053 struct intel_crtc_state *other_crtc_state;
6054
1857e1da
DV
6055 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6056 pipe_name(pipe), pipe_config->fdi_lanes);
6057 if (pipe_config->fdi_lanes > 4) {
6058 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6059 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6060 return -EINVAL;
1857e1da
DV
6061 }
6062
8652744b 6063 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
1857e1da
DV
6064 if (pipe_config->fdi_lanes > 2) {
6065 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6066 pipe_config->fdi_lanes);
6d293983 6067 return -EINVAL;
1857e1da 6068 } else {
6d293983 6069 return 0;
1857e1da
DV
6070 }
6071 }
6072
b7f05d4a 6073 if (INTEL_INFO(dev_priv)->num_pipes == 2)
6d293983 6074 return 0;
1857e1da
DV
6075
6076 /* Ivybridge 3 pipe is really complicated */
6077 switch (pipe) {
6078 case PIPE_A:
6d293983 6079 return 0;
1857e1da 6080 case PIPE_B:
6d293983
ACO
6081 if (pipe_config->fdi_lanes <= 2)
6082 return 0;
6083
b91eb5cc 6084 other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
6d293983
ACO
6085 other_crtc_state =
6086 intel_atomic_get_crtc_state(state, other_crtc);
6087 if (IS_ERR(other_crtc_state))
6088 return PTR_ERR(other_crtc_state);
6089
6090 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
1857e1da
DV
6091 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6092 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6093 return -EINVAL;
1857e1da 6094 }
6d293983 6095 return 0;
1857e1da 6096 case PIPE_C:
251cc67c
VS
6097 if (pipe_config->fdi_lanes > 2) {
6098 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6099 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6100 return -EINVAL;
251cc67c 6101 }
6d293983 6102
b91eb5cc 6103 other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
6d293983
ACO
6104 other_crtc_state =
6105 intel_atomic_get_crtc_state(state, other_crtc);
6106 if (IS_ERR(other_crtc_state))
6107 return PTR_ERR(other_crtc_state);
6108
6109 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
1857e1da 6110 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
6d293983 6111 return -EINVAL;
1857e1da 6112 }
6d293983 6113 return 0;
1857e1da
DV
6114 default:
6115 BUG();
6116 }
6117}
6118
e29c22c0
DV
6119#define RETRY 1
6120static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5cec258b 6121 struct intel_crtc_state *pipe_config)
877d48d5 6122{
1857e1da 6123 struct drm_device *dev = intel_crtc->base.dev;
7c5f93b0 6124 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6d293983
ACO
6125 int lane, link_bw, fdi_dotclock, ret;
6126 bool needs_recompute = false;
877d48d5 6127
e29c22c0 6128retry:
877d48d5
DV
6129 /* FDI is a binary signal running at ~2.7GHz, encoding
6130 * each output octet as 10 bits. The actual frequency
6131 * is stored as a divider into a 100MHz clock, and the
6132 * mode pixel clock is stored in units of 1KHz.
6133 * Hence the bw of each lane in terms of the mode signal
6134 * is:
6135 */
21a727b3 6136 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
877d48d5 6137
241bfc38 6138 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 6139
2bd89a07 6140 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
DV
6141 pipe_config->pipe_bpp);
6142
6143 pipe_config->fdi_lanes = lane;
6144
2bd89a07 6145 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
b31e85ed 6146 link_bw, &pipe_config->fdi_m_n, false);
1857e1da 6147
e3b247da 6148 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
6d293983 6149 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
e29c22c0 6150 pipe_config->pipe_bpp -= 2*3;
7ff89ca2
VS
6151 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6152 pipe_config->pipe_bpp);
6153 needs_recompute = true;
6154 pipe_config->bw_constrained = true;
257a7ffc 6155
7ff89ca2 6156 goto retry;
257a7ffc 6157 }
79e53945 6158
7ff89ca2
VS
6159 if (needs_recompute)
6160 return RETRY;
e70236a8 6161
7ff89ca2 6162 return ret;
e70236a8
JB
6163}
6164
7ff89ca2
VS
6165static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6166 struct intel_crtc_state *pipe_config)
e70236a8 6167{
7ff89ca2
VS
6168 if (pipe_config->pipe_bpp > 24)
6169 return false;
e70236a8 6170
7ff89ca2
VS
6171 /* HSW can handle pixel rate up to cdclk? */
6172 if (IS_HASWELL(dev_priv))
6173 return true;
1b1d2716 6174
65cd2b3f 6175 /*
7ff89ca2
VS
6176 * We compare against max which means we must take
6177 * the increased cdclk requirement into account when
6178 * calculating the new cdclk.
6179 *
6180 * Should measure whether using a lower cdclk w/o IPS
e70236a8 6181 */
7ff89ca2
VS
6182 return pipe_config->pixel_rate <=
6183 dev_priv->max_cdclk_freq * 95 / 100;
e70236a8 6184}
79e53945 6185
7ff89ca2
VS
6186static void hsw_compute_ips_config(struct intel_crtc *crtc,
6187 struct intel_crtc_state *pipe_config)
6188{
6189 struct drm_device *dev = crtc->base.dev;
6190 struct drm_i915_private *dev_priv = to_i915(dev);
34edce2f 6191
7ff89ca2
VS
6192 pipe_config->ips_enabled = i915.enable_ips &&
6193 hsw_crtc_supports_ips(crtc) &&
6194 pipe_config_supports_ips(dev_priv, pipe_config);
34edce2f
VS
6195}
6196
7ff89ca2 6197static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
34edce2f 6198{
7ff89ca2 6199 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
34edce2f 6200
7ff89ca2
VS
6201 /* GDG double wide on either pipe, otherwise pipe A only */
6202 return INTEL_INFO(dev_priv)->gen < 4 &&
6203 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
34edce2f
VS
6204}
6205
ceb99320
VS
6206static uint32_t ilk_pipe_pixel_rate(const struct intel_crtc_state *pipe_config)
6207{
6208 uint32_t pixel_rate;
6209
6210 pixel_rate = pipe_config->base.adjusted_mode.crtc_clock;
6211
6212 /*
6213 * We only use IF-ID interlacing. If we ever use
6214 * PF-ID we'll need to adjust the pixel_rate here.
6215 */
6216
6217 if (pipe_config->pch_pfit.enabled) {
6218 uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
6219 uint32_t pfit_size = pipe_config->pch_pfit.size;
6220
6221 pipe_w = pipe_config->pipe_src_w;
6222 pipe_h = pipe_config->pipe_src_h;
6223
6224 pfit_w = (pfit_size >> 16) & 0xFFFF;
6225 pfit_h = pfit_size & 0xFFFF;
6226 if (pipe_w < pfit_w)
6227 pipe_w = pfit_w;
6228 if (pipe_h < pfit_h)
6229 pipe_h = pfit_h;
6230
6231 if (WARN_ON(!pfit_w || !pfit_h))
6232 return pixel_rate;
6233
6234 pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
6235 pfit_w * pfit_h);
6236 }
6237
6238 return pixel_rate;
6239}
6240
7ff89ca2 6241static void intel_crtc_compute_pixel_rate(struct intel_crtc_state *crtc_state)
34edce2f 6242{
7ff89ca2 6243 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
34edce2f 6244
7ff89ca2
VS
6245 if (HAS_GMCH_DISPLAY(dev_priv))
6246 /* FIXME calculate proper pipe pixel rate for GMCH pfit */
6247 crtc_state->pixel_rate =
6248 crtc_state->base.adjusted_mode.crtc_clock;
6249 else
6250 crtc_state->pixel_rate =
6251 ilk_pipe_pixel_rate(crtc_state);
6252}
34edce2f 6253
7ff89ca2
VS
6254static int intel_crtc_compute_config(struct intel_crtc *crtc,
6255 struct intel_crtc_state *pipe_config)
6256{
6257 struct drm_device *dev = crtc->base.dev;
6258 struct drm_i915_private *dev_priv = to_i915(dev);
6259 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6260 int clock_limit = dev_priv->max_dotclk_freq;
34edce2f 6261
7ff89ca2
VS
6262 if (INTEL_GEN(dev_priv) < 4) {
6263 clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
34edce2f 6264
7ff89ca2
VS
6265 /*
6266 * Enable double wide mode when the dot clock
6267 * is > 90% of the (display) core speed.
6268 */
6269 if (intel_crtc_supports_double_wide(crtc) &&
6270 adjusted_mode->crtc_clock > clock_limit) {
6271 clock_limit = dev_priv->max_dotclk_freq;
6272 pipe_config->double_wide = true;
6273 }
34edce2f
VS
6274 }
6275
7ff89ca2
VS
6276 if (adjusted_mode->crtc_clock > clock_limit) {
6277 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6278 adjusted_mode->crtc_clock, clock_limit,
6279 yesno(pipe_config->double_wide));
6280 return -EINVAL;
6281 }
34edce2f 6282
7ff89ca2
VS
6283 /*
6284 * Pipe horizontal size must be even in:
6285 * - DVO ganged mode
6286 * - LVDS dual channel mode
6287 * - Double wide pipe
6288 */
6289 if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
6290 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6291 pipe_config->pipe_src_w &= ~1;
34edce2f 6292
7ff89ca2
VS
6293 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6294 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
6295 */
6296 if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
6297 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
6298 return -EINVAL;
34edce2f 6299
7ff89ca2 6300 intel_crtc_compute_pixel_rate(pipe_config);
34edce2f 6301
7ff89ca2
VS
6302 if (HAS_IPS(dev_priv))
6303 hsw_compute_ips_config(crtc, pipe_config);
34edce2f 6304
7ff89ca2
VS
6305 if (pipe_config->has_pch_encoder)
6306 return ironlake_fdi_compute_config(crtc, pipe_config);
34edce2f 6307
7ff89ca2 6308 return 0;
34edce2f
VS
6309}
6310
2c07245f 6311static void
a65851af 6312intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 6313{
a65851af
VS
6314 while (*num > DATA_LINK_M_N_MASK ||
6315 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
6316 *num >>= 1;
6317 *den >>= 1;
6318 }
6319}
6320
a65851af 6321static void compute_m_n(unsigned int m, unsigned int n,
b31e85ed
JN
6322 uint32_t *ret_m, uint32_t *ret_n,
6323 bool reduce_m_n)
a65851af 6324{
9a86cda0
JN
6325 /*
6326 * Reduce M/N as much as possible without loss in precision. Several DP
6327 * dongles in particular seem to be fussy about too large *link* M/N
6328 * values. The passed in values are more likely to have the least
6329 * significant bits zero than M after rounding below, so do this first.
6330 */
b31e85ed
JN
6331 if (reduce_m_n) {
6332 while ((m & 1) == 0 && (n & 1) == 0) {
6333 m >>= 1;
6334 n >>= 1;
6335 }
9a86cda0
JN
6336 }
6337
a65851af
VS
6338 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
6339 *ret_m = div_u64((uint64_t) m * *ret_n, n);
6340 intel_reduce_m_n_ratio(ret_m, ret_n);
6341}
6342
e69d0bc1
DV
6343void
6344intel_link_compute_m_n(int bits_per_pixel, int nlanes,
6345 int pixel_clock, int link_clock,
b31e85ed
JN
6346 struct intel_link_m_n *m_n,
6347 bool reduce_m_n)
2c07245f 6348{
e69d0bc1 6349 m_n->tu = 64;
a65851af
VS
6350
6351 compute_m_n(bits_per_pixel * pixel_clock,
6352 link_clock * nlanes * 8,
b31e85ed
JN
6353 &m_n->gmch_m, &m_n->gmch_n,
6354 reduce_m_n);
a65851af
VS
6355
6356 compute_m_n(pixel_clock, link_clock,
b31e85ed
JN
6357 &m_n->link_m, &m_n->link_n,
6358 reduce_m_n);
2c07245f
ZW
6359}
6360
a7615030
CW
6361static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
6362{
d330a953
JN
6363 if (i915.panel_use_ssc >= 0)
6364 return i915.panel_use_ssc != 0;
41aa3448 6365 return dev_priv->vbt.lvds_use_ssc
435793df 6366 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
6367}
6368
7429e9d4 6369static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 6370{
7df00d7a 6371 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 6372}
f47709a9 6373
7429e9d4
DV
6374static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
6375{
6376 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
6377}
6378
f47709a9 6379static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
190f68c5 6380 struct intel_crtc_state *crtc_state,
9e2c8475 6381 struct dpll *reduced_clock)
a7516a05 6382{
9b1e14f4 6383 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
a7516a05
JB
6384 u32 fp, fp2 = 0;
6385
9b1e14f4 6386 if (IS_PINEVIEW(dev_priv)) {
190f68c5 6387 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
a7516a05 6388 if (reduced_clock)
7429e9d4 6389 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 6390 } else {
190f68c5 6391 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
a7516a05 6392 if (reduced_clock)
7429e9d4 6393 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
6394 }
6395
190f68c5 6396 crtc_state->dpll_hw_state.fp0 = fp;
a7516a05 6397
f47709a9 6398 crtc->lowfreq_avail = false;
2d84d2b3 6399 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ab585dea 6400 reduced_clock) {
190f68c5 6401 crtc_state->dpll_hw_state.fp1 = fp2;
f47709a9 6402 crtc->lowfreq_avail = true;
a7516a05 6403 } else {
190f68c5 6404 crtc_state->dpll_hw_state.fp1 = fp;
a7516a05
JB
6405 }
6406}
6407
5e69f97f
CML
6408static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
6409 pipe)
89b667f8
JB
6410{
6411 u32 reg_val;
6412
6413 /*
6414 * PLLB opamp always calibrates to max value of 0x3f, force enable it
6415 * and set it to a reasonable value instead.
6416 */
ab3c759a 6417 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
6418 reg_val &= 0xffffff00;
6419 reg_val |= 0x00000030;
ab3c759a 6420 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 6421
ab3c759a 6422 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
ed58570f
ID
6423 reg_val &= 0x00ffffff;
6424 reg_val |= 0x8c000000;
ab3c759a 6425 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 6426
ab3c759a 6427 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 6428 reg_val &= 0xffffff00;
ab3c759a 6429 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 6430
ab3c759a 6431 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
6432 reg_val &= 0x00ffffff;
6433 reg_val |= 0xb0000000;
ab3c759a 6434 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
6435}
6436
b551842d
DV
6437static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
6438 struct intel_link_m_n *m_n)
6439{
6440 struct drm_device *dev = crtc->base.dev;
fac5e23e 6441 struct drm_i915_private *dev_priv = to_i915(dev);
b551842d
DV
6442 int pipe = crtc->pipe;
6443
e3b95f1e
DV
6444 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6445 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
6446 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
6447 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
6448}
6449
6450static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
6451 struct intel_link_m_n *m_n,
6452 struct intel_link_m_n *m2_n2)
b551842d 6453{
6315b5d3 6454 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
b551842d 6455 int pipe = crtc->pipe;
6e3c9717 6456 enum transcoder transcoder = crtc->config->cpu_transcoder;
b551842d 6457
6315b5d3 6458 if (INTEL_GEN(dev_priv) >= 5) {
b551842d
DV
6459 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
6460 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
6461 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
6462 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
f769cd24
VK
6463 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
6464 * for gen < 8) and if DRRS is supported (to make sure the
6465 * registers are not unnecessarily accessed).
6466 */
920a14b2
TU
6467 if (m2_n2 && (IS_CHERRYVIEW(dev_priv) ||
6468 INTEL_GEN(dev_priv) < 8) && crtc->config->has_drrs) {
f769cd24
VK
6469 I915_WRITE(PIPE_DATA_M2(transcoder),
6470 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
6471 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
6472 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
6473 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
6474 }
b551842d 6475 } else {
e3b95f1e
DV
6476 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6477 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
6478 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
6479 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
6480 }
6481}
6482
fe3cd48d 6483void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
03afc4a2 6484{
fe3cd48d
R
6485 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
6486
6487 if (m_n == M1_N1) {
6488 dp_m_n = &crtc->config->dp_m_n;
6489 dp_m2_n2 = &crtc->config->dp_m2_n2;
6490 } else if (m_n == M2_N2) {
6491
6492 /*
6493 * M2_N2 registers are not supported. Hence m2_n2 divider value
6494 * needs to be programmed into M1_N1.
6495 */
6496 dp_m_n = &crtc->config->dp_m2_n2;
6497 } else {
6498 DRM_ERROR("Unsupported divider value\n");
6499 return;
6500 }
6501
6e3c9717
ACO
6502 if (crtc->config->has_pch_encoder)
6503 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
03afc4a2 6504 else
fe3cd48d 6505 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
03afc4a2
DV
6506}
6507
251ac862
DV
6508static void vlv_compute_dpll(struct intel_crtc *crtc,
6509 struct intel_crtc_state *pipe_config)
bdd4b6a6 6510{
03ed5cbf 6511 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
cd2d34d9 6512 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
03ed5cbf
VS
6513 if (crtc->pipe != PIPE_A)
6514 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
bdd4b6a6 6515
cd2d34d9 6516 /* DPLL not used with DSI, but still need the rest set up */
d7edc4e5 6517 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
cd2d34d9
VS
6518 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
6519 DPLL_EXT_BUFFER_ENABLE_VLV;
6520
03ed5cbf
VS
6521 pipe_config->dpll_hw_state.dpll_md =
6522 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
6523}
bdd4b6a6 6524
03ed5cbf
VS
6525static void chv_compute_dpll(struct intel_crtc *crtc,
6526 struct intel_crtc_state *pipe_config)
6527{
6528 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
cd2d34d9 6529 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
03ed5cbf
VS
6530 if (crtc->pipe != PIPE_A)
6531 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
6532
cd2d34d9 6533 /* DPLL not used with DSI, but still need the rest set up */
d7edc4e5 6534 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
cd2d34d9
VS
6535 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
6536
03ed5cbf
VS
6537 pipe_config->dpll_hw_state.dpll_md =
6538 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
bdd4b6a6
DV
6539}
6540
d288f65f 6541static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 6542 const struct intel_crtc_state *pipe_config)
a0c4da24 6543{
f47709a9 6544 struct drm_device *dev = crtc->base.dev;
fac5e23e 6545 struct drm_i915_private *dev_priv = to_i915(dev);
cd2d34d9 6546 enum pipe pipe = crtc->pipe;
bdd4b6a6 6547 u32 mdiv;
a0c4da24 6548 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 6549 u32 coreclk, reg_val;
a0c4da24 6550
cd2d34d9
VS
6551 /* Enable Refclk */
6552 I915_WRITE(DPLL(pipe),
6553 pipe_config->dpll_hw_state.dpll &
6554 ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
6555
6556 /* No need to actually set up the DPLL with DSI */
6557 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
6558 return;
6559
a580516d 6560 mutex_lock(&dev_priv->sb_lock);
09153000 6561
d288f65f
VS
6562 bestn = pipe_config->dpll.n;
6563 bestm1 = pipe_config->dpll.m1;
6564 bestm2 = pipe_config->dpll.m2;
6565 bestp1 = pipe_config->dpll.p1;
6566 bestp2 = pipe_config->dpll.p2;
a0c4da24 6567
89b667f8
JB
6568 /* See eDP HDMI DPIO driver vbios notes doc */
6569
6570 /* PLL B needs special handling */
bdd4b6a6 6571 if (pipe == PIPE_B)
5e69f97f 6572 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
6573
6574 /* Set up Tx target for periodic Rcomp update */
ab3c759a 6575 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
6576
6577 /* Disable target IRef on PLL */
ab3c759a 6578 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 6579 reg_val &= 0x00ffffff;
ab3c759a 6580 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
6581
6582 /* Disable fast lock */
ab3c759a 6583 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
6584
6585 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
6586 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
6587 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
6588 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 6589 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
6590
6591 /*
6592 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
6593 * but we don't support that).
6594 * Note: don't use the DAC post divider as it seems unstable.
6595 */
6596 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 6597 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 6598
a0c4da24 6599 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 6600 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 6601
89b667f8 6602 /* Set HBR and RBR LPF coefficients */
d288f65f 6603 if (pipe_config->port_clock == 162000 ||
2d84d2b3
VS
6604 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
6605 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
ab3c759a 6606 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 6607 0x009f0003);
89b667f8 6608 else
ab3c759a 6609 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
6610 0x00d0000f);
6611
37a5650b 6612 if (intel_crtc_has_dp_encoder(pipe_config)) {
89b667f8 6613 /* Use SSC source */
bdd4b6a6 6614 if (pipe == PIPE_A)
ab3c759a 6615 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6616 0x0df40000);
6617 else
ab3c759a 6618 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6619 0x0df70000);
6620 } else { /* HDMI or VGA */
6621 /* Use bend source */
bdd4b6a6 6622 if (pipe == PIPE_A)
ab3c759a 6623 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6624 0x0df70000);
6625 else
ab3c759a 6626 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6627 0x0df40000);
6628 }
a0c4da24 6629
ab3c759a 6630 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8 6631 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
2210ce7f 6632 if (intel_crtc_has_dp_encoder(crtc->config))
89b667f8 6633 coreclk |= 0x01000000;
ab3c759a 6634 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 6635
ab3c759a 6636 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
a580516d 6637 mutex_unlock(&dev_priv->sb_lock);
a0c4da24
JB
6638}
6639
d288f65f 6640static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 6641 const struct intel_crtc_state *pipe_config)
9d556c99
CML
6642{
6643 struct drm_device *dev = crtc->base.dev;
fac5e23e 6644 struct drm_i915_private *dev_priv = to_i915(dev);
cd2d34d9 6645 enum pipe pipe = crtc->pipe;
9d556c99 6646 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9cbe40c1 6647 u32 loopfilter, tribuf_calcntr;
9d556c99 6648 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
a945ce7e 6649 u32 dpio_val;
9cbe40c1 6650 int vco;
9d556c99 6651
cd2d34d9
VS
6652 /* Enable Refclk and SSC */
6653 I915_WRITE(DPLL(pipe),
6654 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
6655
6656 /* No need to actually set up the DPLL with DSI */
6657 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
6658 return;
6659
d288f65f
VS
6660 bestn = pipe_config->dpll.n;
6661 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
6662 bestm1 = pipe_config->dpll.m1;
6663 bestm2 = pipe_config->dpll.m2 >> 22;
6664 bestp1 = pipe_config->dpll.p1;
6665 bestp2 = pipe_config->dpll.p2;
9cbe40c1 6666 vco = pipe_config->dpll.vco;
a945ce7e 6667 dpio_val = 0;
9cbe40c1 6668 loopfilter = 0;
9d556c99 6669
a580516d 6670 mutex_lock(&dev_priv->sb_lock);
9d556c99 6671
9d556c99
CML
6672 /* p1 and p2 divider */
6673 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
6674 5 << DPIO_CHV_S1_DIV_SHIFT |
6675 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
6676 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
6677 1 << DPIO_CHV_K_DIV_SHIFT);
6678
6679 /* Feedback post-divider - m2 */
6680 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
6681
6682 /* Feedback refclk divider - n and m1 */
6683 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
6684 DPIO_CHV_M1_DIV_BY_2 |
6685 1 << DPIO_CHV_N_DIV_SHIFT);
6686
6687 /* M2 fraction division */
25a25dfc 6688 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
9d556c99
CML
6689
6690 /* M2 fraction division enable */
a945ce7e
VP
6691 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
6692 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
6693 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
6694 if (bestm2_frac)
6695 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
6696 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
9d556c99 6697
de3a0fde
VP
6698 /* Program digital lock detect threshold */
6699 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
6700 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
6701 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
6702 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
6703 if (!bestm2_frac)
6704 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
6705 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
6706
9d556c99 6707 /* Loop filter */
9cbe40c1
VP
6708 if (vco == 5400000) {
6709 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
6710 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
6711 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
6712 tribuf_calcntr = 0x9;
6713 } else if (vco <= 6200000) {
6714 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
6715 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
6716 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6717 tribuf_calcntr = 0x9;
6718 } else if (vco <= 6480000) {
6719 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
6720 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
6721 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6722 tribuf_calcntr = 0x8;
6723 } else {
6724 /* Not supported. Apply the same limits as in the max case */
6725 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
6726 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
6727 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6728 tribuf_calcntr = 0;
6729 }
9d556c99
CML
6730 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
6731
968040b2 6732 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
9cbe40c1
VP
6733 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
6734 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
6735 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
6736
9d556c99
CML
6737 /* AFC Recal */
6738 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
6739 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
6740 DPIO_AFC_RECAL);
6741
a580516d 6742 mutex_unlock(&dev_priv->sb_lock);
9d556c99
CML
6743}
6744
d288f65f
VS
6745/**
6746 * vlv_force_pll_on - forcibly enable just the PLL
6747 * @dev_priv: i915 private structure
6748 * @pipe: pipe PLL to enable
6749 * @dpll: PLL configuration
6750 *
6751 * Enable the PLL for @pipe using the supplied @dpll config. To be used
6752 * in cases where we need the PLL enabled even when @pipe is not going to
6753 * be enabled.
6754 */
30ad9814 6755int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
3f36b937 6756 const struct dpll *dpll)
d288f65f 6757{
b91eb5cc 6758 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
3f36b937
TU
6759 struct intel_crtc_state *pipe_config;
6760
6761 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
6762 if (!pipe_config)
6763 return -ENOMEM;
6764
6765 pipe_config->base.crtc = &crtc->base;
6766 pipe_config->pixel_multiplier = 1;
6767 pipe_config->dpll = *dpll;
d288f65f 6768
30ad9814 6769 if (IS_CHERRYVIEW(dev_priv)) {
3f36b937
TU
6770 chv_compute_dpll(crtc, pipe_config);
6771 chv_prepare_pll(crtc, pipe_config);
6772 chv_enable_pll(crtc, pipe_config);
d288f65f 6773 } else {
3f36b937
TU
6774 vlv_compute_dpll(crtc, pipe_config);
6775 vlv_prepare_pll(crtc, pipe_config);
6776 vlv_enable_pll(crtc, pipe_config);
d288f65f 6777 }
3f36b937
TU
6778
6779 kfree(pipe_config);
6780
6781 return 0;
d288f65f
VS
6782}
6783
6784/**
6785 * vlv_force_pll_off - forcibly disable just the PLL
6786 * @dev_priv: i915 private structure
6787 * @pipe: pipe PLL to disable
6788 *
6789 * Disable the PLL for @pipe. To be used in cases where we need
6790 * the PLL enabled even when @pipe is not going to be enabled.
6791 */
30ad9814 6792void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
d288f65f 6793{
30ad9814
VS
6794 if (IS_CHERRYVIEW(dev_priv))
6795 chv_disable_pll(dev_priv, pipe);
d288f65f 6796 else
30ad9814 6797 vlv_disable_pll(dev_priv, pipe);
d288f65f
VS
6798}
6799
251ac862
DV
6800static void i9xx_compute_dpll(struct intel_crtc *crtc,
6801 struct intel_crtc_state *crtc_state,
9e2c8475 6802 struct dpll *reduced_clock)
eb1cbe48 6803{
9b1e14f4 6804 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
eb1cbe48 6805 u32 dpll;
190f68c5 6806 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 6807
190f68c5 6808 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 6809
eb1cbe48
DV
6810 dpll = DPLL_VGA_MODE_DIS;
6811
2d84d2b3 6812 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
6813 dpll |= DPLLB_MODE_LVDS;
6814 else
6815 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 6816
73f67aa8
JN
6817 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
6818 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
190f68c5 6819 dpll |= (crtc_state->pixel_multiplier - 1)
198a037f 6820 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 6821 }
198a037f 6822
3d6e9ee0
VS
6823 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
6824 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
4a33e48d 6825 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 6826
37a5650b 6827 if (intel_crtc_has_dp_encoder(crtc_state))
4a33e48d 6828 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
6829
6830 /* compute bitmask from p1 value */
9b1e14f4 6831 if (IS_PINEVIEW(dev_priv))
eb1cbe48
DV
6832 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
6833 else {
6834 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
9beb5fea 6835 if (IS_G4X(dev_priv) && reduced_clock)
eb1cbe48
DV
6836 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
6837 }
6838 switch (clock->p2) {
6839 case 5:
6840 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
6841 break;
6842 case 7:
6843 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
6844 break;
6845 case 10:
6846 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
6847 break;
6848 case 14:
6849 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
6850 break;
6851 }
9b1e14f4 6852 if (INTEL_GEN(dev_priv) >= 4)
eb1cbe48
DV
6853 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
6854
190f68c5 6855 if (crtc_state->sdvo_tv_clock)
eb1cbe48 6856 dpll |= PLL_REF_INPUT_TVCLKINBC;
2d84d2b3 6857 else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 6858 intel_panel_use_ssc(dev_priv))
eb1cbe48
DV
6859 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
6860 else
6861 dpll |= PLL_REF_INPUT_DREFCLK;
6862
6863 dpll |= DPLL_VCO_ENABLE;
190f68c5 6864 crtc_state->dpll_hw_state.dpll = dpll;
8bcc2795 6865
9b1e14f4 6866 if (INTEL_GEN(dev_priv) >= 4) {
190f68c5 6867 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
ef1b460d 6868 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
190f68c5 6869 crtc_state->dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
DV
6870 }
6871}
6872
251ac862
DV
6873static void i8xx_compute_dpll(struct intel_crtc *crtc,
6874 struct intel_crtc_state *crtc_state,
9e2c8475 6875 struct dpll *reduced_clock)
eb1cbe48 6876{
f47709a9 6877 struct drm_device *dev = crtc->base.dev;
fac5e23e 6878 struct drm_i915_private *dev_priv = to_i915(dev);
eb1cbe48 6879 u32 dpll;
190f68c5 6880 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 6881
190f68c5 6882 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 6883
eb1cbe48
DV
6884 dpll = DPLL_VGA_MODE_DIS;
6885
2d84d2b3 6886 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
6887 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6888 } else {
6889 if (clock->p1 == 2)
6890 dpll |= PLL_P1_DIVIDE_BY_TWO;
6891 else
6892 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6893 if (clock->p2 == 4)
6894 dpll |= PLL_P2_DIVIDE_BY_4;
6895 }
6896
50a0bc90
TU
6897 if (!IS_I830(dev_priv) &&
6898 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
4a33e48d
DV
6899 dpll |= DPLL_DVO_2X_MODE;
6900
2d84d2b3 6901 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 6902 intel_panel_use_ssc(dev_priv))
eb1cbe48
DV
6903 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
6904 else
6905 dpll |= PLL_REF_INPUT_DREFCLK;
6906
6907 dpll |= DPLL_VCO_ENABLE;
190f68c5 6908 crtc_state->dpll_hw_state.dpll = dpll;
eb1cbe48
DV
6909}
6910
8a654f3b 6911static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c 6912{
6315b5d3 6913 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
b0e77b9c 6914 enum pipe pipe = intel_crtc->pipe;
6e3c9717 6915 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
7c5f93b0 6916 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
1caea6e9
VS
6917 uint32_t crtc_vtotal, crtc_vblank_end;
6918 int vsyncshift = 0;
4d8a62ea
DV
6919
6920 /* We need to be careful not to changed the adjusted mode, for otherwise
6921 * the hw state checker will get angry at the mismatch. */
6922 crtc_vtotal = adjusted_mode->crtc_vtotal;
6923 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 6924
609aeaca 6925 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 6926 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
6927 crtc_vtotal -= 1;
6928 crtc_vblank_end -= 1;
609aeaca 6929
2d84d2b3 6930 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
609aeaca
VS
6931 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
6932 else
6933 vsyncshift = adjusted_mode->crtc_hsync_start -
6934 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
6935 if (vsyncshift < 0)
6936 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
6937 }
6938
6315b5d3 6939 if (INTEL_GEN(dev_priv) > 3)
fe2b8f9d 6940 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 6941
fe2b8f9d 6942 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
6943 (adjusted_mode->crtc_hdisplay - 1) |
6944 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 6945 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
6946 (adjusted_mode->crtc_hblank_start - 1) |
6947 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 6948 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
6949 (adjusted_mode->crtc_hsync_start - 1) |
6950 ((adjusted_mode->crtc_hsync_end - 1) << 16));
6951
fe2b8f9d 6952 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 6953 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 6954 ((crtc_vtotal - 1) << 16));
fe2b8f9d 6955 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 6956 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 6957 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 6958 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
6959 (adjusted_mode->crtc_vsync_start - 1) |
6960 ((adjusted_mode->crtc_vsync_end - 1) << 16));
6961
b5e508d4
PZ
6962 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
6963 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
6964 * documented on the DDI_FUNC_CTL register description, EDP Input Select
6965 * bits. */
772c2a51 6966 if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
b5e508d4
PZ
6967 (pipe == PIPE_B || pipe == PIPE_C))
6968 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
6969
bc58be60
JN
6970}
6971
6972static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
6973{
6974 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 6975 struct drm_i915_private *dev_priv = to_i915(dev);
bc58be60
JN
6976 enum pipe pipe = intel_crtc->pipe;
6977
b0e77b9c
PZ
6978 /* pipesrc controls the size that is scaled from, which should
6979 * always be the user's requested size.
6980 */
6981 I915_WRITE(PIPESRC(pipe),
6e3c9717
ACO
6982 ((intel_crtc->config->pipe_src_w - 1) << 16) |
6983 (intel_crtc->config->pipe_src_h - 1));
b0e77b9c
PZ
6984}
6985
1bd1bd80 6986static void intel_get_pipe_timings(struct intel_crtc *crtc,
5cec258b 6987 struct intel_crtc_state *pipe_config)
1bd1bd80
DV
6988{
6989 struct drm_device *dev = crtc->base.dev;
fac5e23e 6990 struct drm_i915_private *dev_priv = to_i915(dev);
1bd1bd80
DV
6991 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
6992 uint32_t tmp;
6993
6994 tmp = I915_READ(HTOTAL(cpu_transcoder));
2d112de7
ACO
6995 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
6996 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 6997 tmp = I915_READ(HBLANK(cpu_transcoder));
2d112de7
ACO
6998 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
6999 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7000 tmp = I915_READ(HSYNC(cpu_transcoder));
2d112de7
ACO
7001 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7002 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7003
7004 tmp = I915_READ(VTOTAL(cpu_transcoder));
2d112de7
ACO
7005 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7006 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7007 tmp = I915_READ(VBLANK(cpu_transcoder));
2d112de7
ACO
7008 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7009 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7010 tmp = I915_READ(VSYNC(cpu_transcoder));
2d112de7
ACO
7011 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7012 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7013
7014 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
2d112de7
ACO
7015 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7016 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7017 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
1bd1bd80 7018 }
bc58be60
JN
7019}
7020
7021static void intel_get_pipe_src_size(struct intel_crtc *crtc,
7022 struct intel_crtc_state *pipe_config)
7023{
7024 struct drm_device *dev = crtc->base.dev;
fac5e23e 7025 struct drm_i915_private *dev_priv = to_i915(dev);
bc58be60 7026 u32 tmp;
1bd1bd80
DV
7027
7028 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
7029 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7030 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7031
2d112de7
ACO
7032 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7033 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
DV
7034}
7035
f6a83288 7036void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5cec258b 7037 struct intel_crtc_state *pipe_config)
babea61d 7038{
2d112de7
ACO
7039 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7040 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7041 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7042 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
babea61d 7043
2d112de7
ACO
7044 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7045 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7046 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7047 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
babea61d 7048
2d112de7 7049 mode->flags = pipe_config->base.adjusted_mode.flags;
cd13f5ab 7050 mode->type = DRM_MODE_TYPE_DRIVER;
babea61d 7051
2d112de7 7052 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
cd13f5ab
ML
7053
7054 mode->hsync = drm_mode_hsync(mode);
7055 mode->vrefresh = drm_mode_vrefresh(mode);
7056 drm_mode_set_name(mode);
babea61d
JB
7057}
7058
84b046f3
DV
7059static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7060{
6315b5d3 7061 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
84b046f3
DV
7062 uint32_t pipeconf;
7063
9f11a9e4 7064 pipeconf = 0;
84b046f3 7065
e56134bc
VS
7066 /* we keep both pipes enabled on 830 */
7067 if (IS_I830(dev_priv))
b6b5d049 7068 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
67c72a12 7069
6e3c9717 7070 if (intel_crtc->config->double_wide)
cf532bb2 7071 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 7072
ff9ce46e 7073 /* only g4x and later have fancy bpc/dither controls */
9beb5fea
TU
7074 if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
7075 IS_CHERRYVIEW(dev_priv)) {
ff9ce46e 7076 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6e3c9717 7077 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
ff9ce46e 7078 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 7079 PIPECONF_DITHER_TYPE_SP;
84b046f3 7080
6e3c9717 7081 switch (intel_crtc->config->pipe_bpp) {
ff9ce46e
DV
7082 case 18:
7083 pipeconf |= PIPECONF_6BPC;
7084 break;
7085 case 24:
7086 pipeconf |= PIPECONF_8BPC;
7087 break;
7088 case 30:
7089 pipeconf |= PIPECONF_10BPC;
7090 break;
7091 default:
7092 /* Case prevented by intel_choose_pipe_bpp_dither. */
7093 BUG();
84b046f3
DV
7094 }
7095 }
7096
56b857a5 7097 if (HAS_PIPE_CXSR(dev_priv)) {
84b046f3
DV
7098 if (intel_crtc->lowfreq_avail) {
7099 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7100 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7101 } else {
7102 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
84b046f3
DV
7103 }
7104 }
7105
6e3c9717 7106 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
6315b5d3 7107 if (INTEL_GEN(dev_priv) < 4 ||
2d84d2b3 7108 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
efc2cfff
VS
7109 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7110 else
7111 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7112 } else
84b046f3
DV
7113 pipeconf |= PIPECONF_PROGRESSIVE;
7114
920a14b2 7115 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
666a4537 7116 intel_crtc->config->limited_color_range)
9f11a9e4 7117 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 7118
84b046f3
DV
7119 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7120 POSTING_READ(PIPECONF(intel_crtc->pipe));
7121}
7122
81c97f52
ACO
7123static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
7124 struct intel_crtc_state *crtc_state)
7125{
7126 struct drm_device *dev = crtc->base.dev;
fac5e23e 7127 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 7128 const struct intel_limit *limit;
81c97f52
ACO
7129 int refclk = 48000;
7130
7131 memset(&crtc_state->dpll_hw_state, 0,
7132 sizeof(crtc_state->dpll_hw_state));
7133
2d84d2b3 7134 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
81c97f52
ACO
7135 if (intel_panel_use_ssc(dev_priv)) {
7136 refclk = dev_priv->vbt.lvds_ssc_freq;
7137 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7138 }
7139
7140 limit = &intel_limits_i8xx_lvds;
2d84d2b3 7141 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
81c97f52
ACO
7142 limit = &intel_limits_i8xx_dvo;
7143 } else {
7144 limit = &intel_limits_i8xx_dac;
7145 }
7146
7147 if (!crtc_state->clock_set &&
7148 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7149 refclk, NULL, &crtc_state->dpll)) {
7150 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7151 return -EINVAL;
7152 }
7153
7154 i8xx_compute_dpll(crtc, crtc_state, NULL);
7155
7156 return 0;
7157}
7158
19ec6693
ACO
7159static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
7160 struct intel_crtc_state *crtc_state)
7161{
7162 struct drm_device *dev = crtc->base.dev;
fac5e23e 7163 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 7164 const struct intel_limit *limit;
19ec6693
ACO
7165 int refclk = 96000;
7166
7167 memset(&crtc_state->dpll_hw_state, 0,
7168 sizeof(crtc_state->dpll_hw_state));
7169
2d84d2b3 7170 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
19ec6693
ACO
7171 if (intel_panel_use_ssc(dev_priv)) {
7172 refclk = dev_priv->vbt.lvds_ssc_freq;
7173 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7174 }
7175
7176 if (intel_is_dual_link_lvds(dev))
7177 limit = &intel_limits_g4x_dual_channel_lvds;
7178 else
7179 limit = &intel_limits_g4x_single_channel_lvds;
2d84d2b3
VS
7180 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
7181 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
19ec6693 7182 limit = &intel_limits_g4x_hdmi;
2d84d2b3 7183 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
19ec6693
ACO
7184 limit = &intel_limits_g4x_sdvo;
7185 } else {
7186 /* The option is for other outputs */
7187 limit = &intel_limits_i9xx_sdvo;
7188 }
7189
7190 if (!crtc_state->clock_set &&
7191 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7192 refclk, NULL, &crtc_state->dpll)) {
7193 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7194 return -EINVAL;
7195 }
7196
7197 i9xx_compute_dpll(crtc, crtc_state, NULL);
7198
7199 return 0;
7200}
7201
70e8aa21
ACO
7202static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
7203 struct intel_crtc_state *crtc_state)
7204{
7205 struct drm_device *dev = crtc->base.dev;
fac5e23e 7206 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 7207 const struct intel_limit *limit;
70e8aa21
ACO
7208 int refclk = 96000;
7209
7210 memset(&crtc_state->dpll_hw_state, 0,
7211 sizeof(crtc_state->dpll_hw_state));
7212
2d84d2b3 7213 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
70e8aa21
ACO
7214 if (intel_panel_use_ssc(dev_priv)) {
7215 refclk = dev_priv->vbt.lvds_ssc_freq;
7216 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7217 }
7218
7219 limit = &intel_limits_pineview_lvds;
7220 } else {
7221 limit = &intel_limits_pineview_sdvo;
7222 }
7223
7224 if (!crtc_state->clock_set &&
7225 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7226 refclk, NULL, &crtc_state->dpll)) {
7227 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7228 return -EINVAL;
7229 }
7230
7231 i9xx_compute_dpll(crtc, crtc_state, NULL);
7232
7233 return 0;
7234}
7235
190f68c5
ACO
7236static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7237 struct intel_crtc_state *crtc_state)
79e53945 7238{
c7653199 7239 struct drm_device *dev = crtc->base.dev;
fac5e23e 7240 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 7241 const struct intel_limit *limit;
81c97f52 7242 int refclk = 96000;
79e53945 7243
dd3cd74a
ACO
7244 memset(&crtc_state->dpll_hw_state, 0,
7245 sizeof(crtc_state->dpll_hw_state));
7246
2d84d2b3 7247 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
70e8aa21
ACO
7248 if (intel_panel_use_ssc(dev_priv)) {
7249 refclk = dev_priv->vbt.lvds_ssc_freq;
7250 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7251 }
43565a06 7252
70e8aa21
ACO
7253 limit = &intel_limits_i9xx_lvds;
7254 } else {
7255 limit = &intel_limits_i9xx_sdvo;
81c97f52 7256 }
79e53945 7257
70e8aa21
ACO
7258 if (!crtc_state->clock_set &&
7259 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7260 refclk, NULL, &crtc_state->dpll)) {
7261 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7262 return -EINVAL;
f47709a9 7263 }
7026d4ac 7264
81c97f52 7265 i9xx_compute_dpll(crtc, crtc_state, NULL);
79e53945 7266
c8f7a0db 7267 return 0;
f564048e
EA
7268}
7269
65b3d6a9
ACO
7270static int chv_crtc_compute_clock(struct intel_crtc *crtc,
7271 struct intel_crtc_state *crtc_state)
7272{
7273 int refclk = 100000;
1b6f4958 7274 const struct intel_limit *limit = &intel_limits_chv;
65b3d6a9
ACO
7275
7276 memset(&crtc_state->dpll_hw_state, 0,
7277 sizeof(crtc_state->dpll_hw_state));
7278
65b3d6a9
ACO
7279 if (!crtc_state->clock_set &&
7280 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7281 refclk, NULL, &crtc_state->dpll)) {
7282 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7283 return -EINVAL;
7284 }
7285
7286 chv_compute_dpll(crtc, crtc_state);
7287
7288 return 0;
7289}
7290
7291static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
7292 struct intel_crtc_state *crtc_state)
7293{
7294 int refclk = 100000;
1b6f4958 7295 const struct intel_limit *limit = &intel_limits_vlv;
65b3d6a9
ACO
7296
7297 memset(&crtc_state->dpll_hw_state, 0,
7298 sizeof(crtc_state->dpll_hw_state));
7299
65b3d6a9
ACO
7300 if (!crtc_state->clock_set &&
7301 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7302 refclk, NULL, &crtc_state->dpll)) {
7303 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7304 return -EINVAL;
7305 }
7306
7307 vlv_compute_dpll(crtc, crtc_state);
7308
7309 return 0;
7310}
7311
2fa2fe9a 7312static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5cec258b 7313 struct intel_crtc_state *pipe_config)
2fa2fe9a 7314{
6315b5d3 7315 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2fa2fe9a
DV
7316 uint32_t tmp;
7317
50a0bc90
TU
7318 if (INTEL_GEN(dev_priv) <= 3 &&
7319 (IS_I830(dev_priv) || !IS_MOBILE(dev_priv)))
dc9e7dec
VS
7320 return;
7321
2fa2fe9a 7322 tmp = I915_READ(PFIT_CONTROL);
06922821
DV
7323 if (!(tmp & PFIT_ENABLE))
7324 return;
2fa2fe9a 7325
06922821 7326 /* Check whether the pfit is attached to our pipe. */
6315b5d3 7327 if (INTEL_GEN(dev_priv) < 4) {
2fa2fe9a
DV
7328 if (crtc->pipe != PIPE_B)
7329 return;
2fa2fe9a
DV
7330 } else {
7331 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7332 return;
7333 }
7334
06922821 7335 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a 7336 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
2fa2fe9a
DV
7337}
7338
acbec814 7339static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 7340 struct intel_crtc_state *pipe_config)
acbec814
JB
7341{
7342 struct drm_device *dev = crtc->base.dev;
fac5e23e 7343 struct drm_i915_private *dev_priv = to_i915(dev);
acbec814 7344 int pipe = pipe_config->cpu_transcoder;
9e2c8475 7345 struct dpll clock;
acbec814 7346 u32 mdiv;
662c6ecb 7347 int refclk = 100000;
acbec814 7348
b521973b
VS
7349 /* In case of DSI, DPLL will not be used */
7350 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
f573de5a
SK
7351 return;
7352
a580516d 7353 mutex_lock(&dev_priv->sb_lock);
ab3c759a 7354 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
a580516d 7355 mutex_unlock(&dev_priv->sb_lock);
acbec814
JB
7356
7357 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7358 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7359 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7360 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7361 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7362
dccbea3b 7363 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
acbec814
JB
7364}
7365
5724dbd1
DL
7366static void
7367i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7368 struct intel_initial_plane_config *plane_config)
1ad292b5
JB
7369{
7370 struct drm_device *dev = crtc->base.dev;
fac5e23e 7371 struct drm_i915_private *dev_priv = to_i915(dev);
1ad292b5
JB
7372 u32 val, base, offset;
7373 int pipe = crtc->pipe, plane = crtc->plane;
7374 int fourcc, pixel_format;
6761dd31 7375 unsigned int aligned_height;
b113d5ee 7376 struct drm_framebuffer *fb;
1b842c89 7377 struct intel_framebuffer *intel_fb;
1ad292b5 7378
42a7b088
DL
7379 val = I915_READ(DSPCNTR(plane));
7380 if (!(val & DISPLAY_PLANE_ENABLE))
7381 return;
7382
d9806c9f 7383 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 7384 if (!intel_fb) {
1ad292b5
JB
7385 DRM_DEBUG_KMS("failed to alloc fb\n");
7386 return;
7387 }
7388
1b842c89
DL
7389 fb = &intel_fb->base;
7390
d2e9f5fc
VS
7391 fb->dev = dev;
7392
6315b5d3 7393 if (INTEL_GEN(dev_priv) >= 4) {
18c5247e 7394 if (val & DISPPLANE_TILED) {
49af449b 7395 plane_config->tiling = I915_TILING_X;
bae781b2 7396 fb->modifier = I915_FORMAT_MOD_X_TILED;
18c5247e
DV
7397 }
7398 }
1ad292b5
JB
7399
7400 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 7401 fourcc = i9xx_format_to_fourcc(pixel_format);
2f3f4763 7402 fb->format = drm_format_info(fourcc);
1ad292b5 7403
6315b5d3 7404 if (INTEL_GEN(dev_priv) >= 4) {
49af449b 7405 if (plane_config->tiling)
1ad292b5
JB
7406 offset = I915_READ(DSPTILEOFF(plane));
7407 else
7408 offset = I915_READ(DSPLINOFF(plane));
7409 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7410 } else {
7411 base = I915_READ(DSPADDR(plane));
7412 }
7413 plane_config->base = base;
7414
7415 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
7416 fb->width = ((val >> 16) & 0xfff) + 1;
7417 fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
7418
7419 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 7420 fb->pitches[0] = val & 0xffffffc0;
1ad292b5 7421
d88c4afd 7422 aligned_height = intel_fb_align_height(fb, 0, fb->height);
1ad292b5 7423
f37b5c2b 7424 plane_config->size = fb->pitches[0] * aligned_height;
1ad292b5 7425
2844a921
DL
7426 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7427 pipe_name(pipe), plane, fb->width, fb->height,
272725c7 7428 fb->format->cpp[0] * 8, base, fb->pitches[0],
2844a921 7429 plane_config->size);
1ad292b5 7430
2d14030b 7431 plane_config->fb = intel_fb;
1ad292b5
JB
7432}
7433
70b23a98 7434static void chv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 7435 struct intel_crtc_state *pipe_config)
70b23a98
VS
7436{
7437 struct drm_device *dev = crtc->base.dev;
fac5e23e 7438 struct drm_i915_private *dev_priv = to_i915(dev);
70b23a98
VS
7439 int pipe = pipe_config->cpu_transcoder;
7440 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9e2c8475 7441 struct dpll clock;
0d7b6b11 7442 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
70b23a98
VS
7443 int refclk = 100000;
7444
b521973b
VS
7445 /* In case of DSI, DPLL will not be used */
7446 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7447 return;
7448
a580516d 7449 mutex_lock(&dev_priv->sb_lock);
70b23a98
VS
7450 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
7451 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
7452 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
7453 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
0d7b6b11 7454 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
a580516d 7455 mutex_unlock(&dev_priv->sb_lock);
70b23a98
VS
7456
7457 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
0d7b6b11
ID
7458 clock.m2 = (pll_dw0 & 0xff) << 22;
7459 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
7460 clock.m2 |= pll_dw2 & 0x3fffff;
70b23a98
VS
7461 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
7462 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
7463 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
7464
dccbea3b 7465 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
70b23a98
VS
7466}
7467
0e8ffe1b 7468static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5cec258b 7469 struct intel_crtc_state *pipe_config)
0e8ffe1b 7470{
6315b5d3 7471 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1729050e 7472 enum intel_display_power_domain power_domain;
0e8ffe1b 7473 uint32_t tmp;
1729050e 7474 bool ret;
0e8ffe1b 7475
1729050e
ID
7476 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
7477 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0
ID
7478 return false;
7479
e143a21c 7480 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 7481 pipe_config->shared_dpll = NULL;
eccb140b 7482
1729050e
ID
7483 ret = false;
7484
0e8ffe1b
DV
7485 tmp = I915_READ(PIPECONF(crtc->pipe));
7486 if (!(tmp & PIPECONF_ENABLE))
1729050e 7487 goto out;
0e8ffe1b 7488
9beb5fea
TU
7489 if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
7490 IS_CHERRYVIEW(dev_priv)) {
42571aef
VS
7491 switch (tmp & PIPECONF_BPC_MASK) {
7492 case PIPECONF_6BPC:
7493 pipe_config->pipe_bpp = 18;
7494 break;
7495 case PIPECONF_8BPC:
7496 pipe_config->pipe_bpp = 24;
7497 break;
7498 case PIPECONF_10BPC:
7499 pipe_config->pipe_bpp = 30;
7500 break;
7501 default:
7502 break;
7503 }
7504 }
7505
920a14b2 7506 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
666a4537 7507 (tmp & PIPECONF_COLOR_RANGE_SELECT))
b5a9fa09
DV
7508 pipe_config->limited_color_range = true;
7509
6315b5d3 7510 if (INTEL_GEN(dev_priv) < 4)
282740f7
VS
7511 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
7512
1bd1bd80 7513 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 7514 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 7515
2fa2fe9a
DV
7516 i9xx_get_pfit_config(crtc, pipe_config);
7517
6315b5d3 7518 if (INTEL_GEN(dev_priv) >= 4) {
c231775c 7519 /* No way to read it out on pipes B and C */
920a14b2 7520 if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
c231775c
VS
7521 tmp = dev_priv->chv_dpll_md[crtc->pipe];
7522 else
7523 tmp = I915_READ(DPLL_MD(crtc->pipe));
6c49f241
DV
7524 pipe_config->pixel_multiplier =
7525 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
7526 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 7527 pipe_config->dpll_hw_state.dpll_md = tmp;
50a0bc90 7528 } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
73f67aa8 7529 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
6c49f241
DV
7530 tmp = I915_READ(DPLL(crtc->pipe));
7531 pipe_config->pixel_multiplier =
7532 ((tmp & SDVO_MULTIPLIER_MASK)
7533 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
7534 } else {
7535 /* Note that on i915G/GM the pixel multiplier is in the sdvo
7536 * port and will be fixed up in the encoder->get_config
7537 * function. */
7538 pipe_config->pixel_multiplier = 1;
7539 }
8bcc2795 7540 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
920a14b2 7541 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
1c4e0274
VS
7542 /*
7543 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
7544 * on 830. Filter it out here so that we don't
7545 * report errors due to that.
7546 */
50a0bc90 7547 if (IS_I830(dev_priv))
1c4e0274
VS
7548 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
7549
8bcc2795
DV
7550 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
7551 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
7552 } else {
7553 /* Mask out read-only status bits. */
7554 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
7555 DPLL_PORTC_READY_MASK |
7556 DPLL_PORTB_READY_MASK);
8bcc2795 7557 }
6c49f241 7558
920a14b2 7559 if (IS_CHERRYVIEW(dev_priv))
70b23a98 7560 chv_crtc_clock_get(crtc, pipe_config);
11a914c2 7561 else if (IS_VALLEYVIEW(dev_priv))
acbec814
JB
7562 vlv_crtc_clock_get(crtc, pipe_config);
7563 else
7564 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 7565
0f64614d
VS
7566 /*
7567 * Normally the dotclock is filled in by the encoder .get_config()
7568 * but in case the pipe is enabled w/o any ports we need a sane
7569 * default.
7570 */
7571 pipe_config->base.adjusted_mode.crtc_clock =
7572 pipe_config->port_clock / pipe_config->pixel_multiplier;
7573
1729050e
ID
7574 ret = true;
7575
7576out:
7577 intel_display_power_put(dev_priv, power_domain);
7578
7579 return ret;
0e8ffe1b
DV
7580}
7581
c39055b0 7582static void ironlake_init_pch_refclk(struct drm_i915_private *dev_priv)
13d83a67 7583{
13d83a67 7584 struct intel_encoder *encoder;
1c1a24d2 7585 int i;
74cfd7ac 7586 u32 val, final;
13d83a67 7587 bool has_lvds = false;
199e5d79 7588 bool has_cpu_edp = false;
199e5d79 7589 bool has_panel = false;
99eb6a01
KP
7590 bool has_ck505 = false;
7591 bool can_ssc = false;
1c1a24d2 7592 bool using_ssc_source = false;
13d83a67
JB
7593
7594 /* We need to take the global config into account */
c39055b0 7595 for_each_intel_encoder(&dev_priv->drm, encoder) {
199e5d79
KP
7596 switch (encoder->type) {
7597 case INTEL_OUTPUT_LVDS:
7598 has_panel = true;
7599 has_lvds = true;
7600 break;
7601 case INTEL_OUTPUT_EDP:
7602 has_panel = true;
2de6905f 7603 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
7604 has_cpu_edp = true;
7605 break;
6847d71b
PZ
7606 default:
7607 break;
13d83a67
JB
7608 }
7609 }
7610
6e266956 7611 if (HAS_PCH_IBX(dev_priv)) {
41aa3448 7612 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
7613 can_ssc = has_ck505;
7614 } else {
7615 has_ck505 = false;
7616 can_ssc = true;
7617 }
7618
1c1a24d2
L
7619 /* Check if any DPLLs are using the SSC source */
7620 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
7621 u32 temp = I915_READ(PCH_DPLL(i));
7622
7623 if (!(temp & DPLL_VCO_ENABLE))
7624 continue;
7625
7626 if ((temp & PLL_REF_INPUT_MASK) ==
7627 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
7628 using_ssc_source = true;
7629 break;
7630 }
7631 }
7632
7633 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
7634 has_panel, has_lvds, has_ck505, using_ssc_source);
13d83a67
JB
7635
7636 /* Ironlake: try to setup display ref clock before DPLL
7637 * enabling. This is only under driver's control after
7638 * PCH B stepping, previous chipset stepping should be
7639 * ignoring this setting.
7640 */
74cfd7ac
CW
7641 val = I915_READ(PCH_DREF_CONTROL);
7642
7643 /* As we must carefully and slowly disable/enable each source in turn,
7644 * compute the final state we want first and check if we need to
7645 * make any changes at all.
7646 */
7647 final = val;
7648 final &= ~DREF_NONSPREAD_SOURCE_MASK;
7649 if (has_ck505)
7650 final |= DREF_NONSPREAD_CK505_ENABLE;
7651 else
7652 final |= DREF_NONSPREAD_SOURCE_ENABLE;
7653
8c07eb68 7654 final &= ~DREF_SSC_SOURCE_MASK;
74cfd7ac 7655 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8c07eb68 7656 final &= ~DREF_SSC1_ENABLE;
74cfd7ac
CW
7657
7658 if (has_panel) {
7659 final |= DREF_SSC_SOURCE_ENABLE;
7660
7661 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7662 final |= DREF_SSC1_ENABLE;
7663
7664 if (has_cpu_edp) {
7665 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7666 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
7667 else
7668 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
7669 } else
7670 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
1c1a24d2
L
7671 } else if (using_ssc_source) {
7672 final |= DREF_SSC_SOURCE_ENABLE;
7673 final |= DREF_SSC1_ENABLE;
74cfd7ac
CW
7674 }
7675
7676 if (final == val)
7677 return;
7678
13d83a67 7679 /* Always enable nonspread source */
74cfd7ac 7680 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 7681
99eb6a01 7682 if (has_ck505)
74cfd7ac 7683 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 7684 else
74cfd7ac 7685 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 7686
199e5d79 7687 if (has_panel) {
74cfd7ac
CW
7688 val &= ~DREF_SSC_SOURCE_MASK;
7689 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 7690
199e5d79 7691 /* SSC must be turned on before enabling the CPU output */
99eb6a01 7692 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 7693 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 7694 val |= DREF_SSC1_ENABLE;
e77166b5 7695 } else
74cfd7ac 7696 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
7697
7698 /* Get SSC going before enabling the outputs */
74cfd7ac 7699 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
7700 POSTING_READ(PCH_DREF_CONTROL);
7701 udelay(200);
7702
74cfd7ac 7703 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
7704
7705 /* Enable CPU source on CPU attached eDP */
199e5d79 7706 if (has_cpu_edp) {
99eb6a01 7707 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 7708 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 7709 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 7710 } else
74cfd7ac 7711 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 7712 } else
74cfd7ac 7713 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 7714
74cfd7ac 7715 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
7716 POSTING_READ(PCH_DREF_CONTROL);
7717 udelay(200);
7718 } else {
1c1a24d2 7719 DRM_DEBUG_KMS("Disabling CPU source output\n");
199e5d79 7720
74cfd7ac 7721 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
7722
7723 /* Turn off CPU output */
74cfd7ac 7724 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 7725
74cfd7ac 7726 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
7727 POSTING_READ(PCH_DREF_CONTROL);
7728 udelay(200);
7729
1c1a24d2
L
7730 if (!using_ssc_source) {
7731 DRM_DEBUG_KMS("Disabling SSC source\n");
199e5d79 7732
1c1a24d2
L
7733 /* Turn off the SSC source */
7734 val &= ~DREF_SSC_SOURCE_MASK;
7735 val |= DREF_SSC_SOURCE_DISABLE;
f165d283 7736
1c1a24d2
L
7737 /* Turn off SSC1 */
7738 val &= ~DREF_SSC1_ENABLE;
7739
7740 I915_WRITE(PCH_DREF_CONTROL, val);
7741 POSTING_READ(PCH_DREF_CONTROL);
7742 udelay(200);
7743 }
13d83a67 7744 }
74cfd7ac
CW
7745
7746 BUG_ON(val != final);
13d83a67
JB
7747}
7748
f31f2d55 7749static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 7750{
f31f2d55 7751 uint32_t tmp;
dde86e2d 7752
0ff066a9
PZ
7753 tmp = I915_READ(SOUTH_CHICKEN2);
7754 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
7755 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 7756
cf3598c2
ID
7757 if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
7758 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
0ff066a9 7759 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 7760
0ff066a9
PZ
7761 tmp = I915_READ(SOUTH_CHICKEN2);
7762 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
7763 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 7764
cf3598c2
ID
7765 if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
7766 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
0ff066a9 7767 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
7768}
7769
7770/* WaMPhyProgramming:hsw */
7771static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
7772{
7773 uint32_t tmp;
dde86e2d
PZ
7774
7775 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
7776 tmp &= ~(0xFF << 24);
7777 tmp |= (0x12 << 24);
7778 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
7779
dde86e2d
PZ
7780 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
7781 tmp |= (1 << 11);
7782 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
7783
7784 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
7785 tmp |= (1 << 11);
7786 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
7787
dde86e2d
PZ
7788 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
7789 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7790 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
7791
7792 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
7793 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7794 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
7795
0ff066a9
PZ
7796 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
7797 tmp &= ~(7 << 13);
7798 tmp |= (5 << 13);
7799 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 7800
0ff066a9
PZ
7801 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
7802 tmp &= ~(7 << 13);
7803 tmp |= (5 << 13);
7804 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
7805
7806 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
7807 tmp &= ~0xFF;
7808 tmp |= 0x1C;
7809 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
7810
7811 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
7812 tmp &= ~0xFF;
7813 tmp |= 0x1C;
7814 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
7815
7816 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
7817 tmp &= ~(0xFF << 16);
7818 tmp |= (0x1C << 16);
7819 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
7820
7821 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
7822 tmp &= ~(0xFF << 16);
7823 tmp |= (0x1C << 16);
7824 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
7825
0ff066a9
PZ
7826 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
7827 tmp |= (1 << 27);
7828 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 7829
0ff066a9
PZ
7830 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
7831 tmp |= (1 << 27);
7832 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 7833
0ff066a9
PZ
7834 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
7835 tmp &= ~(0xF << 28);
7836 tmp |= (4 << 28);
7837 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 7838
0ff066a9
PZ
7839 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
7840 tmp &= ~(0xF << 28);
7841 tmp |= (4 << 28);
7842 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
7843}
7844
2fa86a1f
PZ
7845/* Implements 3 different sequences from BSpec chapter "Display iCLK
7846 * Programming" based on the parameters passed:
7847 * - Sequence to enable CLKOUT_DP
7848 * - Sequence to enable CLKOUT_DP without spread
7849 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
7850 */
c39055b0
ACO
7851static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
7852 bool with_spread, bool with_fdi)
f31f2d55 7853{
2fa86a1f
PZ
7854 uint32_t reg, tmp;
7855
7856 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
7857 with_spread = true;
4f8036a2
TU
7858 if (WARN(HAS_PCH_LPT_LP(dev_priv) &&
7859 with_fdi, "LP PCH doesn't have FDI\n"))
2fa86a1f 7860 with_fdi = false;
f31f2d55 7861
a580516d 7862 mutex_lock(&dev_priv->sb_lock);
f31f2d55
PZ
7863
7864 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7865 tmp &= ~SBI_SSCCTL_DISABLE;
7866 tmp |= SBI_SSCCTL_PATHALT;
7867 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7868
7869 udelay(24);
7870
2fa86a1f
PZ
7871 if (with_spread) {
7872 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7873 tmp &= ~SBI_SSCCTL_PATHALT;
7874 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 7875
2fa86a1f
PZ
7876 if (with_fdi) {
7877 lpt_reset_fdi_mphy(dev_priv);
7878 lpt_program_fdi_mphy(dev_priv);
7879 }
7880 }
dde86e2d 7881
4f8036a2 7882 reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
2fa86a1f
PZ
7883 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
7884 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
7885 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246 7886
a580516d 7887 mutex_unlock(&dev_priv->sb_lock);
dde86e2d
PZ
7888}
7889
47701c3b 7890/* Sequence to disable CLKOUT_DP */
c39055b0 7891static void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
47701c3b 7892{
47701c3b
PZ
7893 uint32_t reg, tmp;
7894
a580516d 7895 mutex_lock(&dev_priv->sb_lock);
47701c3b 7896
4f8036a2 7897 reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
47701c3b
PZ
7898 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
7899 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
7900 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
7901
7902 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7903 if (!(tmp & SBI_SSCCTL_DISABLE)) {
7904 if (!(tmp & SBI_SSCCTL_PATHALT)) {
7905 tmp |= SBI_SSCCTL_PATHALT;
7906 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7907 udelay(32);
7908 }
7909 tmp |= SBI_SSCCTL_DISABLE;
7910 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7911 }
7912
a580516d 7913 mutex_unlock(&dev_priv->sb_lock);
47701c3b
PZ
7914}
7915
f7be2c21
VS
7916#define BEND_IDX(steps) ((50 + (steps)) / 5)
7917
7918static const uint16_t sscdivintphase[] = {
7919 [BEND_IDX( 50)] = 0x3B23,
7920 [BEND_IDX( 45)] = 0x3B23,
7921 [BEND_IDX( 40)] = 0x3C23,
7922 [BEND_IDX( 35)] = 0x3C23,
7923 [BEND_IDX( 30)] = 0x3D23,
7924 [BEND_IDX( 25)] = 0x3D23,
7925 [BEND_IDX( 20)] = 0x3E23,
7926 [BEND_IDX( 15)] = 0x3E23,
7927 [BEND_IDX( 10)] = 0x3F23,
7928 [BEND_IDX( 5)] = 0x3F23,
7929 [BEND_IDX( 0)] = 0x0025,
7930 [BEND_IDX( -5)] = 0x0025,
7931 [BEND_IDX(-10)] = 0x0125,
7932 [BEND_IDX(-15)] = 0x0125,
7933 [BEND_IDX(-20)] = 0x0225,
7934 [BEND_IDX(-25)] = 0x0225,
7935 [BEND_IDX(-30)] = 0x0325,
7936 [BEND_IDX(-35)] = 0x0325,
7937 [BEND_IDX(-40)] = 0x0425,
7938 [BEND_IDX(-45)] = 0x0425,
7939 [BEND_IDX(-50)] = 0x0525,
7940};
7941
7942/*
7943 * Bend CLKOUT_DP
7944 * steps -50 to 50 inclusive, in steps of 5
7945 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
7946 * change in clock period = -(steps / 10) * 5.787 ps
7947 */
7948static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
7949{
7950 uint32_t tmp;
7951 int idx = BEND_IDX(steps);
7952
7953 if (WARN_ON(steps % 5 != 0))
7954 return;
7955
7956 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
7957 return;
7958
7959 mutex_lock(&dev_priv->sb_lock);
7960
7961 if (steps % 10 != 0)
7962 tmp = 0xAAAAAAAB;
7963 else
7964 tmp = 0x00000000;
7965 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
7966
7967 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
7968 tmp &= 0xffff0000;
7969 tmp |= sscdivintphase[idx];
7970 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
7971
7972 mutex_unlock(&dev_priv->sb_lock);
7973}
7974
7975#undef BEND_IDX
7976
c39055b0 7977static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
bf8fa3d3 7978{
bf8fa3d3
PZ
7979 struct intel_encoder *encoder;
7980 bool has_vga = false;
7981
c39055b0 7982 for_each_intel_encoder(&dev_priv->drm, encoder) {
bf8fa3d3
PZ
7983 switch (encoder->type) {
7984 case INTEL_OUTPUT_ANALOG:
7985 has_vga = true;
7986 break;
6847d71b
PZ
7987 default:
7988 break;
bf8fa3d3
PZ
7989 }
7990 }
7991
f7be2c21 7992 if (has_vga) {
c39055b0
ACO
7993 lpt_bend_clkout_dp(dev_priv, 0);
7994 lpt_enable_clkout_dp(dev_priv, true, true);
f7be2c21 7995 } else {
c39055b0 7996 lpt_disable_clkout_dp(dev_priv);
f7be2c21 7997 }
bf8fa3d3
PZ
7998}
7999
dde86e2d
PZ
8000/*
8001 * Initialize reference clocks when the driver loads
8002 */
c39055b0 8003void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
dde86e2d 8004{
6e266956 8005 if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
c39055b0 8006 ironlake_init_pch_refclk(dev_priv);
6e266956 8007 else if (HAS_PCH_LPT(dev_priv))
c39055b0 8008 lpt_init_pch_refclk(dev_priv);
dde86e2d
PZ
8009}
8010
6ff93609 8011static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 8012{
fac5e23e 8013 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
79e53945
JB
8014 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8015 int pipe = intel_crtc->pipe;
c8203565
PZ
8016 uint32_t val;
8017
78114071 8018 val = 0;
c8203565 8019
6e3c9717 8020 switch (intel_crtc->config->pipe_bpp) {
c8203565 8021 case 18:
dfd07d72 8022 val |= PIPECONF_6BPC;
c8203565
PZ
8023 break;
8024 case 24:
dfd07d72 8025 val |= PIPECONF_8BPC;
c8203565
PZ
8026 break;
8027 case 30:
dfd07d72 8028 val |= PIPECONF_10BPC;
c8203565
PZ
8029 break;
8030 case 36:
dfd07d72 8031 val |= PIPECONF_12BPC;
c8203565
PZ
8032 break;
8033 default:
cc769b62
PZ
8034 /* Case prevented by intel_choose_pipe_bpp_dither. */
8035 BUG();
c8203565
PZ
8036 }
8037
6e3c9717 8038 if (intel_crtc->config->dither)
c8203565
PZ
8039 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8040
6e3c9717 8041 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
8042 val |= PIPECONF_INTERLACED_ILK;
8043 else
8044 val |= PIPECONF_PROGRESSIVE;
8045
6e3c9717 8046 if (intel_crtc->config->limited_color_range)
3685a8f3 8047 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 8048
c8203565
PZ
8049 I915_WRITE(PIPECONF(pipe), val);
8050 POSTING_READ(PIPECONF(pipe));
8051}
8052
6ff93609 8053static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 8054{
fac5e23e 8055 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
ee2b0b38 8056 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 8057 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
391bf048 8058 u32 val = 0;
ee2b0b38 8059
391bf048 8060 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
ee2b0b38
PZ
8061 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8062
6e3c9717 8063 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
8064 val |= PIPECONF_INTERLACED_ILK;
8065 else
8066 val |= PIPECONF_PROGRESSIVE;
8067
702e7a56
PZ
8068 I915_WRITE(PIPECONF(cpu_transcoder), val);
8069 POSTING_READ(PIPECONF(cpu_transcoder));
391bf048
JN
8070}
8071
391bf048
JN
8072static void haswell_set_pipemisc(struct drm_crtc *crtc)
8073{
fac5e23e 8074 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
391bf048 8075 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756f85cf 8076
391bf048
JN
8077 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
8078 u32 val = 0;
756f85cf 8079
6e3c9717 8080 switch (intel_crtc->config->pipe_bpp) {
756f85cf
PZ
8081 case 18:
8082 val |= PIPEMISC_DITHER_6_BPC;
8083 break;
8084 case 24:
8085 val |= PIPEMISC_DITHER_8_BPC;
8086 break;
8087 case 30:
8088 val |= PIPEMISC_DITHER_10_BPC;
8089 break;
8090 case 36:
8091 val |= PIPEMISC_DITHER_12_BPC;
8092 break;
8093 default:
8094 /* Case prevented by pipe_config_set_bpp. */
8095 BUG();
8096 }
8097
6e3c9717 8098 if (intel_crtc->config->dither)
756f85cf
PZ
8099 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8100
391bf048 8101 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
756f85cf 8102 }
ee2b0b38
PZ
8103}
8104
d4b1931c
PZ
8105int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8106{
8107 /*
8108 * Account for spread spectrum to avoid
8109 * oversubscribing the link. Max center spread
8110 * is 2.5%; use 5% for safety's sake.
8111 */
8112 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 8113 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
8114}
8115
7429e9d4 8116static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 8117{
7429e9d4 8118 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
8119}
8120
b75ca6f6
ACO
8121static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
8122 struct intel_crtc_state *crtc_state,
9e2c8475 8123 struct dpll *reduced_clock)
79e53945 8124{
de13a2e3 8125 struct drm_crtc *crtc = &intel_crtc->base;
79e53945 8126 struct drm_device *dev = crtc->dev;
fac5e23e 8127 struct drm_i915_private *dev_priv = to_i915(dev);
b75ca6f6 8128 u32 dpll, fp, fp2;
3d6e9ee0 8129 int factor;
79e53945 8130
c1858123 8131 /* Enable autotuning of the PLL clock (if permissible) */
8febb297 8132 factor = 21;
3d6e9ee0 8133 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8febb297 8134 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 8135 dev_priv->vbt.lvds_ssc_freq == 100000) ||
6e266956 8136 (HAS_PCH_IBX(dev_priv) && intel_is_dual_link_lvds(dev)))
8febb297 8137 factor = 25;
190f68c5 8138 } else if (crtc_state->sdvo_tv_clock)
8febb297 8139 factor = 20;
c1858123 8140
b75ca6f6
ACO
8141 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
8142
190f68c5 8143 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
b75ca6f6
ACO
8144 fp |= FP_CB_TUNE;
8145
8146 if (reduced_clock) {
8147 fp2 = i9xx_dpll_compute_fp(reduced_clock);
2c07245f 8148
b75ca6f6
ACO
8149 if (reduced_clock->m < factor * reduced_clock->n)
8150 fp2 |= FP_CB_TUNE;
8151 } else {
8152 fp2 = fp;
8153 }
9a7c7890 8154
5eddb70b 8155 dpll = 0;
2c07245f 8156
3d6e9ee0 8157 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
a07d6787
EA
8158 dpll |= DPLLB_MODE_LVDS;
8159 else
8160 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 8161
190f68c5 8162 dpll |= (crtc_state->pixel_multiplier - 1)
ef1b460d 8163 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f 8164
3d6e9ee0
VS
8165 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
8166 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
4a33e48d 8167 dpll |= DPLL_SDVO_HIGH_SPEED;
3d6e9ee0 8168
37a5650b 8169 if (intel_crtc_has_dp_encoder(crtc_state))
4a33e48d 8170 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 8171
7d7f8633
VS
8172 /*
8173 * The high speed IO clock is only really required for
8174 * SDVO/HDMI/DP, but we also enable it for CRT to make it
8175 * possible to share the DPLL between CRT and HDMI. Enabling
8176 * the clock needlessly does no real harm, except use up a
8177 * bit of power potentially.
8178 *
8179 * We'll limit this to IVB with 3 pipes, since it has only two
8180 * DPLLs and so DPLL sharing is the only way to get three pipes
8181 * driving PCH ports at the same time. On SNB we could do this,
8182 * and potentially avoid enabling the second DPLL, but it's not
8183 * clear if it''s a win or loss power wise. No point in doing
8184 * this on ILK at all since it has a fixed DPLL<->pipe mapping.
8185 */
8186 if (INTEL_INFO(dev_priv)->num_pipes == 3 &&
8187 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
8188 dpll |= DPLL_SDVO_HIGH_SPEED;
8189
a07d6787 8190 /* compute bitmask from p1 value */
190f68c5 8191 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 8192 /* also FPA1 */
190f68c5 8193 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 8194
190f68c5 8195 switch (crtc_state->dpll.p2) {
a07d6787
EA
8196 case 5:
8197 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8198 break;
8199 case 7:
8200 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8201 break;
8202 case 10:
8203 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8204 break;
8205 case 14:
8206 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8207 break;
79e53945
JB
8208 }
8209
3d6e9ee0
VS
8210 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8211 intel_panel_use_ssc(dev_priv))
43565a06 8212 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
8213 else
8214 dpll |= PLL_REF_INPUT_DREFCLK;
8215
b75ca6f6
ACO
8216 dpll |= DPLL_VCO_ENABLE;
8217
8218 crtc_state->dpll_hw_state.dpll = dpll;
8219 crtc_state->dpll_hw_state.fp0 = fp;
8220 crtc_state->dpll_hw_state.fp1 = fp2;
de13a2e3
PZ
8221}
8222
190f68c5
ACO
8223static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8224 struct intel_crtc_state *crtc_state)
de13a2e3 8225{
997c030c 8226 struct drm_device *dev = crtc->base.dev;
fac5e23e 8227 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 8228 const struct intel_limit *limit;
997c030c 8229 int refclk = 120000;
de13a2e3 8230
dd3cd74a
ACO
8231 memset(&crtc_state->dpll_hw_state, 0,
8232 sizeof(crtc_state->dpll_hw_state));
8233
ded220e2
ACO
8234 crtc->lowfreq_avail = false;
8235
8236 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
8237 if (!crtc_state->has_pch_encoder)
8238 return 0;
79e53945 8239
2d84d2b3 8240 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
997c030c
ACO
8241 if (intel_panel_use_ssc(dev_priv)) {
8242 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
8243 dev_priv->vbt.lvds_ssc_freq);
8244 refclk = dev_priv->vbt.lvds_ssc_freq;
8245 }
8246
8247 if (intel_is_dual_link_lvds(dev)) {
8248 if (refclk == 100000)
8249 limit = &intel_limits_ironlake_dual_lvds_100m;
8250 else
8251 limit = &intel_limits_ironlake_dual_lvds;
8252 } else {
8253 if (refclk == 100000)
8254 limit = &intel_limits_ironlake_single_lvds_100m;
8255 else
8256 limit = &intel_limits_ironlake_single_lvds;
8257 }
8258 } else {
8259 limit = &intel_limits_ironlake_dac;
8260 }
8261
364ee29d 8262 if (!crtc_state->clock_set &&
997c030c
ACO
8263 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8264 refclk, NULL, &crtc_state->dpll)) {
364ee29d
ACO
8265 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8266 return -EINVAL;
f47709a9 8267 }
79e53945 8268
cbaa3315 8269 ironlake_compute_dpll(crtc, crtc_state, NULL);
66e985c0 8270
efd38b68 8271 if (!intel_get_shared_dpll(crtc, crtc_state, NULL)) {
ded220e2
ACO
8272 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
8273 pipe_name(crtc->pipe));
8274 return -EINVAL;
3fb37703 8275 }
79e53945 8276
c8f7a0db 8277 return 0;
79e53945
JB
8278}
8279
eb14cb74
VS
8280static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8281 struct intel_link_m_n *m_n)
8282{
8283 struct drm_device *dev = crtc->base.dev;
fac5e23e 8284 struct drm_i915_private *dev_priv = to_i915(dev);
eb14cb74
VS
8285 enum pipe pipe = crtc->pipe;
8286
8287 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8288 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8289 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8290 & ~TU_SIZE_MASK;
8291 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8292 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8293 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8294}
8295
8296static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8297 enum transcoder transcoder,
b95af8be
VK
8298 struct intel_link_m_n *m_n,
8299 struct intel_link_m_n *m2_n2)
72419203 8300{
6315b5d3 8301 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
eb14cb74 8302 enum pipe pipe = crtc->pipe;
72419203 8303
6315b5d3 8304 if (INTEL_GEN(dev_priv) >= 5) {
eb14cb74
VS
8305 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8306 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8307 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8308 & ~TU_SIZE_MASK;
8309 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8310 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8311 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
b95af8be
VK
8312 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8313 * gen < 8) and if DRRS is supported (to make sure the
8314 * registers are not unnecessarily read).
8315 */
6315b5d3 8316 if (m2_n2 && INTEL_GEN(dev_priv) < 8 &&
6e3c9717 8317 crtc->config->has_drrs) {
b95af8be
VK
8318 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8319 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8320 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8321 & ~TU_SIZE_MASK;
8322 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8323 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8324 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8325 }
eb14cb74
VS
8326 } else {
8327 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8328 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8329 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8330 & ~TU_SIZE_MASK;
8331 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8332 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8333 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8334 }
8335}
8336
8337void intel_dp_get_m_n(struct intel_crtc *crtc,
5cec258b 8338 struct intel_crtc_state *pipe_config)
eb14cb74 8339{
681a8504 8340 if (pipe_config->has_pch_encoder)
eb14cb74
VS
8341 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8342 else
8343 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be
VK
8344 &pipe_config->dp_m_n,
8345 &pipe_config->dp_m2_n2);
eb14cb74 8346}
72419203 8347
eb14cb74 8348static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5cec258b 8349 struct intel_crtc_state *pipe_config)
eb14cb74
VS
8350{
8351 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be 8352 &pipe_config->fdi_m_n, NULL);
72419203
DV
8353}
8354
bd2e244f 8355static void skylake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 8356 struct intel_crtc_state *pipe_config)
bd2e244f
JB
8357{
8358 struct drm_device *dev = crtc->base.dev;
fac5e23e 8359 struct drm_i915_private *dev_priv = to_i915(dev);
a1b2278e
CK
8360 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
8361 uint32_t ps_ctrl = 0;
8362 int id = -1;
8363 int i;
bd2e244f 8364
a1b2278e
CK
8365 /* find scaler attached to this pipe */
8366 for (i = 0; i < crtc->num_scalers; i++) {
8367 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
8368 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
8369 id = i;
8370 pipe_config->pch_pfit.enabled = true;
8371 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
8372 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
8373 break;
8374 }
8375 }
bd2e244f 8376
a1b2278e
CK
8377 scaler_state->scaler_id = id;
8378 if (id >= 0) {
8379 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
8380 } else {
8381 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
bd2e244f
JB
8382 }
8383}
8384
5724dbd1
DL
8385static void
8386skylake_get_initial_plane_config(struct intel_crtc *crtc,
8387 struct intel_initial_plane_config *plane_config)
bc8d7dff
DL
8388{
8389 struct drm_device *dev = crtc->base.dev;
fac5e23e 8390 struct drm_i915_private *dev_priv = to_i915(dev);
40f46283 8391 u32 val, base, offset, stride_mult, tiling;
bc8d7dff
DL
8392 int pipe = crtc->pipe;
8393 int fourcc, pixel_format;
6761dd31 8394 unsigned int aligned_height;
bc8d7dff 8395 struct drm_framebuffer *fb;
1b842c89 8396 struct intel_framebuffer *intel_fb;
bc8d7dff 8397
d9806c9f 8398 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 8399 if (!intel_fb) {
bc8d7dff
DL
8400 DRM_DEBUG_KMS("failed to alloc fb\n");
8401 return;
8402 }
8403
1b842c89
DL
8404 fb = &intel_fb->base;
8405
d2e9f5fc
VS
8406 fb->dev = dev;
8407
bc8d7dff 8408 val = I915_READ(PLANE_CTL(pipe, 0));
42a7b088
DL
8409 if (!(val & PLANE_CTL_ENABLE))
8410 goto error;
8411
bc8d7dff
DL
8412 pixel_format = val & PLANE_CTL_FORMAT_MASK;
8413 fourcc = skl_format_to_fourcc(pixel_format,
8414 val & PLANE_CTL_ORDER_RGBX,
8415 val & PLANE_CTL_ALPHA_MASK);
2f3f4763 8416 fb->format = drm_format_info(fourcc);
bc8d7dff 8417
40f46283
DL
8418 tiling = val & PLANE_CTL_TILED_MASK;
8419 switch (tiling) {
8420 case PLANE_CTL_TILED_LINEAR:
2f075565 8421 fb->modifier = DRM_FORMAT_MOD_LINEAR;
40f46283
DL
8422 break;
8423 case PLANE_CTL_TILED_X:
8424 plane_config->tiling = I915_TILING_X;
bae781b2 8425 fb->modifier = I915_FORMAT_MOD_X_TILED;
40f46283
DL
8426 break;
8427 case PLANE_CTL_TILED_Y:
bae781b2 8428 fb->modifier = I915_FORMAT_MOD_Y_TILED;
40f46283
DL
8429 break;
8430 case PLANE_CTL_TILED_YF:
bae781b2 8431 fb->modifier = I915_FORMAT_MOD_Yf_TILED;
40f46283
DL
8432 break;
8433 default:
8434 MISSING_CASE(tiling);
8435 goto error;
8436 }
8437
bc8d7dff
DL
8438 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
8439 plane_config->base = base;
8440
8441 offset = I915_READ(PLANE_OFFSET(pipe, 0));
8442
8443 val = I915_READ(PLANE_SIZE(pipe, 0));
8444 fb->height = ((val >> 16) & 0xfff) + 1;
8445 fb->width = ((val >> 0) & 0x1fff) + 1;
8446
8447 val = I915_READ(PLANE_STRIDE(pipe, 0));
d88c4afd 8448 stride_mult = intel_fb_stride_alignment(fb, 0);
bc8d7dff
DL
8449 fb->pitches[0] = (val & 0x3ff) * stride_mult;
8450
d88c4afd 8451 aligned_height = intel_fb_align_height(fb, 0, fb->height);
bc8d7dff 8452
f37b5c2b 8453 plane_config->size = fb->pitches[0] * aligned_height;
bc8d7dff
DL
8454
8455 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8456 pipe_name(pipe), fb->width, fb->height,
272725c7 8457 fb->format->cpp[0] * 8, base, fb->pitches[0],
bc8d7dff
DL
8458 plane_config->size);
8459
2d14030b 8460 plane_config->fb = intel_fb;
bc8d7dff
DL
8461 return;
8462
8463error:
d1a3a036 8464 kfree(intel_fb);
bc8d7dff
DL
8465}
8466
2fa2fe9a 8467static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 8468 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
8469{
8470 struct drm_device *dev = crtc->base.dev;
fac5e23e 8471 struct drm_i915_private *dev_priv = to_i915(dev);
2fa2fe9a
DV
8472 uint32_t tmp;
8473
8474 tmp = I915_READ(PF_CTL(crtc->pipe));
8475
8476 if (tmp & PF_ENABLE) {
fd4daa9c 8477 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
DV
8478 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
8479 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
DV
8480
8481 /* We currently do not free assignements of panel fitters on
8482 * ivb/hsw (since we don't use the higher upscaling modes which
8483 * differentiates them) so just WARN about this case for now. */
5db94019 8484 if (IS_GEN7(dev_priv)) {
cb8b2a30
DV
8485 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
8486 PF_PIPE_SEL_IVB(crtc->pipe));
8487 }
2fa2fe9a 8488 }
79e53945
JB
8489}
8490
5724dbd1
DL
8491static void
8492ironlake_get_initial_plane_config(struct intel_crtc *crtc,
8493 struct intel_initial_plane_config *plane_config)
4c6baa59
JB
8494{
8495 struct drm_device *dev = crtc->base.dev;
fac5e23e 8496 struct drm_i915_private *dev_priv = to_i915(dev);
4c6baa59 8497 u32 val, base, offset;
aeee5a49 8498 int pipe = crtc->pipe;
4c6baa59 8499 int fourcc, pixel_format;
6761dd31 8500 unsigned int aligned_height;
b113d5ee 8501 struct drm_framebuffer *fb;
1b842c89 8502 struct intel_framebuffer *intel_fb;
4c6baa59 8503
42a7b088
DL
8504 val = I915_READ(DSPCNTR(pipe));
8505 if (!(val & DISPLAY_PLANE_ENABLE))
8506 return;
8507
d9806c9f 8508 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 8509 if (!intel_fb) {
4c6baa59
JB
8510 DRM_DEBUG_KMS("failed to alloc fb\n");
8511 return;
8512 }
8513
1b842c89
DL
8514 fb = &intel_fb->base;
8515
d2e9f5fc
VS
8516 fb->dev = dev;
8517
6315b5d3 8518 if (INTEL_GEN(dev_priv) >= 4) {
18c5247e 8519 if (val & DISPPLANE_TILED) {
49af449b 8520 plane_config->tiling = I915_TILING_X;
bae781b2 8521 fb->modifier = I915_FORMAT_MOD_X_TILED;
18c5247e
DV
8522 }
8523 }
4c6baa59
JB
8524
8525 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 8526 fourcc = i9xx_format_to_fourcc(pixel_format);
2f3f4763 8527 fb->format = drm_format_info(fourcc);
4c6baa59 8528
aeee5a49 8529 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
8652744b 8530 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
aeee5a49 8531 offset = I915_READ(DSPOFFSET(pipe));
4c6baa59 8532 } else {
49af449b 8533 if (plane_config->tiling)
aeee5a49 8534 offset = I915_READ(DSPTILEOFF(pipe));
4c6baa59 8535 else
aeee5a49 8536 offset = I915_READ(DSPLINOFF(pipe));
4c6baa59
JB
8537 }
8538 plane_config->base = base;
8539
8540 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
8541 fb->width = ((val >> 16) & 0xfff) + 1;
8542 fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
8543
8544 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 8545 fb->pitches[0] = val & 0xffffffc0;
4c6baa59 8546
d88c4afd 8547 aligned_height = intel_fb_align_height(fb, 0, fb->height);
4c6baa59 8548
f37b5c2b 8549 plane_config->size = fb->pitches[0] * aligned_height;
4c6baa59 8550
2844a921
DL
8551 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8552 pipe_name(pipe), fb->width, fb->height,
272725c7 8553 fb->format->cpp[0] * 8, base, fb->pitches[0],
2844a921 8554 plane_config->size);
b113d5ee 8555
2d14030b 8556 plane_config->fb = intel_fb;
4c6baa59
JB
8557}
8558
0e8ffe1b 8559static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5cec258b 8560 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
8561{
8562 struct drm_device *dev = crtc->base.dev;
fac5e23e 8563 struct drm_i915_private *dev_priv = to_i915(dev);
1729050e 8564 enum intel_display_power_domain power_domain;
0e8ffe1b 8565 uint32_t tmp;
1729050e 8566 bool ret;
0e8ffe1b 8567
1729050e
ID
8568 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8569 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
930e8c9e
PZ
8570 return false;
8571
e143a21c 8572 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 8573 pipe_config->shared_dpll = NULL;
eccb140b 8574
1729050e 8575 ret = false;
0e8ffe1b
DV
8576 tmp = I915_READ(PIPECONF(crtc->pipe));
8577 if (!(tmp & PIPECONF_ENABLE))
1729050e 8578 goto out;
0e8ffe1b 8579
42571aef
VS
8580 switch (tmp & PIPECONF_BPC_MASK) {
8581 case PIPECONF_6BPC:
8582 pipe_config->pipe_bpp = 18;
8583 break;
8584 case PIPECONF_8BPC:
8585 pipe_config->pipe_bpp = 24;
8586 break;
8587 case PIPECONF_10BPC:
8588 pipe_config->pipe_bpp = 30;
8589 break;
8590 case PIPECONF_12BPC:
8591 pipe_config->pipe_bpp = 36;
8592 break;
8593 default:
8594 break;
8595 }
8596
b5a9fa09
DV
8597 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
8598 pipe_config->limited_color_range = true;
8599
ab9412ba 8600 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0 8601 struct intel_shared_dpll *pll;
8106ddbd 8602 enum intel_dpll_id pll_id;
66e985c0 8603
88adfff1
DV
8604 pipe_config->has_pch_encoder = true;
8605
627eb5a3
DV
8606 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
8607 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
8608 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
8609
8610 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 8611
2d1fe073 8612 if (HAS_PCH_IBX(dev_priv)) {
d9a7bc67
ID
8613 /*
8614 * The pipe->pch transcoder and pch transcoder->pll
8615 * mapping is fixed.
8616 */
8106ddbd 8617 pll_id = (enum intel_dpll_id) crtc->pipe;
c0d43d62
DV
8618 } else {
8619 tmp = I915_READ(PCH_DPLL_SEL);
8620 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
8106ddbd 8621 pll_id = DPLL_ID_PCH_PLL_B;
c0d43d62 8622 else
8106ddbd 8623 pll_id= DPLL_ID_PCH_PLL_A;
c0d43d62 8624 }
66e985c0 8625
8106ddbd
ACO
8626 pipe_config->shared_dpll =
8627 intel_get_shared_dpll_by_id(dev_priv, pll_id);
8628 pll = pipe_config->shared_dpll;
66e985c0 8629
2edd6443
ACO
8630 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
8631 &pipe_config->dpll_hw_state));
c93f54cf
DV
8632
8633 tmp = pipe_config->dpll_hw_state.dpll;
8634 pipe_config->pixel_multiplier =
8635 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
8636 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
8637
8638 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
DV
8639 } else {
8640 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
8641 }
8642
1bd1bd80 8643 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 8644 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 8645
2fa2fe9a
DV
8646 ironlake_get_pfit_config(crtc, pipe_config);
8647
1729050e
ID
8648 ret = true;
8649
8650out:
8651 intel_display_power_put(dev_priv, power_domain);
8652
8653 return ret;
0e8ffe1b
DV
8654}
8655
be256dc7
PZ
8656static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
8657{
91c8a326 8658 struct drm_device *dev = &dev_priv->drm;
be256dc7 8659 struct intel_crtc *crtc;
be256dc7 8660
d3fcc808 8661 for_each_intel_crtc(dev, crtc)
e2c719b7 8662 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
8663 pipe_name(crtc->pipe));
8664
e2c719b7
RC
8665 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
8666 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
01403de3
VS
8667 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
8668 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
44cb734c 8669 I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
e2c719b7 8670 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
be256dc7 8671 "CPU PWM1 enabled\n");
772c2a51 8672 if (IS_HASWELL(dev_priv))
e2c719b7 8673 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
c5107b87 8674 "CPU PWM2 enabled\n");
e2c719b7 8675 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
be256dc7 8676 "PCH PWM1 enabled\n");
e2c719b7 8677 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
be256dc7 8678 "Utility pin enabled\n");
e2c719b7 8679 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
be256dc7 8680
9926ada1
PZ
8681 /*
8682 * In theory we can still leave IRQs enabled, as long as only the HPD
8683 * interrupts remain enabled. We used to check for that, but since it's
8684 * gen-specific and since we only disable LCPLL after we fully disable
8685 * the interrupts, the check below should be enough.
8686 */
e2c719b7 8687 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
be256dc7
PZ
8688}
8689
9ccd5aeb
PZ
8690static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
8691{
772c2a51 8692 if (IS_HASWELL(dev_priv))
9ccd5aeb
PZ
8693 return I915_READ(D_COMP_HSW);
8694 else
8695 return I915_READ(D_COMP_BDW);
8696}
8697
3c4c9b81
PZ
8698static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
8699{
772c2a51 8700 if (IS_HASWELL(dev_priv)) {
3c4c9b81
PZ
8701 mutex_lock(&dev_priv->rps.hw_lock);
8702 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
8703 val))
79cf219a 8704 DRM_DEBUG_KMS("Failed to write to D_COMP\n");
3c4c9b81
PZ
8705 mutex_unlock(&dev_priv->rps.hw_lock);
8706 } else {
9ccd5aeb
PZ
8707 I915_WRITE(D_COMP_BDW, val);
8708 POSTING_READ(D_COMP_BDW);
3c4c9b81 8709 }
be256dc7
PZ
8710}
8711
8712/*
8713 * This function implements pieces of two sequences from BSpec:
8714 * - Sequence for display software to disable LCPLL
8715 * - Sequence for display software to allow package C8+
8716 * The steps implemented here are just the steps that actually touch the LCPLL
8717 * register. Callers should take care of disabling all the display engine
8718 * functions, doing the mode unset, fixing interrupts, etc.
8719 */
6ff58d53
PZ
8720static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
8721 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
8722{
8723 uint32_t val;
8724
8725 assert_can_disable_lcpll(dev_priv);
8726
8727 val = I915_READ(LCPLL_CTL);
8728
8729 if (switch_to_fclk) {
8730 val |= LCPLL_CD_SOURCE_FCLK;
8731 I915_WRITE(LCPLL_CTL, val);
8732
f53dd63f
ID
8733 if (wait_for_us(I915_READ(LCPLL_CTL) &
8734 LCPLL_CD_SOURCE_FCLK_DONE, 1))
be256dc7
PZ
8735 DRM_ERROR("Switching to FCLK failed\n");
8736
8737 val = I915_READ(LCPLL_CTL);
8738 }
8739
8740 val |= LCPLL_PLL_DISABLE;
8741 I915_WRITE(LCPLL_CTL, val);
8742 POSTING_READ(LCPLL_CTL);
8743
24d8441d 8744 if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
be256dc7
PZ
8745 DRM_ERROR("LCPLL still locked\n");
8746
9ccd5aeb 8747 val = hsw_read_dcomp(dev_priv);
be256dc7 8748 val |= D_COMP_COMP_DISABLE;
3c4c9b81 8749 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
8750 ndelay(100);
8751
9ccd5aeb
PZ
8752 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
8753 1))
be256dc7
PZ
8754 DRM_ERROR("D_COMP RCOMP still in progress\n");
8755
8756 if (allow_power_down) {
8757 val = I915_READ(LCPLL_CTL);
8758 val |= LCPLL_POWER_DOWN_ALLOW;
8759 I915_WRITE(LCPLL_CTL, val);
8760 POSTING_READ(LCPLL_CTL);
8761 }
8762}
8763
8764/*
8765 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
8766 * source.
8767 */
6ff58d53 8768static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
8769{
8770 uint32_t val;
8771
8772 val = I915_READ(LCPLL_CTL);
8773
8774 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
8775 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
8776 return;
8777
a8a8bd54
PZ
8778 /*
8779 * Make sure we're not on PC8 state before disabling PC8, otherwise
8780 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
a8a8bd54 8781 */
59bad947 8782 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
215733fa 8783
be256dc7
PZ
8784 if (val & LCPLL_POWER_DOWN_ALLOW) {
8785 val &= ~LCPLL_POWER_DOWN_ALLOW;
8786 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 8787 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
8788 }
8789
9ccd5aeb 8790 val = hsw_read_dcomp(dev_priv);
be256dc7
PZ
8791 val |= D_COMP_COMP_FORCE;
8792 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 8793 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
8794
8795 val = I915_READ(LCPLL_CTL);
8796 val &= ~LCPLL_PLL_DISABLE;
8797 I915_WRITE(LCPLL_CTL, val);
8798
93220c08
CW
8799 if (intel_wait_for_register(dev_priv,
8800 LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
8801 5))
be256dc7
PZ
8802 DRM_ERROR("LCPLL not locked yet\n");
8803
8804 if (val & LCPLL_CD_SOURCE_FCLK) {
8805 val = I915_READ(LCPLL_CTL);
8806 val &= ~LCPLL_CD_SOURCE_FCLK;
8807 I915_WRITE(LCPLL_CTL, val);
8808
f53dd63f
ID
8809 if (wait_for_us((I915_READ(LCPLL_CTL) &
8810 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
be256dc7
PZ
8811 DRM_ERROR("Switching back to LCPLL failed\n");
8812 }
215733fa 8813
59bad947 8814 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4c75b940 8815 intel_update_cdclk(dev_priv);
be256dc7
PZ
8816}
8817
765dab67
PZ
8818/*
8819 * Package states C8 and deeper are really deep PC states that can only be
8820 * reached when all the devices on the system allow it, so even if the graphics
8821 * device allows PC8+, it doesn't mean the system will actually get to these
8822 * states. Our driver only allows PC8+ when going into runtime PM.
8823 *
8824 * The requirements for PC8+ are that all the outputs are disabled, the power
8825 * well is disabled and most interrupts are disabled, and these are also
8826 * requirements for runtime PM. When these conditions are met, we manually do
8827 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
8828 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
8829 * hang the machine.
8830 *
8831 * When we really reach PC8 or deeper states (not just when we allow it) we lose
8832 * the state of some registers, so when we come back from PC8+ we need to
8833 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
8834 * need to take care of the registers kept by RC6. Notice that this happens even
8835 * if we don't put the device in PCI D3 state (which is what currently happens
8836 * because of the runtime PM support).
8837 *
8838 * For more, read "Display Sequences for Package C8" on the hardware
8839 * documentation.
8840 */
a14cb6fc 8841void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 8842{
c67a470b
PZ
8843 uint32_t val;
8844
c67a470b
PZ
8845 DRM_DEBUG_KMS("Enabling package C8+\n");
8846
4f8036a2 8847 if (HAS_PCH_LPT_LP(dev_priv)) {
c67a470b
PZ
8848 val = I915_READ(SOUTH_DSPCLK_GATE_D);
8849 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
8850 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
8851 }
8852
c39055b0 8853 lpt_disable_clkout_dp(dev_priv);
c67a470b
PZ
8854 hsw_disable_lcpll(dev_priv, true, true);
8855}
8856
a14cb6fc 8857void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b 8858{
c67a470b
PZ
8859 uint32_t val;
8860
c67a470b
PZ
8861 DRM_DEBUG_KMS("Disabling package C8+\n");
8862
8863 hsw_restore_lcpll(dev_priv);
c39055b0 8864 lpt_init_pch_refclk(dev_priv);
c67a470b 8865
4f8036a2 8866 if (HAS_PCH_LPT_LP(dev_priv)) {
c67a470b
PZ
8867 val = I915_READ(SOUTH_DSPCLK_GATE_D);
8868 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
8869 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
8870 }
c67a470b
PZ
8871}
8872
190f68c5
ACO
8873static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
8874 struct intel_crtc_state *crtc_state)
09b4ddf9 8875{
d7edc4e5 8876 if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
44a126ba
PZ
8877 struct intel_encoder *encoder =
8878 intel_ddi_get_crtc_new_encoder(crtc_state);
8879
8880 if (!intel_get_shared_dpll(crtc, crtc_state, encoder)) {
8881 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
8882 pipe_name(crtc->pipe));
af3997b5 8883 return -EINVAL;
44a126ba 8884 }
af3997b5 8885 }
716c2e55 8886
c7653199 8887 crtc->lowfreq_avail = false;
644cef34 8888
c8f7a0db 8889 return 0;
79e53945
JB
8890}
8891
8b0f7e06
KM
8892static void cannonlake_get_ddi_pll(struct drm_i915_private *dev_priv,
8893 enum port port,
8894 struct intel_crtc_state *pipe_config)
8895{
8896 enum intel_dpll_id id;
8897 u32 temp;
8898
8899 temp = I915_READ(DPCLKA_CFGCR0) & DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(port);
8900 id = temp >> (port * 2);
8901
8902 if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL2))
8903 return;
8904
8905 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
8906}
8907
3760b59c
S
8908static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
8909 enum port port,
8910 struct intel_crtc_state *pipe_config)
8911{
8106ddbd
ACO
8912 enum intel_dpll_id id;
8913
3760b59c
S
8914 switch (port) {
8915 case PORT_A:
08250c4b 8916 id = DPLL_ID_SKL_DPLL0;
3760b59c
S
8917 break;
8918 case PORT_B:
08250c4b 8919 id = DPLL_ID_SKL_DPLL1;
3760b59c
S
8920 break;
8921 case PORT_C:
08250c4b 8922 id = DPLL_ID_SKL_DPLL2;
3760b59c
S
8923 break;
8924 default:
8925 DRM_ERROR("Incorrect port type\n");
8106ddbd 8926 return;
3760b59c 8927 }
8106ddbd
ACO
8928
8929 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
3760b59c
S
8930}
8931
96b7dfb7
S
8932static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
8933 enum port port,
5cec258b 8934 struct intel_crtc_state *pipe_config)
96b7dfb7 8935{
8106ddbd 8936 enum intel_dpll_id id;
a3c988ea 8937 u32 temp;
96b7dfb7
S
8938
8939 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
c856052a 8940 id = temp >> (port * 3 + 1);
96b7dfb7 8941
c856052a 8942 if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL3))
8106ddbd 8943 return;
8106ddbd
ACO
8944
8945 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
96b7dfb7
S
8946}
8947
7d2c8175
DL
8948static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
8949 enum port port,
5cec258b 8950 struct intel_crtc_state *pipe_config)
7d2c8175 8951{
8106ddbd 8952 enum intel_dpll_id id;
c856052a 8953 uint32_t ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
8106ddbd 8954
c856052a 8955 switch (ddi_pll_sel) {
7d2c8175 8956 case PORT_CLK_SEL_WRPLL1:
8106ddbd 8957 id = DPLL_ID_WRPLL1;
7d2c8175
DL
8958 break;
8959 case PORT_CLK_SEL_WRPLL2:
8106ddbd 8960 id = DPLL_ID_WRPLL2;
7d2c8175 8961 break;
00490c22 8962 case PORT_CLK_SEL_SPLL:
8106ddbd 8963 id = DPLL_ID_SPLL;
79bd23da 8964 break;
9d16da65
ACO
8965 case PORT_CLK_SEL_LCPLL_810:
8966 id = DPLL_ID_LCPLL_810;
8967 break;
8968 case PORT_CLK_SEL_LCPLL_1350:
8969 id = DPLL_ID_LCPLL_1350;
8970 break;
8971 case PORT_CLK_SEL_LCPLL_2700:
8972 id = DPLL_ID_LCPLL_2700;
8973 break;
8106ddbd 8974 default:
c856052a 8975 MISSING_CASE(ddi_pll_sel);
8106ddbd
ACO
8976 /* fall through */
8977 case PORT_CLK_SEL_NONE:
8106ddbd 8978 return;
7d2c8175 8979 }
8106ddbd
ACO
8980
8981 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
7d2c8175
DL
8982}
8983
cf30429e
JN
8984static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
8985 struct intel_crtc_state *pipe_config,
d8fc70b7 8986 u64 *power_domain_mask)
cf30429e
JN
8987{
8988 struct drm_device *dev = crtc->base.dev;
fac5e23e 8989 struct drm_i915_private *dev_priv = to_i915(dev);
cf30429e
JN
8990 enum intel_display_power_domain power_domain;
8991 u32 tmp;
8992
d9a7bc67
ID
8993 /*
8994 * The pipe->transcoder mapping is fixed with the exception of the eDP
8995 * transcoder handled below.
8996 */
cf30429e
JN
8997 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8998
8999 /*
9000 * XXX: Do intel_display_power_get_if_enabled before reading this (for
9001 * consistency and less surprising code; it's in always on power).
9002 */
9003 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9004 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9005 enum pipe trans_edp_pipe;
9006 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9007 default:
9008 WARN(1, "unknown pipe linked to edp transcoder\n");
9009 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9010 case TRANS_DDI_EDP_INPUT_A_ON:
9011 trans_edp_pipe = PIPE_A;
9012 break;
9013 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9014 trans_edp_pipe = PIPE_B;
9015 break;
9016 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9017 trans_edp_pipe = PIPE_C;
9018 break;
9019 }
9020
9021 if (trans_edp_pipe == crtc->pipe)
9022 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9023 }
9024
9025 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
9026 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9027 return false;
d8fc70b7 9028 *power_domain_mask |= BIT_ULL(power_domain);
cf30429e
JN
9029
9030 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
9031
9032 return tmp & PIPECONF_ENABLE;
9033}
9034
4d1de975
JN
9035static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
9036 struct intel_crtc_state *pipe_config,
d8fc70b7 9037 u64 *power_domain_mask)
4d1de975
JN
9038{
9039 struct drm_device *dev = crtc->base.dev;
fac5e23e 9040 struct drm_i915_private *dev_priv = to_i915(dev);
4d1de975
JN
9041 enum intel_display_power_domain power_domain;
9042 enum port port;
9043 enum transcoder cpu_transcoder;
9044 u32 tmp;
9045
4d1de975
JN
9046 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
9047 if (port == PORT_A)
9048 cpu_transcoder = TRANSCODER_DSI_A;
9049 else
9050 cpu_transcoder = TRANSCODER_DSI_C;
9051
9052 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
9053 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9054 continue;
d8fc70b7 9055 *power_domain_mask |= BIT_ULL(power_domain);
4d1de975 9056
db18b6a6
ID
9057 /*
9058 * The PLL needs to be enabled with a valid divider
9059 * configuration, otherwise accessing DSI registers will hang
9060 * the machine. See BSpec North Display Engine
9061 * registers/MIPI[BXT]. We can break out here early, since we
9062 * need the same DSI PLL to be enabled for both DSI ports.
9063 */
9064 if (!intel_dsi_pll_is_enabled(dev_priv))
9065 break;
9066
4d1de975
JN
9067 /* XXX: this works for video mode only */
9068 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
9069 if (!(tmp & DPI_ENABLE))
9070 continue;
9071
9072 tmp = I915_READ(MIPI_CTRL(port));
9073 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
9074 continue;
9075
9076 pipe_config->cpu_transcoder = cpu_transcoder;
4d1de975
JN
9077 break;
9078 }
9079
d7edc4e5 9080 return transcoder_is_dsi(pipe_config->cpu_transcoder);
4d1de975
JN
9081}
9082
26804afd 9083static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
5cec258b 9084 struct intel_crtc_state *pipe_config)
26804afd 9085{
6315b5d3 9086 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
d452c5b6 9087 struct intel_shared_dpll *pll;
26804afd
DV
9088 enum port port;
9089 uint32_t tmp;
9090
9091 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9092
9093 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9094
8b0f7e06
KM
9095 if (IS_CANNONLAKE(dev_priv))
9096 cannonlake_get_ddi_pll(dev_priv, port, pipe_config);
9097 else if (IS_GEN9_BC(dev_priv))
96b7dfb7 9098 skylake_get_ddi_pll(dev_priv, port, pipe_config);
cc3f90f0 9099 else if (IS_GEN9_LP(dev_priv))
3760b59c 9100 bxt_get_ddi_pll(dev_priv, port, pipe_config);
96b7dfb7
S
9101 else
9102 haswell_get_ddi_pll(dev_priv, port, pipe_config);
9cd86933 9103
8106ddbd
ACO
9104 pll = pipe_config->shared_dpll;
9105 if (pll) {
2edd6443
ACO
9106 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9107 &pipe_config->dpll_hw_state));
d452c5b6
DV
9108 }
9109
26804afd
DV
9110 /*
9111 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9112 * DDI E. So just check whether this pipe is wired to DDI E and whether
9113 * the PCH transcoder is on.
9114 */
6315b5d3 9115 if (INTEL_GEN(dev_priv) < 9 &&
ca370455 9116 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
26804afd
DV
9117 pipe_config->has_pch_encoder = true;
9118
9119 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9120 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9121 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9122
9123 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9124 }
9125}
9126
0e8ffe1b 9127static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9128 struct intel_crtc_state *pipe_config)
0e8ffe1b 9129{
6315b5d3 9130 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1729050e 9131 enum intel_display_power_domain power_domain;
d8fc70b7 9132 u64 power_domain_mask;
cf30429e 9133 bool active;
0e8ffe1b 9134
1729050e
ID
9135 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9136 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0 9137 return false;
d8fc70b7 9138 power_domain_mask = BIT_ULL(power_domain);
1729050e 9139
8106ddbd 9140 pipe_config->shared_dpll = NULL;
c0d43d62 9141
cf30429e 9142 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
eccb140b 9143
cc3f90f0 9144 if (IS_GEN9_LP(dev_priv) &&
d7edc4e5
VS
9145 bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
9146 WARN_ON(active);
9147 active = true;
4d1de975
JN
9148 }
9149
cf30429e 9150 if (!active)
1729050e 9151 goto out;
0e8ffe1b 9152
d7edc4e5 9153 if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
4d1de975
JN
9154 haswell_get_ddi_port_state(crtc, pipe_config);
9155 intel_get_pipe_timings(crtc, pipe_config);
9156 }
627eb5a3 9157
bc58be60 9158 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 9159
05dc698c
LL
9160 pipe_config->gamma_mode =
9161 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
9162
6315b5d3 9163 if (INTEL_GEN(dev_priv) >= 9) {
1c74eeaf 9164 intel_crtc_init_scalers(crtc, pipe_config);
a1b2278e 9165
af99ceda
CK
9166 pipe_config->scaler_state.scaler_id = -1;
9167 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9168 }
9169
1729050e
ID
9170 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
9171 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
d8fc70b7 9172 power_domain_mask |= BIT_ULL(power_domain);
6315b5d3 9173 if (INTEL_GEN(dev_priv) >= 9)
bd2e244f 9174 skylake_get_pfit_config(crtc, pipe_config);
ff6d9f55 9175 else
1c132b44 9176 ironlake_get_pfit_config(crtc, pipe_config);
bd2e244f 9177 }
88adfff1 9178
772c2a51 9179 if (IS_HASWELL(dev_priv))
e59150dc
JB
9180 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9181 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 9182
4d1de975
JN
9183 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
9184 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
ebb69c95
CT
9185 pipe_config->pixel_multiplier =
9186 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9187 } else {
9188 pipe_config->pixel_multiplier = 1;
9189 }
6c49f241 9190
1729050e
ID
9191out:
9192 for_each_power_domain(power_domain, power_domain_mask)
9193 intel_display_power_put(dev_priv, power_domain);
9194
cf30429e 9195 return active;
0e8ffe1b
DV
9196}
9197
cd5dcbf1 9198static u32 intel_cursor_base(const struct intel_plane_state *plane_state)
1cecc830
VS
9199{
9200 struct drm_i915_private *dev_priv =
9201 to_i915(plane_state->base.plane->dev);
9202 const struct drm_framebuffer *fb = plane_state->base.fb;
9203 const struct drm_i915_gem_object *obj = intel_fb_obj(fb);
9204 u32 base;
9205
9206 if (INTEL_INFO(dev_priv)->cursor_needs_physical)
9207 base = obj->phys_handle->busaddr;
9208 else
9209 base = intel_plane_ggtt_offset(plane_state);
9210
1e7b4fd8
VS
9211 base += plane_state->main.offset;
9212
1cecc830
VS
9213 /* ILK+ do this automagically */
9214 if (HAS_GMCH_DISPLAY(dev_priv) &&
a82256bc 9215 plane_state->base.rotation & DRM_MODE_ROTATE_180)
1cecc830
VS
9216 base += (plane_state->base.crtc_h *
9217 plane_state->base.crtc_w - 1) * fb->format->cpp[0];
9218
9219 return base;
9220}
9221
ed270223
VS
9222static u32 intel_cursor_position(const struct intel_plane_state *plane_state)
9223{
9224 int x = plane_state->base.crtc_x;
9225 int y = plane_state->base.crtc_y;
9226 u32 pos = 0;
9227
9228 if (x < 0) {
9229 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
9230 x = -x;
9231 }
9232 pos |= x << CURSOR_X_SHIFT;
9233
9234 if (y < 0) {
9235 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
9236 y = -y;
9237 }
9238 pos |= y << CURSOR_Y_SHIFT;
9239
9240 return pos;
9241}
9242
3637ecf0
VS
9243static bool intel_cursor_size_ok(const struct intel_plane_state *plane_state)
9244{
9245 const struct drm_mode_config *config =
9246 &plane_state->base.plane->dev->mode_config;
9247 int width = plane_state->base.crtc_w;
9248 int height = plane_state->base.crtc_h;
9249
9250 return width > 0 && width <= config->cursor_width &&
9251 height > 0 && height <= config->cursor_height;
9252}
9253
659056f2
VS
9254static int intel_check_cursor(struct intel_crtc_state *crtc_state,
9255 struct intel_plane_state *plane_state)
9256{
9257 const struct drm_framebuffer *fb = plane_state->base.fb;
1e7b4fd8
VS
9258 int src_x, src_y;
9259 u32 offset;
659056f2
VS
9260 int ret;
9261
9262 ret = drm_plane_helper_check_state(&plane_state->base,
9263 &plane_state->clip,
9264 DRM_PLANE_HELPER_NO_SCALING,
9265 DRM_PLANE_HELPER_NO_SCALING,
9266 true, true);
9267 if (ret)
9268 return ret;
9269
9270 if (!fb)
9271 return 0;
9272
9273 if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
9274 DRM_DEBUG_KMS("cursor cannot be tiled\n");
9275 return -EINVAL;
9276 }
9277
1e7b4fd8
VS
9278 src_x = plane_state->base.src_x >> 16;
9279 src_y = plane_state->base.src_y >> 16;
9280
9281 intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
9282 offset = intel_compute_tile_offset(&src_x, &src_y, plane_state, 0);
9283
9284 if (src_x != 0 || src_y != 0) {
9285 DRM_DEBUG_KMS("Arbitrary cursor panning not supported\n");
9286 return -EINVAL;
9287 }
9288
9289 plane_state->main.offset = offset;
9290
659056f2
VS
9291 return 0;
9292}
9293
292889e1
VS
9294static u32 i845_cursor_ctl(const struct intel_crtc_state *crtc_state,
9295 const struct intel_plane_state *plane_state)
9296{
1e1bb871 9297 const struct drm_framebuffer *fb = plane_state->base.fb;
292889e1 9298
292889e1
VS
9299 return CURSOR_ENABLE |
9300 CURSOR_GAMMA_ENABLE |
9301 CURSOR_FORMAT_ARGB |
1e1bb871 9302 CURSOR_STRIDE(fb->pitches[0]);
292889e1
VS
9303}
9304
659056f2
VS
9305static bool i845_cursor_size_ok(const struct intel_plane_state *plane_state)
9306{
659056f2 9307 int width = plane_state->base.crtc_w;
659056f2
VS
9308
9309 /*
9310 * 845g/865g are only limited by the width of their cursors,
9311 * the height is arbitrary up to the precision of the register.
9312 */
3637ecf0 9313 return intel_cursor_size_ok(plane_state) && IS_ALIGNED(width, 64);
659056f2
VS
9314}
9315
9316static int i845_check_cursor(struct intel_plane *plane,
9317 struct intel_crtc_state *crtc_state,
9318 struct intel_plane_state *plane_state)
9319{
9320 const struct drm_framebuffer *fb = plane_state->base.fb;
659056f2
VS
9321 int ret;
9322
9323 ret = intel_check_cursor(crtc_state, plane_state);
9324 if (ret)
9325 return ret;
9326
9327 /* if we want to turn off the cursor ignore width and height */
1e1bb871 9328 if (!fb)
659056f2
VS
9329 return 0;
9330
9331 /* Check for which cursor types we support */
9332 if (!i845_cursor_size_ok(plane_state)) {
9333 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
9334 plane_state->base.crtc_w,
9335 plane_state->base.crtc_h);
9336 return -EINVAL;
9337 }
9338
1e1bb871 9339 switch (fb->pitches[0]) {
292889e1
VS
9340 case 256:
9341 case 512:
9342 case 1024:
9343 case 2048:
9344 break;
1e1bb871
VS
9345 default:
9346 DRM_DEBUG_KMS("Invalid cursor stride (%u)\n",
9347 fb->pitches[0]);
9348 return -EINVAL;
292889e1
VS
9349 }
9350
659056f2
VS
9351 plane_state->ctl = i845_cursor_ctl(crtc_state, plane_state);
9352
9353 return 0;
292889e1
VS
9354}
9355
b2d03b0d
VS
9356static void i845_update_cursor(struct intel_plane *plane,
9357 const struct intel_crtc_state *crtc_state,
55a08b3f 9358 const struct intel_plane_state *plane_state)
560b85bb 9359{
cd5dcbf1 9360 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
b2d03b0d
VS
9361 u32 cntl = 0, base = 0, pos = 0, size = 0;
9362 unsigned long irqflags;
560b85bb 9363
936e71e3 9364 if (plane_state && plane_state->base.visible) {
55a08b3f
ML
9365 unsigned int width = plane_state->base.crtc_w;
9366 unsigned int height = plane_state->base.crtc_h;
dc41c154 9367
a0864d59 9368 cntl = plane_state->ctl;
dc41c154 9369 size = (height << 12) | width;
560b85bb 9370
b2d03b0d
VS
9371 base = intel_cursor_base(plane_state);
9372 pos = intel_cursor_position(plane_state);
4b0e333e 9373 }
560b85bb 9374
b2d03b0d 9375 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
4726e0b0 9376
e11ffddb
VS
9377 /* On these chipsets we can only modify the base/size/stride
9378 * whilst the cursor is disabled.
9379 */
9380 if (plane->cursor.base != base ||
9381 plane->cursor.size != size ||
9382 plane->cursor.cntl != cntl) {
dd584fc0 9383 I915_WRITE_FW(CURCNTR(PIPE_A), 0);
dd584fc0 9384 I915_WRITE_FW(CURBASE(PIPE_A), base);
dd584fc0 9385 I915_WRITE_FW(CURSIZE, size);
b2d03b0d 9386 I915_WRITE_FW(CURPOS(PIPE_A), pos);
dd584fc0 9387 I915_WRITE_FW(CURCNTR(PIPE_A), cntl);
75343a44 9388
e11ffddb
VS
9389 plane->cursor.base = base;
9390 plane->cursor.size = size;
9391 plane->cursor.cntl = cntl;
9392 } else {
9393 I915_WRITE_FW(CURPOS(PIPE_A), pos);
560b85bb 9394 }
e11ffddb 9395
75343a44 9396 POSTING_READ_FW(CURCNTR(PIPE_A));
b2d03b0d
VS
9397
9398 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
9399}
9400
9401static void i845_disable_cursor(struct intel_plane *plane,
9402 struct intel_crtc *crtc)
9403{
9404 i845_update_cursor(plane, NULL, NULL);
560b85bb
CW
9405}
9406
292889e1
VS
9407static u32 i9xx_cursor_ctl(const struct intel_crtc_state *crtc_state,
9408 const struct intel_plane_state *plane_state)
9409{
9410 struct drm_i915_private *dev_priv =
9411 to_i915(plane_state->base.plane->dev);
9412 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
292889e1
VS
9413 u32 cntl;
9414
9415 cntl = MCURSOR_GAMMA_ENABLE;
9416
9417 if (HAS_DDI(dev_priv))
9418 cntl |= CURSOR_PIPE_CSC_ENABLE;
9419
d509e28b 9420 cntl |= MCURSOR_PIPE_SELECT(crtc->pipe);
292889e1
VS
9421
9422 switch (plane_state->base.crtc_w) {
9423 case 64:
9424 cntl |= CURSOR_MODE_64_ARGB_AX;
9425 break;
9426 case 128:
9427 cntl |= CURSOR_MODE_128_ARGB_AX;
9428 break;
9429 case 256:
9430 cntl |= CURSOR_MODE_256_ARGB_AX;
9431 break;
9432 default:
9433 MISSING_CASE(plane_state->base.crtc_w);
9434 return 0;
9435 }
9436
c2c446ad 9437 if (plane_state->base.rotation & DRM_MODE_ROTATE_180)
292889e1
VS
9438 cntl |= CURSOR_ROTATE_180;
9439
9440 return cntl;
9441}
9442
659056f2 9443static bool i9xx_cursor_size_ok(const struct intel_plane_state *plane_state)
65a21cd6 9444{
024faac7
VS
9445 struct drm_i915_private *dev_priv =
9446 to_i915(plane_state->base.plane->dev);
659056f2
VS
9447 int width = plane_state->base.crtc_w;
9448 int height = plane_state->base.crtc_h;
4b0e333e 9449
3637ecf0 9450 if (!intel_cursor_size_ok(plane_state))
659056f2 9451 return false;
4398ad45 9452
024faac7
VS
9453 /* Cursor width is limited to a few power-of-two sizes */
9454 switch (width) {
659056f2
VS
9455 case 256:
9456 case 128:
659056f2
VS
9457 case 64:
9458 break;
9459 default:
9460 return false;
65a21cd6 9461 }
4b0e333e 9462
024faac7
VS
9463 /*
9464 * IVB+ have CUR_FBC_CTL which allows an arbitrary cursor
9465 * height from 8 lines up to the cursor width, when the
9466 * cursor is not rotated. Everything else requires square
9467 * cursors.
9468 */
9469 if (HAS_CUR_FBC(dev_priv) &&
a82256bc 9470 plane_state->base.rotation & DRM_MODE_ROTATE_0) {
024faac7
VS
9471 if (height < 8 || height > width)
9472 return false;
9473 } else {
9474 if (height != width)
9475 return false;
9476 }
99d1f387 9477
659056f2 9478 return true;
65a21cd6
JB
9479}
9480
659056f2
VS
9481static int i9xx_check_cursor(struct intel_plane *plane,
9482 struct intel_crtc_state *crtc_state,
9483 struct intel_plane_state *plane_state)
cda4b7d3 9484{
659056f2
VS
9485 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
9486 const struct drm_framebuffer *fb = plane_state->base.fb;
659056f2 9487 enum pipe pipe = plane->pipe;
659056f2 9488 int ret;
cda4b7d3 9489
659056f2
VS
9490 ret = intel_check_cursor(crtc_state, plane_state);
9491 if (ret)
9492 return ret;
cda4b7d3 9493
659056f2 9494 /* if we want to turn off the cursor ignore width and height */
1e1bb871 9495 if (!fb)
659056f2 9496 return 0;
55a08b3f 9497
659056f2
VS
9498 /* Check for which cursor types we support */
9499 if (!i9xx_cursor_size_ok(plane_state)) {
9500 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
9501 plane_state->base.crtc_w,
9502 plane_state->base.crtc_h);
9503 return -EINVAL;
cda4b7d3 9504 }
cda4b7d3 9505
1e1bb871
VS
9506 if (fb->pitches[0] != plane_state->base.crtc_w * fb->format->cpp[0]) {
9507 DRM_DEBUG_KMS("Invalid cursor stride (%u) (cursor width %d)\n",
9508 fb->pitches[0], plane_state->base.crtc_w);
9509 return -EINVAL;
659056f2 9510 }
dd584fc0 9511
659056f2
VS
9512 /*
9513 * There's something wrong with the cursor on CHV pipe C.
9514 * If it straddles the left edge of the screen then
9515 * moving it away from the edge or disabling it often
9516 * results in a pipe underrun, and often that can lead to
9517 * dead pipe (constant underrun reported, and it scans
9518 * out just a solid color). To recover from that, the
9519 * display power well must be turned off and on again.
9520 * Refuse the put the cursor into that compromised position.
9521 */
9522 if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_C &&
9523 plane_state->base.visible && plane_state->base.crtc_x < 0) {
9524 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
9525 return -EINVAL;
9526 }
5efb3e28 9527
659056f2 9528 plane_state->ctl = i9xx_cursor_ctl(crtc_state, plane_state);
dd584fc0 9529
659056f2 9530 return 0;
cda4b7d3
CW
9531}
9532
b2d03b0d
VS
9533static void i9xx_update_cursor(struct intel_plane *plane,
9534 const struct intel_crtc_state *crtc_state,
55a08b3f 9535 const struct intel_plane_state *plane_state)
dc41c154 9536{
cd5dcbf1
VS
9537 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
9538 enum pipe pipe = plane->pipe;
024faac7 9539 u32 cntl = 0, base = 0, pos = 0, fbc_ctl = 0;
b2d03b0d 9540 unsigned long irqflags;
dc41c154 9541
b2d03b0d 9542 if (plane_state && plane_state->base.visible) {
a0864d59 9543 cntl = plane_state->ctl;
dc41c154 9544
024faac7
VS
9545 if (plane_state->base.crtc_h != plane_state->base.crtc_w)
9546 fbc_ctl = CUR_FBC_CTL_EN | (plane_state->base.crtc_h - 1);
dc41c154 9547
b2d03b0d
VS
9548 base = intel_cursor_base(plane_state);
9549 pos = intel_cursor_position(plane_state);
9550 }
9551
9552 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
9553
e11ffddb
VS
9554 /*
9555 * On some platforms writing CURCNTR first will also
9556 * cause CURPOS to be armed by the CURBASE write.
9557 * Without the CURCNTR write the CURPOS write would
9558 * arm itself.
9559 *
9560 * CURCNTR and CUR_FBC_CTL are always
9561 * armed by the CURBASE write only.
9562 */
9563 if (plane->cursor.base != base ||
9564 plane->cursor.size != fbc_ctl ||
9565 plane->cursor.cntl != cntl) {
dd584fc0 9566 I915_WRITE_FW(CURCNTR(pipe), cntl);
e11ffddb
VS
9567 if (HAS_CUR_FBC(dev_priv))
9568 I915_WRITE_FW(CUR_FBC_CTL(pipe), fbc_ctl);
b2d03b0d 9569 I915_WRITE_FW(CURPOS(pipe), pos);
75343a44
VS
9570 I915_WRITE_FW(CURBASE(pipe), base);
9571
e11ffddb
VS
9572 plane->cursor.base = base;
9573 plane->cursor.size = fbc_ctl;
9574 plane->cursor.cntl = cntl;
dc41c154 9575 } else {
e11ffddb 9576 I915_WRITE_FW(CURPOS(pipe), pos);
dc41c154
VS
9577 }
9578
dd584fc0 9579 POSTING_READ_FW(CURBASE(pipe));
99d1f387 9580
b2d03b0d 9581 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
dc41c154
VS
9582}
9583
b2d03b0d
VS
9584static void i9xx_disable_cursor(struct intel_plane *plane,
9585 struct intel_crtc *crtc)
cda4b7d3 9586{
b2d03b0d 9587 i9xx_update_cursor(plane, NULL, NULL);
dc41c154
VS
9588}
9589
dc41c154 9590
79e53945
JB
9591/* VESA 640x480x72Hz mode to set on the pipe */
9592static struct drm_display_mode load_detect_mode = {
9593 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
9594 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
9595};
9596
a8bb6818 9597struct drm_framebuffer *
24dbf51a
CW
9598intel_framebuffer_create(struct drm_i915_gem_object *obj,
9599 struct drm_mode_fb_cmd2 *mode_cmd)
d2dff872
CW
9600{
9601 struct intel_framebuffer *intel_fb;
9602 int ret;
9603
9604 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
dcb1394e 9605 if (!intel_fb)
d2dff872 9606 return ERR_PTR(-ENOMEM);
d2dff872 9607
24dbf51a 9608 ret = intel_framebuffer_init(intel_fb, obj, mode_cmd);
dd4916c5
DV
9609 if (ret)
9610 goto err;
d2dff872
CW
9611
9612 return &intel_fb->base;
dcb1394e 9613
dd4916c5 9614err:
dd4916c5 9615 kfree(intel_fb);
dd4916c5 9616 return ERR_PTR(ret);
d2dff872
CW
9617}
9618
9619static u32
9620intel_framebuffer_pitch_for_width(int width, int bpp)
9621{
9622 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
9623 return ALIGN(pitch, 64);
9624}
9625
9626static u32
9627intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
9628{
9629 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
1267a26b 9630 return PAGE_ALIGN(pitch * mode->vdisplay);
d2dff872
CW
9631}
9632
9633static struct drm_framebuffer *
9634intel_framebuffer_create_for_mode(struct drm_device *dev,
9635 struct drm_display_mode *mode,
9636 int depth, int bpp)
9637{
dcb1394e 9638 struct drm_framebuffer *fb;
d2dff872 9639 struct drm_i915_gem_object *obj;
0fed39bd 9640 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872 9641
12d79d78 9642 obj = i915_gem_object_create(to_i915(dev),
d2dff872 9643 intel_framebuffer_size_for_mode(mode, bpp));
fe3db79b
CW
9644 if (IS_ERR(obj))
9645 return ERR_CAST(obj);
d2dff872
CW
9646
9647 mode_cmd.width = mode->hdisplay;
9648 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
9649 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
9650 bpp);
5ca0c34a 9651 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872 9652
24dbf51a 9653 fb = intel_framebuffer_create(obj, &mode_cmd);
dcb1394e 9654 if (IS_ERR(fb))
f0cd5182 9655 i915_gem_object_put(obj);
dcb1394e
LW
9656
9657 return fb;
d2dff872
CW
9658}
9659
9660static struct drm_framebuffer *
9661mode_fits_in_fbdev(struct drm_device *dev,
9662 struct drm_display_mode *mode)
9663{
0695726e 9664#ifdef CONFIG_DRM_FBDEV_EMULATION
fac5e23e 9665 struct drm_i915_private *dev_priv = to_i915(dev);
d2dff872
CW
9666 struct drm_i915_gem_object *obj;
9667 struct drm_framebuffer *fb;
9668
4c0e5528 9669 if (!dev_priv->fbdev)
d2dff872
CW
9670 return NULL;
9671
4c0e5528 9672 if (!dev_priv->fbdev->fb)
d2dff872
CW
9673 return NULL;
9674
4c0e5528
DV
9675 obj = dev_priv->fbdev->fb->obj;
9676 BUG_ON(!obj);
9677
8bcd4553 9678 fb = &dev_priv->fbdev->fb->base;
01f2c773 9679 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
272725c7 9680 fb->format->cpp[0] * 8))
d2dff872
CW
9681 return NULL;
9682
01f2c773 9683 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
9684 return NULL;
9685
edde3617 9686 drm_framebuffer_reference(fb);
d2dff872 9687 return fb;
4520f53a
DV
9688#else
9689 return NULL;
9690#endif
d2dff872
CW
9691}
9692
d3a40d1b
ACO
9693static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
9694 struct drm_crtc *crtc,
9695 struct drm_display_mode *mode,
9696 struct drm_framebuffer *fb,
9697 int x, int y)
9698{
9699 struct drm_plane_state *plane_state;
9700 int hdisplay, vdisplay;
9701 int ret;
9702
9703 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
9704 if (IS_ERR(plane_state))
9705 return PTR_ERR(plane_state);
9706
9707 if (mode)
196cd5d3 9708 drm_mode_get_hv_timing(mode, &hdisplay, &vdisplay);
d3a40d1b
ACO
9709 else
9710 hdisplay = vdisplay = 0;
9711
9712 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
9713 if (ret)
9714 return ret;
9715 drm_atomic_set_fb_for_plane(plane_state, fb);
9716 plane_state->crtc_x = 0;
9717 plane_state->crtc_y = 0;
9718 plane_state->crtc_w = hdisplay;
9719 plane_state->crtc_h = vdisplay;
9720 plane_state->src_x = x << 16;
9721 plane_state->src_y = y << 16;
9722 plane_state->src_w = hdisplay << 16;
9723 plane_state->src_h = vdisplay << 16;
9724
9725 return 0;
9726}
9727
6c5ed5ae
ML
9728int intel_get_load_detect_pipe(struct drm_connector *connector,
9729 struct drm_display_mode *mode,
9730 struct intel_load_detect_pipe *old,
9731 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
9732{
9733 struct intel_crtc *intel_crtc;
d2434ab7
DV
9734 struct intel_encoder *intel_encoder =
9735 intel_attached_encoder(connector);
79e53945 9736 struct drm_crtc *possible_crtc;
4ef69c7a 9737 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
9738 struct drm_crtc *crtc = NULL;
9739 struct drm_device *dev = encoder->dev;
0f0f74bc 9740 struct drm_i915_private *dev_priv = to_i915(dev);
94352cf9 9741 struct drm_framebuffer *fb;
51fd371b 9742 struct drm_mode_config *config = &dev->mode_config;
edde3617 9743 struct drm_atomic_state *state = NULL, *restore_state = NULL;
944b0c76 9744 struct drm_connector_state *connector_state;
4be07317 9745 struct intel_crtc_state *crtc_state;
51fd371b 9746 int ret, i = -1;
79e53945 9747
d2dff872 9748 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 9749 connector->base.id, connector->name,
8e329a03 9750 encoder->base.id, encoder->name);
d2dff872 9751
edde3617
ML
9752 old->restore_state = NULL;
9753
6c5ed5ae 9754 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
6e9f798d 9755
79e53945
JB
9756 /*
9757 * Algorithm gets a little messy:
7a5e4805 9758 *
79e53945
JB
9759 * - if the connector already has an assigned crtc, use it (but make
9760 * sure it's on first)
7a5e4805 9761 *
79e53945
JB
9762 * - try to find the first unused crtc that can drive this connector,
9763 * and use that if we find one
79e53945
JB
9764 */
9765
9766 /* See if we already have a CRTC for this connector */
edde3617
ML
9767 if (connector->state->crtc) {
9768 crtc = connector->state->crtc;
8261b191 9769
51fd371b 9770 ret = drm_modeset_lock(&crtc->mutex, ctx);
4d02e2de 9771 if (ret)
ad3c558f 9772 goto fail;
8261b191
CW
9773
9774 /* Make sure the crtc and connector are running */
edde3617 9775 goto found;
79e53945
JB
9776 }
9777
9778 /* Find an unused one (if possible) */
70e1e0ec 9779 for_each_crtc(dev, possible_crtc) {
79e53945
JB
9780 i++;
9781 if (!(encoder->possible_crtcs & (1 << i)))
9782 continue;
edde3617
ML
9783
9784 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
9785 if (ret)
9786 goto fail;
9787
9788 if (possible_crtc->state->enable) {
9789 drm_modeset_unlock(&possible_crtc->mutex);
a459249c 9790 continue;
edde3617 9791 }
a459249c
VS
9792
9793 crtc = possible_crtc;
9794 break;
79e53945
JB
9795 }
9796
9797 /*
9798 * If we didn't find an unused CRTC, don't use any.
9799 */
9800 if (!crtc) {
7173188d 9801 DRM_DEBUG_KMS("no pipe available for load-detect\n");
f4bf77b4 9802 ret = -ENODEV;
ad3c558f 9803 goto fail;
79e53945
JB
9804 }
9805
edde3617
ML
9806found:
9807 intel_crtc = to_intel_crtc(crtc);
9808
4d02e2de
DV
9809 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
9810 if (ret)
ad3c558f 9811 goto fail;
79e53945 9812
83a57153 9813 state = drm_atomic_state_alloc(dev);
edde3617
ML
9814 restore_state = drm_atomic_state_alloc(dev);
9815 if (!state || !restore_state) {
9816 ret = -ENOMEM;
9817 goto fail;
9818 }
83a57153
ACO
9819
9820 state->acquire_ctx = ctx;
edde3617 9821 restore_state->acquire_ctx = ctx;
83a57153 9822
944b0c76
ACO
9823 connector_state = drm_atomic_get_connector_state(state, connector);
9824 if (IS_ERR(connector_state)) {
9825 ret = PTR_ERR(connector_state);
9826 goto fail;
9827 }
9828
edde3617
ML
9829 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
9830 if (ret)
9831 goto fail;
944b0c76 9832
4be07317
ACO
9833 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
9834 if (IS_ERR(crtc_state)) {
9835 ret = PTR_ERR(crtc_state);
9836 goto fail;
9837 }
9838
49d6fa21 9839 crtc_state->base.active = crtc_state->base.enable = true;
4be07317 9840
6492711d
CW
9841 if (!mode)
9842 mode = &load_detect_mode;
79e53945 9843
d2dff872
CW
9844 /* We need a framebuffer large enough to accommodate all accesses
9845 * that the plane may generate whilst we perform load detection.
9846 * We can not rely on the fbcon either being present (we get called
9847 * during its initialisation to detect all boot displays, or it may
9848 * not even exist) or that it is large enough to satisfy the
9849 * requested mode.
9850 */
94352cf9
DV
9851 fb = mode_fits_in_fbdev(dev, mode);
9852 if (fb == NULL) {
d2dff872 9853 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9 9854 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
d2dff872
CW
9855 } else
9856 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 9857 if (IS_ERR(fb)) {
d2dff872 9858 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
f4bf77b4 9859 ret = PTR_ERR(fb);
412b61d8 9860 goto fail;
79e53945 9861 }
79e53945 9862
d3a40d1b
ACO
9863 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
9864 if (ret)
9865 goto fail;
9866
edde3617
ML
9867 drm_framebuffer_unreference(fb);
9868
9869 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
9870 if (ret)
9871 goto fail;
9872
9873 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
9874 if (!ret)
9875 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
9876 if (!ret)
9877 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
9878 if (ret) {
9879 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
9880 goto fail;
9881 }
8c7b5ccb 9882
3ba86073
ML
9883 ret = drm_atomic_commit(state);
9884 if (ret) {
6492711d 9885 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
412b61d8 9886 goto fail;
79e53945 9887 }
edde3617
ML
9888
9889 old->restore_state = restore_state;
7abbd11f 9890 drm_atomic_state_put(state);
7173188d 9891
79e53945 9892 /* let the connector get through one full cycle before testing */
0f0f74bc 9893 intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
7173188d 9894 return true;
412b61d8 9895
ad3c558f 9896fail:
7fb71c8f
CW
9897 if (state) {
9898 drm_atomic_state_put(state);
9899 state = NULL;
9900 }
9901 if (restore_state) {
9902 drm_atomic_state_put(restore_state);
9903 restore_state = NULL;
9904 }
83a57153 9905
6c5ed5ae
ML
9906 if (ret == -EDEADLK)
9907 return ret;
51fd371b 9908
412b61d8 9909 return false;
79e53945
JB
9910}
9911
d2434ab7 9912void intel_release_load_detect_pipe(struct drm_connector *connector,
49172fee
ACO
9913 struct intel_load_detect_pipe *old,
9914 struct drm_modeset_acquire_ctx *ctx)
79e53945 9915{
d2434ab7
DV
9916 struct intel_encoder *intel_encoder =
9917 intel_attached_encoder(connector);
4ef69c7a 9918 struct drm_encoder *encoder = &intel_encoder->base;
edde3617 9919 struct drm_atomic_state *state = old->restore_state;
d3a40d1b 9920 int ret;
79e53945 9921
d2dff872 9922 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 9923 connector->base.id, connector->name,
8e329a03 9924 encoder->base.id, encoder->name);
d2dff872 9925
edde3617 9926 if (!state)
0622a53c 9927 return;
79e53945 9928
581e49fe 9929 ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
0853695c 9930 if (ret)
edde3617 9931 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
0853695c 9932 drm_atomic_state_put(state);
79e53945
JB
9933}
9934
da4a1efa 9935static int i9xx_pll_refclk(struct drm_device *dev,
5cec258b 9936 const struct intel_crtc_state *pipe_config)
da4a1efa 9937{
fac5e23e 9938 struct drm_i915_private *dev_priv = to_i915(dev);
da4a1efa
VS
9939 u32 dpll = pipe_config->dpll_hw_state.dpll;
9940
9941 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 9942 return dev_priv->vbt.lvds_ssc_freq;
6e266956 9943 else if (HAS_PCH_SPLIT(dev_priv))
da4a1efa 9944 return 120000;
5db94019 9945 else if (!IS_GEN2(dev_priv))
da4a1efa
VS
9946 return 96000;
9947 else
9948 return 48000;
9949}
9950
79e53945 9951/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc 9952static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 9953 struct intel_crtc_state *pipe_config)
79e53945 9954{
f1f644dc 9955 struct drm_device *dev = crtc->base.dev;
fac5e23e 9956 struct drm_i915_private *dev_priv = to_i915(dev);
f1f644dc 9957 int pipe = pipe_config->cpu_transcoder;
293623f7 9958 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945 9959 u32 fp;
9e2c8475 9960 struct dpll clock;
dccbea3b 9961 int port_clock;
da4a1efa 9962 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
9963
9964 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 9965 fp = pipe_config->dpll_hw_state.fp0;
79e53945 9966 else
293623f7 9967 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
9968
9969 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
9b1e14f4 9970 if (IS_PINEVIEW(dev_priv)) {
f2b115e6
AJ
9971 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
9972 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
9973 } else {
9974 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
9975 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
9976 }
9977
5db94019 9978 if (!IS_GEN2(dev_priv)) {
9b1e14f4 9979 if (IS_PINEVIEW(dev_priv))
f2b115e6
AJ
9980 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
9981 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
9982 else
9983 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
9984 DPLL_FPA01_P1_POST_DIV_SHIFT);
9985
9986 switch (dpll & DPLL_MODE_MASK) {
9987 case DPLLB_MODE_DAC_SERIAL:
9988 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
9989 5 : 10;
9990 break;
9991 case DPLLB_MODE_LVDS:
9992 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
9993 7 : 14;
9994 break;
9995 default:
28c97730 9996 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 9997 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 9998 return;
79e53945
JB
9999 }
10000
9b1e14f4 10001 if (IS_PINEVIEW(dev_priv))
dccbea3b 10002 port_clock = pnv_calc_dpll_params(refclk, &clock);
ac58c3f0 10003 else
dccbea3b 10004 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945 10005 } else {
50a0bc90 10006 u32 lvds = IS_I830(dev_priv) ? 0 : I915_READ(LVDS);
b1c560d1 10007 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
10008
10009 if (is_lvds) {
10010 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10011 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
10012
10013 if (lvds & LVDS_CLKB_POWER_UP)
10014 clock.p2 = 7;
10015 else
10016 clock.p2 = 14;
79e53945
JB
10017 } else {
10018 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10019 clock.p1 = 2;
10020 else {
10021 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10022 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10023 }
10024 if (dpll & PLL_P2_DIVIDE_BY_4)
10025 clock.p2 = 4;
10026 else
10027 clock.p2 = 2;
79e53945 10028 }
da4a1efa 10029
dccbea3b 10030 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945
JB
10031 }
10032
18442d08
VS
10033 /*
10034 * This value includes pixel_multiplier. We will use
241bfc38 10035 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
10036 * encoder's get_config() function.
10037 */
dccbea3b 10038 pipe_config->port_clock = port_clock;
f1f644dc
JB
10039}
10040
6878da05
VS
10041int intel_dotclock_calculate(int link_freq,
10042 const struct intel_link_m_n *m_n)
f1f644dc 10043{
f1f644dc
JB
10044 /*
10045 * The calculation for the data clock is:
1041a02f 10046 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 10047 * But we want to avoid losing precison if possible, so:
1041a02f 10048 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
10049 *
10050 * and the link clock is simpler:
1041a02f 10051 * link_clock = (m * link_clock) / n
f1f644dc
JB
10052 */
10053
6878da05
VS
10054 if (!m_n->link_n)
10055 return 0;
f1f644dc 10056
6878da05
VS
10057 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10058}
f1f644dc 10059
18442d08 10060static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 10061 struct intel_crtc_state *pipe_config)
6878da05 10062{
e3b247da 10063 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
79e53945 10064
18442d08
VS
10065 /* read out port_clock from the DPLL */
10066 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 10067
f1f644dc 10068 /*
e3b247da
VS
10069 * In case there is an active pipe without active ports,
10070 * we may need some idea for the dotclock anyway.
10071 * Calculate one based on the FDI configuration.
79e53945 10072 */
2d112de7 10073 pipe_config->base.adjusted_mode.crtc_clock =
21a727b3 10074 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
18442d08 10075 &pipe_config->fdi_m_n);
79e53945
JB
10076}
10077
10078/** Returns the currently programmed mode of the given pipe. */
10079struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10080 struct drm_crtc *crtc)
10081{
fac5e23e 10082 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 10083 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 10084 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
79e53945 10085 struct drm_display_mode *mode;
3f36b937 10086 struct intel_crtc_state *pipe_config;
fe2b8f9d
PZ
10087 int htot = I915_READ(HTOTAL(cpu_transcoder));
10088 int hsync = I915_READ(HSYNC(cpu_transcoder));
10089 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10090 int vsync = I915_READ(VSYNC(cpu_transcoder));
293623f7 10091 enum pipe pipe = intel_crtc->pipe;
79e53945
JB
10092
10093 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10094 if (!mode)
10095 return NULL;
10096
3f36b937
TU
10097 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10098 if (!pipe_config) {
10099 kfree(mode);
10100 return NULL;
10101 }
10102
f1f644dc
JB
10103 /*
10104 * Construct a pipe_config sufficient for getting the clock info
10105 * back out of crtc_clock_get.
10106 *
10107 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10108 * to use a real value here instead.
10109 */
3f36b937
TU
10110 pipe_config->cpu_transcoder = (enum transcoder) pipe;
10111 pipe_config->pixel_multiplier = 1;
10112 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10113 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10114 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
10115 i9xx_crtc_clock_get(intel_crtc, pipe_config);
10116
10117 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
79e53945
JB
10118 mode->hdisplay = (htot & 0xffff) + 1;
10119 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10120 mode->hsync_start = (hsync & 0xffff) + 1;
10121 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10122 mode->vdisplay = (vtot & 0xffff) + 1;
10123 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10124 mode->vsync_start = (vsync & 0xffff) + 1;
10125 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10126
10127 drm_mode_set_name(mode);
79e53945 10128
3f36b937
TU
10129 kfree(pipe_config);
10130
79e53945
JB
10131 return mode;
10132}
10133
10134static void intel_crtc_destroy(struct drm_crtc *crtc)
10135{
10136 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
67e77c5a 10137 struct drm_device *dev = crtc->dev;
51cbaf01 10138 struct intel_flip_work *work;
67e77c5a 10139
5e2d7afc 10140 spin_lock_irq(&dev->event_lock);
5a21b665
DV
10141 work = intel_crtc->flip_work;
10142 intel_crtc->flip_work = NULL;
10143 spin_unlock_irq(&dev->event_lock);
67e77c5a 10144
5a21b665 10145 if (work) {
51cbaf01
ML
10146 cancel_work_sync(&work->mmio_work);
10147 cancel_work_sync(&work->unpin_work);
5a21b665 10148 kfree(work);
67e77c5a 10149 }
79e53945
JB
10150
10151 drm_crtc_cleanup(crtc);
67e77c5a 10152
79e53945
JB
10153 kfree(intel_crtc);
10154}
10155
6b95a207
KH
10156static void intel_unpin_work_fn(struct work_struct *__work)
10157{
51cbaf01
ML
10158 struct intel_flip_work *work =
10159 container_of(__work, struct intel_flip_work, unpin_work);
5a21b665
DV
10160 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10161 struct drm_device *dev = crtc->base.dev;
10162 struct drm_plane *primary = crtc->base.primary;
03f476e1 10163
5a21b665
DV
10164 if (is_mmio_work(work))
10165 flush_work(&work->mmio_work);
03f476e1 10166
5a21b665 10167 mutex_lock(&dev->struct_mutex);
be1e3415 10168 intel_unpin_fb_vma(work->old_vma);
f8c417cd 10169 i915_gem_object_put(work->pending_flip_obj);
5a21b665 10170 mutex_unlock(&dev->struct_mutex);
143f73b3 10171
e8a261ea
CW
10172 i915_gem_request_put(work->flip_queued_req);
10173
5748b6a1
CW
10174 intel_frontbuffer_flip_complete(to_i915(dev),
10175 to_intel_plane(primary)->frontbuffer_bit);
5a21b665
DV
10176 intel_fbc_post_update(crtc);
10177 drm_framebuffer_unreference(work->old_fb);
143f73b3 10178
5a21b665
DV
10179 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
10180 atomic_dec(&crtc->unpin_work_count);
a6747b73 10181
5a21b665
DV
10182 kfree(work);
10183}
d9e86c0e 10184
5a21b665
DV
10185/* Is 'a' after or equal to 'b'? */
10186static bool g4x_flip_count_after_eq(u32 a, u32 b)
10187{
10188 return !((a - b) & 0x80000000);
10189}
143f73b3 10190
5a21b665
DV
10191static bool __pageflip_finished_cs(struct intel_crtc *crtc,
10192 struct intel_flip_work *work)
10193{
10194 struct drm_device *dev = crtc->base.dev;
fac5e23e 10195 struct drm_i915_private *dev_priv = to_i915(dev);
143f73b3 10196
8af29b0c 10197 if (abort_flip_on_reset(crtc))
5a21b665 10198 return true;
143f73b3 10199
5a21b665
DV
10200 /*
10201 * The relevant registers doen't exist on pre-ctg.
10202 * As the flip done interrupt doesn't trigger for mmio
10203 * flips on gmch platforms, a flip count check isn't
10204 * really needed there. But since ctg has the registers,
10205 * include it in the check anyway.
10206 */
9beb5fea 10207 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
5a21b665 10208 return true;
b4a98e57 10209
5a21b665
DV
10210 /*
10211 * BDW signals flip done immediately if the plane
10212 * is disabled, even if the plane enable is already
10213 * armed to occur at the next vblank :(
10214 */
f99d7069 10215
5a21b665
DV
10216 /*
10217 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10218 * used the same base address. In that case the mmio flip might
10219 * have completed, but the CS hasn't even executed the flip yet.
10220 *
10221 * A flip count check isn't enough as the CS might have updated
10222 * the base address just after start of vblank, but before we
10223 * managed to process the interrupt. This means we'd complete the
10224 * CS flip too soon.
10225 *
10226 * Combining both checks should get us a good enough result. It may
10227 * still happen that the CS flip has been executed, but has not
10228 * yet actually completed. But in case the base address is the same
10229 * anyway, we don't really care.
10230 */
10231 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10232 crtc->flip_work->gtt_offset &&
10233 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
10234 crtc->flip_work->flip_count);
10235}
b4a98e57 10236
5a21b665
DV
10237static bool
10238__pageflip_finished_mmio(struct intel_crtc *crtc,
10239 struct intel_flip_work *work)
10240{
10241 /*
10242 * MMIO work completes when vblank is different from
10243 * flip_queued_vblank.
10244 *
10245 * Reset counter value doesn't matter, this is handled by
10246 * i915_wait_request finishing early, so no need to handle
10247 * reset here.
10248 */
10249 return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
6b95a207
KH
10250}
10251
51cbaf01
ML
10252
10253static bool pageflip_finished(struct intel_crtc *crtc,
10254 struct intel_flip_work *work)
10255{
10256 if (!atomic_read(&work->pending))
10257 return false;
10258
10259 smp_rmb();
10260
5a21b665
DV
10261 if (is_mmio_work(work))
10262 return __pageflip_finished_mmio(crtc, work);
10263 else
10264 return __pageflip_finished_cs(crtc, work);
10265}
10266
10267void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
10268{
91c8a326 10269 struct drm_device *dev = &dev_priv->drm;
98187836 10270 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
5a21b665
DV
10271 struct intel_flip_work *work;
10272 unsigned long flags;
10273
10274 /* Ignore early vblank irqs */
10275 if (!crtc)
10276 return;
10277
51cbaf01 10278 /*
5a21b665
DV
10279 * This is called both by irq handlers and the reset code (to complete
10280 * lost pageflips) so needs the full irqsave spinlocks.
51cbaf01 10281 */
5a21b665 10282 spin_lock_irqsave(&dev->event_lock, flags);
e2af48c6 10283 work = crtc->flip_work;
5a21b665
DV
10284
10285 if (work != NULL &&
10286 !is_mmio_work(work) &&
e2af48c6
VS
10287 pageflip_finished(crtc, work))
10288 page_flip_completed(crtc);
5a21b665
DV
10289
10290 spin_unlock_irqrestore(&dev->event_lock, flags);
75f7f3ec
VS
10291}
10292
51cbaf01 10293void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
6b95a207 10294{
91c8a326 10295 struct drm_device *dev = &dev_priv->drm;
98187836 10296 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
51cbaf01 10297 struct intel_flip_work *work;
6b95a207
KH
10298 unsigned long flags;
10299
5251f04e
ML
10300 /* Ignore early vblank irqs */
10301 if (!crtc)
10302 return;
f326038a
DV
10303
10304 /*
10305 * This is called both by irq handlers and the reset code (to complete
10306 * lost pageflips) so needs the full irqsave spinlocks.
e7d841ca 10307 */
6b95a207 10308 spin_lock_irqsave(&dev->event_lock, flags);
e2af48c6 10309 work = crtc->flip_work;
5251f04e 10310
5a21b665
DV
10311 if (work != NULL &&
10312 is_mmio_work(work) &&
e2af48c6
VS
10313 pageflip_finished(crtc, work))
10314 page_flip_completed(crtc);
5251f04e 10315
6b95a207
KH
10316 spin_unlock_irqrestore(&dev->event_lock, flags);
10317}
10318
5a21b665
DV
10319static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
10320 struct intel_flip_work *work)
84c33a64 10321{
5a21b665 10322 work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
84c33a64 10323
5a21b665
DV
10324 /* Ensure that the work item is consistent when activating it ... */
10325 smp_mb__before_atomic();
10326 atomic_set(&work->pending, 1);
10327}
a6747b73 10328
5a21b665
DV
10329static int intel_gen2_queue_flip(struct drm_device *dev,
10330 struct drm_crtc *crtc,
10331 struct drm_framebuffer *fb,
10332 struct drm_i915_gem_object *obj,
10333 struct drm_i915_gem_request *req,
10334 uint32_t flags)
10335{
5a21b665 10336 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
73dec95e 10337 u32 flip_mask, *cs;
143f73b3 10338
73dec95e
TU
10339 cs = intel_ring_begin(req, 6);
10340 if (IS_ERR(cs))
10341 return PTR_ERR(cs);
143f73b3 10342
5a21b665
DV
10343 /* Can't queue multiple flips, so wait for the previous
10344 * one to finish before executing the next.
10345 */
10346 if (intel_crtc->plane)
10347 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10348 else
10349 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
73dec95e
TU
10350 *cs++ = MI_WAIT_FOR_EVENT | flip_mask;
10351 *cs++ = MI_NOOP;
10352 *cs++ = MI_DISPLAY_FLIP | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
10353 *cs++ = fb->pitches[0];
10354 *cs++ = intel_crtc->flip_work->gtt_offset;
10355 *cs++ = 0; /* aux display base address, unused */
143f73b3 10356
5a21b665
DV
10357 return 0;
10358}
84c33a64 10359
5a21b665
DV
10360static int intel_gen3_queue_flip(struct drm_device *dev,
10361 struct drm_crtc *crtc,
10362 struct drm_framebuffer *fb,
10363 struct drm_i915_gem_object *obj,
10364 struct drm_i915_gem_request *req,
10365 uint32_t flags)
10366{
5a21b665 10367 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
73dec95e 10368 u32 flip_mask, *cs;
d55dbd06 10369
73dec95e
TU
10370 cs = intel_ring_begin(req, 6);
10371 if (IS_ERR(cs))
10372 return PTR_ERR(cs);
d55dbd06 10373
5a21b665
DV
10374 if (intel_crtc->plane)
10375 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10376 else
10377 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
73dec95e
TU
10378 *cs++ = MI_WAIT_FOR_EVENT | flip_mask;
10379 *cs++ = MI_NOOP;
10380 *cs++ = MI_DISPLAY_FLIP_I915 | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
10381 *cs++ = fb->pitches[0];
10382 *cs++ = intel_crtc->flip_work->gtt_offset;
10383 *cs++ = MI_NOOP;
fd8e058a 10384
5a21b665
DV
10385 return 0;
10386}
84c33a64 10387
5a21b665
DV
10388static int intel_gen4_queue_flip(struct drm_device *dev,
10389 struct drm_crtc *crtc,
10390 struct drm_framebuffer *fb,
10391 struct drm_i915_gem_object *obj,
10392 struct drm_i915_gem_request *req,
10393 uint32_t flags)
10394{
fac5e23e 10395 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665 10396 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
73dec95e 10397 u32 pf, pipesrc, *cs;
143f73b3 10398
73dec95e
TU
10399 cs = intel_ring_begin(req, 4);
10400 if (IS_ERR(cs))
10401 return PTR_ERR(cs);
143f73b3 10402
5a21b665
DV
10403 /* i965+ uses the linear or tiled offsets from the
10404 * Display Registers (which do not change across a page-flip)
10405 * so we need only reprogram the base address.
10406 */
73dec95e
TU
10407 *cs++ = MI_DISPLAY_FLIP | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
10408 *cs++ = fb->pitches[0];
10409 *cs++ = intel_crtc->flip_work->gtt_offset |
10410 intel_fb_modifier_to_tiling(fb->modifier);
5a21b665
DV
10411
10412 /* XXX Enabling the panel-fitter across page-flip is so far
10413 * untested on non-native modes, so ignore it for now.
10414 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
10415 */
10416 pf = 0;
10417 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
73dec95e 10418 *cs++ = pf | pipesrc;
143f73b3 10419
5a21b665 10420 return 0;
8c9f3aaf
JB
10421}
10422
5a21b665
DV
10423static int intel_gen6_queue_flip(struct drm_device *dev,
10424 struct drm_crtc *crtc,
10425 struct drm_framebuffer *fb,
10426 struct drm_i915_gem_object *obj,
10427 struct drm_i915_gem_request *req,
10428 uint32_t flags)
da20eabd 10429{
fac5e23e 10430 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665 10431 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
73dec95e 10432 u32 pf, pipesrc, *cs;
d21fbe87 10433
73dec95e
TU
10434 cs = intel_ring_begin(req, 4);
10435 if (IS_ERR(cs))
10436 return PTR_ERR(cs);
92826fcd 10437
73dec95e
TU
10438 *cs++ = MI_DISPLAY_FLIP | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
10439 *cs++ = fb->pitches[0] | intel_fb_modifier_to_tiling(fb->modifier);
10440 *cs++ = intel_crtc->flip_work->gtt_offset;
92826fcd 10441
5a21b665
DV
10442 /* Contrary to the suggestions in the documentation,
10443 * "Enable Panel Fitter" does not seem to be required when page
10444 * flipping with a non-native mode, and worse causes a normal
10445 * modeset to fail.
10446 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
10447 */
10448 pf = 0;
10449 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
73dec95e 10450 *cs++ = pf | pipesrc;
7809e5ae 10451
5a21b665 10452 return 0;
7809e5ae
MR
10453}
10454
5a21b665
DV
10455static int intel_gen7_queue_flip(struct drm_device *dev,
10456 struct drm_crtc *crtc,
10457 struct drm_framebuffer *fb,
10458 struct drm_i915_gem_object *obj,
10459 struct drm_i915_gem_request *req,
10460 uint32_t flags)
d21fbe87 10461{
5db94019 10462 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665 10463 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
73dec95e 10464 u32 *cs, plane_bit = 0;
5a21b665 10465 int len, ret;
d21fbe87 10466
5a21b665
DV
10467 switch (intel_crtc->plane) {
10468 case PLANE_A:
10469 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
10470 break;
10471 case PLANE_B:
10472 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
10473 break;
10474 case PLANE_C:
10475 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
10476 break;
10477 default:
10478 WARN_ONCE(1, "unknown plane in flip command\n");
10479 return -ENODEV;
10480 }
10481
10482 len = 4;
b5321f30 10483 if (req->engine->id == RCS) {
5a21b665
DV
10484 len += 6;
10485 /*
10486 * On Gen 8, SRM is now taking an extra dword to accommodate
10487 * 48bits addresses, and we need a NOOP for the batch size to
10488 * stay even.
10489 */
5db94019 10490 if (IS_GEN8(dev_priv))
5a21b665
DV
10491 len += 2;
10492 }
10493
10494 /*
10495 * BSpec MI_DISPLAY_FLIP for IVB:
10496 * "The full packet must be contained within the same cache line."
10497 *
10498 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
10499 * cacheline, if we ever start emitting more commands before
10500 * the MI_DISPLAY_FLIP we may need to first emit everything else,
10501 * then do the cacheline alignment, and finally emit the
10502 * MI_DISPLAY_FLIP.
10503 */
10504 ret = intel_ring_cacheline_align(req);
10505 if (ret)
10506 return ret;
10507
73dec95e
TU
10508 cs = intel_ring_begin(req, len);
10509 if (IS_ERR(cs))
10510 return PTR_ERR(cs);
5a21b665
DV
10511
10512 /* Unmask the flip-done completion message. Note that the bspec says that
10513 * we should do this for both the BCS and RCS, and that we must not unmask
10514 * more than one flip event at any time (or ensure that one flip message
10515 * can be sent by waiting for flip-done prior to queueing new flips).
10516 * Experimentation says that BCS works despite DERRMR masking all
10517 * flip-done completion events and that unmasking all planes at once
10518 * for the RCS also doesn't appear to drop events. Setting the DERRMR
10519 * to zero does lead to lockups within MI_DISPLAY_FLIP.
10520 */
b5321f30 10521 if (req->engine->id == RCS) {
73dec95e
TU
10522 *cs++ = MI_LOAD_REGISTER_IMM(1);
10523 *cs++ = i915_mmio_reg_offset(DERRMR);
10524 *cs++ = ~(DERRMR_PIPEA_PRI_FLIP_DONE |
10525 DERRMR_PIPEB_PRI_FLIP_DONE |
10526 DERRMR_PIPEC_PRI_FLIP_DONE);
5db94019 10527 if (IS_GEN8(dev_priv))
73dec95e
TU
10528 *cs++ = MI_STORE_REGISTER_MEM_GEN8 |
10529 MI_SRM_LRM_GLOBAL_GTT;
5a21b665 10530 else
73dec95e
TU
10531 *cs++ = MI_STORE_REGISTER_MEM | MI_SRM_LRM_GLOBAL_GTT;
10532 *cs++ = i915_mmio_reg_offset(DERRMR);
10533 *cs++ = i915_ggtt_offset(req->engine->scratch) + 256;
5db94019 10534 if (IS_GEN8(dev_priv)) {
73dec95e
TU
10535 *cs++ = 0;
10536 *cs++ = MI_NOOP;
5a21b665
DV
10537 }
10538 }
10539
73dec95e
TU
10540 *cs++ = MI_DISPLAY_FLIP_I915 | plane_bit;
10541 *cs++ = fb->pitches[0] | intel_fb_modifier_to_tiling(fb->modifier);
10542 *cs++ = intel_crtc->flip_work->gtt_offset;
10543 *cs++ = MI_NOOP;
5a21b665
DV
10544
10545 return 0;
10546}
10547
10548static bool use_mmio_flip(struct intel_engine_cs *engine,
10549 struct drm_i915_gem_object *obj)
10550{
10551 /*
10552 * This is not being used for older platforms, because
10553 * non-availability of flip done interrupt forces us to use
10554 * CS flips. Older platforms derive flip done using some clever
10555 * tricks involving the flip_pending status bits and vblank irqs.
10556 * So using MMIO flips there would disrupt this mechanism.
10557 */
10558
10559 if (engine == NULL)
10560 return true;
10561
10562 if (INTEL_GEN(engine->i915) < 5)
10563 return false;
10564
10565 if (i915.use_mmio_flip < 0)
10566 return false;
10567 else if (i915.use_mmio_flip > 0)
10568 return true;
10569 else if (i915.enable_execlists)
10570 return true;
c37efb99 10571
d07f0e59 10572 return engine != i915_gem_object_last_write_engine(obj);
5a21b665
DV
10573}
10574
10575static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
10576 unsigned int rotation,
10577 struct intel_flip_work *work)
10578{
10579 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 10580 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
DV
10581 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
10582 const enum pipe pipe = intel_crtc->pipe;
d2196774 10583 u32 ctl, stride = skl_plane_stride(fb, 0, rotation);
5a21b665
DV
10584
10585 ctl = I915_READ(PLANE_CTL(pipe, 0));
10586 ctl &= ~PLANE_CTL_TILED_MASK;
bae781b2 10587 switch (fb->modifier) {
2f075565 10588 case DRM_FORMAT_MOD_LINEAR:
5a21b665
DV
10589 break;
10590 case I915_FORMAT_MOD_X_TILED:
10591 ctl |= PLANE_CTL_TILED_X;
10592 break;
10593 case I915_FORMAT_MOD_Y_TILED:
10594 ctl |= PLANE_CTL_TILED_Y;
10595 break;
10596 case I915_FORMAT_MOD_Yf_TILED:
10597 ctl |= PLANE_CTL_TILED_YF;
10598 break;
10599 default:
bae781b2 10600 MISSING_CASE(fb->modifier);
5a21b665
DV
10601 }
10602
5a21b665
DV
10603 /*
10604 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
10605 * PLANE_SURF updates, the update is then guaranteed to be atomic.
10606 */
10607 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
10608 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
10609
10610 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
10611 POSTING_READ(PLANE_SURF(pipe, 0));
10612}
10613
10614static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
10615 struct intel_flip_work *work)
10616{
10617 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 10618 struct drm_i915_private *dev_priv = to_i915(dev);
72618ebf 10619 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
5a21b665
DV
10620 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
10621 u32 dspcntr;
10622
10623 dspcntr = I915_READ(reg);
10624
bae781b2 10625 if (fb->modifier == I915_FORMAT_MOD_X_TILED)
5a21b665
DV
10626 dspcntr |= DISPPLANE_TILED;
10627 else
10628 dspcntr &= ~DISPPLANE_TILED;
10629
10630 I915_WRITE(reg, dspcntr);
10631
10632 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
10633 POSTING_READ(DSPSURF(intel_crtc->plane));
10634}
10635
10636static void intel_mmio_flip_work_func(struct work_struct *w)
10637{
10638 struct intel_flip_work *work =
10639 container_of(w, struct intel_flip_work, mmio_work);
10640 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10641 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10642 struct intel_framebuffer *intel_fb =
10643 to_intel_framebuffer(crtc->base.primary->fb);
10644 struct drm_i915_gem_object *obj = intel_fb->obj;
10645
d07f0e59 10646 WARN_ON(i915_gem_object_wait(obj, 0, MAX_SCHEDULE_TIMEOUT, NULL) < 0);
5a21b665
DV
10647
10648 intel_pipe_update_start(crtc);
10649
10650 if (INTEL_GEN(dev_priv) >= 9)
10651 skl_do_mmio_flip(crtc, work->rotation, work);
10652 else
10653 /* use_mmio_flip() retricts MMIO flips to ilk+ */
10654 ilk_do_mmio_flip(crtc, work);
10655
10656 intel_pipe_update_end(crtc, work);
10657}
10658
10659static int intel_default_queue_flip(struct drm_device *dev,
10660 struct drm_crtc *crtc,
10661 struct drm_framebuffer *fb,
10662 struct drm_i915_gem_object *obj,
10663 struct drm_i915_gem_request *req,
10664 uint32_t flags)
10665{
10666 return -ENODEV;
10667}
10668
10669static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
10670 struct intel_crtc *intel_crtc,
10671 struct intel_flip_work *work)
10672{
10673 u32 addr, vblank;
10674
10675 if (!atomic_read(&work->pending))
10676 return false;
10677
10678 smp_rmb();
10679
10680 vblank = intel_crtc_get_vblank_counter(intel_crtc);
10681 if (work->flip_ready_vblank == 0) {
10682 if (work->flip_queued_req &&
f69a02c9 10683 !i915_gem_request_completed(work->flip_queued_req))
5a21b665
DV
10684 return false;
10685
10686 work->flip_ready_vblank = vblank;
10687 }
10688
10689 if (vblank - work->flip_ready_vblank < 3)
10690 return false;
10691
10692 /* Potential stall - if we see that the flip has happened,
10693 * assume a missed interrupt. */
10694 if (INTEL_GEN(dev_priv) >= 4)
10695 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
10696 else
10697 addr = I915_READ(DSPADDR(intel_crtc->plane));
10698
10699 /* There is a potential issue here with a false positive after a flip
10700 * to the same address. We could address this by checking for a
10701 * non-incrementing frame counter.
10702 */
10703 return addr == work->gtt_offset;
10704}
10705
10706void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
10707{
91c8a326 10708 struct drm_device *dev = &dev_priv->drm;
98187836 10709 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
5a21b665
DV
10710 struct intel_flip_work *work;
10711
10712 WARN_ON(!in_interrupt());
10713
10714 if (crtc == NULL)
10715 return;
10716
10717 spin_lock(&dev->event_lock);
e2af48c6 10718 work = crtc->flip_work;
5a21b665
DV
10719
10720 if (work != NULL && !is_mmio_work(work) &&
e2af48c6 10721 __pageflip_stall_check_cs(dev_priv, crtc, work)) {
5a21b665
DV
10722 WARN_ONCE(1,
10723 "Kicking stuck page flip: queued at %d, now %d\n",
e2af48c6
VS
10724 work->flip_queued_vblank, intel_crtc_get_vblank_counter(crtc));
10725 page_flip_completed(crtc);
5a21b665
DV
10726 work = NULL;
10727 }
10728
10729 if (work != NULL && !is_mmio_work(work) &&
e2af48c6 10730 intel_crtc_get_vblank_counter(crtc) - work->flip_queued_vblank > 1)
5a21b665
DV
10731 intel_queue_rps_boost_for_request(work->flip_queued_req);
10732 spin_unlock(&dev->event_lock);
10733}
10734
4c01ded5 10735__maybe_unused
5a21b665
DV
10736static int intel_crtc_page_flip(struct drm_crtc *crtc,
10737 struct drm_framebuffer *fb,
10738 struct drm_pending_vblank_event *event,
10739 uint32_t page_flip_flags)
10740{
10741 struct drm_device *dev = crtc->dev;
fac5e23e 10742 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
DV
10743 struct drm_framebuffer *old_fb = crtc->primary->fb;
10744 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
10745 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10746 struct drm_plane *primary = crtc->primary;
10747 enum pipe pipe = intel_crtc->pipe;
10748 struct intel_flip_work *work;
10749 struct intel_engine_cs *engine;
10750 bool mmio_flip;
8e637178 10751 struct drm_i915_gem_request *request;
058d88c4 10752 struct i915_vma *vma;
5a21b665
DV
10753 int ret;
10754
10755 /*
10756 * drm_mode_page_flip_ioctl() should already catch this, but double
10757 * check to be safe. In the future we may enable pageflipping from
10758 * a disabled primary plane.
10759 */
10760 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
10761 return -EBUSY;
10762
10763 /* Can't change pixel format via MI display flips. */
dbd4d576 10764 if (fb->format != crtc->primary->fb->format)
5a21b665
DV
10765 return -EINVAL;
10766
10767 /*
10768 * TILEOFF/LINOFF registers can't be changed via MI display flips.
10769 * Note that pitch changes could also affect these register.
10770 */
6315b5d3 10771 if (INTEL_GEN(dev_priv) > 3 &&
5a21b665
DV
10772 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
10773 fb->pitches[0] != crtc->primary->fb->pitches[0]))
10774 return -EINVAL;
10775
10776 if (i915_terminally_wedged(&dev_priv->gpu_error))
10777 goto out_hang;
10778
10779 work = kzalloc(sizeof(*work), GFP_KERNEL);
10780 if (work == NULL)
10781 return -ENOMEM;
10782
10783 work->event = event;
10784 work->crtc = crtc;
10785 work->old_fb = old_fb;
10786 INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
10787
10788 ret = drm_crtc_vblank_get(crtc);
10789 if (ret)
10790 goto free_work;
10791
10792 /* We borrow the event spin lock for protecting flip_work */
10793 spin_lock_irq(&dev->event_lock);
10794 if (intel_crtc->flip_work) {
10795 /* Before declaring the flip queue wedged, check if
10796 * the hardware completed the operation behind our backs.
10797 */
10798 if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
10799 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
10800 page_flip_completed(intel_crtc);
10801 } else {
10802 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
10803 spin_unlock_irq(&dev->event_lock);
10804
10805 drm_crtc_vblank_put(crtc);
10806 kfree(work);
10807 return -EBUSY;
10808 }
10809 }
10810 intel_crtc->flip_work = work;
10811 spin_unlock_irq(&dev->event_lock);
10812
10813 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
10814 flush_workqueue(dev_priv->wq);
10815
10816 /* Reference the objects for the scheduled work. */
10817 drm_framebuffer_reference(work->old_fb);
5a21b665
DV
10818
10819 crtc->primary->fb = fb;
10820 update_state_fb(crtc->primary);
faf68d92 10821
25dc556a 10822 work->pending_flip_obj = i915_gem_object_get(obj);
5a21b665
DV
10823
10824 ret = i915_mutex_lock_interruptible(dev);
10825 if (ret)
10826 goto cleanup;
10827
8af29b0c 10828 intel_crtc->reset_count = i915_reset_count(&dev_priv->gpu_error);
8c185eca 10829 if (i915_reset_backoff_or_wedged(&dev_priv->gpu_error)) {
5a21b665 10830 ret = -EIO;
ddbb271a 10831 goto unlock;
5a21b665
DV
10832 }
10833
10834 atomic_inc(&intel_crtc->unpin_work_count);
10835
9beb5fea 10836 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
5a21b665
DV
10837 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
10838
920a14b2 10839 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
3b3f1650 10840 engine = dev_priv->engine[BCS];
bae781b2 10841 if (fb->modifier != old_fb->modifier)
5a21b665
DV
10842 /* vlv: DISPLAY_FLIP fails to change tiling */
10843 engine = NULL;
fd6b8f43 10844 } else if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
3b3f1650 10845 engine = dev_priv->engine[BCS];
6315b5d3 10846 } else if (INTEL_GEN(dev_priv) >= 7) {
d07f0e59 10847 engine = i915_gem_object_last_write_engine(obj);
5a21b665 10848 if (engine == NULL || engine->id != RCS)
3b3f1650 10849 engine = dev_priv->engine[BCS];
5a21b665 10850 } else {
3b3f1650 10851 engine = dev_priv->engine[RCS];
5a21b665
DV
10852 }
10853
10854 mmio_flip = use_mmio_flip(engine, obj);
10855
058d88c4
CW
10856 vma = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
10857 if (IS_ERR(vma)) {
10858 ret = PTR_ERR(vma);
5a21b665 10859 goto cleanup_pending;
058d88c4 10860 }
5a21b665 10861
be1e3415
CW
10862 work->old_vma = to_intel_plane_state(primary->state)->vma;
10863 to_intel_plane_state(primary->state)->vma = vma;
10864
10865 work->gtt_offset = i915_ggtt_offset(vma) + intel_crtc->dspaddr_offset;
5a21b665
DV
10866 work->rotation = crtc->primary->state->rotation;
10867
1f061316
PZ
10868 /*
10869 * There's the potential that the next frame will not be compatible with
10870 * FBC, so we want to call pre_update() before the actual page flip.
10871 * The problem is that pre_update() caches some information about the fb
10872 * object, so we want to do this only after the object is pinned. Let's
10873 * be on the safe side and do this immediately before scheduling the
10874 * flip.
10875 */
10876 intel_fbc_pre_update(intel_crtc, intel_crtc->config,
10877 to_intel_plane_state(primary->state));
10878
5a21b665
DV
10879 if (mmio_flip) {
10880 INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
6277c8d0 10881 queue_work(system_unbound_wq, &work->mmio_work);
5a21b665 10882 } else {
e8a9c58f
CW
10883 request = i915_gem_request_alloc(engine,
10884 dev_priv->kernel_context);
8e637178
CW
10885 if (IS_ERR(request)) {
10886 ret = PTR_ERR(request);
10887 goto cleanup_unpin;
10888 }
10889
a2bc4695 10890 ret = i915_gem_request_await_object(request, obj, false);
8e637178
CW
10891 if (ret)
10892 goto cleanup_request;
10893
5a21b665
DV
10894 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
10895 page_flip_flags);
10896 if (ret)
8e637178 10897 goto cleanup_request;
5a21b665
DV
10898
10899 intel_mark_page_flip_active(intel_crtc, work);
10900
8e637178 10901 work->flip_queued_req = i915_gem_request_get(request);
e642c85b 10902 i915_add_request(request);
5a21b665
DV
10903 }
10904
92117f0b 10905 i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
5a21b665
DV
10906 i915_gem_track_fb(intel_fb_obj(old_fb), obj,
10907 to_intel_plane(primary)->frontbuffer_bit);
10908 mutex_unlock(&dev->struct_mutex);
10909
5748b6a1 10910 intel_frontbuffer_flip_prepare(to_i915(dev),
5a21b665
DV
10911 to_intel_plane(primary)->frontbuffer_bit);
10912
10913 trace_i915_flip_request(intel_crtc->plane, obj);
10914
10915 return 0;
10916
8e637178 10917cleanup_request:
e642c85b 10918 i915_add_request(request);
5a21b665 10919cleanup_unpin:
be1e3415
CW
10920 to_intel_plane_state(primary->state)->vma = work->old_vma;
10921 intel_unpin_fb_vma(vma);
5a21b665 10922cleanup_pending:
5a21b665 10923 atomic_dec(&intel_crtc->unpin_work_count);
ddbb271a 10924unlock:
5a21b665
DV
10925 mutex_unlock(&dev->struct_mutex);
10926cleanup:
10927 crtc->primary->fb = old_fb;
10928 update_state_fb(crtc->primary);
10929
f0cd5182 10930 i915_gem_object_put(obj);
5a21b665
DV
10931 drm_framebuffer_unreference(work->old_fb);
10932
10933 spin_lock_irq(&dev->event_lock);
10934 intel_crtc->flip_work = NULL;
10935 spin_unlock_irq(&dev->event_lock);
10936
10937 drm_crtc_vblank_put(crtc);
10938free_work:
10939 kfree(work);
10940
10941 if (ret == -EIO) {
10942 struct drm_atomic_state *state;
10943 struct drm_plane_state *plane_state;
10944
10945out_hang:
10946 state = drm_atomic_state_alloc(dev);
10947 if (!state)
10948 return -ENOMEM;
b260ac3e 10949 state->acquire_ctx = dev->mode_config.acquire_ctx;
5a21b665
DV
10950
10951retry:
10952 plane_state = drm_atomic_get_plane_state(state, primary);
10953 ret = PTR_ERR_OR_ZERO(plane_state);
10954 if (!ret) {
10955 drm_atomic_set_fb_for_plane(plane_state, fb);
10956
10957 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
10958 if (!ret)
10959 ret = drm_atomic_commit(state);
10960 }
10961
10962 if (ret == -EDEADLK) {
10963 drm_modeset_backoff(state->acquire_ctx);
10964 drm_atomic_state_clear(state);
10965 goto retry;
10966 }
10967
0853695c 10968 drm_atomic_state_put(state);
5a21b665
DV
10969
10970 if (ret == 0 && event) {
10971 spin_lock_irq(&dev->event_lock);
10972 drm_crtc_send_vblank_event(crtc, event);
10973 spin_unlock_irq(&dev->event_lock);
10974 }
10975 }
10976 return ret;
10977}
10978
10979
10980/**
10981 * intel_wm_need_update - Check whether watermarks need updating
10982 * @plane: drm plane
10983 * @state: new plane state
10984 *
10985 * Check current plane state versus the new one to determine whether
10986 * watermarks need to be recalculated.
10987 *
10988 * Returns true or false.
10989 */
10990static bool intel_wm_need_update(struct drm_plane *plane,
10991 struct drm_plane_state *state)
10992{
10993 struct intel_plane_state *new = to_intel_plane_state(state);
10994 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
10995
10996 /* Update watermarks on tiling or size changes. */
936e71e3 10997 if (new->base.visible != cur->base.visible)
5a21b665
DV
10998 return true;
10999
11000 if (!cur->base.fb || !new->base.fb)
11001 return false;
11002
bae781b2 11003 if (cur->base.fb->modifier != new->base.fb->modifier ||
5a21b665 11004 cur->base.rotation != new->base.rotation ||
936e71e3
VS
11005 drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
11006 drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
11007 drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
11008 drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
5a21b665
DV
11009 return true;
11010
11011 return false;
11012}
11013
11014static bool needs_scaling(struct intel_plane_state *state)
11015{
936e71e3
VS
11016 int src_w = drm_rect_width(&state->base.src) >> 16;
11017 int src_h = drm_rect_height(&state->base.src) >> 16;
11018 int dst_w = drm_rect_width(&state->base.dst);
11019 int dst_h = drm_rect_height(&state->base.dst);
5a21b665
DV
11020
11021 return (src_w != dst_w || src_h != dst_h);
11022}
d21fbe87 11023
da20eabd
ML
11024int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11025 struct drm_plane_state *plane_state)
11026{
ab1d3a0e 11027 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
da20eabd
ML
11028 struct drm_crtc *crtc = crtc_state->crtc;
11029 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
e9728bd8 11030 struct intel_plane *plane = to_intel_plane(plane_state->plane);
da20eabd 11031 struct drm_device *dev = crtc->dev;
ed4a6a7c 11032 struct drm_i915_private *dev_priv = to_i915(dev);
da20eabd 11033 struct intel_plane_state *old_plane_state =
e9728bd8 11034 to_intel_plane_state(plane->base.state);
da20eabd
ML
11035 bool mode_changed = needs_modeset(crtc_state);
11036 bool was_crtc_enabled = crtc->state->active;
11037 bool is_crtc_enabled = crtc_state->active;
da20eabd
ML
11038 bool turn_off, turn_on, visible, was_visible;
11039 struct drm_framebuffer *fb = plane_state->fb;
78108b7c 11040 int ret;
da20eabd 11041
e9728bd8 11042 if (INTEL_GEN(dev_priv) >= 9 && plane->id != PLANE_CURSOR) {
da20eabd
ML
11043 ret = skl_update_scaler_plane(
11044 to_intel_crtc_state(crtc_state),
11045 to_intel_plane_state(plane_state));
11046 if (ret)
11047 return ret;
11048 }
11049
936e71e3 11050 was_visible = old_plane_state->base.visible;
1d4258db 11051 visible = plane_state->visible;
da20eabd
ML
11052
11053 if (!was_crtc_enabled && WARN_ON(was_visible))
11054 was_visible = false;
11055
35c08f43
ML
11056 /*
11057 * Visibility is calculated as if the crtc was on, but
11058 * after scaler setup everything depends on it being off
11059 * when the crtc isn't active.
f818ffea
VS
11060 *
11061 * FIXME this is wrong for watermarks. Watermarks should also
11062 * be computed as if the pipe would be active. Perhaps move
11063 * per-plane wm computation to the .check_plane() hook, and
11064 * only combine the results from all planes in the current place?
35c08f43 11065 */
e9728bd8 11066 if (!is_crtc_enabled) {
1d4258db 11067 plane_state->visible = visible = false;
e9728bd8
VS
11068 to_intel_crtc_state(crtc_state)->active_planes &= ~BIT(plane->id);
11069 }
da20eabd
ML
11070
11071 if (!was_visible && !visible)
11072 return 0;
11073
e8861675
ML
11074 if (fb != old_plane_state->base.fb)
11075 pipe_config->fb_changed = true;
11076
da20eabd
ML
11077 turn_off = was_visible && (!visible || mode_changed);
11078 turn_on = visible && (!was_visible || mode_changed);
11079
72660ce0 11080 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
e9728bd8
VS
11081 intel_crtc->base.base.id, intel_crtc->base.name,
11082 plane->base.base.id, plane->base.name,
72660ce0 11083 fb ? fb->base.id : -1);
da20eabd 11084
72660ce0 11085 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
e9728bd8 11086 plane->base.base.id, plane->base.name,
72660ce0 11087 was_visible, visible,
da20eabd
ML
11088 turn_off, turn_on, mode_changed);
11089
caed361d 11090 if (turn_on) {
04548cba 11091 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
b4ede6df 11092 pipe_config->update_wm_pre = true;
caed361d
VS
11093
11094 /* must disable cxsr around plane enable/disable */
e9728bd8 11095 if (plane->id != PLANE_CURSOR)
caed361d
VS
11096 pipe_config->disable_cxsr = true;
11097 } else if (turn_off) {
04548cba 11098 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
b4ede6df 11099 pipe_config->update_wm_post = true;
92826fcd 11100
852eb00d 11101 /* must disable cxsr around plane enable/disable */
e9728bd8 11102 if (plane->id != PLANE_CURSOR)
ab1d3a0e 11103 pipe_config->disable_cxsr = true;
e9728bd8 11104 } else if (intel_wm_need_update(&plane->base, plane_state)) {
04548cba 11105 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
b4ede6df
VS
11106 /* FIXME bollocks */
11107 pipe_config->update_wm_pre = true;
11108 pipe_config->update_wm_post = true;
11109 }
852eb00d 11110 }
da20eabd 11111
8be6ca85 11112 if (visible || was_visible)
e9728bd8 11113 pipe_config->fb_bits |= plane->frontbuffer_bit;
a9ff8714 11114
31ae71fc
ML
11115 /*
11116 * WaCxSRDisabledForSpriteScaling:ivb
11117 *
11118 * cstate->update_wm was already set above, so this flag will
11119 * take effect when we commit and program watermarks.
11120 */
e9728bd8 11121 if (plane->id == PLANE_SPRITE0 && IS_IVYBRIDGE(dev_priv) &&
31ae71fc
ML
11122 needs_scaling(to_intel_plane_state(plane_state)) &&
11123 !needs_scaling(old_plane_state))
11124 pipe_config->disable_lp_wm = true;
d21fbe87 11125
da20eabd
ML
11126 return 0;
11127}
11128
6d3a1ce7
ML
11129static bool encoders_cloneable(const struct intel_encoder *a,
11130 const struct intel_encoder *b)
11131{
11132 /* masks could be asymmetric, so check both ways */
11133 return a == b || (a->cloneable & (1 << b->type) &&
11134 b->cloneable & (1 << a->type));
11135}
11136
11137static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11138 struct intel_crtc *crtc,
11139 struct intel_encoder *encoder)
11140{
11141 struct intel_encoder *source_encoder;
11142 struct drm_connector *connector;
11143 struct drm_connector_state *connector_state;
11144 int i;
11145
aa5e9b47 11146 for_each_new_connector_in_state(state, connector, connector_state, i) {
6d3a1ce7
ML
11147 if (connector_state->crtc != &crtc->base)
11148 continue;
11149
11150 source_encoder =
11151 to_intel_encoder(connector_state->best_encoder);
11152 if (!encoders_cloneable(encoder, source_encoder))
11153 return false;
11154 }
11155
11156 return true;
11157}
11158
6d3a1ce7
ML
11159static int intel_crtc_atomic_check(struct drm_crtc *crtc,
11160 struct drm_crtc_state *crtc_state)
11161{
cf5a15be 11162 struct drm_device *dev = crtc->dev;
fac5e23e 11163 struct drm_i915_private *dev_priv = to_i915(dev);
6d3a1ce7 11164 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cf5a15be
ML
11165 struct intel_crtc_state *pipe_config =
11166 to_intel_crtc_state(crtc_state);
6d3a1ce7 11167 struct drm_atomic_state *state = crtc_state->state;
4d20cd86 11168 int ret;
6d3a1ce7
ML
11169 bool mode_changed = needs_modeset(crtc_state);
11170
852eb00d 11171 if (mode_changed && !crtc_state->active)
caed361d 11172 pipe_config->update_wm_post = true;
eddfcbcd 11173
ad421372
ML
11174 if (mode_changed && crtc_state->enable &&
11175 dev_priv->display.crtc_compute_clock &&
8106ddbd 11176 !WARN_ON(pipe_config->shared_dpll)) {
ad421372
ML
11177 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
11178 pipe_config);
11179 if (ret)
11180 return ret;
11181 }
11182
82cf435b
LL
11183 if (crtc_state->color_mgmt_changed) {
11184 ret = intel_color_check(crtc, crtc_state);
11185 if (ret)
11186 return ret;
e7852a4b
LL
11187
11188 /*
11189 * Changing color management on Intel hardware is
11190 * handled as part of planes update.
11191 */
11192 crtc_state->planes_changed = true;
82cf435b
LL
11193 }
11194
e435d6e5 11195 ret = 0;
86c8bbbe 11196 if (dev_priv->display.compute_pipe_wm) {
e3bddded 11197 ret = dev_priv->display.compute_pipe_wm(pipe_config);
ed4a6a7c
MR
11198 if (ret) {
11199 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
11200 return ret;
11201 }
11202 }
11203
11204 if (dev_priv->display.compute_intermediate_wm &&
11205 !to_intel_atomic_state(state)->skip_intermediate_wm) {
11206 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
11207 return 0;
11208
11209 /*
11210 * Calculate 'intermediate' watermarks that satisfy both the
11211 * old state and the new state. We can program these
11212 * immediately.
11213 */
6315b5d3 11214 ret = dev_priv->display.compute_intermediate_wm(dev,
ed4a6a7c
MR
11215 intel_crtc,
11216 pipe_config);
11217 if (ret) {
11218 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
86c8bbbe 11219 return ret;
ed4a6a7c 11220 }
e3d5457c
VS
11221 } else if (dev_priv->display.compute_intermediate_wm) {
11222 if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
11223 pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
86c8bbbe
MR
11224 }
11225
6315b5d3 11226 if (INTEL_GEN(dev_priv) >= 9) {
e435d6e5
ML
11227 if (mode_changed)
11228 ret = skl_update_scaler_crtc(pipe_config);
11229
73b0ca8e
MK
11230 if (!ret)
11231 ret = skl_check_pipe_max_pixel_rate(intel_crtc,
11232 pipe_config);
e435d6e5 11233 if (!ret)
6ebc6923 11234 ret = intel_atomic_setup_scalers(dev_priv, intel_crtc,
e435d6e5
ML
11235 pipe_config);
11236 }
11237
11238 return ret;
6d3a1ce7
ML
11239}
11240
65b38e0d 11241static const struct drm_crtc_helper_funcs intel_helper_funcs = {
5a21b665
DV
11242 .atomic_begin = intel_begin_crtc_commit,
11243 .atomic_flush = intel_finish_crtc_commit,
6d3a1ce7 11244 .atomic_check = intel_crtc_atomic_check,
f6e5b160
CW
11245};
11246
d29b2f9d
ACO
11247static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11248{
11249 struct intel_connector *connector;
f9e905ca 11250 struct drm_connector_list_iter conn_iter;
d29b2f9d 11251
f9e905ca
DV
11252 drm_connector_list_iter_begin(dev, &conn_iter);
11253 for_each_intel_connector_iter(connector, &conn_iter) {
8863dc7f
DV
11254 if (connector->base.state->crtc)
11255 drm_connector_unreference(&connector->base);
11256
d29b2f9d
ACO
11257 if (connector->base.encoder) {
11258 connector->base.state->best_encoder =
11259 connector->base.encoder;
11260 connector->base.state->crtc =
11261 connector->base.encoder->crtc;
8863dc7f
DV
11262
11263 drm_connector_reference(&connector->base);
d29b2f9d
ACO
11264 } else {
11265 connector->base.state->best_encoder = NULL;
11266 connector->base.state->crtc = NULL;
11267 }
11268 }
f9e905ca 11269 drm_connector_list_iter_end(&conn_iter);
d29b2f9d
ACO
11270}
11271
050f7aeb 11272static void
eba905b2 11273connected_sink_compute_bpp(struct intel_connector *connector,
5cec258b 11274 struct intel_crtc_state *pipe_config)
050f7aeb 11275{
6a2a5c5d 11276 const struct drm_display_info *info = &connector->base.display_info;
050f7aeb
DV
11277 int bpp = pipe_config->pipe_bpp;
11278
11279 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
6a2a5c5d
VS
11280 connector->base.base.id,
11281 connector->base.name);
050f7aeb
DV
11282
11283 /* Don't use an invalid EDID bpc value */
6a2a5c5d 11284 if (info->bpc != 0 && info->bpc * 3 < bpp) {
050f7aeb 11285 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
6a2a5c5d
VS
11286 bpp, info->bpc * 3);
11287 pipe_config->pipe_bpp = info->bpc * 3;
050f7aeb
DV
11288 }
11289
196f954e 11290 /* Clamp bpp to 8 on screens without EDID 1.4 */
6a2a5c5d 11291 if (info->bpc == 0 && bpp > 24) {
196f954e
MK
11292 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
11293 bpp);
11294 pipe_config->pipe_bpp = 24;
050f7aeb
DV
11295 }
11296}
11297
4e53c2e0 11298static int
050f7aeb 11299compute_baseline_pipe_bpp(struct intel_crtc *crtc,
5cec258b 11300 struct intel_crtc_state *pipe_config)
4e53c2e0 11301{
9beb5fea 11302 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1486017f 11303 struct drm_atomic_state *state;
da3ced29
ACO
11304 struct drm_connector *connector;
11305 struct drm_connector_state *connector_state;
1486017f 11306 int bpp, i;
4e53c2e0 11307
9beb5fea
TU
11308 if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
11309 IS_CHERRYVIEW(dev_priv)))
4e53c2e0 11310 bpp = 10*3;
9beb5fea 11311 else if (INTEL_GEN(dev_priv) >= 5)
d328c9d7
DV
11312 bpp = 12*3;
11313 else
11314 bpp = 8*3;
11315
4e53c2e0 11316
4e53c2e0
DV
11317 pipe_config->pipe_bpp = bpp;
11318
1486017f
ACO
11319 state = pipe_config->base.state;
11320
4e53c2e0 11321 /* Clamp display bpp to EDID value */
aa5e9b47 11322 for_each_new_connector_in_state(state, connector, connector_state, i) {
da3ced29 11323 if (connector_state->crtc != &crtc->base)
4e53c2e0
DV
11324 continue;
11325
da3ced29
ACO
11326 connected_sink_compute_bpp(to_intel_connector(connector),
11327 pipe_config);
4e53c2e0
DV
11328 }
11329
11330 return bpp;
11331}
11332
644db711
DV
11333static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11334{
11335 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11336 "type: 0x%x flags: 0x%x\n",
1342830c 11337 mode->crtc_clock,
644db711
DV
11338 mode->crtc_hdisplay, mode->crtc_hsync_start,
11339 mode->crtc_hsync_end, mode->crtc_htotal,
11340 mode->crtc_vdisplay, mode->crtc_vsync_start,
11341 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
11342}
11343
f6982332
TU
11344static inline void
11345intel_dump_m_n_config(struct intel_crtc_state *pipe_config, char *id,
a4309657 11346 unsigned int lane_count, struct intel_link_m_n *m_n)
f6982332 11347{
a4309657
TU
11348 DRM_DEBUG_KMS("%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11349 id, lane_count,
f6982332
TU
11350 m_n->gmch_m, m_n->gmch_n,
11351 m_n->link_m, m_n->link_n, m_n->tu);
11352}
11353
c0b03411 11354static void intel_dump_pipe_config(struct intel_crtc *crtc,
5cec258b 11355 struct intel_crtc_state *pipe_config,
c0b03411
DV
11356 const char *context)
11357{
6a60cd87 11358 struct drm_device *dev = crtc->base.dev;
4f8036a2 11359 struct drm_i915_private *dev_priv = to_i915(dev);
6a60cd87
CK
11360 struct drm_plane *plane;
11361 struct intel_plane *intel_plane;
11362 struct intel_plane_state *state;
11363 struct drm_framebuffer *fb;
11364
66766e4f
TU
11365 DRM_DEBUG_KMS("[CRTC:%d:%s]%s\n",
11366 crtc->base.base.id, crtc->base.name, context);
c0b03411 11367
2c89429e
TU
11368 DRM_DEBUG_KMS("cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
11369 transcoder_name(pipe_config->cpu_transcoder),
c0b03411 11370 pipe_config->pipe_bpp, pipe_config->dither);
a4309657
TU
11371
11372 if (pipe_config->has_pch_encoder)
11373 intel_dump_m_n_config(pipe_config, "fdi",
11374 pipe_config->fdi_lanes,
11375 &pipe_config->fdi_m_n);
f6982332
TU
11376
11377 if (intel_crtc_has_dp_encoder(pipe_config)) {
a4309657
TU
11378 intel_dump_m_n_config(pipe_config, "dp m_n",
11379 pipe_config->lane_count, &pipe_config->dp_m_n);
d806e682
TU
11380 if (pipe_config->has_drrs)
11381 intel_dump_m_n_config(pipe_config, "dp m2_n2",
11382 pipe_config->lane_count,
11383 &pipe_config->dp_m2_n2);
f6982332 11384 }
b95af8be 11385
55072d19 11386 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
2c89429e 11387 pipe_config->has_audio, pipe_config->has_infoframe);
55072d19 11388
c0b03411 11389 DRM_DEBUG_KMS("requested mode:\n");
2d112de7 11390 drm_mode_debug_printmodeline(&pipe_config->base.mode);
c0b03411 11391 DRM_DEBUG_KMS("adjusted mode:\n");
2d112de7
ACO
11392 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
11393 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
a7d1b3f4 11394 DRM_DEBUG_KMS("port clock: %d, pipe src size: %dx%d, pixel rate %d\n",
2c89429e 11395 pipe_config->port_clock,
a7d1b3f4
VS
11396 pipe_config->pipe_src_w, pipe_config->pipe_src_h,
11397 pipe_config->pixel_rate);
dd2f616d
TU
11398
11399 if (INTEL_GEN(dev_priv) >= 9)
11400 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
11401 crtc->num_scalers,
11402 pipe_config->scaler_state.scaler_users,
11403 pipe_config->scaler_state.scaler_id);
a74f8375
TU
11404
11405 if (HAS_GMCH_DISPLAY(dev_priv))
11406 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
11407 pipe_config->gmch_pfit.control,
11408 pipe_config->gmch_pfit.pgm_ratios,
11409 pipe_config->gmch_pfit.lvds_border_bits);
11410 else
11411 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
11412 pipe_config->pch_pfit.pos,
11413 pipe_config->pch_pfit.size,
08c4d7fc 11414 enableddisabled(pipe_config->pch_pfit.enabled));
a74f8375 11415
2c89429e
TU
11416 DRM_DEBUG_KMS("ips: %i, double wide: %i\n",
11417 pipe_config->ips_enabled, pipe_config->double_wide);
6a60cd87 11418
f50b79f0 11419 intel_dpll_dump_hw_state(dev_priv, &pipe_config->dpll_hw_state);
415ff0f6 11420
6a60cd87
CK
11421 DRM_DEBUG_KMS("planes on this crtc\n");
11422 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
b3c11ac2 11423 struct drm_format_name_buf format_name;
6a60cd87
CK
11424 intel_plane = to_intel_plane(plane);
11425 if (intel_plane->pipe != crtc->pipe)
11426 continue;
11427
11428 state = to_intel_plane_state(plane->state);
11429 fb = state->base.fb;
11430 if (!fb) {
1d577e02
VS
11431 DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
11432 plane->base.id, plane->name, state->scaler_id);
6a60cd87
CK
11433 continue;
11434 }
11435
dd2f616d
TU
11436 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d, fb = %ux%u format = %s\n",
11437 plane->base.id, plane->name,
b3c11ac2 11438 fb->base.id, fb->width, fb->height,
438b74a5 11439 drm_get_format_name(fb->format->format, &format_name));
dd2f616d
TU
11440 if (INTEL_GEN(dev_priv) >= 9)
11441 DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
11442 state->scaler_id,
11443 state->base.src.x1 >> 16,
11444 state->base.src.y1 >> 16,
11445 drm_rect_width(&state->base.src) >> 16,
11446 drm_rect_height(&state->base.src) >> 16,
11447 state->base.dst.x1, state->base.dst.y1,
11448 drm_rect_width(&state->base.dst),
11449 drm_rect_height(&state->base.dst));
6a60cd87 11450 }
c0b03411
DV
11451}
11452
5448a00d 11453static bool check_digital_port_conflicts(struct drm_atomic_state *state)
00f0b378 11454{
5448a00d 11455 struct drm_device *dev = state->dev;
da3ced29 11456 struct drm_connector *connector;
2fd96b41 11457 struct drm_connector_list_iter conn_iter;
00f0b378 11458 unsigned int used_ports = 0;
477321e0 11459 unsigned int used_mst_ports = 0;
00f0b378
VS
11460
11461 /*
11462 * Walk the connector list instead of the encoder
11463 * list to detect the problem on ddi platforms
11464 * where there's just one encoder per digital port.
11465 */
2fd96b41
GP
11466 drm_connector_list_iter_begin(dev, &conn_iter);
11467 drm_for_each_connector_iter(connector, &conn_iter) {
0bff4858
VS
11468 struct drm_connector_state *connector_state;
11469 struct intel_encoder *encoder;
11470
11471 connector_state = drm_atomic_get_existing_connector_state(state, connector);
11472 if (!connector_state)
11473 connector_state = connector->state;
11474
5448a00d 11475 if (!connector_state->best_encoder)
00f0b378
VS
11476 continue;
11477
5448a00d
ACO
11478 encoder = to_intel_encoder(connector_state->best_encoder);
11479
11480 WARN_ON(!connector_state->crtc);
00f0b378
VS
11481
11482 switch (encoder->type) {
11483 unsigned int port_mask;
11484 case INTEL_OUTPUT_UNKNOWN:
4f8036a2 11485 if (WARN_ON(!HAS_DDI(to_i915(dev))))
00f0b378 11486 break;
cca0502b 11487 case INTEL_OUTPUT_DP:
00f0b378
VS
11488 case INTEL_OUTPUT_HDMI:
11489 case INTEL_OUTPUT_EDP:
11490 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
11491
11492 /* the same port mustn't appear more than once */
11493 if (used_ports & port_mask)
11494 return false;
11495
11496 used_ports |= port_mask;
477321e0
VS
11497 break;
11498 case INTEL_OUTPUT_DP_MST:
11499 used_mst_ports |=
11500 1 << enc_to_mst(&encoder->base)->primary->port;
11501 break;
00f0b378
VS
11502 default:
11503 break;
11504 }
11505 }
2fd96b41 11506 drm_connector_list_iter_end(&conn_iter);
00f0b378 11507
477321e0
VS
11508 /* can't mix MST and SST/HDMI on the same port */
11509 if (used_ports & used_mst_ports)
11510 return false;
11511
00f0b378
VS
11512 return true;
11513}
11514
83a57153
ACO
11515static void
11516clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
11517{
ff32c54e
VS
11518 struct drm_i915_private *dev_priv =
11519 to_i915(crtc_state->base.crtc->dev);
663a3640 11520 struct intel_crtc_scaler_state scaler_state;
4978cc93 11521 struct intel_dpll_hw_state dpll_hw_state;
8106ddbd 11522 struct intel_shared_dpll *shared_dpll;
ff32c54e 11523 struct intel_crtc_wm_state wm_state;
c4e2d043 11524 bool force_thru;
83a57153 11525
7546a384
ACO
11526 /* FIXME: before the switch to atomic started, a new pipe_config was
11527 * kzalloc'd. Code that depends on any field being zero should be
11528 * fixed, so that the crtc_state can be safely duplicated. For now,
11529 * only fields that are know to not cause problems are preserved. */
11530
663a3640 11531 scaler_state = crtc_state->scaler_state;
4978cc93
ACO
11532 shared_dpll = crtc_state->shared_dpll;
11533 dpll_hw_state = crtc_state->dpll_hw_state;
c4e2d043 11534 force_thru = crtc_state->pch_pfit.force_thru;
04548cba
VS
11535 if (IS_G4X(dev_priv) ||
11536 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
ff32c54e 11537 wm_state = crtc_state->wm;
4978cc93 11538
d2fa80a5
CW
11539 /* Keep base drm_crtc_state intact, only clear our extended struct */
11540 BUILD_BUG_ON(offsetof(struct intel_crtc_state, base));
11541 memset(&crtc_state->base + 1, 0,
11542 sizeof(*crtc_state) - sizeof(crtc_state->base));
4978cc93 11543
663a3640 11544 crtc_state->scaler_state = scaler_state;
4978cc93
ACO
11545 crtc_state->shared_dpll = shared_dpll;
11546 crtc_state->dpll_hw_state = dpll_hw_state;
c4e2d043 11547 crtc_state->pch_pfit.force_thru = force_thru;
04548cba
VS
11548 if (IS_G4X(dev_priv) ||
11549 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
ff32c54e 11550 crtc_state->wm = wm_state;
83a57153
ACO
11551}
11552
548ee15b 11553static int
b8cecdf5 11554intel_modeset_pipe_config(struct drm_crtc *crtc,
b359283a 11555 struct intel_crtc_state *pipe_config)
ee7b9f93 11556{
b359283a 11557 struct drm_atomic_state *state = pipe_config->base.state;
7758a113 11558 struct intel_encoder *encoder;
da3ced29 11559 struct drm_connector *connector;
0b901879 11560 struct drm_connector_state *connector_state;
d328c9d7 11561 int base_bpp, ret = -EINVAL;
0b901879 11562 int i;
e29c22c0 11563 bool retry = true;
ee7b9f93 11564
83a57153 11565 clear_intel_crtc_state(pipe_config);
7758a113 11566
e143a21c
DV
11567 pipe_config->cpu_transcoder =
11568 (enum transcoder) to_intel_crtc(crtc)->pipe;
b8cecdf5 11569
2960bc9c
ID
11570 /*
11571 * Sanitize sync polarity flags based on requested ones. If neither
11572 * positive or negative polarity is requested, treat this as meaning
11573 * negative polarity.
11574 */
2d112de7 11575 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 11576 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
2d112de7 11577 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
2960bc9c 11578
2d112de7 11579 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 11580 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
2d112de7 11581 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
2960bc9c 11582
d328c9d7
DV
11583 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
11584 pipe_config);
11585 if (base_bpp < 0)
4e53c2e0
DV
11586 goto fail;
11587
e41a56be
VS
11588 /*
11589 * Determine the real pipe dimensions. Note that stereo modes can
11590 * increase the actual pipe size due to the frame doubling and
11591 * insertion of additional space for blanks between the frame. This
11592 * is stored in the crtc timings. We use the requested mode to do this
11593 * computation to clearly distinguish it from the adjusted mode, which
11594 * can be changed by the connectors in the below retry loop.
11595 */
196cd5d3 11596 drm_mode_get_hv_timing(&pipe_config->base.mode,
ecb7e16b
GP
11597 &pipe_config->pipe_src_w,
11598 &pipe_config->pipe_src_h);
e41a56be 11599
aa5e9b47 11600 for_each_new_connector_in_state(state, connector, connector_state, i) {
253c84c8
VS
11601 if (connector_state->crtc != crtc)
11602 continue;
11603
11604 encoder = to_intel_encoder(connector_state->best_encoder);
11605
e25148d0
VS
11606 if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
11607 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11608 goto fail;
11609 }
11610
253c84c8
VS
11611 /*
11612 * Determine output_types before calling the .compute_config()
11613 * hooks so that the hooks can use this information safely.
11614 */
11615 pipe_config->output_types |= 1 << encoder->type;
11616 }
11617
e29c22c0 11618encoder_retry:
ef1b460d 11619 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 11620 pipe_config->port_clock = 0;
ef1b460d 11621 pipe_config->pixel_multiplier = 1;
ff9a6750 11622
135c81b8 11623 /* Fill in default crtc timings, allow encoders to overwrite them. */
2d112de7
ACO
11624 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
11625 CRTC_STEREO_DOUBLE);
135c81b8 11626
7758a113
DV
11627 /* Pass our mode to the connectors and the CRTC to give them a chance to
11628 * adjust it according to limitations or connector properties, and also
11629 * a chance to reject the mode entirely.
47f1c6c9 11630 */
aa5e9b47 11631 for_each_new_connector_in_state(state, connector, connector_state, i) {
0b901879 11632 if (connector_state->crtc != crtc)
7758a113 11633 continue;
7ae89233 11634
0b901879
ACO
11635 encoder = to_intel_encoder(connector_state->best_encoder);
11636
0a478c27 11637 if (!(encoder->compute_config(encoder, pipe_config, connector_state))) {
efea6e8e 11638 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
DV
11639 goto fail;
11640 }
ee7b9f93 11641 }
47f1c6c9 11642
ff9a6750
DV
11643 /* Set default port clock if not overwritten by the encoder. Needs to be
11644 * done afterwards in case the encoder adjusts the mode. */
11645 if (!pipe_config->port_clock)
2d112de7 11646 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
241bfc38 11647 * pipe_config->pixel_multiplier;
ff9a6750 11648
a43f6e0f 11649 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 11650 if (ret < 0) {
7758a113
DV
11651 DRM_DEBUG_KMS("CRTC fixup failed\n");
11652 goto fail;
ee7b9f93 11653 }
e29c22c0
DV
11654
11655 if (ret == RETRY) {
11656 if (WARN(!retry, "loop in pipe configuration computation\n")) {
11657 ret = -EINVAL;
11658 goto fail;
11659 }
11660
11661 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
11662 retry = false;
11663 goto encoder_retry;
11664 }
11665
e8fa4270 11666 /* Dithering seems to not pass-through bits correctly when it should, so
611032bf
MN
11667 * only enable it on 6bpc panels and when its not a compliance
11668 * test requesting 6bpc video pattern.
11669 */
11670 pipe_config->dither = (pipe_config->pipe_bpp == 6*3) &&
11671 !pipe_config->dither_force_disable;
62f0ace5 11672 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
d328c9d7 11673 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
4e53c2e0 11674
7758a113 11675fail:
548ee15b 11676 return ret;
ee7b9f93 11677}
47f1c6c9 11678
ea9d758d 11679static void
4740b0f2 11680intel_modeset_update_crtc_state(struct drm_atomic_state *state)
ea9d758d 11681{
0a9ab303 11682 struct drm_crtc *crtc;
aa5e9b47 11683 struct drm_crtc_state *new_crtc_state;
8a75d157 11684 int i;
ea9d758d 11685
7668851f 11686 /* Double check state. */
aa5e9b47
ML
11687 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
11688 to_intel_crtc(crtc)->config = to_intel_crtc_state(new_crtc_state);
fc467a22 11689
61067a5e
ML
11690 /*
11691 * Update legacy state to satisfy fbc code. This can
11692 * be removed when fbc uses the atomic state.
11693 */
11694 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
11695 struct drm_plane_state *plane_state = crtc->primary->state;
11696
11697 crtc->primary->fb = plane_state->fb;
11698 crtc->x = plane_state->src_x >> 16;
11699 crtc->y = plane_state->src_y >> 16;
11700 }
ea9d758d 11701 }
ea9d758d
DV
11702}
11703
3bd26263 11704static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 11705{
3bd26263 11706 int diff;
f1f644dc
JB
11707
11708 if (clock1 == clock2)
11709 return true;
11710
11711 if (!clock1 || !clock2)
11712 return false;
11713
11714 diff = abs(clock1 - clock2);
11715
11716 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
11717 return true;
11718
11719 return false;
11720}
11721
cfb23ed6
ML
11722static bool
11723intel_compare_m_n(unsigned int m, unsigned int n,
11724 unsigned int m2, unsigned int n2,
11725 bool exact)
11726{
11727 if (m == m2 && n == n2)
11728 return true;
11729
11730 if (exact || !m || !n || !m2 || !n2)
11731 return false;
11732
11733 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
11734
31d10b57
ML
11735 if (n > n2) {
11736 while (n > n2) {
cfb23ed6
ML
11737 m2 <<= 1;
11738 n2 <<= 1;
11739 }
31d10b57
ML
11740 } else if (n < n2) {
11741 while (n < n2) {
cfb23ed6
ML
11742 m <<= 1;
11743 n <<= 1;
11744 }
11745 }
11746
31d10b57
ML
11747 if (n != n2)
11748 return false;
11749
11750 return intel_fuzzy_clock_check(m, m2);
cfb23ed6
ML
11751}
11752
11753static bool
11754intel_compare_link_m_n(const struct intel_link_m_n *m_n,
11755 struct intel_link_m_n *m2_n2,
11756 bool adjust)
11757{
11758 if (m_n->tu == m2_n2->tu &&
11759 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
11760 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
11761 intel_compare_m_n(m_n->link_m, m_n->link_n,
11762 m2_n2->link_m, m2_n2->link_n, !adjust)) {
11763 if (adjust)
11764 *m2_n2 = *m_n;
11765
11766 return true;
11767 }
11768
11769 return false;
11770}
11771
4e8048f8
TU
11772static void __printf(3, 4)
11773pipe_config_err(bool adjust, const char *name, const char *format, ...)
11774{
11775 char *level;
11776 unsigned int category;
11777 struct va_format vaf;
11778 va_list args;
11779
11780 if (adjust) {
11781 level = KERN_DEBUG;
11782 category = DRM_UT_KMS;
11783 } else {
11784 level = KERN_ERR;
11785 category = DRM_UT_NONE;
11786 }
11787
11788 va_start(args, format);
11789 vaf.fmt = format;
11790 vaf.va = &args;
11791
11792 drm_printk(level, category, "mismatch in %s %pV", name, &vaf);
11793
11794 va_end(args);
11795}
11796
0e8ffe1b 11797static bool
6315b5d3 11798intel_pipe_config_compare(struct drm_i915_private *dev_priv,
5cec258b 11799 struct intel_crtc_state *current_config,
cfb23ed6
ML
11800 struct intel_crtc_state *pipe_config,
11801 bool adjust)
0e8ffe1b 11802{
cfb23ed6
ML
11803 bool ret = true;
11804
66e985c0
DV
11805#define PIPE_CONF_CHECK_X(name) \
11806 if (current_config->name != pipe_config->name) { \
4e8048f8 11807 pipe_config_err(adjust, __stringify(name), \
66e985c0
DV
11808 "(expected 0x%08x, found 0x%08x)\n", \
11809 current_config->name, \
11810 pipe_config->name); \
cfb23ed6 11811 ret = false; \
66e985c0
DV
11812 }
11813
08a24034
DV
11814#define PIPE_CONF_CHECK_I(name) \
11815 if (current_config->name != pipe_config->name) { \
4e8048f8 11816 pipe_config_err(adjust, __stringify(name), \
08a24034
DV
11817 "(expected %i, found %i)\n", \
11818 current_config->name, \
11819 pipe_config->name); \
cfb23ed6
ML
11820 ret = false; \
11821 }
11822
8106ddbd
ACO
11823#define PIPE_CONF_CHECK_P(name) \
11824 if (current_config->name != pipe_config->name) { \
4e8048f8 11825 pipe_config_err(adjust, __stringify(name), \
8106ddbd
ACO
11826 "(expected %p, found %p)\n", \
11827 current_config->name, \
11828 pipe_config->name); \
11829 ret = false; \
11830 }
11831
cfb23ed6
ML
11832#define PIPE_CONF_CHECK_M_N(name) \
11833 if (!intel_compare_link_m_n(&current_config->name, \
11834 &pipe_config->name,\
11835 adjust)) { \
4e8048f8 11836 pipe_config_err(adjust, __stringify(name), \
cfb23ed6
ML
11837 "(expected tu %i gmch %i/%i link %i/%i, " \
11838 "found tu %i, gmch %i/%i link %i/%i)\n", \
11839 current_config->name.tu, \
11840 current_config->name.gmch_m, \
11841 current_config->name.gmch_n, \
11842 current_config->name.link_m, \
11843 current_config->name.link_n, \
11844 pipe_config->name.tu, \
11845 pipe_config->name.gmch_m, \
11846 pipe_config->name.gmch_n, \
11847 pipe_config->name.link_m, \
11848 pipe_config->name.link_n); \
11849 ret = false; \
11850 }
11851
55c561a7
DV
11852/* This is required for BDW+ where there is only one set of registers for
11853 * switching between high and low RR.
11854 * This macro can be used whenever a comparison has to be made between one
11855 * hw state and multiple sw state variables.
11856 */
cfb23ed6
ML
11857#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
11858 if (!intel_compare_link_m_n(&current_config->name, \
11859 &pipe_config->name, adjust) && \
11860 !intel_compare_link_m_n(&current_config->alt_name, \
11861 &pipe_config->name, adjust)) { \
4e8048f8 11862 pipe_config_err(adjust, __stringify(name), \
cfb23ed6
ML
11863 "(expected tu %i gmch %i/%i link %i/%i, " \
11864 "or tu %i gmch %i/%i link %i/%i, " \
11865 "found tu %i, gmch %i/%i link %i/%i)\n", \
11866 current_config->name.tu, \
11867 current_config->name.gmch_m, \
11868 current_config->name.gmch_n, \
11869 current_config->name.link_m, \
11870 current_config->name.link_n, \
11871 current_config->alt_name.tu, \
11872 current_config->alt_name.gmch_m, \
11873 current_config->alt_name.gmch_n, \
11874 current_config->alt_name.link_m, \
11875 current_config->alt_name.link_n, \
11876 pipe_config->name.tu, \
11877 pipe_config->name.gmch_m, \
11878 pipe_config->name.gmch_n, \
11879 pipe_config->name.link_m, \
11880 pipe_config->name.link_n); \
11881 ret = false; \
88adfff1
DV
11882 }
11883
1bd1bd80
DV
11884#define PIPE_CONF_CHECK_FLAGS(name, mask) \
11885 if ((current_config->name ^ pipe_config->name) & (mask)) { \
4e8048f8
TU
11886 pipe_config_err(adjust, __stringify(name), \
11887 "(%x) (expected %i, found %i)\n", \
11888 (mask), \
1bd1bd80
DV
11889 current_config->name & (mask), \
11890 pipe_config->name & (mask)); \
cfb23ed6 11891 ret = false; \
1bd1bd80
DV
11892 }
11893
5e550656
VS
11894#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
11895 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
4e8048f8 11896 pipe_config_err(adjust, __stringify(name), \
5e550656
VS
11897 "(expected %i, found %i)\n", \
11898 current_config->name, \
11899 pipe_config->name); \
cfb23ed6 11900 ret = false; \
5e550656
VS
11901 }
11902
bb760063
DV
11903#define PIPE_CONF_QUIRK(quirk) \
11904 ((current_config->quirks | pipe_config->quirks) & (quirk))
11905
eccb140b
DV
11906 PIPE_CONF_CHECK_I(cpu_transcoder);
11907
08a24034
DV
11908 PIPE_CONF_CHECK_I(has_pch_encoder);
11909 PIPE_CONF_CHECK_I(fdi_lanes);
cfb23ed6 11910 PIPE_CONF_CHECK_M_N(fdi_m_n);
08a24034 11911
90a6b7b0 11912 PIPE_CONF_CHECK_I(lane_count);
95a7a2ae 11913 PIPE_CONF_CHECK_X(lane_lat_optim_mask);
b95af8be 11914
6315b5d3 11915 if (INTEL_GEN(dev_priv) < 8) {
cfb23ed6
ML
11916 PIPE_CONF_CHECK_M_N(dp_m_n);
11917
cfb23ed6
ML
11918 if (current_config->has_drrs)
11919 PIPE_CONF_CHECK_M_N(dp_m2_n2);
11920 } else
11921 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
eb14cb74 11922
253c84c8 11923 PIPE_CONF_CHECK_X(output_types);
a65347ba 11924
2d112de7
ACO
11925 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
11926 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
11927 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
11928 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
11929 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
11930 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
1bd1bd80 11931
2d112de7
ACO
11932 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
11933 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
11934 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
11935 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
11936 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
11937 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
1bd1bd80 11938
c93f54cf 11939 PIPE_CONF_CHECK_I(pixel_multiplier);
6897b4b5 11940 PIPE_CONF_CHECK_I(has_hdmi_sink);
772c2a51 11941 if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
920a14b2 11942 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
b5a9fa09 11943 PIPE_CONF_CHECK_I(limited_color_range);
15953637
SS
11944
11945 PIPE_CONF_CHECK_I(hdmi_scrambling);
11946 PIPE_CONF_CHECK_I(hdmi_high_tmds_clock_ratio);
e43823ec 11947 PIPE_CONF_CHECK_I(has_infoframe);
6c49f241 11948
9ed109a7
DV
11949 PIPE_CONF_CHECK_I(has_audio);
11950
2d112de7 11951 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
1bd1bd80
DV
11952 DRM_MODE_FLAG_INTERLACE);
11953
bb760063 11954 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
2d112de7 11955 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 11956 DRM_MODE_FLAG_PHSYNC);
2d112de7 11957 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 11958 DRM_MODE_FLAG_NHSYNC);
2d112de7 11959 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 11960 DRM_MODE_FLAG_PVSYNC);
2d112de7 11961 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063
DV
11962 DRM_MODE_FLAG_NVSYNC);
11963 }
045ac3b5 11964
333b8ca8 11965 PIPE_CONF_CHECK_X(gmch_pfit.control);
e2ff2d4a 11966 /* pfit ratios are autocomputed by the hw on gen4+ */
6315b5d3 11967 if (INTEL_GEN(dev_priv) < 4)
7f7d8dd6 11968 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
333b8ca8 11969 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
9953599b 11970
bfd16b2a
ML
11971 if (!adjust) {
11972 PIPE_CONF_CHECK_I(pipe_src_w);
11973 PIPE_CONF_CHECK_I(pipe_src_h);
11974
11975 PIPE_CONF_CHECK_I(pch_pfit.enabled);
11976 if (current_config->pch_pfit.enabled) {
11977 PIPE_CONF_CHECK_X(pch_pfit.pos);
11978 PIPE_CONF_CHECK_X(pch_pfit.size);
11979 }
2fa2fe9a 11980
7aefe2b5 11981 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
a7d1b3f4 11982 PIPE_CONF_CHECK_CLOCK_FUZZY(pixel_rate);
7aefe2b5 11983 }
a1b2278e 11984
e59150dc 11985 /* BDW+ don't expose a synchronous way to read the state */
772c2a51 11986 if (IS_HASWELL(dev_priv))
e59150dc 11987 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 11988
282740f7
VS
11989 PIPE_CONF_CHECK_I(double_wide);
11990
8106ddbd 11991 PIPE_CONF_CHECK_P(shared_dpll);
66e985c0 11992 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 11993 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
DV
11994 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
11995 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
d452c5b6 11996 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
00490c22 11997 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
3f4cd19f
DL
11998 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
11999 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12000 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
c0d43d62 12001
47eacbab
VS
12002 PIPE_CONF_CHECK_X(dsi_pll.ctrl);
12003 PIPE_CONF_CHECK_X(dsi_pll.div);
12004
9beb5fea 12005 if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
42571aef
VS
12006 PIPE_CONF_CHECK_I(pipe_bpp);
12007
2d112de7 12008 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
a9a7e98a 12009 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 12010
66e985c0 12011#undef PIPE_CONF_CHECK_X
08a24034 12012#undef PIPE_CONF_CHECK_I
8106ddbd 12013#undef PIPE_CONF_CHECK_P
1bd1bd80 12014#undef PIPE_CONF_CHECK_FLAGS
5e550656 12015#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 12016#undef PIPE_CONF_QUIRK
88adfff1 12017
cfb23ed6 12018 return ret;
0e8ffe1b
DV
12019}
12020
e3b247da
VS
12021static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
12022 const struct intel_crtc_state *pipe_config)
12023{
12024 if (pipe_config->has_pch_encoder) {
21a727b3 12025 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
e3b247da
VS
12026 &pipe_config->fdi_m_n);
12027 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
12028
12029 /*
12030 * FDI already provided one idea for the dotclock.
12031 * Yell if the encoder disagrees.
12032 */
12033 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
12034 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
12035 fdi_dotclock, dotclock);
12036 }
12037}
12038
c0ead703
ML
12039static void verify_wm_state(struct drm_crtc *crtc,
12040 struct drm_crtc_state *new_state)
08db6652 12041{
6315b5d3 12042 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
08db6652 12043 struct skl_ddb_allocation hw_ddb, *sw_ddb;
3de8a14c 12044 struct skl_pipe_wm hw_wm, *sw_wm;
12045 struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
12046 struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
e7c84544
ML
12047 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12048 const enum pipe pipe = intel_crtc->pipe;
3de8a14c 12049 int plane, level, max_level = ilk_wm_max_level(dev_priv);
08db6652 12050
6315b5d3 12051 if (INTEL_GEN(dev_priv) < 9 || !new_state->active)
08db6652
DL
12052 return;
12053
3de8a14c 12054 skl_pipe_wm_get_hw_state(crtc, &hw_wm);
03af79e0 12055 sw_wm = &to_intel_crtc_state(new_state)->wm.skl.optimal;
3de8a14c 12056
08db6652
DL
12057 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12058 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12059
e7c84544 12060 /* planes */
8b364b41 12061 for_each_universal_plane(dev_priv, pipe, plane) {
3de8a14c 12062 hw_plane_wm = &hw_wm.planes[plane];
12063 sw_plane_wm = &sw_wm->planes[plane];
08db6652 12064
3de8a14c 12065 /* Watermarks */
12066 for (level = 0; level <= max_level; level++) {
12067 if (skl_wm_level_equals(&hw_plane_wm->wm[level],
12068 &sw_plane_wm->wm[level]))
12069 continue;
12070
12071 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",
12072 pipe_name(pipe), plane + 1, level,
12073 sw_plane_wm->wm[level].plane_en,
12074 sw_plane_wm->wm[level].plane_res_b,
12075 sw_plane_wm->wm[level].plane_res_l,
12076 hw_plane_wm->wm[level].plane_en,
12077 hw_plane_wm->wm[level].plane_res_b,
12078 hw_plane_wm->wm[level].plane_res_l);
12079 }
08db6652 12080
3de8a14c 12081 if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
12082 &sw_plane_wm->trans_wm)) {
12083 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",
12084 pipe_name(pipe), plane + 1,
12085 sw_plane_wm->trans_wm.plane_en,
12086 sw_plane_wm->trans_wm.plane_res_b,
12087 sw_plane_wm->trans_wm.plane_res_l,
12088 hw_plane_wm->trans_wm.plane_en,
12089 hw_plane_wm->trans_wm.plane_res_b,
12090 hw_plane_wm->trans_wm.plane_res_l);
12091 }
12092
12093 /* DDB */
12094 hw_ddb_entry = &hw_ddb.plane[pipe][plane];
12095 sw_ddb_entry = &sw_ddb->plane[pipe][plane];
12096
12097 if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
faccd994 12098 DRM_ERROR("mismatch in DDB state pipe %c plane %d (expected (%u,%u), found (%u,%u))\n",
3de8a14c 12099 pipe_name(pipe), plane + 1,
12100 sw_ddb_entry->start, sw_ddb_entry->end,
12101 hw_ddb_entry->start, hw_ddb_entry->end);
12102 }
e7c84544 12103 }
08db6652 12104
27082493
L
12105 /*
12106 * cursor
12107 * If the cursor plane isn't active, we may not have updated it's ddb
12108 * allocation. In that case since the ddb allocation will be updated
12109 * once the plane becomes visible, we can skip this check
12110 */
cd5dcbf1 12111 if (1) {
3de8a14c 12112 hw_plane_wm = &hw_wm.planes[PLANE_CURSOR];
12113 sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
12114
12115 /* Watermarks */
12116 for (level = 0; level <= max_level; level++) {
12117 if (skl_wm_level_equals(&hw_plane_wm->wm[level],
12118 &sw_plane_wm->wm[level]))
12119 continue;
12120
12121 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",
12122 pipe_name(pipe), level,
12123 sw_plane_wm->wm[level].plane_en,
12124 sw_plane_wm->wm[level].plane_res_b,
12125 sw_plane_wm->wm[level].plane_res_l,
12126 hw_plane_wm->wm[level].plane_en,
12127 hw_plane_wm->wm[level].plane_res_b,
12128 hw_plane_wm->wm[level].plane_res_l);
12129 }
12130
12131 if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
12132 &sw_plane_wm->trans_wm)) {
12133 DRM_ERROR("mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
12134 pipe_name(pipe),
12135 sw_plane_wm->trans_wm.plane_en,
12136 sw_plane_wm->trans_wm.plane_res_b,
12137 sw_plane_wm->trans_wm.plane_res_l,
12138 hw_plane_wm->trans_wm.plane_en,
12139 hw_plane_wm->trans_wm.plane_res_b,
12140 hw_plane_wm->trans_wm.plane_res_l);
12141 }
12142
12143 /* DDB */
12144 hw_ddb_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
12145 sw_ddb_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
27082493 12146
3de8a14c 12147 if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
faccd994 12148 DRM_ERROR("mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
27082493 12149 pipe_name(pipe),
3de8a14c 12150 sw_ddb_entry->start, sw_ddb_entry->end,
12151 hw_ddb_entry->start, hw_ddb_entry->end);
27082493 12152 }
08db6652
DL
12153 }
12154}
12155
91d1b4bd 12156static void
677100ce
ML
12157verify_connector_state(struct drm_device *dev,
12158 struct drm_atomic_state *state,
12159 struct drm_crtc *crtc)
8af6cf88 12160{
35dd3c64 12161 struct drm_connector *connector;
aa5e9b47 12162 struct drm_connector_state *new_conn_state;
677100ce 12163 int i;
8af6cf88 12164
aa5e9b47 12165 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
35dd3c64 12166 struct drm_encoder *encoder = connector->encoder;
749d98b8 12167 struct drm_crtc_state *crtc_state = NULL;
ad3c558f 12168
aa5e9b47 12169 if (new_conn_state->crtc != crtc)
e7c84544
ML
12170 continue;
12171
749d98b8
ML
12172 if (crtc)
12173 crtc_state = drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
12174
12175 intel_connector_verify_state(crtc_state, new_conn_state);
8af6cf88 12176
aa5e9b47 12177 I915_STATE_WARN(new_conn_state->best_encoder != encoder,
35dd3c64 12178 "connector's atomic encoder doesn't match legacy encoder\n");
8af6cf88 12179 }
91d1b4bd
DV
12180}
12181
12182static void
86b04268 12183verify_encoder_state(struct drm_device *dev, struct drm_atomic_state *state)
91d1b4bd
DV
12184{
12185 struct intel_encoder *encoder;
86b04268
DV
12186 struct drm_connector *connector;
12187 struct drm_connector_state *old_conn_state, *new_conn_state;
12188 int i;
8af6cf88 12189
b2784e15 12190 for_each_intel_encoder(dev, encoder) {
86b04268 12191 bool enabled = false, found = false;
4d20cd86 12192 enum pipe pipe;
8af6cf88
DV
12193
12194 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12195 encoder->base.base.id,
8e329a03 12196 encoder->base.name);
8af6cf88 12197
86b04268
DV
12198 for_each_oldnew_connector_in_state(state, connector, old_conn_state,
12199 new_conn_state, i) {
12200 if (old_conn_state->best_encoder == &encoder->base)
12201 found = true;
12202
12203 if (new_conn_state->best_encoder != &encoder->base)
8af6cf88 12204 continue;
86b04268 12205 found = enabled = true;
ad3c558f 12206
86b04268 12207 I915_STATE_WARN(new_conn_state->crtc !=
ad3c558f
ML
12208 encoder->base.crtc,
12209 "connector's crtc doesn't match encoder crtc\n");
8af6cf88 12210 }
86b04268
DV
12211
12212 if (!found)
12213 continue;
0e32b39c 12214
e2c719b7 12215 I915_STATE_WARN(!!encoder->base.crtc != enabled,
8af6cf88
DV
12216 "encoder's enabled state mismatch "
12217 "(expected %i, found %i)\n",
12218 !!encoder->base.crtc, enabled);
7c60d198
ML
12219
12220 if (!encoder->base.crtc) {
4d20cd86 12221 bool active;
7c60d198 12222
4d20cd86
ML
12223 active = encoder->get_hw_state(encoder, &pipe);
12224 I915_STATE_WARN(active,
12225 "encoder detached but still enabled on pipe %c.\n",
12226 pipe_name(pipe));
7c60d198 12227 }
8af6cf88 12228 }
91d1b4bd
DV
12229}
12230
12231static void
c0ead703
ML
12232verify_crtc_state(struct drm_crtc *crtc,
12233 struct drm_crtc_state *old_crtc_state,
12234 struct drm_crtc_state *new_crtc_state)
91d1b4bd 12235{
e7c84544 12236 struct drm_device *dev = crtc->dev;
fac5e23e 12237 struct drm_i915_private *dev_priv = to_i915(dev);
91d1b4bd 12238 struct intel_encoder *encoder;
e7c84544
ML
12239 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12240 struct intel_crtc_state *pipe_config, *sw_config;
12241 struct drm_atomic_state *old_state;
12242 bool active;
045ac3b5 12243
e7c84544 12244 old_state = old_crtc_state->state;
ec2dc6a0 12245 __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
e7c84544
ML
12246 pipe_config = to_intel_crtc_state(old_crtc_state);
12247 memset(pipe_config, 0, sizeof(*pipe_config));
12248 pipe_config->base.crtc = crtc;
12249 pipe_config->base.state = old_state;
8af6cf88 12250
78108b7c 12251 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
8af6cf88 12252
e7c84544 12253 active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
d62cf62a 12254
e56134bc
VS
12255 /* we keep both pipes enabled on 830 */
12256 if (IS_I830(dev_priv))
e7c84544 12257 active = new_crtc_state->active;
6c49f241 12258
e7c84544
ML
12259 I915_STATE_WARN(new_crtc_state->active != active,
12260 "crtc active state doesn't match with hw state "
12261 "(expected %i, found %i)\n", new_crtc_state->active, active);
0e8ffe1b 12262
e7c84544
ML
12263 I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
12264 "transitional active state does not match atomic hw state "
12265 "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
4d20cd86 12266
e7c84544
ML
12267 for_each_encoder_on_crtc(dev, crtc, encoder) {
12268 enum pipe pipe;
4d20cd86 12269
e7c84544
ML
12270 active = encoder->get_hw_state(encoder, &pipe);
12271 I915_STATE_WARN(active != new_crtc_state->active,
12272 "[ENCODER:%i] active %i with crtc active %i\n",
12273 encoder->base.base.id, active, new_crtc_state->active);
4d20cd86 12274
e7c84544
ML
12275 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
12276 "Encoder connected to wrong pipe %c\n",
12277 pipe_name(pipe));
4d20cd86 12278
253c84c8
VS
12279 if (active) {
12280 pipe_config->output_types |= 1 << encoder->type;
e7c84544 12281 encoder->get_config(encoder, pipe_config);
253c84c8 12282 }
e7c84544 12283 }
53d9f4e9 12284
a7d1b3f4
VS
12285 intel_crtc_compute_pixel_rate(pipe_config);
12286
e7c84544
ML
12287 if (!new_crtc_state->active)
12288 return;
cfb23ed6 12289
e7c84544 12290 intel_pipe_config_sanity_check(dev_priv, pipe_config);
e3b247da 12291
749d98b8 12292 sw_config = to_intel_crtc_state(new_crtc_state);
6315b5d3 12293 if (!intel_pipe_config_compare(dev_priv, sw_config,
e7c84544
ML
12294 pipe_config, false)) {
12295 I915_STATE_WARN(1, "pipe state doesn't match!\n");
12296 intel_dump_pipe_config(intel_crtc, pipe_config,
12297 "[hw state]");
12298 intel_dump_pipe_config(intel_crtc, sw_config,
12299 "[sw state]");
8af6cf88
DV
12300 }
12301}
12302
91d1b4bd 12303static void
c0ead703
ML
12304verify_single_dpll_state(struct drm_i915_private *dev_priv,
12305 struct intel_shared_dpll *pll,
12306 struct drm_crtc *crtc,
12307 struct drm_crtc_state *new_state)
91d1b4bd 12308{
91d1b4bd 12309 struct intel_dpll_hw_state dpll_hw_state;
e7c84544
ML
12310 unsigned crtc_mask;
12311 bool active;
5358901f 12312
e7c84544 12313 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
5358901f 12314
e7c84544 12315 DRM_DEBUG_KMS("%s\n", pll->name);
5358901f 12316
e7c84544 12317 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
5358901f 12318
e7c84544
ML
12319 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
12320 I915_STATE_WARN(!pll->on && pll->active_mask,
12321 "pll in active use but not on in sw tracking\n");
12322 I915_STATE_WARN(pll->on && !pll->active_mask,
12323 "pll is on but not used by any active crtc\n");
12324 I915_STATE_WARN(pll->on != active,
12325 "pll on state mismatch (expected %i, found %i)\n",
12326 pll->on, active);
12327 }
5358901f 12328
e7c84544 12329 if (!crtc) {
2c42e535 12330 I915_STATE_WARN(pll->active_mask & ~pll->state.crtc_mask,
e7c84544 12331 "more active pll users than references: %x vs %x\n",
2c42e535 12332 pll->active_mask, pll->state.crtc_mask);
5358901f 12333
e7c84544
ML
12334 return;
12335 }
12336
12337 crtc_mask = 1 << drm_crtc_index(crtc);
12338
12339 if (new_state->active)
12340 I915_STATE_WARN(!(pll->active_mask & crtc_mask),
12341 "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
12342 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
12343 else
12344 I915_STATE_WARN(pll->active_mask & crtc_mask,
12345 "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
12346 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
2dd66ebd 12347
2c42e535 12348 I915_STATE_WARN(!(pll->state.crtc_mask & crtc_mask),
e7c84544 12349 "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
2c42e535 12350 crtc_mask, pll->state.crtc_mask);
66e985c0 12351
2c42e535 12352 I915_STATE_WARN(pll->on && memcmp(&pll->state.hw_state,
e7c84544
ML
12353 &dpll_hw_state,
12354 sizeof(dpll_hw_state)),
12355 "pll hw state mismatch\n");
12356}
12357
12358static void
c0ead703
ML
12359verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
12360 struct drm_crtc_state *old_crtc_state,
12361 struct drm_crtc_state *new_crtc_state)
e7c84544 12362{
fac5e23e 12363 struct drm_i915_private *dev_priv = to_i915(dev);
e7c84544
ML
12364 struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
12365 struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
12366
12367 if (new_state->shared_dpll)
c0ead703 12368 verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
e7c84544
ML
12369
12370 if (old_state->shared_dpll &&
12371 old_state->shared_dpll != new_state->shared_dpll) {
12372 unsigned crtc_mask = 1 << drm_crtc_index(crtc);
12373 struct intel_shared_dpll *pll = old_state->shared_dpll;
12374
12375 I915_STATE_WARN(pll->active_mask & crtc_mask,
12376 "pll active mismatch (didn't expect pipe %c in active mask)\n",
12377 pipe_name(drm_crtc_index(crtc)));
2c42e535 12378 I915_STATE_WARN(pll->state.crtc_mask & crtc_mask,
e7c84544
ML
12379 "pll enabled crtcs mismatch (found %x in enabled mask)\n",
12380 pipe_name(drm_crtc_index(crtc)));
5358901f 12381 }
8af6cf88
DV
12382}
12383
e7c84544 12384static void
c0ead703 12385intel_modeset_verify_crtc(struct drm_crtc *crtc,
677100ce
ML
12386 struct drm_atomic_state *state,
12387 struct drm_crtc_state *old_state,
12388 struct drm_crtc_state *new_state)
e7c84544 12389{
5a21b665
DV
12390 if (!needs_modeset(new_state) &&
12391 !to_intel_crtc_state(new_state)->update_pipe)
12392 return;
12393
c0ead703 12394 verify_wm_state(crtc, new_state);
677100ce 12395 verify_connector_state(crtc->dev, state, crtc);
c0ead703
ML
12396 verify_crtc_state(crtc, old_state, new_state);
12397 verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
e7c84544
ML
12398}
12399
12400static void
c0ead703 12401verify_disabled_dpll_state(struct drm_device *dev)
e7c84544 12402{
fac5e23e 12403 struct drm_i915_private *dev_priv = to_i915(dev);
e7c84544
ML
12404 int i;
12405
12406 for (i = 0; i < dev_priv->num_shared_dpll; i++)
c0ead703 12407 verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
e7c84544
ML
12408}
12409
12410static void
677100ce
ML
12411intel_modeset_verify_disabled(struct drm_device *dev,
12412 struct drm_atomic_state *state)
e7c84544 12413{
86b04268 12414 verify_encoder_state(dev, state);
677100ce 12415 verify_connector_state(dev, state, NULL);
c0ead703 12416 verify_disabled_dpll_state(dev);
e7c84544
ML
12417}
12418
80715b2f
VS
12419static void update_scanline_offset(struct intel_crtc *crtc)
12420{
4f8036a2 12421 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
80715b2f
VS
12422
12423 /*
12424 * The scanline counter increments at the leading edge of hsync.
12425 *
12426 * On most platforms it starts counting from vtotal-1 on the
12427 * first active line. That means the scanline counter value is
12428 * always one less than what we would expect. Ie. just after
12429 * start of vblank, which also occurs at start of hsync (on the
12430 * last active line), the scanline counter will read vblank_start-1.
12431 *
12432 * On gen2 the scanline counter starts counting from 1 instead
12433 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12434 * to keep the value positive), instead of adding one.
12435 *
12436 * On HSW+ the behaviour of the scanline counter depends on the output
12437 * type. For DP ports it behaves like most other platforms, but on HDMI
12438 * there's an extra 1 line difference. So we need to add two instead of
12439 * one to the value.
8f4d3809
VS
12440 *
12441 * On VLV/CHV DSI the scanline counter would appear to increment
12442 * approx. 1/3 of a scanline before start of vblank. Unfortunately
12443 * that means we can't tell whether we're in vblank or not while
12444 * we're on that particular line. We must still set scanline_offset
12445 * to 1 so that the vblank timestamps come out correct when we query
12446 * the scanline counter from within the vblank interrupt handler.
12447 * However if queried just before the start of vblank we'll get an
12448 * answer that's slightly in the future.
80715b2f 12449 */
4f8036a2 12450 if (IS_GEN2(dev_priv)) {
124abe07 12451 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
80715b2f
VS
12452 int vtotal;
12453
124abe07
VS
12454 vtotal = adjusted_mode->crtc_vtotal;
12455 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
80715b2f
VS
12456 vtotal /= 2;
12457
12458 crtc->scanline_offset = vtotal - 1;
4f8036a2 12459 } else if (HAS_DDI(dev_priv) &&
2d84d2b3 12460 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
80715b2f
VS
12461 crtc->scanline_offset = 2;
12462 } else
12463 crtc->scanline_offset = 1;
12464}
12465
ad421372 12466static void intel_modeset_clear_plls(struct drm_atomic_state *state)
ed6739ef 12467{
225da59b 12468 struct drm_device *dev = state->dev;
ed6739ef 12469 struct drm_i915_private *dev_priv = to_i915(dev);
0a9ab303 12470 struct drm_crtc *crtc;
aa5e9b47 12471 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
0a9ab303 12472 int i;
ed6739ef
ACO
12473
12474 if (!dev_priv->display.crtc_compute_clock)
ad421372 12475 return;
ed6739ef 12476
aa5e9b47 12477 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
fb1a38a9 12478 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8106ddbd 12479 struct intel_shared_dpll *old_dpll =
aa5e9b47 12480 to_intel_crtc_state(old_crtc_state)->shared_dpll;
0a9ab303 12481
aa5e9b47 12482 if (!needs_modeset(new_crtc_state))
225da59b
ACO
12483 continue;
12484
aa5e9b47 12485 to_intel_crtc_state(new_crtc_state)->shared_dpll = NULL;
fb1a38a9 12486
8106ddbd 12487 if (!old_dpll)
fb1a38a9 12488 continue;
0a9ab303 12489
a1c414ee 12490 intel_release_shared_dpll(old_dpll, intel_crtc, state);
ad421372 12491 }
ed6739ef
ACO
12492}
12493
99d736a2
ML
12494/*
12495 * This implements the workaround described in the "notes" section of the mode
12496 * set sequence documentation. When going from no pipes or single pipe to
12497 * multiple pipes, and planes are enabled after the pipe, we need to wait at
12498 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
12499 */
12500static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
12501{
12502 struct drm_crtc_state *crtc_state;
12503 struct intel_crtc *intel_crtc;
12504 struct drm_crtc *crtc;
12505 struct intel_crtc_state *first_crtc_state = NULL;
12506 struct intel_crtc_state *other_crtc_state = NULL;
12507 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
12508 int i;
12509
12510 /* look at all crtc's that are going to be enabled in during modeset */
aa5e9b47 12511 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
99d736a2
ML
12512 intel_crtc = to_intel_crtc(crtc);
12513
12514 if (!crtc_state->active || !needs_modeset(crtc_state))
12515 continue;
12516
12517 if (first_crtc_state) {
12518 other_crtc_state = to_intel_crtc_state(crtc_state);
12519 break;
12520 } else {
12521 first_crtc_state = to_intel_crtc_state(crtc_state);
12522 first_pipe = intel_crtc->pipe;
12523 }
12524 }
12525
12526 /* No workaround needed? */
12527 if (!first_crtc_state)
12528 return 0;
12529
12530 /* w/a possibly needed, check how many crtc's are already enabled. */
12531 for_each_intel_crtc(state->dev, intel_crtc) {
12532 struct intel_crtc_state *pipe_config;
12533
12534 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
12535 if (IS_ERR(pipe_config))
12536 return PTR_ERR(pipe_config);
12537
12538 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
12539
12540 if (!pipe_config->base.active ||
12541 needs_modeset(&pipe_config->base))
12542 continue;
12543
12544 /* 2 or more enabled crtcs means no need for w/a */
12545 if (enabled_pipe != INVALID_PIPE)
12546 return 0;
12547
12548 enabled_pipe = intel_crtc->pipe;
12549 }
12550
12551 if (enabled_pipe != INVALID_PIPE)
12552 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
12553 else if (other_crtc_state)
12554 other_crtc_state->hsw_workaround_pipe = first_pipe;
12555
12556 return 0;
12557}
12558
8d96561a
VS
12559static int intel_lock_all_pipes(struct drm_atomic_state *state)
12560{
12561 struct drm_crtc *crtc;
12562
12563 /* Add all pipes to the state */
12564 for_each_crtc(state->dev, crtc) {
12565 struct drm_crtc_state *crtc_state;
12566
12567 crtc_state = drm_atomic_get_crtc_state(state, crtc);
12568 if (IS_ERR(crtc_state))
12569 return PTR_ERR(crtc_state);
12570 }
12571
12572 return 0;
12573}
12574
27c329ed
ML
12575static int intel_modeset_all_pipes(struct drm_atomic_state *state)
12576{
12577 struct drm_crtc *crtc;
27c329ed 12578
8d96561a
VS
12579 /*
12580 * Add all pipes to the state, and force
12581 * a modeset on all the active ones.
12582 */
27c329ed 12583 for_each_crtc(state->dev, crtc) {
9780aad5
VS
12584 struct drm_crtc_state *crtc_state;
12585 int ret;
12586
27c329ed
ML
12587 crtc_state = drm_atomic_get_crtc_state(state, crtc);
12588 if (IS_ERR(crtc_state))
12589 return PTR_ERR(crtc_state);
12590
12591 if (!crtc_state->active || needs_modeset(crtc_state))
12592 continue;
12593
12594 crtc_state->mode_changed = true;
12595
12596 ret = drm_atomic_add_affected_connectors(state, crtc);
12597 if (ret)
9780aad5 12598 return ret;
27c329ed
ML
12599
12600 ret = drm_atomic_add_affected_planes(state, crtc);
12601 if (ret)
9780aad5 12602 return ret;
27c329ed
ML
12603 }
12604
9780aad5 12605 return 0;
27c329ed
ML
12606}
12607
c347a676 12608static int intel_modeset_checks(struct drm_atomic_state *state)
054518dd 12609{
565602d7 12610 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 12611 struct drm_i915_private *dev_priv = to_i915(state->dev);
565602d7 12612 struct drm_crtc *crtc;
aa5e9b47 12613 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
565602d7 12614 int ret = 0, i;
054518dd 12615
b359283a
ML
12616 if (!check_digital_port_conflicts(state)) {
12617 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
12618 return -EINVAL;
12619 }
12620
565602d7
ML
12621 intel_state->modeset = true;
12622 intel_state->active_crtcs = dev_priv->active_crtcs;
bb0f4aab
VS
12623 intel_state->cdclk.logical = dev_priv->cdclk.logical;
12624 intel_state->cdclk.actual = dev_priv->cdclk.actual;
565602d7 12625
aa5e9b47
ML
12626 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
12627 if (new_crtc_state->active)
565602d7
ML
12628 intel_state->active_crtcs |= 1 << i;
12629 else
12630 intel_state->active_crtcs &= ~(1 << i);
8b4a7d05 12631
aa5e9b47 12632 if (old_crtc_state->active != new_crtc_state->active)
8b4a7d05 12633 intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
565602d7
ML
12634 }
12635
054518dd
ACO
12636 /*
12637 * See if the config requires any additional preparation, e.g.
12638 * to adjust global state with pipes off. We need to do this
12639 * here so we can get the modeset_pipe updated config for the new
12640 * mode set on this crtc. For other crtcs we need to use the
12641 * adjusted_mode bits in the crtc directly.
12642 */
27c329ed 12643 if (dev_priv->display.modeset_calc_cdclk) {
27c329ed 12644 ret = dev_priv->display.modeset_calc_cdclk(state);
c89e39f3
CT
12645 if (ret < 0)
12646 return ret;
27c329ed 12647
8d96561a 12648 /*
bb0f4aab 12649 * Writes to dev_priv->cdclk.logical must protected by
8d96561a
VS
12650 * holding all the crtc locks, even if we don't end up
12651 * touching the hardware
12652 */
bb0f4aab
VS
12653 if (!intel_cdclk_state_compare(&dev_priv->cdclk.logical,
12654 &intel_state->cdclk.logical)) {
8d96561a
VS
12655 ret = intel_lock_all_pipes(state);
12656 if (ret < 0)
12657 return ret;
12658 }
12659
12660 /* All pipes must be switched off while we change the cdclk. */
bb0f4aab
VS
12661 if (!intel_cdclk_state_compare(&dev_priv->cdclk.actual,
12662 &intel_state->cdclk.actual)) {
27c329ed 12663 ret = intel_modeset_all_pipes(state);
8d96561a
VS
12664 if (ret < 0)
12665 return ret;
12666 }
e8788cbc 12667
bb0f4aab
VS
12668 DRM_DEBUG_KMS("New cdclk calculated to be logical %u kHz, actual %u kHz\n",
12669 intel_state->cdclk.logical.cdclk,
12670 intel_state->cdclk.actual.cdclk);
e0ca7a6b 12671 } else {
bb0f4aab 12672 to_intel_atomic_state(state)->cdclk.logical = dev_priv->cdclk.logical;
e0ca7a6b 12673 }
054518dd 12674
ad421372 12675 intel_modeset_clear_plls(state);
054518dd 12676
565602d7 12677 if (IS_HASWELL(dev_priv))
ad421372 12678 return haswell_mode_set_planes_workaround(state);
99d736a2 12679
ad421372 12680 return 0;
c347a676
ACO
12681}
12682
aa363136
MR
12683/*
12684 * Handle calculation of various watermark data at the end of the atomic check
12685 * phase. The code here should be run after the per-crtc and per-plane 'check'
12686 * handlers to ensure that all derived state has been updated.
12687 */
55994c2c 12688static int calc_watermark_data(struct drm_atomic_state *state)
aa363136
MR
12689{
12690 struct drm_device *dev = state->dev;
98d39494 12691 struct drm_i915_private *dev_priv = to_i915(dev);
98d39494
MR
12692
12693 /* Is there platform-specific watermark information to calculate? */
12694 if (dev_priv->display.compute_global_watermarks)
55994c2c
MR
12695 return dev_priv->display.compute_global_watermarks(state);
12696
12697 return 0;
aa363136
MR
12698}
12699
74c090b1
ML
12700/**
12701 * intel_atomic_check - validate state object
12702 * @dev: drm device
12703 * @state: state to validate
12704 */
12705static int intel_atomic_check(struct drm_device *dev,
12706 struct drm_atomic_state *state)
c347a676 12707{
dd8b3bdb 12708 struct drm_i915_private *dev_priv = to_i915(dev);
aa363136 12709 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
c347a676 12710 struct drm_crtc *crtc;
aa5e9b47 12711 struct drm_crtc_state *old_crtc_state, *crtc_state;
c347a676 12712 int ret, i;
61333b60 12713 bool any_ms = false;
c347a676 12714
74c090b1 12715 ret = drm_atomic_helper_check_modeset(dev, state);
054518dd
ACO
12716 if (ret)
12717 return ret;
12718
aa5e9b47 12719 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, crtc_state, i) {
cfb23ed6
ML
12720 struct intel_crtc_state *pipe_config =
12721 to_intel_crtc_state(crtc_state);
1ed51de9
DV
12722
12723 /* Catch I915_MODE_FLAG_INHERITED */
aa5e9b47 12724 if (crtc_state->mode.private_flags != old_crtc_state->mode.private_flags)
1ed51de9 12725 crtc_state->mode_changed = true;
cfb23ed6 12726
af4a879e 12727 if (!needs_modeset(crtc_state))
c347a676
ACO
12728 continue;
12729
af4a879e
DV
12730 if (!crtc_state->enable) {
12731 any_ms = true;
cfb23ed6 12732 continue;
af4a879e 12733 }
cfb23ed6 12734
26495481
DV
12735 /* FIXME: For only active_changed we shouldn't need to do any
12736 * state recomputation at all. */
12737
1ed51de9
DV
12738 ret = drm_atomic_add_affected_connectors(state, crtc);
12739 if (ret)
12740 return ret;
b359283a 12741
cfb23ed6 12742 ret = intel_modeset_pipe_config(crtc, pipe_config);
25aa1c39
ML
12743 if (ret) {
12744 intel_dump_pipe_config(to_intel_crtc(crtc),
12745 pipe_config, "[failed]");
c347a676 12746 return ret;
25aa1c39 12747 }
c347a676 12748
73831236 12749 if (i915.fastboot &&
6315b5d3 12750 intel_pipe_config_compare(dev_priv,
aa5e9b47 12751 to_intel_crtc_state(old_crtc_state),
1ed51de9 12752 pipe_config, true)) {
26495481 12753 crtc_state->mode_changed = false;
aa5e9b47 12754 pipe_config->update_pipe = true;
26495481
DV
12755 }
12756
af4a879e 12757 if (needs_modeset(crtc_state))
26495481 12758 any_ms = true;
cfb23ed6 12759
af4a879e
DV
12760 ret = drm_atomic_add_affected_planes(state, crtc);
12761 if (ret)
12762 return ret;
61333b60 12763
26495481
DV
12764 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
12765 needs_modeset(crtc_state) ?
12766 "[modeset]" : "[fastset]");
c347a676
ACO
12767 }
12768
61333b60
ML
12769 if (any_ms) {
12770 ret = intel_modeset_checks(state);
12771
12772 if (ret)
12773 return ret;
e0ca7a6b 12774 } else {
bb0f4aab 12775 intel_state->cdclk.logical = dev_priv->cdclk.logical;
e0ca7a6b 12776 }
76305b1a 12777
dd8b3bdb 12778 ret = drm_atomic_helper_check_planes(dev, state);
aa363136
MR
12779 if (ret)
12780 return ret;
12781
f51be2e0 12782 intel_fbc_choose_crtc(dev_priv, state);
55994c2c 12783 return calc_watermark_data(state);
054518dd
ACO
12784}
12785
5008e874 12786static int intel_atomic_prepare_commit(struct drm_device *dev,
d07f0e59 12787 struct drm_atomic_state *state)
5008e874 12788{
fac5e23e 12789 struct drm_i915_private *dev_priv = to_i915(dev);
5008e874
ML
12790 struct drm_crtc_state *crtc_state;
12791 struct drm_crtc *crtc;
12792 int i, ret;
12793
aa5e9b47 12794 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
5a21b665 12795 if (state->legacy_cursor_update)
a6747b73
ML
12796 continue;
12797
5a21b665
DV
12798 ret = intel_crtc_wait_for_pending_flips(crtc);
12799 if (ret)
12800 return ret;
5008e874 12801
5a21b665
DV
12802 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
12803 flush_workqueue(dev_priv->wq);
d55dbd06
ML
12804 }
12805
f935675f
ML
12806 ret = mutex_lock_interruptible(&dev->struct_mutex);
12807 if (ret)
12808 return ret;
12809
5008e874 12810 ret = drm_atomic_helper_prepare_planes(dev, state);
f7e5838b 12811 mutex_unlock(&dev->struct_mutex);
7580d774 12812
5008e874
ML
12813 return ret;
12814}
12815
a2991414
ML
12816u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
12817{
12818 struct drm_device *dev = crtc->base.dev;
12819
12820 if (!dev->max_vblank_count)
ca814b25 12821 return drm_crtc_accurate_vblank_count(&crtc->base);
a2991414
ML
12822
12823 return dev->driver->get_vblank_counter(dev, crtc->pipe);
12824}
12825
5a21b665
DV
12826static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
12827 struct drm_i915_private *dev_priv,
12828 unsigned crtc_mask)
e8861675 12829{
5a21b665
DV
12830 unsigned last_vblank_count[I915_MAX_PIPES];
12831 enum pipe pipe;
12832 int ret;
e8861675 12833
5a21b665
DV
12834 if (!crtc_mask)
12835 return;
e8861675 12836
5a21b665 12837 for_each_pipe(dev_priv, pipe) {
98187836
VS
12838 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
12839 pipe);
e8861675 12840
5a21b665 12841 if (!((1 << pipe) & crtc_mask))
e8861675
ML
12842 continue;
12843
e2af48c6 12844 ret = drm_crtc_vblank_get(&crtc->base);
5a21b665
DV
12845 if (WARN_ON(ret != 0)) {
12846 crtc_mask &= ~(1 << pipe);
12847 continue;
e8861675
ML
12848 }
12849
e2af48c6 12850 last_vblank_count[pipe] = drm_crtc_vblank_count(&crtc->base);
e8861675
ML
12851 }
12852
5a21b665 12853 for_each_pipe(dev_priv, pipe) {
98187836
VS
12854 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
12855 pipe);
5a21b665 12856 long lret;
e8861675 12857
5a21b665
DV
12858 if (!((1 << pipe) & crtc_mask))
12859 continue;
d55dbd06 12860
5a21b665
DV
12861 lret = wait_event_timeout(dev->vblank[pipe].queue,
12862 last_vblank_count[pipe] !=
e2af48c6 12863 drm_crtc_vblank_count(&crtc->base),
5a21b665 12864 msecs_to_jiffies(50));
d55dbd06 12865
5a21b665 12866 WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
d55dbd06 12867
e2af48c6 12868 drm_crtc_vblank_put(&crtc->base);
d55dbd06
ML
12869 }
12870}
12871
5a21b665 12872static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
a6747b73 12873{
5a21b665
DV
12874 /* fb updated, need to unpin old fb */
12875 if (crtc_state->fb_changed)
12876 return true;
a6747b73 12877
5a21b665
DV
12878 /* wm changes, need vblank before final wm's */
12879 if (crtc_state->update_wm_post)
12880 return true;
a6747b73 12881
5eeb798b 12882 if (crtc_state->wm.need_postvbl_update)
5a21b665 12883 return true;
a6747b73 12884
5a21b665 12885 return false;
e8861675
ML
12886}
12887
896e5bb0
L
12888static void intel_update_crtc(struct drm_crtc *crtc,
12889 struct drm_atomic_state *state,
12890 struct drm_crtc_state *old_crtc_state,
aa5e9b47 12891 struct drm_crtc_state *new_crtc_state,
896e5bb0
L
12892 unsigned int *crtc_vblank_mask)
12893{
12894 struct drm_device *dev = crtc->dev;
12895 struct drm_i915_private *dev_priv = to_i915(dev);
12896 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
aa5e9b47
ML
12897 struct intel_crtc_state *pipe_config = to_intel_crtc_state(new_crtc_state);
12898 bool modeset = needs_modeset(new_crtc_state);
896e5bb0
L
12899
12900 if (modeset) {
12901 update_scanline_offset(intel_crtc);
12902 dev_priv->display.crtc_enable(pipe_config, state);
12903 } else {
aa5e9b47
ML
12904 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
12905 pipe_config);
896e5bb0
L
12906 }
12907
12908 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
12909 intel_fbc_enable(
12910 intel_crtc, pipe_config,
12911 to_intel_plane_state(crtc->primary->state));
12912 }
12913
12914 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
12915
12916 if (needs_vblank_wait(pipe_config))
12917 *crtc_vblank_mask |= drm_crtc_mask(crtc);
12918}
12919
12920static void intel_update_crtcs(struct drm_atomic_state *state,
12921 unsigned int *crtc_vblank_mask)
12922{
12923 struct drm_crtc *crtc;
aa5e9b47 12924 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
896e5bb0
L
12925 int i;
12926
aa5e9b47
ML
12927 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
12928 if (!new_crtc_state->active)
896e5bb0
L
12929 continue;
12930
12931 intel_update_crtc(crtc, state, old_crtc_state,
aa5e9b47 12932 new_crtc_state, crtc_vblank_mask);
896e5bb0
L
12933 }
12934}
12935
27082493
L
12936static void skl_update_crtcs(struct drm_atomic_state *state,
12937 unsigned int *crtc_vblank_mask)
12938{
0f0f74bc 12939 struct drm_i915_private *dev_priv = to_i915(state->dev);
27082493
L
12940 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
12941 struct drm_crtc *crtc;
ce0ba283 12942 struct intel_crtc *intel_crtc;
aa5e9b47 12943 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
ce0ba283 12944 struct intel_crtc_state *cstate;
27082493
L
12945 unsigned int updated = 0;
12946 bool progress;
12947 enum pipe pipe;
5eff503b
ML
12948 int i;
12949
12950 const struct skl_ddb_entry *entries[I915_MAX_PIPES] = {};
12951
aa5e9b47 12952 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i)
5eff503b 12953 /* ignore allocations for crtc's that have been turned off. */
aa5e9b47 12954 if (new_crtc_state->active)
5eff503b 12955 entries[i] = &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb;
27082493
L
12956
12957 /*
12958 * Whenever the number of active pipes changes, we need to make sure we
12959 * update the pipes in the right order so that their ddb allocations
12960 * never overlap with eachother inbetween CRTC updates. Otherwise we'll
12961 * cause pipe underruns and other bad stuff.
12962 */
12963 do {
27082493
L
12964 progress = false;
12965
aa5e9b47 12966 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
27082493
L
12967 bool vbl_wait = false;
12968 unsigned int cmask = drm_crtc_mask(crtc);
ce0ba283
L
12969
12970 intel_crtc = to_intel_crtc(crtc);
12971 cstate = to_intel_crtc_state(crtc->state);
12972 pipe = intel_crtc->pipe;
27082493 12973
5eff503b 12974 if (updated & cmask || !cstate->base.active)
27082493 12975 continue;
5eff503b
ML
12976
12977 if (skl_ddb_allocation_overlaps(entries, &cstate->wm.skl.ddb, i))
27082493
L
12978 continue;
12979
12980 updated |= cmask;
5eff503b 12981 entries[i] = &cstate->wm.skl.ddb;
27082493
L
12982
12983 /*
12984 * If this is an already active pipe, it's DDB changed,
12985 * and this isn't the last pipe that needs updating
12986 * then we need to wait for a vblank to pass for the
12987 * new ddb allocation to take effect.
12988 */
ce0ba283 12989 if (!skl_ddb_entry_equal(&cstate->wm.skl.ddb,
512b5527 12990 &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb) &&
aa5e9b47 12991 !new_crtc_state->active_changed &&
27082493
L
12992 intel_state->wm_results.dirty_pipes != updated)
12993 vbl_wait = true;
12994
12995 intel_update_crtc(crtc, state, old_crtc_state,
aa5e9b47 12996 new_crtc_state, crtc_vblank_mask);
27082493
L
12997
12998 if (vbl_wait)
0f0f74bc 12999 intel_wait_for_vblank(dev_priv, pipe);
27082493
L
13000
13001 progress = true;
13002 }
13003 } while (progress);
13004}
13005
ba318c61
CW
13006static void intel_atomic_helper_free_state(struct drm_i915_private *dev_priv)
13007{
13008 struct intel_atomic_state *state, *next;
13009 struct llist_node *freed;
13010
13011 freed = llist_del_all(&dev_priv->atomic_helper.free_list);
13012 llist_for_each_entry_safe(state, next, freed, freed)
13013 drm_atomic_state_put(&state->base);
13014}
13015
13016static void intel_atomic_helper_free_state_worker(struct work_struct *work)
13017{
13018 struct drm_i915_private *dev_priv =
13019 container_of(work, typeof(*dev_priv), atomic_helper.free_work);
13020
13021 intel_atomic_helper_free_state(dev_priv);
13022}
13023
94f05024 13024static void intel_atomic_commit_tail(struct drm_atomic_state *state)
a6778b3c 13025{
94f05024 13026 struct drm_device *dev = state->dev;
565602d7 13027 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 13028 struct drm_i915_private *dev_priv = to_i915(dev);
aa5e9b47 13029 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
7580d774 13030 struct drm_crtc *crtc;
5a21b665 13031 struct intel_crtc_state *intel_cstate;
5a21b665 13032 bool hw_check = intel_state->modeset;
d8fc70b7 13033 u64 put_domains[I915_MAX_PIPES] = {};
5a21b665 13034 unsigned crtc_vblank_mask = 0;
e95433c7 13035 int i;
a6778b3c 13036
ea0000f0
DV
13037 drm_atomic_helper_wait_for_dependencies(state);
13038
c3b32658 13039 if (intel_state->modeset)
5a21b665 13040 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
565602d7 13041
aa5e9b47 13042 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
a539205a
ML
13043 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13044
aa5e9b47
ML
13045 if (needs_modeset(new_crtc_state) ||
13046 to_intel_crtc_state(new_crtc_state)->update_pipe) {
5a21b665
DV
13047 hw_check = true;
13048
13049 put_domains[to_intel_crtc(crtc)->pipe] =
13050 modeset_get_crtc_power_domains(crtc,
aa5e9b47 13051 to_intel_crtc_state(new_crtc_state));
5a21b665
DV
13052 }
13053
aa5e9b47 13054 if (!needs_modeset(new_crtc_state))
61333b60
ML
13055 continue;
13056
aa5e9b47
ML
13057 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
13058 to_intel_crtc_state(new_crtc_state));
460da916 13059
29ceb0e6
VS
13060 if (old_crtc_state->active) {
13061 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
4a806558 13062 dev_priv->display.crtc_disable(to_intel_crtc_state(old_crtc_state), state);
eddfcbcd 13063 intel_crtc->active = false;
58f9c0bc 13064 intel_fbc_disable(intel_crtc);
eddfcbcd 13065 intel_disable_shared_dpll(intel_crtc);
9bbc8258
VS
13066
13067 /*
13068 * Underruns don't always raise
13069 * interrupts, so check manually.
13070 */
13071 intel_check_cpu_fifo_underruns(dev_priv);
13072 intel_check_pch_fifo_underruns(dev_priv);
b9001114 13073
e62929b3
ML
13074 if (!crtc->state->active) {
13075 /*
13076 * Make sure we don't call initial_watermarks
13077 * for ILK-style watermark updates.
ff32c54e
VS
13078 *
13079 * No clue what this is supposed to achieve.
e62929b3 13080 */
ff32c54e 13081 if (INTEL_GEN(dev_priv) >= 9)
e62929b3
ML
13082 dev_priv->display.initial_watermarks(intel_state,
13083 to_intel_crtc_state(crtc->state));
e62929b3 13084 }
a539205a 13085 }
b8cecdf5 13086 }
7758a113 13087
ea9d758d
DV
13088 /* Only after disabling all output pipelines that will be changed can we
13089 * update the the output configuration. */
4740b0f2 13090 intel_modeset_update_crtc_state(state);
f6e5b160 13091
565602d7 13092 if (intel_state->modeset) {
4740b0f2 13093 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
33c8df89 13094
b0587e4d 13095 intel_set_cdclk(dev_priv, &dev_priv->cdclk.actual);
f6d1973d 13096
656d1b89
L
13097 /*
13098 * SKL workaround: bspec recommends we disable the SAGV when we
13099 * have more then one pipe enabled
13100 */
56feca91 13101 if (!intel_can_enable_sagv(state))
16dcdc4e 13102 intel_disable_sagv(dev_priv);
656d1b89 13103
677100ce 13104 intel_modeset_verify_disabled(dev, state);
4740b0f2 13105 }
47fab737 13106
896e5bb0 13107 /* Complete the events for pipes that have now been disabled */
aa5e9b47
ML
13108 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
13109 bool modeset = needs_modeset(new_crtc_state);
80715b2f 13110
1f7528c4 13111 /* Complete events for now disable pipes here. */
aa5e9b47 13112 if (modeset && !new_crtc_state->active && new_crtc_state->event) {
1f7528c4 13113 spin_lock_irq(&dev->event_lock);
aa5e9b47 13114 drm_crtc_send_vblank_event(crtc, new_crtc_state->event);
1f7528c4
DV
13115 spin_unlock_irq(&dev->event_lock);
13116
aa5e9b47 13117 new_crtc_state->event = NULL;
1f7528c4 13118 }
177246a8
MR
13119 }
13120
896e5bb0
L
13121 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
13122 dev_priv->display.update_crtcs(state, &crtc_vblank_mask);
13123
94f05024
DV
13124 /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
13125 * already, but still need the state for the delayed optimization. To
13126 * fix this:
13127 * - wrap the optimization/post_plane_update stuff into a per-crtc work.
13128 * - schedule that vblank worker _before_ calling hw_done
13129 * - at the start of commit_tail, cancel it _synchrously
13130 * - switch over to the vblank wait helper in the core after that since
13131 * we don't need out special handling any more.
13132 */
5a21b665
DV
13133 if (!state->legacy_cursor_update)
13134 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
13135
13136 /*
13137 * Now that the vblank has passed, we can go ahead and program the
13138 * optimal watermarks on platforms that need two-step watermark
13139 * programming.
13140 *
13141 * TODO: Move this (and other cleanup) to an async worker eventually.
13142 */
aa5e9b47
ML
13143 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
13144 intel_cstate = to_intel_crtc_state(new_crtc_state);
5a21b665
DV
13145
13146 if (dev_priv->display.optimize_watermarks)
ccf010fb
ML
13147 dev_priv->display.optimize_watermarks(intel_state,
13148 intel_cstate);
5a21b665
DV
13149 }
13150
aa5e9b47 13151 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
5a21b665
DV
13152 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
13153
13154 if (put_domains[i])
13155 modeset_put_power_domains(dev_priv, put_domains[i]);
13156
aa5e9b47 13157 intel_modeset_verify_crtc(crtc, state, old_crtc_state, new_crtc_state);
5a21b665
DV
13158 }
13159
56feca91 13160 if (intel_state->modeset && intel_can_enable_sagv(state))
16dcdc4e 13161 intel_enable_sagv(dev_priv);
656d1b89 13162
94f05024
DV
13163 drm_atomic_helper_commit_hw_done(state);
13164
d5553c09
CW
13165 if (intel_state->modeset) {
13166 /* As one of the primary mmio accessors, KMS has a high
13167 * likelihood of triggering bugs in unclaimed access. After we
13168 * finish modesetting, see if an error has been flagged, and if
13169 * so enable debugging for the next modeset - and hope we catch
13170 * the culprit.
13171 */
13172 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
5a21b665 13173 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
d5553c09 13174 }
5a21b665
DV
13175
13176 mutex_lock(&dev->struct_mutex);
13177 drm_atomic_helper_cleanup_planes(dev, state);
13178 mutex_unlock(&dev->struct_mutex);
13179
ea0000f0
DV
13180 drm_atomic_helper_commit_cleanup_done(state);
13181
0853695c 13182 drm_atomic_state_put(state);
f30da187 13183
ba318c61 13184 intel_atomic_helper_free_state(dev_priv);
94f05024
DV
13185}
13186
13187static void intel_atomic_commit_work(struct work_struct *work)
13188{
c004a90b
CW
13189 struct drm_atomic_state *state =
13190 container_of(work, struct drm_atomic_state, commit_work);
13191
94f05024
DV
13192 intel_atomic_commit_tail(state);
13193}
13194
c004a90b
CW
13195static int __i915_sw_fence_call
13196intel_atomic_commit_ready(struct i915_sw_fence *fence,
13197 enum i915_sw_fence_notify notify)
13198{
13199 struct intel_atomic_state *state =
13200 container_of(fence, struct intel_atomic_state, commit_ready);
13201
13202 switch (notify) {
13203 case FENCE_COMPLETE:
13204 if (state->base.commit_work.func)
13205 queue_work(system_unbound_wq, &state->base.commit_work);
13206 break;
13207
13208 case FENCE_FREE:
eb955eee
CW
13209 {
13210 struct intel_atomic_helper *helper =
13211 &to_i915(state->base.dev)->atomic_helper;
13212
13213 if (llist_add(&state->freed, &helper->free_list))
13214 schedule_work(&helper->free_work);
13215 break;
13216 }
c004a90b
CW
13217 }
13218
13219 return NOTIFY_DONE;
13220}
13221
6c9c1b38
DV
13222static void intel_atomic_track_fbs(struct drm_atomic_state *state)
13223{
aa5e9b47 13224 struct drm_plane_state *old_plane_state, *new_plane_state;
6c9c1b38 13225 struct drm_plane *plane;
6c9c1b38
DV
13226 int i;
13227
aa5e9b47 13228 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i)
faf5bf0a 13229 i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
aa5e9b47 13230 intel_fb_obj(new_plane_state->fb),
faf5bf0a 13231 to_intel_plane(plane)->frontbuffer_bit);
6c9c1b38
DV
13232}
13233
94f05024
DV
13234/**
13235 * intel_atomic_commit - commit validated state object
13236 * @dev: DRM device
13237 * @state: the top-level driver state object
13238 * @nonblock: nonblocking commit
13239 *
13240 * This function commits a top-level state object that has been validated
13241 * with drm_atomic_helper_check().
13242 *
94f05024
DV
13243 * RETURNS
13244 * Zero for success or -errno.
13245 */
13246static int intel_atomic_commit(struct drm_device *dev,
13247 struct drm_atomic_state *state,
13248 bool nonblock)
13249{
13250 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 13251 struct drm_i915_private *dev_priv = to_i915(dev);
94f05024
DV
13252 int ret = 0;
13253
94f05024
DV
13254 ret = drm_atomic_helper_setup_commit(state, nonblock);
13255 if (ret)
13256 return ret;
13257
c004a90b
CW
13258 drm_atomic_state_get(state);
13259 i915_sw_fence_init(&intel_state->commit_ready,
13260 intel_atomic_commit_ready);
94f05024 13261
d07f0e59 13262 ret = intel_atomic_prepare_commit(dev, state);
94f05024
DV
13263 if (ret) {
13264 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
c004a90b 13265 i915_sw_fence_commit(&intel_state->commit_ready);
94f05024
DV
13266 return ret;
13267 }
13268
440df938
VS
13269 /*
13270 * The intel_legacy_cursor_update() fast path takes care
13271 * of avoiding the vblank waits for simple cursor
13272 * movement and flips. For cursor on/off and size changes,
13273 * we want to perform the vblank waits so that watermark
13274 * updates happen during the correct frames. Gen9+ have
13275 * double buffered watermarks and so shouldn't need this.
13276 *
13277 * Do this after drm_atomic_helper_setup_commit() and
13278 * intel_atomic_prepare_commit() because we still want
13279 * to skip the flip and fb cleanup waits. Although that
13280 * does risk yanking the mapping from under the display
13281 * engine.
13282 *
13283 * FIXME doing watermarks and fb cleanup from a vblank worker
13284 * (assuming we had any) would solve these problems.
13285 */
13286 if (INTEL_GEN(dev_priv) < 9)
13287 state->legacy_cursor_update = false;
13288
0806f4ee
ML
13289 ret = drm_atomic_helper_swap_state(state, true);
13290 if (ret) {
13291 i915_sw_fence_commit(&intel_state->commit_ready);
13292
13293 mutex_lock(&dev->struct_mutex);
13294 drm_atomic_helper_cleanup_planes(dev, state);
13295 mutex_unlock(&dev->struct_mutex);
13296 return ret;
13297 }
94f05024 13298 dev_priv->wm.distrust_bios_wm = false;
3c0fb588 13299 intel_shared_dpll_swap_state(state);
6c9c1b38 13300 intel_atomic_track_fbs(state);
94f05024 13301
c3b32658
ML
13302 if (intel_state->modeset) {
13303 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
13304 sizeof(intel_state->min_pixclk));
13305 dev_priv->active_crtcs = intel_state->active_crtcs;
bb0f4aab
VS
13306 dev_priv->cdclk.logical = intel_state->cdclk.logical;
13307 dev_priv->cdclk.actual = intel_state->cdclk.actual;
c3b32658
ML
13308 }
13309
0853695c 13310 drm_atomic_state_get(state);
c004a90b
CW
13311 INIT_WORK(&state->commit_work,
13312 nonblock ? intel_atomic_commit_work : NULL);
13313
13314 i915_sw_fence_commit(&intel_state->commit_ready);
13315 if (!nonblock) {
13316 i915_sw_fence_wait(&intel_state->commit_ready);
94f05024 13317 intel_atomic_commit_tail(state);
c004a90b 13318 }
75714940 13319
74c090b1 13320 return 0;
7f27126e
JB
13321}
13322
f6e5b160 13323static const struct drm_crtc_funcs intel_crtc_funcs = {
3fab2f09 13324 .gamma_set = drm_atomic_helper_legacy_gamma_set,
74c090b1 13325 .set_config = drm_atomic_helper_set_config,
f6e5b160 13326 .destroy = intel_crtc_destroy,
4c01ded5 13327 .page_flip = drm_atomic_helper_page_flip,
1356837e
MR
13328 .atomic_duplicate_state = intel_crtc_duplicate_state,
13329 .atomic_destroy_state = intel_crtc_destroy_state,
8c6b709d 13330 .set_crc_source = intel_crtc_set_crc_source,
f6e5b160
CW
13331};
13332
6beb8c23
MR
13333/**
13334 * intel_prepare_plane_fb - Prepare fb for usage on plane
13335 * @plane: drm plane to prepare for
13336 * @fb: framebuffer to prepare for presentation
13337 *
13338 * Prepares a framebuffer for usage on a display plane. Generally this
13339 * involves pinning the underlying object and updating the frontbuffer tracking
13340 * bits. Some older platforms need special physical address handling for
13341 * cursor planes.
13342 *
f935675f
ML
13343 * Must be called with struct_mutex held.
13344 *
6beb8c23
MR
13345 * Returns 0 on success, negative error code on failure.
13346 */
13347int
13348intel_prepare_plane_fb(struct drm_plane *plane,
1832040d 13349 struct drm_plane_state *new_state)
465c120c 13350{
c004a90b
CW
13351 struct intel_atomic_state *intel_state =
13352 to_intel_atomic_state(new_state->state);
b7f05d4a 13353 struct drm_i915_private *dev_priv = to_i915(plane->dev);
844f9111 13354 struct drm_framebuffer *fb = new_state->fb;
6beb8c23 13355 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
1ee49399 13356 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
c004a90b 13357 int ret;
465c120c 13358
57822dc6
CW
13359 if (obj) {
13360 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
13361 INTEL_INFO(dev_priv)->cursor_needs_physical) {
fabac484 13362 const int align = intel_cursor_alignment(dev_priv);
57822dc6
CW
13363
13364 ret = i915_gem_object_attach_phys(obj, align);
13365 if (ret) {
13366 DRM_DEBUG_KMS("failed to attach phys object\n");
13367 return ret;
13368 }
13369 } else {
13370 struct i915_vma *vma;
13371
13372 vma = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
13373 if (IS_ERR(vma)) {
13374 DRM_DEBUG_KMS("failed to pin object\n");
13375 return PTR_ERR(vma);
13376 }
13377
13378 to_intel_plane_state(new_state)->vma = vma;
13379 }
13380 }
13381
1ee49399 13382 if (!obj && !old_obj)
465c120c
MR
13383 return 0;
13384
5008e874
ML
13385 if (old_obj) {
13386 struct drm_crtc_state *crtc_state =
c004a90b
CW
13387 drm_atomic_get_existing_crtc_state(new_state->state,
13388 plane->state->crtc);
5008e874
ML
13389
13390 /* Big Hammer, we also need to ensure that any pending
13391 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
13392 * current scanout is retired before unpinning the old
13393 * framebuffer. Note that we rely on userspace rendering
13394 * into the buffer attached to the pipe they are waiting
13395 * on. If not, userspace generates a GPU hang with IPEHR
13396 * point to the MI_WAIT_FOR_EVENT.
13397 *
13398 * This should only fail upon a hung GPU, in which case we
13399 * can safely continue.
13400 */
c004a90b
CW
13401 if (needs_modeset(crtc_state)) {
13402 ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
13403 old_obj->resv, NULL,
13404 false, 0,
13405 GFP_KERNEL);
13406 if (ret < 0)
13407 return ret;
f4457ae7 13408 }
5008e874
ML
13409 }
13410
c004a90b
CW
13411 if (new_state->fence) { /* explicit fencing */
13412 ret = i915_sw_fence_await_dma_fence(&intel_state->commit_ready,
13413 new_state->fence,
13414 I915_FENCE_TIMEOUT,
13415 GFP_KERNEL);
13416 if (ret < 0)
13417 return ret;
13418 }
13419
c37efb99
CW
13420 if (!obj)
13421 return 0;
13422
c004a90b
CW
13423 if (!new_state->fence) { /* implicit fencing */
13424 ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
13425 obj->resv, NULL,
13426 false, I915_FENCE_TIMEOUT,
13427 GFP_KERNEL);
13428 if (ret < 0)
13429 return ret;
6b5e90f5
CW
13430
13431 i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
c004a90b 13432 }
5a21b665 13433
d07f0e59 13434 return 0;
6beb8c23
MR
13435}
13436
38f3ce3a
MR
13437/**
13438 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13439 * @plane: drm plane to clean up for
13440 * @fb: old framebuffer that was on plane
13441 *
13442 * Cleans up a framebuffer that has just been removed from a plane.
f935675f
ML
13443 *
13444 * Must be called with struct_mutex held.
38f3ce3a
MR
13445 */
13446void
13447intel_cleanup_plane_fb(struct drm_plane *plane,
1832040d 13448 struct drm_plane_state *old_state)
38f3ce3a 13449{
be1e3415 13450 struct i915_vma *vma;
38f3ce3a 13451
be1e3415
CW
13452 /* Should only be called after a successful intel_prepare_plane_fb()! */
13453 vma = fetch_and_zero(&to_intel_plane_state(old_state)->vma);
13454 if (vma)
13455 intel_unpin_fb_vma(vma);
465c120c
MR
13456}
13457
6156a456
CK
13458int
13459skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13460{
5b7280f0 13461 struct drm_i915_private *dev_priv;
6156a456 13462 int max_scale;
5b7280f0 13463 int crtc_clock, max_dotclk;
6156a456 13464
bf8a0af0 13465 if (!intel_crtc || !crtc_state->base.enable)
6156a456
CK
13466 return DRM_PLANE_HELPER_NO_SCALING;
13467
5b7280f0
ACO
13468 dev_priv = to_i915(intel_crtc->base.dev);
13469
6156a456 13470 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
5b7280f0
ACO
13471 max_dotclk = to_intel_atomic_state(crtc_state->base.state)->cdclk.logical.cdclk;
13472
13473 if (IS_GEMINILAKE(dev_priv))
13474 max_dotclk *= 2;
6156a456 13475
5b7280f0 13476 if (WARN_ON_ONCE(!crtc_clock || max_dotclk < crtc_clock))
6156a456
CK
13477 return DRM_PLANE_HELPER_NO_SCALING;
13478
13479 /*
13480 * skl max scale is lower of:
13481 * close to 3 but not 3, -1 is for that purpose
13482 * or
13483 * cdclk/crtc_clock
13484 */
5b7280f0
ACO
13485 max_scale = min((1 << 16) * 3 - 1,
13486 (1 << 8) * ((max_dotclk << 8) / crtc_clock));
6156a456
CK
13487
13488 return max_scale;
13489}
13490
465c120c 13491static int
282dbf9b 13492intel_check_primary_plane(struct intel_plane *plane,
061e4b8d 13493 struct intel_crtc_state *crtc_state,
3c692a41
GP
13494 struct intel_plane_state *state)
13495{
282dbf9b 13496 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
2b875c22 13497 struct drm_crtc *crtc = state->base.crtc;
6156a456 13498 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
061e4b8d
ML
13499 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13500 bool can_position = false;
b63a16f6 13501 int ret;
465c120c 13502
b63a16f6 13503 if (INTEL_GEN(dev_priv) >= 9) {
693bdc28
VS
13504 /* use scaler when colorkey is not required */
13505 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
13506 min_scale = 1;
13507 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
13508 }
d8106366 13509 can_position = true;
6156a456 13510 }
d8106366 13511
cc926387
DV
13512 ret = drm_plane_helper_check_state(&state->base,
13513 &state->clip,
13514 min_scale, max_scale,
13515 can_position, true);
b63a16f6
VS
13516 if (ret)
13517 return ret;
13518
cc926387 13519 if (!state->base.fb)
b63a16f6
VS
13520 return 0;
13521
13522 if (INTEL_GEN(dev_priv) >= 9) {
13523 ret = skl_check_plane_surface(state);
13524 if (ret)
13525 return ret;
a0864d59
VS
13526
13527 state->ctl = skl_plane_ctl(crtc_state, state);
13528 } else {
5b7fcc44
VS
13529 ret = i9xx_check_plane_surface(state);
13530 if (ret)
13531 return ret;
13532
a0864d59 13533 state->ctl = i9xx_plane_ctl(crtc_state, state);
b63a16f6
VS
13534 }
13535
13536 return 0;
14af293f
GP
13537}
13538
5a21b665
DV
13539static void intel_begin_crtc_commit(struct drm_crtc *crtc,
13540 struct drm_crtc_state *old_crtc_state)
13541{
13542 struct drm_device *dev = crtc->dev;
62e0fb88 13543 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665 13544 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b707aa50
L
13545 struct intel_crtc_state *intel_cstate =
13546 to_intel_crtc_state(crtc->state);
ccf010fb 13547 struct intel_crtc_state *old_intel_cstate =
5a21b665 13548 to_intel_crtc_state(old_crtc_state);
ccf010fb
ML
13549 struct intel_atomic_state *old_intel_state =
13550 to_intel_atomic_state(old_crtc_state->state);
5a21b665
DV
13551 bool modeset = needs_modeset(crtc->state);
13552
567f0792
ML
13553 if (!modeset &&
13554 (intel_cstate->base.color_mgmt_changed ||
13555 intel_cstate->update_pipe)) {
13556 intel_color_set_csc(crtc->state);
13557 intel_color_load_luts(crtc->state);
13558 }
13559
5a21b665
DV
13560 /* Perform vblank evasion around commit operation */
13561 intel_pipe_update_start(intel_crtc);
13562
13563 if (modeset)
e62929b3 13564 goto out;
5a21b665 13565
ccf010fb
ML
13566 if (intel_cstate->update_pipe)
13567 intel_update_pipe_config(intel_crtc, old_intel_cstate);
13568 else if (INTEL_GEN(dev_priv) >= 9)
5a21b665 13569 skl_detach_scalers(intel_crtc);
62e0fb88 13570
e62929b3 13571out:
ccf010fb
ML
13572 if (dev_priv->display.atomic_update_watermarks)
13573 dev_priv->display.atomic_update_watermarks(old_intel_state,
13574 intel_cstate);
5a21b665
DV
13575}
13576
13577static void intel_finish_crtc_commit(struct drm_crtc *crtc,
13578 struct drm_crtc_state *old_crtc_state)
13579{
13580 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13581
13582 intel_pipe_update_end(intel_crtc, NULL);
13583}
13584
cf4c7c12 13585/**
4a3b8769
MR
13586 * intel_plane_destroy - destroy a plane
13587 * @plane: plane to destroy
cf4c7c12 13588 *
4a3b8769
MR
13589 * Common destruction function for all types of planes (primary, cursor,
13590 * sprite).
cf4c7c12 13591 */
4a3b8769 13592void intel_plane_destroy(struct drm_plane *plane)
465c120c 13593{
465c120c 13594 drm_plane_cleanup(plane);
69ae561f 13595 kfree(to_intel_plane(plane));
465c120c
MR
13596}
13597
65a3fea0 13598const struct drm_plane_funcs intel_plane_funcs = {
70a101f8
MR
13599 .update_plane = drm_atomic_helper_update_plane,
13600 .disable_plane = drm_atomic_helper_disable_plane,
3d7d6510 13601 .destroy = intel_plane_destroy,
c196e1d6 13602 .set_property = drm_atomic_helper_plane_set_property,
a98b3431
MR
13603 .atomic_get_property = intel_plane_atomic_get_property,
13604 .atomic_set_property = intel_plane_atomic_set_property,
ea2c67bb
MR
13605 .atomic_duplicate_state = intel_plane_duplicate_state,
13606 .atomic_destroy_state = intel_plane_destroy_state,
465c120c
MR
13607};
13608
f79f2692
ML
13609static int
13610intel_legacy_cursor_update(struct drm_plane *plane,
13611 struct drm_crtc *crtc,
13612 struct drm_framebuffer *fb,
13613 int crtc_x, int crtc_y,
13614 unsigned int crtc_w, unsigned int crtc_h,
13615 uint32_t src_x, uint32_t src_y,
34a2ab5e
DV
13616 uint32_t src_w, uint32_t src_h,
13617 struct drm_modeset_acquire_ctx *ctx)
f79f2692
ML
13618{
13619 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
13620 int ret;
13621 struct drm_plane_state *old_plane_state, *new_plane_state;
13622 struct intel_plane *intel_plane = to_intel_plane(plane);
13623 struct drm_framebuffer *old_fb;
13624 struct drm_crtc_state *crtc_state = crtc->state;
be1e3415 13625 struct i915_vma *old_vma;
f79f2692
ML
13626
13627 /*
13628 * When crtc is inactive or there is a modeset pending,
13629 * wait for it to complete in the slowpath
13630 */
13631 if (!crtc_state->active || needs_modeset(crtc_state) ||
13632 to_intel_crtc_state(crtc_state)->update_pipe)
13633 goto slow;
13634
13635 old_plane_state = plane->state;
13636
13637 /*
13638 * If any parameters change that may affect watermarks,
13639 * take the slowpath. Only changing fb or position should be
13640 * in the fastpath.
13641 */
13642 if (old_plane_state->crtc != crtc ||
13643 old_plane_state->src_w != src_w ||
13644 old_plane_state->src_h != src_h ||
13645 old_plane_state->crtc_w != crtc_w ||
13646 old_plane_state->crtc_h != crtc_h ||
a5509abd 13647 !old_plane_state->fb != !fb)
f79f2692
ML
13648 goto slow;
13649
13650 new_plane_state = intel_plane_duplicate_state(plane);
13651 if (!new_plane_state)
13652 return -ENOMEM;
13653
13654 drm_atomic_set_fb_for_plane(new_plane_state, fb);
13655
13656 new_plane_state->src_x = src_x;
13657 new_plane_state->src_y = src_y;
13658 new_plane_state->src_w = src_w;
13659 new_plane_state->src_h = src_h;
13660 new_plane_state->crtc_x = crtc_x;
13661 new_plane_state->crtc_y = crtc_y;
13662 new_plane_state->crtc_w = crtc_w;
13663 new_plane_state->crtc_h = crtc_h;
13664
13665 ret = intel_plane_atomic_check_with_state(to_intel_crtc_state(crtc->state),
13666 to_intel_plane_state(new_plane_state));
13667 if (ret)
13668 goto out_free;
13669
f79f2692
ML
13670 ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
13671 if (ret)
13672 goto out_free;
13673
13674 if (INTEL_INFO(dev_priv)->cursor_needs_physical) {
fabac484 13675 int align = intel_cursor_alignment(dev_priv);
f79f2692
ML
13676
13677 ret = i915_gem_object_attach_phys(intel_fb_obj(fb), align);
13678 if (ret) {
13679 DRM_DEBUG_KMS("failed to attach phys object\n");
13680 goto out_unlock;
13681 }
13682 } else {
13683 struct i915_vma *vma;
13684
13685 vma = intel_pin_and_fence_fb_obj(fb, new_plane_state->rotation);
13686 if (IS_ERR(vma)) {
13687 DRM_DEBUG_KMS("failed to pin object\n");
13688
13689 ret = PTR_ERR(vma);
13690 goto out_unlock;
13691 }
be1e3415
CW
13692
13693 to_intel_plane_state(new_plane_state)->vma = vma;
f79f2692
ML
13694 }
13695
13696 old_fb = old_plane_state->fb;
be1e3415 13697 old_vma = to_intel_plane_state(old_plane_state)->vma;
f79f2692
ML
13698
13699 i915_gem_track_fb(intel_fb_obj(old_fb), intel_fb_obj(fb),
13700 intel_plane->frontbuffer_bit);
13701
13702 /* Swap plane state */
13703 new_plane_state->fence = old_plane_state->fence;
13704 *to_intel_plane_state(old_plane_state) = *to_intel_plane_state(new_plane_state);
13705 new_plane_state->fence = NULL;
13706 new_plane_state->fb = old_fb;
be1e3415 13707 to_intel_plane_state(new_plane_state)->vma = old_vma;
f79f2692 13708
72259536
VS
13709 if (plane->state->visible) {
13710 trace_intel_update_plane(plane, to_intel_crtc(crtc));
282dbf9b 13711 intel_plane->update_plane(intel_plane,
a5509abd
VS
13712 to_intel_crtc_state(crtc->state),
13713 to_intel_plane_state(plane->state));
72259536
VS
13714 } else {
13715 trace_intel_disable_plane(plane, to_intel_crtc(crtc));
282dbf9b 13716 intel_plane->disable_plane(intel_plane, to_intel_crtc(crtc));
72259536 13717 }
f79f2692
ML
13718
13719 intel_cleanup_plane_fb(plane, new_plane_state);
13720
13721out_unlock:
13722 mutex_unlock(&dev_priv->drm.struct_mutex);
13723out_free:
13724 intel_plane_destroy_state(plane, new_plane_state);
13725 return ret;
13726
f79f2692
ML
13727slow:
13728 return drm_atomic_helper_update_plane(plane, crtc, fb,
13729 crtc_x, crtc_y, crtc_w, crtc_h,
34a2ab5e 13730 src_x, src_y, src_w, src_h, ctx);
f79f2692
ML
13731}
13732
13733static const struct drm_plane_funcs intel_cursor_plane_funcs = {
13734 .update_plane = intel_legacy_cursor_update,
13735 .disable_plane = drm_atomic_helper_disable_plane,
13736 .destroy = intel_plane_destroy,
13737 .set_property = drm_atomic_helper_plane_set_property,
13738 .atomic_get_property = intel_plane_atomic_get_property,
13739 .atomic_set_property = intel_plane_atomic_set_property,
13740 .atomic_duplicate_state = intel_plane_duplicate_state,
13741 .atomic_destroy_state = intel_plane_destroy_state,
13742};
13743
b079bd17 13744static struct intel_plane *
580503c7 13745intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
465c120c 13746{
fca0ce2a
VS
13747 struct intel_plane *primary = NULL;
13748 struct intel_plane_state *state = NULL;
465c120c 13749 const uint32_t *intel_primary_formats;
93ca7e00 13750 unsigned int supported_rotations;
45e3743a 13751 unsigned int num_formats;
fca0ce2a 13752 int ret;
465c120c
MR
13753
13754 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
b079bd17
VS
13755 if (!primary) {
13756 ret = -ENOMEM;
fca0ce2a 13757 goto fail;
b079bd17 13758 }
465c120c 13759
8e7d688b 13760 state = intel_create_plane_state(&primary->base);
b079bd17
VS
13761 if (!state) {
13762 ret = -ENOMEM;
fca0ce2a 13763 goto fail;
b079bd17
VS
13764 }
13765
8e7d688b 13766 primary->base.state = &state->base;
ea2c67bb 13767
465c120c
MR
13768 primary->can_scale = false;
13769 primary->max_downscale = 1;
580503c7 13770 if (INTEL_GEN(dev_priv) >= 9) {
6156a456 13771 primary->can_scale = true;
af99ceda 13772 state->scaler_id = -1;
6156a456 13773 }
465c120c 13774 primary->pipe = pipe;
e3c566df
VS
13775 /*
13776 * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
13777 * port is hooked to pipe B. Hence we want plane A feeding pipe B.
13778 */
13779 if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4)
13780 primary->plane = (enum plane) !pipe;
13781 else
13782 primary->plane = (enum plane) pipe;
b14e5848 13783 primary->id = PLANE_PRIMARY;
a9ff8714 13784 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
c59cb179 13785 primary->check_plane = intel_check_primary_plane;
465c120c 13786
580503c7 13787 if (INTEL_GEN(dev_priv) >= 9) {
6c0fd451
DL
13788 intel_primary_formats = skl_primary_formats;
13789 num_formats = ARRAY_SIZE(skl_primary_formats);
a8d201af
ML
13790
13791 primary->update_plane = skylake_update_primary_plane;
13792 primary->disable_plane = skylake_disable_primary_plane;
580503c7 13793 } else if (INTEL_GEN(dev_priv) >= 4) {
568db4f2
DL
13794 intel_primary_formats = i965_primary_formats;
13795 num_formats = ARRAY_SIZE(i965_primary_formats);
a8d201af
ML
13796
13797 primary->update_plane = i9xx_update_primary_plane;
13798 primary->disable_plane = i9xx_disable_primary_plane;
6c0fd451
DL
13799 } else {
13800 intel_primary_formats = i8xx_primary_formats;
13801 num_formats = ARRAY_SIZE(i8xx_primary_formats);
a8d201af
ML
13802
13803 primary->update_plane = i9xx_update_primary_plane;
13804 primary->disable_plane = i9xx_disable_primary_plane;
465c120c
MR
13805 }
13806
580503c7
VS
13807 if (INTEL_GEN(dev_priv) >= 9)
13808 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
13809 0, &intel_plane_funcs,
38573dc1 13810 intel_primary_formats, num_formats,
e6fc3b68 13811 NULL,
38573dc1
VS
13812 DRM_PLANE_TYPE_PRIMARY,
13813 "plane 1%c", pipe_name(pipe));
9beb5fea 13814 else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
580503c7
VS
13815 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
13816 0, &intel_plane_funcs,
38573dc1 13817 intel_primary_formats, num_formats,
e6fc3b68 13818 NULL,
38573dc1
VS
13819 DRM_PLANE_TYPE_PRIMARY,
13820 "primary %c", pipe_name(pipe));
13821 else
580503c7
VS
13822 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
13823 0, &intel_plane_funcs,
38573dc1 13824 intel_primary_formats, num_formats,
e6fc3b68 13825 NULL,
38573dc1
VS
13826 DRM_PLANE_TYPE_PRIMARY,
13827 "plane %c", plane_name(primary->plane));
fca0ce2a
VS
13828 if (ret)
13829 goto fail;
48404c1e 13830
5481e27f 13831 if (INTEL_GEN(dev_priv) >= 9) {
93ca7e00 13832 supported_rotations =
c2c446ad
RF
13833 DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 |
13834 DRM_MODE_ROTATE_180 | DRM_MODE_ROTATE_270;
4ea7be2b
VS
13835 } else if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
13836 supported_rotations =
c2c446ad
RF
13837 DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180 |
13838 DRM_MODE_REFLECT_X;
5481e27f 13839 } else if (INTEL_GEN(dev_priv) >= 4) {
93ca7e00 13840 supported_rotations =
c2c446ad 13841 DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180;
93ca7e00 13842 } else {
c2c446ad 13843 supported_rotations = DRM_MODE_ROTATE_0;
93ca7e00
VS
13844 }
13845
5481e27f 13846 if (INTEL_GEN(dev_priv) >= 4)
93ca7e00 13847 drm_plane_create_rotation_property(&primary->base,
c2c446ad 13848 DRM_MODE_ROTATE_0,
93ca7e00 13849 supported_rotations);
48404c1e 13850
ea2c67bb
MR
13851 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13852
b079bd17 13853 return primary;
fca0ce2a
VS
13854
13855fail:
13856 kfree(state);
13857 kfree(primary);
13858
b079bd17 13859 return ERR_PTR(ret);
465c120c
MR
13860}
13861
b079bd17 13862static struct intel_plane *
b2d03b0d
VS
13863intel_cursor_plane_create(struct drm_i915_private *dev_priv,
13864 enum pipe pipe)
3d7d6510 13865{
fca0ce2a
VS
13866 struct intel_plane *cursor = NULL;
13867 struct intel_plane_state *state = NULL;
13868 int ret;
3d7d6510
MR
13869
13870 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
b079bd17
VS
13871 if (!cursor) {
13872 ret = -ENOMEM;
fca0ce2a 13873 goto fail;
b079bd17 13874 }
3d7d6510 13875
8e7d688b 13876 state = intel_create_plane_state(&cursor->base);
b079bd17
VS
13877 if (!state) {
13878 ret = -ENOMEM;
fca0ce2a 13879 goto fail;
b079bd17
VS
13880 }
13881
8e7d688b 13882 cursor->base.state = &state->base;
ea2c67bb 13883
3d7d6510
MR
13884 cursor->can_scale = false;
13885 cursor->max_downscale = 1;
13886 cursor->pipe = pipe;
13887 cursor->plane = pipe;
b14e5848 13888 cursor->id = PLANE_CURSOR;
a9ff8714 13889 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
b2d03b0d
VS
13890
13891 if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
13892 cursor->update_plane = i845_update_cursor;
13893 cursor->disable_plane = i845_disable_cursor;
659056f2 13894 cursor->check_plane = i845_check_cursor;
b2d03b0d
VS
13895 } else {
13896 cursor->update_plane = i9xx_update_cursor;
13897 cursor->disable_plane = i9xx_disable_cursor;
659056f2 13898 cursor->check_plane = i9xx_check_cursor;
b2d03b0d 13899 }
3d7d6510 13900
cd5dcbf1
VS
13901 cursor->cursor.base = ~0;
13902 cursor->cursor.cntl = ~0;
024faac7
VS
13903
13904 if (IS_I845G(dev_priv) || IS_I865G(dev_priv) || HAS_CUR_FBC(dev_priv))
13905 cursor->cursor.size = ~0;
3d7d6510 13906
580503c7 13907 ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
f79f2692 13908 0, &intel_cursor_plane_funcs,
fca0ce2a
VS
13909 intel_cursor_formats,
13910 ARRAY_SIZE(intel_cursor_formats),
e6fc3b68 13911 NULL, DRM_PLANE_TYPE_CURSOR,
38573dc1 13912 "cursor %c", pipe_name(pipe));
fca0ce2a
VS
13913 if (ret)
13914 goto fail;
4398ad45 13915
5481e27f 13916 if (INTEL_GEN(dev_priv) >= 4)
93ca7e00 13917 drm_plane_create_rotation_property(&cursor->base,
c2c446ad
RF
13918 DRM_MODE_ROTATE_0,
13919 DRM_MODE_ROTATE_0 |
13920 DRM_MODE_ROTATE_180);
4398ad45 13921
580503c7 13922 if (INTEL_GEN(dev_priv) >= 9)
af99ceda
CK
13923 state->scaler_id = -1;
13924
ea2c67bb
MR
13925 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
13926
b079bd17 13927 return cursor;
fca0ce2a
VS
13928
13929fail:
13930 kfree(state);
13931 kfree(cursor);
13932
b079bd17 13933 return ERR_PTR(ret);
3d7d6510
MR
13934}
13935
1c74eeaf
NM
13936static void intel_crtc_init_scalers(struct intel_crtc *crtc,
13937 struct intel_crtc_state *crtc_state)
549e2bfb 13938{
65edccce
VS
13939 struct intel_crtc_scaler_state *scaler_state =
13940 &crtc_state->scaler_state;
1c74eeaf 13941 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
549e2bfb 13942 int i;
549e2bfb 13943
1c74eeaf
NM
13944 crtc->num_scalers = dev_priv->info.num_scalers[crtc->pipe];
13945 if (!crtc->num_scalers)
13946 return;
13947
65edccce
VS
13948 for (i = 0; i < crtc->num_scalers; i++) {
13949 struct intel_scaler *scaler = &scaler_state->scalers[i];
13950
13951 scaler->in_use = 0;
13952 scaler->mode = PS_SCALER_MODE_DYN;
549e2bfb
CK
13953 }
13954
13955 scaler_state->scaler_id = -1;
13956}
13957
5ab0d85b 13958static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
79e53945
JB
13959{
13960 struct intel_crtc *intel_crtc;
f5de6e07 13961 struct intel_crtc_state *crtc_state = NULL;
b079bd17
VS
13962 struct intel_plane *primary = NULL;
13963 struct intel_plane *cursor = NULL;
a81d6fa0 13964 int sprite, ret;
79e53945 13965
955382f3 13966 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
b079bd17
VS
13967 if (!intel_crtc)
13968 return -ENOMEM;
79e53945 13969
f5de6e07 13970 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
b079bd17
VS
13971 if (!crtc_state) {
13972 ret = -ENOMEM;
f5de6e07 13973 goto fail;
b079bd17 13974 }
550acefd
ACO
13975 intel_crtc->config = crtc_state;
13976 intel_crtc->base.state = &crtc_state->base;
07878248 13977 crtc_state->base.crtc = &intel_crtc->base;
f5de6e07 13978
580503c7 13979 primary = intel_primary_plane_create(dev_priv, pipe);
b079bd17
VS
13980 if (IS_ERR(primary)) {
13981 ret = PTR_ERR(primary);
3d7d6510 13982 goto fail;
b079bd17 13983 }
d97d7b48 13984 intel_crtc->plane_ids_mask |= BIT(primary->id);
3d7d6510 13985
a81d6fa0 13986 for_each_sprite(dev_priv, pipe, sprite) {
b079bd17
VS
13987 struct intel_plane *plane;
13988
580503c7 13989 plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
d2b2cbce 13990 if (IS_ERR(plane)) {
b079bd17
VS
13991 ret = PTR_ERR(plane);
13992 goto fail;
13993 }
d97d7b48 13994 intel_crtc->plane_ids_mask |= BIT(plane->id);
a81d6fa0
VS
13995 }
13996
580503c7 13997 cursor = intel_cursor_plane_create(dev_priv, pipe);
d2b2cbce 13998 if (IS_ERR(cursor)) {
b079bd17 13999 ret = PTR_ERR(cursor);
3d7d6510 14000 goto fail;
b079bd17 14001 }
d97d7b48 14002 intel_crtc->plane_ids_mask |= BIT(cursor->id);
3d7d6510 14003
5ab0d85b 14004 ret = drm_crtc_init_with_planes(&dev_priv->drm, &intel_crtc->base,
b079bd17
VS
14005 &primary->base, &cursor->base,
14006 &intel_crtc_funcs,
4d5d72b7 14007 "pipe %c", pipe_name(pipe));
3d7d6510
MR
14008 if (ret)
14009 goto fail;
79e53945 14010
80824003 14011 intel_crtc->pipe = pipe;
e3c566df 14012 intel_crtc->plane = primary->plane;
80824003 14013
1c74eeaf
NM
14014 /* initialize shared scalers */
14015 intel_crtc_init_scalers(intel_crtc, crtc_state);
14016
22fd0fab
JB
14017 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14018 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
e2af48c6
VS
14019 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = intel_crtc;
14020 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = intel_crtc;
22fd0fab 14021
79e53945 14022 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
87b6b101 14023
8563b1e8
LL
14024 intel_color_init(&intel_crtc->base);
14025
87b6b101 14026 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
b079bd17
VS
14027
14028 return 0;
3d7d6510
MR
14029
14030fail:
b079bd17
VS
14031 /*
14032 * drm_mode_config_cleanup() will free up any
14033 * crtcs/planes already initialized.
14034 */
f5de6e07 14035 kfree(crtc_state);
3d7d6510 14036 kfree(intel_crtc);
b079bd17
VS
14037
14038 return ret;
79e53945
JB
14039}
14040
752aa88a
JB
14041enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14042{
6e9f798d 14043 struct drm_device *dev = connector->base.dev;
752aa88a 14044
51fd371b 14045 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
752aa88a 14046
51ec53da 14047 if (!connector->base.state->crtc)
752aa88a
JB
14048 return INVALID_PIPE;
14049
51ec53da 14050 return to_intel_crtc(connector->base.state->crtc)->pipe;
752aa88a
JB
14051}
14052
08d7b3d1 14053int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 14054 struct drm_file *file)
08d7b3d1 14055{
08d7b3d1 14056 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7707e653 14057 struct drm_crtc *drmmode_crtc;
c05422d5 14058 struct intel_crtc *crtc;
08d7b3d1 14059
7707e653 14060 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
71240ed2 14061 if (!drmmode_crtc)
3f2c2057 14062 return -ENOENT;
08d7b3d1 14063
7707e653 14064 crtc = to_intel_crtc(drmmode_crtc);
c05422d5 14065 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 14066
c05422d5 14067 return 0;
08d7b3d1
CW
14068}
14069
66a9278e 14070static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 14071{
66a9278e
DV
14072 struct drm_device *dev = encoder->base.dev;
14073 struct intel_encoder *source_encoder;
79e53945 14074 int index_mask = 0;
79e53945
JB
14075 int entry = 0;
14076
b2784e15 14077 for_each_intel_encoder(dev, source_encoder) {
bc079e8b 14078 if (encoders_cloneable(encoder, source_encoder))
66a9278e
DV
14079 index_mask |= (1 << entry);
14080
79e53945
JB
14081 entry++;
14082 }
4ef69c7a 14083
79e53945
JB
14084 return index_mask;
14085}
14086
646d5772 14087static bool has_edp_a(struct drm_i915_private *dev_priv)
4d302442 14088{
646d5772 14089 if (!IS_MOBILE(dev_priv))
4d302442
CW
14090 return false;
14091
14092 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14093 return false;
14094
5db94019 14095 if (IS_GEN5(dev_priv) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
14096 return false;
14097
14098 return true;
14099}
14100
6315b5d3 14101static bool intel_crt_present(struct drm_i915_private *dev_priv)
84b4e042 14102{
6315b5d3 14103 if (INTEL_GEN(dev_priv) >= 9)
884497ed
DL
14104 return false;
14105
50a0bc90 14106 if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
84b4e042
JB
14107 return false;
14108
920a14b2 14109 if (IS_CHERRYVIEW(dev_priv))
84b4e042
JB
14110 return false;
14111
4f8036a2
TU
14112 if (HAS_PCH_LPT_H(dev_priv) &&
14113 I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
65e472e4
VS
14114 return false;
14115
70ac54d0 14116 /* DDI E can't be used if DDI A requires 4 lanes */
4f8036a2 14117 if (HAS_DDI(dev_priv) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
70ac54d0
VS
14118 return false;
14119
e4abb733 14120 if (!dev_priv->vbt.int_crt_support)
84b4e042
JB
14121 return false;
14122
14123 return true;
14124}
14125
8090ba8c
ID
14126void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
14127{
14128 int pps_num;
14129 int pps_idx;
14130
14131 if (HAS_DDI(dev_priv))
14132 return;
14133 /*
14134 * This w/a is needed at least on CPT/PPT, but to be sure apply it
14135 * everywhere where registers can be write protected.
14136 */
14137 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
14138 pps_num = 2;
14139 else
14140 pps_num = 1;
14141
14142 for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
14143 u32 val = I915_READ(PP_CONTROL(pps_idx));
14144
14145 val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
14146 I915_WRITE(PP_CONTROL(pps_idx), val);
14147 }
14148}
14149
44cb734c
ID
14150static void intel_pps_init(struct drm_i915_private *dev_priv)
14151{
cc3f90f0 14152 if (HAS_PCH_SPLIT(dev_priv) || IS_GEN9_LP(dev_priv))
44cb734c
ID
14153 dev_priv->pps_mmio_base = PCH_PPS_BASE;
14154 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
14155 dev_priv->pps_mmio_base = VLV_PPS_BASE;
14156 else
14157 dev_priv->pps_mmio_base = PPS_BASE;
8090ba8c
ID
14158
14159 intel_pps_unlock_regs_wa(dev_priv);
44cb734c
ID
14160}
14161
c39055b0 14162static void intel_setup_outputs(struct drm_i915_private *dev_priv)
79e53945 14163{
4ef69c7a 14164 struct intel_encoder *encoder;
cb0953d7 14165 bool dpd_is_edp = false;
79e53945 14166
44cb734c
ID
14167 intel_pps_init(dev_priv);
14168
97a824e1
ID
14169 /*
14170 * intel_edp_init_connector() depends on this completing first, to
14171 * prevent the registeration of both eDP and LVDS and the incorrect
14172 * sharing of the PPS.
14173 */
c39055b0 14174 intel_lvds_init(dev_priv);
79e53945 14175
6315b5d3 14176 if (intel_crt_present(dev_priv))
c39055b0 14177 intel_crt_init(dev_priv);
cb0953d7 14178
cc3f90f0 14179 if (IS_GEN9_LP(dev_priv)) {
c776eb2e
VK
14180 /*
14181 * FIXME: Broxton doesn't support port detection via the
14182 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14183 * detect the ports.
14184 */
c39055b0
ACO
14185 intel_ddi_init(dev_priv, PORT_A);
14186 intel_ddi_init(dev_priv, PORT_B);
14187 intel_ddi_init(dev_priv, PORT_C);
c6c794a2 14188
c39055b0 14189 intel_dsi_init(dev_priv);
4f8036a2 14190 } else if (HAS_DDI(dev_priv)) {
0e72a5b5
ED
14191 int found;
14192
de31facd
JB
14193 /*
14194 * Haswell uses DDI functions to detect digital outputs.
14195 * On SKL pre-D0 the strap isn't connected, so we assume
14196 * it's there.
14197 */
77179400 14198 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
de31facd 14199 /* WaIgnoreDDIAStrap: skl */
b976dc53 14200 if (found || IS_GEN9_BC(dev_priv))
c39055b0 14201 intel_ddi_init(dev_priv, PORT_A);
0e72a5b5
ED
14202
14203 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14204 * register */
14205 found = I915_READ(SFUSE_STRAP);
14206
14207 if (found & SFUSE_STRAP_DDIB_DETECTED)
c39055b0 14208 intel_ddi_init(dev_priv, PORT_B);
0e72a5b5 14209 if (found & SFUSE_STRAP_DDIC_DETECTED)
c39055b0 14210 intel_ddi_init(dev_priv, PORT_C);
0e72a5b5 14211 if (found & SFUSE_STRAP_DDID_DETECTED)
c39055b0 14212 intel_ddi_init(dev_priv, PORT_D);
2800e4c2
RV
14213 /*
14214 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14215 */
b976dc53 14216 if (IS_GEN9_BC(dev_priv) &&
2800e4c2
RV
14217 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14218 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14219 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
c39055b0 14220 intel_ddi_init(dev_priv, PORT_E);
2800e4c2 14221
6e266956 14222 } else if (HAS_PCH_SPLIT(dev_priv)) {
cb0953d7 14223 int found;
dd11bc10 14224 dpd_is_edp = intel_dp_is_edp(dev_priv, PORT_D);
270b3042 14225
646d5772 14226 if (has_edp_a(dev_priv))
c39055b0 14227 intel_dp_init(dev_priv, DP_A, PORT_A);
cb0953d7 14228
dc0fa718 14229 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 14230 /* PCH SDVOB multiplex with HDMIB */
c39055b0 14231 found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
30ad48b7 14232 if (!found)
c39055b0 14233 intel_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
5eb08b69 14234 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
c39055b0 14235 intel_dp_init(dev_priv, PCH_DP_B, PORT_B);
30ad48b7
ZW
14236 }
14237
dc0fa718 14238 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
c39055b0 14239 intel_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
30ad48b7 14240
dc0fa718 14241 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
c39055b0 14242 intel_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
30ad48b7 14243
5eb08b69 14244 if (I915_READ(PCH_DP_C) & DP_DETECTED)
c39055b0 14245 intel_dp_init(dev_priv, PCH_DP_C, PORT_C);
5eb08b69 14246
270b3042 14247 if (I915_READ(PCH_DP_D) & DP_DETECTED)
c39055b0 14248 intel_dp_init(dev_priv, PCH_DP_D, PORT_D);
920a14b2 14249 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
22f35042 14250 bool has_edp, has_port;
457c52d8 14251
e17ac6db
VS
14252 /*
14253 * The DP_DETECTED bit is the latched state of the DDC
14254 * SDA pin at boot. However since eDP doesn't require DDC
14255 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14256 * eDP ports may have been muxed to an alternate function.
14257 * Thus we can't rely on the DP_DETECTED bit alone to detect
14258 * eDP ports. Consult the VBT as well as DP_DETECTED to
14259 * detect eDP ports.
22f35042
VS
14260 *
14261 * Sadly the straps seem to be missing sometimes even for HDMI
14262 * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
14263 * and VBT for the presence of the port. Additionally we can't
14264 * trust the port type the VBT declares as we've seen at least
14265 * HDMI ports that the VBT claim are DP or eDP.
e17ac6db 14266 */
dd11bc10 14267 has_edp = intel_dp_is_edp(dev_priv, PORT_B);
22f35042
VS
14268 has_port = intel_bios_is_port_present(dev_priv, PORT_B);
14269 if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
c39055b0 14270 has_edp &= intel_dp_init(dev_priv, VLV_DP_B, PORT_B);
22f35042 14271 if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
c39055b0 14272 intel_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
585a94b8 14273
dd11bc10 14274 has_edp = intel_dp_is_edp(dev_priv, PORT_C);
22f35042
VS
14275 has_port = intel_bios_is_port_present(dev_priv, PORT_C);
14276 if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
c39055b0 14277 has_edp &= intel_dp_init(dev_priv, VLV_DP_C, PORT_C);
22f35042 14278 if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
c39055b0 14279 intel_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
19c03924 14280
920a14b2 14281 if (IS_CHERRYVIEW(dev_priv)) {
22f35042
VS
14282 /*
14283 * eDP not supported on port D,
14284 * so no need to worry about it
14285 */
14286 has_port = intel_bios_is_port_present(dev_priv, PORT_D);
14287 if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
c39055b0 14288 intel_dp_init(dev_priv, CHV_DP_D, PORT_D);
22f35042 14289 if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
c39055b0 14290 intel_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
9418c1f1
VS
14291 }
14292
c39055b0 14293 intel_dsi_init(dev_priv);
5db94019 14294 } else if (!IS_GEN2(dev_priv) && !IS_PINEVIEW(dev_priv)) {
27185ae1 14295 bool found = false;
7d57382e 14296
e2debe91 14297 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14298 DRM_DEBUG_KMS("probing SDVOB\n");
c39055b0 14299 found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
9beb5fea 14300 if (!found && IS_G4X(dev_priv)) {
b01f2c3a 14301 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
c39055b0 14302 intel_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
b01f2c3a 14303 }
27185ae1 14304
9beb5fea 14305 if (!found && IS_G4X(dev_priv))
c39055b0 14306 intel_dp_init(dev_priv, DP_B, PORT_B);
725e30ad 14307 }
13520b05
KH
14308
14309 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 14310
e2debe91 14311 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14312 DRM_DEBUG_KMS("probing SDVOC\n");
c39055b0 14313 found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
b01f2c3a 14314 }
27185ae1 14315
e2debe91 14316 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 14317
9beb5fea 14318 if (IS_G4X(dev_priv)) {
b01f2c3a 14319 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
c39055b0 14320 intel_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
b01f2c3a 14321 }
9beb5fea 14322 if (IS_G4X(dev_priv))
c39055b0 14323 intel_dp_init(dev_priv, DP_C, PORT_C);
725e30ad 14324 }
27185ae1 14325
9beb5fea 14326 if (IS_G4X(dev_priv) && (I915_READ(DP_D) & DP_DETECTED))
c39055b0 14327 intel_dp_init(dev_priv, DP_D, PORT_D);
5db94019 14328 } else if (IS_GEN2(dev_priv))
c39055b0 14329 intel_dvo_init(dev_priv);
79e53945 14330
56b857a5 14331 if (SUPPORTS_TV(dev_priv))
c39055b0 14332 intel_tv_init(dev_priv);
79e53945 14333
c39055b0 14334 intel_psr_init(dev_priv);
7c8f8a70 14335
c39055b0 14336 for_each_intel_encoder(&dev_priv->drm, encoder) {
4ef69c7a
CW
14337 encoder->base.possible_crtcs = encoder->crtc_mask;
14338 encoder->base.possible_clones =
66a9278e 14339 intel_encoder_clones(encoder);
79e53945 14340 }
47356eb6 14341
c39055b0 14342 intel_init_pch_refclk(dev_priv);
270b3042 14343
c39055b0 14344 drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
79e53945
JB
14345}
14346
14347static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14348{
14349 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 14350
ef2d633e 14351 drm_framebuffer_cleanup(fb);
70001cd2 14352
dd689287
CW
14353 i915_gem_object_lock(intel_fb->obj);
14354 WARN_ON(!intel_fb->obj->framebuffer_references--);
14355 i915_gem_object_unlock(intel_fb->obj);
14356
f8c417cd 14357 i915_gem_object_put(intel_fb->obj);
70001cd2 14358
79e53945
JB
14359 kfree(intel_fb);
14360}
14361
14362static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 14363 struct drm_file *file,
79e53945
JB
14364 unsigned int *handle)
14365{
14366 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 14367 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 14368
cc917ab4
CW
14369 if (obj->userptr.mm) {
14370 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14371 return -EINVAL;
14372 }
14373
05394f39 14374 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
14375}
14376
86c98588
RV
14377static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14378 struct drm_file *file,
14379 unsigned flags, unsigned color,
14380 struct drm_clip_rect *clips,
14381 unsigned num_clips)
14382{
5a97bcc6 14383 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
86c98588 14384
5a97bcc6 14385 i915_gem_object_flush_if_display(obj);
d59b21ec 14386 intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
86c98588
RV
14387
14388 return 0;
14389}
14390
79e53945
JB
14391static const struct drm_framebuffer_funcs intel_fb_funcs = {
14392 .destroy = intel_user_framebuffer_destroy,
14393 .create_handle = intel_user_framebuffer_create_handle,
86c98588 14394 .dirty = intel_user_framebuffer_dirty,
79e53945
JB
14395};
14396
b321803d 14397static
920a14b2
TU
14398u32 intel_fb_pitch_limit(struct drm_i915_private *dev_priv,
14399 uint64_t fb_modifier, uint32_t pixel_format)
b321803d 14400{
24dbf51a 14401 u32 gen = INTEL_GEN(dev_priv);
b321803d
DL
14402
14403 if (gen >= 9) {
ac484963
VS
14404 int cpp = drm_format_plane_cpp(pixel_format, 0);
14405
b321803d
DL
14406 /* "The stride in bytes must not exceed the of the size of 8K
14407 * pixels and 32K bytes."
14408 */
ac484963 14409 return min(8192 * cpp, 32768);
6401c37d 14410 } else if (gen >= 5 && !HAS_GMCH_DISPLAY(dev_priv)) {
b321803d
DL
14411 return 32*1024;
14412 } else if (gen >= 4) {
14413 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14414 return 16*1024;
14415 else
14416 return 32*1024;
14417 } else if (gen >= 3) {
14418 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14419 return 8*1024;
14420 else
14421 return 16*1024;
14422 } else {
14423 /* XXX DSPC is limited to 4k tiled */
14424 return 8*1024;
14425 }
14426}
14427
24dbf51a
CW
14428static int intel_framebuffer_init(struct intel_framebuffer *intel_fb,
14429 struct drm_i915_gem_object *obj,
14430 struct drm_mode_fb_cmd2 *mode_cmd)
79e53945 14431{
24dbf51a 14432 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
b3c11ac2 14433 struct drm_format_name_buf format_name;
dd689287
CW
14434 u32 pitch_limit, stride_alignment;
14435 unsigned int tiling, stride;
24dbf51a 14436 int ret = -EINVAL;
79e53945 14437
dd689287
CW
14438 i915_gem_object_lock(obj);
14439 obj->framebuffer_references++;
14440 tiling = i915_gem_object_get_tiling(obj);
14441 stride = i915_gem_object_get_stride(obj);
14442 i915_gem_object_unlock(obj);
dd4916c5 14443
2a80eada 14444 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
c2ff7370
VS
14445 /*
14446 * If there's a fence, enforce that
14447 * the fb modifier and tiling mode match.
14448 */
14449 if (tiling != I915_TILING_NONE &&
14450 tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
144cc143 14451 DRM_DEBUG_KMS("tiling_mode doesn't match fb modifier\n");
24dbf51a 14452 goto err;
2a80eada
DV
14453 }
14454 } else {
c2ff7370 14455 if (tiling == I915_TILING_X) {
2a80eada 14456 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
c2ff7370 14457 } else if (tiling == I915_TILING_Y) {
144cc143 14458 DRM_DEBUG_KMS("No Y tiling for legacy addfb\n");
24dbf51a 14459 goto err;
2a80eada
DV
14460 }
14461 }
14462
9a8f0a12
TU
14463 /* Passed in modifier sanity checking. */
14464 switch (mode_cmd->modifier[0]) {
14465 case I915_FORMAT_MOD_Y_TILED:
14466 case I915_FORMAT_MOD_Yf_TILED:
6315b5d3 14467 if (INTEL_GEN(dev_priv) < 9) {
144cc143
VS
14468 DRM_DEBUG_KMS("Unsupported tiling 0x%llx!\n",
14469 mode_cmd->modifier[0]);
24dbf51a 14470 goto err;
9a8f0a12 14471 }
2f075565 14472 case DRM_FORMAT_MOD_LINEAR:
9a8f0a12
TU
14473 case I915_FORMAT_MOD_X_TILED:
14474 break;
14475 default:
144cc143
VS
14476 DRM_DEBUG_KMS("Unsupported fb modifier 0x%llx!\n",
14477 mode_cmd->modifier[0]);
24dbf51a 14478 goto err;
c16ed4be 14479 }
57cd6508 14480
c2ff7370
VS
14481 /*
14482 * gen2/3 display engine uses the fence if present,
14483 * so the tiling mode must match the fb modifier exactly.
14484 */
14485 if (INTEL_INFO(dev_priv)->gen < 4 &&
14486 tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
144cc143 14487 DRM_DEBUG_KMS("tiling_mode must match fb modifier exactly on gen2/3\n");
9aceb5c1 14488 goto err;
c2ff7370
VS
14489 }
14490
920a14b2 14491 pitch_limit = intel_fb_pitch_limit(dev_priv, mode_cmd->modifier[0],
b321803d 14492 mode_cmd->pixel_format);
a35cdaa0 14493 if (mode_cmd->pitches[0] > pitch_limit) {
144cc143 14494 DRM_DEBUG_KMS("%s pitch (%u) must be at most %d\n",
2f075565 14495 mode_cmd->modifier[0] != DRM_FORMAT_MOD_LINEAR ?
144cc143
VS
14496 "tiled" : "linear",
14497 mode_cmd->pitches[0], pitch_limit);
24dbf51a 14498 goto err;
c16ed4be 14499 }
5d7bd705 14500
c2ff7370
VS
14501 /*
14502 * If there's a fence, enforce that
14503 * the fb pitch and fence stride match.
14504 */
144cc143
VS
14505 if (tiling != I915_TILING_NONE && mode_cmd->pitches[0] != stride) {
14506 DRM_DEBUG_KMS("pitch (%d) must match tiling stride (%d)\n",
14507 mode_cmd->pitches[0], stride);
24dbf51a 14508 goto err;
c16ed4be 14509 }
5d7bd705 14510
57779d06 14511 /* Reject formats not supported by any plane early. */
308e5bcb 14512 switch (mode_cmd->pixel_format) {
57779d06 14513 case DRM_FORMAT_C8:
04b3924d
VS
14514 case DRM_FORMAT_RGB565:
14515 case DRM_FORMAT_XRGB8888:
14516 case DRM_FORMAT_ARGB8888:
57779d06
VS
14517 break;
14518 case DRM_FORMAT_XRGB1555:
6315b5d3 14519 if (INTEL_GEN(dev_priv) > 3) {
144cc143
VS
14520 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14521 drm_get_format_name(mode_cmd->pixel_format, &format_name));
9aceb5c1 14522 goto err;
c16ed4be 14523 }
57779d06 14524 break;
57779d06 14525 case DRM_FORMAT_ABGR8888:
920a14b2 14526 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
6315b5d3 14527 INTEL_GEN(dev_priv) < 9) {
144cc143
VS
14528 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14529 drm_get_format_name(mode_cmd->pixel_format, &format_name));
9aceb5c1 14530 goto err;
6c0fd451
DL
14531 }
14532 break;
14533 case DRM_FORMAT_XBGR8888:
04b3924d 14534 case DRM_FORMAT_XRGB2101010:
57779d06 14535 case DRM_FORMAT_XBGR2101010:
6315b5d3 14536 if (INTEL_GEN(dev_priv) < 4) {
144cc143
VS
14537 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14538 drm_get_format_name(mode_cmd->pixel_format, &format_name));
9aceb5c1 14539 goto err;
c16ed4be 14540 }
b5626747 14541 break;
7531208b 14542 case DRM_FORMAT_ABGR2101010:
920a14b2 14543 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
144cc143
VS
14544 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14545 drm_get_format_name(mode_cmd->pixel_format, &format_name));
9aceb5c1 14546 goto err;
7531208b
DL
14547 }
14548 break;
04b3924d
VS
14549 case DRM_FORMAT_YUYV:
14550 case DRM_FORMAT_UYVY:
14551 case DRM_FORMAT_YVYU:
14552 case DRM_FORMAT_VYUY:
ab33081a 14553 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
144cc143
VS
14554 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14555 drm_get_format_name(mode_cmd->pixel_format, &format_name));
9aceb5c1 14556 goto err;
c16ed4be 14557 }
57cd6508
CW
14558 break;
14559 default:
144cc143
VS
14560 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14561 drm_get_format_name(mode_cmd->pixel_format, &format_name));
9aceb5c1 14562 goto err;
57cd6508
CW
14563 }
14564
90f9a336
VS
14565 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14566 if (mode_cmd->offsets[0] != 0)
24dbf51a 14567 goto err;
90f9a336 14568
24dbf51a
CW
14569 drm_helper_mode_fill_fb_struct(&dev_priv->drm,
14570 &intel_fb->base, mode_cmd);
d88c4afd
VS
14571
14572 stride_alignment = intel_fb_stride_alignment(&intel_fb->base, 0);
14573 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
144cc143
VS
14574 DRM_DEBUG_KMS("pitch (%d) must be at least %u byte aligned\n",
14575 mode_cmd->pitches[0], stride_alignment);
d88c4afd
VS
14576 goto err;
14577 }
14578
c7d73f6a
DV
14579 intel_fb->obj = obj;
14580
6687c906
VS
14581 ret = intel_fill_fb_info(dev_priv, &intel_fb->base);
14582 if (ret)
9aceb5c1 14583 goto err;
2d7a215f 14584
24dbf51a
CW
14585 ret = drm_framebuffer_init(obj->base.dev,
14586 &intel_fb->base,
14587 &intel_fb_funcs);
79e53945
JB
14588 if (ret) {
14589 DRM_ERROR("framebuffer init failed %d\n", ret);
24dbf51a 14590 goto err;
79e53945
JB
14591 }
14592
79e53945 14593 return 0;
24dbf51a
CW
14594
14595err:
dd689287
CW
14596 i915_gem_object_lock(obj);
14597 obj->framebuffer_references--;
14598 i915_gem_object_unlock(obj);
24dbf51a 14599 return ret;
79e53945
JB
14600}
14601
79e53945
JB
14602static struct drm_framebuffer *
14603intel_user_framebuffer_create(struct drm_device *dev,
14604 struct drm_file *filp,
1eb83451 14605 const struct drm_mode_fb_cmd2 *user_mode_cmd)
79e53945 14606{
dcb1394e 14607 struct drm_framebuffer *fb;
05394f39 14608 struct drm_i915_gem_object *obj;
76dc3769 14609 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
79e53945 14610
03ac0642
CW
14611 obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
14612 if (!obj)
cce13ff7 14613 return ERR_PTR(-ENOENT);
79e53945 14614
24dbf51a 14615 fb = intel_framebuffer_create(obj, &mode_cmd);
dcb1394e 14616 if (IS_ERR(fb))
f0cd5182 14617 i915_gem_object_put(obj);
dcb1394e
LW
14618
14619 return fb;
79e53945
JB
14620}
14621
778e23a9
CW
14622static void intel_atomic_state_free(struct drm_atomic_state *state)
14623{
14624 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
14625
14626 drm_atomic_state_default_release(state);
14627
14628 i915_sw_fence_fini(&intel_state->commit_ready);
14629
14630 kfree(state);
14631}
14632
79e53945 14633static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 14634 .fb_create = intel_user_framebuffer_create,
0632fef6 14635 .output_poll_changed = intel_fbdev_output_poll_changed,
5ee67f1c
MR
14636 .atomic_check = intel_atomic_check,
14637 .atomic_commit = intel_atomic_commit,
de419ab6
ML
14638 .atomic_state_alloc = intel_atomic_state_alloc,
14639 .atomic_state_clear = intel_atomic_state_clear,
778e23a9 14640 .atomic_state_free = intel_atomic_state_free,
79e53945
JB
14641};
14642
88212941
ID
14643/**
14644 * intel_init_display_hooks - initialize the display modesetting hooks
14645 * @dev_priv: device private
14646 */
14647void intel_init_display_hooks(struct drm_i915_private *dev_priv)
e70236a8 14648{
7ff89ca2
VS
14649 intel_init_cdclk_hooks(dev_priv);
14650
88212941 14651 if (INTEL_INFO(dev_priv)->gen >= 9) {
bc8d7dff 14652 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14653 dev_priv->display.get_initial_plane_config =
14654 skylake_get_initial_plane_config;
bc8d7dff
DL
14655 dev_priv->display.crtc_compute_clock =
14656 haswell_crtc_compute_clock;
14657 dev_priv->display.crtc_enable = haswell_crtc_enable;
14658 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 14659 } else if (HAS_DDI(dev_priv)) {
0e8ffe1b 14660 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14661 dev_priv->display.get_initial_plane_config =
14662 ironlake_get_initial_plane_config;
797d0259
ACO
14663 dev_priv->display.crtc_compute_clock =
14664 haswell_crtc_compute_clock;
4f771f10
PZ
14665 dev_priv->display.crtc_enable = haswell_crtc_enable;
14666 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 14667 } else if (HAS_PCH_SPLIT(dev_priv)) {
0e8ffe1b 14668 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
5724dbd1
DL
14669 dev_priv->display.get_initial_plane_config =
14670 ironlake_get_initial_plane_config;
3fb37703
ACO
14671 dev_priv->display.crtc_compute_clock =
14672 ironlake_crtc_compute_clock;
76e5a89c
DV
14673 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14674 dev_priv->display.crtc_disable = ironlake_crtc_disable;
65b3d6a9 14675 } else if (IS_CHERRYVIEW(dev_priv)) {
89b667f8 14676 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14677 dev_priv->display.get_initial_plane_config =
14678 i9xx_get_initial_plane_config;
65b3d6a9
ACO
14679 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
14680 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14681 dev_priv->display.crtc_disable = i9xx_crtc_disable;
14682 } else if (IS_VALLEYVIEW(dev_priv)) {
14683 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14684 dev_priv->display.get_initial_plane_config =
14685 i9xx_get_initial_plane_config;
14686 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
89b667f8
JB
14687 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14688 dev_priv->display.crtc_disable = i9xx_crtc_disable;
19ec6693
ACO
14689 } else if (IS_G4X(dev_priv)) {
14690 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14691 dev_priv->display.get_initial_plane_config =
14692 i9xx_get_initial_plane_config;
14693 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
14694 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14695 dev_priv->display.crtc_disable = i9xx_crtc_disable;
70e8aa21
ACO
14696 } else if (IS_PINEVIEW(dev_priv)) {
14697 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14698 dev_priv->display.get_initial_plane_config =
14699 i9xx_get_initial_plane_config;
14700 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
14701 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14702 dev_priv->display.crtc_disable = i9xx_crtc_disable;
81c97f52 14703 } else if (!IS_GEN2(dev_priv)) {
0e8ffe1b 14704 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14705 dev_priv->display.get_initial_plane_config =
14706 i9xx_get_initial_plane_config;
d6dfee7a 14707 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
76e5a89c
DV
14708 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14709 dev_priv->display.crtc_disable = i9xx_crtc_disable;
81c97f52
ACO
14710 } else {
14711 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14712 dev_priv->display.get_initial_plane_config =
14713 i9xx_get_initial_plane_config;
14714 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
14715 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14716 dev_priv->display.crtc_disable = i9xx_crtc_disable;
f564048e 14717 }
e70236a8 14718
88212941 14719 if (IS_GEN5(dev_priv)) {
3bb11b53 14720 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
88212941 14721 } else if (IS_GEN6(dev_priv)) {
3bb11b53 14722 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
88212941 14723 } else if (IS_IVYBRIDGE(dev_priv)) {
3bb11b53
SJ
14724 /* FIXME: detect B0+ stepping and use auto training */
14725 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
88212941 14726 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
3bb11b53 14727 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
445e780b
VS
14728 }
14729
27082493
L
14730 if (dev_priv->info.gen >= 9)
14731 dev_priv->display.update_crtcs = skl_update_crtcs;
14732 else
14733 dev_priv->display.update_crtcs = intel_update_crtcs;
14734
5a21b665
DV
14735 switch (INTEL_INFO(dev_priv)->gen) {
14736 case 2:
14737 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14738 break;
14739
14740 case 3:
14741 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14742 break;
14743
14744 case 4:
14745 case 5:
14746 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14747 break;
14748
14749 case 6:
14750 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14751 break;
14752 case 7:
14753 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
14754 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14755 break;
14756 case 9:
14757 /* Drop through - unsupported since execlist only. */
14758 default:
14759 /* Default just returns -ENODEV to indicate unsupported */
14760 dev_priv->display.queue_flip = intel_default_queue_flip;
14761 }
e70236a8
JB
14762}
14763
435793df
KP
14764/*
14765 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14766 */
14767static void quirk_ssc_force_disable(struct drm_device *dev)
14768{
fac5e23e 14769 struct drm_i915_private *dev_priv = to_i915(dev);
435793df 14770 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 14771 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
14772}
14773
4dca20ef 14774/*
5a15ab5b
CE
14775 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14776 * brightness value
4dca20ef
CE
14777 */
14778static void quirk_invert_brightness(struct drm_device *dev)
14779{
fac5e23e 14780 struct drm_i915_private *dev_priv = to_i915(dev);
4dca20ef 14781 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 14782 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
14783}
14784
9c72cc6f
SD
14785/* Some VBT's incorrectly indicate no backlight is present */
14786static void quirk_backlight_present(struct drm_device *dev)
14787{
fac5e23e 14788 struct drm_i915_private *dev_priv = to_i915(dev);
9c72cc6f
SD
14789 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14790 DRM_INFO("applying backlight present quirk\n");
14791}
14792
c99a259b
MN
14793/* Toshiba Satellite P50-C-18C requires T12 delay to be min 800ms
14794 * which is 300 ms greater than eDP spec T12 min.
14795 */
14796static void quirk_increase_t12_delay(struct drm_device *dev)
14797{
14798 struct drm_i915_private *dev_priv = to_i915(dev);
14799
14800 dev_priv->quirks |= QUIRK_INCREASE_T12_DELAY;
14801 DRM_INFO("Applying T12 delay quirk\n");
14802}
14803
b690e96c
JB
14804struct intel_quirk {
14805 int device;
14806 int subsystem_vendor;
14807 int subsystem_device;
14808 void (*hook)(struct drm_device *dev);
14809};
14810
5f85f176
EE
14811/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14812struct intel_dmi_quirk {
14813 void (*hook)(struct drm_device *dev);
14814 const struct dmi_system_id (*dmi_id_list)[];
14815};
14816
14817static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14818{
14819 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14820 return 1;
14821}
14822
14823static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14824 {
14825 .dmi_id_list = &(const struct dmi_system_id[]) {
14826 {
14827 .callback = intel_dmi_reverse_brightness,
14828 .ident = "NCR Corporation",
14829 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14830 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14831 },
14832 },
14833 { } /* terminating entry */
14834 },
14835 .hook = quirk_invert_brightness,
14836 },
14837};
14838
c43b5634 14839static struct intel_quirk intel_quirks[] = {
435793df
KP
14840 /* Lenovo U160 cannot use SSC on LVDS */
14841 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
14842
14843 /* Sony Vaio Y cannot use SSC on LVDS */
14844 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 14845
be505f64
AH
14846 /* Acer Aspire 5734Z must invert backlight brightness */
14847 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14848
14849 /* Acer/eMachines G725 */
14850 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14851
14852 /* Acer/eMachines e725 */
14853 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
14854
14855 /* Acer/Packard Bell NCL20 */
14856 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
14857
14858 /* Acer Aspire 4736Z */
14859 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
14860
14861 /* Acer Aspire 5336 */
14862 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
2e93a1aa
SD
14863
14864 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
14865 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
d4967d8c 14866
dfb3d47b
SD
14867 /* Acer C720 Chromebook (Core i3 4005U) */
14868 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
14869
b2a9601c 14870 /* Apple Macbook 2,1 (Core 2 T7400) */
14871 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
14872
1b9448b0
JN
14873 /* Apple Macbook 4,1 */
14874 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
14875
d4967d8c
SD
14876 /* Toshiba CB35 Chromebook (Celeron 2955U) */
14877 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
724cb06f
SD
14878
14879 /* HP Chromebook 14 (Celeron 2955U) */
14880 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
cf6f0af9
JN
14881
14882 /* Dell Chromebook 11 */
14883 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
9be64eee
JN
14884
14885 /* Dell Chromebook 11 (2015 version) */
14886 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
c99a259b
MN
14887
14888 /* Toshiba Satellite P50-C-18C */
14889 { 0x191B, 0x1179, 0xF840, quirk_increase_t12_delay },
b690e96c
JB
14890};
14891
14892static void intel_init_quirks(struct drm_device *dev)
14893{
14894 struct pci_dev *d = dev->pdev;
14895 int i;
14896
14897 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
14898 struct intel_quirk *q = &intel_quirks[i];
14899
14900 if (d->device == q->device &&
14901 (d->subsystem_vendor == q->subsystem_vendor ||
14902 q->subsystem_vendor == PCI_ANY_ID) &&
14903 (d->subsystem_device == q->subsystem_device ||
14904 q->subsystem_device == PCI_ANY_ID))
14905 q->hook(dev);
14906 }
5f85f176
EE
14907 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
14908 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
14909 intel_dmi_quirks[i].hook(dev);
14910 }
b690e96c
JB
14911}
14912
9cce37f4 14913/* Disable the VGA plane that we never use */
29b74b7f 14914static void i915_disable_vga(struct drm_i915_private *dev_priv)
9cce37f4 14915{
52a05c30 14916 struct pci_dev *pdev = dev_priv->drm.pdev;
9cce37f4 14917 u8 sr1;
920a14b2 14918 i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
9cce37f4 14919
2b37c616 14920 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
52a05c30 14921 vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 14922 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
14923 sr1 = inb(VGA_SR_DATA);
14924 outb(sr1 | 1<<5, VGA_SR_DATA);
52a05c30 14925 vga_put(pdev, VGA_RSRC_LEGACY_IO);
9cce37f4
JB
14926 udelay(300);
14927
01f5a626 14928 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9cce37f4
JB
14929 POSTING_READ(vga_reg);
14930}
14931
f817586c
DV
14932void intel_modeset_init_hw(struct drm_device *dev)
14933{
fac5e23e 14934 struct drm_i915_private *dev_priv = to_i915(dev);
1a617b77 14935
4c75b940 14936 intel_update_cdclk(dev_priv);
bb0f4aab 14937 dev_priv->cdclk.logical = dev_priv->cdclk.actual = dev_priv->cdclk.hw;
1a617b77 14938
46f16e63 14939 intel_init_clock_gating(dev_priv);
f817586c
DV
14940}
14941
d93c0372
MR
14942/*
14943 * Calculate what we think the watermarks should be for the state we've read
14944 * out of the hardware and then immediately program those watermarks so that
14945 * we ensure the hardware settings match our internal state.
14946 *
14947 * We can calculate what we think WM's should be by creating a duplicate of the
14948 * current state (which was constructed during hardware readout) and running it
14949 * through the atomic check code to calculate new watermark values in the
14950 * state object.
14951 */
14952static void sanitize_watermarks(struct drm_device *dev)
14953{
14954 struct drm_i915_private *dev_priv = to_i915(dev);
14955 struct drm_atomic_state *state;
ccf010fb 14956 struct intel_atomic_state *intel_state;
d93c0372
MR
14957 struct drm_crtc *crtc;
14958 struct drm_crtc_state *cstate;
14959 struct drm_modeset_acquire_ctx ctx;
14960 int ret;
14961 int i;
14962
14963 /* Only supported on platforms that use atomic watermark design */
ed4a6a7c 14964 if (!dev_priv->display.optimize_watermarks)
d93c0372
MR
14965 return;
14966
14967 /*
14968 * We need to hold connection_mutex before calling duplicate_state so
14969 * that the connector loop is protected.
14970 */
14971 drm_modeset_acquire_init(&ctx, 0);
14972retry:
0cd1262d 14973 ret = drm_modeset_lock_all_ctx(dev, &ctx);
d93c0372
MR
14974 if (ret == -EDEADLK) {
14975 drm_modeset_backoff(&ctx);
14976 goto retry;
14977 } else if (WARN_ON(ret)) {
0cd1262d 14978 goto fail;
d93c0372
MR
14979 }
14980
14981 state = drm_atomic_helper_duplicate_state(dev, &ctx);
14982 if (WARN_ON(IS_ERR(state)))
0cd1262d 14983 goto fail;
d93c0372 14984
ccf010fb
ML
14985 intel_state = to_intel_atomic_state(state);
14986
ed4a6a7c
MR
14987 /*
14988 * Hardware readout is the only time we don't want to calculate
14989 * intermediate watermarks (since we don't trust the current
14990 * watermarks).
14991 */
602ae835
VS
14992 if (!HAS_GMCH_DISPLAY(dev_priv))
14993 intel_state->skip_intermediate_wm = true;
ed4a6a7c 14994
d93c0372
MR
14995 ret = intel_atomic_check(dev, state);
14996 if (ret) {
14997 /*
14998 * If we fail here, it means that the hardware appears to be
14999 * programmed in a way that shouldn't be possible, given our
15000 * understanding of watermark requirements. This might mean a
15001 * mistake in the hardware readout code or a mistake in the
15002 * watermark calculations for a given platform. Raise a WARN
15003 * so that this is noticeable.
15004 *
15005 * If this actually happens, we'll have to just leave the
15006 * BIOS-programmed watermarks untouched and hope for the best.
15007 */
15008 WARN(true, "Could not determine valid watermarks for inherited state\n");
b9a1b717 15009 goto put_state;
d93c0372
MR
15010 }
15011
15012 /* Write calculated watermark values back */
aa5e9b47 15013 for_each_new_crtc_in_state(state, crtc, cstate, i) {
d93c0372
MR
15014 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
15015
ed4a6a7c 15016 cs->wm.need_postvbl_update = true;
ccf010fb 15017 dev_priv->display.optimize_watermarks(intel_state, cs);
d93c0372
MR
15018 }
15019
b9a1b717 15020put_state:
0853695c 15021 drm_atomic_state_put(state);
0cd1262d 15022fail:
d93c0372
MR
15023 drm_modeset_drop_locks(&ctx);
15024 drm_modeset_acquire_fini(&ctx);
15025}
15026
b079bd17 15027int intel_modeset_init(struct drm_device *dev)
79e53945 15028{
72e96d64
JL
15029 struct drm_i915_private *dev_priv = to_i915(dev);
15030 struct i915_ggtt *ggtt = &dev_priv->ggtt;
8cc87b75 15031 enum pipe pipe;
46f297fb 15032 struct intel_crtc *crtc;
79e53945
JB
15033
15034 drm_mode_config_init(dev);
15035
15036 dev->mode_config.min_width = 0;
15037 dev->mode_config.min_height = 0;
15038
019d96cb
DA
15039 dev->mode_config.preferred_depth = 24;
15040 dev->mode_config.prefer_shadow = 1;
15041
25bab385
TU
15042 dev->mode_config.allow_fb_modifiers = true;
15043
e6ecefaa 15044 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 15045
400c19d9 15046 init_llist_head(&dev_priv->atomic_helper.free_list);
eb955eee 15047 INIT_WORK(&dev_priv->atomic_helper.free_work,
ba318c61 15048 intel_atomic_helper_free_state_worker);
eb955eee 15049
b690e96c
JB
15050 intel_init_quirks(dev);
15051
62d75df7 15052 intel_init_pm(dev_priv);
1fa61106 15053
b7f05d4a 15054 if (INTEL_INFO(dev_priv)->num_pipes == 0)
b079bd17 15055 return 0;
e3c74757 15056
69f92f67
LW
15057 /*
15058 * There may be no VBT; and if the BIOS enabled SSC we can
15059 * just keep using it to avoid unnecessary flicker. Whereas if the
15060 * BIOS isn't using it, don't assume it will work even if the VBT
15061 * indicates as much.
15062 */
6e266956 15063 if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
69f92f67
LW
15064 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15065 DREF_SSC1_ENABLE);
15066
15067 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
15068 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
15069 bios_lvds_use_ssc ? "en" : "dis",
15070 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
15071 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
15072 }
15073 }
15074
5db94019 15075 if (IS_GEN2(dev_priv)) {
a6c45cf0
CW
15076 dev->mode_config.max_width = 2048;
15077 dev->mode_config.max_height = 2048;
5db94019 15078 } else if (IS_GEN3(dev_priv)) {
5e4d6fa7
KP
15079 dev->mode_config.max_width = 4096;
15080 dev->mode_config.max_height = 4096;
79e53945 15081 } else {
a6c45cf0
CW
15082 dev->mode_config.max_width = 8192;
15083 dev->mode_config.max_height = 8192;
79e53945 15084 }
068be561 15085
2a307c2e
JN
15086 if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
15087 dev->mode_config.cursor_width = IS_I845G(dev_priv) ? 64 : 512;
dc41c154 15088 dev->mode_config.cursor_height = 1023;
5db94019 15089 } else if (IS_GEN2(dev_priv)) {
068be561
DL
15090 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15091 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15092 } else {
15093 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15094 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15095 }
15096
72e96d64 15097 dev->mode_config.fb_base = ggtt->mappable_base;
79e53945 15098
28c97730 15099 DRM_DEBUG_KMS("%d display pipe%s available.\n",
b7f05d4a
TU
15100 INTEL_INFO(dev_priv)->num_pipes,
15101 INTEL_INFO(dev_priv)->num_pipes > 1 ? "s" : "");
79e53945 15102
055e393f 15103 for_each_pipe(dev_priv, pipe) {
b079bd17
VS
15104 int ret;
15105
5ab0d85b 15106 ret = intel_crtc_init(dev_priv, pipe);
b079bd17
VS
15107 if (ret) {
15108 drm_mode_config_cleanup(dev);
15109 return ret;
15110 }
79e53945
JB
15111 }
15112
e72f9fbf 15113 intel_shared_dpll_init(dev);
ee7b9f93 15114
5be6e334
VS
15115 intel_update_czclk(dev_priv);
15116 intel_modeset_init_hw(dev);
15117
b2045352 15118 if (dev_priv->max_cdclk_freq == 0)
4c75b940 15119 intel_update_max_cdclk(dev_priv);
b2045352 15120
9cce37f4 15121 /* Just disable it once at startup */
29b74b7f 15122 i915_disable_vga(dev_priv);
c39055b0 15123 intel_setup_outputs(dev_priv);
11be49eb 15124
6e9f798d 15125 drm_modeset_lock_all(dev);
aecd36b8 15126 intel_modeset_setup_hw_state(dev, dev->mode_config.acquire_ctx);
6e9f798d 15127 drm_modeset_unlock_all(dev);
46f297fb 15128
d3fcc808 15129 for_each_intel_crtc(dev, crtc) {
eeebeac5
ML
15130 struct intel_initial_plane_config plane_config = {};
15131
46f297fb
JB
15132 if (!crtc->active)
15133 continue;
15134
46f297fb 15135 /*
46f297fb
JB
15136 * Note that reserving the BIOS fb up front prevents us
15137 * from stuffing other stolen allocations like the ring
15138 * on top. This prevents some ugliness at boot time, and
15139 * can even allow for smooth boot transitions if the BIOS
15140 * fb is large enough for the active pipe configuration.
15141 */
eeebeac5
ML
15142 dev_priv->display.get_initial_plane_config(crtc,
15143 &plane_config);
15144
15145 /*
15146 * If the fb is shared between multiple heads, we'll
15147 * just get the first one.
15148 */
15149 intel_find_initial_plane_obj(crtc, &plane_config);
46f297fb 15150 }
d93c0372
MR
15151
15152 /*
15153 * Make sure hardware watermarks really match the state we read out.
15154 * Note that we need to do this after reconstructing the BIOS fb's
15155 * since the watermark calculation done here will use pstate->fb.
15156 */
602ae835
VS
15157 if (!HAS_GMCH_DISPLAY(dev_priv))
15158 sanitize_watermarks(dev);
b079bd17
VS
15159
15160 return 0;
2c7111db
CW
15161}
15162
2ee0da16 15163void i830_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
7fad798e 15164{
2ee0da16
VS
15165 /* 640x480@60Hz, ~25175 kHz */
15166 struct dpll clock = {
15167 .m1 = 18,
15168 .m2 = 7,
15169 .p1 = 13,
15170 .p2 = 4,
15171 .n = 2,
15172 };
15173 u32 dpll, fp;
15174 int i;
7fad798e 15175
2ee0da16
VS
15176 WARN_ON(i9xx_calc_dpll_params(48000, &clock) != 25154);
15177
15178 DRM_DEBUG_KMS("enabling pipe %c due to force quirk (vco=%d dot=%d)\n",
15179 pipe_name(pipe), clock.vco, clock.dot);
15180
15181 fp = i9xx_dpll_compute_fp(&clock);
15182 dpll = (I915_READ(DPLL(pipe)) & DPLL_DVO_2X_MODE) |
15183 DPLL_VGA_MODE_DIS |
15184 ((clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT) |
15185 PLL_P2_DIVIDE_BY_4 |
15186 PLL_REF_INPUT_DREFCLK |
15187 DPLL_VCO_ENABLE;
15188
15189 I915_WRITE(FP0(pipe), fp);
15190 I915_WRITE(FP1(pipe), fp);
15191
15192 I915_WRITE(HTOTAL(pipe), (640 - 1) | ((800 - 1) << 16));
15193 I915_WRITE(HBLANK(pipe), (640 - 1) | ((800 - 1) << 16));
15194 I915_WRITE(HSYNC(pipe), (656 - 1) | ((752 - 1) << 16));
15195 I915_WRITE(VTOTAL(pipe), (480 - 1) | ((525 - 1) << 16));
15196 I915_WRITE(VBLANK(pipe), (480 - 1) | ((525 - 1) << 16));
15197 I915_WRITE(VSYNC(pipe), (490 - 1) | ((492 - 1) << 16));
15198 I915_WRITE(PIPESRC(pipe), ((640 - 1) << 16) | (480 - 1));
15199
15200 /*
15201 * Apparently we need to have VGA mode enabled prior to changing
15202 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
15203 * dividers, even though the register value does change.
15204 */
15205 I915_WRITE(DPLL(pipe), dpll & ~DPLL_VGA_MODE_DIS);
15206 I915_WRITE(DPLL(pipe), dpll);
15207
15208 /* Wait for the clocks to stabilize. */
15209 POSTING_READ(DPLL(pipe));
15210 udelay(150);
15211
15212 /* The pixel multiplier can only be updated once the
15213 * DPLL is enabled and the clocks are stable.
15214 *
15215 * So write it again.
15216 */
15217 I915_WRITE(DPLL(pipe), dpll);
15218
15219 /* We do this three times for luck */
15220 for (i = 0; i < 3 ; i++) {
15221 I915_WRITE(DPLL(pipe), dpll);
15222 POSTING_READ(DPLL(pipe));
15223 udelay(150); /* wait for warmup */
7fad798e
DV
15224 }
15225
2ee0da16
VS
15226 I915_WRITE(PIPECONF(pipe), PIPECONF_ENABLE | PIPECONF_PROGRESSIVE);
15227 POSTING_READ(PIPECONF(pipe));
15228}
15229
15230void i830_disable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
15231{
15232 DRM_DEBUG_KMS("disabling pipe %c due to force quirk\n",
15233 pipe_name(pipe));
7fad798e 15234
2ee0da16
VS
15235 assert_plane_disabled(dev_priv, PLANE_A);
15236 assert_plane_disabled(dev_priv, PLANE_B);
6c5ed5ae 15237
2ee0da16
VS
15238 I915_WRITE(PIPECONF(pipe), 0);
15239 POSTING_READ(PIPECONF(pipe));
7fad798e 15240
2ee0da16
VS
15241 if (wait_for(pipe_dsl_stopped(dev_priv, pipe), 100))
15242 DRM_ERROR("pipe %c off wait timed out\n", pipe_name(pipe));
6c5ed5ae 15243
2ee0da16
VS
15244 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
15245 POSTING_READ(DPLL(pipe));
7fad798e
DV
15246}
15247
fa555837
DV
15248static bool
15249intel_check_plane_mapping(struct intel_crtc *crtc)
15250{
b7f05d4a 15251 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
649636ef 15252 u32 val;
fa555837 15253
b7f05d4a 15254 if (INTEL_INFO(dev_priv)->num_pipes == 1)
fa555837
DV
15255 return true;
15256
649636ef 15257 val = I915_READ(DSPCNTR(!crtc->plane));
fa555837
DV
15258
15259 if ((val & DISPLAY_PLANE_ENABLE) &&
15260 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15261 return false;
15262
15263 return true;
15264}
15265
02e93c35
VS
15266static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15267{
15268 struct drm_device *dev = crtc->base.dev;
15269 struct intel_encoder *encoder;
15270
15271 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15272 return true;
15273
15274 return false;
15275}
15276
496b0fc3
ML
15277static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
15278{
15279 struct drm_device *dev = encoder->base.dev;
15280 struct intel_connector *connector;
15281
15282 for_each_connector_on_encoder(dev, &encoder->base, connector)
15283 return connector;
15284
15285 return NULL;
15286}
15287
a168f5b3
VS
15288static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
15289 enum transcoder pch_transcoder)
15290{
15291 return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
15292 (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == TRANSCODER_A);
15293}
15294
aecd36b8
VS
15295static void intel_sanitize_crtc(struct intel_crtc *crtc,
15296 struct drm_modeset_acquire_ctx *ctx)
24929352
DV
15297{
15298 struct drm_device *dev = crtc->base.dev;
fac5e23e 15299 struct drm_i915_private *dev_priv = to_i915(dev);
4d1de975 15300 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
24929352 15301
24929352 15302 /* Clear any frame start delays used for debugging left by the BIOS */
4d1de975
JN
15303 if (!transcoder_is_dsi(cpu_transcoder)) {
15304 i915_reg_t reg = PIPECONF(cpu_transcoder);
15305
15306 I915_WRITE(reg,
15307 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15308 }
24929352 15309
d3eaf884 15310 /* restore vblank interrupts to correct state */
9625604c 15311 drm_crtc_vblank_reset(&crtc->base);
d297e103 15312 if (crtc->active) {
f9cd7b88
VS
15313 struct intel_plane *plane;
15314
9625604c 15315 drm_crtc_vblank_on(&crtc->base);
f9cd7b88
VS
15316
15317 /* Disable everything but the primary plane */
15318 for_each_intel_plane_on_crtc(dev, crtc, plane) {
15319 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
15320 continue;
15321
72259536 15322 trace_intel_disable_plane(&plane->base, crtc);
282dbf9b 15323 plane->disable_plane(plane, crtc);
f9cd7b88 15324 }
9625604c 15325 }
d3eaf884 15326
24929352 15327 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
DV
15328 * disable the crtc (and hence change the state) if it is wrong. Note
15329 * that gen4+ has a fixed plane -> pipe mapping. */
6315b5d3 15330 if (INTEL_GEN(dev_priv) < 4 && !intel_check_plane_mapping(crtc)) {
24929352
DV
15331 bool plane;
15332
78108b7c
VS
15333 DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
15334 crtc->base.base.id, crtc->base.name);
24929352
DV
15335
15336 /* Pipe has the wrong plane attached and the plane is active.
15337 * Temporarily change the plane mapping and disable everything
15338 * ... */
15339 plane = crtc->plane;
1d4258db 15340 crtc->base.primary->state->visible = true;
24929352 15341 crtc->plane = !plane;
da1d0e26 15342 intel_crtc_disable_noatomic(&crtc->base, ctx);
24929352 15343 crtc->plane = plane;
24929352 15344 }
24929352
DV
15345
15346 /* Adjust the state of the output pipe according to whether we
15347 * have active connectors/encoders. */
842e0307 15348 if (crtc->active && !intel_crtc_has_encoders(crtc))
da1d0e26 15349 intel_crtc_disable_noatomic(&crtc->base, ctx);
24929352 15350
49cff963 15351 if (crtc->active || HAS_GMCH_DISPLAY(dev_priv)) {
4cc31489
DV
15352 /*
15353 * We start out with underrun reporting disabled to avoid races.
15354 * For correct bookkeeping mark this on active crtcs.
15355 *
c5ab3bc0
DV
15356 * Also on gmch platforms we dont have any hardware bits to
15357 * disable the underrun reporting. Which means we need to start
15358 * out with underrun reporting disabled also on inactive pipes,
15359 * since otherwise we'll complain about the garbage we read when
15360 * e.g. coming up after runtime pm.
15361 *
4cc31489
DV
15362 * No protection against concurrent access is required - at
15363 * worst a fifo underrun happens which also sets this to false.
15364 */
15365 crtc->cpu_fifo_underrun_disabled = true;
a168f5b3
VS
15366 /*
15367 * We track the PCH trancoder underrun reporting state
15368 * within the crtc. With crtc for pipe A housing the underrun
15369 * reporting state for PCH transcoder A, crtc for pipe B housing
15370 * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
15371 * and marking underrun reporting as disabled for the non-existing
15372 * PCH transcoders B and C would prevent enabling the south
15373 * error interrupt (see cpt_can_enable_serr_int()).
15374 */
15375 if (has_pch_trancoder(dev_priv, (enum transcoder)crtc->pipe))
15376 crtc->pch_fifo_underrun_disabled = true;
4cc31489 15377 }
24929352
DV
15378}
15379
15380static void intel_sanitize_encoder(struct intel_encoder *encoder)
15381{
15382 struct intel_connector *connector;
24929352
DV
15383
15384 /* We need to check both for a crtc link (meaning that the
15385 * encoder is active and trying to read from a pipe) and the
15386 * pipe itself being active. */
15387 bool has_active_crtc = encoder->base.crtc &&
15388 to_intel_crtc(encoder->base.crtc)->active;
15389
496b0fc3
ML
15390 connector = intel_encoder_find_connector(encoder);
15391 if (connector && !has_active_crtc) {
24929352
DV
15392 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15393 encoder->base.base.id,
8e329a03 15394 encoder->base.name);
24929352
DV
15395
15396 /* Connector is active, but has no active pipe. This is
15397 * fallout from our resume register restoring. Disable
15398 * the encoder manually again. */
15399 if (encoder->base.crtc) {
fd6bbda9
ML
15400 struct drm_crtc_state *crtc_state = encoder->base.crtc->state;
15401
24929352
DV
15402 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15403 encoder->base.base.id,
8e329a03 15404 encoder->base.name);
fd6bbda9 15405 encoder->disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
a62d1497 15406 if (encoder->post_disable)
fd6bbda9 15407 encoder->post_disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
24929352 15408 }
7f1950fb 15409 encoder->base.crtc = NULL;
24929352
DV
15410
15411 /* Inconsistent output/port/pipe state happens presumably due to
15412 * a bug in one of the get_hw_state functions. Or someplace else
15413 * in our code, like the register restore mess on resume. Clamp
15414 * things to off as a safer default. */
fd6bbda9
ML
15415
15416 connector->base.dpms = DRM_MODE_DPMS_OFF;
15417 connector->base.encoder = NULL;
24929352
DV
15418 }
15419 /* Enabled encoders without active connectors will be fixed in
15420 * the crtc fixup. */
15421}
15422
29b74b7f 15423void i915_redisable_vga_power_on(struct drm_i915_private *dev_priv)
0fde901f 15424{
920a14b2 15425 i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
0fde901f 15426
04098753
ID
15427 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15428 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
29b74b7f 15429 i915_disable_vga(dev_priv);
04098753
ID
15430 }
15431}
15432
29b74b7f 15433void i915_redisable_vga(struct drm_i915_private *dev_priv)
04098753 15434{
8dc8a27c
PZ
15435 /* This function can be called both from intel_modeset_setup_hw_state or
15436 * at a very early point in our resume sequence, where the power well
15437 * structures are not yet restored. Since this function is at a very
15438 * paranoid "someone might have enabled VGA while we were not looking"
15439 * level, just check if the power well is enabled instead of trying to
15440 * follow the "don't touch the power well if we don't need it" policy
15441 * the rest of the driver uses. */
6392f847 15442 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
15443 return;
15444
29b74b7f 15445 i915_redisable_vga_power_on(dev_priv);
6392f847
ID
15446
15447 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
0fde901f
KM
15448}
15449
f9cd7b88 15450static bool primary_get_hw_state(struct intel_plane *plane)
98ec7739 15451{
f9cd7b88 15452 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
98ec7739 15453
f9cd7b88 15454 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
d032ffa0
ML
15455}
15456
f9cd7b88
VS
15457/* FIXME read out full plane state for all planes */
15458static void readout_plane_state(struct intel_crtc *crtc)
d032ffa0 15459{
e9728bd8
VS
15460 struct intel_plane *primary = to_intel_plane(crtc->base.primary);
15461 bool visible;
d032ffa0 15462
e9728bd8 15463 visible = crtc->active && primary_get_hw_state(primary);
b26d3ea3 15464
e9728bd8
VS
15465 intel_set_plane_visible(to_intel_crtc_state(crtc->base.state),
15466 to_intel_plane_state(primary->base.state),
15467 visible);
98ec7739
VS
15468}
15469
30e984df 15470static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352 15471{
fac5e23e 15472 struct drm_i915_private *dev_priv = to_i915(dev);
24929352 15473 enum pipe pipe;
24929352
DV
15474 struct intel_crtc *crtc;
15475 struct intel_encoder *encoder;
15476 struct intel_connector *connector;
f9e905ca 15477 struct drm_connector_list_iter conn_iter;
5358901f 15478 int i;
24929352 15479
565602d7
ML
15480 dev_priv->active_crtcs = 0;
15481
d3fcc808 15482 for_each_intel_crtc(dev, crtc) {
a8cd6da0
VS
15483 struct intel_crtc_state *crtc_state =
15484 to_intel_crtc_state(crtc->base.state);
3b117c8f 15485
ec2dc6a0 15486 __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
565602d7
ML
15487 memset(crtc_state, 0, sizeof(*crtc_state));
15488 crtc_state->base.crtc = &crtc->base;
24929352 15489
565602d7
ML
15490 crtc_state->base.active = crtc_state->base.enable =
15491 dev_priv->display.get_pipe_config(crtc, crtc_state);
15492
15493 crtc->base.enabled = crtc_state->base.enable;
15494 crtc->active = crtc_state->base.active;
15495
aca1ebf4 15496 if (crtc_state->base.active)
565602d7
ML
15497 dev_priv->active_crtcs |= 1 << crtc->pipe;
15498
f9cd7b88 15499 readout_plane_state(crtc);
24929352 15500
78108b7c
VS
15501 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
15502 crtc->base.base.id, crtc->base.name,
a8cd6da0 15503 enableddisabled(crtc_state->base.active));
24929352
DV
15504 }
15505
5358901f
DV
15506 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15507 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15508
2edd6443 15509 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
2c42e535
ACO
15510 &pll->state.hw_state);
15511 pll->state.crtc_mask = 0;
d3fcc808 15512 for_each_intel_crtc(dev, crtc) {
a8cd6da0
VS
15513 struct intel_crtc_state *crtc_state =
15514 to_intel_crtc_state(crtc->base.state);
15515
15516 if (crtc_state->base.active &&
15517 crtc_state->shared_dpll == pll)
2c42e535 15518 pll->state.crtc_mask |= 1 << crtc->pipe;
5358901f 15519 }
2c42e535 15520 pll->active_mask = pll->state.crtc_mask;
5358901f 15521
1e6f2ddc 15522 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
2c42e535 15523 pll->name, pll->state.crtc_mask, pll->on);
5358901f
DV
15524 }
15525
b2784e15 15526 for_each_intel_encoder(dev, encoder) {
24929352
DV
15527 pipe = 0;
15528
15529 if (encoder->get_hw_state(encoder, &pipe)) {
a8cd6da0
VS
15530 struct intel_crtc_state *crtc_state;
15531
98187836 15532 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
a8cd6da0 15533 crtc_state = to_intel_crtc_state(crtc->base.state);
e2af48c6 15534
045ac3b5 15535 encoder->base.crtc = &crtc->base;
a8cd6da0
VS
15536 crtc_state->output_types |= 1 << encoder->type;
15537 encoder->get_config(encoder, crtc_state);
24929352
DV
15538 } else {
15539 encoder->base.crtc = NULL;
15540 }
15541
6f2bcceb 15542 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
08c4d7fc
TU
15543 encoder->base.base.id, encoder->base.name,
15544 enableddisabled(encoder->base.crtc),
6f2bcceb 15545 pipe_name(pipe));
24929352
DV
15546 }
15547
f9e905ca
DV
15548 drm_connector_list_iter_begin(dev, &conn_iter);
15549 for_each_intel_connector_iter(connector, &conn_iter) {
24929352
DV
15550 if (connector->get_hw_state(connector)) {
15551 connector->base.dpms = DRM_MODE_DPMS_ON;
2aa974c9
ML
15552
15553 encoder = connector->encoder;
15554 connector->base.encoder = &encoder->base;
15555
15556 if (encoder->base.crtc &&
15557 encoder->base.crtc->state->active) {
15558 /*
15559 * This has to be done during hardware readout
15560 * because anything calling .crtc_disable may
15561 * rely on the connector_mask being accurate.
15562 */
15563 encoder->base.crtc->state->connector_mask |=
15564 1 << drm_connector_index(&connector->base);
e87a52b3
ML
15565 encoder->base.crtc->state->encoder_mask |=
15566 1 << drm_encoder_index(&encoder->base);
2aa974c9
ML
15567 }
15568
24929352
DV
15569 } else {
15570 connector->base.dpms = DRM_MODE_DPMS_OFF;
15571 connector->base.encoder = NULL;
15572 }
15573 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
08c4d7fc
TU
15574 connector->base.base.id, connector->base.name,
15575 enableddisabled(connector->base.encoder));
24929352 15576 }
f9e905ca 15577 drm_connector_list_iter_end(&conn_iter);
7f4c6284
VS
15578
15579 for_each_intel_crtc(dev, crtc) {
a8cd6da0
VS
15580 struct intel_crtc_state *crtc_state =
15581 to_intel_crtc_state(crtc->base.state);
aca1ebf4
VS
15582 int pixclk = 0;
15583
7f4c6284 15584 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
a8cd6da0
VS
15585 if (crtc_state->base.active) {
15586 intel_mode_from_pipe_config(&crtc->base.mode, crtc_state);
15587 intel_mode_from_pipe_config(&crtc_state->base.adjusted_mode, crtc_state);
7f4c6284
VS
15588 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
15589
15590 /*
15591 * The initial mode needs to be set in order to keep
15592 * the atomic core happy. It wants a valid mode if the
15593 * crtc's enabled, so we do the above call.
15594 *
7800fb69
DV
15595 * But we don't set all the derived state fully, hence
15596 * set a flag to indicate that a full recalculation is
15597 * needed on the next commit.
7f4c6284 15598 */
a8cd6da0 15599 crtc_state->base.mode.private_flags = I915_MODE_FLAG_INHERITED;
9eca6832 15600
a7d1b3f4
VS
15601 intel_crtc_compute_pixel_rate(crtc_state);
15602
15603 if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv) ||
15604 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
15605 pixclk = crtc_state->pixel_rate;
aca1ebf4
VS
15606 else
15607 WARN_ON(dev_priv->display.modeset_calc_cdclk);
15608
15609 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
a8cd6da0 15610 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
aca1ebf4
VS
15611 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
15612
5caa0fea
DV
15613 drm_calc_timestamping_constants(&crtc->base,
15614 &crtc_state->base.adjusted_mode);
9eca6832 15615 update_scanline_offset(crtc);
7f4c6284 15616 }
e3b247da 15617
aca1ebf4
VS
15618 dev_priv->min_pixclk[crtc->pipe] = pixclk;
15619
a8cd6da0 15620 intel_pipe_config_sanity_check(dev_priv, crtc_state);
7f4c6284 15621 }
30e984df
DV
15622}
15623
62b69566
ACO
15624static void
15625get_encoder_power_domains(struct drm_i915_private *dev_priv)
15626{
15627 struct intel_encoder *encoder;
15628
15629 for_each_intel_encoder(&dev_priv->drm, encoder) {
15630 u64 get_domains;
15631 enum intel_display_power_domain domain;
15632
15633 if (!encoder->get_power_domains)
15634 continue;
15635
15636 get_domains = encoder->get_power_domains(encoder);
15637 for_each_power_domain(domain, get_domains)
15638 intel_display_power_get(dev_priv, domain);
15639 }
15640}
15641
043e9bda
ML
15642/* Scan out the current hw modeset state,
15643 * and sanitizes it to the current state
15644 */
15645static void
aecd36b8
VS
15646intel_modeset_setup_hw_state(struct drm_device *dev,
15647 struct drm_modeset_acquire_ctx *ctx)
30e984df 15648{
fac5e23e 15649 struct drm_i915_private *dev_priv = to_i915(dev);
30e984df 15650 enum pipe pipe;
30e984df
DV
15651 struct intel_crtc *crtc;
15652 struct intel_encoder *encoder;
35c95375 15653 int i;
30e984df
DV
15654
15655 intel_modeset_readout_hw_state(dev);
24929352
DV
15656
15657 /* HW state is read out, now we need to sanitize this mess. */
62b69566
ACO
15658 get_encoder_power_domains(dev_priv);
15659
b2784e15 15660 for_each_intel_encoder(dev, encoder) {
24929352
DV
15661 intel_sanitize_encoder(encoder);
15662 }
15663
055e393f 15664 for_each_pipe(dev_priv, pipe) {
98187836 15665 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
e2af48c6 15666
aecd36b8 15667 intel_sanitize_crtc(crtc, ctx);
6e3c9717
ACO
15668 intel_dump_pipe_config(crtc, crtc->config,
15669 "[setup_hw_state]");
24929352 15670 }
9a935856 15671
d29b2f9d
ACO
15672 intel_modeset_update_connector_atomic_state(dev);
15673
35c95375
DV
15674 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15675 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15676
2dd66ebd 15677 if (!pll->on || pll->active_mask)
35c95375
DV
15678 continue;
15679
15680 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15681
2edd6443 15682 pll->funcs.disable(dev_priv, pll);
35c95375
DV
15683 pll->on = false;
15684 }
15685
04548cba
VS
15686 if (IS_G4X(dev_priv)) {
15687 g4x_wm_get_hw_state(dev);
15688 g4x_wm_sanitize(dev_priv);
15689 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
6eb1a681 15690 vlv_wm_get_hw_state(dev);
602ae835
VS
15691 vlv_wm_sanitize(dev_priv);
15692 } else if (IS_GEN9(dev_priv)) {
3078999f 15693 skl_wm_get_hw_state(dev);
602ae835 15694 } else if (HAS_PCH_SPLIT(dev_priv)) {
243e6a44 15695 ilk_wm_get_hw_state(dev);
602ae835 15696 }
292b990e
ML
15697
15698 for_each_intel_crtc(dev, crtc) {
d8fc70b7 15699 u64 put_domains;
292b990e 15700
74bff5f9 15701 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
292b990e
ML
15702 if (WARN_ON(put_domains))
15703 modeset_put_power_domains(dev_priv, put_domains);
15704 }
15705 intel_display_set_init_power(dev_priv, false);
010cf73d 15706
8d8c386c
ID
15707 intel_power_domains_verify_state(dev_priv);
15708
010cf73d 15709 intel_fbc_init_pipe_state(dev_priv);
043e9bda 15710}
7d0bc1ea 15711
043e9bda
ML
15712void intel_display_resume(struct drm_device *dev)
15713{
e2c8b870
ML
15714 struct drm_i915_private *dev_priv = to_i915(dev);
15715 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
15716 struct drm_modeset_acquire_ctx ctx;
043e9bda 15717 int ret;
f30da187 15718
e2c8b870 15719 dev_priv->modeset_restore_state = NULL;
73974893
ML
15720 if (state)
15721 state->acquire_ctx = &ctx;
043e9bda 15722
e2c8b870 15723 drm_modeset_acquire_init(&ctx, 0);
043e9bda 15724
73974893
ML
15725 while (1) {
15726 ret = drm_modeset_lock_all_ctx(dev, &ctx);
15727 if (ret != -EDEADLK)
15728 break;
043e9bda 15729
e2c8b870 15730 drm_modeset_backoff(&ctx);
e2c8b870 15731 }
043e9bda 15732
73974893 15733 if (!ret)
581e49fe 15734 ret = __intel_display_resume(dev, state, &ctx);
73974893 15735
e2c8b870
ML
15736 drm_modeset_drop_locks(&ctx);
15737 drm_modeset_acquire_fini(&ctx);
043e9bda 15738
0853695c 15739 if (ret)
e2c8b870 15740 DRM_ERROR("Restoring old state failed with %i\n", ret);
3c5e37f1
CW
15741 if (state)
15742 drm_atomic_state_put(state);
2c7111db
CW
15743}
15744
15745void intel_modeset_gem_init(struct drm_device *dev)
15746{
dc97997a 15747 struct drm_i915_private *dev_priv = to_i915(dev);
484b41dd 15748
dc97997a 15749 intel_init_gt_powersave(dev_priv);
ae48434c 15750
1ee8da6d 15751 intel_setup_overlay(dev_priv);
1ebaa0b9
CW
15752}
15753
15754int intel_connector_register(struct drm_connector *connector)
15755{
15756 struct intel_connector *intel_connector = to_intel_connector(connector);
15757 int ret;
15758
15759 ret = intel_backlight_device_register(intel_connector);
15760 if (ret)
15761 goto err;
15762
15763 return 0;
0962c3c9 15764
1ebaa0b9
CW
15765err:
15766 return ret;
79e53945
JB
15767}
15768
c191eca1 15769void intel_connector_unregister(struct drm_connector *connector)
4932e2c3 15770{
e63d87c0 15771 struct intel_connector *intel_connector = to_intel_connector(connector);
4932e2c3 15772
e63d87c0 15773 intel_backlight_device_unregister(intel_connector);
4932e2c3 15774 intel_panel_destroy_backlight(connector);
4932e2c3
ID
15775}
15776
79e53945
JB
15777void intel_modeset_cleanup(struct drm_device *dev)
15778{
fac5e23e 15779 struct drm_i915_private *dev_priv = to_i915(dev);
652c393a 15780
eb955eee
CW
15781 flush_work(&dev_priv->atomic_helper.free_work);
15782 WARN_ON(!llist_empty(&dev_priv->atomic_helper.free_list));
15783
dc97997a 15784 intel_disable_gt_powersave(dev_priv);
2eb5252e 15785
fd0c0642
DV
15786 /*
15787 * Interrupts and polling as the first thing to avoid creating havoc.
2eb5252e 15788 * Too much stuff here (turning of connectors, ...) would
fd0c0642
DV
15789 * experience fancy races otherwise.
15790 */
2aeb7d3a 15791 intel_irq_uninstall(dev_priv);
eb21b92b 15792
fd0c0642
DV
15793 /*
15794 * Due to the hpd irq storm handling the hotplug work can re-arm the
15795 * poll handlers. Hence disable polling after hpd handling is shut down.
15796 */
f87ea761 15797 drm_kms_helper_poll_fini(dev);
fd0c0642 15798
723bfd70
JB
15799 intel_unregister_dsm_handler();
15800
c937ab3e 15801 intel_fbc_global_disable(dev_priv);
69341a5e 15802
1630fe75
CW
15803 /* flush any delayed tasks or pending work */
15804 flush_scheduled_work();
15805
79e53945 15806 drm_mode_config_cleanup(dev);
4d7bb011 15807
1ee8da6d 15808 intel_cleanup_overlay(dev_priv);
ae48434c 15809
dc97997a 15810 intel_cleanup_gt_powersave(dev_priv);
f5949141 15811
40196446 15812 intel_teardown_gmbus(dev_priv);
79e53945
JB
15813}
15814
df0e9248
CW
15815void intel_connector_attach_encoder(struct intel_connector *connector,
15816 struct intel_encoder *encoder)
15817{
15818 connector->encoder = encoder;
15819 drm_mode_connector_attach_encoder(&connector->base,
15820 &encoder->base);
79e53945 15821}
28d52043
DA
15822
15823/*
15824 * set vga decode state - true == enable VGA decode
15825 */
6315b5d3 15826int intel_modeset_vga_set_state(struct drm_i915_private *dev_priv, bool state)
28d52043 15827{
6315b5d3 15828 unsigned reg = INTEL_GEN(dev_priv) >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
15829 u16 gmch_ctrl;
15830
75fa041d
CW
15831 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15832 DRM_ERROR("failed to read control word\n");
15833 return -EIO;
15834 }
15835
c0cc8a55
CW
15836 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15837 return 0;
15838
28d52043
DA
15839 if (state)
15840 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15841 else
15842 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
15843
15844 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15845 DRM_ERROR("failed to write control word\n");
15846 return -EIO;
15847 }
15848
28d52043
DA
15849 return 0;
15850}
c4a1d9e4 15851
98a2f411
CW
15852#if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
15853
c4a1d9e4 15854struct intel_display_error_state {
ff57f1b0
PZ
15855
15856 u32 power_well_driver;
15857
63b66e5b
CW
15858 int num_transcoders;
15859
c4a1d9e4
CW
15860 struct intel_cursor_error_state {
15861 u32 control;
15862 u32 position;
15863 u32 base;
15864 u32 size;
52331309 15865 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
15866
15867 struct intel_pipe_error_state {
ddf9c536 15868 bool power_domain_on;
c4a1d9e4 15869 u32 source;
f301b1e1 15870 u32 stat;
52331309 15871 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
15872
15873 struct intel_plane_error_state {
15874 u32 control;
15875 u32 stride;
15876 u32 size;
15877 u32 pos;
15878 u32 addr;
15879 u32 surface;
15880 u32 tile_offset;
52331309 15881 } plane[I915_MAX_PIPES];
63b66e5b
CW
15882
15883 struct intel_transcoder_error_state {
ddf9c536 15884 bool power_domain_on;
63b66e5b
CW
15885 enum transcoder cpu_transcoder;
15886
15887 u32 conf;
15888
15889 u32 htotal;
15890 u32 hblank;
15891 u32 hsync;
15892 u32 vtotal;
15893 u32 vblank;
15894 u32 vsync;
15895 } transcoder[4];
c4a1d9e4
CW
15896};
15897
15898struct intel_display_error_state *
c033666a 15899intel_display_capture_error_state(struct drm_i915_private *dev_priv)
c4a1d9e4 15900{
c4a1d9e4 15901 struct intel_display_error_state *error;
63b66e5b
CW
15902 int transcoders[] = {
15903 TRANSCODER_A,
15904 TRANSCODER_B,
15905 TRANSCODER_C,
15906 TRANSCODER_EDP,
15907 };
c4a1d9e4
CW
15908 int i;
15909
c033666a 15910 if (INTEL_INFO(dev_priv)->num_pipes == 0)
63b66e5b
CW
15911 return NULL;
15912
9d1cb914 15913 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
15914 if (error == NULL)
15915 return NULL;
15916
c033666a 15917 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
ff57f1b0
PZ
15918 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
15919
055e393f 15920 for_each_pipe(dev_priv, i) {
ddf9c536 15921 error->pipe[i].power_domain_on =
f458ebbc
DV
15922 __intel_display_power_is_enabled(dev_priv,
15923 POWER_DOMAIN_PIPE(i));
ddf9c536 15924 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
15925 continue;
15926
5efb3e28
VS
15927 error->cursor[i].control = I915_READ(CURCNTR(i));
15928 error->cursor[i].position = I915_READ(CURPOS(i));
15929 error->cursor[i].base = I915_READ(CURBASE(i));
c4a1d9e4
CW
15930
15931 error->plane[i].control = I915_READ(DSPCNTR(i));
15932 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
c033666a 15933 if (INTEL_GEN(dev_priv) <= 3) {
51889b35 15934 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
15935 error->plane[i].pos = I915_READ(DSPPOS(i));
15936 }
c033666a 15937 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
ca291363 15938 error->plane[i].addr = I915_READ(DSPADDR(i));
c033666a 15939 if (INTEL_GEN(dev_priv) >= 4) {
c4a1d9e4
CW
15940 error->plane[i].surface = I915_READ(DSPSURF(i));
15941 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15942 }
15943
c4a1d9e4 15944 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1 15945
c033666a 15946 if (HAS_GMCH_DISPLAY(dev_priv))
f301b1e1 15947 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
15948 }
15949
4d1de975 15950 /* Note: this does not include DSI transcoders. */
c033666a 15951 error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
2d1fe073 15952 if (HAS_DDI(dev_priv))
63b66e5b
CW
15953 error->num_transcoders++; /* Account for eDP. */
15954
15955 for (i = 0; i < error->num_transcoders; i++) {
15956 enum transcoder cpu_transcoder = transcoders[i];
15957
ddf9c536 15958 error->transcoder[i].power_domain_on =
f458ebbc 15959 __intel_display_power_is_enabled(dev_priv,
38cc1daf 15960 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 15961 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
15962 continue;
15963
63b66e5b
CW
15964 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15965
15966 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15967 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15968 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15969 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15970 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15971 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15972 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
15973 }
15974
15975 return error;
15976}
15977
edc3d884
MK
15978#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15979
c4a1d9e4 15980void
edc3d884 15981intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
15982 struct intel_display_error_state *error)
15983{
5a4c6f1b 15984 struct drm_i915_private *dev_priv = m->i915;
c4a1d9e4
CW
15985 int i;
15986
63b66e5b
CW
15987 if (!error)
15988 return;
15989
b7f05d4a 15990 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev_priv)->num_pipes);
8652744b 15991 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
edc3d884 15992 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 15993 error->power_well_driver);
055e393f 15994 for_each_pipe(dev_priv, i) {
edc3d884 15995 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536 15996 err_printf(m, " Power: %s\n",
87ad3212 15997 onoff(error->pipe[i].power_domain_on));
edc3d884 15998 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 15999 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
16000
16001 err_printf(m, "Plane [%d]:\n", i);
16002 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
16003 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
5f56d5f9 16004 if (INTEL_GEN(dev_priv) <= 3) {
edc3d884
MK
16005 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
16006 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 16007 }
772c2a51 16008 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
edc3d884 16009 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
5f56d5f9 16010 if (INTEL_GEN(dev_priv) >= 4) {
edc3d884
MK
16011 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
16012 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
16013 }
16014
edc3d884
MK
16015 err_printf(m, "Cursor [%d]:\n", i);
16016 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
16017 err_printf(m, " POS: %08x\n", error->cursor[i].position);
16018 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 16019 }
63b66e5b
CW
16020
16021 for (i = 0; i < error->num_transcoders; i++) {
da205630 16022 err_printf(m, "CPU transcoder: %s\n",
63b66e5b 16023 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536 16024 err_printf(m, " Power: %s\n",
87ad3212 16025 onoff(error->transcoder[i].power_domain_on));
63b66e5b
CW
16026 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
16027 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
16028 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
16029 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
16030 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
16031 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
16032 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
16033 }
c4a1d9e4 16034}
98a2f411
CW
16035
16036#endif